def get_data_base(topo, flow_name): g = topo.topo_graph.to_directed() for node in networkx.nodes(g): g.add_node(node, type=node_to_type(node)) g.add_node(node, label=topo.get_host_ip(node)) if SDCNodeIdType.is_switch(node): fx, fy = get_fixed_pos(node) g.add_node(node, fx=fx, fy=fy) else: g.add_node(node, y=SCREEN_SIZE_Y, x=SCREEN_SIZE_X / 2) bw_usage = network_monitor.get_bw_usage_all_incoming(flow_name=flow_name) for this_node in bw_usage: if SDCNodeIdType.is_host(this_node): this_switch = topo.get_host_mac(this_node) else: this_switch = this_node if this_switch == None: continue #print this_node, bw_usage[this_node] for this_inport, bw in bw_usage[this_node]: other_switch = topo.get_connected_node_via_port( this_switch, this_inport) if other_switch and this_switch: #print "Viz: %s -> %s : %s"%(other_switch,this_switch,str(bw)) g[other_switch][this_switch]['weight'] = bw g[other_switch][this_switch]['width'] = bw_to_width(bw) return json_graph.node_link_data(g)
def build_default_path_port_match(self): self.default_port_match = defaultdict(dict) for switch in self.get_all_nodes(): if SDCNodeIdType.get_type(switch) == SDCNodeIdType.Aggr or SDCNodeIdType.get_type(switch) == SDCNodeIdType.Edge: upports, downports = network_defpath.get_up_down_ports(self, switch) for i in range(max(len(upports), len(downports))): inport = downports[i%len(downports)] outport = upports[i%len(upports)] self.default_port_match[switch][inport]=outport
def get_up_down_connected(topo, switch_id): type = SDCNodeIdType.get_type(switch_id) up_connected = set() down_connected = set() for other_id in topo.get_all_connected(switch_id): if SDCNodeIdType.get_type(other_id) > type: up_connected.add(other_id) elif SDCNodeIdType.get_type(other_id) < type: down_connected.add(other_id) return (list(up_connected), list(down_connected))
def get_up_down_ports(topo, switch_id): type = SDCNodeIdType.get_type(switch_id) upports = set() downports = set() for port in topo.get_all_ports(switch_id): other_id = topo.get_connected_node_via_port(switch_id, port) print "Debug: (%s,type=%s) port=%s, other=%s" % ( switch_id, str(type), str(port), str(other_id)) if other_id == None or SDCNodeIdType.get_type(other_id) < type: downports.add(port) elif SDCNodeIdType.get_type(other_id) > type: upports.add(port) return (sorted(list(upports)), sorted(list(downports)))
def add_path_down_direct_hosts(topo, switch_id): for node_id in topo.get_all_connected(switch_id): if SDCNodeIdType.is_host(node_id): outport = topo.get_switch_port_to_dst(switch_id, node_id) add_flow_path(switch_id, outport, network_manager.ODL_FLOW_PRIORITY_DEFAULT_PATH_FOR_HOST,\ match_dst_mac=node_id, \ table_id = TABLE_ID_HOST)
def get_all_switches(self): all_nodes = self.nodes.keys() all_switches = [] for node_id in all_nodes: if SDCNodeIdType.is_switch(node_id): all_switches.append(node_id) return all_switches
def get_data_extra(topo, flow_name): extra_path = [] bw_usage = network_monitor.get_bw_usage_all_link_flows(flow_name=flow_name) for this_node in bw_usage: if SDCNodeIdType.is_host(this_node): this_switch = topo.get_host_mac(this_node) else: this_switch = this_node #print "Debug for bw usage:",this_node, bw_usage[this_node] for this_inport, flow_bw_pair in bw_usage[this_node]: # flow_bw_pair = [ (srcdst1, bw1), (srcdst2, bw2), ... ] other_switch = topo.get_connected_node_via_port( this_switch, this_inport) if other_switch and this_switch: #print "Viz: %s -> %s : "%(other_switch,this_switch), flow_bw_pair for flowkey, bw_val in flow_bw_pair: if bw_val > GRAPH_BW_THRESHOLD: extra_path.append({ "source": other_switch, "target": this_switch, "bw": int(bw_val), "width": bw_to_width(bw_val), "addr": flowkey, "value": get_flowkey_hash(flowkey) }) return extra_path
def get_topology_info(): # returns edges / pod info: # ( ( (pod0_edge0_hosts...), (pod0_edge1_hosts..), ..), # ( (pod1_edge0_hosts...), (pod1_edge1_hosts..), ..), # ...other pods... ) compute_nodes = [] for n in range(2,10): compute_nodes.append("192.168.0."+str(n)) topo = SDCTopo(sdcon_config.ODL_CONTROLLER_URL, sdcon_config.ODL_CONTROLLER_ID, sdcon_config.ODL_CONTROLLER_PW) edge_hosts = {} # Find all edge nodes and their hosts for switch in topo.get_all_switches(): if SDCNodeIdType.get_type(switch) == SDCNodeIdType.Edge: switch_hosts = list() for host_mac in topo.get_all_connected(switch): if SDCNodeIdType.is_host(host_mac): host = topo.get_host_ip(host_mac) if host in compute_nodes: switch_hosts.append(host) edge_hosts[switch] = switch_hosts print edge_hosts # Find all pods pods = [] for aggr_switch in topo.get_all_switches(): if SDCNodeIdType.get_type(aggr_switch) == SDCNodeIdType.Aggr: edges = set() for edge_switch in topo.get_all_connected(aggr_switch): if SDCNodeIdType.get_type(edge_switch) == SDCNodeIdType.Edge: edges.update([edge_switch]) pods.append(frozenset(edges)) pods = list( set(pods) ) # list of edge switches in a frozenset. print pods # Put hosts into pod / edge pod_edge_hosts = [] for edges in pods: hosts_in_edge =[] for edge in edges: hosts_in_edge.append( edge_hosts[edge] ) pod_edge_hosts.append(hosts_in_edge) return pod_edge_hosts
def get_fixed_pos(node): if SDCNodeIdType.is_host(node): return None x_index, y_index = get_fixed_pos_index(node) if y_index == 0: x_index = float(x_index) * (MAX_HORIZONTAL_SWITCHES / 2) + 0.5 x = (x_index + 0.5) / MAX_HORIZONTAL_SWITCHES y = (y_index + 0.5) / MAX_TIER return int(SCREEN_SIZE_X * x) + SCREEN_MARGIN_LEFT, int( SCREEN_SIZE_Y * y) + SCREEN_MARGIN_TOP
def get_sub_hosts(topo, switch_id): # Find all connected hosts under this switch (downstream only) up_nodes, down_nodes = get_up_down_connected(topo, switch_id) connected_hosts = set() for down_n in down_nodes: if SDCNodeIdType.is_host(down_n): connected_hosts.add(down_n) else: sub_hosts = get_sub_hosts(topo, down_n) connected_hosts.update(sub_hosts) return list(connected_hosts)
def add_path_down_indirect_hosts(topo, switch_id): up_nodes, down_nodes = get_up_down_connected(topo, switch_id) for down_switch in down_nodes: if SDCNodeIdType.is_host(down_switch): continue # rule applies only to switches outport = topo.get_switch_port_to_dst(switch_id, down_switch) hosts = get_sub_hosts(topo, down_switch) for dst_mac in hosts: add_flow_path(switch_id, outport, network_manager.ODL_FLOW_PRIORITY_DEFAULT_PATH_FOR_HOST,\ match_dst_mac=dst_mac, \ table_id = TABLE_ID_HOST)
def get_super_hosts(topo, switch_id): # Find all connected hosts of my upper switches (for up-stream.) Not including my sub-host up_nodes, down_nodes = get_up_down_connected(topo, switch_id) connected_hosts = set() my_sub = get_sub_hosts(topo, switch_id) for upnode in up_nodes: if SDCNodeIdType.is_host(upnode): connected_hosts.add(upnode) else: # Result = {All sub nodes of upper layer} - {my sub} up_sub = get_sub_hosts(topo, upnode) connected_hosts.update(up_sub) connected_hosts.difference_update(my_sub) super_hosts = get_super_hosts(topo, upnode) connected_hosts.update(super_hosts) return list(connected_hosts)
def add_path_extra_for_controller(topo): # Additional rules necessary for the controller node. extra_next_switch = {} controller_mac = topo.get_host_mac(CONTROLLER_IP_FOR_EXTRA_DEFAULT_PATH) controller_switch = topo.get_connected_switch( CONTROLLER_IP_FOR_EXTRA_DEFAULT_PATH) for switch in topo.get_all_nodes(): if SDCNodeIdType.get_type(switch) == SDCNodeIdType.Edge: paths = networkx.all_shortest_paths(topo.topo_graph, source=switch, target=controller_switch, weight=None) path_list = list(paths)[0] for i in range(len(path_list) - 1): extra_next_switch[str(path_list[i])] = str(path_list[i + 1]) for switch in extra_next_switch: outport = topo.get_switch_port_to_dst(switch, extra_next_switch[switch]) add_flow_path(switch, outport, network_manager.ODL_FLOW_PRIORITY_DEFAULT_PATH_FOR_HOST,\ match_dst_mac=controller_mac, \ table_id = TABLE_ID_HOST)
def is_host(self): return SDCNodeIdType.is_host(self.id)
def node_to_type(node): if SDCNodeIdType.is_host(node): return 1 return 2