Skip to content

Commit

Permalink
Add error details for setupIndieNetns
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Jan 5, 2024
1 parent 98a13a8 commit a9fc8f9
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions control/netns_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package control

import (
"fmt"
"net"
"os"
"path"
Expand Down Expand Up @@ -55,18 +56,18 @@ func setupIndieNetns() (err error) {

hostNetns, err := netns.Get()
if err != nil {
return
return fmt.Errorf("Failed to get host netns: %v", err)
}
defer netns.Set(hostNetns)

// ip netns a daens
DeleteNamedNetns("daens")
indieNetns, err = netns.NewNamed("daens")
if err != nil {
return
return fmt.Errorf("Failed to create netns: %v", err)
}
if err = netns.Set(hostNetns); err != nil {
return
return fmt.Errorf("Failed to switch to host netns: %v", err)
}
// ip l a dae0 type veth peer name dae0peer
DeleteLink("dae0")
Expand All @@ -76,62 +77,62 @@ func setupIndieNetns() (err error) {
},
PeerName: "dae0peer",
}); err != nil {
return
return fmt.Errorf("Failed to add veth pair: %v", err)
}
dae0, err := netlink.LinkByName("dae0")
if err != nil {
return
return fmt.Errorf("Failed to get link dae0: %v", err)
}
dae0peer, err := netlink.LinkByName("dae0peer")
if err != nil {
return
return fmt.Errorf("Failed to get link dae0peer: %v", err)
}
// ip l s dae0 up
if err = netlink.LinkSetUp(dae0); err != nil {
return
return fmt.Errorf("Failed to set link dae0 up: %v", err)
}
// sysctl net.ipv4.conf.{dae0,all}.rp_filter=0
if err = SetRpFilter("dae0", "0"); err != nil {
return
return fmt.Errorf("Failed to set rp_filter for dae0: %v", err)
}
if err = SetRpFilter("all", "0"); err != nil {
return
return fmt.Errorf("Failed to set rp_filter for all: %v", err)
}
// ip l s dae0peer netns daens
if err = netlink.LinkSetNsFd(dae0peer, int(indieNetns)); err != nil {
return
return fmt.Errorf("Failed to move dae0peer to daens: %v", err)
}
// ip net e daens
if err = netns.Set(indieNetns); err != nil {
return
return fmt.Errorf("Failed to switch to daens: %v", err)
}
// (ip net e daens) ip l s dae0peer up
if err = netlink.LinkSetUp(dae0peer); err != nil {
return
return fmt.Errorf("Failed to set link dae0peer up: %v", err)
}
// (ip net e daens) ip a a 169.254.0.1 dev dae0peer
ip, ipNet, err := net.ParseCIDR("169.254.0.1/24")
ipNet.IP = ip
if err != nil {
return
return fmt.Errorf("Failed to parse ip: %v", err)
}
if err = netlink.AddrAdd(dae0peer, &netlink.Addr{IPNet: ipNet}); err != nil {
return
return fmt.Errorf("Failed to add addr to dae0peer: %v", err)
}
// (ip net e daens) ip r a default dev dae0peer
if err = netlink.RouteAdd(&netlink.Route{
LinkIndex: dae0peer.Attrs().Index,
Dst: &net.IPNet{IP: net.IPv4(0, 0, 0, 0), Mask: net.CIDRMask(0, 32)},
Gw: nil,
}); err != nil {
return
return fmt.Errorf("Failed to add route to dae0peer: %v", err)
}
return
}

func DeleteNamedNetns(name string) error {
namedPath := path.Join("/run/netns", name)
unix.Unmount(namedPath, unix.MNT_DETACH)
unix.Unmount(namedPath, unix.MNT_DETACH|unix.MNT_FORCE)
return os.Remove(namedPath)
}

Expand Down

0 comments on commit a9fc8f9

Please sign in to comment.