Skip to content

Commit

Permalink
change all macvlans to veth
Browse files Browse the repository at this point in the history
Signed-off-by: Ashraf Fouda <[email protected]>
  • Loading branch information
ashraffouda committed Aug 26, 2024
1 parent 532fc64 commit 0e6c53b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmds/modules/netlightd/nft/rules.nft
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion pkg/netlight/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/netlight/ifaceutil/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 36 additions & 32 deletions pkg/netlight/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/primitives/vm-light/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 0e6c53b

Please sign in to comment.