-
Notifications
You must be signed in to change notification settings - Fork 3
/
controller.py
155 lines (135 loc) · 5.57 KB
/
controller.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python2
# Copyright 2019 Belma Turkovic
# TU Delft Embedded and Networked Systems Group.
# NOTICE: THIS FILE IS BASED ON https://github.com/p4lang/tutorials/tree/master/exercises/p4runtime, BUT WAS MODIFIED UNDER COMPLIANCE
# WITH THE APACHE 2.0 LICENCE FROM THE ORIGINAL WORK.
import argparse
import grpc
import os
import sys
from time import sleep
# Import P4Runtime lib from parent utils dir
# Probably there's a better way of doing this.
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "utils/"))
import bmv2
from switch import ShutdownAllSwitchConnections
from utils.convert import encodeNum
import helper
def writeIpv4Rules(p4info_helper, sw_id, dst_ip_addr, port):
table_entry = p4info_helper.buildTableEntry(
table_name="MyIngress.ipv4_lpm",
match_fields={"hdr.ipv4.dstAddr": (dst_ip_addr, 32)},
action_name="MyIngress.ipv4_forward",
action_params={"port": port},
)
sw_id.WriteTableEntry(table_entry)
print("Installed ingress forwarding rule on %s" % sw_id.name)
def readTableRules(p4info_helper, sw):
"""
Reads the table entries from all tables on the switch.
:param p4info_helper: the P4Info helper
:param sw: the switch connection
"""
print("\n----- Reading tables rules for %s -----" % sw.name)
for response in sw.ReadTableEntries():
for entity in response.entities:
entry = entity.table_entry
table_name = p4info_helper.get_tables_name(entry.table_id)
print("%s: " % table_name, end="")
for m in entry.match:
print(
p4info_helper.get_match_field_name(table_name, m.field_id), end=""
)
print("%r" % (p4info_helper.get_match_field_value(m),), end="")
action = entry.action.action
action_name = p4info_helper.get_actions_name(action.action_id)
print("-> action:%s with parameters:" % action_name, end="")
for p in action.params:
print(
" %s"
% p4info_helper.get_action_param_name(action_name, p.param_id),
end="",
)
print(" %r" % p.value, end="")
print("")
def printGrpcError(e):
print("gRPC Error:", e.details(), end="")
status_code = e.code()
print("(%s)" % status_code.name, end="")
traceback = sys.exc_info()[2]
print("[%s:%d]" % (traceback.tb_frame.f_code.co_filename, traceback.tb_lineno))
def main(p4info_file_path, bmv2_file_path):
# Instantiate a P4Runtime helper from the p4info file
p4info_helper = helper.P4InfoHelper(p4info_file_path)
try:
# Create a switch connection object for s1 and s2;
# this is backed by a P4Runtime gRPC connection.
# Also, dump all P4Runtime messages sent to switch to given txt files.
s1 = bmv2.Bmv2SwitchConnection(
name="s0",
address="0.0.0.0:50051",
device_id=1,
proto_dump_file="p4runtime.log",
)
# Send master arbitration update message to establish this controller as
# master (required by P4Runtime before performing any other write operation)
if s1.MasterArbitrationUpdate() == None:
print("Failed to establish the connection")
# Install the P4 program on the switches
s1.SetForwardingPipelineConfig(
p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path
)
print("Installed P4 Program using SetForwardingPipelineConfig on s1")
# Forward all packet to the controller (CPU_PORT 255)
writeIpv4Rules(p4info_helper, sw_id=s1, dst_ip_addr="10.10.10.1", port=255)
writeIpv4Rules(p4info_helper, sw_id=s1, dst_ip_addr="10.10.10.2", port=255)
writeIpv4Rules(p4info_helper, sw_id=s1, dst_ip_addr="10.10.3.3", port=255)
# read all table rules
readTableRules(p4info_helper, s1)
while True:
packetin = s1.PacketIn() # Packet in!
if packetin is not None:
print("PACKET IN received")
print(packetin)
packet = packetin.packet.payload
packetout = p4info_helper.buildPacketOut(
payload=packet, # send the packet in you received back to output port 3!
metadata={
1: encodeNum(3, 16),
}, # egress_spec (check @controller_header("packet_out") in the p4 code)
)
print("send PACKET OUT")
print(s1.PacketOut(packetout))
except KeyboardInterrupt:
print(" Shutting down.")
except grpc.RpcError as e:
printGrpcError(e)
ShutdownAllSwitchConnections()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="P4Runtime Controller")
parser.add_argument(
"--p4info",
help="p4info proto in text format from p4c",
type=str,
action="store",
required=False,
default="./firmeware.p4info.txt",
)
parser.add_argument(
"--bmv2-json",
help="BMv2 JSON file from p4c",
type=str,
action="store",
required=False,
default="./simple.json",
)
args = parser.parse_args()
if not os.path.exists(args.p4info):
parser.print_help()
print("\np4info file %s not found!" % args.p4info)
parser.exit(1)
if not os.path.exists(args.bmv2_json):
parser.print_help()
print("\nBMv2 JSON file %s not found!" % args.bmv2_json)
parser.exit(2)
main(args.p4info, args.bmv2_json)