diff --git a/P4D2_2017_Fall/exercises/basic/README.md b/P4D2_2017_Fall/exercises/basic/README.md index b84f732..b335042 100644 --- a/P4D2_2017_Fall/exercises/basic/README.md +++ b/P4D2_2017_Fall/exercises/basic/README.md @@ -140,7 +140,7 @@ Other questions to consider: There are several problems that might manifest as you develop your program: -1. `basic.p4` might fails to compile. In this case, `run.sh` will +1. `basic.p4` might fail to compile. In this case, `run.sh` will report the error emitted from the compiler and halt. 2. `basic.p4` might compile but fail to support the control plane @@ -167,6 +167,7 @@ make stop ## Next Steps -Congratulations, your implementation works! Move on to the next -exercise: implementing the [basic tunneling](./basic_tunnel)! +Congratulations, your implementation works! In the next exercise we +will build on top of this and add support for a basic tunneling +protocol: [basic_tunnel](../basic_tunnel)! diff --git a/P4D2_2017_Fall/exercises/basic/receive.py b/P4D2_2017_Fall/exercises/basic/receive.py index c93182f..f729682 100755 --- a/P4D2_2017_Fall/exercises/basic/receive.py +++ b/P4D2_2017_Fall/exercises/basic/receive.py @@ -6,7 +6,7 @@ import os from scapy.all import sniff, sendp, hexdump, get_if_list, get_if_hwaddr from scapy.all import Packet, IPOption from scapy.all import ShortField, IntField, LongField, BitField, FieldListField, FieldLenField -from scapy.all import IP, UDP, Raw +from scapy.all import IP, TCP, UDP, Raw from scapy.layers.inet import _IPOption_HDR def get_if(): @@ -34,10 +34,11 @@ class IPOption_MRI(IPOption): IntField("", 0), length_from=lambda pkt:pkt.count*4) ] def handle_pkt(pkt): - print "got a packet" - pkt.show2() -# hexdump(pkt) - sys.stdout.flush() + if TCP in pkt and pkt[TCP].dport == 1234: + print "got a packet" + pkt.show2() + # hexdump(pkt) + sys.stdout.flush() def main(): @@ -45,7 +46,7 @@ def main(): iface = ifaces[0] print "sniffing on %s" % iface sys.stdout.flush() - sniff(filter="tcp", iface = iface, + sniff(iface = iface, prn = lambda x: handle_pkt(x)) if __name__ == '__main__': diff --git a/P4D2_2017_Fall/exercises/basic_tunnel/README.md b/P4D2_2017_Fall/exercises/basic_tunnel/README.md index 82bf50b..effbc4b 100644 --- a/P4D2_2017_Fall/exercises/basic_tunnel/README.md +++ b/P4D2_2017_Fall/exercises/basic_tunnel/README.md @@ -1,21 +1,15 @@ -# Implementing Basic Forwarding +# Implementing Basic Tunneling ## Introduction -The objective of this exercise is to write a P4 program that -implements basic forwarding. To keep things simple, we will just -implement forwarding for IPv4. +In this exercise, we will add support for a basic tunneling protocol +to the IP router that you completed in the previous assignment. To do so, +we will define a new header type to encapsulate the IP packet and +modify the switch to perform routing using our new header. -With IPv4 forwarding, the switch must perform the following actions -for every packet: (i) update the source and destination MAC addresses, -(ii) decrement the time-to-live (TTL) in the IP header, and (iii) -forward the packet out the appropriate port. - -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. We have already defined -the control plane rules, so you only need to implement the data plane -logic of your P4 program. +The new header type will contain a protocol ID, which indicates the type +of packet being encapsulated, along with a destination ID to be used for +routing. > **Spoiler alert:** There is a reference solution in the `solution` > sub-directory. Feel free to compare your implementation to the @@ -23,23 +17,22 @@ logic of your P4 program. ## Step 1: Run the (incomplete) starter code -The directory with this README also contains a skeleton P4 program, -`basic.p4`, which initially drops all packets. Your job will be to -extend this skeleton program to properly forward IPv4 packets. +The starter code for this assignment is in a file called `basic_tunnel.p4` +and is simply the solution to the IP router from the previous exercise. -Before that, let's compile the incomplete `basic.p4` and bring -up a switch in Mininet to test its behavior. +Let's first compile this code to and send a packet between two end hosts +to ensure that the IP routing is working as expected. 1. In your shell, run: ```bash ./run.sh ``` This will: - * compile `basic.p4`, and + * compile `basic_tunnel.p4`, and * start a Mininet instance with three switches (`s1`, `s2`, `s3`) configured in a triangle, each connected to one host (`h1`, `h2`, and `h3`). - * The hosts are assigned IPs of `10.0.1.1`, `10.0.2.2`, etc. + * The hosts are assigned IPs of `10.0.1.1`, `10.0.2.2`, and `10.0.3.3`. 2. You should now see a Mininet command prompt. Open two terminals for `h1` and `h2`, respectively: @@ -55,12 +48,16 @@ server. In `h2`'s xterm, start the server: ```bash ./send.py 10.0.2.2 "P4 is cool" ``` - The message will not be received. -5. Type `exit` to leave each xterm and the Mininet command line. + The packet should be received at `h2`. If you examine the received + packet you should see that is consists of an Ethernet header, an IP + header, and the message. If you change the destination IP address + (e.g. try to send to `10.0.3.3`) then the message should not be + received by h2. +5. Type `exit` or `Ctrl-D` to leave each xterm and the Mininet command line. -The message was not received because each switch is programmed -according to `basic.p4`, which drops all packets on arrival. -Your job is to extend this file so it forwards packets. +Each switch is forwarding based on the destination IP address. Your +job is to change the switch functionality so that they instead decide +the destination port using our new tunnel header. ### A note about the control plane @@ -69,11 +66,11 @@ 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 -`run.sh` script will install packet-processing rules in the tables of -each switch. These are defined in the `sX-commands.txt` files, where -`X` corresponds to the switch number. +In this exercise, you will need to add a couple of static control plane +rules for the tunneling protocol. As part of bringing up the Mininet +instance, the `run.sh` script will install packet-processing rules in +the tables of each switch. These are defined in the `sX-commands.txt` +files, where `X` corresponds to the switch number. **Important:** A P4 program also defines the interface between the switch pipeline and control plane. The commands in the files @@ -81,67 +78,72 @@ switch pipeline and control plane. The commands in the files and any changes in the P4 program that add or rename tables, keys, or actions will need to be reflected in these command files. -## Step 2: Implement L3 forwarding +## Step 2: Implement Basic Tunneling -The `basic.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. +The `basic_tunnel.p4` file contains an implementation of a basic IP router. +It also contains comments marked with `TODO` which indicate the functionality +that you need to implement. A complete implementation of the `basic_tunnel.p4` +switch will be able to forward based on the contents of a custom encapsulation +header as well as perform normal IP forwarding if the encapsulation header +does not exist in the packet. -A complete `basic.p4` will contain the following components: +Your job will be to do the following: -1. Header type definitions for Ethernet (`ethernet_t`) and IPv4 (`ipv4_t`). -2. **TODO:** Parsers for Ethernet and IPv4 that populate `ethernet_t` and `ipv4_t` fields. -3. An action to drop a packet, using `mark_to_drop()`. -4. **TODO:** An action (called `ipv4_forward`) that: - 1. Sets the egress port for the next hop. - 2. Updates the ethernet destination address with the address of the next hop. - 3. Updates the ethernet source address with the address of the switch. - 4. Decrements the TTL. -5. **TODO:** A control that: - 1. Defines a table that will read an IPv4 destination address, and - invoke either `drop` or `ipv4_forward`. - 2. An `apply` block that applies the table. -6. **TODO:** A deparser that selects the order - in which fields inserted into the outgoing packet. -7. 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. +1. **TODO:** Add a new header type called `myTunnel_t` that contains two 16-bit fields: `proto_id` and `dst_id`. +2. **TODO:** Add a `myTunnel_t` header to the `headers` struct. +2. **TODO:** Update the parser to extract either the `myTunnel` header or `ipv4` header based on the `etherType` field in the Ethernet header. The parser should also extract the `ipv4` header after the `myTunnel` header if `proto_id` == `TYPE_IPV4` (i.e. 0x0800). +3. **TODO:** Define a new action called `myTunnel_forward` that simply sets the egress port (i.e. `egress_spec` field of the `standard_metadata` bus) to the port number provided by the control plane. +4. **TODO:** Define a new table called `myTunnel_exact` that perfoms an exact match on the `dst_id` field of the `myTunnel` header. This table should invoke either the `myTunnel_forward` action if the there is a match in the table and it should invoke the `drop` action otherwise. +5. **TODO:** Update the `apply` statement in the `MyIngress` control block to apply your newly defined `myTunnel_exact` table if the `myTunnel` header is valid. Otherwise, invoke the `ipv4_lpm` table if the `ipv4` header is valid. +6. **TODO:** Update the deparser to emit the `ethernet`, then `myTunnel`, then `ipv4` headers. Remember that the deparser will only emit a header if it is valid. A header's implicit validity bit is set by the parser upon extraction. So there is no need to check header validity here. +7. **TODO:** Add static rules for your newly defined table so that the switches will forward correctly for each possible value of `dst_id`. See the diagram below for the topology's port configuration as well as how we will assign IDs to hosts. For this step you will need to add your forwarding rules to the `sX-commands.txt` files. + +![topology](./topo.pdf) ## Step 3: Run your solution -Follow the instructions from Step 1. This time, your message from -`h1` should be delivered to `h2`. +Follow the instructions from Step 1. This time when you send a packet from +`h1` to `h2` try using the following command to send a packet that uses +our new `myTunnel` header. +```bash +./send.py 10.0.2.2 "P4 is cool" --dst_id 2 +``` + +You should see a packet arrive at `h2` which contains the `MyTunnel` header. +Also note that changing the destination IP address will not prevent the packet +from arriving at `h2`. This is because the switch is no longer using the IP header for routing when the `MyTunnel` header is in the packet. + +> Python Scapy does not natively support the `myTunnel` header +> type so we have provided a file called `myTunnel_header.py` which +> adds support to Scapy for our new custom header. Feel free to inspect +> this file if you are interested in learning how to do this. ### Food for thought -The "test suite" for your solution---sending a message from `h1` to -`h2`---is not very robust. What else should you test to be confident -of your implementation? +To make this tunneling exercise a bit more interesting (and realistic) +how might you change the P4 code to have the switches add the `myTunnel` +header to an IP packet upon ingress to the network and then remove the +`myTunnel` header as the packet leaves to the network to an end host? -> Although the Python `scapy` library is outside the scope of this tutorial, -> it can be used to generate packets for testing. The `send.py` file shows how -> to use it. +Hints: -Other questions to consider: - - How would you enhance your program to support next hops? - - Is this program enough to replace a router? What's missing? + - The ingress switch will need to map the destination IP address to the corresponding `dst_id` for the `myTunnel` header. Also remember to set explicitly set the validity bit for the `myTunnel` header so that it can be emitted by the deparser. + - The egress switch will need to remove the `myTunnel` header from the packet after looking up the appropriate output port using the `dst_id` field. ### Troubleshooting There are several problems that might manifest as you develop your program: -1. `basic.p4` might fails to compile. In this case, `run.sh` will +1. `basic_tunnel.p4` might fail to compile. In this case, `run.sh` will report the error emitted from the compiler and halt. -2. `basic.p4` might compile but fail to support the control plane +2. `basic_tunnel.p4` might compile but fail to support the control plane rules in the `s1-commands.txt` through `s3-command.txt` files that `run.sh` tries to install using the Bmv2 CLI. In this case, `run.sh` will report these errors to `stderr`. Use these error messages to fix -your `basic.p4` implementation. +your `basic_tunnel.p4` implementation or forwarding rules. -3. `basic.p4` might compile, and the control plane rules might be +3. `basic_tunnel.p4` might compile, and the control plane rules might be installed, but the switch might not process packets in the desired way. The `build/logs/.log` files contain detailed logs that describing how each switch processes each packet. The output is @@ -159,5 +161,6 @@ mn -c ## Next Steps -Congratulations, your implementation works! Move on to the next -exercise: implementing [Explicit Congestion Notification](../ecn). +Congratulations, your implementation works! Move onto the next assignment +[p4runtime](../p4runtime)! + diff --git a/P4D2_2017_Fall/exercises/basic_tunnel/basic_tunnel.p4 b/P4D2_2017_Fall/exercises/basic_tunnel/basic_tunnel.p4 index 9d371c3..68cba8d 100644 --- a/P4D2_2017_Fall/exercises/basic_tunnel/basic_tunnel.p4 +++ b/P4D2_2017_Fall/exercises/basic_tunnel/basic_tunnel.p4 @@ -2,6 +2,7 @@ #include #include +// NOTE: new type added here const bit<16> TYPE_MYTUNNEL = 0x1212; const bit<16> TYPE_IPV4 = 0x800; @@ -19,6 +20,7 @@ header ethernet_t { bit<16> etherType; } +// NOTE: added new header type header myTunnel_t { bit<16> proto_id; bit<16> dst_id; @@ -43,6 +45,7 @@ struct metadata { /* empty */ } +// NOTE: Added new header type to headers struct struct headers { ethernet_t ethernet; myTunnel_t myTunnel; @@ -53,6 +56,7 @@ struct headers { *********************** P A R S E R *********************************** *************************************************************************/ +// TODO: Update the parser to parse the myTunel header as well parser MyParser(packet_in packet, out headers hdr, inout metadata meta, @@ -65,17 +69,8 @@ parser MyParser(packet_in packet, state parse_ethernet { packet.extract(hdr.ethernet); transition select(hdr.ethernet.etherType) { - TYPE_MYTUNNEL: parse_myTunnel; - TYPE_IPV4: parse_ipv4; - default: accept; - } - } - - state parse_myTunnel { - packet.extract(hdr.myTunnel); - transition select(hdr.myTunnel.proto_id) { - TYPE_IPV4: parse_ipv4; - default: accept; + TYPE_IPV4 : parse_ipv4; + default : accept; } } @@ -84,6 +79,7 @@ parser MyParser(packet_in packet, transition accept; } + } /************************************************************************* @@ -125,27 +121,17 @@ control MyIngress(inout headers hdr, size = 1024; default_action = NoAction(); } - - action myTunnel_forward(egressSpec_t port) { - standard_metadata.egress_spec = port; - } - table myTunnel_exact { - key = { - hdr.myTunnel.dst_id: exact; - } - actions = { - myTunnel_forward; - drop; - } - size = 1024; - default_action = drop(); - } + // TODO: declare a new action: myTunnel_forward(egressSpec_t port) + + + // TODO: declare a new table: myTunnel_exact + // TODO: also remember to add table entries! + apply { - if (hdr.myTunnel.isValid()) { - myTunnel_exact.apply(); - } else if (hdr.ipv4.isValid()) { + // TODO: Update control flow + if (hdr.ipv4.isValid()) { ipv4_lpm.apply(); } } @@ -192,7 +178,7 @@ control MyComputeChecksum(inout headers hdr, inout metadata meta) { control MyDeparser(packet_out packet, in headers hdr) { apply { packet.emit(hdr.ethernet); - packet.emit(hdr.myTunnel); + // TODO: emit myTunnel header as well packet.emit(hdr.ipv4); } } diff --git a/P4D2_2017_Fall/exercises/basic_tunnel/receive.py b/P4D2_2017_Fall/exercises/basic_tunnel/receive.py index e83dd5e..167a9c8 100755 --- a/P4D2_2017_Fall/exercises/basic_tunnel/receive.py +++ b/P4D2_2017_Fall/exercises/basic_tunnel/receive.py @@ -6,7 +6,7 @@ import os from scapy.all import sniff, sendp, hexdump, get_if_list, get_if_hwaddr from scapy.all import Packet, IPOption from scapy.all import ShortField, IntField, LongField, BitField, FieldListField, FieldLenField -from scapy.all import IP, UDP, Raw +from scapy.all import IP, TCP, UDP, Raw from scapy.layers.inet import _IPOption_HDR from myTunnel_header import MyTunnel @@ -23,7 +23,7 @@ def get_if(): return iface def handle_pkt(pkt): - if MyTunnel in pkt: + if MyTunnel in pkt or (TCP in pkt and pkt[TCP].dport == 1234): print "got a packet" pkt.show2() # hexdump(pkt) diff --git a/P4D2_2017_Fall/exercises/basic_tunnel/send.py b/P4D2_2017_Fall/exercises/basic_tunnel/send.py index 5279cba..4f9828c 100755 --- a/P4D2_2017_Fall/exercises/basic_tunnel/send.py +++ b/P4D2_2017_Fall/exercises/basic_tunnel/send.py @@ -4,6 +4,7 @@ import sys import socket import random import struct +import argparse from scapy.all import sendp, send, get_if_list, get_if_hwaddr, hexdump from scapy.all import Packet @@ -23,18 +24,25 @@ def get_if(): return iface def main(): + parser = argparse.ArgumentParser() + parser.add_argument('ip_addr', type=str, help="The destination IP address to use") + parser.add_argument('message', type=str, help="The message to include in packet") + parser.add_argument('--dst_id', type=int, default=None, help='The myTunnel dst_id to use, if unspecified then myTunnel header will not be included in packet') + args = parser.parse_args() - if len(sys.argv)<4: - print 'pass 3 arguments: ""' - exit(1) - - addr = socket.gethostbyname(sys.argv[1]) - dst_id = int(sys.argv[2]) + addr = socket.gethostbyname(args.ip_addr) + dst_id = args.dst_id iface = get_if() - print "sending on interface %s to id %s" % (iface, str(dst_id)) - pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff') - pkt = pkt / MyTunnel(dst_id=dst_id) / IP(dst=addr) / sys.argv[3] + if (dst_id is not None): + print "sending on interface {} to dst_id {}".format(iface, str(dst_id)) + pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff') + pkt = pkt / MyTunnel(dst_id=dst_id) / IP(dst=addr) / args.message + else: + print "sending on interface {} to IP addr {}".format(iface, str(addr)) + pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff') + pkt = pkt / IP(dst=addr) / TCP(dport=1234, sport=random.randint(49152,65535)) / args.message + pkt.show2() # hexdump(pkt) # print "len(pkt) = ", len(pkt) diff --git a/P4D2_2017_Fall/exercises/basic_tunnel/solution/basic_tunnel.p4 b/P4D2_2017_Fall/exercises/basic_tunnel/solution/basic_tunnel.p4 index 9d371c3..83b29db 100644 --- a/P4D2_2017_Fall/exercises/basic_tunnel/solution/basic_tunnel.p4 +++ b/P4D2_2017_Fall/exercises/basic_tunnel/solution/basic_tunnel.p4 @@ -143,11 +143,15 @@ control MyIngress(inout headers hdr, } apply { - if (hdr.myTunnel.isValid()) { - myTunnel_exact.apply(); - } else if (hdr.ipv4.isValid()) { + if (hdr.ipv4.isValid() && !hdr.myTunnel.isValid()) { + // Process only non-tunneled IPv4 packets ipv4_lpm.apply(); } + + if (hdr.myTunnel.isValid()) { + // process tunneled packets + myTunnel_exact.apply(); + } } } diff --git a/P4D2_2017_Fall/exercises/basic_tunnel/topo.pdf b/P4D2_2017_Fall/exercises/basic_tunnel/topo.pdf new file mode 100644 index 0000000..111acd6 Binary files /dev/null and b/P4D2_2017_Fall/exercises/basic_tunnel/topo.pdf differ