blob: 8d13c43fb091e09cab5bf61f336abcc6f3b20b96 [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Brenden Blanco2cea0cd2015-06-16 13:50:16 -07002# Copyright (c) PLUMgrid, Inc.
3# Licensed under the Apache License, Version 2.0 (the "License")
4
Brenden Blancoc35989d2015-09-02 18:04:07 -07005from bcc import BPF
Brenden Blanco2cea0cd2015-06-16 13:50:16 -07006from pyroute2 import IPRoute, NetNS, IPDB, NSPopen
Brenden Blanco085379b2015-06-18 00:28:47 -07007from simulation import Simulation
Brenden Blanco2cea0cd2015-06-16 13:50:16 -07008import sys
9from time import sleep
Yonghong Songc9573672015-06-23 12:36:56 -070010from builtins import input
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070011
12ipr = IPRoute()
13ipdb = IPDB(nl=ipr)
14b = BPF(src_file="tc_neighbor_sharing.c", debug=0)
15
16wan_fn = b.load_func("classify_wan", BPF.SCHED_CLS)
17pass_fn = b.load_func("pass", BPF.SCHED_CLS)
18neighbor_fn = b.load_func("classify_neighbor", BPF.SCHED_CLS)
19
20num_neighbors = 3
21num_locals = 2
22
23# class to build the simulation network
Brenden Blanco085379b2015-06-18 00:28:47 -070024class SharedNetSimulation(Simulation):
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070025
Brenden Blanco085379b2015-06-18 00:28:47 -070026 def __init__(self, ipdb):
27 super(SharedNetSimulation, self).__init__(ipdb)
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070028
29 # Create the wan namespace, and attach an ingress filter for throttling
30 # inbound (download) traffic
Brenden Blanco085379b2015-06-18 00:28:47 -070031 wan_if = self._create_ns("wan0", ipaddr="172.16.1.5/24")[1]
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070032 ipr.tc("add", "ingress", wan_if["index"], "ffff:")
33 ipr.tc("add-filter", "bpf", wan_if["index"], ":1", fd=wan_fn.fd,
34 prio=1, name=wan_fn.name, parent="ffff:", action="drop",
35 classid=1, rate="128kbit", burst=1024 * 32, mtu=16 * 1024)
36 ipr.tc("add-filter", "bpf", wan_if["index"], ":2", fd=pass_fn.fd,
37 prio=2, name=pass_fn.name, parent="ffff:", action="drop",
38 classid=2, rate="1024kbit", burst=1024 * 32, mtu=16 * 1024)
39 self.wan_if = wan_if
40
Brenden Blanco085379b2015-06-18 00:28:47 -070041 # start the namespaces that compose the network, interconnect them with the
42 # bridge, and attach the tc filters
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070043 def start(self):
44 neighbor_list = []
45 local_list = []
Brenden Blanco085379b2015-06-18 00:28:47 -070046 cmd = ["netserver", "-D"]
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070047 for i in range(0, num_neighbors):
Brenden Blanco085379b2015-06-18 00:28:47 -070048 ipaddr = "172.16.1.%d/24" % (i + 100)
49 ret = self._create_ns("neighbor%d" % i, ipaddr=ipaddr,
50 fn=neighbor_fn, cmd=cmd)
51 neighbor_list.append(ret)
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070052 for i in range(0, num_locals):
Brenden Blanco085379b2015-06-18 00:28:47 -070053 ipaddr = "172.16.1.%d/24" % (i + 150)
54 ret = self._create_ns("local%d" % i, ipaddr=ipaddr,
55 fn=pass_fn, cmd=cmd)
56 local_list.append(ret)
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070057
58 with ipdb.create(ifname="br100", kind="bridge") as br100:
59 for x in neighbor_list:
60 br100.add_port(x[1])
61 for x in local_list:
62 br100.add_port(x[1])
63 br100.add_port(self.wan_if)
64 br100.up()
65
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070066try:
Brenden Blanco085379b2015-06-18 00:28:47 -070067 sim = SharedNetSimulation(ipdb)
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070068 sim.start()
Brenden Blancoc630f622015-06-16 17:18:58 -070069 print("Network ready. Create a shell in the wan0 namespace and test with netperf")
70 print(" (Neighbors are 172.16.1.100-%d, and LAN clients are 172.16.1.150-%d)"
71 % (100 + num_neighbors - 1, 150 + num_locals - 1))
72 print(" e.g.: ip netns exec wan0 netperf -H 172.16.1.100 -l 2")
Yonghong Songc9573672015-06-23 12:36:56 -070073 input("Press enter when finished: ")
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070074finally:
75 if "sim" in locals(): sim.release()
76 if "br100" in ipdb.interfaces: ipdb.interfaces.br100.remove().commit()
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070077 ipdb.release()
78
79