added a counter example
This commit is contained in:
parent
e81db93021
commit
1809653107
@ -12,6 +12,7 @@ included:
|
|||||||
- `TLV_parsing`: how to parse IPv4 options
|
- `TLV_parsing`: how to parse IPv4 options
|
||||||
- `register`: how to use registers in P4 and read / write the state from the
|
- `register`: how to use registers in P4 and read / write the state from the
|
||||||
control plane
|
control plane
|
||||||
|
- `counter`: how to use counters in P4
|
||||||
|
|
||||||
All examples are orgranized the same way, with a `p4src` directory containing
|
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
|
the P4 source code, and a `README` file describing the P4 program and explaining
|
||||||
|
14
examples/counter/README.md
Normal file
14
examples/counter/README.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# Counter
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
This directory is a playground for counters in P4. The P4 program includes a
|
||||||
|
direct counter and an indirect counter.
|
||||||
|
|
||||||
|
### Running the demo
|
||||||
|
|
||||||
|
To run the demo:
|
||||||
|
- start the switch with `./run_switch.sh`, wait until `READY` is displayed.
|
||||||
|
- run the demo script with `./run_demo.sh`. The script will send packets /
|
||||||
|
display the counter values / reset the counters. Look at the script for more
|
||||||
|
details.
|
2
examples/counter/commands.txt
Normal file
2
examples/counter/commands.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
table_set_default m_table m_action
|
||||||
|
table_add m_table m_action aa:bb:cc:dd:ee:ff => 0
|
92
examples/counter/p4src/counter.p4
Normal file
92
examples/counter/p4src/counter.p4
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
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() {
|
||||||
|
}
|
||||||
|
|
||||||
|
counter my_indirect_counter {
|
||||||
|
type: packets;
|
||||||
|
static: m_table;
|
||||||
|
instance_count: 16384;
|
||||||
|
}
|
||||||
|
|
||||||
|
counter my_direct_counter {
|
||||||
|
type: bytes;
|
||||||
|
direct: m_table;
|
||||||
|
}
|
||||||
|
|
||||||
|
action m_action(idx) {
|
||||||
|
count(my_indirect_counter, idx);
|
||||||
|
drop();
|
||||||
|
}
|
||||||
|
|
||||||
|
table m_table {
|
||||||
|
reads {
|
||||||
|
ethernet.srcAddr : exact;
|
||||||
|
}
|
||||||
|
actions {
|
||||||
|
m_action; _nop;
|
||||||
|
}
|
||||||
|
size : 16384;
|
||||||
|
}
|
||||||
|
|
||||||
|
control ingress {
|
||||||
|
apply(m_table);
|
||||||
|
}
|
||||||
|
|
||||||
|
control egress {
|
||||||
|
}
|
63
examples/counter/run_demo.sh
Executable file
63
examples/counter/run_demo.sh
Executable file
@ -0,0 +1,63 @@
|
|||||||
|
#!/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
|
||||||
|
|
||||||
|
echo "resetting counters"
|
||||||
|
echo "counter_reset my_indirect_counter" | $CLI_PATH counter.json
|
||||||
|
echo "counter_reset my_direct_counter" | $CLI_PATH counter.json
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo "displaying counters [0]"
|
||||||
|
echo "counter_read my_indirect_counter 0" | $CLI_PATH counter.json
|
||||||
|
echo "counter_read my_direct_counter 0" | $CLI_PATH counter.json
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo "sending 1 packet of size 70"
|
||||||
|
sudo python send_one.py
|
||||||
|
sleep 1
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo "displaying counters [0]"
|
||||||
|
echo "counter_read my_indirect_counter 0" | $CLI_PATH counter.json
|
||||||
|
echo "counter_read my_direct_counter 0" | $CLI_PATH counter.json
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo "sending 3 packets of size 70"
|
||||||
|
sudo python send_one.py
|
||||||
|
sudo python send_one.py
|
||||||
|
sudo python send_one.py
|
||||||
|
sleep 1
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo "displaying counters [0]"
|
||||||
|
echo "counter_read my_indirect_counter 0" | $CLI_PATH counter.json
|
||||||
|
echo "counter_read my_direct_counter 0" | $CLI_PATH counter.json
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo
|
||||||
|
echo "resetting counters"
|
||||||
|
echo "counter_reset my_indirect_counter" | $CLI_PATH counter.json
|
||||||
|
echo "counter_reset my_direct_counter" | $CLI_PATH counter.json
|
41
examples/counter/run_switch.sh
Executable file
41
examples/counter/run_switch.sh
Executable file
@ -0,0 +1,41 @@
|
|||||||
|
#!/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/counter.p4 --json counter.json
|
||||||
|
sudo echo "sudo" > /dev/null
|
||||||
|
sudo $BMV2_PATH/targets/simple_switch/simple_switch counter.json \
|
||||||
|
-i 0@veth0 -i 1@veth2 -i 2@veth4 -i 3@veth6 -i 4@veth8 \
|
||||||
|
--nanolog ipc:///tmp/bm-0-log.ipc \
|
||||||
|
--pcap &
|
||||||
|
sleep 2
|
||||||
|
$CLI_PATH counter.json < commands.txt
|
||||||
|
echo "READY!!!"
|
||||||
|
fg
|
4
examples/counter/send_one.py
Normal file
4
examples/counter/send_one.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from scapy.all import *
|
||||||
|
|
||||||
|
p = Ether(src="aa:bb:cc:dd:ee:ff") / IP(dst="10.0.1.10") / TCP() / "aaaaaaaaaaaaaaaa"
|
||||||
|
sendp(p, iface = "veth1", verbose = 0)
|
Loading…
x
Reference in New Issue
Block a user