def load_ttf(self): ''' loads topology transfer function ''' f = TF(1) f.load_from_json("%s/topology.tf.json"%self.settings["input_path"]) return f
def load_ntf(self): ''' load transfer functions into a emulated transfer function with @layer layers. ''' if "remove_duplicates" in self.settings.keys() and \ self.settings["remove_duplicates"]: emul_tf = emulated_tf(self.settings["num_layers"],True) else: emul_tf = emulated_tf(self.settings["num_layers"],False) emul_tf.set_fwd_engine_stage(self.settings["fwd_engine_layer"]) emul_tf.set_multipliers(self.settings["switch_id_multiplier"], \ self.settings["port_type_multiplier"], \ self.settings["out_port_type_const"]) for rtr_name in self.settings["rtr_names"]: f = TF(1) f.load_from_json("%s/%s.tf.json"%(self.settings["input_path"], rtr_name)) if "hash_table" in self.settings.keys(): f.activate_hash_table(self.settings["hash_table"]) emul_tf.append_tf(f) emul_tf.length = f.length return emul_tf
def load_ttf(self): ''' loads topology transfer function ''' f = TF(1) f.load_from_json("%s/topology.tf.json" % self.settings["input_path"]) return f
def testRW1(self): tf = TF(1) tf.add_rewrite_rule(TF.create_standard_rule([1], "10xxxxxx", [2], "10011111", "01100000")) hs = headerspace(1) hs.add_hs(wildcard_create_from_string("1001xxxx")) result = tf.T(hs, 1) self.assertEqual(len(result), 1) self.assert_(wildcard_is_equal(result[0][0].hs_list[0], wildcard_create_from_string("1111xxxx")))
def testInverse(self): tf = TF(1) tf.add_rewrite_rule(TF.create_standard_rule([1], "10xxxxxx", [2], "10011111", "01100000")) hs = headerspace(1) hs.add_hs(wildcard_create_from_string("111xxxxx")) hs.diff_hs(wildcard_create_from_string("1110xxxx")) result = tf.T_inv(hs, 2) self.assertEqual(len(result), 1) self.assertEqual(result[0][0].count(), 1) self.assertEqual(result[0][0].count_diff(), 1) self.assert_(wildcard_is_equal(result[0][0].hs_list[0], wildcard_create_from_string("10xxxxxx"))) self.assert_(wildcard_is_equal(result[0][0].hs_diff[0][0], wildcard_create_from_string("10x0xxxx")))
def testDependency(self): tf = TF(1) tf.add_fwd_rule(TF.create_standard_rule([1], "10xxxxxx", [2], \ None, None)) tf.add_rewrite_rule(TF.create_standard_rule([1], "1xxxxxxx", [3], "00111111", "10000000","",[])) hs = headerspace(1) hs.add_hs(wildcard_create_from_string("xxxxxxxx")) result = tf.T(hs, 1) self.assertEqual(len(result), 2, "Expecting both rules to be matched") self.assertTrue(wildcard_is_equal( result[1][0].hs_list[0],\ wildcard_create_from_string("10xxxxxx"),\ ), \ "unexpected second byte array")
def compose_standard_rules(rule1, rule2): mid_ports = [val for val in rule2["in_ports"] if val in rule1["out_ports"]] if len(mid_ports) == 0: return None ### finding match #rule 2 is a link rule if rule2["match"] == None: match = bytearray(rule1["match"]) else: # if rule 1 is a fwd or link rule if rule1["mask"] == None: # if rule 1 is a link rule if rule1["match"] == None: match = bytearray(rule2["match"]) else: match = byte_array_intersect(rule2["match"], rule1["match"]) # if rule 1 is a rewrite rule else: match_inv = byte_array_or( byte_array_and(rule2["match"], rule1['mask']), rule1['inverse_rewrite']) match = byte_array_intersect(match_inv, rule1["match"]) if len(match) == 0: return None ### finding mask and rewrite mask = None rewrite = None if rule2["mask"] == None: mask = rule1["mask"] rewrite = rule1["rewrite"] elif rule1["mask"] == None: mask = rule2["mask"] rewrite = rule2["rewrite"] else: # mask = mask1 & mask2 # rewrite = (rewrite1 & mask2) | (rewrite2 & !mask2) mask = byte_array_and(rule1["mask"], rule2["mask"]) rewrite = byte_array_or( byte_array_and(rule1["rewrite"], rule2["mask"]), byte_array_and(rule2["rewrite"], byte_array_not(rule2["mask"]))) in_ports = rule1["in_ports"] out_ports = rule2["out_ports"] if rule1["file"] == rule2["file"]: file_name = rule1["file"] else: file_name = "%s , %s" % (rule1["file"], rule2["file"]) lines = rule1["line"] lines.extend(rule2["line"]) result_rule = TF.create_standard_rule(in_ports, match, out_ports, mask, rewrite, file_name, lines) return result_rule
def compose_standard_rules(rule1,rule2): mid_ports = [val for val in rule2["in_ports"] if val in rule1["out_ports"]] if len(mid_ports) == 0: return None ### finding match #rule 2 is a link rule if rule2["match"] == None: match = wildcard_copy(rule1["match"]) else: # if rule 1 is a fwd or link rule if rule1["mask"] == None: # if rule 1 is a link rule if rule1["match"] == None: match = wildcard_copy(rule2["match"]) else: match = wildcard_intersect(rule2["match"],rule1["match"]) # if rule 1 is a rewrite rule else: match_inv = wildcard_or(\ wildcard_and(rule2["match"],rule1['mask']),\ rule1['inverse_rewrite']) match = wildcard_intersect(match_inv,rule1["match"]) if len(match) == 0: return None ### finding mask and rewrite mask = None rewrite = None if rule2["mask"] == None: mask = rule1["mask"] rewrite = rule1["rewrite"] elif rule1["mask"] == None: mask = rule2["mask"] rewrite = rule2["rewrite"] else: # mask = mask1 & mask2 # rewrite = (rewrite1 & mask2) | (rewrite2 & !mask2) mask = wildcard_and(rule1["mask"],rule2["mask"]) rewrite = wildcard_or(wildcard_and(rule1["rewrite"],rule2["mask"]),wildcard_and(rule2["rewrite"],wildcard_not(rule2["mask"]))) in_ports = rule1["in_ports"] out_ports = rule2["out_ports"] if rule1["file"] == rule2["file"]: file_name = rule1["file"] else: file_name = "%s , %s"%(rule1["file"],rule2["file"]) lines = rule1["line"] lines.extend(rule2["line"]) result_rule = TF.create_standard_rule(in_ports, match, out_ports, mask, rewrite, file_name, lines) return result_rule
def load_ntf(self): ''' load transfer functions into a emulated transfer function with @layer layers. ''' if "remove_duplicates" in self.settings.keys() and \ self.settings["remove_duplicates"]: emul_tf = emulated_tf(self.settings["num_layers"], True) else: emul_tf = emulated_tf(self.settings["num_layers"], False) emul_tf.set_fwd_engine_stage(self.settings["fwd_engine_layer"]) emul_tf.set_multipliers(self.settings["switch_id_multiplier"], \ self.settings["port_type_multiplier"], \ self.settings["out_port_type_const"]) for rtr_name in self.settings["rtr_names"]: f = TF(1) f.load_from_json("%s/%s.tf.json" % (self.settings["input_path"], rtr_name)) if "hash_table" in self.settings.keys(): f.activate_hash_table(self.settings["hash_table"]) emul_tf.append_tf(f) emul_tf.length = f.length return emul_tf
def testDependency(self): tf = TF(1) tf.add_fwd_rule(TF.create_standard_rule([1], "10xxxxxx", [2], None, None)) tf.add_rewrite_rule(TF.create_standard_rule([1], "1xxxxxxx", [3], "00111111", "10000000", "", [])) hs = headerspace(1) hs.add_hs(wildcard_create_from_string("xxxxxxxx")) result = tf.T(hs, 1) self.assertEqual(len(result), 2, "Expecting both rules to be matched") self.assertTrue( wildcard_is_equal(result[1][0].hs_list[0], wildcard_create_from_string("10xxxxxx")), "unexpected second byte array", )
def testRW1(self): tf = TF(1) tf.add_rewrite_rule(TF.create_standard_rule([1], "10xxxxxx", [2], \ "10011111", "01100000")) hs = headerspace(1) hs.add_hs(wildcard_create_from_string("1001xxxx")) result = tf.T(hs, 1) self.assertEqual(len(result), 1) self.assert_(wildcard_is_equal(result[0][0].hs_list[0],\ wildcard_create_from_string("1111xxxx")))
def expand_NTF(NTF, num_rules): port_groups = [[1,2,7,101,102,103,104,105,106,107], [3,4,14,18,201,202,203,204,205], [5,6,8,301,302,303,304,305,306,307], [9,10,401,402,403,404,405,406,407,408]] per_port_rules = num_rules/4 for j in range(4): for i in range(per_port_rules): first = random.randrange(32,255) second = random.randrange(1,255) ip_addr = dotted_ip_to_int("10.1.%d.%d"%(first,second)) subnet = random.randrange(27,32) ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,ip_addr,subnet) in_port = random.choice(port_groups[j]) out_port = random.choice(port_groups[j]) rule = TF.create_standard_rule([in_port], ip_match, [out_port], None, None, "dummy", []) NTF.add_fwd_rule(rule)
def parse_new_rule_tokens(tokens, mapf, rtr): in_ports = [] out_ports = [] match = wildcard_create_bit_repeat(HS_FORMAT["length"], 0x3) mask = None rw = None for token in tokens: parts = token.split("=") field_name = parts[0] if field_name.startswith("new"): if mask == None: mask = wildcard_create_bit_repeat(HS_FORMAT["length"], 0x2) rw = wildcard_create_bit_repeat(HS_FORMAT["length"], 0x1) field_name = field_name[4:] if field_name in ["ip_src", "ip_dst"]: [value, left_mask] = dotted_subnet_to_int(parts[1]) right_mask = 32 - left_mask else: value = int(parts[1]) right_mask = 0 set_header_field(cisco_router.HS_FORMAT(), mask, field_name, 0, right_mask) set_header_field(cisco_router.HS_FORMAT(), rw, field_name, value, right_mask) else: if field_name in ["in_ports", "out_ports"]: ports = parts[1].split(",") ports_int = [] for port in ports: ports_int.append(int(mapf[rtr][port])) if field_name == "in_ports": in_ports = ports_int else: out_ports = ports_int else: if field_name in ["ip_src", "ip_dst"]: [value, left_mask] = dotted_subnet_to_int(parts[1]) right_mask = 32 - left_mask else: value = int(parts[1]) right_mask = 0 set_header_field(cisco_router.HS_FORMAT(), match, field_name, value, right_mask) rule = TF.create_standard_rule(in_ports, match, out_ports, mask, rw, "", []) return rule
def parse_new_rule_tokens(tokens,mapf,rtr): in_ports = [] out_ports = [] match = wildcard_create_bit_repeat(HS_FORMAT["length"],0x3) mask = None rw = None for token in tokens: parts = token.split("=") field_name = parts[0] if field_name.startswith("new"): if mask == None: mask = wildcard_create_bit_repeat(HS_FORMAT["length"],0x2) rw = wildcard_create_bit_repeat(HS_FORMAT["length"],0x1) field_name = field_name[4:] if field_name in ["ip_src","ip_dst"]: [value,left_mask] = dotted_subnet_to_int(parts[1]) right_mask = 32 - left_mask else: value = int(parts[1]) right_mask = 0 set_header_field(cisco_router.HS_FORMAT(), mask, field_name, 0, right_mask) set_header_field(cisco_router.HS_FORMAT(), rw, field_name, value, right_mask) else: if field_name in ["in_ports","out_ports"]: ports = parts[1].split(",") ports_int = [] for port in ports: ports_int.append(int(mapf[rtr][port])) if field_name == "in_ports": in_ports = ports_int else: out_ports = ports_int else: if field_name in ["ip_src","ip_dst"]: [value,left_mask] = dotted_subnet_to_int(parts[1]) right_mask = 32 - left_mask else: value = int(parts[1]) right_mask = 0 set_header_field(cisco_router.HS_FORMAT(), match, field_name, value, right_mask) rule = TF.create_standard_rule(in_ports, match, out_ports, mask, rw, "", []) return rule
def testInverse(self): tf = TF(1) tf.add_rewrite_rule(TF.create_standard_rule([1], "10xxxxxx", [2], \ "10011111", "01100000")) hs = headerspace(1) hs.add_hs(wildcard_create_from_string("111xxxxx")) hs.diff_hs(wildcard_create_from_string("1110xxxx")) result = tf.T_inv(hs, 2) self.assertEqual(len(result), 1) self.assertEqual(result[0][0].count(),1) self.assertEqual(result[0][0].count_diff(),1) self.assert_(wildcard_is_equal(result[0][0].hs_list[0],\ wildcard_create_from_string("10xxxxxx"),\ )) self.assert_(wildcard_is_equal(result[0][0].hs_diff[0][0],\ wildcard_create_from_string("10x0xxxx"),\ ))
def make_NTF(num_mbox): line_counter = 1 num_stack = num_mbox + 1 NTF = TF(format["length"]) m_addr = [dotted_ip_to_int("10.1.1.1"), dotted_ip_to_int("10.1.2.2"), dotted_ip_to_int("10.1.3.3"), dotted_ip_to_int("10.1.4.4"), dotted_ip_to_int("10.1.5.5"),] m_subnet = [dotted_ip_to_int("10.1.1.0"), dotted_ip_to_int("10.1.2.0"), dotted_ip_to_int("10.1.3.0"), dotted_ip_to_int("10.1.4.0"), dotted_ip_to_int("10.1.5.0"),] sender_addr = dotted_ip_to_int("10.1.10.1") receiver_addr = dotted_ip_to_int("10.1.20.1") network_subnet = dotted_ip_to_int("10.1.0.0") # rules for R1 for i in range(num_mbox): ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,m_addr[i],32) rule = TF.create_standard_rule([1,7], ip_match, [2], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,receiver_addr,32) rule = TF.create_standard_rule([1,2], ip_match, [7], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,sender_addr,32) rule = TF.create_standard_rule([2,7], ip_match, [1], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 # Add rule for R2: all_ip_star_ingress_ports = [] for i in range(num_mbox): ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,m_addr[i],32) rule = TF.create_standard_rule([3,4], ip_match, [14+4*i], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 all_ip_star_ingress_ports.append(14+4*i) for i in range(num_mbox): rule = TF.create_custom_rule(make_IP_str_match(all_ip_star_ingress_ports), make_IP_str_transform(m_addr[i], 32, [all_ip_star_ingress_ports[i]]), make_IP_str_inv_match([all_ip_star_ingress_ports[i]], m_addr[i], 32), make_IP_str_inv_transform(all_ip_star_ingress_ports), "sample.txt", [line_counter]) NTF.add_custom_rule(rule) line_counter += 1 rule = TF.create_custom_rule(make_IP_str_match(all_ip_star_ingress_ports), make_IP_str_transform(sender_addr, 32, [4]), make_IP_str_inv_match([4], sender_addr, 32), make_IP_str_inv_transform(all_ip_star_ingress_ports), "sample.txt", [line_counter]) NTF.add_custom_rule(rule) line_counter += 1 rule = TF.create_custom_rule(make_IP_str_match(all_ip_star_ingress_ports), make_IP_str_transform(receiver_addr, 32, [3]), make_IP_str_inv_match([3], receiver_addr, 32), make_IP_str_inv_transform(all_ip_star_ingress_ports), "sample.txt", [line_counter]) NTF.add_custom_rule(rule) line_counter += 1 # add rule for R3: ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,receiver_addr,32) rule = TF.create_standard_rule([5,6], ip_match, [8], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,sender_addr,32) rule = TF.create_standard_rule([5], ip_match, [6], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 rule = TF.create_custom_rule(make_IP_str_match([8]), make_IP_str_transform(receiver_addr, 32, [8]), make_IP_str_inv_match([8], receiver_addr, 32), make_IP_str_inv_transform([8]), "sample.txt", [line_counter]) NTF.add_custom_rule(rule) line_counter += 1 rule = TF.create_custom_rule(make_IP_str_match([8]), make_IP_str_transform(sender_addr, 32, [6]), make_IP_str_inv_match([6], sender_addr, 32), make_IP_str_inv_transform([8]), "sample.txt", [line_counter]) NTF.add_custom_rule(rule) line_counter += 1 for i in range(num_mbox): rule = TF.create_custom_rule(make_IP_str_match([8]), make_IP_str_transform(m_addr[i], 32, [5]), make_IP_str_inv_match([5], m_addr[i], 32), make_IP_str_inv_transform([8]), "sample.txt", [line_counter]) NTF.add_custom_rule(rule) line_counter += 1 ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,m_addr[i],32) rule = TF.create_standard_rule([6], ip_match, [5], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 # add rule for R4,R5,R8,R9,... for i in range(num_mbox): ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,m_subnet[i],24) rule = TF.create_standard_rule([15 + 4*i], ip_match, [16 + 4*i], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,network_subnet,18) rule = TF.create_standard_rule([16 + 4*i], ip_match, [15 + 4*i], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 # add rule for R6 ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,receiver_addr,32) rule = TF.create_standard_rule([9], ip_match, [10], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 ip_match = make_byte_array_ip_star_hdr(None,[],[],0,0,network_subnet,16) rule = TF.create_standard_rule([10], ip_match, [9], None, None, "sample.txt", [line_counter]) NTF.add_fwd_rule(rule) line_counter += 1 # add rule for R7 rule = TF.create_custom_rule(make_IP_str_match([11]), make_IP_str_transform(receiver_addr, 32, [12]), make_IP_str_inv_match([12], receiver_addr, 32), make_IP_str_inv_transform([11]), "sample.txt", [line_counter]) NTF.add_custom_rule(rule) line_counter += 1 rule = TF.create_custom_rule(make_IP_str_match([11]), make_IP_str_transform(network_subnet, 21, [11]), make_IP_str_inv_match([11], network_subnet, 21), make_IP_str_inv_transform([11]), "sample.txt", [line_counter]) NTF.add_custom_rule(rule) line_counter += 1 # rule for middle boxes: for i in range(num_mbox): rule = TF.create_custom_rule(make_mbox_match(m_addr[i], 32, 17+4*i), make_mbox_tf(), make_mbox_inv_match(m_addr[i], 32, 17+4*i), make_mbox_tf(), "sample.txt", [line_counter]) NTF.add_custom_rule(rule) line_counter += 1 return NTF
f = open("%s/%s" % (args.data_path, args.map_file), 'r') mapf = json.load(f) mapf_extended = copy.deepcopy(mapf) inv_mapf = {} for rtr in mapf: for port in mapf[rtr]: inv_mapf[int(mapf[rtr][port])] = "%s-%s" % (rtr, port) inv_mapf[int(mapf[rtr][port]) + OUTPORT_CONST] = "%s-%s" % (rtr, port) inv_mapf[int(mapf[rtr][port]) + INTER_CONST] = "^%s-%s" % (rtr, port) mapf_extended[rtr]["^%s" % port] = mapf[rtr][port] + INTER_CONST fwd_id = get_fwd_port_id(int(mapf[rtr][port])) inv_mapf[fwd_id] = "FWD-ENGINE" mapf_extended[rtr]["^"] = fwd_id f = TF(1) f.load_from_json("%s/%s.tf.json" % (args.data_path, args.rtr_name)) print "Modifying transfer function of router ", args.rtr_name if args.view: stage = args.view[0] i = 1 for rule in f.rules: if stage == get_stage(rule): print i, ":", get_openflow_rule(rule, inv_mapf) i = i + 1 if args.rm: indices = args.rm[0].split(",") indices = [int(i) for i in indices] indices.sort(reverse=True) for index in indices:
def generate_transfer_functions(settings): st = time() if ("replace_vlans" in settings.keys()): has_replaced_vlan = True else: has_replaced_vlan = False if "arp_table_file_sfx" in settings.keys(): arp_sfx = settings["arp_table_file_sfx"] else: arp_sfx = "_arp_table.txt" if "mac_table_file_sfx" in settings.keys(): mac_sfx = settings["mac_table_file_sfx"] else: mac_sfx = "_mac_table.txt" if "config_file_sfx" in settings.keys(): config_sfx = settings["config_file_sfx"] else: config_sfx = "_config.txt" if "spanning_tree_file_sfx" in settings.keys(): span_sfx = settings["spanning_tree_file_sfx"] else: span_sfx = "_spanning_tree.txt" if "route_table_file_sfx" in settings.keys(): route_sfx = settings["route_table_file_sfx"] else: route_sfx = "_route.txt" # generate transfer functions L = 0 id = 1 cs_list = {} for i in range(len(settings["rtr_names"])): rtr_name = settings["rtr_names"][i] cs = cisco_router(id) if has_replaced_vlan: cs.set_replaced_vlan(settings["replace_vlans"][i]) if "hs_format" in settings.keys(): cs.set_hs_format(settings["hs_format"]) L = cs.hs_format["length"] tf = TF(L) tf.set_prefix_id(rtr_name) cs.read_arp_table_file("%s/%s%s" % (settings["input_path"], rtr_name, arp_sfx)) cs.read_mac_table_file("%s/%s%s" % (settings["input_path"], rtr_name, mac_sfx)) cs.read_spanning_tree_file("%s/%s%s"%\ (settings["input_path"],rtr_name,span_sfx)) cs.read_config_file("%s/%s%s" % (settings["input_path"], rtr_name, config_sfx)) cs.read_route_file("%s/%s%s" % (settings["input_path"], rtr_name, route_sfx)) if ("optimize_fwd_table" not in settings.keys() or \ settings["optimize_fwd_table"]): cs.optimize_forwarding_table() if ("fwd_table_only" in settings.keys() and settings["fwd_table_only"]): cs.generate_port_ids_only_for_output_ports() cs.generate_fwd_table_tf(tf) else: cs.generate_port_ids([]) cs.generate_transfer_function(tf) if (not os.path.isdir(settings["output_path"])): os.makedirs(settings["output_path"]) tf.save_as_json("%s/%s.tf.json" % (settings["output_path"], rtr_name)) tf.save_object_to_file("%s/%s.tf" % (settings["output_path"], rtr_name)) id += 1 cs_list[rtr_name] = cs #generate port maps f = open("%s/port_map.json" % settings["output_path"], 'w') port_map = {} for rtr in cs_list.keys(): cs = cs_list[rtr] port_map[rtr] = cs.port_to_id f.write(json.dumps(port_map)) f.close() #write topology: if "topology" in settings.keys(): print "===Generating Topology===" out_port_addition = cisco_router.PORT_TYPE_MULTIPLIER * \ cisco_router.OUTPUT_PORT_TYPE_CONST topology = settings["topology"] tf = TF(L) for (from_router, from_port, to_router, to_port) in topology: from_cs = cs_list[from_router] to_cs = cs_list[to_router] rule = TF.create_standard_rule(\ [from_cs.get_port_id(from_port) + out_port_addition],\ None,[to_cs.get_port_id(to_port)],\ None, None, "", []) tf.add_link_rule(rule) rule = TF.create_standard_rule(\ [to_cs.get_port_id(to_port) + out_port_addition], \ None,[from_cs.get_port_id(from_port)], \ None, None, "", []) tf.add_link_rule(rule) tf.save_as_json("%s/topology.tf.json" % settings["output_path"]) tf.save_object_to_file("%s/topology.tf" % settings["output_path"]) en = time() print "completed in ", en - st, "seconds"
"soza_rtr", "sozb_rtr", "yoza_rtr", "yozb_rtr", ], "input_path":"../examples/stanford/tf_stanford_backbone", "switch_id_multiplier":cisco_router.SWITCH_ID_MULTIPLIER, "port_type_multiplier":cisco_router.PORT_TYPE_MULTIPLIER, "mid_port_type_const":cisco_router.INTERMEDIATE_PORT_TYPE_CONST, } loader = net_loader(settings) (map,inv_map) = loader.load_port_map() #topology f = TF(1) f.load_object_from_file("%s/topology.tf"%settings["input_path"]) #net plumber instance N = NetPlumber(f.length) #adding links for rule in f.rules: input_ports = rule["in_ports"] output_ports = rule["out_ports"] for input_port in input_ports: for output_port in output_ports: N.add_link(input_port, output_port) # add links for intermediate port f = open("%s/port_map.txt"%settings["input_path"],'r')
def make_TTF(num_mbox): TTF = TF(format["length"]*2) rule = {} rule["in_ports"] = [0] rule["out_ports"] = [1] TTF.add_link_rule(rule) rule["in_ports"] = [1] rule["out_ports"] = [0] TTF.add_link_rule(rule) rule["in_ports"] = [2] rule["out_ports"] = [3] TTF.add_link_rule(rule) rule["in_ports"] = [3] rule["out_ports"] = [2] TTF.add_link_rule(rule) rule["in_ports"] = [5] rule["out_ports"] = [4] TTF.add_link_rule(rule) rule["in_ports"] = [4] rule["out_ports"] = [5] TTF.add_link_rule(rule) rule["in_ports"] = [6] rule["out_ports"] = [7] TTF.add_link_rule(rule) rule["in_ports"] = [7] rule["out_ports"] = [6] TTF.add_link_rule(rule) rule["in_ports"] = [8] rule["out_ports"] = [9] TTF.add_link_rule(rule) rule["in_ports"] = [9] rule["out_ports"] = [8] TTF.add_link_rule(rule) rule["in_ports"] = [10] rule["out_ports"] = [11] TTF.add_link_rule(rule) rule["in_ports"] = [11] rule["out_ports"] = [10] TTF.add_link_rule(rule) rule["in_ports"] = [12] rule["out_ports"] = [13] TTF.add_link_rule(rule) rule["in_ports"] = [13] rule["out_ports"] = [12] TTF.add_link_rule(rule) for i in range(num_mbox): rule["in_ports"] = [14 + i*4] rule["out_ports"] = [15 + i*4] TTF.add_link_rule(rule) rule["in_ports"] = [15 + i*4] rule["out_ports"] = [14 + i*4] TTF.add_link_rule(rule) rule["in_ports"] = [16 + i*4] rule["out_ports"] = [17 + i*4] TTF.add_link_rule(rule) rule["in_ports"] = [17 + i*4] rule["out_ports"] = [16 + i*4] TTF.add_link_rule(rule) return TTF
], "input_path": "../examples/stanford/tf_stanford_backbone", "switch_id_multiplier": cisco_router.SWITCH_ID_MULTIPLIER, "port_type_multiplier": cisco_router.PORT_TYPE_MULTIPLIER, "mid_port_type_const": cisco_router.INTERMEDIATE_PORT_TYPE_CONST, } loader = net_loader(settings) (map, inv_map) = loader.load_port_map() #topology f = TF(1) f.load_object_from_file("%s/topology.tf" % settings["input_path"]) #net plumber instance N = NetPlumber(f.length) #adding links for rule in f.rules: input_ports = rule["in_ports"] output_ports = rule["out_ports"] for input_port in input_ports: for output_port in output_ports: N.add_link(input_port, output_port) # add links for intermediate port f = open("%s/port_map.txt" % settings["input_path"], 'r')
from headerspace.tf import TF import json import sys rtr_names = [ "atla", "chic", "hous", "kans", "losa", "newy32aoa", "salt", "seat", "wash" ] PORT_TYPE_MULTIPLIER = 10000 SWITCH_ID_MULTIPLIER = 100000 path = sys.argv[1] out_path = sys.argv[2] #path = "i2_tfs" #out_path = "i2_json" table_id = 0 topo = TF(1) topo.load_object_from_file("%s/%s.tf" % (path, "backbone_topology")) topology = {"topology": []} for rule in topo.rules: in_ports = rule["in_ports"] out_ports = rule["out_ports"] for in_port in in_ports: for out_port in out_ports: topology["topology"].append({"src": in_port, "dst": out_port}) topo.save_as_json("%s/%s.json" % (path, "topology")) for rtr in rtr_names: tf = TF(1) tf.load_object_from_file("%s/%s.tf" % (path, rtr)) #tf.save_as_json("%s/%s.tf.json"%(path,rtr)) table_id += 1
format["ip_dst_len"] = 4 format["length"] = 4 rtr_names = ["bbra_rtr", "bbrb_rtr", "boza_rtr", "bozb_rtr", "coza_rtr", "cozb_rtr", "goza_rtr", "gozb_rtr", "poza_rtr", "pozb_rtr", "roza_rtr", "rozb_rtr", "soza_rtr", "sozb_rtr", "yoza_rtr", "yozb_rtr", ] if (not os.path.isdir("stanford_openflow_rules")): os.makedirs("stanford_openflow_rules") for rtr_name in rtr_names: f = TF(1) f.load_object_from_file("tf_simple_stanford_backbone/%s.tf"%rtr_name) #OFG = OpenFlow_Rule_Generator(f,ciscoRouter(1).HS_FORMAT()) OFG = OpenFlow_Rule_Generator(f,format) OFG.generate_of_rules("stanford_openflow_rules/%s.of"%rtr_name)
f = open("%s/%s"%(args.data_path,args.map_file),'r') mapf = json.load(f) inv_mapf = {} for rtr in mapf: for port in mapf[rtr]: inv_mapf[int(mapf[rtr][port])] = "%s-%s"%(rtr,port) inv_mapf[int(mapf[rtr][port])+OUTPORT_CONST] = "%s-%s"%(rtr,port) inv_mapf[int(mapf[rtr][port])+INTER_CONST] = "^%s-%s"%(rtr,port) fwd_id = get_fwd_port_id(int(mapf[rtr][port])) inv_mapf[fwd_id] = "FWD-ENGINE" tfs = {} files_in_dir = os.listdir(args.data_path) for file_in_dir in files_in_dir: if file_in_dir.endswith(".tf.json"): tf = TF(1) tf.load_from_json("%s/%s"%(args.data_path,file_in_dir)) tfs[file_in_dir[0:-8]] = tf command = args.exe_path + "/" + args.network if (args.loop): command = command + " -loop" if (args.in_header): header = make_header(args.in_header) command = command + " -h " + header.__str__(0) src_id = mapf[args.source[0]][args.source[1]] if (args.one_hop): if not args.destination:
"chic", "hous", "kans", "losa", "newy32aoa", "salt", "seat", "wash" ] PORT_TYPE_MULTIPLIER = 10000 SWITCH_ID_MULTIPLIER = 100000 path = "i2_tfs" out_path = "i2_json_rules" table_id = 0 topo = TF(1) topo.load_object_from_file("%s/%s.tf"%(path,"topology")) topology = {"topology":[]} for rule in topo.rules: in_ports = rule["in_ports"] out_ports = rule["out_ports"] for in_port in in_ports: for out_port in out_ports: topology["topology"].append({"src":in_port,"dst":out_port}) topo.save_as_json("%s/%s.json"%(path,"topology")) for rtr in rtr_names: tf = TF(1) tf.load_object_from_file("%s/%s.tf"%(path,rtr)) tf.save_as_json("%s/%s.tf.json"%(path,rtr)) table_id += 1
f = open("%s/%s"%(args.data_path,args.map_file),'r') mapf = json.load(f) inv_mapf = {} for rtr in mapf: for port in mapf[rtr]: inv_mapf[int(mapf[rtr][port])] = "%s-%s"%(rtr,port) inv_mapf[int(mapf[rtr][port])+OUTPORT_CONST] = "%s-%s"%(rtr,port) inv_mapf[int(mapf[rtr][port])+INTER_CONST] = "^%s-%s"%(rtr,port) fwd_id = get_fwd_port_id(int(mapf[rtr][port])) inv_mapf[fwd_id] = "FWD-ENGINE" tfs = {} files_in_dir = os.listdir(args.data_path) for file_in_dir in files_in_dir: if file_in_dir.endswith(".tf.json"): tf = TF(1) tf.load_from_json("%s/%s"%(args.data_path,file_in_dir)) tfs[file_in_dir[0:-8]] = tf command = args.exe_path + "/" + args.network if (args.in_header): header = make_header(args.in_header) command = command + " -h " + header.__str__(0) src_id = mapf[args.source[0]][args.source[1]] if (args.one_hop): if args.destination: sys.exit("Error: you need to specify a destination using -d arg.") command = command + " -o" if args.destination[1] == "^":
def generate_transfer_functions(settings): st = time() if ("replace_vlans" in settings.keys()): has_replaced_vlan = True else: has_replaced_vlan = False if "arp_table_file_sfx" in settings.keys(): arp_sfx = settings["arp_table_file_sfx"] else: arp_sfx = "_arp_table.txt" if "mac_table_file_sfx" in settings.keys(): mac_sfx = settings["mac_table_file_sfx"] else: mac_sfx = "_mac_table.txt" if "config_file_sfx" in settings.keys(): config_sfx = settings["config_file_sfx"] else: config_sfx = "_config.txt" if "spanning_tree_file_sfx" in settings.keys(): span_sfx = settings["spanning_tree_file_sfx"] else: span_sfx = "_spanning_tree.txt" if "route_table_file_sfx" in settings.keys(): route_sfx = settings["route_table_file_sfx"] else: route_sfx = "_route.txt" # generate transfer functions L = 0 id = 1 cs_list = {} for i in range(len(settings["rtr_names"])): rtr_name = settings["rtr_names"][i] cs = cisco_router(id) if has_replaced_vlan: cs.set_replaced_vlan(settings["replace_vlans"][i]) if "hs_format" in settings.keys(): cs.set_hs_format(settings["hs_format"]) L = cs.hs_format["length"] tf = TF(L) tf.set_prefix_id(rtr_name) cs.read_arp_table_file("%s/%s%s"%(settings["input_path"],rtr_name,arp_sfx)) cs.read_mac_table_file("%s/%s%s"%(settings["input_path"],rtr_name,mac_sfx)) cs.read_spanning_tree_file("%s/%s%s"%\ (settings["input_path"],rtr_name,span_sfx)) cs.read_config_file("%s/%s%s"%(settings["input_path"],rtr_name,config_sfx)) cs.read_route_file("%s/%s%s"%(settings["input_path"],rtr_name,route_sfx)) if ("optimize_fwd_table" not in settings.keys() or \ settings["optimize_fwd_table"]): cs.optimize_forwarding_table() if ("fwd_table_only" in settings.keys() and settings["fwd_table_only"]): cs.generate_port_ids_only_for_output_ports() cs.generate_fwd_table_tf(tf) else: cs.generate_port_ids([]) cs.generate_transfer_function(tf) tf.save_as_json("%s/%s.tf.json"%(settings["output_path"],rtr_name)) tf.save_object_to_file("%s/%s.tf"%(settings["output_path"],rtr_name)) id += 1 cs_list[rtr_name] = cs #generate port maps f = open("%s/port_map.json"%settings["output_path"],'w') port_map = {} for rtr in cs_list.keys(): cs = cs_list[rtr] port_map[rtr] = cs.port_to_id f.write(json.dumps(port_map)) f.close() #write topology: if "topology" in settings.keys(): print "===Generating Topology===" out_port_addition = cisco_router.PORT_TYPE_MULTIPLIER * \ cisco_router.OUTPUT_PORT_TYPE_CONST topology = settings["topology"] tf = TF(L) for (from_router,from_port,to_router,to_port) in topology: from_cs = cs_list[from_router] to_cs = cs_list[to_router] rule = TF.create_standard_rule(\ [from_cs.get_port_id(from_port) + out_port_addition],\ None,[to_cs.get_port_id(to_port)],\ None, None, "", []) tf.add_link_rule(rule) rule = TF.create_standard_rule(\ [to_cs.get_port_id(to_port) + out_port_addition], \ None,[from_cs.get_port_id(from_port)], \ None, None, "", []) tf.add_link_rule(rule) tf.save_as_json("%s/topology.tf.json"%settings["output_path"]) tf.save_object_to_file("%s/topology.tf"%settings["output_path"]) en = time() print "completed in ",en - st, "seconds"
f = open("%s/%s"%(args.data_path,args.map_file),'r') mapf = json.load(f) mapf_extended = copy.deepcopy(mapf) inv_mapf = {} for rtr in mapf: for port in mapf[rtr]: inv_mapf[int(mapf[rtr][port])] = "%s-%s"%(rtr,port) inv_mapf[int(mapf[rtr][port])+OUTPORT_CONST] = "%s-%s"%(rtr,port) inv_mapf[int(mapf[rtr][port])+INTER_CONST] = "^%s-%s"%(rtr,port) mapf_extended[rtr]["^%s"%port] = mapf[rtr][port] + INTER_CONST fwd_id = get_fwd_port_id(int(mapf[rtr][port])) inv_mapf[fwd_id] = "FWD-ENGINE" mapf_extended[rtr]["^"] = fwd_id f = TF(1) f.load_from_json("%s/%s.tf.json"%(args.data_path,args.rtr_name)) print "Modifying transfer function of router ",args.rtr_name if args.view: stage = args.view[0] i = 1 for rule in f.rules: if stage == get_stage(rule): print i,":",get_openflow_rule(rule,inv_mapf) i = i + 1; if args.rm: indices = args.rm[0].split(",") indices = [int(i) for i in indices] indices.sort(reverse=True) for index in indices:
format["ip_dst_pos"] = 0 format["ip_dst_len"] = 4 format["length"] = 4 rtr_names = [ "bbra_rtr", "bbrb_rtr", "boza_rtr", "bozb_rtr", "coza_rtr", "cozb_rtr", "goza_rtr", "gozb_rtr", "poza_rtr", "pozb_rtr", "roza_rtr", "rozb_rtr", "soza_rtr", "sozb_rtr", "yoza_rtr", "yozb_rtr", ] for rtr_name in rtr_names: f = TF(1) f.load_object_from_file("../work/tf_simple_stanford_backbone/%s.tf" % rtr_name) #OFG = OpenFlow_Rule_Generator(f,ciscoRouter(1).HS_FORMAT()) OFG = OpenFlow_Rule_Generator(f, format) OFG.generate_of_rules("%s.of" % rtr_name)