Example #1
0
def test_gen_of_pd(tmpdir):
    input_p4 = os.path.join("tests", "p4_programs", "l2_openflow.p4")
    assert os.path.exists(input_p4)
    p = str(tmpdir)
    h = HLIR(input_p4)
    more_primitives = json.loads(
        resource_string(__name__,
                        os.path.join('..', 'p4c_bm', 'primitives.json')))
    h.add_primitives(more_primitives)
    assert h.build()
    json_dict = gen_json.json_dict_create(h)
    assert json_dict

    # hack the args
    from argparse import Namespace
    args = Namespace(plugin_list=["of"],
                     openflow_mapping_dir=os.path.join("tests", "of_mapping"),
                     openflow_mapping_mod="l2_openflow")

    gen_pd.generate_pd_source(json_dict, p, "pref", args)
    # now we check for all generated files
    of_path = tmpdir.join("plugin", "of")
    inc_path = of_path.join("inc")
    src_path = of_path.join("src")
    assert inc_path.ensure_dir()
    assert src_path.ensure_dir()
    expected_inc_path = "p4c_bm/plugin/of/inc"
    expected_inc = [f for f in os.listdir(expected_inc_path)]
    expected_src_path = "p4c_bm/plugin/of/src/"
    expected_src = [f for f in os.listdir(expected_src_path)]
    assert set(expected_inc) == set([f.basename for f in inc_path.listdir()])
    assert set(expected_src) == set([f.basename for f in src_path.listdir()])
Example #2
0
def test_gen_of_pd(tmpdir):
    input_p4 = os.path.join("tests", "p4_programs", "l2_openflow.p4")
    assert os.path.exists(input_p4)
    p = str(tmpdir)
    h = HLIR(input_p4)
    more_primitives = json.loads(
        resource_string(__name__,
                        os.path.join('..', 'p4c_bm', 'primitives.json'))
    )
    h.add_primitives(more_primitives)
    assert h.build()
    json_dict = gen_json.json_dict_create(h)
    assert json_dict

    # hack the args
    from argparse import Namespace
    args = Namespace(plugin_list=["of"],
                     openflow_mapping_dir=os.path.join("tests", "of_mapping"),
                     openflow_mapping_mod="l2_openflow")

    gen_pd.generate_pd_source(json_dict, p, "pref", args)
    # now we check for all generated files
    of_path = tmpdir.join("plugin", "of")
    inc_path = of_path.join("inc")
    src_path = of_path.join("src")
    assert inc_path.ensure_dir()
    assert src_path.ensure_dir()
    expected_inc_path = "p4c_bm/plugin/of/inc"
    expected_inc = [f for f in os.listdir(expected_inc_path)]
    expected_src_path = "p4c_bm/plugin/of/src/"
    expected_src = [f for f in os.listdir(expected_src_path)]
    assert set(expected_inc) == set([f.basename for f in inc_path.listdir()])
    assert set(expected_src) == set([f.basename for f in src_path.listdir()])
Example #3
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    # parse preprocessor flags
    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    if args.json:
        path_json = _validate_path(args.json)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        h = HLIR(args.source)
        h.add_preprocessor_args("-D__TARGET_BMV2__")
        for parg in preprocessor_args:
            h.add_preprocessor_args(parg)
        # in addition to standard P4 primitives
        more_primitives = json.loads(
            resource_string(__name__, 'primitives.json')
        )
        h.add_primitives(more_primitives)
        if not h.build(analyze=False):
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix)
Example #4
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    # parse preprocessor flags
    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    if args.json:
        path_json = _validate_path(args.json)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        h = HLIR(args.source)
        h.add_preprocessor_args("-D__TARGET_BMV2__")
        for parg in preprocessor_args:
            h.add_preprocessor_args(parg)
        # in addition to standard P4 primitives
        more_primitives = json.loads(
            resource_string(__name__, 'primitives.json'))
        h.add_primitives(more_primitives)
        if not h.build(analyze=False):
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix)
Example #5
0
def test_gen_json(input_p4):
    assert os.path.exists(input_p4)
    h = HLIR(input_p4)
    more_primitives = json.loads(
        resource_string(__name__,
                        os.path.join('..', 'p4c_bm', 'primitives.json')))
    h.add_primitives(more_primitives)
    assert h.build()
    json_dict = gen_json.json_dict_create(h)
    assert json_dict
Example #6
0
def test_gen_json(input_p4):
    assert os.path.exists(input_p4)
    h = HLIR(input_p4)
    more_primitives = json.loads(
        resource_string(__name__,
                        os.path.join('..', 'p4c_bm', 'primitives.json'))
    )
    h.add_primitives(more_primitives)
    assert h.build()
    json_dict = gen_json.json_dict_create(h)
    assert json_dict
Example #7
0
def test_gen_json(input_p4):
    assert os.path.exists(input_p4)
    h = HLIR(input_p4)
    more_primitives = json.loads(
        resource_string(__name__,
                        os.path.join('..', 'p4c_bm', 'primitives.json')))
    h.add_primitives(more_primitives)
    assert h.build()
    if "negative" in input_p4:  # negative test => compiler must exit
        with pytest.raises(SystemExit):
            gen_json.json_dict_create(h)
    else:
        # using keep_pragmas == True to maximize coverage
        json_dict = gen_json.json_dict_create(h, keep_pragmas=True)
        assert json_dict
Example #8
0
def test_gen_json(input_p4):
    assert os.path.exists(input_p4)
    h = HLIR(input_p4)
    more_primitives = json.loads(
        resource_string(__name__,
                        os.path.join('..', 'p4c_bm', 'primitives.json'))
    )
    h.add_primitives(more_primitives)
    assert h.build()
    if "negative" in input_p4:  # negative test => compiler must exit
        with pytest.raises(SystemExit):
            gen_json.json_dict_create(h)
    else:
        # using keep_pragmas == True to maximize coverage
        json_dict = gen_json.json_dict_create(h, keep_pragmas=True)
        assert json_dict
Example #9
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    if args.json:
        path_json = _validate_path(args.json)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        h = HLIR(args.source)
        h.add_preprocessor_args("-D__TARGET_BMV2__")
        # in addition to standard P4 primitives
        more_primitives = json.loads(
            resource_string(__name__, 'primitives.json')
        )
        h.add_primitives(more_primitives)
        if not h.build():
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix)
Example #10
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    if args.json:
        path_json = _validate_path(args.json)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        h = HLIR(args.source)
        h.add_preprocessor_args("-D__TARGET_BMV2__")
        # in addition to standard P4 primitives
        more_primitives = json.loads(
            resource_string(__name__, 'primitives.json'))
        h.add_primitives(more_primitives)
        if not h.build():
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix)
Example #11
0
def test_gen_pd(input_p4, tmpdir):
    assert os.path.exists(input_p4)
    p = str(tmpdir)
    h = HLIR(input_p4)
    more_primitives = json.loads(
        resource_string(__name__,
                        os.path.join('..', 'p4c_bm', 'primitives.json')))
    h.add_primitives(more_primitives)
    assert h.build()
    json_dict = gen_json.json_dict_create(h)
    assert json_dict
    gen_pd.generate_pd_source(json_dict, p, "pref")
    # now we check for all generated files
    inc_path = tmpdir.join("pd")
    src_path = tmpdir.join("src")
    assert inc_path.ensure_dir()
    assert src_path.ensure_dir()
    expected_inc_path = "p4c_bm/templates/pd/"
    expected_inc = [f for f in os.listdir(expected_inc_path)]
    expected_src_path = "p4c_bm/templates/src/"
    expected_src = [f for f in os.listdir(expected_src_path)]
    assert set(expected_inc) == set([f.basename for f in inc_path.listdir()])
    assert set(expected_src) == set([f.basename for f in src_path.listdir()])
Example #12
0
def test_gen_pd(input_p4, tmpdir):
    assert os.path.exists(input_p4)
    p = str(tmpdir)
    h = HLIR(input_p4)
    more_primitives = json.loads(
        resource_string(__name__,
                        os.path.join('..', 'p4c_bm', 'primitives.json'))
    )
    h.add_primitives(more_primitives)
    assert h.build()
    json_dict = gen_json.json_dict_create(h)
    assert json_dict
    gen_pd.generate_pd_source(json_dict, p, "pref")
    # now we check for all generated files
    inc_path = tmpdir.join("pd")
    src_path = tmpdir.join("src")
    assert inc_path.ensure_dir()
    assert src_path.ensure_dir()
    expected_inc_path = "p4c_bm/templates/pd/"
    expected_inc = [f for f in os.listdir(expected_inc_path)]
    expected_src_path = "p4c_bm/templates/src/"
    expected_src = [f for f in os.listdir(expected_src_path)]
    assert set(expected_inc) == set([f.basename for f in inc_path.listdir()])
    assert set(expected_src) == set([f.basename for f in src_path.listdir()])
Example #13
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    gen_dir = os.path.abspath(args.gen_dir)
    if os.path.exists(gen_dir):
        if not os.path.isdir(gen_dir):
            sys.stderr.write(args.gen_dir + " exists but is not a directory\n")
            sys.exit(1)
    else:
        try:
            os.mkdir(gen_dir)
        except:
            sys.stderr.write("Could not create output directory %s\n" %
                             args.gen_dir)
            sys.exit(1) 

    if args.p4_name:
        p4_name = args.p4_name
    else:
        p4_name = _get_p4_basename(args.source)

    if args.p4_prefix:
        p4_prefix = args.p4_prefix
    else:
        p4_prefix = p4_name

    h = HLIR(args.source)
    h.add_preprocessor_args("-D__TARGET_BM__")
    for parg in preprocessor_args:
        h.add_preprocessor_args(parg)
    # in addition to standard P4 primitives
    more_primitives = json.loads(resource_string(__name__, 'primitives.json'))
    h.add_primitives(more_primitives)

    if not h.build():
        print "Error while building HLIR"
        sys.exit(1)

    print "Generating files in directory", gen_dir

    render_dict = smart.render_dict_create(h, 
                                           p4_name, p4_prefix,
                                           args.meta_config,
                                           args.public_inc_path,
                                           dump_yaml = args.dump_yaml)

    if args.openflow_mapping_dir and args.openflow_mapping_mod:
        sys.path.append(args.openflow_mapping_dir)
        render_dict['openflow_mapping_mod'] = args.openflow_mapping_mod

    smart.render_all_files(render_dict, gen_dir,
                           with_thrift = args.thrift,
                           with_plugin_list = args.plugin_list)
Example #14
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    gen_dir = os.path.abspath(args.gen_dir)
    if os.path.exists(gen_dir):
        if not os.path.isdir(gen_dir):
            sys.stderr.write(args.gen_dir + " exists but is not a directory\n")
            sys.exit(1)
    else:
        try:
            os.mkdir(gen_dir)
        except:
            sys.stderr.write("Could not create output directory %s\n" %
                             args.gen_dir)
            sys.exit(1) 

    if args.p4_name:
        p4_name = args.p4_name
    else:
        p4_name = _get_p4_basename(args.source)

    if args.p4_prefix:
        p4_prefix = args.p4_prefix
    else:
        p4_prefix = p4_name

    h = HLIR(args.source)
    h.add_preprocessor_args("-D__TARGET_BM__")
    for parg in preprocessor_args:
        h.add_preprocessor_args(parg)
    # in addition to standard P4 primitives
    more_primitives = json.loads(resource_string(__name__, 'primitives.json'))
    h.add_primitives(more_primitives)

    if not h.build():
        print "Error while building HLIR"
        sys.exit(1)

    print "Generating files in directory", gen_dir

    render_dict = smart.render_dict_create(h, 
                                           p4_name, p4_prefix,
                                           args.meta_config,
                                           args.public_inc_path,
                                           dump_yaml = args.dump_yaml)

    # @OVS: dumps the render dict for flow_type_checker.py
    pickle.dump(render_dict, open(gen_dir+"/dict.pickle", "wb"))

    # @OVS: optimizer configurations
    # @Shahbaz: enumerate number of adjustment actions and based on that
    # set this to true or false.
    render_dict["OPT_INLINE_EDITING"] = False

    if args.openflow_mapping_dir and args.openflow_mapping_mod:
        sys.path.append(args.openflow_mapping_dir)
        render_dict['openflow_mapping_mod'] = args.openflow_mapping_mod

    smart.render_all_files(render_dict, gen_dir,
                           with_thrift = args.thrift,
                           with_plugin_list = args.plugin_list)
Example #15
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    gen_dir = os.path.abspath(args.gen_dir)
    if os.path.exists(gen_dir):
        if not os.path.isdir(gen_dir):
            sys.stderr.write(args.gen_dir + " exists but is not a directory\n")
            sys.exit(1)
    else:
        try:
            os.mkdir(gen_dir)
        except:
            sys.stderr.write("Could not create output directory %s\n" %
                             args.gen_dir)
            sys.exit(1)

    if args.p4_name:
        p4_name = args.p4_name
    else:
        p4_name = _get_p4_basename(args.source)

    if args.p4_prefix:
        p4_prefix = args.p4_prefix
    else:
        p4_prefix = p4_name

    h = HLIR(args.source)
    h.add_preprocessor_args("-D__TARGET_BM__")
    for parg in preprocessor_args:
        h.add_preprocessor_args(parg)
    # in addition to standard P4 primitives
    more_primitives = json.loads(resource_string(__name__, 'primitives.json'))
    h.add_primitives(more_primitives)

    if not h.build():
        print "Error while building HLIR"
        sys.exit(1)

    print "Generating files in directory", gen_dir

    render_dict = smart.render_dict_create(h,
                                           p4_name,
                                           p4_prefix,
                                           args.meta_config,
                                           args.public_inc_path,
                                           dump_yaml=args.dump_yaml)
    render_dict['hlir'] = h

    if args.openflow_mapping_dir and args.openflow_mapping_mod:
        sys.path.append(args.openflow_mapping_dir)
        render_dict['openflow_mapping_mod'] = args.openflow_mapping_mod

    smart.render_all_files(render_dict,
                           gen_dir,
                           with_thrift=args.thrift,
                           with_plugin_list=args.plugin_list,
                           with_plugin_path=args.plugin_path)