| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- //go:build !android && !e2e_testing
- // +build !android,!e2e_testing
- package udp
- import (
- "encoding/binary"
- "errors"
- "fmt"
- "net"
- "net/netip"
- "syscall"
- "unsafe"
- "github.com/rcrowley/go-metrics"
- "github.com/sirupsen/logrus"
- "github.com/slackhq/nebula/config"
- "golang.org/x/sys/unix"
- )
- type StdConn struct {
- sysFd int
- isV4 bool
- l *logrus.Logger
- batch int
- gsoSupported bool
- }
- func maybeIPV4(ip net.IP) (net.IP, bool) {
- ip4 := ip.To4()
- if ip4 != nil {
- return ip4, true
- }
- return ip, false
- }
- // supportsUDPOffload checks if the kernel supports UDP GSO (Generic Segmentation Offload)
- // by attempting to get the UDP_SEGMENT socket option.
- func supportsUDPOffload(fd int) bool {
- _, err := unix.GetsockoptInt(fd, unix.IPPROTO_UDP, unix.UDP_SEGMENT)
- return err == nil
- }
- const (
- // Maximum number of datagrams that can be coalesced with GSO
- udpSegmentMaxDatagrams = 64
- )
- // setGSOSize writes a UDP_SEGMENT control message to the provided buffer
- // with the given segment size. Returns the actual control message length.
- func setGSOSize(control []byte, gsoSize uint16) int {
- // Build the cmsghdr structure
- cmsgLen := unix.CmsgLen(2) // 2 bytes for uint16 segment size
- cmsg := (*unix.Cmsghdr)(unsafe.Pointer(&control[0]))
- cmsg.Level = unix.IPPROTO_UDP
- cmsg.Type = unix.UDP_SEGMENT
- cmsg.SetLen(cmsgLen)
- // Write the segment size after the header (after cmsghdr)
- binary.NativeEndian.PutUint16(control[unix.SizeofCmsghdr:], gsoSize)
- return unix.CmsgSpace(2) // aligned size
- }
- func NewListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
- af := unix.AF_INET6
- if ip.Is4() {
- af = unix.AF_INET
- }
- syscall.ForkLock.RLock()
- fd, err := unix.Socket(af, unix.SOCK_DGRAM, unix.IPPROTO_UDP)
- if err == nil {
- unix.CloseOnExec(fd)
- }
- syscall.ForkLock.RUnlock()
- if err != nil {
- unix.Close(fd)
- return nil, fmt.Errorf("unable to open socket: %s", err)
- }
- if multi {
- if err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
- return nil, fmt.Errorf("unable to set SO_REUSEPORT: %s", err)
- }
- }
- var sa unix.Sockaddr
- if ip.Is4() {
- sa4 := &unix.SockaddrInet4{Port: port}
- sa4.Addr = ip.As4()
- sa = sa4
- } else {
- sa6 := &unix.SockaddrInet6{Port: port}
- sa6.Addr = ip.As16()
- sa = sa6
- }
- if err = unix.Bind(fd, sa); err != nil {
- return nil, fmt.Errorf("unable to bind to socket: %s", err)
- }
- gsoSupported := supportsUDPOffload(fd)
- if gsoSupported {
- l.Info("UDP GSO offload is supported")
- }
- return &StdConn{sysFd: fd, isV4: ip.Is4(), l: l, batch: batch, gsoSupported: gsoSupported}, err
- }
- func (u *StdConn) SupportsMultipleReaders() bool {
- return true
- }
- func (u *StdConn) SupportsGSO() bool {
- return u.gsoSupported
- }
- func (u *StdConn) Rebind() error {
- return nil
- }
- func (u *StdConn) SetRecvBuffer(n int) error {
- return unix.SetsockoptInt(u.sysFd, unix.SOL_SOCKET, unix.SO_RCVBUFFORCE, n)
- }
- func (u *StdConn) SetSendBuffer(n int) error {
- return unix.SetsockoptInt(u.sysFd, unix.SOL_SOCKET, unix.SO_SNDBUFFORCE, n)
- }
- func (u *StdConn) SetSoMark(mark int) error {
- return unix.SetsockoptInt(u.sysFd, unix.SOL_SOCKET, unix.SO_MARK, mark)
- }
- func (u *StdConn) GetRecvBuffer() (int, error) {
- return unix.GetsockoptInt(int(u.sysFd), unix.SOL_SOCKET, unix.SO_RCVBUF)
- }
- func (u *StdConn) GetSendBuffer() (int, error) {
- return unix.GetsockoptInt(int(u.sysFd), unix.SOL_SOCKET, unix.SO_SNDBUF)
- }
- func (u *StdConn) GetSoMark() (int, error) {
- return unix.GetsockoptInt(int(u.sysFd), unix.SOL_SOCKET, unix.SO_MARK)
- }
- func (u *StdConn) LocalAddr() (netip.AddrPort, error) {
- sa, err := unix.Getsockname(u.sysFd)
- if err != nil {
- return netip.AddrPort{}, err
- }
- switch sa := sa.(type) {
- case *unix.SockaddrInet4:
- return netip.AddrPortFrom(netip.AddrFrom4(sa.Addr), uint16(sa.Port)), nil
- case *unix.SockaddrInet6:
- return netip.AddrPortFrom(netip.AddrFrom16(sa.Addr), uint16(sa.Port)), nil
- default:
- return netip.AddrPort{}, fmt.Errorf("unsupported sock type: %T", sa)
- }
- }
- func (u *StdConn) ListenOut(r EncReader) {
- var ip netip.Addr
- msgs, buffers, names := u.PrepareRawMessages(u.batch)
- read := u.ReadMulti
- if u.batch == 1 {
- read = u.ReadSingle
- }
- for {
- n, err := read(msgs)
- if err != nil {
- u.l.WithError(err).Debug("udp socket is closed, exiting read loop")
- return
- }
- for i := 0; i < n; i++ {
- // Its ok to skip the ok check here, the slicing is the only error that can occur and it will panic
- if u.isV4 {
- ip, _ = netip.AddrFromSlice(names[i][4:8])
- } else {
- ip, _ = netip.AddrFromSlice(names[i][8:24])
- }
- r(netip.AddrPortFrom(ip.Unmap(), binary.BigEndian.Uint16(names[i][2:4])), buffers[i][:msgs[i].Len])
- }
- }
- }
- func (u *StdConn) ReadSingle(msgs []rawMessage) (int, error) {
- for {
- n, _, err := unix.Syscall6(
- unix.SYS_RECVMSG,
- uintptr(u.sysFd),
- uintptr(unsafe.Pointer(&(msgs[0].Hdr))),
- 0,
- 0,
- 0,
- 0,
- )
- if err != 0 {
- return 0, &net.OpError{Op: "recvmsg", Err: err}
- }
- msgs[0].Len = uint32(n)
- return 1, nil
- }
- }
- func (u *StdConn) ReadMulti(msgs []rawMessage) (int, error) {
- for {
- n, _, err := unix.Syscall6(
- unix.SYS_RECVMMSG,
- uintptr(u.sysFd),
- uintptr(unsafe.Pointer(&msgs[0])),
- uintptr(len(msgs)),
- unix.MSG_WAITFORONE,
- 0,
- 0,
- )
- if err != 0 {
- return 0, &net.OpError{Op: "recvmmsg", Err: err}
- }
- return int(n), nil
- }
- }
- func (u *StdConn) WriteTo(b []byte, ip netip.AddrPort) error {
- if u.isV4 {
- return u.writeTo4(b, ip)
- }
- return u.writeTo6(b, ip)
- }
- func (u *StdConn) writeTo6(b []byte, ip netip.AddrPort) error {
- var rsa unix.RawSockaddrInet6
- rsa.Family = unix.AF_INET6
- rsa.Addr = ip.Addr().As16()
- binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&rsa.Port))[:], ip.Port())
- for {
- _, _, err := unix.Syscall6(
- unix.SYS_SENDTO,
- uintptr(u.sysFd),
- uintptr(unsafe.Pointer(&b[0])),
- uintptr(len(b)),
- uintptr(0),
- uintptr(unsafe.Pointer(&rsa)),
- uintptr(unix.SizeofSockaddrInet6),
- )
- if err != 0 {
- return &net.OpError{Op: "sendto", Err: err}
- }
- return nil
- }
- }
- func (u *StdConn) writeTo4(b []byte, ip netip.AddrPort) error {
- if !ip.Addr().Is4() {
- return ErrInvalidIPv6RemoteForSocket
- }
- var rsa unix.RawSockaddrInet4
- rsa.Family = unix.AF_INET
- rsa.Addr = ip.Addr().As4()
- binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&rsa.Port))[:], ip.Port())
- for {
- _, _, err := unix.Syscall6(
- unix.SYS_SENDTO,
- uintptr(u.sysFd),
- uintptr(unsafe.Pointer(&b[0])),
- uintptr(len(b)),
- uintptr(0),
- uintptr(unsafe.Pointer(&rsa)),
- uintptr(unix.SizeofSockaddrInet4),
- )
- if err != 0 {
- return &net.OpError{Op: "sendto", Err: err}
- }
- return nil
- }
- }
- func (u *StdConn) ReloadConfig(c *config.C) {
- b := c.GetInt("listen.read_buffer", 0)
- if b > 0 {
- err := u.SetRecvBuffer(b)
- if err == nil {
- s, err := u.GetRecvBuffer()
- if err == nil {
- u.l.WithField("size", s).Info("listen.read_buffer was set")
- } else {
- u.l.WithError(err).Warn("Failed to get listen.read_buffer")
- }
- } else {
- u.l.WithError(err).Error("Failed to set listen.read_buffer")
- }
- }
- b = c.GetInt("listen.write_buffer", 0)
- if b > 0 {
- err := u.SetSendBuffer(b)
- if err == nil {
- s, err := u.GetSendBuffer()
- if err == nil {
- u.l.WithField("size", s).Info("listen.write_buffer was set")
- } else {
- u.l.WithError(err).Warn("Failed to get listen.write_buffer")
- }
- } else {
- u.l.WithError(err).Error("Failed to set listen.write_buffer")
- }
- }
- b = c.GetInt("listen.so_mark", 0)
- s, err := u.GetSoMark()
- if b > 0 || (err == nil && s != 0) {
- err := u.SetSoMark(b)
- if err == nil {
- s, err := u.GetSoMark()
- if err == nil {
- u.l.WithField("mark", s).Info("listen.so_mark was set")
- } else {
- u.l.WithError(err).Warn("Failed to get listen.so_mark")
- }
- } else {
- u.l.WithError(err).Error("Failed to set listen.so_mark")
- }
- }
- }
- func (u *StdConn) getMemInfo(meminfo *[unix.SK_MEMINFO_VARS]uint32) error {
- var vallen uint32 = 4 * unix.SK_MEMINFO_VARS
- _, _, err := unix.Syscall6(unix.SYS_GETSOCKOPT, uintptr(u.sysFd), uintptr(unix.SOL_SOCKET), uintptr(unix.SO_MEMINFO), uintptr(unsafe.Pointer(meminfo)), uintptr(unsafe.Pointer(&vallen)), 0)
- if err != 0 {
- return err
- }
- return nil
- }
- func (u *StdConn) Close() error {
- return syscall.Close(u.sysFd)
- }
- func (u *StdConn) WriteBatch(pkts []BatchPacket) (int, error) {
- if len(pkts) == 0 {
- return 0, nil
- }
- // If GSO is supported, try to coalesce packets to the same destination
- if u.gsoSupported {
- return u.writeBatchGSO(pkts)
- }
- return u.writeBatchSendmmsg(pkts)
- }
- // writeBatchSendmmsg sends packets using sendmmsg without GSO coalescing
- func (u *StdConn) writeBatchSendmmsg(pkts []BatchPacket) (int, error) {
- msgs := make([]rawMessage, len(pkts))
- iovecs := make([]iovec, len(pkts))
- var names4 []unix.RawSockaddrInet4
- var names6 []unix.RawSockaddrInet6
- if u.isV4 {
- names4 = make([]unix.RawSockaddrInet4, len(pkts))
- } else {
- names6 = make([]unix.RawSockaddrInet6, len(pkts))
- }
- for i := range pkts {
- setIovecBase(&iovecs[i], &pkts[i].Payload[0])
- setIovecLen(&iovecs[i], len(pkts[i].Payload))
- msgs[i].Hdr.Iov = &iovecs[i]
- setMsghdrIovlen(&msgs[i].Hdr, 1)
- if u.isV4 {
- names4[i].Family = unix.AF_INET
- names4[i].Addr = pkts[i].Addr.Addr().As4()
- binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&names4[i].Port))[:], pkts[i].Addr.Port())
- msgs[i].Hdr.Name = (*byte)(unsafe.Pointer(&names4[i]))
- msgs[i].Hdr.Namelen = unix.SizeofSockaddrInet4
- } else {
- names6[i].Family = unix.AF_INET6
- names6[i].Addr = pkts[i].Addr.Addr().As16()
- binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&names6[i].Port))[:], pkts[i].Addr.Port())
- msgs[i].Hdr.Name = (*byte)(unsafe.Pointer(&names6[i]))
- msgs[i].Hdr.Namelen = unix.SizeofSockaddrInet6
- }
- }
- var sent int
- for sent < len(msgs) {
- n, _, errno := unix.Syscall6(
- unix.SYS_SENDMMSG,
- uintptr(u.sysFd),
- uintptr(unsafe.Pointer(&msgs[sent])),
- uintptr(len(msgs)-sent),
- 0,
- 0,
- 0,
- )
- if errno == unix.EINTR {
- continue
- }
- if errno != 0 {
- return sent, &net.OpError{Op: "sendmmsg", Err: errno}
- }
- sent += int(n)
- }
- return sent, nil
- }
- // writeBatchGSO sends packets using GSO coalescing when possible.
- // Packets to the same destination with the same size are coalesced into a single
- // GSO message. Mixed destinations or sizes fall back to individual sendmmsg calls.
- func (u *StdConn) writeBatchGSO(pkts []BatchPacket) (int, error) {
- // Group packets by destination and try to coalesce
- totalSent := 0
- i := 0
- for i < len(pkts) {
- // Find a run of packets to the same destination with compatible sizes
- startIdx := i
- dst := pkts[i].Addr
- segmentSize := len(pkts[i].Payload)
- // Count how many packets we can coalesce (same destination, same size except possibly last)
- coalescedCount := 1
- totalSize := segmentSize
- for i+coalescedCount < len(pkts) && coalescedCount < udpSegmentMaxDatagrams {
- next := pkts[i+coalescedCount]
- if next.Addr != dst {
- break
- }
- nextSize := len(next.Payload)
- // For GSO, all packets except the last must have the same size
- // The last packet can be smaller (but not larger)
- if nextSize != segmentSize {
- // Check if this could be the last packet (smaller is ok)
- if nextSize < segmentSize && i+coalescedCount == len(pkts)-1 {
- coalescedCount++
- totalSize += nextSize
- }
- break
- }
- coalescedCount++
- totalSize += nextSize
- }
- // If we have multiple packets to coalesce, use GSO
- if coalescedCount > 1 {
- err := u.sendGSO(pkts[startIdx:startIdx+coalescedCount], dst, segmentSize, totalSize)
- if err != nil {
- // If GSO fails (e.g., EIO due to NIC not supporting checksum offload),
- // disable GSO and fall back to sendmmsg for the rest
- if isGSOError(err) {
- u.l.WithError(err).Warn("GSO send failed, disabling GSO for this connection")
- u.gsoSupported = false
- // Send remaining packets with sendmmsg
- remaining, rerr := u.writeBatchSendmmsg(pkts[startIdx:])
- return totalSent + remaining, rerr
- }
- return totalSent, err
- }
- totalSent += coalescedCount
- i += coalescedCount
- } else {
- // Single packet, send without GSO overhead
- err := u.WriteTo(pkts[i].Payload, pkts[i].Addr)
- if err != nil {
- return totalSent, err
- }
- totalSent++
- i++
- }
- }
- return totalSent, nil
- }
- // sendGSO sends coalesced packets using UDP GSO
- func (u *StdConn) sendGSO(pkts []BatchPacket, dst netip.AddrPort, segmentSize, totalSize int) error {
- // Allocate a buffer large enough for all packet payloads
- coalescedBuf := make([]byte, totalSize)
- offset := 0
- for _, pkt := range pkts {
- copy(coalescedBuf[offset:], pkt.Payload)
- offset += len(pkt.Payload)
- }
- // Prepare control message with GSO segment size
- control := make([]byte, unix.CmsgSpace(2))
- controlLen := setGSOSize(control, uint16(segmentSize))
- // Prepare the iovec
- iov := iovec{}
- setIovecBase(&iov, &coalescedBuf[0])
- setIovecLen(&iov, totalSize)
- // Prepare the msghdr
- var hdr msghdr
- hdr.Iov = &iov
- setMsghdrIovlen(&hdr, 1)
- hdr.Control = &control[0]
- setMsghdrControllen(&hdr, controlLen)
- // Set destination address
- if u.isV4 {
- var rsa unix.RawSockaddrInet4
- rsa.Family = unix.AF_INET
- rsa.Addr = dst.Addr().As4()
- binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&rsa.Port))[:], dst.Port())
- hdr.Name = (*byte)(unsafe.Pointer(&rsa))
- hdr.Namelen = unix.SizeofSockaddrInet4
- } else {
- var rsa unix.RawSockaddrInet6
- rsa.Family = unix.AF_INET6
- rsa.Addr = dst.Addr().As16()
- binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&rsa.Port))[:], dst.Port())
- hdr.Name = (*byte)(unsafe.Pointer(&rsa))
- hdr.Namelen = unix.SizeofSockaddrInet6
- }
- for {
- _, _, errno := unix.Syscall6(
- unix.SYS_SENDMSG,
- uintptr(u.sysFd),
- uintptr(unsafe.Pointer(&hdr)),
- 0,
- 0,
- 0,
- 0,
- )
- if errno == unix.EINTR {
- continue
- }
- if errno != 0 {
- return &net.OpError{Op: "sendmsg", Err: errno}
- }
- return nil
- }
- }
- // isGSOError returns true if the error indicates GSO is not supported by the NIC
- func isGSOError(err error) bool {
- var opErr *net.OpError
- if !errors.As(err, &opErr) {
- return false
- }
- // EIO typically means the NIC doesn't support checksum offload required for GSO
- return errors.Is(opErr.Err, unix.EIO)
- }
- func NewUDPStatsEmitter(udpConns []Conn) func() {
- // Check if our kernel supports SO_MEMINFO before registering the gauges
- var udpGauges [][unix.SK_MEMINFO_VARS]metrics.Gauge
- var meminfo [unix.SK_MEMINFO_VARS]uint32
- if err := udpConns[0].(*StdConn).getMemInfo(&meminfo); err == nil {
- udpGauges = make([][unix.SK_MEMINFO_VARS]metrics.Gauge, len(udpConns))
- for i := range udpConns {
- udpGauges[i] = [unix.SK_MEMINFO_VARS]metrics.Gauge{
- metrics.GetOrRegisterGauge(fmt.Sprintf("udp.%d.rmem_alloc", i), nil),
- metrics.GetOrRegisterGauge(fmt.Sprintf("udp.%d.rcvbuf", i), nil),
- metrics.GetOrRegisterGauge(fmt.Sprintf("udp.%d.wmem_alloc", i), nil),
- metrics.GetOrRegisterGauge(fmt.Sprintf("udp.%d.sndbuf", i), nil),
- metrics.GetOrRegisterGauge(fmt.Sprintf("udp.%d.fwd_alloc", i), nil),
- metrics.GetOrRegisterGauge(fmt.Sprintf("udp.%d.wmem_queued", i), nil),
- metrics.GetOrRegisterGauge(fmt.Sprintf("udp.%d.optmem", i), nil),
- metrics.GetOrRegisterGauge(fmt.Sprintf("udp.%d.backlog", i), nil),
- metrics.GetOrRegisterGauge(fmt.Sprintf("udp.%d.drops", i), nil),
- }
- }
- }
- return func() {
- for i, gauges := range udpGauges {
- if err := udpConns[i].(*StdConn).getMemInfo(&meminfo); err == nil {
- for j := 0; j < unix.SK_MEMINFO_VARS; j++ {
- gauges[j].Update(int64(meminfo[j]))
- }
- }
- }
- }
- }
|