Skip to content

Commit ab29d06

Browse files
authored
Update Flowcache exercise (#679)
* Add Flowcache to main README. Update Flowcache README and P4 Signed-off-by: Dscano <d.scano89@gmail.com> * Update Flowcache README and P4 Signed-off-by: Dscano <d.scano89@gmail.com> * Update Flowcache README Signed-off-by: Dscano <d.scano89@gmail.com> * Update counters in P4 code and Added counters and flow rules check Signed-off-by: Dscano <d.scano89@gmail.com> * Update P4 flow rule to perform ping properly, WIP debug Packet-in Signed-off-by: Dscano <d.scano89@gmail.com> * Network reachability fixed; all hosts are now reachable. Signed-off-by: Dscano <d.scano89@gmail.com> * Fixed Counter Reading Signed-off-by: Dscano <d.scano89@gmail.com> * Fixed Counter in P4, Debugging idle_time Signed-off-by: Dscano <d.scano89@gmail.com> * Final implementation of flowcache Signed-off-by: Dscano <d.scano89@gmail.com> * Fix section 2 title Signed-off-by: Dscano <d.scano89@gmail.com> --------- Signed-off-by: Dscano <d.scano89@gmail.com>
1 parent 83d5af4 commit ab29d06

6 files changed

Lines changed: 973 additions & 455 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ you get started with P4 programming, organized into several modules:
3434
- [P4Runtime](./exercises/p4runtime)<br>
3535
<small>This exercise involves implementing a control plane using P4Runtime to send flow entries to switches for tunneling traffic between hosts. Students modify the provided P4 program and controller script to establish connections, push P4 programs, install tunnel ingress rules, and read tunnel counters, enhancing their understanding of P4Runtime and network forwarding logic.</small>
3636

37+
- [Flowcache](./exercises/flowcache)<br>
38+
<small> This exercise implements a program named flowcache.p4, which handles the PacketIn and PacketOut mechanisms, along with an idle timeout mechanism for table entries. In the data plane, packets are sent to the P4Runtime controller using the PacketIn. Upon receiving these packets, the controller adds an entry to the flow table. While the controller computes and installs the flow rule, PacketOut are sent to forward the packets to its destination. Once a flow entry is installed and no packets match it, the idle timeout start. Once a flow entry is installed and no packets match it, the idle timeout starts. When the timer expires, an IdleTimeoutNotification message is sent to the controller, which is responsible for reinstalling the expired flow entry.</small>
39+
3740
3. Monitoring and Debugging
3841
- [Explicit Congestion Notification](./exercises/ecn)<br>
3942
<small>In this tutorial, you'll enhance a basic L3 forwarding P4 program with Explicit Congestion Notification (ECN) support, enabling end-to-end notification of network congestion without packet drops. By modifying the `ecn.p4` file, you'll implement ECN logic such as updating the ECN flag based on queue length thresholds and configuring static rules for proper ECN handling, followed by testing the solution in Mininet to verify packet forwarding and ECN flag manipulation.</small>

exercises/flowcache/README.md

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11

22
[comment]: # (SPDX-License-Identifier: Apache-2.0)
33

4-
# WORK IN PROGRESS
5-
64
## Introduction
75

8-
In this exercise, we will be using P4Runtime to send flow entries to the
9-
switch instead of using the switch's CLI. We will be building on the same P4
10-
program that you used in the [basic_tunnel](../basic_tunnel) exercise. The
11-
P4 program has been renamed to `advanced_tunnel.p4` and has been augmented
12-
with two counters (`ingressTunnelCounter`, `egressTunnelCounter`) and
13-
two new actions (`myTunnel_ingress`, `myTunnel_egress`).
6+
The goal of this exercise is to write a P4 program that implements the `PacketIn` and `PacketOut` mechanisms, along with an idle timeout mechanism for table entries. The control plane uses P4Runtime to configure and manage the data plane. The P4 program `flowcache.p4` contains one ingress table (`flow_cache`) and two counters (`ingressPktOutCounter`, `egressPktInCounter`).
7+
8+
The action `flow_unknown` in the `flow_cache` table triggers the generation of a PacketIn when no flow rules match the packet. The PacketIn is then forwarded to the controller via P4Runtime. Upon receiving these packets, the controller adds an entry to the flow table with action `cached_action`. While the controller computes and installs the flow rule, PacketOut messages are sent via P4Runtime to the data plane in order to forward the packets to their destination.
9+
10+
Once a flow entry is installed and no packets match it, the idle timeout start. Once a flow entry is installed and no packets match it, the idle timeout starts. When the timer expires, an IdleTimeoutNotification message is sent to the controller.
11+
12+
The Counters `ingressPktOutCounter` and `egressPktInCounter` are used to verify whether `PacketIn` and `PacketOut` messages are sent or received.
1413

1514
You will use the starter program, `mycontroller.py`, and a few helper
1615
libraries in the `p4runtime_lib` directory to create the table entries
17-
necessary to tunnel traffic between host 1 and 2.
16+
necessary to forward traffic between hosts.
1817

1918
> **Spoiler alert:** There is a reference solution in the `solution`
2019
> sub-directory. Feel free to compare your implementation to the
2120
> reference.
2221
2322
## Step 1: Run the (incomplete) starter code
2423

25-
The starter code for this assignment is in a file called `mycontroller.py`,
26-
and it will install only some of the rules that you need to tunnel traffic between
27-
two hosts.
24+
The starter code for this assignment is in files called `flowcache.p4` and `mycontroller.py`. Your job will be to finalize the P4 program to properly implement the `PacketIn` and `PacketOut` idle timeout mechanismes.
2825

29-
Let's first compile the new P4 program, start the network, use `mycontroller.py`
30-
to install a few rules, and look at the `ingressTunnelCounter` to see that things
31-
are working as expected.
26+
Let's first compile the new P4 program, start the network, and use `mycontroller.py` to install the flowcache pipeline in the data plane.
3227

3328
1. In your shell, run:
3429
```bash
3530
make
3631
```
3732
This will:
38-
* compile `advanced_tunnel.p4`,
33+
* compile `flowcache.p4`,
3934
* start a Mininet instance with three switches (`s1`, `s2`, `s3`)
4035
configured in a triangle, each connected to one host (`h1`, `h2`, `h3`), and
4136
* assign IPs of `10.0.1.1`, `10.0.2.2`, `10.0.3.3` to the respective hosts.
@@ -49,31 +44,21 @@ are working as expected.
4944

5045
3. Open another shell and run the starter code:
5146
```bash
52-
cd ~/tutorials/exercises/p4runtime
47+
cd ~/tutorials/exercises/flowcache
5348
./mycontroller.py
5449
```
55-
This will install the `advanced_tunnel.p4` program on the switches and push the
56-
tunnel ingress rules.
57-
The program prints the tunnel ingress and egress counters every 2 seconds.
58-
You should see the ingress tunnel counter for s1 increasing:
59-
```
60-
s1 ingressTunnelCounter 100: 2 packets
61-
```
62-
The other counters should remain at zero.
63-
50+
This will install the `flowcache.p4` program on the switches, and it will wait for notifications from the data plane.
6451
4. Press `Ctrl-C` to the second shell to stop `mycontroller.py`
6552

66-
Each switch is currently mapping traffic into tunnels based on the destination IP
67-
address. Your job is to write the rules that forward the traffic between the switches
68-
based on the tunnel ID.
53+
Each switch currently has no flow rules. So, as soon as a packet is received, it will be sent to the control plane within a PacketIn. Your job is to write the functions that handle PacketIn, PacketOut, and idle timeout mechanisms. Then, you can verify that PacketIn and PacketOut are working by reading the counters.
6954

7055
### Potential Issues
7156

7257
If you see the following error message when running `mycontroller.py`, then
7358
the gRPC server is not running on one or more switches.
7459

7560
```
76-
p4@p4:~/tutorials/exercises/p4runtime$ ./mycontroller.py
61+
p4@p4:~/tutorials/exercises/flowcache$ ./mycontroller.py
7762
...
7863
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, Connect Failed)>
7964
```
@@ -95,7 +80,7 @@ table entries like we have in the previous exercises.
9580

9681
**Important:** A P4 program also defines the interface between the
9782
switch pipeline and control plane. This interface is defined in the
98-
`advanced_tunnel.p4info` file. The table entries that you build in `mycontroller.py`
83+
`flow_cache.p4info` file. The table entries that you build in `mycontroller.py`
9984
refer to specific tables, keys, and actions by name, and we use a P4Info helper
10085
to convert the names into the IDs that are required for P4Runtime. Any changes
10186
in the P4 program that add or rename tables, keys, or actions will need to be
@@ -126,19 +111,20 @@ CPU port for your P4 program to process. The controller metadata
126111
header, if any, will always be the _first_ header of the packet as
127112
seen by your P4 parser.
128113

129-
## Step 2: Implement Tunnel Forwarding
114+
## Step 2: Implement flowcache control messages handling
130115

131116
The `mycontroller.py` file is a basic controller plane that does the following:
132117
1. Establishes a gRPC connection to the switches for the P4Runtime service.
133118
2. Pushes the P4 program to each switch.
134-
3. Writes tunnel ingress and tunnel egress rules for two tunnels between h1 and h2.
135-
4. Reads tunnel ingress and egress counters every 2 seconds.
119+
3. Receives `PacketIn` messages and generates flow rules to provide connectivity among all hosts.
120+
4. Sends `PacketOut` messages to forward the packets to their destination while the corresponding flow rules are not yet installed.
121+
5. Handles the `IdleTimeoutNotification`.
122+
6. Reads `PacketIn` and `PacketOut` ingress and egress counters periodically.
136123

137124
It also contains comments marked with `TODO` which indicate the functionality
138125
that you need to implement.
139126

140-
Your job will be to write the tunnel transit rule in the `writeTunnelRules` function
141-
that will match on tunnel ID and forward packets to the next hop.
127+
Your job will be to write functions that will handle the `PacketIn`, `PacketOut` and idle timeout mechanismes.
142128

143129
![topology](../basic_tunnel/topo.png)
144130

@@ -179,14 +165,6 @@ You might notice that the rules that are printed by `mycontroller.py` contain th
179165
IDs rather than the table names. You can use the P4Info helper to translate these IDs
180166
into entry names.
181167

182-
Also, you may want to think about the following:
183-
- What assumptions about the topology are baked into your implementation? How would you
184-
need to change it for a more realistic network?
185-
186-
- Why are the byte counters different between the ingress and egress counters?
187-
188-
- What is the TTL in the ICMP replies? Why is it the value that it is?
189-
Hint: The default TTL is 64 for packets sent by the hosts.
190168

191169
If you are interested, you can find the protocol buffer and gRPC definitions here:
192170
- [P4Runtime](https://github.com/p4lang/p4runtime/blob/main/proto/p4/v1/p4runtime.proto)
@@ -208,13 +186,11 @@ To run the reference solution, you should run the following command from the
208186
solution/mycontroller.py
209187
```
210188

211-
212189
## Next Steps
213190

214191
Congratulations, your implementation works! Move onto the next assignment
215192
[Explicit Congestion Notification](../ecn)
216193

217-
218194
## Relevant Documentation
219195

220196
Documentation on the Usage of Gateway (gw) and ARP Commands in topology.json is [here](https://github.com/p4lang/tutorials/tree/master/exercises/basic#the-use-of-gateway-gw-and-arp-commands-in-topologyjson)

0 commit comments

Comments
 (0)