]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: hoist Getsockname out of NetlinkRIB loops
authorMatt Layher <mdlayher@gmail.com>
Wed, 21 Jul 2021 20:03:34 +0000 (16:03 -0400)
committerMatt Layher <mdlayher@gmail.com>
Mon, 16 Aug 2021 17:40:17 +0000 (17:40 +0000)
Calling Getsockname once to fetch the Pid field from the *SockaddrNetlink
is necessary, but this data will remain static for the rest of the netlink
socket's lifetime. Moving this call and type assertion outside of the inner
loops will remove a number of unnecessary system calls.

Change-Id: I7e7e81866af1a31fccdaaf7531efd6cc4cbb8926
Reviewed-on: https://go-review.googlesource.com/c/go/+/336369
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
src/syscall/netlink_linux.go

index 0937ff797ad4dcd3ae3323a8a71e1d75c6775d7a..2d810705bf1bcd38f162bf5a639244619af6a163 100644 (file)
@@ -55,14 +55,22 @@ func NetlinkRIB(proto, family int) ([]byte, error) {
                return nil, err
        }
        defer Close(s)
-       lsa := &SockaddrNetlink{Family: AF_NETLINK}
-       if err := Bind(s, lsa); err != nil {
+       sa := &SockaddrNetlink{Family: AF_NETLINK}
+       if err := Bind(s, sa); err != nil {
                return nil, err
        }
        wb := newNetlinkRouteRequest(proto, 1, family)
-       if err := Sendto(s, wb, 0, lsa); err != nil {
+       if err := Sendto(s, wb, 0, sa); err != nil {
                return nil, err
        }
+       lsa, err := Getsockname(s)
+       if err != nil {
+               return nil, err
+       }
+       lsanl, ok := lsa.(*SockaddrNetlink)
+       if !ok {
+               return nil, EINVAL
+       }
        var tab []byte
        rbNew := make([]byte, Getpagesize())
 done:
@@ -82,16 +90,7 @@ done:
                        return nil, err
                }
                for _, m := range msgs {
-                       lsa, err := Getsockname(s)
-                       if err != nil {
-                               return nil, err
-                       }
-                       switch v := lsa.(type) {
-                       case *SockaddrNetlink:
-                               if m.Header.Seq != 1 || m.Header.Pid != v.Pid {
-                                       return nil, EINVAL
-                               }
-                       default:
+                       if m.Header.Seq != 1 || m.Header.Pid != lsanl.Pid {
                                return nil, EINVAL
                        }
                        if m.Header.Type == NLMSG_DONE {