Example #1
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 #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 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 #4
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 #5
0
def test_gen_json_field_aliases(input_aliases):
    assert os.path.exists(input_aliases)

    input_p4 = os.path.join("tests", "p4_programs", "triv_eth.p4")
    assert os.path.exists(input_p4)
    h = HLIR(input_p4)
    assert h.build()

    if "error" in input_aliases:
        # make sure that the program exits
        with pytest.raises(SystemExit):
            gen_json.json_dict_create(h, input_aliases)
    else:
        assert "sample" in input_aliases
        json_dict = gen_json.json_dict_create(h, input_aliases)
        assert json_dict
        assert "field_aliases" in json_dict
Example #6
0
def test_gen_json_field_aliases(input_aliases):
    assert os.path.exists(input_aliases)

    input_p4 = os.path.join("tests", "p4_programs", "triv_eth.p4")
    assert os.path.exists(input_p4)
    h = HLIR(input_p4)
    assert h.build()

    if "error" in input_aliases:
        # make sure that the program exits
        with pytest.raises(SystemExit):
            gen_json.json_dict_create(h, input_aliases)
    else:
        assert "sample" in input_aliases
        json_dict = gen_json.json_dict_create(h, input_aliases)
        assert json_dict
        assert "field_aliases" in 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()
    json_dict = gen_json.json_dict_create(h)
    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()
    json_dict = gen_json.json_dict_create(h)
    assert json_dict
Example #9
0
def test_gen_pd(input_p4, tmpdir):
    assert os.path.exists(input_p4)
    p = str(tmpdir)
    h = HLIR(input_p4)
    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 #10
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 #11
0
def main():
    argparser = argparse.ArgumentParser(
        description="P4 to Bluespec Translator")
    argparser.add_argument('source',
                           metavar='source',
                           type=str,
                           help='A source file to include in the P4 program.')
    argparser.add_argument('--json',
                           dest='json',
                           type=str,
                           help='Dump the JSON representation to this file.',
                           required=False)
    argparser.add_argument('--p4-v1.1',
                           action='store_true',
                           help='Run the compiler on a P4 v1.1 program.',
                           default=False,
                           required=False)
    argparser.add_argument('--mem',
                           action='store_true',
                           help='Generate Pipeline with Shared Buffer.',
                           default=False,
                           required=False)
    options = argparser.parse_args()

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

    p4_v1_1 = getattr(options, 'p4_v1.1')
    if p4_v1_1:
        try:
            import p4_hlir_v1_1  # NOQA
        except ImportError:  # pragma: no cover
            print "You requested P4 v1.1 but the corresponding p4-hlir",\
                "package does not seem to be installed"
            sys.exit(1)

    if p4_v1_1:
        from p4_hlir_v1_1.main import HLIR
        primitives_res = 'primitives_v1_1.json'
    else:
        from p4_hlir.main import HLIR
        primitives_res = 'primitives.json'

    h = HLIR(options.source)

    more_primitives = json.loads(resource_string(__name__, primitives_res))
    h.add_primitives(more_primitives)

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

    # frontend
    json_dict = gen_json.json_dict_create(h, None, p4_v1_1)
    if options.json:
        print "Generating json output to", path_json
        with open(path_json, 'w') as fp:
            json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    # entry point for mid-end
    ir = p4fpga.ir_create(json_dict)

    if not os.path.exists("generatedbsv"):
        os.makedirs("generatedbsv")

    p4name = os.path.splitext(os.path.basename(options.source))[0]
    p4name = re.sub(r'\d+[-]+', '', p4name)

    generate_struct(ir)
    generate_parser(ir)
    generate_deparser(ir)
    generate_basicblock(ir)
    generate_union(ir)
    generate_table(ir)
    generate_pipeline(ir)

    with_mem = getattr(options, 'mem')
    if with_mem:
        generate_file(top.TopMemory(p4name),
                      os.path.join('generatedbsv', "Main.bsv"))
    else:
        generate_file(top.TopStream(p4name),
                      os.path.join('generatedbsv', 'Main.bsv'))

    generate_file(top.API(p4name), os.path.join('generatedbsv', "MainAPI.bsv"))
    generate_file(top.Defs([]), os.path.join('generatedbsv', "MainDefs.bsv"))

    bsvgen_table.simgen()
Example #12
0
def test_gen_json(input_p4):
    assert os.path.exists(input_p4)
    h = HLIR(input_p4)
    assert h.build()
    json_dict = gen_json.json_dict_create(h)
    assert json_dict