Skip to content

Commit 76df937

Browse files
committed
start firecracker VM in non-default netns, when specified
If the client specifies a network namespace name in the firecracker VM config, use that to start the firecracker process. Signed-off-by: Anirudh Aithal <[email protected]>
1 parent eecc586 commit 76df937

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/containerd/go-runc v0.0.0-20180907222934-5a6d9f37cfa3 // indirect
1414
github.com/containerd/ttrpc v0.0.0-20181001154009-f51df4475b76
1515
github.com/containerd/typeurl v0.0.0-20181015155603-461401dc8f19
16+
github.com/containernetworking/plugins v0.7.4
1617
github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142 // indirect
1718
github.com/docker/distribution v2.7.1+incompatible // indirect
1819
github.com/docker/docker v1.13.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ github.com/containerd/ttrpc v0.0.0-20181001154009-f51df4475b76 h1:vUPO9S35+FvukX
3030
github.com/containerd/ttrpc v0.0.0-20181001154009-f51df4475b76/go.mod h1:PvCDdDGpgqzQIzDW1TphrGLssLDZp2GuS+X5DkEJB8o=
3131
github.com/containerd/typeurl v0.0.0-20181015155603-461401dc8f19 h1:gzdItdct+4eLnZxiZi1YcIXx3uo5QWa/xXKnsldEqY8=
3232
github.com/containerd/typeurl v0.0.0-20181015155603-461401dc8f19/go.mod h1:Cm3kwCdlkCfMSHURc+r6fwoGH6/F1hH3S4sg0rLFWPc=
33+
github.com/containernetworking/plugins v0.7.4 h1:ugkuXfg1Pdzm54U5DGMzreYIkZPSCmSq4rm5TIXVICA=
34+
github.com/containernetworking/plugins v0.7.4/go.mod h1:dagHaAhNjXjT9QYOklkKJDGaQPTg4pf//FrUcJeb7FU=
3335
github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142 h1:3jFq2xL4ZajGK4aZY8jz+DAF0FHjI51BXjjSwCzS1Dk=
3436
github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
3537
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

runtime/service.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"os/exec"
2424
"path/filepath"
25+
pkgruntime "runtime"
2526
"syscall"
2627
"time"
2728
"unsafe"
@@ -37,6 +38,7 @@ import (
3738
"github.com/containerd/fifo"
3839
"github.com/containerd/ttrpc"
3940
"github.com/containerd/typeurl"
41+
"github.com/containernetworking/plugins/pkg/ns"
4042
firecracker "github.com/firecracker-microvm/firecracker-go-sdk"
4143
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
4244
ptypes "github.com/gogo/protobuf/types"
@@ -52,6 +54,7 @@ import (
5254
const (
5355
defaultVsockPort = 10789
5456
supportedMountFSType = "ext4"
57+
namedNetNSPath = "/var/run/netns/"
5558
)
5659

5760
// implements shimapi
@@ -683,7 +686,7 @@ func (s *service) startVM(ctx context.Context,
683686
s.machineCID = cid
684687

685688
log.G(ctx).Info("starting instance")
686-
if err := s.machine.Start(vmmCtx); err != nil {
689+
if err := s.netNSStartVM(vmmCtx, vmConfig); err != nil {
687690
return nil, err
688691
}
689692

@@ -702,6 +705,30 @@ func (s *service) startVM(ctx context.Context,
702705
return apiClient, nil
703706
}
704707

708+
// netNSStartVM starts the firecracker process with the network namespace
709+
// specified in the VM config. If the namespace is not specified, the process
710+
// is started in the default network namespace.
711+
func (s *service) netNSStartVM(vmmCtx context.Context, vmConfig *proto.FirecrackerConfig) error {
712+
netNSName := netNSFromProto(vmConfig)
713+
if netNSName == "" {
714+
return s.machine.Start(vmmCtx)
715+
}
716+
// Get the network namespace handle.
717+
netNS, err := ns.GetNS(namedNetNSPath + netNSName)
718+
if err != nil {
719+
return errors.Wrapf(err, "unable to find netns %s", netNSName)
720+
}
721+
// It's unsafe to switch network namespaces without locking down the OS
722+
// thread. Lock it for the start VM operation.
723+
pkgruntime.LockOSThread()
724+
defer pkgruntime.UnlockOSThread()
725+
726+
return netNS.Do(func(_ ns.NetNS) error {
727+
// Start the firecracker process in the target network namespace.
728+
return s.machine.Start(vmmCtx)
729+
})
730+
}
731+
705732
func (s *service) stopVM() error {
706733
return s.machine.StopVMM()
707734
}

runtime/taskfirecrackeropts.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ func overrideVMConfigFromTaskOpts(
6969
return cfg, drivesBuilder, nil
7070
}
7171

72+
// netNSFromProto returns the network namespace set, if any in the protobuf
73+
// message.
74+
func netNSFromProto(vmConfig *proto.FirecrackerConfig) string {
75+
if vmConfig != nil {
76+
return vmConfig.FirecrackerNetworkNamespace
77+
}
78+
79+
return ""
80+
}
81+
7282
// networkConfigFromProto creates a firecracker NetworkInterface object from
7383
// the protobuf FirecrackerNetworkInterface message.
7484
func networkConfigFromProto(nwIface *proto.FirecrackerNetworkInterface) firecracker.NetworkInterface {

0 commit comments

Comments
 (0)