Ejemplo n.º 1
0
 def set_field(self, arr, field, value, right_mask):
   '''
   Sets the @field in wildcard @arr to @value.
   @arr: the wildcard to set the field bits to value.
   @field: 'vlan', 'ip_src', 'ip_dst', 'ip_proto', 'transport_src', 
   'transport_dst', 'transport_ctrl'
   @value: an integer number, of the width equal to field's width
   @right_mask: number of bits, from right that should be ignored when 
   written to field. e.g. to have a /24 ip address, set mask to 8.
   '''
   set_header_field(self.hs_format,arr, field, value, right_mask)
Ejemplo n.º 2
0
def make_header(h_desc):
  all_x = wildcard_create_bit_repeat(cisco_router.HS_FORMAT()["length"],0x3)
  parts = h_desc.split(",")
  fields = ["vlan", "ip_src", "ip_dst", "ip_proto", "transport_src", "transport_dst"]
  for part in parts:
    tmp = part.split("=")
    field = tmp[0]
    value = tmp[1]
    if field in ["ip_src","ip_dst"]:
      (ip,sub) = dotted_subnet_to_int(value)
      set_header_field(cisco_router.HS_FORMAT(), all_x, field, ip, 32-sub)
    else:
      set_header_field(cisco_router.HS_FORMAT(), all_x, field, int(value), 0)
  return all_x 
Ejemplo n.º 3
0
def make_header(h_desc):
  all_x = wildcard_create_bit_repeat(cisco_router.HS_FORMAT()["length"],0x3)
  parts = h_desc.split(",")
  fields = ["vlan", "ip_src", "ip_dst", "ip_proto", "transport_src", "transport_dst"]
  for part in parts:
    tmp = part.split("=")
    field = tmp[0]
    value = tmp[1]
    if field in ["ip_src","ip_dst"]:
      (ip,sub) = dotted_subnet_to_int(value)
      set_header_field(cisco_router.HS_FORMAT(), all_x, field, ip, 32-sub)
    else:
      set_header_field(cisco_router.HS_FORMAT(), all_x, field, int(value), 0)
  return all_x 
Ejemplo n.º 4
0
 def __add_action_to_rule(self, action, rule, rtr):
     #print "Action:", action, " Rule: ",rule
     mask = wildcard_create_bit_repeat(self.format["length"], 2)
     rewrite = wildcard_create_bit_repeat(self.format["length"], 1)
     out_ports = []
     rw = False
     push = False
     pop = False
     for operation in action.keys():
         if operation == "set_nw_src" or operation == "set_nw_dst":
             rw = True
             set_header_field(self.format, mask, operation[4:], 0, 0)
             set_header_field(self.format, rewrite, operation[4:],
                              dotted_ip_to_int(action[operation]), 0)
         elif operation == "set_dl_src" or operation == "set_dl_dst":
             rw = True
             set_header_field(self.format, mask, operation[4:], 0, 0)
             set_header_field(self.format, rewrite, operation[4:],
                              mac_to_int(action[operation]), 0)
         elif operation == "output":
             '''
             if action[operation] in self.port_members:
               out_ports = self.__encode_port_list(self.port_members[action[operation]],rtr)
             else:
             '''
             if action[operation].startswith("mport"):
                 out_ports = self.__expand_mport(rtr, action[operation])
             else:
                 out_ports = [action[operation]]
             out_ports = self.__compress_port_list(out_ports)
             out_ports = self.__encode_port_list(out_ports, rtr)
         elif operation == "push_ip":
             push = True
             rule["encap_pos"] = self.format["nw_src_pos"]
             rule["encap_len"] = 8
         elif operation == "pop_ip":
             pop = True
             rule["decap_pos"] = self.format["nw_src_pos"]
             rule["decap_len"] = 8
     rule["out_ports"] = out_ports
     if push:
         rule["action"] = "encap"
         rule["mask"] = mask
         rule["rewrite"] = rewrite
     elif pop:
         rule["action"] = "decap"
         rule["mask"] = None
         rule["rewrite"] = None
     elif rw:
         rule["action"] = "rw"
         rule["mask"] = mask
         rule["rewrite"] = rewrite
     else:
         rule["action"] = "fwd"
         rule["mask"] = None
         rule["rewrite"] = None
Ejemplo n.º 5
0
 def __add_action_to_rule(self,action,rule,rtr):
   #print "Action:", action, " Rule: ",rule
   mask = wildcard_create_bit_repeat(self.format["length"],2)
   rewrite = wildcard_create_bit_repeat(self.format["length"],1)
   out_ports = []
   rw = False
   push = False
   pop = False
   for operation in action.keys():
     if operation == "set_nw_src" or operation == "set_nw_dst":
       rw = True
       set_header_field(self.format, mask, operation[4:], 0, 0)
       set_header_field(self.format, rewrite, operation[4:], dotted_ip_to_int(action[operation]), 0)
     elif operation == "set_dl_src" or operation == "set_dl_dst":
       rw = True
       set_header_field(self.format, mask, operation[4:], 0, 0)
       set_header_field(self.format, rewrite, operation[4:], mac_to_int(action[operation]), 0)
     elif operation == "output":
       '''
       if action[operation] in self.port_members:
         out_ports = self.__encode_port_list(self.port_members[action[operation]],rtr)
       else:
       '''
       if action[operation].startswith("mport"):
         out_ports = self.__expand_mport(rtr, action[operation])
       else:
         out_ports = [action[operation]]
       out_ports = self.__compress_port_list(out_ports)
       out_ports = self.__encode_port_list(out_ports, rtr)
     elif operation == "push_ip":
       push = True
       rule["encap_pos"] = self.format["nw_src_pos"]
       rule["encap_len"] = 8
     elif operation == "pop_ip":
       pop = True
       rule["decap_pos"] = self.format["nw_src_pos"]
       rule["decap_len"] = 8
   rule["out_ports"] = out_ports
   if push:
     rule["action"] = "encap"
     rule["mask"] = mask
     rule["rewrite"] = rewrite
   elif pop:
     rule["action"] = "decap"
     rule["mask"] = None
     rule["rewrite"] = None
   elif rw:
     rule["action"] = "rw"
     rule["mask"] = mask
     rule["rewrite"] = rewrite
   else:
     rule["action"] = "fwd"
     rule["mask"] = None
     rule["rewrite"] = None
Ejemplo n.º 6
0
 def __parse_flow_match(self, flow_match):
     parts = flow_match.split(" ")
     match = wildcard_create_bit_repeat(self.format["length"], 3)
     num_fields = 0
     for part in parts:
         if not part.startswith("priority") and part != "":
             fv = part.split("=")
             field = fv[0]
             value = fv[1]
             if field == "dl_src" or field == "dl_dst":
                 '''
                 m = mac_to_int(value)
                 if m != 0:
                   set_header_field(self.format, match, field, m, 0)
                 '''
                 pass
             elif field == "nw_src" or field == "nw_dst":
                 num_fields += 1
                 (ip, subnet) = dotted_subnet_to_int(value)
                 set_header_field(self.format, match, field, ip,
                                  32 - subnet)
             elif field == "nw_tos":
                 num_fields += 1
                 set_header_field(self.format, match, field, int(value), 0)
             elif field == "dl_type":
                 num_fields += 1
                 set_header_field(self.format, match, field,
                                  l2_proto_to_int(value), 0)
     if num_fields > 0:
         return match
     else:
         return None
Ejemplo n.º 7
0
 def __parse_flow_match(self,flow_match):
   parts = flow_match.split(" ")
   match = wildcard_create_bit_repeat(self.format["length"],3)
   num_fields = 0
   for part in parts:
     if not part.startswith("priority") and part != "":
       fv = part.split("=")
       field = fv[0]
       value = fv[1]
       if field == "dl_src" or field=="dl_dst":
         '''
         m = mac_to_int(value)
         if m != 0:
           set_header_field(self.format, match, field, m, 0)
         '''
         pass
       elif field == "nw_src" or field == "nw_dst":
         num_fields += 1
         (ip,subnet) = dotted_subnet_to_int(value)
         set_header_field(self.format, match, field, ip, 32-subnet)
       elif field == "nw_tos":
         num_fields += 1
         set_header_field(self.format, match, field, int(value), 0)
       elif field == "dl_type":
         num_fields += 1
         set_header_field(self.format, match, field, l2_proto_to_int(value), 0)
   if num_fields > 0:
     return match
   else:
     return None
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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
    cisco_router.SWITCH_ID_MULTIPLIER,
    "port_type_multiplier":
    cisco_router.PORT_TYPE_MULTIPLIER,
    "out_port_type_const":
    cisco_router.OUTPUT_PORT_TYPE_CONST,
    "remove_duplicates":
    True,
}

(ntf, ttf, name_to_id, id_to_name) = load_network(settings)

# create all-x packet as input headerspace.
all_x = wildcard_create_bit_repeat(ntf.length, 0x3)
# uncomment to set some field
#set_header_field(cisco_router.HS_FORMAT(), all_x, "field", value, right_mask)
set_header_field(cisco_router.HS_FORMAT(), all_x, "vlan", 10, 0)
test_pkt = headerspace(ntf.length)
test_pkt.add_hs(all_x)

#set some input/output ports
output_port_addition = cisco_router.PORT_TYPE_MULTIPLIER * \
cisco_router.OUTPUT_PORT_TYPE_CONST
src_port_id = name_to_id["yoza_rtr"]["te1/2"]
dst_port_ids = [name_to_id["poza_rtr"]["te2/4"] + output_port_addition]

#start reachability test and print results
st = time()
paths = find_reachability(ntf, ttf, src_port_id, dst_port_ids, test_pkt)
en = time()
print_paths(paths, id_to_name)
            "num_layers":3,
            "fwd_engine_layer":2,
            "input_path":"tf_stanford_backbone",
            "switch_id_multiplier":cisco_router.SWITCH_ID_MULTIPLIER,
            "port_type_multiplier":cisco_router.PORT_TYPE_MULTIPLIER,
            "out_port_type_const":cisco_router.OUTPUT_PORT_TYPE_CONST,
            "remove_duplicates":True,
            }

(ntf,ttf,name_to_id,id_to_name) = load_network(settings)

# create all-x packet as input headerspace.
all_x = wildcard_create_bit_repeat(ntf.length,0x3)
# uncomment to set some field
#set_header_field(cisco_router.HS_FORMAT(), all_x, "field", value, right_mask)
set_header_field(cisco_router.HS_FORMAT(), all_x, "vlan", 10, 0)
test_pkt = headerspace(ntf.length)
test_pkt.add_hs(all_x)

#set some input/output ports
output_port_addition = cisco_router.PORT_TYPE_MULTIPLIER * \
cisco_router.OUTPUT_PORT_TYPE_CONST
src_port_id = name_to_id["yoza_rtr"]["te1/2"]
dst_port_ids = [name_to_id["poza_rtr"]["te2/4"]+output_port_addition]

#start reachability test and print results
st = time()
paths = find_reachability(ntf, ttf, src_port_id, dst_port_ids, test_pkt)
en = time()
print_paths(paths, id_to_name)