-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoftraf.py
459 lines (392 loc) · 16.9 KB
/
oftraf.py
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
# Copyright (c) 2015 Intracom S.A. Telecom Solutions. All rights reserved.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v1.0 which accompanies this distribution,
# and is available at http://www.eclipse.org/legal/epl-v10.html
""" OpenFlow traffic monitor """
import argparse
import bottle
import curses
import dpkt
import json
import os
import pcap
import time
import threading
from sets import Set
of10_valid_types = Set(['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
'\x10', '\x11', '\x12', '\x13', '\x14', '\x15'])
"""set of str: the valid OpenFlow 1.0 message types
"""
of10_types = {
'\x00': 'OFPT_HELLO',
'\x01': 'OFPT_ERROR',
'\x02': 'OFPT_ECHO_REQUEST',
'\x03': 'OFPT_ECHO_REPLY',
'\x04': 'OFPT_VENDOR',
'\x05': 'OFPT_FEATURES_REQUEST',
'\x06': 'OFPT_FEATURES_REPLY',
'\x07': 'OFPT_GET_CONFIG_REQUEST',
'\x08': 'OFPT_GET_CONFIG_REPLY',
'\x09': 'OFPT_SET_CONFIG',
'\x0a': 'OFPT_PACKET_IN',
'\x0b': 'OFPT_FLOW_REMOVED',
'\x0c': 'OFPT_PORT_STATUS',
'\x0d': 'OFPT_PACKET_OUT',
'\x0e': 'OFPT_FLOW_MOD',
'\x0f': 'OFPT_PORT_MOD',
'\x10': 'OFPT_STATS_REQUEST',
'\x11': 'OFPT_STATS_REPLY',
'\x12': 'OFPT_BARRIER_REQUEST',
'\x13': 'OFPT_BARRIER_REPLY',
'\x14': 'OFPT_QUEUE_GET_CONFIG_REQUEST',
'\x15': 'OFPT_QUEUE_GET_CONFIG_REPLY'
}
"""dict of str:str: maps a packet type to an OpenFlow 1.0 message type
"""
of13_valid_types = Set(['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
'\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d'])
"""set of str: the valid OpenFlow 1.3 message types
"""
of13_types = {
'\x00': 'OFPT_HELLO',
'\x01': 'OFPT_ERROR',
'\x02': 'OFPT_ECHO_REQUEST',
'\x03': 'OFPT_ECHO_REPLY',
'\x04': 'OFPT_EXPERIMENTER',
'\x05': 'OFPT_FEATURES_REQUEST',
'\x06': 'OFPT_FEATURES_REPLY',
'\x07': 'OFPT_GET_CONFIG_REQUEST',
'\x08': 'OFPT_GET_CONFIG_REPLY',
'\x09': 'OFPT_SET_CONFIG',
'\x0a': 'OFPT_PACKET_IN',
'\x0b': 'OFPT_FLOW_REMOVED',
'\x0c': 'OFPT_PORT_STATUS',
'\x0d': 'OFPT_PACKET_OUT',
'\x0e': 'OFPT_FLOW_MOD',
'\x0f': 'OFPT_GROUP_MOD',
'\x10': 'OFPT_PORT_MOD',
'\x11': 'OFPT_TABLE_MOD',
'\x12': 'OFPT_MULTIPART_REQUEST',
'\x13': 'OFPT_MULTIPART_REPLY',
'\x14': 'OFPT_BARRIER_REQUEST',
'\x15': 'OFPT_BARRIER_REPLY',
'\x16': 'OFPT_QUEUE_GET_CONFIG_REQUEST',
'\x17': 'OFPT_QUEUE_GET_CONFIG_REPLY',
'\x18': 'OFPT_ROLE_REQUEST',
'\x19': 'OFPT_ROLE_REPLY',
'\x1a': 'OFPT_GET_ASYNC_REQUEST',
'\x1b': 'OFPT_GET_ASYNC_REPLY',
'\x1c': 'OFPT_SET_ASYNC',
'\x1d': 'OFPT_METER_MOD'
}
"""dict of str:str: maps a packet type to an OpenFlow 1.3 message type
"""
of10_in_counts = {}
"""dict of str:list: maps an incoming OF10 message type to a list with total counts and
bytes for that type
"""
of10_out_counts = {}
"""dict of str:list: maps an outgoing OF10 message type to a list with total counts and
bytes for that type
"""
of13_in_counts = {}
"""dict of str:list: maps an incoming OF13 message type to a list with total counts and
bytes for that type
"""
of13_out_counts = {}
"""dict of str:list: maps an outgoing OF13 message type to a list with total counts and
bytes for that type
"""
tcp_of_in_counts = [0L, 0L]
"""list of long: list with total counts and bytes for the incoming tcp packets
with OF payload
"""
of_in_counts = [0L, 0L]
"""list of long: list with total counts and bytes for the incoming OF packets
"""
tcp_of_out_counts = [0L, 0L]
"""list of long: list with total counts and bytes for the outgoing tcp packets
with OF payload
"""
of_out_counts = [0L, 0L]
"""list of long: list with total counts and bytes for the outgoing OF packets
"""
def get_length_field_value(payload):
"""Gets the value of length field (16 bit field) of OpenFlow payload.
OpenFlow header defines a 16 bit value, called length. This is the number
of bytes of the corresponded OpenFlow packet, including the header bytes.
Args:
payload (list<char>): bytes list of tcp payload. We want this length
value as an integer.
Returns:
Length of an OpenFlow packet in number of bytes
"""
length_high = hex(ord(payload[2]))
length_low = hex(ord(payload[3]))
length = int(length_high + length_low.replace('0x',''), 16)
return length
def of_sniff(ifname, ofport):
"""Sniff the specified interface for OF packets at the specified OF port.
Packets will be filtered based on whether their TCP src port or TCP dst port
equals the OF port given.
Args:
ifname (str): network interface name
ofport (int): OF port number used for packet filtering
"""
tcp_pkts_malformed = 0L
of10_in_pkts_malformed = 0L
of10_out_pkts_malformed = 0L
of13_in_pkts_malformed = 0L
of13_out_pkts_malformed = 0L
try:
for _, pkt in pcap.pcap(name=ifname, immediate=False):
eth = dpkt.ethernet.Ethernet(pkt)
if hasattr(eth.data, 'data'):
tcp = eth.data.data
else:
continue
if type(tcp) != dpkt.tcp.TCP:
tcp_pkts_malformed += 1
continue
# do not further analyze the packet if it does not have any
# OpenFlow payload
payload = tcp.data
nbytes = len(payload)
if nbytes <= 1:
continue
# Check if tcp has openflow payload
if tcp.dport != int(ofport) and tcp.sport != int(ofport):
continue
# element 0: tcp with OF payload count
# element 1: total tcp with OF payload bytes
if tcp.dport == int(ofport):
tcp_of_in_counts[0] += 1
tcp_of_in_counts[1] += nbytes
elif tcp.sport == int(ofport):
tcp_of_out_counts[0] += 1
tcp_of_out_counts[1] += nbytes
#List of all encapsulated OpenFlow messages in tcp payload
of_packets = []
# OpenFlow header is 4 bytes
while(len(payload) >= 4):
of_length = get_length_field_value(payload)
if of_length == 0:
of_packets.append(payload)
break
of_packets.append(payload[0:of_length-1])
payload = payload[of_length:len(payload)]
for of_packet in of_packets:
of_packet_bytes = len(of_packet)
if of_packet_bytes < 4:
continue
of_version = of_packet[0]
of_type = of_packet[1]
# OF1.0
if of_version == '\x01':
# Incoming message
if tcp.dport == int(ofport):
if of_type not in of10_valid_types:
of10_in_pkts_malformed += 1
continue
if not of10_types[of_type] in of10_in_counts:
of10_in_counts[of10_types[of_type]] = [1L, long(of_packet_bytes)]
else:
of10_in_counts[of10_types[of_type]][0] += 1L
of10_in_counts[of10_types[of_type]][1] += long(of_packet_bytes)
of_in_counts[0] += 1L
of_in_counts[1] += long(of_packet_bytes)
# Outgoing message
elif tcp.sport == int(ofport):
if of_type not in of10_valid_types:
of10_out_pkts_malformed += 1
continue
if not of10_types[of_type] in of10_out_counts:
of10_out_counts[of10_types[of_type]] = [1L, long(of_packet_bytes)]
else:
of10_out_counts[of10_types[of_type]][0] += 1L
of10_out_counts[of10_types[of_type]][1] += long(of_packet_bytes)
of_out_counts[0] += 1L
of_out_counts[1] += long(of_packet_bytes)
# OF1.3
elif of_version == '\x04':
# Incoming message
if tcp.dport == int(ofport):
if of_type not in of13_valid_types:
of13_in_pkts_malformed += 1
continue
if not of13_types[of_type] in of13_in_counts:
of13_in_counts[of13_types[of_type]] = [1L, long(of_packet_bytes)]
else:
of13_in_counts[of13_types[of_type]][0] += 1L
of13_in_counts[of13_types[of_type]][1] += long(of_packet_bytes)
of_in_counts[0] += 1L
of_in_counts[1] += long(of_packet_bytes)
# Outgoing message
elif tcp.sport == int(ofport):
if of_type not in of13_valid_types:
of13_out_pkts_malformed += 1
continue
if not of13_types[of_type] in of13_out_counts:
of13_out_counts[of13_types[of_type]] = [1L, long(of_packet_bytes)]
else:
of13_out_counts[of13_types[of_type]][0] += 1L
of13_out_counts[of13_types[of_type]][1] += long(of_packet_bytes)
of_out_counts[0] += 1L
of_out_counts[1] += long(of_packet_bytes)
except KeyboardInterrupt:
os._exit(1)
def print_stats():
""" Print statistics in an endless loop
"""
start = time.time()
prev_in = [0,0]
prev_out = [0,0]
sleep_secs = 1
try:
win = curses.initscr()
while True:
time.sleep(sleep_secs)
curr_in = of_in_counts
curr_out = of_out_counts
tcp_curr_in = tcp_of_in_counts
tcp_curr_out = tcp_of_out_counts
curr_of10_in = of10_in_counts
curr_of10_out = of10_out_counts
curr_of13_in = of13_in_counts
curr_of13_out = of13_out_counts
in_rate = [(b-a)/float(sleep_secs) for (a,b) in zip(prev_in, curr_in)]
out_rate = [(b-a)/float(sleep_secs) for (a,b) in zip(prev_out, curr_out)]
elapsed_secs = '%.4f' % (time.time() - start)
out = '{0}{1:15}\n'.format('Elapsed seconds:', elapsed_secs)
out += '{0:15}{1:15}\n'.format('OF in pps:', in_rate[0])
out += '{0:15}{1:15}\n'.format('OF in Bps:', in_rate[1])
out += '{0:15}{1:15}\n'.format('OF out pps:', out_rate[0])
out += '{0:15}{1:15}\n'.format('OF out Bps:', out_rate[1])
out += '\n'
out += '{0:38}{1:15}{2:15}\n'.format('Packet Type', 'Count', 'Bytes')
out += '--------------------------------------------------------------\n'
out += '{0:38}{1:15}{2:15}\n'.format('Total OF in:',
str(curr_in[0]),
str(curr_in[1]))
out += '{0:38}{1:15}{2:15}\n'.format('Total OF out:',
str(curr_out[0]),
str(curr_out[1]))
out += '{0:38}{1:15}{2:15}\n'.format('Total TCP with OF payload in:',
str(tcp_curr_in[0]),
str(tcp_curr_in[1]))
out += '{0:38}{1:15}{2:15}\n'.format('Total TCP with OF payload out:',
str(tcp_curr_out[0]),
str(tcp_curr_out[1]))
out += '\nIncoming\n'
out += '----------------\n'
for key in sorted(of13_in_counts):
out += '{0:38}{1:15}{2:15}\n'.format('OF13_' + key + ':',
str(curr_of13_in[key][0]),
str(curr_of13_in[key][1]))
for key in sorted(of10_in_counts):
out += '{0:38}{1:15}{2:15}\n'.format('OF10_' + key + ':',
str(curr_of10_in[key][0]),
str(curr_of10_in[key][1]))
out += '\nOutgoing\n'
out += '----------------\n'
for key in sorted(of13_out_counts):
out += '{0:38}{1:15}{2:15}\n'.format('OF13_' + key + ':',
str(curr_of13_out[key][0]),
str(curr_of13_out[key][1]))
for key in sorted(of10_out_counts):
out += '{0:38}{1:15}{2:15}\n'.format('OF10_' + key + ':',
str(curr_of10_out[key][0]),
str(curr_of10_out[key][1]))
prev_in = list(curr_in)
prev_out = list(curr_out)
win.addstr(0,0, out)
win.refresh()
except KeyboardInterrupt:
os._exit(1)
@bottle.route('/get_of_counts', method='GET')
def get_of_counts():
""" Callback function for summary statistics
Returns:
str: JSON object with total OF in and OF out counts
"""
cnts = {}
cnts["OF_in_counts"] = of_in_counts
cnts["OF_out_counts"] = of_out_counts
cnts["TCP_OF_in_counts"] = tcp_of_in_counts
cnts["TCP_OF_out_counts"] = tcp_of_out_counts
return bottle.HTTPResponse(status=200,
headers={'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'},
body=json.dumps(cnts))
@bottle.route('/get_of10_counts', method='GET')
def get_of10_counts():
""" Callback function for detailed OF10 statistics
Returns:
str: JSON object with detailed OF10 counts
"""
cnts = {}
cnts["OF10_in_counts"] = of10_in_counts
cnts["OF10_out_counts"] = of10_out_counts
return bottle.HTTPResponse(status=200,
headers={'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'},
body=json.dumps(cnts))
@bottle.route('/get_of13_counts', method='GET')
def get_of13_counts():
""" Callback function for detailed OF13 statistics
Returns:
str: JSON object with detailed OF13 counts
"""
cnts = {}
cnts["OF13_in_counts"] = of13_in_counts
cnts["OF13_out_counts"] = of13_out_counts
return bottle.HTTPResponse(status=200,
headers={'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'},
body=json.dumps(cnts))
@bottle.route('/stop', method='GET')
def stop():
""" Callback function for stopping oftraf
"""
os._exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--rest-host',
required=True,
type=str,
dest='rest_host',
action='store',
help='IP address or hostname of the interface the REST'
' server should listen to')
parser.add_argument('--rest-port',
required=True,
type=str,
dest='rest_port',
action='store',
help='Port number the REST server should listen to')
parser.add_argument('--of-port',
required=True,
type=str,
dest='of_port',
action='store',
help='OpenFlow port number based on which packet'
' filtering will take place')
parser.add_argument('--ifname',
required=True,
type=str,
dest='ifname',
action='store',
help='Network interface to sniff OF packets from')
parser.add_argument('--server',
required=False,
dest='is_server',
action='store_true',
default=False,
help='Run oftraf as server only without printing stats')
args = parser.parse_args()
sniffer = threading.Thread(target=of_sniff, args=(args.ifname, args.of_port))
sniffer.daemon = True
sniffer.start()
if not args.is_server:
poller = threading.Thread(target=print_stats)
poller.daemon = True
poller.start()
bottle.run(host=args.rest_host, port=args.rest_port, quiet=True)