From 0e6c53b35b845c157f477335c8cdcb762e65547a Mon Sep 17 00:00:00 2001 From: Ashraf Fouda Date: Mon, 26 Aug 2024 14:42:49 +0300 Subject: [PATCH] change all macvlans to veth Signed-off-by: Ashraf Fouda --- cmds/modules/netlightd/nft/rules.nft | 2 +- pkg/netlight/bridge/bridge.go | 2 +- pkg/netlight/ifaceutil/interface.go | 13 ++++-- pkg/netlight/resource/resource.go | 68 +++++++++++++++------------- pkg/primitives/vm-light/utils.go | 2 +- 5 files changed, 49 insertions(+), 38 deletions(-) diff --git a/cmds/modules/netlightd/nft/rules.nft b/cmds/modules/netlightd/nft/rules.nft index 4a919cb0e..fa17bba39 100644 --- a/cmds/modules/netlightd/nft/rules.nft +++ b/cmds/modules/netlightd/nft/rules.nft @@ -49,6 +49,6 @@ table bridge filter { table ip nat { chain postrouting { type nat hook postrouting priority srcnat; policy accept; - iifname "gw" masquerade fully-random + iifname "br-ndmz" masquerade fully-random } } diff --git a/pkg/netlight/bridge/bridge.go b/pkg/netlight/bridge/bridge.go index e939292f2..cdea34f14 100644 --- a/pkg/netlight/bridge/bridge.go +++ b/pkg/netlight/bridge/bridge.go @@ -116,7 +116,7 @@ func Attach(link netlink.Link, bridge *netlink.Bridge, vlan *uint16, name ...str n = name[0] } //we need to create an veth pair to wire 2 bridges. - veth, err := ifaceutil.MakeVethPair(n, bridge.Name, 1500) + veth, err := ifaceutil.MakeVethPair(n, bridge.Name, 1500, "") if err != nil { return err } diff --git a/pkg/netlight/ifaceutil/interface.go b/pkg/netlight/ifaceutil/interface.go index a33d3995b..067318991 100644 --- a/pkg/netlight/ifaceutil/interface.go +++ b/pkg/netlight/ifaceutil/interface.go @@ -134,13 +134,20 @@ func RandomName(prefix string) (string, error) { } // MakeVethPair creates a veth pair -func MakeVethPair(name, master string, mtu int) (netlink.Link, error) { +func MakeVethPair(name, master string, mtu int, peerPrefix string) (netlink.Link, error) { masterLink, err := netlink.LinkByName(master) if err != nil { return nil, fmt.Errorf("master link: %s not found: %v", master, err) } - - peer := fmt.Sprintf("%s-p", name) + peer := "" + if peerPrefix == "" { + peer = fmt.Sprintf("p-%s", name) + } else { + peer = fmt.Sprintf("%s-%s", peerPrefix, name) + } + if len(peer) > 15 { + peer = peer[0:15] + } veth := &netlink.Veth{ LinkAttrs: netlink.LinkAttrs{ Name: name, diff --git a/pkg/netlight/resource/resource.go b/pkg/netlight/resource/resource.go index 8d71208de..c13bf1fb8 100644 --- a/pkg/netlight/resource/resource.go +++ b/pkg/netlight/resource/resource.go @@ -77,22 +77,23 @@ func Create(name string, master *netlink.Bridge, ndmzIP *net.IPNet, ndmzGwIP *ne } if !ifaceutil.Exists(infPrivate, netNS) { - err = netNS.Do(func(_ ns.NetNS) error { - _, err = ifaceutil.MakeVethPair(infPrivate, privateNetBr, 1500) - return err - - }) + privateLink, err := ifaceutil.MakeVethPair(infPrivate, privateNetBr, 1500, nsName[0:3]) if err != nil { return nil, fmt.Errorf("failed to create private link: %w", err) } + err = netlink.LinkSetNsFd(privateLink, int(netNS.Fd())) + if err != nil { + return nil, fmt.Errorf("failed to move public link %s to namespace:%s : %w", infPublic, netNS.Path(), err) + } + } } // create public interface and attach it to ndmz bridge if !ifaceutil.Exists(infPublic, netNS) { - pubLink, err := ifaceutil.MakeVethPair(infPublic, master.Name, 1500) + pubLink, err := ifaceutil.MakeVethPair(infPublic, master.Name, 1500, nsName) if err != nil { - return nil, fmt.Errorf("failed to create public link: %w", err) + return nil, fmt.Errorf("failed to create public link in namespace %s: %w", nsName[0:3], err) } err = netlink.LinkSetNsFd(pubLink, int(netNS.Fd())) if err != nil { @@ -102,7 +103,7 @@ func Create(name string, master *netlink.Bridge, ndmzIP *net.IPNet, ndmzGwIP *ne } if !ifaceutil.Exists(infMycelium, netNS) { - myceliumLink, err := ifaceutil.MakeVethPair(infMycelium, myBr, 1500) + myceliumLink, err := ifaceutil.MakeVethPair(infMycelium, myBr, 1500, nsName[0:3]) if err != nil { return nil, fmt.Errorf("failed to create mycelium link: %w", err) } @@ -136,7 +137,6 @@ func Create(name string, master *netlink.Bridge, ndmzIP *net.IPNet, ndmzGwIP *ne } } - // if err := setLinkAddr(infPrivate, ) if err := netlink.RouteAdd(&netlink.Route{ Gw: ndmzGwIP.IP, }); err != nil && !os.IsExist(err) { @@ -379,39 +379,43 @@ func (r *Resource) AttachMyceliumZDB(id string, zdbNS ns.NetNS) (err error) { if err != nil { return } - // routes := []*netlink.Route{ - // { - // Dst: &net.IPNet{ - // IP: net.ParseIP("400::"), - // Mask: net.CIDRMask(7, 128), - // }, - // Gw: gw.IP, - // }, - // } deviceName := ifaceutil.DeviceNameFromInputBytes([]byte(id)) linkName := fmt.Sprintf("m-%s", deviceName) // if !ifaceutil.Exists(linkName, zdbNS) { - zdbLink, err := ifaceutil.MakeVethPair(linkName, "mdmz", 1500) + zdbLink, err := ifaceutil.MakeVethPair(linkName, "mdmz", 1500, nsName[0:3]) if err != nil { - return fmt.Errorf("failed to create zdb link: %w", err) + return fmt.Errorf("failed to create zdb link %s : %w", linkName, err) } - err = netlink.LinkSetNsFd(zdbLink, int(netNS.Fd())) + err = netlink.LinkSetNsFd(zdbLink, int(zdbNS.Fd())) if err != nil { - return fmt.Errorf("failed to move mycelium link: %s to namespace:%s : %w", linkName, netNS.Path(), err) + return fmt.Errorf("failed to move zdb link: %s to namespace:%s : %w", linkName, netNS.Path(), err) } - err = setLinkAddr(linkName, &ip) - if err != nil { - return err - } - return netlink.RouteAdd(&netlink.Route{ - Dst: &net.IPNet{ - IP: net.ParseIP("400::"), - Mask: net.CIDRMask(7, 128), - }, - Gw: gw.IP, + + return zdbNS.Do(func(_ ns.NetNS) error { + err = setLinkAddr(linkName, &ip) + if err != nil { + return err + } + + if err := ifaceutil.SetLoUp(); err != nil { + return fmt.Errorf("failed to set lo up for namespace '%s': %w", nsName, err) + } + + if err := options.SetIPv6Forwarding(true); err != nil { + return fmt.Errorf("failed to enable ipv6 forwarding in namespace %q: %w", nsName, err) + } + + return netlink.RouteAdd(&netlink.Route{ + Dst: &net.IPNet{ + IP: net.ParseIP("400::"), + Mask: net.CIDRMask(7, 128), + }, + Gw: gw.IP, + }) + }) } return nil diff --git a/pkg/primitives/vm-light/utils.go b/pkg/primitives/vm-light/utils.go index a6c150882..cc3982f27 100644 --- a/pkg/primitives/vm-light/utils.go +++ b/pkg/primitives/vm-light/utils.go @@ -204,7 +204,7 @@ func (p *Manager) newPrivNetworkInterface(ctx context.Context, dl gridtypes.Depl tapName := wl.ID.Unique(string(inf.Network)) iface, err := network.AttachPrivate(ctx, string(netID), tapName, inf.IP) if err != nil { - return pkg.VMIface{}, errors.Wrap(err, "could not set up tap device") + return pkg.VMIface{}, errors.Wrap(err, "could not set up tap device for private interface") } out := pkg.VMIface{