blob: 23fab7f4e84a52b153e702ddd1aa647e2a6c4674 [file] [log] [blame]
Zsolt Harasztia133a452016-12-22 01:26:57 -08001import os
2import sys
3import requests
4from termcolor import cprint, colored
5from os.path import join as pjoin
6
7
8def p_cookie(cookie):
9 cookie = str(cookie)
10 if len(cookie) > 8:
11 return cookie[:6] + '...'
12 else:
13 return cookie
14
15'''
16 OFPP_NORMAL = 0x7ffffffa; /* Forward using non-OpenFlow pipeline. */
17 OFPP_FLOOD = 0x7ffffffb; /* Flood using non-OpenFlow pipeline. */
18 OFPP_ALL = 0x7ffffffc; /* All standard ports except input port. */
19 OFPP_CONTROLLER = 0x7ffffffd; /* Send to controller. */
20 OFPP_LOCAL = 0x7ffffffe; /* Local openflow "port". */
21 OFPP_ANY = 0x7fffffff; /* Special value used in some requests when
22'''
23
24
25def p_port(port):
26 if port & 0x7fffffff == 0x7ffffffa:
27 return 'NORMAL'
28 elif port & 0x7fffffff == 0x7ffffffb:
29 return 'FLOOD'
30 elif port & 0x7fffffff == 0x7ffffffc:
31 return 'ALL'
32 elif port & 0x7fffffff == 0x7ffffffd:
33 return 'CONTROLLER'
34 elif port & 0x7fffffff == 0x7ffffffe:
35 return 'LOCAL'
36 elif port & 0x7fffffff == 0x7fffffff:
37 return 'ANY'
38 else:
39 return str(port)
40
41
42def p_vlan_vid(vlan_vid):
43 if vlan_vid == 0:
44 return 'untagged'
45 assert vlan_vid & 4096 == 4096
46 return str(vlan_vid - 4096)
47
48
49def p_ipv4(x):
50 return '.'.join(str(v) for v in [
51 (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff
52 ])
53
54
55field_printers = {
56 'IN_PORT': lambda f: (100, 'in_port', p_port(f['port'])),
57 'VLAN_VID': lambda f: (101, 'vlan_vid', p_vlan_vid(f['vlan_vid'])),
58 'VLAN_PCP': lambda f: (102, 'vlan_pcp', str(f['vlan_pcp'])),
59 'ETH_TYPE': lambda f: (103, 'eth_type', '%X' % f['eth_type']),
60 'IPV4_DST': lambda f: (104, 'ipv4_dst', p_ipv4(f['ipv4_dst'])),
61 'IP_PROTO': lambda f: (105, 'ip_proto', str(f['ip_proto']))
62}
63
64
65def p_field(field):
66 assert field['oxm_class'].endswith('OPENFLOW_BASIC')
67 ofb = field['ofb_field']
68 assert not ofb['has_mask']
69 type = ofb['type'][len('OFPXMT_OFB_'):]
70 weight, field_name, value = field_printers[type](ofb)
71 return 1000 + weight, 'set_' + field_name, value
72
73
74action_printers = {
75 'SET_FIELD': lambda a: p_field(a['set_field']['field']),
76 'POP_VLAN': lambda a: (2000, 'pop_vlan', 'Yes'),
77 'PUSH_VLAN': lambda a: (2001, 'push_vlan', '%x' % a['push']['ethertype']),
78 'GROUP': lambda a: (3000, 'group', p_port(a['group']['group_id'])),
79 'OUTPUT': lambda a: (4000, 'output', p_port(a['output']['port'])),
80}
81
82
83def print_flows(what, id, type, flows, groups):
84
85 print
86 print ''.join([
87 '{} '.format(what),
88 colored(id, color='green', attrs=['bold']),
89 ' (type: ',
90 colored(type, color='blue'),
91 ')'
92 ])
93 print 'Flows ({}):'.format(len(flows))
94
95 max_field_lengths = {}
96 field_names = {}
97
98 def update_max_length(field_key, string):
99 length = len(string)
100 if length > max_field_lengths.get(field_key, 0):
101 max_field_lengths[field_key] = length
102
103 def add_field_type(field_key, field_name):
104 if field_key not in field_names:
105 field_names[field_key] = field_name
106 update_max_length(field_key, field_name)
107 else:
108 assert field_names[field_key] == field_name
109
110 cell_values = {}
111
112 # preprocess data
113 if not flows:
114 return
115 for i, flow in enumerate(flows):
116
117 def add_field(field_key, field_name, value):
118 add_field_type(field_key, field_name)
119 row = cell_values.setdefault(i, {})
120 row[field_key] = value
121 update_max_length(field_key, value)
122
123 add_field(0, 'table_id', value=str(flow['table_id']))
124 add_field(1, 'priority', value=str(flow['priority']))
125 add_field(2, 'cookie', p_cookie(flow['cookie']))
126
127 assert flow['match']['type'] == 'OFPMT_OXM'
128 for field in flow['match']['oxm_fields']:
129 assert field['oxm_class'].endswith('OPENFLOW_BASIC')
130 ofb = field['ofb_field']
131 assert not ofb['has_mask'], 'masked match not handled yet' # TODO
132 type = ofb['type'][len('OFPXMT_OFB_'):]
133 add_field(*field_printers[type](ofb))
134
135 for instruction in flow['instructions']:
136 if instruction['type'] == 4:
137 for action in instruction['actions']['actions']:
138 type = action['type'][len('OFPAT_'):]
139 add_field(*action_printers[type](action))
140
141 # print header
142 field_keys = sorted(field_names.keys())
143 def p_sep():
144 print '+' + '+'.join(
145 [(max_field_lengths[k] + 2) * '-' for k in field_keys]) + '+'
146
147 p_sep()
148 print '| ' + ' | '.join(
149 '%%%ds' % max_field_lengths[k] % field_names[k]
150 for k in field_keys) + ' |'
151 p_sep()
152
153 # print values
154 for i in xrange(len(flows)):
155 row = cell_values[i]
156 cprint('| ' + ' | '.join(
157 '%%%ds' % max_field_lengths[k] % row.get(k, '')
158 for k in field_keys
159 ) + ' |')
160 if not ((i + 1) % 3):
161 p_sep()
162
163 if ((i + 1) % 3):
164 p_sep()
165
166 # TODO groups TBF
167 assert len(groups) == 0
168
169