Browse code

libnetwork/osl: move all networkNamespace methods together

These methods were sprinkled throughout the code; let's move
them together.

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

Sebastiaan van Stijn authored on 2023/08/09 06:04:19
Showing 2 changed files
... ...
@@ -73,14 +73,6 @@ func (i *nwIface) Routes() []*net.IPNet {
73 73
 	return routes
74 74
 }
75 75
 
76
-func (n *networkNamespace) Interfaces() []Interface {
77
-	ifaces := make([]Interface, len(n.iFaces))
78
-	for i, iface := range n.iFaces {
79
-		ifaces[i] = iface
80
-	}
81
-	return ifaces
82
-}
83
-
84 76
 func (i *nwIface) Remove() error {
85 77
 	i.ns.Lock()
86 78
 	isDefault := i.ns.isDefault
... ...
@@ -46,23 +46,6 @@ var (
46 46
 	prefix           = defaultPrefix
47 47
 )
48 48
 
49
-// The networkNamespace type is the linux implementation of the Sandbox
50
-// interface. It represents a linux network namespace, and moves an interface
51
-// into it when called on method AddInterface or sets the gateway etc.
52
-type networkNamespace struct {
53
-	path         string
54
-	iFaces       []*nwIface
55
-	gw           net.IP
56
-	gwv6         net.IP
57
-	staticRoutes []*types.StaticRoute
58
-	neighbors    []*neigh
59
-	nextIfIndex  map[string]int
60
-	isDefault    bool
61
-	nlHandle     *netlink.Handle
62
-	loV6Enabled  bool
63
-	sync.Mutex
64
-}
65
-
66 49
 // SetBasePath sets the base url prefix for the ns path
67 50
 func SetBasePath(path string) {
68 51
 	prefix = path
... ...
@@ -242,14 +225,6 @@ func NewSandbox(key string, osCreate, isRestore bool) (Sandbox, error) {
242 242
 	return n, nil
243 243
 }
244 244
 
245
-func (n *networkNamespace) InterfaceOptions() IfaceOptionSetter {
246
-	return n
247
-}
248
-
249
-func (n *networkNamespace) NeighborOptions() NeighborOptionSetter {
250
-	return n
251
-}
252
-
253 245
 func mountNetworkNamespace(basePath string, lnPath string) error {
254 246
 	return syscall.Mount(basePath, lnPath, "bind", syscall.MS_BIND, "")
255 247
 }
... ...
@@ -338,6 +313,39 @@ func createNamespaceFile(path string) (err error) {
338 338
 	return err
339 339
 }
340 340
 
341
+// The networkNamespace type is the linux implementation of the Sandbox
342
+// interface. It represents a linux network namespace, and moves an interface
343
+// into it when called on method AddInterface or sets the gateway etc.
344
+type networkNamespace struct {
345
+	path         string
346
+	iFaces       []*nwIface
347
+	gw           net.IP
348
+	gwv6         net.IP
349
+	staticRoutes []*types.StaticRoute
350
+	neighbors    []*neigh
351
+	nextIfIndex  map[string]int
352
+	isDefault    bool
353
+	nlHandle     *netlink.Handle
354
+	loV6Enabled  bool
355
+	sync.Mutex
356
+}
357
+
358
+func (n *networkNamespace) Interfaces() []Interface {
359
+	ifaces := make([]Interface, len(n.iFaces))
360
+	for i, iface := range n.iFaces {
361
+		ifaces[i] = iface
362
+	}
363
+	return ifaces
364
+}
365
+
366
+func (n *networkNamespace) InterfaceOptions() IfaceOptionSetter {
367
+	return n
368
+}
369
+
370
+func (n *networkNamespace) NeighborOptions() NeighborOptionSetter {
371
+	return n
372
+}
373
+
341 374
 func (n *networkNamespace) loopbackUp() error {
342 375
 	iface, err := n.nlHandle.LinkByName("lo")
343 376
 	if err != nil {
... ...
@@ -598,6 +606,26 @@ func (n *networkNamespace) checkLoV6() {
598 598
 	n.loV6Enabled = enable
599 599
 }
600 600
 
601
+// ApplyOSTweaks applies linux configs on the sandbox
602
+func (n *networkNamespace) ApplyOSTweaks(types []SandboxType) {
603
+	for _, t := range types {
604
+		switch t {
605
+		case SandboxTypeLoadBalancer, SandboxTypeIngress:
606
+			kernel.ApplyOSTweaks(map[string]*kernel.OSValue{
607
+				// disables any special handling on port reuse of existing IPVS connection table entries
608
+				// more info: https://github.com/torvalds/linux/blame/v5.15/Documentation/networking/ipvs-sysctl.rst#L32
609
+				"net.ipv4.vs.conn_reuse_mode": {Value: "0", CheckFn: nil},
610
+				// expires connection from the IPVS connection table when the backend is not available
611
+				// more info: https://github.com/torvalds/linux/blame/v5.15/Documentation/networking/ipvs-sysctl.rst#L133
612
+				"net.ipv4.vs.expire_nodest_conn": {Value: "1", CheckFn: nil},
613
+				// expires persistent connections to destination servers with weights set to 0
614
+				// more info: https://github.com/torvalds/linux/blame/v5.15/Documentation/networking/ipvs-sysctl.rst#L151
615
+				"net.ipv4.vs.expire_quiescent_template": {Value: "1", CheckFn: nil},
616
+			})
617
+		}
618
+	}
619
+}
620
+
601 621
 func setIPv6(nspath, iface string, enable bool) error {
602 622
 	errCh := make(chan error, 1)
603 623
 	go func() {
... ...
@@ -663,23 +691,3 @@ func setIPv6(nspath, iface string, enable bool) error {
663 663
 	}()
664 664
 	return <-errCh
665 665
 }
666
-
667
-// ApplyOSTweaks applies linux configs on the sandbox
668
-func (n *networkNamespace) ApplyOSTweaks(types []SandboxType) {
669
-	for _, t := range types {
670
-		switch t {
671
-		case SandboxTypeLoadBalancer, SandboxTypeIngress:
672
-			kernel.ApplyOSTweaks(map[string]*kernel.OSValue{
673
-				// disables any special handling on port reuse of existing IPVS connection table entries
674
-				// more info: https://github.com/torvalds/linux/blame/v5.15/Documentation/networking/ipvs-sysctl.rst#L32
675
-				"net.ipv4.vs.conn_reuse_mode": {Value: "0", CheckFn: nil},
676
-				// expires connection from the IPVS connection table when the backend is not available
677
-				// more info: https://github.com/torvalds/linux/blame/v5.15/Documentation/networking/ipvs-sysctl.rst#L133
678
-				"net.ipv4.vs.expire_nodest_conn": {Value: "1", CheckFn: nil},
679
-				// expires persistent connections to destination servers with weights set to 0
680
-				// more info: https://github.com/torvalds/linux/blame/v5.15/Documentation/networking/ipvs-sysctl.rst#L151
681
-				"net.ipv4.vs.expire_quiescent_template": {Value: "1", CheckFn: nil},
682
-			})
683
-		}
684
-	}
685
-}