Skip to content

Commit

Permalink
Upload Course Material
Browse files Browse the repository at this point in the history
  • Loading branch information
ImanRHT committed Dec 20, 2024
1 parent 467501e commit e5f8804
Show file tree
Hide file tree
Showing 147 changed files with 17,602 additions and 0 deletions.
Binary file added Assignment/.DS_Store
Binary file not shown.
Binary file added Assignment/P4_tutorial.pdf
Binary file not shown.
Binary file added Assignment/exercises/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions Assignment/exercises/acl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BMV2_SWITCH_EXE = simple_switch_grpc
NO_P4 = true
P4C_ARGS = --p4runtime-file $(basename $@).p4info --p4runtime-format text

include ../../utils/Makefile
182 changes: 182 additions & 0 deletions Assignment/exercises/acl/acl.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>

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;

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 {
/* empty */
}

struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
}

/*************************************************************************
*********************** 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 select(hdr.ipv4.protocol) {
default: 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();
}

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;
}

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


apply {
if (hdr.ipv4.isValid()) {
ipv4_lpm.apply();
/* TODO: add your table to the control flow */

}

}
}

/*************************************************************************
**************** 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 {
}
}

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

V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;
60 changes: 60 additions & 0 deletions Assignment/exercises/acl/receive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python
import sys
import struct
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, TCP, UDP, Raw
from scapy.layers.inet import _IPOption_HDR

def get_if():
ifs=get_if_list()
iface=None
for i in get_if_list():
if "eth0" in i:
iface=i
break;
if not iface:
print "Cannot find eth0 interface"
exit(1)
return iface

class IPOption_MRI(IPOption):
name = "MRI"
option = 31
fields_desc = [ _IPOption_HDR,
FieldLenField("length", None, fmt="B",
length_of="swids",
adjust=lambda pkt,l:l+4),
ShortField("count", 0),
FieldListField("swids",
[],
IntField("", 0),
length_from=lambda pkt:pkt.count*4) ]
def handle_pkt(pkt, input_dport):
if TCP in pkt and pkt[TCP].dport == input_dport:
# print "got a packet"
# pkt.show2()
print(pkt.load)
# hexdump(pkt)
sys.stdout.flush()
elif UDP in pkt and pkt[UDP].dport == input_dport:
# print "got a packet"
# pkt.show2()
print(pkt.load)
# hexdump(pkt)
sys.stdout.flush()


def main():
ifaces = filter(lambda i: 'eth' in i, os.listdir('/sys/class/net/'))
iface = ifaces[0]
# print "sniffing on %s" % iface
sys.stdout.flush()
sniff(iface = iface,
prn = lambda x: handle_pkt(x, int(sys.argv[1])))

if __name__ == '__main__':
main()
57 changes: 57 additions & 0 deletions Assignment/exercises/acl/s1-acl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"target": "bmv2",
"p4info": "build/acl.p4info",
"bmv2_json": "build/acl.json",
"table_entries": [
{
"table": "MyIngress.ipv4_lpm",
"default_action": true,
"action_name": "MyIngress.drop",
"action_params": { }
},
{
"table": "MyIngress.ipv4_lpm",
"match": {
"hdr.ipv4.dstAddr": ["10.0.1.1", 32]
},
"action_name": "MyIngress.ipv4_forward",
"action_params": {
"dstAddr": "00:00:00:00:01:01",
"port": 1
}
},
{
"table": "MyIngress.ipv4_lpm",
"match": {
"hdr.ipv4.dstAddr": ["10.0.1.2", 32]
},
"action_name": "MyIngress.ipv4_forward",
"action_params": {
"dstAddr": "00:00:00:00:01:02",
"port": 2
}
},
{
"table": "MyIngress.ipv4_lpm",
"match": {
"hdr.ipv4.dstAddr": ["10.0.1.3", 32]
},
"action_name": "MyIngress.ipv4_forward",
"action_params": {
"dstAddr": "00:00:00:00:01:03",
"port": 3
}
},
{
"table": "MyIngress.ipv4_lpm",
"match": {
"hdr.ipv4.dstAddr": ["10.0.1.4", 32]
},
"action_name": "MyIngress.ipv4_forward",
"action_params": {
"dstAddr": "00:00:00:00:01:04",
"port": 4
}
}
]
}
44 changes: 44 additions & 0 deletions Assignment/exercises/acl/send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python
import argparse
import sys
import socket
import random
import struct

from scapy.all import sendp, send, get_if_list, get_if_hwaddr
from scapy.all import Packet
from scapy.all import Ether, IP, UDP, TCP

def get_if():
ifs=get_if_list()
iface=None # "h1-eth0"
for i in get_if_list():
if "eth0" in i:
iface=i
break;
if not iface:
print "Cannot find eth0 interface"
exit(1)
return iface

def main():

if len(sys.argv)<3:
print 'pass 2 arguments: <destination> <type> <dport> "<message>"'
exit(1)

addr = socket.gethostbyname(sys.argv[1])
iface = get_if()

print "sending on interface %s to %s" % (iface, str(addr))
pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff')
if sys.argv[2] == "TCP":
pkt = pkt /IP(dst=addr) / TCP(dport=int(sys.argv[3]), sport=random.randint(49152,65535)) / sys.argv[4]
else:
pkt = pkt /IP(dst=addr) / UDP(dport=int(sys.argv[3]), sport=random.randint(49152,65535)) / sys.argv[4]
pkt.show2()
sendp(pkt, iface=iface, verbose=False)


if __name__ == '__main__':
main()
15 changes: 15 additions & 0 deletions Assignment/exercises/acl/topology.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"hosts": [
"h1",
"h2",
"h3",
"h4"
],
"switches": {
"s1": { "runtime_json" : "s1-acl.json" }
},
"links": [
["h1", "s1"], ["s1", "h2"],
["s1", "h3"], ["s1", "h4"]
]
}
Binary file added Assignment/p4-cheat-sheet.pdf
Binary file not shown.
Loading

0 comments on commit e5f8804

Please sign in to comment.