diff --git a/examples/README.md b/examples/README.md index 2a50344..1fa1d67 100644 --- a/examples/README.md +++ b/examples/README.md @@ -10,6 +10,8 @@ included: packet, encapsulate it and send it to a special port. - `meter`: how to use indirect meters in P4. - `TLV_parsing`: how to parse IPv4 options +- `register`: how to use registers in P4 and read / write the state from the + control plane All examples are orgranized the same way, with a `p4src` directory containing the P4 source code, and a `README` file describing the P4 program and explaining diff --git a/examples/register/README.md b/examples/register/README.md new file mode 100644 index 0000000..75e15e8 --- /dev/null +++ b/examples/register/README.md @@ -0,0 +1,20 @@ +# Register + +## Description + +This program illustrates as simply as possible how to use registers in P4 and +read / write the register state using the bmv2 runtime CLI. + +This will probably change in the future but as of now the data plane is not +doing anything (so don't try to send packets). However you can take a look at +the P4 source code and at the read_register and write_register scripts. You can +also run the following demo: + +### Running the demo + +To run the demo: +- start the switch with `./run_switch.sh`. +- write a register cell with `write_register.sh`. For example: + `./write_register.sh 123 88` to set `my_register[123]` to 88. +- read a register cell with `read_register.sh`. For example: + `./read_register.sh 123` to read `my_register[123]`. diff --git a/examples/register/p4src/register.p4 b/examples/register/p4src/register.p4 new file mode 100644 index 0000000..a61e5ca --- /dev/null +++ b/examples/register/p4src/register.p4 @@ -0,0 +1,87 @@ +/* +Copyright 2013-present Barefoot Networks, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +header_type ethernet_t { + fields { + dstAddr : 48; + srcAddr : 48; + etherType : 16; + } +} + +header_type intrinsic_metadata_t { + fields { + mcast_grp : 4; + egress_rid : 4; + mcast_hash : 16; + lf_field_list: 32; + } +} + +header_type meta_t { + fields { + register_tmp : 32; + } +} + +metadata meta_t meta; + +parser start { + return parse_ethernet; +} + +header ethernet_t ethernet; +metadata intrinsic_metadata_t intrinsic_metadata; + +parser parse_ethernet { + extract(ethernet); + return ingress; +} + +action _drop() { + drop(); +} + +action _nop() { +} + +register my_register { + width: 32; + static: m_table; + instance_count: 16384; +} + +action m_action(register_idx) { + register_read(meta.register_tmp, my_register, register_idx); + // TODO +} + +table m_table { + reads { + ethernet.srcAddr : exact; + } + actions { + m_action; _nop; + } + size : 16384; +} + +control ingress { + apply(m_table); +} + +control egress { +} diff --git a/examples/register/read_register.sh b/examples/register/read_register.sh new file mode 100755 index 0000000..41fae2b --- /dev/null +++ b/examples/register/read_register.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Copyright 2013-present Barefoot Networks, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +source $THIS_DIR/../env.sh + +CLI_PATH=$BMV2_PATH/targets/simple_switch/sswitch_CLI + +if [ $# -lt 1 ]; then + echo "Please specify register index" + exit 1 +fi +index=$1 + +echo "register_read my_register $index" | $CLI_PATH register.json diff --git a/examples/register/run_switch.sh b/examples/register/run_switch.sh new file mode 100755 index 0000000..f5f9b22 --- /dev/null +++ b/examples/register/run_switch.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# Copyright 2013-present Barefoot Networks, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +source $THIS_DIR/../env.sh + +P4C_BM_SCRIPT=$P4C_BM_PATH/p4c_bm/__main__.py + +SWITCH_PATH=$BMV2_PATH/targets/simple_switch/simple_switch + +CLI_PATH=$BMV2_PATH/targets/simple_switch/sswitch_CLI + +# Probably not very elegant but it works nice here: we enable interactive mode +# to be able to use fg. We start the switch in the background, sleep for 2 +# minutes to give it time to start, then add the entries and put the switch +# process back in the foreground +set -m +$P4C_BM_SCRIPT p4src/register.p4 --json register.json +sudo echo "sudo" > /dev/null +sudo $BMV2_PATH/targets/simple_switch/simple_switch register.json \ + -i 0@veth0 -i 1@veth2 -i 2@veth4 -i 3@veth6 -i 4@veth8 \ + --nanolog ipc:///tmp/bm-0-log.ipc \ + --pcap diff --git a/examples/register/write_register.sh b/examples/register/write_register.sh new file mode 100755 index 0000000..c22a25d --- /dev/null +++ b/examples/register/write_register.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Copyright 2013-present Barefoot Networks, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +source $THIS_DIR/../env.sh + +CLI_PATH=$BMV2_PATH/targets/simple_switch/sswitch_CLI + +if [ $# -lt 2 ]; then + echo "Please specify register index and value, in this order" + exit 1 +fi +index=$1 +value=$2 + +echo "register_write my_register $index $value" | $CLI_PATH register.json