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

Change behavior of Subscribe to non-blocking. Fix test. #941

Merged
merged 1 commit into from
Mar 1, 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
3 changes: 3 additions & 0 deletions addr_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ func addrSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- AddrUpdate, done <-c
for {
msgs, from, err := s.Receive()
if err != nil {
if err == syscall.EAGAIN {
continue
}
if cberr != nil {
cberr(fmt.Errorf("Receive failed: %v",
err))
Expand Down
29 changes: 16 additions & 13 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,28 +996,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)
}

// 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 @@ -2353,6 +2353,9 @@ func linkSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- LinkUpdate, done <-c
for {
msgs, from, err := s.Receive()
if err != nil {
if err == syscall.EAGAIN {
continue
}
if cberr != nil {
cberr(fmt.Errorf("Receive failed: %v",
err))
Expand Down
7 changes: 3 additions & 4 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,10 +1605,8 @@ func TestLinkAddDelVxlanFlowBased(t *testing.T) {
}

func TestLinkAddDelBareUDP(t *testing.T) {
if os.Getenv("CI") == "true" {
t.Skipf("Fails in CI due to operation not supported (missing kernel module?)")
}
minKernelRequired(t, 5, 8)
minKernelRequired(t, 5, 1)
setUpNetlinkTestWithKModule(t, "bareudp")
tearDown := setUpNetlinkTest(t)
defer tearDown()

Expand All @@ -1635,6 +1633,7 @@ func TestBareUDPCompareToIP(t *testing.T) {
}
// requires iproute2 >= 5.10
minKernelRequired(t, 5, 9)
setUpNetlinkTestWithKModule(t, "bareudp")
ns, tearDown := setUpNamedNetlinkTest(t)
defer tearDown()

Expand Down
3 changes: 3 additions & 0 deletions neigh_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ func neighSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- NeighUpdate, done <
for {
msgs, from, err := s.Receive()
if err != nil {
if err == syscall.EAGAIN {
continue
}
if cberr != nil {
cberr(err)
}
Expand Down
10 changes: 10 additions & 0 deletions nl/nl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,16 @@ func Subscribe(protocol int, groups ...uint) (*NetlinkSocket, error) {
if err != nil {
return nil, err
}

// Sometimes (socket_linux.go:SocketGet), Subscribe is used to create a socket
// that subscirbed to no groups. So we don't need to set nonblock there.
if len(groups) > 0 {
if err := unix.SetNonblock(fd, true); err != nil {
unix.Close(fd)
return nil, err
}
}

s := &NetlinkSocket{
fd: int32(fd),
}
Expand Down
3 changes: 3 additions & 0 deletions proc_event_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func ProcEventMonitor(ch chan<- ProcEvent, done <-chan struct{}, errorChan chan<
for {
msgs, from, err := s.Receive()
if err != nil {
if err == syscall.EAGAIN {
continue
}
errorChan <- err
return
}
Expand Down
3 changes: 3 additions & 0 deletions route_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,9 @@ func routeSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- RouteUpdate, done <
for {
msgs, from, err := s.Receive()
if err != nil {
if err == syscall.EAGAIN {
continue
}
if cberr != nil {
cberr(fmt.Errorf("Receive failed: %v",
err))
Expand Down
3 changes: 3 additions & 0 deletions socket_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ loop:
for {
msgs, from, err := s.Receive()
if err != nil {
if err == syscall.EAGAIN {
continue
}
return err
}
if from.Pid != nl.PidKernel {
Expand Down
4 changes: 4 additions & 0 deletions xfrm_monitor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package netlink

import (
"fmt"
"syscall"

"github.com/vishvananda/netlink/nl"
"github.com/vishvananda/netns"
Expand Down Expand Up @@ -56,6 +57,9 @@ func XfrmMonitor(ch chan<- XfrmMsg, done <-chan struct{}, errorChan chan<- error
for {
msgs, from, err := s.Receive()
if err != nil {
if err == syscall.EAGAIN {
continue
}
errorChan <- err
return
}
Expand Down
Loading