Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nemu3
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
nemu3
Commits
53c49b08
Commit
53c49b08
authored
Dec 16, 2011
by
Martín Ferrari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mass change: rename netns to nemu.
parent
eac808b8
Changes
21
Show whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
230 additions
and
229 deletions
+230
-229
DEPENDENCIES
DEPENDENCIES
+2
-2
MANIFEST
MANIFEST
+1
-1
benchmarks/linear-raw-throughput.py
benchmarks/linear-raw-throughput.py
+5
-5
sample-api.txt
sample-api.txt
+14
-14
sample.py
sample.py
+10
-10
setup.py
setup.py
+6
-5
src/nemu/__init__.py
src/nemu/__init__.py
+2
-2
src/nemu/environ.py
src/nemu/environ.py
+1
-1
src/nemu/interface.py
src/nemu/interface.py
+52
-52
src/nemu/iproute.py
src/nemu/iproute.py
+1
-1
src/nemu/node.py
src/nemu/node.py
+17
-17
src/nemu/protocol.py
src/nemu/protocol.py
+25
-25
src/nemu/subprocess_.py
src/nemu/subprocess_.py
+5
-5
test/test_core.py
test/test_core.py
+37
-37
test/test_interfaces.py
test/test_interfaces.py
+7
-7
test/test_node.py
test/test_node.py
+11
-11
test/test_protocol.py
test/test_protocol.py
+7
-7
test/test_routing.py
test/test_routing.py
+3
-3
test/test_subprocess.py
test/test_subprocess.py
+7
-7
test/test_switch.py
test/test_switch.py
+14
-14
test/test_util.py
test/test_util.py
+3
-3
No files found.
DEPENDENCIES
View file @
53c49b08
Required packages for using ne
tns
Required packages for using ne
mu
================================
=
================================
python-unshare http://pypi.python.org/pypi/python-unshare
python-unshare http://pypi.python.org/pypi/python-unshare
python-passfd http://pypi.python.org/pypi/python-passfd
python-passfd http://pypi.python.org/pypi/python-passfd
...
...
MANIFEST
View file @
53c49b08
...
@@ -3,7 +3,7 @@ Makefile
...
@@ -3,7 +3,7 @@ Makefile
sample-api.py
sample-api.py
setup.cfg
setup.cfg
setup.py
setup.py
src/ne
tns
/__init__.py
src/ne
mu
/__init__.py
t/test_core.py
t/test_core.py
t/test_interfaces.py
t/test_interfaces.py
t/test_node.py
t/test_node.py
...
...
benchmarks/linear-raw-throughput.py
View file @
53c49b08
#!/usr/bin/env python
#!/usr/bin/env python
# vim: ts=4:sw=4:et:ai:sts=4
# vim: ts=4:sw=4:et:ai:sts=4
import
csv
,
getopt
,
ne
tns
,
os
,
os
.
path
,
re
,
select
,
subprocess
,
sys
import
csv
,
getopt
,
ne
mu
,
os
,
os
.
path
,
re
,
select
,
subprocess
,
sys
__doc__
=
"""Creates a linear network topology, and measures the maximum
__doc__
=
"""Creates a linear network topology, and measures the maximum
end-to-end throughput for the specified packet size."""
end-to-end throughput for the specified packet size."""
...
@@ -107,7 +107,7 @@ def main():
...
@@ -107,7 +107,7 @@ def main():
if
not
(
time
or
nbytes
or
packets
):
if
not
(
time
or
nbytes
or
packets
):
time
=
10
time
=
10
udp_perf
=
ne
tns
.
environ
.
find_bin
(
"udp-perf"
,
udp_perf
=
ne
mu
.
environ
.
find_bin
(
"udp-perf"
,
extra_path
=
[
"."
,
os
.
path
.
dirname
(
sys
.
argv
[
0
])])
extra_path
=
[
"."
,
os
.
path
.
dirname
(
sys
.
argv
[
0
])])
if
not
udp_perf
:
if
not
udp_perf
:
raise
RuntimeError
(
"Cannot find `udp-perf'"
)
raise
RuntimeError
(
"Cannot find `udp-perf'"
)
...
@@ -197,11 +197,11 @@ def create_topo(n, p2p, delay, jitter, bw):
...
@@ -197,11 +197,11 @@ def create_topo(n, p2p, delay, jitter, bw):
interfaces = []
interfaces = []
links = []
links = []
for i in range(n):
for i in range(n):
nodes.append(ne
tns
.Node())
nodes.append(ne
mu
.Node())
if p2p:
if p2p:
interfaces = [[None]]
interfaces = [[None]]
for i in range(n - 1):
for i in range(n - 1):
a, b = ne
tns
.P2PInterface.create_pair(nodes[i], nodes[i + 1])
a, b = ne
mu
.P2PInterface.create_pair(nodes[i], nodes[i + 1])
interfaces[i].append(a)
interfaces[i].append(a)
interfaces.append([])
interfaces.append([])
interfaces[i + 1] = [b]
interfaces[i + 1] = [b]
...
@@ -218,7 +218,7 @@ def create_topo(n, p2p, delay, jitter, bw):
...
@@ -218,7 +218,7 @@ def create_topo(n, p2p, delay, jitter, bw):
right = None
right = None
interfaces.append((left, right))
interfaces.append((left, right))
for i in range(n - 1):
for i in range(n - 1):
links = ne
tns
.Switch(bandwidth = bw, delay = delay,
links = ne
mu
.Switch(bandwidth = bw, delay = delay,
delay_jitter = jitter)
delay_jitter = jitter)
links.up = True
links.up = True
links.connect(interfaces[i][1])
links.connect(interfaces[i][1])
...
...
sample-api.txt
View file @
53c49b08
#/usr/bin/env python
#/usr/bin/env python
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
import ne
tns
import ne
mu
import signal
import signal
# run_as: user to setuid() to before running applications (this is assumed to
# run_as: user to setuid() to before running applications (this is assumed to
# run as root)
# run as root)
ne
tns
.config.run_as = 'nobody'
ne
mu
.config.run_as = 'nobody'
# Clean-up is essential to avoid leaving bridge devices all over the place
# Clean-up is essential to avoid leaving bridge devices all over the place
# (luckily, the veths die automatically). This installs signals and exit
# (luckily, the veths die automatically). This installs signals and exit
# handlers.
# handlers.
ne
tns
.set_cleanup_hooks(on_exit = True,
ne
mu
.set_cleanup_hooks(on_exit = True,
on_signals = [signal.SIGTERM, signal.SIGINT])
on_signals = [signal.SIGTERM, signal.SIGINT])
# each Node is a netns
# each Node is a netns
a = ne
tns
.Node()
a = ne
mu
.Node()
b = ne
tns
.Node()
b = ne
mu
.Node()
print "Nodes started with pids: %d and %d" % (a.pid, b.pid)
print "Nodes started with pids: %d and %d" % (a.pid, b.pid)
# interface object maps to a veth pair with one end in a netns
# interface object maps to a veth pair with one end in a netns
if0 = a.add_if(lladdr = '42:71:e0:90:ca:42')
if0 = a.add_if(lladdr = '42:71:e0:90:ca:42')
# This is equivalent
# This is equivalent
#if0 = ne
tns
.NodeInterface(a)
#if0 = ne
mu
.NodeInterface(a)
#if0.lladdr = '42:71:e0:90:ca:42'
#if0.lladdr = '42:71:e0:90:ca:42'
if1 = b.add_if(mtu = 1492)
if1 = b.add_if(mtu = 1492)
...
@@ -31,7 +31,7 @@ if2 = b.import_if('tun0')
...
@@ -31,7 +31,7 @@ if2 = b.import_if('tun0')
# each Switch is a linux bridge, all the parameters are applied to the
# each Switch is a linux bridge, all the parameters are applied to the
# associated interfaces as tc qdiscs.
# associated interfaces as tc qdiscs.
switch0 = ne
tns
.Switch(bandwidth = 100 * 1024 * 1024,
switch0 = ne
mu
.Switch(bandwidth = 100 * 1024 * 1024,
delay = 0.01, delay_jitter = 0.001,
delay = 0.01, delay_jitter = 0.001,
delay_correlation = 0.25, delay_distribution = 'normal',
delay_correlation = 0.25, delay_distribution = 'normal',
loss = 0.005, loss_correlation = 0.20,
loss = 0.005, loss_correlation = 0.20,
...
@@ -45,15 +45,15 @@ switch0.connect(if1)
...
@@ -45,15 +45,15 @@ switch0.connect(if1)
# Should be experimented with Tom Geoff's patch to see if the bridge could be
# Should be experimented with Tom Geoff's patch to see if the bridge could be
# avoided; but for that the API would be slightly different, as these would be
# avoided; but for that the API would be slightly different, as these would be
# point-to-point interfaces and links.
# point-to-point interfaces and links.
# ppp0 = ne
tns
.PPPSwitch(a, b, bandwidth = ....)
# ppp0 = ne
mu
.PPPSwitch(a, b, bandwidth = ....)
# if0 = ppp0.interface(a)
# if0 = ppp0.interface(a)
# For now, we have simple P2P interfaces:
# For now, we have simple P2P interfaces:
(pppa, pppb) = ne
tns
.P2PInterface.create_pair(a, b)
(pppa, pppb) = ne
mu
.P2PInterface.create_pair(a, b)
# Add and connect a tap device (as if a external router were plugged into a
# Add and connect a tap device (as if a external router were plugged into a
# switch)
# switch)
if2 = ne
tns
.ImportedInterface('tap0')
if2 = ne
mu
.ImportedInterface('tap0')
switch0.connect(if2)
switch0.connect(if2)
switch0.up = True
switch0.up = True
...
@@ -78,8 +78,8 @@ addrs = if0.get_addresses()
...
@@ -78,8 +78,8 @@ addrs = if0.get_addresses()
stats = if0.get_stats()
stats = if0.get_stats()
routes = a.get_routes()
routes = a.get_routes()
ifaces = a.get_interfaces()
ifaces = a.get_interfaces()
nodes = ne
tns
.get_nodes()
nodes = ne
mu
.get_nodes()
switches = ne
tns
.get_switches()
switches = ne
mu
.get_switches()
stats = link0.get_stats()
stats = link0.get_stats()
# Run a process in background
# Run a process in background
...
@@ -101,14 +101,14 @@ app2.wait()
...
@@ -101,14 +101,14 @@ app2.wait()
def setup_linear_topology(n, bd, delay):
def setup_linear_topology(n, bd, delay):
nodes = []
nodes = []
for i in range(n):
for i in range(n):
nodes.append(ne
tns
.Node())
nodes.append(ne
mu
.Node())
for i in range(n - 1):
for i in range(n - 1):
if1 = nodes[i].add_if()
if1 = nodes[i].add_if()
if2 = nodes[i + 1].add_if()
if2 = nodes[i + 1].add_if()
if1.add_v4_address(addr = ('10.0.%d.2' % i), prefix_len = 24)
if1.add_v4_address(addr = ('10.0.%d.2' % i), prefix_len = 24)
if2.add_v4_address(addr = ('10.0.%d.1' % i), prefix_len = 24)
if2.add_v4_address(addr = ('10.0.%d.1' % i), prefix_len = 24)
switch = ne
tns
.Switch(bandwidth = bd, delay = delay)
switch = ne
mu
.Switch(bandwidth = bd, delay = delay)
switch.connect(if1)
switch.connect(if1)
switch.connect(if2)
switch.connect(if2)
...
...
sample.py
View file @
53c49b08
#!/usr/bin/env python
#!/usr/bin/env python
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
import
os
,
ne
tns
,
subprocess
,
time
import
os
,
ne
mu
,
subprocess
,
time
xterm
=
ne
tns
.
environ
.
find_bin
(
"xterm"
)
xterm
=
ne
mu
.
environ
.
find_bin
(
"xterm"
)
X
=
"DISPLAY"
in
os
.
environ
and
xterm
X
=
"DISPLAY"
in
os
.
environ
and
xterm
# each Node is a netns
# each Node is a netns
node0
=
ne
tns
.
Node
(
forward_X11
=
X
)
node0
=
ne
mu
.
Node
(
forward_X11
=
X
)
node1
=
ne
tns
.
Node
(
forward_X11
=
X
)
node1
=
ne
mu
.
Node
(
forward_X11
=
X
)
node2
=
ne
tns
.
Node
(
forward_X11
=
X
)
node2
=
ne
mu
.
Node
(
forward_X11
=
X
)
print
"Nodes started with pids: %s"
%
str
((
node0
.
pid
,
node1
.
pid
,
print
"Nodes started with pids: %s"
%
str
((
node0
.
pid
,
node1
.
pid
,
node2
.
pid
))
node2
.
pid
))
# interface object maps to a veth pair with one end in a netns
# interface object maps to a veth pair with one end in a netns
if0
=
ne
tns
.
NodeInterface
(
node0
)
if0
=
ne
mu
.
NodeInterface
(
node0
)
if1a
=
ne
tns
.
NodeInterface
(
node1
)
if1a
=
ne
mu
.
NodeInterface
(
node1
)
# Between node1 and node2, we use a P2P interface
# Between node1 and node2, we use a P2P interface
(
if1b
,
if2
)
=
ne
tns
.
P2PInterface
.
create_pair
(
node1
,
node2
)
(
if1b
,
if2
)
=
ne
mu
.
P2PInterface
.
create_pair
(
node1
,
node2
)
switch0
=
ne
tns
.
Switch
(
switch0
=
ne
mu
.
Switch
(
bandwidth
=
100
*
1024
*
1024
,
bandwidth
=
100
*
1024
*
1024
,
delay
=
0.1
,
# 100 ms
delay
=
0.1
,
# 100 ms
delay_jitter
=
0.01
,
# 10ms
delay_jitter
=
0.01
,
# 10ms
...
@@ -56,7 +56,7 @@ print "Connectivity IPv4 OK!"
...
@@ -56,7 +56,7 @@ print "Connectivity IPv4 OK!"
if
X
:
if
X
:
app1
=
node1
.
Popen
(
"%s -geometry -0+0 -e %s -ni %s"
%
app1
=
node1
.
Popen
(
"%s -geometry -0+0 -e %s -ni %s"
%
(
xterm
,
ne
tns
.
environ
.
tcpdump_path
,
if1b
.
name
),
shell
=
True
)
(
xterm
,
ne
mu
.
environ
.
tcpdump_path
,
if1b
.
name
),
shell
=
True
)
time
.
sleep
(
3
)
time
.
sleep
(
3
)
app0
=
node0
.
Popen
(
"%s -geometry +0+0 -e ping -c 10 10.0.1.2"
%
xterm
,
app0
=
node0
.
Popen
(
"%s -geometry +0+0 -e ping -c 10 10.0.1.2"
%
xterm
,
shell
=
True
)
shell
=
True
)
...
...
setup.py
View file @
53c49b08
#!/usr/bin/env python
#!/usr/bin/env python
#
vim: set fileencoding=utf-8
#
-*- coding: utf-8 -*-
# vim: ts=4:sw=4:et:ai:sts=4
# vim: ts=4:sw=4:et:ai:sts=4
from
distutils.core
import
setup
,
Extension
,
Command
from
distutils.core
import
setup
,
Extension
,
Command
setup
(
setup
(
name
=
'ne
tns
'
,
name
=
'ne
mu
'
,
version
=
'0.1'
,
version
=
'0.1'
,
description
=
'''A framework for creating emulated networks in a
description
=
'''A framework for creating emulated networks in a
single host and run experiments on them'''
,
single host and run experiments on them'''
,
# long_description = longdesc,
# long_description = longdesc,
author
=
'Mart
i
n Ferrari'
,
author
=
'Mart
í
n Ferrari'
,
author_email
=
'martin.ferrari@gmail.com'
,
author_email
=
'martin.ferrari@gmail.com'
,
url
=
'http://
yans.pl.sophia.inria.fr/code/hgwebdir.cgi/netns
/'
,
url
=
'http://
code.google.com/p/nemu
/'
,
license
=
'GPLv2'
,
license
=
'GPLv2'
,
platforms
=
'Linux'
,
platforms
=
'Linux'
,
packages
=
[
'ne
tns
'
],
packages
=
[
'ne
mu
'
],
package_dir
=
{
''
:
'src'
}
package_dir
=
{
''
:
'src'
}
)
)
src/ne
tns
/__init__.py
→
src/ne
mu
/__init__.py
View file @
53c49b08
...
@@ -18,8 +18,8 @@
...
@@ -18,8 +18,8 @@
# Nemu. If not, see <http://www.gnu.org/licenses/>.
# Nemu. If not, see <http://www.gnu.org/licenses/>.
import
os
,
pwd
import
os
,
pwd
from
ne
tns
.node
import
*
from
ne
mu
.node
import
*
from
ne
tns
.interface
import
*
from
ne
mu
.interface
import
*
class
__Config
(
object
):
class
__Config
(
object
):
def
__init__
(
self
):
def
__init__
(
self
):
...
...
src/ne
tns
/environ.py
→
src/ne
mu
/environ.py
View file @
53c49b08
...
@@ -147,7 +147,7 @@ def log_use_syslog(use = True, ident = None, logopt = 0,
...
@@ -147,7 +147,7 @@ def log_use_syslog(use = True, ident = None, logopt = 0,
return
return
if
not
ident
:
if
not
ident
:
#ident = os.path.basename(sys.argv[0])
#ident = os.path.basename(sys.argv[0])
ident
=
"ne
tns
"
ident
=
"ne
mu
"
syslog
.
openlog
(
"%s[%d]"
%
(
ident
,
os
.
getpid
()),
logopt
,
facility
)
syslog
.
openlog
(
"%s[%d]"
%
(
ident
,
os
.
getpid
()),
logopt
,
facility
)
_log_syslog_opts
=
(
ident
,
logopt
,
facility
)
_log_syslog_opts
=
(
ident
,
logopt
,
facility
)
_log_use_syslog
=
True
_log_use_syslog
=
True
...
...
src/ne
tns
/interface.py
→
src/ne
mu
/interface.py
View file @
53c49b08
...
@@ -18,8 +18,8 @@
...
@@ -18,8 +18,8 @@
# Nemu. If not, see <http://www.gnu.org/licenses/>.
# Nemu. If not, see <http://www.gnu.org/licenses/>.
import
os
,
weakref
import
os
,
weakref
import
ne
tns
.iproute
import
ne
mu
.iproute
from
ne
tns
.environ
import
*
from
ne
mu
.environ
import
*
__all__
=
[
'NodeInterface'
,
'P2PInterface'
,
'ImportedInterface'
,
__all__
=
[
'NodeInterface'
,
'P2PInterface'
,
'ImportedInterface'
,
'ImportedNodeInterface'
,
'Switch'
]
'ImportedNodeInterface'
,
'Switch'
]
...
@@ -94,24 +94,24 @@ class NSInterface(Interface):
...
@@ -94,24 +94,24 @@ class NSInterface(Interface):
if
name
[
0
]
==
'_'
:
# forbid anything that doesn't start with a _
if
name
[
0
]
==
'_'
:
# forbid anything that doesn't start with a _
super
(
Interface
,
self
).
__setattr__
(
name
,
value
)
super
(
Interface
,
self
).
__setattr__
(
name
,
value
)
return
return
iface
=
ne
tns
.
iproute
.
interface
(
index
=
self
.
index
)
iface
=
ne
mu
.
iproute
.
interface
(
index
=
self
.
index
)
setattr
(
iface
,
name
,
value
)
setattr
(
iface
,
name
,
value
)
return
self
.
_slave
.
set_if
(
iface
)
return
self
.
_slave
.
set_if
(
iface
)
def
add_v4_address
(
self
,
address
,
prefix_len
,
broadcast
=
None
):
def
add_v4_address
(
self
,
address
,
prefix_len
,
broadcast
=
None
):
addr
=
ne
tns
.
iproute
.
ipv4address
(
address
,
prefix_len
,
broadcast
)
addr
=
ne
mu
.
iproute
.
ipv4address
(
address
,
prefix_len
,
broadcast
)
self
.
_slave
.
add_addr
(
self
.
index
,
addr
)
self
.
_slave
.
add_addr
(
self
.
index
,
addr
)
def
add_v6_address
(
self
,
address
,
prefix_len
):
def
add_v6_address
(
self
,
address
,
prefix_len
):
addr
=
ne
tns
.
iproute
.
ipv6address
(
address
,
prefix_len
)
addr
=
ne
mu
.
iproute
.
ipv6address
(
address
,
prefix_len
)
self
.
_slave
.
add_addr
(
self
.
index
,
addr
)
self
.
_slave
.
add_addr
(
self
.
index
,
addr
)
def
del_v4_address
(
self
,
address
,
prefix_len
,
broadcast
=
None
):
def
del_v4_address
(
self
,
address
,
prefix_len
,
broadcast
=
None
):
addr
=
ne
tns
.
iproute
.
ipv4address
(
address
,
prefix_len
,
broadcast
)
addr
=
ne
mu
.
iproute
.
ipv4address
(
address
,
prefix_len
,
broadcast
)
self
.
_slave
.
del_addr
(
self
.
index
,
addr
)
self
.
_slave
.
del_addr
(
self
.
index
,
addr
)
def
del_v6_address
(
self
,
address
,
prefix_len
):
def
del_v6_address
(
self
,
address
,
prefix_len
):
addr
=
ne
tns
.
iproute
.
ipv6address
(
address
,
prefix_len
)
addr
=
ne
mu
.
iproute
.
ipv6address
(
address
,
prefix_len
)
self
.
_slave
.
del_addr
(
self
.
index
,
addr
)
self
.
_slave
.
del_addr
(
self
.
index
,
addr
)
def
get_addresses
(
self
):
def
get_addresses
(
self
):
...
@@ -139,13 +139,13 @@ class NodeInterface(NSInterface):
...
@@ -139,13 +139,13 @@ class NodeInterface(NSInterface):
"""Create a new interface. `node' is the name space in which this
"""Create a new interface. `node' is the name space in which this
interface should be put."""
interface should be put."""
self
.
_slave
=
None
self
.
_slave
=
None
if1
=
ne
tns
.
iproute
.
interface
(
name
=
self
.
_gen_if_name
())
if1
=
ne
mu
.
iproute
.
interface
(
name
=
self
.
_gen_if_name
())
if2
=
ne
tns
.
iproute
.
interface
(
name
=
self
.
_gen_if_name
())
if2
=
ne
mu
.
iproute
.
interface
(
name
=
self
.
_gen_if_name
())
ctl
,
ns
=
ne
tns
.
iproute
.
create_if_pair
(
if1
,
if2
)
ctl
,
ns
=
ne
mu
.
iproute
.
create_if_pair
(
if1
,
if2
)
try
:
try
:
ne
tns
.
iproute
.
change_netns
(
ns
,
node
.
pid
)
ne
mu
.
iproute
.
change_netns
(
ns
,
node
.
pid
)
except
:
except
:
ne
tns
.
iproute
.
del_if
(
ctl
)
ne
mu
.
iproute
.
del_if
(
ctl
)
# the other interface should go away automatically
# the other interface should go away automatically
raise
raise
self
.
_control
=
SlaveInterface
(
ctl
.
index
)
self
.
_control
=
SlaveInterface
(
ctl
.
index
)
...
@@ -173,14 +173,14 @@ class P2PInterface(NSInterface):
...
@@ -173,14 +173,14 @@ class P2PInterface(NSInterface):
def
create_pair
(
node1
,
node2
):
def
create_pair
(
node1
,
node2
):
"""Create and return a pair of connected P2PInterface objects,
"""Create and return a pair of connected P2PInterface objects,
assigned to name spaces represented by `node1' and `node2'."""
assigned to name spaces represented by `node1' and `node2'."""
if1
=
ne
tns
.
iproute
.
interface
(
name
=
P2PInterface
.
_gen_if_name
())
if1
=
ne
mu
.
iproute
.
interface
(
name
=
P2PInterface
.
_gen_if_name
())
if2
=
ne
tns
.
iproute
.
interface
(
name
=
P2PInterface
.
_gen_if_name
())
if2
=
ne
mu
.
iproute
.
interface
(
name
=
P2PInterface
.
_gen_if_name
())
pair
=
ne
tns
.
iproute
.
create_if_pair
(
if1
,
if2
)
pair
=
ne
mu
.
iproute
.
create_if_pair
(
if1
,
if2
)
try
:
try
:
ne
tns
.
iproute
.
change_netns
(
pair
[
0
],
node1
.
pid
)
ne
mu
.
iproute
.
change_netns
(
pair
[
0
],
node1
.
pid
)
ne
tns
.
iproute
.
change_netns
(
pair
[
1
],
node2
.
pid
)
ne
mu
.
iproute
.
change_netns
(
pair
[
1
],
node2
.
pid
)
except
:
except
:
ne
tns
.
iproute
.
del_if
(
pair
[
0
])
ne
mu
.
iproute
.
del_if
(
pair
[
0
])
# the other interface should go away automatically
# the other interface should go away automatically
raise
raise
...
@@ -215,13 +215,13 @@ class ImportedNodeInterface(NSInterface):
...
@@ -215,13 +215,13 @@ class ImportedNodeInterface(NSInterface):
self
.
_slave
=
None
self
.
_slave
=
None
self
.
_migrate
=
migrate
self
.
_migrate
=
migrate
if
self
.
_migrate
:
if
self
.
_migrate
:
iface
=
ne
tns
.
iproute
.
get_if
(
iface
)
iface
=
ne
mu
.
iproute
.
get_if
(
iface
)
self
.
_original_state
=
iface
.
copy
()
self
.
_original_state
=
iface
.
copy
()
# Change the name to avoid clashes
# Change the name to avoid clashes
iface
.
name
=
self
.
_gen_if_name
()
iface
.
name
=
self
.
_gen_if_name
()
ne
tns
.
iproute
.
set_if
(
iface
)
ne
mu
.
iproute
.
set_if
(
iface
)
# Migrate it
# Migrate it
ne
tns
.
iproute
.
change_netns
(
iface
,
node
.
pid
)
ne
mu
.
iproute
.
change_netns
(
iface
,
node
.
pid
)
else
:
else
:
iface
=
node
.
_slave
.
get_if_data
(
iface
)
iface
=
node
.
_slave
.
get_if_data
(
iface
)
self
.
_original_state
=
iface
.
copy
()
self
.
_original_state
=
iface
.
copy
()
...
@@ -239,7 +239,7 @@ class ImportedNodeInterface(NSInterface):
...
@@ -239,7 +239,7 @@ class ImportedNodeInterface(NSInterface):
self
.
_slave
.
set_if
(
self
.
_original_state
)
self
.
_slave
.
set_if
(
self
.
_original_state
)
if
self
.
_migrate
:
if
self
.
_migrate
:
# else, assume it is already in the main name space
# else, assume it is already in the main name space
ne
tns
.
iproute
.
set_if
(
self
.
_original_state
)
ne
mu
.
iproute
.
set_if
(
self
.
_original_state
)
self
.
_slave
=
None
self
.
_slave
=
None
class
TapNodeInterface
(
NSInterface
):
class
TapNodeInterface
(
NSInterface
):
...
@@ -251,9 +251,9 @@ class TapNodeInterface(NSInterface):
...
@@ -251,9 +251,9 @@ class TapNodeInterface(NSInterface):
interface should be put."""
interface should be put."""
self
.
_fd
=
None
self
.
_fd
=
None
self
.
_slave
=
None
self
.
_slave
=
None
iface
=
ne
tns
.
iproute
.
interface
(
name
=
self
.
_gen_if_name
())
iface
=
ne
mu
.
iproute
.
interface
(
name
=
self
.
_gen_if_name
())
iface
,
self
.
_fd
=
ne
tns
.
iproute
.
create_tap
(
iface
,
use_pi
=
use_pi
)
iface
,
self
.
_fd
=
ne
mu
.
iproute
.
create_tap
(
iface
,
use_pi
=
use_pi
)
ne
tns
.
iproute
.
change_netns
(
iface
.
name
,
node
.
pid
)
ne
mu
.
iproute
.
change_netns
(
iface
.
name
,
node
.
pid
)
super
(
TapNodeInterface
,
self
).
__init__
(
node
,
iface
.
index
)
super
(
TapNodeInterface
,
self
).
__init__
(
node
,
iface
.
index
)
@
property
@
property
...
@@ -278,10 +278,10 @@ class TunNodeInterface(NSInterface):
...
@@ -278,10 +278,10 @@ class TunNodeInterface(NSInterface):
interface should be put."""
interface should be put."""
self
.
_fd
=
None
self
.
_fd
=
None
self
.
_slave
=
None
self
.
_slave
=
None
iface
=
ne
tns
.
iproute
.
interface
(
name
=
self
.
_gen_if_name
())
iface
=
ne
mu
.
iproute
.
interface
(
name
=
self
.
_gen_if_name
())
iface
,
self
.
_fd
=
ne
tns
.
iproute
.
create_tap
(
iface
,
use_pi
=
use_pi
,
iface
,
self
.
_fd
=
ne
mu
.
iproute
.
create_tap
(
iface
,
use_pi
=
use_pi
,
tun
=
True
)
tun
=
True
)
ne
tns
.
iproute
.
change_netns
(
iface
.
name
,
node
.
pid
)
ne
mu
.
iproute
.
change_netns
(
iface
.
name
,
node
.
pid
)
super
(
TunNodeInterface
,
self
).
__init__
(
node
,
iface
.
index
)
super
(
TunNodeInterface
,
self
).
__init__
(
node
,
iface
.
index
)
@
property
@
property
...
@@ -307,35 +307,35 @@ class ExternalInterface(Interface):
...
@@ -307,35 +307,35 @@ class ExternalInterface(Interface):
# some black magic to automatically get/set interface attributes
# some black magic to automatically get/set interface attributes
def
__getattr__
(
self
,
name
):
def
__getattr__
(
self
,
name
):
iface
=
ne
tns
.
iproute
.
get_if
(
self
.
index
)
iface
=
ne
mu
.
iproute
.
get_if
(
self
.
index
)
return
getattr
(
iface
,
name
)
return
getattr
(
iface
,
name
)
def
__setattr__
(
self
,
name
,
value
):
def
__setattr__
(
self
,
name
,
value
):
if
name
[
0
]
==
'_'
:
# forbid anything that doesn't start with a _
if
name
[
0
]
==
'_'
:
# forbid anything that doesn't start with a _
super
(
ExternalInterface
,
self
).
__setattr__
(
name
,
value
)
super
(
ExternalInterface
,
self
).
__setattr__
(
name
,
value
)
return
return
iface
=
ne
tns
.
iproute
.
interface
(
index
=
self
.
index
)
iface
=
ne
mu
.
iproute
.
interface
(
index
=
self
.
index
)
setattr
(
iface
,
name
,
value
)
setattr
(
iface
,
name
,
value
)
return
ne
tns
.
iproute
.
set_if
(
iface
)
return
ne
mu
.
iproute
.
set_if
(
iface
)
def
add_v4_address
(
self
,
address
,
prefix_len
,
broadcast
=
None
):
def
add_v4_address
(
self
,
address
,
prefix_len
,
broadcast
=
None
):
addr
=
ne
tns
.
iproute
.
ipv4address
(
address
,
prefix_len
,
broadcast
)
addr
=
ne
mu
.
iproute
.
ipv4address
(
address
,
prefix_len
,
broadcast
)
ne
tns
.
iproute
.
add_addr
(
self
.
index
,
addr
)
ne
mu
.
iproute
.
add_addr
(
self
.
index
,
addr
)
def
add_v6_address
(
self
,
address
,
prefix_len
):
def
add_v6_address
(
self
,
address
,
prefix_len
):
addr
=
ne
tns
.
iproute
.
ipv6address
(
address
,
prefix_len
)
addr
=
ne
mu
.
iproute
.
ipv6address
(
address
,
prefix_len
)
ne
tns
.
iproute
.
add_addr
(
self
.
index
,
addr
)
ne
mu
.
iproute
.
add_addr
(
self
.
index
,
addr
)
def
del_v4_address
(
self
,
address
,
prefix_len
,
broadcast
=
None
):
def
del_v4_address
(
self
,
address
,
prefix_len
,
broadcast
=
None
):
addr
=
ne
tns
.
iproute
.
ipv4address
(
address
,
prefix_len
,
broadcast
)
addr
=
ne
mu
.
iproute
.
ipv4address
(
address
,
prefix_len
,
broadcast
)
ne
tns
.
iproute
.
del_addr
(
self
.
index
,
addr
)
ne
mu
.
iproute
.
del_addr
(
self
.
index
,
addr
)
def
del_v6_address
(
self
,
address
,
prefix_len
):
def
del_v6_address
(
self
,
address
,
prefix_len
):
addr
=
ne
tns
.
iproute
.
ipv6address
(
address
,
prefix_len
)
addr
=
ne
mu
.
iproute
.
ipv6address
(
address
,
prefix_len
)
ne
tns
.
iproute
.
del_addr
(
self
.
index
,
addr
)
ne
mu
.
iproute
.
del_addr
(
self
.
index
,
addr
)
def
get_addresses
(
self
):
def
get_addresses
(
self
):
addresses
=
ne
tns
.
iproute
.
get_addr_data
(
self
.
index
)
addresses
=
ne
mu
.
iproute
.
get_addr_data
(
self
.
index
)
ret
=
[]
ret
=
[]
for
a
in
addresses
:
for
a
in
addresses
:
if
hasattr
(
a
,
'broadcast'
):
if
hasattr
(
a
,
'broadcast'
):
...
@@ -363,10 +363,10 @@ class ImportedInterface(ExternalInterface):
...
@@ -363,10 +363,10 @@ class ImportedInterface(ExternalInterface):
not migrated inside the name space. This kind of interfaces can only be
not migrated inside the name space. This kind of interfaces can only be
connected to Switch objects and not assigned to a name space. On
connected to Switch objects and not assigned to a name space. On
destruction, the code will try to restore the interface to the state it
destruction, the code will try to restore the interface to the state it
was in before being imported into ne
tns
."""
was in before being imported into ne
mu
."""
def
__init__
(
self
,
iface
):
def
__init__
(
self
,
iface
):
self
.
_original_state
=
None
self
.
_original_state
=
None
iface
=
ne
tns
.
iproute
.
get_if
(
iface
)
iface
=
ne
mu
.
iproute
.
get_if
(
iface
)
self
.
_original_state
=
iface
.
copy
()
self
.
_original_state
=
iface
.
copy
()
super
(
ImportedInterface
,
self
).
__init__
(
iface
.
index
)
super
(
ImportedInterface
,
self
).
__init__
(
iface
.
index
)
...
@@ -374,7 +374,7 @@ class ImportedInterface(ExternalInterface):
...
@@ -374,7 +374,7 @@ class ImportedInterface(ExternalInterface):
def
destroy
(
self
):
# override: restore as much as possible
def
destroy
(
self
):
# override: restore as much as possible
if
self
.
_original_state
:
if
self
.
_original_state
:
debug
(
"ImportedInterface(0x%x).destroy()"
%
id
(
self
))
debug
(
"ImportedInterface(0x%x).destroy()"
%
id
(
self
))
ne
tns
.
iproute
.
set_if
(
self
.
_original_state
)
ne
mu
.
iproute
.
set_if
(
self
.
_original_state
)
self
.
_original_state
=
None
self
.
_original_state
=
None
# Switch is just another interface type
# Switch is just another interface type
...
@@ -395,7 +395,7 @@ class Switch(ExternalInterface):
...
@@ -395,7 +395,7 @@ class Switch(ExternalInterface):
self
.
_parameters
=
{}
self
.
_parameters
=
{}
self
.
_ports
=
weakref
.
WeakValueDictionary
()
self
.
_ports
=
weakref
.
WeakValueDictionary
()
iface
=
ne
tns
.
iproute
.
create_bridge
(
self
.
_gen_br_name
())
iface
=
ne
mu
.
iproute
.
create_bridge
(
self
.
_gen_br_name
())
super
(
Switch
,
self
).
__init__
(
iface
.
index
)
super
(
Switch
,
self
).
__init__
(
iface
.
index
)
# FIXME: is this correct/desirable/etc?
# FIXME: is this correct/desirable/etc?
...
@@ -406,7 +406,7 @@ class Switch(ExternalInterface):
...
@@ -406,7 +406,7 @@ class Switch(ExternalInterface):
self
.
set_parameters
(
**
args
)
self
.
set_parameters
(
**
args
)
def
__getattr__
(
self
,
name
):
def
__getattr__
(
self
,
name
):
iface
=
ne
tns
.
iproute
.
get_bridge
(
self
.
index
)
iface
=
ne
mu
.
iproute
.
get_bridge
(
self
.
index
)
return
getattr
(
iface
,
name
)
return
getattr
(
iface
,
name
)
def
__setattr__
(
self
,
name
,
value
):
def
__setattr__
(
self
,
name
,
value
):
...
@@ -419,9 +419,9 @@ class Switch(ExternalInterface):
...
@@ -419,9 +419,9 @@ class Switch(ExternalInterface):
if
self
.
_check_port
(
i
.
index
):
if
self
.
_check_port
(
i
.
index
):
setattr
(
i
,
name
,
value
)
setattr
(
i
,
name
,
value
)
# Set bridge
# Set bridge
iface
=
ne
tns
.
iproute
.
bridge
(
index
=
self
.
index
)
iface
=
ne
mu
.
iproute
.
bridge
(
index
=
self
.
index
)
setattr
(
iface
,
name
,
value
)
setattr
(
iface
,
name
,
value
)
ne
tns
.
iproute
.
set_bridge
(
iface
)
ne
mu
.
iproute
.
set_bridge
(
iface
)
def
destroy
(
self
):
def
destroy
(
self
):
if
not
self
.
index
:
if
not
self
.
index
:
...
@@ -437,14 +437,14 @@ class Switch(ExternalInterface):
...
@@ -437,14 +437,14 @@ class Switch(ExternalInterface):
self
.
disconnect
(
p
)
self
.
disconnect
(
p
)
self
.
_ports
.
clear
()
self
.
_ports
.
clear
()
ne
tns
.
iproute
.
del_bridge
(
self
.
index
)
ne
mu
.
iproute
.
del_bridge
(
self
.
index
)
self
.
_idx
=
None
self
.
_idx
=
None
def
connect
(
self
,
iface
):
def
connect
(
self
,
iface
):
assert
iface
.
control
.
index
not
in
self
.
_ports
assert
iface
.
control
.
index
not
in
self
.
_ports
try
:
try
:
self
.
_apply_parameters
(
self
.
_parameters
,
iface
.
control
)
self
.
_apply_parameters
(
self
.
_parameters
,
iface
.
control
)
ne
tns
.
iproute
.
add_bridge_port
(
self
.
index
,
iface
.
control
.
index
)
ne
mu
.
iproute
.
add_bridge_port
(
self
.
index
,
iface
.
control
.
index
)
except
:
except
:
self
.
_apply_parameters
({},
iface
.
control
)
self
.
_apply_parameters
({},
iface
.
control
)
raise
raise
...
@@ -453,7 +453,7 @@ class Switch(ExternalInterface):
...
@@ -453,7 +453,7 @@ class Switch(ExternalInterface):
self
.
_ports
[
iface
.
control
.
index
]
=
iface
.
control
self
.
_ports
[
iface
.
control
.
index
]
=
iface
.
control
def
_check_port
(
self
,
port_index
):
def
_check_port
(
self
,
port_index
):
ports
=
ne
tns
.
iproute
.
get_bridge_data
()[
2
]
ports
=
ne
mu
.
iproute
.
get_bridge_data
()[
2
]
if
self
.
index
in
ports
and
port_index
in
ports
[
self
.
index
]:
if
self
.
index
in
ports
and
port_index
in
ports
[
self
.
index
]:
return
True
return
True
# else
# else
...
@@ -466,7 +466,7 @@ class Switch(ExternalInterface):
...
@@ -466,7 +466,7 @@ class Switch(ExternalInterface):
assert
iface
.
control
.
index
in
self
.
_ports
assert
iface
.
control
.
index
in
self
.
_ports
if
not
self
.
_check_port
(
iface
.
control
.
index
):
if
not
self
.
_check_port
(
iface
.
control
.
index
):
return
return
ne
tns
.
iproute
.
del_bridge_port
(
self
.
index
,
iface
.
control
.
index
)
ne
mu
.
iproute
.
del_bridge_port
(
self
.
index
,
iface
.
control
.
index
)
self
.
_apply_parameters
({},
iface
.
control
)
self
.
_apply_parameters
({},
iface
.
control
)
del
self
.
_ports
[
iface
.
control
.
index
]
del
self
.
_ports
[
iface
.
control
.
index
]
...
@@ -506,5 +506,5 @@ class Switch(ExternalInterface):
...
@@ -506,5 +506,5 @@ class Switch(ExternalInterface):
def
_apply_parameters
(
self
,
parameters
,
port
=
None
):
def
_apply_parameters
(
self
,
parameters
,
port
=
None
):
for
i
in
[
port
]
if
port
else
self
.
_ports
.
values
():
for
i
in
[
port
]
if
port
else
self
.
_ports
.
values
():
ne
tns
.
iproute
.
set_tc
(
i
.
index
,
**
parameters
)
ne
mu
.
iproute
.
set_tc
(
i
.
index
,
**
parameters
)
src/ne
tns
/iproute.py
→
src/ne
mu
/iproute.py
View file @
53c49b08
...
@@ -18,7 +18,7 @@
...
@@ -18,7 +18,7 @@
# Nemu. If not, see <http://www.gnu.org/licenses/>.
# Nemu. If not, see <http://www.gnu.org/licenses/>.
import
copy
,
fcntl
,
os
,
re
,
socket
,
struct
,
subprocess
,
sys
import
copy
,
fcntl
,
os
,
re
,
socket
,
struct
,
subprocess
,
sys
from
ne
tns
.environ
import
*
from
ne
mu
.environ
import
*
# helpers
# helpers
def
_any_to_bool
(
any
):
def
_any_to_bool
(
any
):
...
...
src/ne
tns
/node.py
→
src/ne
mu
/node.py
View file @
53c49b08
...
@@ -18,8 +18,8 @@
...
@@ -18,8 +18,8 @@
# Nemu. If not, see <http://www.gnu.org/licenses/>.
# Nemu. If not, see <http://www.gnu.org/licenses/>.
import
os
,
socket
,
sys
,
traceback
,
unshare
,
weakref
import
os
,
socket
,
sys
,
traceback
,
unshare
,
weakref
from
ne
tns
.environ
import
*
from
ne
mu
.environ
import
*
import
ne
tns.interface
,
netns
.
protocol
,
netns
.
subprocess_
import
ne
mu.interface
,
nemu
.
protocol
,
nemu
.
subprocess_
__all__
=
[
'Node'
,
'get_nodes'
,
'import_if'
]
__all__
=
[
'Node'
,
'get_nodes'
,
'import_if'
]
...
@@ -35,7 +35,7 @@ class Node(object):
...
@@ -35,7 +35,7 @@ class Node(object):
"""Create a new node in the emulation. Implemented as a separate
"""Create a new node in the emulation. Implemented as a separate
process in a new network name space. Requires root privileges to run.
process in a new network name space. Requires root privileges to run.
If
keep
ns is true, the network name space is not created and can be
If
nonet
ns is true, the network name space is not created and can be
run as a normal user, for testing."""
run as a normal user, for testing."""
# Initialize attributes, in case something fails during __init__
# Initialize attributes, in case something fails during __init__
...
@@ -47,7 +47,7 @@ class Node(object):
...
@@ -47,7 +47,7 @@ class Node(object):
fd
,
pid
=
_start_child
(
nonetns
)
fd
,
pid
=
_start_child
(
nonetns
)
self
.
_pid
=
pid
self
.
_pid
=
pid
debug
(
"Node(0x%x).__init__(), pid = %s"
%
(
id
(
self
),
pid
))
debug
(
"Node(0x%x).__init__(), pid = %s"
%
(
id
(
self
),
pid
))
self
.
_slave
=
ne
tns
.
protocol
.
Client
(
fd
,
fd
)
self
.
_slave
=
ne
mu
.
protocol
.
Client
(
fd
,
fd
)
if
forward_X11
:
if
forward_X11
:
self
.
_slave
.
enable_x11_forwarding
()
self
.
_slave
.
enable_x11_forwarding
()
...
@@ -93,44 +93,44 @@ class Node(object):
...
@@ -93,44 +93,44 @@ class Node(object):
self
.
_processes
[
subprocess
.
pid
]
=
subprocess
self
.
_processes
[
subprocess
.
pid
]
=
subprocess
def
Subprocess
(
self
,
*
kargs
,
**
kwargs
):
def
Subprocess
(
self
,
*
kargs
,
**
kwargs
):
return
ne
tns
.
subprocess_
.
Subprocess
(
self
,
*
kargs
,
**
kwargs
)
return
ne
mu
.
subprocess_
.
Subprocess
(
self
,
*
kargs
,
**
kwargs
)
def
Popen
(
self
,
*
kargs
,
**
kwargs
):
def
Popen
(
self
,
*
kargs
,
**
kwargs
):
return
ne
tns
.
subprocess_
.
Popen
(
self
,
*
kargs
,
**
kwargs
)
return
ne
mu
.
subprocess_
.
Popen
(
self
,
*
kargs
,
**
kwargs
)
def
system
(
self
,
*
kargs
,
**
kwargs
):
def
system
(
self
,
*
kargs
,
**
kwargs
):
return
ne
tns
.
subprocess_
.
system
(
self
,
*
kargs
,
**
kwargs
)
return
ne
mu
.
subprocess_
.
system
(
self
,
*
kargs
,
**
kwargs
)
def
backticks
(
self
,
*
kargs
,
**
kwargs
):
def
backticks
(
self
,
*
kargs
,
**
kwargs
):
return
ne
tns
.
subprocess_
.
backticks
(
self
,
*
kargs
,
**
kwargs
)
return
ne
mu
.
subprocess_
.
backticks
(
self
,
*
kargs
,
**
kwargs
)
def
backticks_raise
(
self
,
*
kargs
,
**
kwargs
):
def
backticks_raise
(
self
,
*
kargs
,
**
kwargs
):
return
ne
tns
.
subprocess_
.
backticks_raise
(
self
,
*
kargs
,
**
kwargs
)
return
ne
mu
.
subprocess_
.
backticks_raise
(
self
,
*
kargs
,
**
kwargs
)
# Interfaces
# Interfaces
def
_add_interface
(
self
,
interface
):
def
_add_interface
(
self
,
interface
):
self
.
_interfaces
[
interface
.
index
]
=
interface
self
.
_interfaces
[
interface
.
index
]
=
interface
def
add_if
(
self
,
**
kwargs
):
def
add_if
(
self
,
**
kwargs
):
i
=
ne
tns
.
interface
.
NodeInterface
(
self
)
i
=
ne
mu
.
interface
.
NodeInterface
(
self
)
for
k
,
v
in
kwargs
.
items
():
for
k
,
v
in
kwargs
.
items
():
setattr
(
i
,
k
,
v
)
setattr
(
i
,
k
,
v
)
return
i
return
i
def
add_tap
(
self
,
use_pi
=
False
,
**
kwargs
):
def
add_tap
(
self
,
use_pi
=
False
,
**
kwargs
):
i
=
ne
tns
.
interface
.
TapNodeInterface
(
self
,
use_pi
)
i
=
ne
mu
.
interface
.
TapNodeInterface
(
self
,
use_pi
)
for
k
,
v
in
kwargs
.
items
():
for
k
,
v
in
kwargs
.
items
():
setattr
(
i
,
k
,
v
)
setattr
(
i
,
k
,
v
)
return
i
return
i
def
add_tun
(
self
,
use_pi
=
False
,
**
kwargs
):
def
add_tun
(
self
,
use_pi
=
False
,
**
kwargs
):
i
=
ne
tns
.
interface
.
TunNodeInterface
(
self
,
use_pi
)
i
=
ne
mu
.
interface
.
TunNodeInterface
(
self
,
use_pi
)
for
k
,
v
in
kwargs
.
items
():
for
k
,
v
in
kwargs
.
items
():
setattr
(
i
,
k
,
v
)
setattr
(
i
,
k
,
v
)
return
i
return
i
def
import_if
(
self
,
interface
):
def
import_if
(
self
,
interface
):
return
ne
tns
.
interface
.
ImportedNodeInterface
(
self
,
interface
)
return
ne
mu
.
interface
.
ImportedNodeInterface
(
self
,
interface
)
def
del_if
(
self
,
iface
):
def
del_if
(
self
,
iface
):
"""Doesn't destroy the interface if it wasn't created by us."""
"""Doesn't destroy the interface if it wasn't created by us."""
...
@@ -146,7 +146,7 @@ class Node(object):
...
@@ -146,7 +146,7 @@ class Node(object):
ifaces
=
self
.
_slave
.
get_if_data
()
ifaces
=
self
.
_slave
.
get_if_data
()
for
i
in
ifaces
:
for
i
in
ifaces
:
if
i
not
in
self
.
_interfaces
:
if
i
not
in
self
.
_interfaces
:
iface
=
ne
tns
.
interface
.
ImportedNodeInterface
(
self
,
i
,
iface
=
ne
mu
.
interface
.
ImportedNodeInterface
(
self
,
i
,
migrate
=
False
)
migrate
=
False
)
self
.
_auto_interfaces
.
append
(
iface
)
# keep it referenced!
self
.
_auto_interfaces
.
append
(
iface
)
# keep it referenced!
self
.
_interfaces
[
i
]
=
iface
self
.
_interfaces
[
i
]
=
iface
...
@@ -161,7 +161,7 @@ class Node(object):
...
@@ -161,7 +161,7 @@ class Node(object):
def
route
(
self
,
tipe
=
'unicast'
,
prefix
=
None
,
prefix_len
=
0
,
def
route
(
self
,
tipe
=
'unicast'
,
prefix
=
None
,
prefix_len
=
0
,
nexthop
=
None
,
interface
=
None
,
metric
=
0
):
nexthop
=
None
,
interface
=
None
,
metric
=
0
):
return
ne
tns
.
iproute
.
route
(
tipe
,
prefix
,
prefix_len
,
nexthop
,
return
ne
mu
.
iproute
.
route
(
tipe
,
prefix
,
prefix_len
,
nexthop
,
interface
.
index
if
interface
else
None
,
metric
)
interface
.
index
if
interface
else
None
,
metric
)
def
add_route
(
self
,
*
args
,
**
kwargs
):
def
add_route
(
self
,
*
args
,
**
kwargs
):
...
@@ -197,7 +197,7 @@ def _start_child(nonetns):
...
@@ -197,7 +197,7 @@ def _start_child(nonetns):
# FIXME: clean up signal handers, atexit functions, etc.
# FIXME: clean up signal handers, atexit functions, etc.
try
:
try
:
s0
.
close
()
s0
.
close
()
srv
=
ne
tns
.
protocol
.
Server
(
s1
,
s1
)
srv
=
ne
mu
.
protocol
.
Server
(
s1
,
s1
)
if
not
nonetns
:
if
not
nonetns
:
# create new name space
# create new name space
unshare
.
unshare
(
unshare
.
CLONE_NEWNET
)
unshare
.
unshare
(
unshare
.
CLONE_NEWNET
)
...
@@ -222,4 +222,4 @@ def _start_child(nonetns):
...
@@ -222,4 +222,4 @@ def _start_child(nonetns):
# NOTREACHED
# NOTREACHED
get_nodes
=
Node
.
get_nodes
get_nodes
=
Node
.
get_nodes
import_if
=
ne
tns
.
interface
.
ImportedInterface
import_if
=
ne
mu
.
interface
.
ImportedInterface
src/ne
tns
/protocol.py
→
src/ne
mu
/protocol.py
View file @
53c49b08
...
@@ -19,8 +19,8 @@
...
@@ -19,8 +19,8 @@
import
base64
,
errno
,
os
,
passfd
,
re
,
select
,
signal
,
socket
,
sys
,
tempfile
import
base64
,
errno
,
os
,
passfd
,
re
,
select
,
signal
,
socket
,
sys
,
tempfile
import
time
,
traceback
,
unshare
import
time
,
traceback
,
unshare
import
ne
tns.subprocess_
,
netns
.
iproute
import
ne
mu.subprocess_
,
nemu
.
iproute
from
ne
tns
.environ
import
*
from
ne
mu
.environ
import
*
try
:
try
:
from
cPickle
import
loads
,
dumps
from
cPickle
import
loads
,
dumps
...
@@ -119,7 +119,7 @@ class Server(object):
...
@@ -119,7 +119,7 @@ class Server(object):
while
time
.
time
()
-
now
<
KILL_WAIT
:
while
time
.
time
()
-
now
<
KILL_WAIT
:
for
pid
in
ch
:
for
pid
in
ch
:
try
:
try
:
if
ne
tns
.
subprocess_
.
poll
(
pid
):
if
ne
mu
.
subprocess_
.
poll
(
pid
):
ch
.
remove
(
pid
)
ch
.
remove
(
pid
)
except
OSError
,
e
:
except
OSError
,
e
:
if
e
.
errno
==
errno
.
ECHILD
:
if
e
.
errno
==
errno
.
ECHILD
:
...
@@ -136,7 +136,7 @@ class Server(object):
...
@@ -136,7 +136,7 @@ class Server(object):
os
.
kill
(
-
pid
,
signal
.
SIGKILL
)
os
.
kill
(
-
pid
,
signal
.
SIGKILL
)
for
pid
in
ch
:
for
pid
in
ch
:
try
:
try
:
ne
tns
.
subprocess_
.
poll
(
pid
)
ne
mu
.
subprocess_
.
poll
(
pid
)
except
OSError
,
e
:
except
OSError
,
e
:
if
e
.
errno
!=
errno
.
ECHILD
:
if
e
.
errno
!=
errno
.
ECHILD
:
raise
raise
...
@@ -368,7 +368,7 @@ class Server(object):
...
@@ -368,7 +368,7 @@ class Server(object):
"%s/unix:%d"
%
(
socket
.
gethostname
(),
display
),
"%s/unix:%d"
%
(
socket
.
gethostname
(),
display
),
protoname
,
hexkey
])
protoname
,
hexkey
])
if
user
:
if
user
:
user
,
uid
,
gid
=
ne
tns
.
subprocess_
.
get_user
(
user
)
user
,
uid
,
gid
=
ne
mu
.
subprocess_
.
get_user
(
user
)
os
.
chown
(
xauth
,
uid
,
gid
)
os
.
chown
(
xauth
,
uid
,
gid
)
params
[
'env'
][
'DISPLAY'
]
=
"127.0.0.1:%d"
%
display
params
[
'env'
][
'DISPLAY'
]
=
"127.0.0.1:%d"
%
display
...
@@ -385,7 +385,7 @@ class Server(object):
...
@@ -385,7 +385,7 @@ class Server(object):
del
params
[
'env'
][
'DISPLAY'
]
del
params
[
'env'
][
'DISPLAY'
]
try
:
try
:
chld
=
ne
tns
.
subprocess_
.
spawn
(
**
params
)
chld
=
ne
mu
.
subprocess_
.
spawn
(
**
params
)
finally
:
finally
:
# I can close the fds now
# I can close the fds now
for
d
in
(
'stdin'
,
'stdout'
,
'stderr'
):
for
d
in
(
'stdin'
,
'stdout'
,
'stderr'
):
...
@@ -406,9 +406,9 @@ class Server(object):
...
@@ -406,9 +406,9 @@ class Server(object):
self
.
reply
(
500
,
"Process does not exist."
)
self
.
reply
(
500
,
"Process does not exist."
)
return
return
if
cmdname
==
'PROC POLL'
:
if
cmdname
==
'PROC POLL'
:
ret
=
ne
tns
.
subprocess_
.
poll
(
pid
)
ret
=
ne
mu
.
subprocess_
.
poll
(
pid
)
else
:
else
:
ret
=
ne
tns
.
subprocess_
.
wait
(
pid
)
ret
=
ne
mu
.
subprocess_
.
wait
(
pid
)
if
ret
!=
None
:
if
ret
!=
None
:
self
.
_children
.
remove
(
pid
)
self
.
_children
.
remove
(
pid
)
...
@@ -438,9 +438,9 @@ class Server(object):
...
@@ -438,9 +438,9 @@ class Server(object):
def
do_IF_LIST
(
self
,
cmdname
,
ifnr
=
None
):
def
do_IF_LIST
(
self
,
cmdname
,
ifnr
=
None
):
if
ifnr
==
None
:
if
ifnr
==
None
:
ifdata
=
ne
tns
.
iproute
.
get_if_data
()[
0
]
ifdata
=
ne
mu
.
iproute
.
get_if_data
()[
0
]
else
:
else
:
ifdata
=
ne
tns
.
iproute
.
get_if
(
ifnr
)
ifdata
=
ne
mu
.
iproute
.
get_if
(
ifnr
)
self
.
reply
(
200
,
[
"# Interface data follows."
,
self
.
reply
(
200
,
[
"# Interface data follows."
,
_b64
(
dumps
(
ifdata
,
protocol
=
2
))])
_b64
(
dumps
(
ifdata
,
protocol
=
2
))])
...
@@ -453,20 +453,20 @@ class Server(object):
...
@@ -453,20 +453,20 @@ class Server(object):
for
i
in
range
(
len
(
args
)
/
2
):
for
i
in
range
(
len
(
args
)
/
2
):
d
[
str
(
args
[
i
*
2
])]
=
args
[
i
*
2
+
1
]
d
[
str
(
args
[
i
*
2
])]
=
args
[
i
*
2
+
1
]
iface
=
ne
tns
.
iproute
.
interface
(
**
d
)
iface
=
ne
mu
.
iproute
.
interface
(
**
d
)
ne
tns
.
iproute
.
set_if
(
iface
)
ne
mu
.
iproute
.
set_if
(
iface
)
self
.
reply
(
200
,
"Done."
)
self
.
reply
(
200
,
"Done."
)
def
do_IF_RTRN
(
self
,
cmdname
,
ifnr
,
ns
):
def
do_IF_RTRN
(
self
,
cmdname
,
ifnr
,
ns
):
ne
tns
.
iproute
.
change_netns
(
ifnr
,
ns
)
ne
mu
.
iproute
.
change_netns
(
ifnr
,
ns
)
self
.
reply
(
200
,
"Done."
)
self
.
reply
(
200
,
"Done."
)
def
do_IF_DEL
(
self
,
cmdname
,
ifnr
):
def
do_IF_DEL
(
self
,
cmdname
,
ifnr
):
ne
tns
.
iproute
.
del_if
(
ifnr
)
ne
mu
.
iproute
.
del_if
(
ifnr
)
self
.
reply
(
200
,
"Done."
)
self
.
reply
(
200
,
"Done."
)
def
do_ADDR_LIST
(
self
,
cmdname
,
ifnr
=
None
):
def
do_ADDR_LIST
(
self
,
cmdname
,
ifnr
=
None
):
addrdata
=
ne
tns
.
iproute
.
get_addr_data
()[
0
]
addrdata
=
ne
mu
.
iproute
.
get_addr_data
()[
0
]
if
ifnr
!=
None
:
if
ifnr
!=
None
:
addrdata
=
addrdata
[
ifnr
]
addrdata
=
addrdata
[
ifnr
]
self
.
reply
(
200
,
[
"# Address data follows."
,
self
.
reply
(
200
,
[
"# Address data follows."
,
...
@@ -474,34 +474,34 @@ class Server(object):
...
@@ -474,34 +474,34 @@ class Server(object):
def
do_ADDR_ADD
(
self
,
cmdname
,
ifnr
,
address
,
prefixlen
,
broadcast
=
None
):
def
do_ADDR_ADD
(
self
,
cmdname
,
ifnr
,
address
,
prefixlen
,
broadcast
=
None
):
if
address
.
find
(
":"
)
<
0
:
# crude, I know
if
address
.
find
(
":"
)
<
0
:
# crude, I know
a
=
ne
tns
.
iproute
.
ipv4address
(
address
,
prefixlen
,
broadcast
)
a
=
ne
mu
.
iproute
.
ipv4address
(
address
,
prefixlen
,
broadcast
)
else
:
else
:
a
=
ne
tns
.
iproute
.
ipv6address
(
address
,
prefixlen
)
a
=
ne
mu
.
iproute
.
ipv6address
(
address
,
prefixlen
)
ne
tns
.
iproute
.
add_addr
(
ifnr
,
a
)
ne
mu
.
iproute
.
add_addr
(
ifnr
,
a
)
self
.
reply
(
200
,
"Done."
)
self
.
reply
(
200
,
"Done."
)
def
do_ADDR_DEL
(
self
,
cmdname
,
ifnr
,
address
,
prefixlen
):
def
do_ADDR_DEL
(
self
,
cmdname
,
ifnr
,
address
,
prefixlen
):
if
address
.
find
(
":"
)
<
0
:
# crude, I know
if
address
.
find
(
":"
)
<
0
:
# crude, I know
a
=
ne
tns
.
iproute
.
ipv4address
(
address
,
prefixlen
,
None
)
a
=
ne
mu
.
iproute
.
ipv4address
(
address
,
prefixlen
,
None
)
else
:
else
:
a
=
ne
tns
.
iproute
.
ipv6address
(
address
,
prefixlen
)
a
=
ne
mu
.
iproute
.
ipv6address
(
address
,
prefixlen
)
ne
tns
.
iproute
.
del_addr
(
ifnr
,
a
)
ne
mu
.
iproute
.
del_addr
(
ifnr
,
a
)
self
.
reply
(
200
,
"Done."
)
self
.
reply
(
200
,
"Done."
)
def
do_ROUT_LIST
(
self
,
cmdname
):
def
do_ROUT_LIST
(
self
,
cmdname
):
rdata
=
ne
tns
.
iproute
.
get_route_data
()
rdata
=
ne
mu
.
iproute
.
get_route_data
()
self
.
reply
(
200
,
[
"# Routing data follows."
,
self
.
reply
(
200
,
[
"# Routing data follows."
,
_b64
(
dumps
(
rdata
,
protocol
=
2
))])
_b64
(
dumps
(
rdata
,
protocol
=
2
))])
def
do_ROUT_ADD
(
self
,
cmdname
,
tipe
,
prefix
,
prefixlen
,
nexthop
,
ifnr
,
def
do_ROUT_ADD
(
self
,
cmdname
,
tipe
,
prefix
,
prefixlen
,
nexthop
,
ifnr
,
metric
):
metric
):
ne
tns
.
iproute
.
add_route
(
netns
.
iproute
.
route
(
tipe
,
prefix
,
prefixlen
,
ne
mu
.
iproute
.
add_route
(
nemu
.
iproute
.
route
(
tipe
,
prefix
,
prefixlen
,
nexthop
,
ifnr
or
None
,
metric
))
nexthop
,
ifnr
or
None
,
metric
))
self
.
reply
(
200
,
"Done."
)
self
.
reply
(
200
,
"Done."
)
def
do_ROUT_DEL
(
self
,
cmdname
,
tipe
,
prefix
,
prefixlen
,
nexthop
,
ifnr
,
def
do_ROUT_DEL
(
self
,
cmdname
,
tipe
,
prefix
,
prefixlen
,
nexthop
,
ifnr
,
metric
):
metric
):
ne
tns
.
iproute
.
del_route
(
netns
.
iproute
.
route
(
tipe
,
prefix
,
prefixlen
,
ne
mu
.
iproute
.
del_route
(
nemu
.
iproute
.
route
(
tipe
,
prefix
,
prefixlen
,
nexthop
,
ifnr
or
None
,
metric
))
nexthop
,
ifnr
or
None
,
metric
))
self
.
reply
(
200
,
"Done."
)
self
.
reply
(
200
,
"Done."
)
...
@@ -631,7 +631,7 @@ class Client(object):
...
@@ -631,7 +631,7 @@ class Client(object):
"""Start a subprocess in the slave; the interface resembles
"""Start a subprocess in the slave; the interface resembles
subprocess.Popen, but with less functionality. In particular
subprocess.Popen, but with less functionality. In particular
stdin/stdout/stderr can only be None or a open file descriptor.
stdin/stdout/stderr can only be None or a open file descriptor.
See ne
tns
.subprocess_.spawn for details."""
See ne
mu
.subprocess_.spawn for details."""
if
executable
==
None
:
if
executable
==
None
:
executable
=
argv
[
0
]
executable
=
argv
[
0
]
...
...
src/ne
tns
/subprocess_.py
→
src/ne
mu
/subprocess_.py
View file @
53c49b08
...
@@ -18,7 +18,7 @@
...
@@ -18,7 +18,7 @@
# Nemu. If not, see <http://www.gnu.org/licenses/>.
# Nemu. If not, see <http://www.gnu.org/licenses/>.
import
fcntl
,
grp
,
os
,
pickle
,
pwd
,
signal
,
select
,
sys
,
time
,
traceback
import
fcntl
,
grp
,
os
,
pickle
,
pwd
,
signal
,
select
,
sys
,
time
,
traceback
from
ne
tns
.environ
import
eintr_wrapper
from
ne
mu
.environ
import
eintr_wrapper
__all__
=
[
'PIPE'
,
'STDOUT'
,
'Popen'
,
'Subprocess'
,
'spawn'
,
'wait'
,
'poll'
,
__all__
=
[
'PIPE'
,
'STDOUT'
,
'Popen'
,
'Subprocess'
,
'spawn'
,
'wait'
,
'poll'
,
'get_user'
,
'system'
,
'backticks'
,
'backticks_raise'
]
'get_user'
,
'system'
,
'backticks'
,
'backticks_raise'
]
...
@@ -28,7 +28,7 @@ __all__ = [ 'PIPE', 'STDOUT', 'Popen', 'Subprocess', 'spawn', 'wait', 'poll',
...
@@ -28,7 +28,7 @@ __all__ = [ 'PIPE', 'STDOUT', 'Popen', 'Subprocess', 'spawn', 'wait', 'poll',
KILL_WAIT
=
3
# seconds
KILL_WAIT
=
3
# seconds
class
Subprocess
(
object
):
class
Subprocess
(
object
):
"""Class that allows the execution of programs inside a ne
tns
Node. This is
"""Class that allows the execution of programs inside a ne
mu
Node. This is
the base class for all process operations, Popen provides a more high level
the base class for all process operations, Popen provides a more high level
interface."""
interface."""
# FIXME
# FIXME
...
@@ -40,7 +40,7 @@ class Subprocess(object):
...
@@ -40,7 +40,7 @@ class Subprocess(object):
"""Forks and execs a program, with stdio redirection and user
"""Forks and execs a program, with stdio redirection and user
switching.
switching.
A ne
tns
Node to run the program is is specified as the first parameter.
A ne
mu
Node to run the program is is specified as the first parameter.
The program is specified by `executable', if it does not contain any
The program is specified by `executable', if it does not contain any
slash, the PATH environment variable is used to search for the file.
slash, the PATH environment variable is used to search for the file.
...
@@ -148,7 +148,7 @@ class Popen(Subprocess):
...
@@ -148,7 +148,7 @@ class Popen(Subprocess):
def
__init__
(
self
,
node
,
argv
,
executable
=
None
,
def
__init__
(
self
,
node
,
argv
,
executable
=
None
,
stdin
=
None
,
stdout
=
None
,
stderr
=
None
,
bufsize
=
0
,
stdin
=
None
,
stdout
=
None
,
stderr
=
None
,
bufsize
=
0
,
shell
=
False
,
cwd
=
None
,
env
=
None
,
user
=
None
):
shell
=
False
,
cwd
=
None
,
env
=
None
,
user
=
None
):
"""As in Subprocess, `node' specifies the ne
tns
Node to run in.
"""As in Subprocess, `node' specifies the ne
mu
Node to run in.
The `stdin', `stdout', and `stderr' parameters also accept the special
The `stdin', `stdout', and `stderr' parameters also accept the special
values subprocess.PIPE or subprocess.STDOUT. Check the stdlib's
values subprocess.PIPE or subprocess.STDOUT. Check the stdlib's
...
@@ -267,7 +267,7 @@ def backticks_raise(node, args):
...
@@ -267,7 +267,7 @@ def backticks_raise(node, args):
# =======================================================================
# =======================================================================
#
#
# Server-side code, called from ne
tns
.protocol.Server
# Server-side code, called from ne
mu
.protocol.Server
def
spawn
(
executable
,
argv
=
None
,
cwd
=
None
,
env
=
None
,
close_fds
=
False
,
def
spawn
(
executable
,
argv
=
None
,
cwd
=
None
,
env
=
None
,
close_fds
=
False
,
stdin
=
None
,
stdout
=
None
,
stderr
=
None
,
user
=
None
):
stdin
=
None
,
stdout
=
None
,
stderr
=
None
,
user
=
None
):
...
...
test/test_core.py
View file @
53c49b08
...
@@ -2,27 +2,27 @@
...
@@ -2,27 +2,27 @@
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
import
grp
,
os
,
pwd
,
select
,
time
,
unittest
import
grp
,
os
,
pwd
,
select
,
time
,
unittest
import
ne
tns
,
test_util
import
ne
mu
,
test_util
class
TestConfigure
(
unittest
.
TestCase
):
class
TestConfigure
(
unittest
.
TestCase
):
def
test_config_run_as_static
(
self
):
def
test_config_run_as_static
(
self
):
# Don't allow root as default user
# Don't allow root as default user
self
.
assertRaises
(
AttributeError
,
setattr
,
ne
tns
.
config
,
self
.
assertRaises
(
AttributeError
,
setattr
,
ne
mu
.
config
,
'run_as'
,
'root'
)
'run_as'
,
'root'
)
self
.
assertRaises
(
AttributeError
,
setattr
,
ne
tns
.
config
,
self
.
assertRaises
(
AttributeError
,
setattr
,
ne
mu
.
config
,
'run_as'
,
0
)
'run_as'
,
0
)
# Don't allow invalid users
# Don't allow invalid users
self
.
assertRaises
(
AttributeError
,
setattr
,
ne
tns
.
config
,
self
.
assertRaises
(
AttributeError
,
setattr
,
ne
mu
.
config
,
'run_as'
,
'foobarbaz'
)
# hope nobody has this user!
'run_as'
,
'foobarbaz'
)
# hope nobody has this user!
self
.
assertRaises
(
AttributeError
,
setattr
,
ne
tns
.
config
,
self
.
assertRaises
(
AttributeError
,
setattr
,
ne
mu
.
config
,
'run_as'
,
-
1
)
'run_as'
,
-
1
)
class
TestGlobal
(
unittest
.
TestCase
):
class
TestGlobal
(
unittest
.
TestCase
):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_run_ping_p2pif
(
self
):
def
test_run_ping_p2pif
(
self
):
n1
=
ne
tns
.
Node
()
n1
=
ne
mu
.
Node
()
n2
=
ne
tns
.
Node
()
n2
=
ne
mu
.
Node
()
i1
,
i2
=
ne
tns
.
P2PInterface
.
create_pair
(
n1
,
n2
)
i1
,
i2
=
ne
mu
.
P2PInterface
.
create_pair
(
n1
,
n2
)
i1
.
up
=
i2
.
up
=
True
i1
.
up
=
i2
.
up
=
True
i1
.
lladdr
=
'd6:4b:3f:f7:ff:7e'
i1
.
lladdr
=
'd6:4b:3f:f7:ff:7e'
i2
.
lladdr
=
'd6:4b:3f:f7:ff:7f'
i2
.
lladdr
=
'd6:4b:3f:f7:ff:7f'
...
@@ -46,12 +46,12 @@ class TestGlobal(unittest.TestCase):
...
@@ -46,12 +46,12 @@ class TestGlobal(unittest.TestCase):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_run_ping_node_if
(
self
):
def
test_run_ping_node_if
(
self
):
n1
=
ne
tns
.
Node
()
n1
=
ne
mu
.
Node
()
n2
=
ne
tns
.
Node
()
n2
=
ne
mu
.
Node
()
i1
=
n1
.
add_if
()
i1
=
n1
.
add_if
()
i2
=
n2
.
add_if
()
i2
=
n2
.
add_if
()
i1
.
up
=
i2
.
up
=
True
i1
.
up
=
i2
.
up
=
True
l
=
ne
tns
.
Switch
()
l
=
ne
mu
.
Switch
()
l
.
connect
(
i1
)
l
.
connect
(
i1
)
l
.
connect
(
i2
)
l
.
connect
(
i2
)
l
.
up
=
True
l
.
up
=
True
...
@@ -66,11 +66,11 @@ class TestGlobal(unittest.TestCase):
...
@@ -66,11 +66,11 @@ class TestGlobal(unittest.TestCase):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_run_ping_routing_p2p
(
self
):
def
test_run_ping_routing_p2p
(
self
):
n1
=
ne
tns
.
Node
()
n1
=
ne
mu
.
Node
()
n2
=
ne
tns
.
Node
()
n2
=
ne
mu
.
Node
()
n3
=
ne
tns
.
Node
()
n3
=
ne
mu
.
Node
()
i12
,
i21
=
ne
tns
.
P2PInterface
.
create_pair
(
n1
,
n2
)
i12
,
i21
=
ne
mu
.
P2PInterface
.
create_pair
(
n1
,
n2
)
i23
,
i32
=
ne
tns
.
P2PInterface
.
create_pair
(
n2
,
n3
)
i23
,
i32
=
ne
mu
.
P2PInterface
.
create_pair
(
n2
,
n3
)
i12
.
up
=
i21
.
up
=
i23
.
up
=
i32
.
up
=
True
i12
.
up
=
i21
.
up
=
i23
.
up
=
i32
.
up
=
True
i12
.
add_v4_address
(
'10.0.0.1'
,
24
)
i12
.
add_v4_address
(
'10.0.0.1'
,
24
)
i21
.
add_v4_address
(
'10.0.0.2'
,
24
)
i21
.
add_v4_address
(
'10.0.0.2'
,
24
)
...
@@ -90,16 +90,16 @@ class TestGlobal(unittest.TestCase):
...
@@ -90,16 +90,16 @@ class TestGlobal(unittest.TestCase):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_run_ping_routing
(
self
):
def
test_run_ping_routing
(
self
):
n1
=
ne
tns
.
Node
()
n1
=
ne
mu
.
Node
()
n2
=
ne
tns
.
Node
()
n2
=
ne
mu
.
Node
()
n3
=
ne
tns
.
Node
()
n3
=
ne
mu
.
Node
()
i1
=
n1
.
add_if
()
i1
=
n1
.
add_if
()
i2a
=
n2
.
add_if
()
i2a
=
n2
.
add_if
()
i2b
=
n2
.
add_if
()
i2b
=
n2
.
add_if
()
i3
=
n3
.
add_if
()
i3
=
n3
.
add_if
()
i1
.
up
=
i2a
.
up
=
i2b
.
up
=
i3
.
up
=
True
i1
.
up
=
i2a
.
up
=
i2b
.
up
=
i3
.
up
=
True
l1
=
ne
tns
.
Switch
()
l1
=
ne
mu
.
Switch
()
l2
=
ne
tns
.
Switch
()
l2
=
ne
mu
.
Switch
()
l1
.
connect
(
i1
)
l1
.
connect
(
i1
)
l1
.
connect
(
i2a
)
l1
.
connect
(
i2a
)
l2
.
connect
(
i2b
)
l2
.
connect
(
i2b
)
...
@@ -125,8 +125,8 @@ class TestGlobal(unittest.TestCase):
...
@@ -125,8 +125,8 @@ class TestGlobal(unittest.TestCase):
def
test_run_ping_tap
(
self
):
def
test_run_ping_tap
(
self
):
"""This test simulates a point to point connection between two hosts
"""This test simulates a point to point connection between two hosts
using two tap devices"""
using two tap devices"""
n1
=
ne
tns
.
Node
()
n1
=
ne
mu
.
Node
()
n2
=
ne
tns
.
Node
()
n2
=
ne
mu
.
Node
()
tap1
=
n1
.
add_tap
()
tap1
=
n1
.
add_tap
()
tap2
=
n2
.
add_tap
()
tap2
=
n2
.
add_tap
()
...
@@ -157,10 +157,10 @@ class TestGlobal(unittest.TestCase):
...
@@ -157,10 +157,10 @@ class TestGlobal(unittest.TestCase):
def
test_run_ping_tap_routing
(
self
):
def
test_run_ping_tap_routing
(
self
):
"""This test simulates a point to point connection between two hosts
"""This test simulates a point to point connection between two hosts
using two tap devices"""
using two tap devices"""
n1
=
ne
tns
.
Node
()
n1
=
ne
mu
.
Node
()
n2
=
ne
tns
.
Node
()
n2
=
ne
mu
.
Node
()
n3
=
ne
tns
.
Node
()
n3
=
ne
mu
.
Node
()
n4
=
ne
tns
.
Node
()
n4
=
ne
mu
.
Node
()
i1
=
n1
.
add_if
()
i1
=
n1
.
add_if
()
i2
=
n2
.
add_if
()
i2
=
n2
.
add_if
()
...
@@ -171,8 +171,8 @@ class TestGlobal(unittest.TestCase):
...
@@ -171,8 +171,8 @@ class TestGlobal(unittest.TestCase):
i1
.
up
=
i2
.
up
=
tap1
.
up
=
tap2
.
up
=
i3
.
up
=
i4
.
up
=
True
i1
.
up
=
i2
.
up
=
tap1
.
up
=
tap2
.
up
=
i3
.
up
=
i4
.
up
=
True
l1
=
ne
tns
.
Switch
()
l1
=
ne
mu
.
Switch
()
l2
=
ne
tns
.
Switch
()
l2
=
ne
mu
.
Switch
()
l1
.
connect
(
i1
)
l1
.
connect
(
i1
)
l1
.
connect
(
i2
)
l1
.
connect
(
i2
)
...
@@ -215,26 +215,26 @@ class TestGlobal(unittest.TestCase):
...
@@ -215,26 +215,26 @@ class TestGlobal(unittest.TestCase):
class
TestX11
(
unittest
.
TestCase
):
class
TestX11
(
unittest
.
TestCase
):
@
test_util
.
skipUnless
(
"DISPLAY"
in
os
.
environ
,
"Test requires working X11"
)
@
test_util
.
skipUnless
(
"DISPLAY"
in
os
.
environ
,
"Test requires working X11"
)
@
test_util
.
skipUnless
(
ne
tns
.
environ
.
xdpyinfo_path
,
"Test requires xdpyinfo"
)
@
test_util
.
skipUnless
(
ne
mu
.
environ
.
xdpyinfo_path
,
"Test requires xdpyinfo"
)
def
test_run_xdpyinfo
(
self
):
def
test_run_xdpyinfo
(
self
):
xdpy
=
ne
tns
.
environ
.
xdpyinfo_path
xdpy
=
ne
mu
.
environ
.
xdpyinfo_path
info
=
ne
tns
.
environ
.
backticks
([
xdpy
])
info
=
ne
mu
.
environ
.
backticks
([
xdpy
])
# remove first line, contains the display name
# remove first line, contains the display name
info
=
info
.
partition
(
"
\
n
"
)[
2
]
info
=
info
.
partition
(
"
\
n
"
)[
2
]
n
=
ne
tns
.
Node
(
nonetns
=
True
,
forward_X11
=
True
)
n
=
ne
mu
.
Node
(
nonetns
=
True
,
forward_X11
=
True
)
info2
=
n
.
backticks
([
xdpy
])
info2
=
n
.
backticks
([
xdpy
])
info2
=
info2
.
partition
(
"
\
n
"
)[
2
]
info2
=
info2
.
partition
(
"
\
n
"
)[
2
]
self
.
assertEquals
(
info
,
info2
)
self
.
assertEquals
(
info
,
info2
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
"DISPLAY"
in
os
.
environ
,
"Test requires working X11"
)
@
test_util
.
skipUnless
(
"DISPLAY"
in
os
.
environ
,
"Test requires working X11"
)
@
test_util
.
skipUnless
(
ne
tns
.
environ
.
xdpyinfo_path
,
"Test requires xdpyinfo"
)
@
test_util
.
skipUnless
(
ne
mu
.
environ
.
xdpyinfo_path
,
"Test requires xdpyinfo"
)
def
test_run_xdpyinfo_netns
(
self
):
def
test_run_xdpyinfo_netns
(
self
):
xdpy
=
ne
tns
.
environ
.
xdpyinfo_path
xdpy
=
ne
mu
.
environ
.
xdpyinfo_path
info
=
ne
tns
.
environ
.
backticks
([
xdpy
])
info
=
ne
mu
.
environ
.
backticks
([
xdpy
])
# remove first line, contains the display name
# remove first line, contains the display name
info
=
info
.
partition
(
"
\
n
"
)[
2
]
info
=
info
.
partition
(
"
\
n
"
)[
2
]
n
=
ne
tns
.
Node
(
forward_X11
=
True
)
n
=
ne
mu
.
Node
(
forward_X11
=
True
)
info2
=
n
.
backticks
([
xdpy
])
info2
=
n
.
backticks
([
xdpy
])
info2
=
info2
.
partition
(
"
\
n
"
)[
2
]
info2
=
info2
.
partition
(
"
\
n
"
)[
2
]
self
.
assertEquals
(
info
,
info2
)
self
.
assertEquals
(
info
,
info2
)
...
...
test/test_interfaces.py
View file @
53c49b08
...
@@ -2,8 +2,8 @@
...
@@ -2,8 +2,8 @@
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
from
test_util
import
get_devs
,
get_devs_netns
from
test_util
import
get_devs
,
get_devs_netns
from
ne
tns
.environ
import
*
from
ne
mu
.environ
import
*
import
ne
tns
,
test_util
import
ne
mu
,
test_util
import
os
,
unittest
import
os
,
unittest
class
TestUtils
(
unittest
.
TestCase
):
class
TestUtils
(
unittest
.
TestCase
):
...
@@ -22,7 +22,7 @@ class TestUtils(unittest.TestCase):
...
@@ -22,7 +22,7 @@ class TestUtils(unittest.TestCase):
class
TestInterfaces
(
unittest
.
TestCase
):
class
TestInterfaces
(
unittest
.
TestCase
):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_interface_creation
(
self
):
def
test_interface_creation
(
self
):
node0
=
ne
tns
.
Node
()
node0
=
ne
mu
.
Node
()
ifaces
=
[]
ifaces
=
[]
for
i
in
range
(
5
):
for
i
in
range
(
5
):
ifaces
.
append
(
node0
.
add_if
())
ifaces
.
append
(
node0
.
add_if
())
...
@@ -40,12 +40,12 @@ class TestInterfaces(unittest.TestCase):
...
@@ -40,12 +40,12 @@ class TestInterfaces(unittest.TestCase):
devs
=
get_devs
()
devs
=
get_devs
()
for
i
in
range
(
5
):
for
i
in
range
(
5
):
peer_name
=
ne
tns
.
iproute
.
get_if
(
ifaces
[
i
].
control
.
index
).
name
peer_name
=
ne
mu
.
iproute
.
get_if
(
ifaces
[
i
].
control
.
index
).
name
self
.
assertTrue
(
peer_name
in
devs
)
self
.
assertTrue
(
peer_name
in
devs
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_interface_settings
(
self
):
def
test_interface_settings
(
self
):
node0
=
ne
tns
.
Node
()
node0
=
ne
mu
.
Node
()
if0
=
node0
.
add_if
(
lladdr
=
'42:71:e0:90:ca:42'
,
mtu
=
1492
)
if0
=
node0
.
add_if
(
lladdr
=
'42:71:e0:90:ca:42'
,
mtu
=
1492
)
self
.
assertEquals
(
if0
.
lladdr
,
'42:71:e0:90:ca:42'
,
self
.
assertEquals
(
if0
.
lladdr
,
'42:71:e0:90:ca:42'
,
"Constructor parameters"
)
"Constructor parameters"
)
...
@@ -90,7 +90,7 @@ class TestInterfaces(unittest.TestCase):
...
@@ -90,7 +90,7 @@ class TestInterfaces(unittest.TestCase):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_interface_addresses
(
self
):
def
test_interface_addresses
(
self
):
node0
=
ne
tns
.
Node
()
node0
=
ne
mu
.
Node
()
if0
=
node0
.
add_if
()
if0
=
node0
.
add_if
()
if0
.
add_v4_address
(
address
=
'10.0.0.1'
,
prefix_len
=
24
,
if0
.
add_v4_address
(
address
=
'10.0.0.1'
,
prefix_len
=
24
,
broadcast
=
'10.0.0.255'
)
broadcast
=
'10.0.0.255'
)
...
@@ -124,7 +124,7 @@ class TestWithDummy(unittest.TestCase):
...
@@ -124,7 +124,7 @@ class TestWithDummy(unittest.TestCase):
test_util
.
get_linux_ver
()
>=
test_util
.
make_linux_ver
(
"2.6.35"
),
test_util
.
get_linux_ver
()
>=
test_util
.
make_linux_ver
(
"2.6.35"
),
"Test trigger a kernel bug on 2.6.34"
)
"Test trigger a kernel bug on 2.6.34"
)
def
test_interface_migration
(
self
):
def
test_interface_migration
(
self
):
node
=
ne
tns
.
Node
()
node
=
ne
mu
.
Node
()
self
.
dummyname
=
"dummy%d"
%
os
.
getpid
()
self
.
dummyname
=
"dummy%d"
%
os
.
getpid
()
self
.
assertEquals
(
os
.
system
(
"%s link add name %s type dummy"
%
self
.
assertEquals
(
os
.
system
(
"%s link add name %s type dummy"
%
(
ip_path
,
self
.
dummyname
)),
0
)
(
ip_path
,
self
.
dummyname
)),
0
)
...
...
test/test_node.py
View file @
53c49b08
#!/usr/bin/env python
#!/usr/bin/env python
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
import
ne
tns
,
netns
.
environ
,
test_util
import
ne
mu
,
nemu
.
environ
,
test_util
import
os
,
signal
,
subprocess
,
sys
,
time
import
os
,
signal
,
subprocess
,
sys
,
time
import
unittest
import
unittest
class
TestNode
(
unittest
.
TestCase
):
class
TestNode
(
unittest
.
TestCase
):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_node
(
self
):
def
test_node
(
self
):
node
=
ne
tns
.
Node
()
node
=
ne
mu
.
Node
()
self
.
failIfEqual
(
node
.
pid
,
os
.
getpid
())
self
.
failIfEqual
(
node
.
pid
,
os
.
getpid
())
self
.
failIfEqual
(
node
.
pid
,
None
)
self
.
failIfEqual
(
node
.
pid
,
None
)
# check if it really exists
# check if it really exists
os
.
kill
(
node
.
pid
,
0
)
os
.
kill
(
node
.
pid
,
0
)
nodes
=
ne
tns
.
get_nodes
()
nodes
=
ne
mu
.
get_nodes
()
self
.
assertEquals
(
nodes
,
[
node
])
self
.
assertEquals
(
nodes
,
[
node
])
self
.
assertTrue
(
node
.
get_interface
(
"lo"
).
up
)
self
.
assertTrue
(
node
.
get_interface
(
"lo"
).
up
)
@
test_util
.
skip
(
"Not implemented"
)
@
test_util
.
skip
(
"Not implemented"
)
def
test_detect_fork
(
self
):
def
test_detect_fork
(
self
):
# Test that ne
tns
recognises a fork
# Test that ne
mu
recognises a fork
chld
=
os
.
fork
()
chld
=
os
.
fork
()
if
chld
==
0
:
if
chld
==
0
:
if
len
(
ne
tns
.
get_nodes
())
==
0
:
if
len
(
ne
mu
.
get_nodes
())
==
0
:
os
.
_exit
(
0
)
os
.
_exit
(
0
)
os
.
_exit
(
1
)
os
.
_exit
(
1
)
(
pid
,
exitcode
)
=
os
.
waitpid
(
chld
,
0
)
(
pid
,
exitcode
)
=
os
.
waitpid
(
chld
,
0
)
...
@@ -33,25 +33,25 @@ class TestNode(unittest.TestCase):
...
@@ -33,25 +33,25 @@ class TestNode(unittest.TestCase):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_cleanup
(
self
):
def
test_cleanup
(
self
):
def
create_stuff
():
def
create_stuff
():
a
=
ne
tns
.
Node
()
a
=
ne
mu
.
Node
()
b
=
ne
tns
.
Node
()
b
=
ne
mu
.
Node
()
ifa
=
a
.
add_if
()
ifa
=
a
.
add_if
()
ifb
=
b
.
add_if
()
ifb
=
b
.
add_if
()
switch
=
ne
tns
.
Switch
()
switch
=
ne
mu
.
Switch
()
switch
.
connect
(
ifa
)
switch
.
connect
(
ifa
)
switch
.
connect
(
ifb
)
switch
.
connect
(
ifb
)
# Test automatic destruction
# Test automatic destruction
orig_devs
=
len
(
test_util
.
get_devs
())
orig_devs
=
len
(
test_util
.
get_devs
())
create_stuff
()
create_stuff
()
self
.
assertEquals
(
ne
tns
.
get_nodes
(),
[])
self
.
assertEquals
(
ne
mu
.
get_nodes
(),
[])
self
.
assertEquals
(
orig_devs
,
len
(
test_util
.
get_devs
()))
self
.
assertEquals
(
orig_devs
,
len
(
test_util
.
get_devs
()))
# Test at_exit hooks
# Test at_exit hooks
orig_devs
=
len
(
test_util
.
get_devs
())
orig_devs
=
len
(
test_util
.
get_devs
())
chld
=
os
.
fork
()
chld
=
os
.
fork
()
if
chld
==
0
:
if
chld
==
0
:
ne
tns
.
set_cleanup_hooks
(
on_exit
=
True
,
on_signals
=
[])
ne
mu
.
set_cleanup_hooks
(
on_exit
=
True
,
on_signals
=
[])
create_stuff
()
create_stuff
()
os
.
_exit
(
0
)
os
.
_exit
(
0
)
os
.
waitpid
(
chld
,
0
)
os
.
waitpid
(
chld
,
0
)
...
@@ -61,7 +61,7 @@ class TestNode(unittest.TestCase):
...
@@ -61,7 +61,7 @@ class TestNode(unittest.TestCase):
orig_devs
=
len
(
test_util
.
get_devs
())
orig_devs
=
len
(
test_util
.
get_devs
())
chld
=
os
.
fork
()
chld
=
os
.
fork
()
if
chld
==
0
:
if
chld
==
0
:
ne
tns
.
set_cleanup_hooks
(
on_exit
=
False
,
ne
mu
.
set_cleanup_hooks
(
on_exit
=
False
,
on_signals
=
[
signal
.
SIGTERM
])
on_signals
=
[
signal
.
SIGTERM
])
create_stuff
()
create_stuff
()
while
True
:
while
True
:
...
...
test/test_protocol.py
View file @
53c49b08
#!/usr/bin/env python
#!/usr/bin/env python
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
import
ne
tns
.protocol
import
ne
mu
.protocol
import
os
,
socket
,
sys
,
threading
,
unittest
import
os
,
socket
,
sys
,
threading
,
unittest
class
TestServer
(
unittest
.
TestCase
):
class
TestServer
(
unittest
.
TestCase
):
...
@@ -22,10 +22,10 @@ class TestServer(unittest.TestCase):
...
@@ -22,10 +22,10 @@ class TestServer(unittest.TestCase):
break
break
def
run_server
():
def
run_server
():
srv
=
ne
tns
.
protocol
.
Server
(
s0
,
s0
)
srv
=
ne
mu
.
protocol
.
Server
(
s0
,
s0
)
srv
.
run
()
srv
.
run
()
srv
=
ne
tns
.
protocol
.
Server
(
s2
.
fileno
(),
s2
.
fileno
())
srv
=
ne
mu
.
protocol
.
Server
(
s2
.
fileno
(),
s2
.
fileno
())
srv
.
run
()
srv
.
run
()
t
=
threading
.
Thread
(
target
=
run_server
)
t
=
threading
.
Thread
(
target
=
run_server
)
t
.
start
()
t
.
start
()
...
@@ -47,11 +47,11 @@ class TestServer(unittest.TestCase):
...
@@ -47,11 +47,11 @@ class TestServer(unittest.TestCase):
(
s0
,
s1
)
=
socket
.
socketpair
(
socket
.
AF_UNIX
,
socket
.
SOCK_STREAM
,
0
)
(
s0
,
s1
)
=
socket
.
socketpair
(
socket
.
AF_UNIX
,
socket
.
SOCK_STREAM
,
0
)
def
run_server
():
def
run_server
():
ne
tns
.
protocol
.
Server
(
s0
,
s0
).
run
()
ne
mu
.
protocol
.
Server
(
s0
,
s0
).
run
()
t
=
threading
.
Thread
(
target
=
run_server
)
t
=
threading
.
Thread
(
target
=
run_server
)
t
.
start
()
t
.
start
()
cli
=
ne
tns
.
protocol
.
Client
(
s1
,
s1
)
cli
=
ne
mu
.
protocol
.
Client
(
s1
,
s1
)
# make PROC SIN fail
# make PROC SIN fail
self
.
assertRaises
(
OSError
,
cli
.
spawn
,
"/bin/true"
,
stdin
=
-
1
)
self
.
assertRaises
(
OSError
,
cli
.
spawn
,
"/bin/true"
,
stdin
=
-
1
)
...
@@ -69,7 +69,7 @@ class TestServer(unittest.TestCase):
...
@@ -69,7 +69,7 @@ class TestServer(unittest.TestCase):
def
test_basic_stuff
(
self
):
def
test_basic_stuff
(
self
):
(
s0
,
s1
)
=
socket
.
socketpair
(
socket
.
AF_UNIX
,
socket
.
SOCK_STREAM
,
0
)
(
s0
,
s1
)
=
socket
.
socketpair
(
socket
.
AF_UNIX
,
socket
.
SOCK_STREAM
,
0
)
srv
=
ne
tns
.
protocol
.
Server
(
s0
,
s0
)
srv
=
ne
mu
.
protocol
.
Server
(
s0
,
s0
)
s1
=
s1
.
makefile
(
"r+"
,
1
)
s1
=
s1
.
makefile
(
"r+"
,
1
)
def
check_error
(
self
,
cmd
,
code
=
500
):
def
check_error
(
self
,
cmd
,
code
=
500
):
...
@@ -120,7 +120,7 @@ class TestServer(unittest.TestCase):
...
@@ -120,7 +120,7 @@ class TestServer(unittest.TestCase):
check_error
(
self
,
"proc crte =a"
)
# invalid b64
check_error
(
self
,
"proc crte =a"
)
# invalid b64
# simulate proc mode
# simulate proc mode
srv
.
_commands
=
ne
tns
.
protocol
.
_proc_commands
srv
.
_commands
=
ne
mu
.
protocol
.
_proc_commands
check_error
(
self
,
"proc crte foo"
)
check_error
(
self
,
"proc crte foo"
)
check_error
(
self
,
"proc poll 0"
)
check_error
(
self
,
"proc poll 0"
)
check_error
(
self
,
"proc wait 0"
)
check_error
(
self
,
"proc wait 0"
)
...
...
test/test_routing.py
View file @
53c49b08
#!/usr/bin/env python
#!/usr/bin/env python
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
import
ne
tns
,
test_util
import
ne
mu
,
test_util
import
os
,
unittest
import
os
,
unittest
class
TestRouting
(
unittest
.
TestCase
):
class
TestRouting
(
unittest
.
TestCase
):
@
test_util
.
skip
(
"Programatic detection of duplicate routes not implemented"
)
@
test_util
.
skip
(
"Programatic detection of duplicate routes not implemented"
)
def
test_base_routing
(
self
):
def
test_base_routing
(
self
):
node
=
ne
tns
.
Node
(
nonetns
=
True
)
node
=
ne
mu
.
Node
(
nonetns
=
True
)
routes
=
node
.
get_routes
()
# main netns routes!
routes
=
node
.
get_routes
()
# main netns routes!
if
(
len
(
routes
)):
if
(
len
(
routes
)):
self
.
assertRaises
(
RuntimeError
,
node
.
add_route
,
routes
[
0
])
self
.
assertRaises
(
RuntimeError
,
node
.
add_route
,
routes
[
0
])
...
@@ -16,7 +16,7 @@ class TestRouting(unittest.TestCase):
...
@@ -16,7 +16,7 @@ class TestRouting(unittest.TestCase):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_routing
(
self
):
def
test_routing
(
self
):
node
=
ne
tns
.
Node
()
node
=
ne
mu
.
Node
()
self
.
assertEquals
(
len
(
node
.
get_routes
()),
0
)
self
.
assertEquals
(
len
(
node
.
get_routes
()),
0
)
if0
=
node
.
add_if
()
if0
=
node
.
add_if
()
...
...
test/test_subprocess.py
View file @
53c49b08
#!/usr/bin/env python
#!/usr/bin/env python
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
import
ne
tns
,
netns
.
subprocess_
,
test_util
import
ne
mu
,
nemu
.
subprocess_
,
test_util
import
grp
,
os
,
pwd
,
signal
,
socket
,
sys
,
time
,
unittest
import
grp
,
os
,
pwd
,
signal
,
socket
,
sys
,
time
,
unittest
from
ne
tns
.subprocess_
import
*
from
ne
mu
.subprocess_
import
*
def
_stat
(
path
):
def
_stat
(
path
):
try
:
try
:
...
@@ -79,7 +79,7 @@ class TestSubprocess(unittest.TestCase):
...
@@ -79,7 +79,7 @@ class TestSubprocess(unittest.TestCase):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
test_Subprocess_chuser
(
self
):
def
test_Subprocess_chuser
(
self
):
node
=
ne
tns
.
Node
(
nonetns
=
True
)
node
=
ne
mu
.
Node
(
nonetns
=
True
)
user
=
'nobody'
user
=
'nobody'
p
=
Subprocess
(
node
,
[
'/bin/sleep'
,
'1000'
],
user
=
user
)
p
=
Subprocess
(
node
,
[
'/bin/sleep'
,
'1000'
],
user
=
user
)
self
.
_check_ownership
(
user
,
p
.
pid
)
self
.
_check_ownership
(
user
,
p
.
pid
)
...
@@ -123,7 +123,7 @@ class TestSubprocess(unittest.TestCase):
...
@@ -123,7 +123,7 @@ class TestSubprocess(unittest.TestCase):
self
.
assertEquals
(
wait
(
p
),
0
)
self
.
assertEquals
(
wait
(
p
),
0
)
def
test_Subprocess_basic
(
self
):
def
test_Subprocess_basic
(
self
):
node
=
ne
tns
.
Node
(
nonetns
=
True
)
node
=
ne
mu
.
Node
(
nonetns
=
True
)
# User does not exist
# User does not exist
self
.
assertRaises
(
ValueError
,
Subprocess
,
node
,
self
.
assertRaises
(
ValueError
,
Subprocess
,
node
,
[
'/bin/sleep'
,
'1000'
],
user
=
self
.
nouser
)
[
'/bin/sleep'
,
'1000'
],
user
=
self
.
nouser
)
...
@@ -201,7 +201,7 @@ class TestSubprocess(unittest.TestCase):
...
@@ -201,7 +201,7 @@ class TestSubprocess(unittest.TestCase):
self
.
assertEquals
(
p
.
wait
(),
-
signal
.
SIGTERM
)
self
.
assertEquals
(
p
.
wait
(),
-
signal
.
SIGTERM
)
def
test_Popen
(
self
):
def
test_Popen
(
self
):
node
=
ne
tns
.
Node
(
nonetns
=
True
)
node
=
ne
mu
.
Node
(
nonetns
=
True
)
# repeat test with Popen interface
# repeat test with Popen interface
r0
,
w0
=
os
.
pipe
()
r0
,
w0
=
os
.
pipe
()
...
@@ -291,7 +291,7 @@ class TestSubprocess(unittest.TestCase):
...
@@ -291,7 +291,7 @@ class TestSubprocess(unittest.TestCase):
self
.
assertEquals
(
p
.
communicate
(
_longstring
),
(
_longstring
,
)
*
2
)
self
.
assertEquals
(
p
.
communicate
(
_longstring
),
(
_longstring
,
)
*
2
)
def
test_backticks
(
self
):
def
test_backticks
(
self
):
node
=
ne
tns
.
Node
(
nonetns
=
True
)
node
=
ne
mu
.
Node
(
nonetns
=
True
)
self
.
assertEquals
(
backticks
(
node
,
"echo hello world"
),
"hello world
\
n
"
)
self
.
assertEquals
(
backticks
(
node
,
"echo hello world"
),
"hello world
\
n
"
)
self
.
assertEquals
(
backticks
(
node
,
r"echo hello\
\ world"
),
self
.
assertEquals
(
backticks
(
node
,
r"echo hello\
\ world"
),
"hello world
\
n
"
)
"hello world
\
n
"
)
...
@@ -303,7 +303,7 @@ class TestSubprocess(unittest.TestCase):
...
@@ -303,7 +303,7 @@ class TestSubprocess(unittest.TestCase):
self
.
assertRaises
(
RuntimeError
,
backticks_raise
,
node
,
"kill $$"
)
self
.
assertRaises
(
RuntimeError
,
backticks_raise
,
node
,
"kill $$"
)
def
test_system
(
self
):
def
test_system
(
self
):
node
=
ne
tns
.
Node
(
nonetns
=
True
)
node
=
ne
mu
.
Node
(
nonetns
=
True
)
self
.
assertEquals
(
system
(
node
,
"true"
),
0
)
self
.
assertEquals
(
system
(
node
,
"true"
),
0
)
self
.
assertEquals
(
system
(
node
,
"false"
),
1
)
self
.
assertEquals
(
system
(
node
,
"false"
),
1
)
...
...
test/test_switch.py
View file @
53c49b08
...
@@ -2,16 +2,16 @@
...
@@ -2,16 +2,16 @@
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
import
os
,
unittest
import
os
,
unittest
import
ne
tns
,
test_util
,
netns
.
environ
import
ne
mu
,
test_util
,
nemu
.
environ
class
TestSwitch
(
unittest
.
TestCase
):
class
TestSwitch
(
unittest
.
TestCase
):
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
@
test_util
.
skipUnless
(
os
.
getuid
()
==
0
,
"Test requires root privileges"
)
def
setUp
(
self
):
def
setUp
(
self
):
n1
=
ne
tns
.
Node
()
n1
=
ne
mu
.
Node
()
n2
=
ne
tns
.
Node
()
n2
=
ne
mu
.
Node
()
i1
=
n1
.
add_if
()
i1
=
n1
.
add_if
()
i2
=
n2
.
add_if
()
i2
=
n2
.
add_if
()
l
=
ne
tns
.
Switch
()
l
=
ne
mu
.
Switch
()
l
.
connect
(
i1
)
l
.
connect
(
i1
)
l
.
connect
(
i2
)
l
.
connect
(
i2
)
self
.
stuff
=
(
n1
,
n2
,
i1
,
i2
,
l
)
self
.
stuff
=
(
n1
,
n2
,
i1
,
i2
,
l
)
...
@@ -20,7 +20,7 @@ class TestSwitch(unittest.TestCase):
...
@@ -20,7 +20,7 @@ class TestSwitch(unittest.TestCase):
def
test_switch_base
(
self
):
def
test_switch_base
(
self
):
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
l
.
mtu
=
3000
l
.
mtu
=
3000
ifdata
=
ne
tns
.
iproute
.
get_if_data
()[
0
]
ifdata
=
ne
mu
.
iproute
.
get_if_data
()[
0
]
self
.
assertEquals
(
ifdata
[
l
.
index
].
mtu
,
3000
)
self
.
assertEquals
(
ifdata
[
l
.
index
].
mtu
,
3000
)
self
.
assertEquals
(
ifdata
[
i1
.
control
.
index
].
mtu
,
3000
,
self
.
assertEquals
(
ifdata
[
i1
.
control
.
index
].
mtu
,
3000
,
"MTU propagation"
)
"MTU propagation"
)
...
@@ -35,11 +35,11 @@ class TestSwitch(unittest.TestCase):
...
@@ -35,11 +35,11 @@ class TestSwitch(unittest.TestCase):
"UP propagation"
)
"UP propagation"
)
l
.
up
=
True
l
.
up
=
True
ifdata
=
ne
tns
.
iproute
.
get_if_data
()[
0
]
ifdata
=
ne
mu
.
iproute
.
get_if_data
()[
0
]
self
.
assertEquals
(
ifdata
[
i1
.
control
.
index
].
up
,
True
,
"UP propagation"
)
self
.
assertEquals
(
ifdata
[
i1
.
control
.
index
].
up
,
True
,
"UP propagation"
)
self
.
assertEquals
(
ifdata
[
i2
.
control
.
index
].
up
,
True
,
"UP propagation"
)
self
.
assertEquals
(
ifdata
[
i2
.
control
.
index
].
up
,
True
,
"UP propagation"
)
tcdata
=
ne
tns
.
iproute
.
get_tc_data
()[
0
]
tcdata
=
ne
mu
.
iproute
.
get_tc_data
()[
0
]
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
{
"qdiscs"
:
{}})
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
{
"qdiscs"
:
{}})
self
.
assertEquals
(
tcdata
[
i2
.
control
.
index
],
{
"qdiscs"
:
{}})
self
.
assertEquals
(
tcdata
[
i2
.
control
.
index
],
{
"qdiscs"
:
{}})
...
@@ -50,11 +50,11 @@ class TestSwitch(unittest.TestCase):
...
@@ -50,11 +50,11 @@ class TestSwitch(unittest.TestCase):
# Test strange rules handling
# Test strange rules handling
os
.
system
((
"%s qd add dev %s root prio bands 3 "
+
os
.
system
((
"%s qd add dev %s root prio bands 3 "
+
"priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1"
)
%
"priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1"
)
%
(
ne
tns
.
environ
.
tc_path
,
i1
.
control
.
name
))
(
ne
mu
.
environ
.
tc_path
,
i1
.
control
.
name
))
tcdata
=
ne
tns
.
iproute
.
get_tc_data
()[
0
]
tcdata
=
ne
mu
.
iproute
.
get_tc_data
()[
0
]
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
"foreign"
)
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
"foreign"
)
l
.
set_parameters
(
bandwidth
=
13107200
)
# 100 mbits
l
.
set_parameters
(
bandwidth
=
13107200
)
# 100 mbits
tcdata
=
ne
tns
.
iproute
.
get_tc_data
()[
0
]
tcdata
=
ne
mu
.
iproute
.
get_tc_data
()[
0
]
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
{
"bandwidth"
:
13107000
,
"qdiscs"
:
{
"tbf"
:
"1"
}})
{
"bandwidth"
:
13107000
,
"qdiscs"
:
{
"tbf"
:
"1"
}})
...
@@ -76,14 +76,14 @@ class TestSwitch(unittest.TestCase):
...
@@ -76,14 +76,14 @@ class TestSwitch(unittest.TestCase):
def
_test_none
(
self
):
def
_test_none
(
self
):
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
l
.
set_parameters
()
l
.
set_parameters
()
tcdata
=
ne
tns
.
iproute
.
get_tc_data
()[
0
]
tcdata
=
ne
mu
.
iproute
.
get_tc_data
()[
0
]
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
{
"qdiscs"
:
{}})
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
{
"qdiscs"
:
{}})
self
.
assertEquals
(
tcdata
[
i2
.
control
.
index
],
{
"qdiscs"
:
{}})
self
.
assertEquals
(
tcdata
[
i2
.
control
.
index
],
{
"qdiscs"
:
{}})
def
_test_tbf
(
self
):
def
_test_tbf
(
self
):
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
l
.
set_parameters
(
bandwidth
=
13107200
)
# 100 mbits
l
.
set_parameters
(
bandwidth
=
13107200
)
# 100 mbits
tcdata
=
ne
tns
.
iproute
.
get_tc_data
()[
0
]
tcdata
=
ne
mu
.
iproute
.
get_tc_data
()[
0
]
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
# adjust for tc rounding
# adjust for tc rounding
{
"bandwidth"
:
13107000
,
"qdiscs"
:
{
"tbf"
:
"1"
}})
{
"bandwidth"
:
13107000
,
"qdiscs"
:
{
"tbf"
:
"1"
}})
...
@@ -93,7 +93,7 @@ class TestSwitch(unittest.TestCase):
...
@@ -93,7 +93,7 @@ class TestSwitch(unittest.TestCase):
def
_test_netem
(
self
):
def
_test_netem
(
self
):
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
l
.
set_parameters
(
delay
=
0.001
)
# 1ms
l
.
set_parameters
(
delay
=
0.001
)
# 1ms
tcdata
=
ne
tns
.
iproute
.
get_tc_data
()[
0
]
tcdata
=
ne
mu
.
iproute
.
get_tc_data
()[
0
]
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
{
"delay"
:
0.001
,
"qdiscs"
:
{
"netem"
:
"2"
}})
{
"delay"
:
0.001
,
"qdiscs"
:
{
"netem"
:
"2"
}})
self
.
assertEquals
(
tcdata
[
i2
.
control
.
index
],
self
.
assertEquals
(
tcdata
[
i2
.
control
.
index
],
...
@@ -102,7 +102,7 @@ class TestSwitch(unittest.TestCase):
...
@@ -102,7 +102,7 @@ class TestSwitch(unittest.TestCase):
def
_test_both
(
self
):
def
_test_both
(
self
):
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
(
n1
,
n2
,
i1
,
i2
,
l
)
=
self
.
stuff
l
.
set_parameters
(
bandwidth
=
13107200
,
delay
=
0.001
)
# 100 mbits, 1ms
l
.
set_parameters
(
bandwidth
=
13107200
,
delay
=
0.001
)
# 100 mbits, 1ms
tcdata
=
ne
tns
.
iproute
.
get_tc_data
()[
0
]
tcdata
=
ne
mu
.
iproute
.
get_tc_data
()[
0
]
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
self
.
assertEquals
(
tcdata
[
i1
.
control
.
index
],
{
"bandwidth"
:
13107000
,
"delay"
:
0.001
,
{
"bandwidth"
:
13107000
,
"delay"
:
0.001
,
"qdiscs"
:
{
"tbf"
:
"1"
,
"netem"
:
"2"
}})
"qdiscs"
:
{
"tbf"
:
"1"
,
"netem"
:
"2"
}})
...
...
test/test_util.py
View file @
53c49b08
...
@@ -2,8 +2,8 @@
...
@@ -2,8 +2,8 @@
# vim:ts=4:sw=4:et:ai:sts=4
# vim:ts=4:sw=4:et:ai:sts=4
import
os
,
re
,
subprocess
,
sys
import
os
,
re
,
subprocess
,
sys
import
ne
tns
.subprocess_
import
ne
mu
.subprocess_
from
ne
tns
.environ
import
*
from
ne
mu
.environ
import
*
def
process_ipcmd
(
str
):
def
process_ipcmd
(
str
):
cur
=
None
cur
=
None
...
@@ -58,7 +58,7 @@ def get_devs():
...
@@ -58,7 +58,7 @@ def get_devs():
return process_ipcmd(outdata)
return process_ipcmd(outdata)
def get_devs_netns(node):
def get_devs_netns(node):
out = ne
tns
.subprocess_.backticks_raise(node, [ip_path, "addr", "list"])
out = ne
mu
.subprocess_.backticks_raise(node, [ip_path, "addr", "list"])
return process_ipcmd(out)
return process_ipcmd(out)
def make_linux_ver(string):
def make_linux_ver(string):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment