-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_plane.py
More file actions
134 lines (108 loc) · 4.74 KB
/
Copy pathcontrol_plane.py
File metadata and controls
134 lines (108 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Name: Junyong Zhao
# PennKey: junyong
import argparse
import json
import os
import sys
import threading
from time import sleep
sys.path.append("utils")
import bmv2
import helper
from convert import *
# User control plane code goes here
def RunControlPlane(sw, id_num, p4info_helper):
# TODO: finish this function to properly configure the switch and to learn
# new MAC addresses as they arrive. A correct implementation will not
# use id_num.
# add multicast ports for every switch
mac_adds = set()
mcasts_entry = p4info_helper.buildMulticastEntry(1, [2, 3])
sw.AddMulticastGroup(mcasts_entry)
mcasts_entry = p4info_helper.buildMulticastEntry(2, [1, 3])
sw.AddMulticastGroup(mcasts_entry)
mcasts_entry = p4info_helper.buildMulticastEntry(3, [1, 2])
sw.AddMulticastGroup(mcasts_entry)
digest_request = p4info_helper.buildDigestConfig("ethlearn_digest_t")
# listen to digest reporting
while 1:
response = sw.GetDigest(digest_request)
mac = decodeMac(response.digest.data[0].struct.members[0].bitstring)
port = decodeNum(response.digest.data[0].struct.members[1].bitstring)
# build for tiLearnMAC table
tilearn_entry = p4info_helper.buildTableEntry(
table_name="cis553Ingress.tiLearnMAC",
match_fields={"hdr.ethernet.srcAddr": mac},
action_name="cis553Ingress.NoAct",
action_params={})
# build for tiForward table
tifwd_entry = p4info_helper.buildTableEntry(
table_name="cis553Ingress.tiForward",
match_fields={"hdr.ethernet.dstAddr": mac},
action_name="cis553Ingress.aiForward",
action_params={"egress_port": port})
# build for tiFilter table
tiflt_entry = p4info_helper.buildTableEntry(
table_name="cis553Ingress.tiFilter",
match_fields={"hdr.ethernet.dstAddr": mac,
"standard_metadata.ingress_port": port},
action_name="cis553Ingress.drop",
action_params={})
# update tables
if mac not in mac_adds:
sw.WriteTableEntry(tilearn_entry)
sw.WriteTableEntry(tifwd_entry)
sw.WriteTableEntry(tiflt_entry)
else:
sw.UpdateTableEntry(tilearn_entry)
sw.UpdateTableEntry(tifwd_entry)
sw.UpdateTableEntry(tiflt_entry)
mac_adds.add(mac)
sw.shutdown()
# Starts a control plane for each switch. Hardcoded for our Mininet topology.
def ConfigureNetwork(p4info_file="build/data_plane.p4info",
bmv2_json="build/data_plane.json"):
p4info_helper = helper.P4InfoHelper(p4info_file)
threads = []
print "Connecting to P4Runtime server on s1..."
sw1 = bmv2.Bmv2SwitchConnection('s1', "127.0.0.1:50051", 0)
sw1.MasterArbitrationUpdate()
sw1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info,
bmv2_json_file_path=bmv2_json)
t = threading.Thread(target=RunControlPlane, args=(sw1, 1, p4info_helper))
t.start()
threads.append(t)
print "Connecting to P4Runtime server on s2..."
sw2 = bmv2.Bmv2SwitchConnection('s2', "127.0.0.1:50052", 1)
sw2.MasterArbitrationUpdate()
sw2.SetForwardingPipelineConfig(p4info=p4info_helper.p4info,
bmv2_json_file_path=bmv2_json)
t = threading.Thread(target=RunControlPlane, args=(sw2, 2, p4info_helper))
t.start()
threads.append(t)
print "Connecting to P4Runtime server on s3..."
sw3 = bmv2.Bmv2SwitchConnection('s3', "127.0.0.1:50053", 2)
sw3.MasterArbitrationUpdate()
sw3.SetForwardingPipelineConfig(p4info=p4info_helper.p4info,
bmv2_json_file_path=bmv2_json)
t = threading.Thread(target=RunControlPlane, args=(sw3, 3, p4info_helper))
t.start()
threads.append(t)
for t in threads:
t.join()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='CIS553 P4Runtime Controller')
parser.add_argument("-b", '--bmv2-json',
help="path to BMv2 switch description (json)",
type=str, action="store",
default="build/data_plane.json")
parser.add_argument("-c", '--p4info-file',
help="path to P4Runtime protobuf description (text)",
type=str, action="store",
default="build/data_plane.p4info")
args = parser.parse_args()
if not os.path.exists(args.p4info_file):
parser.error("File %s does not exist!" % args.p4info_file)
if not os.path.exists(args.bmv2_json):
parser.error("File %s does not exist!" % args.bmv2_json)
ConfigureNetwork(args.p4info_file, args.bmv2_json)