-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoftee.go
634 lines (575 loc) · 18.1 KB
/
oftee.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// OFTEE command to start a OpenFlow tee proxy. This command parses the
// environment for configuration information and then starts a processing
// loop for OpenFlow messages.
package main
import (
"bufio"
"bytes"
"encoding/binary"
"flag"
"fmt"
"io"
"net"
"net/url"
"os"
"strconv"
"strings"
"github.com/ciena/oftee/api"
"github.com/ciena/oftee/connections"
"github.com/ciena/oftee/criteria"
"github.com/ciena/oftee/injector"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/kelseyhightower/envconfig"
of "github.com/netrack/openflow"
"github.com/netrack/openflow/ofp"
log "github.com/sirupsen/logrus"
)
const (
// ReadBufferSize Buffer size when reading
ReadBufferSize = 2048
// Supported and future supported URL schemes
// SchemeTCP prefex for TCP URI scheme
SchemeTCP = "tcp"
// SchemeHTTP prefex for HTTP URI scheme
SchemeHTTP = "http"
// SchemeKafka prefex for Kafka URI scheme
SchemeKafka = "kafka"
// Supported end point configuration terms
// TermAction term used in match / action to depict an action
TermAction = "action"
// TermDLType term use in match / action to depict a dl_type match
TermDLType = "dl_type"
)
// App Maintains the application configuration and runtime state
type App struct {
ShowHelp bool `envconfig:"HELP" default:"false" desc:"show this message"`
ListenOn string `envconfig:"LISTEN_ON" default:":8000" required:"true" desc:"connection on which to listen for an open flow device"`
APIOn string `envconfig:"API_ON" default:":8002" required:"true" desc:"port on which to listen to accept API requests"`
ProxyTo string `envconfig:"PROXY_TO" default:":8001" required:"true" desc:"connection on which to attach to an SDN controller"`
TeeTo []string `envconfig:"TEE_TO" desc:"list of connections on which tee packet in messages"`
TeeRawPackets bool `envconfig:"TEE_RAW" default:"false" desc:"only tee raw packets to the client, openflow headers not included"`
LogLevel string `envconfig:"LOG_LEVEL" default:"debug" desc:"logging level"`
ShareConnections bool `envconfig:"SHARE_CONNECTIONS" default:"true" desc:"use shared connections to outbound end points"`
CPUProfile string `envconfig:"CPU_PROFILE" default:"cpu.pprof" desc:"file to which to write CPU profile data"`
MemProfile string `envconfig:"MEM_PROFILE" default:"mem.pprof" desc:"file to which to write MEM profile data"`
listener net.Listener
endpoints connections.Endpoints
api *api.API
}
// OpenFlowContext provides context for OF packet in messages
type OpenFlowContext struct {
DatapathID uint64
Port uint32
}
func (c *OpenFlowContext) String() string {
return fmt.Sprintf("[0x%016x, 0x%04x]", c.DatapathID, c.Port)
}
// Len returns the length of the OpenFlowContext
func (c *OpenFlowContext) Len() uint16 {
return 12
}
// WriteTo writes the open flow context to the provided writer
func (c *OpenFlowContext) WriteTo(w io.Writer) (int64, error) {
buf := make([]byte, 12)
binary.BigEndian.PutUint64(buf, c.DatapathID)
binary.BigEndian.PutUint32(buf[8:], c.Port)
val, err := w.Write(buf)
return int64(val), err
}
// Why or why does Go not have a simply int minimum function, ok, i get it,
// proverb A little copying is better than a little dependency, but this could
// be part of a standard lib
func min(a, b int) int {
if a < b {
return a
}
return b
}
func (app *App) cleanup() {
}
func (app *App) removeInjector(inject injector.Injector) {
app.api.DPIDMappingListener <- api.DPIDMapping{
Action: api.MapActionDelete,
DPID: inject.GetDPID(),
Inject: nil,
}
}
// close wraps an io.Closer.Close call so that any error can be logged
func close(c io.Closer) {
if err := c.Close(); err != nil {
log.
WithError(err).
Error("Error when attempting to close resource")
}
}
// Handle a single connection from a device
func (app *App) handle(conn net.Conn, endpoints connections.Endpoints) error {
// Close the connection when we are no longer handling it
defer close(conn)
var (
err error
buffer = new(bytes.Buffer)
match criteria.Criteria
header of.Header
context OpenFlowContext
hCount, piCount int64
left uint16
packetIn ofp.PacketIn
featuresReply ofp.SwitchFeatures
proxyURL *url.URL
proxyTarget string
)
// Parse URL to proxy
if strings.Index(app.ProxyTo, "://") == -1 {
proxyTarget = app.ProxyTo
} else {
if proxyURL, err = url.Parse(app.ProxyTo); err != nil {
log.
WithFields(log.Fields{"proxy": app.ProxyTo}).
WithError(err).
Error("Unable to parse URL to SDN controller")
return err
}
if proxyURL.Scheme != "tcp" {
log.
WithFields(log.Fields{
"scheme": proxyURL.Scheme,
"proxy": app.ProxyTo,
}).
Error("Only TCP connections are supported to SDN controller")
return err
}
proxyTarget = proxyURL.Host
}
// Create connection to SDN controller
proxy := new(connections.TCPConnection)
if proxy.Connection, err = net.Dial("tcp", proxyTarget); err != nil {
log.
WithFields(log.Fields{"proxy": app.ProxyTo}).
WithError(err).
Error("Unable to connect to SDN controller")
return err
}
defer close(proxy.Connection)
proxy.Criteria = criteria.Criteria{}
inject := injector.NewOFDeviceInjector()
defer inject.Stop()
defer app.removeInjector(inject)
// Anything from the controller, just send to the device
go func(_conn net.Conn, _proxy *connections.TCPConnection, _inject injector.Injector) {
// If this fails, bad things are going to happen all over
// and we just need to drop the connection to device and
// have everything restart
if _, err := _inject.Copy(_conn, _proxy.Connection); err != nil {
log.
WithError(err).
WithFields(log.Fields{
"proxy": _proxy.Connection,
}).
Error("Communication from controller to device failed")
// Force the connection to close, which should
// cause the read loop below to fail out
if err = _conn.Close(); err != nil {
// Ignore
}
}
}(conn, proxy, inject)
reader := bufio.NewReaderSize(conn, ReadBufferSize)
for {
// Read open flow header, if this does not work then we have
// a serious error, so fail fast and move on
hCount, err = header.ReadFrom(reader)
if err != nil && err != io.EOF {
log.
WithError(err).
Debug("Failed to read OpenFlow message header")
return err
}
// If we have a packet in message then this will be tee-ed
// to those end points that match, else we just proxy to
// the controller.
switch header.Type {
case of.TypePacketIn:
log.
WithFields(log.Fields{
"of_version": header.Version,
"of_message": header.Type.String(),
"of_transaction": header.Transaction,
"length": header.Length,
"header_length": hCount,
}).
Debug("SENDING: all end-points")
// Read the packet in message header, have to create a LimitReader as the ofp.packetIn
// interface does an io.ReadAll, which will read more than the frame size. This reads
// the packet in header and the packet.
piCount, err = packetIn.ReadFrom(io.LimitReader(reader, int64(header.Length)-hCount))
if err != nil || piCount != int64(header.Length)-hCount {
log.
WithError(err).
Debug("Failed to read OpenFlow Packet In message header")
return err
}
// Look for the port in contained in the message
for _, xm := range packetIn.Match.Fields {
if xm.Type == ofp.XMTypeInPort {
context.Port = binary.BigEndian.Uint32(xm.Value)
}
}
// Reset the buffer to read the packet in message and
// write the headers to the buffer
buffer.Reset()
if _, err = context.WriteTo(buffer); err != nil {
log.
WithError(err).
Error("Failed to write OpenFlow context to packet in buffer")
return err
}
if _, err = header.WriteTo(buffer); err != nil {
log.
WithError(err).
Error("Failed to write OpenFlow header to packet in buffer")
return err
}
if _, err = packetIn.WriteTo(buffer); err != nil {
log.
WithError(err).
Error("Failed to write packet in to packet in buffer")
return err
}
// Load and decode the packet being packeted in so we
// can compare match criteria
pkt := gopacket.NewPacket(packetIn.Data,
layers.LayerTypeEthernet,
gopacket.DecodeOptions{Lazy: true, NoCopy: true})
eth := pkt.Layer(layers.LayerTypeEthernet)
if eth == nil {
log.
WithFields(log.Fields{
"packet": fmt.Sprintf("%02x", packetIn.Data),
"buffer": fmt.Sprintf("%02x", buffer),
}).
Debug("Not ethernet packet, can't match")
continue
}
log.
WithFields(log.Fields{
"dl_type": fmt.Sprintf("0x%04x", uint16(eth.(*layers.Ethernet).EthernetType)),
}).
Debug("match")
match = criteria.Criteria{
Set: criteria.BitDLType,
DlType: uint16(eth.(*layers.Ethernet).EthernetType),
}
// packet in to the SDN controller and packet out
// to those end points that match the criteria
if _, err = proxy.Write(buffer.Bytes()[context.Len() : context.Len()+header.Length]); err != nil {
log.
WithError(err).
Error("Unexpected error while writing packet to controller")
return err
}
// TODO loop until all bytes are written
log.
WithFields(log.Fields{
"context": context.String(),
"openflow": fmt.Sprintf("%02x", buffer.Bytes()[context.Len():context.Len()+header.Length-packetIn.Length]),
"packet": fmt.Sprintf("%02x", packetIn.Data),
}).
Debug("packet in")
if app.TeeRawPackets {
_, err = endpoints.ConditionalWrite(packetIn.Data, match)
} else {
_, err = endpoints.ConditionalWrite(buffer.Bytes()[:context.Len()+header.Length], match)
}
if err != nil {
log.
WithError(err).
Error("Unexpected error while writing to TEE clients")
return err
}
// TODO loop until all bytes are written
case of.TypeFeaturesReply:
log.WithFields(log.Fields{
"of_version": header.Version,
"of_message": header.Type.String(),
"of_transaction": header.Transaction,
"length": header.Length,
}).Debug("Sniffing for DPID")
piCount, err = featuresReply.ReadFrom(reader)
app.api.DPIDMappingListener <- api.DPIDMapping{
Action: api.MapActionAdd,
DPID: featuresReply.DatapathID,
Inject: inject,
}
inject.SetDPID(featuresReply.DatapathID)
context.DatapathID = featuresReply.DatapathID
log.WithFields(log.Fields{
"dpid": fmt.Sprintf("0x%016x", featuresReply.DatapathID),
}).Debug("Sniffed DPID")
if _, err = header.WriteTo(proxy); err != nil {
log.
WithError(err).
Error("Unexpected error while writing features reply open flow header to controller")
return err
}
if _, err = featuresReply.WriteTo(proxy); err != nil {
log.
WithError(err).
Error("Unexpected error while writing features reply header to controller")
return err
}
left = header.Length - uint16(hCount) - uint16(piCount)
if _, err = io.CopyN(proxy, reader, int64(left)); err != nil {
log.
WithError(err).
Error("Unexpected error while writing features reply header to controller")
return err
}
default:
// All messages that are not packet in messages are
// only proxied to the SDN controller. No buffering,
// just grab bits, push bits.
log.WithFields(log.Fields{
"of_version": header.Version,
"of_message": header.Type.String(),
"of_transaction": header.Transaction,
"length": header.Length,
}).Debug("SENDING: SDN controller")
if _, err = header.WriteTo(proxy); err != nil && err != io.EOF {
log.
WithError(err).
Error("Unexpected error while writing generic open flow header to controller")
return err
}
left = header.Length - uint16(hCount)
if _, err = io.CopyN(proxy, reader, int64(left)); err != nil && err != io.EOF {
log.
WithError(err).
Error("Unexpected error while writting open flow message body to controller")
return err
}
}
}
}
// EstablishEndpointConnections creates connections entities to the configured
// endpoints specified as configuration options
func (app *App) EstablishEndpointConnections() (connections.Endpoints, error) {
var u *url.URL
var c connections.Connection
var tcp *connections.TCPConnection
var match criteria.Criteria
var addr string
var parts, terms []string
var err error
endpoints := make([]connections.Connection, len(app.TeeTo))
for i, spec := range app.TeeTo {
if len(spec) != 0 {
// The connection address is of the form
// [match],action=url
// Where [match] is a list of match terms, currently
// only dl_type is supported.
parts = strings.Split(spec, ";")
match = criteria.Criteria{}
if len(parts) == 1 {
addr = spec
} else {
addr = ""
for _, part := range parts {
terms = strings.Split(part, "=")
switch strings.ToLower(terms[0]) {
case TermAction:
addr = terms[1]
case TermDLType:
ethType, err := strconv.ParseUint(terms[1], 0, 16)
match.Set |= criteria.BitDLType
match.DlType = uint16(ethType)
if err != nil {
log.
WithFields(log.Fields{
"term": terms[0],
"value": terms[1],
}).
Error("Unable to convert term to uint16")
return nil, err
}
log.
WithFields(log.Fields{
"term": terms[0],
"value": terms[1],
}).
Debug("Found condition")
default:
log.
WithFields(log.Fields{
"term": terms[0],
"value": terms[1],
}).
Error("Unknown end point term")
return nil, fmt.Errorf("Unknown end point term '%s'", terms[0])
}
}
}
// Read schema from connection string
u, err = url.Parse(addr)
if err != nil {
log.
WithFields(log.Fields{"connect": addr}).
WithError(err).
Error("Unable to parse connection string")
return nil, err
}
switch strings.ToLower(u.Scheme) {
default:
u.Host = addr
fallthrough
case SchemeTCP:
tcp = (&connections.TCPConnection{
Criteria: match,
}).Initialize()
tcp.Connection, err = net.Dial("tcp", u.Host)
c = tcp
case SchemeHTTP:
c = (&connections.HTTPConnection{
Connection: *u,
Criteria: match,
}).Initialize()
err = nil
}
if err != nil {
log.
WithFields(log.Fields{"connection": addr}).
WithError(err).
Error("Unable to connect to outbound end point")
return nil, err
}
log.WithFields(log.Fields{
"connection": addr,
"c": c,
"host": u.Host,
}).Info("Created outbound end point connection")
// Encapsulated call to ListenAndSend to enable error
// checking
go func(_c connections.Connection) {
for {
if err := _c.ListenAndSend(); err != nil {
if err == connections.ErrUninitialized {
log.
WithError(err).
Fatal("Attempt to use unitialized connection")
} else {
log.
WithError(err).
Fatal("Unexpected error")
}
}
}
}(c)
endpoints[i] = c
}
}
return endpoints, nil
}
// ListenAndServe Listen for connections from open flow devices and process their
// messages
func (app *App) ListenAndServe() (err error) {
// Bind to connection for accepting connections
app.listener, err = net.Listen("tcp", app.ListenOn)
if err != nil {
log.
WithFields(log.Fields{
"listen-port": app.ListenOn,
}).
WithError(err).
Fatalf("Unable to establish the ability to listen on connection for OpenFlow devices")
}
// Loop forever waiting for a connection and processing it
endpoints := app.endpoints
for {
conn, err := app.listener.Accept()
if err != nil {
// Not fatal if a connection fails, forget it and move on
log.
WithError(err).
Error("Error while accepting connection")
continue
}
log.WithFields(log.Fields{
"remote-connection": conn.RemoteAddr().String(),
}).Debug("Received connection")
endpoints = app.endpoints
if !app.ShareConnections {
endpoints, err = app.EstablishEndpointConnections()
if err != nil {
log.
WithError(err).
Error("Unable to establish non-shared outbound endpoint connections")
continue
}
}
go func(_conn net.Conn, _endpoints connections.Endpoints) {
if err := app.handle(_conn, _endpoints); err != nil {
log.
WithError(err).
WithFields(log.Fields{
"connection": _conn,
}).
Error("Connection to device terminated with an error")
}
}(conn, endpoints)
}
}
func main() {
var app App
// This application is not configured by command line options, so
// if we have an unknown options or they used -h/--help to ask for
// usage, give it to them
var flags flag.FlagSet
err := flags.Parse(os.Args[1:])
if err != nil {
if err := envconfig.Usage("", &app); err != nil {
log.
WithError(err).
Error("Unexpected error encountered while displaying usage")
}
return
}
// Load the application configuration from the environment and initialize
// the logging system
err = envconfig.Process("", &app)
if err != nil {
log.WithError(err).Fatal("Unable to parse application configuration")
}
// Set the logging level, if it can't be parsed then default to warning
logLevel, err := log.ParseLevel(app.LogLevel)
if err != nil {
log.
WithFields(log.Fields{
"log-level": app.LogLevel,
}).
WithError(err).
Warn("Unable to parse log level specified, defaulting to Warning")
logLevel = log.WarnLevel
}
log.SetLevel(logLevel)
// If the help message is requested, then display and return
if app.ShowHelp {
if err := envconfig.Usage("", &app); err != nil {
log.
WithError(err).
Error("Unexpected error encountered while displaying usage")
}
return
}
// Create and invoke the API sub-system
app.api = api.NewAPI(app.APIOn, app.CPUProfile, app.MemProfile)
go app.api.ListenAndServe()
// Connect to shared outbound end point connections, if requested
if app.ShareConnections {
if app.endpoints, err = app.EstablishEndpointConnections(); err != nil {
log.WithError(err).Fatal("Unable to establish connections to outbound end points, terminating")
}
}
// Listen and serve device requests
log.Fatal(app.ListenAndServe())
}