The objective of this exercise is to write a P4 program that implements packet cloning in IPv4.
Cloning a packet can be done in two ways: (i) from ingress-to-egress and (ii) from egress-to-egress. The source and destination points determine where the cloning action takes place (ingress or egress) and in which pipeline the packet is cloned to (egress).
To clone the packet, the switch must perform the following actions for every packet: (i) determine the type of the clone and relate the packet to a mirror session ID, so it can be later identified, (ii) determine if the packet instance type corresponds to that of a cloned packet so that some actions can be applied to modify its headers (it will be easier to quickly determine what the copy is).
Your switch will have a single table, which the control plane will populate with static rules. Each rule will map an IP address to the MAC address and output port for the next hop. Besides, commands are provided to the switches to enforce a specific port to which cloned packets will be forwarded. We have already defined the control plane rules, so you only need to implement the data plane logic of your P4 program.
Spoiler alert: There is a reference solution in the
solution
sub-directory. Feel free to compare your implementation to the reference.
The directory with this README also contains a skeleton P4 program,
clone.p4
, which initially forwards one IPv4 packet. Your job will
be to extend this skeleton program to properly clone packets.
Before that, let's compile the incomplete clone.p4
and bring
up a switch in Mininet to test its behavior.
-
In your shell, run:
make run
This will:
- compile
clone.p4
, and - start a Mininet instance with three switches (
s1
,s2
,s3
) configured in a triangle, each connected to one host (h1
,h2
, andh3
). - The hosts are assigned IPs of
10.0.1.1
,10.0.2.2
, and10.0.3.3
.
- compile
-
You should now see a Mininet command prompt. Open two terminals for
h1
andh2
, respectively:mininet> xterm h1 h2
-
Each host includes a small Python-based messaging client and server. In
h2
's xterm, start the server:./receive.py
-
In
h1
's xterm, send a message toh2
:./send.py 10.0.2.2 "P4 is cool"
The message will be received once.
-
Type
exit
to leave each xterm and the Mininet command line. Then, to stop mininet:make stop
And to delete all pcaps, build files, and logs:
make clean
The message was received because the forwarading behaviour is provided. However, a copy is expected in the exercise. Your job is to extend this file so it initially clones the packet to be forwarded.
A P4 program defines a packet-processing pipeline, but the rules within each table are inserted by the control plane. When a rule matches a packet, its action is invoked with parameters supplied by the control plane as part of the rule.
In this exercise, we have already implemented the the control plane
logic for you. As part of bringing up the Mininet instance, the
make run
command will install specific mirroring/cloning commands and
packet-processing rules in the tables of each switch. These are defined
in the sX-commands.txt
and the sX-runtime.json
files, where X
corresponds to the switch number.
Important: We use P4Runtime to install the control plane rules. The
content of files sX-commands.txt
and sX-runtime.json
refer to specific
commands, names of tables, keys, and actions, as defined in the P4Info file
produced by the compiler (look for the file build/clone.p4info
after
executing make run
or compile.sh
). Any changes in the P4 program that
add or rename tables, keys, or actions will need to be reflected in
the sX-runtime.json
files.
The clone.p4
file contains a skeleton P4 program with key pieces of
logic replaced by TODO
comments. Your implementation should follow
the structure given in this file---replace each TODO
with logic
implementing the missing piece.
A complete clone.p4
will contain the following components:
- Header type definitions for Ethernet (
ethernet_t
) and IPv4 (ipv4_t
). - Parsers for Ethernet and IPv4 that populate
ethernet_t
andipv4_t
fields. - An action to drop a packet, using
mark_to_drop()
. - An action (called
ipv4_forward
) that:- Sets the egress port for the next hop.
- Updates the ethernet destination address with the address of the next hop.
- Updates the ethernet source address with the address of the switch.
- Decrements the TTL.
- A control ingress that:
- Defines a table that will read an IPv4 destination address, and
invoke either
drop
oripv4_forward
. - TODO: Defines the clone type and the mirror session ID.
- An
apply
block that applies the table and call the clone action.
- Defines a table that will read an IPv4 destination address, and
invoke either
- A control egress that:
- Implements a way to clear the TTL value and to change the source IPv4 address.
- TODO An
apply
block that determines whether the packet is cloned, and if so, call the relevant actions.
- A deparser that selects the order in which fields inserted into the outgoing packet.
- A
package
instantiation supplied with the parser, control, and deparser.In general, a package also requires instances of checksum verification and recomputation controls. These are not necessary for this tutorial and are replaced with instantiations of empty controls.
Follow the instructions from Step 1. This time, your message from h1
should be delivered twice to h2
, with some minor differences in its headers.
Note: in case you still received one message when applying the solution, try exiting the Mininet CLI and run "make clean" and "make run" again.
Questions to consider:
- Is the order between forwarding and cloning relevant? How does it react for each case?
- Why is a cloned packet being forwarded to the source of the communication? (That is, when sending from h1 to h2, why the packet is also reaching h1?)
- Why is a default egress port assigned per switch in the
sX-commands.txt
, instead of leveraging onstandard_metadata.egress_spec
?
There are several problems that might manifest as you develop your program:
-
clone.p4
might fail to compile. In this case,make run
will report the error emitted from the compiler and halt. -
clone.p4
might compile, and the control plane rules might be installed, but the switch might not process packets in the desired way. The/tmp/p4s.<switch-name>.log
files contain detailed logs that describing how each switch processes each packet. The output is detailed and can help pinpoint logic errors in your implementation.
In the latter two cases above, make run
may leave a Mininet instance
running in the background. Use the following command to clean up
these instances:
make clean