netlink icon indicating copy to clipboard operation
netlink copied to clipboard

link: Create some exported constants for each link type

Open sboeuf opened this issue 7 years ago • 4 comments

I think that having the link type such as "tap", "macvlan", "ipvlan", ... being hardcoded directly into each implementation of the function Type() from the Link interface is not a great idea since it prevents those from being reused as standards.

What I'm suggesting is that we could define some constants like:

const NetlinkLinkTypeMacvtap = "macvtap"

so that each implementation would be along those lines:

func (macvtap Macvtap) Type() string {
	return NetlinkLinkTypeMacvtap
}

and at the same time could be reused by any code importing this library.

sboeuf avatar Oct 26 '18 20:10 sboeuf

/cc @amshinde @mcastelino

sboeuf avatar Oct 26 '18 20:10 sboeuf

Any news on this? I would like to use this library to create Wireguard interfaces (www.wireguard.com), but atm it is a bit limiting. Having something like this would make it much more comfortable to use.

aroundthfur avatar Feb 03 '20 15:02 aroundthfur

As alternative for now, if someone else has this use-case, you can simply do this:

package main

import (
        "fmt"
        "github.com/vishvananda/netlink"
        "log"
)

// WG links are virtual wireguard ethernet devices
type WG struct {
        netlink.LinkAttrs
}

func (wglink *WG) Attrs() *netlink.LinkAttrs {
        return &wglink.LinkAttrs
}

func (wglink *WG) Type() string {
        return "wireguard"
}

func main() {

        wa := netlink.NewLinkAttrs()
        wa.Name = "wg0"
        wgdev := &WG{LinkAttrs: wa}
        err := netlink.LinkAdd(wgdev)
        if err != nil {
                log.Fatal(err)
        }

        w, _ := netlink.LinkByName("wg0")
        fmt.Printf("%+v\n", w)

}

But it would still be nice to have a generic way of doing it instead.

aroundthfur avatar Feb 03 '20 15:02 aroundthfur

Worked for me, thanks @aroundthfur

anderspitman avatar Feb 02 '23 00:02 anderspitman