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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# vim:ts=4:sw=4:et:ai:sts=4
import os, weakref
import netns.iproute
__all__ = ['NodeInterface', 'P2PInterface', 'ImportedInterface',
'ForeignNodeInterface', 'ImportedNodeInterface', 'Link']
class Interface(object):
"""Just a base class for the *Interface classes: assign names and handle
destruction."""
_nextid = 0
@staticmethod
def _gen_next_id():
n = Interface._nextid
Interface._nextid += 1
return n
@staticmethod
def _gen_if_name():
n = Interface._gen_next_id()
# Max 15 chars
return "NETNSif-%.4x%.3x" % (os.getpid(), n)
def __init__(self, index):
self._idx = index
def __del__(self):
self.destroy()
def destroy(self):
raise NotImplementedError
@property
def index(self):
"""Interface index as seen by the kernel."""
return self._idx
@property
def control(self):
"""Associated interface in the main name space (if it exists). Only
control interfaces can be put into a Link, for example."""
return None
class NSInterface(Interface):
"""Add user-facing methods for interfaces that go into a netns."""
def __init__(self, node, index):
super(NSInterface, self).__init__(index)
self._slave = node._slave
# Disable auto-configuration
# you wish: need to take into account the nonetns mode; plus not
# touching some pre-existing ifaces
#node.system([sysctl_path, '-w', 'net.ipv6.conf.%s.autoconf=0' %
#self.name])
node._add_interface(self)
# some black magic to automatically get/set interface attributes
def __getattr__(self, name):
iface = self._slave.get_if_data(self.index)
return getattr(iface, name)
def __setattr__(self, name, value):
if name[0] == '_': # forbid anything that doesn't start with a _
super(Interface, self).__setattr__(name, value)
return
iface = netns.iproute.interface(index = self.index)
setattr(iface, name, value)
return self._slave.set_if(iface)
def add_v4_address(self, address, prefix_len, broadcast = None):
addr = netns.iproute.ipv4address(address, prefix_len, broadcast)
self._slave.add_addr(self.index, addr)
def add_v6_address(self, address, prefix_len):
addr = netns.iproute.ipv6address(address, prefix_len)
self._slave.add_addr(self.index, addr)
def del_v4_address(self, address, prefix_len, broadcast = None):
addr = netns.iproute.ipv4address(address, prefix_len, broadcast)
self._slave.del_addr(self.index, addr)
def del_v6_address(self, address, prefix_len):
addr = netns.iproute.ipv6address(address, prefix_len)
self._slave.del_addr(self.index, addr)
def get_addresses(self):
addresses = self._slave.get_addr_data(self.index)
ret = []
for a in addresses:
if hasattr(a, 'broadcast'):
ret.append(dict(
address = a.address,
prefix_len = a.prefix_len,
broadcast = a.broadcast,
family = 'inet'))
else:
ret.append(dict(
address = a.address,
prefix_len = a.prefix_len,
family = 'inet6'))
return ret
class NodeInterface(NSInterface):
"""Class to create and handle a virtual interface inside a name space, it
can be connected to a Link object with emulation of link
characteristics."""
def __init__(self, node):
"""Create a new interface. `node' is the name space in which this
interface should be put."""
if1 = netns.iproute.interface(name = self._gen_if_name())
if2 = netns.iproute.interface(name = self._gen_if_name())
ctl, ns = netns.iproute.create_if_pair(if1, if2)
try:
netns.iproute.change_netns(ns, node.pid)
except:
netns.iproute.del_if(ctl)
# the other interface should go away automatically
raise
self._control = SlaveInterface(ctl.index)
super(NodeInterface, self).__init__(node, ns.index)
@property
def control(self):
return self._control
def destroy(self):
if self._slave:
if self.index in self._slave.get_if_data():
self._slave.del_if(self.index)
self._slave = None
class P2PInterface(NSInterface):
"""Class to create and handle point-to-point interfaces between name
spaces, without using Link objects. Those do not allow any kind of traffic
shaping.
As two interfaces need to be created, instead of using the class
constructor, use the P2PInterface.create_pair() static method."""
@staticmethod
def create_pair(node1, node2):
"""Create and return a pair of connected P2PInterface objects, assigned
to name spaces represented by `node1' and `node2'."""
if1 = netns.iproute.interface(name = P2PInterface._gen_if_name())
if2 = netns.iproute.interface(name = P2PInterface._gen_if_name())
pair = netns.iproute.create_if_pair(if1, if2)
try:
netns.iproute.change_netns(pair[0], node1.pid)
netns.iproute.change_netns(pair[1], node2.pid)
except:
netns.iproute.del_if(pair[0])
# the other interface should go away automatically
raise
o1 = P2PInterface.__new__(P2PInterface)
super(P2PInterface, o1).__init__(node1, pair[0].index)
o2 = P2PInterface.__new__(P2PInterface)
super(P2PInterface, o2).__init__(node2, pair[1].index)
return o1, o2
def __init__(self):
"Not to be called directly. Use P2PInterface.create_pair()"
raise RuntimeError(P2PInterface.__init__.__doc__)
def destroy(self):
if self._slave:
if self.index in self._slave.get_if_data():
self._slave.del_if(self.index)
self._slave = None
class ForeignNodeInterface(NSInterface):
"""Class to handle already existing interfaces inside a name space, usually
just the loopback device, but it can be other user-created interfaces. On
destruction, the code will try to restore the interface to the state it was
in before being imported into netns."""
def __init__(self, node, iface):
iface = node._slave.get_if_data(iface)
self._original_state = iface.copy()
super(ForeignNodeInterface, self).__init__(node, iface.index)
# FIXME: register somewhere for destruction!
def destroy(self): # override: restore as much as possible
if self._slave:
if self.index in self._slave.get_if_data():
self._slave.set_if(self._original_state)
self._slave = None
class ImportedNodeInterface(NSInterface):
"""Class to handle already existing interfaces that are migrated inside a
name space: real devices, tun devices, etc. On destruction, the interface
will be restored to the original name space and will try to restore the
original state."""
def __init__(self, node, iface):
iface = netns.iproute.get_if(iface)
self._original_state = iface.copy()
# Change the name to avoid clashes
iface.name = self._gen_if_name()
netns.iproute.set_if(iface)
netns.iproute.change_netns(iface, node.pid)
super(ImportedNodeInterface, self).__init__(node, iface.index)
def destroy(self): # override: restore as much as possible
if self._slave:
if self.index in self._slave.get_if_data():
self._slave.change_netns(self.index, os.getpid())
# else, assume it is in the main name space
netns.iproute.set_if(self._original_state)
self._slave = None
class ExternalInterface(Interface):
"""Add user-facing methods for interfaces that run in the main namespace."""
@property
def control(self):
# This is *the* control interface
return self
# some black magic to automatically get/set interface attributes
def __getattr__(self, name):
iface = netns.iproute.get_if(self.index)
return getattr(iface, name)
def __setattr__(self, name, value):
if name[0] == '_': # forbid anything that doesn't start with a _
super(ExternalInterface, self).__setattr__(name, value)
return
iface = netns.iproute.interface(index = self.index)
setattr(iface, name, value)
return netns.iproute.set_if(iface)
def add_v4_address(self, address, prefix_len, broadcast = None):
addr = netns.iproute.ipv4address(address, prefix_len, broadcast)
netns.iproute.add_addr(self.index, addr)
def add_v6_address(self, address, prefix_len):
addr = netns.iproute.ipv6address(address, prefix_len)
netns.iproute.add_addr(self.index, addr)
def del_v4_address(self, address, prefix_len, broadcast = None):
addr = netns.iproute.ipv4address(address, prefix_len, broadcast)
netns.iproute.del_addr(self.index, addr)
def del_v6_address(self, address, prefix_len):
addr = netns.iproute.ipv6address(address, prefix_len)
netns.iproute.del_addr(self.index, addr)
def get_addresses(self):
addresses = netns.iproute.get_addr_data(self.index)
ret = []
for a in addresses:
if hasattr(a, 'broadcast'):
ret.append(dict(
address = a.address,
prefix_len = a.prefix_len,
broadcast = a.broadcast,
family = 'inet'))
else:
ret.append(dict(
address = a.address,
prefix_len = a.prefix_len,
family = 'inet6'))
return ret
class SlaveInterface(ExternalInterface):
"""Class to handle the main-name-space-facing couples of Nodeinterface.
Does nothing, just avoids any destroy code."""
def destroy(self):
pass
class ImportedInterface(ExternalInterface):
"""Class to handle already existing interfaces. Analogous to
ImportedNodeInterface, this class only differs in that the interface is not
migrated inside the name space. This kind of interfaces can only be
connected to Link objects and not assigned to a name space. On
destruction, the code will try to restore the interface to the state it was
in before being imported into netns."""
def __init__(self, iface):
iface = netns.iproute.get_if(iface)
self._original_state = iface.copy()
super(ImportedInterface, self).__init__(iface.index)
# FIXME: register somewhere for destruction!
def destroy(self): # override: restore as much as possible
if self._original_state:
netns.iproute.set_if(self._original_state)
self._original_state = None
# Link is just another interface type
class Link(ExternalInterface):
@staticmethod
def _gen_br_name():
n = Link._gen_next_id()
# Max 15 chars
return "NETNSbr-%.4x%.3x" % (os.getpid(), n)
def __init__(self, **args):
"""Creates a new Link object, which models a linux bridge device.
Parameters are passed to the set_parameters() method after creation."""
iface = netns.iproute.create_bridge(self._gen_br_name())
super(Link, self).__init__(iface.index)
self._parameters = {}
self._ports = weakref.WeakValueDictionary()
# FIXME: is this correct/desirable/etc?
self.stp = False
self.forward_delay = 0
# FIXME: register somewhere
if args:
self.set_parameters(**args)
def __getattr__(self, name):
iface = netns.iproute.get_bridge(self.index)
return getattr(iface, name)
def __setattr__(self, name, value):
if name[0] == '_': # forbid anything that doesn't start with a _
super(Link, self).__setattr__(name, value)
return
# Set ports
if name in ('up', 'mtu'):
for i in self._ports.values():
setattr(i, name, value)
# Set bridge
iface = netns.iproute.bridge(index = self.index)
setattr(iface, name, value)
netns.iproute.set_bridge(iface)
def __del__(self):
self.destroy()
def destroy(self):
if not self.index:
return
self.up = False
for p in self._ports.values():
try:
self.disconnect(p)
except:
pass
self._ports.clear()
netns.iproute.del_bridge(self.index)
self._idx = None
def connect(self, iface):
assert iface.control.index not in self._ports
try:
self._apply_parameters(self._parameters, iface.control)
netns.iproute.add_bridge_port(self.index, iface.control.index)
except:
self._apply_parameters({}, iface.control)
raise
iface.control.up = self.up
iface.control.mtu = self.mtu
self._ports[iface.control.index] = iface.control
def disconnect(self, iface):
assert iface.control.index in self._ports
netns.iproute.del_bridge_port(self.index, iface.control.index)
self._apply_parameters({}, iface.control)
del self._ports[iface.control.index]
def set_parameters(self, bandwidth = None,
delay = None, delay_jitter = None,
delay_correlation = None, delay_distribution = None,
loss = None, loss_correlation = None,
dup = None, dup_correlation = None,
corrupt = None, corrupt_correlation = None):
"""Set the parameters that control the link characteristics. For the
description of each, refer to netem documentation:
http://www.linuxfoundation.org/collaborate/workgroups/networking/netem
Arguments:
- `bandwidth' should be specified in bits per second.
- `delay' and `delay_jitter' are specified in seconds.
- `delay_distribution' is the name of a distribution description file;
`iproute' comes by default with `normal', `pareto', and
`paretonormal'.
- `delay_correlation', `loss', `loss_correlation', `dup',
`dup_correlation', `corrupt', and `corrupt_correlation' take a
percentage value in the form of a number between 0 and 1. (50% is
passed as 0.5)."""
parameters = dict(bandwidth = bandwidth,
delay = delay, delay_jitter = delay_jitter,
delay_correlation = delay_correlation,
delay_distribution = delay_distribution,
loss = loss, loss_correlation = loss_correlation,
dup = dup, dup_correlation = dup_correlation,
corrupt = corrupt, corrupt_correlation = corrupt_correlation)
try:
self._apply_parameters(parameters)
except:
self._apply_parameters(self._parameters)
raise
self._parameters = parameters
def _apply_parameters(self, parameters, port = None):
for i in [port] if port else self._ports.values():
netns.iproute.set_tc(i.index, **parameters)