forked from CiscoDevNet/bigmuddy-network-telemetry-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxport_udp.go
371 lines (331 loc) · 8.78 KB
/
xport_udp.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
//
// October 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
// Handle UDP transports
package main
import (
"encoding/hex"
"encoding/json"
_ "fmt"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
"net"
"time"
)
const (
XPORT_UDPPOLLWAIT = 3
XPORT_UDPRETRY = 10
)
type serverUDP struct {
name string
bindpoint *net.UDPAddr
encap string
// OS receive buffer, can be tuned in config.
rxBuf int
//
// Log data into debug log
logData bool
// Listener over which connections are accepted.
listener *net.UDPConn
// Control channel used to control the server
ctrlChan <-chan *ctrlMsg
// Data channels fed by the server
dataChans []chan<- dataMsg
//
// Channel used by listen handler to signal it has closed.
closedListener chan struct{}
//
// Cancelled by conductor?
cancelled chan struct{}
}
//
// runServer is a UDP handler.
func (s *serverUDP) runServer() {
conn := s.listener
defer close(s.closedListener)
logctx := logger.WithFields(log.Fields{
"name": s.name,
"local": conn.LocalAddr().String()})
logctx.Info("UDP server run starting")
if s.rxBuf != 0 {
err := conn.SetReadBuffer(s.rxBuf)
if err != nil {
logctx.WithError(err).WithFields(
log.Fields{
"rxBuf": s.rxBuf,
}).Error(
"RxBuf size (check OS max, e.g. sysctl -w net.core.rmem_max)")
}
}
//
// Fetch a parser
err, parser := getNewEncapParser(s.name, s.encap, nil)
if err != nil {
logctx.WithError(err).Error("UDP parser setup")
return
}
for {
err, buffer := parser.nextBlockBuffer()
if err != nil {
logctx.WithError(err).Error("UDP failed to retrieve buffer")
goto out
}
length, remoteAddr, err := conn.ReadFromUDP(*buffer)
if err != nil {
//
// This may be normal operation; i.e. parent closed
// binding. We have no way of distinguishing short of
// some horrid match of the error string.
// https://github.com/golang/go/issues/4373
//
// But we can check for cancelled...
select {
case <-s.cancelled:
logctx.WithFields(
log.Fields{
"remote": remoteAddr.String(),
}).Debug("Reading from UDP port, cancelled")
default:
xportUDPMetaMonitor.CountersErrors.WithLabelValues(
s.name, remoteAddr.String()).Inc()
logctx.WithError(err).WithFields(
log.Fields{
"remote": remoteAddr.String(),
}).Error("Reading from UDP port")
}
goto out
}
trimBuf := (*buffer)[:length]
// fmt.Printf("length: %d/%d\n", length, len(trimBuf))
err, msgs := parser.nextBlock(trimBuf, remoteAddr)
if err != nil {
xportUDPMetaMonitor.CountersErrors.WithLabelValues(
s.name, remoteAddr.String()).Inc()
logctx.WithError(err).WithFields(
log.Fields{
"remote": remoteAddr.String(),
}).Error("Failed to extract next buffer")
goto out
}
if s.logData {
logctx.WithFields(log.Fields{
"remote": remoteAddr.String(),
"dataMsgCount": len(msgs),
"msglen": len(trimBuf),
"msg": hex.EncodeToString(trimBuf),
}).Debug("UDP server logdata")
}
xportUDPMetaMonitor.CountersMsgs.WithLabelValues(
s.name, remoteAddr.String()).Inc()
xportUDPMetaMonitor.CountersBytes.WithLabelValues(
s.name, remoteAddr.String()).Add(float64(length))
if msgs == nil {
continue
}
//
// Now we have content. What to do with it?
//
// Spray the generated messages across each available
// downstream channel
//
// Given this is UDP, rather than block on channel if channel
// is full, we drop and count. This ensures that the drop
// damage is limited to the slow consumer rather than all
// consumers. (There is still a window of opportunity between
// capacity test and send, if other producers feed the channel
// but we can live with that.)
//
for _, msg := range msgs {
for _, dataChan := range s.dataChans {
if cap(dataChan) == len(dataChan) {
// Count drops and continue. We need to add metadata
// to channel to do a better job of identifying
// laggards.
xportUDPMetaMonitor.CountersDrops.WithLabelValues(
s.name, remoteAddr.String()).Inc()
continue
}
select {
case dataChan <- msg:
// job done for this msg on this channel
case <-s.cancelled:
goto out
}
}
}
}
out:
logctx.Info("UDP server run stopping")
}
func (s *serverUDP) startStickyServer() {
//
// Prime loop by closing listener channel
close(s.closedListener)
for {
select {
case <-s.closedListener:
//
// Listener is closed. Recreate listener, set up new
// closedListener and kick off.
var err error
s.listener, err = net.ListenUDP("udp", s.bindpoint)
if err != nil {
logger.WithError(err).WithFields(log.Fields{
"name": s.name,
"bindpoint": s.bindpoint,
}).Error("UDP server failed to bind, retrying")
time.Sleep(time.Second * XPORT_UDPRETRY)
continue
} else {
s.closedListener = make(chan struct{})
go s.runServer()
}
case msg := <-s.ctrlChan:
switch msg.id {
case REPORT:
stats := msgStats{}
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(
"UDP server loop, rxed SHUTDOWN, closing binding")
close(s.cancelled)
if s.listener != nil {
s.listener.Close()
//
// Closing the listen port will cause reading from
// it to fail, and running server to return.
// Wait for signal we're done
<-s.closedListener
}
logger.WithFields(
log.Fields{
"name": s.name,
"bindpoint": s.bindpoint,
}).Debug("UDP 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(
"UDP server loop, unknown ctrl message")
}
}
}
}
func addUDPServer(
name string,
bindpoint *net.UDPAddr,
encap string,
dataChans []chan<- dataMsg,
ctrlChan <-chan *ctrlMsg,
rxBuf int,
logData bool) error {
s := new(serverUDP)
s.name = name
s.bindpoint = bindpoint
s.encap = encap
s.logData = logData
s.dataChans = dataChans
s.ctrlChan = ctrlChan
s.rxBuf = rxBuf
s.closedListener = make(chan struct{})
s.cancelled = make(chan struct{})
go s.startStickyServer()
return nil
}
// Module implement inputNodeModule interface
type udpInputModule struct {
}
func udpInputModuleNew() inputNodeModule {
return &udpInputModule{}
}
func (m *udpInputModule) 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
}
bindpoint, err := net.ResolveUDPAddr("udp", listen)
if err != nil {
logger.WithError(err).WithFields(
log.Fields{"name": name}).Error(
"attribute 'listen' unparseable as local UDP address")
return err, nil
}
encap, err := nc.config.GetString(name, "encap")
if err != nil {
encap = "st"
}
//
// If not set, will default to false, but let's be clear.
logData, _ := nc.config.GetBool(name, "logdata")
rxBuf, _ := nc.config.GetInt(name, "rxbuf")
//
// 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 = addUDPServer(
name, bindpoint, encap, dataChans, ctrlChan, rxBuf, logData)
return err, ctrlChan
}
type xportUDPMetaMonitorType struct {
CountersMsgs *prometheus.CounterVec
CountersBytes *prometheus.CounterVec
CountersErrors *prometheus.CounterVec
CountersDrops *prometheus.CounterVec
}
var xportUDPMetaMonitor *xportUDPMetaMonitorType
func init() {
xportUDPMetaMonitor = &xportUDPMetaMonitorType{
CountersMsgs: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "xportUDP_messages",
Help: "Messages",
},
[]string{"section", "peer"}),
CountersBytes: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "xportUDP_bytes",
Help: "Bytes",
},
[]string{"section", "peer"}),
CountersErrors: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "xportUDP_errors",
Help: "Errors",
},
[]string{"section", "peer"}),
CountersDrops: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "xportUDP_drops",
Help: "Drops",
},
[]string{"section", "peer"}),
}
// Dump content
prometheus.MustRegister(xportUDPMetaMonitor.CountersMsgs)
prometheus.MustRegister(xportUDPMetaMonitor.CountersBytes)
prometheus.MustRegister(xportUDPMetaMonitor.CountersErrors)
prometheus.MustRegister(xportUDPMetaMonitor.CountersDrops)
}