Browse code

libnetwork/osl: remove Interface Interface

There's only one implementation; let's use that.
Also fixing a linting issue;

libnetwork/osl/interface_linux.go:91:2: S1001: should use copy(to, from) instead of a loop (gosimple)
for i, iface := range n.iFaces {
^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2023/08/03 06:24:33
Showing 6 changed files
... ...
@@ -14,12 +14,12 @@ import (
14 14
 	"github.com/vishvananda/netns"
15 15
 )
16 16
 
17
-// nwIface represents the settings and identity of a network device.
17
+// Interface represents the settings and identity of a network device.
18 18
 // It is used as a return type for Network.Link, and it is common practice
19 19
 // for the caller to use this information when moving interface SrcName from
20 20
 // host namespace to DstName in a different net namespace with the appropriate
21 21
 // network settings.
22
-type nwIface struct {
22
+type Interface struct {
23 23
 	srcName     string
24 24
 	dstName     string
25 25
 	master      string
... ...
@@ -34,7 +34,7 @@ type nwIface struct {
34 34
 }
35 35
 
36 36
 // SrcName returns the name of the interface in the origin network namespace.
37
-func (i *nwIface) SrcName() string {
37
+func (i *Interface) SrcName() string {
38 38
 	return i.srcName
39 39
 }
40 40
 
... ...
@@ -42,46 +42,46 @@ func (i *nwIface) SrcName() string {
42 42
 // moved inside a network namespace. When the caller passes in a DstName,
43 43
 // it is only expected to pass a prefix. The name will be modified with an
44 44
 // auto-generated suffix.
45
-func (i *nwIface) DstName() string {
45
+func (i *Interface) DstName() string {
46 46
 	return i.dstName
47 47
 }
48 48
 
49
-func (i *nwIface) DstMaster() string {
49
+func (i *Interface) DstMaster() string {
50 50
 	return i.dstMaster
51 51
 }
52 52
 
53 53
 // Bridge returns true if the interface is a bridge.
54
-func (i *nwIface) Bridge() bool {
54
+func (i *Interface) Bridge() bool {
55 55
 	return i.bridge
56 56
 }
57 57
 
58 58
 // Master returns the srcname of the master interface for this interface.
59
-func (i *nwIface) Master() string {
59
+func (i *Interface) Master() string {
60 60
 	return i.master
61 61
 }
62 62
 
63
-func (i *nwIface) MacAddress() net.HardwareAddr {
63
+func (i *Interface) MacAddress() net.HardwareAddr {
64 64
 	return types.GetMacCopy(i.mac)
65 65
 }
66 66
 
67 67
 // Address returns the IPv4 address for the interface.
68
-func (i *nwIface) Address() *net.IPNet {
68
+func (i *Interface) Address() *net.IPNet {
69 69
 	return types.GetIPNetCopy(i.address)
70 70
 }
71 71
 
72 72
 // AddressIPv6 returns the IPv6 address for the interface.
73
-func (i *nwIface) AddressIPv6() *net.IPNet {
73
+func (i *Interface) AddressIPv6() *net.IPNet {
74 74
 	return types.GetIPNetCopy(i.addressIPv6)
75 75
 }
76 76
 
77 77
 // LinkLocalAddresses returns the link-local IP addresses assigned to the
78 78
 // interface.
79
-func (i *nwIface) LinkLocalAddresses() []*net.IPNet {
79
+func (i *Interface) LinkLocalAddresses() []*net.IPNet {
80 80
 	return i.llAddrs
81 81
 }
82 82
 
83 83
 // Routes returns IP routes for the interface.
84
-func (i *nwIface) Routes() []*net.IPNet {
84
+func (i *Interface) Routes() []*net.IPNet {
85 85
 	routes := make([]*net.IPNet, len(i.routes))
86 86
 	for index, route := range i.routes {
87 87
 		routes[index] = types.GetIPNetCopy(route)
... ...
@@ -92,7 +92,7 @@ func (i *nwIface) Routes() []*net.IPNet {
92 92
 
93 93
 // Remove an interface from the sandbox by renaming to original name
94 94
 // and moving it out of the sandbox.
95
-func (i *nwIface) Remove() error {
95
+func (i *Interface) Remove() error {
96 96
 	i.ns.Lock()
97 97
 	isDefault := i.ns.isDefault
98 98
 	nlh := i.ns.nlHandle
... ...
@@ -143,7 +143,7 @@ func (i *nwIface) Remove() error {
143 143
 }
144 144
 
145 145
 // Statistics returns the sandbox's side veth interface statistics.
146
-func (i *nwIface) Statistics() (*types.InterfaceStatistics, error) {
146
+func (i *Interface) Statistics() (*types.InterfaceStatistics, error) {
147 147
 	l, err := i.ns.nlHandle.LinkByName(i.DstName())
148 148
 	if err != nil {
149 149
 		return nil, fmt.Errorf("failed to retrieve the statistics for %s in netns %s: %v", i.DstName(), i.ns.path, err)
... ...
@@ -185,7 +185,7 @@ func (n *networkNamespace) findDst(srcName string, isBridge bool) string {
185 185
 // to only provide a prefix for DstName. The AddInterface api will auto-generate
186 186
 // an appropriate suffix for the DstName to disambiguate.
187 187
 func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...IfaceOption) error {
188
-	i := &nwIface{
188
+	i := &Interface{
189 189
 		srcName: srcName,
190 190
 		dstName: dstPrefix,
191 191
 		ns:      n,
... ...
@@ -299,10 +299,10 @@ func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...If
299 299
 	return nil
300 300
 }
301 301
 
302
-func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
302
+func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
303 303
 	ifaceName := iface.Attrs().Name
304 304
 	ifaceConfigurators := []struct {
305
-		Fn         func(*netlink.Handle, netlink.Link, *nwIface) error
305
+		Fn         func(*netlink.Handle, netlink.Link, *Interface) error
306 306
 		ErrMessage string
307 307
 	}{
308 308
 		{setInterfaceName, fmt.Sprintf("error renaming interface %q to %q", ifaceName, i.DstName())},
... ...
@@ -321,7 +321,7 @@ func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *nwIface) err
321 321
 	return nil
322 322
 }
323 323
 
324
-func setInterfaceMaster(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
324
+func setInterfaceMaster(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
325 325
 	if i.DstMaster() == "" {
326 326
 		return nil
327 327
 	}
... ...
@@ -331,14 +331,14 @@ func setInterfaceMaster(nlh *netlink.Handle, iface netlink.Link, i *nwIface) err
331 331
 	})
332 332
 }
333 333
 
334
-func setInterfaceMAC(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
334
+func setInterfaceMAC(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
335 335
 	if i.MacAddress() == nil {
336 336
 		return nil
337 337
 	}
338 338
 	return nlh.LinkSetHardwareAddr(iface, i.MacAddress())
339 339
 }
340 340
 
341
-func setInterfaceIP(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
341
+func setInterfaceIP(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
342 342
 	if i.Address() == nil {
343 343
 		return nil
344 344
 	}
... ...
@@ -349,7 +349,7 @@ func setInterfaceIP(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
349 349
 	return nlh.AddrAdd(iface, ipAddr)
350 350
 }
351 351
 
352
-func setInterfaceIPv6(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
352
+func setInterfaceIPv6(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
353 353
 	if i.AddressIPv6() == nil {
354 354
 		return nil
355 355
 	}
... ...
@@ -363,7 +363,7 @@ func setInterfaceIPv6(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error
363 363
 	return nlh.AddrAdd(iface, ipAddr)
364 364
 }
365 365
 
366
-func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
366
+func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
367 367
 	for _, llIP := range i.LinkLocalAddresses() {
368 368
 		ipAddr := &netlink.Addr{IPNet: llIP}
369 369
 		if err := nlh.AddrAdd(iface, ipAddr); err != nil {
... ...
@@ -373,11 +373,11 @@ func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *nwIfac
373 373
 	return nil
374 374
 }
375 375
 
376
-func setInterfaceName(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
376
+func setInterfaceName(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
377 377
 	return nlh.LinkSetName(iface, i.DstName())
378 378
 }
379 379
 
380
-func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
380
+func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
381 381
 	for _, route := range i.Routes() {
382 382
 		err := nlh.RouteAdd(&netlink.Route{
383 383
 			Scope:     netlink.SCOPE_LINK,
... ...
@@ -2,4 +2,4 @@
2 2
 
3 3
 package osl
4 4
 
5
-type nwIface struct{}
5
+type Interface struct{}
... ...
@@ -319,7 +319,7 @@ func createNamespaceFile(path string) (err error) {
319 319
 // can be added dynamically.
320 320
 type networkNamespace struct {
321 321
 	path         string
322
-	iFaces       []*nwIface
322
+	iFaces       []*Interface
323 323
 	gw           net.IP
324 324
 	gwv6         net.IP
325 325
 	staticRoutes []*types.StaticRoute
... ...
@@ -335,11 +335,9 @@ type networkNamespace struct {
335 335
 // method. Note that this doesn't include network interfaces added in any
336 336
 // other way (such as the default loopback interface which is automatically
337 337
 // created on creation of a sandbox).
338
-func (n *networkNamespace) Interfaces() []Interface {
339
-	ifaces := make([]Interface, len(n.iFaces))
340
-	for i, iface := range n.iFaces {
341
-		ifaces[i] = iface
342
-	}
338
+func (n *networkNamespace) Interfaces() []*Interface {
339
+	ifaces := make([]*Interface, len(n.iFaces))
340
+	copy(ifaces, n.iFaces)
343 341
 	return ifaces
344 342
 }
345 343
 
... ...
@@ -483,7 +481,7 @@ func (n *networkNamespace) Destroy() error {
483 483
 func (n *networkNamespace) Restore(ifsopt map[Iface][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error {
484 484
 	// restore interfaces
485 485
 	for name, opts := range ifsopt {
486
-		i := &nwIface{
486
+		i := &Interface{
487 487
 			srcName: name.SrcName,
488 488
 			dstName: name.DstPrefix,
489 489
 			ns:      n,
... ...
@@ -24,7 +24,7 @@ func WithFamily(family int) NeighOption {
24 24
 	}
25 25
 }
26 26
 
27
-func (i *nwIface) processInterfaceOptions(options ...IfaceOption) error {
27
+func (i *Interface) processInterfaceOptions(options ...IfaceOption) error {
28 28
 	for _, opt := range options {
29 29
 		if opt != nil {
30 30
 			// TODO(thaJeztah): use multi-error instead of returning early.
... ...
@@ -38,7 +38,7 @@ func (i *nwIface) processInterfaceOptions(options ...IfaceOption) error {
38 38
 
39 39
 // WithIsBridge sets whether the interface is a bridge.
40 40
 func WithIsBridge(isBridge bool) IfaceOption {
41
-	return func(i *nwIface) error {
41
+	return func(i *Interface) error {
42 42
 		i.bridge = isBridge
43 43
 		return nil
44 44
 	}
... ...
@@ -48,7 +48,7 @@ func WithIsBridge(isBridge bool) IfaceOption {
48 48
 // master interface name should refer to the srcName of a previously added
49 49
 // interface of type bridge.
50 50
 func WithMaster(name string) IfaceOption {
51
-	return func(i *nwIface) error {
51
+	return func(i *Interface) error {
52 52
 		i.master = name
53 53
 		return nil
54 54
 	}
... ...
@@ -56,7 +56,7 @@ func WithMaster(name string) IfaceOption {
56 56
 
57 57
 // WithMACAddress sets the interface MAC-address.
58 58
 func WithMACAddress(mac net.HardwareAddr) IfaceOption {
59
-	return func(i *nwIface) error {
59
+	return func(i *Interface) error {
60 60
 		i.mac = mac
61 61
 		return nil
62 62
 	}
... ...
@@ -64,7 +64,7 @@ func WithMACAddress(mac net.HardwareAddr) IfaceOption {
64 64
 
65 65
 // WithIPv4Address sets the IPv4 address of the interface.
66 66
 func WithIPv4Address(addr *net.IPNet) IfaceOption {
67
-	return func(i *nwIface) error {
67
+	return func(i *Interface) error {
68 68
 		i.address = addr
69 69
 		return nil
70 70
 	}
... ...
@@ -72,7 +72,7 @@ func WithIPv4Address(addr *net.IPNet) IfaceOption {
72 72
 
73 73
 // WithIPv6Address sets the IPv6 address of the interface.
74 74
 func WithIPv6Address(addr *net.IPNet) IfaceOption {
75
-	return func(i *nwIface) error {
75
+	return func(i *Interface) error {
76 76
 		i.addressIPv6 = addr
77 77
 		return nil
78 78
 	}
... ...
@@ -80,7 +80,7 @@ func WithIPv6Address(addr *net.IPNet) IfaceOption {
80 80
 
81 81
 // WithLinkLocalAddresses set the link-local IP addresses of the interface.
82 82
 func WithLinkLocalAddresses(list []*net.IPNet) IfaceOption {
83
-	return func(i *nwIface) error {
83
+	return func(i *Interface) error {
84 84
 		i.llAddrs = list
85 85
 		return nil
86 86
 	}
... ...
@@ -88,7 +88,7 @@ func WithLinkLocalAddresses(list []*net.IPNet) IfaceOption {
88 88
 
89 89
 // WithRoutes sets the interface routes.
90 90
 func WithRoutes(routes []*net.IPNet) IfaceOption {
91
-	return func(i *nwIface) error {
91
+	return func(i *Interface) error {
92 92
 		i.routes = routes
93 93
 		return nil
94 94
 	}
... ...
@@ -22,7 +22,7 @@ type Iface struct {
22 22
 }
23 23
 
24 24
 // IfaceOption is a function option type to set interface options.
25
-type IfaceOption func(i *nwIface) error
25
+type IfaceOption func(i *Interface) error
26 26
 
27 27
 // NeighOption is a function option type to set neighbor options.
28 28
 type NeighOption func(nh *neigh)
... ...
@@ -100,7 +100,7 @@ type Info interface {
100 100
 	// method. Note that this doesn't include network interfaces added in any
101 101
 	// other way (such as the default loopback interface which is automatically
102 102
 	// created on creation of a sandbox).
103
-	Interfaces() []Interface
103
+	Interfaces() []*Interface
104 104
 
105 105
 	// Gateway returns the IPv4 gateway for the sandbox.
106 106
 	Gateway() net.IP
... ...
@@ -113,45 +113,3 @@ type Info interface {
113 113
 	// refer to.
114 114
 	StaticRoutes() []*types.StaticRoute
115 115
 }
116
-
117
-// Interface represents the settings and identity of a network device. It is
118
-// used as a return type for Network.Link, and it is common practice for the
119
-// caller to use this information when moving interface SrcName from host
120
-// namespace to DstName in a different net namespace with the appropriate
121
-// network settings.
122
-type Interface interface {
123
-	// SrcName returns the name of the interface in the origin network namespace.
124
-	SrcName() string
125
-
126
-	// DstName returns the name that will be assigned to the interface once
127
-	// moved inside a network namespace. When the caller passes in a DstName,
128
-	// it is only expected to pass a prefix. The name will be modified with an
129
-	// auto-generated suffix.
130
-	DstName() string
131
-
132
-	// Address returns the IPv4 address for the interface.
133
-	Address() *net.IPNet
134
-
135
-	// AddressIPv6 returns the IPv6 address for the interface.
136
-	AddressIPv6() *net.IPNet
137
-
138
-	// LinkLocalAddresses returns the link-local IP addresses assigned to the
139
-	// interface.
140
-	LinkLocalAddresses() []*net.IPNet
141
-
142
-	// Routes returns IP routes for the interface.
143
-	Routes() []*net.IPNet
144
-
145
-	// Bridge returns true if the interface is a bridge.
146
-	Bridge() bool
147
-
148
-	// Master returns the srcname of the master interface for this interface.
149
-	Master() string
150
-
151
-	// Remove an interface from the sandbox by renaming to original name
152
-	// and moving it out of the sandbox.
153
-	Remove() error
154
-
155
-	// Statistics returns the statistics for this interface
156
-	Statistics() (*types.InterfaceStatistics, error)
157
-}
... ...
@@ -85,7 +85,7 @@ func newInfo(t *testing.T, hnd *netlink.Handle) (Sandbox, error) {
85 85
 
86 86
 	// Store the sandbox side pipe interface
87 87
 	// This is needed for cleanup on DeleteEndpoint()
88
-	intf1 := &nwIface{
88
+	intf1 := &Interface{
89 89
 		srcName:     vethName2,
90 90
 		dstName:     sboxIfaceName,
91 91
 		address:     addr,
... ...
@@ -93,7 +93,7 @@ func newInfo(t *testing.T, hnd *netlink.Handle) (Sandbox, error) {
93 93
 		routes:      []*net.IPNet{route},
94 94
 	}
95 95
 
96
-	intf2 := &nwIface{
96
+	intf2 := &Interface{
97 97
 		srcName: "testbridge",
98 98
 		dstName: sboxIfaceName,
99 99
 		bridge:  true,
... ...
@@ -107,14 +107,14 @@ func newInfo(t *testing.T, hnd *netlink.Handle) (Sandbox, error) {
107 107
 		return nil, err
108 108
 	}
109 109
 
110
-	intf3 := &nwIface{
110
+	intf3 := &Interface{
111 111
 		srcName: vethName4,
112 112
 		dstName: sboxIfaceName,
113 113
 		master:  "testbridge",
114 114
 	}
115 115
 
116 116
 	return &networkNamespace{
117
-		iFaces: []*nwIface{intf1, intf2, intf3},
117
+		iFaces: []*Interface{intf1, intf2, intf3},
118 118
 		gw:     net.ParseIP("192.168.1.1"),
119 119
 		gwv6:   net.ParseIP("fe80::1"),
120 120
 	}, nil
... ...
@@ -182,7 +182,7 @@ func TestDisableIPv6DAD(t *testing.T) {
182 182
 	nlh := n.nlHandle
183 183
 
184 184
 	ipv6, _ := types.ParseCIDR("2001:db8::44/64")
185
-	iface := &nwIface{addressIPv6: ipv6, ns: n, dstName: "sideA"}
185
+	iface := &Interface{addressIPv6: ipv6, ns: n, dstName: "sideA"}
186 186
 
187 187
 	veth := &netlink.Veth{
188 188
 		LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
... ...
@@ -242,7 +242,7 @@ func TestSetInterfaceIP(t *testing.T) {
242 242
 
243 243
 	ipv4, _ := types.ParseCIDR("172.30.0.33/24")
244 244
 	ipv6, _ := types.ParseCIDR("2001:db8::44/64")
245
-	iface := &nwIface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
245
+	iface := &Interface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
246 246
 
247 247
 	if err := nlh.LinkAdd(&netlink.Veth{
248 248
 		LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
... ...
@@ -316,7 +316,7 @@ func TestLiveRestore(t *testing.T) {
316 316
 
317 317
 	ipv4, _ := types.ParseCIDR("172.30.0.33/24")
318 318
 	ipv6, _ := types.ParseCIDR("2001:db8::44/64")
319
-	iface := &nwIface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
319
+	iface := &Interface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
320 320
 
321 321
 	if err := nlh.LinkAdd(&netlink.Veth{
322 322
 		LinkAttrs: netlink.LinkAttrs{Name: "sideA"},