Skip to content

Commit ee10704

Browse files
committed
Initial commit
1 parent 86c6b06 commit ee10704

File tree

14 files changed

+1068
-4
lines changed

14 files changed

+1068
-4
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# CWM-ProgNets
2-
Welcome to the Programmable Networks course work module!
1+
# CWM-ProgNets-Internal
2+
Welcome to Programmable Networks Devices CWM!
33

4-
This repository contains the exercises for the CWM. Technical material and instructions can be found on Canvas.
4+
This repository is organised as follows:
55

6-
Please fork this repository to your user.
6+
* assignmentX - A folder dedicated to assignment X. Not all assignments have a dedicated folder.
7+
8+
* scripts - Useful scripts for various assignments. Refer to the notes for guidance.

assignment1/send.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python
2+
3+
from scapy.all import Ether, IP, sendp, get_if_hwaddr, get_if_list, TCP, Raw, UDP
4+
import sys
5+
import random, string
6+
7+
8+
def randomword(length):
9+
return ''.join(random.choice(string.ascii_lowercase) for i in range(length))
10+
11+
def send_random_traffic(num_packets, interface, src_ip, dst_ip):
12+
dst_mac = "00:00:00:00:00:01"
13+
src_mac= "00:00:00:00:00:02"
14+
total_pkts = 0
15+
port = 1024
16+
for i in range(num_packets):
17+
data = randomword(22)
18+
p = Ether(dst=dst_mac,src=src_mac)/IP(dst=dst_ip,src=src_ip)
19+
p = p/UDP(sport= 50000, dport=port)/Raw(load=data)
20+
sendp(p, iface = interface, inter = 0.01)
21+
# If you want to see the contents of the packet, uncomment the line below
22+
# print(p.show())
23+
total_pkts += 1
24+
print("Sent %s packets in total" % total_pkts)
25+
26+
if __name__ == '__main__':
27+
if len(sys.argv) < 5:
28+
print("Usage: python send.py number_of_packets interface_name src_ip_address dst_ip_address")
29+
sys.exit(1)
30+
else:
31+
num_packets = sys.argv[1]
32+
interface = sys.argv[2]
33+
src_ip = sys.argv[3]
34+
dst_ip = sys.argv[4]
35+
send_random_traffic(int(num_packets), interface, src_ip, dst_ip)

assignment2/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Plotting Figures
2+
3+
4+
1. Save your data a space-delimied format, such as in the example `test.data` file.
5+
6+
The first column contains x-axis values (e.g., time). The second column contains y-axis values (e.g., packet drops).
7+
8+
2. Using a text editor, modify paramters in `plot.py`
9+
10+
The parameters to be modified are:
11+
12+
- filename - name of the data file.
13+
- label – label to be used in the legend, e.g., “iperf2” or “iperf3”.
14+
- xlabel – x-axis label
15+
- ylabel – y-axis label
16+
- title – title of the plotted graph
17+
- fig\_name – name of the saved file
18+
19+
3. To plot your figure, run the python script:
20+
```
21+
python3 plot.py
22+
```

assignment2/plot.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# !/usr/bin/python3
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
# parameters to modify
6+
filename="test.data"
7+
label='label'
8+
xlabel = 'xlabel'
9+
ylabel = 'ylabel'
10+
title='Simple plot'
11+
fig_name='test.png'
12+
13+
14+
t = np.loadtxt(filename, delimiter=" ", dtype="float")
15+
16+
plt.plot(t[:,0], t[:,1], label=label) # Plot some data on the (implicit) axes.
17+
plt.xlabel(xlabel)
18+
plt.ylabel(ylabel)
19+
plt.title(title)
20+
plt.legend()
21+
plt.savefig(fig_name)
22+
plt.show()

assignment2/test.data

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 100
2+
2 300.239
3+
3 500

assignment3/reflector.p4

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/* -*- P4_16 -*- */
2+
#include <core.p4>
3+
#include <v1model.p4>
4+
5+
/*************************************************************************
6+
*********************** H E A D E R S ***********************************
7+
*************************************************************************/
8+
9+
typedef bit<9> egressSpec_t;
10+
typedef bit<48> macAddr_t;
11+
12+
header ethernet_t {
13+
macAddr_t dstAddr;
14+
macAddr_t srcAddr;
15+
bit<16> etherType;
16+
}
17+
18+
struct metadata {
19+
/* empty */
20+
}
21+
22+
struct headers {
23+
ethernet_t ethernet;
24+
}
25+
26+
/*************************************************************************
27+
*********************** P A R S E R ***********************************
28+
*************************************************************************/
29+
30+
parser MyParser(packet_in packet,
31+
out headers hdr,
32+
inout metadata meta,
33+
inout standard_metadata_t standard_metadata) {
34+
35+
state start {
36+
packet.extract(hdr.ethernet);
37+
transition accept;
38+
}
39+
40+
}
41+
42+
43+
/*************************************************************************
44+
************ C H E C K S U M V E R I F I C A T I O N *************
45+
*************************************************************************/
46+
47+
control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
48+
apply { }
49+
}
50+
51+
52+
/*************************************************************************
53+
************** I N G R E S S P R O C E S S I N G *******************
54+
*************************************************************************/
55+
56+
control MyIngress(inout headers hdr,
57+
inout metadata meta,
58+
inout standard_metadata_t standard_metadata) {
59+
60+
action swap_mac_addresses() {
61+
macAddr_t tmp_mac;
62+
tmp_mac = hdr.ethernet.dstAddr;
63+
hdr.ethernet.dstAddr = hdr.ethernet.srcAddr;
64+
hdr.ethernet.srcAddr = tmp_mac;
65+
66+
//send it back to the same port
67+
standard_metadata.egress_spec = standard_metadata.ingress_port;
68+
}
69+
70+
action drop() {
71+
mark_to_drop(standard_metadata);
72+
}
73+
74+
table src_mac_drop {
75+
key = {
76+
hdr.ethernet.srcAddr: exact;
77+
}
78+
actions = {
79+
swap_mac_addresses;
80+
drop;
81+
NoAction;
82+
}
83+
size = 1024;
84+
default_action = swap_mac_addresses();
85+
}
86+
87+
apply {
88+
if (hdr.ethernet.isValid()) {
89+
src_mac_drop.apply();
90+
}
91+
}
92+
}
93+
94+
95+
96+
97+
98+
/*************************************************************************
99+
**************** E G R E S S P R O C E S S I N G *******************
100+
*************************************************************************/
101+
102+
control MyEgress(inout headers hdr,
103+
inout metadata meta,
104+
inout standard_metadata_t standard_metadata) {
105+
apply { }
106+
}
107+
108+
/*************************************************************************
109+
************* C H E C K S U M C O M P U T A T I O N **************
110+
*************************************************************************/
111+
112+
control MyComputeChecksum(inout headers hdr, inout metadata meta) {
113+
apply {
114+
115+
}
116+
}
117+
118+
119+
/*************************************************************************
120+
*********************** D E P A R S E R *******************************
121+
*************************************************************************/
122+
123+
control MyDeparser(packet_out packet, in headers hdr) {
124+
apply {
125+
packet.emit(hdr.ethernet);
126+
}
127+
}
128+
129+
/*************************************************************************
130+
*********************** S W I T C H *******************************
131+
*************************************************************************/
132+
133+
V1Switch(
134+
MyParser(),
135+
MyVerifyChecksum(),
136+
MyIngress(),
137+
MyEgress(),
138+
MyComputeChecksum(),
139+
MyDeparser()
140+
) main;

assignment3/reflector_class.p4

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* -*- P4_16 -*- */
2+
#include <core.p4>
3+
#include <v1model.p4>
4+
5+
/*************************************************************************
6+
*********************** H E A D E R S ***********************************
7+
*************************************************************************/
8+
9+
typedef bit<9> egressSpec_t;
10+
typedef bit<48> macAddr_t;
11+
12+
header ethernet_t {
13+
//TODO: define the following header fields
14+
//macAddr_t type destination address
15+
//macAddr_t type source address
16+
/16 bit etherType
17+
}
18+
19+
struct metadata {
20+
/* empty */
21+
}
22+
23+
struct headers {
24+
//TODO: define a header ethernet of type ethernet_t
25+
}
26+
27+
/*************************************************************************
28+
*********************** P A R S E R ***********************************
29+
*************************************************************************/
30+
31+
parser MyParser(packet_in packet,
32+
out headers hdr,
33+
inout metadata meta,
34+
inout standard_metadata_t standard_metadata) {
35+
36+
state start {
37+
//TODO: define a state that extracts the ethernet header
38+
//and transitions to accept
39+
}
40+
41+
}
42+
43+
44+
/*************************************************************************
45+
************ C H E C K S U M V E R I F I C A T I O N *************
46+
*************************************************************************/
47+
48+
control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
49+
apply { }
50+
}
51+
52+
53+
/*************************************************************************
54+
************** I N G R E S S P R O C E S S I N G *******************
55+
*************************************************************************/
56+
57+
control MyIngress(inout headers hdr,
58+
inout metadata meta,
59+
inout standard_metadata_t standard_metadata) {
60+
61+
action swap_mac_addresses() {
62+
macAddr_t tmp_mac;
63+
//TODO: swap source and destination MAC addresses
64+
//use the defined temp variable tmp_mac
65+
66+
//TODO: send the packet back to the same port
67+
}
68+
69+
action drop() {
70+
mark_to_drop(standard_metadata);
71+
}
72+
73+
table src_mac_drop {
74+
key = {
75+
//TODO: define an exact match key using the source MAC address
76+
}
77+
actions = {
78+
//TODO: define 3 actions: swap_mac_addresses, drop, NoAction.
79+
}
80+
//TODO: define a table size of 1024 entries
81+
82+
//TODO: define the default action to return the packet to the source
83+
}
84+
85+
apply {
86+
//TODO: Check if the Ethernet header is valid
87+
//if so, lookup the source MAC in the table and decide what to do
88+
}
89+
}
90+
}
91+
92+
93+
94+
95+
96+
/*************************************************************************
97+
**************** E G R E S S P R O C E S S I N G *******************
98+
*************************************************************************/
99+
100+
control MyEgress(inout headers hdr,
101+
inout metadata meta,
102+
inout standard_metadata_t standard_metadata) {
103+
apply { }
104+
}
105+
106+
/*************************************************************************
107+
************* C H E C K S U M C O M P U T A T I O N **************
108+
*************************************************************************/
109+
110+
control MyComputeChecksum(inout headers hdr, inout metadata meta) {
111+
apply {
112+
113+
}
114+
}
115+
116+
117+
/*************************************************************************
118+
*********************** D E P A R S E R *******************************
119+
*************************************************************************/
120+
121+
control MyDeparser(packet_out packet, in headers hdr) {
122+
apply {
123+
//TODO: emit the packet with a valid Ethernet header
124+
}
125+
}
126+
127+
/*************************************************************************
128+
*********************** S W I T C H *******************************
129+
*************************************************************************/
130+
131+
V1Switch(
132+
MyParser(),
133+
MyVerifyChecksum(),
134+
MyIngress(),
135+
MyEgress(),
136+
MyComputeChecksum(),
137+
MyDeparser()
138+
) main;

0 commit comments

Comments
 (0)