Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) PLUMgrid, Inc. |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 4 | |
Brenden Blanco | c35989d | 2015-09-02 18:04:07 -0700 | [diff] [blame] | 5 | from bcc import BPF |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 6 | from pyroute2 import IPRoute, NetNS, IPDB, NSPopen |
Brenden Blanco | 085379b | 2015-06-18 00:28:47 -0700 | [diff] [blame] | 7 | from simulation import Simulation |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 8 | import sys |
| 9 | from time import sleep |
Yonghong Song | c957367 | 2015-06-23 12:36:56 -0700 | [diff] [blame] | 10 | from builtins import input |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 11 | |
| 12 | ipr = IPRoute() |
| 13 | ipdb = IPDB(nl=ipr) |
| 14 | b = BPF(src_file="tc_neighbor_sharing.c", debug=0) |
| 15 | |
| 16 | wan_fn = b.load_func("classify_wan", BPF.SCHED_CLS) |
| 17 | pass_fn = b.load_func("pass", BPF.SCHED_CLS) |
| 18 | neighbor_fn = b.load_func("classify_neighbor", BPF.SCHED_CLS) |
| 19 | |
| 20 | num_neighbors = 3 |
| 21 | num_locals = 2 |
| 22 | |
| 23 | # class to build the simulation network |
Brenden Blanco | 085379b | 2015-06-18 00:28:47 -0700 | [diff] [blame] | 24 | class SharedNetSimulation(Simulation): |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 25 | |
Brenden Blanco | 085379b | 2015-06-18 00:28:47 -0700 | [diff] [blame] | 26 | def __init__(self, ipdb): |
| 27 | super(SharedNetSimulation, self).__init__(ipdb) |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 28 | |
| 29 | # Create the wan namespace, and attach an ingress filter for throttling |
| 30 | # inbound (download) traffic |
Brenden Blanco | 085379b | 2015-06-18 00:28:47 -0700 | [diff] [blame] | 31 | wan_if = self._create_ns("wan0", ipaddr="172.16.1.5/24")[1] |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 32 | 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 Blanco | 085379b | 2015-06-18 00:28:47 -0700 | [diff] [blame] | 41 | # start the namespaces that compose the network, interconnect them with the |
| 42 | # bridge, and attach the tc filters |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 43 | def start(self): |
| 44 | neighbor_list = [] |
| 45 | local_list = [] |
Brenden Blanco | 085379b | 2015-06-18 00:28:47 -0700 | [diff] [blame] | 46 | cmd = ["netserver", "-D"] |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 47 | for i in range(0, num_neighbors): |
Brenden Blanco | 085379b | 2015-06-18 00:28:47 -0700 | [diff] [blame] | 48 | 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 Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 52 | for i in range(0, num_locals): |
Brenden Blanco | 085379b | 2015-06-18 00:28:47 -0700 | [diff] [blame] | 53 | 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 Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 57 | |
| 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 Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 66 | try: |
Brenden Blanco | 085379b | 2015-06-18 00:28:47 -0700 | [diff] [blame] | 67 | sim = SharedNetSimulation(ipdb) |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 68 | sim.start() |
Brenden Blanco | c630f62 | 2015-06-16 17:18:58 -0700 | [diff] [blame] | 69 | 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 Song | c957367 | 2015-06-23 12:36:56 -0700 | [diff] [blame] | 73 | input("Press enter when finished: ") |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 74 | finally: |
| 75 | if "sim" in locals(): sim.release() |
| 76 | if "br100" in ipdb.interfaces: ipdb.interfaces.br100.remove().commit() |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 77 | ipdb.release() |
| 78 | |
| 79 | |