forked from p4lang/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82bde33
commit a67330c
Showing
10 changed files
with
505 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# tutorials | ||
Holds different P4 language tutorials | ||
P4 language tutorials |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
table_set_default ecmp_group _drop | ||
table_set_default ecmp_nhop _drop | ||
table_set_default forward _drop | ||
table_set_default send_frame _drop | ||
table_add ecmp_group set_ecmp_select 10.0.0.1/32 => 0 2 | ||
table_add ecmp_nhop set_nhop 0 => 10.0.1.1 1 | ||
table_add ecmp_nhop set_nhop 1 => 10.0.2.1 2 | ||
table_add forward set_dmac 10.0.1.1 => 00:04:00:00:00:00 | ||
table_add forward set_dmac 10.0.2.1 => 00:04:00:00:00:01 | ||
table_add send_frame rewrite_mac 1 => 00:aa:bb:00:00:00 | ||
table_add send_frame rewrite_mac 2 => 00:aa:bb:00:00:01 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
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 ipv4_t { | ||
fields { | ||
version : 4; | ||
ihl : 4; | ||
diffserv : 8; | ||
totalLen : 16; | ||
identification : 16; | ||
flags : 3; | ||
fragOffset : 13; | ||
ttl : 8; | ||
protocol : 8; | ||
hdrChecksum : 16; | ||
srcAddr : 32; | ||
dstAddr: 32; | ||
} | ||
} | ||
|
||
header_type tcp_t { | ||
fields { | ||
srcPort : 16; | ||
dstPort : 16; | ||
seqNo : 32; | ||
ackNo : 32; | ||
dataOffset : 4; | ||
res : 3; | ||
ecn : 3; | ||
ctrl : 6; | ||
window : 16; | ||
checksum : 16; | ||
urgentPtr : 16; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
SIGCOMM_2015/flowlet_switching/p4src/includes/intrinsic.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
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 intrinsic_metadata_t { | ||
fields { | ||
ingress_global_timestamp : 48; | ||
lf_field_list : 32; | ||
mcast_grp : 16; | ||
egress_rid : 16; | ||
} | ||
} | ||
|
||
metadata intrinsic_metadata_t intrinsic_metadata; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
parser start { | ||
return parse_ethernet; | ||
} | ||
|
||
#define ETHERTYPE_IPV4 0x0800 | ||
|
||
header ethernet_t ethernet; | ||
|
||
parser parse_ethernet { | ||
extract(ethernet); | ||
return select(latest.etherType) { | ||
ETHERTYPE_IPV4 : parse_ipv4; | ||
default: ingress; | ||
} | ||
} | ||
|
||
header ipv4_t ipv4; | ||
|
||
field_list ipv4_checksum_list { | ||
ipv4.version; | ||
ipv4.ihl; | ||
ipv4.diffserv; | ||
ipv4.totalLen; | ||
ipv4.identification; | ||
ipv4.flags; | ||
ipv4.fragOffset; | ||
ipv4.ttl; | ||
ipv4.protocol; | ||
ipv4.srcAddr; | ||
ipv4.dstAddr; | ||
} | ||
|
||
field_list_calculation ipv4_checksum { | ||
input { | ||
ipv4_checksum_list; | ||
} | ||
algorithm : csum16; | ||
output_width : 16; | ||
} | ||
|
||
calculated_field ipv4.hdrChecksum { | ||
verify ipv4_checksum; | ||
update ipv4_checksum; | ||
} | ||
|
||
#define IP_PROTOCOLS_TCP 6 | ||
|
||
parser parse_ipv4 { | ||
extract(ipv4); | ||
return select(latest.protocol) { | ||
IP_PROTOCOLS_TCP : parse_tcp; | ||
default: ingress; | ||
} | ||
} | ||
|
||
header tcp_t tcp; | ||
|
||
parser parse_tcp { | ||
extract(tcp); | ||
return ingress; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
#include "includes/headers.p4" | ||
#include "includes/parser.p4" | ||
#include "includes/intrinsic.p4" | ||
|
||
header_type ingress_metadata_t { | ||
fields { | ||
ecmp_offset : 14; // offset into the ecmp table | ||
|
||
nhop_ipv4 : 32; | ||
|
||
// TODO: add your flowlet metadata here | ||
} | ||
} | ||
|
||
metadata ingress_metadata_t ingress_metadata; | ||
|
||
action _drop() { | ||
drop(); | ||
} | ||
|
||
action set_nhop(nhop_ipv4, port) { | ||
modify_field(ingress_metadata.nhop_ipv4, nhop_ipv4); | ||
modify_field(standard_metadata.egress_spec, port); | ||
add_to_field(ipv4.ttl, -1); | ||
} | ||
|
||
#define ECMP_BIT_WIDTH 10 | ||
#define ECMP_GROUP_TABLE_SIZE 1024 | ||
#define ECMP_NHOP_TABLE_SIZE 16384 | ||
|
||
// TODO: add flowlet id to hash fields | ||
|
||
field_list l3_hash_fields { | ||
ipv4.srcAddr; | ||
ipv4.dstAddr; | ||
ipv4.protocol; | ||
tcp.srcPort; | ||
tcp.dstPort; | ||
} | ||
|
||
field_list_calculation ecmp_hash { | ||
input { | ||
l3_hash_fields; | ||
} | ||
algorithm : crc16; | ||
output_width : ECMP_BIT_WIDTH; | ||
} | ||
|
||
|
||
action set_ecmp_select(ecmp_base, ecmp_count) { | ||
modify_field_with_hash_based_offset(ingress_metadata.ecmp_offset, ecmp_base, | ||
ecmp_hash, ecmp_count); | ||
} | ||
|
||
table ecmp_group { | ||
reads { | ||
ipv4.dstAddr : lpm; | ||
} | ||
actions { | ||
_drop; | ||
set_ecmp_select; | ||
} | ||
size : ECMP_GROUP_TABLE_SIZE; | ||
} | ||
|
||
table ecmp_nhop { | ||
reads { | ||
ingress_metadata.ecmp_offset : exact; | ||
} | ||
actions { | ||
_drop; | ||
set_nhop; | ||
} | ||
size : ECMP_NHOP_TABLE_SIZE; | ||
} | ||
|
||
action set_dmac(dmac) { | ||
modify_field(ethernet.dstAddr, dmac); | ||
} | ||
|
||
table forward { | ||
reads { | ||
ingress_metadata.nhop_ipv4 : exact; | ||
} | ||
actions { | ||
set_dmac; | ||
_drop; | ||
} | ||
size: 512; | ||
} | ||
|
||
action rewrite_mac(smac) { | ||
modify_field(ethernet.srcAddr, smac); | ||
} | ||
|
||
table send_frame { | ||
reads { | ||
standard_metadata.egress_port: exact; | ||
} | ||
actions { | ||
rewrite_mac; | ||
_drop; | ||
} | ||
size: 256; | ||
} | ||
|
||
control ingress { | ||
// TODO: flowlet switching | ||
apply(ecmp_group); | ||
apply(ecmp_nhop); | ||
apply(forward); | ||
} | ||
|
||
control egress { | ||
apply(send_frame); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/tools/runtime_CLI.py | ||
|
||
# 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/simple_router.p4 --json simple_router.json | ||
sudo echo "sudo" > /dev/null | ||
sudo $BMV2_PATH/targets/simple_switch/simple_switch simple_router.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 --json simple_router.json < commands.txt | ||
echo "READY!!!" | ||
fg |
Oops, something went wrong.