Skip to content

Commit

Permalink
Merge pull request #3 from deepakkumarbhagat/link_modify
Browse files Browse the repository at this point in the history
Link modify, re-base from upstream
  • Loading branch information
kmabda authored Jun 15, 2023
2 parents c2befd3 + 31fffa1 commit 2376db9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
13 changes: 6 additions & 7 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,18 +1012,12 @@ func (h *Handle) LinkAdd(link Link) error {
return h.linkModify(link, unix.NLM_F_CREATE|unix.NLM_F_EXCL|unix.NLM_F_ACK)
}

// LinkModify modify an existing link device. The type and features of the device
// are taken from the parameters in the link object.
// Equivalent to: `ip link add $link`
func LinkModify(link Link) error {
return pkgHandle.LinkModify(link)
}

// LinkModify modify an existing link device. The type and features of the device
// are taken from the parameters in the link object.
// Equivalent to: `ip link add $link`
func (h *Handle) LinkModify(link Link) error {
return h.linkModify(link, unix.NLM_F_CREATE|unix.NLM_F_ACK)
return h.linkModify(link, unix.NLM_F_REQUEST|unix.NLM_F_ACK)
}

func (h *Handle) linkModify(link Link, flags int) error {
Expand Down Expand Up @@ -1171,6 +1165,11 @@ func (h *Handle) linkModify(link Link, flags int) error {
nameData := nl.NewRtAttr(unix.IFLA_IFNAME, nl.ZeroTerminated(base.Name))
req.AddData(nameData)

if base.Alias != "" {
alias := nl.NewRtAttr(unix.IFLA_IFALIAS, []byte(base.Alias))
req.AddData(alias)
}

if base.MTU > 0 {
mtu := nl.NewRtAttr(unix.IFLA_MTU, nl.Uint32Attr(uint32(base.MTU)))
req.AddData(mtu)
Expand Down
31 changes: 31 additions & 0 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,37 @@ func TestLinkAddDelDummyWithGroup(t *testing.T) {
testLinkAddDel(t, &Dummy{LinkAttrs{Name: "foo", Group: 42}})
}

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

linkName := "foo"
originalMTU := 1500
updatedMTU := 1442

link := &Dummy{LinkAttrs{Name: linkName, MTU: originalMTU}}
base := link.Attrs()

if err := LinkAdd(link); err != nil {
t.Fatal(err)
}

link.MTU = updatedMTU
if err := LinkModify(link); err != nil {
t.Fatal(err)
}

result, err := LinkByName(linkName)
if err != nil {
t.Fatal(err)
}

rBase := result.Attrs()
if rBase.MTU != updatedMTU {
t.Fatalf("MTU is %d, should be %d", rBase.MTU, base.MTU)
}
}

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

0 comments on commit 2376db9

Please sign in to comment.