Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace macvlan with veth pairs #2414

Merged
merged 8 commits into from
Oct 15, 2024
Merged

replace macvlan with veth pairs #2414

merged 8 commits into from
Oct 15, 2024

Conversation

Omarabdul3ziz
Copy link
Contributor

Description

replace macvlan with veth pairs

Related Issues

List of related issues

Checklist

  • Tests included
  • Build pass
  • Documentation
  • Code format and docstring

Co-authored-by: Ashraf Fouda <ashraf.m.fouda@gmail.com>
Comment on lines 173 to 207
name := filepath.Base(netNs.Path())
peerPrefix := name
if len(name) > 4 {
peerPrefix = name[0:4]
}

if !ifaceutil.Exists(ZDBYggIface, netNs) {
yggLink, err := ifaceutil.MakeVethPair(ZDBYggIface, types.YggBridge, 1500, peerPrefix)
if err != nil {
return net.IPNet{}, fmt.Errorf("failed to create ygg link: %w", err)
}

err = netlink.LinkSetNsFd(yggLink, int(netNs.Fd()))
if err != nil {
return net.IPNet{}, fmt.Errorf("failed to move ygg link: %s to namespace:%s : %w", ZDBYggIface, netNs.Path(), err)
}

if err := netNs.Do(func(_ ns.NetNS) error {
err = setLinkAddr(ZDBYggIface, &ip)
if err != nil {
return err
}

if err := ifaceutil.SetLoUp(); err != nil {
return fmt.Errorf("failed to set lo up for namespace '%s': %w", name, err)
}

if err := options.SetIPv6Forwarding(true); err != nil {
return fmt.Errorf("failed to enable ipv6 forwarding in namespace %q: %w", name, err)
}

return netlink.RouteAdd(&route)
}); err != nil {
return net.IPNet{}, fmt.Errorf("failed to setup link addresses %s: %w", ZDBYggIface, err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Omarabdul3ziz Omarabdul3ziz marked this pull request as draft September 26, 2024 10:52
@@ -179,6 +217,40 @@ func MakeVethPair(name, master string, mtu int) (netlink.Link, error) {
return veth2, nil
}

// moveLinkToNamespace moves the veth link to the specified namespace and renames it
func moveLinkToNamespace(veth netlink.Link, tmpName, finalName string, netNs ns.NetNS, peerLink netlink.Link) (netlink.Link, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i suggest we make tmpName > name the underlying method shouldn't know about tmpName I think
and finalName > renameTo which would be a pointer because it maybe nil if we need to just move it to the name space

return fmt.Errorf("failed to set link address: %w", err)
}

// if err := options.Set(name, options.IPv6Disable(false)); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unneeded commented coder

return netlink.LinkSetUp(link)
}

func createInterface(ifName, bridge string, netNs ns.NetNS, ip *net.IPNet, route *netlink.Route) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename it to CreateVethInterface

- create the tmp name inside the veth creation
- rename the function/argument
Copy link
Member

@muhamadazmy muhamadazmy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @Omarabdul3ziz for the good work :) I left few comments below that might be helpful

return nil, fmt.Errorf("master link: %s not found: %v", master, err)
}

nsName := filepath.Base(netNs.Path())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if netNs is nil I see you already calling the function with nil as a last argument already in some places

Comment on lines 170 to 177
tmpName, err := ip.RandomVethName()
if err != nil {
return nil, err
}
ifName := name
if netNs != nil {
ifName = tmpName
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be easier to read if it's re-written like

ifName := name
if netNs != nil {
   ifName, err = ip.RandomVethName()
   if err != nil {
       return nil, err
   }
}

// either from the host, or from the namespace
var veth2 netlink.Link

veth2, err = netlink.LinkByName(name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a link with name name even exist here? Note that so far the 2 links that exists are ifName and peer, not name!

Also veth2 is not a good varible name. i don't know what you refer too.

if err := netlink.LinkSetUp(peerLink); err != nil {
return nil, fmt.Errorf("failed to set veth peer up: %w", err)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this function should return anything because the veth can be inside a namespace. So returning a link that is inside a namespace does not make any sense. Normally to work with links that lives inside a namespace you have to do

ns.Run(func() {
  // load the nic here
  // work with the nic
}

},
Gw: gw.IP,
// LinkIndex:... this is set by macvlan.Install
route := netlink.Route{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think even if this now has only a single route. It's important to understand that this won't always be the case. It's totally legal to configure multiple routes and taking away this ability is not recommended.

- refetch the created link when needed instead of returning it from the creation method
- organize and enhance naming
@Omarabdul3ziz Omarabdul3ziz marked this pull request as ready for review October 14, 2024 12:26
ashraffouda
ashraffouda previously approved these changes Oct 14, 2024
- validate the peer absence
iwanbk
iwanbk previously approved these changes Oct 15, 2024
@Omarabdul3ziz Omarabdul3ziz merged commit 03e2e06 into main Oct 15, 2024
24 checks passed
@Omarabdul3ziz Omarabdul3ziz deleted the main_replace_macvlan branch October 15, 2024 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants