forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendToEndTest.py
More file actions
executable file
·375 lines (304 loc) · 12.3 KB
/
Copy pathendToEndTest.py
File metadata and controls
executable file
·375 lines (304 loc) · 12.3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python3
# Copyright (c) Barefoot Networks, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
# Testing example for P4->EBPF compiler
#
# This program exercises the simple.c EBPF program
# generated from the simple.p4 source file.
import subprocess
import ctypes
import time
import sys
import os
from bcc import BPF
from pyroute2 import IPRoute, NSPopen, NetNS
from netaddr import IPAddress
### This part is a simple generic network simulaton toolkit
class Base(object):
def __init__(self):
self.verbose = True
def message(self, *args):
if self.verbose:
print(*args)
class Endpoint(Base):
# a network interface really
def __init__(self, ipaddress, ethaddress):
Base.__init__(self)
self.mac_addr = ethaddress
self.ipaddress = ipaddress
self.prefixlen = 24
self.parent = None
def __str__(self):
return "Endpoint " + str(self.ipaddress)
def set_parent(self, parent):
assert isinstance(parent, Node)
self.parent = parent
def get_ip_address(self):
return IPAddress(self.ipaddress)
class Node(Base):
# Used to represent one of clt, sw, srv
# Each lives in its own namespace
def __init__(self, name):
Base.__init__(self)
self.name = name
self.endpoints = []
self.get_ns() # as a side-effect creates namespace
def add_endpoint(self, endpoint):
assert isinstance(endpoint, Endpoint)
self.endpoints.append(endpoint)
endpoint.set_parent(self)
def __str__(self):
return "Node " + self.name
def get_ns_name(self):
return self.name
def get_ns(self):
nsname = self.get_ns_name()
ns = NetNS(nsname)
return ns
def remove(self):
ns = self.get_ns();
ns.close()
ns.remove()
def execute(self, command):
# Run a command in the node's namespace
# Return the command's exit code
self.message(self.name, "Executing", command)
nsn = self.get_ns_name()
pipe = NSPopen(nsn, command)
result = pipe.wait()
pipe.release()
return result
def set_arp(self, destination):
assert isinstance(destination, Endpoint)
command = ["arp", "-s", str(destination.ipaddress),
str(destination.mac_addr)]
self.execute(command)
class NetworkBase(Base):
def __init__(self):
Base.__init__(self)
self.ipr = IPRoute()
self.nodes = []
def add_node(self, node):
assert isinstance(node, Node)
self.nodes.append(node)
def get_interface_name(self, source, dest):
assert isinstance(source, Node)
assert isinstance(dest, Node)
interface_name = "veth-" + source.name + "-" + dest.name
return interface_name
def get_interface(self, ifname):
interfaces = self.ipr.link_lookup(ifname=ifname)
if len(interfaces) != 1:
raise Exception("Could not identify interface " + ifname)
ix = interfaces[0]
assert isinstance(ix, int)
return ix
def set_interface_ipaddress(self, node, ifname, address, mask):
# Ask a node to set the specified interface address
if address is None:
return
assert isinstance(node, Node)
command = ["ip", "addr", "add", str(address) + "/" + str(mask),
"dev", str(ifname)]
result = node.execute(command)
assert(result == 0)
def create_link(self, src, dest):
assert isinstance(src, Endpoint)
assert isinstance(dest, Endpoint)
ifname = self.get_interface_name(src.parent, dest.parent)
destname = self.get_interface_name(dest.parent, src.parent)
self.ipr.link_create(ifname=ifname, kind="veth", peer=destname)
self.message("Create", ifname, "link")
# Set source endpoint information
ix = self.get_interface(ifname)
self.ipr.link("set", index=ix, address=src.mac_addr)
# push source endpoint into source namespace
self.ipr.link("set", index=ix,
net_ns_fd=src.parent.get_ns_name(), state="up")
# Set interface ip address; seems to be
# lost of set prior to moving to namespace
self.set_interface_ipaddress(
src.parent, ifname, src.ipaddress , src.prefixlen)
# Sef destination endpoint information
ix = self.get_interface(destname)
self.ipr.link("set", index=ix, address=dest.mac_addr)
# push destination endpoint into the destination namespace
self.ipr.link("set", index=ix,
net_ns_fd=dest.parent.get_ns_name(), state="up")
# Set interface ip address
self.set_interface_ipaddress(dest.parent, destname,
dest.ipaddress, dest.prefixlen)
def show_interfaces(self, node):
cmd = ["ip", "addr"]
if node is None:
# Run with no namespace
subprocess.call(cmd)
else:
# Run in node's namespace
assert isinstance(node, Node)
self.message("Enumerating all interfaces in ", node.name)
node.execute(cmd)
def delete(self):
self.message("Deleting virtual network")
for n in self.nodes:
n.remove()
self.ipr.close()
### Here begins the concrete instantiation of the network
# Network setup:
# Each of these is a separate namespace.
#
# 62:ce:1b:48:3e:61 a2:59:94:cf:51:09
# 96:a4:85:fe:2a:11 62:ce:1b:48:3e:60
# /------------------\ /-----------------\
# ---------- -------- ---------
# | clt | | sw | | srv |
# ---------- -------- ---------
# 10.0.0.11 10.0.0.10
#
class SimulatedNetwork(NetworkBase):
def __init__(self):
NetworkBase.__init__(self)
self.client = Node("clt")
self.add_node(self.client)
self.client_endpoint = Endpoint("10.0.0.11", "96:a4:85:fe:2a:11")
self.client.add_endpoint(self.client_endpoint)
self.server = Node("srv")
self.add_node(self.server)
self.server_endpoint = Endpoint("10.0.0.10", "a2:59:94:cf:51:09")
self.server.add_endpoint(self.server_endpoint)
self.switch = Node("sw")
self.add_node(self.switch)
self.sw_clt_endpoint = Endpoint(None, "62:ce:1b:48:3e:61")
self.sw_srv_endpoint = Endpoint(None, "62:ce:1b:48:3e:60")
self.switch.add_endpoint(self.sw_clt_endpoint)
self.switch.add_endpoint(self.sw_srv_endpoint)
def run_method_in_node(self, node, method, args):
# run a method of the SimulatedNetwork class in a different namespace
# return the exit code
assert isinstance(node, Node)
assert isinstance(args, list)
torun = __file__
args.insert(0, torun)
args.insert(1, method)
return node.execute(args) # runs the command argv[0] method args
def instantiate(self):
# Creates the various namespaces
self.message("Creating virtual network")
self.message("Create client-switch link")
self.create_link(self.client_endpoint, self.sw_clt_endpoint)
self.message("Create server-switch link")
self.create_link(self.server_endpoint, self.sw_srv_endpoint)
self.show_interfaces(self.client)
self.show_interfaces(self.server)
self.show_interfaces(self.switch)
self.message("Set ARP mappings")
self.client.set_arp(self.server_endpoint)
self.server.set_arp(self.client_endpoint)
def setup_switch(self):
# This method is run in the switch namespace.
self.message("Compiling and loading BPF program")
b = BPF(src_file="./simple.c", debug=0)
fn = b.load_func("ebpf_filter", BPF.SCHED_CLS)
self.message("BPF program loaded")
self.message("Discovering tables")
routing_tbl = b.get_table("routing")
routing_miss_tbl = b.get_table("ebpf_routing_miss")
cnt_tbl = b.get_table("cnt")
self.message("Hooking up BPF classifiers using TC")
interfname = self.get_interface_name(self.switch, self.server)
sw_srv_idx = self.get_interface(interfname)
self.ipr.tc("add", "ingress", sw_srv_idx, "ffff:")
self.ipr.tc("add-filter", "bpf", sw_srv_idx, ":1", fd=fn.fd,
name=fn.name, parent="ffff:", action="ok", classid=1)
interfname = self.get_interface_name(self.switch, self.client)
sw_clt_idx = self.get_interface(interfname)
self.ipr.tc("add", "ingress", sw_clt_idx, "ffff:")
self.ipr.tc("add-filter", "bpf", sw_clt_idx, ":1", fd=fn.fd,
name=fn.name, parent="ffff:", action="ok", classid=1)
self.message("Populating tables from the control plane")
cltip = self.client_endpoint.get_ip_address()
srvip = self.server_endpoint.get_ip_address()
# BCC does not support tbl.Leaf when the type contains a union,
# so we have to make up the value type manually. Unfortunately
# these sizes are not portable...
class Forward(ctypes.Structure):
_fields_ = [("port", ctypes.c_ushort)]
class Nop(ctypes.Structure):
_fields_ = []
class Union(ctypes.Union):
_fields_ = [("nop", Nop),
("forward", Forward)]
class Value(ctypes.Structure):
_fields_ = [("action", ctypes.c_uint),
("u", Union)]
if False:
# This is how it should ideally be done, but it does not work
routing_tbl[routing_tbl.Key(int(cltip))] = routing_tbl.Leaf(
1, sw_clt_idx)
routing_tbl[routing_tbl.Key(int(srvip))] = routing_tbl.Leaf(
1, sw_srv_idx)
else:
v1 = Value()
v1.action = 1
v1.u.forward.port = sw_clt_idx
v2 = Value()
v2.action = 1;
v2.u.forward.port = sw_srv_idx
routing_tbl[routing_tbl.Key(int(cltip))] = v1
routing_tbl[routing_tbl.Key(int(srvip))] = v2
self.message("Dumping table contents")
for key, leaf in routing_tbl.items():
self.message(str(IPAddress(key.key_field_0)),
leaf.action, leaf.u.forward.port)
def run(self):
self.message("Pinging server from client")
ping = ["ping", self.server_endpoint.ipaddress, "-c", "2"]
result = self.client.execute(ping)
if result != 0:
raise Exception("Test failed")
else:
print("Test succeeded!")
def prepare_switch(self):
self.message("Configuring switch")
# Re-invokes this script in the switch namespace;
# this causes the setup_switch method to be run in that context.
# This is the same as running self.setup_switch()
# but in the switch namespace
self.run_method_in_node(self.switch, "setup_switch", [])
def compile(source, destination):
try:
status = subprocess.call(
"../compiler/p4toEbpf.py " + source + " -o " + destination,
shell=True)
if status < 0:
print("Child was terminated by signal", -status, file=sys.stderr)
else:
print("Child returned", status, file=sys.stderr)
except OSError as e:
print("Execution failed:", e, file=sys.stderr)
raise e
def start_simulation():
compile("testprograms/simple.p4", "simple.c")
network = SimulatedNetwork()
network.instantiate()
network.prepare_switch()
network.run()
network.delete()
os.remove("simple.c")
def main(argv):
print(str(argv))
if len(argv) == 1:
# Main entry point: start simulation
start_simulation()
else:
# We are invoked with some arguments (probably in a different namespace)
# First argument is a method name, rest are method arguments.
# Create a SimulatedNetwork and invoke the specified method with the
# specified arguments.
network = SimulatedNetwork()
methodname = argv[1]
arguments = argv[2:]
method = getattr(network, methodname)
method(*arguments)
if __name__ == '__main__':
main(sys.argv)