forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 7
/
socket.go
481 lines (399 loc) · 11.9 KB
/
socket.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright 2016-2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package proxy
import (
"fmt"
"net"
"os"
"sync"
"syscall"
"time"
"golang.org/x/sys/unix"
"github.com/cilium/cilium/pkg/flowdebug"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/maps/ctmap"
"github.com/cilium/cilium/pkg/u8proto"
"github.com/sirupsen/logrus"
)
const (
fieldConn = "conn"
fieldSize = "size"
fieldConnPair = "connPair"
)
type proxySocket struct {
// listener is the TCP listener.
listener net.Listener
// locker protects closing the closing channel and accessing pairs.
locker lock.Mutex
closing chan struct{}
// pairs is the set of active connection pairs.
pairs []*connectionPair
}
func listenSocket(address string, mark int, transparent bool) (*proxySocket, error) {
socket := &proxySocket{
closing: make(chan struct{}),
}
addr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return nil, err
}
family := syscall.AF_INET
if addr.IP.To4() == nil {
family = syscall.AF_INET6
}
fd, err := syscall.Socket(family, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
return nil, fmt.Errorf("unable to set SO_REUSEADDR socket option: %s", err)
}
if transparent {
if family == syscall.AF_INET {
err = syscall.SetsockoptInt(fd, unix.SOL_IP, unix.IP_TRANSPARENT, 1)
} else {
err = syscall.SetsockoptInt(fd, unix.SOL_IPV6, unix.IPV6_TRANSPARENT, 1)
}
if err != nil {
return nil, fmt.Errorf("unable to set SO_TRANSPARENT socket option: %s", err)
}
}
if mark != 0 {
setFdMark(fd, mark)
}
sockAddr, err := ipToSockaddr(family, addr.IP, addr.Port, addr.Zone)
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, 128); err != nil {
syscall.Close(fd)
return nil, err
}
f := os.NewFile(uintptr(fd), addr.String())
defer f.Close()
socket.listener, err = net.FileListener(f)
if err != nil {
return nil, err
}
return socket, nil
}
func setLinger(c net.Conn, linger time.Duration) error {
if tcp, ok := c.(*net.TCPConn); ok {
if err := tcp.SetLinger(int(linger.Seconds())); err != nil {
return fmt.Errorf("unable to set SO_LINGER socket option: %s", err)
}
}
return nil
}
// Accept calls Accept() on the listen socket of the proxy.
// If not nil, afterClose is called after the connection pair has been closed.
// If cascadeClose is true, the returned connectionPair will immediately be
// closed when this listen socket is closed.
func (s *proxySocket) Accept(cascadeClose bool) (*connectionPair, error) {
c, err := s.listener.Accept()
if err != nil {
return nil, err
}
// Set the SO_LINGER socket option so that a request connection is
// guaranteed to be closed within the proxyConnectionCloseTimeout.
// If the linger timeout expires, the connection is closed with a RST,
// which is useful to signal to the client that the termination is
// abnormal.
if err = setLinger(c, proxyConnectionCloseTimeout); err != nil {
c.Close()
return nil, err
}
// Enable keepalive on all accepted connections to force data on the
// TCP connection in regular intervals to ensure that the datapath
// never expires state associated with this connection.
if err = setKeepAlive(c); err != nil {
c.Close()
return nil, err
}
var afterClose func(*connectionPair)
if cascadeClose {
afterClose = s.connectionPairClosed
}
pair := newConnectionPair(afterClose)
s.locker.Lock()
if cascadeClose {
s.pairs = append(s.pairs, pair)
}
pair.Rx.SetConnection(c)
s.locker.Unlock()
return pair, nil
}
func (s *proxySocket) connectionPairClosed(pair *connectionPair) {
scopedLog := log.WithField(fieldConnPair, pair)
scopedLog.Debug("Connection pair closed, removing from proxy socket cascading delete list")
s.locker.Lock()
defer s.locker.Unlock()
for i, p := range s.pairs {
if p == pair {
scopedLog.Debug("Connection pair removed from proxy socket cascading delete list")
// Delete the pair from the list.
numPairs := len(s.pairs)
s.pairs[i] = s.pairs[numPairs-1]
s.pairs = s.pairs[:numPairs-1]
return
}
}
}
// Close closes the proxy socket and stops accepting new connections.
func (s *proxySocket) Close() {
s.locker.Lock()
select {
case <-s.closing:
s.locker.Unlock()
return
default:
}
close(s.closing)
s.listener.Close()
pairs := s.pairs
s.pairs = nil
s.locker.Unlock()
// Immediately close all connection pairs for which cascading close was
// requested in Accept.
for _, pair := range pairs {
pair.Rx.Close()
}
}
type socketQueue chan []byte
type proxyConnection struct {
// isRequestDirection indicates the direction of the TCP connection:
// ingress/request (true) or egress/response (false).
isRequestDirection bool
// conn is the underlying TCP connection.
conn net.Conn
// queue is the queue of messages to send.
queue socketQueue
// closeLocker is used to ensure the close channel may only be closed once.
closeLocker lock.Mutex
// close is the channel that is closed to indicate that the connection
// must be closed. closeLocker must be held when attempting to close this
// channel to prevent closing it multiple times.
close chan struct{}
// afterClose is a function that is called after the connection's queue is
// closed.
afterClose func()
}
func newProxyConnection(rx bool, afterClose func()) *proxyConnection {
return &proxyConnection{
isRequestDirection: rx,
queue: make(socketQueue, socketQueueSize),
close: make(chan struct{}),
afterClose: afterClose,
}
}
// SetConnection associates an established connection to the proxy connection.
// It starts a goroutine to write Enqueue()ed messages into the connection.
func (c *proxyConnection) SetConnection(conn net.Conn) {
if c.conn != nil {
log.WithField(fieldConn, c).Panic("Established connection is already associated")
}
c.conn = conn
go c.writeQueuedMessages()
}
func fmtAddress(a net.Addr) string {
if a == nil {
return "nil"
}
return a.String()
}
// Closed returns true if the connection is closed
func (c *proxyConnection) Closed() bool {
return c == nil || c.conn == nil
}
func (c *proxyConnection) String() string {
if c.isRequestDirection {
if c.Closed() {
return "rx:closed"
}
return fmt.Sprintf("rx:%s->%s",
fmtAddress(c.conn.RemoteAddr()),
fmtAddress(c.conn.LocalAddr()))
}
if c.Closed() {
return "tx:closed"
}
return fmt.Sprintf("tx:%s->%s",
fmtAddress(c.conn.LocalAddr()),
fmtAddress(c.conn.RemoteAddr()))
}
func (c *proxyConnection) writeQueuedMessages() {
scopedLog := log.WithField(fieldConn, c)
defer c.Close()
for {
select {
case <-c.close:
scopedLog.Debug("Connection closed, message queue exiting")
return
case msg, more := <-c.queue:
if !more {
// This should never happen since the queue channel is never closed.
return
}
// Write the entire message into the socket.
_, err := c.conn.Write(msg)
// Ignore any write errors in case the socket has been closed by this proxy.
select {
case <-c.close:
scopedLog.Debug("Connection closed, message queue exiting")
return
default:
}
if err != nil {
scopedLog.WithError(err).Warn("Error while writing to socket, closing socket")
return
}
}
}
}
func (c *proxyConnection) direction() string {
if c.isRequestDirection {
return "request"
}
return "response"
}
// Enqueue queues a message to be written into the connection.
func (c *proxyConnection) Enqueue(msg []byte) {
scopedLog := log.WithFields(logrus.Fields{
fieldConn: c,
fieldSize: len(msg),
})
flowdebug.Log(scopedLog, fmt.Sprintf("Enqueueing %s message", c.direction()))
select {
case <-c.close:
flowdebug.Log(scopedLog, fmt.Sprintf("%s connection is closed; dropping message", c.direction()))
case c.queue <- msg:
flowdebug.Log(scopedLog, fmt.Sprintf("Enqueued %s message", c.direction()))
}
}
// Close closes this connection.
// The connection on the other side of the proxy is closed after it is queued
// for closing or after proxyConnectionCloseTimeout.
func (c *proxyConnection) Close() {
scopedLog := log.WithField(fieldConn, c)
c.closeLocker.Lock()
select {
case <-c.close:
// Already closed. Nothing to do.
c.closeLocker.Unlock()
return
default:
}
// Cause writeQueuedMessages to terminate.
close(c.close)
c.closeLocker.Unlock()
// Actually close the TCP connection. This will unblock any eventually
// blocking c.conn.Write call in writeQueuedMessages.
if !c.Closed() {
scopedLog.Debug("Closing socket")
c.conn.Close()
}
// Call connectionPair.close() concurrently so that this call doesn't block
// while waiting for the other connection in the pair to close.
go c.afterClose()
}
type connectionPair struct {
Rx, Tx *proxyConnection
afterCloseOnce sync.Once
afterClose func()
}
func newConnectionPair(afterClose func(*connectionPair)) *connectionPair {
pair := &connectionPair{}
pair.Rx = newProxyConnection(true, pair.close)
pair.Tx = newProxyConnection(false, pair.close)
if afterClose != nil {
pair.afterClose = func() { afterClose(pair) }
}
return pair
}
func (p *connectionPair) String() string {
return p.Rx.String() + "<->" + p.Tx.String()
}
func (p *connectionPair) close() {
scopedLog := log.WithField(fieldConnPair, p)
// Wait for both Rx and Tx to be closed or for timeout.
timeout := time.NewTimer(proxyConnectionCloseTimeout)
var bothClosed bool
select {
case <-p.Rx.close:
scopedLog.Debug("Rx is already closed, waiting for Tx to close")
// Rx is closed. Wait for Tx to close or timeout.
select {
case <-p.Tx.close:
bothClosed = true
case <-timeout.C:
scopedLog.Debug("Timeout while waiting for Tx to close; closing Tx")
p.Tx.Close()
}
case <-p.Tx.close:
// Tx is closed. Wait for Rx to close or timeout.
scopedLog.Debug("Tx is already closed, waiting for Rx to close")
select {
case <-p.Rx.close:
bothClosed = true
case <-timeout.C:
scopedLog.Debug("Timeout while waiting for Rx to close; closing Rx")
p.Rx.Close()
}
default:
// This case should never be selected, since connectionPair.close() is
// called only from proxyConnection.Close() after the close channel is
// closed, so at least one of the cases above can always be selected.
}
timeout.Stop()
if bothClosed {
scopedLog.Debug("Both Rx and Tx are closed")
if p.afterClose != nil {
p.afterCloseOnce.Do(p.afterClose)
}
}
}
func lookupSrcID(mapname, remoteAddr, localAddr string, ingress bool) (uint32, error) {
val, err := ctmap.Lookup(mapname, remoteAddr, localAddr, u8proto.TCP, ingress)
if err != nil {
flowdebug.Log(log.WithField(logfields.Object, logfields.Repr(val)), "Did not find proxy entry!")
return 0, err
}
flowdebug.Log(log.WithField(logfields.Object, logfields.Repr(val)), "Found proxy entry")
return val.SourceSecurityID, nil
}
func setFdMark(fd, mark int) {
scopedLog := log.WithFields(logrus.Fields{
fieldFd: fd,
fieldMarker: mark,
})
flowdebug.Log(scopedLog, "Setting packet marker of socket")
err := syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_MARK, mark)
if err != nil {
scopedLog.WithError(err).Warn("Unable to set SO_MARK")
}
}
func setSocketMark(c net.Conn, mark int) {
if tc, ok := c.(*net.TCPConn); ok {
if f, err := tc.File(); err == nil {
defer f.Close()
setFdMark(int(f.Fd()), mark)
}
}
}