-
Notifications
You must be signed in to change notification settings - Fork 10
/
conn.go
399 lines (361 loc) · 8.8 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// +build linux
package iouring
import (
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
"sync/atomic"
"syscall"
)
const (
POLLIN = 0x1
POLLPRI = 0x2
POLLOUT = 0x4
POLLERR = 0x8
POLLHUP = 0x10
POLLNVAL = 0x20
// SOReuseport is the socket option to reuse socket port.
SOReuseport int = 0x0F
// TCPFastopen is the socket option to open a TCP fast open.
TCPFastopen int = 0x17
)
// FastOpenAllowed return nil if fast open is enabled.
func FastOpenAllowed() error {
b, err := ioutil.ReadFile("/proc/sys/net/ipv4/tcp_fack")
if err != nil {
return err
}
allowed, err := strconv.Atoi(strings.Replace(string(b), "\n", "", -1))
if err != nil {
return err
}
if allowed != 3 {
return fmt.Errorf("set /proc/sys/net/ipv4/tcp_fastopen to 3")
}
return nil
}
type connInfo struct {
fd int
id uint64
sqeIds chan uint64
}
type addr struct {
net string
s string
}
// Network implements the net.Addr interface.
func (a *addr) Network() string {
return a.net
}
// String implements the net.Addr interface.
func (a *addr) String() string {
return a.s
}
type ringListener struct {
debug bool
r *Ring
f *os.File
a *addr
stop chan struct{}
errHandler func(error)
newConn chan net.Conn
connGet chan chan net.Conn
}
// run is used to interact with the ring
func (l *ringListener) run() {
id := l.r.ID()
fd := int(l.f.Fd())
cInfo := &connInfo{
fd: fd,
id: id,
}
sqe, commit := l.r.SubmitEntry()
sqe.Opcode = PollAdd
sqe.Fd = int32(fd)
sqe.UFlags = int32(POLLIN)
sqe.UserData = id
commit()
conns := map[uint64]*connInfo{id: cInfo}
for {
select {
case <-l.stop:
return
default:
_, err := l.r.Enter(1024, 1, EnterGetEvents, nil)
if err != nil {
if l.errHandler != nil {
l.errHandler(err)
}
continue
}
l.walkCq(conns)
}
}
}
func (l *ringListener) walkCq(conns map[uint64]*connInfo) {
head := atomic.LoadUint32(l.r.cq.Head)
tail := atomic.LoadUint32(l.r.cq.Tail)
mask := atomic.LoadUint32(l.r.cq.Mask)
if head&mask == tail&mask {
return
}
seenIdx := head & mask
seenEnd := false
if l.debug {
sqHead := *l.r.sq.Head
sqTail := *l.r.sq.Tail
sqMask := *l.r.sq.Mask
cqHead := *l.r.cq.Head
cqTail := *l.r.cq.Tail
cqMask := *l.r.cq.Mask
fmt.Printf("sq head: %v tail: %v\nsq entries: %+v\n", sqHead&sqMask, sqTail&sqMask, l.r.sq.Entries[:9])
fmt.Printf("cq head: %v tail: %v\ncq entries: %+v\n", cqHead&cqMask, cqTail&cqMask, l.r.cq.Entries[:9])
}
for i := seenIdx; i <= tail&mask; i++ {
cqe := l.r.cq.Entries[i]
if (cqe.Flags&CqSeenFlag == CqSeenFlag || cqe.IsZero()) && !seenEnd {
seenIdx = i
} else {
seenEnd = true
}
cInfo, ok := conns[cqe.UserData]
if !ok {
continue
}
l.r.cq.Entries[i].Flags |= CqSeenFlag
head = atomic.LoadUint32(l.r.cq.Head)
if seenIdx > head {
atomic.CompareAndSwapUint32(l.r.cq.Head, head, seenIdx)
}
l.onListen(conns, cInfo)
return
}
// Handle wrapping.
seenIdx = uint32(0)
seenEnd = false
tail = atomic.LoadUint32(l.r.cq.Tail)
mask = atomic.LoadUint32(l.r.cq.Mask)
for i := uint32(0); i <= tail&mask; i++ {
cqe := l.r.cq.Entries[i]
if (cqe.Flags&CqSeenFlag == CqSeenFlag || cqe.IsZero()) && !seenEnd {
seenIdx = i
} else {
seenEnd = true
}
cInfo, ok := conns[cqe.UserData]
if !ok {
continue
}
l.r.cq.Entries[i].Flags |= CqSeenFlag
head = atomic.LoadUint32(l.r.cq.Head)
if seenIdx > head {
atomic.CompareAndSwapUint32(l.r.cq.Head, head, seenIdx)
}
l.onListen(conns, cInfo)
return
}
}
// onListen is called when processing a cqe for a listening socket.
func (l *ringListener) onListen(conns map[uint64]*connInfo, cInfo *connInfo) {
var (
newConnInfo connInfo
offset int64
rc = ringConn{
stop: make(chan struct{}, 2),
poll: make(chan uint64, 64),
r: l.r,
}
)
for {
// Wait for a new connection to arrive and add it to the ring.
newFd, sa, err := syscall.Accept4(cInfo.fd, syscall.SOCK_NONBLOCK)
if err != nil {
// TODO: Log this or something?
panic(err.Error())
}
rc.fd = newFd
rc.laddr = l.a
rc.raddr = &addr{net: l.a.net}
switch sockType := sa.(type) {
case *syscall.SockaddrInet4:
rc.raddr.s = fmt.Sprintf("%s:%d", sockType.Addr, sockType.Port)
case *syscall.SockaddrInet6:
rc.raddr.s = fmt.Sprintf("%s:%d", sockType.Addr, sockType.Port)
case *syscall.SockaddrUnix:
rc.raddr.s = sockType.Name
}
rc.offset = &offset
break
}
// Add the new connection back to the ring.
sqe, commit := l.r.SubmitEntry()
sqe.Opcode = PollAdd
sqe.Fd = int32(rc.fd)
sqe.UFlags = int32(POLLIN)
sqe.UserData = newConnInfo.id
commit()
ready := int32(1)
rc.pollReady = &ready
go rc.run()
// Add the old connection back as well.
sqe, commit = l.r.SubmitEntry()
sqe.Opcode = PollAdd
sqe.Fd = int32(cInfo.fd)
sqe.UFlags = int32(POLLIN)
sqe.UserData = uint64(cInfo.fd)
commit()
// Wait for the new connection to be accepted.
// TODO: If this is unbuffered it will block, alternatively it could be
// sent in a separate goroutine to ensure the main ring code isn't
// blocking.
l.newConn <- &rc
}
// Close implements the net.Listener interface.
func (l *ringListener) Close() error {
close(l.stop)
return l.f.Close()
}
// Addr implements the net.Listener interface.
func (l *ringListener) Addr() net.Addr {
return l.a
}
// Accept implements the net.Listener interface.
func (l *ringListener) Accept() (net.Conn, error) {
return <-l.newConn, nil
}
// Returns the file descriptor of the connection.
func (l *ringListener) Fd() int {
if l.f == nil {
return -1
}
return int(l.f.Fd())
}
// SockoptListener returns a net.Listener that is Ring based.
func (r *Ring) SockoptListener(network, address string, errHandler func(error), sockopts ...int) (net.Listener, error) {
var (
err error
fd int
sockAddr syscall.Sockaddr
)
l := &ringListener{
r: r,
a: &addr{net: network},
stop: make(chan struct{}),
newConn: make(chan net.Conn, 1024),
connGet: make(chan chan net.Conn),
errHandler: errHandler,
}
switch network {
case "tcp", "tcp4":
fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, fmt.Errorf("could not open socket")
}
netAddr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, fmt.Errorf("could not open socket")
}
l.a.net = netAddr.Network()
l.a.s = netAddr.String()
var ipAddr [4]byte
copy(ipAddr[:], netAddr.IP)
sockAddr = &syscall.SockaddrInet4{
Port: netAddr.Port,
Addr: ipAddr,
}
case "tcp6":
fd, err = syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, fmt.Errorf("could not open socket")
}
netAddr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, fmt.Errorf("could not open socket")
}
l.a.net = netAddr.Network()
l.a.s = netAddr.String()
ipAddr := [16]byte{}
copy(ipAddr[:], netAddr.IP)
sockAddr = &syscall.SockaddrInet6{
Port: netAddr.Port,
Addr: ipAddr,
}
case "unix":
fd, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
sockAddr = &syscall.SockaddrUnix{
Name: address,
}
case "udp", "udp4":
fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
if err != nil {
return nil, fmt.Errorf("could not open socket")
}
netAddr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, fmt.Errorf("could not open socket")
}
ipAddr := [4]byte{}
copy(ipAddr[:], netAddr.IP)
sockAddr = &syscall.SockaddrInet4{
Port: netAddr.Port,
Addr: ipAddr,
}
case "udp6":
fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
if err != nil {
return nil, fmt.Errorf("could not open socket")
}
netAddr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, fmt.Errorf("could not open socket")
}
l.a.net = netAddr.Network()
l.a.s = netAddr.String()
ipAddr := [16]byte{}
copy(ipAddr[:], netAddr.IP)
sockAddr = &syscall.SockaddrInet6{
Port: netAddr.Port,
Addr: ipAddr,
}
default:
return nil, fmt.Errorf("unknown network family: %s", network)
}
if err != nil {
syscall.Close(fd)
return nil, err
}
for _, sockopt := range sockopts {
if sockopt == SOReuseport {
err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, sockopt, 1)
if err != nil {
syscall.Close(fd)
return nil, err
}
} else if sockopt == TCPFastopen {
if err := FastOpenAllowed(); err != nil {
return nil, err
}
err = syscall.SetsockoptInt(fd, syscall.SOL_TCP, sockopt, 1)
if err != nil {
syscall.Close(fd)
return nil, err
}
}
}
if err := syscall.Bind(fd, sockAddr); err != nil {
syscall.Close(fd)
return nil, err
}
if err := syscall.Listen(fd, syscall.SOMAXCONN); err != nil {
syscall.Close(fd)
return nil, err
}
f := os.NewFile(uintptr(fd), "l")
l.f = f
l.debug = r.debug
go l.run()
return l, nil
}