Пример #1
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 
Пример #2
0
def parse_single_h(header):
    hs_format = cisco_router.HS_FORMAT()
    header = header.strip("() ").split(",")
    hs_bytes = [b.strip("D") for b in reversed(header)]
    FEC_string = ""
    if "vlan_pos" in hs_format.keys(
    ) and hs_bytes[hs_format["vlan_pos"]] != "X":
        v = int(hs_bytes[hs_format["vlan_pos"]]) + \
            int(hs_bytes[hs_format["vlan_pos"] + 1]) * 256
        FEC_string = "VLAN:%d " % v
    if "ip_dst_pos" in hs_format.keys():
        ip_address = ""
        subnet = 32
        for i in range(4):
            if 'x' in hs_bytes[hs_format["ip_dst_pos"] + i]:
                oct = hs_bytes[hs_format["ip_dst_pos"] + i]
                subnet -= oct.count("x")
                oct = oct.replace("x", "0")
                ip_address = "%d.%s" % (int(oct, 2), ip_address)
            elif hs_bytes[hs_format["ip_dst_pos"] + i] == 'X':
                ip_address = "0.%s" % ip_address
                subnet -= 8
            else:
                ip_address = "%s.%s" % (hs_bytes[hs_format["ip_dst_pos"] + i],
                                        ip_address)
        FEC_string = FEC_string + "DST-IP:%s/%d " % (ip_address[:-1], subnet)
    return FEC_string
Пример #3
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
Пример #4
0
def get_openflow_rule(tfs,inv_mapf,rule_id):
  ofg = OpenFlow_Rule_Generator(None,cisco_router.HS_FORMAT())
  tokens = rule_id.split("_")
  tf_name = ""
  for i in range(len(tokens)-1):
    tf_name += tokens[i] + "_"
  tf_name = tf_name[:-1]
  if tf_name == "":
    tf = tfs["topology"]
    rule = tf.id_to_rule[rule_id]
    rprint = "LINK: %s-->%s"%(inv_mapf[rule["in_ports"][0]],inv_mapf[rule["out_ports"][0]])
    return rprint
  else:
    tf = tfs[tf_name]
    rule = tf.id_to_rule[rule_id]
    of_rule = ofg.parse_rule(rule)
    (match,rw) = ofg.pretify(of_rule)
    return "%s %s"%(match,rw)
    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)
Пример #6
0
#!/usr/bin/python

import argparse
import json
import sys
import copy
from config_parser.transfer_function_to_openflow import OpenFlow_Rule_Generator
from config_parser.cisco_router_parser import cisco_router
from headerspace.tf import TF
from utils.wildcard_utils import set_header_field
from utils.wildcard import wildcard_create_bit_repeat
from utils.helper import dotted_subnet_to_int

OUTPORT_CONST = cisco_router.OUTPUT_PORT_TYPE_CONST * cisco_router.PORT_TYPE_MULTIPLIER
INTER_CONST = cisco_router.INTERMEDIATE_PORT_TYPE_CONST * cisco_router.PORT_TYPE_MULTIPLIER
HS_FORMAT = cisco_router.HS_FORMAT()


def get_fwd_port_id(a_port):
    return int(
        a_port /
        cisco_router.SWITCH_ID_MULTIPLIER) * cisco_router.SWITCH_ID_MULTIPLIER


ofg = OpenFlow_Rule_Generator(None, cisco_router.HS_FORMAT())


def get_openflow_rule(rule, inv_mapf):
    in_ports = "in_ports:"
    for p in rule["in_ports"]:
        in_ports = in_ports + inv_mapf[p] + ","