Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
592be8e
example
slankdev Jun 5, 2023
a12b7d7
dockerfile
slankdev Jun 5, 2023
0f3c6e7
ex
slankdev Jun 5, 2023
cb773de
img
slankdev Jun 5, 2023
8001054
img
slankdev Jun 5, 2023
f186ca0
ex
slankdev Jun 5, 2023
27cf9ec
ex
slankdev Jun 5, 2023
3180ef2
ex
slankdev Jun 5, 2023
90359da
ex
slankdev Jun 5, 2023
b23cd5e
2023.06.05-01:16
slankdev Jun 5, 2023
17103bf
2023.06.06-01:35
slankdev Jun 6, 2023
f3e667e
2023.06.06-03:28
slankdev Jun 6, 2023
b84a363
ex
slankdev Jun 6, 2023
7503c8e
2023.06.06-04:29
slankdev Jun 6, 2023
d3f52cd
Update README.md
slankdev Jun 6, 2023
58e5d01
Update README.md
slankdev Jun 6, 2023
d802bbf
Update README.md
slankdev Jun 6, 2023
f50434e
Update README.md
slankdev Jun 6, 2023
dca1cb2
2023.06.06-05:09
slankdev Jun 6, 2023
99e178e
img
slankdev Jun 6, 2023
7858afa
2023.06.06-11:13
slankdev Jun 6, 2023
6cbef86
2023.06.06-11:13
slankdev Jun 6, 2023
f0b4524
2023.06.06-11:21
slankdev Jun 6, 2023
5c25476
2023.06.06-11:21
slankdev Jun 6, 2023
3fe59cc
2023.06.06-11:27
slankdev Jun 6, 2023
2be642c
2023.06.06-11:40
slankdev Jun 6, 2023
4776415
2023.06.06-11:44
slankdev Jun 6, 2023
1116170
img
slankdev Jun 6, 2023
6302a86
img
slankdev Jun 6, 2023
ed3b6be
2023.06.06-12:25
slankdev Jun 6, 2023
93d1e6b
2023.06.06-12:25
slankdev Jun 6, 2023
c49f8cd
2023.06.06-12:26
slankdev Jun 6, 2023
f6ff683
2023.06.06-12:27
slankdev Jun 6, 2023
d90dc1a
2023.06.06-12:34
slankdev Jun 6, 2023
01b303f
2023.06.06-12:34
slankdev Jun 6, 2023
9ab7bd5
2023.06.06-12:39
slankdev Jun 6, 2023
a8a4737
2023.06.06-12:59
slankdev Jun 6, 2023
fdea37b
2023.06.06-13:14
slankdev Jun 6, 2023
e85f97c
2023.06.06-13:15
slankdev Jun 6, 2023
5c59570
2023.06.06-13:15
slankdev Jun 6, 2023
93b1f23
2023.06.06-13:24
slankdev Jun 6, 2023
5539b2d
2023.06.06-13:24
slankdev Jun 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Dockerfiles/p4bmv2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM p4lang/p4runtime-sh:latest as deps

# p4lang/behavioral-model is included by p4lang/p4c
FROM p4lang/p4c
RUN apt-get update -y && apt-get install -y iproute2 vim
COPY ./wait.sh /usr/bin/wait.sh
RUN chmod +x /usr/bin/wait.sh

ENV VENV /p4runtime-sh/venv
RUN apt-get update && apt-get install -y --no-install-recommends \
curl git python3 python3-venv && rm -rf /var/cache/apt/* /var/lib/apt/lists/*
COPY --from=deps /p4runtime-sh /p4runtime-sh
COPY ./p4runtime-sh /p4runtime-sh/p4runtime-sh
RUN pip3 install p4runtime-shell && chmod +x /p4runtime-sh/p4runtime-sh

RUN apt-get update -y && apt-get install -y tcpdump psmisc iputils-ping net-tools
8 changes: 8 additions & 0 deletions Dockerfiles/p4bmv2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
IMG=tinynetwork/p4bmv2:develop
build:
docker build -t $(IMG) .
push:
docker push $(IMG)
all: build push
run:
docker run --rm -it --privileged $(IMG) bash
19 changes: 19 additions & 0 deletions Dockerfiles/p4bmv2/p4runtime-sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3

# Copyright 2019 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.
#

import p4runtime_sh.shell as sh
sh.main()
11 changes: 11 additions & 0 deletions Dockerfiles/p4bmv2/wait.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
for i in `seq 10`; do
echo EOF | bm_CLI
if [ "$?" == "0" ]; then
echo "bm_CLI is ready!"
exit 0
fi
sleep 1
done
echo "bm_CLI is not ready"
exit 1
15 changes: 15 additions & 0 deletions examples/basic_p4/l3switch/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
c:
docker cp main.py P4:/
docker exec P4 killall -9 python3 || true
docker exec -it P4 python3 /main.py

d:
docker cp main.p4 P4:/
docker exec P4 killall -9 simple_switch_grpc || true
docker exec P4 p4c --target bmv2 --arch v1model /main.p4 --p4runtime-files p4info.txt -o /
docker exec -it P4 simple_switch_grpc /main.json \
-i 1@vm1 -i 2@vm2 -i 3@vm3 \
--nanolog ipc:///tmp/bm-0-log.ipc \
--log-console -L debug \
--notifications-addr ipc:///tmp/bmv2-0-notifications.ipc \
-- --cpu-port 255
78 changes: 78 additions & 0 deletions examples/basic_p4/l3switch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# P4Runtime and BM-CLI mapping

## Running p4runtime-shell

```
docker exec P4 simple_switch_grpc /main.json -i 1@vm1 -i 2@vm2 -i 3@vm3 --nanolog ipc:///tmp/bm-0-log.ipc --log-console -L debug --notifications-addr ipc:///tmp/bmv2-0-notifications.ipc -- --cpu-port 255
// copy p4info.txt and main.json
docker run -it --net container:P4 --name p4r -v /tmp/P4runtime-nanoswitch/:/tmp/ myproj/p4rt-sh-dev /bin/bash
source $VENV/bin/activate
/p4runtime-sh/p4runtime-sh --grpc-addr 0.0.0.0:9559 --device-id 0 --election-id 0,1 --config /p4info.txt,/main.json
```

for all in one image
```
/p4runtime-sh/p4runtime-sh --grpc-addr 0.0.0.0:9559 --device-id 0 --election-id 0,1 --config /p4c/p4info.txt,/main.json
```

## Misc

- https://github.com/p4lang/p4runtime-shell#available-commands
- https://github.com/p4lang/p4runtime-shell/blob/main/usage/packet_io.md

```
tables
actions
packet_in.sniff(lambda m: print(m), timeout=1)
```

## Modifying Default Table Entry

bm cli of `simple_switch`
```
table_set_default MyIngress.ipv4_lpm MyIngress.drop
```

p4runtime cli of `simple_switch_grpc`
```
te = table_entry["MyIngress.ipv4_lpm"](action="MyIngress.drop", is_default=True)
te.modify
```

## Inserting Table Entry

bm cli of `simple_switch`
```
table_add MyIngress.ipv4_lpm MyIngress.ipv4_forward 192.168.10.10/32 => 10:10:10:10:10:10 1
table_add MyIngress.ipv4_lpm MyIngress.ipv4_forward 192.168.20.20/32 => 20:20:20:20:20:20 2
table_add MyIngress.ipv4_lpm MyIngress.ipv4_forward 192.168.30.30/32 => 30:30:30:30:30:30 3
```

p4runtime cli of `simple_switch_grpc`
```
te1 = table_entry["MyIngress.ipv4_lpm"](action="MyIngress.ipv4_forward")
te1.match["hdr.ipv4.dstAddr"] = "192.168.20.20/32"
te1.action["dstAddr"] = '20:20:20:20:20:20'
te1.action["port"] = "2"
te1.insert()

te2 = table_entry["MyIngress.ipv4_lpm"](action="MyIngress.ipv4_forward")
te2.match["hdr.ipv4.dstAddr"] = "192.168.10.10/32"
te2.action["dstAddr"] = '10:10:10:10:10:10'
te2.action["port"] = "1"
te2.insert()

te3 = table_entry["MyIngress.ipv4_lpm"](action="MyIngress.ipv4_forward")
te3.match["hdr.ipv4.dstAddr"] = "192.168.30.30/32"
te3.action["dstAddr"] = '30:30:30:30:30:30'
te3.action["port"] = "3"
te3.insert()

te4 = table_entry["MyIngress.ipv4_lpm"](action="MyIngress.ipv4_forward")
te4.match["hdr.ipv4.dstAddr"] = "192.168.40.40/32"
te4.action["dstAddr"] = 'aa:aa:aa:aa:aa:aa'
te4.action["port"] = "255"
te4.insert()

for te in table_entry["MyIngress.ipv4_lpm"].read(): print(te)
```
193 changes: 193 additions & 0 deletions examples/basic_p4/l3switch/main.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>
#define CPU_PORT 255
const bit<16> TYPE_IPV4 = 0x800;

/*************************************************************************
*********************** H E A D E R S ***********************************
*************************************************************************/

typedef bit<9> egressSpec_t;
typedef bit<48> macAddr_t;
typedef bit<32> ip4Addr_t;

@controller_header("packet_in")
header packet_in_header_t {
bit<9> ingress_port;
bit<7> _pad;
}

header ethernet_t {
macAddr_t dstAddr;
macAddr_t srcAddr;
bit<16> etherType;
}

header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
ip4Addr_t srcAddr;
ip4Addr_t dstAddr;
}

struct metadata {
bit<9> ingress_port;
bit<7> _pad;
}

struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
packet_in_header_t packet_in;
}

/*************************************************************************
*********************** P A R S E R ***********************************
*************************************************************************/

parser MyParser(packet_in packet,
out headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {

state start {
transition parse_ethernet;
}

state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
TYPE_IPV4: parse_ipv4;
default: accept;
}
}

state parse_ipv4 {
packet.extract(hdr.ipv4);
transition accept;
}

}

/*************************************************************************
************ C H E C K S U M V E R I F I C A T I O N *************
*************************************************************************/

control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
apply { }
}


/*************************************************************************
************** I N G R E S S P R O C E S S I N G *******************
*************************************************************************/

control MyIngress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
action drop() {
mark_to_drop(standard_metadata);
}

action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) {
standard_metadata.egress_spec = port;
hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
hdr.ethernet.dstAddr = dstAddr;
hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
}

action to_controller() {
standard_metadata.egress_spec = CPU_PORT;
hdr.packet_in.setValid();
hdr.packet_in.ingress_port = standard_metadata.ingress_port;
}

table ipv4_lpm {
key = {
hdr.ipv4.dstAddr: lpm;
}
actions = {
ipv4_forward;
drop;
NoAction;
to_controller;
}
size = 1024;
default_action = drop();
}

apply {
if (hdr.ipv4.isValid()) {
ipv4_lpm.apply();
}
}
}

/*************************************************************************
**************** E G R E S S P R O C E S S I N G *******************
*************************************************************************/

control MyEgress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
apply {
}
}

/*************************************************************************
************* C H E C K S U M C O M P U T A T I O N **************
*************************************************************************/

control MyComputeChecksum(inout headers hdr, inout metadata meta) {
apply {
update_checksum(
hdr.ipv4.isValid(),
{ hdr.ipv4.version,
hdr.ipv4.ihl,
hdr.ipv4.diffserv,
hdr.ipv4.totalLen,
hdr.ipv4.identification,
hdr.ipv4.flags,
hdr.ipv4.fragOffset,
hdr.ipv4.ttl,
hdr.ipv4.protocol,
hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr },
hdr.ipv4.hdrChecksum,
HashAlgorithm.csum16);
}
}

/*************************************************************************
*********************** D E P A R S E R *******************************
*************************************************************************/

control MyDeparser(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr.packet_in);
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv4);
}
}

/*************************************************************************
*********************** S W I T C H *******************************
*************************************************************************/

V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;
Loading