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 set_rule(self, table, in_ports, out_ports, match, mask, rewrite): ''' sets the essential infomation about a rule and automatically computes inverse match and rewrite. ''' self.input_ports = in_ports self.output_ports = out_ports self.match = match self.mask = mask self.rewrite = rewrite self.table = table if (mask.__class__ == wildcard and rewrite.__class__ == wildcard): masked = wildcard_and(match, mask) rewritten = wildcard_or(masked, rewrite) self.inverse_match = rewritten self.inverse_rewrite = wildcard_and(wildcard_not(mask), match) else: self.mask = None self.rewrite = None self.inverse_match = match self.inverse_rewrite = None return self
def set_rule(self,table,in_ports,out_ports,match,mask,rewrite): ''' sets the essential infomation about a rule and automatically computes inverse match and rewrite. ''' self.input_ports = in_ports self.output_ports = out_ports self.match = match self.mask = mask self.rewrite = rewrite self.table = table if (mask.__class__ == wildcard and rewrite.__class__ == wildcard): masked = wildcard_and(match, mask) rewritten = wildcard_or(masked, rewrite) self.inverse_match = rewritten self.inverse_rewrite = wildcard_and(wildcard_not(mask), match) else: self.mask = None self.rewrite = None self.inverse_match = match self.inverse_rewrite = None return self
def find_new_field(self, field_match, field_mask, field_rewrite): ''' finds out the new value for this field. If it is unknown (i.e. there are wildcard bits in it), returns None. ''' all_masked = True for i in range(len(field_mask)): if (field_mask[i] != 0xaaaa): all_masked = False if (all_masked): return None rw = wildcard_or(\ wildcard_and(field_match,field_mask),\ field_rewrite) try: value = int(rw.__str__(0).replace(",", ""), 2) return value except: print "ERROR: Unexpected rewrite action. Ignored. %s - %s - %s - %s"%\ (field_match,field_mask,field_rewrite,rw) return None
def find_new_field(self,field_match,field_mask,field_rewrite): ''' finds out the new value for this field. If it is unknown (i.e. there are wildcard bits in it), returns None. ''' all_masked = True for i in range (len(field_mask)): if (field_mask[i] != 0xaaaa): all_masked = False if (all_masked): return None rw = wildcard_or(\ wildcard_and(field_match,field_mask),\ field_rewrite) try: value = int(rw.__str__(0).replace(",", ""),2) return value except: print "ERROR: Unexpected rewrite action. Ignored. %s - %s - %s - %s"%\ (field_match,field_mask,field_rewrite,rw) return None