Skip to content

Commit c530772

Browse files
committed
Add support for Linux Network Devices
Implement support for passing Linux Network Devices to the container network namespace. The network device is passed during the creation of the container, before the process is started. It implements the logic defined in the OCI runtime specification. Change-Id: I190d5c444f03e65bbbfe5b4bc90809c0ad0a2017 Signed-off-by: Antonio Ojea <[email protected]>
1 parent 40548cc commit c530772

File tree

11 files changed

+508
-0
lines changed

11 files changed

+508
-0
lines changed

Diff for: features.go

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ var featuresCommand = cli.Command{
6363
Enabled: &t,
6464
},
6565
},
66+
NetDevices: &features.NetDevices{
67+
Enabled: &t,
68+
},
6669
},
6770
PotentiallyUnsafeConfigAnnotations: []string{
6871
"bundle",

Diff for: libcontainer/configs/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ type Config struct {
115115
// The device nodes that should be automatically created within the container upon container start. Note, make sure that the node is marked as allowed in the cgroup as well!
116116
Devices []*devices.Device `json:"devices"`
117117

118+
// NetDevices are key-value pairs, keyed by network device name, moved to the container's network namespace.
119+
NetDevices map[string]*LinuxNetDevice `json:"netDevices"`
120+
118121
MountLabel string `json:"mount_label"`
119122

120123
// Hostname optionally sets the container's hostname if provided

Diff for: libcontainer/configs/netdevices.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package configs
2+
3+
// LinuxNetDevice represents a single network device to be added to the container's network namespace
4+
type LinuxNetDevice struct {
5+
// Name of the device in the container namespace.
6+
Name string `json:"name,omitempty"`
7+
}

Diff for: libcontainer/configs/validate/validator.go

+43
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func Validate(config *configs.Config) error {
2424
cgroupsCheck,
2525
rootfs,
2626
network,
27+
netdevices,
2728
uts,
2829
security,
2930
namespaces,
@@ -70,6 +71,48 @@ func rootfs(config *configs.Config) error {
7071
return nil
7172
}
7273

74+
// https://elixir.bootlin.com/linux/v6.12/source/net/core/dev.c#L1066
75+
func devValidName(name string) bool {
76+
if len(name) == 0 || len(name) > unix.IFNAMSIZ {
77+
return false
78+
}
79+
if (name == ".") || (name == "..") {
80+
return false
81+
}
82+
if strings.Contains(name, "/") || strings.Contains(name, ":") || strings.Contains(name, " ") {
83+
return false
84+
}
85+
return true
86+
}
87+
88+
func netdevices(config *configs.Config) error {
89+
if len(config.NetDevices) == 0 {
90+
return nil
91+
}
92+
if !config.Namespaces.Contains(configs.NEWNET) {
93+
return errors.New("unable to move network devices without a private NET namespace")
94+
}
95+
path := config.Namespaces.PathOf(configs.NEWNET)
96+
if path == "" {
97+
return errors.New("unable to move network devices without a private NET namespace")
98+
}
99+
if config.RootlessEUID || config.RootlessCgroups {
100+
return errors.New("network devices are not supported for rootless containers")
101+
}
102+
103+
for name, netdev := range config.NetDevices {
104+
if !devValidName(name) {
105+
return fmt.Errorf("invalid network device name %q", name)
106+
}
107+
if netdev.Name != "" {
108+
if !devValidName(netdev.Name) {
109+
return fmt.Errorf("invalid network device name %q", netdev.Name)
110+
}
111+
}
112+
}
113+
return nil
114+
}
115+
73116
func network(config *configs.Config) error {
74117
if !config.Namespaces.Contains(configs.NEWNET) {
75118
if len(config.Networks) > 0 || len(config.Routes) > 0 {

Diff for: libcontainer/configs/validate/validator_test.go

+127
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,130 @@ func TestValidateIOPriority(t *testing.T) {
877877
}
878878
}
879879
}
880+
881+
func TestValidateNetDevices(t *testing.T) {
882+
testCases := []struct {
883+
name string
884+
isErr bool
885+
config *configs.Config
886+
}{
887+
{
888+
name: "network device",
889+
config: &configs.Config{
890+
Namespaces: configs.Namespaces(
891+
[]configs.Namespace{
892+
{
893+
Type: configs.NEWNET,
894+
Path: "/var/run/netns/blue",
895+
},
896+
},
897+
),
898+
NetDevices: map[string]*configs.LinuxNetDevice{
899+
"eth0": {},
900+
},
901+
},
902+
},
903+
{
904+
name: "network device rename",
905+
config: &configs.Config{
906+
Namespaces: configs.Namespaces(
907+
[]configs.Namespace{
908+
{
909+
Type: configs.NEWNET,
910+
Path: "/var/run/netns/blue",
911+
},
912+
},
913+
),
914+
NetDevices: map[string]*configs.LinuxNetDevice{
915+
"eth0": {
916+
Name: "c0",
917+
},
918+
},
919+
},
920+
},
921+
{
922+
name: "network device host network",
923+
isErr: true,
924+
config: &configs.Config{
925+
Namespaces: configs.Namespaces(
926+
[]configs.Namespace{},
927+
),
928+
NetDevices: map[string]*configs.LinuxNetDevice{
929+
"eth0": {},
930+
},
931+
},
932+
},
933+
{
934+
name: "network device rootless EUID",
935+
isErr: true,
936+
config: &configs.Config{
937+
Namespaces: configs.Namespaces(
938+
[]configs.Namespace{
939+
{
940+
Type: configs.NEWNET,
941+
Path: "/var/run/netns/blue",
942+
},
943+
},
944+
),
945+
RootlessEUID: true,
946+
NetDevices: map[string]*configs.LinuxNetDevice{
947+
"eth0": {},
948+
},
949+
},
950+
},
951+
{
952+
name: "network device rootless",
953+
isErr: true,
954+
config: &configs.Config{
955+
Namespaces: configs.Namespaces(
956+
[]configs.Namespace{
957+
{
958+
Type: configs.NEWNET,
959+
Path: "/var/run/netns/blue",
960+
},
961+
},
962+
),
963+
RootlessCgroups: true,
964+
NetDevices: map[string]*configs.LinuxNetDevice{
965+
"eth0": {},
966+
},
967+
},
968+
},
969+
{
970+
name: "network device bad name",
971+
isErr: true,
972+
config: &configs.Config{
973+
Namespaces: configs.Namespaces(
974+
[]configs.Namespace{
975+
{
976+
Type: configs.NEWNET,
977+
Path: "/var/run/netns/blue",
978+
},
979+
},
980+
),
981+
NetDevices: map[string]*configs.LinuxNetDevice{
982+
"eth0": {
983+
Name: "eth0/",
984+
},
985+
},
986+
},
987+
},
988+
}
989+
990+
for _, tc := range testCases {
991+
tc := tc
992+
t.Run(tc.name, func(t *testing.T) {
993+
config := tc.config
994+
config.Rootfs = "/var"
995+
996+
err := Validate(config)
997+
if tc.isErr && err == nil {
998+
t.Error("expected error, got nil")
999+
}
1000+
1001+
if !tc.isErr && err != nil {
1002+
t.Error(err)
1003+
}
1004+
})
1005+
}
1006+
}

Diff for: libcontainer/factory_linux.go

+12
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ func Create(root, id string, config *configs.Config) (*Container, error) {
9191
if err := os.Mkdir(stateDir, 0o711); err != nil {
9292
return nil, err
9393
}
94+
95+
// move the specified devices to the container network namespace
96+
nsPath := config.Namespaces.PathOf(configs.NEWNET)
97+
if nsPath != "" {
98+
for name, netDevice := range config.NetDevices {
99+
err := netnsAttach(name, nsPath, *netDevice)
100+
if err != nil {
101+
return nil, err
102+
}
103+
}
104+
}
105+
94106
c := &Container{
95107
id: id,
96108
stateDir: stateDir,

0 commit comments

Comments
 (0)