├── .bazelrc ├── .bazelversion ├── .clang_format_extra.cfg ├── .dockerignore ├── .github ├── dependabot.yml └── workflows │ ├── bazel-build.yml │ ├── build.yml │ └── test.yml ├── .gitignore ├── .gitmodules ├── BUILD ├── CLI ├── .gitignore ├── Makefile.am ├── act_profs │ ├── act_prof_common.c │ ├── act_prof_common.h │ ├── act_prof_create_member.c │ ├── act_prof_dump.c │ └── act_prof_groups.c ├── add_p4.c ├── commands.h ├── counter_commands.c ├── device_commands.c ├── direct_res_reset.c ├── error_codes.c ├── error_codes.h ├── main.c ├── meter_commands.c ├── p4_config_repo.c ├── p4_config_repo.h ├── table_add.c ├── table_common.c ├── table_common.h ├── table_delete.c ├── table_dump.c ├── table_modify.c ├── table_reset_default.c ├── table_set_default.c ├── utils.c └── utils.h ├── CONTRIBUTING.md ├── Dockerfile ├── Dockerfile.bmv2 ├── LICENSE ├── Makefile.am ├── README.md ├── WORKSPACE ├── autogen.sh ├── bazel ├── BUILD ├── deps.bzl ├── external │ └── gnmi.BUILD └── workspace_rule.bzl ├── bin ├── .gitignore ├── Makefile.am ├── gen_pi_json.c └── rpc_server.c ├── configure.ac ├── docs ├── 00_revision_history.md ├── 01_overview.md ├── 02_architecture.md ├── bfn_latex.sty ├── doxygen.cfg └── msg_format.md ├── examples ├── .clang_format_extra.cfg ├── .gitignore ├── Makefile.am ├── README.md ├── example_1.c ├── example_2.c └── testdata │ └── simple_router.json ├── frontends_extra ├── Makefile.am └── cpp │ ├── BUILD │ ├── CPPLINT.cfg │ ├── Makefile.am │ ├── PI │ └── frontends │ │ └── cpp │ │ └── tables.h │ ├── configure.ac │ ├── example │ ├── .gitignore │ ├── CPPLINT.cfg │ ├── Makefile.am │ └── example.cpp │ ├── m4 │ ├── ax_cxx_compile_stdcxx_11.m4 │ └── ax_pthread.m4 │ └── src │ └── tables.cpp ├── generators ├── .gitignore ├── Makefile.am └── fe_defines.c ├── include ├── Makefile.am └── PI │ ├── frontends │ └── generic │ │ └── pi.h │ ├── int │ ├── pi_int.h │ ├── rpc_common.h │ └── serialize.h │ ├── p4info.h │ ├── p4info │ ├── act_profs.h │ ├── actions.h │ ├── counters.h │ ├── digests.h │ ├── meters.h │ └── tables.h │ ├── pi.h │ ├── pi_act_prof.h │ ├── pi_base.h │ ├── pi_clone.h │ ├── pi_counter.h │ ├── pi_learn.h │ ├── pi_mc.h │ ├── pi_meter.h │ ├── pi_tables.h │ ├── pi_value.h │ └── target │ ├── pi_act_prof_imp.h │ ├── pi_clone_imp.h │ ├── pi_counter_imp.h │ ├── pi_imp.h │ ├── pi_learn_imp.h │ ├── pi_mc_imp.h │ ├── pi_meter_imp.h │ └── pi_tables_imp.h ├── lib ├── BUILD ├── Makefile.am ├── _assert.c ├── _assert.h ├── read_file.c ├── read_file.h ├── vector.c └── vector.h ├── m4 ├── .gitignore ├── adl_recursive_eval.m4 ├── ax_cxx_compile_stdcxx_11.m4 ├── ax_prog_doxygen.m4 └── ax_pthread.m4 ├── proto ├── .gitignore ├── BUILD ├── CPPLINT.cfg ├── Makefile.am ├── PI │ └── proto │ │ └── util.h ├── README.md ├── configure.ac ├── demo_grpc │ ├── .gitignore │ ├── 1sw_demo.py │ ├── Makefile.am │ ├── README.md │ ├── app.cpp │ ├── p4_mininet.py │ ├── pi_server_main.cpp │ ├── resources │ │ ├── architecture.graphml │ │ ├── architecture.png │ │ └── demo_topology.png │ ├── simple_router.json │ ├── simple_router.p4 │ ├── simple_router.p4info.txt │ ├── simple_router_mgr.cpp │ ├── simple_router_mgr.h │ ├── simple_router_wcounter.json │ ├── simple_router_wcounter.p4 │ ├── simple_router_wcounter.p4info.txt │ ├── test_perf.cpp │ ├── web_server.cpp │ └── web_server.h ├── frontend │ ├── BUILD │ ├── Makefile.am │ ├── PI │ │ └── frontends │ │ │ └── proto │ │ │ ├── device_mgr.h │ │ │ └── logging.h │ └── src │ │ ├── access_arbitration.cpp │ │ ├── access_arbitration.h │ │ ├── action_helpers.cpp │ │ ├── action_helpers.h │ │ ├── action_prof_mgr.cpp │ │ ├── action_prof_mgr.h │ │ ├── bimap.h │ │ ├── common.cpp │ │ ├── common.h │ │ ├── device_mgr.cpp │ │ ├── digest_mgr.cpp │ │ ├── digest_mgr.h │ │ ├── idle_timeout_buffer.cpp │ │ ├── idle_timeout_buffer.h │ │ ├── logger.h │ │ ├── logging.cpp │ │ ├── match_key_helpers.cpp │ │ ├── match_key_helpers.h │ │ ├── packet_io_mgr.cpp │ │ ├── packet_io_mgr.h │ │ ├── pre_clone_mgr.cpp │ │ ├── pre_clone_mgr.h │ │ ├── pre_mc_mgr.cpp │ │ ├── pre_mc_mgr.h │ │ ├── report_error.h │ │ ├── status_macros.h │ │ ├── statusor.h │ │ ├── table_info_store.cpp │ │ ├── table_info_store.h │ │ ├── task_queue.h │ │ ├── watch_port_enforcer.cpp │ │ └── watch_port_enforcer.h ├── gnmi ├── google │ └── rpc │ │ ├── code.proto │ │ └── status.proto ├── m4 │ ├── ax_boost_base.m4 │ ├── ax_boost_system.m4 │ ├── ax_boost_thread.m4 │ ├── ax_compare_version.m4 │ ├── ax_cxx_compile_stdcxx_11.m4 │ ├── ax_pthread.m4 │ └── pkg.m4 ├── p4 │ ├── config │ ├── server │ │ └── v1 │ │ │ └── config.proto │ ├── tmp │ │ └── p4config.proto │ └── v1 ├── p4info │ ├── .gitignore │ ├── BUILD │ ├── Makefile.am │ ├── PI │ │ └── proto │ │ │ └── p4info_to_and_from_proto.h │ ├── convert_p4info.cpp │ ├── p4info_to_and_from_proto.cpp │ └── xform │ │ ├── README.md │ │ ├── examples │ │ ├── anno_xform1.p4 │ │ ├── descrip.txt │ │ └── gen_p4info.sh │ │ └── xform_anno.py ├── ptf │ ├── .gitignore │ ├── P4RuntimePTF.pptx │ ├── README.md │ ├── base_test.py │ ├── bmv2 │ │ ├── gen_bmv2_config.py │ │ ├── gen_bmv2_config.sh │ │ ├── port_map.json │ │ ├── start_switch.sh │ │ ├── veth_setup.sh │ │ └── veth_teardown.sh │ ├── l3_host_fwd │ │ ├── l3_host_fwd.p4 │ │ └── test │ │ │ └── l3_host_fwd.py │ └── ptf_runner.py ├── server │ ├── BUILD │ ├── Makefile.am │ ├── PI │ │ ├── CPPLINT.cfg │ │ └── proto │ │ │ └── pi_server.h │ ├── gnmi.h │ ├── gnmi_dummy.cpp │ ├── gnmi_sysrepo.cpp │ ├── log.h │ ├── pi_server.cpp │ ├── pi_server_testing.h │ ├── shared_mutex.h │ ├── uint128.cpp │ └── uint128.h ├── server_config │ ├── server_config.cpp │ └── server_config.h ├── src │ └── util.cpp ├── sysrepo │ ├── docker_entry_point.sh │ └── install_yangs.sh.in ├── tests │ ├── .gitignore │ ├── BUILD │ ├── CPPLINT.cfg │ ├── Makefile.am │ ├── main.cpp │ ├── matchers.cpp │ ├── matchers.h │ ├── mock_switch.cpp │ ├── mock_switch.h │ ├── server │ │ ├── test_arbitration.cpp │ │ ├── test_gnmi.cpp │ │ ├── test_no_pipeline_config.cpp │ │ ├── test_pi_server.cpp │ │ ├── test_server_config.cpp │ │ ├── utils.cpp │ │ └── utils.h │ ├── stream_receiver.h │ ├── test_p4info_convert.cpp │ ├── test_proto_fe.cpp │ ├── test_proto_fe_access_arbitration.cpp │ ├── test_proto_fe_base.cpp │ ├── test_proto_fe_base.h │ ├── test_proto_fe_bytestrings.cpp │ ├── test_proto_fe_digest.cpp │ ├── test_proto_fe_packet_io.cpp │ ├── test_proto_fe_set_pipeline_config.cpp │ ├── test_proto_fe_watch_port_enforcer.cpp │ └── test_task_queue.cpp ├── third_party │ ├── BUILD │ ├── Makefile.am │ └── fmt │ │ ├── LICENSE.rst │ │ ├── _assert.cc │ │ ├── _assert.h │ │ ├── container.h │ │ ├── format.cc │ │ ├── format.h │ │ ├── ostream.cc │ │ ├── ostream.h │ │ ├── posix.cc │ │ ├── posix.h │ │ ├── printf.cc │ │ ├── printf.h │ │ ├── string.h │ │ └── time.h └── yang │ ├── iana-if-type.yang │ ├── ietf-inet-types@2013-07-15.yang │ ├── ietf-interfaces@2014-05-08.yang │ ├── ietf-netconf-acm@2012-02-22.yang │ ├── ietf-netconf-notifications.yang │ ├── ietf-netconf@2011-06-01.yang │ └── ietf-yang-types@2013-07-15.yang ├── src ├── Makefile.am ├── cb_mgr.c ├── cb_mgr.h ├── config_readers │ ├── bmv2_json_reader.c │ ├── native_json_reader.c │ └── readers.h ├── device_map.c ├── device_map.h ├── frontends │ └── generic │ │ └── pi.c ├── p4info │ ├── act_profs.c │ ├── act_profs_int.h │ ├── actions.c │ ├── actions_int.h │ ├── counters.c │ ├── counters_int.h │ ├── digests.c │ ├── digests_int.h │ ├── fast_id_vector.c │ ├── fast_id_vector.h │ ├── meters.c │ ├── meters_int.h │ ├── p4info.c │ ├── p4info_common.c │ ├── p4info_common.h │ ├── p4info_name_map.c │ ├── p4info_name_map.h │ ├── p4info_struct.c │ ├── p4info_struct.h │ ├── tables.c │ └── tables_int.h ├── p4info_int.h ├── pi.c ├── pi_act_prof.c ├── pi_clone.c ├── pi_counter.c ├── pi_learn.c ├── pi_learn_int.h ├── pi_mc.c ├── pi_meter.c ├── pi_notifications_pub.c ├── pi_notifications_pub.h ├── pi_rpc_server.c ├── pi_tables.c ├── pi_tables_int.h ├── pi_value.c └── utils │ ├── logging.c │ ├── logging.h │ ├── serialize.c │ └── utils.h ├── targets ├── Makefile.am ├── bmv2 │ ├── .gitignore │ ├── CPPLINT.cfg │ ├── Makefile.am │ ├── action_helpers.cpp │ ├── action_helpers.h │ ├── common.cpp │ ├── common.h │ ├── configure.ac │ ├── conn_mgr.cpp │ ├── conn_mgr.h │ ├── cpu_send_recv.cpp │ ├── cpu_send_recv.h │ ├── direct_res_spec.cpp │ ├── direct_res_spec.h │ ├── m4 │ │ ├── adl_recursive_eval.m4 │ │ ├── ax_cxx_compile_stdcxx_11.m4 │ │ ├── ax_prefix_config_h.m4 │ │ └── ax_pthread.m4 │ ├── pi_act_prof_imp.cpp │ ├── pi_clone_imp.cpp │ ├── pi_counter_imp.cpp │ ├── pi_imp.cpp │ ├── pi_learn_imp.cpp │ ├── pi_mc_imp.cpp │ ├── pi_meter_imp.cpp │ └── pi_tables_imp.cpp ├── dummy │ ├── Makefile.am │ ├── func_counter.c │ ├── func_counter.h │ ├── pi_act_prof_imp.c │ ├── pi_clone_imp.c │ ├── pi_counter_imp.c │ ├── pi_imp.c │ ├── pi_learn_imp.c │ ├── pi_mc_imp.c │ ├── pi_meter_imp.c │ └── pi_tables_imp.c └── rpc │ ├── Makefile.am │ ├── README.md │ ├── notifications.c │ ├── pi_act_prof_imp.c │ ├── pi_clone_imp.c │ ├── pi_counter_imp.c │ ├── pi_imp.c │ ├── pi_learn_imp.c │ ├── pi_mc_imp.c │ ├── pi_meter_imp.c │ ├── pi_rpc.c │ ├── pi_rpc.h │ └── pi_tables_imp.c ├── tests ├── .gitignore ├── BUILD ├── CLI │ ├── .gitignore │ ├── Makefile.am │ ├── act_prof.test │ ├── counter.test │ ├── counter_and_meter_direct.test │ ├── counter_direct.test │ ├── device_commands.test │ ├── device_update.test │ ├── direct_res_reset.test │ ├── meter.test │ ├── meter_direct.test │ ├── run_one_test.py.in │ ├── table_dump.test │ ├── table_dump_valid.test │ ├── table_indirect.test │ ├── table_wkey.test │ ├── test.supp │ └── testdata │ │ ├── .gitignore │ │ ├── act_prof.in │ │ ├── act_prof.out │ │ ├── counter.in │ │ ├── counter.out │ │ ├── counter_and_meter_direct.in │ │ ├── counter_and_meter_direct.out │ │ ├── counter_direct.in │ │ ├── counter_direct.out │ │ ├── device_commands.in │ │ ├── device_commands.out │ │ ├── device_update.in │ │ ├── device_update.out │ │ ├── direct_res_reset.in │ │ ├── direct_res_reset.out │ │ ├── ecmp.json │ │ ├── meter.in │ │ ├── meter.out │ │ ├── meter_direct.in │ │ ├── meter_direct.out │ │ ├── simple_router.json │ │ ├── stats.json │ │ ├── swap_1.json │ │ ├── swap_1.p4 │ │ ├── swap_2.json │ │ ├── swap_2.p4 │ │ ├── table_dump.in │ │ ├── table_dump.out │ │ ├── table_dump_valid.in │ │ ├── table_dump_valid.out │ │ ├── table_indirect.in │ │ ├── table_indirect.out │ │ ├── table_wkey.in │ │ ├── table_wkey.out │ │ └── valid.json ├── Makefile.am ├── frontends │ └── generic │ │ └── test.c ├── main.c ├── test_bmv2_json_reader.c ├── test_getnetv.c ├── test_p4info.c ├── testdata │ ├── act_prof.json │ ├── act_prof.p4 │ ├── customids.p4 │ ├── customids.p4info.txt │ ├── ecmp.json │ ├── id_collision.json │ ├── l2_switch.json │ ├── l2_switch.p4 │ ├── pragmas.json │ ├── pragmas.p4 │ ├── reconcile_1.p4info.txt │ ├── reconcile_2.p4info.txt │ ├── reconcile_3.p4info.txt │ ├── simple_router.json │ ├── stats.json │ ├── stats.p4 │ ├── unittest.entries.txt │ ├── unittest.p4 │ ├── unittest.p4info.txt │ ├── valid.json │ └── valid.p4 ├── utils.c └── utils.h ├── third_party ├── BUILD ├── Makefile.am ├── cJSON │ ├── BUILD │ ├── Makefile.am │ ├── README │ ├── include │ │ └── cJSON │ │ │ └── cJSON.h │ └── src │ │ └── cJSON.c ├── gtest.am ├── unity │ ├── Makefile.am │ ├── README.md │ ├── include │ │ └── unity │ │ │ ├── unity.h │ │ │ ├── unity_fixture.h │ │ │ ├── unity_fixture_internals.h │ │ │ ├── unity_fixture_malloc_overrides.h │ │ │ └── unity_internals.h │ ├── license.txt │ └── src │ │ ├── unity.c │ │ └── unity_fixture.c └── uthash.am └── tools ├── check_style.sh ├── clang_format_check.py ├── cpplint.py ├── run_valgrind.sh └── update_test_bmv2_jsons.py /.bazelrc: -------------------------------------------------------------------------------- 1 | build --conlyopt='-std=gnu11' --cxxopt='-std=gnu++14' 2 | -------------------------------------------------------------------------------- /.bazelversion: -------------------------------------------------------------------------------- 1 | 5.4.0 2 | -------------------------------------------------------------------------------- /.clang_format_extra.cfg: -------------------------------------------------------------------------------- 1 | set noparent 2 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | **/.git/* 2 | **/.gitmodules 3 | .github 4 | **/Dockerfile 5 | **/Dockerfile.bmv2 6 | **/*.md 7 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | # Workflow files stored in the default location of `.github/workflows` 5 | directory: "/" 6 | schedule: 7 | interval: "weekly" 8 | open-pull-requests-limit: 5 9 | -------------------------------------------------------------------------------- /.github/workflows/bazel-build.yml: -------------------------------------------------------------------------------- 1 | name: "Test Bazel Build" 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | schedule: 9 | - cron: "0 0 * * *" 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-22.04 14 | env: 15 | BAZEL: bazelisk-linux-amd64 16 | steps: 17 | - uses: actions/checkout@v4 18 | with: 19 | submodules: recursive 20 | 21 | - name: Mount bazel cache 22 | uses: actions/cache@v4 23 | with: 24 | # See https://docs.bazel.build/versions/master/output_directories.html 25 | path: "~/.cache/bazel" 26 | # Create a new cache entry whenever Bazel files change. 27 | # See https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows 28 | key: bazel-${{ runner.os }}-build-${{ hashFiles('**/*.bzl', '**/*.bazel') }} 29 | restore-keys: | 30 | bazel-${{ runner.os }}-build- 31 | 32 | - name: Install bazelisk 33 | run: | 34 | curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/v1.15.0/$BAZEL" 35 | chmod +x $BAZEL 36 | sudo mv $BAZEL /usr/local/bin/bazel 37 | 38 | - name: Build and run tests 39 | run: bazel test //proto/tests:pi_proto_tests //proto/tests:pi_proto_server_tests 40 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build and push latest image 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - stable 8 | schedule: 9 | # We run it every 4 hours, starting at 02:15. 10 | # Note that this will only update the 'latest' tag (not the 'stable' tag). 11 | # Ideally, we would update the image every time the base image 12 | # (p4lang/third-party) is updated, but we deem this good enough. 13 | - cron: '15 2-23/4 * * *' # "At minute 15 past every 4th hour from 2 through 23." 14 | 15 | jobs: 16 | build: 17 | if: ${{ github.repository == 'p4lang/PI' }} 18 | runs-on: [ubuntu-latest] 19 | steps: 20 | - uses: actions/checkout@v4 21 | with: 22 | submodules: recursive 23 | - name: Determine Docker image tag 24 | id: get-tag 25 | shell: bash 26 | run: | 27 | TAG="" 28 | if [[ "$GITHUB_REF" =~ "main" ]]; then 29 | TAG="latest" 30 | elif [[ "$GITHUB_REF" =~ "stable" ]]; then 31 | TAG="stable" 32 | else 33 | echo "Invalid Github ref $GITHUB_REF" 34 | exit 1 35 | fi 36 | echo "Tag is $TAG" 37 | echo "::set-output name=tag::$TAG" 38 | - name: Set up Docker Buildx 39 | uses: docker/setup-buildx-action@v3 40 | - name: Login to DockerHub 41 | uses: docker/login-action@v3 42 | with: 43 | username: ${{ secrets.DOCKER_USERNAME }} 44 | password: ${{ secrets.DOCKER_PASSWORD }} 45 | - name: Build and push Docker image to registry 46 | uses: docker/build-push-action@v6 47 | with: 48 | context: . 49 | push: true 50 | tags: p4lang/pi:${{ steps.get-tag.outputs.tag }} 51 | cache-from: type=gha 52 | cache-to: type=gha,mode=max 53 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | push: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | check-changes: 13 | name: Check whether tests need to be run based on diff 14 | runs-on: [ubuntu-latest] 15 | steps: 16 | - uses: actions/checkout@v4 17 | with: 18 | fetch-depth: 0 19 | - uses: antrea-io/has-changes@v2 20 | id: check_diff 21 | with: 22 | paths-ignore: docs/* *.md bazel/* 23 | outputs: 24 | has_changes: ${{ steps.check_diff.outputs.has_changes }} 25 | 26 | test: 27 | needs: check-changes 28 | if: ${{ needs.check-changes.outputs.has_changes == 'yes' }} 29 | runs-on: [ubuntu-latest] 30 | strategy: 31 | matrix: 32 | include: 33 | - cxx: g++ 34 | cc: gcc 35 | - cxx: clang++-8 36 | cc: clang-8 37 | - cxx: clang++-10 38 | cc: clang-10 39 | steps: 40 | - uses: actions/checkout@v4 41 | with: 42 | submodules: 'recursive' 43 | - name: Build test Docker image 44 | env: 45 | CC: ${{ matrix.cc }} 46 | CXX: ${{ matrix.cxx }} 47 | run: | 48 | docker build -t pi --build-arg IMAGE_TYPE=test --build-arg CC=$CC --build-arg CXX=$CXX -f Dockerfile.bmv2 . 49 | - name: Run unit tests 50 | run: | 51 | docker run -w /PI pi bash -c \ 52 | 'apt-get update && apt-get install -y python3-pip && \ 53 | python3 -m pip install ptf scapy grpcio googleapis-common-protos protobuf==3.20 && \ 54 | python3 ./proto/ptf/base_test.py' 55 | - name: Run tests 56 | run: | 57 | docker run -w /PI pi bash -c \ 58 | 'make check -j2 || (find . -name "test-suite.log" | xargs cat)' 59 | - name: Check style 60 | run: | 61 | docker run -w /PI pi ./tools/check_style.sh 62 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | 34 | # Emacs files 35 | *~ 36 | 37 | # autotools files 38 | Makefile 39 | Makefile.in 40 | .deps/ 41 | .libs/ 42 | config.* 43 | configure 44 | aclocal.m4 45 | autom4te.cache/ 46 | compile 47 | depcomp 48 | install-sh 49 | libtool 50 | ltmain.sh 51 | missing 52 | stamp-h1 53 | test-driver 54 | tests/test-suite.log 55 | m4/ 56 | .dirstamp 57 | py-compile 58 | 59 | *.tar.gz 60 | 61 | # Python 62 | *.pyc 63 | 64 | doxygen-out 65 | 66 | core 67 | 68 | # Bazel build directories 69 | bazel-* 70 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third_party/googletest"] 2 | path = third_party/googletest 3 | url = https://github.com/google/googletest 4 | ignore = untracked 5 | [submodule "proto/openconfig/gnmi"] 6 | path = proto/openconfig/gnmi 7 | url = https://github.com/openconfig/gnmi.git 8 | [submodule "proto/openconfig/public"] 9 | path = proto/openconfig/public 10 | url = https://github.com/openconfig/public.git 11 | [submodule "proto/p4runtime"] 12 | path = proto/p4runtime 13 | url = https://github.com/p4lang/p4runtime.git 14 | [submodule "third_party/uthash"] 15 | path = third_party/uthash 16 | url = https://github.com/troydhanson/uthash.git 17 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//visibility:public"], 3 | ) 4 | 5 | filegroup( 6 | name = "pihdrs", 7 | srcs = glob(["include/**/*.h"]), 8 | visibility = ["//:__subpackages__"], 9 | ) 10 | 11 | cc_library( 12 | name = "piutils", 13 | srcs = glob(["src/utils/*.c"]) + ["//:pihdrs"], 14 | hdrs = glob(["src/utils/*.h"]), 15 | includes = ["src/utils", "include"], 16 | deps = [], 17 | visibility = ["//:__subpackages__"], 18 | ) 19 | 20 | 21 | cc_library( 22 | name = "pip4info", 23 | srcs = ["src/p4info_int.h", ":pihdrs"] 24 | + glob(["src/p4info/*.c", "src/p4info/*.h"]) 25 | + glob(["src/config_readers/*.c", "src/config_readers/*.h"]), 26 | hdrs = glob(["include/PI/p4info/*.h"]), 27 | includes = ["include", "src"], 28 | copts = ["-DPI_LOG_ON"], 29 | deps = ["//third_party/cJSON:picjson", 30 | "//third_party:piuthash", 31 | "//lib:pitoolkit", 32 | ":piutils"], 33 | ) 34 | 35 | # using glob looks a bit nasty because of the files we have to exclude, but in 36 | # the absence of a CI for the Bazel build, it is less error-prone that listing 37 | # all files manually. 38 | cc_library( 39 | name = "pi", 40 | srcs = [":pihdrs"] 41 | + glob(["src/*.c"], exclude=[ 42 | "src/pi_notifications_pub.c", "src/pi_rpc_server.c"]) 43 | + glob(["src/*.h"], exclude=[ 44 | "src/p4info_int.h", "src/pi_notifications_pub.h"]), 45 | hdrs = glob(["include/PI/*.h", "include/PI/target/*.h"]) 46 | + ["include/PI/int/pi_int.h", "include/PI/int/serialize.h"], 47 | includes = ["include"], 48 | deps = [":pip4info"], 49 | ) 50 | 51 | cc_library( 52 | name = "pifegeneric", 53 | srcs = ["src/frontends/generic/pi.c"], 54 | hdrs = ["include/PI/frontends/generic/pi.h"], 55 | includes = ["include"], 56 | copts = ["-DPI_LOG_ON"], 57 | deps = [":pi"], 58 | ) 59 | -------------------------------------------------------------------------------- /CLI/.gitignore: -------------------------------------------------------------------------------- 1 | pi_CLI_* 2 | -------------------------------------------------------------------------------- /CLI/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | lib_LTLIBRARIES = libpicli.la 4 | 5 | AM_CPPFLAGS = \ 6 | -I$(top_srcdir)/include \ 7 | -I$(top_srcdir)/lib \ 8 | -isystem$(top_srcdir)/third_party/uthash/src 9 | 10 | libpicli_la_SOURCES = \ 11 | commands.h \ 12 | add_p4.c \ 13 | counter_commands.c \ 14 | device_commands.c \ 15 | meter_commands.c \ 16 | table_add.c \ 17 | table_delete.c \ 18 | table_dump.c \ 19 | table_modify.c \ 20 | table_set_default.c \ 21 | table_reset_default.c \ 22 | error_codes.h \ 23 | error_codes.c \ 24 | utils.h \ 25 | utils.c \ 26 | table_common.h \ 27 | table_common.c \ 28 | p4_config_repo.h \ 29 | p4_config_repo.c \ 30 | direct_res_reset.c \ 31 | main.c 32 | 33 | libpicli_la_SOURCES += \ 34 | act_profs/act_prof_create_member.c \ 35 | act_profs/act_prof_dump.c \ 36 | act_profs/act_prof_groups.c \ 37 | act_profs/act_prof_common.h \ 38 | act_profs/act_prof_common.c 39 | 40 | bin_PROGRAMS = pi_CLI_dummy 41 | 42 | common_libs = \ 43 | libpicli.la \ 44 | $(top_builddir)/src/libpi.la \ 45 | $(top_builddir)/src/libpifegeneric.la \ 46 | $(top_builddir)/src/libpip4info.la \ 47 | $(top_builddir)/lib/libpitoolkit.la 48 | 49 | pi_CLI_dummy_LDADD = $(common_libs) \ 50 | $(top_builddir)/targets/dummy/libpi_dummy.la 51 | pi_CLI_dummy_SOURCES = 52 | 53 | if WITH_BMV2 54 | bin_PROGRAMS += pi_CLI_bmv2 55 | # TODO(antonin) the bm libs should probably have better names... 56 | pi_CLI_bmv2_LDADD = $(common_libs) \ 57 | $(top_builddir)/targets/bmv2/libpi_bmv2.la \ 58 | -lthrift -lruntimestubs -lsimpleswitch_thrift 59 | pi_CLI_bmv2_SOURCES = 60 | # Dummy C++ source to cause C++ linking. 61 | nodist_EXTRA_pi_CLI_bmv2_SOURCES = dummy.cxx 62 | endif 63 | 64 | if WITH_INTERNAL_RPC 65 | bin_PROGRAMS += pi_CLI_rpc 66 | pi_CLI_rpc_LDADD = $(common_libs) \ 67 | $(top_builddir)/targets/rpc/libpi_rpc.la 68 | pi_CLI_rpc_SOURCES = 69 | endif 70 | -------------------------------------------------------------------------------- /CLI/act_profs/act_prof_common.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_CLI_TABLE_INDIRECT_TABLE_INDIRECT_COMMON_H_ 23 | #define PI_CLI_TABLE_INDIRECT_TABLE_INDIRECT_COMMON_H_ 24 | 25 | #include "error_codes.h" 26 | 27 | #include "PI/pi.h" 28 | 29 | extern const pi_p4info_t *p4info_curr; 30 | extern pi_dev_tgt_t dev_tgt; 31 | extern pi_session_handle_t sess; 32 | 33 | char *complete_act_prof(const char *text, int state); 34 | char *complete_act_prof_and_action(const char *text, int state); 35 | 36 | #endif // PI_CLI_TABLE_INDIRECT_TABLE_INDIRECT_COMMON_H_ 37 | -------------------------------------------------------------------------------- /CLI/direct_res_reset.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include "error_codes.h" 23 | #include "table_common.h" 24 | 25 | char direct_res_reset_hs[] = 26 | "Remove direct resource configs which are kept ready for next table_add: " 27 | "direct_res_reset"; 28 | 29 | pi_cli_status_t do_direct_res_reset(char *subcmd) { 30 | // better way of doing this? 31 | if (subcmd && *subcmd != '\0') return PI_CLI_STATUS_TOO_MANY_ARGS; 32 | 33 | reset_direct_resource_configs(); 34 | 35 | return PI_CLI_STATUS_SUCCESS; 36 | } 37 | -------------------------------------------------------------------------------- /CLI/error_codes.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_CLI_ERROR_CODES_H_ 23 | #define PI_CLI_ERROR_CODES_H_ 24 | 25 | typedef enum { 26 | PI_CLI_STATUS_SUCCESS = 0, 27 | PI_CLI_STATUS_TARGET_ERROR, 28 | PI_CLI_STATUS_TOO_FEW_ARGS, 29 | PI_CLI_STATUS_TOO_MANY_ARGS, 30 | PI_CLI_STATUS_INVALID_TABLE_NAME, 31 | PI_CLI_STATUS_INVALID_ACTION_NAME, 32 | PI_CLI_STATUS_TOO_FEW_MATCH_FIELDS, 33 | PI_CLI_STATUS_INVALID_VALID_MATCH_FIELD, 34 | PI_CLI_STATUS_INVALID_EXACT_MATCH_FIELD, 35 | PI_CLI_STATUS_INVALID_LPM_MATCH_FIELD, 36 | PI_CLI_STATUS_INVALID_TERNARY_MATCH_FIELD, 37 | PI_CLI_STATUS_INVALID_RANGE_MATCH_FIELD, 38 | PI_CLI_STATUS_INVALID_OPTIONAL_MATCH_FIELD, 39 | PI_CLI_STATUS_INVALID_COMMAND_FORMAT, 40 | PI_CLI_STATUS_TOO_FEW_ACTION_PARAMS, 41 | PI_CLI_STATUS_INVALID_ENTRY_HANDLE, 42 | PI_CLI_STATUS_INVALID_DEVICE_ID, 43 | PI_CLI_STATUS_INVALID_INDIRECT_HANDLE, 44 | PI_CLI_STATUS_INVALID_P4_CONFIG_TYPE, 45 | PI_CLI_STATUS_INVALID_P4_CONFIG, 46 | PI_CLI_STATUS_INVALID_P4_CONFIG_ID, 47 | PI_CLI_STATUS_INVALID_COUNTER_NAME, 48 | PI_CLI_STATUS_INVALID_METER_NAME, 49 | PI_CLI_STATUS_INVALID_FILE_NAME, 50 | 51 | PI_CLI_STATUS_ERROR, 52 | } pi_cli_status_t; 53 | 54 | const char *error_code_to_string(pi_cli_status_t error); 55 | 56 | #endif // PI_CLI_ERROR_CODES_H_ 57 | -------------------------------------------------------------------------------- /CLI/p4_config_repo.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * Copyright 2021 VMware, Inc. 3 | * SPDX-License-Identifier: Apache-2.0 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /* 19 | * Antonin Bas 20 | * 21 | */ 22 | 23 | #include "p4_config_repo.h" 24 | 25 | #include 26 | 27 | typedef struct { 28 | p4_config_id_t id; 29 | pi_p4info_t *p4info; 30 | UT_hash_handle hh; 31 | } p4_config_hash_t; 32 | 33 | static p4_config_hash_t *repo = NULL; 34 | static p4_config_id_t id_ctr = 0; 35 | 36 | p4_config_id_t p4_config_add(pi_p4info_t *p4info) { 37 | p4_config_hash_t *config_hash; 38 | config_hash = malloc(sizeof(*config_hash)); 39 | config_hash->id = id_ctr++; 40 | config_hash->p4info = p4info; 41 | HASH_ADD(hh, repo, id, sizeof(config_hash->id), config_hash); 42 | return config_hash->id; 43 | } 44 | 45 | pi_p4info_t *p4_config_get(p4_config_id_t id) { 46 | p4_config_hash_t *config_hash; 47 | HASH_FIND(hh, repo, &id, sizeof(id), config_hash); 48 | return (config_hash) ? config_hash->p4info : NULL; 49 | } 50 | 51 | pi_p4info_t *p4_config_get_first() { 52 | // hash map is sorted by insertion order 53 | return (repo) ? repo->p4info : NULL; 54 | } 55 | 56 | void p4_config_cleanup() { 57 | p4_config_hash_t *config_hash, *tmp; 58 | // deletion-safe iteration 59 | HASH_ITER(hh, repo, config_hash, tmp) { 60 | pi_destroy_config(config_hash->p4info); 61 | HASH_DEL(repo, config_hash); 62 | free(config_hash); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /CLI/p4_config_repo.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | 24 | typedef uint32_t p4_config_id_t; 25 | 26 | p4_config_id_t p4_config_add(pi_p4info_t *p4info); 27 | 28 | pi_p4info_t *p4_config_get(p4_config_id_t id); 29 | pi_p4info_t *p4_config_get_first(); 30 | 31 | void p4_config_cleanup(); 32 | -------------------------------------------------------------------------------- /CLI/utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_CLI_UTILS_H_ 23 | #define PI_CLI_UTILS_H_ 24 | 25 | #include 26 | 27 | #include 28 | 29 | int count_tokens(const char *str); 30 | 31 | // client needs to free memory when done using it 32 | char *get_token_from_buffer(char *buffer, size_t index); 33 | 34 | char *complete_p4_table(const char *text, int len, int state); 35 | char *complete_p4_action(const char *text, int len, int state, 36 | const char *table); 37 | 38 | size_t parse_fixed_args(char *s, const char **dest, size_t expected); 39 | 40 | void parse_kv_pair(char *s, char **k, char **v); 41 | 42 | int param_to_bytes(const char *param, char *bytes, size_t bitwidth); 43 | 44 | // meant to be used when the completion only involves one resource name; 45 | // multiple resource types can be provided (e.g. direct & indirect) and the 46 | // completion function will iterate over all resources for each provided type. 47 | char *complete_one_name(const char *text, int state, size_t num_res_types, 48 | const pi_res_type_id_t *res_types); 49 | 50 | void print_hexstr(const char *bytes, size_t nbytes); 51 | 52 | #endif // PI_CLI_UTILS_H_ 53 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # DOXYGEN SUPPORT 2 | @DX_RULES@ 3 | 4 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 5 | 6 | if WITH_CLI 7 | MAYBE_CLI = CLI 8 | endif 9 | 10 | if WITH_PROTO 11 | MAYBE_PROTO = proto 12 | endif 13 | 14 | # examples need to come after generators! 15 | # proto depends on frontends_extra/cpp! 16 | SUBDIRS = third_party lib include src generators targets $(MAYBE_CLI) examples \ 17 | bin tests frontends_extra $(MAYBE_PROTO) 18 | 19 | DIST_SUBDIRS = third_party lib include src generators targets CLI examples \ 20 | bin tests frontends_extra $(MAYBE_PROTO) 21 | 22 | AM_DISTCHECK_CONFIGURE_FLAGS = --with-bmv2 --with-fe-cpp 23 | 24 | # ensure the distribution of the doxygen configuration file 25 | EXTRA_DIST = $(top_srcdir)/docs/doxygen.cfg \ 26 | $(top_srcdir)/docs/bfn_latex.sty \ 27 | $(top_srcdir)/docs/*.md 28 | 29 | mostlyclean-local: 30 | -$(RM) -r $(DX_CLEANFILES) 31 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "com_github_p4lang_PI") 2 | 3 | load("//bazel:deps.bzl", "PI_deps") 4 | PI_deps() 5 | 6 | load("@com_github_p4lang_p4runtime//:p4runtime_deps.bzl", "p4runtime_deps") 7 | p4runtime_deps() 8 | 9 | # -- Transitive dependencies of P4Runtime dependencies ------------------------- 10 | 11 | load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") 12 | protobuf_deps() 13 | 14 | load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") 15 | rules_proto_dependencies() 16 | rules_proto_toolchains() 17 | 18 | load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") 19 | go_rules_dependencies() 20 | 21 | load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language") 22 | switched_rules_by_language( 23 | name = "com_google_googleapis_imports", 24 | grpc = True, 25 | cc = True, 26 | python = True, 27 | ) 28 | 29 | load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps") 30 | grpc_deps() 31 | load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") 32 | grpc_extra_deps() 33 | load("@com_github_grpc_grpc//bazel:grpc_python_deps.bzl", "grpc_python_deps") 34 | grpc_python_deps() 35 | 36 | load("@rules_python//python:pip.bzl", "pip_parse") 37 | pip_parse( 38 | name = "grpc_python_dependencies", 39 | requirements_lock = "@com_github_grpc_grpc//:requirements.bazel.txt", 40 | ) 41 | 42 | load("@grpc_python_dependencies//:requirements.bzl", "install_deps") 43 | install_deps() 44 | 45 | # -- Boost dependencies ------------------------- 46 | 47 | load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps") 48 | boost_deps() 49 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Copyright 2016 Barefoot Networks, Inc. 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | # Antonin Bas (antonin@barefootnetworks.com) 19 | 20 | exec autoreconf -fi 21 | -------------------------------------------------------------------------------- /bazel/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | -------------------------------------------------------------------------------- /bazel/external/gnmi.BUILD: -------------------------------------------------------------------------------- 1 | load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") 2 | 3 | package( 4 | default_visibility = [ "//visibility:public" ], 5 | ) 6 | 7 | proto_library( 8 | name = "gnmi_ext_proto", 9 | srcs = ["gnmi_ext/gnmi_ext.proto"], 10 | ) 11 | 12 | proto_library( 13 | name = "gnmi_proto", 14 | srcs = ["gnmi/gnmi.proto"], 15 | deps = [ 16 | ":gnmi_ext_proto", 17 | "@com_google_protobuf//:descriptor_proto", 18 | "@com_google_protobuf//:any_proto", 19 | ], 20 | ) 21 | 22 | cc_proto_library( 23 | name = "gnmi_ext_cc_proto", 24 | deps = [":gnmi_ext_proto"] 25 | ) 26 | 27 | cc_proto_library( 28 | name = "gnmi_cc_proto", 29 | deps = ["@com_github_openconfig_gnmi//:gnmi_proto"], 30 | ) 31 | 32 | cc_grpc_library( 33 | name = "gnmi_cc_grpc", 34 | srcs = [":gnmi_proto"], 35 | deps = [":gnmi_cc_proto"], 36 | grpc_only = True, 37 | ) -------------------------------------------------------------------------------- /bin/.gitignore: -------------------------------------------------------------------------------- 1 | pi_rpc_server 2 | pi_gen_native_json 3 | -------------------------------------------------------------------------------- /bin/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | AM_CPPFLAGS += \ 4 | -I$(top_srcdir)/include 5 | 6 | bin_PROGRAMS = pi_gen_native_json 7 | 8 | pi_gen_native_json_SOURCES = gen_pi_json.c 9 | 10 | pi_gen_native_json_LDADD = \ 11 | $(top_builddir)/src/libpip4info.la 12 | 13 | # This is the rpc server for bmv2 14 | if WITH_BMV2 15 | if WITH_INTERNAL_RPC 16 | bin_PROGRAMS += pi_rpc_server 17 | 18 | pi_rpc_server_SOURCES = rpc_server.c 19 | 20 | pi_rpc_server_LDADD = \ 21 | $(top_builddir)/src/libpi.la \ 22 | $(top_builddir)/src/libpip4info.la \ 23 | $(top_builddir)/targets/bmv2/libpi_bmv2.la \ 24 | -lthrift -lruntimestubs -lsimpleswitch_thrift 25 | 26 | # Dummy C++ source to cause C++ linking. 27 | nodist_EXTRA_pi_rpc_server_SOURCES = dummy.cxx 28 | endif 29 | endif 30 | -------------------------------------------------------------------------------- /bin/gen_pi_json.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | // Generates the PI JSON from the Bmv2 JSON 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | #include 29 | 30 | // TODO(antonin): this is just temporary, to ensure no logs go to stdout 31 | extern void pi_logs_off(); 32 | 33 | int main(int argc, char *argv[]) { 34 | if (argc != 2) { 35 | fprintf(stderr, "P4 configuration needed.\n"); 36 | fprintf(stderr, "Usage: %s \n", argv[0]); 37 | return 1; 38 | } 39 | 40 | pi_logs_off(); 41 | 42 | pi_status_t status; 43 | pi_p4info_t *p4info; 44 | status = pi_add_config_from_file(argv[1], PI_CONFIG_TYPE_BMV2_JSON, &p4info); 45 | if (status != PI_STATUS_SUCCESS) { 46 | fprintf(stderr, "Error while loading config.\n"); 47 | return 1; 48 | } 49 | 50 | char *native_json = pi_serialize_config(p4info, 1); 51 | 52 | printf("%s\n", native_json); 53 | 54 | pi_destroy_config(p4info); 55 | pi_free_serialized_config(native_json); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /docs/00_revision_history.md: -------------------------------------------------------------------------------- 1 | # Revision History 2 | 3 | | Revision | Date | Originator | Description | 4 | | :------: | :---------- | :---------- | :-------------- | 5 | | 0.7 | 15 Oct 2016 | Antonin Bas | Initial API | 6 | | 0.8 | 31 Jan 2017 | Antonin Bas | Software architecture description | 7 | -------------------------------------------------------------------------------- /docs/01_overview.md: -------------------------------------------------------------------------------- 1 | # Overview 2 | 3 | Protocol Independent API (PI or P4 Runtime) defines a set of APIs that 4 | allow interacting with entities defined in a P4 program. These 5 | include: tables, counters, meters ... 6 | 7 | PI APIs are defined at the level of properties that can be 8 | effected. Examples include adding and deleting table entries. They are 9 | independent from the actual instance of the controlled object (and 10 | thus the name) which is passed as a parameter to the API. 11 | -------------------------------------------------------------------------------- /docs/02_architecture.md: -------------------------------------------------------------------------------- 1 | # PI Architecture 2 | 3 | PI is designed to make the interface to data plane objects that can be 4 | expressed in P4 uniform and independent of the instances of the 5 | objects. 6 | 7 | ## P4 objects and APIs 8 | To accomplish this, the API defines the following APIs: 9 | 10 | ### Tables 11 | 12 | ### Counters 13 | 14 | ### Meters 15 | 16 | ### Other externs 17 | 18 | 19 | ## Linking objects to APIs 20 | 21 | How do we go from a P4 name to an the PI API 22 | 23 | ## Protobuf 24 | 25 | ## PI and PD 26 | 27 | ## Database and JSON 28 | 29 | ## Interoperation with target specific APIs 30 | -------------------------------------------------------------------------------- /docs/bfn_latex.sty: -------------------------------------------------------------------------------- 1 | 2 | \newcommand{\bfnlogo}[1]{% 3 | \begin{figure}[H]% 4 | \centering% 5 | \includegraphics[width=\textwidth]{#1}% 6 | \end{figure}% 7 | } 8 | 9 | \newcommand{\bfndisclaimer}[1]{% 10 | \vfill% 11 | #1% 12 | } 13 | 14 | \newcommand{\bfncopyright}[1]{% 15 | \begin{center}% 16 | \textcopyright \space 2013-2017 Barefoot Networks, Inc. All rights reserved.% 17 | \end{center}% 18 | } 19 | 20 | %Define headers/footers for rest of document. 21 | \usepackage{fancyhdr} 22 | \pagestyle{fancyplain} 23 | \fancyhead[L]{\sl\sffamily\footnotesize\textbf{\today}} 24 | \fancyhead[R]{\sl\sffamily\footnotesize\textbf{\chaptermark}} 25 | \fancyfoot[L]{\sffamily\footnotesize\textbf{Barefoot Networks}} 26 | %\fancyfoot[C]{\sffamily\footnotesize\thepage} 27 | \fancyfoot[R]{\sffamily\footnotesize\thepage} 28 | \renewcommand\footrulewidth{.4pt} 29 | -------------------------------------------------------------------------------- /examples/.clang_format_extra.cfg: -------------------------------------------------------------------------------- 1 | # exclude auto-generated file 2 | exclude_files=pi_fe_defines_router\.h 3 | -------------------------------------------------------------------------------- /examples/.gitignore: -------------------------------------------------------------------------------- 1 | example_1 2 | example_2 3 | pi_fe_defines_router.h 4 | -------------------------------------------------------------------------------- /examples/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | AM_CPPFLAGS = \ 4 | -I$(top_srcdir)/include \ 5 | -I$(top_srcdir)/lib \ 6 | -DTESTDATADIR=\"$(abs_srcdir)/testdata\" 7 | 8 | # TODO(antonin): find a way to remove libpip4info as a dep here (should come 9 | # with libpi) 10 | LDADD = \ 11 | $(top_builddir)/src/libpi.la \ 12 | $(top_builddir)/src/libpifegeneric.la \ 13 | $(top_builddir)/src/libpip4info.la \ 14 | $(top_builddir)/targets/dummy/libpi_dummy.la 15 | 16 | check_PROGRAMS = example_1 example_2 17 | 18 | example_1_SOURCES = example_1.c 19 | 20 | # for example_2 21 | p4_name = router 22 | 23 | fe_defines = pi_fe_defines_$(p4_name).h 24 | 25 | gen_fe_defines = $(top_builddir)/generators/pi_gen_fe_defines 26 | 27 | $(fe_defines) : $(srcdir)/testdata/simple_router.json $(gen_fe_defines) 28 | $(gen_fe_defines) -c $< -d $(abs_builddir) -n $(p4_name) 29 | 30 | example_2_SOURCES = example_2.c 31 | nodist_example_2_SOURCES = $(fe_defines) 32 | # This is a bit risky. See "Recording Dependencies manually" at 33 | # https://www.gnu.org/software/automake/manual/html_node/Built-Sources-Example.html#Built-Sources-Example 34 | example_2.$(OBJEXT) : $(fe_defines) 35 | 36 | EXTRA_DIST = \ 37 | testdata/simple_router.json 38 | 39 | CLEANFILES = $(fe_defines) 40 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | This demonstrates how to use the PI library. 2 | 3 | In example_1, there is no auto-generated code at all. The example refers to P4 4 | objects by names (C strings) and obtains ids using these names. 5 | 6 | In example_2, a lightweight auto-generated header, which only contains 7 | preprocessor macros is included for clearer code. 8 | -------------------------------------------------------------------------------- /examples/testdata/simple_router.json: -------------------------------------------------------------------------------- 1 | ../../tests/testdata/simple_router.json -------------------------------------------------------------------------------- /frontends_extra/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | if WITH_FE_CPP 4 | MAYBE_CPP = cpp 5 | endif 6 | 7 | SUBDIRS = $(MAYBE_CPP) 8 | 9 | DIST_SUBDIRS = $(SUBDIRS) 10 | -------------------------------------------------------------------------------- /frontends_extra/cpp/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//visibility:public"], 3 | ) 4 | 5 | cc_library( 6 | name = "pifecpp", 7 | srcs = ["src/tables.cpp"], 8 | hdrs = ["PI/frontends/cpp/tables.h"], 9 | includes = ["."], 10 | deps = ["//:pi"], 11 | ) 12 | -------------------------------------------------------------------------------- /frontends_extra/cpp/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | set noparent 2 | filter=-build/include_subdir,-build/c++11 3 | exclude_files=config\.h 4 | -------------------------------------------------------------------------------- /frontends_extra/cpp/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | SUBDIRS = . example 4 | 5 | AM_CPPFLAGS = \ 6 | -I$(top_srcdir)/../../include 7 | 8 | libpifecpp_la_SOURCES = \ 9 | src/tables.cpp 10 | 11 | nobase_include_HEADERS = \ 12 | PI/frontends/cpp/tables.h 13 | 14 | lib_LTLIBRARIES = libpifecpp.la 15 | -------------------------------------------------------------------------------- /frontends_extra/cpp/configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ([2.68]) 5 | AC_INIT([PI-fe-cpp], [0.1], [antonin@barefootnetworks.com]) 6 | AC_CONFIG_AUX_DIR([.]) 7 | AM_INIT_AUTOMAKE([foreign subdir-objects]) 8 | AC_CONFIG_HEADERS([config.h]) 9 | 10 | AC_PROG_CC 11 | AC_PROG_CXX 12 | LT_INIT 13 | 14 | AC_CONFIG_MACRO_DIR([m4]) 15 | 16 | AC_LANG_PUSH(C) 17 | AC_LANG_PUSH(C++) 18 | 19 | AX_CXX_COMPILE_STDCXX_11([noext],[mandatory]) 20 | 21 | want_bmv2=no 22 | AC_ARG_WITH([bmv2], 23 | AS_HELP_STRING([--with-bmv2], [Build for bmv2 target]), 24 | [want_bmv2="$withval"], []) 25 | 26 | AM_CONDITIONAL([WITH_BMV2], [test "$want_bmv2" = yes]) 27 | 28 | AC_ARG_ENABLE([Werror], 29 | AS_HELP_STRING([--enable-Werror], [Make all compiler warnings fatal]), 30 | [enable_Werror="$enableval"], [enable_Werror=no]) 31 | 32 | AC_TYPE_UINT8_T 33 | AC_TYPE_UINT16_T 34 | AC_TYPE_UINT32_T 35 | AC_TYPE_UINT64_T 36 | AC_TYPE_SIZE_T 37 | 38 | # check for pthreads 39 | AX_PTHREAD([], [AC_MSG_ERROR([Missing pthread library])]) 40 | 41 | EXTRA_CXXFLAGS="-Wall -Wextra" 42 | AS_IF([test "$enable_Werror" = "yes"], [EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS -Werror"]) 43 | AC_SUBST([AM_CXXFLAGS], ["$PTHREAD_CFLAGS $EXTRA_CXXFLAGS"]) 44 | 45 | # Generate makefiles 46 | AC_CONFIG_FILES([Makefile 47 | example/Makefile]) 48 | 49 | AC_OUTPUT 50 | -------------------------------------------------------------------------------- /frontends_extra/cpp/example/.gitignore: -------------------------------------------------------------------------------- 1 | example 2 | pi_fe_defines_* 3 | -------------------------------------------------------------------------------- /frontends_extra/cpp/example/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | exclude_files=pi_fe_defines_p4\.h 2 | -------------------------------------------------------------------------------- /frontends_extra/cpp/example/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | check_PROGRAMS = example 4 | testdata_dirpath = $(top_srcdir)/../../examples/testdata 5 | p4_name = p4 6 | AM_CPPFLAGS = \ 7 | -I$(top_srcdir) \ 8 | -I$(top_srcdir)/../../include \ 9 | -I$(top_srcdir)/../../lib \ 10 | -DTESTDATADIR=\"$(testdata_dirpath)\" 11 | fe_defines = pi_fe_defines_$(p4_name).h 12 | gen_fe_defines = $(top_builddir)/../../generators/pi_gen_fe_defines 13 | $(fe_defines) : $(testdata_dirpath)/simple_router.json $(gen_fe_defines) 14 | $(gen_fe_defines) -c $< -d $(abs_builddir) -n $(p4_name) 15 | example_SOURCES = example.cpp 16 | nodist_example_SOURCES = $(fe_defines) 17 | # This is a bit risky. See "Recording Dependencies manually" at 18 | # https://www.gnu.org/software/automake/manual/html_node/Built-Sources-Example.html#Built-Sources-Example 19 | example.$(OBJEXT) : $(fe_defines) 20 | 21 | if WITH_BMV2 22 | LDADD = \ 23 | $(top_builddir)/../../src/libpi.la \ 24 | $(top_builddir)/libpifecpp.la \ 25 | $(top_builddir)/../../targets/bmv2/libpi_bmv2.la \ 26 | $(top_builddir)/../../src/libpip4info.la \ 27 | -lthrift -lruntimestubs -lsimpleswitch_thrift 28 | else 29 | LDADD = \ 30 | $(top_builddir)/../../src/libpi.la \ 31 | $(top_builddir)/libpifecpp.la \ 32 | $(top_builddir)/../../src/libpip4info.la \ 33 | $(top_builddir)/../../targets/dummy/libpi_dummy.la 34 | endif 35 | 36 | CLEANFILES = $(fe_defines) 37 | -------------------------------------------------------------------------------- /generators/.gitignore: -------------------------------------------------------------------------------- 1 | pi_gen_fe_defines 2 | -------------------------------------------------------------------------------- /generators/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | AM_CPPFLAGS = \ 4 | -I$(top_srcdir)/include \ 5 | -I$(top_srcdir)/lib 6 | 7 | bin_PROGRAMS = pi_gen_fe_defines 8 | 9 | pi_gen_fe_defines_SOURCES = fe_defines.c 10 | 11 | pi_gen_fe_defines_LDADD = \ 12 | $(top_builddir)/src/libpip4info.la \ 13 | $(top_builddir)/src/libpiutils.la \ 14 | $(top_builddir)/lib/libpitoolkit.la 15 | -------------------------------------------------------------------------------- /include/Makefile.am: -------------------------------------------------------------------------------- 1 | # TODO(antonin): move act_prof to some "extern" directory 2 | 3 | nobase_include_HEADERS = \ 4 | PI/pi.h \ 5 | PI/pi_base.h \ 6 | PI/p4info.h \ 7 | PI/pi_tables.h \ 8 | PI/pi_value.h \ 9 | PI/pi_act_prof.h \ 10 | PI/pi_counter.h \ 11 | PI/pi_meter.h \ 12 | PI/pi_learn.h \ 13 | PI/pi_mc.h \ 14 | PI/pi_clone.h 15 | 16 | nobase_include_HEADERS += \ 17 | PI/frontends/generic/pi.h 18 | 19 | nobase_include_HEADERS += \ 20 | PI/p4info/actions.h \ 21 | PI/p4info/tables.h \ 22 | PI/p4info/act_profs.h \ 23 | PI/p4info/counters.h \ 24 | PI/p4info/meters.h \ 25 | PI/p4info/digests.h 26 | 27 | nobase_include_HEADERS += \ 28 | PI/int/pi_int.h \ 29 | PI/int/serialize.h \ 30 | PI/int/rpc_common.h 31 | 32 | nobase_include_HEADERS += \ 33 | PI/target/pi_imp.h \ 34 | PI/target/pi_tables_imp.h \ 35 | PI/target/pi_act_prof_imp.h \ 36 | PI/target/pi_counter_imp.h \ 37 | PI/target/pi_meter_imp.h \ 38 | PI/target/pi_learn_imp.h \ 39 | PI/target/pi_mc_imp.h \ 40 | PI/target/pi_clone_imp.h 41 | -------------------------------------------------------------------------------- /include/PI/target/pi_clone_imp.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_INC_PI_TARGET_PI_CLONE_IMP_H_ 23 | #define PI_INC_PI_TARGET_PI_CLONE_IMP_H_ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | pi_status_t _pi_clone_session_set( 34 | pi_session_handle_t session_handle, pi_dev_tgt_t dev_tgt, 35 | pi_clone_session_id_t clone_session_id, 36 | const pi_clone_session_config_t *clone_session_config); 37 | 38 | pi_status_t _pi_clone_session_reset(pi_session_handle_t session_handle, 39 | pi_dev_tgt_t dev_tgt, 40 | pi_clone_session_id_t clone_session_id); 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | 46 | #endif // PI_INC_PI_TARGET_PI_CLONE_IMP_H_ 47 | -------------------------------------------------------------------------------- /include/PI/target/pi_learn_imp.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_INC_PI_TARGET_PI_LEARN_IMP_H_ 23 | #define PI_INC_PI_TARGET_PI_LEARN_IMP_H_ 24 | 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | pi_status_t pi_learn_new_msg(pi_learn_msg_t *msg); 32 | 33 | pi_status_t _pi_learn_config_set(pi_session_handle_t session_handle, 34 | pi_dev_id_t dev_id, pi_p4_id_t learn_id, 35 | const pi_learn_config_t *config); 36 | 37 | pi_status_t _pi_learn_msg_ack(pi_session_handle_t session_handle, 38 | pi_dev_id_t dev_id, pi_p4_id_t learn_id, 39 | pi_learn_msg_id_t msg_id); 40 | 41 | pi_status_t _pi_learn_msg_done(pi_learn_msg_t *msg); 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif // PI_INC_PI_TARGET_PI_LEARN_IMP_H_ 48 | -------------------------------------------------------------------------------- /lib/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//:__subpackages__"], 3 | ) 4 | 5 | cc_library( 6 | name = "pitoolkit", 7 | srcs = glob(["*.c"]), 8 | hdrs = glob(["*.h"]), 9 | includes = ["."], 10 | deps = [], 11 | ) 12 | -------------------------------------------------------------------------------- /lib/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | noinst_LTLIBRARIES = libpitoolkit.la 4 | 5 | libpitoolkit_la_SOURCES = \ 6 | _assert.h \ 7 | _assert.c \ 8 | vector.h \ 9 | vector.c \ 10 | read_file.c \ 11 | read_file.h 12 | -------------------------------------------------------------------------------- /lib/_assert.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include "_assert.h" 23 | 24 | #include 25 | #include 26 | 27 | void _pi_assert(const char* expr, const char* file, int line) { 28 | fprintf(stderr, "Assertion '%s' failed, file '%s' line %d", expr, file, line); 29 | abort(); 30 | } 31 | -------------------------------------------------------------------------------- /lib/_assert.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_TOOLKIT__ASSERT_H_ 23 | #define PI_TOOLKIT__ASSERT_H_ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | // An assert that cannot be removed with NDEBUG 30 | 31 | // TODO(antonin): is this portable? 32 | #define _PI_NORETURN __attribute__((noreturn)) 33 | 34 | _PI_NORETURN void _pi_assert(const char* expr, const char* file, int line); 35 | 36 | #define _PI_ASSERT(expr) \ 37 | ((expr) ? (void)0 : _pi_assert(#expr, __FILE__, __LINE__)) 38 | 39 | #define _PI_UNREACHABLE(msg) _pi_assert(msg, __FILE__, __LINE__) 40 | 41 | #define _PI_UNUSED(x) ((void)x) 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif // PI_TOOLKIT__ASSERT_H_ 48 | -------------------------------------------------------------------------------- /lib/read_file.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | char *read_file(const char *path) { 21 | char *source = NULL; 22 | FILE *fp = fopen(path, "r"); 23 | if (fp != NULL) { 24 | /* Go to the end of the file. */ 25 | if (fseek(fp, 0L, SEEK_END) == 0) { 26 | /* Get the size of the file. */ 27 | long bufsize = ftell(fp); 28 | if (bufsize == -1) { /* Error */ 29 | } 30 | 31 | /* Allocate our buffer to that size. */ 32 | source = malloc(sizeof(char) * (bufsize + 1)); 33 | 34 | /* Go back to the start of the file. */ 35 | if (fseek(fp, 0L, SEEK_SET) != 0) { /* Error */ 36 | } 37 | 38 | /* Read the entire file into memory. */ 39 | size_t newLen = fread(source, sizeof(char), bufsize, fp); 40 | if (newLen == 0) { 41 | fputs("Error reading file", stderr); 42 | } else { 43 | source[newLen++] = '\0'; /* Just to be safe. */ 44 | } 45 | } 46 | fclose(fp); 47 | } 48 | return source; 49 | } 50 | -------------------------------------------------------------------------------- /lib/read_file.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PI_TOOLKIT_READ_FILE_H_ 18 | #define PI_TOOLKIT_READ_FILE_H_ 19 | 20 | char *read_file(const char *path); 21 | 22 | #endif // PI_TOOLKIT_READ_FILE_H_ 23 | -------------------------------------------------------------------------------- /lib/vector.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_TOOLKIT_VECTOR_H_ 23 | #define PI_TOOLKIT_VECTOR_H_ 24 | 25 | #include 26 | 27 | typedef struct vector_s vector_t; 28 | 29 | vector_t *vector_create(size_t e_size, size_t init_capacity); 30 | 31 | typedef void (*VectorCleanFn)(void *e); 32 | 33 | vector_t *vector_create_wclean(size_t e_size, size_t init_capacity, 34 | VectorCleanFn clean_fn); 35 | 36 | void vector_destroy(vector_t *v); 37 | 38 | void vector_push_back(vector_t *v, void *e); 39 | 40 | // add an element at the end, with memory initialized to 0 41 | void vector_push_back_empty(vector_t *v); 42 | 43 | void *vector_at(const vector_t *v, size_t index); 44 | 45 | void *vector_data(const vector_t *v); 46 | 47 | size_t vector_size(const vector_t *v); 48 | 49 | void vector_remove(vector_t *v, size_t index); 50 | 51 | void vector_remove_e(vector_t *v, void *e); 52 | 53 | void *vector_back(vector_t *v); 54 | 55 | /* typedef int (*VectorCmpFn)(const void *e1, const void *e2); */ 56 | /* void *vector_search(vector_t *v, VectorCmpFn cmp_fn, size_t start_index); */ 57 | 58 | #endif // PI_TOOLKIT_VECTOR_H_ 59 | -------------------------------------------------------------------------------- /m4/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/PI/d28b31e4fa05b51f93b9810f5a3ef4a57fbfb8a8/m4/.gitignore -------------------------------------------------------------------------------- /m4/adl_recursive_eval.m4: -------------------------------------------------------------------------------- 1 | dnl adl_RECURSIVE_EVAL(VALUE, RESULT) 2 | dnl ================================= 3 | dnl Interpolate the VALUE in loop until it doesn't change, 4 | dnl and set the result to $RESULT. 5 | dnl WARNING: It's easy to get an infinite loop with some unsane input. 6 | AC_DEFUN([adl_RECURSIVE_EVAL], 7 | [_lcl_receval="$1" 8 | $2=`(test "x$prefix" = xNONE && prefix="$ac_default_prefix" 9 | test "x$exec_prefix" = xNONE && exec_prefix="${prefix}" 10 | _lcl_receval_old='' 11 | while test "[$]_lcl_receval_old" != "[$]_lcl_receval"; do 12 | _lcl_receval_old="[$]_lcl_receval" 13 | eval _lcl_receval="\"[$]_lcl_receval\"" 14 | done 15 | echo "[$]_lcl_receval")`]) 16 | -------------------------------------------------------------------------------- /proto/.gitignore: -------------------------------------------------------------------------------- 1 | cpp_out 2 | grpc_out 3 | py_out 4 | proto_files.ts 5 | sysrepo/install_yangs.sh 6 | -------------------------------------------------------------------------------- /proto/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//visibility:public"], 3 | ) 4 | 5 | load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") 6 | 7 | cc_library( 8 | name = "piprotoutil", 9 | srcs = ["src/util.cpp"], 10 | hdrs = ["PI/proto/util.h"], 11 | includes = ["."], 12 | deps = ["@com_github_p4lang_p4runtime//:p4info_cc_proto"], 13 | ) 14 | 15 | proto_library( 16 | name = "p4config_proto", 17 | srcs = ["p4/tmp/p4config.proto"] 18 | ) 19 | 20 | cc_proto_library( 21 | name = "p4config_cc_proto", 22 | deps = [":p4config_proto"], 23 | ) 24 | 25 | cc_library( 26 | name = "piprotoserverconfig", 27 | srcs = ["server_config/server_config.cpp"], 28 | hdrs = ["server_config/server_config.h"], 29 | includes = ["."], 30 | deps = [":p4serverconfig_cc_proto"], 31 | visibility = ["//proto:__subpackages__"], 32 | ) 33 | 34 | proto_library( 35 | name = "p4serverconfig_proto", 36 | srcs = ["p4/server/v1/config.proto"] 37 | ) 38 | 39 | cc_proto_library( 40 | name = "p4serverconfig_cc_proto", 41 | deps = [":p4serverconfig_proto"], 42 | ) 43 | 44 | cc_grpc_library( 45 | name = "p4serverconfig_cc_grpc", 46 | srcs = [":p4serverconfig_proto"], 47 | deps = [":p4serverconfig_cc_proto"], 48 | grpc_only = True, 49 | ) 50 | -------------------------------------------------------------------------------- /proto/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | set noparent 2 | filter=-build/include_subdir,-build/c++11 3 | -------------------------------------------------------------------------------- /proto/PI/proto/util.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_PROTO_UTIL_H_ 23 | #define PI_PROTO_UTIL_H_ 24 | 25 | #include 26 | 27 | #include "p4/config/v1/p4info.pb.h" 28 | 29 | namespace pi { 30 | 31 | namespace proto { 32 | 33 | namespace util { 34 | 35 | using p4_id_t = uint32_t; 36 | 37 | constexpr p4_id_t invalid_id() { return 0; } 38 | 39 | p4::config::v1::P4Ids::Prefix resource_type_from_id(p4_id_t p4_id); 40 | 41 | } // namespace util 42 | 43 | } // namespace proto 44 | 45 | } // namespace pi 46 | 47 | #endif // PI_PROTO_UTIL_H_ 48 | -------------------------------------------------------------------------------- /proto/demo_grpc/.gitignore: -------------------------------------------------------------------------------- 1 | controller 2 | pi_grpc_server 3 | pi_server_dummy 4 | test_client 5 | test_perf 6 | # function calls counters when using pi_server_dummy 7 | func_counter.txt 8 | -------------------------------------------------------------------------------- /proto/demo_grpc/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | AM_CPPFLAGS = \ 4 | -I$(top_srcdir)/../include \ 5 | -I$(top_srcdir)/frontend \ 6 | -I$(top_srcdir)/server \ 7 | -I$(top_builddir)/cpp_out \ 8 | -I$(top_builddir)/grpc_out \ 9 | -I$(top_srcdir)/p4info 10 | 11 | AM_CXXFLAGS = -Wall -Werror -Wno-unused-command-line-argument 12 | 13 | noinst_PROGRAMS = controller pi_server_dummy test_perf 14 | 15 | pi_server_dummy_SOURCES = pi_server_main.cpp 16 | 17 | test_perf_SOURCES = test_perf.cpp 18 | 19 | controller_SOURCES = \ 20 | simple_router_mgr.cpp \ 21 | simple_router_mgr.h \ 22 | web_server.cpp \ 23 | web_server.h \ 24 | app.cpp 25 | 26 | COMMON_SERVER_LIBS = \ 27 | $(top_builddir)/server/libpigrpcserver.la \ 28 | $(top_builddir)/frontend/libpifeproto.la \ 29 | $(top_builddir)/libpiprotogrpc.la \ 30 | $(top_builddir)/libpiprotobuf.la \ 31 | $(top_builddir)/../src/libpiall.la 32 | 33 | if WITH_BMV2 34 | bin_PROGRAMS = pi_grpc_server 35 | 36 | pi_grpc_server_SOURCES = pi_server_main.cpp 37 | 38 | pi_grpc_server_LDADD = \ 39 | $(COMMON_SERVER_LIBS) \ 40 | $(top_builddir)/../targets/bmv2/libpi_bmv2.la \ 41 | -lthrift -lruntimestubs -lsimpleswitch_thrift 42 | endif # WITH_BMV2 43 | 44 | pi_server_dummy_LDADD = \ 45 | $(COMMON_SERVER_LIBS) \ 46 | $(top_builddir)/../targets/dummy/libpi_dummy.la 47 | 48 | test_perf_LDADD = \ 49 | $(top_builddir)/../src/libpip4info.la \ 50 | $(top_builddir)/libpiprotogrpc.la \ 51 | $(top_builddir)/libpiprotobuf.la \ 52 | $(top_builddir)/p4info/libpiconvertproto.la \ 53 | $(PROTOBUF_LIBS) $(GRPC_LIBS) 54 | 55 | controller_LDADD = \ 56 | $(top_builddir)/../src/libpip4info.la \ 57 | $(top_builddir)/libpiprotogrpc.la \ 58 | $(top_builddir)/libpiprotobuf.la \ 59 | $(top_builddir)/p4info/libpiconvertproto.la \ 60 | -lmicrohttpd $(BOOST_SYSTEM_LIB) \ 61 | $(PROTOBUF_LIBS) $(GRPC_LIBS) 62 | -------------------------------------------------------------------------------- /proto/demo_grpc/resources/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/PI/d28b31e4fa05b51f93b9810f5a3ef4a57fbfb8a8/proto/demo_grpc/resources/architecture.png -------------------------------------------------------------------------------- /proto/demo_grpc/resources/demo_topology.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/PI/d28b31e4fa05b51f93b9810f5a3ef4a57fbfb8a8/proto/demo_grpc/resources/demo_topology.png -------------------------------------------------------------------------------- /proto/demo_grpc/web_server.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | 27 | struct MHD_Daemon; 28 | 29 | class SimpleRouterMgr; 30 | 31 | class WebServer { 32 | public: 33 | WebServer(SimpleRouterMgr *simple_router_mgr, int port = 8888); 34 | ~WebServer(); 35 | 36 | void set_json_name(const std::string &json_name); 37 | std::string get_json_name() const; 38 | 39 | int query_counter(const std::string &counter_name, size_t index, 40 | uint64_t *packets, uint64_t *bytes); 41 | 42 | int update_json_config(const std::string &config_buffer, 43 | const std::string *p4info_buffer); 44 | 45 | int start(); 46 | 47 | private: 48 | SimpleRouterMgr *simple_router_mgr{nullptr}; 49 | int port; 50 | std::string current_json{""}; 51 | struct MHD_Daemon *daemon{NULL}; 52 | mutable std::mutex mutex; 53 | }; 54 | -------------------------------------------------------------------------------- /proto/frontend/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//visibility:public"], 3 | ) 4 | 5 | cc_library( 6 | name = "pifeproto", 7 | srcs = glob(["src/*.cpp", "src/*.h"]), 8 | hdrs = ["PI/frontends/proto/device_mgr.h", 9 | "PI/frontends/proto/logging.h"], 10 | includes = ["."], 11 | copts = ["-DUSE_ABSL=1"], 12 | deps = ["@com_github_p4lang_p4runtime//:p4runtime_cc_grpc", 13 | "@com_github_p4lang_p4runtime//:p4runtime_cc_proto", 14 | "@com_google_googleapis//google/rpc:code_cc_proto", 15 | "@com_google_protobuf//:protobuf", 16 | "@com_github_grpc_grpc//:grpc++", 17 | "//proto:p4serverconfig_cc_proto", 18 | "//proto:piprotoutil", 19 | "//proto:piprotoserverconfig", 20 | "//proto/third_party:fmt", 21 | "//proto:p4config_cc_proto", 22 | "//frontends_extra/cpp:pifecpp", 23 | "//proto/p4info:piconvertproto", 24 | "@com_google_absl//absl/synchronization:synchronization"], 25 | ) 26 | -------------------------------------------------------------------------------- /proto/frontend/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | # for deprecated Protobuf fields 4 | AM_CXXFLAGS += -Wno-error=deprecated-declarations 5 | 6 | # include path for protoc-generated code will change when we agree on an 7 | # installation path 8 | AM_CPPFLAGS = \ 9 | -I$(top_srcdir)/../include \ 10 | -I$(top_builddir)/cpp_out \ 11 | -I$(top_srcdir)/../frontends_extra/cpp \ 12 | -I$(top_srcdir) \ 13 | -I$(top_srcdir)/p4info \ 14 | -I$(top_srcdir)/third_party 15 | 16 | libpifeproto_la_SOURCES = \ 17 | src/bimap.h \ 18 | src/device_mgr.cpp \ 19 | src/access_arbitration.h \ 20 | src/access_arbitration.cpp \ 21 | src/action_prof_mgr.h \ 22 | src/action_prof_mgr.cpp \ 23 | src/table_info_store.h \ 24 | src/table_info_store.cpp \ 25 | src/action_helpers.h \ 26 | src/action_helpers.cpp \ 27 | src/match_key_helpers.h \ 28 | src/match_key_helpers.cpp \ 29 | src/packet_io_mgr.h \ 30 | src/packet_io_mgr.cpp \ 31 | src/common.h \ 32 | src/common.cpp \ 33 | src/logger.h \ 34 | src/logging.cpp \ 35 | src/report_error.h \ 36 | src/pre_mc_mgr.h \ 37 | src/pre_mc_mgr.cpp \ 38 | src/pre_clone_mgr.h \ 39 | src/pre_clone_mgr.cpp \ 40 | src/task_queue.h \ 41 | src/digest_mgr.h \ 42 | src/digest_mgr.cpp \ 43 | src/status_macros.h \ 44 | src/statusor.h \ 45 | src/idle_timeout_buffer.h \ 46 | src/idle_timeout_buffer.cpp \ 47 | src/watch_port_enforcer.h \ 48 | src/watch_port_enforcer.cpp 49 | 50 | libpifeproto_la_LIBADD = \ 51 | $(top_builddir)/../frontends_extra/cpp/libpifecpp.la \ 52 | $(top_builddir)/p4info/libpiconvertproto.la \ 53 | $(top_builddir)/libserverconfig.la \ 54 | $(top_builddir)/libpiprotobuf.la \ 55 | $(top_builddir)/../src/libpip4info.la \ 56 | $(top_builddir)/third_party/libfmt.la \ 57 | $(BOOST_THREAD_LIB) 58 | 59 | nobase_include_HEADERS = \ 60 | PI/frontends/proto/device_mgr.h \ 61 | PI/frontends/proto/logging.h 62 | 63 | lib_LTLIBRARIES = libpifeproto.la 64 | -------------------------------------------------------------------------------- /proto/frontend/PI/frontends/proto/logging.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_FRONTENDS_PROTO_LOGGING_H_ 23 | #define PI_FRONTENDS_PROTO_LOGGING_H_ 24 | 25 | #include 26 | 27 | namespace pi { 28 | 29 | namespace fe { 30 | 31 | namespace proto { 32 | 33 | class LogWriterIface { 34 | public: 35 | enum class Severity { 36 | TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL 37 | }; 38 | 39 | virtual ~LogWriterIface() { } 40 | 41 | virtual void write(Severity severity, const char *msg) { 42 | (void) severity; 43 | (void) msg; 44 | } 45 | }; 46 | 47 | class LoggerConfig { 48 | public: 49 | using Severity = LogWriterIface::Severity; 50 | 51 | // configuration methods are not thread-safe 52 | static void set_writer(std::shared_ptr writer); 53 | static void set_min_severity(Severity min_severity); 54 | 55 | private: 56 | LoggerConfig(); 57 | }; 58 | 59 | } // namespace proto 60 | 61 | } // namespace fe 62 | 63 | } // namespace pi 64 | 65 | #endif // PI_FRONTENDS_PROTO_LOGGING_H_ 66 | -------------------------------------------------------------------------------- /proto/frontend/src/action_helpers.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * Copyright 2021 VMware, Inc. 3 | * SPDX-License-Identifier: Apache-2.0 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /* 19 | * Antonin Bas 20 | * 21 | */ 22 | 23 | #ifndef SRC_ACTION_HELPERS_H_ 24 | #define SRC_ACTION_HELPERS_H_ 25 | 26 | #include 27 | #include 28 | 29 | #include "google/rpc/status.pb.h" 30 | #include "p4/v1/p4runtime.pb.h" 31 | 32 | namespace pi { 33 | 34 | namespace fe { 35 | 36 | namespace proto { 37 | 38 | using Status = ::google::rpc::Status; 39 | 40 | Status construct_action_data(const pi_p4info_t *p4info, 41 | const p4::v1::Action &action, 42 | pi::ActionData *action_data); 43 | 44 | } // namespace proto 45 | 46 | } // namespace fe 47 | 48 | } // namespace pi 49 | 50 | #endif // SRC_ACTION_HELPERS_H_ 51 | -------------------------------------------------------------------------------- /proto/frontend/src/bimap.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef SRC_BIMAP_H_ 23 | #define SRC_BIMAP_H_ 24 | 25 | #include 26 | 27 | namespace pi { 28 | 29 | namespace fe { 30 | 31 | namespace proto { 32 | 33 | template 34 | class BiMap { 35 | public: 36 | void add_mapping_1_2(const T1 &t1, const T2 &t2) { 37 | map_1_2.emplace(t1, t2); 38 | map_2_1.emplace(t2, t1); 39 | } 40 | 41 | // returns nullptr if no matching entry 42 | const T2 *get_from_1(const T1 &t1) const { 43 | auto it = map_1_2.find(t1); 44 | return (it == map_1_2.end()) ? nullptr : &it->second; 45 | } 46 | 47 | const T1 *get_from_2(const T2 &t2) const { 48 | auto it = map_2_1.find(t2); 49 | return (it == map_2_1.end()) ? nullptr : &it->second; 50 | } 51 | 52 | void remove_from_1(const T1 &t1) { 53 | map_1_2.erase(t1); 54 | } 55 | 56 | void remove_from_2(const T2 &t2) { 57 | map_2_1.erase(t2); 58 | } 59 | 60 | bool empty() const { return map_1_2.empty(); } 61 | 62 | private: 63 | std::unordered_map map_1_2{}; 64 | std::unordered_map map_2_1{}; 65 | }; 66 | 67 | } // namespace proto 68 | 69 | } // namespace fe 70 | 71 | } // namespace pi 72 | 73 | #endif // SRC_BIMAP_H_ 74 | -------------------------------------------------------------------------------- /proto/frontend/src/logging.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | 24 | #include "logger.h" 25 | 26 | namespace pi { 27 | 28 | namespace fe { 29 | 30 | namespace proto { 31 | 32 | LoggerConfig::LoggerConfig() = default; 33 | 34 | void 35 | LoggerConfig::set_writer(std::shared_ptr writer) { 36 | Logger::get()->set_writer(writer); 37 | } 38 | 39 | void 40 | LoggerConfig::set_min_severity(Severity min_severity) { 41 | Logger::get()->set_min_severity(min_severity); 42 | } 43 | 44 | } // namespace proto 45 | 46 | } // namespace fe 47 | 48 | } // namespace pi 49 | -------------------------------------------------------------------------------- /proto/gnmi: -------------------------------------------------------------------------------- 1 | openconfig/gnmi/proto/gnmi -------------------------------------------------------------------------------- /proto/p4/config: -------------------------------------------------------------------------------- 1 | ../p4runtime/proto/p4/config -------------------------------------------------------------------------------- /proto/p4/server/v1/config.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2019 VMware, Inc. 2 | // SPDX-License-Identifier: Apache-2.0 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | syntax = "proto3"; 17 | 18 | package p4.server.v1; 19 | 20 | service ServerConfig { 21 | rpc Set(SetRequest) returns (SetResponse); 22 | rpc Get(GetRequest) returns (GetResponse); 23 | } 24 | 25 | message SetRequest { 26 | uint64 device_id = 1; 27 | Config config = 2; 28 | } 29 | 30 | message SetResponse { 31 | } 32 | 33 | message GetRequest { 34 | uint64 device_id = 1; 35 | } 36 | 37 | message GetResponse { 38 | Config config = 1; 39 | } 40 | 41 | message Config { 42 | StreamConfig stream = 1; 43 | } 44 | 45 | message StreamConfig { 46 | enum ErrorReportingLevel { 47 | DISABLED = 0; 48 | ENABLED = 1; 49 | DETAILED = 2; 50 | } 51 | 52 | // Stream error-reporting is disabled by default. We recommend only enabling 53 | // it for debugging purposes. 54 | ErrorReportingLevel error_reporting = 1; 55 | } 56 | -------------------------------------------------------------------------------- /proto/p4/tmp/p4config.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2013-present Barefoot Networks, Inc. 2 | // SPDX-License-Identifier: Apache-2.0 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | syntax = "proto3"; 17 | 18 | // This package is temporary. 19 | 20 | package p4.tmp; 21 | 22 | // p4-device specific config 23 | message P4DeviceConfig { 24 | message Extras { 25 | map kv = 1; 26 | } 27 | bool reassign = 1; 28 | Extras extras = 2; 29 | bytes device_data = 3; 30 | }; 31 | -------------------------------------------------------------------------------- /proto/p4/v1: -------------------------------------------------------------------------------- 1 | ../p4runtime/proto/p4/v1 -------------------------------------------------------------------------------- /proto/p4info/.gitignore: -------------------------------------------------------------------------------- 1 | pi_convert_p4info 2 | -------------------------------------------------------------------------------- /proto/p4info/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//visibility:public"], 3 | ) 4 | 5 | cc_library( 6 | name = "piconvertproto", 7 | srcs = ["p4info_to_and_from_proto.cpp"], 8 | hdrs = ["PI/proto/p4info_to_and_from_proto.h"], 9 | includes = ["."], 10 | deps = ["//:pip4info", 11 | "@com_github_p4lang_p4runtime//:p4info_cc_proto"], 12 | ) 13 | 14 | cc_binary( 15 | name = "pi_convert_p4info", 16 | srcs = ["convert_p4info.cpp"], 17 | deps = [":piconvertproto"], 18 | ) 19 | -------------------------------------------------------------------------------- /proto/p4info/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | AM_CPPFLAGS = \ 4 | -I$(top_srcdir)/../include \ 5 | -I$(top_srcdir)/../src \ 6 | -I$(top_builddir)/cpp_out 7 | 8 | bin_PROGRAMS = pi_convert_p4info 9 | 10 | # convenience library to convert back-and-forth between p4info and the P4Info 11 | # protobuf message 12 | lib_LTLIBRARIES = libpiconvertproto.la 13 | 14 | libpiconvertproto_la_SOURCES = \ 15 | p4info_to_and_from_proto.cpp 16 | 17 | nobase_include_HEADERS = PI/proto/p4info_to_and_from_proto.h 18 | 19 | libpiconvertproto_la_LIBADD = \ 20 | $(top_builddir)/libpiprotobuf.la \ 21 | $(PROTOBUF_LIBS) 22 | 23 | pi_convert_p4info_SOURCES = \ 24 | convert_p4info.cpp 25 | 26 | pi_convert_p4info_LDADD = \ 27 | $(builddir)/libpiconvertproto.la \ 28 | $(top_builddir)/libpiprotobuf.la \ 29 | $(top_builddir)/../src/libpip4info.la \ 30 | $(PROTOBUF_LIBS) 31 | -------------------------------------------------------------------------------- /proto/p4info/PI/proto/p4info_to_and_from_proto.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PROTO_P4INFO_PI_PROTO_P4INFO_TO_AND_FROM_PROTO_H_ 23 | #define PROTO_P4INFO_PI_PROTO_P4INFO_TO_AND_FROM_PROTO_H_ 24 | 25 | #include "p4/config/v1/p4info.pb.h" 26 | 27 | #include 28 | 29 | namespace pi { 30 | 31 | namespace p4info { 32 | 33 | p4::config::v1::P4Info p4info_serialize_to_proto(const pi_p4info_t *p4info); 34 | 35 | // returns true if success, false otherwise 36 | bool p4info_proto_reader(const p4::config::v1::P4Info &p4info_proto, 37 | pi_p4info_t **p4info); 38 | 39 | } // namespace p4info 40 | 41 | } // namespace pi 42 | 43 | #endif // PROTO_P4INFO_PI_PROTO_P4INFO_TO_AND_FROM_PROTO_H_ 44 | -------------------------------------------------------------------------------- /proto/p4info/xform/examples/descrip.txt: -------------------------------------------------------------------------------- 1 | A long description for the P4Info. 2 | line 2 3 | line 3 4 | line 4 5 | 6 | -------------------------------------------------------------------------------- /proto/p4info/xform/examples/gen_p4info.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # 3 | # gen_p4info.sh - Utility to generate P4Info in three different formats from one P4_16 source file. 4 | # 5 | 6 | # Copyright 2018-present Keysight Technologies, Inc. 7 | # SPDX-License-Identifier: Apache-2.0 8 | # 9 | # Licensed under the Apache License, Version 2.0 (the "License"); 10 | # you may not use this file except in compliance with the License. 11 | # You may obtain a copy of the License at 12 | # 13 | # http://www.apache.org/licenses/LICENSE-2.0 14 | # 15 | # Unless required by applicable law or agreed to in writing, software 16 | # distributed under the License is distributed on an "AS IS" BASIS, 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | # See the License for the specific language governing permissions and 19 | # limitations under the License. 20 | # 21 | 22 | # 23 | # Chris Sommers (chris.sommers@keysight.com) 24 | # 25 | if [ $# -ne 1 ]; then 26 | echo "USAGE: $0 " 27 | exit 1 28 | fi 29 | 30 | echo "Generating $1.p4info.proto ..." 31 | echo "p4c-bm2-ss --p4runtime-file $1.p4info.proto --p4runtime-format binary $1" 32 | p4c-bm2-ss --p4runtime-file $1.p4info.proto --p4runtime-format binary $1 33 | echo "Generating $1.p4info.json ..." 34 | echo "p4c-bm2-ss --p4runtime-file $1.p4info.json --p4runtime-format json $1" 35 | p4c-bm2-ss --p4runtime-file $1.p4info.json --p4runtime-format json $1 36 | echo "Generating $1.p4info.txt ..." 37 | echo "p4c-bm2-ss --p4runtime-file $1.p4info.txt --p4runtime-format text $1" 38 | p4c-bm2-ss --p4runtime-file $1.p4info.txt --p4runtime-format text $1 39 | -------------------------------------------------------------------------------- /proto/ptf/.gitignore: -------------------------------------------------------------------------------- 1 | *.proto.txt 2 | *.bin 3 | *.log 4 | *.pcap 5 | -------------------------------------------------------------------------------- /proto/ptf/P4RuntimePTF.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/PI/d28b31e4fa05b51f93b9810f5a3ef4a57fbfb8a8/proto/ptf/P4RuntimePTF.pptx -------------------------------------------------------------------------------- /proto/ptf/bmv2/gen_bmv2_config.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2017 Barefoot Networks, Inc. 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | # Antonin Bas (antonin@barefootnetworks.com) 19 | 20 | if [ $# -ne 2 ]; then 21 | echo "Expects exactly 2 arguments" 22 | echo "Usage: $0 " 23 | exit 1 24 | fi 25 | 26 | THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 27 | 28 | python3 $THIS_DIR/gen_bmv2_config.py $1 \ 29 | --out-bin $2/device_config.bin --out-p4info $2/p4info.proto.txt 30 | -------------------------------------------------------------------------------- /proto/ptf/bmv2/port_map.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "ptf_port": 0, 4 | "p4_port": 0, 5 | "iface_name": "veth1" 6 | }, 7 | { 8 | "ptf_port": 1, 9 | "p4_port": 1, 10 | "iface_name": "veth3" 11 | }, 12 | { 13 | "ptf_port": 2, 14 | "p4_port": 2, 15 | "iface_name": "veth5" 16 | }, 17 | { 18 | "ptf_port": 3, 19 | "p4_port": 3, 20 | "iface_name": "veth7" 21 | }, 22 | { 23 | "ptf_port": 4, 24 | "p4_port": 4, 25 | "iface_name": "veth9" 26 | }, 27 | { 28 | "ptf_port": 5, 29 | "p4_port": 5, 30 | "iface_name": "veth11" 31 | }, 32 | { 33 | "ptf_port": 6, 34 | "p4_port": 6, 35 | "iface_name": "veth13" 36 | }, 37 | { 38 | "ptf_port": 7, 39 | "p4_port": 7, 40 | "iface_name": "veth15" 41 | } 42 | ] 43 | -------------------------------------------------------------------------------- /proto/ptf/bmv2/start_switch.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2017 Barefoot Networks, Inc. 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | # Antonin Bas (antonin@barefootnetworks.com) 19 | 20 | if [[ $EUID -ne 0 ]]; then 21 | echo "This script should be run using sudo or as the root user" 22 | exit 1 23 | fi 24 | simple_switch_grpc -i 0@veth0 -i 1@veth2 -i 2@veth4 -i 3@veth6 \ 25 | -i 4@veth8 -i 5@veth10 -i 6@veth12 -i 7@veth14 \ 26 | --log-console --no-p4 \ 27 | -- --grpc-server-addr 0.0.0.0:9559 28 | -------------------------------------------------------------------------------- /proto/ptf/bmv2/veth_setup.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2017 Barefoot Networks, Inc. 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | # Antonin Bas (antonin@barefootnetworks.com) 19 | 20 | noOfVeths=8 21 | if [ $# -eq 1 ]; then 22 | noOfVeths=$1 23 | fi 24 | echo "No of veth pairs is $noOfVeths" 25 | last=`expr $noOfVeths - 1` 26 | veths=`seq 0 1 $last` 27 | 28 | for i in $veths; do 29 | intf0="veth$(($i*2))" 30 | intf1="veth$(($i*2+1))" 31 | if ! ip link show $intf0 &> /dev/null; then 32 | ip link add name $intf0 type veth peer name $intf1 &> /dev/null 33 | fi 34 | ip link set dev $intf0 up 35 | ip link set dev $intf1 up 36 | ifconfig $intf0 mtu 10240 up 37 | ifconfig $intf1 mtu 10240 up 38 | TOE_OPTIONS="rx tx sg tso ufo gso gro lro rxvlan txvlan rxhash" 39 | for TOE_OPTION in $TOE_OPTIONS; do 40 | /sbin/ethtool --offload $intf0 "$TOE_OPTION" off &> /dev/null 41 | /sbin/ethtool --offload $intf1 "$TOE_OPTION" off &> /dev/null 42 | done 43 | sysctl net.ipv6.conf.$intf0.disable_ipv6=1 &> /dev/null 44 | sysctl net.ipv6.conf.$intf1.disable_ipv6=1 &> /dev/null 45 | done 46 | -------------------------------------------------------------------------------- /proto/ptf/bmv2/veth_teardown.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2017 Barefoot Networks, Inc. 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | # Antonin Bas (antonin@barefootnetworks.com) 19 | 20 | noOfVeths=8 21 | if [ $# -eq 1 ]; then 22 | noOfVeths=$1 23 | fi 24 | echo "No of veth pairs is $noOfVeths" 25 | idx=0 26 | while [ $idx -lt $noOfVeths ] 27 | do 28 | intf0="veth$(($idx*2))" 29 | intf1="veth$(($idx*2+1))" 30 | if ip link show $intf0 &> /dev/null; then 31 | ip link delete $intf0 type veth 32 | fi 33 | if ip link show $intf1 &> /dev/null; then 34 | ip link delete $intf1 type veth 35 | fi 36 | idx=$((idx + 1)) 37 | done 38 | -------------------------------------------------------------------------------- /proto/server/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//visibility:public"], 3 | ) 4 | 5 | cc_library( 6 | name = "piserver", 7 | srcs = ["gnmi_dummy.cpp", 8 | "log.h", 9 | "pi_server.cpp", 10 | "shared_mutex.h", 11 | "uint128.h", 12 | "uint128.cpp"], 13 | hdrs = ["PI/proto/pi_server.h", "pi_server_testing.h", "gnmi.h"], 14 | copts = ["-DUSE_ABSL=1"], 15 | deps = ["@com_github_p4lang_p4runtime//:p4runtime_cc_grpc", 16 | "@com_google_googleapis//google/rpc:code_cc_proto", 17 | "@com_google_protobuf//:protobuf", 18 | "@com_github_grpc_grpc//:grpc++", 19 | "@com_github_openconfig_gnmi//:gnmi_cc_grpc", 20 | "//proto:p4serverconfig_cc_grpc", 21 | "//proto/frontend:pifeproto", 22 | "@com_google_absl//absl/synchronization:synchronization"], 23 | ) 24 | -------------------------------------------------------------------------------- /proto/server/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | AM_CPPFLAGS = \ 4 | -I$(top_srcdir) \ 5 | -I$(top_srcdir)/frontend \ 6 | -isystem $(top_builddir)/cpp_out \ 7 | -isystem $(top_builddir)/grpc_out 8 | 9 | if WITH_SYSREPO 10 | AM_CPPFLAGS += -DWITH_SYSREPO 11 | endif 12 | 13 | AM_CXXFLAGS = -Wall -Werror 14 | 15 | lib_LTLIBRARIES = libpigrpcserver.la 16 | 17 | libpigrpcserver_la_SOURCES = \ 18 | gnmi.h \ 19 | gnmi_dummy.cpp \ 20 | log.h \ 21 | pi_server.cpp \ 22 | shared_mutex.h \ 23 | uint128.h \ 24 | uint128.cpp \ 25 | pi_server_testing.h 26 | 27 | if WITH_SYSREPO 28 | libpigrpcserver_la_SOURCES += \ 29 | gnmi_sysrepo.cpp 30 | endif 31 | 32 | nobase_include_HEADERS = PI/proto/pi_server.h 33 | 34 | libpigrpcserver_la_LIBADD = \ 35 | $(top_builddir)/frontend/libpifeproto.la \ 36 | $(top_builddir)/libpiprotogrpc.la \ 37 | $(top_builddir)/libpiprotobuf.la \ 38 | $(PROTOBUF_LIBS) $(GRPC_LIBS) \ 39 | $(BOOST_THREAD_LIB) $(BOOST_SYSTEM_LIB) 40 | # causes issue with libpiproto as it includes a version of google.rpc.Status 41 | # -lgrpc++_error_details 42 | -------------------------------------------------------------------------------- /proto/server/PI/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | filter=-build/header_guard 2 | -------------------------------------------------------------------------------- /proto/server/gnmi.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PROTO_SERVER_GNMI_H_ 23 | #define PROTO_SERVER_GNMI_H_ 24 | 25 | #include 26 | 27 | #include "gnmi/gnmi.grpc.pb.h" 28 | 29 | namespace pi { 30 | 31 | namespace server { 32 | 33 | std::unique_ptr make_gnmi_service_dummy(); 34 | 35 | #ifdef WITH_SYSREPO 36 | std::unique_ptr make_gnmi_service_sysrepo(); 37 | #endif // WITH_SYSREPO 38 | 39 | } // namespace server 40 | 41 | } // namespace pi 42 | 43 | #endif // PROTO_SERVER_GNMI_H_ 44 | -------------------------------------------------------------------------------- /proto/server/log.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PROTO_SERVER_LOG_H_ 23 | #define PROTO_SERVER_LOG_H_ 24 | 25 | #include 26 | 27 | // #define DEBUG 28 | 29 | #ifdef DEBUG 30 | #define ENABLE_SIMPLELOG true 31 | #else 32 | #define ENABLE_SIMPLELOG false 33 | #endif 34 | 35 | #define SIMPLELOG if (ENABLE_SIMPLELOG) std::cout 36 | 37 | #endif // PROTO_SERVER_LOG_H_ 38 | -------------------------------------------------------------------------------- /proto/server/pi_server_testing.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PROTO_SERVER_PI_SERVER_TESTING_H_ 23 | #define PROTO_SERVER_PI_SERVER_TESTING_H_ 24 | 25 | #include 26 | 27 | namespace p4 { 28 | 29 | namespace v1 { 30 | 31 | class PacketIn; 32 | 33 | } // namespace v1 34 | 35 | } // namespace p4 36 | 37 | namespace pi { 38 | 39 | namespace server { 40 | 41 | namespace testing { 42 | 43 | void send_packet_in(::pi::fe::proto::DeviceMgr::device_id_t device_id, 44 | p4::v1::PacketIn *packet); 45 | 46 | size_t max_connections(); 47 | 48 | } // namespace testing 49 | 50 | } // namespace server 51 | 52 | } // namespace pi 53 | 54 | #endif // PROTO_SERVER_PI_SERVER_TESTING_H_ 55 | -------------------------------------------------------------------------------- /proto/server/uint128.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include "uint128.h" 23 | 24 | #include 25 | #include 26 | 27 | namespace { 28 | 29 | // Saves the internal state of a stream and restores it in the destructor 30 | struct StreamStateSaver final { 31 | explicit StreamStateSaver(std::ios &s) // NOLINT(runtime/references) 32 | : ref(s) { 33 | state.copyfmt(s); 34 | } 35 | 36 | ~StreamStateSaver() { 37 | ref.copyfmt(state); 38 | } 39 | 40 | std::ios &ref; 41 | std::ios state{nullptr}; 42 | }; 43 | 44 | } // namespace 45 | 46 | std::ostream &operator<<(std::ostream &out, const Uint128 &n) { 47 | StreamStateSaver state_saver(out); 48 | out << "0x"; 49 | if (n.high_ == 0) 50 | out << std::hex << n.low_; 51 | else 52 | out << std::hex << n.high_ << std::setw(16) << std::setfill('0') << n.low_; 53 | return out; 54 | } 55 | -------------------------------------------------------------------------------- /proto/server_config/server_config.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 VMware, Inc. 2 | // SPDX-License-Identifier: Apache-2.0 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | #include "server_config.h" 17 | 18 | #include 19 | 20 | #include 21 | 22 | #include "p4/server/v1/config.pb.h" 23 | 24 | namespace pi { 25 | 26 | namespace fe { 27 | 28 | namespace proto { 29 | 30 | /* static */ 31 | template 32 | bool 33 | ServerConfigFromText::parse(const std::string &config_text, 34 | T *server_config) { 35 | return ::google::protobuf::TextFormat::ParseFromString( 36 | config_text, server_config); 37 | } 38 | 39 | template class ServerConfigFromText; 40 | 41 | } // namespace proto 42 | 43 | } // namespace fe 44 | 45 | } // namespace pi 46 | 47 | -------------------------------------------------------------------------------- /proto/src/util.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | 24 | namespace pi { 25 | 26 | namespace proto { 27 | 28 | namespace util { 29 | 30 | p4::config::v1::P4Ids::Prefix 31 | resource_type_from_id(p4_id_t p4_id) { 32 | using p4::config::v1::P4Ids; 33 | switch (p4_id >> 24) { 34 | case static_cast(P4Ids::UNSPECIFIED): 35 | return P4Ids::UNSPECIFIED; 36 | case static_cast(P4Ids::ACTION): 37 | return P4Ids::ACTION; 38 | case static_cast(P4Ids::TABLE): 39 | return P4Ids::TABLE; 40 | case static_cast(P4Ids::VALUE_SET): 41 | return P4Ids::VALUE_SET; 42 | case static_cast(P4Ids::ACTION_PROFILE): 43 | return P4Ids::ACTION_PROFILE; 44 | case static_cast(P4Ids::COUNTER): 45 | return P4Ids::COUNTER; 46 | case static_cast(P4Ids::DIRECT_COUNTER): 47 | return P4Ids::DIRECT_COUNTER; 48 | case static_cast(P4Ids::METER): 49 | return P4Ids::METER; 50 | case static_cast(P4Ids::DIRECT_METER): 51 | return P4Ids::DIRECT_METER; 52 | default: 53 | return P4Ids::UNSPECIFIED; 54 | } 55 | } 56 | 57 | } // namespace util 58 | 59 | } // namespace proto 60 | 61 | } // namespace pi 62 | -------------------------------------------------------------------------------- /proto/sysrepo/docker_entry_point.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Copyright 2017 Barefoot Networks, Inc. 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | # Antonin Bas (antonin@barefootnetworks.com) 19 | 20 | sysrepod 21 | # execute docker command 22 | exec "$@" 23 | -------------------------------------------------------------------------------- /proto/sysrepo/install_yangs.sh.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | OPENCONFIG_ROOT="@abs_top_srcdir@/openconfig/public/release/models" 6 | 7 | SEARCH_DIRS="-s @abs_top_srcdir@/yang \ 8 | -s $OPENCONFIG_ROOT \ 9 | -s $OPENCONFIG_ROOT/types" 10 | 11 | YANGS="@abs_top_srcdir@/yang/iana-if-type.yang \ 12 | $OPENCONFIG_ROOT/interfaces/openconfig-interfaces.yang \ 13 | $OPENCONFIG_ROOT/platform/openconfig-platform.yang" 14 | 15 | # Sysrepo deamon complains if this is missing 16 | YANGS="$YANGS @abs_top_srcdir@/yang/ietf-netconf-notifications.yang" 17 | 18 | # Sysrepocfg cannot export the openconfig-interfaces data tree if this is not 19 | # explicitly installed. 20 | # See https://github.com/sysrepo/sysrepo/issues/1015 21 | # I don't know if this is the intended behavior or an issue, so in the meantime 22 | # I explicitly install it. 23 | YANGS="$YANGS @abs_top_srcdir@/yang/ietf-interfaces@2014-05-08.yang" 24 | 25 | # openconfig-interfaces.yang uses a "when" statement on state data, which is not 26 | # correct YANG. This is my workaround. 27 | # See https://github.com/openconfig/public/issues/108 28 | # The generated file is never cleaned up so 'make distcheck' will probably fail 29 | mkdir -p @abs_builddir@ 30 | @SED@ 's;when "oc-if:state/oc-if:type;when "oc-if:config/oc-if:type;g' \ 31 | $OPENCONFIG_ROOT/interfaces/openconfig-if-ethernet.yang > @abs_builddir@/openconfig-if-ethernet.yang 32 | YANGS="$YANGS @abs_builddir@/openconfig-if-ethernet.yang" 33 | 34 | # openconfig-yang-types.yang uses regex anchors (POSIX regex) which is not 35 | # correct YANG. This is my workaround. 36 | # See https://github.com/openconfig/public/issues/44 37 | @SED@ "s;\(\s'\)\^;\1;g" \ 38 | $OPENCONFIG_ROOT/types/openconfig-yang-types.yang > @abs_builddir@/openconfig-yang-types.yang 39 | @SED@ -i "s;\$\('\;\);\1;g" @abs_builddir@/openconfig-yang-types.yang 40 | YANGS="$YANGS @abs_builddir@/openconfig-yang-types.yang" 41 | 42 | for YANG in $YANGS; do 43 | sysrepoctl -i $SEARCH_DIRS --yang $YANG 44 | done 45 | -------------------------------------------------------------------------------- /proto/tests/.gitignore: -------------------------------------------------------------------------------- 1 | test* 2 | !*.cpp 3 | -------------------------------------------------------------------------------- /proto/tests/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//visibility:private"], 3 | ) 4 | 5 | cc_library( 6 | name = "testutils", 7 | srcs = ["main.cpp", 8 | "matchers.cpp", 9 | "mock_switch.cpp", 10 | "stream_receiver.h"], 11 | hdrs = ["matchers.h", "mock_switch.h"], 12 | deps = ["@com_google_googletest//:gtest", 13 | "@com_github_p4lang_p4runtime//:p4runtime_cc_proto", 14 | "//proto/frontend:pifeproto", 15 | "@boost//:optional", 16 | "@boost//:functional"], 17 | testonly = True, 18 | ) 19 | 20 | cc_test( 21 | name = "pi_proto_tests", 22 | srcs = ["test_p4info_convert.cpp", 23 | "test_task_queue.cpp", 24 | "test_proto_fe_base.h"] 25 | + glob(["test_proto_fe*.cpp"]), 26 | deps = ["@com_google_googletest//:gtest", 27 | "@com_github_p4lang_p4runtime//:p4runtime_cc_proto", 28 | "//proto/frontend:pifeproto", 29 | ":testutils", 30 | "@boost//:optional"], 31 | data = ["//tests:exported_testdata"], 32 | copts = ['-DTESTDATADIR=\\"tests/testdata\\"'], 33 | ) 34 | 35 | cc_test( 36 | name = "pi_proto_server_tests", 37 | srcs = glob(["server/*.cpp", "server/*.h"]), 38 | deps = ["@com_google_googletest//:gtest", 39 | "@com_github_p4lang_p4runtime//:p4runtime_cc_grpc", 40 | "@com_github_p4lang_p4runtime//:p4runtime_cc_proto", 41 | "//proto/server:piserver", 42 | ":testutils", 43 | "@boost//:optional", 44 | "@com_github_grpc_grpc//:grpc++"], 45 | data = ["//tests:exported_testdata"], 46 | copts = ['-DTESTDATADIR=\\"tests/testdata\\"', 47 | "-Iproto/server", "-Iproto/tests", 48 | "-I$(GENDIR)/proto/p4serverconfig_cc_grpc_pb/proto"], 49 | ) 50 | -------------------------------------------------------------------------------- /proto/tests/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | filter=-readability/function 2 | -------------------------------------------------------------------------------- /proto/tests/main.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | 24 | int main(int argc, char* argv[]) { 25 | ::testing::InitGoogleTest(&argc, argv); 26 | return RUN_ALL_TESTS(); 27 | } 28 | -------------------------------------------------------------------------------- /proto/tests/server/test_pi_server.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, Google Inc. 2 | // SPDX-License-Identifier: Apache-2.0 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | namespace pi { 21 | namespace proto { 22 | namespace testing { 23 | namespace { 24 | 25 | const char grpc_server_addr[] = "0.0.0.0"; 26 | 27 | void StartGrpcServer(int port) { 28 | std::string bind_addr = 29 | std::string(grpc_server_addr) + ":" + std::to_string(port); 30 | PIGrpcServerRunAddr(bind_addr.c_str()); 31 | } 32 | 33 | void ShutdownGrpcServer() { 34 | PIGrpcServerShutdown(); 35 | } 36 | 37 | TEST(TestPIGrpcServer, DefaultPort) { 38 | // maybe we should just get rid of the tests in this file now that we let the 39 | // OS bind to any port and use PIGrpcServerGetPort() in other tests 40 | int port = 1024; 41 | for (; port < 4096; port++) { 42 | StartGrpcServer(port); 43 | if (PIGrpcServerGetPort() == port) break; 44 | } 45 | ASSERT_EQ(PIGrpcServerGetPort(), port); 46 | ShutdownGrpcServer(); 47 | } 48 | 49 | TEST(TestPIGrpcServer, RandomPort) { 50 | StartGrpcServer(0); 51 | EXPECT_NE(PIGrpcServerGetPort(), 0); 52 | ShutdownGrpcServer(); 53 | } 54 | 55 | } // namespace 56 | } // namespace testing 57 | } // namespace proto 58 | } // namespace pi 59 | -------------------------------------------------------------------------------- /proto/tests/server/utils.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include "utils.h" 23 | 24 | namespace pi { 25 | namespace proto { 26 | namespace testing { 27 | 28 | constexpr char TestServer::bind_any_addr[]; 29 | 30 | } // namespace testing 31 | } // namespace proto 32 | } // namespace pi 33 | -------------------------------------------------------------------------------- /proto/tests/server/utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PROTO_TESTS_SERVER_UTILS_H_ 23 | #define PROTO_TESTS_SERVER_UTILS_H_ 24 | 25 | #include 26 | 27 | #include 28 | 29 | namespace pi { 30 | namespace proto { 31 | namespace testing { 32 | 33 | class TestServer { 34 | public: 35 | TestServer() { 36 | PIGrpcServerRunAddr(bind_any_addr); 37 | server_port = PIGrpcServerGetPort(); 38 | } 39 | 40 | ~TestServer() { 41 | PIGrpcServerShutdown(); 42 | } 43 | 44 | std::string bind_addr() const { 45 | return std::string("0.0.0.0:") + std::to_string(server_port); 46 | } 47 | 48 | private: 49 | static constexpr char bind_any_addr[] = "[::]:0"; 50 | int server_port; 51 | }; 52 | 53 | } // namespace testing 54 | } // namespace proto 55 | } // namespace pi 56 | 57 | #endif // PROTO_TESTS_SERVER_UTILS_H_ 58 | -------------------------------------------------------------------------------- /proto/tests/test_proto_fe_base.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include "test_proto_fe_base.h" 23 | 24 | namespace pi { 25 | namespace proto { 26 | namespace testing { 27 | 28 | /* static */constexpr const char *DeviceMgrBaseTest::invalid_p4_id_error_str; 29 | 30 | /* static */ pi_p4info_t *DeviceMgrUnittestBaseTest::p4info = nullptr; 31 | /* static */ p4configv1::P4Info DeviceMgrUnittestBaseTest::p4info_proto; 32 | 33 | } // namespace testing 34 | } // namespace proto 35 | } // namespace pi 36 | -------------------------------------------------------------------------------- /proto/third_party/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//:__subpackages__"], 3 | ) 4 | 5 | cc_library( 6 | name = "fmt", 7 | srcs = ["fmt/container.h", 8 | "fmt/format.cc", 9 | "fmt/ostream.cc", 10 | "fmt/ostream.h", 11 | "fmt/posix.cc", 12 | "fmt/posix.h", 13 | "fmt/printf.cc", 14 | "fmt/printf.h", 15 | "fmt/string.h", 16 | "fmt/time.h", 17 | "fmt/_assert.h", 18 | "fmt/_assert.cc"], 19 | hdrs = ["fmt/format.h"], 20 | includes = ["."], 21 | ) -------------------------------------------------------------------------------- /proto/third_party/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | noinst_LTLIBRARIES = libfmt.la 4 | 5 | libfmt_la_SOURCES = \ 6 | fmt/container.h \ 7 | fmt/format.cc \ 8 | fmt/format.h \ 9 | fmt/ostream.cc \ 10 | fmt/ostream.h \ 11 | fmt/posix.cc \ 12 | fmt/posix.h \ 13 | fmt/printf.cc \ 14 | fmt/printf.h \ 15 | fmt/string.h \ 16 | fmt/time.h \ 17 | fmt/_assert.h \ 18 | fmt/_assert.cc 19 | -------------------------------------------------------------------------------- /proto/third_party/fmt/LICENSE.rst: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 - 2016, Victor Zverovich 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /proto/third_party/fmt/_assert.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | /* 17 | * Antonin Bas (antonin@barefootnetworks.com) 18 | * 19 | */ 20 | 21 | #include "_assert.h" 22 | 23 | #include 24 | #include 25 | 26 | namespace fmt { 27 | 28 | void _assert(const char* expr, const char* file, int line) { 29 | std::cerr << "Assertion '" << expr << "' failed, file '" << file 30 | << "' line '" << line << "'.\n"; 31 | std::abort(); 32 | } 33 | 34 | } // namespace fmt 35 | -------------------------------------------------------------------------------- /proto/third_party/fmt/_assert.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | /* 17 | * Antonin Bas (antonin@barefootnetworks.com) 18 | * 19 | */ 20 | 21 | #ifndef FMT__ASSERT_H_ 22 | #define FMT__ASSERT_H_ 23 | 24 | // An assert that cannot be removed with NDEBUG 25 | 26 | namespace fmt { 27 | 28 | [[ noreturn ]] void _assert(const char* expr, const char* file, int line); 29 | 30 | } // namespace fmt 31 | 32 | #define _FMT_ASSERT(expr) \ 33 | ((expr) ? (void)0 : fmt::_assert(#expr, __FILE__, __LINE__)) 34 | 35 | #define _FMT_UNREACHABLE(msg) fmt::_assert(msg, __FILE__, __LINE__) 36 | 37 | #define _FMT_UNUSED(x) ((void)x) 38 | 39 | #endif // FMT__ASSERT_H_ 40 | -------------------------------------------------------------------------------- /proto/third_party/fmt/ostream.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Formatting library for C++ - std::ostream support 3 | 4 | Copyright (c) 2012 - 2016, Victor Zverovich 5 | All rights reserved. 6 | 7 | For the license information refer to format.h. 8 | */ 9 | 10 | #include "ostream.h" 11 | 12 | namespace fmt { 13 | 14 | namespace internal { 15 | FMT_FUNC void write(std::ostream &os, Writer &w) { 16 | const char *data = w.data(); 17 | typedef internal::MakeUnsigned::Type UnsignedStreamSize; 18 | UnsignedStreamSize size = w.size(); 19 | UnsignedStreamSize max_size = 20 | internal::to_unsigned((std::numeric_limits::max)()); 21 | do { 22 | UnsignedStreamSize n = size <= max_size ? size : max_size; 23 | os.write(data, static_cast(n)); 24 | data += n; 25 | size -= n; 26 | } while (size != 0); 27 | } 28 | } 29 | 30 | FMT_FUNC void print(std::ostream &os, CStringRef format_str, ArgList args) { 31 | MemoryWriter w; 32 | w.write(format_str, args); 33 | internal::write(os, w); 34 | } 35 | } // namespace fmt 36 | -------------------------------------------------------------------------------- /proto/third_party/fmt/printf.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Formatting library for C++ 3 | 4 | Copyright (c) 2012 - 2016, Victor Zverovich 5 | All rights reserved. 6 | 7 | For the license information refer to format.h. 8 | */ 9 | 10 | #include "format.h" 11 | #include "printf.h" 12 | 13 | namespace fmt { 14 | 15 | template 16 | void printf(BasicWriter &w, BasicCStringRef format, ArgList args); 17 | 18 | FMT_FUNC int fprintf(std::FILE *f, CStringRef format, ArgList args) { 19 | MemoryWriter w; 20 | printf(w, format, args); 21 | std::size_t size = w.size(); 22 | return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast(size); 23 | } 24 | 25 | #ifndef FMT_HEADER_ONLY 26 | 27 | template void PrintfFormatter::format(CStringRef format); 28 | template void PrintfFormatter::format(WCStringRef format); 29 | 30 | #endif // FMT_HEADER_ONLY 31 | 32 | } // namespace fmt 33 | -------------------------------------------------------------------------------- /src/cb_mgr.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_CB_MGR_H_ 23 | #define PI_SRC_CB_MGR_H_ 24 | 25 | #include "device_map.h" 26 | 27 | typedef void (*GenericFnPtr)(); 28 | 29 | typedef struct { 30 | GenericFnPtr cb; 31 | void *cookie; 32 | } cb_data_t; 33 | 34 | typedef struct { 35 | device_map_t device_cbs; 36 | cb_data_t default_cb; 37 | } cb_mgr_t; 38 | 39 | void cb_mgr_init(cb_mgr_t *cb_mgr); 40 | 41 | void cb_mgr_destroy(cb_mgr_t *cb_mgr); 42 | 43 | const cb_data_t *cb_mgr_get(cb_mgr_t *cb_mgr, pi_dev_id_t dev_id); 44 | 45 | void cb_mgr_add(cb_mgr_t *cb_mgr, pi_dev_id_t dev_id, GenericFnPtr cb, 46 | void *cb_cookie); 47 | 48 | void cb_mgr_rm(cb_mgr_t *cb_mgr, pi_dev_id_t dev_id); 49 | 50 | const cb_data_t *cb_mgr_get_default(cb_mgr_t *cb_mgr); 51 | 52 | void cb_mgr_set_default(cb_mgr_t *cb_mgr, GenericFnPtr cb, void *cb_cookie); 53 | 54 | void cb_mgr_reset_default(cb_mgr_t *cb_mgr); 55 | 56 | const cb_data_t *cb_mgr_get_or_default(cb_mgr_t *cb_mgr, pi_dev_id_t dev_id); 57 | 58 | #endif // PI_SRC_CB_MGR_H_ 59 | -------------------------------------------------------------------------------- /src/config_readers/readers.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_CONFIG_READERS_READERS_H_ 23 | #define PI_SRC_CONFIG_READERS_READERS_H_ 24 | 25 | #include "PI/pi_base.h" 26 | 27 | pi_status_t pi_bmv2_json_reader(const char *config, pi_p4info_t *p4info); 28 | 29 | pi_status_t pi_native_json_reader(const char *config, pi_p4info_t *p4info); 30 | 31 | #endif // PI_SRC_CONFIG_READERS_READERS_H_ 32 | -------------------------------------------------------------------------------- /src/device_map.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * Copyright 2021 VMware, Inc. 3 | * SPDX-License-Identifier: Apache-2.0 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /* 19 | * Antonin Bas 20 | * 21 | */ 22 | 23 | #ifndef PI_SRC_DEVICE_MAP_H_ 24 | #define PI_SRC_DEVICE_MAP_H_ 25 | 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | typedef struct device_entry_s device_entry_t; 32 | 33 | // This is actually a sorted vector, we expect the num of devices to be small. 34 | typedef struct { 35 | device_entry_t *entries; 36 | int size; 37 | int capacity; 38 | } device_map_t; 39 | 40 | void device_map_create(device_map_t *map); 41 | 42 | // returns false if device already exists 43 | bool device_map_add(device_map_t *map, pi_dev_id_t dev_id, void *e); 44 | 45 | // returns false if device does not exists 46 | bool device_map_remove(device_map_t *map, pi_dev_id_t dev_id); 47 | 48 | bool device_map_exists(device_map_t *map, pi_dev_id_t dev_id); 49 | 50 | // returns NULL if device does not exists 51 | void *device_map_get(device_map_t *map, pi_dev_id_t dev_id); 52 | 53 | typedef void (*DeviceMapApplyFn)(void *e, void *cookie); 54 | 55 | void device_map_for_each(device_map_t *map, DeviceMapApplyFn fn, void *cookie); 56 | 57 | size_t device_map_count(device_map_t *map); 58 | 59 | void device_map_destroy(device_map_t *map); 60 | 61 | #endif // PI_SRC_DEVICE_MAP_H_ 62 | -------------------------------------------------------------------------------- /src/p4info/act_profs_int.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_P4INFO_ACT_PROFS_INT_H_ 23 | #define PI_SRC_P4INFO_ACT_PROFS_INT_H_ 24 | 25 | #include "PI/p4info/act_profs.h" 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | void pi_p4info_act_prof_init(pi_p4info_t *p4info, size_t num_act_profs); 32 | 33 | void pi_p4info_act_prof_free(pi_p4info_t *p4info); 34 | 35 | void pi_p4info_act_prof_add(pi_p4info_t *p4info, pi_p4_id_t act_prof_id, 36 | const char *name, bool with_selector, 37 | size_t max_size); 38 | 39 | void pi_p4info_act_prof_add_table(pi_p4info_t *p4info, pi_p4_id_t act_prof_id, 40 | pi_p4_id_t table_id); 41 | 42 | void pi_p4info_act_prof_set_max_grp_size(pi_p4info_t *p4info, 43 | pi_p4_id_t act_prof_id, 44 | size_t max_grp_size); 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif // PI_SRC_P4INFO_ACT_PROFS_INT_H_ 51 | -------------------------------------------------------------------------------- /src/p4info/actions_int.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_P4INFO_ACTIONS_INT_H_ 23 | #define PI_SRC_P4INFO_ACTIONS_INT_H_ 24 | 25 | #include "PI/p4info/actions.h" 26 | 27 | #include "p4info_common.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | void pi_p4info_action_init(pi_p4info_t *p4info, size_t num_actions); 34 | 35 | void pi_p4info_action_add(pi_p4info_t *p4info, pi_p4_id_t action_id, 36 | const char *name, size_t num_params); 37 | 38 | void pi_p4info_action_add_param(pi_p4info_t *p4info, pi_p4_id_t action_id, 39 | pi_p4_id_t param_id, const char *name, 40 | size_t bitwidth); 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | 46 | #endif // PI_SRC_P4INFO_ACTIONS_INT_H_ 47 | -------------------------------------------------------------------------------- /src/p4info/counters_int.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_P4INFO_COUNTERS_INT_H_ 23 | #define PI_SRC_P4INFO_COUNTERS_INT_H_ 24 | 25 | #include "PI/p4info/counters.h" 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | void pi_p4info_counter_init(pi_p4info_t *p4info, size_t num_counters); 32 | void pi_p4info_direct_counter_init(pi_p4info_t *p4info, 33 | size_t num_direct_counters); 34 | 35 | void pi_p4info_counter_add(pi_p4info_t *p4info, pi_p4_id_t counter_id, 36 | const char *name, 37 | pi_p4info_counter_unit_t counter_unit, size_t size); 38 | void pi_p4info_direct_counter_add(pi_p4info_t *p4info, pi_p4_id_t counter_id, 39 | const char *name, 40 | pi_p4info_counter_unit_t counter_unit, 41 | size_t size, pi_p4_id_t direct_table_id); 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif // PI_SRC_P4INFO_COUNTERS_INT_H_ 48 | -------------------------------------------------------------------------------- /src/p4info/digests_int.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_P4INFO_DIGESTS_INT_H_ 23 | #define PI_SRC_P4INFO_DIGESTS_INT_H_ 24 | 25 | #include "PI/p4info/digests.h" 26 | 27 | #include "p4info_common.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | void pi_p4info_digest_init(pi_p4info_t *p4info, size_t num_digests); 34 | 35 | void pi_p4info_digest_add(pi_p4info_t *p4info, pi_p4_id_t digest_id, 36 | const char *name, size_t num_fields); 37 | 38 | void pi_p4info_digest_add_field(pi_p4info_t *p4info, pi_p4_id_t digest_id, 39 | const char *name, size_t bitwidth); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif // PI_SRC_P4INFO_DIGESTS_INT_H_ 46 | -------------------------------------------------------------------------------- /src/p4info/fast_id_vector.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include "fast_id_vector.h" 23 | 24 | #include 25 | #include 26 | 27 | void id_vector_init(id_vector_t *id_vector) { 28 | id_vector->size = 0; 29 | id_vector->capacity = 0; 30 | } 31 | 32 | void id_vector_push_back(id_vector_t *id_vector, pi_p4_id_t id) { 33 | if (id_vector->size < ID_VECTOR_INLINE_IDS) { 34 | id_vector->ids.direct[id_vector->size] = id; 35 | } else if (id_vector->size > ID_VECTOR_INLINE_IDS) { 36 | if (id_vector->size >= id_vector->capacity) { 37 | id_vector->capacity *= 2; 38 | id_vector->ids.indirect = realloc( 39 | id_vector->ids.indirect, id_vector->capacity * sizeof(pi_p4_id_t)); 40 | } 41 | id_vector->ids.indirect[id_vector->size] = id; 42 | } else { 43 | id_vector->capacity = 2 * ID_VECTOR_INLINE_IDS; 44 | pi_p4_id_t *ids = malloc(id_vector->capacity * sizeof(pi_p4_id_t)); 45 | memcpy(ids, id_vector->ids.direct, sizeof(id_vector->ids.direct)); 46 | id_vector->ids.indirect = ids; 47 | id_vector->ids.indirect[id_vector->size] = id; 48 | } 49 | id_vector->size += 1; 50 | } 51 | 52 | void id_vector_destroy(id_vector_t *id_vector) { 53 | if (id_vector->size > ID_VECTOR_INLINE_IDS) free(id_vector->ids.indirect); 54 | } 55 | -------------------------------------------------------------------------------- /src/p4info/fast_id_vector.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_P4INFO_FAST_ID_VECTOR_H_ 23 | #define PI_SRC_P4INFO_FAST_ID_VECTOR_H_ 24 | 25 | #include 26 | 27 | #include 28 | 29 | // A vector of ids optimized for p4info. The vector can only grow and ids are 30 | // stored inline until a limit is reached, at which point they are relocated to 31 | // some new memory. 32 | 33 | #define ID_VECTOR_INLINE_IDS 8 34 | 35 | typedef struct id_vector_s { 36 | size_t size; 37 | size_t capacity; 38 | union { 39 | pi_p4_id_t *indirect; 40 | pi_p4_id_t direct[ID_VECTOR_INLINE_IDS]; 41 | } ids; 42 | } id_vector_t; 43 | 44 | void id_vector_init(id_vector_t *id_vector); 45 | 46 | #define ID_VECTOR_INIT(vec) id_vector_init(&vec); 47 | 48 | void id_vector_push_back(id_vector_t *id_vector, pi_p4_id_t id); 49 | 50 | #define ID_VECTOR_PUSH_BACK(vec, id) id_vector_push_back(&vec, id); 51 | 52 | #define ID_VECTOR_GET(vec) \ 53 | ((vec.size <= ID_VECTOR_INLINE_IDS) ? vec.ids.direct : vec.ids.indirect) 54 | 55 | #define ID_VECTOR_NUM(vec) (vec.size) 56 | 57 | void id_vector_destroy(id_vector_t *id_vector); 58 | 59 | #define ID_VECTOR_DESTROY(vec) id_vector_destroy(&vec); 60 | 61 | #endif // PI_SRC_P4INFO_FAST_ID_VECTOR_H_ 62 | -------------------------------------------------------------------------------- /src/p4info/meters_int.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_P4INFO_METERS_INT_H_ 23 | #define PI_SRC_P4INFO_METERS_INT_H_ 24 | 25 | #include "PI/p4info/meters.h" 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | void pi_p4info_meter_init(pi_p4info_t *p4info, size_t num_meters); 32 | void pi_p4info_direct_meter_init(pi_p4info_t *p4info, size_t num_direct_meters); 33 | 34 | void pi_p4info_meter_add(pi_p4info_t *p4info, pi_p4_id_t meter_id, 35 | const char *name, pi_p4info_meter_unit_t meter_unit, 36 | pi_p4info_meter_type_t meter_type, size_t size); 37 | void pi_p4info_direct_meter_add(pi_p4info_t *p4info, pi_p4_id_t meter_id, 38 | const char *name, 39 | pi_p4info_meter_unit_t meter_unit, 40 | pi_p4info_meter_type_t meter_type, size_t size, 41 | pi_p4_id_t direct_table_id); 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif // PI_SRC_P4INFO_METERS_INT_H_ 48 | -------------------------------------------------------------------------------- /src/p4info/p4info_common.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_P4INFO_P4INFO_COMMON_H_ 23 | #define PI_SRC_P4INFO_P4INFO_COMMON_H_ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | typedef struct p4info_common_s p4info_common_t; 30 | 31 | void p4info_common_push_back_annotation(p4info_common_t *common, 32 | const char *annotation); 33 | 34 | char const *const *p4info_common_annotations(p4info_common_t *common, 35 | size_t *num_annotations); 36 | 37 | void p4info_common_push_back_alias(p4info_common_t *common, const char *alias); 38 | 39 | char const *const *p4info_common_aliases(p4info_common_t *common, 40 | size_t *num_aliases); 41 | 42 | typedef struct cJSON cJSON; 43 | void p4info_common_serialize(cJSON *object, const p4info_common_t *common); 44 | 45 | void p4info_common_init(p4info_common_t *common); 46 | 47 | void p4info_common_destroy(p4info_common_t *common); 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif // PI_SRC_P4INFO_P4INFO_COMMON_H_ 54 | -------------------------------------------------------------------------------- /src/p4info/p4info_name_map.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * Copyright 2021 VMware, Inc. 3 | * SPDX-License-Identifier: Apache-2.0 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /* 19 | * Antonin Bas 20 | * 21 | */ 22 | 23 | #include "p4info_name_map.h" 24 | 25 | #include 26 | 27 | struct p4info_name_hash_s { 28 | const char *name; 29 | pi_p4_id_t id; 30 | UT_hash_handle hh; 31 | }; 32 | 33 | void p4info_name_map_init(p4info_name_map_t *map) { map->hash = NULL; } 34 | 35 | int p4info_name_map_add(p4info_name_map_t *map, const char *name, 36 | pi_p4_id_t id) { 37 | p4info_name_hash_t *hash; 38 | HASH_FIND_STR(map->hash, name, hash); 39 | if (hash) return 0; 40 | hash = malloc(sizeof(*hash)); 41 | hash->name = name; 42 | hash->id = id; 43 | HASH_ADD_KEYPTR(hh, map->hash, hash->name, strlen(hash->name), hash); 44 | return 1; 45 | } 46 | 47 | pi_p4_id_t p4info_name_map_get(const p4info_name_map_t *map, const char *name) { 48 | p4info_name_hash_t *hash; 49 | HASH_FIND_STR(map->hash, name, hash); 50 | return (hash) ? hash->id : PI_INVALID_ID; 51 | } 52 | 53 | void p4info_name_map_destroy(p4info_name_map_t *map) { 54 | p4info_name_hash_t *hash, *tmp; 55 | HASH_ITER(hh, map->hash, hash, tmp) { // deletion-safe iteration 56 | HASH_DEL(map->hash, hash); 57 | free(hash); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/p4info/p4info_name_map.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * Copyright 2021 VMware, Inc. 3 | * SPDX-License-Identifier: Apache-2.0 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /* 19 | * Antonin Bas 20 | * 21 | */ 22 | 23 | #ifndef PI_SRC_P4INFO_P4INFO_NAME_MAP_H_ 24 | #define PI_SRC_P4INFO_P4INFO_NAME_MAP_H_ 25 | 26 | #include 27 | 28 | typedef struct p4info_name_hash_s p4info_name_hash_t; 29 | 30 | typedef struct { 31 | p4info_name_hash_t *hash; 32 | } p4info_name_map_t; 33 | 34 | void p4info_name_map_init(p4info_name_map_t *map); 35 | 36 | // returns 1 if value succesfully inserted, 0 if key was already present 37 | int p4info_name_map_add(p4info_name_map_t *map, const char *name, 38 | pi_p4_id_t id); 39 | 40 | pi_p4_id_t p4info_name_map_get(const p4info_name_map_t *map, const char *name); 41 | 42 | void p4info_name_map_destroy(p4info_name_map_t *map); 43 | 44 | #endif // PI_SRC_P4INFO_P4INFO_NAME_MAP_H_ 45 | -------------------------------------------------------------------------------- /src/p4info_int.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_P4INFO_INT_H_ 23 | #define PI_SRC_P4INFO_INT_H_ 24 | 25 | #include "p4info/act_profs_int.h" 26 | #include "p4info/actions_int.h" 27 | #include "p4info/counters_int.h" 28 | #include "p4info/digests_int.h" 29 | #include "p4info/meters_int.h" 30 | #include "p4info/tables_int.h" 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | pi_status_t pi_p4info_add_alias(pi_p4info_t *p4info, pi_p4_id_t id, 37 | const char *alias); 38 | 39 | char const *const *pi_p4info_get_aliases(const pi_p4info_t *p4info, 40 | pi_p4_id_t id, size_t *num_aliases); 41 | 42 | pi_status_t pi_p4info_add_annotation(pi_p4info_t *p4info, pi_p4_id_t id, 43 | const char *annotation); 44 | 45 | char const *const *pi_p4info_get_annotations(const pi_p4info_t *p4info, 46 | pi_p4_id_t id, 47 | size_t *num_annotations); 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif // PI_SRC_P4INFO_INT_H_ 54 | -------------------------------------------------------------------------------- /src/pi_clone.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | pi_status_t pi_clone_session_set( 30 | pi_session_handle_t session_handle, pi_dev_tgt_t dev_tgt, 31 | pi_clone_session_id_t clone_session_id, 32 | const pi_clone_session_config_t *clone_session_config) { 33 | return _pi_clone_session_set(session_handle, dev_tgt, clone_session_id, 34 | clone_session_config); 35 | } 36 | 37 | pi_status_t pi_clone_session_reset(pi_session_handle_t session_handle, 38 | pi_dev_tgt_t dev_tgt, 39 | pi_clone_session_id_t clone_session_id) { 40 | return _pi_clone_session_reset(session_handle, dev_tgt, clone_session_id); 41 | } 42 | -------------------------------------------------------------------------------- /src/pi_learn_int.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_PI_LEARN_INT_H_ 23 | #define PI_SRC_PI_LEARN_INT_H_ 24 | 25 | #include "PI/pi_base.h" 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | pi_status_t pi_learn_init(); 32 | 33 | pi_status_t pi_learn_destroy(); 34 | 35 | pi_status_t pi_learn_assign_device(pi_dev_id_t dev_id); 36 | 37 | pi_status_t pi_learn_remove_device(pi_dev_id_t dev_id); 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | 43 | #endif // PI_SRC_PI_LEARN_INT_H_ 44 | -------------------------------------------------------------------------------- /src/pi_notifications_pub.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_PI_NOTIFICATIONS_PUB_H_ 23 | #define PI_SRC_PI_NOTIFICATIONS_PUB_H_ 24 | 25 | #include 26 | 27 | pi_status_t pi_notifications_init(const char *notifications_addr); 28 | 29 | void pi_notifications_pub_learn(const pi_learn_msg_t *msg); 30 | 31 | void pi_notifications_pub_packetin(pi_dev_id_t dev_id, const char *pkt, 32 | size_t size); 33 | 34 | #endif // PI_SRC_PI_NOTIFICATIONS_PUB_H_ 35 | -------------------------------------------------------------------------------- /src/pi_tables_int.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_SRC_PI_TABLE_INT_H_ 23 | #define PI_SRC_PI_TABLE_INT_H_ 24 | 25 | #include "PI/pi_base.h" 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | pi_status_t pi_table_init(); 32 | 33 | pi_status_t pi_table_destroy(); 34 | 35 | pi_status_t pi_table_assign_device(pi_dev_id_t dev_id); 36 | 37 | pi_status_t pi_table_remove_device(pi_dev_id_t dev_id); 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | 43 | #endif // PI_SRC_PI_TABLE_INT_H_ 44 | -------------------------------------------------------------------------------- /src/utils/logging.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | int _logs_on = 1; 18 | 19 | void pi_logs_on() { _logs_on = 1; } 20 | 21 | void pi_logs_off() { _logs_on = 0; } 22 | -------------------------------------------------------------------------------- /src/utils/logging.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // TODO: temporary placeholder 18 | 19 | #ifndef PI_SRC_UTILS_LOGGING_H_ 20 | #define PI_SRC_UTILS_LOGGING_H_ 21 | 22 | #include 23 | 24 | extern int _logs_on; 25 | 26 | void pi_logs_on(); 27 | void pi_logs_off(); 28 | 29 | #ifdef PI_LOG_ON 30 | #define PI_LOG_DEBUG(...) \ 31 | if (_logs_on) fprintf(stderr, __VA_ARGS__) 32 | #define PI_LOG_ERROR(...) \ 33 | if (_logs_on) fprintf(stderr, __VA_ARGS__) 34 | #else 35 | #define PI_LOG_DEBUG(...) 36 | #define PI_LOG_ERROR(...) 37 | #endif 38 | 39 | #endif // PI_SRC_UTILS_LOGGING_H_ 40 | -------------------------------------------------------------------------------- /src/utils/utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PI_SRC_UTILS_UTILS_H_ 18 | #define PI_SRC_UTILS_UTILS_H_ 19 | 20 | #include 21 | 22 | #ifndef htonll 23 | static inline uint64_t htonll(uint64_t n) { 24 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 25 | return n; 26 | #else 27 | return (((uint64_t)htonl(n)) << 32) + htonl(n >> 32); 28 | #endif 29 | } 30 | #endif // htonll 31 | 32 | #ifndef ntohll 33 | static inline uint64_t ntohll(uint64_t n) { 34 | #if __BYTE_ORDER__ == __BIG_ENDIAN__ 35 | return n; 36 | #else 37 | return (((uint64_t)ntohl(n)) << 32) + ntohl(n >> 32); 38 | #endif 39 | } 40 | #endif // ntohll 41 | 42 | #endif // PI_SRC_UTILS_UTILS_H_ 43 | -------------------------------------------------------------------------------- /targets/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | if WITH_BMV2 4 | MAYBE_BMV2 = bmv2 5 | endif 6 | 7 | if WITH_INTERNAL_RPC 8 | MAYBE_RPC = rpc 9 | endif 10 | 11 | SUBDIRS = dummy $(MAYBE_RPC) $(MAYBE_BMV2) 12 | 13 | DIST_SUBDIRS = dummy rpc $(MAYBE_BMV2) 14 | -------------------------------------------------------------------------------- /targets/bmv2/.gitignore: -------------------------------------------------------------------------------- 1 | pi-bmv2-config.h 2 | -------------------------------------------------------------------------------- /targets/bmv2/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | filter=-build/include_subdir,-build/c++11,-build/header_guard 2 | exclude_files=.*config\.h 3 | -------------------------------------------------------------------------------- /targets/bmv2/Makefile.am: -------------------------------------------------------------------------------- 1 | # a little hacky: the headers are in the parent of the configure subdir 2 | AM_CPPFLAGS += \ 3 | -I$(top_srcdir)/../../include 4 | 5 | if WITH_PCAP_FIX 6 | AM_CPPFLAGS += -DWITH_PCAP_FIX 7 | endif 8 | 9 | libpi_bmv2_la_SOURCES = \ 10 | pi-bmv2-config.h \ 11 | pi_imp.cpp \ 12 | pi_tables_imp.cpp \ 13 | pi_act_prof_imp.cpp \ 14 | pi_counter_imp.cpp \ 15 | pi_meter_imp.cpp \ 16 | pi_learn_imp.cpp \ 17 | pi_mc_imp.cpp \ 18 | pi_clone_imp.cpp \ 19 | conn_mgr.h \ 20 | conn_mgr.cpp \ 21 | common.h \ 22 | common.cpp \ 23 | action_helpers.h \ 24 | action_helpers.cpp \ 25 | direct_res_spec.h \ 26 | direct_res_spec.cpp \ 27 | cpu_send_recv.h \ 28 | cpu_send_recv.cpp 29 | 30 | libpi_bmv2_la_LIBADD = \ 31 | $(top_builddir)/../../src/libpip4info.la \ 32 | -lbmp4apps 33 | 34 | lib_LTLIBRARIES = libpi_bmv2.la 35 | 36 | distclean-local: distclean-ax-prefix-config-h 37 | distclean-ax-prefix-config-h: 38 | rm -f pi-bmv2-config.h 39 | -------------------------------------------------------------------------------- /targets/bmv2/action_helpers.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_BMV2_ACTION_HELPERS_H_ 23 | #define PI_BMV2_ACTION_HELPERS_H_ 24 | 25 | #include 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | namespace pibmv2 { 33 | 34 | std::vector build_action_data(const pi_action_data_t *action_data, 35 | const pi_p4info_t *p4info); 36 | 37 | char *dump_action_data(const pi_p4info_t *p4info, char *data, 38 | pi_p4_id_t action_id, 39 | const ::bm_runtime::standard::BmActionData ¶ms); 40 | 41 | } // namespace pibmv2 42 | 43 | #endif // PI_BMV2_ACTION_HELPERS_H_ 44 | -------------------------------------------------------------------------------- /targets/bmv2/common.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | 24 | #include "common.h" 25 | 26 | namespace pibmv2 { 27 | 28 | DeviceInfo *device_info_state = nullptr; 29 | 30 | } // namespace pibmv2 31 | -------------------------------------------------------------------------------- /targets/bmv2/conn_mgr.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_BMV2_CONN_MGR_H_ 23 | #define PI_BMV2_CONN_MGR_H_ 24 | 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | using namespace ::bm_runtime::standard; // NOLINT(build/namespaces) 31 | using namespace ::bm_runtime::simple_pre_lag; // NOLINT(build/namespaces) 32 | 33 | namespace pibmv2 { 34 | 35 | using dev_id_t = uint64_t; 36 | 37 | struct Client { 38 | StandardClient *c; 39 | std::unique_lock _lock; 40 | }; 41 | 42 | struct McClient { 43 | SimplePreLAGClient *c; 44 | std::unique_lock _lock; 45 | }; 46 | 47 | struct conn_mgr_t; 48 | 49 | conn_mgr_t *conn_mgr_create(); 50 | void conn_mgr_destroy(conn_mgr_t *conn_mgr_state); 51 | 52 | Client conn_mgr_client(conn_mgr_t *, dev_id_t dev_id); 53 | McClient conn_mgr_mc_client(conn_mgr_t *, dev_id_t dev_id); 54 | 55 | int conn_mgr_client_init(conn_mgr_t *, dev_id_t dev_id, int thrift_port_num); 56 | int conn_mgr_client_close(conn_mgr_t *, dev_id_t dev_id); 57 | 58 | } // namespace pibmv2 59 | 60 | #endif // PI_BMV2_CONN_MGR_H_ 61 | -------------------------------------------------------------------------------- /targets/bmv2/cpu_send_recv.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | typedef struct pcap pcap_t; 32 | 33 | namespace pibmv2 { 34 | 35 | class CpuSendRecv { 36 | public: 37 | CpuSendRecv(); 38 | 39 | ~CpuSendRecv(); 40 | 41 | void start(); 42 | int add_device(const std::string &cpu_iface, pi_dev_id_t dev_id); 43 | int remove_device(pi_dev_id_t dev_id); 44 | 45 | int send_pkt(pi_dev_id_t dev_id, const char *pkt, size_t size); 46 | 47 | private: 48 | struct OneDevice { 49 | std::string cpu_iface; 50 | pi_dev_id_t dev_id; 51 | pcap_t *pcap; 52 | int fd; 53 | }; 54 | 55 | void recv_loop(); 56 | void recv_one(const OneDevice &device); 57 | 58 | fd_set fds; 59 | int max_fd{0}; 60 | std::vector devices{}; 61 | std::thread recv_thread{}; 62 | bool stop_recv_thread{false}; 63 | mutable std::mutex mutex{}; 64 | }; 65 | 66 | } // namespace pibmv2 67 | -------------------------------------------------------------------------------- /targets/bmv2/direct_res_spec.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_BMV2_DIRECT_RES_SPEC_H_ 23 | #define PI_BMV2_DIRECT_RES_SPEC_H_ 24 | 25 | #include 26 | 27 | #include 28 | 29 | #include "conn_mgr.h" 30 | 31 | namespace pibmv2 { 32 | 33 | BmCounterValue convert_from_counter_data(const pi_counter_data_t *from); 34 | 35 | std::vector convert_from_meter_spec( 36 | const pi_meter_spec_t *meter_spec); 37 | 38 | } // namespace pibmv2 39 | 40 | #endif // PI_BMV2_DIRECT_RES_SPEC_H_ 41 | -------------------------------------------------------------------------------- /targets/bmv2/m4/adl_recursive_eval.m4: -------------------------------------------------------------------------------- 1 | dnl adl_RECURSIVE_EVAL(VALUE, RESULT) 2 | dnl ================================= 3 | dnl Interpolate the VALUE in loop until it doesn't change, 4 | dnl and set the result to $RESULT. 5 | dnl WARNING: It's easy to get an infinite loop with some unsane input. 6 | AC_DEFUN([adl_RECURSIVE_EVAL], 7 | [_lcl_receval="$1" 8 | $2=`(test "x$prefix" = xNONE && prefix="$ac_default_prefix" 9 | test "x$exec_prefix" = xNONE && exec_prefix="${prefix}" 10 | _lcl_receval_old='' 11 | while test "[$]_lcl_receval_old" != "[$]_lcl_receval"; do 12 | _lcl_receval_old="[$]_lcl_receval" 13 | eval _lcl_receval="\"[$]_lcl_receval\"" 14 | done 15 | echo "[$]_lcl_receval")`]) 16 | -------------------------------------------------------------------------------- /targets/bmv2/pi_clone_imp.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | extern "C" { 27 | 28 | pi_status_t _pi_clone_session_set( 29 | pi_session_handle_t session_handle, 30 | pi_dev_tgt_t dev_tgt, 31 | pi_clone_session_id_t clone_session_id, 32 | const pi_clone_session_config_t *clone_session_config) { 33 | (void)session_handle; 34 | (void)dev_tgt; 35 | (void)clone_session_id; 36 | (void)clone_session_config; 37 | return PI_STATUS_NOT_IMPLEMENTED_BY_TARGET; 38 | } 39 | 40 | pi_status_t _pi_clone_session_reset( 41 | pi_session_handle_t session_handle, 42 | pi_dev_tgt_t dev_tgt, 43 | pi_clone_session_id_t clone_session_id) { 44 | (void)session_handle; 45 | (void)dev_tgt; 46 | (void)clone_session_id; 47 | return PI_STATUS_NOT_IMPLEMENTED_BY_TARGET; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /targets/bmv2/pi_learn_imp.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "common.h" 27 | 28 | extern "C" { 29 | 30 | pi_status_t _pi_learn_config_set(pi_session_handle_t session_handle, 31 | pi_dev_id_t dev_id, pi_p4_id_t learn_id, 32 | const pi_learn_config_t *config) { 33 | (void)session_handle; 34 | (void)dev_id; 35 | (void)learn_id; 36 | (void)config; 37 | return PI_STATUS_NOT_IMPLEMENTED_BY_TARGET; 38 | } 39 | 40 | pi_status_t _pi_learn_msg_ack(pi_session_handle_t session_handle, 41 | pi_dev_id_t dev_id, 42 | pi_p4_id_t learn_id, 43 | pi_learn_msg_id_t msg_id) { 44 | (void)session_handle; 45 | (void)dev_id; 46 | (void)learn_id; 47 | (void)msg_id; 48 | return PI_STATUS_NOT_IMPLEMENTED_BY_TARGET; 49 | } 50 | 51 | pi_status_t _pi_learn_msg_done(pi_learn_msg_t *msg) { 52 | (void)msg; 53 | return PI_STATUS_NOT_IMPLEMENTED_BY_TARGET; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /targets/dummy/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS = \ 2 | -I$(top_srcdir)/include \ 3 | -I$(top_srcdir)/lib \ 4 | -isystem$(top_srcdir)/third_party/uthash/src 5 | 6 | libpi_dummy_la_SOURCES = \ 7 | pi_imp.c \ 8 | pi_tables_imp.c \ 9 | pi_act_prof_imp.c \ 10 | pi_counter_imp.c \ 11 | pi_meter_imp.c \ 12 | pi_learn_imp.c \ 13 | pi_mc_imp.c \ 14 | pi_clone_imp.c \ 15 | func_counter.c \ 16 | func_counter.h 17 | 18 | lib_LTLIBRARIES = libpi_dummy.la 19 | -------------------------------------------------------------------------------- /targets/dummy/func_counter.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | void func_counter_init(); 23 | 24 | void func_counter_increment(const char *func_name); 25 | 26 | int func_counter_get(const char *func_name); 27 | 28 | // returns 0 if success, 1 otherwise 29 | int func_counter_dump_to_file(const char *path); 30 | 31 | void func_counter_destroy(); 32 | -------------------------------------------------------------------------------- /targets/dummy/pi_clone_imp.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "func_counter.h" 27 | 28 | pi_status_t _pi_clone_session_set( 29 | pi_session_handle_t session_handle, pi_dev_tgt_t dev_tgt, 30 | pi_clone_session_id_t clone_session_id, 31 | const pi_clone_session_config_t *clone_session_config) { 32 | (void)session_handle; 33 | (void)dev_tgt; 34 | (void)clone_session_id; 35 | (void)clone_session_config; 36 | func_counter_increment(__func__); 37 | return PI_STATUS_SUCCESS; 38 | } 39 | 40 | pi_status_t _pi_clone_session_reset(pi_session_handle_t session_handle, 41 | pi_dev_tgt_t dev_tgt, 42 | pi_clone_session_id_t clone_session_id) { 43 | (void)session_handle; 44 | (void)dev_tgt; 45 | (void)clone_session_id; 46 | func_counter_increment(__func__); 47 | return PI_STATUS_SUCCESS; 48 | } 49 | -------------------------------------------------------------------------------- /targets/dummy/pi_learn_imp.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | 24 | #include 25 | 26 | #include "func_counter.h" 27 | 28 | pi_status_t _pi_learn_config_set(pi_session_handle_t session_handle, 29 | pi_dev_id_t dev_id, pi_p4_id_t learn_id, 30 | const pi_learn_config_t *config) { 31 | (void)session_handle; 32 | (void)dev_id; 33 | (void)learn_id; 34 | (void)config; 35 | func_counter_increment(__func__); 36 | return PI_STATUS_SUCCESS; 37 | } 38 | 39 | pi_status_t _pi_learn_msg_ack(pi_session_handle_t session_handle, 40 | pi_dev_id_t dev_id, pi_p4_id_t learn_id, 41 | pi_learn_msg_id_t msg_id) { 42 | (void)session_handle; 43 | (void)dev_id; 44 | (void)learn_id; 45 | (void)msg_id; 46 | func_counter_increment(__func__); 47 | return PI_STATUS_SUCCESS; 48 | } 49 | 50 | pi_status_t _pi_learn_msg_done(pi_learn_msg_t *msg) { 51 | (void)msg; 52 | func_counter_increment(__func__); 53 | return PI_STATUS_SUCCESS; 54 | } 55 | -------------------------------------------------------------------------------- /targets/rpc/Makefile.am: -------------------------------------------------------------------------------- 1 | # a little hacky: the headers are in the parent of the configure subdir 2 | AM_CPPFLAGS += \ 3 | -I$(top_srcdir)/include 4 | 5 | libpi_rpc_la_SOURCES = \ 6 | pi_rpc.h \ 7 | pi_rpc.c \ 8 | pi_imp.c \ 9 | pi_tables_imp.c \ 10 | pi_act_prof_imp.c \ 11 | pi_counter_imp.c \ 12 | pi_meter_imp.c \ 13 | pi_learn_imp.c \ 14 | pi_mc_imp.c \ 15 | pi_clone_imp.c \ 16 | notifications.c 17 | 18 | libpi_rpc_la_LIBADD = \ 19 | $(top_builddir)/src/libpip4info.la 20 | 21 | lib_LTLIBRARIES = libpi_rpc.la 22 | -------------------------------------------------------------------------------- /targets/rpc/README.md: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /targets/rpc/pi_clone_imp.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | pi_status_t _pi_clone_session_set( 27 | pi_session_handle_t session_handle, pi_dev_tgt_t dev_tgt, 28 | pi_clone_session_id_t clone_session_id, 29 | const pi_clone_session_config_t *clone_session_config) { 30 | (void)session_handle; 31 | (void)dev_tgt; 32 | (void)clone_session_id; 33 | (void)clone_session_config; 34 | return PI_STATUS_RPC_NOT_IMPLEMENTED; 35 | } 36 | 37 | pi_status_t _pi_clone_session_reset(pi_session_handle_t session_handle, 38 | pi_dev_tgt_t dev_tgt, 39 | pi_clone_session_id_t clone_session_id) { 40 | (void)session_handle; 41 | (void)dev_tgt; 42 | (void)clone_session_id; 43 | return PI_STATUS_RPC_NOT_IMPLEMENTED; 44 | } 45 | -------------------------------------------------------------------------------- /targets/rpc/pi_rpc.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include "pi_rpc.h" 23 | 24 | char *rpc_addr = NULL; 25 | char *notifications_addr = NULL; 26 | 27 | pi_rpc_state_t state; 28 | 29 | pi_status_t retrieve_rep_hdr(const char *rep, pi_rpc_id_t req_id) { 30 | pi_rpc_id_t recv_id; 31 | pi_status_t recv_status; 32 | rep += retrieve_rpc_id(rep, &recv_id); 33 | if (recv_id != req_id) return PI_STATUS_RPC_TRANSPORT_ERROR; 34 | rep += retrieve_status(rep, &recv_status); 35 | 36 | return recv_status; 37 | } 38 | 39 | pi_status_t wait_for_status(pi_rpc_id_t req_id) { 40 | rep_hdr_t rep; 41 | int rc = nn_recv(state.s, &rep, sizeof(rep), 0); 42 | if (rc != sizeof(rep)) return PI_STATUS_RPC_TRANSPORT_ERROR; 43 | return retrieve_rep_hdr((char *)&rep, req_id); 44 | } 45 | 46 | size_t emit_req_hdr(char *hdr, pi_rpc_id_t id, pi_rpc_type_t type) { 47 | size_t s = 0; 48 | s += emit_rpc_id(hdr, id); 49 | s += emit_rpc_type(hdr + s, type); 50 | return s; 51 | } 52 | -------------------------------------------------------------------------------- /targets/rpc/pi_rpc.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_RPC_PI_RPC_H_ 23 | #define PI_RPC_PI_RPC_H_ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | typedef struct { 34 | int init; 35 | pi_rpc_id_t req_id; 36 | int s; 37 | } pi_rpc_state_t; 38 | 39 | extern char *rpc_addr; 40 | extern char *notifications_addr; 41 | 42 | extern pi_rpc_state_t state; 43 | 44 | pi_status_t retrieve_rep_hdr(const char *rep, pi_rpc_id_t req_id); 45 | 46 | pi_status_t wait_for_status(pi_rpc_id_t req_id); 47 | 48 | size_t emit_req_hdr(char *hdr, pi_rpc_id_t id, pi_rpc_type_t type); 49 | 50 | #endif // PI_RPC_PI_RPC_H_ 51 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | test* 2 | !*.c 3 | !testdata 4 | func_counter.txt 5 | -------------------------------------------------------------------------------- /tests/BUILD: -------------------------------------------------------------------------------- 1 | filegroup( 2 | name = "exported_testdata", 3 | srcs = glob(["testdata/*.txt", "testdata/*.json"]), 4 | visibility = ["//:__subpackages__"], 5 | ) -------------------------------------------------------------------------------- /tests/CLI/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !run_one_test.py.in 3 | !*.test 4 | !testdata 5 | !test.supp 6 | -------------------------------------------------------------------------------- /tests/CLI/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | AM_TESTS_ENVIRONMENT = export PI_TEST_WITH_VALGRIND=1; 4 | 5 | TESTS = \ 6 | table_dump.test \ 7 | table_dump_valid.test \ 8 | table_indirect.test \ 9 | table_wkey.test \ 10 | device_commands.test \ 11 | counter.test \ 12 | counter_direct.test \ 13 | meter.test \ 14 | meter_direct.test \ 15 | counter_and_meter_direct.test \ 16 | direct_res_reset.test \ 17 | device_update.test \ 18 | act_prof.test 19 | 20 | # test_config.py.in included by default 21 | EXTRA_DIST = \ 22 | run_one_test.py \ 23 | test.supp \ 24 | table_dump.test \ 25 | testdata/table_dump.in \ 26 | testdata/table_dump.out \ 27 | testdata/simple_router.json \ 28 | table_dump_valid.test \ 29 | testdata/table_dump_valid.in \ 30 | testdata/table_dump_valid.out \ 31 | testdata/valid.json \ 32 | table_indirect.test \ 33 | testdata/table_indirect.in \ 34 | testdata/table_indirect.out \ 35 | testdata/ecmp.json \ 36 | table_wkey.test \ 37 | testdata/table_wkey.in \ 38 | testdata/table_wkey.out \ 39 | device_commands.test \ 40 | testdata/device_commands.in \ 41 | testdata/device_commands.out \ 42 | act_prof.test \ 43 | testdata/act_prof.in \ 44 | testdata/act_prof.out 45 | 46 | EXTRA_DIST += \ 47 | testdata/stats.json \ 48 | counter.test \ 49 | testdata/counter.in \ 50 | testdata/counter.out \ 51 | counter_direct.test \ 52 | testdata/counter_direct.in \ 53 | testdata/counter_direct.out \ 54 | meter.test \ 55 | testdata/meter.in \ 56 | testdata/meter.out \ 57 | meter_direct.test \ 58 | testdata/meter_direct.in \ 59 | testdata/meter_direct.out \ 60 | counter_and_meter_direct.test \ 61 | testdata/counter_and_meter_direct.in \ 62 | testdata/counter_and_meter_direct.out \ 63 | direct_res_reset.test \ 64 | testdata/direct_res_reset.in \ 65 | testdata/direct_res_reset.out 66 | 67 | EXTRA_DIST += \ 68 | device_update.test \ 69 | testdata/device_update.in \ 70 | testdata/device_update.out \ 71 | testdata/swap_1.json \ 72 | testdata/swap_2.json 73 | -------------------------------------------------------------------------------- /tests/CLI/act_prof.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata act_prof ecmp.json 3 | -------------------------------------------------------------------------------- /tests/CLI/counter.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata counter stats.json 3 | -------------------------------------------------------------------------------- /tests/CLI/counter_and_meter_direct.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata counter_and_meter_direct stats.json 3 | -------------------------------------------------------------------------------- /tests/CLI/counter_direct.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata counter_direct stats.json 3 | -------------------------------------------------------------------------------- /tests/CLI/device_commands.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata device_commands simple_router.json 3 | -------------------------------------------------------------------------------- /tests/CLI/device_update.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata device_update swap_1.json 3 | -------------------------------------------------------------------------------- /tests/CLI/direct_res_reset.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata direct_res_reset stats.json 3 | -------------------------------------------------------------------------------- /tests/CLI/meter.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata meter stats.json 3 | -------------------------------------------------------------------------------- /tests/CLI/meter_direct.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata meter_direct stats.json 3 | -------------------------------------------------------------------------------- /tests/CLI/table_dump.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata table_dump simple_router.json 3 | -------------------------------------------------------------------------------- /tests/CLI/table_dump_valid.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata table_dump_valid valid.json 3 | -------------------------------------------------------------------------------- /tests/CLI/table_indirect.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata table_indirect ecmp.json 3 | -------------------------------------------------------------------------------- /tests/CLI/table_wkey.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata table_wkey simple_router.json 3 | -------------------------------------------------------------------------------- /tests/CLI/test.supp: -------------------------------------------------------------------------------- 1 | { 2 | 3 | Memcheck:Free 4 | fun:free 5 | fun:__libc_freeres 6 | fun:_vgnU_freeres 7 | } 8 | -------------------------------------------------------------------------------- /tests/CLI/testdata/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !*.in 3 | !*.json 4 | !*.p4 5 | !*.out 6 | -------------------------------------------------------------------------------- /tests/CLI/testdata/act_prof.in: -------------------------------------------------------------------------------- 1 | act_prof_create_member ecmp_action_profile set_nhop 10.0.0.1 7 2 | act_prof_create_group ecmp_action_profile 3 | act_prof_add_member_to_group ecmp_action_profile 0 9223372036854775808 4 | act_prof_dump ecmp_action_profile 5 | -------------------------------------------------------------------------------- /tests/CLI/testdata/act_prof.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Member was successfully created with handle 0. 6 | ???? 7 | Group was successfully created with handle 9223372036854775808. 8 | ???? 9 | Member was successfully added to group. 10 | ???? 11 | Successfully retrieved 1 member(s) and 1 group(s). 12 | ========== 13 | MEMBERS 14 | ********** 15 | Dumping member 0 16 | Action entry: set_nhop - 0a000001, 0007 17 | ========== 18 | GROUPS 19 | ********** 20 | Dumping group 9223372036854775808 21 | Members: [0] 22 | ???? 23 | -------------------------------------------------------------------------------- /tests/CLI/testdata/counter.in: -------------------------------------------------------------------------------- 1 | counter_reset CounterA 9 2 | counter_read CounterA 9 3 | counter_write CounterA 9 packets=7 bytes=9993 4 | counter_read CounterA 9 5 | counter_write CounterA 9 packets=12 6 | counter_read CounterA 9 7 | counter_reset CounterA 9 8 | counter_read CounterA 9 9 | -------------------------------------------------------------------------------- /tests/CLI/testdata/counter.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | ???? 6 | Dumping counter entry: 7 | Packets: 0 8 | Bytes: 0 9 | ???? 10 | Dumping counter entry: 11 | Packets: 7 12 | Bytes: 9993 13 | ???? 14 | Dumping counter entry: 15 | Packets: 7 16 | Bytes: 9993 17 | ???? 18 | Dumping counter entry: 19 | Packets: 12 20 | ???? 21 | Dumping counter entry: 22 | Packets: 12 23 | Bytes: 9993 24 | ???? 25 | ???? 26 | Dumping counter entry: 27 | Packets: 0 28 | Bytes: 0 29 | ???? 30 | -------------------------------------------------------------------------------- /tests/CLI/testdata/counter_and_meter_direct.in: -------------------------------------------------------------------------------- 1 | counter_write ExactOne_counter NEXT_ENTRY bytes=388 2 | meter_set ExactOne_meter NEXT_ENTRY 1:1 2:3 3 | table_add ExactOne 0 => actionA 0 4 | counter_read ExactOne_counter 0 5 | meter_read_spec ExactOne_meter 0 6 | counter_write ExactOne_counter NEXT_ENTRY bytes=99 packets=2 7 | table_modify ExactOne 0 => actionA 0 8 | counter_read ExactOne_counter 0 9 | meter_read_spec ExactOne_meter 0 10 | -------------------------------------------------------------------------------- /tests/CLI/testdata/counter_and_meter_direct.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Dumping counter entry: 6 | Bytes: 388 7 | Direct resource spec was stored. 8 | ???? 9 | Dumping meter spec: 10 | Meter unit: bytes 11 | Meter type: color unaware 12 | Committed information rate: 1 13 | Committed burst size: 1 14 | Peak information rate: 2 15 | Peak burst size: 3 16 | Direct resource spec was stored. 17 | ???? 18 | Entry was successfully added with handle 0. 19 | ???? 20 | Dumping counter entry: 21 | Packets: 0 22 | Bytes: 388 23 | ???? 24 | Dumping meter spec: 25 | Meter unit: bytes 26 | Meter type: color unaware 27 | Committed information rate: 1 28 | Committed burst size: 1 29 | Peak information rate: 2 30 | Peak burst size: 3 31 | ???? 32 | Dumping counter entry: 33 | Packets: 2 34 | Bytes: 99 35 | Direct resource spec was stored. 36 | ???? 37 | Entry with handle 0 was successfully modified. 38 | ???? 39 | Dumping counter entry: 40 | Packets: 2 41 | Bytes: 99 42 | ???? 43 | Dumping meter spec: 44 | Meter unit: bytes 45 | Meter type: color unaware 46 | Committed information rate: 1 47 | Committed burst size: 1 48 | Peak information rate: 2 49 | Peak burst size: 3 50 | ???? 51 | -------------------------------------------------------------------------------- /tests/CLI/testdata/counter_direct.in: -------------------------------------------------------------------------------- 1 | counter_write ExactOne_counter NEXT_ENTRY bytes=388 2 | table_add ExactOne 0 => actionA 0 3 | counter_read ExactOne_counter 0 4 | counter_reset ExactOne_counter 0 5 | counter_read ExactOne_counter 0 6 | counter_write ExactOne_counter 0 packets=7 bytes=9993 7 | counter_read ExactOne_counter 0 8 | counter_write ExactOne_counter 0 packets=12 9 | counter_read ExactOne_counter 0 10 | counter_reset ExactOne_counter 0 11 | counter_read ExactOne_counter 0 12 | -------------------------------------------------------------------------------- /tests/CLI/testdata/counter_direct.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Dumping counter entry: 6 | Bytes: 388 7 | Direct resource spec was stored. 8 | ???? 9 | Entry was successfully added with handle 0. 10 | ???? 11 | Dumping counter entry: 12 | Packets: 0 13 | Bytes: 388 14 | ???? 15 | ???? 16 | Dumping counter entry: 17 | Packets: 0 18 | Bytes: 0 19 | ???? 20 | Dumping counter entry: 21 | Packets: 7 22 | Bytes: 9993 23 | ???? 24 | Dumping counter entry: 25 | Packets: 7 26 | Bytes: 9993 27 | ???? 28 | Dumping counter entry: 29 | Packets: 12 30 | ???? 31 | Dumping counter entry: 32 | Packets: 12 33 | Bytes: 9993 34 | ???? 35 | ???? 36 | Dumping counter entry: 37 | Packets: 0 38 | Bytes: 0 39 | ???? 40 | -------------------------------------------------------------------------------- /tests/CLI/testdata/device_commands.in: -------------------------------------------------------------------------------- 1 | show_devices 2 | remove_device 8589934592 3 | show_devices 4 | -------------------------------------------------------------------------------- /tests/CLI/testdata/device_commands.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Showing devices: 6 | 8589934592 (selected) 7 | ???? 8 | Device removed successfully. 9 | ???? 10 | Showing devices: 11 | ???? 12 | -------------------------------------------------------------------------------- /tests/CLI/testdata/device_update.in: -------------------------------------------------------------------------------- 1 | add_p4 swap_2.json bmv2 2 | table_dump t_ingress_1 3 | table_dump t_ingress_2 4 | update_device_start 1 swap_2.json 5 | table_dump t_ingress_1 6 | table_dump t_ingress_2 7 | update_device_end 8 | table_dump t_ingress_1 9 | table_dump t_ingress_2 10 | -------------------------------------------------------------------------------- /tests/CLI/testdata/device_update.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | P4 config added with id 1 6 | ???? 7 | Successfully retrieved 0 entrie(s). 8 | ========== 9 | TABLE ENTRIES 10 | ========== 11 | Dumping default entry 12 | EMPTY 13 | ========== 14 | ???? 15 | ???? 16 | Device update started. 17 | ???? 18 | ???? 19 | Successfully retrieved 0 entrie(s). 20 | ========== 21 | TABLE ENTRIES 22 | ========== 23 | Dumping default entry 24 | EMPTY 25 | ========== 26 | ???? 27 | Device update done. 28 | ???? 29 | ???? 30 | Successfully retrieved 0 entrie(s). 31 | ========== 32 | TABLE ENTRIES 33 | ========== 34 | Dumping default entry 35 | EMPTY 36 | ========== 37 | ???? 38 | -------------------------------------------------------------------------------- /tests/CLI/testdata/direct_res_reset.in: -------------------------------------------------------------------------------- 1 | counter_write ExactOne_counter NEXT_ENTRY bytes=388 2 | direct_res_reset 3 | table_add ExactOne 0 => actionA 0 4 | counter_read ExactOne_counter 0 5 | -------------------------------------------------------------------------------- /tests/CLI/testdata/direct_res_reset.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Dumping counter entry: 6 | Bytes: 388 7 | Direct resource spec was stored. 8 | ???? 9 | ???? 10 | Entry was successfully added with handle 0. 11 | ???? 12 | Dumping counter entry: 13 | Packets: 0 14 | Bytes: 0 15 | ???? 16 | -------------------------------------------------------------------------------- /tests/CLI/testdata/ecmp.json: -------------------------------------------------------------------------------- 1 | ../../testdata/ecmp.json -------------------------------------------------------------------------------- /tests/CLI/testdata/meter.in: -------------------------------------------------------------------------------- 1 | meter_read_spec MeterA 9 2 | meter_set MeterA 9 1:3 2:12 3 | meter_read_spec MeterA 9 4 | meter_set MeterA 9 400:300 1000:300 5 | meter_read_spec MeterA 9 6 | -------------------------------------------------------------------------------- /tests/CLI/testdata/meter.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Error when trying to read meter spec 6 | ???? 7 | Dumping meter spec: 8 | Meter unit: bytes 9 | Meter type: color unaware 10 | Committed information rate: 1 11 | Committed burst size: 3 12 | Peak information rate: 2 13 | Peak burst size: 12 14 | ???? 15 | Dumping meter spec: 16 | Meter unit: bytes 17 | Meter type: color unaware 18 | Committed information rate: 1 19 | Committed burst size: 3 20 | Peak information rate: 2 21 | Peak burst size: 12 22 | ???? 23 | Dumping meter spec: 24 | Meter unit: bytes 25 | Meter type: color unaware 26 | Committed information rate: 400 27 | Committed burst size: 300 28 | Peak information rate: 1000 29 | Peak burst size: 300 30 | ???? 31 | Dumping meter spec: 32 | Meter unit: bytes 33 | Meter type: color unaware 34 | Committed information rate: 400 35 | Committed burst size: 300 36 | Peak information rate: 1000 37 | Peak burst size: 300 38 | ???? 39 | -------------------------------------------------------------------------------- /tests/CLI/testdata/meter_direct.in: -------------------------------------------------------------------------------- 1 | meter_read_spec ExactOne_meter 0 2 | meter_set ExactOne_meter NEXT_ENTRY 1:1 2:2 3 | table_add ExactOne 0 => actionA 0 4 | meter_read_spec ExactOne_meter 0 5 | meter_set ExactOne_meter 0 1:3 2:12 6 | meter_read_spec ExactOne_meter 0 7 | meter_set ExactOne_meter 0 400:300 1000:300 8 | meter_read_spec ExactOne_meter 0 9 | -------------------------------------------------------------------------------- /tests/CLI/testdata/meter_direct.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Error when trying to read meter spec 6 | ???? 7 | Dumping meter spec: 8 | Meter unit: bytes 9 | Meter type: color unaware 10 | Committed information rate: 1 11 | Committed burst size: 1 12 | Peak information rate: 2 13 | Peak burst size: 2 14 | Direct resource spec was stored. 15 | ???? 16 | Entry was successfully added with handle 0. 17 | ???? 18 | Dumping meter spec: 19 | Meter unit: bytes 20 | Meter type: color unaware 21 | Committed information rate: 1 22 | Committed burst size: 1 23 | Peak information rate: 2 24 | Peak burst size: 2 25 | ???? 26 | Dumping meter spec: 27 | Meter unit: bytes 28 | Meter type: color unaware 29 | Committed information rate: 1 30 | Committed burst size: 3 31 | Peak information rate: 2 32 | Peak burst size: 12 33 | ???? 34 | Dumping meter spec: 35 | Meter unit: bytes 36 | Meter type: color unaware 37 | Committed information rate: 1 38 | Committed burst size: 3 39 | Peak information rate: 2 40 | Peak burst size: 12 41 | ???? 42 | Dumping meter spec: 43 | Meter unit: bytes 44 | Meter type: color unaware 45 | Committed information rate: 400 46 | Committed burst size: 300 47 | Peak information rate: 1000 48 | Peak burst size: 300 49 | ???? 50 | Dumping meter spec: 51 | Meter unit: bytes 52 | Meter type: color unaware 53 | Committed information rate: 400 54 | Committed burst size: 300 55 | Peak information rate: 1000 56 | Peak burst size: 300 57 | ???? 58 | -------------------------------------------------------------------------------- /tests/CLI/testdata/simple_router.json: -------------------------------------------------------------------------------- 1 | ../../testdata/simple_router.json -------------------------------------------------------------------------------- /tests/CLI/testdata/stats.json: -------------------------------------------------------------------------------- 1 | ../../testdata/stats.json -------------------------------------------------------------------------------- /tests/CLI/testdata/swap_1.p4: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | header_type hdr1_t { 18 | fields { 19 | f1 : 8; 20 | f2 : 8; 21 | } 22 | } 23 | 24 | header hdr1_t hdr1; 25 | 26 | parser start { 27 | extract(hdr1); 28 | return ingress; 29 | } 30 | 31 | action a11() { 32 | modify_field(standard_metadata.egress_spec, 1); 33 | } 34 | 35 | action a12() { 36 | modify_field(standard_metadata.egress_spec, 2); 37 | } 38 | 39 | table t_ingress_1 { 40 | reads { 41 | hdr1.f1 : exact; 42 | } 43 | actions { 44 | a11; a12; 45 | } 46 | size : 128; 47 | } 48 | 49 | control ingress { 50 | apply(t_ingress_1); 51 | } 52 | 53 | control egress { 54 | } 55 | -------------------------------------------------------------------------------- /tests/CLI/testdata/swap_2.p4: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | header_type hdr2_t { 18 | fields { 19 | f1 : 8; 20 | f2 : 8; 21 | f3 : 16; 22 | } 23 | } 24 | 25 | header hdr2_t hdr2; 26 | 27 | parser start { 28 | extract(hdr2); 29 | return ingress; 30 | } 31 | 32 | action a21() { 33 | modify_field(standard_metadata.egress_spec, 3); 34 | } 35 | 36 | action a22() { 37 | modify_field(standard_metadata.egress_spec, 4); 38 | } 39 | 40 | table t_ingress_2 { 41 | reads { 42 | hdr2.f1 : exact; 43 | } 44 | actions { 45 | a21; a22; 46 | } 47 | size : 64; 48 | } 49 | 50 | control ingress { 51 | apply(t_ingress_2); 52 | } 53 | 54 | control egress { 55 | } 56 | -------------------------------------------------------------------------------- /tests/CLI/testdata/table_dump.in: -------------------------------------------------------------------------------- 1 | table_add ipv4_lpm 10.0.0.1/12 => set_nhop 10.0.0.1 13 2 | table_dump ipv4_lpm 3 | table_set_default ipv4_lpm set_nhop 8.8.8.8 9 4 | table_add ipv4_lpm 192.0.0.0/8 => set_nhop 192.168.0.1 4 5 | table_dump ipv4_lpm 6 | table_delete ipv4_lpm 0 7 | table_dump ipv4_lpm 8 | table_reset_default ipv4_lpm 9 | table_delete ipv4_lpm 1 10 | table_dump ipv4_lpm 11 | -------------------------------------------------------------------------------- /tests/CLI/testdata/table_dump.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Entry was successfully added with handle 0. 6 | ???? 7 | Successfully retrieved 1 entrie(s). 8 | ========== 9 | TABLE ENTRIES 10 | ********** 11 | Dumping entry 0 12 | Match key: 13 | * ipv4.dstAddr : LPM 0a000001/12 14 | Action entry: set_nhop - 0a000001, 0013 15 | ========== 16 | Dumping default entry 17 | EMPTY 18 | ========== 19 | ???? 20 | Default entry was successfully set. 21 | ???? 22 | Entry was successfully added with handle 1. 23 | ???? 24 | Successfully retrieved 2 entrie(s). 25 | ========== 26 | TABLE ENTRIES 27 | ********** 28 | Dumping entry 0 29 | Match key: 30 | * ipv4.dstAddr : LPM 0a000001/12 31 | Action entry: set_nhop - 0a000001, 0013 32 | ********** 33 | Dumping entry 1 34 | Match key: 35 | * ipv4.dstAddr : LPM c0000000/8 36 | Action entry: set_nhop - c0a80001, 0004 37 | ========== 38 | Dumping default entry 39 | Action entry: set_nhop - 08080808, 0009 40 | ========== 41 | ???? 42 | Entry with handle 0 was successfully removed. 43 | ???? 44 | Successfully retrieved 1 entrie(s). 45 | ========== 46 | TABLE ENTRIES 47 | ********** 48 | Dumping entry 1 49 | Match key: 50 | * ipv4.dstAddr : LPM c0000000/8 51 | Action entry: set_nhop - c0a80001, 0004 52 | ========== 53 | Dumping default entry 54 | Action entry: set_nhop - 08080808, 0009 55 | ========== 56 | ???? 57 | Default entry was successfully reset. 58 | ???? 59 | Entry with handle 1 was successfully removed. 60 | ???? 61 | Successfully retrieved 0 entrie(s). 62 | ========== 63 | TABLE ENTRIES 64 | ========== 65 | Dumping default entry 66 | EMPTY 67 | ========== 68 | ???? 69 | -------------------------------------------------------------------------------- /tests/CLI/testdata/table_dump_valid.in: -------------------------------------------------------------------------------- 1 | table_add t1 true 0 => noop 2 | table_dump t1 3 | -------------------------------------------------------------------------------- /tests/CLI/testdata/table_dump_valid.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Entry was successfully added with handle 0. 6 | ???? 7 | Successfully retrieved 1 entrie(s). 8 | ========== 9 | TABLE ENTRIES 10 | ********** 11 | Dumping entry 0 12 | Match key: 13 | * ethernet.$valid$ : VALID 01 14 | * h1.$valid$ : VALID 00 15 | Action entry: noop - 16 | ========== 17 | Dumping default entry 18 | EMPTY 19 | ========== 20 | ???? 21 | -------------------------------------------------------------------------------- /tests/CLI/testdata/table_indirect.in: -------------------------------------------------------------------------------- 1 | act_prof_create_member ecmp_action_profile set_nhop 10.0.0.1 7 2 | table_add ecmp_group 10.0.0.1/16 => 0 3 | act_prof_create_member ecmp_action_profile set_nhop 10.0.0.2 9 4 | table_modify ecmp_group 0 => 1 5 | table_set_default ecmp_group 0 6 | table_dump ecmp_group 7 | table_delete ecmp_group 0 8 | -------------------------------------------------------------------------------- /tests/CLI/testdata/table_indirect.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Member was successfully created with handle 0. 6 | ???? 7 | Entry was successfully added with handle 0. 8 | ???? 9 | Member was successfully created with handle 1. 10 | ???? 11 | Entry with handle 0 was successfully modified. 12 | ???? 13 | Default entry was successfully set. 14 | ???? 15 | Successfully retrieved 1 entrie(s). 16 | ========== 17 | TABLE ENTRIES 18 | ********** 19 | Dumping entry 0 20 | Match key: 21 | * ipv4.dstAddr : LPM 0a000001/16 22 | Indirect handle: 1 23 | ========== 24 | Dumping default entry 25 | Indirect handle: 0 26 | ========== 27 | ???? 28 | Entry with handle 0 was successfully removed. 29 | ???? 30 | -------------------------------------------------------------------------------- /tests/CLI/testdata/table_wkey.in: -------------------------------------------------------------------------------- 1 | table_add ipv4_lpm 10.0.0.1/12 => set_nhop 10.0.0.1 13 2 | table_dump ipv4_lpm 3 | table_delete_wkey ipv4_lpm 10.0.0.1/12 4 | table_dump ipv4_lpm 5 | table_add ipv4_lpm 10.0.0.1/12 => set_nhop 10.0.0.1 13 6 | table_dump ipv4_lpm 7 | table_modify_wkey ipv4_lpm 10.0.0.1/12 => set_nhop 10.0.0.2 99 8 | table_dump ipv4_lpm 9 | -------------------------------------------------------------------------------- /tests/CLI/testdata/table_wkey.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Device assigned successfully. 3 | Selecting device. 4 | ???? 5 | Entry was successfully added with handle 0. 6 | ???? 7 | Successfully retrieved 1 entrie(s). 8 | ========== 9 | TABLE ENTRIES 10 | ********** 11 | Dumping entry 0 12 | Match key: 13 | * ipv4.dstAddr : LPM 0a000001/12 14 | Action entry: set_nhop - 0a000001, 0013 15 | ========== 16 | Dumping default entry 17 | EMPTY 18 | ========== 19 | ???? 20 | Entry was successfully removed. 21 | ???? 22 | Successfully retrieved 0 entrie(s). 23 | ========== 24 | TABLE ENTRIES 25 | ========== 26 | Dumping default entry 27 | EMPTY 28 | ========== 29 | ???? 30 | Entry was successfully added with handle 16777216. 31 | ???? 32 | Successfully retrieved 1 entrie(s). 33 | ========== 34 | TABLE ENTRIES 35 | ********** 36 | Dumping entry 16777216 37 | Match key: 38 | * ipv4.dstAddr : LPM 0a000001/12 39 | Action entry: set_nhop - 0a000001, 0013 40 | ========== 41 | Dumping default entry 42 | EMPTY 43 | ========== 44 | ???? 45 | Entry was successfully modified. 46 | ???? 47 | Successfully retrieved 1 entrie(s). 48 | ========== 49 | TABLE ENTRIES 50 | ********** 51 | Dumping entry 16777216 52 | Match key: 53 | * ipv4.dstAddr : LPM 0a000001/12 54 | Action entry: set_nhop - 0a000002, 0099 55 | ========== 56 | Dumping default entry 57 | EMPTY 58 | ========== 59 | ???? 60 | -------------------------------------------------------------------------------- /tests/CLI/testdata/valid.json: -------------------------------------------------------------------------------- 1 | ../../testdata/valid.json -------------------------------------------------------------------------------- /tests/main.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #include "unity/unity_fixture.h" 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | extern void test_bmv2_json_reader(); 29 | extern void test_getnetv(); 30 | extern void test_p4info(); 31 | extern void test_frontends_generic(); 32 | 33 | static void run() { 34 | #ifdef TEST_BMV2_JSON_READER 35 | test_bmv2_json_reader(); 36 | #endif 37 | #ifdef TEST_GETNETV 38 | test_getnetv(); 39 | #endif 40 | #ifdef TEST_P4INFO 41 | test_p4info(); 42 | #endif 43 | #ifdef TEST_FRONTENDS_GENERIC 44 | test_frontends_generic(); 45 | #endif 46 | } 47 | 48 | int main(int argc, const char *argv[]) { 49 | unsigned int seed = (unsigned int)time(NULL); 50 | // TODO(antonin): do something more robust, maybe with getopt 51 | for (int i = 0; i < argc; i++) { 52 | if (strcmp(argv[i], "--seed") == 0) { 53 | seed = (unsigned int)atoi(argv[++i]); 54 | } 55 | } 56 | printf("Using seed %u for tests\n", seed); 57 | srand(seed); 58 | return UnityMain(argc, argv, run); 59 | } 60 | -------------------------------------------------------------------------------- /tests/testdata/act_prof.p4: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | action a() { } 18 | 19 | action_profile ap { actions { a; } size : 128; } 20 | 21 | table t1 { action_profile : ap; } 22 | table t2 { action_profile : ap; } 23 | 24 | control ingress { apply(t1); apply(t2); } 25 | 26 | parser start { return ingress; } 27 | -------------------------------------------------------------------------------- /tests/testdata/id_collision.json: -------------------------------------------------------------------------------- 1 | { 2 | "__meta__": { 3 | "version": [ 4 | 2, 5 | 0 6 | ] 7 | }, 8 | "header_types": [], 9 | "headers": [], 10 | "meter_arrays": [], 11 | "actions": [ 12 | { "name": "ae0ea", "id": 0, "runtime_data": [], "primitives": [], "pragmas": [] }, 13 | { "name": "bb6df", "id": 1, "runtime_data": [], "primitives": [], "pragmas": [] } 14 | ], 15 | "pipelines": [], 16 | "learn_lists": [], 17 | "counter_arrays": [], 18 | "register_arrays": [] 19 | } 20 | -------------------------------------------------------------------------------- /tests/testdata/reconcile_1.p4info.txt: -------------------------------------------------------------------------------- 1 | tables { 2 | preamble { 3 | id: 33582705 4 | name: "T1" 5 | alias: "T1" 6 | } 7 | match_fields { 8 | id: 1 9 | name: "mf1" 10 | bitwidth: 8 11 | match_type: EXACT 12 | } 13 | action_refs { 14 | id: 16783703 15 | } 16 | action_refs { 17 | id: 16800567 18 | annotations: "@defaultonly()" 19 | } 20 | size: 64 21 | } 22 | actions { 23 | preamble { 24 | id: 16800567 25 | name: "NoAction" 26 | alias: "NoAction" 27 | } 28 | } 29 | actions { 30 | preamble { 31 | id: 16783703 32 | name: "actionA" 33 | alias: "actionA" 34 | } 35 | params { 36 | id: 1 37 | name: "p1" 38 | bitwidth: 8 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/testdata/reconcile_2.p4info.txt: -------------------------------------------------------------------------------- 1 | tables { 2 | preamble { 3 | id: 33582705 4 | name: "T2" 5 | alias: "T2" 6 | } 7 | match_fields { 8 | id: 1 9 | name: "mf1" 10 | bitwidth: 8 11 | match_type: EXACT 12 | } 13 | action_refs { 14 | id: 16783703 15 | } 16 | action_refs { 17 | id: 16783704 18 | } 19 | action_refs { 20 | id: 16800567 21 | annotations: "@defaultonly()" 22 | } 23 | size: 64 24 | } 25 | actions { 26 | preamble { 27 | id: 16800567 28 | name: "NoAction" 29 | alias: "NoAction" 30 | } 31 | } 32 | actions { 33 | preamble { 34 | id: 16783703 35 | name: "actionA" 36 | alias: "actionA" 37 | } 38 | params { 39 | id: 1 40 | name: "p1" 41 | bitwidth: 8 42 | } 43 | } 44 | actions { 45 | preamble { 46 | id: 16783704 47 | name: "actionB" 48 | alias: "actionB" 49 | } 50 | params { 51 | id: 1 52 | name: "p1" 53 | bitwidth: 8 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/testdata/reconcile_3.p4info.txt: -------------------------------------------------------------------------------- 1 | tables { 2 | preamble { 3 | id: 33582705 4 | name: "T3" 5 | alias: "T3" 6 | } 7 | match_fields { 8 | id: 1 9 | name: "mf1" 10 | bitwidth: 8 11 | match_type: EXACT 12 | } 13 | action_refs { 14 | id: 16783703 15 | } 16 | action_refs { 17 | id: 16800567 18 | annotations: "@defaultonly()" 19 | } 20 | size: 64 21 | } 22 | actions { 23 | preamble { 24 | id: 16800567 25 | name: "NoAction" 26 | alias: "NoAction" 27 | } 28 | } 29 | actions { 30 | preamble { 31 | id: 16783703 32 | name: "actionA" 33 | alias: "actionA" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/testdata/unittest.entries.txt: -------------------------------------------------------------------------------- 1 | updates { 2 | type: INSERT 3 | entity { 4 | table_entry { 5 | table_id: 33618629 6 | match { 7 | field_id: 1 8 | exact { 9 | value: "\000\001" 10 | } 11 | } 12 | action { 13 | action { 14 | action_id: 16809468 15 | params { 16 | param_id: 1 17 | value: "\001" 18 | } 19 | } 20 | } 21 | } 22 | } 23 | } 24 | updates { 25 | type: INSERT 26 | entity { 27 | table_entry { 28 | table_id: 33618629 29 | match { 30 | field_id: 1 31 | exact { 32 | value: "\000\002" 33 | } 34 | } 35 | action { 36 | action { 37 | action_id: 16809468 38 | params { 39 | param_id: 1 40 | value: "\002" 41 | } 42 | } 43 | } 44 | } 45 | } 46 | } 47 | updates { 48 | type: INSERT 49 | entity { 50 | table_entry { 51 | table_id: 33618629 52 | match { 53 | field_id: 1 54 | exact { 55 | value: "\000\003" 56 | } 57 | } 58 | action { 59 | action { 60 | action_id: 16809468 61 | params { 62 | param_id: 1 63 | value: "\003" 64 | } 65 | } 66 | } 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /tests/testdata/valid.p4: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | // Standard L2 Ethernet header 17 | header_type ethernet_t { 18 | fields { 19 | dst_addr : 48; // width in bits 20 | src_addr : 48; 21 | ethertype : 16; 22 | } 23 | } 24 | 25 | header ethernet_t ethernet; 26 | 27 | header_type h1_t { 28 | fields { 29 | f1 : 32; 30 | } 31 | } 32 | 33 | header h1_t h1; 34 | 35 | parser start { 36 | extract(ethernet); 37 | extract(h1); 38 | return ingress; 39 | } 40 | 41 | action noop() { } 42 | 43 | table t1 { 44 | reads { 45 | ethernet : valid; 46 | h1.f1 : valid; 47 | } 48 | actions { 49 | noop; 50 | } 51 | } 52 | 53 | control ingress { 54 | apply(t1); 55 | } 56 | -------------------------------------------------------------------------------- /tests/utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /* 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef PI_TESTS_UTILS_H_ 23 | #define PI_TESTS_UTILS_H_ 24 | 25 | int cmp_cJSON(const char *json_str_1, const char *json_str_2); 26 | 27 | #endif // PI_TESTS_UTILS_H_ 28 | -------------------------------------------------------------------------------- /third_party/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//:__subpackages__"], 3 | ) 4 | 5 | cc_library( 6 | name = "piuthash", 7 | hdrs = glob(["uthash/src/*.h"]), 8 | includes = ["uthash/src"], 9 | deps = [], 10 | ) 11 | -------------------------------------------------------------------------------- /third_party/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | if WITH_GTEST 4 | include gtest.am 5 | endif 6 | 7 | include uthash.am 8 | 9 | SUBDIRS = cJSON unity 10 | -------------------------------------------------------------------------------- /third_party/cJSON/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//:__subpackages__"], 3 | ) 4 | 5 | licenses( 6 | ["notice"], # MIT 7 | ) 8 | 9 | cc_library( 10 | name = "picjson", 11 | srcs = glob(["src/cJSON.c"]), 12 | hdrs = glob(["include/cJSON/cJSON.h"]), 13 | includes = ["include", "include/cJSON"], 14 | deps = [], 15 | ) 16 | -------------------------------------------------------------------------------- /third_party/cJSON/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | 4 | noinst_LTLIBRARIES = libpicjson.la 5 | 6 | libpicjson_la_SOURCES = src/cJSON.c include/cJSON/cJSON.h 7 | libpicjson_la_LIBADD = -lm 8 | 9 | AM_CPPFLAGS = -I$(srcdir)/include/cJSON 10 | AM_CFLAGS = 11 | -------------------------------------------------------------------------------- /third_party/unity/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | noinst_LTLIBRARIES = libunity.la 4 | 5 | libunity_la_SOURCES = \ 6 | src/unity.c \ 7 | src/unity_fixture.c \ 8 | include/unity/unity.h \ 9 | include/unity/unity_fixture.h \ 10 | include/unity/unity_fixture_internals.h \ 11 | include/unity/unity_fixture_malloc_overrides.h \ 12 | include/unity/unity_internals.h 13 | 14 | AM_CPPFLAGS = -I$(srcdir)/include/unity 15 | -------------------------------------------------------------------------------- /third_party/unity/README.md: -------------------------------------------------------------------------------- 1 | See https://github.com/ThrowTheSwitch/Unity 2 | -------------------------------------------------------------------------------- /third_party/unity/include/unity/unity_fixture_internals.h: -------------------------------------------------------------------------------- 1 | //- Copyright (c) 2010 James Grenning and Contributed to Unity Project 2 | /* ========================================== 3 | Unity Project - A Test Framework for C 4 | Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams 5 | [Released under MIT License. Please refer to license.txt for details] 6 | ========================================== */ 7 | 8 | #ifndef UNITY_FIXTURE_INTERNALS_H_ 9 | #define UNITY_FIXTURE_INTERNALS_H_ 10 | 11 | struct _UnityFixture 12 | { 13 | int Verbose; 14 | unsigned int RepeatCount; 15 | const char* NameFilter; 16 | const char* GroupFilter; 17 | }; 18 | extern struct _UnityFixture UnityFixture; 19 | 20 | typedef void unityfunction(void); 21 | void UnityTestRunner(unityfunction* setup, 22 | unityfunction* body, 23 | unityfunction* teardown, 24 | const char* printableName, 25 | const char* group, 26 | const char* name, 27 | const char* file, unsigned int line); 28 | 29 | void UnityIgnoreTest(const char* printableName, const char* group, const char* name); 30 | void UnityMalloc_StartTest(void); 31 | void UnityMalloc_EndTest(void); 32 | int UnityGetCommandLineOptions(int argc, const char* argv[]); 33 | void UnityConcludeFixtureTest(void); 34 | 35 | void UnityPointer_Set(void** ptr, void* newValue, UNITY_LINE_TYPE line); 36 | void UnityPointer_UndoAllSets(void); 37 | void UnityPointer_Init(void); 38 | 39 | #endif /* UNITY_FIXTURE_INTERNALS_H_ */ 40 | -------------------------------------------------------------------------------- /third_party/unity/license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /third_party/uthash.am: -------------------------------------------------------------------------------- 1 | noinst_HEADERS = \ 2 | uthash/src/utarray.h \ 3 | uthash/src/uthash.h \ 4 | uthash/src/utlist.h \ 5 | uthash/src/utringbuffer.h \ 6 | uthash/src/utstack.h \ 7 | uthash/src/utstring.h 8 | -------------------------------------------------------------------------------- /tools/run_valgrind.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2013-present Barefoot Networks, Inc. 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | # Antonin Bas (antonin@barefootnetworks.com) 19 | 20 | THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 21 | 22 | TEST_DIR=$THIS_DIR/../tests 23 | 24 | cd $TEST_DIR 25 | 26 | libtool --mode=execute valgrind --leak-check=full --show-reachable=yes ./test_all 27 | return_status=$? 28 | 29 | cd - 30 | 31 | exit $return_status 32 | --------------------------------------------------------------------------------