├── .dockerignore ├── .github ├── dependabot.yml └── workflows │ ├── build.yml │ ├── doxygen.yml │ ├── lifecycle_management.yml │ └── test.yml ├── .gitignore ├── .gitmodules ├── CONTRIBUTING.md ├── CPPLINT.cfg ├── Dockerfile ├── Dockerfile.noPI ├── Doxyfile ├── Doxymain.md ├── LICENSE ├── Makefile.am ├── PI ├── Makefile.am ├── bm │ └── PI │ │ └── pi.h └── src │ ├── action_helpers.cpp │ ├── action_helpers.h │ ├── common.cpp │ ├── common.h │ ├── device_state.cpp │ ├── device_state.h │ ├── direct_res_spec.cpp │ ├── direct_res_spec.h │ ├── group_selection.cpp │ ├── group_selection.h │ ├── pi.cpp │ ├── 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 ├── README.md ├── VERSION ├── autogen.sh ├── ci ├── check-nnpy.py ├── codecov.sh ├── common.sh ├── install-nanomsg.sh ├── install-nnpy.sh └── install-thrift.sh ├── configure.ac ├── docs ├── JSON_format.md ├── p4dbg_user_guide.md ├── performance.md ├── runtime_CLI.md └── simple_switch.md ├── examples └── custom_extern │ ├── .gitignore │ ├── Makefile │ ├── README.md │ ├── example.p4 │ ├── extern_lib │ ├── Makefile │ ├── declaration.p4 │ └── definition.cpp │ ├── test.py │ └── tools ├── hooks ├── README.md └── build ├── include ├── Makefile.am └── bm │ ├── bm_apps │ ├── learn.h │ ├── notifications.h │ └── packet_pipe.h │ ├── bm_grpc │ └── pem.h │ ├── bm_runtime │ └── bm_runtime.h │ ├── bm_sim │ ├── CPPLINT.cfg │ ├── P4Objects.h │ ├── _assert.h │ ├── action_entry.h │ ├── action_profile.h │ ├── actions.h │ ├── ageing.h │ ├── bignum.h │ ├── bytecontainer.h │ ├── calculations.h │ ├── checksums.h │ ├── conditionals.h │ ├── context.h │ ├── control_action.h │ ├── control_flow.h │ ├── core │ │ ├── CPPLINT.cfg │ │ └── primitives.h │ ├── counters.h │ ├── data.h │ ├── debugger.h │ ├── deparser.h │ ├── dev_mgr.h │ ├── device_id.h │ ├── dynamic_bitset.h │ ├── entries.h │ ├── enums.h │ ├── event_logger.h │ ├── expressions.h │ ├── extern.h │ ├── field_lists.h │ ├── fields.h │ ├── handle_mgr.h │ ├── header_unions.h │ ├── headers.h │ ├── learning.h │ ├── logger.h │ ├── lookup_structures.h │ ├── match_error_codes.h │ ├── match_key_types.h │ ├── match_tables.h │ ├── match_units.h │ ├── meters.h │ ├── named_p4object.h │ ├── nn.h │ ├── options_parse.h │ ├── packet.h │ ├── packet_buffer.h │ ├── packet_handler.h │ ├── parser.h │ ├── parser_error.h │ ├── pcap_file.h │ ├── periodic_task.h │ ├── phv.h │ ├── phv_forward.h │ ├── phv_source.h │ ├── pipeline.h │ ├── port_monitor.h │ ├── pre.h │ ├── queue.h │ ├── queueing.h │ ├── ras.h │ ├── runtime_interface.h │ ├── short_alloc.h │ ├── simple_pre.h │ ├── simple_pre_lag.h │ ├── source_info.h │ ├── stacks.h │ ├── stateful.h │ ├── switch.h │ ├── tables.h │ ├── target_parser.h │ └── transport.h │ └── thrift │ └── stdcxx.h ├── install_deps.sh ├── install_deps_ubuntu_22.04.sh ├── m4 ├── ax_cxx_check_lib.m4 ├── ax_cxx_compile_stdcxx.m4 ├── ax_cxx_compile_stdcxx_17.m4 ├── ax_prefix_config_h.m4 └── ax_pthread.m4 ├── mininet ├── .gitignore ├── 1sw_demo.py ├── p4_mininet.py ├── simple_router.json ├── simple_router.p4 ├── stress_test_commands.txt └── stress_test_ipv4.py.in ├── pdfixed ├── Makefile.am ├── include │ ├── Makefile.am │ └── bm │ │ └── pdfixed │ │ ├── int │ │ ├── pd_conn_mgr.h │ │ ├── pd_helpers.h │ │ └── pd_notifications.h │ │ ├── pd_common.h │ │ ├── pd_mirroring.h │ │ ├── pd_pre.h │ │ ├── pd_static.h │ │ └── pd_swap.h ├── src │ ├── pd_helpers.cpp │ ├── pd_mirroring.cpp │ ├── pd_notifications.cpp │ ├── pd_pre.cpp │ ├── pd_static.cpp │ └── pd_swap.cpp ├── thrift-src │ ├── conn_mgr_pd_rpc_server.ipp │ ├── mc_pd_rpc_server.ipp │ ├── pdfixed_rpc_server.cpp │ ├── pdfixed_rpc_server.h │ └── sswitch_pd_rpc_server.ipp └── thrift │ ├── conn_mgr_pd_rpc.thrift │ ├── mc_pd_rpc.thrift │ ├── res.thrift │ └── sswitch_pd_rpc.thrift ├── services ├── .gitignore ├── CPPLINT.cfg ├── Makefile.am └── p4 │ └── bm │ └── dataplane_interface.proto ├── src ├── BMI │ ├── BMI │ │ └── bmi_port.h │ ├── Makefile.am │ ├── bmi_interface.c │ ├── bmi_interface.h │ └── bmi_port.c ├── Makefile.am ├── bf_lpm_trie │ ├── Makefile.am │ ├── bf_lpm_trie.c │ └── bf_lpm_trie │ │ └── bf_lpm_trie.h ├── bm_apps │ ├── CPPLINT.cfg │ ├── Makefile.am │ ├── examples │ │ ├── .gitignore │ │ ├── Makefile.am │ │ └── test_packet_pipe.cpp │ ├── learn.cpp │ ├── nn.h │ ├── notifications.cpp │ └── packet_pipe.cpp ├── bm_runtime │ ├── Makefile.am │ ├── SimplePreLAG_server.cpp │ ├── SimplePre_server.cpp │ ├── Standard_server.cpp │ └── server.cpp └── bm_sim │ ├── .gitignore │ ├── CPPLINT.cfg │ ├── Makefile.am │ ├── P4Objects.cpp │ ├── _assert.cpp │ ├── action_profile.cpp │ ├── actions.cpp │ ├── ageing.cpp │ ├── bytecontainer.cpp │ ├── calculations.cpp │ ├── checksums.cpp │ ├── conditionals.cpp │ ├── context.cpp │ ├── control_action.cpp │ ├── core │ ├── CPPLINT.cfg │ └── primitives.cpp │ ├── counters.cpp │ ├── crc_map.cpp │ ├── crc_map.h │ ├── crc_tables.h │ ├── debugger.cpp │ ├── deparser.cpp │ ├── dev_mgr.cpp │ ├── dev_mgr_bmi.cpp │ ├── dev_mgr_packet_in.cpp │ ├── enums.cpp │ ├── event_logger.cpp │ ├── expressions.cpp │ ├── extern.cpp │ ├── extract.h │ ├── fields.cpp │ ├── header_unions.cpp │ ├── headers.cpp │ ├── learning.cpp │ ├── logger.cpp │ ├── lookup_structures.cpp │ ├── lpm_trie.h │ ├── match_error_codes.cpp │ ├── match_tables.cpp │ ├── match_units.cpp │ ├── md5.c │ ├── md5.h │ ├── meters.cpp │ ├── options_parse.cpp │ ├── packet.cpp │ ├── parser.cpp │ ├── parser_error.cpp │ ├── pcap_file.cpp │ ├── periodic_task.cpp │ ├── phv.cpp │ ├── phv_source.cpp │ ├── pipeline.cpp │ ├── port_monitor.cpp │ ├── simple_pre.cpp │ ├── simple_pre_lag.cpp │ ├── source_info.cpp │ ├── stacks.cpp │ ├── stateful.cpp │ ├── switch.cpp │ ├── tables.cpp │ ├── target_parser.cpp │ ├── transport.cpp │ ├── transport_nn.cpp │ ├── utils.h │ ├── version.cpp.in │ ├── version.h │ ├── xxhash.c │ └── xxhash.h ├── targets ├── CPPLINT.cfg ├── Makefile.am ├── README.md ├── l2_switch │ ├── .gitignore │ ├── Makefile.am │ ├── commands.txt │ ├── l2_switch.cpp │ ├── l2_switch.json │ ├── l2_switch.p4 │ ├── learn_client │ │ ├── .gitignore │ │ ├── Makefile.am │ │ └── learn_client.cpp │ ├── primitives.cpp │ ├── run.sh │ └── runtime_CLI ├── pna_nic │ ├── .gitignore │ ├── CPPLINT.cfg │ ├── Makefile.am │ ├── externs │ │ ├── pna_counter.cpp │ │ ├── pna_counter.h │ │ ├── pna_hash.cpp │ │ ├── pna_hash.h │ │ ├── pna_internet_checksum.cpp │ │ ├── pna_internet_checksum.h │ │ ├── pna_meter.cpp │ │ ├── pna_meter.h │ │ ├── pna_random.cpp │ │ └── pna_random.h │ ├── main.cpp │ ├── pna_nic.cpp │ ├── pna_nic.h │ ├── pna_nic_CLI.in │ ├── pnic_CLI │ ├── pnic_CLI.py │ ├── primitives.cpp │ ├── runtime_CLI │ ├── tests │ │ ├── .gitignore │ │ ├── Makefile.am │ │ ├── main.cpp │ │ ├── test_parse_pna.cpp │ │ └── testdata │ │ │ └── pna-demo-L2-one-table.json │ └── thrift │ │ ├── pna_nic.thrift │ │ └── src │ │ └── PnaNic_server.cpp ├── psa_switch │ ├── .gitignore │ ├── CPPLINT.cfg │ ├── Makefile.am │ ├── externs │ │ ├── psa_counter.cpp │ │ ├── psa_counter.h │ │ ├── psa_hash.cpp │ │ ├── psa_hash.h │ │ ├── psa_internet_checksum.cpp │ │ ├── psa_internet_checksum.h │ │ ├── psa_meter.cpp │ │ ├── psa_meter.h │ │ ├── psa_random.cpp │ │ └── psa_random.h │ ├── main.cpp │ ├── primitives.cpp │ ├── psa_switch.cpp │ ├── psa_switch.h │ ├── psa_switch_CLI.in │ ├── pswitch_CLI │ ├── pswitch_CLI.py │ ├── runtime_CLI │ ├── tests │ │ ├── .gitignore │ │ ├── Makefile.am │ │ ├── main.cpp │ │ ├── test_hash.cpp │ │ └── test_internet_checksum.cpp │ └── thrift │ │ ├── psa_switch.thrift │ │ └── src │ │ └── PsaSwitch_server.cpp ├── simple_router │ ├── .gitignore │ ├── Makefile.am │ ├── commands.txt │ ├── primitives.cpp │ ├── run.sh │ ├── runtime_CLI │ ├── simple_router.cpp │ ├── simple_router.json │ ├── simple_router.p4 │ └── swap_test.txt ├── simple_switch │ ├── .gitignore │ ├── CPPLINT.cfg │ ├── Makefile.am │ ├── bm │ │ └── simple_switch │ │ │ └── runner.h │ ├── main.cpp │ ├── primitives.cpp │ ├── register_access.h │ ├── runner.cpp │ ├── runtime_CLI │ ├── simple_switch.cpp │ ├── simple_switch.h │ ├── simple_switch_CLI.in │ ├── sswitch_CLI │ ├── sswitch_CLI.py │ ├── tests │ │ ├── .gitignore │ │ ├── CLI_tests │ │ │ ├── .gitignore │ │ │ ├── Makefile.am │ │ │ ├── indirect_res.test │ │ │ ├── mc_dump.test │ │ │ ├── run_one_test.py.in │ │ │ ├── table_dump.test │ │ │ ├── table_dump_extra.test │ │ │ ├── table_dump_old.test │ │ │ └── testdata │ │ │ │ ├── .gitignore │ │ │ │ ├── ecmp.json │ │ │ │ ├── indirect_res.in │ │ │ │ ├── indirect_res.json │ │ │ │ ├── indirect_res.out │ │ │ │ ├── indirect_res.p4 │ │ │ │ ├── mc_dump.in │ │ │ │ ├── mc_dump.out │ │ │ │ ├── table_dump.in │ │ │ │ ├── table_dump.out │ │ │ │ ├── table_dump_extra.in │ │ │ │ ├── table_dump_extra.json │ │ │ │ ├── table_dump_extra.out │ │ │ │ ├── table_dump_extra.p4 │ │ │ │ ├── table_dump_old.in │ │ │ │ └── table_dump_old.out │ │ ├── Makefile.am │ │ ├── main.cpp │ │ ├── test_packet_redirect.cpp │ │ ├── test_parser_error.cpp │ │ ├── test_queueing.cpp │ │ ├── test_recirc.cpp │ │ ├── test_swap.cpp │ │ ├── test_truncate.cpp │ │ └── testdata │ │ │ ├── packet_redirect.json │ │ │ ├── packet_redirect.p4 │ │ │ ├── parser_error.json │ │ │ ├── parser_error.p4 │ │ │ ├── queueing.json │ │ │ ├── queueing.p4 │ │ │ ├── recirc.json │ │ │ ├── recirc.p4 │ │ │ ├── swap_1.json │ │ │ ├── swap_1.p4 │ │ │ ├── swap_2.json │ │ │ ├── swap_2.p4 │ │ │ ├── truncate.json │ │ │ └── truncate.p4 │ └── thrift │ │ ├── simple_switch.thrift │ │ └── src │ │ └── SimpleSwitch_server.cpp ├── simple_switch_grpc │ ├── .gitignore │ ├── Makefile.am │ ├── README.md │ ├── configure.ac │ ├── gnmi_sub_on_change.py │ ├── gnmi_sub_once.py │ ├── m4 │ │ ├── ax_cxx_compile_stdcxx.m4 │ │ ├── ax_cxx_compile_stdcxx_17.m4 │ │ └── ax_pthread.m4 │ ├── main.cpp │ ├── switch_runner.cpp │ ├── switch_runner.h │ ├── switch_sysrepo.cpp │ ├── switch_sysrepo.h │ └── tests │ │ ├── Makefile.am │ │ ├── base_test.cpp │ │ ├── base_test.h │ │ ├── example.cpp │ │ ├── example.run.in │ │ ├── main.cpp │ │ ├── test_action_profile.cpp │ │ ├── test_basic.cpp │ │ ├── test_counter.cpp │ │ ├── test_digest.cpp │ │ ├── test_gnmi.cpp │ │ ├── test_grpc_dp.cpp │ │ ├── test_idle_timeout.cpp │ │ ├── test_meter.cpp │ │ ├── test_optional.cpp │ │ ├── test_packet_io.cpp │ │ ├── test_pre.cpp │ │ ├── test_ternary.cpp │ │ ├── testdata │ │ ├── action_profile.json │ │ ├── action_profile.p4 │ │ ├── action_profile.proto.txt │ │ ├── clone.json │ │ ├── clone.p4 │ │ ├── clone.proto.txt │ │ ├── counter.json │ │ ├── counter.p4 │ │ ├── counter.proto.txt │ │ ├── digest.json │ │ ├── digest.p4 │ │ ├── digest.proto.txt │ │ ├── loopback.json │ │ ├── loopback.p4 │ │ ├── loopback.proto.txt │ │ ├── meter.json │ │ ├── meter.p4 │ │ ├── meter.proto.txt │ │ ├── multicast.json │ │ ├── multicast.p4 │ │ ├── multicast.proto.txt │ │ ├── optional.json │ │ ├── optional.p4 │ │ ├── optional.proto.txt │ │ ├── packet_io.json │ │ ├── packet_io.p4 │ │ ├── packet_io.proto.txt │ │ ├── simple_router.json │ │ ├── simple_router.proto.txt │ │ ├── ternary.json │ │ ├── ternary.p4 │ │ └── ternary.proto.txt │ │ ├── utils.cpp │ │ └── utils.h └── test_utils │ ├── Makefile.am │ ├── utils.cpp │ └── utils.h ├── tests ├── .gitignore ├── CPPLINT.cfg ├── Makefile.am ├── bmi_stubs.c ├── main.cpp ├── primitives.cpp ├── stress_tests │ ├── .gitignore │ ├── Makefile.am │ ├── gen_udp_tcp_traffic.py │ ├── get_stats.sh │ ├── stress_utils.cpp │ ├── stress_utils.h │ ├── test_LPM_match_1.cpp │ ├── test_exact_match_1.cpp │ ├── test_parser_deparser_1.cpp │ ├── test_ternary_match_1.cpp │ └── testdata │ │ ├── LPM_match_1.json │ │ ├── LPM_match_1.p4 │ │ ├── exact_match_1.json │ │ ├── exact_match_1.p4 │ │ ├── parser_deparser_1.json │ │ ├── parser_deparser_1.p4 │ │ ├── ternary_match_1.json │ │ ├── ternary_match_1.p4 │ │ └── udp_tcp_traffic.bin ├── test_actions.cpp ├── test_ageing.cpp ├── test_assert_assume.cpp ├── test_bm_apps.cpp ├── test_calculations.cpp ├── test_checksums.cpp ├── test_conditionals.cpp ├── test_control_flow.cpp ├── test_core_primitives.cpp ├── test_counters.cpp ├── test_data.cpp ├── test_devmgr.cpp ├── test_enums.cpp ├── test_expressions.cpp ├── test_extern.cpp ├── test_fields.cpp ├── test_handle_mgr.cpp ├── test_header_stacks.cpp ├── test_header_unions.cpp ├── test_headers.cpp ├── test_learning.cpp ├── test_log_msg.cpp ├── test_meters.cpp ├── test_p4objects.cpp ├── test_packet.cpp ├── test_parser.cpp ├── test_pcap.cpp ├── test_periodic_tasks.cpp ├── test_phv.cpp ├── test_pre.cpp ├── test_queue.cpp ├── test_queueing.cpp ├── test_ras.cpp ├── test_runtime_iface.cpp ├── test_stateful.cpp ├── test_switch.cpp ├── test_tables.cpp ├── test_target_parser.cpp ├── testdata │ ├── config_options.json │ ├── empty_config.json │ ├── empty_config.md5 │ ├── en0.pcap │ ├── header_stack.json │ ├── header_stack.p4 │ ├── lo0.pcap │ ├── logging.json │ ├── one_extern.json │ ├── one_header.json │ ├── parse_vset.json │ ├── parse_vset.p4 │ ├── pvs_struct_2.json │ ├── pvs_struct_2.p4 │ ├── runtime_iface.json │ ├── runtime_iface.p4 │ ├── serialize.json │ ├── serialize.p4 │ ├── unions_e2e_options_bos.json │ └── unions_e2e_options_count.json ├── utils.cpp.in ├── utils.h └── valgrind.test.supp ├── third_party ├── Makefile.am ├── gtest │ ├── Makefile.am │ ├── include │ │ └── gtest │ │ │ ├── gtest-assertion-result.h │ │ │ ├── gtest-death-test.h │ │ │ ├── gtest-matchers.h │ │ │ ├── gtest-message.h │ │ │ ├── gtest-param-test.h │ │ │ ├── gtest-printers.h │ │ │ ├── gtest-spi.h │ │ │ ├── gtest-test-part.h │ │ │ ├── gtest-typed-test.h │ │ │ ├── gtest.h │ │ │ ├── gtest_pred_impl.h │ │ │ ├── gtest_prod.h │ │ │ └── internal │ │ │ ├── custom │ │ │ ├── README.md │ │ │ ├── gtest-port.h │ │ │ ├── gtest-printers.h │ │ │ └── gtest.h │ │ │ ├── gtest-death-test-internal.h │ │ │ ├── gtest-filepath.h │ │ │ ├── gtest-internal.h │ │ │ ├── gtest-param-util.h │ │ │ ├── gtest-port-arch.h │ │ │ ├── gtest-port.h │ │ │ ├── gtest-string.h │ │ │ └── gtest-type-util.h │ └── src │ │ ├── gtest-all.cc │ │ ├── gtest-assertion-result.cc │ │ ├── gtest-death-test.cc │ │ ├── gtest-filepath.cc │ │ ├── gtest-internal-inl.h │ │ ├── gtest-matchers.cc │ │ ├── gtest-port.cc │ │ ├── gtest-printers.cc │ │ ├── gtest-test-part.cc │ │ ├── gtest-typed-test.cc │ │ ├── gtest.cc │ │ └── gtest_main.cc ├── jsoncpp │ ├── Makefile.am │ ├── include │ │ └── jsoncpp │ │ │ └── json.h │ └── src │ │ └── jsoncpp.cpp └── spdlog │ ├── Makefile.am │ └── bm │ └── spdlog │ ├── LICENSE │ ├── async_logger.h │ ├── common.h │ ├── details │ ├── async_log_helper.h │ ├── async_logger_impl.h │ ├── file_helper.h │ ├── format.cc │ ├── format.h │ ├── line_logger.h │ ├── log_msg.h │ ├── logger_impl.h │ ├── mpmc_bounded_q.h │ ├── null_mutex.h │ ├── os.h │ ├── pattern_formatter_impl.h │ ├── registry.h │ └── spdlog_impl.h │ ├── formatter.h │ ├── logger.h │ ├── sinks │ ├── android_sink.h │ ├── base_sink.h │ ├── dist_sink.h │ ├── file_sinks.h │ ├── null_sink.h │ ├── ostream_sink.h │ ├── sink.h │ ├── stdout_sinks.h │ └── syslog_sink.h │ ├── spdlog.h │ └── tweakme.h ├── thrift_src ├── .gitignore ├── Makefile.am ├── simple_pre.thrift ├── simple_pre_lag.thrift └── standard.thrift └── tools ├── .gitignore ├── Makefile.am ├── bm_CLI.in ├── bm_nanomsg_events.in ├── bm_p4dbg.in ├── bmpy_utils.py ├── check_style.sh ├── cpplint.py ├── get_version.sh ├── macos ├── bootstrap_mac.sh └── brews │ ├── nnpy.rb │ └── thrift.rb ├── nanomsg_client.py ├── p4dbg.py ├── run_valgrind.sh ├── runtime_CLI.py ├── veth_setup.sh └── veth_teardown.sh /.dockerignore: -------------------------------------------------------------------------------- 1 | **/.git 2 | **/.gitmodules 3 | .github/ 4 | **/Dockerfile 5 | **/*.md 6 | -------------------------------------------------------------------------------- /.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/doxygen.yml: -------------------------------------------------------------------------------- 1 | name: Doxygen 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | upload-doc: 10 | if: ${{ github.repository == 'p4lang/behavioral-model' && github.ref == 'refs/heads/main' }} 11 | name: Upload bmv2.org Doxygen documentation to S3 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v4 15 | - name: Run Doxygen 16 | run: | 17 | docker run --rm -v $(pwd):/data hrektts/doxygen doxygen 18 | - name: Upload to S3 19 | uses: jakejarvis/s3-sync-action@v0.5.1 20 | with: 21 | args: --acl public-read --follow-symlinks --delete 22 | env: 23 | AWS_S3_BUCKET: 'bmv2.org' 24 | AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} 25 | AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 26 | AWS_REGION: 'us-west-2' 27 | SOURCE_DIR: 'doxygen-out/html' 28 | -------------------------------------------------------------------------------- /.github/workflows/lifecycle_management.yml: -------------------------------------------------------------------------------- 1 | name: "Issues and PRs lifecycle management" 2 | on: 3 | schedule: 4 | # every day at midnight 5 | - cron: "0 0 * * *" 6 | 7 | jobs: 8 | stale: 9 | if: github.repository == 'p4lang/behavioral-model' 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/stale@v9 13 | with: 14 | repo-token: ${{ secrets.GITHUB_TOKEN }} 15 | stale-issue-message: 'This issue is stale because it has been open 180 days with no activity. Remove stale label or comment, or this will be closed in 180 days' 16 | stale-pr-message: 'This PR is stale because it has been open 180 days with no activity. Remove stale label or comment, or this will be closed in 180 days' 17 | stale-issue-label: 'lifecycle/stale' 18 | stale-pr-label: 'lifecycle/stale' 19 | days-before-stale: 180 20 | days-before-close: 180 21 | exempt-issue-labels: 'lifecycle/frozen' 22 | exempt-pr-labels: 'lifecycle/frozen' 23 | exempt-assignees: 'antoninbas' 24 | remove-stale-when-updated: true 25 | debug-only: false 26 | operations-per-run: 200 27 | start-date: '2020-01-01T00:00:00Z' 28 | skip: 29 | if: github.repository != 'p4lang/behavioral-model' 30 | runs-on: ubuntu-latest 31 | steps: 32 | - name: Skip 33 | run: | 34 | echo "Skipping lifecyle management because workflow cannot be run from fork" 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | 30 | # Python byte code 31 | *.pyc 32 | 33 | build/ 34 | lib* 35 | 36 | # Vim 37 | *.swp 38 | 39 | # Emacs 40 | *~ 41 | 42 | *.pcap 43 | 44 | # Tags 45 | GPATH 46 | GRTAGS 47 | GSYMS 48 | GTAGS 49 | 50 | tools/bm_runtime/ 51 | 52 | # Files generated by autotools 53 | .deps 54 | Makefile 55 | Makefile.in 56 | configure 57 | config.log 58 | config.h 59 | aclocal.m4 60 | autom4te.cache/ 61 | compile 62 | config.h.in 63 | config.status 64 | depcomp 65 | install-sh 66 | missing 67 | stamp-h1 68 | targets/simple_router/simple_router 69 | test-driver 70 | .dirstamp 71 | tests/test_* 72 | !tests/test_*.cpp 73 | !tests/testdata/*.pcap 74 | *.log 75 | *.trs 76 | config.guess 77 | config.sub 78 | ltmain.sh 79 | m4/ltoptions.m4 80 | m4/ltsugar.m4 81 | m4/ltversion.m4 82 | m4/lt~obsolete.m4 83 | py-compile 84 | .libs 85 | 86 | *.ts 87 | 88 | # gcov 89 | *.gcno 90 | *.gcda 91 | *.gcov 92 | 93 | # doxygen 94 | doxygen-out 95 | 96 | # thrift 97 | gen-cpp 98 | gen-py 99 | 100 | VERSION-build 101 | 102 | # dist archives 103 | *.tar.gz 104 | 105 | core 106 | vgcore* 107 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/behavioral-model/512caf07145762329a74494dee836e51a4af79e6/.gitmodules -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | You can fork the repo and submit a pull request in Github. For more information 2 | send us an email (p4-dev@lists.p4.org). 3 | 4 | ### Apache CLA 5 | 6 | All developers must sign the [P4.org](http://p4.org) CLA and return it to 7 | (membership@p4.org) before making contributions. The CLA is available 8 | [here](https://p4.org/assets/P4_Language_Consortium_Membership_Agreement.pdf). 9 | 10 | ### Coding guidelines 11 | 12 | Any contribution to the C++ core code (in particular the [bm_sim](src/bm_sim) 13 | module) must respect the coding guidelines. We rely heavily on the [Google C++ 14 | Style Guide](https://google.github.io/styleguide/cppguide.html), with some 15 | differences listed in this repository's 16 | [wiki](https://github.com/p4lang/behavioral-model/wiki/Coding-guidelines). 17 | Every submitted pull request will go through our CI tests, which include 18 | running `cpplint.py` to ensure correct style and formatting. 19 | You can run the following helper script to check code-style locally: 20 | 21 | ./tools/check_style.sh 22 | 23 | ### Building locally 24 | 25 | We recommend that you build with a recent C++ compiler and with `-Werror` since 26 | our CI tests use this flag. In order to build with the same flags as the CI 27 | tester, please run `configure` with the following flags: 28 | 29 | ./configure --with-pdfixed --with-pi --with-stress-tests --enable-debugger --enable-Werror 30 | -------------------------------------------------------------------------------- /CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | filter=-build/include_subdir 2 | filter=-build/c++11 3 | -------------------------------------------------------------------------------- /Doxymain.md: -------------------------------------------------------------------------------- 1 | # IMPLEMENTING YOUR SWITCH TARGET WITH BMv2 2 | 3 | This guide and the accompanying doxygen documentation are targetted at 4 | developpers that wish to design their own switch model using the building blocks 5 | exposed by bmv2. We have not documented every single method in doxygen; instead 6 | only the public methods which can help you design and implement your own model 7 | have been documented and are visible here. 8 | 9 | If you are not yet familiar with bmv2 (if you have never used bmv2 before), you 10 | should be starting with the [bmv2 README] 11 | (https://github.com/p4lang/behavioral-model). 12 | 13 | The bmv2 code already comes with 3 example targets: [simple_router] 14 | (https://github.com/p4lang/behavioral-model/tree/main/targets/simple_router), 15 | [l2_switch] 16 | (https://github.com/p4lang/behavioral-model/tree/main/targets/l2_switch) and 17 | [simple_switch] 18 | (https://github.com/p4lang/behavioral-model/tree/main/targets/simple_switch). 19 | simple_router is the smallest and simplest one, and I suggest starting with 20 | it. l2_switch introduces some additional complexity by including a packet 21 | replication engine (to support multicast). simple_switch is the standard P4 22 | target and although it includes a lot of functionality, the code is still 23 | relatively small and straightforward. 24 | 25 | As the bmv2 code becomes more stable, we intend to improve this documentation 26 | and include a step-by-step guide on how to design and implement your target 27 | using bmv2. I believe that a step-by-step guide recreating the implementation of 28 | the l2_switch target would be most useful. 29 | 30 | 31 | ## FAQ 32 | 33 | TODO 34 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | if COND_TARGETS 4 | MAYBE_TARGETS = targets 5 | endif 6 | 7 | if COND_PDFIXED 8 | MAYBE_PDFIXED = pdfixed 9 | endif 10 | 11 | if COND_THRIFT 12 | MAYBE_THRIFT = thrift_src 13 | MAYBE_TESTS = tests 14 | endif 15 | 16 | if COND_PI 17 | MAYBE_PI = PI services 18 | endif 19 | 20 | # simple_switch depends on libbmpi so PI needs to appear first 21 | SUBDIRS = $(MAYBE_THRIFT) third_party src include \ 22 | $(MAYBE_TESTS) $(MAYBE_PI) $(MAYBE_TARGETS) tools $(MAYBE_PDFIXED) 23 | 24 | # I am leaving all style-related files (cpplint) out of dist on purpose, maybe 25 | # will add them later if needed 26 | 27 | EXTRA_DIST = \ 28 | Doxyfile \ 29 | Doxymain.md \ 30 | LICENSE \ 31 | README.md \ 32 | install_deps.sh \ 33 | autogen.sh 34 | 35 | # 'ci' files, used by install_deps.sh to install some dependencies 36 | EXTRA_DIST += \ 37 | ci/check-nnpy.py \ 38 | ci/common.sh \ 39 | ci/install-nanomsg.sh \ 40 | ci/install-nnpy.sh \ 41 | ci/install-thrift.sh 42 | 43 | # mininet directory 44 | EXTRA_DIST += \ 45 | mininet/1sw_demo.py \ 46 | mininet/p4_mininet.py \ 47 | mininet/stress_test_ipv4.py.in \ 48 | mininet/simple_router.p4 \ 49 | mininet/simple_router.json \ 50 | mininet/stress_test_commands.txt 51 | 52 | # docs directory 53 | EXTRA_DIST += \ 54 | docs/JSON_format.md 55 | 56 | EXTRA_DIST += \ 57 | VERSION 58 | 59 | dist-hook: 60 | @if test -f $(srcdir)/VERSION-build; then \ 61 | cp $(srcdir)/VERSION-build $(distdir); \ 62 | else :; \ 63 | fi 64 | 65 | AM_DISTCHECK_CONFIGURE_FLAGS = --with-pdfixed --with-stress-tests 66 | -------------------------------------------------------------------------------- /PI/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | if COND_TARGETS 4 | AM_CPPFLAGS += -I$(top_srcdir)/targets/simple_switch -DWITH_SIMPLE_SWITCH 5 | endif 6 | 7 | libbmpi_la_SOURCES = \ 8 | src/pi.cpp \ 9 | src/pi_imp.cpp \ 10 | src/pi_tables_imp.cpp \ 11 | src/pi_act_prof_imp.cpp \ 12 | src/pi_counter_imp.cpp \ 13 | src/pi_meter_imp.cpp \ 14 | src/pi_learn_imp.cpp \ 15 | src/pi_mc_imp.cpp \ 16 | src/pi_clone_imp.cpp \ 17 | src/common.h \ 18 | src/common.cpp \ 19 | src/action_helpers.h \ 20 | src/action_helpers.cpp \ 21 | src/direct_res_spec.h \ 22 | src/direct_res_spec.cpp \ 23 | src/group_selection.h \ 24 | src/group_selection.cpp \ 25 | src/device_state.h \ 26 | src/device_state.cpp 27 | 28 | lib_LTLIBRARIES = libbmpi.la 29 | 30 | nobase_include_HEADERS = bm/PI/pi.h 31 | -------------------------------------------------------------------------------- /PI/bm/PI/pi.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 BM_PI_PI_H_ 22 | #define BM_PI_PI_H_ 23 | 24 | #include 25 | 26 | #include 27 | 28 | namespace bm { 29 | 30 | class SwitchWContexts; // forward declaration 31 | 32 | namespace pi { 33 | 34 | void register_switch(bm::SwitchWContexts *sw, uint32_t cpu_port = 0); 35 | 36 | pi_status_t table_idle_timeout_notify(pi_dev_id_t dev_id, pi_p4_id_t table_id, 37 | pi_entry_handle_t entry_handle); 38 | 39 | } // namespace pi 40 | 41 | } // namespace bm 42 | 43 | #endif // BM_PI_PI_H_ 44 | -------------------------------------------------------------------------------- /PI/src/action_helpers.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 SRC_ACTION_HELPERS_H_ 22 | #define SRC_ACTION_HELPERS_H_ 23 | 24 | #include 25 | 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | namespace pibmv2 { 32 | 33 | bm::ActionData build_action_data(const pi_action_data_t *action_data, 34 | const pi_p4info_t *p4info); 35 | 36 | char *dump_action_data(const pi_p4info_t *p4info, char *data, 37 | pi_p4_id_t action_id, const bm::ActionData &action_data); 38 | 39 | } // namespace pibmv2 40 | 41 | #endif // SRC_ACTION_HELPERS_H_ 42 | -------------------------------------------------------------------------------- /PI/src/common.cpp: -------------------------------------------------------------------------------- 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 "common.h" 22 | 23 | #include // for std::copy 24 | 25 | namespace pibmv2 { 26 | 27 | Buffer::Buffer(size_t capacity) { 28 | data_.reserve(capacity); 29 | } 30 | 31 | char * 32 | Buffer::extend(size_t s) { 33 | if (s == 0) return nullptr; 34 | const auto size = data_.size(); 35 | data_.resize(size + s); 36 | return &data_[size]; 37 | } 38 | 39 | char * 40 | Buffer::copy() const { 41 | char *res = new char[data_.size()]; 42 | std::copy(data_.begin(), data_.end(), res); 43 | return res; 44 | } 45 | 46 | char * 47 | Buffer::data() { 48 | return data_.data(); 49 | } 50 | 51 | size_t 52 | Buffer::size() const { 53 | return data_.size(); 54 | } 55 | 56 | } // namespace pibmv2 57 | -------------------------------------------------------------------------------- /PI/src/device_state.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019-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 "device_state.h" 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | #include "common.h" 29 | 30 | namespace pibmv2 { 31 | 32 | DeviceState::DeviceState(const pi_p4info_t *p4info) { 33 | for (auto act_prof_id = pi_p4info_act_prof_begin(p4info); 34 | act_prof_id != pi_p4info_act_prof_end(p4info); 35 | act_prof_id = pi_p4info_act_prof_next(p4info, act_prof_id)) { 36 | auto selector = std::make_shared(); 37 | act_prof_selection.emplace(act_prof_id, selector); 38 | const std::string name(pi_p4info_any_name_from_id(p4info, act_prof_id)); 39 | pibmv2::switch_->set_group_selector(0, name, selector); 40 | } 41 | } 42 | 43 | DeviceLock *device_lock = nullptr; 44 | DeviceState *device_state = nullptr; 45 | 46 | } // namespace pibmv2 47 | -------------------------------------------------------------------------------- /PI/src/direct_res_spec.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 SRC_DIRECT_RES_SPEC_H_ 22 | #define SRC_DIRECT_RES_SPEC_H_ 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | namespace pibmv2 { 30 | 31 | void convert_from_counter_data(const pi_counter_data_t *from, 32 | uint64_t *bytes, uint64_t *packets); 33 | 34 | void convert_to_counter_data(pi_counter_data_t *to, 35 | uint64_t bytes, uint64_t packets); 36 | 37 | std::vector convert_from_meter_spec( 38 | const pi_meter_spec_t *meter_spec); 39 | 40 | void convert_to_meter_spec(const pi_p4info_t *p4info, pi_p4_id_t m_id, 41 | pi_meter_spec_t *meter_spec, 42 | const std::vector &rates); 43 | 44 | } // namespace pibmv2 45 | 46 | #endif // SRC_DIRECT_RES_SPEC_H_ 47 | -------------------------------------------------------------------------------- /PI/src/pi.cpp: -------------------------------------------------------------------------------- 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 22 | 23 | #include "common.h" 24 | 25 | namespace pibmv2 { 26 | 27 | bm::SwitchWContexts *switch_ = nullptr; 28 | 29 | uint32_t cpu_port = 0; 30 | 31 | } // namespace pibmv2 32 | 33 | namespace bm { 34 | 35 | namespace pi { 36 | 37 | void register_switch(bm::SwitchWContexts *sw, uint32_t cpu_port) { 38 | ::pibmv2::switch_ = sw; 39 | ::pibmv2::cpu_port = cpu_port; 40 | } 41 | 42 | } // namespace pi 43 | 44 | } // namespace bm 45 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 1.15.0 -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # generates config.h.in 4 | autoheader 5 | 6 | autoreconf -fi 7 | -------------------------------------------------------------------------------- /ci/check-nnpy.py: -------------------------------------------------------------------------------- 1 | import nnpy 2 | import sys 3 | 4 | pub = nnpy.Socket(nnpy.AF_SP, nnpy.PUB) 5 | pub.bind('inproc://foo') 6 | 7 | sub = nnpy.Socket(nnpy.AF_SP, nnpy.SUB) 8 | sub.connect('inproc://foo') 9 | sub.setsockopt(nnpy.SUB, nnpy.SUB_SUBSCRIBE, '') 10 | 11 | pub.send('hello, world') 12 | recv = sub.recv() 13 | 14 | if recv != 'hello, world': 15 | sys.exit(1) 16 | -------------------------------------------------------------------------------- /ci/codecov.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ "$GCOV" != "" ]; then 4 | lcov --gcov-tool $GCOV --capture --directory src/bm_sim/.libs/ --output-file coverage.info 5 | lcov --remove coverage.info '/usr/*' --output-file coverage.info 6 | lcov --remove coverage.info 'third_party/*' --output-file coverage.info 7 | bash <(curl -s https://codecov.io/bash) -f coverage.info 8 | fi 9 | -------------------------------------------------------------------------------- /ci/common.sh: -------------------------------------------------------------------------------- 1 | force=0 2 | while [[ $# -gt 0 ]]; do 3 | key="$1" 4 | case $key in 5 | -f|--force) 6 | force=1 7 | ;; 8 | *) 9 | # unknown option 10 | ;; 11 | esac 12 | shift # past argument or value 13 | done 14 | 15 | function quit { 16 | if [ $force == 0 ]; then 17 | echo "skipping installation, you can force installation with '-f'" 18 | exit 0 19 | fi 20 | } 21 | 22 | function check_lib { 23 | ldconfig -p | grep $2 &> /dev/null 24 | if [ $? == 0 ]; then 25 | echo "$2 found" 26 | quit 27 | fi 28 | ldconfig -p | grep $1 &> /dev/null 29 | if [ $? == 0 ]; then 30 | echo "a version of $1 was found, but not $2" 31 | echo "you may experience issues when using a different version" 32 | quit 33 | fi 34 | } 35 | -------------------------------------------------------------------------------- /ci/install-nanomsg.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 4 | source $THIS_DIR/common.sh 5 | 6 | # nanomsg is very confusing in how it manages SOVERSION vs VERSION, but this 7 | # should be okay... (5.0.0 is the SOVERSION) 8 | check_lib libnanomsg libnanomsg.so.5.0.0 9 | 10 | set -e 11 | wget https://github.com/nanomsg/nanomsg/archive/1.0.0.tar.gz -O nanomsg-1.0.0.tar.gz 12 | tar -xzvf nanomsg-1.0.0.tar.gz 13 | cd nanomsg-1.0.0 14 | mkdir build 15 | cd build 16 | # I added -DCMAKE_INSTALL_PREFIX=/usr because on my Ubuntu 14.04 machine, the 17 | # library is installed in /usr/local/lib/x86_64-linux-gnu/ by default, and for 18 | # some reason ldconfig cannot find it 19 | cmake .. -DCMAKE_INSTALL_PREFIX=/usr 20 | cmake --build . 21 | sudo cmake --build . --target install 22 | cd .. 23 | -------------------------------------------------------------------------------- /ci/install-nnpy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | sudo pip3 install nnpy cffi 4 | -------------------------------------------------------------------------------- /ci/install-thrift.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 4 | source $THIS_DIR/common.sh 5 | 6 | check_lib libthrift libthrift-0.13.0 7 | 8 | set -e 9 | # Make it possible to get thrift in China 10 | # wget http://archive.apache.org/dist/thrift/0.13.0/thrift-0.13.0.tar.gz 11 | # tar -xzvf thrift-0.13.0.tar.gz 12 | git clone -b 0.13.0 https://github.com/apache/thrift.git thrift-0.13.0 13 | cd thrift-0.13.0 14 | ./bootstrap.sh 15 | ./configure --with-cpp=yes --with-c_glib=no --with-java=no --with-ruby=no --with-erlang=no --with-go=no --with-nodejs=no 16 | make -j2 && sudo make install 17 | cd lib/py 18 | sudo python3 setup.py install 19 | cd ../../.. 20 | -------------------------------------------------------------------------------- /examples/custom_extern/.gitignore: -------------------------------------------------------------------------------- 1 | *.json 2 | *.so 3 | !Makefile 4 | -------------------------------------------------------------------------------- /examples/custom_extern/Makefile: -------------------------------------------------------------------------------- 1 | P4_PROGRAM := example 2 | P4_JSON = $(P4_PROGRAM).json 3 | 4 | P4C = p4c-bm2-ss 5 | P4C_ARGS = --emit-externs 6 | 7 | EXTERN_DIR = extern_lib 8 | EXTERN_LIB = $(EXTERN_DIR)/definition.so 9 | 10 | .PHONY: build start test stop clean 11 | 12 | build: $(P4_JSON) $(EXTERN_LIB) 13 | 14 | %.json: %.p4 15 | $(P4C) $(P4C_ARGS) -o $@ $< 16 | 17 | $(EXTERN_LIB): 18 | cd $(EXTERN_DIR) && $(MAKE) 19 | 20 | start: build 21 | sudo tools/veth_setup.sh 22 | sudo simple_switch -i 0@veth0 -i 1@veth2 $(P4_JSON) \ 23 | -- --load-modules=$(EXTERN_LIB) 24 | 25 | test: 26 | sudo python3 test.py 27 | 28 | stop: 29 | sudo tools/veth_teardown.sh 30 | 31 | clean: 32 | rm -f $(P4_PROGRAM) 33 | cd $(EXTERN_DIR) && $(MAKE) clean 34 | -------------------------------------------------------------------------------- /examples/custom_extern/extern_lib/Makefile: -------------------------------------------------------------------------------- 1 | LIBS = definition.so 2 | 3 | CXX = g++ 4 | CXXFLAGS = -Wall -Wextra -g -O2 -fPIC 5 | LDFLAGS = -shared 6 | 7 | .PHONY: all clean 8 | 9 | all: $(LIBS) 10 | 11 | %.so: %.cpp 12 | $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $< 13 | 14 | clean: 15 | rm -f $(LIBS) 16 | -------------------------------------------------------------------------------- /examples/custom_extern/extern_lib/declaration.p4: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 P4lang Authors 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 | // Example custom extern function. 17 | extern void custom_set(inout T a, in T b); 18 | 19 | // Example custom extern object. 20 | extern CustomCounter { 21 | CustomCounter(T init_count); 22 | void reset(); 23 | void read(out T count); 24 | void increment_by(in T amount); 25 | } 26 | -------------------------------------------------------------------------------- /examples/custom_extern/extern_lib/definition.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 P4lang Authors 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 | #include 17 | 18 | // Example custom extern function. 19 | void custom_set(bm::Data & a, const bm::Data & b) { 20 | a.set(b); 21 | } 22 | BM_REGISTER_EXTERN_FUNCTION(custom_set, bm::Data &, const bm::Data &); 23 | 24 | // Example custom extern object. 25 | class CustomCounter : public bm::ExternType { 26 | public: 27 | BM_EXTERN_ATTRIBUTES { 28 | BM_EXTERN_ATTRIBUTE_ADD(init_count); 29 | } 30 | 31 | void init() override { 32 | reset(); 33 | } 34 | 35 | void reset() { 36 | count = init_count; 37 | } 38 | 39 | void read(bm::Data &count) const { 40 | count = this->count; 41 | } 42 | 43 | void increment_by(const bm::Data &amount) { 44 | count.set(count.get() + amount.get()); 45 | } 46 | 47 | private: 48 | bm::Data init_count{0}; 49 | bm::Data count{0}; 50 | }; 51 | BM_REGISTER_EXTERN(CustomCounter); 52 | BM_REGISTER_EXTERN_METHOD(CustomCounter, reset); 53 | BM_REGISTER_EXTERN_METHOD(CustomCounter, read, bm::Data &); 54 | BM_REGISTER_EXTERN_METHOD(CustomCounter, increment_by, const bm::Data &); 55 | -------------------------------------------------------------------------------- /examples/custom_extern/test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2022 P4lang Authors 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 | from scapy.all import Ether, Packet, XByteField 18 | from scapy.all import AsyncSniffer, sendp 19 | 20 | 21 | class TestPacket(Packet): 22 | name = "TestPacket" 23 | fields_desc = [ 24 | XByteField('a', 0), 25 | XByteField('b', 0), 26 | XByteField('c', 0), 27 | XByteField('d', 0), 28 | ] 29 | 30 | def report(pkt_sent, pkt_received): 31 | print() 32 | print('Packet sent:') 33 | pkt_sent.show2() 34 | print('Packet received:') 35 | pkt_received.show2() 36 | 37 | def main(): 38 | # Build the test packet. 39 | pkt = Ether() / TestPacket() 40 | 41 | # Prepare to sniff 1 packet. 42 | sniffer = AsyncSniffer( 43 | iface='veth3', 44 | count=1, 45 | prn=lambda pkt_received: report(pkt, pkt_received) 46 | ) 47 | sniffer.start() 48 | 49 | # Send the test packet. 50 | sendp(pkt, iface="veth1") 51 | 52 | # Wait until 1 packet is sniffed and report. 53 | sniffer.join() 54 | 55 | 56 | if __name__ == main(): 57 | main() 58 | -------------------------------------------------------------------------------- /examples/custom_extern/tools: -------------------------------------------------------------------------------- 1 | ../../tools -------------------------------------------------------------------------------- /hooks/README.md: -------------------------------------------------------------------------------- 1 | Hooks for dockerhub build. 2 | See [docker 3 | documentation](https://docs.docker.com/docker-cloud/builds/advanced/#override-build-test-or-push-commands). 4 | -------------------------------------------------------------------------------- /hooks/build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Overrides the default build command with one that sets PARENT_VERSION arg to 3 | # the appropriate value. behavioral-model:latest will be built from pi:latest, 4 | # while behavioral-model:stable will be built from pi:stable. Thanks to this we 5 | # don't have to duplicate the Dockerfile for the sake of dockerhub. 6 | 7 | # dockerhub uses DOCKER_TAG and not CACHE_TAG 8 | echo "DOCKER_TAG: $DOCKER_TAG" 9 | # echo "CACHE_TAG: $CACHE_TAG" 10 | # dockerhub seems to be using BUILD_PATH and not DOCKERFILE_PATH (which is 11 | # empty) 12 | echo "BUILD_PATH: $BUILD_PATH" 13 | # echo "DOCKERFILE_PATH: $DOCKERFILE_PATH" 14 | echo "IMAGE_NAME: $IMAGE_NAME" 15 | DOCKERFILE_PATH_OPTION= 16 | if [ "$BUILD_PATH" != "/" ]; then 17 | # remove leading '/' 18 | DOCKERFILE_PATH_OPTION="-f ${BUILD_PATH:1}" 19 | fi 20 | # Special case for release, since we do not have a release PI image at the 21 | # moment 22 | if [ "$DOCKER_TAG" = "release" ]; then 23 | PARENT_VERSION="latest" 24 | else 25 | PARENT_VERSION=$DOCKER_TAG 26 | fi 27 | docker build --build-arg PARENT_VERSION=$PARENT_VERSION $DOCKERFILE_PATH_OPTION -t $IMAGE_NAME . 28 | -------------------------------------------------------------------------------- /include/bm/bm_grpc/pem.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * Copyright 2022 VMware, Inc. 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 BM_BM_GRPC_PEM_H_ 18 | #define BM_BM_GRPC_PEM_H_ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | namespace bm { 27 | 28 | class read_pem_exception : public std::exception { 29 | public: 30 | read_pem_exception(const std::string &filename, const std::string &error) 31 | : filename(filename), error(error) { } 32 | 33 | std::string msg() const { 34 | std::stringstream ss; 35 | ss << "Error when reading pem file '" << filename << "': " << error << "\n"; 36 | return ss.str(); 37 | } 38 | 39 | const char *what() const noexcept override { 40 | return error.c_str(); 41 | } 42 | 43 | private: 44 | std::string filename; 45 | std::string error; 46 | }; 47 | 48 | std::string read_pem_file(const std::string &filename) { 49 | std::ifstream fs(filename, std::ios::in); 50 | if (!fs) { 51 | throw read_pem_exception(filename, "file cannot be opened"); 52 | } 53 | return std::string((std::istreambuf_iterator(fs)), 54 | std::istreambuf_iterator()); 55 | } 56 | 57 | } // namespace bm 58 | 59 | #endif // BM_BM_GRPC_PEM_H_ 60 | -------------------------------------------------------------------------------- /include/bm/bm_runtime/bm_runtime.h: -------------------------------------------------------------------------------- 1 | #ifndef _BM_RUNTIME_BM_RUNTIME_H_ 2 | #define _BM_RUNTIME_BM_RUNTIME_H_ 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace thrift_provider; 11 | using namespace thrift_provider::transport; 12 | using namespace thrift_provider::protocol; 13 | 14 | using ::stdcxx::shared_ptr; 15 | 16 | namespace bm_runtime { 17 | 18 | extern TMultiplexedProcessor *processor_; 19 | 20 | template 21 | int add_service(const std::string &service_name, 22 | shared_ptr handler) { 23 | processor_->registerProcessor(service_name, 24 | shared_ptr(new Processor(handler))); 25 | return 0; 26 | } 27 | 28 | int start_server(bm::SwitchWContexts *sw, int port); 29 | 30 | } 31 | 32 | #endif // _BM_RUNTIME_BM_RUNTIME_H_ 33 | -------------------------------------------------------------------------------- /include/bm/bm_sim/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | exclude_files=nn\.h 2 | exclude_files=short_alloc\.h 3 | -------------------------------------------------------------------------------- /include/bm/bm_sim/_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 BM_BM_SIM__ASSERT_H_ 22 | #define BM_BM_SIM__ASSERT_H_ 23 | 24 | // An assert that cannot be removed with NDEBUG 25 | 26 | namespace bm { 27 | 28 | [[ noreturn ]] void _bm_assert(const char* expr, const char* file, int line); 29 | 30 | } // namespace bm 31 | 32 | #define _BM_ASSERT(expr) \ 33 | ((expr) ? (void)0 : bm::_bm_assert(#expr, __FILE__, __LINE__)) 34 | 35 | #define _BM_UNREACHABLE(msg) bm::_bm_assert(msg, __FILE__, __LINE__) 36 | 37 | #define _BM_UNUSED(x) ((void)x) 38 | 39 | #endif // BM_BM_SIM__ASSERT_H_ 40 | -------------------------------------------------------------------------------- /include/bm/bm_sim/control_action.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 BM_BM_SIM_CONTROL_ACTION_H_ 22 | #define BM_BM_SIM_CONTROL_ACTION_H_ 23 | 24 | #include 25 | #include 26 | 27 | #include "control_flow.h" 28 | 29 | namespace bm { 30 | 31 | class Packet; 32 | class ActionFn; 33 | 34 | class ControlAction : public ControlFlowNode { 35 | public: 36 | ControlAction(const std::string &name, p4object_id_t id); 37 | 38 | ControlAction(const std::string &name, p4object_id_t id, 39 | std::unique_ptr source_info); 40 | 41 | void set_next_node(ControlFlowNode *next_node); 42 | 43 | void set_action(ActionFn *action); 44 | 45 | const ControlFlowNode *operator()(Packet *pkt) const override; 46 | 47 | private: 48 | ControlFlowNode *next_node{nullptr}; 49 | ActionFn *action; 50 | }; 51 | 52 | } // namespace bm 53 | 54 | #endif // BM_BM_SIM_CONTROL_ACTION_H_ 55 | -------------------------------------------------------------------------------- /include/bm/bm_sim/core/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | filter=-runtime/references 2 | -------------------------------------------------------------------------------- /include/bm/bm_sim/device_id.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 BM_BM_SIM_DEVICE_ID_H_ 22 | #define BM_BM_SIM_DEVICE_ID_H_ 23 | 24 | #include 25 | #include 26 | 27 | namespace bm { 28 | 29 | // s_* for serialized value format (e.g. notifications) 30 | using device_id_t = uint64_t; 31 | using s_device_id_t = device_id_t; 32 | using cxt_id_t = uint32_t; 33 | using s_cxt_id_t = cxt_id_t; 34 | 35 | } // namespace bm 36 | 37 | #endif // BM_BM_SIM_DEVICE_ID_H_ 38 | -------------------------------------------------------------------------------- /include/bm/bm_sim/match_error_codes.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 BM_BM_SIM_MATCH_ERROR_CODES_H_ 22 | #define BM_BM_SIM_MATCH_ERROR_CODES_H_ 23 | 24 | namespace bm { 25 | 26 | enum class MatchErrorCode { 27 | SUCCESS = 0, 28 | TABLE_FULL, 29 | INVALID_HANDLE, 30 | EXPIRED_HANDLE, 31 | COUNTERS_DISABLED, 32 | METERS_DISABLED, 33 | AGEING_DISABLED, 34 | INVALID_TABLE_NAME, 35 | INVALID_ACTION_NAME, 36 | WRONG_TABLE_TYPE, 37 | INVALID_MBR_HANDLE, 38 | MBR_STILL_USED, 39 | MBR_ALREADY_IN_GRP, 40 | MBR_NOT_IN_GRP, 41 | INVALID_GRP_HANDLE, 42 | GRP_STILL_USED, 43 | EMPTY_GRP, 44 | DUPLICATE_ENTRY, 45 | BAD_MATCH_KEY, 46 | INVALID_METER_OPERATION, 47 | DEFAULT_ACTION_IS_CONST, 48 | DEFAULT_ENTRY_IS_CONST, 49 | NO_DEFAULT_ENTRY, 50 | INVALID_ACTION_PROFILE_NAME, 51 | NO_ACTION_PROFILE_SELECTION, 52 | IMMUTABLE_TABLE_ENTRIES, 53 | BAD_ACTION_DATA, 54 | NO_TABLE_KEY, 55 | ERROR, 56 | }; 57 | 58 | const char *match_error_code_to_string(MatchErrorCode code); 59 | 60 | } // namespace bm 61 | 62 | #endif // BM_BM_SIM_MATCH_ERROR_CODES_H_ 63 | -------------------------------------------------------------------------------- /include/bm/bm_sim/packet_handler.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 | #ifndef BM_BM_SIM_PACKET_HANDLER_H_ 17 | #define BM_BM_SIM_PACKET_HANDLER_H_ 18 | 19 | #include 20 | 21 | namespace bm { 22 | 23 | class PacketDispatcherIface { 24 | public: 25 | using PacketHandler = std::function; 27 | enum class ReturnCode { 28 | SUCCESS, 29 | UNSUPPORTED, 30 | ERROR 31 | }; 32 | 33 | virtual ReturnCode set_packet_handler(const PacketHandler &handler, 34 | void* cookie) = 0; 35 | }; 36 | 37 | class PacketReceiverIface { 38 | public: 39 | virtual void send_packet(int port_num, const char* buffer, int len) = 0; 40 | }; 41 | 42 | } // namespace bm 43 | 44 | #endif // BM_BM_SIM_PACKET_HANDLER_H_ 45 | -------------------------------------------------------------------------------- /include/bm/bm_sim/phv_forward.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 BM_BM_SIM_PHV_FORWARD_H_ 22 | #define BM_BM_SIM_PHV_FORWARD_H_ 23 | 24 | namespace bm { 25 | 26 | using header_id_t = int; 27 | using header_stack_id_t = int; 28 | using header_union_id_t = int; 29 | using header_union_stack_id_t = int; 30 | 31 | class PHV; 32 | class PHVFactory; 33 | class Header; 34 | class Field; 35 | 36 | } // namespace bm 37 | 38 | #endif // BM_BM_SIM_PHV_FORWARD_H_ 39 | -------------------------------------------------------------------------------- /include/bm/bm_sim/phv_source.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 BM_BM_SIM_PHV_SOURCE_H_ 22 | #define BM_BM_SIM_PHV_SOURCE_H_ 23 | 24 | #include 25 | 26 | #include "device_id.h" 27 | #include "phv_forward.h" 28 | 29 | namespace bm { 30 | 31 | class PHVSourceIface { 32 | public: 33 | virtual ~PHVSourceIface() { } 34 | 35 | std::unique_ptr get(cxt_id_t cxt); 36 | 37 | void release(cxt_id_t cxt, std::unique_ptr phv); 38 | 39 | void set_phv_factory(cxt_id_t cxt, const PHVFactory *factory); 40 | 41 | size_t phvs_in_use(cxt_id_t cxt); 42 | 43 | static std::unique_ptr make_phv_source(size_t size = 1); 44 | 45 | private: 46 | virtual std::unique_ptr get_(cxt_id_t cxt) = 0; 47 | 48 | virtual void release_(cxt_id_t cxt, std::unique_ptr phv) = 0; 49 | 50 | virtual void set_phv_factory_(cxt_id_t cxt, const PHVFactory *factory) = 0; 51 | 52 | virtual size_t phvs_in_use_(cxt_id_t cxt) = 0; 53 | }; 54 | 55 | } // namespace bm 56 | 57 | #endif // BM_BM_SIM_PHV_SOURCE_H_ 58 | -------------------------------------------------------------------------------- /include/bm/bm_sim/pre.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 | * Srikrishna Gopu (krishna@barefootnetworks.com) 18 | * Antonin Bas (antonin@barefootnetworks.com) 19 | * 20 | */ 21 | 22 | #ifndef BM_BM_SIM_PRE_H_ 23 | #define BM_BM_SIM_PRE_H_ 24 | 25 | #include 26 | #include 27 | 28 | namespace bm { 29 | 30 | namespace McPre { 31 | 32 | template 33 | class Set { 34 | public: 35 | using reference = typename std::bitset::reference; 36 | 37 | public: 38 | constexpr Set() noexcept { } 39 | 40 | explicit Set(const std::string &str) 41 | : port_map(str) { } 42 | 43 | bool operator[] (size_t pos) const { return port_map[pos]; } 44 | reference operator[] (size_t pos) { return port_map[pos]; } 45 | 46 | constexpr size_t size() const noexcept { return port_map.size(); } 47 | 48 | private: 49 | std::bitset port_map{}; 50 | }; 51 | 52 | } // namespace McPre 53 | 54 | } // namespace bm 55 | 56 | #endif // BM_BM_SIM_PRE_H_ 57 | -------------------------------------------------------------------------------- /include/bm/thrift/stdcxx.h: -------------------------------------------------------------------------------- 1 | #ifndef _BM_STDCXX_H_ 2 | #define _BM_STDCXX_H_ 3 | 4 | #include 5 | 6 | namespace thrift_provider = apache::thrift; 7 | 8 | #if BM_THRIFT_VERSION < 1300 9 | #ifdef BM_HAVE_THRIFT_STDCXX_H 10 | #include 11 | namespace stdcxx = thrift_provider::stdcxx; 12 | #else 13 | namespace stdcxx = boost; 14 | #endif // BM_HAVE_THRIFT_STDCXX_H 15 | #else 16 | namespace stdcxx = std; 17 | #endif // BM_THRIFT_VERSION < 1300 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /install_deps.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | ubuntu_release=`lsb_release -s -r` 4 | sudo apt-get install -y \ 5 | automake \ 6 | cmake \ 7 | libgmp-dev \ 8 | libpcap-dev \ 9 | libboost-dev \ 10 | libboost-test-dev \ 11 | libboost-program-options-dev \ 12 | libboost-system-dev \ 13 | libboost-filesystem-dev \ 14 | libboost-thread-dev \ 15 | libevent-dev \ 16 | libtool \ 17 | flex \ 18 | bison \ 19 | pkg-config \ 20 | g++ \ 21 | libssl-dev \ 22 | libffi-dev \ 23 | python3-dev \ 24 | python3-pip \ 25 | wget 26 | 27 | tmpdir=`mktemp -d -p .` 28 | cd $tmpdir 29 | 30 | bash ../ci/install-thrift.sh 31 | bash ../ci/install-nanomsg.sh 32 | sudo ldconfig 33 | bash ../ci/install-nnpy.sh 34 | 35 | cd .. 36 | sudo rm -rf $tmpdir 37 | -------------------------------------------------------------------------------- /install_deps_ubuntu_22.04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | apt-get update 5 | apt-get install -y curl gnupg 6 | echo 'deb http://download.opensuse.org/repositories/home:/p4lang/xUbuntu_22.04/ /' | tee /etc/apt/sources.list.d/home:p4lang.list 7 | curl -fsSL https://download.opensuse.org/repositories/home:p4lang/xUbuntu_22.04/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_p4lang.gpg > /dev/null 8 | 9 | apt-get update 10 | 11 | apt-get install -qq --no-install-recommends \ 12 | automake \ 13 | build-essential \ 14 | cmake \ 15 | git \ 16 | g++ \ 17 | libboost-dev \ 18 | libboost-filesystem-dev \ 19 | libboost-program-options-dev \ 20 | libboost-system-dev \ 21 | libboost-thread-dev \ 22 | libgmp-dev \ 23 | libgrpc++-dev \ 24 | libgrpc-dev \ 25 | libopenmpi-dev \ 26 | libnanomsg-dev \ 27 | libpcap-dev \ 28 | libprotobuf-dev \ 29 | libprotoc-dev \ 30 | libssl-dev \ 31 | libthrift-0.16.0 \ 32 | libthrift-dev \ 33 | libtool \ 34 | pkg-config \ 35 | protobuf-compiler \ 36 | protobuf-compiler-grpc \ 37 | python3-all \ 38 | python3-six \ 39 | python3-thrift \ 40 | thrift-compiler \ 41 | libreadline-dev \ 42 | p4lang-pi 43 | 44 | ldconfig 45 | -------------------------------------------------------------------------------- /m4/ax_cxx_compile_stdcxx_17.m4: -------------------------------------------------------------------------------- 1 | # ============================================================================= 2 | # https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_17.html 3 | # ============================================================================= 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CXX_COMPILE_STDCXX_17([ext|noext], [mandatory|optional]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check for baseline language coverage in the compiler for the C++17 12 | # standard; if necessary, add switches to CXX and CXXCPP to enable 13 | # support. 14 | # 15 | # This macro is a convenience alias for calling the AX_CXX_COMPILE_STDCXX 16 | # macro with the version set to C++17. The two optional arguments are 17 | # forwarded literally as the second and third argument respectively. 18 | # Please see the documentation for the AX_CXX_COMPILE_STDCXX macro for 19 | # more information. If you want to use this macro, you also need to 20 | # download the ax_cxx_compile_stdcxx.m4 file. 21 | # 22 | # LICENSE 23 | # 24 | # Copyright (c) 2015 Moritz Klammler 25 | # Copyright (c) 2016 Krzesimir Nowak 26 | # 27 | # Copying and distribution of this file, with or without modification, are 28 | # permitted in any medium without royalty provided the copyright notice 29 | # and this notice are preserved. This file is offered as-is, without any 30 | # warranty. 31 | 32 | #serial 2 33 | 34 | AX_REQUIRE_DEFINED([AX_CXX_COMPILE_STDCXX]) 35 | AC_DEFUN([AX_CXX_COMPILE_STDCXX_17], [AX_CXX_COMPILE_STDCXX([17], [$1], [$2])]) 36 | -------------------------------------------------------------------------------- /mininet/.gitignore: -------------------------------------------------------------------------------- 1 | stress_test_ipv4.py 2 | -------------------------------------------------------------------------------- /mininet/stress_test_commands.txt: -------------------------------------------------------------------------------- 1 | table_set_default send_frame _drop 2 | table_set_default forward _drop 3 | table_set_default ipv4_lpm _drop 4 | table_add send_frame rewrite_mac 1 => 00:aa:bb:00:00:00 5 | table_add send_frame rewrite_mac 2 => 00:aa:bb:00:00:01 6 | table_add forward set_dmac 10.0.0.10 => 00:04:00:00:00:00 7 | table_add forward set_dmac 10.0.1.10 => 00:04:00:00:00:01 8 | table_add ipv4_lpm set_nhop 10.0.0.10/32 => 10.0.0.10 1 9 | table_add ipv4_lpm set_nhop 10.0.1.10/32 => 10.0.1.10 2 10 | -------------------------------------------------------------------------------- /pdfixed/include/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | pdf_headers = \ 4 | bm/pdfixed/pd_pre.h \ 5 | bm/pdfixed/pd_mirroring.h \ 6 | bm/pdfixed/pd_static.h \ 7 | bm/pdfixed/pd_common.h \ 8 | bm/pdfixed/pd_swap.h 9 | 10 | # these are needed when compiling the P4-dependent PD 11 | pdf_int_headers = \ 12 | bm/pdfixed/int/pd_conn_mgr.h \ 13 | bm/pdfixed/int/pd_helpers.h \ 14 | bm/pdfixed/int/pd_notifications.h 15 | 16 | nobase_include_HEADERS = $(pdf_headers) $(pdf_int_headers) 17 | -------------------------------------------------------------------------------- /pdfixed/include/bm/pdfixed/int/pd_helpers.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 BM_PDFIXED_INT_PD_HELPERS_H_ 22 | #define BM_PDFIXED_INT_PD_HELPERS_H_ 23 | 24 | #include 25 | 26 | #include 27 | 28 | #include 29 | 30 | using ::bm_runtime::standard::BmMeterRateConfig; 31 | 32 | std::vector 33 | pd_bytes_meter_spec_to_rates(p4_pd_bytes_meter_spec_t *meter_spec); 34 | 35 | std::vector 36 | pd_packets_meter_spec_to_rates(p4_pd_packets_meter_spec_t *meter_spec); 37 | 38 | #endif // BM_PDFIXED_INT_PD_HELPERS_H_ 39 | -------------------------------------------------------------------------------- /pdfixed/include/bm/pdfixed/int/pd_notifications.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 BM_PDFIXED_INT_PD_NOTIFICATIONS_H_ 22 | #define BM_PDFIXED_INT_PD_NOTIFICATIONS_H_ 23 | 24 | typedef void (*NotificationCb)(const char *hdr, const char *data); 25 | 26 | int pd_notifications_add_device(int dev_id, const char *notifications_addr, 27 | NotificationCb ageing_cb, 28 | NotificationCb learning_cb); 29 | 30 | int pd_notifications_remove_device(int dev_id); 31 | 32 | #endif // BM_PDFIXED_INT_PD_NOTIFICATIONS_H_ 33 | -------------------------------------------------------------------------------- /pdfixed/include/bm/pdfixed/pd_static.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 BM_PDFIXED_PD_STATIC_H_ 22 | #define BM_PDFIXED_PD_STATIC_H_ 23 | 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | p4_pd_status_t 31 | p4_pd_init(void); 32 | 33 | void 34 | p4_pd_cleanup(void); 35 | 36 | p4_pd_status_t 37 | p4_pd_client_init(p4_pd_sess_hdl_t *sess_hdl); 38 | 39 | p4_pd_status_t 40 | p4_pd_client_cleanup(p4_pd_sess_hdl_t sess_hdl); 41 | 42 | p4_pd_status_t 43 | p4_pd_begin_txn(p4_pd_sess_hdl_t shdl, bool isAtomic, bool isHighPri); 44 | 45 | p4_pd_status_t 46 | p4_pd_verify_txn(p4_pd_sess_hdl_t shdl); 47 | 48 | p4_pd_status_t 49 | p4_pd_abort_txn(p4_pd_sess_hdl_t shdl); 50 | 51 | p4_pd_status_t 52 | p4_pd_commit_txn(p4_pd_sess_hdl_t shdl, bool hwSynchronous); 53 | 54 | p4_pd_status_t 55 | p4_pd_complete_operations(p4_pd_sess_hdl_t shdl); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif // BM_PDFIXED_PD_STATIC_H_ 62 | -------------------------------------------------------------------------------- /pdfixed/include/bm/pdfixed/pd_swap.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 BM_PDFIXED_PD_SWAP_H_ 22 | #define BM_PDFIXED_PD_SWAP_H_ 23 | 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | p4_pd_status_t 31 | p4_pd_load_new_config(p4_pd_sess_hdl_t shdl, uint8_t dev_id, 32 | const char *config_str); 33 | 34 | p4_pd_status_t 35 | p4_pd_swap_configs(p4_pd_sess_hdl_t shdl, uint8_t dev_id); 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | 41 | #endif // BM_PDFIXED_PD_SWAP_H_ 42 | -------------------------------------------------------------------------------- /pdfixed/thrift-src/conn_mgr_pd_rpc_server.ipp: -------------------------------------------------------------------------------- 1 | #include "conn_mgr.h" 2 | #include 3 | 4 | #include 5 | 6 | using namespace ::conn_mgr_pd_rpc; 7 | using namespace ::res_pd_rpc; 8 | 9 | class conn_mgrHandler : virtual public conn_mgrIf { 10 | public: 11 | conn_mgrHandler() { 12 | 13 | } 14 | 15 | void echo(const std::string& s) { 16 | std::cerr << "Echo: " << s << std::endl; 17 | } 18 | 19 | void init(){ 20 | std::cerr << "In init\n"; 21 | p4_pd_init(); 22 | } 23 | 24 | void cleanup(){ 25 | std::cerr << "In cleanup\n"; 26 | p4_pd_cleanup(); 27 | } 28 | 29 | SessionHandle_t client_init(){ 30 | std::cerr << "In client_init\n"; 31 | 32 | p4_pd_sess_hdl_t sess_hdl; 33 | p4_pd_client_init(&sess_hdl); 34 | return sess_hdl; 35 | } 36 | 37 | int32_t client_cleanup(const SessionHandle_t sess_hdl){ 38 | std::cerr << "In client_cleanup\n"; 39 | 40 | return p4_pd_client_cleanup(sess_hdl); 41 | } 42 | 43 | int32_t begin_txn(const SessionHandle_t sess_hdl, const bool isAtomic, const bool isHighPri){ 44 | std::cerr << "In begin_txn\n"; 45 | 46 | return p4_pd_begin_txn(sess_hdl, isAtomic, isHighPri); 47 | } 48 | 49 | int32_t verify_txn(const SessionHandle_t sess_hdl){ 50 | std::cerr << "In verify_txn\n"; 51 | 52 | return p4_pd_verify_txn(sess_hdl); 53 | } 54 | 55 | int32_t abort_txn(const SessionHandle_t sess_hdl){ 56 | std::cerr << "In abort_txn\n"; 57 | 58 | return p4_pd_abort_txn(sess_hdl); 59 | } 60 | 61 | int32_t commit_txn(const SessionHandle_t sess_hdl, const bool hwSynchronous){ 62 | std::cerr << "In commit_txn\n"; 63 | 64 | return p4_pd_commit_txn(sess_hdl, hwSynchronous); 65 | } 66 | 67 | int32_t complete_operations(const SessionHandle_t sess_hdl){ 68 | std::cerr << "In complete_operations\n"; 69 | 70 | return p4_pd_complete_operations(sess_hdl); 71 | } 72 | }; 73 | -------------------------------------------------------------------------------- /pdfixed/thrift-src/pdfixed_rpc_server.h: -------------------------------------------------------------------------------- 1 | #ifndef _P4_PDFIXED_RPC_SERVER_H_ 2 | #define _P4_PDFIXED_RPC_SERVER_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | int start_bfn_pd_rpc_server(void **server_cookie); 9 | 10 | #ifdef __cplusplus 11 | } 12 | #endif 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /pdfixed/thrift-src/sswitch_pd_rpc_server.ipp: -------------------------------------------------------------------------------- 1 | #include "sswitch.h" 2 | 3 | #include 4 | 5 | #include 6 | 7 | using namespace ::sswitch_pd_rpc; 8 | using namespace ::res_pd_rpc; 9 | 10 | class sswitchHandler : virtual public sswitchIf { 11 | public: 12 | sswitchHandler() { 13 | 14 | } 15 | 16 | // mirroring api 17 | 18 | int32_t mirroring_mapping_add(const int32_t mirror_id, const int32_t egress_port) { 19 | std::cerr << "In mirroring_mapping_add\n"; 20 | return p4_pd_mirroring_mapping_add(mirror_id, egress_port); 21 | } 22 | 23 | int32_t mirroring_mapping_delete(const int32_t mirror_id) { 24 | std::cerr << "In mirroring_mapping_delete\n"; 25 | return p4_pd_mirroring_mapping_delete(mirror_id); 26 | } 27 | 28 | int32_t mirroring_mapping_get_egress_port(int32_t mirror_id) { 29 | std::cerr << "In mirroring_mapping_get_egress_port\n"; 30 | return p4_pd_mirroring_mapping_get_egress_port(mirror_id); 31 | } 32 | 33 | }; 34 | -------------------------------------------------------------------------------- /pdfixed/thrift/conn_mgr_pd_rpc.thrift: -------------------------------------------------------------------------------- 1 | include "res.thrift" 2 | 3 | namespace py conn_mgr_pd_rpc 4 | namespace cpp conn_mgr_pd_rpc 5 | namespace c_glib conn_mgr_pd_rpc 6 | 7 | service conn_mgr { 8 | # Test echo interface 9 | void echo(1:string s); 10 | 11 | void init(); 12 | 13 | void cleanup(); 14 | 15 | res.SessionHandle_t client_init(); 16 | 17 | i32 client_cleanup(1:res.SessionHandle_t sess_hdl); 18 | 19 | i32 begin_txn(1:res.SessionHandle_t sess_hdl, 2:bool isAtomic, 3:bool isHighPri); 20 | 21 | i32 verify_txn(1:res.SessionHandle_t sess_hdl); 22 | 23 | i32 abort_txn(1:res.SessionHandle_t sess_hdl); 24 | 25 | i32 commit_txn(1:res.SessionHandle_t sess_hdl, 2:bool hwSynchronous); 26 | 27 | i32 complete_operations(1:res.SessionHandle_t sess_hdl); 28 | } 29 | -------------------------------------------------------------------------------- /pdfixed/thrift/mc_pd_rpc.thrift: -------------------------------------------------------------------------------- 1 | include "res.thrift" 2 | 3 | namespace py mc_pd_rpc 4 | namespace cpp mc_pd_rpc 5 | namespace c_glib mc_pd_rpc 6 | 7 | typedef i32 McHandle_t 8 | 9 | service mc { 10 | # Multicast APIs. 11 | i32 mc_init (); 12 | res.SessionHandle_t mc_create_session (); 13 | i32 mc_destroy_session (1: res.SessionHandle_t sess_hdl); 14 | i32 mc_complete_operations(1: res.SessionHandle_t sess_hdl); 15 | McHandle_t mc_mgrp_create (1: res.SessionHandle_t sess_hdl, 2: i32 dev_id, 3: i16 mgid); 16 | i32 mc_mgrp_destroy (1: res.SessionHandle_t sess_hdl, 2: i32 dev_id, 3: McHandle_t grp_hdl); 17 | McHandle_t mc_node_create (1: res.SessionHandle_t sess_hdl, 2: i32 dev_id, 3: i16 rid, 4: binary port_map, 5: binary lag_map); 18 | i32 mc_node_update (1: res.SessionHandle_t sess_hdl, 2: i32 dev_id, 3: McHandle_t l1_hdl, 4: binary port_map, 5: binary lag_map); 19 | i32 mc_node_destroy (1: res.SessionHandle_t sess_hdl, 2: i32 dev_id, 3: McHandle_t l1_hdl); 20 | i32 mc_associate_node (1: res.SessionHandle_t sess_hdl, 2: i32 dev_id, 3: McHandle_t grp_hdl, 4: McHandle_t l1_hdl); 21 | i32 mc_dissociate_node(1: res.SessionHandle_t sess_hdl, 2: i32 dev_id, 3: McHandle_t grp_hdl, 4: McHandle_t l1_hdl); 22 | i32 mc_set_lag_membership(1: res.SessionHandle_t sess_hdl, 2: i32 dev_id, 3: byte lag_index, 4: binary port_map); 23 | } 24 | -------------------------------------------------------------------------------- /pdfixed/thrift/res.thrift: -------------------------------------------------------------------------------- 1 | namespace py res_pd_rpc 2 | namespace cpp res_pd_rpc 3 | namespace c_glib res_pd_rpc 4 | 5 | typedef i32 SessionHandle_t 6 | struct DevTarget_t { 7 | 1: required byte dev_id; 8 | 2: required i16 dev_pipe_id; 9 | } 10 | -------------------------------------------------------------------------------- /pdfixed/thrift/sswitch_pd_rpc.thrift: -------------------------------------------------------------------------------- 1 | include "res.thrift" 2 | 3 | namespace py sswitch_pd_rpc 4 | namespace cpp sswitch_pd_rpc 5 | namespace c_glib sswitch_pd_rpc 6 | 7 | service sswitch { 8 | # mirroring api 9 | 10 | i32 mirroring_mapping_add(1:i32 mirror_id, 2:i32 egress_port); 11 | i32 mirroring_mapping_delete(1:i32 mirror_id); 12 | i32 mirroring_mapping_get_egress_port(1:i32 mirror_id); 13 | } 14 | -------------------------------------------------------------------------------- /services/.gitignore: -------------------------------------------------------------------------------- 1 | cpp_out 2 | grpc_out 3 | py_out 4 | 5 | -------------------------------------------------------------------------------- /services/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | # in case the src dir is used as the build dir 2 | exclude_files=cpp_out|grpc_out|config.h 3 | -------------------------------------------------------------------------------- /src/BMI/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | if WITH_PCAP_FIX 4 | AM_CPPFLAGS += -DWITH_PCAP_FIX 5 | endif 6 | 7 | noinst_LTLIBRARIES = libbmi.la 8 | 9 | libbmi_la_SOURCES = \ 10 | bmi_interface.c \ 11 | bmi_interface.h \ 12 | bmi_port.c \ 13 | BMI/bmi_port.h 14 | -------------------------------------------------------------------------------- /src/BMI/bmi_interface.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 _BMI_INTERFACE_ 22 | #define _BMI_INTERFACE_ 23 | 24 | typedef struct bmi_interface_s bmi_interface_t; 25 | 26 | typedef enum 27 | { 28 | bmi_input_dumper, 29 | bmi_output_dumper 30 | } bmi_dumper_kind_t; 31 | 32 | int bmi_interface_create(bmi_interface_t **bmi, const char *device); 33 | 34 | int bmi_interface_add_dumper(bmi_interface_t *bmi, const char *filename, bmi_dumper_kind_t dumper_kind); 35 | 36 | int bmi_interface_destroy(bmi_interface_t *bmi); 37 | 38 | int bmi_interface_send(bmi_interface_t *bmi, const char *data, int len); 39 | 40 | int bmi_interface_recv(bmi_interface_t *bmi, const char **data); 41 | 42 | int bmi_interface_recv_with_copy(bmi_interface_t *bmi, char *data, int max_len); 43 | 44 | int bmi_interface_get_fd(bmi_interface_t *bmi); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | if COND_THRIFT 4 | MAYBE_BM_RUNTIME = bm_runtime 5 | endif 6 | 7 | if COND_NANOMSG 8 | MAYBE_BM_APPS = bm_apps 9 | endif 10 | 11 | SUBDIRS = bf_lpm_trie BMI bm_sim $(MAYBE_BM_RUNTIME) $(MAYBE_BM_APPS) 12 | 13 | lib_LTLIBRARIES = libbmall.la 14 | 15 | if COND_NANOMSG 16 | lib_LTLIBRARIES += libbmp4apps.la 17 | libbmp4apps_la_SOURCES = 18 | # Dummy C++ source to cause C++ linking. 19 | nodist_EXTRA_libbmp4apps_la_SOURCES = dummy.cpp 20 | libbmp4apps_la_LIBADD = bm_apps/libbmapps.la 21 | endif 22 | 23 | libbmall_la_SOURCES = 24 | # Dummy C++ source to cause C++ linking. 25 | nodist_EXTRA_libbmall_la_SOURCES = dummy.cpp 26 | libbmall_la_LIBADD = \ 27 | $(top_builddir)/src/bm_sim/libbmsim.la \ 28 | $(top_builddir)/src/bf_lpm_trie/libbflpmtrie.la \ 29 | $(top_builddir)/src/BMI/libbmi.la \ 30 | $(top_builddir)/third_party/jsoncpp/libjson.la 31 | 32 | if COND_THRIFT 33 | libbmall_la_LIBADD += \ 34 | $(top_builddir)/src/bm_runtime/libbmruntime.la \ 35 | $(top_builddir)/thrift_src/libruntimestubs.la 36 | endif 37 | 38 | libbmall_la_LIBADD += \ 39 | -lboost_system $(THRIFT_LIB) -lboost_program_options -lboost_filesystem 40 | -------------------------------------------------------------------------------- /src/bf_lpm_trie/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | noinst_LTLIBRARIES = libbflpmtrie.la 4 | 5 | libbflpmtrie_la_SOURCES = \ 6 | bf_lpm_trie.c \ 7 | bf_lpm_trie/bf_lpm_trie.h 8 | -------------------------------------------------------------------------------- /src/bf_lpm_trie/bf_lpm_trie/bf_lpm_trie.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2013-present Barefoot Networks, Inc. 2 | * Copyright 2021 VMware, Inc. 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 19 | * 20 | */ 21 | 22 | #ifndef _BF_LPM_TRIE_H 23 | #define _BF_LPM_TRIE_H 24 | 25 | #ifdef __cplusplus 26 | extern "C"{ 27 | #endif 28 | 29 | #include 30 | #include 31 | 32 | typedef struct bf_lpm_trie_s bf_lpm_trie_t; 33 | 34 | typedef uintptr_t value_t; 35 | 36 | bf_lpm_trie_t *bf_lpm_trie_create(size_t key_width_bytes, bool auto_shrink); 37 | 38 | void bf_lpm_trie_destroy(bf_lpm_trie_t *t); 39 | 40 | void bf_lpm_trie_insert(bf_lpm_trie_t *trie, 41 | const char *prefix, int prefix_length, 42 | const value_t value); 43 | 44 | bool bf_lpm_trie_has_prefix(const bf_lpm_trie_t *trie, 45 | const char *prefix, int prefix_length); 46 | 47 | bool bf_lpm_trie_retrieve_value(const bf_lpm_trie_t *trie, 48 | const char *prefix, int prefix_length, 49 | value_t *pvalue); 50 | 51 | bool bf_lpm_trie_lookup(const bf_lpm_trie_t *trie, const char *key, 52 | value_t *pvalue); 53 | 54 | bool bf_lpm_trie_delete(bf_lpm_trie_t *trie, const char *prefix, 55 | int prefix_length); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /src/bm_apps/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | exclude_files=nn\.h 2 | -------------------------------------------------------------------------------- /src/bm_apps/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | SUBDIRS = . examples 4 | 5 | libbmapps_la_SOURCES = \ 6 | notifications.cpp \ 7 | packet_pipe.cpp \ 8 | nn.h 9 | 10 | libbmapps_la_LIBADD = \ 11 | -lnanomsg 12 | 13 | if COND_THRIFT 14 | AM_CPPFLAGS += \ 15 | -I$(top_builddir)/thrift_src/gen-cpp 16 | 17 | libbmapps_la_SOURCES += learn.cpp 18 | 19 | libbmapps_la_LIBADD += \ 20 | $(top_builddir)/thrift_src/libruntimestubs.la \ 21 | $(THRIFT_LIB) 22 | endif 23 | 24 | noinst_LTLIBRARIES = libbmapps.la 25 | -------------------------------------------------------------------------------- /src/bm_apps/examples/.gitignore: -------------------------------------------------------------------------------- 1 | test_packet_pipe 2 | -------------------------------------------------------------------------------- /src/bm_apps/examples/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_PROGRAMS = test_packet_pipe 2 | test_packet_pipe_SOURCES = test_packet_pipe.cpp 3 | test_packet_pipe_LDADD = $(top_builddir)/src/bm_apps/libbmapps.la 4 | -------------------------------------------------------------------------------- /src/bm_runtime/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | AM_CPPFLAGS += \ 4 | -I$(top_builddir)/thrift_src/gen-cpp 5 | 6 | noinst_LTLIBRARIES = libbmruntime.la 7 | 8 | libbmruntime_la_SOURCES = \ 9 | server.cpp \ 10 | Standard_server.cpp \ 11 | SimplePre_server.cpp \ 12 | SimplePreLAG_server.cpp 13 | -------------------------------------------------------------------------------- /src/bm_sim/.gitignore: -------------------------------------------------------------------------------- 1 | version.cpp 2 | -------------------------------------------------------------------------------- /src/bm_sim/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | exclude_files=xxhash\.h 2 | exclude_files=md5\.h 3 | -------------------------------------------------------------------------------- /src/bm_sim/_assert.cpp: -------------------------------------------------------------------------------- 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 22 | 23 | #include 24 | #include 25 | 26 | namespace bm { 27 | 28 | void _bm_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 bm 35 | -------------------------------------------------------------------------------- /src/bm_sim/bytecontainer.cpp: -------------------------------------------------------------------------------- 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 22 | 23 | #include 24 | #include 25 | 26 | #include "utils.h" 27 | 28 | namespace bm { 29 | 30 | std::string 31 | ByteContainer::to_hex(size_t start, size_t s, bool upper_case) const { 32 | assert(start + s <= size()); 33 | 34 | std::ostringstream ret; 35 | // in debug mode, some compilers perform bound-checking even for operator[] 36 | // utils::dump_hexstring(ret, &bytes[start], &bytes[start + s], upper_case); 37 | auto first = bytes.begin() + start; 38 | utils::dump_hexstring(ret, first, first + s, upper_case); 39 | return ret.str(); 40 | } 41 | 42 | } // namespace bm 43 | -------------------------------------------------------------------------------- /src/bm_sim/control_action.cpp: -------------------------------------------------------------------------------- 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 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | namespace bm { 28 | 29 | ControlAction::ControlAction(const std::string &name, p4object_id_t id) 30 | : ControlFlowNode(name, id) { } 31 | 32 | ControlAction::ControlAction(const std::string &name, p4object_id_t id, 33 | std::unique_ptr source_info) 34 | : ControlFlowNode(name, id, std::move(source_info)) { } 35 | 36 | void 37 | ControlAction::set_next_node(ControlFlowNode *next_node) { 38 | this->next_node = next_node; 39 | } 40 | 41 | void 42 | ControlAction::set_action(ActionFn *action) { 43 | this->action = action; 44 | } 45 | 46 | const ControlFlowNode * 47 | ControlAction::operator()(Packet *pkt) const { 48 | assert(action); 49 | ActionFnEntry action_entry(action); 50 | // TODO(unknown): log action call with source information, or ActionFnEntry 51 | // log is sufficient? 52 | action_entry(pkt); 53 | return next_node; 54 | } 55 | 56 | } // namespace bm 57 | -------------------------------------------------------------------------------- /src/bm_sim/core/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | filter=-runtime/references 2 | -------------------------------------------------------------------------------- /src/bm_sim/counters.cpp: -------------------------------------------------------------------------------- 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 22 | 23 | #include 24 | 25 | namespace bm { 26 | 27 | Counter::CounterErrorCode 28 | Counter::query_counter(counter_value_t *bytes, counter_value_t *packets) const { 29 | *bytes = this->bytes; 30 | *packets = this->packets; 31 | return SUCCESS; 32 | } 33 | 34 | Counter::CounterErrorCode 35 | Counter::reset_counter() { 36 | bytes = 0u; 37 | packets = 0u; 38 | return SUCCESS; 39 | } 40 | 41 | Counter::CounterErrorCode 42 | Counter::write_counter(counter_value_t bytes, counter_value_t packets) { 43 | this->bytes = bytes; 44 | this->packets = packets; 45 | return SUCCESS; 46 | } 47 | 48 | void 49 | Counter::serialize(std::ostream *out) const { 50 | (*out) << bytes << " " << packets << "\n"; 51 | } 52 | 53 | void 54 | Counter::deserialize(std::istream *in) { 55 | uint64_t b, p; 56 | (*in) >> b >> p; 57 | bytes = b; 58 | packets = p; 59 | } 60 | 61 | Counter::CounterErrorCode 62 | CounterArray::reset_counters() { 63 | for (Counter &c : counters) 64 | c.reset_counter(); 65 | return Counter::SUCCESS; 66 | } 67 | 68 | } // namespace bm 69 | -------------------------------------------------------------------------------- /src/bm_sim/extern.cpp: -------------------------------------------------------------------------------- 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 22 | 23 | #include 24 | 25 | namespace bm { 26 | 27 | ExternFactoryMap * 28 | ExternFactoryMap::get_instance() { 29 | static ExternFactoryMap instance; 30 | return &instance; 31 | } 32 | 33 | int 34 | ExternFactoryMap::register_extern_type(const char *extern_type_name, 35 | ExternFactoryFn fn) { 36 | const std::string str_name = std::string(extern_type_name); 37 | auto it = factory_map.find(str_name); 38 | if (it != factory_map.end()) return 0; 39 | factory_map[str_name] = std::move(fn); 40 | return 1; 41 | } 42 | 43 | std::unique_ptr 44 | ExternFactoryMap::get_extern_instance( 45 | const std::string &extern_type_name) const { 46 | auto it = factory_map.find(extern_type_name); 47 | if (it == factory_map.end()) return nullptr; 48 | return it->second(); 49 | } 50 | 51 | void 52 | ExternType::_set_p4objects(P4Objects *p4objects) { 53 | this->p4objects = p4objects; 54 | } 55 | 56 | void 57 | ExternType::_set_name_and_id(const std::string &name, p4object_id_t id) { 58 | this->name = name; 59 | this->id = id; 60 | } 61 | 62 | } // namespace bm 63 | -------------------------------------------------------------------------------- /src/bm_sim/header_unions.cpp: -------------------------------------------------------------------------------- 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 22 | #include 23 | 24 | namespace bm { 25 | 26 | bool 27 | HeaderUnion::cmp(const HeaderUnion &other) const { 28 | return valid && other.valid && 29 | (valid_header_idx == other.valid_header_idx) && 30 | headers[valid_header_idx].get().cmp(other.headers[valid_header_idx]); 31 | } 32 | 33 | } // namespace bm 34 | -------------------------------------------------------------------------------- /src/bm_sim/md5.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. 3 | * MD5 Message-Digest Algorithm (RFC 1321). 4 | * 5 | * Homepage: 6 | * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 7 | * 8 | * Author: 9 | * Alexander Peslyak, better known as Solar Designer 10 | * 11 | * This software was written by Alexander Peslyak in 2001. No copyright is 12 | * claimed, and the software is hereby placed in the public domain. 13 | * In case this attempt to disclaim copyright and place the software in the 14 | * public domain is deemed null and void, then the software is 15 | * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the 16 | * general public under the following terms: 17 | * 18 | * Redistribution and use in source and binary forms, with or without 19 | * modification, are permitted. 20 | * 21 | * There's ABSOLUTELY NO WARRANTY, express or implied. 22 | * 23 | * See md5.c for more information. 24 | */ 25 | 26 | #ifdef HAVE_OPENSSL 27 | #include 28 | #elif !defined(_MD5_H) 29 | #define _MD5_H 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | /* Any 32-bit or wider unsigned integer data type will do */ 36 | typedef unsigned int MD5_u32plus; 37 | 38 | typedef struct { 39 | MD5_u32plus lo, hi; 40 | MD5_u32plus a, b, c, d; 41 | unsigned char buffer[64]; 42 | MD5_u32plus block[16]; 43 | } MD5_CTX; 44 | 45 | extern void MD5_Init(MD5_CTX *ctx); 46 | extern void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size); 47 | extern void MD5_Final(unsigned char *result, MD5_CTX *ctx); 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/bm_sim/pipeline.cpp: -------------------------------------------------------------------------------- 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 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | namespace bm { 28 | 29 | void 30 | Pipeline::apply(Packet *pkt) { 31 | BMELOG(pipeline_start, *pkt, *this); 32 | // TODO(antonin) 33 | // this is temporary while we experiment with the debugger 34 | DEBUGGER_NOTIFY_CTR( 35 | Debugger::PacketId::make(pkt->get_packet_id(), pkt->get_copy_id()), 36 | DBG_CTR_CONTROL | get_id()); 37 | BMLOG_DEBUG_PKT(*pkt, "Pipeline '{}': start", get_name()); 38 | const ControlFlowNode *node = first_node; 39 | while (node) { 40 | if (pkt->is_marked_for_exit()) { 41 | BMLOG_DEBUG_PKT(*pkt, "Packet is marked for exit, interrupting pipeline"); 42 | break; 43 | } 44 | node = (*node)(pkt); 45 | } 46 | BMELOG(pipeline_done, *pkt, *this); 47 | DEBUGGER_NOTIFY_CTR( 48 | Debugger::PacketId::make(pkt->get_packet_id(), pkt->get_copy_id()), 49 | DBG_CTR_EXIT(DBG_CTR_CONTROL) | get_id()); 50 | BMLOG_DEBUG_PKT(*pkt, "Pipeline '{}': end", get_name()); 51 | } 52 | 53 | } // namespace bm 54 | -------------------------------------------------------------------------------- /src/bm_sim/source_info.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 Cisco Systems, 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 | * Andy Fingerhut (jafinger@cisco.com) 18 | * 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | namespace bm { 25 | 26 | void SourceInfo::init_to_string() { 27 | std::stringstream result; 28 | result << filename << "(" << line << ")"; 29 | string_representation = result.str(); 30 | } 31 | 32 | } // namespace bm 33 | -------------------------------------------------------------------------------- /src/bm_sim/tables.cpp: -------------------------------------------------------------------------------- 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 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | namespace bm { 28 | 29 | MatchActionTable::MatchActionTable( 30 | const std::string &name, p4object_id_t id, 31 | std::unique_ptr match_table) 32 | : ControlFlowNode(name, id), 33 | match_table(std::move(match_table)) { } 34 | 35 | const ControlFlowNode * 36 | MatchActionTable::operator()(Packet *pkt) const { 37 | // TODO(antonin) this is temporary while we experiment with the debugger 38 | DEBUGGER_NOTIFY_CTR( 39 | Debugger::PacketId::make(pkt->get_packet_id(), pkt->get_copy_id()), 40 | DBG_CTR_TABLE | get_id()); 41 | BMLOG_TRACE_PKT(*pkt, "Applying table '{}'", get_name()); 42 | const auto next = match_table->apply_action(pkt); 43 | DEBUGGER_NOTIFY_CTR( 44 | Debugger::PacketId::make(pkt->get_packet_id(), pkt->get_copy_id()), 45 | DBG_CTR_EXIT(DBG_CTR_TABLE) | get_id()); 46 | return next; 47 | } 48 | 49 | } // namespace bm 50 | -------------------------------------------------------------------------------- /src/bm_sim/version.cpp.in: -------------------------------------------------------------------------------- 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 "version.h" 22 | 23 | namespace bm { 24 | 25 | const char bm_version_str[] = "@BM_VERSION@"; 26 | 27 | } // namespace bm 28 | -------------------------------------------------------------------------------- /src/bm_sim/version.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 BM_SIM_VERSION_H_ 22 | #define BM_SIM_VERSION_H_ 23 | 24 | namespace bm { 25 | 26 | extern const char bm_version_str[]; 27 | 28 | } // namespace bm 29 | 30 | #endif // BM_SIM_VERSION_H_ 31 | -------------------------------------------------------------------------------- /targets/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | filter=-build/include_subdir 2 | filter=-build/c++11 3 | filter=-runtime/references 4 | -------------------------------------------------------------------------------- /targets/Makefile.am: -------------------------------------------------------------------------------- 1 | MAYBE_SECONDARY_TARGETS = 2 | 3 | if COND_THRIFT 4 | MAYBE_SECONDARY_TARGETS += simple_router l2_switch 5 | endif 6 | 7 | if COND_PI 8 | MAYBE_SECONDARY_TARGETS += simple_switch_grpc 9 | endif 10 | 11 | SUBDIRS = test_utils simple_switch psa_switch pna_nic $(MAYBE_SECONDARY_TARGETS) 12 | -------------------------------------------------------------------------------- /targets/l2_switch/.gitignore: -------------------------------------------------------------------------------- 1 | /l2_switch 2 | -------------------------------------------------------------------------------- /targets/l2_switch/Makefile.am: -------------------------------------------------------------------------------- 1 | if COND_NANOMSG 2 | MAYBE_LEARN_CLIENT = learn_client 3 | endif 4 | 5 | SUBDIRS = . $(MAYBE_LEARN_CLIENT) 6 | 7 | noinst_PROGRAMS = l2_switch 8 | l2_switch_SOURCES = l2_switch.cpp primitives.cpp 9 | l2_switch_LDADD = $(top_builddir)/src/bm_runtime/libbmruntime.la \ 10 | $(top_builddir)/src/bm_sim/libbmsim.la \ 11 | $(top_builddir)/src/bf_lpm_trie/libbflpmtrie.la \ 12 | $(top_builddir)/thrift_src/libruntimestubs.la \ 13 | $(top_builddir)/src/BMI/libbmi.la \ 14 | $(top_builddir)/third_party/jsoncpp/libjson.la \ 15 | -lboost_system $(THRIFT_LIB) -lboost_program_options -lboost_filesystem 16 | -------------------------------------------------------------------------------- /targets/l2_switch/commands.txt: -------------------------------------------------------------------------------- 1 | table_set_default smac mac_learn 2 | table_set_default dmac broadcast 3 | mc_mgrp_create 1 4 | mc_node_create 0 1 5 | mc_node_create 1 2 6 | mc_node_associate 1 0 7 | mc_node_associate 1 1 8 | -------------------------------------------------------------------------------- /targets/l2_switch/learn_client/.gitignore: -------------------------------------------------------------------------------- 1 | /learn_client 2 | -------------------------------------------------------------------------------- /targets/l2_switch/learn_client/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS += \ 2 | -I$(top_builddir)/thrift_src/gen-cpp 3 | 4 | noinst_PROGRAMS = learn_client 5 | learn_client_SOURCES = learn_client.cpp 6 | learn_client_LDADD = $(top_builddir)/src/bm_apps/libbmapps.la 7 | -------------------------------------------------------------------------------- /targets/l2_switch/run.sh: -------------------------------------------------------------------------------- 1 | sudo ./l2_switch -i 0@veth1 -i 1@veth3 -i 2@veth5 -i 3@veth7 --pcap l2_switch.json 2 | -------------------------------------------------------------------------------- /targets/l2_switch/runtime_CLI: -------------------------------------------------------------------------------- 1 | ../../tools/runtime_CLI.py -------------------------------------------------------------------------------- /targets/pna_nic/.gitignore: -------------------------------------------------------------------------------- 1 | /pna_nic 2 | pnic_runtime 3 | pna_nic_CLI 4 | -------------------------------------------------------------------------------- /targets/pna_nic/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | # in case the src dir is used as the build dir 2 | exclude_files=gen-cpp 3 | -------------------------------------------------------------------------------- /targets/pna_nic/externs/pna_counter.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 Marvell Technology, 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 | * Rupesh Chiluka (rchiluka@marvell.com) 18 | * 19 | */ 20 | 21 | 22 | #include "pna_counter.h" 23 | 24 | namespace bm { 25 | 26 | namespace pna { 27 | 28 | void 29 | PNA_Counter::count(const Data &index) { 30 | _counter->get_counter( 31 | index.get()).increment_counter(get_packet()); 32 | } 33 | 34 | Counter & 35 | PNA_Counter::get_counter(size_t idx) { 36 | return _counter->get_counter(idx); 37 | } 38 | 39 | const Counter & 40 | PNA_Counter::get_counter(size_t idx) const { 41 | return _counter->get_counter(idx); 42 | } 43 | 44 | Counter::CounterErrorCode 45 | PNA_Counter::reset_counters(){ 46 | return _counter->reset_counters(); 47 | } 48 | 49 | BM_REGISTER_EXTERN_W_NAME(Counter, PNA_Counter); 50 | BM_REGISTER_EXTERN_W_NAME_METHOD(Counter, PNA_Counter, count, const Data &); 51 | 52 | } // namespace bm::pna 53 | 54 | } // namespace bm 55 | 56 | int import_counters(){ 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /targets/pna_nic/externs/pna_counter.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 Marvell Technology, 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 | * Rupesh Chiluka (rchiluka@marvell.com) 18 | * 19 | */ 20 | 21 | 22 | #ifndef PNA_NIC_PNA_COUNTER_H_ 23 | #define PNA_NIC_PNA_COUNTER_H_ 24 | 25 | #include 26 | #include 27 | 28 | namespace bm { 29 | 30 | namespace pna { 31 | 32 | class PNA_Counter : public bm::ExternType { 33 | public: 34 | static constexpr p4object_id_t spec_id = 0xffffffff; 35 | 36 | BM_EXTERN_ATTRIBUTES { 37 | BM_EXTERN_ATTRIBUTE_ADD(n_counters); 38 | BM_EXTERN_ATTRIBUTE_ADD(type); 39 | } 40 | 41 | void init() override { 42 | _counter = std::unique_ptr( 43 | new CounterArray(get_name() + ".$impl", 44 | spec_id, 45 | n_counters.get())); 46 | } 47 | 48 | void count(const Data &index); 49 | 50 | Counter &get_counter(size_t idx); 51 | 52 | const Counter &get_counter(size_t idx) const; 53 | 54 | Counter::CounterErrorCode reset_counters(); 55 | 56 | size_t size() const { return _counter->size(); }; 57 | 58 | private: 59 | Data n_counters; 60 | Data type; 61 | std::unique_ptr _counter; 62 | }; 63 | 64 | } // namespace bm::pna 65 | 66 | } // namespace bm 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /targets/pna_nic/externs/pna_hash.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 Marvell Technology, 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 | * Rupesh Chiluka (rchiluka@marvell.com) 18 | * 19 | */ 20 | 21 | 22 | #ifndef PNA_NIC_PNA_HASH_H_ 23 | #define PNA_NIC_PNA_HASH_H_ 24 | 25 | #include 26 | #include 27 | 28 | namespace bm { 29 | 30 | namespace pna { 31 | 32 | class PNA_Hash : public bm::ExternType { 33 | public: 34 | 35 | BM_EXTERN_ATTRIBUTES { 36 | BM_EXTERN_ATTRIBUTE_ADD(algo); 37 | } 38 | 39 | void init() override; 40 | 41 | void get_hash(Field &dst, const std::vector &fields); 42 | 43 | void get_hash_mod(Field &dst, const Data &base, const std::vector &fields, const Data &max); 44 | 45 | uint64_t compute(const char *buffer, size_t s); 46 | 47 | private: 48 | std::string algo; 49 | std::unique_ptr calc; 50 | 51 | }; 52 | 53 | } // namespace bm::pna 54 | 55 | } // namespace bm 56 | #endif 57 | -------------------------------------------------------------------------------- /targets/pna_nic/externs/pna_internet_checksum.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 Marvell Technology, 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 | * Rupesh Chiluka (rchiluka@marvell.com) 18 | * 19 | */ 20 | 21 | 22 | #ifndef PNA_NIC_PNA_INTERNETCHECKSUM_H_ 23 | #define PNA_NIC_PNA_INTERNETCHECKSUM_H_ 24 | 25 | #include 26 | 27 | namespace bm { 28 | 29 | namespace pna { 30 | 31 | class PNA_InternetChecksum : public bm::ExternType { 32 | public: 33 | 34 | BM_EXTERN_ATTRIBUTES { 35 | } 36 | 37 | void init() override; 38 | 39 | void get(Field &dst) const; 40 | 41 | void get_verify(Field &dst, Field &equOp) const; 42 | 43 | void clear(); 44 | 45 | void add(const std::vector &fields); 46 | 47 | void subtract(const std::vector &fields); 48 | 49 | void get_state(Field &dst) const; 50 | 51 | void set_state(const Data &src); 52 | 53 | private: 54 | uint16_t sum; 55 | 56 | }; 57 | 58 | } // namespace bm::pna 59 | 60 | } // namespace bm 61 | #endif 62 | -------------------------------------------------------------------------------- /targets/pna_nic/externs/pna_meter.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 Marvell Technology, 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 | * Rupesh Chiluka (rchiluka@marvell.com) 18 | * 19 | */ 20 | 21 | #ifndef PNA_NIC_PNA_METER_H_ 22 | #define PNA_NIC_PNA_METER_H_ 23 | 24 | #include 25 | #include 26 | 27 | namespace bm { 28 | 29 | namespace pna { 30 | 31 | class PNA_Meter : public bm::ExternType { 32 | public: 33 | static constexpr p4object_id_t spec_id = 0xfffffffe; 34 | 35 | BM_EXTERN_ATTRIBUTES { 36 | BM_EXTERN_ATTRIBUTE_ADD(n_meters); 37 | BM_EXTERN_ATTRIBUTE_ADD(type); 38 | BM_EXTERN_ATTRIBUTE_ADD(is_direct); 39 | BM_EXTERN_ATTRIBUTE_ADD(rate_count); 40 | } 41 | 42 | void init() override; 43 | 44 | void execute(const Data &index, Data &value); 45 | 46 | Meter &get_meter(size_t idx); 47 | 48 | const Meter &get_meter(size_t idx) const; 49 | 50 | Meter::MeterErrorCode set_rates(const std::vector &configs); 51 | 52 | size_t size() const { return _meter->size(); }; 53 | 54 | private: 55 | Data n_meters; 56 | std::string type; 57 | Data is_direct; 58 | Data rate_count; 59 | Data color; 60 | std::unique_ptr _meter; 61 | }; 62 | 63 | } // namespace bm::pna 64 | 65 | } // namespace bm 66 | #endif 67 | -------------------------------------------------------------------------------- /targets/pna_nic/externs/pna_random.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 Marvell Technology, 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 | * Rupesh Chiluka (rchiluka@marvell.com) 18 | * 19 | */ 20 | 21 | #ifndef PNA_NIC_PNA_RANDOM_H_ 22 | #define PNA_NIC_PNA_RANDOM_H_ 23 | 24 | #include 25 | 26 | namespace bm { 27 | 28 | namespace pna { 29 | 30 | class PNA_Random : public bm::ExternType { 31 | public: 32 | BM_EXTERN_ATTRIBUTES { 33 | BM_EXTERN_ATTRIBUTE_ADD(min); 34 | BM_EXTERN_ATTRIBUTE_ADD(max); 35 | } 36 | 37 | void init() override; 38 | 39 | void read(Data &value); 40 | 41 | private: 42 | Data min; 43 | Data max; 44 | uint64_t min_val; 45 | uint64_t max_val; 46 | }; 47 | 48 | } // namespace bm::pna 49 | 50 | } // namespace bm 51 | #endif 52 | -------------------------------------------------------------------------------- /targets/pna_nic/pna_nic_CLI.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2024 Marvell Technology, Inc. 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 | # Rupesh Chiluka (rchiluka@marvell.com) 19 | # 20 | # 21 | 22 | # This is just a wrapper script around pnic_CLI.py 23 | # It makes sure that the script works correctly no matter where Python 24 | # dependencies are installed 25 | 26 | import sys 27 | sys.path.append("@pythondir@") 28 | 29 | import pnic_CLI 30 | pnic_CLI.main() 31 | -------------------------------------------------------------------------------- /targets/pna_nic/pnic_CLI: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | 5 | json="" 6 | if [ $# -eq 1 ]; then 7 | echo "Using JSON input $1" 8 | json="--json $1" 9 | fi 10 | port="" 11 | if [ $# -eq 2 ]; then 12 | echo "Using Thrift port $2" 13 | port="--thrift-port $2" 14 | else 15 | echo "No Thrift port specified, using CLI default" 16 | fi 17 | if [ $# -gt 2 ]; then 18 | echo "Invalid number of arguments" 19 | echo "Usage: $0 [ []]" 20 | exit 1 21 | fi 22 | 23 | CLI=$THIS_DIR/pnic_CLI.py 24 | TOOLS_DIR=$THIS_DIR/../../tools/ 25 | 26 | PYTHONPATH=$PYTHONPATH:$TOOLS_DIR python3 $CLI $json $port 27 | -------------------------------------------------------------------------------- /targets/pna_nic/runtime_CLI: -------------------------------------------------------------------------------- 1 | ../../tools/runtime_CLI.py -------------------------------------------------------------------------------- /targets/pna_nic/tests/.gitignore: -------------------------------------------------------------------------------- 1 | test* 2 | !test*.cpp 3 | !testdata 4 | -------------------------------------------------------------------------------- /targets/pna_nic/tests/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = . 2 | 3 | AM_CPPFLAGS += \ 4 | -isystem $(top_srcdir)/third_party/gtest/include \ 5 | -I$(srcdir)/.. \ 6 | -I$(srcdir)/ \ 7 | -I$(top_srcdir)/targets/test_utils \ 8 | -DTESTDATADIR=\"$(srcdir)/testdata\" 9 | 10 | LDADD = $(builddir)/../libpnanic.la \ 11 | $(top_builddir)/third_party/gtest/libgtest.la \ 12 | $(top_builddir)/targets/test_utils/libtargetstestutils.la \ 13 | $(top_builddir)/src/bm_apps/libbmapps.la \ 14 | -lboost_filesystem 15 | 16 | # Define unit tests 17 | common_source = main.cpp 18 | TESTS = test_parse_pna 19 | 20 | check_PROGRAMS = $(TESTS) test_all 21 | 22 | # Sources for tests 23 | test_parse_pna_SOURCES = $(common_source) test_parse_pna.cpp 24 | 25 | test_all_SOURCES = $(common_source) \ 26 | test_parse_pna.cpp 27 | 28 | EXTRA_DIST = \ 29 | testdata/pna-demo-L2-one-table.json 30 | -------------------------------------------------------------------------------- /targets/pna_nic/tests/main.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 Marvell Technology, 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 | * Rupesh Chiluka (rchiluka@marvell.com) 18 | * 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | bool WITH_VALGRIND = false; 28 | 29 | int main(int argc, char* argv[]) { 30 | ::testing::InitGoogleTest(&argc, argv); 31 | return RUN_ALL_TESTS(); 32 | } 33 | -------------------------------------------------------------------------------- /targets/pna_nic/thrift/pna_nic.thrift: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 Marvell Technology, 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 | * Rupesh Chiluka (rchiluka@marvell.com) 18 | * 19 | */ 20 | 21 | namespace cpp pnic_runtime 22 | namespace py pnic_runtime 23 | 24 | service PnaNic { 25 | 26 | // these methods are here as an experiment, prefer get_time_elapsed_us() when 27 | // possible 28 | i64 get_time_elapsed_us(); 29 | i64 get_time_since_epoch_us(); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /targets/psa_switch/.gitignore: -------------------------------------------------------------------------------- 1 | /psa_switch 2 | pswitch_runtime 3 | psa_switch_CLI 4 | -------------------------------------------------------------------------------- /targets/psa_switch/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | # in case the src dir is used as the build dir 2 | exclude_files=gen-cpp 3 | -------------------------------------------------------------------------------- /targets/psa_switch/externs/psa_counter.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019-present Derek So 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 | * Derek So (dts76@cornell.edu) 18 | * 19 | */ 20 | 21 | 22 | #include "psa_counter.h" 23 | 24 | namespace bm { 25 | 26 | namespace psa { 27 | 28 | void 29 | PSA_Counter::count(const Data &index) { 30 | _counter->get_counter( 31 | index.get()).increment_counter(get_packet()); 32 | } 33 | 34 | Counter & 35 | PSA_Counter::get_counter(size_t idx) { 36 | return _counter->get_counter(idx); 37 | } 38 | 39 | const Counter & 40 | PSA_Counter::get_counter(size_t idx) const { 41 | return _counter->get_counter(idx); 42 | } 43 | 44 | Counter::CounterErrorCode 45 | PSA_Counter::reset_counters(){ 46 | return _counter->reset_counters(); 47 | } 48 | 49 | BM_REGISTER_EXTERN_W_NAME(Counter, PSA_Counter); 50 | BM_REGISTER_EXTERN_W_NAME_METHOD(Counter, PSA_Counter, count, const Data &); 51 | 52 | } // namespace bm::psa 53 | 54 | } // namespace bm 55 | 56 | int import_counters(){ 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /targets/psa_switch/externs/psa_counter.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019-present Derek So 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 | * Derek So (dts76@cornell.edu) 18 | * 19 | */ 20 | 21 | 22 | #ifndef PSA_SWITCH_PSA_COUNTER_H_ 23 | #define PSA_SWITCH_PSA_COUNTER_H_ 24 | 25 | #include 26 | #include 27 | 28 | namespace bm { 29 | 30 | namespace psa { 31 | 32 | class PSA_Counter : public bm::ExternType { 33 | public: 34 | static constexpr p4object_id_t spec_id = 0xffffffff; 35 | 36 | BM_EXTERN_ATTRIBUTES { 37 | BM_EXTERN_ATTRIBUTE_ADD(n_counters); 38 | BM_EXTERN_ATTRIBUTE_ADD(type); 39 | } 40 | 41 | void init() override { 42 | _counter = std::unique_ptr( 43 | new CounterArray(get_name() + ".$impl", 44 | spec_id, 45 | n_counters.get())); 46 | } 47 | 48 | void count(const Data &index); 49 | 50 | Counter &get_counter(size_t idx); 51 | 52 | const Counter &get_counter(size_t idx) const; 53 | 54 | Counter::CounterErrorCode reset_counters(); 55 | 56 | size_t size() const { return _counter->size(); }; 57 | 58 | private: 59 | Data n_counters; 60 | Data type; 61 | std::unique_ptr _counter; 62 | }; 63 | 64 | } // namespace bm::psa 65 | 66 | } // namespace bm 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /targets/psa_switch/externs/psa_hash.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 SYRMIA LLC 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 | * Dusan Krdzic (dusan.krdzic@syrmia.com) 17 | * 18 | */ 19 | 20 | 21 | #ifndef PSA_SWITCH_PSA_HASH_H_ 22 | #define PSA_SWITCH_PSA_HASH_H_ 23 | 24 | #include 25 | #include 26 | 27 | namespace bm { 28 | 29 | namespace psa { 30 | 31 | class PSA_Hash : public bm::ExternType { 32 | public: 33 | 34 | BM_EXTERN_ATTRIBUTES { 35 | BM_EXTERN_ATTRIBUTE_ADD(algo); 36 | } 37 | 38 | void init() override; 39 | 40 | void get_hash(Field &dst, const std::vector &fields); 41 | 42 | void get_hash_mod(Field &dst, const Data &base, const std::vector &fields, const Data &max); 43 | 44 | uint64_t compute(const char *buffer, size_t s); 45 | 46 | private: 47 | std::string algo; 48 | std::unique_ptr calc; 49 | 50 | }; 51 | 52 | } // namespace bm::psa 53 | 54 | } // namespace bm 55 | #endif 56 | -------------------------------------------------------------------------------- /targets/psa_switch/externs/psa_internet_checksum.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 SYRMIA LLC 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 | * Dusan Krdzic (dusan.krdzic@syrmia.com) 17 | * 18 | */ 19 | 20 | 21 | #ifndef PSA_SWITCH_PSA_INTERNETCHECKSUM_H_ 22 | #define PSA_SWITCH_PSA_INTERNETCHECKSUM_H_ 23 | 24 | #include 25 | 26 | namespace bm { 27 | 28 | namespace psa { 29 | 30 | class PSA_InternetChecksum : public bm::ExternType { 31 | public: 32 | 33 | BM_EXTERN_ATTRIBUTES { 34 | } 35 | 36 | void init() override; 37 | 38 | void get(Field &dst) const; 39 | 40 | void get_verify(Field &dst, Field &equOp) const; 41 | 42 | void clear(); 43 | 44 | void add(const std::vector &fields); 45 | 46 | void subtract(const std::vector &fields); 47 | 48 | void get_state(Field &dst) const; 49 | 50 | void set_state(const Data &src); 51 | 52 | private: 53 | uint16_t sum; 54 | 55 | }; 56 | 57 | } // namespace bm::psa 58 | 59 | } // namespace bm 60 | #endif 61 | -------------------------------------------------------------------------------- /targets/psa_switch/externs/psa_meter.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020-present Cornell University 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 | * Yunhe Liu (yunheliu@cs.cornell.edu) 17 | * 18 | */ 19 | 20 | #ifndef PSA_SWITCH_PSA_METER_H_ 21 | #define PSA_SWITCH_PSA_METER_H_ 22 | 23 | #include 24 | #include 25 | 26 | namespace bm { 27 | 28 | namespace psa { 29 | 30 | class PSA_Meter : public bm::ExternType { 31 | public: 32 | static constexpr p4object_id_t spec_id = 0xfffffffe; 33 | 34 | BM_EXTERN_ATTRIBUTES { 35 | BM_EXTERN_ATTRIBUTE_ADD(n_meters); 36 | BM_EXTERN_ATTRIBUTE_ADD(type); 37 | BM_EXTERN_ATTRIBUTE_ADD(is_direct); 38 | BM_EXTERN_ATTRIBUTE_ADD(rate_count); 39 | } 40 | 41 | void init() override; 42 | 43 | void execute(const Data &index, Data &value); 44 | 45 | Meter &get_meter(size_t idx); 46 | 47 | const Meter &get_meter(size_t idx) const; 48 | 49 | Meter::MeterErrorCode set_rates(const std::vector &configs); 50 | 51 | size_t size() const { return _meter->size(); }; 52 | 53 | private: 54 | Data n_meters; 55 | std::string type; 56 | Data is_direct; 57 | Data rate_count; 58 | Data color; 59 | std::unique_ptr _meter; 60 | }; 61 | 62 | } // namespace bm::psa 63 | 64 | } // namespace bm 65 | #endif 66 | -------------------------------------------------------------------------------- /targets/psa_switch/externs/psa_random.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2020-present Cornell University 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 | * Yunhe Liu (yunheliu@cs.cornell.edu) 17 | * 18 | */ 19 | 20 | #include "psa_random.h" 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | namespace bm { 28 | 29 | namespace psa { 30 | 31 | void 32 | PSA_Random::init() { 33 | min_val = min.get_uint64(); 34 | max_val = max.get_uint64(); 35 | _BM_ASSERT((max_val > min_val) && "[Error] Random number range must be positive."); 36 | 37 | /* Note: Even though PSA spec mentioned range should be a power of 2 for 38 | * max portability, bmv2 does not impose this restriction. 39 | */ 40 | } 41 | 42 | void 43 | PSA_Random::read(Data &value) { 44 | using engine = std::default_random_engine; 45 | using hash = std::hash; 46 | static thread_local engine generator(hash()(std::this_thread::get_id())); 47 | using distrib64 = std::uniform_int_distribution; 48 | distrib64 distribution(min_val, max_val); 49 | value.set(distribution(generator)); 50 | } 51 | 52 | BM_REGISTER_EXTERN_W_NAME(Random, PSA_Random); 53 | BM_REGISTER_EXTERN_W_NAME_METHOD(Random, PSA_Random, read, Data &); 54 | 55 | } // namespace bm::psa 56 | 57 | } // namespace bm 58 | 59 | int import_random(){ 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /targets/psa_switch/externs/psa_random.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020-present Cornell University 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 | * Yunhe Liu (yunheliu@cs.cornell.edu) 17 | * 18 | */ 19 | 20 | #ifndef PSA_SWITCH_PSA_RANDOM_H_ 21 | #define PSA_SWITCH_PSA_RANDOM_H_ 22 | 23 | #include 24 | 25 | namespace bm { 26 | 27 | namespace psa { 28 | 29 | class PSA_Random : public bm::ExternType { 30 | public: 31 | BM_EXTERN_ATTRIBUTES { 32 | BM_EXTERN_ATTRIBUTE_ADD(min); 33 | BM_EXTERN_ATTRIBUTE_ADD(max); 34 | } 35 | 36 | void init() override; 37 | 38 | void read(Data &value); 39 | 40 | private: 41 | Data min; 42 | Data max; 43 | uint64_t min_val; 44 | uint64_t max_val; 45 | }; 46 | 47 | } // namespace bm::psa 48 | 49 | } // namespace bm 50 | #endif 51 | -------------------------------------------------------------------------------- /targets/psa_switch/psa_switch_CLI.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2013-present Barefoot Networks, Inc. 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 | # This is just a wrapper script around pswitch_CLI.py 23 | # It makes sure that the script works correctly no matter where Python 24 | # dependencies are installed 25 | 26 | import sys 27 | sys.path.append("@pythondir@") 28 | 29 | import pswitch_CLI 30 | pswitch_CLI.main() 31 | -------------------------------------------------------------------------------- /targets/psa_switch/pswitch_CLI: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | 5 | json="" 6 | if [ $# -eq 1 ]; then 7 | echo "Using JSON input $1" 8 | json="--json $1" 9 | fi 10 | port="" 11 | if [ $# -eq 2 ]; then 12 | echo "Using Thrift port $2" 13 | port="--thrift-port $2" 14 | else 15 | echo "No Thrift port specified, using CLI default" 16 | fi 17 | if [ $# -gt 2 ]; then 18 | echo "Invalid number of arguments" 19 | echo "Usage: $0 [ []]" 20 | exit 1 21 | fi 22 | 23 | CLI=$THIS_DIR/pswitch_CLI.py 24 | TOOLS_DIR=$THIS_DIR/../../tools/ 25 | 26 | PYTHONPATH=$PYTHONPATH:$TOOLS_DIR python3 $CLI $json $port 27 | -------------------------------------------------------------------------------- /targets/psa_switch/runtime_CLI: -------------------------------------------------------------------------------- 1 | ../../tools/runtime_CLI.py -------------------------------------------------------------------------------- /targets/psa_switch/tests/.gitignore: -------------------------------------------------------------------------------- 1 | test* 2 | !test*.cpp 3 | -------------------------------------------------------------------------------- /targets/psa_switch/tests/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = . 2 | 3 | AM_CPPFLAGS += \ 4 | -isystem $(top_srcdir)/third_party/gtest/include \ 5 | -I$(srcdir)/.. \ 6 | -I$(srcdir)/ \ 7 | -DTESTDATADIR=\"$(srcdir)/testdata\" 8 | LDADD = $(builddir)/../libpsaswitch.la \ 9 | $(top_builddir)/third_party/gtest/libgtest.la \ 10 | $(top_builddir)/src/bm_apps/libbmapps.la \ 11 | -lboost_filesystem 12 | 13 | # Define unit tests 14 | common_source = main.cpp 15 | TESTS = test_internet_checksum \ 16 | test_hash 17 | 18 | check_PROGRAMS = $(TESTS) test_all 19 | 20 | # Sources for tests 21 | test_internet_checksum_SOURCES = $(common_source) test_internet_checksum.cpp 22 | test_hash_SOURCES = $(common_source) test_hash.cpp 23 | 24 | test_all_SOURCES = $(common_source) \ 25 | test_internet_checksum.cpp \ 26 | test_hash.cpp 27 | -------------------------------------------------------------------------------- /targets/psa_switch/tests/main.cpp: -------------------------------------------------------------------------------- 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 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | bool WITH_VALGRIND = false; 28 | 29 | int main(int argc, char* argv[]) { 30 | ::testing::InitGoogleTest(&argc, argv); 31 | return RUN_ALL_TESTS(); 32 | } 33 | -------------------------------------------------------------------------------- /targets/simple_router/.gitignore: -------------------------------------------------------------------------------- 1 | /simple_router 2 | -------------------------------------------------------------------------------- /targets/simple_router/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_PROGRAMS = simple_router 2 | simple_router_SOURCES = simple_router.cpp primitives.cpp 3 | simple_router_LDADD = \ 4 | $(top_builddir)/src/bm_runtime/libbmruntime.la \ 5 | $(top_builddir)/src/bm_sim/libbmsim.la \ 6 | $(top_builddir)/src/bf_lpm_trie/libbflpmtrie.la \ 7 | $(top_builddir)/thrift_src/libruntimestubs.la \ 8 | $(top_builddir)/src/BMI/libbmi.la \ 9 | $(top_builddir)/third_party/jsoncpp/libjson.la \ 10 | -lboost_system $(THRIFT_LIB) -lboost_program_options -lboost_filesystem 11 | -------------------------------------------------------------------------------- /targets/simple_router/commands.txt: -------------------------------------------------------------------------------- 1 | table_set_default send_frame _drop 2 | table_set_default forward _drop 3 | table_set_default ipv4_lpm _drop 4 | table_add send_frame rewrite_mac 1 => 00:aa:bb:00:00:00 5 | table_add send_frame rewrite_mac 2 => 00:aa:bb:00:00:01 6 | table_add forward set_dmac 10.0.0.10 => 00:04:00:00:00:00 7 | table_add forward set_dmac 10.0.1.10 => 00:04:00:00:00:01 8 | table_add ipv4_lpm set_nhop 10.0.0.10/32 => 10.0.0.10 1 9 | table_add ipv4_lpm set_nhop 10.0.1.10/32 => 10.0.1.10 2 10 | -------------------------------------------------------------------------------- /targets/simple_router/primitives.cpp: -------------------------------------------------------------------------------- 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 22 | #include 23 | 24 | template 25 | using ActionPrimitive = bm::ActionPrimitive; 26 | 27 | using bm::Data; 28 | using bm::Field; 29 | using bm::Header; 30 | 31 | class modify_field : public ActionPrimitive { 32 | void operator ()(Field &f, const Data &d) { 33 | bm::core::assign()(f, d); 34 | } 35 | }; 36 | 37 | REGISTER_PRIMITIVE(modify_field); 38 | 39 | class add_to_field : public ActionPrimitive { 40 | void operator ()(Field &f, const Data &d) { 41 | f.add(f, d); 42 | } 43 | }; 44 | 45 | REGISTER_PRIMITIVE(add_to_field); 46 | 47 | class drop : public ActionPrimitive<> { 48 | void operator ()() { 49 | get_field("standard_metadata.egress_spec").set(511); 50 | } 51 | }; 52 | 53 | REGISTER_PRIMITIVE(drop); 54 | -------------------------------------------------------------------------------- /targets/simple_router/run.sh: -------------------------------------------------------------------------------- 1 | sudo ./simple_router -i 0@veth1 -i 1@veth3 -i 2@veth5 -i 3@veth7 --pcap simple_router.json 2 | -------------------------------------------------------------------------------- /targets/simple_router/runtime_CLI: -------------------------------------------------------------------------------- 1 | ../../tools/runtime_CLI.py -------------------------------------------------------------------------------- /targets/simple_switch/.gitignore: -------------------------------------------------------------------------------- 1 | /simple_switch 2 | sswitch_runtime 3 | simple_switch_CLI 4 | -------------------------------------------------------------------------------- /targets/simple_switch/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | # in case the src dir is used as the build dir 2 | exclude_files=gen-cpp 3 | -------------------------------------------------------------------------------- /targets/simple_switch/bm/simple_switch/runner.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-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 SIMPLE_SWITCH_BM_SIMPLE_SWITCH_RUNNER_H_ 22 | #define SIMPLE_SWITCH_BM_SIMPLE_SWITCH_RUNNER_H_ 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | class SimpleSwitch; 32 | 33 | namespace bm { 34 | 35 | namespace sswitch { 36 | 37 | class SimpleSwitchRunner { 38 | public: 39 | static constexpr uint32_t default_drop_port = 511; 40 | 41 | explicit SimpleSwitchRunner(uint32_t cpu_port = 0, 42 | uint32_t drop_port = default_drop_port); 43 | ~SimpleSwitchRunner(); 44 | 45 | int init_and_start(const bm::OptionsParser &parser); 46 | 47 | device_id_t get_device_id() const; 48 | 49 | DevMgr *get_dev_mgr(); 50 | 51 | private: 52 | uint32_t cpu_port{0}; 53 | std::unique_ptr simple_switch; 54 | }; 55 | 56 | } // namespace sswitch 57 | 58 | } // namespace bm 59 | 60 | #endif // SIMPLE_SWITCH_BM_SIMPLE_SWITCH_RUNNER_H_ 61 | -------------------------------------------------------------------------------- /targets/simple_switch/runtime_CLI: -------------------------------------------------------------------------------- 1 | ../../tools/runtime_CLI.py -------------------------------------------------------------------------------- /targets/simple_switch/simple_switch_CLI.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2013-present Barefoot Networks, Inc. 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 | # This is just a wrapper script around sswitch_CLI.py 23 | # It makes sure that the script works correctly no matter where Python 24 | # dependencies are installed 25 | 26 | import sys 27 | sys.path.append("@pythondir@") 28 | 29 | import sswitch_CLI 30 | sswitch_CLI.main() 31 | -------------------------------------------------------------------------------- /targets/simple_switch/sswitch_CLI: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | 5 | json="" 6 | if [ $# -eq 1 ]; then 7 | echo "Using JSON input $1" 8 | json="--json $1" 9 | fi 10 | port="" 11 | if [ $# -eq 2 ]; then 12 | echo "Using Thrift port $2" 13 | port="--thrift-port $2" 14 | else 15 | echo "No Thrift port specified, using CLI default" 16 | fi 17 | if [ $# -gt 2 ]; then 18 | echo "Invalid number of arguments" 19 | echo "Usage: $0 [ []]" 20 | exit 1 21 | fi 22 | 23 | CLI=$THIS_DIR/sswitch_CLI.py 24 | TOOLS_DIR=$THIS_DIR/../../tools/ 25 | 26 | PYTHONPATH=$PYTHONPATH:$TOOLS_DIR python3 $CLI $json $port 27 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/.gitignore: -------------------------------------------------------------------------------- 1 | test* 2 | !test*.cpp 3 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/.gitignore: -------------------------------------------------------------------------------- 1 | run_one_test.py 2 | !testdata 3 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | TESTS = \ 4 | table_dump_old.test \ 5 | table_dump.test \ 6 | table_dump_extra.test \ 7 | indirect_res.test \ 8 | mc_dump.test 9 | 10 | # run_one_test.py.in included by default 11 | # note that we do not distribute the P4 files on purpose (there is no point), 12 | # even though they are checked in 13 | EXTRA_DIST = \ 14 | table_dump_old.test \ 15 | testdata/table_dump_old.in \ 16 | testdata/table_dump_old.out \ 17 | table_dump.test \ 18 | testdata/table_dump.in \ 19 | testdata/table_dump.out \ 20 | testdata/ecmp.json \ 21 | table_dump_extra.test \ 22 | testdata/table_dump_extra.in \ 23 | testdata/table_dump_extra.out \ 24 | testdata/table_dump_extra.json \ 25 | indirect_res.test \ 26 | testdata/indirect_res.in \ 27 | testdata/indirect_res.out \ 28 | testdata/indirect_res.json \ 29 | mc_dump.test \ 30 | testdata/mc_dump.in \ 31 | testdata/mc_dump.out 32 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/indirect_res.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata indirect_res indirect_res.json 3 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/mc_dump.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata mc_dump ecmp.json --pre SimplePreLAG 3 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/table_dump.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata table_dump ecmp.json 3 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/table_dump_extra.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata table_dump_extra table_dump_extra.json 3 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/table_dump_old.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./run_one_test.py $srcdir/testdata table_dump_old ecmp.json 3 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/.gitignore: -------------------------------------------------------------------------------- 1 | !*.out 2 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/indirect_res.in: -------------------------------------------------------------------------------- 1 | counter_read my_indirect_counter 0 2 | counter_read my_indirect_counter 16 3 | meter_set_rates my_indirect_meter 0 1:1 2:1 4 | meter_set_rates my_indirect_meter 16 1:1 2:1 5 | meter_set_rates my_indirect_meter 0 2:1 1:1 6 | meter_get_rates my_indirect_meter 0 7 | register_read my_register 0 8 | register_read my_register 16 9 | register_write my_register 0 1 10 | register_write my_register 16 1 11 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/indirect_res.out: -------------------------------------------------------------------------------- 1 | ???? 2 | my_indirect_counter[0]= (0 bytes, 0 packets) 3 | ???? 4 | Invalid counter operation (INVALID_INDEX) 5 | ???? 6 | ???? 7 | Invalid meter operation (INVALID_INDEX) 8 | ???? 9 | Invalid meter operation (INVALID_INFO_RATE_VALUE) 10 | ???? 11 | 0: info rate = 1.0, burst size = 1 12 | 1: info rate = 2.0, burst size = 1 13 | ???? 14 | my_register[0]= 0 15 | ???? 16 | Invalid register operation (INVALID_INDEX) 17 | ???? 18 | ???? 19 | Invalid register operation (INVALID_INDEX) 20 | ???? 21 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/indirect_res.p4: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013-present Barefoot Networks, Inc. 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 | parser start { 18 | return ingress; 19 | } 20 | 21 | header_type meta_t { 22 | fields { 23 | f : 32; 24 | } 25 | } 26 | 27 | header meta_t meta; 28 | 29 | counter my_indirect_counter { 30 | type: packets; 31 | static: m_table; 32 | instance_count: 16; 33 | } 34 | 35 | meter my_indirect_meter { 36 | type: packets; 37 | static: m_table; 38 | instance_count: 16; 39 | } 40 | 41 | register my_register { 42 | width: 32; 43 | static: m_table; 44 | instance_count: 16; 45 | } 46 | 47 | action m_action() { 48 | count(my_indirect_counter, 1); 49 | execute_meter(my_indirect_meter, 1, meta.f); 50 | register_write(my_register, 1, 0xab); 51 | } 52 | 53 | table m_table { 54 | actions { m_action; } 55 | size : 1024; 56 | } 57 | 58 | control ingress { 59 | apply(m_table); 60 | } 61 | 62 | control egress { 63 | } 64 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/mc_dump.in: -------------------------------------------------------------------------------- 1 | mc_mgrp_create 1 2 | mc_node_create 2 1 5 8 | 1 3 3 | mc_node_associate 1 0 4 | mc_node_create 4 5 8 | 1 2 5 | mc_node_associate 1 1 6 | mc_set_lag_membership 1 6 9 12 7 | mc_dump 8 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/mc_dump.out: -------------------------------------------------------------------------------- 1 | ???? 2 | Creating multicast group 1 3 | ???? 4 | Creating node with rid 2 , port map 100100010 and lag map 1010 5 | node was created with handle 0 6 | ???? 7 | Associating node 0 to multicast group 1 8 | ???? 9 | Creating node with rid 4 , port map 100100000 and lag map 110 10 | node was created with handle 1 11 | ???? 12 | Associating node 1 to multicast group 1 13 | ???? 14 | Setting lag membership: 1 <- 1001001000000 15 | ???? 16 | ========== 17 | MC ENTRIES 18 | ********** 19 | mgrp(1) 20 | -> (L1h=0, rid=2) -> (ports=[1, 5, 8], lags=[1, 3]) 21 | -> (L1h=1, rid=4) -> (ports=[5, 8], lags=[1, 2]) 22 | ========== 23 | LAGS 24 | lag(1) -> ports=[5, 8] 25 | ========== 26 | ???? 27 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/table_dump.in: -------------------------------------------------------------------------------- 1 | table_num_entries ecmp_group 2 | act_prof_create_group ecmp_action_profile 3 | act_prof_create_member ecmp_action_profile set_nhop 10.0.0.1 1 4 | table_indirect_add ecmp_group 10.0.0.1/12 => 0 5 | act_prof_add_member_to_group ecmp_action_profile 0 0 6 | table_indirect_add_with_group ecmp_group 10.0.1.1/32 => 0 7 | table_dump ecmp_group 8 | table_num_entries ecmp_group 9 | table_dump_entry ecmp_group 0 10 | act_prof_dump_member ecmp_action_profile 0 11 | act_prof_dump_group ecmp_action_profile 0 12 | table_dump_entry_from_key ecmp_group 10.0.0.1/12 13 | table_clear ecmp_group 14 | table_num_entries ecmp_group 15 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/table_dump.out: -------------------------------------------------------------------------------- 1 | table_dump_old.out -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/table_dump_extra.in: -------------------------------------------------------------------------------- 1 | table_dump empty_key 2 | table_set_default empty_key _nop 3 | table_dump empty_key 4 | table_reset_default empty_key 5 | table_dump empty_key 6 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/table_dump_extra.out: -------------------------------------------------------------------------------- 1 | ???? 2 | ========== 3 | TABLE ENTRIES 4 | ========== 5 | Dumping default entry 6 | EMPTY 7 | ========== 8 | ???? 9 | Setting default action of empty_key 10 | action: _nop 11 | runtime data: 12 | ???? 13 | ========== 14 | TABLE ENTRIES 15 | ========== 16 | Dumping default entry 17 | Action entry: _nop - 18 | ========== 19 | ???? 20 | ???? 21 | ========== 22 | TABLE ENTRIES 23 | ========== 24 | Dumping default entry 25 | EMPTY 26 | ========== 27 | ???? 28 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/table_dump_extra.p4: -------------------------------------------------------------------------------- 1 | parser start { 2 | return ingress; 3 | } 4 | 5 | action _nop() { } 6 | 7 | table empty_key { 8 | actions { _nop; } 9 | } 10 | 11 | control ingress { 12 | apply(empty_key); 13 | } 14 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/table_dump_old.in: -------------------------------------------------------------------------------- 1 | table_num_entries ecmp_group 2 | table_indirect_create_group ecmp_group 3 | table_indirect_create_member ecmp_group set_nhop 10.0.0.1 1 4 | table_indirect_add ecmp_group 10.0.0.1/12 => 0 5 | table_indirect_add_member_to_group ecmp_group 0 0 6 | table_indirect_add_with_group ecmp_group 10.0.1.1/32 => 0 7 | table_dump ecmp_group 8 | table_num_entries ecmp_group 9 | table_dump_entry ecmp_group 0 10 | table_dump_member ecmp_group 0 11 | table_dump_group ecmp_group 0 12 | table_dump_entry_from_key ecmp_group 10.0.0.1/12 13 | table_clear ecmp_group 14 | table_num_entries ecmp_group 15 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/CLI_tests/testdata/table_dump_old.out: -------------------------------------------------------------------------------- 1 | ???? 2 | 0 3 | ???? 4 | Group has been created with handle 0 5 | ???? 6 | Member has been created with handle 0 7 | ???? 8 | Adding entry to indirect match table ecmp_group 9 | Entry has been added with handle 0 10 | ???? 11 | ???? 12 | Adding entry to indirect match table ecmp_group 13 | Entry has been added with handle 1 14 | ???? 15 | ========== 16 | TABLE ENTRIES 17 | ********** 18 | Dumping entry 0x0 19 | Match key: 20 | * ipv4.dstAddr : LPM 0a000001/12 21 | Index: member(0) 22 | ********** 23 | Dumping entry 0x1 24 | Match key: 25 | * ipv4.dstAddr : LPM 0a000101/32 26 | Index: group(0) 27 | ========== 28 | MEMBERS 29 | ********** 30 | Dumping member 0 31 | Action entry: set_nhop - 0a000001, 01 32 | ========== 33 | GROUPS 34 | ********** 35 | Dumping group 0 36 | Members: [0] 37 | ========== 38 | Dumping default entry 39 | EMPTY 40 | ========== 41 | ???? 42 | 2 43 | ???? 44 | Dumping entry 0x0 45 | Match key: 46 | * ipv4.dstAddr : LPM 0a000001/12 47 | Index: member(0) 48 | ???? 49 | Dumping member 0 50 | Action entry: set_nhop - 0a000001, 01 51 | ???? 52 | Dumping group 0 53 | Members: [0] 54 | ???? 55 | Dumping entry 0x0 56 | Match key: 57 | * ipv4.dstAddr : LPM 0a000001/12 58 | Index: member(0) 59 | ???? 60 | ???? 61 | 0 62 | ???? 63 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/Makefile.am: -------------------------------------------------------------------------------- 1 | if COND_THRIFT 2 | MAYBE_CLI_TESTS = CLI_tests 3 | endif 4 | 5 | SUBDIRS = . $(MAYBE_CLI_TESTS) 6 | 7 | AM_CPPFLAGS += \ 8 | -isystem $(top_srcdir)/third_party/gtest/include \ 9 | -I$(srcdir)/.. \ 10 | -I$(srcdir)/ \ 11 | -I$(top_srcdir)/targets/test_utils \ 12 | -DTESTDATADIR=\"$(srcdir)/testdata\" 13 | 14 | LDADD = $(builddir)/../libsimpleswitch.la \ 15 | $(top_builddir)/third_party/gtest/libgtest.la \ 16 | $(top_builddir)/targets/test_utils/libtargetstestutils.la \ 17 | $(top_builddir)/src/bm_apps/libbmapps.la \ 18 | -lboost_filesystem 19 | 20 | # Define unit tests 21 | common_source = main.cpp ../../test_utils/utils.cpp ../../test_utils/utils.h 22 | TESTS = test_packet_redirect \ 23 | test_truncate \ 24 | test_swap \ 25 | test_queueing \ 26 | test_recirc \ 27 | test_parser_error 28 | 29 | check_PROGRAMS = $(TESTS) test_all 30 | 31 | # Sources for tests 32 | test_packet_redirect_SOURCES = $(common_source) test_packet_redirect.cpp 33 | test_truncate_SOURCES = $(common_source) test_truncate.cpp 34 | test_swap_SOURCES = $(common_source) test_swap.cpp 35 | test_queueing_SOURCES = $(common_source) test_queueing.cpp 36 | test_recirc_SOURCES = $(common_source) test_recirc.cpp 37 | test_parser_error_SOURCES = $(common_source) test_parser_error.cpp 38 | 39 | test_all_SOURCES = $(common_source) \ 40 | test_packet_redirect.cpp \ 41 | test_truncate.cpp \ 42 | test_swap.cpp \ 43 | test_queueing.cpp \ 44 | test_recirc.cpp \ 45 | test_parser_error.cpp 46 | 47 | EXTRA_DIST = \ 48 | testdata/packet_redirect.json \ 49 | testdata/truncate.json \ 50 | testdata/swap_1.json \ 51 | testdata/swap_2.json \ 52 | testdata/queueing.json \ 53 | testdata/recirc.json \ 54 | testdata/parser_error.p4 \ 55 | testdata/parser_error.json 56 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/main.cpp: -------------------------------------------------------------------------------- 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 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | bool WITH_VALGRIND = false; 28 | 29 | int main(int argc, char* argv[]) { 30 | ::testing::InitGoogleTest(&argc, argv); 31 | return RUN_ALL_TESTS(); 32 | } 33 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/testdata/swap_1.p4: -------------------------------------------------------------------------------- 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 | header_type hdr1_t { 17 | fields { 18 | f1 : 8; 19 | f2 : 8; 20 | } 21 | } 22 | 23 | header hdr1_t hdr1; 24 | 25 | parser start { 26 | extract(hdr1); 27 | return ingress; 28 | } 29 | 30 | action a11() { 31 | modify_field(standard_metadata.egress_spec, 1); 32 | } 33 | 34 | action a12() { 35 | modify_field(standard_metadata.egress_spec, 2); 36 | } 37 | 38 | table t_ingress_1 { 39 | reads { 40 | hdr1.f1 : exact; 41 | } 42 | actions { 43 | a11; a12; 44 | } 45 | size : 128; 46 | } 47 | 48 | control ingress { 49 | apply(t_ingress_1); 50 | } 51 | 52 | control egress { 53 | } 54 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/testdata/swap_2.p4: -------------------------------------------------------------------------------- 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 | header_type hdr2_t { 17 | fields { 18 | f1 : 8; 19 | f2 : 8; 20 | f3 : 16; 21 | } 22 | } 23 | 24 | header hdr2_t hdr2; 25 | 26 | parser start { 27 | extract(hdr2); 28 | return ingress; 29 | } 30 | 31 | action a21() { 32 | modify_field(standard_metadata.egress_spec, 3); 33 | } 34 | 35 | action a22() { 36 | modify_field(standard_metadata.egress_spec, 4); 37 | } 38 | 39 | table t_ingress_2 { 40 | reads { 41 | hdr2.f1 : exact; 42 | } 43 | actions { 44 | a21; a22; 45 | } 46 | size : 64; 47 | } 48 | 49 | control ingress { 50 | apply(t_ingress_2); 51 | } 52 | 53 | control egress { 54 | } 55 | -------------------------------------------------------------------------------- /targets/simple_switch/tests/testdata/truncate.p4: -------------------------------------------------------------------------------- 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 | header_type hdrA_t { 17 | fields { 18 | f1 : 8; 19 | f2 : 64; 20 | } 21 | } 22 | 23 | header hdrA_t hdrA; 24 | 25 | parser start { 26 | extract(hdrA); 27 | return ingress; 28 | } 29 | 30 | action _nop() { 31 | 32 | } 33 | 34 | action _truncate(new_length, port) { 35 | modify_field(standard_metadata.egress_spec, port); 36 | truncate(new_length); 37 | } 38 | 39 | table t_ingress { 40 | reads { 41 | hdrA.f1 : exact; 42 | } 43 | actions { 44 | _nop; _truncate; 45 | } 46 | size : 128; 47 | } 48 | 49 | control ingress { 50 | apply(t_ingress); 51 | } 52 | 53 | control egress { 54 | } 55 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/.gitignore: -------------------------------------------------------------------------------- 1 | m4/ltoptions.m4 2 | m4/ltsugar.m4 3 | m4/ltversion.m4 4 | m4/lt~obsolete.m4 5 | 6 | simple_switch_grpc 7 | 8 | tests/example 9 | tests/example.run 10 | tests/test_gtest 11 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | SUBDIRS = . tests 4 | 5 | AM_CPPFLAGS += \ 6 | -I$(srcdir)/../../include \ 7 | -I$(srcdir)/../simple_switch \ 8 | -I$(srcdir)/../../PI \ 9 | -isystem$(srcdir)/../../third_party/spdlog 10 | 11 | if WITH_SYSREPO 12 | AM_CPPFLAGS += -DWITH_SYSREPO 13 | endif 14 | 15 | if WITH_THRIFT 16 | AM_CPPFLAGS += -DWITH_THRIFT \ 17 | -I$(builddir)/../simple_switch/gen-cpp 18 | endif 19 | 20 | bin_PROGRAMS = simple_switch_grpc 21 | 22 | simple_switch_grpc_SOURCES = main.cpp 23 | 24 | simple_switch_grpc_LDADD = \ 25 | libsimple_switch_grpc.la 26 | 27 | # We follow this tutorial to link with grpc++_reflection: 28 | # https://github.com/grpc/grpc/blob/master/doc/server_reflection_tutorial.md 29 | simple_switch_grpc_LDFLAGS = \ 30 | -Wl,--no-as-needed,-lgrpc++_reflection,--as-needed \ 31 | -rdynamic 32 | 33 | noinst_LTLIBRARIES = libsimple_switch_grpc.la 34 | 35 | libsimple_switch_grpc_la_SOURCES = \ 36 | switch_runner.cpp switch_runner.h 37 | if WITH_SYSREPO 38 | libsimple_switch_grpc_la_SOURCES += \ 39 | switch_sysrepo.h switch_sysrepo.cpp 40 | endif 41 | 42 | libsimple_switch_grpc_la_LIBADD = \ 43 | $(builddir)/../simple_switch/libsimpleswitch.la \ 44 | $(builddir)/../../PI/libbmpi.la \ 45 | $(builddir)/../../services/libbm_grpc_dataplane.la 46 | 47 | if WITH_THRIFT 48 | libsimple_switch_grpc_la_LIBADD += \ 49 | $(builddir)/../../src/bm_runtime/libbmruntime.la \ 50 | $(builddir)/../../thrift_src/libruntimestubs.la \ 51 | $(builddir)/../simple_switch/libsimpleswitch_thrift.la 52 | endif 53 | 54 | libsimple_switch_grpc_la_LIBADD += \ 55 | -lpifeproto -lpigrpcserver -lpi -lpip4info \ 56 | $(GRPC_LIBS) $(PROTOBUF_LIBS) 57 | 58 | AM_CPPFLAGS += \ 59 | -I$(builddir)/../../services/cpp_out \ 60 | -I$(builddir)/../../services/grpc_out 61 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/m4/ax_cxx_compile_stdcxx_17.m4: -------------------------------------------------------------------------------- 1 | # ============================================================================= 2 | # https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_17.html 3 | # ============================================================================= 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CXX_COMPILE_STDCXX_17([ext|noext], [mandatory|optional]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check for baseline language coverage in the compiler for the C++17 12 | # standard; if necessary, add switches to CXX and CXXCPP to enable 13 | # support. 14 | # 15 | # This macro is a convenience alias for calling the AX_CXX_COMPILE_STDCXX 16 | # macro with the version set to C++17. The two optional arguments are 17 | # forwarded literally as the second and third argument respectively. 18 | # Please see the documentation for the AX_CXX_COMPILE_STDCXX macro for 19 | # more information. If you want to use this macro, you also need to 20 | # download the ax_cxx_compile_stdcxx.m4 file. 21 | # 22 | # LICENSE 23 | # 24 | # Copyright (c) 2015 Moritz Klammler 25 | # Copyright (c) 2016 Krzesimir Nowak 26 | # 27 | # Copying and distribution of this file, with or without modification, are 28 | # permitted in any medium without royalty provided the copyright notice 29 | # and this notice are preserved. This file is offered as-is, without any 30 | # warranty. 31 | 32 | #serial 2 33 | 34 | AX_REQUIRE_DEFINED([AX_CXX_COMPILE_STDCXX]) 35 | AC_DEFUN([AX_CXX_COMPILE_STDCXX_17], [AX_CXX_COMPILE_STDCXX([17], [$1], [$2])]) 36 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/switch_sysrepo.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 SIMPLE_SWITCH_GRPC_SWITCH_SYSREPO_H_ 22 | #define SIMPLE_SWITCH_GRPC_SWITCH_SYSREPO_H_ 23 | 24 | #include 25 | 26 | #include 27 | #include 28 | 29 | namespace bm { 30 | 31 | class DevMgr; 32 | 33 | } // namespace bm 34 | 35 | namespace sswitch_grpc { 36 | 37 | class PortStateMap; 38 | class SysrepoSubscriber; 39 | class SysrepoStateProvider; 40 | 41 | class SysrepoDriver { 42 | public: 43 | SysrepoDriver(bm::device_id_t device_id, bm::DevMgr *dev_mgr); 44 | ~SysrepoDriver(); 45 | 46 | bool start(); 47 | 48 | // Used to add interfaces provided on the command-line with --interface / -i 49 | void add_iface(int port, const std::string &name); 50 | 51 | private: 52 | const bm::device_id_t my_device_id; 53 | const bm::DevMgr *dev_mgr; // non-owning pointer 54 | std::unique_ptr port_state_map; 55 | std::unique_ptr sysrepo_subscriber; 56 | std::unique_ptr sysrepo_state_provider; 57 | }; 58 | 59 | } // namespace sswitch_grpc 60 | 61 | #endif // SIMPLE_SWITCH_GRPC_SWITCH_SYSREPO_H_ 62 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/example.run.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | @builddir@/../simple_switch_grpc --log-console --no-p4 --device-id 99 & 4 | switch_PID=$! 5 | # making sure switch is ready 6 | echo "Sleeping 10 seconds..." 7 | sleep 10 8 | 9 | @builddir@/example 10 | test_status=$? 11 | 12 | kill $switch_PID 13 | 14 | exit $test_status 15 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/action_profile.proto.txt: -------------------------------------------------------------------------------- 1 | pkg_info { 2 | arch: "v1model" 3 | } 4 | tables { 5 | preamble { 6 | id: 33586946 7 | name: "IndirectWS" 8 | alias: "IndirectWS" 9 | } 10 | match_fields { 11 | id: 1 12 | name: "h.hdr.in_" 13 | bitwidth: 8 14 | match_type: EXACT 15 | } 16 | action_refs { 17 | id: 16811746 18 | } 19 | action_refs { 20 | id: 16800567 21 | annotations: "@defaultonly" 22 | scope: DEFAULT_ONLY 23 | } 24 | implementation_id: 285237193 25 | size: 512 26 | } 27 | actions { 28 | preamble { 29 | id: 16800567 30 | name: "NoAction" 31 | alias: "NoAction" 32 | } 33 | } 34 | actions { 35 | preamble { 36 | id: 16811746 37 | name: "send" 38 | alias: "send" 39 | } 40 | params { 41 | id: 1 42 | name: "eg_port" 43 | bitwidth: 9 44 | } 45 | } 46 | action_profiles { 47 | preamble { 48 | id: 285237193 49 | name: "ActProfWS" 50 | alias: "ActProfWS" 51 | } 52 | table_ids: 33586946 53 | with_selector: true 54 | size: 128 55 | } 56 | type_info { 57 | } 58 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/clone.p4: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-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 | #include 17 | #include 18 | 19 | header Hdr { 20 | bit<16> f1; 21 | } 22 | 23 | struct Headers { 24 | Hdr hdr; 25 | } 26 | 27 | struct Meta { } 28 | 29 | parser p(packet_in b, out Headers h, 30 | inout Meta m, inout standard_metadata_t sm) { 31 | state start { 32 | b.extract(h.hdr); 33 | transition accept; 34 | } 35 | } 36 | 37 | control vrfy(inout Headers h, inout Meta m) { apply {} } 38 | control update(inout Headers h, inout Meta m) { apply {} } 39 | 40 | control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 41 | apply { 42 | clone(CloneType.I2E, (bit<32>)h.hdr.f1 /* session id */); 43 | sm.egress_spec = sm.ingress_port; 44 | } 45 | } 46 | 47 | control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 48 | apply { h.hdr.f1 = sm.egress_rid; } 49 | } 50 | 51 | control deparser(packet_out b, in Headers h) { 52 | apply { b.emit(h.hdr); } 53 | } 54 | 55 | V1Switch(p(), vrfy(), ingress(), egress(), update(), deparser()) main; 56 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/clone.proto.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/behavioral-model/512caf07145762329a74494dee836e51a4af79e6/targets/simple_switch_grpc/tests/testdata/clone.proto.txt -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/counter.p4: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, Google 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 | #include 16 | #include 17 | 18 | struct Headers {} 19 | 20 | struct Meta {} 21 | 22 | parser p(packet_in b, out Headers h, 23 | inout Meta m, inout standard_metadata_t sm) { 24 | state start { 25 | transition accept; 26 | } 27 | } 28 | 29 | control vrfy(inout Headers h, inout Meta m) { apply {} } 30 | control update(inout Headers h, inout Meta m) { apply {} } 31 | 32 | control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 33 | apply {} 34 | } 35 | 36 | control deparser(packet_out b, in Headers h) { 37 | apply {} 38 | } 39 | 40 | control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 41 | @name(".cntr") 42 | direct_counter(CounterType.packets_and_bytes) cntr; 43 | @name(".port_redirect") 44 | action port_redirect() { 45 | sm.egress_spec = sm.ingress_port; 46 | cntr.count(); 47 | } 48 | @name(".t_redirect") 49 | table t_redirect { 50 | key = { sm.packet_length : exact; } 51 | actions = { port_redirect; } 52 | counters = cntr; 53 | } 54 | apply { t_redirect.apply(); } 55 | } 56 | 57 | V1Switch(p(), vrfy(), ingress(), egress(), update(), deparser()) main; 58 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/counter.proto.txt: -------------------------------------------------------------------------------- 1 | tables { 2 | preamble { 3 | id: 33591959 4 | name: "t_redirect" 5 | alias: "t_redirect" 6 | } 7 | match_fields { 8 | id: 1 9 | name: "sm.packet_length" 10 | bitwidth: 32 11 | match_type: EXACT 12 | } 13 | action_refs { 14 | id: 16795932 15 | } 16 | action_refs { 17 | id: 16800567 18 | annotations: "@defaultonly()" 19 | } 20 | direct_resource_ids: 318799756 21 | size: 1024 22 | } 23 | actions { 24 | preamble { 25 | id: 16800567 26 | name: "NoAction" 27 | alias: "NoAction" 28 | } 29 | } 30 | actions { 31 | preamble { 32 | id: 16795932 33 | name: "port_redirect" 34 | alias: "port_redirect" 35 | } 36 | } 37 | direct_counters { 38 | preamble { 39 | id: 318799756 40 | name: "cntr" 41 | alias: "cntr" 42 | } 43 | spec { 44 | unit: BOTH 45 | } 46 | direct_table_id: 33591959 47 | } 48 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/digest.proto.txt: -------------------------------------------------------------------------------- 1 | pkg_info { 2 | arch: "v1model" 3 | } 4 | tables { 5 | preamble { 6 | id: 33610141 7 | name: "ingress.smac" 8 | alias: "smac" 9 | } 10 | match_fields { 11 | id: 1 12 | name: "h.ethernet.smac" 13 | bitwidth: 48 14 | match_type: EXACT 15 | } 16 | action_refs { 17 | id: 16783753 18 | } 19 | action_refs { 20 | id: 16800567 21 | } 22 | size: 4096 23 | idle_timeout_behavior: NOTIFY_CONTROL 24 | } 25 | actions { 26 | preamble { 27 | id: 16800567 28 | name: "NoAction" 29 | alias: "NoAction" 30 | } 31 | } 32 | actions { 33 | preamble { 34 | id: 16783753 35 | name: "ingress.send_digest" 36 | alias: "send_digest" 37 | } 38 | } 39 | digests { 40 | preamble { 41 | id: 385912120 42 | name: "L2_digest" 43 | alias: "L2_digest" 44 | } 45 | type_spec { 46 | struct { 47 | name: "L2_digest" 48 | } 49 | } 50 | } 51 | type_info { 52 | structs { 53 | key: "L2_digest" 54 | value { 55 | members { 56 | name: "smac" 57 | type_spec { 58 | bitstring { 59 | bit { 60 | bitwidth: 48 61 | } 62 | } 63 | } 64 | } 65 | members { 66 | name: "ig_port" 67 | type_spec { 68 | bitstring { 69 | bit { 70 | bitwidth: 9 71 | } 72 | } 73 | } 74 | } 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/loopback.p4: -------------------------------------------------------------------------------- 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 | parser start { 17 | return ingress; 18 | } 19 | 20 | action redirect() { 21 | modify_field(standard_metadata.egress_spec, standard_metadata.ingress_port); 22 | } 23 | 24 | table t_redirect { 25 | actions { redirect; } 26 | default_action: redirect(); 27 | } 28 | 29 | control ingress { 30 | apply(t_redirect); 31 | } 32 | 33 | control egress { } 34 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/loopback.proto.txt: -------------------------------------------------------------------------------- 1 | tables { 2 | preamble { 3 | id: 33591959 4 | name: "t_redirect" 5 | alias: "t_redirect" 6 | } 7 | action_refs { 8 | id: 16794474 9 | } 10 | size: 1024 11 | } 12 | actions { 13 | preamble { 14 | id: 16794474 15 | name: "redirect" 16 | alias: "redirect" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/meter.p4: -------------------------------------------------------------------------------- 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 | #include 17 | #include 18 | 19 | struct Headers {} 20 | 21 | struct Meta { 22 | bit<3> color; 23 | } 24 | 25 | parser p(packet_in b, out Headers h, 26 | inout Meta m, inout standard_metadata_t sm) { 27 | state start { 28 | transition accept; 29 | } 30 | } 31 | 32 | control vrfy(inout Headers h, inout Meta m) { apply {} } 33 | control update(inout Headers h, inout Meta m) { apply {} } 34 | 35 | control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 36 | apply {} 37 | } 38 | 39 | control deparser(packet_out b, in Headers h) { 40 | apply {} 41 | } 42 | 43 | control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 44 | direct_meter >(MeterType.packets) mtr; 45 | action port_redirect() { 46 | sm.egress_spec = sm.ingress_port; 47 | mtr.read(m.color); 48 | } 49 | table t_redirect { 50 | key = { sm.ingress_port : exact; } 51 | actions = { port_redirect; } 52 | meters = mtr; 53 | } 54 | apply { t_redirect.apply(); } 55 | } 56 | 57 | V1Switch(p(), vrfy(), ingress(), egress(), update(), deparser()) main; 58 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/meter.proto.txt: -------------------------------------------------------------------------------- 1 | tables { 2 | preamble { 3 | id: 33579641 4 | name: "ingress.t_redirect" 5 | alias: "t_redirect" 6 | } 7 | match_fields { 8 | id: 1 9 | name: "sm.ingress_port" 10 | bitwidth: 9 11 | match_type: EXACT 12 | } 13 | action_refs { 14 | id: 16793582 15 | } 16 | action_refs { 17 | id: 16800567 18 | annotations: "@defaultonly()" 19 | } 20 | direct_resource_ids: 352362736 21 | size: 1024 22 | } 23 | actions { 24 | preamble { 25 | id: 16800567 26 | name: "NoAction" 27 | alias: "NoAction" 28 | } 29 | } 30 | actions { 31 | preamble { 32 | id: 16793582 33 | name: "ingress.port_redirect" 34 | alias: "port_redirect" 35 | } 36 | } 37 | direct_meters { 38 | preamble { 39 | id: 352362736 40 | name: "ingress.mtr" 41 | alias: "mtr" 42 | } 43 | spec { 44 | unit: PACKETS 45 | } 46 | direct_table_id: 33579641 47 | } 48 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/multicast.p4: -------------------------------------------------------------------------------- 1 | /* Copyright 2018-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 | #include 17 | #include 18 | 19 | header Hdr { 20 | bit<16> f1; 21 | } 22 | 23 | struct Headers { 24 | Hdr hdr; 25 | } 26 | 27 | struct Meta { } 28 | 29 | parser p(packet_in b, out Headers h, 30 | inout Meta m, inout standard_metadata_t sm) { 31 | state start { 32 | b.extract(h.hdr); 33 | transition accept; 34 | } 35 | } 36 | 37 | control vrfy(inout Headers h, inout Meta m) { apply {} } 38 | control update(inout Headers h, inout Meta m) { apply {} } 39 | 40 | control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 41 | apply { sm.mcast_grp = h.hdr.f1; } 42 | } 43 | 44 | control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 45 | apply { h.hdr.f1 = sm.egress_rid; } 46 | } 47 | 48 | control deparser(packet_out b, in Headers h) { 49 | apply { b.emit(h.hdr); } 50 | } 51 | 52 | V1Switch(p(), vrfy(), ingress(), egress(), update(), deparser()) main; 53 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/multicast.proto.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/behavioral-model/512caf07145762329a74494dee836e51a4af79e6/targets/simple_switch_grpc/tests/testdata/multicast.proto.txt -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/optional.p4: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 VMware, 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 | #include 17 | #include 18 | 19 | header Hdr { 20 | bit<16> f1; 21 | } 22 | 23 | struct Headers { 24 | Hdr hdr; 25 | } 26 | 27 | struct Meta { } 28 | 29 | parser p(packet_in b, out Headers h, 30 | inout Meta m, inout standard_metadata_t sm) { 31 | state start { 32 | b.extract(h.hdr); 33 | transition accept; 34 | } 35 | } 36 | 37 | control vrfy(inout Headers h, inout Meta m) { apply {} } 38 | control update(inout Headers h, inout Meta m) { apply {} } 39 | 40 | control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 41 | action send_1() { sm.egress_spec = 1; } 42 | action send_2() { sm.egress_spec = 2; } 43 | table opt { 44 | key = { h.hdr.f1 : optional; } 45 | actions = { NoAction; send_1; send_2; } 46 | default_action = NoAction(); 47 | } 48 | apply { opt.apply(); } 49 | } 50 | 51 | control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 52 | apply { } 53 | } 54 | 55 | control deparser(packet_out b, in Headers h) { 56 | apply { b.emit(h.hdr); } 57 | } 58 | 59 | V1Switch(p(), vrfy(), ingress(), egress(), update(), deparser()) main; 60 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/optional.proto.txt: -------------------------------------------------------------------------------- 1 | pkg_info { 2 | arch: "v1model" 3 | } 4 | tables { 5 | preamble { 6 | id: 33607135 7 | name: "ingress.opt" 8 | alias: "opt" 9 | } 10 | match_fields { 11 | id: 1 12 | name: "h.hdr.f1" 13 | bitwidth: 16 14 | match_type: OPTIONAL 15 | } 16 | action_refs { 17 | id: 16800567 18 | } 19 | action_refs { 20 | id: 16806341 21 | } 22 | action_refs { 23 | id: 16813617 24 | } 25 | size: 1024 26 | } 27 | actions { 28 | preamble { 29 | id: 16800567 30 | name: "NoAction" 31 | alias: "NoAction" 32 | } 33 | } 34 | actions { 35 | preamble { 36 | id: 16806341 37 | name: "ingress.send_1" 38 | alias: "send_1" 39 | } 40 | } 41 | actions { 42 | preamble { 43 | id: 16813617 44 | name: "ingress.send_2" 45 | alias: "send_2" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/packet_io.p4: -------------------------------------------------------------------------------- 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 | #define CPU_PORT 64 17 | 18 | parser start { 19 | return ingress; 20 | } 21 | 22 | action redirect() { modify_field(standard_metadata.egress_spec, CPU_PORT); } 23 | 24 | table t_redirect { 25 | actions { redirect; } 26 | default_action: redirect(); 27 | } 28 | 29 | control ingress { 30 | apply(t_redirect); 31 | } 32 | 33 | control egress { } 34 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/packet_io.proto.txt: -------------------------------------------------------------------------------- 1 | tables { 2 | preamble { 3 | id: 33591959 4 | name: "t_redirect" 5 | alias: "t_redirect" 6 | } 7 | action_refs { 8 | id: 16794474 9 | } 10 | size: 1024 11 | } 12 | actions { 13 | preamble { 14 | id: 16794474 15 | name: "redirect" 16 | alias: "redirect" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/ternary.p4: -------------------------------------------------------------------------------- 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 | #include 17 | #include 18 | 19 | header Hdr { 20 | bit<16> f1; 21 | } 22 | 23 | struct Headers { 24 | Hdr hdr; 25 | } 26 | 27 | struct Meta { } 28 | 29 | parser p(packet_in b, out Headers h, 30 | inout Meta m, inout standard_metadata_t sm) { 31 | state start { 32 | b.extract(h.hdr); 33 | transition accept; 34 | } 35 | } 36 | 37 | control vrfy(inout Headers h, inout Meta m) { apply {} } 38 | control update(inout Headers h, inout Meta m) { apply {} } 39 | 40 | control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 41 | action send_1() { sm.egress_spec = 1; } 42 | action send_2() { sm.egress_spec = 2; } 43 | table ter { 44 | key = { h.hdr.f1 : ternary; } 45 | actions = { NoAction; send_1; send_2; } 46 | default_action = NoAction(); 47 | } 48 | apply { ter.apply(); } 49 | } 50 | 51 | control egress(inout Headers h, inout Meta m, inout standard_metadata_t sm) { 52 | apply { } 53 | } 54 | 55 | control deparser(packet_out b, in Headers h) { 56 | apply { b.emit(h.hdr); } 57 | } 58 | 59 | V1Switch(p(), vrfy(), ingress(), egress(), update(), deparser()) main; 60 | -------------------------------------------------------------------------------- /targets/simple_switch_grpc/tests/testdata/ternary.proto.txt: -------------------------------------------------------------------------------- 1 | tables { 2 | preamble { 3 | id: 33596279 4 | name: "ingress.ter" 5 | alias: "ter" 6 | } 7 | match_fields { 8 | id: 1 9 | name: "h.hdr.f1" 10 | bitwidth: 16 11 | match_type: TERNARY 12 | } 13 | action_refs { 14 | id: 16800567 15 | } 16 | action_refs { 17 | id: 16806341 18 | } 19 | action_refs { 20 | id: 16813617 21 | } 22 | size: 1024 23 | } 24 | actions { 25 | preamble { 26 | id: 16800567 27 | name: "NoAction" 28 | alias: "NoAction" 29 | } 30 | } 31 | actions { 32 | preamble { 33 | id: 16806341 34 | name: "ingress.send_1" 35 | alias: "send_1" 36 | } 37 | } 38 | actions { 39 | preamble { 40 | id: 16813617 41 | name: "ingress.send_2" 42 | alias: "send_2" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /targets/test_utils/Makefile.am: -------------------------------------------------------------------------------- 1 | check_LTLIBRARIES = libtargetstestutils.la 2 | 3 | libtargetstestutils_la_SOURCES = \ 4 | utils.cpp \ 5 | utils.h 6 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | test* 2 | !test*.cpp 3 | !stress_tests* 4 | !testdata 5 | utils.cpp 6 | -------------------------------------------------------------------------------- /tests/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | # for tests we relax a lot of constraints (mostly interested in format checks) 2 | filter=-build/namespaces 3 | filter=-runtime/explicit 4 | filter=-runtime/references 5 | filter=-runtime/printf 6 | filter=-runtime/threadsafe_fn 7 | -------------------------------------------------------------------------------- /tests/main.cpp: -------------------------------------------------------------------------------- 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 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | bool WITH_VALGRIND = false; 28 | 29 | int main(int argc, char* argv[]) { 30 | ::testing::InitGoogleTest(&argc, argv); 31 | for (int i = 0; i < argc; i++) { 32 | if (strcmp(argv[i], "--valgrind") == 0) { 33 | WITH_VALGRIND = true; 34 | } 35 | } 36 | return RUN_ALL_TESTS(); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /tests/stress_tests/.gitignore: -------------------------------------------------------------------------------- 1 | test* 2 | !test*.cpp 3 | !testdata 4 | -------------------------------------------------------------------------------- /tests/stress_tests/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS += \ 2 | -I$(top_srcdir)/src/BMI \ 3 | -isystem $(top_srcdir)/third_party \ 4 | -DTESTDATADIR=\"$(abs_srcdir)/testdata\" 5 | LDADD = \ 6 | $(top_builddir)/third_party/gtest/libgtest.la \ 7 | $(top_builddir)/src/bm_apps/libbmapps.la \ 8 | $(top_builddir)/src/bm_sim/libbmsim.la \ 9 | $(top_builddir)/src/bf_lpm_trie/libbflpmtrie.la \ 10 | $(top_builddir)/third_party/jsoncpp/libjson.la \ 11 | -lboost_system -lboost_filesystem -lboost_program_options 12 | 13 | # TODO(antonin): should the traffic.bin files be generated at compile time by 14 | # running the Python generator (instead of being checked in)? 15 | 16 | common_source = ../bmi_stubs.c stress_utils.h stress_utils.cpp 17 | 18 | TESTS = \ 19 | test_parser_deparser_1 \ 20 | test_exact_match_1 \ 21 | test_LPM_match_1 \ 22 | test_ternary_match_1 23 | 24 | check_PROGRAMS = $(TESTS) 25 | 26 | test_parser_deparser_1_SOURCES = $(common_source) test_parser_deparser_1.cpp 27 | test_exact_match_1_SOURCES = $(common_source) test_exact_match_1.cpp 28 | test_LPM_match_1_SOURCES = $(common_source) test_LPM_match_1.cpp 29 | test_ternary_match_1_SOURCES = $(common_source) test_ternary_match_1.cpp 30 | 31 | EXTRA_DIST = \ 32 | testdata/parser_deparser_1.p4 \ 33 | testdata/parser_deparser_1.json \ 34 | testdata/exact_match_1.p4 \ 35 | testdata/exact_match_1.json \ 36 | testdata/LPM_match_1.p4 \ 37 | testdata/LPM_match_1.json \ 38 | testdata/ternary_match_1.p4 \ 39 | testdata/ternary_match_1.json \ 40 | testdata/udp_tcp_traffic.bin 41 | -------------------------------------------------------------------------------- /tests/stress_tests/get_stats.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | 5 | TESTS="test_parser_deparser_1 \ 6 | test_exact_match_1 \ 7 | test_LPM_match_1 \ 8 | test_ternary_match_1" 9 | 10 | nruns=5 11 | if [ $# -eq 1 ]; then 12 | nruns=$1 13 | fi 14 | 15 | median_idx=$(($nruns / 2)) 16 | 17 | do_runs() { 18 | local tname=$1 19 | local n=$2 20 | local pps_list="" 21 | local pps="" 22 | for i in `seq 1 $n`; do 23 | pps=$($THIS_DIR/$tname | awk '/Switch was processing/{ print $4 }') 24 | pps_list="$pps_list $pps" 25 | done 26 | echo $pps_list 27 | } 28 | 29 | for tname in $TESTS; do 30 | pps_list=$(do_runs $tname $nruns) 31 | pps_list=$(echo $pps_list | tr " " "\n" | sort -g | tr "\n" " ") 32 | pps_list=($pps_list) 33 | median_pps=${pps_list[$median_idx]} 34 | echo "$tname $median_pps" 35 | done 36 | -------------------------------------------------------------------------------- /tests/stress_tests/testdata/LPM_match_1.p4: -------------------------------------------------------------------------------- 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 | header_type ethernet_t { 17 | fields { 18 | dstAddr : 48; 19 | srcAddr : 48; 20 | etherType : 16; 21 | } 22 | } 23 | 24 | parser start { 25 | return parse_ethernet; 26 | } 27 | 28 | header ethernet_t ethernet; 29 | 30 | parser parse_ethernet { 31 | extract(ethernet); 32 | return ingress; 33 | } 34 | 35 | action _nop() { } 36 | 37 | table LPM_1 { 38 | reads { 39 | ethernet.dstAddr : lpm; 40 | } 41 | actions { _nop; } 42 | size : 65536; 43 | } 44 | 45 | table LPM_2 { 46 | reads { 47 | ethernet.srcAddr : lpm; 48 | } 49 | actions { _nop; } 50 | size : 65536; 51 | } 52 | 53 | table LPM_3 { 54 | reads { 55 | ethernet.srcAddr : lpm; 56 | ethernet.dstAddr : exact; 57 | } 58 | actions { _nop; } 59 | size : 65536; 60 | } 61 | 62 | control ingress { 63 | apply(LPM_1); 64 | apply(LPM_2); 65 | apply(LPM_3); 66 | } 67 | 68 | control egress { } 69 | -------------------------------------------------------------------------------- /tests/stress_tests/testdata/exact_match_1.p4: -------------------------------------------------------------------------------- 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 | header_type ethernet_t { 17 | fields { 18 | dstAddr : 48; 19 | srcAddr : 48; 20 | etherType : 16; 21 | } 22 | } 23 | 24 | parser start { 25 | return parse_ethernet; 26 | } 27 | 28 | header ethernet_t ethernet; 29 | 30 | parser parse_ethernet { 31 | extract(ethernet); 32 | return ingress; 33 | } 34 | 35 | action _nop() { } 36 | 37 | table exact_1 { 38 | reads { 39 | ethernet.dstAddr : exact; 40 | } 41 | actions { _nop; } 42 | size : 65536; 43 | } 44 | 45 | table exact_2 { 46 | reads { 47 | ethernet.srcAddr : exact; 48 | } 49 | actions { _nop; } 50 | size : 65536; 51 | } 52 | 53 | table exact_3 { 54 | reads { 55 | ethernet.srcAddr : exact; 56 | ethernet.dstAddr : exact; 57 | } 58 | actions { _nop; } 59 | size : 65536; 60 | } 61 | 62 | control ingress { 63 | apply(exact_1); 64 | apply(exact_2); 65 | apply(exact_3); 66 | } 67 | 68 | control egress { } 69 | -------------------------------------------------------------------------------- /tests/stress_tests/testdata/ternary_match_1.p4: -------------------------------------------------------------------------------- 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 | header_type ethernet_t { 17 | fields { 18 | dstAddr : 48; 19 | srcAddr : 48; 20 | etherType : 16; 21 | } 22 | } 23 | 24 | parser start { 25 | return parse_ethernet; 26 | } 27 | 28 | header ethernet_t ethernet; 29 | 30 | parser parse_ethernet { 31 | extract(ethernet); 32 | return ingress; 33 | } 34 | 35 | action _nop() { } 36 | 37 | table ternary_1 { 38 | reads { 39 | ethernet.dstAddr : ternary; 40 | } 41 | actions { _nop; } 42 | size : 65536; 43 | } 44 | 45 | table ternary_2 { 46 | reads { 47 | ethernet.srcAddr : ternary; 48 | } 49 | actions { _nop; } 50 | size : 65536; 51 | } 52 | 53 | table ternary_3 { 54 | reads { 55 | ethernet.srcAddr : ternary; 56 | ethernet.dstAddr : ternary; 57 | } 58 | actions { _nop; } 59 | size : 65536; 60 | } 61 | 62 | control ingress { 63 | apply(ternary_1); 64 | apply(ternary_2); 65 | apply(ternary_3); 66 | } 67 | 68 | control egress { } 69 | -------------------------------------------------------------------------------- /tests/stress_tests/testdata/udp_tcp_traffic.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/behavioral-model/512caf07145762329a74494dee836e51a4af79e6/tests/stress_tests/testdata/udp_tcp_traffic.bin -------------------------------------------------------------------------------- /tests/test_ras.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 VMware, 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 18 | * 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | using bm::RandAccessUIntSet; 29 | 30 | class RandomAccessSetTest : public ::testing::Test { }; 31 | 32 | TEST_F(RandomAccessSetTest, LargeTestGetNth) { 33 | RandAccessUIntSet ras; 34 | std::vector mbrs; 35 | int size = 128; 36 | for (auto i = 0; i < size; i++) { 37 | auto mbr = static_cast(rand() % 65536); 38 | ras.add(mbr); 39 | mbrs.push_back(mbr); 40 | } 41 | std::sort(mbrs.begin(), mbrs.end()); 42 | size_t nth = 0; 43 | for (auto i = 0; i < 10000; i++) { 44 | auto mbr = ras.get_nth(nth); 45 | ASSERT_EQ(mbrs[nth], mbr); 46 | nth = (nth + 10) % size; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /tests/testdata/config_options.json: -------------------------------------------------------------------------------- 1 | { 2 | "config_options" : { 3 | "key1" : "aaa", 4 | "key2" : 12345 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/testdata/empty_config.json: -------------------------------------------------------------------------------- 1 | { } 2 | -------------------------------------------------------------------------------- /tests/testdata/empty_config.md5: -------------------------------------------------------------------------------- 1 | 73c388c9c0318194369fdba26cb36d68 -------------------------------------------------------------------------------- /tests/testdata/en0.pcap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/behavioral-model/512caf07145762329a74494dee836e51a4af79e6/tests/testdata/en0.pcap -------------------------------------------------------------------------------- /tests/testdata/header_stack.p4: -------------------------------------------------------------------------------- 1 | header_type hdr_t { 2 | fields { 3 | f1 : 16; 4 | f2 : 16; 5 | } 6 | } 7 | 8 | header hdr_t hdr[3]; 9 | 10 | parser start { 11 | extract(hdr[0]); 12 | extract(hdr[1]); 13 | return ingress; 14 | } 15 | 16 | action a1(v1) { 17 | modify_field(hdr[1].f1, v1); 18 | pop(hdr, 1); 19 | } 20 | 21 | table t1 { 22 | actions { a1; } 23 | } 24 | 25 | control ingress { 26 | apply(t1); 27 | } 28 | 29 | control egress { } 30 | -------------------------------------------------------------------------------- /tests/testdata/lo0.pcap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p4lang/behavioral-model/512caf07145762329a74494dee836e51a4af79e6/tests/testdata/lo0.pcap -------------------------------------------------------------------------------- /tests/testdata/one_extern.json: -------------------------------------------------------------------------------- 1 | { 2 | "extern_instances": [ 3 | { 4 | "name": "extern_1", 5 | "id": 0, 6 | "type": "DummyExtern", 7 | "attribute_values": [] 8 | } 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /tests/testdata/one_header.json: -------------------------------------------------------------------------------- 1 | { 2 | "header_types": [{"name": "hdr_t", "id": 0, "fields": [ 3 | ["f1", 32], ["f2", 32], ["f3", 32]]}], 4 | "headers": [{"name": "hdr", "id": 0, "header_type": "hdr_t"}] 5 | } 6 | -------------------------------------------------------------------------------- /tests/testdata/parse_vset.p4: -------------------------------------------------------------------------------- 1 | // Standard L2 Ethernet header 2 | header_type ethernet_t { 3 | fields { 4 | dst_addr : 48; // width in bits 5 | src_addr : 48; 6 | ethertype : 16; 7 | } 8 | } 9 | 10 | header ethernet_t ethernet; 11 | 12 | header_type my_header_t { 13 | fields { 14 | f1 : 3; 15 | f2 : 13; 16 | } 17 | } 18 | 19 | header my_header_t my_header; 20 | 21 | parser_value_set pv1; 22 | parser_value_set pv2; 23 | 24 | header_type my_meta_t { 25 | fields { 26 | pv_parsed : 8; 27 | } 28 | } 29 | 30 | metadata my_meta_t my_meta; 31 | 32 | parser start { 33 | extract(ethernet); 34 | return select(ethernet.ethertype) { 35 | 0x0800 : ingress; 36 | pv1 : parse_pv1; 37 | pv2 : parse_pv2; 38 | default : ingress; 39 | } 40 | } 41 | 42 | parser parse_pv1 { 43 | set_metadata(my_meta.pv_parsed, 1); 44 | extract(my_header); 45 | return select(my_header.f1, my_header.f2) { 46 | pv2 : parse_pv2; 47 | default : ingress; 48 | } 49 | } 50 | 51 | parser parse_pv2 { 52 | set_metadata(my_meta.pv_parsed, 2); 53 | return ingress; 54 | } 55 | 56 | action route_eth(egress_spec, src_addr) { 57 | modify_field(standard_metadata.egress_spec, egress_spec); 58 | modify_field(ethernet.src_addr, src_addr); 59 | } 60 | action noop() { } 61 | 62 | table routing { 63 | reads { 64 | ethernet.dst_addr : lpm; 65 | } 66 | actions { 67 | route_eth; 68 | noop; 69 | } 70 | } 71 | 72 | control ingress { 73 | apply(routing); 74 | } 75 | -------------------------------------------------------------------------------- /tests/testdata/pvs_struct_2.p4: -------------------------------------------------------------------------------- 1 | // P4 program originally written by Andy Fingerhut (@jafingerhut) for the 2 | // p4lang/p4c testsuite, as pvs-struct-2-bmv2.p4. 3 | 4 | #include 5 | 6 | header data_h { 7 | bit<32> da; 8 | bit<32> db; 9 | } 10 | 11 | struct my_packet { 12 | data_h data; 13 | } 14 | 15 | struct my_metadata { 16 | data_h[2] data; 17 | } 18 | 19 | struct value_set_t { 20 | bit<16> field; 21 | bit<3> field2; 22 | } 23 | 24 | parser MyParser(packet_in b, out my_packet p, inout my_metadata m, inout standard_metadata_t s) { 25 | 26 | value_set(4) pvs; 27 | 28 | state start { 29 | b.extract(p.data); 30 | transition select(p.data.da[15:0], p.data.db[7:5]) { 31 | pvs: accept; 32 | (0x810, 0x1) : foo; 33 | } 34 | } 35 | 36 | state foo { 37 | transition accept; 38 | } 39 | } 40 | 41 | control MyVerifyChecksum(inout my_packet hdr, inout my_metadata meta) { 42 | apply { } 43 | } 44 | 45 | 46 | control MyIngress(inout my_packet p, inout my_metadata meta, inout standard_metadata_t s) { 47 | action set_data() { 48 | } 49 | table t { 50 | actions = { set_data; } 51 | key = { meta.data[0].da : exact;} 52 | } 53 | apply { 54 | t.apply(); 55 | } 56 | } 57 | 58 | control MyEgress(inout my_packet p, inout my_metadata m, inout standard_metadata_t s) { 59 | apply { 60 | } 61 | } 62 | 63 | control MyComputeChecksum(inout my_packet p, inout my_metadata m) { 64 | apply { } 65 | } 66 | 67 | control MyDeparser(packet_out b, in my_packet p) { 68 | apply { } 69 | } 70 | 71 | V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main; 72 | -------------------------------------------------------------------------------- /tests/testdata/runtime_iface.p4: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013-present Barefoot Networks, Inc. 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 | parser start { 18 | return ingress; 19 | } 20 | 21 | header_type meta_t { 22 | fields { 23 | f : 32; 24 | } 25 | } 26 | 27 | header meta_t meta; 28 | 29 | counter my_indirect_counter { 30 | type: packets; 31 | static: m_table; 32 | instance_count: 16; 33 | } 34 | 35 | meter my_indirect_meter { 36 | type: packets; 37 | static: m_table; 38 | instance_count: 16; 39 | } 40 | 41 | register my_register { 42 | width: 32; 43 | static: m_table; 44 | instance_count: 16; 45 | } 46 | 47 | action m_action() { 48 | count(my_indirect_counter, 1); 49 | execute_meter(my_indirect_meter, 1, meta.f); 50 | register_write(my_register, 1, 0xab); 51 | } 52 | 53 | table m_table { 54 | actions { m_action; } 55 | size : 1024; 56 | } 57 | 58 | control ingress { 59 | apply(m_table); 60 | } 61 | 62 | control egress { 63 | } 64 | -------------------------------------------------------------------------------- /tests/valgrind.test.supp: -------------------------------------------------------------------------------- 1 | { 2 | debugger1 3 | Memcheck:Leak 4 | match-leak-kinds: reachable 5 | fun:_Znwm 6 | fun:__cxx_global_var_init70 7 | fun:_GLOBAL__sub_I_debugger.cpp 8 | } 9 | { 10 | event_logger1 11 | Memcheck:Leak 12 | match-leak-kinds: reachable 13 | fun:_Znwm 14 | fun:__cxx_global_var_init47 15 | fun:_GLOBAL__sub_I_event_logger.cpp 16 | } 17 | { 18 | event_logger2 19 | Memcheck:Leak 20 | match-leak-kinds: reachable 21 | fun:_Znwm 22 | fun:_ZN2bm14TransportIface10make_dummyEv 23 | fun:__cxx_global_var_init47 24 | fun:_GLOBAL__sub_I_event_logger.cpp 25 | } 26 | { 27 | packet1 28 | Memcheck:Leak 29 | match-leak-kinds: reachable 30 | fun:_Znwm 31 | fun:__cxx_global_var_init7 32 | fun:_GLOBAL__sub_I_packet.cpp 33 | } 34 | { 35 | unknown 36 | Memcheck:Leak 37 | match-leak-kinds: reachable 38 | fun:malloc 39 | obj:/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22 40 | fun:call_init.part.0 41 | fun:call_init 42 | fun:_dl_init 43 | obj:/lib/x86_64-linux-gnu/ld-2.19.so 44 | obj:* 45 | obj:* 46 | obj:* 47 | } 48 | -------------------------------------------------------------------------------- /third_party/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = gtest jsoncpp spdlog 2 | -------------------------------------------------------------------------------- /third_party/gtest/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS = -I$(srcdir)/include 2 | AM_CXXFLAGS = 3 | 4 | check_LTLIBRARIES = libgtest.la 5 | 6 | EXTRA_DIST = \ 7 | include/gtest/gtest-printers.h \ 8 | include/gtest/gtest-test-part.h \ 9 | include/gtest/internal/custom/gtest-printers.h \ 10 | include/gtest/internal/custom/gtest.h \ 11 | include/gtest/internal/custom/gtest-port.h \ 12 | include/gtest/internal/gtest-internal.h \ 13 | include/gtest/internal/gtest-port-arch.h \ 14 | include/gtest/internal/gtest-port.h \ 15 | include/gtest/internal/gtest-type-util.h \ 16 | include/gtest/internal/gtest-string.h \ 17 | include/gtest/internal/gtest-filepath.h \ 18 | include/gtest/internal/gtest-param-util.h \ 19 | include/gtest/internal/gtest-death-test-internal.h \ 20 | include/gtest/gtest-param-test.h \ 21 | include/gtest/gtest-typed-test.h \ 22 | include/gtest/gtest-message.h \ 23 | include/gtest/gtest_prod.h \ 24 | include/gtest/gtest_pred_impl.h \ 25 | include/gtest/gtest-spi.h \ 26 | include/gtest/gtest-death-test.h \ 27 | include/gtest/gtest-assertion-result.h \ 28 | include/gtest/gtest-matchers.h 29 | 30 | EXTRA_DIST += \ 31 | src/gtest-test-part.cc \ 32 | src/gtest-filepath.cc \ 33 | src/gtest-typed-test.cc \ 34 | src/gtest-port.cc \ 35 | src/gtest_main.cc \ 36 | src/gtest-death-test.cc \ 37 | src/gtest.cc \ 38 | src/gtest-printers.cc \ 39 | src/gtest-assertion-result.cc \ 40 | src/gtest-matchers.cc \ 41 | src/gtest-internal-inl.h 42 | 43 | libgtest_la_SOURCES = \ 44 | src/gtest-all.cc \ 45 | include/gtest/gtest.h 46 | -------------------------------------------------------------------------------- /third_party/gtest/include/gtest/internal/custom/README.md: -------------------------------------------------------------------------------- 1 | # Customization Points 2 | 3 | The custom directory is an injection point for custom user configurations. 4 | 5 | ## Header `gtest.h` 6 | 7 | ### The following macros can be defined: 8 | 9 | * `GTEST_OS_STACK_TRACE_GETTER_` - The name of an implementation of 10 | `OsStackTraceGetterInterface`. 11 | * `GTEST_CUSTOM_TEMPDIR_FUNCTION_` - An override for `testing::TempDir()`. See 12 | `testing::TempDir` for semantics and signature. 13 | 14 | ## Header `gtest-port.h` 15 | 16 | The following macros can be defined: 17 | 18 | ### Logging: 19 | 20 | * `GTEST_LOG_(severity)` 21 | * `GTEST_CHECK_(condition)` 22 | * Functions `LogToStderr()` and `FlushInfoLog()` have to be provided too. 23 | 24 | ### Threading: 25 | 26 | * `GTEST_HAS_NOTIFICATION_` - Enabled if Notification is already provided. 27 | * `GTEST_HAS_MUTEX_AND_THREAD_LOCAL_` - Enabled if `Mutex` and `ThreadLocal` 28 | are already provided. Must also provide `GTEST_DECLARE_STATIC_MUTEX_(mutex)` 29 | and `GTEST_DEFINE_STATIC_MUTEX_(mutex)` 30 | * `GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)` 31 | * `GTEST_LOCK_EXCLUDED_(locks)` 32 | 33 | ### Underlying library support features 34 | 35 | * `GTEST_HAS_CXXABI_H_` 36 | 37 | ### Exporting API symbols: 38 | 39 | * `GTEST_API_` - Specifier for exported symbols. 40 | 41 | ## Header `gtest-printers.h` 42 | 43 | * See documentation at `gtest/gtest-printers.h` for details on how to define a 44 | custom printer. 45 | -------------------------------------------------------------------------------- /third_party/jsoncpp/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS = -I$(srcdir)/include 2 | AM_CXXFLAGS = 3 | 4 | noinst_LTLIBRARIES = libjson.la 5 | 6 | libjson_la_SOURCES = src/jsoncpp.cpp include/jsoncpp/json.h 7 | -------------------------------------------------------------------------------- /third_party/spdlog/Makefile.am: -------------------------------------------------------------------------------- 1 | nobase_include_HEADERS =\ 2 | bm/spdlog/async_logger.h \ 3 | bm/spdlog/common.h \ 4 | bm/spdlog/formatter.h \ 5 | bm/spdlog/logger.h \ 6 | bm/spdlog/spdlog.h \ 7 | bm/spdlog/tweakme.h \ 8 | bm/spdlog/details/async_logger_impl.h \ 9 | bm/spdlog/details/async_log_helper.h \ 10 | bm/spdlog/details/file_helper.h \ 11 | bm/spdlog/details/format.cc \ 12 | bm/spdlog/details/format.h \ 13 | bm/spdlog/details/line_logger.h \ 14 | bm/spdlog/details/logger_impl.h \ 15 | bm/spdlog/details/log_msg.h \ 16 | bm/spdlog/details/mpmc_bounded_q.h \ 17 | bm/spdlog/details/null_mutex.h \ 18 | bm/spdlog/details/os.h \ 19 | bm/spdlog/details/pattern_formatter_impl.h \ 20 | bm/spdlog/details/registry.h \ 21 | bm/spdlog/details/spdlog_impl.h \ 22 | bm/spdlog/sinks/android_sink.h \ 23 | bm/spdlog/sinks/base_sink.h \ 24 | bm/spdlog/sinks/dist_sink.h \ 25 | bm/spdlog/sinks/file_sinks.h \ 26 | bm/spdlog/sinks/null_sink.h \ 27 | bm/spdlog/sinks/ostream_sink.h \ 28 | bm/spdlog/sinks/sink.h \ 29 | bm/spdlog/sinks/stdout_sinks.h \ 30 | bm/spdlog/sinks/syslog_sink.h 31 | -------------------------------------------------------------------------------- /third_party/spdlog/bm/spdlog/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Gabi Melman. 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 | -------------------------------------------------------------------------------- /thrift_src/.gitignore: -------------------------------------------------------------------------------- 1 | gen-cpp/* 2 | gen-py/* 3 | *.ts 4 | -------------------------------------------------------------------------------- /tools/.gitignore: -------------------------------------------------------------------------------- 1 | bm_CLI 2 | bm_p4dbg 3 | bm_nanomsg_events 4 | -------------------------------------------------------------------------------- /tools/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 2 | 3 | EXTRA_DIST = \ 4 | get_version.sh 5 | 6 | EXTRA_DIST += \ 7 | run_valgrind.sh \ 8 | veth_setup.sh \ 9 | veth_teardown.sh 10 | 11 | python_PYTHON = 12 | bin_SCRIPTS = 13 | 14 | if COND_THRIFT 15 | 16 | python_PYTHON += bmpy_utils.py runtime_CLI.py 17 | 18 | bm_CLI: bm_CLI.in 19 | 20 | bin_SCRIPTS += bm_CLI 21 | 22 | EXTRA_DIST += bm_CLI.in 23 | 24 | endif # COND_THRIF 25 | 26 | if COND_NANOMSG 27 | 28 | python_PYTHON += p4dbg.py nanomsg_client.py 29 | 30 | bm_p4dbg: bm_p4dbg.in 31 | bm_nanomsg_events: bm_nanomsg_events.in 32 | 33 | bin_SCRIPTS += bm_p4dbg bm_nanomsg_events 34 | 35 | EXTRA_DIST += bm_p4dbg.in bm_nanomsg_events.in 36 | 37 | endif # COND_NANOMSG 38 | 39 | # See 40 | # http://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Installation-Directory-Variables.html 41 | edit = sed \ 42 | -e 's|@pythondir[@]|$(pythondir)|g' 43 | 44 | $(bin_SCRIPTS): Makefile 45 | rm -f $@ $@.tmp 46 | $(edit) $(srcdir)/$@.in >$@.tmp 47 | chmod +x $@.tmp 48 | chmod a-w $@.tmp 49 | mv $@.tmp $@ 50 | 51 | CLEANFILES = $(bin_SCRIPTS) 52 | -------------------------------------------------------------------------------- /tools/bm_CLI.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2013-present Barefoot Networks, Inc. 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 | # This is just a wrapper script around runtime_CLI.py 23 | # It makes sure that the script works correctly no matter where Python 24 | # dependencies are installed 25 | 26 | import sys 27 | sys.path.append("@pythondir@") 28 | 29 | import runtime_CLI 30 | runtime_CLI.main() 31 | -------------------------------------------------------------------------------- /tools/bm_nanomsg_events.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2013-present Barefoot Networks, Inc. 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 | # This is just a wrapper script around nanomsg_client.py 23 | # It makes sure that the script works correctly no matter where Python 24 | # dependencies are installed 25 | 26 | import sys 27 | sys.path.append("@pythondir@") 28 | 29 | import nanomsg_client 30 | nanomsg_client.main() 31 | -------------------------------------------------------------------------------- /tools/bm_p4dbg.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2013-present Barefoot Networks, Inc. 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 | # This is just a wrapper script around p4dbg.py 23 | # It makes sure that the script works correctly no matter where Python 24 | # dependencies are installed 25 | 26 | import sys 27 | sys.path.append("@pythondir@") 28 | 29 | import p4dbg 30 | p4dbg.main() 31 | -------------------------------------------------------------------------------- /tools/check_style.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 4 | 5 | ROOT_DIR=$THIS_DIR/.. 6 | 7 | return_status=0 8 | 9 | function run_cpplint() { 10 | # $1 is directory 11 | # $2 is root 12 | python3 $THIS_DIR/cpplint.py --root=$2 $( find $1 -name \*.h -or -name \*.cpp ) 13 | return_status=$(($return_status || $?)) 14 | } 15 | 16 | run_cpplint $ROOT_DIR/src/bm_sim src 17 | run_cpplint $ROOT_DIR/src/bm_apps src 18 | run_cpplint $ROOT_DIR/include/bm/bm_sim include 19 | run_cpplint $ROOT_DIR/include/bm/bm_apps include 20 | run_cpplint $ROOT_DIR/include/bm/bm_grpc include 21 | run_cpplint $ROOT_DIR/targets/simple_switch targets 22 | run_cpplint $ROOT_DIR/targets/l2_switch targets 23 | run_cpplint $ROOT_DIR/targets/simple_router targets 24 | run_cpplint $ROOT_DIR/targets/simple_switch_grpc targets 25 | 26 | run_cpplint $ROOT_DIR/tests . 27 | 28 | run_cpplint $ROOT_DIR/pdfixed/src src 29 | run_cpplint $ROOT_DIR/pdfixed/include pdfixed/include 30 | 31 | run_cpplint $ROOT_DIR/PI PI 32 | 33 | echo "********************************" 34 | if [ $return_status -eq 0 ]; then 35 | echo "STYLE CHECK SUCCESS" 36 | else 37 | echo "STYLE CHECK FAILURE" 38 | fi 39 | 40 | exit $return_status 41 | -------------------------------------------------------------------------------- /tools/get_version.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2013-present Barefoot Networks, Inc. 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 (antonin@barefootnetworks.com) 20 | # 21 | # 22 | 23 | THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 24 | 25 | version_str="$(cat $THIS_DIR/../VERSION | tr -d '\n')" 26 | 27 | bf_path="$THIS_DIR/../VERSION-build" 28 | bversion_str="" 29 | if [ -f $bf_path ]; then 30 | bversion_str="$(cat $bf_path | tr -d '\n')" 31 | else 32 | if [ ! -d "$THIS_DIR/../.git" ]; then 33 | bversion_str="unknown" 34 | else 35 | git_sha_str="$(git rev-parse @)" 36 | if [ $? -ne 0 ]; then 37 | bversion_str="unknown" 38 | else 39 | bversion_str=${git_sha_str:0:8} 40 | fi 41 | fi 42 | fi 43 | 44 | version_str="$version_str-$bversion_str" 45 | 46 | # Omit the trailing newline, so that m4_esyscmd can use the result directly. 47 | printf %s "$version_str" 48 | -------------------------------------------------------------------------------- /tools/macos/bootstrap_mac.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | set -e 4 | set -x 5 | 6 | tools_dir=`dirname $0` 7 | 8 | # check for brew, 9 | if [[ -z `which brew` ]]; then 10 | # install homebrew 11 | /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 12 | # python pip installs in /usr/local/tests 13 | sudo mkdir -p /usr/local/tests 14 | sudo chown ${USER}:admin /usr/local/tests 15 | fi 16 | 17 | # check for pip 18 | if [[ -z `which pip` ]]; then 19 | (>&2 echo 'Please install pip by "curl -O https://bootstrap.pypa.io/get-pip.py | sudo python3 get-pip.py"') 20 | exit 1 21 | fi 22 | 23 | # check for clang version 24 | cxx_full_version=`clang++ --version | grep version` 25 | cxx_version=`echo $cxx_full_version | cut -f 4 -d " "` 26 | if [[ $cxx_version < "8.0.0" ]]; then 27 | echo "Please install Xcode 8 or command line tools v8 or higher." 28 | echo "It is a requirement for thread_local support" 29 | echo "" 30 | echo "You have: $cxx_full_version" 31 | exit 1 32 | fi 33 | 34 | brew update 35 | 36 | # install basic tools 37 | brew install automake autoconf bison boost clang-format cmake coreutils \ 38 | doxygen gcc@5 gmp libevent openssl pkg-config wget 39 | # bison needs a bit more nudging 40 | brew link --overwrite --force bison 41 | 42 | # we need realpath from coreutils 43 | brews_dir=`realpath $tools_dir`/brews 44 | 45 | # thrift 46 | brew install --HEAD ${brews_dir}/thrift.rb --with-python --with-libevent 47 | 48 | brew install python 49 | /usr/local/bin/pip install pcapy 50 | 51 | # nanomsg 52 | brew install nanomsg 53 | 54 | # # scapy 55 | pip install scapy 56 | 57 | # nnpy 58 | /usr/local/bin/pip install pycparser 59 | brew install ${brews_dir}/nnpy.rb 60 | 61 | /usr/local/bin/pip install PyYAML 62 | -------------------------------------------------------------------------------- /tools/macos/brews/nnpy.rb: -------------------------------------------------------------------------------- 1 | class Nnpy < Formula 2 | desc "cffi-based Python bindings for nanomsg" 3 | homepage "https://github.com/nanomsg/nnpy" 4 | url "https://github.com/nanomsg/nnpy.git", :revision => "c7e718a5173447c85182dc45f99e2abcf9cd4065" 5 | version "1.2" 6 | 7 | depends_on :python 8 | depends_on "nanomsg" 9 | depends_on "pkg-config" 10 | depends_on "libffi" 11 | 12 | resource "cffi" do 13 | url "https://pypi.python.org/packages/0a/f3/686af8873b70028fccf67b15c78fd4e4667a3da995007afc71e786d61b0a/cffi-1.8.3.tar.gz#md5=c8e877fe0426a99d0cf5872cf2f95b27" 14 | sha256 "c321bd46faa7847261b89c0469569530cad5a41976bb6dba8202c0159f476568" 15 | end 16 | 17 | def install 18 | version = Language::Python.major_minor_version "python" 19 | dest_path = lib/"python#{version}/site-packages" 20 | dest_path.mkpath 21 | 22 | bundle_path = libexec/"lib/python#{version}/site-packages" 23 | ENV.prepend_create_path "PYTHONPATH", bundle_path 24 | resource("cffi").stage do 25 | system "python", *Language::Python.setup_install_args(libexec) 26 | end 27 | (dest_path/"homebrew-nnpy-bundle.pth").write "#{bundle_path}\n" 28 | 29 | ENV.prepend_create_path "PYTHONPATH", prefix/"lib/python#{version}/site-packages" 30 | system "python", *Language::Python.setup_install_args(prefix) 31 | end 32 | 33 | test do 34 | system "true" 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /tools/run_valgrind.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 4 | 5 | TEST_DIR=$THIS_DIR/../tests 6 | 7 | cd $TEST_DIR 8 | 9 | libtool --mode=execute valgrind \ 10 | --suppressions=valgrind.test.supp --leak-check=full --show-reachable=yes \ 11 | ./test_all --valgrind 12 | return_status=$? 13 | 14 | cd - 15 | 16 | exit $return_status 17 | -------------------------------------------------------------------------------- /tools/veth_setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for idx in 0 1 2 3 4 5 6 7 8; do 3 | intf0="veth$(($idx*2))" 4 | intf1="veth$(($idx*2+1))" 5 | if ! ip link show $intf0 &> /dev/null; then 6 | ip link add name $intf0 type veth peer name $intf1 7 | ip link set dev $intf0 up 8 | ip link set dev $intf1 up 9 | 10 | # Set the MTU of these interfaces to be larger than default of 11 | # 1500 bytes, so that P4 behavioral-model testing can be done 12 | # on jumbo frames. 13 | # Note: ifconfig is deprecated, and no longer installed by 14 | # default in Ubuntu Linux minimal installs starting with 15 | # Ubuntu 18.04. The ip command is installed in Ubuntu 16 | # versions since at least 16.04, and probably older versions, 17 | # too. 18 | ip link set $intf0 mtu 9500 19 | ip link set $intf1 mtu 9500 20 | 21 | # Disable IPv6 on the interfaces, so that the Linux kernel 22 | # will not automatically send IPv6 MDNS, Router Solicitation, 23 | # and Multicast Listener Report packets on the interface, 24 | # which can make P4 program debugging more confusing. 25 | # 26 | # Testing indicates that we can still send IPv6 packets across 27 | # such interfaces, both from scapy to simple_switch, and from 28 | # simple_switch out to scapy sniffing. 29 | # 30 | # https://superuser.com/questions/356286/how-can-i-switch-off-ipv6-nd-ra-transmissions-in-linux 31 | sysctl net.ipv6.conf.${intf0}.disable_ipv6=1 32 | sysctl net.ipv6.conf.${intf1}.disable_ipv6=1 33 | fi 34 | done 35 | -------------------------------------------------------------------------------- /tools/veth_teardown.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for idx in 0 1 2 3 4 5 6 7 8; do 3 | intf="veth$(($idx*2))" 4 | if ip link show $intf &> /dev/null; then 5 | ip link delete $intf type veth 6 | fi 7 | done 8 | --------------------------------------------------------------------------------