added a simple register example
This commit is contained in:
parent
a1c21604d4
commit
ca39826949
@ -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
|
||||
|
20
examples/register/README.md
Normal file
20
examples/register/README.md
Normal file
@ -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]`.
|
87
examples/register/p4src/register.p4
Normal file
87
examples/register/p4src/register.p4
Normal file
@ -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 {
|
||||
}
|
29
examples/register/read_register.sh
Executable file
29
examples/register/read_register.sh
Executable file
@ -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
|
37
examples/register/run_switch.sh
Executable file
37
examples/register/run_switch.sh
Executable file
@ -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
|
30
examples/register/write_register.sh
Executable file
30
examples/register/write_register.sh
Executable file
@ -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
|
Loading…
x
Reference in New Issue
Block a user