Skip to content

Commit 77bf9f1

Browse files
committed
performance4: use buffer.view based channels instead of pipe
1 parent 02eaeb5 commit 77bf9f1

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

overlay/user.go

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/sirupsen/logrus"
88
"github.com/slackhq/nebula/config"
99
"github.com/slackhq/nebula/iputil"
10+
"gvisor.dev/gvisor/pkg/buffer"
1011
)
1112

1213
func NewUserDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error) {
@@ -15,25 +16,18 @@ func NewUserDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr *net.IPNet,
1516

1617
func NewUserDevice(tunCidr *net.IPNet) (Device, error) {
1718
// these pipes guarantee each write/read will match 1:1
18-
or, ow := io.Pipe()
19-
ir, iw := io.Pipe()
2019
return &UserDevice{
21-
tunCidr: tunCidr,
22-
outboundReader: or,
23-
outboundWriter: ow,
24-
inboundReader: ir,
25-
inboundWriter: iw,
20+
tunCidr: tunCidr,
21+
outboundChannel: make(chan *buffer.View),
22+
inboundChannel: make(chan *buffer.View),
2623
}, nil
2724
}
2825

2926
type UserDevice struct {
3027
tunCidr *net.IPNet
3128

32-
outboundReader *io.PipeReader
33-
outboundWriter *io.PipeWriter
34-
35-
inboundReader *io.PipeReader
36-
inboundWriter *io.PipeWriter
29+
outboundChannel chan *buffer.View
30+
inboundChannel chan *buffer.View
3731
}
3832

3933
func (d *UserDevice) Activate() error {
@@ -46,18 +40,41 @@ func (d *UserDevice) NewMultiQueueReader() (io.ReadWriteCloser, error) {
4640
return d, nil
4741
}
4842

49-
func (d *UserDevice) Pipe() (*io.PipeReader, *io.PipeWriter) {
50-
return d.inboundReader, d.outboundWriter
43+
func (d *UserDevice) Pipe() (<-chan *buffer.View, chan<- *buffer.View) {
44+
return d.inboundChannel, d.outboundChannel
5145
}
5246

5347
func (d *UserDevice) Read(p []byte) (n int, err error) {
54-
return d.outboundReader.Read(p)
48+
view, ok := <-d.outboundChannel
49+
if !ok {
50+
return 0, io.EOF
51+
}
52+
return view.Read(p)
53+
}
54+
func (d *UserDevice) WriteTo(w io.Writer) (n int64, err error) {
55+
view, ok := <-d.outboundChannel
56+
if !ok {
57+
return 0, io.EOF
58+
}
59+
return view.WriteTo(w)
5560
}
61+
5662
func (d *UserDevice) Write(p []byte) (n int, err error) {
57-
return d.inboundWriter.Write(p)
63+
view := buffer.NewViewWithData(p)
64+
d.inboundChannel <- view
65+
return view.Size(), nil
5866
}
67+
func (d *UserDevice) ReadFrom(r io.Reader) (n int64, err error) {
68+
view := buffer.NewViewSize(2048)
69+
n, err = view.ReadFrom(r)
70+
if n > 0 {
71+
d.inboundChannel <- view
72+
}
73+
return
74+
}
75+
5976
func (d *UserDevice) Close() error {
60-
d.inboundWriter.Close()
61-
d.outboundWriter.Close()
77+
close(d.inboundChannel)
78+
close(d.outboundChannel)
6279
return nil
6380
}

service/service.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,17 @@ func New(config *config.C, logger *logrus.Logger) (*Service, error) {
105105
tcpFwd := tcp.NewForwarder(s.ipstack, tcpReceiveBufferSize, maxInFlightConnectionAttempts, s.tcpHandler)
106106
s.ipstack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpFwd.HandlePacket)
107107

108-
reader, writer := device.Pipe()
108+
nebula_tun_reader, nebula_tun_writer := device.Pipe()
109109

110110
go func() {
111111
<-ctx.Done()
112-
reader.Close()
113-
writer.Close()
112+
close(nebula_tun_writer)
114113
}()
115114

116115
// create Goroutines to forward packets between Nebula and Gvisor
117116
eg.Go(func() error {
118-
buf := make([]byte, header.IPv4MaximumHeaderSize+header.IPv4MaximumPayloadSize)
119117
for {
120-
// this will read exactly one packet
121-
n, err := reader.Read(buf)
122-
if err != nil {
123-
return err
124-
}
125-
view := buffer.NewViewWithData(buf[:n])
118+
view := <-nebula_tun_reader
126119
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
127120
Payload: buffer.MakeWithView(view),
128121
})
@@ -142,11 +135,7 @@ func New(config *config.C, logger *logrus.Logger) (*Service, error) {
142135
}
143136
continue
144137
}
145-
bufView := packet.ToView()
146-
if _, err := bufView.WriteTo(writer); err != nil {
147-
return err
148-
}
149-
bufView.Release()
138+
nebula_tun_writer <- packet.ToView()
150139
}
151140
})
152141

0 commit comments

Comments
 (0)