def get_port_desc(dp, waiters, to_user=True): stats = dp.ofproto_parser.OFPPortDescStatsRequest(dp, 0) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) descs = [] for msg in msgs: stats = msg.body for stat in stats: d = { 'hw_addr': stat.hw_addr, 'name': stat.name.decode('utf-8', errors='replace'), 'config': stat.config, 'state': stat.state, 'curr': stat.curr, 'advertised': stat.advertised, 'supported': stat.supported, 'peer': stat.peer, 'curr_speed': stat.curr_speed, 'max_speed': stat.max_speed } if to_user: d['port_no'] = UTIL.ofp_port_to_user(stat.port_no) else: d['port_no'] = stat.port_no descs.append(d) return wrap_dpid_dict(dp, descs, to_user)
def get_group_stats(dp, waiters, group_id=None, to_user=True): if group_id is None: group_id = dp.ofproto.OFPG_ALL else: group_id = UTIL.ofp_group_from_user(group_id) stats = dp.ofproto_parser.OFPGroupStatsRequest(dp, 0, group_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) groups = [] for msg in msgs: for stats in msg.body: g = stats.to_jsondict()[stats.__class__.__name__] bucket_stats = [] for bucket_stat in stats.bucket_stats: c = bucket_stat.to_jsondict()[bucket_stat.__class__.__name__] bucket_stats.append(c) g['bucket_stats'] = bucket_stats if to_user: g['group_id'] = UTIL.ofp_group_to_user(stats.group_id) groups.append(g) return wrap_dpid_dict(dp, groups, to_user)
def get_port_desc(dp, waiters): stats = dp.ofproto_parser.OFPFeaturesRequest(dp) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) descs = [] for msg in msgs: stats = msg.ports for stat in stats.values(): d = { 'port_no': UTIL.ofp_port_to_user(stat.port_no), 'hw_addr': stat.hw_addr, 'name': stat.name.decode('utf-8'), 'config': stat.config, 'state': stat.state, 'curr': stat.curr, 'advertised': stat.advertised, 'supported': stat.supported, 'peer': stat.peer, 'curr_speed': stat.curr_speed, 'max_speed': stat.max_speed } descs.append(d) return {str(dp.id): descs}
def get_group_stats(dp, waiters, group_id=None): if group_id is None: group_id = dp.ofproto.OFPG_ALL else: group_id = str_to_int(group_id) stats = dp.ofproto_parser.OFPGroupStatsRequest(dp, group_id, 0) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) groups = [] for msg in msgs: for stats in msg.body: bucket_counters = [] for bucket_counter in stats.bucket_counters: c = { 'packet_count': bucket_counter.packet_count, 'byte_count': bucket_counter.byte_count } bucket_counters.append(c) g = { 'length': stats.length, 'group_id': UTIL.ofp_group_to_user(stats.group_id), 'ref_count': stats.ref_count, 'packet_count': stats.packet_count, 'byte_count': stats.byte_count, 'bucket_stats': bucket_counters } groups.append(g) return {str(dp.id): groups}
def get_queue_stats(dp, waiters, port=None, queue_id=None, to_user=True): ofp = dp.ofproto if port is None: port = ofp.OFPP_ANY else: port = str_to_int(port) if queue_id is None: queue_id = ofp.OFPQ_ALL else: queue_id = str_to_int(queue_id) stats = dp.ofproto_parser.OFPQueueStatsRequest(dp, 0, port, queue_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) s = [] for msg in msgs: stats = msg.body for stat in stats: s.append({ 'duration_nsec': stat.duration_nsec, 'duration_sec': stat.duration_sec, 'port_no': stat.port_no, 'queue_id': stat.queue_id, 'tx_bytes': stat.tx_bytes, 'tx_errors': stat.tx_errors, 'tx_packets': stat.tx_packets }) return wrap_dpid_dict(dp, s, to_user)
def get_meter_stats(dp, waiters, meter_id=None, to_user=True): if meter_id is None: meter_id = dp.ofproto.OFPM_ALL else: meter_id = UTIL.ofp_meter_from_user(meter_id) stats = dp.ofproto_parser.OFPMeterStatsRequest(dp, 0, meter_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) meters = [] for msg in msgs: for stats in msg.body: s = stats.to_jsondict()[stats.__class__.__name__] bands = [] for band in stats.band_stats: b = band.to_jsondict()[band.__class__.__name__] bands.append(b) s['band_stats'] = bands if to_user: s['meter_id'] = UTIL.ofp_meter_to_user(stats.meter_id) meters.append(s) return wrap_dpid_dict(dp, meters, to_user)
def get_port_desc(dp, waiters, port_no=None, to_user=True): if port_no is None: port_no = dp.ofproto.OFPP_ANY else: port_no = UTIL.ofp_port_from_user(port_no) stats = dp.ofproto_parser.OFPPortDescStatsRequest(dp, 0, port_no) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) descs = [] for msg in msgs: stats = msg.body for stat in stats: d = stat.to_jsondict()[stat.__class__.__name__] properties = [] for prop in stat.properties: p = prop.to_jsondict()[prop.__class__.__name__] if to_user: t = UTIL.ofp_port_desc_prop_type_to_user(prop.type) p['type'] = t if t != prop.type else 'UNKNOWN' properties.append(p) d['name'] = stat.name.decode('utf-8') d['properties'] = properties if to_user: d['port_no'] = UTIL.ofp_port_to_user(stat.port_no) descs.append(d) return wrap_dpid_dict(dp, descs, to_user)
def get_queue_desc(dp, waiters, port_no=None, queue_id=None, to_user=True): if port_no is None: port_no = dp.ofproto.OFPP_ANY else: port_no = UTIL.ofp_port_from_user(port_no) if queue_id is None: queue_id = dp.ofproto.OFPQ_ALL else: queue_id = UTIL.ofp_queue_from_user(queue_id) stats = dp.ofproto_parser.OFPQueueDescStatsRequest(dp, 0, port_no, queue_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) configs = [] for msg in msgs: for queue in msg.body: q = queue.to_jsondict()[queue.__class__.__name__] prop_list = [] for prop in queue.properties: p = prop.to_jsondict()[prop.__class__.__name__] if to_user: t = UTIL.ofp_queue_desc_prop_type_to_user(prop.type) p['type'] = t if t != prop.type else 'UNKNOWN' prop_list.append(p) q['properties'] = prop_list configs.append(q) return wrap_dpid_dict(dp, configs, to_user)
def get_port_stats(dp, waiters, port=None): if port is None: port = dp.ofproto.OFPP_NONE else: port = str_to_int(port) stats = dp.ofproto_parser.OFPPortStatsRequest( dp, 0, port) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) ports = [] for msg in msgs: for stats in msg.body: s = {'port_no': UTIL.ofp_port_to_user(stats.port_no), 'rx_packets': stats.rx_packets, 'tx_packets': stats.tx_packets, 'rx_bytes': stats.rx_bytes, 'tx_bytes': stats.tx_bytes, 'rx_dropped': stats.rx_dropped, 'tx_dropped': stats.tx_dropped, 'rx_errors': stats.rx_errors, 'tx_errors': stats.tx_errors, 'rx_frame_err': stats.rx_frame_err, 'rx_over_err': stats.rx_over_err, 'rx_crc_err': stats.rx_crc_err, 'collisions': stats.collisions} ports.append(s) return {str(dp.id): ports}
def get_queue_stats(dp, waiters, port=None, queue_id=None): if port is None: port = dp.ofproto.OFPP_ALL else: port = str_to_int(port) if queue_id is None: queue_id = dp.ofproto.OFPQ_ALL else: queue_id = str_to_int(queue_id) stats = dp.ofproto_parser.OFPQueueStatsRequest(dp, 0, port, queue_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) s = [] for msg in msgs: stats = msg.body for stat in stats: s.append({'port_no': stat.port_no, 'queue_id': stat.queue_id, 'tx_bytes': stat.tx_bytes, 'tx_errors': stat.tx_errors, 'tx_packets': stat.tx_packets}) return {str(dp.id): s}
def get_aggregate_flow_stats(dp, waiters, flow=None): flow = flow if flow else {} table_id = UTIL.ofp_table_from_user( flow.get('table_id', dp.ofproto.OFPTT_ALL)) out_port = UTIL.ofp_port_from_user( flow.get('out_port', dp.ofproto.OFPP_ANY)) out_group = UTIL.ofp_group_from_user( flow.get('out_group', dp.ofproto.OFPG_ANY)) cookie = str_to_int(flow.get('cookie', 0)) cookie_mask = str_to_int(flow.get('cookie_mask', 0)) match = to_match(dp, flow.get('match', {})) stats = dp.ofproto_parser.OFPAggregateStatsRequest(dp, table_id, out_port, out_group, cookie, cookie_mask, match) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) flows = [] for msg in msgs: stats = msg.body s = { 'packet_count': stats.packet_count, 'byte_count': stats.byte_count, 'flow_count': stats.flow_count } flows.append(s) return {str(dp.id): flows}
def get_flow_stats(dp, waiters, flow=None, to_user=True): flow = flow if flow else {} table_id = UTIL.ofp_table_from_user( flow.get('table_id', dp.ofproto.OFPTT_ALL)) flags = str_to_int(flow.get('flags', 0)) out_port = UTIL.ofp_port_from_user( flow.get('out_port', dp.ofproto.OFPP_ANY)) out_group = UTIL.ofp_group_from_user( flow.get('out_group', dp.ofproto.OFPG_ANY)) cookie = str_to_int(flow.get('cookie', 0)) cookie_mask = str_to_int(flow.get('cookie_mask', 0)) match = to_match(dp, flow.get('match', {})) # Note: OpenFlow does not allow to filter flow entries by priority, # but for efficiency, ofctl provides the way to do it. priority = str_to_int(flow.get('priority', -1)) stats = dp.ofproto_parser.OFPFlowStatsRequest(dp, flags, table_id, out_port, out_group, cookie, cookie_mask, match) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) flows = [] for msg in msgs: for stats in msg.body: if 0 <= priority != stats.priority: continue s = stats.to_jsondict()[stats.__class__.__name__] s['stats'] = stats_to_str(stats.stats) s['match'] = match_to_str(stats.match) flows.append(s) return wrap_dpid_dict(dp, flows, to_user)
def get_group_desc(dp, waiters, to_user=True): stats = dp.ofproto_parser.OFPGroupDescStatsRequest(dp, 0) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) descs = [] for msg in msgs: for stats in msg.body: d = stats.to_jsondict()[stats.__class__.__name__] buckets = [] for bucket in stats.buckets: b = bucket.to_jsondict()[bucket.__class__.__name__] actions = [] for action in bucket.actions: if to_user: actions.append(action_to_str(action)) else: actions.append(action) b['actions'] = actions buckets.append(b) d['buckets'] = buckets if to_user: d['group_id'] = UTIL.ofp_group_to_user(stats.group_id) t = UTIL.ofp_group_type_to_user(stats.type) d['type'] = t if t != stats.type else 'UNKNOWN' descs.append(d) return wrap_dpid_dict(dp, descs, to_user)
def get_aggregate_flow_stats(dp, waiters, flow=None, to_user=True): flow = flow if flow else {} table_id = UTIL.ofp_table_from_user( flow.get('table_id', dp.ofproto.OFPTT_ALL)) flags = str_to_int(flow.get('flags', 0)) out_port = UTIL.ofp_port_from_user( flow.get('out_port', dp.ofproto.OFPP_ANY)) out_group = UTIL.ofp_group_from_user( flow.get('out_group', dp.ofproto.OFPG_ANY)) cookie = str_to_int(flow.get('cookie', 0)) cookie_mask = str_to_int(flow.get('cookie_mask', 0)) match = to_match(dp, flow.get('match', {})) stats = dp.ofproto_parser.OFPAggregateStatsRequest(dp, flags, table_id, out_port, out_group, cookie, cookie_mask, match) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) flows = [] for msg in msgs: stats = msg.body s = stats.to_jsondict()[stats.__class__.__name__] s['stats'] = stats_to_str(stats.stats) flows.append(s) return wrap_dpid_dict(dp, flows, to_user)
def get_port_stats(dp, waiters, port_no=None, to_user=True): if port_no is None: port_no = dp.ofproto.OFPP_ANY else: port_no = UTIL.ofp_port_from_user(port_no) stats = dp.ofproto_parser.OFPPortStatsRequest(dp, 0, port_no) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) ports = [] for msg in msgs: for stats in msg.body: s = stats.to_jsondict()[stats.__class__.__name__] properties = [] for prop in stats.properties: p = prop.to_jsondict()[prop.__class__.__name__] t = UTIL.ofp_port_stats_prop_type_to_user(prop.type) p['type'] = t if t != prop.type else 'UNKNOWN' properties.append(p) s['properties'] = properties if to_user: s['port_no'] = UTIL.ofp_port_to_user(stats.port_no) ports.append(s) return wrap_dpid_dict(dp, ports, to_user)
def get_queue_stats(dp, waiters, port_no=None, queue_id=None, to_user=True): if port_no is None: port_no = dp.ofproto.OFPP_ANY else: port_no = UTIL.ofp_port_from_user(port_no) if queue_id is None: queue_id = dp.ofproto.OFPQ_ALL else: queue_id = UTIL.ofp_queue_from_user(queue_id) stats = dp.ofproto_parser.OFPQueueStatsRequest(dp, 0, port_no, queue_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) desc = [] for msg in msgs: stats = msg.body for stat in stats: s = stat.to_jsondict()[stat.__class__.__name__] properties = [] for prop in stat.properties: p = prop.to_jsondict()[prop.__class__.__name__] if to_user: t = UTIL.ofp_queue_stats_prop_type_to_user(prop.type) p['type'] = t if t != p['type'] else 'UNKNOWN' properties.append(p) s['properties'] = properties desc.append(s) return wrap_dpid_dict(dp, desc, to_user)
def get_flow_stats(dp, waiters, flow=None, to_user=True): flow = flow if flow else {} table_id = UTIL.ofp_table_from_user( flow.get('table_id', dp.ofproto.OFPTT_ALL)) flags = str_to_int(flow.get('flags', 0)) out_port = UTIL.ofp_port_from_user( flow.get('out_port', dp.ofproto.OFPP_ANY)) out_group = UTIL.ofp_group_from_user( flow.get('out_group', dp.ofproto.OFPG_ANY)) cookie = str_to_int(flow.get('cookie', 0)) cookie_mask = str_to_int(flow.get('cookie_mask', 0)) match = to_match(dp, flow.get('match', {})) # Note: OpenFlow does not allow to filter flow entries by priority, # but for efficiency, ofctl provides the way to do it. priority = str_to_int(flow.get('priority', -1)) stats = dp.ofproto_parser.OFPFlowStatsRequest(dp, flags, table_id, out_port, out_group, cookie, cookie_mask, match) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) flows = [] for msg in msgs: for stats in msg.body: if 0 <= priority != stats.priority: continue s = { 'priority': stats.priority, 'cookie': stats.cookie, 'idle_timeout': stats.idle_timeout, 'hard_timeout': stats.hard_timeout, 'byte_count': stats.byte_count, 'duration_sec': stats.duration_sec, 'duration_nsec': stats.duration_nsec, 'packet_count': stats.packet_count, 'length': stats.length, 'flags': stats.flags } if to_user: s['actions'] = actions_to_str(stats.instructions) s['match'] = match_to_str(stats.match) s['table_id'] = UTIL.ofp_table_to_user(stats.table_id) else: s['actions'] = stats.instructions s['instructions'] = stats.instructions s['match'] = stats.match s['table_id'] = stats.table_id flows.append(s) return wrap_dpid_dict(dp, flows, to_user)
def get_desc_stats(dp, waiters, to_user=True): stats = dp.ofproto_parser.OFPDescStatsRequest(dp, 0) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) s = {} for msg in msgs: stats = msg.body s = stats.to_jsondict()[stats.__class__.__name__] return wrap_dpid_dict(dp, s, to_user)
def get_queue_config(dp, waiters, port=None, to_user=True): ofp = dp.ofproto if port is None: port = ofp.OFPP_ANY else: port = UTIL.ofp_port_from_user(str_to_int(port)) stats = dp.ofproto_parser.OFPQueueGetConfigRequest(dp, port) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) prop_type = { dp.ofproto.OFPQT_MIN_RATE: 'MIN_RATE', dp.ofproto.OFPQT_MAX_RATE: 'MAX_RATE', dp.ofproto.OFPQT_EXPERIMENTER: 'EXPERIMENTER' } configs = [] for config in msgs: queue_list = [] for queue in config.queues: prop_list = [] for prop in queue.properties: p = {'property': prop_type.get(prop.property, 'UNKNOWN')} if prop.property == dp.ofproto.OFPQT_MIN_RATE or \ prop.property == dp.ofproto.OFPQT_MAX_RATE: p['rate'] = prop.rate elif prop.property == dp.ofproto.OFPQT_EXPERIMENTER: p['experimenter'] = prop.experimenter p['data'] = prop.data prop_list.append(p) q = {'properties': prop_list} if to_user: q['port'] = UTIL.ofp_port_to_user(queue.port) q['queue_id'] = UTIL.ofp_queue_to_user(queue.queue_id) else: q['port'] = queue.port q['queue_id'] = queue.queue_id queue_list.append(q) c = {'queues': queue_list} if to_user: c['port'] = UTIL.ofp_port_to_user(config.port) else: c['port'] = config.port configs.append(c) return wrap_dpid_dict(dp, configs, to_user)
def get_desc_stats(dp, waiters): stats = dp.ofproto_parser.OFPDescStatsRequest(dp) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) s = {} for msg in msgs: stats = msg.body s = stats.to_jsondict()[stats.__class__.__name__] return {str(dp.id): s}
def get_meter_features(dp, waiters, to_user=True): ofp = dp.ofproto type_convert = { ofp.OFPMBT_DROP: 'DROP', ofp.OFPMBT_DSCP_REMARK: 'DSCP_REMARK' } capa_convert = { ofp.OFPMF_KBPS: 'KBPS', ofp.OFPMF_PKTPS: 'PKTPS', ofp.OFPMF_BURST: 'BURST', ofp.OFPMF_STATS: 'STATS' } stats = dp.ofproto_parser.OFPMeterFeaturesStatsRequest(dp, 0) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) features = [] for msg in msgs: for feature in msg.body: band_types = [] for k, v in type_convert.items(): if (1 << k) & feature.band_types: if to_user: band_types.append(v) else: band_types.append(k) capabilities = [] for k, v in sorted(capa_convert.items()): if k & feature.capabilities: if to_user: capabilities.append(v) else: capabilities.append(k) f = { 'max_meter': feature.max_meter, 'band_types': band_types, 'capabilities': capabilities, 'max_bands': feature.max_bands, 'max_color': feature.max_color } features.append(f) return wrap_dpid_dict(dp, features, to_user)
def get_meter_desc(dp, waiters, meter_id=None, to_user=True): flags = { dp.ofproto.OFPMF_KBPS: 'KBPS', dp.ofproto.OFPMF_PKTPS: 'PKTPS', dp.ofproto.OFPMF_BURST: 'BURST', dp.ofproto.OFPMF_STATS: 'STATS' } if meter_id is None: meter_id = dp.ofproto.OFPM_ALL else: meter_id = UTIL.ofp_meter_from_user(meter_id) stats = dp.ofproto_parser.OFPMeterDescStatsRequest(dp, 0, meter_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) configs = [] for msg in msgs: for config in msg.body: c = config.to_jsondict()[config.__class__.__name__] bands = [] for band in config.bands: b = band.to_jsondict()[band.__class__.__name__] if to_user: t = UTIL.ofp_meter_band_type_to_user(band.type) b['type'] = t if t != band.type else 'UNKNOWN' bands.append(b) c_flags = [] for k, v in sorted(flags.items()): if k & config.flags: if to_user: c_flags.append(v) else: c_flags.append(k) c['flags'] = c_flags c['bands'] = bands if to_user: c['meter_id'] = UTIL.ofp_meter_to_user(config.meter_id) configs.append(c) return wrap_dpid_dict(dp, configs, to_user)
def get_table_stats(dp, waiters): stats = dp.ofproto_parser.OFPTableStatsRequest(dp, 0) ofp = dp.ofproto msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) match_convert = {ofp.OFPFW_IN_PORT: 'IN_PORT', ofp.OFPFW_DL_VLAN: 'DL_VLAN', ofp.OFPFW_DL_SRC: 'DL_SRC', ofp.OFPFW_DL_DST: 'DL_DST', ofp.OFPFW_DL_TYPE: 'DL_TYPE', ofp.OFPFW_NW_PROTO: 'NW_PROTO', ofp.OFPFW_TP_SRC: 'TP_SRC', ofp.OFPFW_TP_DST: 'TP_DST', ofp.OFPFW_NW_SRC_SHIFT: 'NW_SRC_SHIFT', ofp.OFPFW_NW_SRC_BITS: 'NW_SRC_BITS', ofp.OFPFW_NW_SRC_MASK: 'NW_SRC_MASK', ofp.OFPFW_NW_SRC: 'NW_SRC', ofp.OFPFW_NW_SRC_ALL: 'NW_SRC_ALL', ofp.OFPFW_NW_DST_SHIFT: 'NW_DST_SHIFT', ofp.OFPFW_NW_DST_BITS: 'NW_DST_BITS', ofp.OFPFW_NW_DST_MASK: 'NW_DST_MASK', ofp.OFPFW_NW_DST: 'NW_DST', ofp.OFPFW_NW_DST_ALL: 'NW_DST_ALL', ofp.OFPFW_DL_VLAN_PCP: 'DL_VLAN_PCP', ofp.OFPFW_NW_TOS: 'NW_TOS', ofp.OFPFW_ALL: 'ALL', ofp.OFPFW_ICMP_TYPE: 'ICMP_TYPE', ofp.OFPFW_ICMP_CODE: 'ICMP_CODE'} tables = [] for msg in msgs: stats = msg.body for stat in stats: wildcards = [] for k, v in match_convert.items(): if (1 << k) & stat.wildcards: wildcards.append(v) s = {'table_id': UTIL.ofp_table_to_user(stat.table_id), 'name': stat.name.decode('utf-8'), 'wildcards': wildcards, 'max_entries': stat.max_entries, 'active_count': stat.active_count, 'lookup_count': stat.lookup_count, 'matched_count': stat.matched_count} tables.append(s) return {str(dp.id): tables}
def get_group_desc(dp, waiters, to_user=True): type_convert = { dp.ofproto.OFPGT_ALL: 'ALL', dp.ofproto.OFPGT_SELECT: 'SELECT', dp.ofproto.OFPGT_INDIRECT: 'INDIRECT', dp.ofproto.OFPGT_FF: 'FF' } stats = dp.ofproto_parser.OFPGroupDescStatsRequest(dp, 0) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) descs = [] for msg in msgs: for stats in msg.body: buckets = [] for bucket in stats.buckets: actions = [] for action in bucket.actions: if to_user: actions.append(action_to_str(action)) else: actions.append(action) b = { 'weight': bucket.weight, 'watch_port': bucket.watch_port, 'watch_group': bucket.watch_group, 'actions': actions } buckets.append(b) d = {'buckets': buckets} if to_user: d['group_id'] = UTIL.ofp_group_to_user(stats.group_id) d['type'] = type_convert.get(stats.type) else: d['group_id'] = stats.group_id d['type'] = stats.type descs.append(d) return wrap_dpid_dict(dp, descs, to_user)
def get_table_stats(dp, waiters, to_user=True): stats = dp.ofproto_parser.OFPTableStatsRequest(dp, 0) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) tables = [] for msg in msgs: stats = msg.body for stat in stats: s = stat.to_jsondict()[stat.__class__.__name__] if to_user: s['table_id'] = UTIL.ofp_table_to_user(stat.table_id) tables.append(s) return wrap_dpid_dict(dp, tables, to_user)
def get_group_desc(dp, waiters, group_id=None, to_user=True): if group_id is None: group_id = dp.ofproto.OFPG_ALL else: group_id = UTIL.ofp_group_from_user(group_id) stats = dp.ofproto_parser.OFPGroupDescStatsRequest(dp, 0, group_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) descs = [] for msg in msgs: for stats in msg.body: d = stats.to_jsondict()[stats.__class__.__name__] buckets = [] for bucket in stats.buckets: b = bucket.to_jsondict()[bucket.__class__.__name__] actions = [] for action in bucket.actions: if to_user: actions.append(action_to_str(action)) else: actions.append(action) properties = [] for prop in bucket.properties: p = prop.to_jsondict()[prop.__class__.__name__] t = UTIL.ofp_group_bucket_prop_type_to_user(prop.type) p['type'] = t if t != prop.type else 'UNKNOWN' properties.append(p) b['actions'] = actions b['properties'] = properties buckets.append(b) d['buckets'] = buckets if to_user: d['group_id'] = UTIL.ofp_group_to_user(stats.group_id) t = UTIL.ofp_group_type_to_user(stats.type) d['type'] = t if t != stats.type else 'UNKNOWN' descs.append(d) return wrap_dpid_dict(dp, descs, to_user)
def get_flow_stats(dp, waiters, flow=None): flow = flow if flow else {} match = to_match(dp, flow.get('match', {})) table_id = UTIL.ofp_table_from_user( flow.get('table_id', 0xff)) out_port = UTIL.ofp_port_from_user( flow.get('out_port', dp.ofproto.OFPP_NONE)) # Note: OpenFlow does not allow to filter flow entries by priority, # but for efficiency, ofctl provides the way to do it. priority = str_to_int(flow.get('priority', -1)) stats = dp.ofproto_parser.OFPFlowStatsRequest( dp, 0, match, table_id, out_port) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) flows = [] for msg in msgs: for stats in msg.body: if 0 <= priority != stats.priority: continue actions = actions_to_str(stats.actions) match = match_to_str(stats.match) s = {'priority': stats.priority, 'cookie': stats.cookie, 'idle_timeout': stats.idle_timeout, 'hard_timeout': stats.hard_timeout, 'actions': actions, 'match': match, 'byte_count': stats.byte_count, 'duration_sec': stats.duration_sec, 'duration_nsec': stats.duration_nsec, 'packet_count': stats.packet_count, 'table_id': UTIL.ofp_table_to_user(stats.table_id)} flows.append(s) return {str(dp.id): flows}
def get_port_stats(dp, waiters, port=None, to_user=True): if port is None: port = dp.ofproto.OFPP_ANY else: port = str_to_int(port) stats = dp.ofproto_parser.OFPPortStatsRequest(dp, 0, port) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) ports = [] for msg in msgs: for stats in msg.body: s = { 'rx_packets': stats.rx_packets, 'tx_packets': stats.tx_packets, 'rx_bytes': stats.rx_bytes, 'tx_bytes': stats.tx_bytes, 'rx_dropped': stats.rx_dropped, 'tx_dropped': stats.tx_dropped, 'rx_errors': stats.rx_errors, 'tx_errors': stats.tx_errors, 'rx_frame_err': stats.rx_frame_err, 'rx_over_err': stats.rx_over_err, 'rx_crc_err': stats.rx_crc_err, 'collisions': stats.collisions, 'duration_sec': stats.duration_sec, 'duration_nsec': stats.duration_nsec } if to_user: s['port_no'] = UTIL.ofp_port_to_user(stats.port_no) else: s['port_no'] = stats.port_no ports.append(s) return wrap_dpid_dict(dp, ports, to_user)
def get_meter_stats(dp, waiters, meter_id=None, to_user=True): if meter_id is None: meter_id = dp.ofproto.OFPM_ALL else: meter_id = str_to_int(meter_id) stats = dp.ofproto_parser.OFPMeterStatsRequest(dp, 0, meter_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) meters = [] for msg in msgs: for stats in msg.body: bands = [] for band in stats.band_stats: b = { 'packet_band_count': band.packet_band_count, 'byte_band_count': band.byte_band_count } bands.append(b) s = { 'len': stats.len, 'flow_count': stats.flow_count, 'packet_in_count': stats.packet_in_count, 'byte_in_count': stats.byte_in_count, 'duration_sec': stats.duration_sec, 'duration_nsec': stats.duration_nsec, 'band_stats': bands } if to_user: s['meter_id'] = UTIL.ofp_meter_to_user(stats.meter_id) else: s['meter_id'] = stats.meter_id meters.append(s) return wrap_dpid_dict(dp, meters, to_user)
def get_group_stats(dp, waiters, group_id=None, to_user=True): if group_id is None: group_id = dp.ofproto.OFPG_ALL else: group_id = str_to_int(group_id) stats = dp.ofproto_parser.OFPGroupStatsRequest(dp, 0, group_id) msgs = [] ofctl_utils.send_stats_request(dp, stats, waiters, msgs, LOG) groups = [] for msg in msgs: for stats in msg.body: bucket_stats = [] for bucket_stat in stats.bucket_stats: c = { 'packet_count': bucket_stat.packet_count, 'byte_count': bucket_stat.byte_count } bucket_stats.append(c) g = { 'length': stats.length, 'ref_count': stats.ref_count, 'packet_count': stats.packet_count, 'byte_count': stats.byte_count, 'duration_sec': stats.duration_sec, 'duration_nsec': stats.duration_nsec, 'bucket_stats': bucket_stats } if to_user: g['group_id'] = UTIL.ofp_group_to_user(stats.group_id) else: g['group_id'] = stats.group_id groups.append(g) return wrap_dpid_dict(dp, groups, to_user)