blob: aa9888dfac7f1cb5f3d36d5fe9a92a0ccf1ca847 [file] [log] [blame]
Yonghong Song1bd744d2015-08-10 17:33:24 -07001import os
Yonghong Song485af952015-07-10 10:12:47 -07002import subprocess
Yonghong Song8cb8e962015-08-10 11:35:28 -07003import pyroute2
Brenden Blanco085379b2015-06-18 00:28:47 -07004from pyroute2 import IPRoute, NetNS, IPDB, NSPopen
5
6class Simulation(object):
7 """
8 Helper class for controlling multiple namespaces. Inherit from
9 this class and setup your namespaces.
10 """
11
12 def __init__(self, ipdb):
13 self.ipdb = ipdb
14 self.ipdbs = {}
15 self.namespaces = []
16 self.processes = []
17 self.released = False
18
Yonghong Song485af952015-07-10 10:12:47 -070019 # helper function to add additional ifc to namespace
Yonghong Songa51ec2f2015-07-10 22:58:59 -070020 # if called directly outside Simulation class, "ifc_base_name" should be
21 # different from "name", the "ifc_base_name" and "name" are the same for
22 # the first ifc created by namespace
Brenden Blancoa3ef55b2015-07-13 21:10:39 -070023 def _ns_add_ifc(self, name, ns_ifc, ifc_base_name=None, in_ifc=None,
24 out_ifc=None, ipaddr=None, macaddr=None, fn=None, cmd=None,
25 action="ok", disable_ipv6=False):
Yonghong Song485af952015-07-10 10:12:47 -070026 if name in self.ipdbs:
27 ns_ipdb = self.ipdbs[name]
28 else:
Yonghong Songc90bece2015-08-04 11:10:31 -070029 try:
30 nl=NetNS(name)
31 self.namespaces.append(nl)
32 except KeyboardInterrupt:
33 # remove the namespace if it has been created
34 pyroute2.netns.remove(name)
35 raise
36 ns_ipdb = IPDB(nl)
37 self.ipdbs[nl.netns] = ns_ipdb
Brenden Blancoa3ef55b2015-07-13 21:10:39 -070038 if disable_ipv6:
Yonghong Song8cb8e962015-08-10 11:35:28 -070039 cmd1 = ["sysctl", "-q", "-w",
Brenden Blanco9d2f33a2015-07-13 22:44:16 -070040 "net.ipv6.conf.default.disable_ipv6=1"]
Yonghong Song8cb8e962015-08-10 11:35:28 -070041 nsp = NSPopen(ns_ipdb.nl.netns, cmd1)
Brenden Blancoa3ef55b2015-07-13 21:10:39 -070042 nsp.wait(); nsp.release()
43 ns_ipdb.interfaces.lo.up().commit()
Brenden Blanco085379b2015-06-18 00:28:47 -070044 if in_ifc:
45 in_ifname = in_ifc.ifname
Yonghong Songc90bece2015-08-04 11:10:31 -070046 with in_ifc as v:
47 # move half of veth into namespace
48 v.net_ns_fd = ns_ipdb.nl.netns
Brenden Blanco085379b2015-06-18 00:28:47 -070049 else:
Yonghong Song1bd744d2015-08-10 17:33:24 -070050 # delete the potentially leaf-over veth interfaces
Yonghong Song83f800f2015-08-10 19:04:58 -070051 ipr = IPRoute()
52 for i in ipr.link_lookup(ifname='%sa' % ifc_base_name): ipr.link_remove(i)
Yonghong Songdf73a822015-08-12 08:21:57 -070053 ipr.close()
Yonghong Songc90bece2015-08-04 11:10:31 -070054 try:
55 out_ifc = self.ipdb.create(ifname="%sa" % ifc_base_name, kind="veth",
56 peer="%sb" % ifc_base_name).commit()
57 in_ifc = self.ipdb.interfaces[out_ifc.peer]
58 in_ifname = in_ifc.ifname
59 with in_ifc as v:
60 v.net_ns_fd = ns_ipdb.nl.netns
61 except KeyboardInterrupt:
62 # explicitly remove the interface
63 out_ifname = "%sa" % ifc_base_name
64 if out_ifname in self.ipdb.interfaces: self.ipdb.interfaces[out_ifname].remove().commit()
65 raise
66
Brenden Blanco085379b2015-06-18 00:28:47 -070067 if out_ifc: out_ifc.up().commit()
Brenden Blancofd4e4a52015-07-13 20:57:34 -070068 ns_ipdb.interfaces.lo.up().commit()
Yonghong Songc90bece2015-08-04 11:10:31 -070069 in_ifc = ns_ipdb.interfaces[in_ifname]
Brenden Blanco085379b2015-06-18 00:28:47 -070070 with in_ifc as v:
Yonghong Song485af952015-07-10 10:12:47 -070071 v.ifname = ns_ifc
Brenden Blanco085379b2015-06-18 00:28:47 -070072 if ipaddr: v.add_ip("%s" % ipaddr)
73 if macaddr: v.address = macaddr
74 v.up()
Brenden Blanco9d2f33a2015-07-13 22:44:16 -070075 if disable_ipv6:
Yonghong Song8cb8e962015-08-10 11:35:28 -070076 cmd1 = ["sysctl", "-q", "-w",
Brenden Blanco9d2f33a2015-07-13 22:44:16 -070077 "net.ipv6.conf.%s.disable_ipv6=1" % out_ifc.ifname]
Yonghong Song8cb8e962015-08-10 11:35:28 -070078 subprocess.call(cmd1)
Brenden Blanco085379b2015-06-18 00:28:47 -070079 if fn and out_ifc:
80 self.ipdb.nl.tc("add", "ingress", out_ifc["index"], "ffff:")
81 self.ipdb.nl.tc("add-filter", "bpf", out_ifc["index"], ":1",
82 fd=fn.fd, name=fn.name, parent="ffff:",
83 action=action, classid=1)
Brenden Blanco085379b2015-06-18 00:28:47 -070084 if cmd:
85 self.processes.append(NSPopen(ns_ipdb.nl.netns, cmd))
86 return (ns_ipdb, out_ifc, in_ifc)
87
Yonghong Song485af952015-07-10 10:12:47 -070088 # helper function to create a namespace and a veth connecting it
89 def _create_ns(self, name, in_ifc=None, out_ifc=None, ipaddr=None,
90 macaddr=None, fn=None, cmd=None, action="ok", disable_ipv6=False):
Yonghong Songa51ec2f2015-07-10 22:58:59 -070091 (ns_ipdb, out_ifc, in_ifc) = self._ns_add_ifc(name, "eth0", name, in_ifc, out_ifc,
92 ipaddr, macaddr, fn, cmd, action,
93 disable_ipv6)
Yonghong Song485af952015-07-10 10:12:47 -070094 return (ns_ipdb, out_ifc, in_ifc)
95
Brenden Blanco085379b2015-06-18 00:28:47 -070096 def release(self):
97 if self.released: return
98 self.released = True
99 for p in self.processes:
100 if p.released: continue
Brenden Blancofd4e4a52015-07-13 20:57:34 -0700101 try:
102 p.kill()
103 p.wait()
104 except:
105 pass
106 finally:
107 p.release()
Brenden Blanco085379b2015-06-18 00:28:47 -0700108 for name, db in self.ipdbs.items(): db.release()
109 for ns in self.namespaces: ns.remove()
110