internal/netlink: set receive buffer size
All checks were successful
Test / Create distribution (push) Successful in 1m14s
Test / Sandbox (push) Successful in 3m2s
Test / Hakurei (push) Successful in 4m15s
Test / ShareFS (push) Successful in 4m15s
Test / Sandbox (race detector) (push) Successful in 5m32s
Test / Hakurei (race detector) (push) Successful in 6m44s
Test / Flake checks (push) Successful in 1m24s

This is done by both systemd sd-device and AOSP ueventd to improve robustness. Rosa OS will still handle ENOBUFS via coldboot but a big buffer should mitigate this as well.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-03-30 02:14:47 +09:00
parent 141958656f
commit 584e302168
5 changed files with 29 additions and 8 deletions

View File

@@ -179,7 +179,7 @@ func (direct) mustLoopback(ctx context.Context, msg message.Msg) {
lo = ifi.Index
}
c, err := netlink.DialRoute()
c, err := netlink.DialRoute(0)
if err != nil {
msg.GetLogger().Fatalln(err)
}

View File

@@ -46,7 +46,11 @@ type Conn struct {
// Dial returns the address of a newly connected generic netlink connection of
// specified family and groups.
func Dial(family int, groups uint32) (*Conn, error) {
//
// For a nonzero rcvbuf, the socket receive buffer size is set to its absolute
// value via SO_RCVBUF for a positive value, or SO_RCVBUFFORCE for a negative
// value.
func Dial(family int, groups uint32, rcvbuf int64) (*Conn, error) {
var c Conn
if fd, err := syscall.Socket(
syscall.AF_NETLINK,
@@ -75,6 +79,23 @@ func Dial(family int, groups uint32) (*Conn, error) {
return nil, syscall.ENOTRECOVERABLE
}
if rcvbuf != 0 {
opt := syscall.SO_RCVBUF
if rcvbuf < 0 {
opt = syscall.SO_RCVBUFFORCE
rcvbuf = -rcvbuf
}
if err = syscall.SetsockoptInt(
fd,
syscall.SOL_SOCKET,
opt,
int(rcvbuf),
); err != nil {
_ = syscall.Close(fd)
return nil, os.NewSyscallError("setsockopt", err)
}
}
c.family = family
c.f = os.NewFile(uintptr(fd), "netlink")
if c.raw, err = c.f.SyscallConn(); err != nil {

View File

@@ -13,8 +13,8 @@ type RouteConn struct{ conn *Conn }
func (c *RouteConn) Close() error { return c.conn.Close() }
// DialRoute returns the address of a newly connected [RouteConn].
func DialRoute() (*RouteConn, error) {
c, err := Dial(syscall.NETLINK_ROUTE, 0)
func DialRoute(rcvbuf int64) (*RouteConn, error) {
c, err := Dial(syscall.NETLINK_ROUTE, 0, rcvbuf)
if err != nil {
return nil, err
}

View File

@@ -50,9 +50,9 @@ func (c *Conn) exitExcl() { c.excl.Store(false) }
func (c *Conn) Close() error { return c.conn.Close() }
// Dial returns the address of a newly connected [Conn].
func Dial() (*Conn, error) {
func Dial(rcvbuf int64) (*Conn, error) {
// kernel group is hard coded in lib/kobject_uevent.c, undocumented
c, err := netlink.Dial(syscall.NETLINK_KOBJECT_UEVENT, 1)
c, err := netlink.Dial(syscall.NETLINK_KOBJECT_UEVENT, 1, rcvbuf)
if err != nil {
return nil, err
}

View File

@@ -116,7 +116,7 @@ func adeB[V any, S interface {
func TestDialConsume(t *testing.T) {
t.Parallel()
c, err := uevent.Dial()
c, err := uevent.Dial(0)
if err != nil {
t.Fatalf("Dial: error = %v", err)
}
@@ -127,7 +127,7 @@ func TestDialConsume(t *testing.T) {
})
// check kernel-assigned port id
c0, err0 := uevent.Dial()
c0, err0 := uevent.Dial(0)
if err0 != nil {
t.Fatalf("Dial: error = %v", err)
}