mirror of
https://github.com/slackhq/nebula.git
synced 2025-11-22 16:34:25 +01:00
Compare commits
3 Commits
channels-s
...
channels-s
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2bb43fb42 | ||
|
|
7999b62147 | ||
|
|
2ab75709ad |
191
cmd/gso/gso.go
Normal file
191
cmd/gso/gso.go
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// UDP_SEGMENT enables GSO segmentation
|
||||||
|
UDP_SEGMENT = 103
|
||||||
|
// Maximum GSO segment size (typical MTU - headers)
|
||||||
|
maxGSOSize = 1400
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
destAddr := flag.String("dest", "10.4.0.16:4202", "Destination address")
|
||||||
|
gsoSize := flag.Int("gso", 1400, "GSO segment size")
|
||||||
|
totalSize := flag.Int("size", 14000, "Total payload size to send")
|
||||||
|
count := flag.Int("count", 1, "Number of packets to send")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if *gsoSize > maxGSOSize {
|
||||||
|
log.Fatalf("GSO size %d exceeds maximum %d", *gsoSize, maxGSOSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve destination address
|
||||||
|
_, err := net.ResolveUDPAddr("udp", *destAddr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to resolve address: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a raw UDP socket with GSO support
|
||||||
|
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_UDP)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create socket: %v", err)
|
||||||
|
}
|
||||||
|
defer unix.Close(fd)
|
||||||
|
|
||||||
|
// Bind to a local address
|
||||||
|
localAddr := &unix.SockaddrInet4{
|
||||||
|
Port: 0, // Let the system choose a port
|
||||||
|
}
|
||||||
|
if err := unix.Bind(fd, localAddr); err != nil {
|
||||||
|
log.Fatalf("Failed to bind socket: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Sending UDP packets with GSO enabled\n")
|
||||||
|
fmt.Printf("Destination: %s\n", *destAddr)
|
||||||
|
fmt.Printf("GSO segment size: %d bytes\n", *gsoSize)
|
||||||
|
fmt.Printf("Total payload size: %d bytes\n", *totalSize)
|
||||||
|
fmt.Printf("Number of packets: %d\n\n", *count)
|
||||||
|
|
||||||
|
// Create payload
|
||||||
|
payload := make([]byte, *totalSize)
|
||||||
|
for i := range payload {
|
||||||
|
payload[i] = byte(i % 256)
|
||||||
|
}
|
||||||
|
|
||||||
|
dest := netip.MustParseAddrPort(*destAddr)
|
||||||
|
|
||||||
|
//if err := unix.SetsockoptInt(fd, unix.SOL_UDP, unix.UDP_SEGMENT, 1400); err != nil {
|
||||||
|
// panic(err)
|
||||||
|
//}
|
||||||
|
|
||||||
|
for i := 0; i < *count; i++ {
|
||||||
|
err := WriteBatch(fd, payload, dest, uint16(*gsoSize), true)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Send error on packet %d: %v", i, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i+1)%100 == 0 || i == *count-1 {
|
||||||
|
fmt.Printf("Sent %d packets\n", i+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("now, let's send without the correct ctrl header\n")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
for i := 0; i < *count; i++ {
|
||||||
|
err := WriteBatch(fd, payload, dest, uint16(*gsoSize), false)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Send error on packet %d: %v", i, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i+1)%100 == 0 || i == *count-1 {
|
||||||
|
fmt.Printf("Sent %d packets\n", i+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteBatch(fd int, payload []byte, addr netip.AddrPort, segSize uint16, withHeader bool) error {
|
||||||
|
msgs := make([]rawMessage, 0, 1)
|
||||||
|
iovs := make([]iovec, 0, 1)
|
||||||
|
names := make([][unix.SizeofSockaddrInet6]byte, 0, 1)
|
||||||
|
|
||||||
|
sent := 0
|
||||||
|
|
||||||
|
pkts := []BatchPacket{
|
||||||
|
{
|
||||||
|
Payload: payload,
|
||||||
|
Addr: addr,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pkt := range pkts {
|
||||||
|
if len(pkt.Payload) == 0 {
|
||||||
|
sent++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
msgs = append(msgs, rawMessage{})
|
||||||
|
iovs = append(iovs, iovec{})
|
||||||
|
names = append(names, [unix.SizeofSockaddrInet6]byte{})
|
||||||
|
|
||||||
|
idx := len(msgs) - 1
|
||||||
|
msg := &msgs[idx]
|
||||||
|
iov := &iovs[idx]
|
||||||
|
name := &names[idx]
|
||||||
|
|
||||||
|
setIovecSlice(iov, pkt.Payload)
|
||||||
|
msg.Hdr.Iov = iov
|
||||||
|
msg.Hdr.Iovlen = 1
|
||||||
|
|
||||||
|
if withHeader {
|
||||||
|
setRawMessageControl(msg, buildGSOControlMessage(segSize)) //
|
||||||
|
} else {
|
||||||
|
setRawMessageControl(msg, nil) //
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.Hdr.Flags = 0
|
||||||
|
|
||||||
|
nameLen, err := encodeSockaddr(name[:], pkt.Addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
msg.Hdr.Name = &name[0]
|
||||||
|
msg.Hdr.Namelen = nameLen
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(msgs) == 0 {
|
||||||
|
return errors.New("nothing to write")
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := 0
|
||||||
|
for offset < len(msgs) {
|
||||||
|
n, _, errno := unix.Syscall6(
|
||||||
|
unix.SYS_SENDMMSG,
|
||||||
|
uintptr(fd),
|
||||||
|
uintptr(unsafe.Pointer(&msgs[offset])),
|
||||||
|
uintptr(len(msgs)-offset),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
|
||||||
|
if errno != 0 {
|
||||||
|
if errno == unix.EINTR {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return &net.OpError{Op: "sendmmsg", Err: errno}
|
||||||
|
}
|
||||||
|
|
||||||
|
if n == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
offset += int(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildGSOControlMessage(segSize uint16) []byte {
|
||||||
|
control := make([]byte, unix.CmsgSpace(2))
|
||||||
|
hdr := (*unix.Cmsghdr)(unsafe.Pointer(&control[0]))
|
||||||
|
hdr.Level = unix.SOL_UDP
|
||||||
|
hdr.Type = unix.UDP_SEGMENT
|
||||||
|
setCmsgLen(hdr, unix.CmsgLen(2))
|
||||||
|
binary.NativeEndian.PutUint16(control[unix.CmsgLen(0):unix.CmsgLen(0)+2], uint16(segSize))
|
||||||
|
|
||||||
|
return control
|
||||||
|
}
|
||||||
85
cmd/gso/helper.go
Normal file
85
cmd/gso/helper.go
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
type iovec struct {
|
||||||
|
Base *byte
|
||||||
|
Len uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
type msghdr struct {
|
||||||
|
Name *byte
|
||||||
|
Namelen uint32
|
||||||
|
Pad0 [4]byte
|
||||||
|
Iov *iovec
|
||||||
|
Iovlen uint64
|
||||||
|
Control *byte
|
||||||
|
Controllen uint64
|
||||||
|
Flags int32
|
||||||
|
Pad1 [4]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type rawMessage struct {
|
||||||
|
Hdr msghdr
|
||||||
|
Len uint32
|
||||||
|
Pad0 [4]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type BatchPacket struct {
|
||||||
|
Payload []byte
|
||||||
|
Addr netip.AddrPort
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeSockaddr(dst []byte, addr netip.AddrPort) (uint32, error) {
|
||||||
|
if addr.Addr().Is4() {
|
||||||
|
if !addr.Addr().Is4() {
|
||||||
|
return 0, fmt.Errorf("Listener is IPv4, but writing to IPv6 remote")
|
||||||
|
}
|
||||||
|
var sa unix.RawSockaddrInet4
|
||||||
|
sa.Family = unix.AF_INET
|
||||||
|
sa.Addr = addr.Addr().As4()
|
||||||
|
binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&sa.Port))[:], addr.Port())
|
||||||
|
size := unix.SizeofSockaddrInet4
|
||||||
|
copy(dst[:size], (*(*[unix.SizeofSockaddrInet4]byte)(unsafe.Pointer(&sa)))[:])
|
||||||
|
return uint32(size), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var sa unix.RawSockaddrInet6
|
||||||
|
sa.Family = unix.AF_INET6
|
||||||
|
sa.Addr = addr.Addr().As16()
|
||||||
|
binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&sa.Port))[:], addr.Port())
|
||||||
|
size := unix.SizeofSockaddrInet6
|
||||||
|
copy(dst[:size], (*(*[unix.SizeofSockaddrInet6]byte)(unsafe.Pointer(&sa)))[:])
|
||||||
|
return uint32(size), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setRawMessageControl(msg *rawMessage, buf []byte) {
|
||||||
|
if len(buf) == 0 {
|
||||||
|
msg.Hdr.Control = nil
|
||||||
|
msg.Hdr.Controllen = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msg.Hdr.Control = &buf[0]
|
||||||
|
msg.Hdr.Controllen = uint64(len(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
func setCmsgLen(h *unix.Cmsghdr, l int) {
|
||||||
|
h.Len = uint64(l)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setIovecSlice(iov *iovec, b []byte) {
|
||||||
|
if len(b) == 0 {
|
||||||
|
iov.Base = nil
|
||||||
|
iov.Len = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
iov.Base = &b[0]
|
||||||
|
iov.Len = uint64(len(b))
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
// TODO: In a 5Gbps test, 1024 is not sufficient. With a 1400 MTU this is about 1.4Gbps of window, assuming full packets.
|
// TODO: In a 5Gbps test, 1024 is not sufficient. With a 1400 MTU this is about 1.4Gbps of window, assuming full packets.
|
||||||
// 4092 should be sufficient for 5Gbps
|
// 4092 should be sufficient for 5Gbps
|
||||||
const ReplayWindow = 1024
|
const ReplayWindow = 8192
|
||||||
|
|
||||||
type ConnectionState struct {
|
type ConnectionState struct {
|
||||||
eKey *NebulaCipherState
|
eKey *NebulaCipherState
|
||||||
|
|||||||
74
interface.go
74
interface.go
@@ -96,11 +96,9 @@ type Interface struct {
|
|||||||
|
|
||||||
l *logrus.Logger
|
l *logrus.Logger
|
||||||
|
|
||||||
inPool sync.Pool
|
pktPool *packet.Pool
|
||||||
inbound chan *packet.Packet
|
inbound chan *packet.Packet
|
||||||
|
outbound chan *packet.Packet
|
||||||
outPool sync.Pool
|
|
||||||
outbound chan *[]byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EncWriter interface {
|
type EncWriter interface {
|
||||||
@@ -203,20 +201,13 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
//TODO: configurable size
|
//TODO: configurable size
|
||||||
inbound: make(chan *packet.Packet, 1028),
|
inbound: make(chan *packet.Packet, 2048),
|
||||||
outbound: make(chan *[]byte, 1028),
|
outbound: make(chan *packet.Packet, 2048),
|
||||||
|
|
||||||
l: c.l,
|
l: c.l,
|
||||||
}
|
}
|
||||||
|
|
||||||
ifce.inPool = sync.Pool{New: func() any {
|
ifce.pktPool = packet.GetPool()
|
||||||
return packet.New()
|
|
||||||
}}
|
|
||||||
|
|
||||||
ifce.outPool = sync.Pool{New: func() any {
|
|
||||||
t := make([]byte, mtu)
|
|
||||||
return &t
|
|
||||||
}}
|
|
||||||
|
|
||||||
ifce.tryPromoteEvery.Store(c.tryPromoteEvery)
|
ifce.tryPromoteEvery.Store(c.tryPromoteEvery)
|
||||||
ifce.reQueryEvery.Store(c.reQueryEvery)
|
ifce.reQueryEvery.Store(c.reQueryEvery)
|
||||||
@@ -267,19 +258,21 @@ func (f *Interface) activate() error {
|
|||||||
|
|
||||||
func (f *Interface) run(c context.Context) (func(), error) {
|
func (f *Interface) run(c context.Context) (func(), error) {
|
||||||
for i := 0; i < f.routines; i++ {
|
for i := 0; i < f.routines; i++ {
|
||||||
// Launch n queues to read packets from udp
|
// read packets from udp and queue to f.inbound
|
||||||
f.wg.Add(1)
|
f.wg.Add(1)
|
||||||
go f.listenOut(i)
|
go f.listenOut(i)
|
||||||
|
|
||||||
// Launch n queues to read packets from tun dev
|
// Launch n queues to read packets from inside tun dev and queue to f.outbound
|
||||||
f.wg.Add(1)
|
//todo this never stops f.wg.Add(1)
|
||||||
go f.listenIn(f.readers[i], i)
|
go f.listenIn(f.readers[i], i)
|
||||||
|
|
||||||
// Launch n queues to read packets from tun dev
|
// Launch n workers to process traffic from f.inbound and smash it onto the inside of the tun
|
||||||
|
f.wg.Add(1)
|
||||||
|
go f.workerIn(i, c)
|
||||||
f.wg.Add(1)
|
f.wg.Add(1)
|
||||||
go f.workerIn(i, c)
|
go f.workerIn(i, c)
|
||||||
|
|
||||||
// Launch n queues to read packets from tun dev
|
// read from f.outbound and write to UDP (outside the tun)
|
||||||
f.wg.Add(1)
|
f.wg.Add(1)
|
||||||
go f.workerOut(i, c)
|
go f.workerOut(i, c)
|
||||||
}
|
}
|
||||||
@@ -296,22 +289,7 @@ func (f *Interface) listenOut(i int) {
|
|||||||
li = f.outside
|
li = f.outside
|
||||||
}
|
}
|
||||||
|
|
||||||
err := li.ListenOut(func(fromUdpAddr netip.AddrPort, payload []byte) {
|
err := li.ListenOut(f.pktPool.Get, f.inbound)
|
||||||
p := f.inPool.Get().(*packet.Packet)
|
|
||||||
//TODO: have the listener store this in the msgs array after a read instead of doing a copy
|
|
||||||
|
|
||||||
p.Payload = p.Payload[:mtu]
|
|
||||||
copy(p.Payload, payload)
|
|
||||||
p.Payload = p.Payload[:len(payload)]
|
|
||||||
p.Addr = fromUdpAddr
|
|
||||||
f.inbound <- p
|
|
||||||
//select {
|
|
||||||
//case f.inbound <- p:
|
|
||||||
//default:
|
|
||||||
// f.l.Error("Dropped packet from inbound channel")
|
|
||||||
//}
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil && !f.closed.Load() {
|
if err != nil && !f.closed.Load() {
|
||||||
f.l.WithError(err).Error("Error while reading packet inbound packet, closing")
|
f.l.WithError(err).Error("Error while reading packet inbound packet, closing")
|
||||||
//TODO: Trigger Control to close
|
//TODO: Trigger Control to close
|
||||||
@@ -325,9 +303,8 @@ func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
|
|||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
p := f.outPool.Get().(*[]byte)
|
p := f.pktPool.Get()
|
||||||
*p = (*p)[:mtu]
|
n, err := reader.Read(p.Payload)
|
||||||
n, err := reader.Read(*p)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !f.closed.Load() {
|
if !f.closed.Load() {
|
||||||
f.l.WithError(err).Error("Error while reading outbound packet, closing")
|
f.l.WithError(err).Error("Error while reading outbound packet, closing")
|
||||||
@@ -336,7 +313,7 @@ func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
*p = (*p)[:n]
|
p.Payload = (p.Payload)[:n]
|
||||||
//TODO: nonblocking channel write
|
//TODO: nonblocking channel write
|
||||||
f.outbound <- p
|
f.outbound <- p
|
||||||
//select {
|
//select {
|
||||||
@@ -361,9 +338,19 @@ func (f *Interface) workerIn(i int, ctx context.Context) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case p := <-f.inbound:
|
case p := <-f.inbound:
|
||||||
|
if p.SegSize > 0 && p.SegSize < len(p.Payload) {
|
||||||
|
for offset := 0; offset < len(p.Payload); offset += p.SegSize {
|
||||||
|
end := offset + p.SegSize
|
||||||
|
if end > len(p.Payload) {
|
||||||
|
end = len(p.Payload)
|
||||||
|
}
|
||||||
|
f.readOutsidePackets(p.Addr, nil, result2[:0], p.Payload[offset:end], h, fwPacket2, lhh, nb2, i, conntrackCache.Get(f.l))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
f.readOutsidePackets(p.Addr, nil, result2[:0], p.Payload, h, fwPacket2, lhh, nb2, i, conntrackCache.Get(f.l))
|
f.readOutsidePackets(p.Addr, nil, result2[:0], p.Payload, h, fwPacket2, lhh, nb2, i, conntrackCache.Get(f.l))
|
||||||
p.Payload = p.Payload[:mtu]
|
}
|
||||||
f.inPool.Put(p)
|
|
||||||
|
f.pktPool.Put(p)
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
f.wg.Done()
|
f.wg.Done()
|
||||||
return
|
return
|
||||||
@@ -380,9 +367,8 @@ func (f *Interface) workerOut(i int, ctx context.Context) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case data := <-f.outbound:
|
case data := <-f.outbound:
|
||||||
f.consumeInsidePacket(*data, fwPacket1, nb1, result1, i, conntrackCache.Get(f.l))
|
f.consumeInsidePacket(data.Payload, fwPacket1, nb1, result1, i, conntrackCache.Get(f.l))
|
||||||
*data = (*data)[:mtu]
|
f.pktPool.Put(data)
|
||||||
f.outPool.Put(data)
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
f.wg.Done()
|
f.wg.Done()
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,12 +1,45 @@
|
|||||||
package packet
|
package packet
|
||||||
|
|
||||||
import "net/netip"
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
const Size = 0xffff
|
||||||
|
|
||||||
type Packet struct {
|
type Packet struct {
|
||||||
Payload []byte
|
Payload []byte
|
||||||
|
Control []byte
|
||||||
|
SegSize int
|
||||||
Addr netip.AddrPort
|
Addr netip.AddrPort
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Packet {
|
func New() *Packet {
|
||||||
return &Packet{Payload: make([]byte, 9001)}
|
return &Packet{
|
||||||
|
Payload: make([]byte, Size),
|
||||||
|
Control: make([]byte, unix.CmsgSpace(2)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Pool struct {
|
||||||
|
pool sync.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
var bigPool = &Pool{
|
||||||
|
pool: sync.Pool{New: func() any { return New() }},
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPool() *Pool {
|
||||||
|
return bigPool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pool) Get() *Packet {
|
||||||
|
return p.pool.Get().(*Packet)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pool) Put(x *Packet) {
|
||||||
|
x.Payload = x.Payload[:Size]
|
||||||
|
p.pool.Put(x)
|
||||||
}
|
}
|
||||||
|
|||||||
10
udp/conn.go
10
udp/conn.go
@@ -4,19 +4,19 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/packet"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MTU = 9001
|
const MTU = 9001
|
||||||
|
|
||||||
type EncReader func(
|
type EncReader func(*packet.Packet)
|
||||||
addr netip.AddrPort,
|
|
||||||
payload []byte,
|
type PacketBufferGetter func() *packet.Packet
|
||||||
)
|
|
||||||
|
|
||||||
type Conn interface {
|
type Conn interface {
|
||||||
Rebind() error
|
Rebind() error
|
||||||
LocalAddr() (netip.AddrPort, error)
|
LocalAddr() (netip.AddrPort, error)
|
||||||
ListenOut(r EncReader) error
|
ListenOut(pg PacketBufferGetter, pc chan *packet.Packet) error
|
||||||
WriteTo(b []byte, addr netip.AddrPort) error
|
WriteTo(b []byte, addr netip.AddrPort) error
|
||||||
ReloadConfig(c *config.C)
|
ReloadConfig(c *config.C)
|
||||||
Close() error
|
Close() error
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/rcrowley/go-metrics"
|
"github.com/rcrowley/go-metrics"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/packet"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,6 +26,9 @@ type StdConn struct {
|
|||||||
isV4 bool
|
isV4 bool
|
||||||
l *logrus.Logger
|
l *logrus.Logger
|
||||||
batch int
|
batch int
|
||||||
|
enableGRO bool
|
||||||
|
enableGSO bool
|
||||||
|
//gso gsoState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
|
func NewListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
|
||||||
@@ -118,10 +122,10 @@ func (u *StdConn) LocalAddr() (netip.AddrPort, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) ListenOut(r EncReader) error {
|
func (u *StdConn) ListenOut(pg PacketBufferGetter, pc chan *packet.Packet) error {
|
||||||
var ip netip.Addr
|
var ip netip.Addr
|
||||||
|
|
||||||
msgs, buffers, names := u.PrepareRawMessages(u.batch)
|
msgs, packets, names := u.PrepareRawMessages(u.batch, pg)
|
||||||
read := u.ReadMulti
|
read := u.ReadMulti
|
||||||
if u.batch == 1 {
|
if u.batch == 1 {
|
||||||
read = u.ReadSingle
|
read = u.ReadSingle
|
||||||
@@ -134,17 +138,57 @@ func (u *StdConn) ListenOut(r EncReader) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
|
out := packets[i]
|
||||||
|
out.Payload = out.Payload[:msgs[i].Len]
|
||||||
|
|
||||||
// Its ok to skip the ok check here, the slicing is the only error that can occur and it will panic
|
// Its ok to skip the ok check here, the slicing is the only error that can occur and it will panic
|
||||||
if u.isV4 {
|
if u.isV4 {
|
||||||
ip, _ = netip.AddrFromSlice(names[i][4:8])
|
ip, _ = netip.AddrFromSlice(names[i][4:8])
|
||||||
} else {
|
} else {
|
||||||
ip, _ = netip.AddrFromSlice(names[i][8:24])
|
ip, _ = netip.AddrFromSlice(names[i][8:24])
|
||||||
}
|
}
|
||||||
r(netip.AddrPortFrom(ip.Unmap(), binary.BigEndian.Uint16(names[i][2:4])), buffers[i][:msgs[i].Len])
|
out.Addr = netip.AddrPortFrom(ip.Unmap(), binary.BigEndian.Uint16(names[i][2:4]))
|
||||||
|
ctrlLen := getRawMessageControlLen(&msgs[i])
|
||||||
|
if ctrlLen > 0 {
|
||||||
|
packets[i].SegSize = parseGROControl(packets[i].Control[:ctrlLen])
|
||||||
|
} else {
|
||||||
|
packets[i].SegSize = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
pc <- out
|
||||||
|
|
||||||
|
//rotate this packet out so we don't overwrite it
|
||||||
|
packets[i] = pg()
|
||||||
|
msgs[i].Hdr.Iov.Base = &packets[i].Payload[0]
|
||||||
|
if u.enableGRO {
|
||||||
|
msgs[i].Hdr.Control = &packets[i].Control[0]
|
||||||
|
msgs[i].Hdr.Controllen = uint64(cap(packets[i].Control))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseGROControl(control []byte) int {
|
||||||
|
if len(control) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
cmsgs, err := unix.ParseSocketControlMessage(control)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cmsgs {
|
||||||
|
if c.Header.Level == unix.SOL_UDP && c.Header.Type == unix.UDP_GRO && len(c.Data) >= 2 {
|
||||||
|
segSize := int(binary.LittleEndian.Uint16(c.Data[:2]))
|
||||||
|
return segSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func (u *StdConn) ReadSingle(msgs []rawMessage) (int, error) {
|
func (u *StdConn) ReadSingle(msgs []rawMessage) (int, error) {
|
||||||
for {
|
for {
|
||||||
n, _, err := unix.Syscall6(
|
n, _, err := unix.Syscall6(
|
||||||
@@ -299,6 +343,28 @@ func (u *StdConn) ReloadConfig(c *config.C) {
|
|||||||
u.l.WithError(err).Error("Failed to set listen.so_mark")
|
u.l.WithError(err).Error("Failed to set listen.so_mark")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
u.configureGRO(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *StdConn) configureGRO(enable bool) {
|
||||||
|
if enable == u.enableGRO {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if enable {
|
||||||
|
if err := unix.SetsockoptInt(u.sysFd, unix.SOL_UDP, unix.UDP_GRO, 1); err != nil {
|
||||||
|
u.l.WithError(err).Warn("Failed to enable UDP GRO")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u.enableGRO = true
|
||||||
|
u.l.Info("UDP GRO enabled")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := unix.SetsockoptInt(u.sysFd, unix.SOL_UDP, unix.UDP_GRO, 0); err != nil && err != unix.ENOPROTOOPT {
|
||||||
|
u.l.WithError(err).Warn("Failed to disable UDP GRO")
|
||||||
|
}
|
||||||
|
u.enableGRO = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) getMemInfo(meminfo *[unix.SK_MEMINFO_VARS]uint32) error {
|
func (u *StdConn) getMemInfo(meminfo *[unix.SK_MEMINFO_VARS]uint32) error {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
package udp
|
package udp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/slackhq/nebula/packet"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,17 +34,39 @@ type rawMessage struct {
|
|||||||
Pad0 [4]byte
|
Pad0 [4]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) PrepareRawMessages(n int) ([]rawMessage, [][]byte, [][]byte) {
|
func setRawMessageControl(msg *rawMessage, buf []byte) {
|
||||||
|
if len(buf) == 0 {
|
||||||
|
msg.Hdr.Control = nil
|
||||||
|
msg.Hdr.Controllen = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msg.Hdr.Control = &buf[0]
|
||||||
|
msg.Hdr.Controllen = uint64(len(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRawMessageControlLen(msg *rawMessage) int {
|
||||||
|
return int(msg.Hdr.Controllen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setCmsgLen(h *unix.Cmsghdr, l int) {
|
||||||
|
h.Len = uint64(l)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *StdConn) PrepareRawMessages(n int, pg PacketBufferGetter) ([]rawMessage, []*packet.Packet, [][]byte) {
|
||||||
msgs := make([]rawMessage, n)
|
msgs := make([]rawMessage, n)
|
||||||
buffers := make([][]byte, n)
|
|
||||||
names := make([][]byte, n)
|
names := make([][]byte, n)
|
||||||
|
|
||||||
|
packets := make([]*packet.Packet, n)
|
||||||
|
for i := range packets {
|
||||||
|
packets[i] = pg()
|
||||||
|
}
|
||||||
|
//todo?
|
||||||
|
|
||||||
for i := range msgs {
|
for i := range msgs {
|
||||||
buffers[i] = make([]byte, MTU)
|
|
||||||
names[i] = make([]byte, unix.SizeofSockaddrInet6)
|
names[i] = make([]byte, unix.SizeofSockaddrInet6)
|
||||||
|
|
||||||
vs := []iovec{
|
vs := []iovec{
|
||||||
{Base: &buffers[i][0], Len: uint64(len(buffers[i]))},
|
{Base: &packets[i].Payload[0], Len: uint64(packet.Size)},
|
||||||
}
|
}
|
||||||
|
|
||||||
msgs[i].Hdr.Iov = &vs[0]
|
msgs[i].Hdr.Iov = &vs[0]
|
||||||
@@ -51,7 +74,14 @@ func (u *StdConn) PrepareRawMessages(n int) ([]rawMessage, [][]byte, [][]byte) {
|
|||||||
|
|
||||||
msgs[i].Hdr.Name = &names[i][0]
|
msgs[i].Hdr.Name = &names[i][0]
|
||||||
msgs[i].Hdr.Namelen = uint32(len(names[i]))
|
msgs[i].Hdr.Namelen = uint32(len(names[i]))
|
||||||
|
if u.enableGRO {
|
||||||
|
msgs[i].Hdr.Control = &packets[i].Control[0]
|
||||||
|
msgs[i].Hdr.Controllen = uint64(len(packets[i].Control))
|
||||||
|
} else {
|
||||||
|
msgs[i].Hdr.Control = nil
|
||||||
|
msgs[i].Hdr.Controllen = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return msgs, buffers, names
|
return msgs, packets, names
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user