def rule_construct(old_path, new_path, match, state, prt, out_port_old, out_port_new, clk, table_id=0): rule_set = {} if old_path: rule_set = {} #intersect_set = [] for i in range(len(new_path) - 1): #if (old_path[i] in new_path) and (old_path[i+1] not in new_path): if (new_path[i] in old_path) and (new_path[i + 1] not in old_path): inter_first = i break #intersect_set.append(i) for i in range(len(new_path) - 1): if (new_path[len(new_path) - i - 1] in old_path) and (new_path[len(new_path) - i - 2] not in old_path): inter_last = len(new_path) - i - 1 break print inter_first print inter_last for j in range(inter_first, inter_last): i = new_path[j] rule_set[i] = {'add': [], 'del': []} if i in old_path: rule_set[i]['del'].append( rule(i, match, out_port_old[i], clk - 1, clk - 1, table_id, prt)) rule_set[i]['add'].append( rule(i, match, out_port_new[i], clk, 0, table_id, prt)) for i in (set(old_path) - set(new_path)): if i not in rule_set: rule_set[i] = {'add': [], 'del': []} rule_set[i]['del'].append( rule(i, match, out_port_old[i], clk - 1, clk - 1, table_id, prt)) else: for i in set(new_path): rule_set[i] = {'add': [], 'del': []} if i == new_path[0]: rule_set[i]['add'].append( rule(i, match, out_port_new[i], 0, clk, table_id, prt)) else: rule_set[i]['add'].append( rule(i, match, out_port_new[i], clk, 0, table_id, prt)) return rule_set
def rule_construct_normal(old_path, new_path, match, state, prt, out_port, dst_mac, flow_id=0, table_id=0): # a simplified version of one-big-switch rule_set = {} #match = {} #match['ipv4_dst'] = flow['ipv4_dst'] #match['ipv4_src'] = flow['ipv4_src'] if old_path: for i in range(1, len(old_path) - 2): if (old_path[i] in new_path) and (old_path[i + 1] not in new_path): rule_set[old_path[i]] = {'add': [], 'del': []} rext = state.get_table(old_path[i], table_id).get_rule(match) if rext.get_prt() == prt: rule_set[old_path[i]]['del'].append( rule(old_path[i], rext.get_match(), rext.get_action(), rext.get_dst_mac(), rext.get_rtmp(), rext.get_ttmp(), rext.get_flow_id(), table_id, rext.get_prt())) rule_set[old_path[i]]['add'].append( rule(old_path[i], match, out_port[old_path[i]], dst_mac[new_path[i + 1]], 0, 0, flow_id, table_id, prt)) for i in (set(old_path[1:-1]) - set(new_path[1:-1])): if i not in rule_set: rule_set[i] = {'add': [], 'del': []} rext = state.get_table(i, table_id).get_rule(match) if rext.get_prt() == prt: rule_set[i]['del'].append( rule(i, rext.get_match(), rext.get_action(), rext.get_dst_mac(), rext.get_rtmp(), rext.get_ttmp(), rext.get_flow_id(), table_id, rext.get_prt())) for i in range(1, len(new_path) - 1): if new_path[i] not in old_path: rule_set[new_path[i]] = {'add': [], 'del': []} rule_set[new_path[i]]['add'].append( rule(new_path[i], match, out_port[new_path[i]], dst_mac[new_path[i + 1]], 0, 0, flow_id, table_id, prt)) return rule_set
def sb_rule_clean(old_path, new_path, match, clk, in_port, table_id=0): sb_set = {} for i in (set(old_path) - set(new_path)): if i not in sb_set: sb_set[i] = {'add': [], 'del': []} sb_set[i]['del'].append( rule(i, match, in_port[i], clk, clk, table_id, SBPRT)) return sb_set
def SingleSwitchTopoConfig(self, p4info_file_path, bmv2_file_path): p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) sw = p4runtime_lib.bmv2.Bmv2SwitchConnection('s1', address='localhost:50051') sw.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) sleep(1) #how to construct sw_rule table_entry = table_entry_construct_new(p4info_helper, table_id=1) sw.WriteTableEntry(table_entry) sleep(1) table_entry = table_entry_construct_new(p4info_helper, table_id=2) sw.WriteTableEntry(table_entry) #init sw_rule = {} sw_rule['add'] = [] sw_rule['del'] = [] #sw_rule['add'].append(rule('s1', {}, 100, 0, 0, 0, 1)) #sw_rule['add'].append(rule('s1', {'ipv4_dst':'10.0.0.0/255.0.0.0', 'ipv4_src':'10.0.0.0/255.0.0.0'}, 100, 0, -1, 0, 2)) #writeRules(K, p4info_helper, i, "10.0.0.0", "255.0.0.0", "10.0.0.0", "255.0.0.0", rtmp_max, 0, 1, action_flag=1, priority=PRTMAX-2) #writeRules(K, p4info_helper, i, "0.0.0.0", "0.0.0.0", "0.0.0.0", "0.0.0.0", rtmp_max, 0, 1, action_flag=2, priority=PRTMAX-1) #sw = p4runtime_lib.bmv2.Bmv2SwitchConnection(grpc2name(K, sw_id), address='localhost:%d' %(sw_id+50051)) #table_entry = table_entry_construct(p4info_helper, src_ip_addr, src_addr_mask, dst_ip_addr, dst_addr_mask, rtmp, ttmp, out_port, action_flag, priority) #sw.WriteTableEntry(table_entry,update_flag=update_flag_write) #routing rules ip_src = '10.0.0.10' ip_dst = '10.0.1.10' match = {} match['ipv4_dst'] = ip_dst match['ipv4_src'] = ip_src match_reverse = {} match_reverse['ipv4_dst'] = ip_src match_reverse['ipv4_src'] = ip_dst sw_rule['add'].append(rule('s1', match, 1, 1, 2, 0, 20)) sw_rule['add'].append(rule('s1', match_reverse, 2, 2, 1, 0, 20)) #self.oneSwitchDeploy(p4info_helper, sw, sw_rule) self.oneSwitchDeployNew(p4info_helper, sw, sw_rule)
def bufferTest(self, p4info_file_path): p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) sw = p4runtime_lib.bmv2.Bmv2SwitchConnection('s1', address='localhost:50051') sw_rule = {} sw_rule['add'] = [] sw_rule['del'] = [] #routing rules with new tmp ip_src = '10.0.0.10' ip_dst = '10.0.1.10' match = {} match['ipv4_dst'] = ip_dst match['ipv4_src'] = ip_src sw_rule['del'].append(rule('s1', match, 1, 1, 2, 0, 20)) sw_rule['add'].append(rule('s1', match, 2, 2, 2, 0, 20)) #self.oneSwitchDeploy(p4info_helper, sw, sw_rule) self.oneSwitchDeployNew(p4info_helper, sw, sw_rule)
def rule_construct_normal(old_path, new_path, flow, state, prt, out_port): rule_set = {} table_id = 0 match = {} match['ipv4_dst'] = flow['ipv4_dst'] match['ipv4_src'] = flow['ipv4_src'] match["eth_type"] = 2048 if old_path: intersect_set = [] for i in range(len(old_path) - 1): if (old_path[i] in new_path) and (old_path[i + 1] not in new_path): intersect_set.append(old_path[i]) for i in intersect_set: rule_set[i] = {} rule_set[i]['add'] = [] rule_set[i]['del'] = [] rext = state.get_table(i, table_id).get_rule(flow) if rext.get_prt() == prt: df = difference(rext.get_match_bin(), match_parse(flow)) if df: for j in df: rule_set[i]['add'].append( rule(i, match_reverse(j), rext.get_rtmp(), rext.get_ttmp(), rext.get_action(), table_id, prt)) rule_set[i]['del'].append( rule(i, rext.get_match(), rext.get_rtmp(), rext.get_ttmp(), rext.get_action(), table_id, prt)) rule_set[i]['add'].append( rule(i, match, rtmp_max, 0, out_port[i], table_id, prt)) for i in (set(old_path) - set(new_path)): if i not in rule_set.keys(): rule_set[i] = {} rule_set[i]['add'] = [] rule_set[i]['del'] = [] rext = state.get_table(i, table_id).get_rule(flow) if rext.get_prt() == prt: df = difference(rext.get_match_bin(), match_parse(flow)) if df: for j in df: rule_set[i]['add'].append( rule(i, match_reverse(j), rext.get_rtmp(), rext.get_ttmp(), rext.get_action(), table_id, prt)) rule_set[i]['del'].append( rule(i, rext.get_match(), rext.get_rtmp(), rext.get_ttmp(), rext.get_action(), table_id, prt)) for i in (set(new_path) - set(old_path)): rule_set[i] = {} rule_set[i]['add'] = [] rule_set[i]['del'] = [] rule_set[i]['add'].append( rule(i, match, rtmp_max, 0, out_port[i], table_id, prt)) return rule_set
def rule_construct_cu_twice(old_path, new_path, flow, state, prt, out_port, clk): rule_set = {} table_id = 0 match = {} match['ipv4_dst'] = flow['ipv4_dst'] match['ipv4_src'] = flow['ipv4_src'] match["eth_type"] = 2048 for i in range(1, len(new_path)): rule_set[new_path[i]] = {} rule_set[new_path[i]]['add'] = [] rule_set[new_path[i]]['del'] = [] if i == len(new_path) - 1: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, clk, -1, out_port[new_path[i]], table_id, prt)) else: if i == 0: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, -1, clk, out_port[new_path[i]], table_id, 0)) else: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, clk, clk, out_port[new_path[i]], table_id, prt)) first_rule = {} first_rule[new_path[0]] = {} first_rule[new_path[0]]['add'] = [ rule(new_path[0], match, -1, clk, out_port[new_path[0]], table_id, prt) ] first_rule[new_path[0]]['del'] = [ rule(new_path[0], match, -1, clk - 1, out_port[new_path[0]], table_id, prt) ] #first_rule[new_path[0]]['del'].append(rule(new_path[0], match, -1, clk, out_port[new_path[0]], table_id, 0)) return {'rule_set': rule_set, 'first_rule': first_rule}
def sb_rule_construct(old_path, new_path, flow, clk, in_port): sb_set = {} match = {} match['ipv4_dst'] = flow['ipv4_dst'] match['ipv4_src'] = flow['ipv4_src'] match["eth_type"] = 2048 table_id = 0 for i in (set(old_path) - set(new_path)): if i not in sb_set.keys(): sb_set[i] = {} sb_set[i]['add'] = [] sb_set[i]['del'] = [] sb_set[i]['del'].append( rule(i, match, clk, clk, in_port[i], table_id, SBPRT)) return sb_set
def setTMP(old_path, new_path, match, state, rule_set, clk, in_port, dst_mac, flow_id, table_id=0): # set rule timestamp for i in rule_set: for r in rule_set[i]['add']: r.set_rtmp(clk) state = state_update(rule_set, state, table_id) # add send_back rules for i in range(1, len(old_path) - 1): if old_path[i] not in new_path: if old_path[i] not in rule_set: rule_set[old_path[i]] = {'add': [], 'del': []} rule_set[old_path[i]]['add'].append( rule(old_path[i], match, in_port[old_path[i]], dst_mac[old_path[i - 1]], clk, clk, flow_id, table_id, SBPRT)) # state.print_state() for i in range(1, len(new_path) - 2): if new_path[i] in rule_set: for r in rule_set[new_path[i]]['add']: if r.get_ttmp() == 0: tb_next = state.get_table(new_path[i + 1], table_id) r_next = tb_next.get_rule(match) #r_next.print_rule() r.set_ttmp(r_next.get_rtmp()) state = state_update(rule_set, state, table_id) return rule_set
def rule_construct_cu(old_path, new_path, match, state, prt, out_port_old, out_port_new, clk, table_id=0): rule_deploy = [] if not old_path: rule_set = {} for i in range(len(new_path)): rule_set[new_path[i]] = {'add': [], 'del': []} if i == len(new_path) - 1: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, out_port_new[new_path[i]], clk, 0, table_id, prt)) elif i == 0: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, out_port_new[new_path[i]], 0, clk, table_id, prt)) else: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, out_port_new[new_path[i]], clk, clk, table_id, prt)) rule_deploy.append(rule_set) else: rule_set = {} for i in range(1, len(new_path)): rule_set[new_path[i]] = {'add': [], 'del': []} if i == len(new_path) - 1: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, out_port_new[new_path[i]], clk, 0, table_id, prt)) else: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, out_port_new[new_path[i]], clk, clk, table_id, prt)) rule_deploy.append(rule_set) first_rule = {} first_rule[new_path[0]] = {} first_rule[new_path[0]]['add'] = [ rule(new_path[0], match, out_port_new[new_path[0]], 0, clk, table_id, prt) ] first_rule[new_path[0]]['del'] = [ rule(new_path[0], match, out_port_old[new_path[0]], 0, clk - 1, table_id, prt) ] rule_deploy.append(first_rule) del_set = {} for i in range(1, len(old_path)): del_set[old_path[i]] = {'add': [], 'del': []} if i == len(old_path) - 1: del_set[old_path[i]]['del'].append( rule(old_path[i], match, out_port_old[old_path[i]], clk - 1, 0, table_id, prt)) else: del_set[old_path[i]]['del'].append( rule(old_path[i], match, out_port_old[old_path[i]], clk - 1, clk - 1, table_id, prt)) rule_deploy.append(del_set) for rule_set in rule_deploy: state = state_update(rule_set, state, table_id, 1) #state.print_state() return rule_deploy
def setTMP(old_path, new_path, flow, new_state, rule_set, clk, in_port): #rule_set = copy.deepcopy(rule_set_old) #if old_path: # i = 0 # while old_path[i] in new_path: # i = i + 1 # inter_node = old_path[i-1] #else: # inter_node = new_path[0] # search for the first intersection node table_id = 0 match = {} match['ipv4_dst'] = flow['ipv4_dst'] match['ipv4_src'] = flow['ipv4_src'] match["eth_type"] = 2048 for i in (set(old_path) - set(new_path)): if i not in rule_set.keys(): rule_set[i] = {} rule_set[i]['add'] = [] rule_set[i]['del'] = [] rule_set[i]['add'].append( rule(i, match, clk, clk, in_port[i], table_id, SBPRT)) for i in range(len(new_path) - 1): if new_path[i] in rule_set.keys() and rule_set[new_path[i]]['add']: for r in rule_set[new_path[i]]['add']: if r.get_ttmp() == 0: tb_cur = new_state.get_table(new_path[i], table_id) tb_next = new_state.get_table(new_path[i + 1], table_id) r_exact = r.get_exact_match(tb_cur) rule_inf = [] ttmp_list = [] for t in tb_next.get_all_rules(): #tmp = [] for j in r_exact: #print "t" #print t.print_rule() #print "r" #print r.print_rule() #print old_path #print new_path if intersection(t.get_match_bin(), j): rule_inf.append(t) ttmp_list.append(t.get_rtmp()) #tmp = tmp + difference(j, t.get_match_bin()) #r_exact = tmp #if not r_exact: #break #print "\nttmp set:" #print ttmp_list #for rk in rule_inf: # rk.print_rule() if max(ttmp_list) == clk: r.set_ttmp(clk) for j in range(len(ttmp_list)): if ttmp_list[j] < clk: rule_set[new_path[i + 1]]['del'].append( rule(new_path[i + 1], rule_inf[j].get_match(), rule_inf[j].get_rtmp(), rule_inf[j].get_ttmp(), rule_inf[j].get_action(), table_id, rule_inf[j].get_prt())) rule_set[new_path[i + 1]]['add'].append( rule(new_path[i + 1], rule_inf[j].get_match(), clk, rule_inf[j].get_ttmp(), rule_inf[j].get_action(), table_id, rule_inf[j].get_prt())) else: r.set_ttmp(min(ttmp_list)) """ if i+2 < len(new_path)-2: next = old_state.get_table(new_path[k], table_id).get_rule(flow).get_action() while next in new_path[k+1:len(new_path)-1]: next = old_state.get_table(next, table_id).get_rule(flow).get_action() if next != new_path[len(new_path)]: for j in range(inter_node, i): x = new_state.get_table(new_path[j], table_id).get_rule(flow) if x.get_rtmp() < clk: rule_set[new_path[j]]['del'].append(rule(new_path[j], x.get_match(), x.get_rtmp(), x.get_ttmp(), x.get_action(), table_id, x.get_prt())) rule_set[new_path[j]]['add'].append(rule(new_path[j], x.get_match(), clk, clk, x.get_action(), table_id, x.get_prt())) else: r.set_ttmp(min(ttmp_list)) """ return rule_set
def rule_construct_coco_twice(old_path, new_path, flow, state, prt, out_port, clk): rule_set_first = {} rule_set_second = {} table_id = 0 match = {} match['ipv4_dst'] = flow['ipv4_dst'] match['ipv4_src'] = flow['ipv4_src'] match["eth_type"] = 2048 if old_path: intersect_set = [] for i in range(len(new_path) - 1): if (new_path[i] in old_path) and (new_path[i + 1] not in old_path): intersect_set.append(i) for i in range(len(new_path)): if i >= intersect_set[0]: rule_set_first[new_path[i]] = {} rule_set_first[new_path[i]]['add'] = [] rule_set_first[new_path[i]]['del'] = [] rule_set_second[new_path[i]] = {} rule_set_second[new_path[i]]['add'] = [] rule_set_second[new_path[i]]['del'] = [] """ rext = state.get_table(new_path[i], table_id).get_rule(flow) if rext and rext.get_prt() == prt: df = difference(rext.get_match_bin(), match_parse(flow)) if df: for j in df: rule_set_first[new_path[i]]['add'].append(rule(new_path[i], match_reverse(j), rext.get_rtmp(), rext.get_ttmp(), rext.get_action(), table_id, prt)) rule_set_first[new_path[i]]['del'].append(rule(new_path[i], rext.get_match(), rext.get_rtmp(), rext.get_ttmp(), rext.get_action(), table_id, prt)) """ if i == 0: rule_set_first[new_path[i]]['add'].append( rule(new_path[i], match, -1, clk, out_port[new_path[i]], table_id, 0)) rule_set_second[new_path[i]]['del'].append( rule(new_path[i], match, -1, clk, out_port[new_path[i]], table_id, 0)) rule_set_second[new_path[i]]['add'].append( rule(new_path[i], match, -1, clk, out_port[new_path[i]], table_id, prt + 1)) else: if i == len(new_path) - 1: rule_set_first[new_path[i]]['add'].append( rule(new_path[i], match, clk, -1, out_port[new_path[i]], table_id, 0)) rule_set_second[new_path[i]]['del'].append( rule(new_path[i], match, clk, -1, out_port[new_path[i]], table_id, 0)) rule_set_second[new_path[i]]['add'].append( rule(new_path[i], match, clk, -1, out_port[new_path[i]], table_id, prt + 1)) else: rule_set_first[new_path[i]]['add'].append( rule(new_path[i], match, clk, clk, out_port[new_path[i]], table_id, 0)) rule_set_second[new_path[i]]['del'].append( rule(new_path[i], match, clk, clk, out_port[new_path[i]], table_id, 0)) rule_set_second[new_path[i]]['add'].append( rule(new_path[i], match, clk, clk, out_port[new_path[i]], table_id, prt + 1)) """ for i in (set(old_path) - set(new_path)): if i not in rule_set.keys(): rule_set_first[i] = {} rule_set_first[i]['add'] = [] rule_set_first[i]['del'] = [] rext = state.get_table(i, table_id).get_rule(flow) if rext.get_prt() == prt: df = difference(rext.get_match_bin(), match_parse(flow)) if df: for j in df: rule_set_first[i]['add'].append(rule(i, match_reverse(j), rext.get_rtmp(), rext.get_ttmp(), rext.get_action(), table_id, prt)) rule_set_first[i]['del'].append(rule(i, rext.get_match(), rext.get_rtmp(), rext.get_ttmp(), rext.get_action(), table_id, prt)) #rule_set[i]['del'].append(rule(i, {}, 1, 1, 0, table_id, 0)) rule_set_first[i]['add'].append(rule(i, match, clk, clk, 0, table_id, prt)) """ return { 'rule_set_first': rule_set_first, 'rule_set_second': rule_set_second }
def rule_construct_coco_final(old_path, new_path, flow, state, prt, out_port_old, out_port_new, clk): table_id = 0 match = {} match['ipv4_dst'] = flow['ipv4_dst'] match['ipv4_src'] = flow['ipv4_src'] match["eth_type"] = 2048 if not old_path: rule_set = {} for i in range(len(new_path)): rule_set[new_path[i]] = {} rule_set[new_path[i]]['add'] = [] rule_set[new_path[i]]['del'] = [] if i == len(new_path) - 1: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, clk, 0, out_port_new[new_path[i]], table_id, prt)) else: if i == 0: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, 0, clk, out_port_new[new_path[i]], table_id, prt)) else: rule_set[new_path[i]]['add'].append( rule(new_path[i], match, clk, clk, out_port_new[new_path[i]], table_id, prt)) return rule_set else: intersect_set = [] for i in range(len(new_path) - 1): if (new_path[i] in old_path) and (new_path[i + 1] not in old_path): intersect_set.append(new_path[i]) rule_set_first = {} rule_set_second = {} rule_set_third = {} rule_set_fourth = {} for j in range(new_path.index(intersect_set[0]), len(new_path)): i = new_path[j] rule_set_first[i] = {} rule_set_first[i]['add'] = [] rule_set_first[i]['del'] = [] rule_set_second[i] = {} rule_set_second[i]['add'] = [] rule_set_second[i]['del'] = [] rule_set_third[i] = {} rule_set_third[i]['add'] = [] rule_set_third[i]['del'] = [] rule_set_fourth[i] = {} rule_set_fourth[i]['add'] = [] rule_set_fourth[i]['del'] = [] if i == new_path[0]: rule_set_first[i]['add'].append( rule(i, match, 0, clk + 1, out_port_new[i], table_id, prt - 1)) rule_set_second[i]['del'].append( rule(i, match, 0, clk + 1, out_port_new[i], table_id, prt - 1)) rule_set_second[i]['add'].append( rule(i, match, 0, clk + 1, out_port_new[i], table_id, prt + 1)) rule_set_third[i]['del'].append( rule(i, match, 0, clk + 1, out_port_new[i], table_id, prt + 1)) rule_set_third[i]['add'].append( rule(i, match, 0, clk, out_port_new[i], table_id, prt + 1)) rule_set_fourth[i]['del'].append( rule(i, match, 0, clk, out_port_new[i], table_id, prt + 1)) rule_set_fourth[i]['add'].append( rule(i, match, 0, clk, out_port_new[i], table_id, prt)) else: if i == new_path[len(new_path) - 1]: rule_set_first[i]['add'].append( rule(i, match, clk + 1, 0, out_port_new[i], table_id, prt - 1)) rule_set_second[i]['del'].append( rule(i, match, clk + 1, 0, out_port_new[i], table_id, prt - 1)) rule_set_second[i]['add'].append( rule(i, match, clk + 1, 0, out_port_new[i], table_id, prt + 1)) rule_set_fourth[i]['del'].append( rule(i, match, clk + 1, 0, out_port_new[i], table_id, prt + 1)) rule_set_fourth[i]['add'].append( rule(i, match, clk, 0, out_port_new[i], table_id, prt)) else: rule_set_first[i]['add'].append( rule(i, match, clk + 1, clk + 1, out_port_new[i], table_id, prt - 1)) rule_set_second[i]['del'].append( rule(i, match, clk + 1, clk + 1, out_port_new[i], table_id, prt - 1)) rule_set_second[i]['add'].append( rule(i, match, clk + 1, clk + 1, out_port_new[i], table_id, prt + 1)) rule_set_third[i]['del'].append( rule(i, match, clk + 1, clk + 1, out_port_new[i], table_id, prt + 1)) rule_set_third[i]['add'].append( rule(i, match, clk + 1, clk, out_port_new[i], table_id, prt + 1)) rule_set_fourth[i]['del'].append( rule(i, match, clk + 1, clk, out_port_new[i], table_id, prt + 1)) rule_set_fourth[i]['add'].append( rule(i, match, clk, clk, out_port_new[i], table_id, prt)) for j in range(old_path.index(intersect_set[0]), len(old_path)): i = old_path[j] if i not in rule_set_third.keys(): rule_set_third[i] = {} rule_set_third[i]['add'] = [] rule_set_third[i]['del'] = [] if i == old_path[0]: rule_set_third[i]['del'].append( rule(i, match, 0, clk, out_port_old[i], table_id, prt)) else: if i == old_path[len(old_path) - 1]: rule_set_third[i]['del'].append( rule(i, match, clk, 0, out_port_old[i], table_id, prt)) else: rule_set_third[i]['del'].append( rule(i, match, clk, clk, out_port_old[i], table_id, prt)) return { 'rule_set_first': rule_set_first, 'rule_set_second': rule_set_second, 'rule_set_third': rule_set_third, 'rule_set_fourth': rule_set_fourth }
def rule_construct(old_path, new_path, flow, state, prt, out_port_old, out_port_new, clk): rule_set = {} table_id = 0 match = {} match['ipv4_dst'] = flow['ipv4_dst'] match['ipv4_src'] = flow['ipv4_src'] match["eth_type"] = 2048 #flow_bin = match_parse(flow) if old_path: #intersect_set = [] for i in range(len(new_path) - 1): #if (old_path[i] in new_path) and (old_path[i+1] not in new_path): if (new_path[i] in old_path) and (new_path[i + 1] not in old_path): inter_first = i break #intersect_set.append(i) for i in range(len(new_path) - 1): if (new_path[len(new_path) - i - 1] in old_path) and (new_path[len(new_path) - i - 2] not in old_path): inter_last = len(new_path) - i - 1 break #print inter_first #print inter_last for j in range(inter_first, inter_last): i = new_path[j] rule_set[i] = {} rule_set[i]['add'] = [] rule_set[i]['del'] = [] if i in old_path: rule_set[i]['del'].append( rule(i, match, clk - 1, clk - 1, out_port_old[i], table_id, prt)) rule_set[i]['add'].append( rule(i, match, clk, 0, out_port_new[i], table_id, prt)) for i in (set(old_path) - set(new_path)): if i not in rule_set.keys(): rule_set[i] = {} rule_set[i]['add'] = [] rule_set[i]['del'] = [] rule_set[i]['del'].append( rule(i, match, clk - 1, clk - 1, out_port_old[i], table_id, prt)) else: for i in set(new_path): rule_set[i] = {} rule_set[i]['add'] = [] rule_set[i]['del'] = [] if i == new_path[0]: rule_set[i]['add'].append( rule(i, match, 0, clk, out_port_new[i], table_id, prt)) else: rule_set[i]['add'].append( rule(i, match, clk, 0, out_port_new[i], table_id, prt)) #if i == new_path[len(new_path)-1]: # rule_set[i]['add'].append(rule(i, match, clk, 0, out_port_new[i], table_id, prt)) #else: # rule_set[i]['add'].append(rule(i, match, clk, 0, out_port_new[i], table_id, prt)) return rule_set