Skip to content

Commit b68cddc

Browse files
committed
feat(network): let a monitor keep the network namespace of the container
urunc gives every guest its network through a tap device: it creates one in the network namespace of the container and redirects the traffic of the container's interface to it. That fits a monitor that emulates a network device, but not one whose guest reaches the network through sockets that the monitor itself opens in the namespace. For such a monitor the redirect would take the traffic away from those sockets. Add UsesHostNet to the VMM interface, false on every monitor so far. When a monitor reports it, both the libcontainer and the plain paths skip SetupNet, so no tap is created and the interface of the container is left as it is, and mark the network parameters with HostNet so the monitor knows to open the network to its guest. Such a monitor needs neither /dev/net/tun in its rootfs nor the CAP_NET_ADMIN that urunc adds to the monitor's capabilities for the tap, so leave both out. PR: #1135 Signed-off-by: danbugs <danilochiarlone@gmail.com> Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk> Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
1 parent a7ca0e0 commit b68cddc

19 files changed

Lines changed: 194 additions & 33 deletions

‎cmd/urunc/create_libcontainer.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ func buildMonitorArgs(id string, logFormat string) []string {
133133
// pivot: /proc is always mounted in the monitor rootfs, so this re-execs the
134134
// sealed urunc binary without urunc having to be copied into that rootfs.
135135
func monitorProcess(cmd *cli.Command, u *unikontainers.Unikontainer, readyFile *os.File) (*libcontainer.Process, error) {
136+
caps, err := u.MonitorCapabilities()
137+
if err != nil {
138+
return nil, err
139+
}
136140
process := &libcontainer.Process{
137141
Args: buildMonitorArgs(u.State.ID, cmd.String("log-format")),
138142
Env: os.Environ(),
@@ -141,7 +145,7 @@ func monitorProcess(cmd *cli.Command, u *unikontainers.Unikontainer, readyFile *
141145
Cwd: "/",
142146
// TODO set uid, gid and additional groups here
143147
Init: true,
144-
Capabilities: unikontainers.MonitorCapabilities(u.Spec),
148+
Capabilities: caps,
145149
LogLevel: strconv.Itoa(int(logrus.GetLevel())),
146150
// The ready pipe's write end. It must be the only/first ExtraFile so the
147151
// monitor inherits it at unikontainers.ReadyPipeFD:

‎pkg/network/network.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const (
3333

3434
var netlog = logrus.WithField("subsystem", "network")
3535

36+
// ErrNoContainerIface reports a network namespace with no usable interface,
37+
// as in a container started with no network.
38+
var ErrNoContainerIface = errors.New("no suitable network interface found in namespace")
39+
3640
type UnikernelNetworkInfo struct {
3741
TapDevice string
3842
EthDevice Interface
@@ -403,7 +407,18 @@ func discoverContainerIface() (netlink.Link, error) {
403407
}
404408
netlog.Debugf("skipping interface %s: no default route found", attrs.Name)
405409
}
406-
return nil, errors.New("no suitable network interface found in namespace")
410+
return nil, ErrNoContainerIface
411+
}
412+
413+
// ContainerIface describes the interface of the container in the current
414+
// network namespace, without touching it, for a monitor that gets no tap:
415+
// ErrNoContainerIface when there is none.
416+
func ContainerIface() (Interface, error) {
417+
link, err := discoverContainerIface()
418+
if err != nil {
419+
return Interface{}, err
420+
}
421+
return getInterfaceInfo(link.Attrs().Name)
407422
}
408423

409424
func deleteAllQDiscs(device netlink.Link) error {

‎pkg/unikontainers/hypervisors/cloud_hypervisor.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func (ch *CloudHypervisor) UsesKVM() bool {
4949
return true
5050
}
5151

52+
// UsesTUNTAP reports that the guest gets its network through a tap device.
53+
func (ch *CloudHypervisor) UsesTUNTAP() bool {
54+
return true
55+
}
56+
5257
// SupportsControlSocket reports that Cloud Hypervisor exposes a control socket
5358
// (its REST API socket).
5459
func (ch *CloudHypervisor) SupportsControlSocket() bool {

‎pkg/unikontainers/hypervisors/firecracker.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ func (fc *Firecracker) UsesKVM() bool {
9494
return true
9595
}
9696

97+
// UsesTUNTAP reports that the guest gets its network through a tap device.
98+
func (fc *Firecracker) UsesTUNTAP() bool {
99+
return true
100+
}
101+
97102
// SupportsSharedfs returns a bool value depending on the monitor support for shared-fs
98103
func (fc *Firecracker) SupportsSharedfs(_ string) bool {
99104
return false

‎pkg/unikontainers/hypervisors/hedge.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ func (h *Hedge) UsesKVM() bool {
4646
return true
4747
}
4848

49+
// UsesTUNTAP reports that the guest gets its network through a tap device.
50+
func (h *Hedge) UsesTUNTAP() bool {
51+
return true
52+
}
53+
4954
// SupportsSharedfs returns a bool value depending on the monitor support for shared-fs
5055
func (h *Hedge) SupportsSharedfs(_ string) bool {
5156
return false

‎pkg/unikontainers/hypervisors/hvt.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ func (h *HVT) UsesKVM() bool {
135135
return true
136136
}
137137

138+
// UsesTUNTAP reports that the guest gets its network through a tap device.
139+
func (h *HVT) UsesTUNTAP() bool {
140+
return true
141+
}
142+
138143
// SupportsSharedfs returns a bool value depending on the monitor support for shared-fs
139144
func (h *HVT) SupportsSharedfs(_ string) bool {
140145
return false

‎pkg/unikontainers/hypervisors/hyperlight.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ func (h *Hyperlight) SupportsControlSocket() bool {
5454
return false
5555
}
5656

57+
// UsesTUNTAP reports that hluk needs no tap: it serves the guest's sockets
58+
// itself, each guest socket call becoming a call on a real socket hluk opens,
59+
// so the guest shares the pod's network namespace, its address, routes and
60+
// NetworkPolicy, with no tap in between.
61+
func (h *Hyperlight) UsesTUNTAP() bool {
62+
return false
63+
}
64+
5765
// Path returns the path to the hluk binary.
5866
func (h *Hyperlight) Path() string {
5967
return h.binaryPath

‎pkg/unikontainers/hypervisors/hyperlight_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,11 @@ func TestHyperlightBuildExecCmd(t *testing.T) {
124124
})
125125
}
126126
}
127+
128+
func TestHyperlightUsesTUNTAP(t *testing.T) {
129+
t.Parallel()
130+
assert.False(t, (&Hyperlight{}).UsesTUNTAP(), "hyperlight-unikraft opens the guest's sockets itself, with no tap")
131+
for _, tap := range []types.VMM{&Qemu{}, &Firecracker{}, &CloudHypervisor{}, &HVT{}, &SPT{}, &Hedge{}} {
132+
assert.True(t, tap.UsesTUNTAP(), "%T gets a tap", tap)
133+
}
134+
}

‎pkg/unikontainers/hypervisors/qemu.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ func (q *Qemu) UsesKVM() bool {
5151
return true
5252
}
5353

54+
// UsesTUNTAP reports that the guest gets its network through a tap device.
55+
func (q *Qemu) UsesTUNTAP() bool {
56+
return true
57+
}
58+
5459
// SupportsSharedfs returns a bool value depending on the monitor support for shared-fs
5560
func (q *Qemu) SupportsSharedfs(_ string) bool {
5661
return true

‎pkg/unikontainers/hypervisors/spt.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ func (s *SPT) UsesKVM() bool {
4545
return false
4646
}
4747

48+
// UsesTUNTAP reports that the guest gets its network through a tap device.
49+
func (s *SPT) UsesTUNTAP() bool {
50+
return true
51+
}
52+
4853
// SupportsSharedfs returns a bool value depending on the monitor support for shared-fs
4954
func (s *SPT) SupportsSharedfs(_ string) bool {
5055
return false

0 commit comments

Comments
 (0)