forked from CiscoDevNet/bigmuddy-network-telemetry-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxport_tcp.go
423 lines (365 loc) · 9.3 KB
/
xport_tcp.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
//
// January 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
// Handle TCP transports
package main
import (
"encoding/hex"
"encoding/json"
log "github.com/Sirupsen/logrus"
"io"
"net"
"sync"
"time"
)
const (
//
// Time to wait before attempting to accept connection.
XPORT_TCP_WAIT_TO_REBIND = 1
)
type serverTCP struct {
name string
bindpoint string
encap string
//
// Log data into debug log
logData bool
//
// TCP keepalive period in second. 0 stops pipeline from enabling
// it.
keepaliveSeconds time.Duration
// Listener over which connections are accepted.
listener net.Listener
// Control channel used to control the server
ctrlChan <-chan *ctrlMsg
// Data channels fed by the server
dataChans []chan<- dataMsg
//
// Wait group used to synchronise shutdown of all open
// connection
connectionGroup *sync.WaitGroup
//
// Channel used to signal up to conductor that we are done
doneCancel chan int
//
// Channel used withing the server to request connections come
// down
cancelConn chan struct{}
}
func (s *serverTCP) cancelled() bool {
select {
case <-s.cancelConn:
return true
default:
// Nothing to do, go
// back sound the
// loop.
}
return false
}
func (s *serverTCP) handleConnection(conn net.Conn) {
logctx := logger.WithFields(
log.Fields{
"name": s.name,
"local": conn.LocalAddr().String(),
"remote": conn.RemoteAddr().String(),
"encap": s.encap,
"keepalive": s.keepaliveSeconds,
})
logctx.Info("TCP server accepted connection")
defer func() {
// Log, close the connection, and indicate to the wait
// group that we are done.
logctx.Info("TCP server closing connection")
conn.Close()
s.connectionGroup.Done()
}()
//
// Fetch a parser of the appropriate type.
err, parser := getNewEncapParser(s.name, s.encap, conn.RemoteAddr())
if err != nil {
logctx.WithError(err).Error("TCP server failed to fetch parser")
return
}
// Make buffered channels which will allow us to handle
// cancellation while waiting to read from socket.
//
// Anonymous function reading from socket will signal back over
// error or result channels, unless operation is cancelled
// first.
//
// A buffer length of one is sufficient; it is only used to
// decouple reading from handler. A vain early optimisation is to
// reuse the channel across the iterations reading from socket
// rather than adding a channel every time; hence the use of the
// buffered channel as opposed to using the pattern of simply
// closing channel as done signal.
readDone := make(chan int, 1)
readErr := make(chan error, 1)
//
// Setup TCP Keepalive. On linux, confirm using:
// ss -n -i -t -o '( sport = :<listenport> )'
// State Recv-Q Send-Q ... Address:Port
// ESTAB 0 0 ...:62436 timer:(keepalive,8min58sec,0)
//
if s.keepaliveSeconds != 0 {
err = conn.(*net.TCPConn).SetKeepAlive(true)
if err != nil {
logctx.WithError(err).Error("TCP keepalive setup failed")
} else {
err = conn.(*net.TCPConn).SetKeepAlivePeriod(s.keepaliveSeconds)
if err != nil {
logctx.WithError(err).Error("TCP keepalive period setup failed")
}
}
}
for {
// Get the next buffer expected by the parser
err, buffer := parser.nextBlockBuffer()
if err != nil || len(*buffer) == 0 {
logger.WithError(err).Error("TCP server failed to fetch buffer")
return
}
//
// What are we waiting for:
if s.logData {
logger.WithFields(log.Fields{
"len": len(*buffer),
}).Debug("waiting to read 'len' from socket")
}
go func() {
n, err := io.ReadFull(conn, *buffer)
if err != nil {
readErr <- err
} else {
readDone <- n
}
}()
select {
case <-s.cancelConn:
//
// Shutting down?
logger.Info("Cancelled connection")
return
case err = <-readErr:
//
// Read failed
logger.WithError(err).Error("TCP server failed on read full")
return
case <-readDone:
//
// We're done reading what we expected
}
//
// We got the data we were expecting
err, msgs := parser.nextBlock(*buffer, nil)
if err != nil {
logger.WithError(err).WithFields(
log.Fields{
"len": len(*buffer),
"msg": hex.Dump(*buffer),
}).Error("Failed to extract next buffer")
return
}
if s.logData {
logger.WithFields(log.Fields{
"dataMsgCount": len(msgs),
"len": len(*buffer),
"msg": hex.Dump(*buffer),
}).Debug("TCP server logdata")
}
//
// It is perfectly valid for there not
// to be a message to send on; e.g. we
// have just read and on the wire
// header which simply updates parser
// state.
if msgs == nil {
continue
}
//
// Spray the generated messages across each
// available downstream channel
//
for _, msg := range msgs {
for _, dataChan := range s.dataChans {
dataChan <- msg
}
}
}
}
func (s *serverTCP) acceptTCPConnections() {
defer func() {
// Wait for exit of cancelled connections if necessary.
logger.WithFields(log.Fields{
"name": s.name,
"bindpoint": s.bindpoint,
}).Debug("TCP server waiting for connections to cancel")
s.connectionGroup.Wait()
logger.WithFields(log.Fields{
"name": s.name,
"bindpoint": s.bindpoint,
}).Debug("TCP server destroying binding")
//
// Tell top half we're done cleaning up
s.doneCancel <- 1
// Canceller will ensure server is removed from
// serversTCP with something like this:
// delete(serversTCP, s.bindpoint)
}()
for {
conn, err := s.listener.Accept()
if s.cancelled() {
//
// We may be here because the northbound
// cancelled on us (and called Close)
logger.WithFields(log.Fields{
"name": s.name,
"bindpoint": s.bindpoint,
}).Debug("TCP server cancel binding")
return
}
if err != nil {
logger.WithError(err).WithFields(
log.Fields{
"name": s.name,
"bindpoint": s.bindpoint,
}).Error("TCP connection accept failed")
// We keep trying, but use a retry
// timeout. Note that when we're in this
// sleep, we will also not be handling
// deletes.
time.Sleep(
XPORT_TCP_WAIT_TO_REBIND * time.Second)
}
//
// Look for cancellation from controller.
s.connectionGroup.Add(1)
go s.handleConnection(conn)
}
}
func (s *serverTCP) startServer() {
var stats msgStats
logger.WithFields(log.Fields{
"name": s.name,
"listen": s.bindpoint}).Info("TCP server starting")
//
// Start accepting connections
go s.acceptTCPConnections()
for {
select {
case msg := <-s.ctrlChan:
switch msg.id {
case REPORT:
content, _ := json.Marshal(stats)
resp := &ctrlMsg{
id: ACK,
content: content,
respChan: nil,
}
msg.respChan <- resp
case SHUTDOWN:
logger.WithFields(
log.Fields{"name": s.name}).Info(
"TCP server loop, rxed SHUTDOWN, closing connections")
//
// Flag cancellation of binding and
// its connections and wait for
// cancellation to complete
// synchronously.
close(s.cancelConn)
s.listener.Close()
logger.WithFields(
log.Fields{
"name": s.name,
"bindpoint": s.bindpoint,
}).Info("TCP server notify conductor binding is closed")
resp := &ctrlMsg{
id: ACK,
respChan: nil,
}
msg.respChan <- resp
return
default:
logger.WithFields(
log.Fields{"name": s.name}).Error(
"TCP server loop, unknown ctrl message")
}
}
}
}
//
// addTCPServer adds the new service to serversTCP if necessary.
// Runs in the context of the conductor handler
func addTCPServer(
name string,
bindpoint string,
encap string,
dataChans []chan<- dataMsg,
ctrlChan <-chan *ctrlMsg,
keepalive int,
logData bool) error {
listener, err := net.Listen("tcp", bindpoint)
if err != nil {
logger.WithError(err).WithFields(log.Fields{
"name": name,
"bindpoint": bindpoint,
}).Error("TCP server failed to bind")
return err
}
s := new(serverTCP)
s.name = name
s.listener = listener
s.bindpoint = bindpoint
s.encap = encap
s.logData = logData
s.keepaliveSeconds = time.Duration(keepalive) * time.Second
s.dataChans = dataChans
s.ctrlChan = ctrlChan
s.cancelConn = make(chan struct{})
s.doneCancel = make(chan int)
s.connectionGroup = new(sync.WaitGroup)
go s.startServer()
return nil
}
// Module implement inputNodeModule interface
type tcpInputModule struct {
}
func tcpInputModuleNew() inputNodeModule {
return &tcpInputModule{}
}
func (m *tcpInputModule) configure(
name string,
nc nodeConfig,
dataChans []chan<- dataMsg) (error, chan<- *ctrlMsg) {
listen, err := nc.config.GetString(name, "listen")
if err != nil {
logger.WithError(err).WithFields(
log.Fields{"name": name}).Error(
"attribute 'listen' must be specified in this section")
return err, nil
}
//
// If not set, will default to false, but let's be clear.
logData, _ := nc.config.GetBool(name, "logdata")
if err != nil {
logData = false
}
encap, err := nc.config.GetString(name, "encap")
if err != nil {
encap = "st"
}
keepalive, _ := nc.config.GetInt(name, "keepalive_seconds")
//
// Create a control channel which will be used to control us,
// and kick off the server which will accept connections and
// listen for control requests.
ctrlChan := make(chan *ctrlMsg)
err = addTCPServer(
name, listen, encap, dataChans, ctrlChan, keepalive, logData)
return err, ctrlChan
}