blob: 2c6a0f3d69807eae9ff3e358666a6f1bdee02fc3 [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()
Brenden Blanco97a0cac2017-05-18 09:57:42 -070069 ns_ipdb.initdb()
Yonghong Songc90bece2015-08-04 11:10:31 -070070 in_ifc = ns_ipdb.interfaces[in_ifname]
Brenden Blanco085379b2015-06-18 00:28:47 -070071 with in_ifc as v:
Yonghong Song485af952015-07-10 10:12:47 -070072 v.ifname = ns_ifc
Brenden Blanco085379b2015-06-18 00:28:47 -070073 if ipaddr: v.add_ip("%s" % ipaddr)
74 if macaddr: v.address = macaddr
75 v.up()
Brenden Blanco9d2f33a2015-07-13 22:44:16 -070076 if disable_ipv6:
Yonghong Song8cb8e962015-08-10 11:35:28 -070077 cmd1 = ["sysctl", "-q", "-w",
Brenden Blanco9d2f33a2015-07-13 22:44:16 -070078 "net.ipv6.conf.%s.disable_ipv6=1" % out_ifc.ifname]
Yonghong Song8cb8e962015-08-10 11:35:28 -070079 subprocess.call(cmd1)
Brenden Blanco085379b2015-06-18 00:28:47 -070080 if fn and out_ifc:
81 self.ipdb.nl.tc("add", "ingress", out_ifc["index"], "ffff:")
82 self.ipdb.nl.tc("add-filter", "bpf", out_ifc["index"], ":1",
83 fd=fn.fd, name=fn.name, parent="ffff:",
84 action=action, classid=1)
Brenden Blanco085379b2015-06-18 00:28:47 -070085 if cmd:
86 self.processes.append(NSPopen(ns_ipdb.nl.netns, cmd))
87 return (ns_ipdb, out_ifc, in_ifc)
88
Yonghong Song485af952015-07-10 10:12:47 -070089 # helper function to create a namespace and a veth connecting it
90 def _create_ns(self, name, in_ifc=None, out_ifc=None, ipaddr=None,
91 macaddr=None, fn=None, cmd=None, action="ok", disable_ipv6=False):
Yonghong Songa51ec2f2015-07-10 22:58:59 -070092 (ns_ipdb, out_ifc, in_ifc) = self._ns_add_ifc(name, "eth0", name, in_ifc, out_ifc,
93 ipaddr, macaddr, fn, cmd, action,
94 disable_ipv6)
Yonghong Song485af952015-07-10 10:12:47 -070095 return (ns_ipdb, out_ifc, in_ifc)
96
Brenden Blanco085379b2015-06-18 00:28:47 -070097 def release(self):
98 if self.released: return
99 self.released = True
100 for p in self.processes:
101 if p.released: continue
Brenden Blancofd4e4a52015-07-13 20:57:34 -0700102 try:
103 p.kill()
104 p.wait()
105 except:
106 pass
107 finally:
108 p.release()
Brenden Blanco085379b2015-06-18 00:28:47 -0700109 for name, db in self.ipdbs.items(): db.release()
110 for ns in self.namespaces: ns.remove()
111