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

Add support for alternative names #862

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type LinkAttrs struct {
MasterIndex int // must be the index of a bridge
Namespace interface{} // nil | NsPid | NsFd
Alias string
AltNames []string
Statistics *LinkStatistics
Promisc int
Allmulti int
Expand Down
97 changes: 84 additions & 13 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,58 @@ func (h *Handle) LinkSetAlias(link Link, name string) error {
return err
}

// LinkAddAltName adds a new alternative name for the link device.
// Equivalent to: `ip link property add $link altname $name`
func LinkAddAltName(link Link, name string) error {
return pkgHandle.LinkAddAltName(link, name)
}

// LinkAddAltName adds a new alternative name for the link device.
// Equivalent to: `ip link property add $link altname $name`
func (h *Handle) LinkAddAltName(link Link, name string) error {
base := link.Attrs()
h.ensureIndex(base)
req := h.newNetlinkRequest(unix.RTM_NEWLINKPROP, unix.NLM_F_ACK)

msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
msg.Index = int32(base.Index)
req.AddData(msg)

data := nl.NewRtAttr(unix.IFLA_PROP_LIST|unix.NLA_F_NESTED, nil)
data.AddRtAttr(unix.IFLA_ALT_IFNAME, []byte(name))

req.AddData(data)

_, err := req.Execute(unix.NETLINK_ROUTE, 0)
return err
}

// LinkDelAltName delete an alternative name for the link device.
// Equivalent to: `ip link property del $link altname $name`
func LinkDelAltName(link Link, name string) error {
return pkgHandle.LinkDelAltName(link, name)
}

// LinkDelAltName delete an alternative name for the link device.
// Equivalent to: `ip link property del $link altname $name`
func (h *Handle) LinkDelAltName(link Link, name string) error {
base := link.Attrs()
h.ensureIndex(base)
req := h.newNetlinkRequest(unix.RTM_DELLINKPROP, unix.NLM_F_ACK)

msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
msg.Index = int32(base.Index)
req.AddData(msg)

data := nl.NewRtAttr(unix.IFLA_PROP_LIST|unix.NLA_F_NESTED, nil)
data.AddRtAttr(unix.IFLA_ALT_IFNAME, []byte(name))

req.AddData(data)

_, err := req.Execute(unix.NETLINK_ROUTE, 0)
return err
}

// LinkSetHardwareAddr sets the hardware address of the link device.
// Equivalent to: `ip link set $link address $hwaddr`
func LinkSetHardwareAddr(link Link, hwaddr net.HardwareAddr) error {
Expand Down Expand Up @@ -996,28 +1048,28 @@ func LinkSetXdpFdWithFlags(link Link, fd, flags int) error {
// LinkSetGSOMaxSegs sets the GSO maximum segment count of the link device.
// Equivalent to: `ip link set $link gso_max_segs $maxSegs`
func LinkSetGSOMaxSegs(link Link, maxSegs int) error {
return pkgHandle.LinkSetGSOMaxSegs(link, maxSegs)
return pkgHandle.LinkSetGSOMaxSegs(link, maxSegs)
SchSeba marked this conversation as resolved.
Show resolved Hide resolved
}

// LinkSetGSOMaxSegs sets the GSO maximum segment count of the link device.
// Equivalent to: `ip link set $link gso_max_segs $maxSegs`
func (h *Handle) LinkSetGSOMaxSegs(link Link, maxSize int) error {
base := link.Attrs()
h.ensureIndex(base)
req := h.newNetlinkRequest(unix.RTM_SETLINK, unix.NLM_F_ACK)
base := link.Attrs()
h.ensureIndex(base)
req := h.newNetlinkRequest(unix.RTM_SETLINK, unix.NLM_F_ACK)

msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
msg.Index = int32(base.Index)
req.AddData(msg)
msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
msg.Index = int32(base.Index)
req.AddData(msg)

b := make([]byte, 4)
native.PutUint32(b, uint32(maxSize))
b := make([]byte, 4)
native.PutUint32(b, uint32(maxSize))

data := nl.NewRtAttr(unix.IFLA_GSO_MAX_SEGS, b)
req.AddData(data)
data := nl.NewRtAttr(unix.IFLA_GSO_MAX_SEGS, b)
req.AddData(data)

_, err := req.Execute(unix.NETLINK_ROUTE, 0)
return err
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
return err
}

// LinkSetGSOMaxSize sets the IPv6 GSO maximum size of the link device.
Expand Down Expand Up @@ -1770,6 +1822,13 @@ func (h *Handle) linkByNameDump(name string) (Link, error) {
if link.Attrs().Name == name {
return link, nil
}

// support finding interfaces also via altnames
for _, altName := range link.Attrs().AltNames {
if altName == name {
return link, nil
}
}
}
return nil, LinkNotFoundError{fmt.Errorf("Link %s not found", name)}
}
Expand Down Expand Up @@ -2136,6 +2195,18 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
protinfo := parseProtinfo(attrs)
base.Protinfo = &protinfo
}
case unix.IFLA_PROP_LIST | unix.NLA_F_NESTED:
attrs, err := nl.ParseRouteAttr(attr.Value[:])
if err != nil {
return nil, err
}

base.AltNames = []string{}
for _, attr := range attrs {
if attr.Attr.Type == unix.IFLA_ALT_IFNAME {
base.AltNames = append(base.AltNames, nl.BytesToString(attr.Value))
}
}
case unix.IFLA_OPERSTATE:
base.OperState = LinkOperState(uint8(attr.Value[0]))
case unix.IFLA_PHYS_SWITCH_ID:
Expand Down
74 changes: 74 additions & 0 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,80 @@ func TestLinkSet(t *testing.T) {
}
}

func TestLinkAltName(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()

iface := &Dummy{LinkAttrs{Name: "bar"}}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}

link, err := LinkByName("bar")
if err != nil {
t.Fatal(err)
}

err = LinkAddAltName(link, "altname")
SchSeba marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("Could not add altname: %v", err)
}

err = LinkAddAltName(link, "altname2")
if err != nil {
t.Fatalf("Could not add altname: %v", err)
}

link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}

altNameExist := false
altName2Exist := false
for _, altName := range link.Attrs().AltNames {
if altName == "altname" {
altNameExist = true
} else if altName == "altname2" {
altName2Exist = true
}

}
if !altNameExist {
t.Fatal("Could not find altname")
}

if !altName2Exist {
t.Fatal("Could not find altname2")
}

link, err = LinkByName("altname")
if err != nil {
t.Fatal(err)
}

err = LinkDelAltName(link, "altname")
if err != nil {
t.Fatalf("Could not delete altname: %v", err)
}

link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}

altNameExist = false
for _, altName := range link.Attrs().AltNames {
if altName == "altname" {
altNameExist = true
break
}
}
if altNameExist {
t.Fatal("altname still exist")
}
}

func TestLinkSetARP(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
Expand Down
Loading