コード例 #1
0
    def __init__(self, thrift_ip='localhost', thrift_port=9090):
        pre = runtime_CLI.PreType.SimplePreLAG
        services = runtime_CLI.RuntimeAPI.get_thrift_services(pre)
        services.extend(SimpleSwitchAPI.get_thrift_services())

        standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect(
            thrift_ip, thrift_port, services)

        runtime_CLI.load_json_config(standard_client)
        self.ssapi = SimpleSwitchAPI(pre, standard_client, mc_client,
                                     sswitch_client)

        self.json_str = runtime_CLI.utils.get_json_config(
            standard_client, None)
        self.json_ = json.loads(self.json_str)

        self.register_names = {
            it["name"]: it["size"]
            for it in self.json_["register_arrays"]
        }
        self.counter_names = {
            it["name"]: it["size"]
            for it in self.json_["counter_arrays"]
        }

        print()
コード例 #2
0
 def __init__(self, switch_config, thrift_ip, thrift_port):
     # Initialize own variables
     self.miss_counter = switch_config["miss_counter"]
     self.mismatch_counter = switch_config["mismatch_counter"]
     self.ip2hc_counter = switch_config["ip2hc_counter"]
     self.ip2hc_counter_bitmap = switch_config["ip2hc_counter_bitmap"]
     self.ip2hc_mat = switch_config["ip2hc_mat"]
     self.read_hc_function = switch_config["read_hc_function"]
     self.nethcf_state = switch_config["nethcf_state"]
     self.dirty_flag = switch_config["dirty_flag"]
     self.dirty_bitmap = switch_config["dirty_bitmap"]
     self.error_hint_str = ("Please check whether the switch "
                            "is well configured and running.")
     # Initialize API of BMv2 runtime
     pre = runtime_CLI.PreType.SimplePreLAG
     services = runtime_CLI.RuntimeAPI.get_thrift_services(pre)
     services.extend(SimpleSwitchAPI.get_thrift_services())
     try:
         standard_client, mc_client, sswitch_client = \
             runtime_CLI.thrift_connect(thrift_ip, thrift_port, services)
     except:
         print(self.error_hint_str)
         exit(-1)
     else:
         runtime_CLI.load_json_config(standard_client)
         self.runtime_api = SimpleSwitchAPI(pre, standard_client, mc_client,
                                            sswitch_client)
コード例 #3
0
def main():
    args = get_parser().parse_args()
    standard_client, mc_client = thrift_connect(
        args.thrift_ip, args.thrift_port,
        RuntimeAPI.get_thrift_services(args.pre))

    load_json_config(standard_client, args.json)
    PrettyAPI(args.pre, standard_client, mc_client).cmdloop()
コード例 #4
0
ファイル: p4dbg.py プロジェクト: pfpsim/p4-behavioral-model
 def do_CLI(self, line):
     "Connects to the bmv2 using the CLI, e.g. CLI table_dump <table_name>"
     if not with_runtime_CLI:
         return UIn_Error("Runtime CLI not available")
     services = runtime_CLI.RuntimeAPI.get_thrift_services(
         runtime_CLI.PreType.None)
     standard_client, mc_client = runtime_CLI.thrift_connect(
         args.thrift_ip, args.thrift_port, services)
     runtime_CLI.RuntimeAPI(runtime_CLI.PreType.None, standard_client,
                            mc_client).onecmd(line)
コード例 #5
0
def main():
    args = runtime_CLI.get_parser().parse_args()

    runtime_CLI.load_json(args.json)

    services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre)
    services.extend(SimpleSwitchAPI.get_thrift_services())

    standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect(
        args.thrift_ip, args.thrift_port, services
    )

    SimpleSwitchAPI(args.pre, standard_client, mc_client, sswitch_client).cmdloop()
コード例 #6
0
def main():
    args = runtime_CLI.get_parser().parse_args()

    args.pre = runtime_CLI.PreType.SimplePreLAG

    runtime_CLI.load_json(args.json)

    services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre)
    services.extend(SimpleSwitchAPI.get_thrift_services())

    standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect(args.thrift_ip, args.thrift_port, services)

    SimpleSwitchAPI(args.pre, standard_client, mc_client, sswitch_client).cmdloop()
コード例 #7
0
def main():
    args = runtime_CLI.get_parser().parse_args()

    args.pre = runtime_CLI.PreType.SimplePreLAG

    services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre)
    services.extend(PsaSwitchAPI.get_thrift_services())

    standard_client, mc_client, pswitch_client = runtime_CLI.thrift_connect(
        args.thrift_ip, args.thrift_port, services)

    runtime_CLI.load_json_config(standard_client, args.json, load_json_psa)

    PsaSwitchAPI(args.pre, standard_client, mc_client,
                 pswitch_client).cmdloop()
コード例 #8
0
def main():
    print("Jiarong: Enter sswitch_CLI.py")
    args = runtime_CLI.get_parser().parse_args()

    args.pre = runtime_CLI.PreType.SimplePreLAG

    services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre)
    services.extend(SimpleSwitchAPI.get_thrift_services())

    standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect(
        args.thrift_ip, args.thrift_port, services)

    runtime_CLI.load_json_config(standard_client, args.json)

    SimpleSwitchAPI(args.pre, standard_client, mc_client,
                    sswitch_client).cmdloop()
コード例 #9
0
    def __init__(self, thrift_ip='localhost', thrift_port=9090):
        pre = runtime_CLI.PreType.SimplePreLAG
        services = runtime_CLI.RuntimeAPI.get_thrift_services(pre)
        services.extend(SimpleSwitchAPI.get_thrift_services())

        standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect(
            thrift_ip, thrift_port, services)

        runtime_CLI.load_json_config(standard_client)
        self.ssapi = SimpleSwitchAPI(pre, standard_client, mc_client, sswitch_client)

        self.json_str = runtime_CLI.utils.get_json_config(standard_client, None)
        self.json_ = json.loads(self.json_str)

        self.register_names = {it["name"]: it["size"] for it in self.json_["register_arrays"]}
        self.counter_names = {it["name"]: it["size"] for it in self.json_["counter_arrays"]}

        print()
コード例 #10
0
def send_commands(thrift_port, json, commands):
    if not has_api:
        print(
            "Commands requested, but Runtime CLI not present! Ensure $BMV2_REPO/tools is on PYTHONPATH"
        )
        raise Exception("Could not execute commands: Runtime API not present")

    services = RuntimeAPI.get_thrift_services('SimplePre')
    services.extend(SimpleSwitchAPI.get_thrift_services())

    standard_client, mc_client, sswitch_client = thrift_connect(
        'localhost', thrift_port, services)

    load_json_config(standard_client, json)

    api = SimpleSwitchAPI('SimplePre', standard_client, mc_client,
                          sswitch_client)

    for command in commands:
        api.onecmd(command)
コード例 #11
0
def main():
    args = runtime_CLI.get_parser().parse_args()

    args.pre = runtime_CLI.PreType.SimplePreLAG

    services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre)
    services.extend(SimpleSwitchAPI.get_thrift_services())

    standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect(
        args.thrift_ip, args.thrift_port, services)

    runtime_CLI.load_json_config(standard_client, args.json)

    if args.command != []:
        for cmd in args.command:
            SimpleSwitchAPI(args.pre, standard_client, mc_client,
                            sswitch_client).onecmd(cmd)
    else:
        SimpleSwitchAPI(args.pre, standard_client, mc_client,
                        sswitch_client).cmdloop()
コード例 #12
0
ファイル: p4Controller.py プロジェクト: gustavo978/helpful
def main():
    # Init Log
    global process
    log = p4Logging()

    # Init the runtime API
    pre = runtime_CLI.PreType.SimplePreLAG
    services = runtime_CLI.RuntimeAPI.get_thrift_services(pre)
    services.extend(SimpleSwitchAPI.get_thrift_services())
    standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect(
        'localhost', 9090, services)
    runtime_CLI.load_json_config(standard_client, "")
    cli = SimpleSwitchAPI(pre, standard_client, mc_client, sswitch_client)

    # Add default rules on the switch
    process = ProcessPacketOut(cli, log)
    process.add_default_rules()

    #Start sniff
    sniff(iface="s1-eth3", prn=process_packet, store=0)
コード例 #13
0
ファイル: controller.py プロジェクト: p4-programs/p4-nat
def main():

    global rAPI, rndDesc

    args = get_parser().parse_args()

    standard_client, mc_client = thrift_connect(
        args.thrift_ip, args.thrift_port,
        RuntimeAPI.get_thrift_services(args.pre))

    load_json_config(standard_client, args.json)

    rAPI = RuntimeAPI(args.pre, standard_client, mc_client)

    # PRNG
    rndDesc = Random.new()

    print "sniffing on %s" % args.iface
    sys.stdout.flush()
    sniff(iface=args.iface, prn=handle_pkt(rAPI))
    rndDesc.close()
コード例 #14
0
 def ConnectToThrift(self, ip, port, services, json):
     standard_client, mc_client = cli.thrift_connect(
         ip, port, cli.RuntimeAPI.get_thrift_services(services))
     cli.load_json_config(standard_client, json)
     return standard_client, mc_client
コード例 #15
0
                    help='cpu port',
                    type=str,
                    action="store",
                    default='veth250')
args = parser.parse_args()
#don't care about multicast in this example
args.pre = runtime_CLI.PreType.None

services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre)
services.extend(SimpleSwitchAPI.get_thrift_services())

# standard client deals in tables (add, delete, get)
# mc_client deals with multicast groups
# sswitch_client deals with mirroring
# for this very simple example, focus is on standard_client
standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect(
    args.thrift_ip, args.thrift_port, services)
# load table info from compiled json
runtime_CLI.load_json_config(standard_client, args.json)


def get_table(table_name):
    key = (runtime_CLI.ResType.table, table_name)
    if key in runtime_CLI.SUFFIX_LOOKUP_MAP:
        return runtime_CLI.SUFFIX_LOOKUP_MAP[(runtime_CLI.ResType.table,
                                              table_name)]
    raise Exception("no table " + table_name + " found")


def i2bytes(nr, bw):
    return runtime_CLI.int_to_bytes(nr, (bw + 7) / 8)
コード例 #16
0
    def __init__(self, json_file, folder, num_ports=8):
        self.modified_tables = []

        self.json_file = json_file
        self.json_file_abspath = os.path.abspath(json_file)
        self.num_ports = num_ports
        self.pcap_filename_prefix = 'pcap'

        # TBD: See bmv2stf.py for ideas on running multiple
        # simple_switch processes in parallel on the same machine.
        # This code does not support that yet.
        self.folder = folder
        self.thrift_port_num = 9090

        # See step (1) above
        intf_args = []
        self.intf_info = {}
        for i in range(self.num_ports):
            intf_args.append('-i')
            intf_args.append(self.intf_num_to_simple_switch_arg(i))
            self.intf_info[i] = {
                'pcap_in_fname': self.intf_num_to_filename(i, 'in'),
                'pcap_out_fname': self.intf_num_to_filename(i, 'out')
            }
            logging.debug("Creating named pipe '%s'"
                          "" % (self.intf_info[i]['pcap_in_fname']))
            os.mkfifo(self.intf_info[i]['pcap_in_fname'])

        # Workaround for problem that I have only seen on some
        # systems, but not others, for reasons that I don't
        # understand.  The symptom of the problem is that when running
        # pytest, even as root, multiple of the test cases fail with
        # "Exception: Initializing simple_switch failed", and the
        # following output labeled "Captured stderr call".

        # Nanomsg returned a exception when trying to bind to address 'ipc:///tmp/bmv2-0-notifications.ipc'.
        # The exception is: Address already in use
        # This may happen if
        # 1) the address provided is invalid,
        # 2) another instance of bmv2 is running and using the same address, or
        # 3) you have insufficent permissions (e.g. you are using an IPC socket on Unix, the file already exists and you don't have permission to access it)

        # I have tried adding debug messages to a few places in the
        # simple_switch executable to discover why this failure
        # occurs, but haven't discovered a reason for it.  Removing
        # this file seems to avoid the problem.
        ipc_fname = '/tmp/bmv2-0-notifications.ipc'
        if os.path.exists(ipc_fname):
            logf_append('Found file %s -- try to remove it' % (ipc_fname))
            os.remove('/tmp/bmv2-0-notifications.ipc')
            if os.path.exists(ipc_fname):
                logf_append('After trying to remove file %s it still exists'
                            '' % (ipc_fname))
            else:
                logf_append('File %s successfully removed' % (ipc_fname))
        else:
            logf_append('No file found: %s -- good' % (ipc_fname))

        # Start simple_switch.   See step (2) above.
        ss_cmd_args = ([
            'simple_switch', '--log-console', '--thrift-port',
            str(self.thrift_port_num), '--use-files', '0'
        ] + intf_args + [self.json_file_abspath])
        logging.debug("Starting simple_switch in directory %s with args: %s"
                      "" % (self.folder, ss_cmd_args))
        self.proc = subprocess.Popen(
            ss_cmd_args, stdout=subprocess.PIPE, cwd=self.folder)

        # See step (3) above.
        # p4c/backends/bmv2/bmv2stf.py has scary-looking "DANGER"
        # comment about opening the named fifos in the same order that
        # simple_switch does, else things can deadlock.  Not sure why
        # deadlock might occur, but may as well do it the same order
        # here.
        for i in sorted(self.intf_info):
            self.open_pcap_in_intf(i)

        # Wait for simple_switch to finish initializing
        # See step (4) above.
        init_done = False
        ss_ready_msg = 'Pcap reader: starting PcapFilesReader'
        logging.debug("Waiting for this line of output from simple_switch: %s",
                      ss_ready_msg)
        for line in iter(self.proc.stdout.readline, ''):
            logging.debug("Line from simple_switch log: %s", line.rstrip())
            if ss_ready_msg in str(line):
                logging.debug("Found expected log line.  Considering"
                              " simple_switch initialized enough to continue")
                init_done = True
                break

        if not init_done:
            raise Exception('Initializing simple_switch failed')

        # See step (5) above.
        # XXX: read params from config
        pre = PreType.SimplePreLAG
        num_tries = 0
        while True:
            try:
                standard_client, mc_client = thrift_connect(
                    'localhost',
                    str(self.thrift_port_num),
                    RuntimeAPI.get_thrift_services(pre))
            except:
                num_tries += 1
                time.sleep(1)
                logging.debug("Failed thrift_connect attempt try #%d"
                              "" % (num_tries))
                if num_tries == 3:
                    logging.error("Failed 3 times in a row. Giving up.")
                    sys.exit(1)
            else:
                break

        load_json_config(standard_client)
        self.api = RuntimeAPI(pre, standard_client, mc_client)
コード例 #17
0
def connect_to_switch(switch_port):
    standard_client, mc_client = runtime_CLI.thrift_connect(
        "127.0.0.1", int(switch_port),
        runtime_CLI.RuntimeAPI.get_thrift_services(1))
    return standard_client, mc_client