Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 17 | import sys |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 18 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 19 | from google.protobuf.json_format import MessageToDict |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 20 | from termcolor import cprint, colored |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 21 | |
| 22 | from cli.table import TablePrinter |
| 23 | |
| 24 | |
| 25 | _printfn = lambda l: sys.stdout.write(l + '\n') |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 26 | |
| 27 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 28 | def pb2dict(pb_msg): |
| 29 | d = MessageToDict(pb_msg, including_default_value_fields=1, |
| 30 | preserving_proto_field_name=1) |
| 31 | return d |
| 32 | |
| 33 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 34 | def p_cookie(cookie): |
Gertjan Van Droogenbroeck | 308a9cf | 2017-10-11 15:59:51 -0500 | [diff] [blame] | 35 | cookie = '%x' % int(cookie) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 36 | if len(cookie) > 8: |
Gertjan Van Droogenbroeck | 308a9cf | 2017-10-11 15:59:51 -0500 | [diff] [blame] | 37 | return '~' + cookie[len(cookie)-8:] |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 38 | else: |
| 39 | return cookie |
| 40 | |
| 41 | ''' |
| 42 | OFPP_NORMAL = 0x7ffffffa; /* Forward using non-OpenFlow pipeline. */ |
| 43 | OFPP_FLOOD = 0x7ffffffb; /* Flood using non-OpenFlow pipeline. */ |
| 44 | OFPP_ALL = 0x7ffffffc; /* All standard ports except input port. */ |
| 45 | OFPP_CONTROLLER = 0x7ffffffd; /* Send to controller. */ |
| 46 | OFPP_LOCAL = 0x7ffffffe; /* Local openflow "port". */ |
| 47 | OFPP_ANY = 0x7fffffff; /* Special value used in some requests when |
| 48 | ''' |
| 49 | |
| 50 | |
| 51 | def p_port(port): |
| 52 | if port & 0x7fffffff == 0x7ffffffa: |
| 53 | return 'NORMAL' |
| 54 | elif port & 0x7fffffff == 0x7ffffffb: |
| 55 | return 'FLOOD' |
| 56 | elif port & 0x7fffffff == 0x7ffffffc: |
| 57 | return 'ALL' |
| 58 | elif port & 0x7fffffff == 0x7ffffffd: |
| 59 | return 'CONTROLLER' |
| 60 | elif port & 0x7fffffff == 0x7ffffffe: |
| 61 | return 'LOCAL' |
| 62 | elif port & 0x7fffffff == 0x7fffffff: |
| 63 | return 'ANY' |
| 64 | else: |
| 65 | return str(port) |
| 66 | |
| 67 | |
| 68 | def p_vlan_vid(vlan_vid): |
| 69 | if vlan_vid == 0: |
| 70 | return 'untagged' |
| 71 | assert vlan_vid & 4096 == 4096 |
| 72 | return str(vlan_vid - 4096) |
| 73 | |
| 74 | |
| 75 | def p_ipv4(x): |
| 76 | return '.'.join(str(v) for v in [ |
| 77 | (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff |
| 78 | ]) |
| 79 | |
| 80 | |
| 81 | field_printers = { |
| 82 | 'IN_PORT': lambda f: (100, 'in_port', p_port(f['port'])), |
| 83 | 'VLAN_VID': lambda f: (101, 'vlan_vid', p_vlan_vid(f['vlan_vid'])), |
| 84 | 'VLAN_PCP': lambda f: (102, 'vlan_pcp', str(f['vlan_pcp'])), |
| 85 | 'ETH_TYPE': lambda f: (103, 'eth_type', '%X' % f['eth_type']), |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 86 | 'IP_PROTO': lambda f: (104, 'ip_proto', str(f['ip_proto'])), |
| 87 | 'IPV4_DST': lambda f: (105, 'ipv4_dst', p_ipv4(f['ipv4_dst'])), |
| 88 | 'UDP_SRC': lambda f: (106, 'udp_src', str(f['udp_src'])), |
| 89 | 'UDP_DST': lambda f: (107, 'udp_dst', str(f['udp_dst'])), |
| 90 | 'TCP_SRC': lambda f: (108, 'tcp_src', str(f['tcp_src'])), |
| 91 | 'TCP_DST': lambda f: (109, 'tcp_dst', str(f['tcp_dst'])), |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 92 | 'METADATA': lambda f: (110, 'metadata', str(f['table_metadata'])), |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | def p_field(field): |
| 97 | assert field['oxm_class'].endswith('OPENFLOW_BASIC') |
| 98 | ofb = field['ofb_field'] |
| 99 | assert not ofb['has_mask'] |
| 100 | type = ofb['type'][len('OFPXMT_OFB_'):] |
| 101 | weight, field_name, value = field_printers[type](ofb) |
| 102 | return 1000 + weight, 'set_' + field_name, value |
| 103 | |
| 104 | |
| 105 | action_printers = { |
| 106 | 'SET_FIELD': lambda a: p_field(a['set_field']['field']), |
| 107 | 'POP_VLAN': lambda a: (2000, 'pop_vlan', 'Yes'), |
| 108 | 'PUSH_VLAN': lambda a: (2001, 'push_vlan', '%x' % a['push']['ethertype']), |
| 109 | 'GROUP': lambda a: (3000, 'group', p_port(a['group']['group_id'])), |
| 110 | 'OUTPUT': lambda a: (4000, 'output', p_port(a['output']['port'])), |
| 111 | } |
| 112 | |
| 113 | |
ntoorchi | 9cb00a7 | 2019-07-17 10:43:54 -0700 | [diff] [blame] | 114 | def print_flows(what, id, type, flows, groups, printfn=_printfn, flows_info=[], fields_to_omit=[]): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 115 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 116 | header = ''.join([ |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 117 | '{} '.format(what), |
| 118 | colored(id, color='green', attrs=['bold']), |
| 119 | ' (type: ', |
| 120 | colored(type, color='blue'), |
| 121 | ')' |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 122 | ]) + '\nFlows ({}):'.format(len(flows)) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 123 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 124 | table = TablePrinter() |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 125 | for i, flow in enumerate(flows): |
| 126 | |
ntoorchi | 9cb00a7 | 2019-07-17 10:43:54 -0700 | [diff] [blame] | 127 | if flows_info: |
| 128 | flow_info = flows_info[i] |
| 129 | else: |
| 130 | flow_info = dict() |
| 131 | |
| 132 | if 'table_id' not in fields_to_omit: |
| 133 | table.add_cell(i, 0, 'table_id', value=str(flow['table_id'])) |
| 134 | if 'flow_id' not in fields_to_omit and 'flow_id' in flow_info: |
| 135 | table.add_cell(i, 1, 'flow_id', value=str(flow_info['flow_id'])) |
| 136 | if 'flow_category' not in fields_to_omit and 'flow_category' in flow_info: |
| 137 | table.add_cell(i, 2, 'flow_category', value=str(flow_info['flow_category'])) |
| 138 | if 'flow_type' not in fields_to_omit and 'flow_type' in flow_info: |
| 139 | table.add_cell(i, 3, 'flow_type', value=str(flow_info['flow_type'])) |
| 140 | if 'priority' not in fields_to_omit: |
| 141 | table.add_cell(i, 4, 'priority', value=str(flow['priority'])) |
| 142 | if 'gemport_id' not in fields_to_omit and 'gemport_id' in flow_info: |
| 143 | table.add_cell(i, 5, 'gemport_id', value=str(flow_info['gemport_id'])) |
| 144 | if 'alloc_id' not in fields_to_omit and 'alloc_id' in flow_info: |
| 145 | table.add_cell(i, 6, 'alloc_id', value=str(flow_info['alloc_id'])) |
| 146 | if 'o_pbits' not in fields_to_omit and 'o_pbits' in flow_info: |
| 147 | table.add_cell(i, 7, 'o_pbits', value=str(flow_info['o_pbits'])) |
| 148 | if 'pon_intf_onu_id' not in fields_to_omit and 'pon_intf_onu_id' in flow_info: |
| 149 | table.add_cell(i, 8, 'intf_onu_id', value=str(flow_info['pon_intf_onu_id'])) |
| 150 | if 'cookie' not in fields_to_omit: |
| 151 | table.add_cell(i, 9, 'cookie', p_cookie(flow['cookie'])) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 152 | |
| 153 | assert flow['match']['type'] == 'OFPMT_OXM' |
| 154 | for field in flow['match']['oxm_fields']: |
| 155 | assert field['oxm_class'].endswith('OPENFLOW_BASIC') |
| 156 | ofb = field['ofb_field'] |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 157 | type = ofb['type'][len('OFPXMT_OFB_'):] |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 158 | table.add_cell(i, *field_printers[type](ofb)) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 159 | |
| 160 | for instruction in flow['instructions']: |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 161 | itype = instruction['type'] |
Koray Kokten | efcdf52 | 2018-12-06 00:16:56 +0300 | [diff] [blame] | 162 | if itype == 4 or itype == 3: |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 163 | for action in instruction['actions']['actions']: |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 164 | atype = action['type'][len('OFPAT_'):] |
| 165 | table.add_cell(i, *action_printers[atype](action)) |
| 166 | elif itype == 1: |
ntoorchi | 9cb00a7 | 2019-07-17 10:43:54 -0700 | [diff] [blame] | 167 | if 'goto-table' not in fields_to_omit: |
| 168 | table.add_cell(i, 10000, 'goto-table', |
| 169 | instruction['goto_table']['table_id']) |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 170 | elif itype == 2: |
ntoorchi | 9cb00a7 | 2019-07-17 10:43:54 -0700 | [diff] [blame] | 171 | if 'write-metadata' not in fields_to_omit: |
| 172 | table.add_cell(i, 10001, 'write-metadata', |
| 173 | instruction['write_metadata']['metadata']) |
Gamze Abaka | 61c2e98 | 2018-02-14 11:03:36 +0000 | [diff] [blame] | 174 | elif itype == 5: |
ntoorchi | 9cb00a7 | 2019-07-17 10:43:54 -0700 | [diff] [blame] | 175 | if 'clear-actions' not in fields_to_omit: |
| 176 | table.add_cell(i, 10002, 'clear-actions', []) |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 177 | elif itype == 6: |
ntoorchi | 9cb00a7 | 2019-07-17 10:43:54 -0700 | [diff] [blame] | 178 | if 'meter' not in fields_to_omit: |
| 179 | table.add_cell(i, 10003, 'meter', |
| 180 | instruction['meter']['meter_id']) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 181 | else: |
| 182 | raise NotImplementedError( |
| 183 | 'not handling instruction type {}'.format(itype)) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 184 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 185 | table.print_table(header, printfn) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 186 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 187 | |
Gertjan Van Droogenbroeck | 308a9cf | 2017-10-11 15:59:51 -0500 | [diff] [blame] | 188 | def print_groups(what, id, type, groups, printfn=_printfn): |
| 189 | header = ''.join([ |
| 190 | '{} '.format(what), |
| 191 | colored(id, color='green', attrs=['bold']), |
| 192 | ' (type: ', |
| 193 | colored(type, color='blue'), |
| 194 | ')' |
| 195 | ]) + '\nGroups ({}):'.format(len(groups)) |
| 196 | |
| 197 | table = TablePrinter() |
| 198 | for i, group in enumerate(groups): |
| 199 | output_ports = [] |
| 200 | for bucket in group['desc']['buckets']: |
| 201 | for action in bucket['actions']: |
| 202 | if action['type'] == 'OFPAT_OUTPUT': |
| 203 | output_ports.append(action['output']['port']) |
| 204 | table.add_cell(i, 0, 'group_id', value=str(group['desc']['group_id'])) |
| 205 | table.add_cell(i, 1, 'buckets', value=str(dict(output=output_ports))) |
| 206 | |
| 207 | table.print_table(header, printfn) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 208 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 209 | |
| 210 | def print_meters(what, id, type, meters, printfn=_printfn): |
| 211 | header = ''.join([ |
| 212 | '{} '.format(what), |
| 213 | colored(id, color='green', attrs=['bold']), |
| 214 | ' (type: ', |
| 215 | colored(type, color='blue'), |
| 216 | ')' |
| 217 | ]) + '\nMeters ({}):'.format(len(meters)) |
| 218 | |
| 219 | table = TablePrinter() |
| 220 | for i, meter in enumerate(meters): |
| 221 | bands = [] |
| 222 | for meter_band in meter['config']['bands']: |
| 223 | bands.append(meter_band) |
| 224 | table.add_cell(i, 0, 'meter_id', value=str(meter['config']['meter_id'])) |
| 225 | table.add_cell(i, 1, 'meter_bands', value=str(dict(bands=bands))) |
| 226 | |
| 227 | table.print_table(header, printfn) |
| 228 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 229 | def dict2line(d): |
| 230 | assert isinstance(d, dict) |
| 231 | return ', '.join('{}: {}'.format(k, v) for k, v in sorted(d.items())) |
Lydia Fang | 01f2e85 | 2017-06-28 17:24:58 -0700 | [diff] [blame] | 232 | |
| 233 | def enum2name(msg_obj, enum_type, enum_value): |
| 234 | descriptor = msg_obj.DESCRIPTOR.enum_types_by_name[enum_type] |
| 235 | name = descriptor.values_by_number[enum_value].name |
| 236 | return name |