Skip to content

Commit

Permalink
Ability to set multicast attribute on a link
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisafa committed May 20, 2021
1 parent 4cb3795 commit ed687b3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type LinkAttrs struct {
Statistics *LinkStatistics
Promisc int
Allmulti int
Multi int
Xdp *LinkXdp
EncapType string
Protinfo *Protinfo
Expand Down
45 changes: 45 additions & 0 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,47 @@ func (h *Handle) LinkSetAllmulticastOff(link Link) error {
return err
}

func LinkSetMulticastOn(link Link) error {
return pkgHandle.LinkSetMulticastOn(link)
}

// LinkSetAllmulticastOn enables the reception of all hardware multicast packets for the link device.
// Equivalent to: `ip link set $link allmulticast on`
func (h *Handle) LinkSetMulticastOn(link Link) error {
base := link.Attrs()
h.ensureIndex(base)
req := h.newNetlinkRequest(unix.RTM_NEWLINK, unix.NLM_F_ACK)

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

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

func LinkSetMulticastOff(link Link) error {
return pkgHandle.LinkSetMulticastOff(link)
}

// LinkSetAllmulticastOff disables the reception of all hardware multicast packets for the link device.
// Equivalent to: `ip link set $link allmulticast off`
func (h *Handle) LinkSetMulticastOff(link Link) error {
base := link.Attrs()
h.ensureIndex(base)
req := h.newNetlinkRequest(unix.RTM_NEWLINK, unix.NLM_F_ACK)

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

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

func MacvlanMACAddrAdd(link Link, addr net.HardwareAddr) error {
return pkgHandle.MacvlanMACAddrAdd(link, addr)
}
Expand Down Expand Up @@ -1648,6 +1689,10 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
if msg.Flags&unix.IFF_ALLMULTI != 0 {
base.Allmulti = 1
}
if msg.Flags&unix.IFF_MULTICAST != 0 {
base.Multi = 1
}

var (
link Link
stats32 *LinkStatistics32
Expand Down
50 changes: 50 additions & 0 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2663,6 +2663,56 @@ func TestLinkSetAllmulticast(t *testing.T) {
}
}

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

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

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

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

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

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

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

if link.Attrs().Multi != 1 {
t.Fatal("IFF_MULTICAST was not set")
}

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

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

if link.Attrs().Multi != 0 {
t.Fatal("IFF_MULTICAST is still set")
}
}

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

0 comments on commit ed687b3

Please sign in to comment.