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
75f3762b
Commit
75f3762b
authored
Apr 02, 2012
by
Martín Ferrari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some linting of the code.
parent
675a3819
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
118 additions
and
104 deletions
+118
-104
examples/sample.py
examples/sample.py
+1
-1
src/nemu/environ.py
src/nemu/environ.py
+72
-60
src/nemu/interface.py
src/nemu/interface.py
+1
-1
src/nemu/iproute.py
src/nemu/iproute.py
+23
-23
src/nemu/node.py
src/nemu/node.py
+2
-2
src/nemu/protocol.py
src/nemu/protocol.py
+4
-4
test/test_core.py
test/test_core.py
+4
-4
test/test_interfaces.py
test/test_interfaces.py
+3
-3
test/test_node.py
test/test_node.py
+5
-3
test/test_switch.py
test/test_switch.py
+1
-1
test/test_util.py
test/test_util.py
+2
-2
No files found.
examples/sample.py
View file @
75f3762b
...
@@ -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
,
nemu
.
environ
.
tcpdump_path
,
if1b
.
name
),
shell
=
True
)
(
xterm
,
nemu
.
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
)
...
...
src/nemu/environ.py
View file @
75f3762b
...
@@ -20,54 +20,54 @@
...
@@ -20,54 +20,54 @@
import
errno
,
os
,
os
.
path
,
socket
,
subprocess
,
sys
,
syslog
import
errno
,
os
,
os
.
path
,
socket
,
subprocess
,
sys
,
syslog
from
syslog
import
LOG_ERR
,
LOG_WARNING
,
LOG_NOTICE
,
LOG_INFO
,
LOG_DEBUG
from
syslog
import
LOG_ERR
,
LOG_WARNING
,
LOG_NOTICE
,
LOG_INFO
,
LOG_DEBUG
__all__
=
[
"ip_path"
,
"tc_path"
,
"brctl_path"
,
"sysctl_path"
,
"hz"
]
__all__
+=
[
"tcpdump_path"
,
"netperf_path"
,
"xauth_path"
,
"xdpyinfo_path"
]
__all__
=
[
"IP_PATH"
,
"TC_PATH"
,
"BRCTL_PATH"
,
"SYSCTL_PATH"
,
"HZ"
]
__all__
+=
[
"TCPDUMP_PATH"
,
"NETPERF_PATH"
,
"XAUTH_PATH"
,
"XDPYINFO_PATH"
]
__all__
+=
[
"execute"
,
"backticks"
,
"eintr_wrapper"
]
__all__
+=
[
"execute"
,
"backticks"
,
"eintr_wrapper"
]
__all__
+=
[
"find_listen_port"
]
__all__
+=
[
"find_listen_port"
]
__all__
+=
[
"LOG_ERR"
,
"LOG_WARNING"
,
"LOG_NOTICE"
,
"LOG_INFO"
,
"LOG_DEBUG"
]
__all__
+=
[
"LOG_ERR"
,
"LOG_WARNING"
,
"LOG_NOTICE"
,
"LOG_INFO"
,
"LOG_DEBUG"
]
__all__
+=
[
"set_log_level"
,
"logger"
]
__all__
+=
[
"set_log_level"
,
"logger"
]
__all__
+=
[
"error"
,
"warning"
,
"notice"
,
"info"
,
"debug"
]
__all__
+=
[
"error"
,
"warning"
,
"notice"
,
"info"
,
"debug"
]
def
find_bin
(
name
,
extra_path
=
None
):
def
find_bin
(
name
,
extra_path
=
None
):
"""Try hard to find the location of needed programs."""
search
=
[]
search
=
[]
if
"PATH"
in
os
.
environ
:
if
"PATH"
in
os
.
environ
:
search
+=
os
.
environ
[
"PATH"
].
split
(
":"
)
search
+=
os
.
environ
[
"PATH"
].
split
(
":"
)
for
pref
in
(
"/"
,
"/usr/"
,
"/usr/local/"
):
search
.
extend
(
os
.
path
.
join
(
x
,
y
)
for
d
in
(
"bin"
,
"sbin"
):
for
x
in
(
"/"
,
"/usr/"
,
"/usr/local/"
)
search
.
append
(
pref
+
d
)
for
y
in
(
"bin"
,
"sbin"
)
)
if
extra_path
:
if
extra_path
:
search
+=
extra_path
search
+=
extra_path
for
d
in
search
:
for
dirr
in
search
:
try
:
path
=
os
.
path
.
join
(
dirr
,
name
)
os
.
stat
(
d
+
"/"
+
name
)
if
os
.
path
.
exists
(
path
):
return
d
+
"/"
+
name
return
path
except
OSError
,
e
:
if
e
.
errno
!=
os
.
errno
.
ENOENT
:
raise
return
None
return
None
def
find_bin_or_die
(
name
,
extra_path
=
None
):
def
find_bin_or_die
(
name
,
extra_path
=
None
):
r
=
find_bin
(
name
)
"""Try hard to find the location of needed programs; raise on failure."""
if
not
r
:
res
=
find_bin
(
name
,
extra_path
)
raise
RuntimeError
((
"Cannot find `%s' command, impossible to "
+
if
not
res
:
"continue."
)
%
name
)
raise
RuntimeError
(
"Cannot find `%s', impossible to continue."
%
name
)
return
r
return
r
es
ip_path
=
find_bin_or_die
(
"ip"
)
IP_PATH
=
find_bin_or_die
(
"ip"
)
tc_path
=
find_bin_or_die
(
"tc"
)
TC_PATH
=
find_bin_or_die
(
"tc"
)
brctl_path
=
find_bin_or_die
(
"brctl"
)
BRCTL_PATH
=
find_bin_or_die
(
"brctl"
)
sysctl_path
=
find_bin_or_die
(
"sysctl"
)
SYSCTL_PATH
=
find_bin_or_die
(
"sysctl"
)
# Optional tools
# Optional tools
tcpdump_path
=
find_bin
(
"tcpdump"
)
TCPDUMP_PATH
=
find_bin
(
"tcpdump"
)
netperf_path
=
find_bin
(
"netperf"
)
NETPERF_PATH
=
find_bin
(
"netperf"
)
xauth_path
=
find_bin
(
"xauth"
)
XAUTH_PATH
=
find_bin
(
"xauth"
)
xdpyinfo_path
=
find_bin
(
"xdpyinfo"
)
XDPYINFO_PATH
=
find_bin
(
"xdpyinfo"
)
# Seems this is completely bogus. At least, we can assume that the internal HZ
# Seems this is completely bogus. At least, we can assume that the internal HZ
# is bigger than this.
# is bigger than this.
hz
=
os
.
sysconf
(
"SC_CLK_TCK"
)
HZ
=
os
.
sysconf
(
"SC_CLK_TCK"
)
try
:
try
:
os
.
stat
(
"/sys/class/net"
)
os
.
stat
(
"/sys/class/net"
)
...
@@ -76,43 +76,56 @@ except:
...
@@ -76,43 +76,56 @@ except:
"continue."
)
"continue."
)
def
execute
(
cmd
):
def
execute
(
cmd
):
"""Execute a command, if the return value is non-zero, raise an exception.
Raises:
RuntimeError: the command was unsuccessful (return code != 0).
"""
debug
(
"execute(%s)"
%
cmd
)
debug
(
"execute(%s)"
%
cmd
)
null
=
open
(
"/dev/null"
,
"r+"
)
null
=
open
(
"/dev/null"
,
"r+"
)
p
=
subprocess
.
Popen
(
cmd
,
stdout
=
null
,
stderr
=
subprocess
.
PIPE
)
p
roc
=
subprocess
.
Popen
(
cmd
,
stdout
=
null
,
stderr
=
subprocess
.
PIPE
)
out
,
err
=
p
.
communicate
()
_
,
err
=
proc
.
communicate
()
if
p
.
returncode
!=
0
:
if
p
roc
.
returncode
!=
0
:
raise
RuntimeError
(
"Error executing `%s': %s"
%
(
" "
.
join
(
cmd
),
err
))
raise
RuntimeError
(
"Error executing `%s': %s"
%
(
" "
.
join
(
cmd
),
err
))
def
backticks
(
cmd
):
def
backticks
(
cmd
):
"""Execute a command and capture its output.
If the return value is non-zero, raise an exception.
Returns:
(stdout, stderr): tuple containing the captured output.
Raises:
RuntimeError: the command was unsuccessful (return code != 0).
"""
debug
(
"backticks(%s)"
%
cmd
)
debug
(
"backticks(%s)"
%
cmd
)
p
=
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
p
roc
=
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
stderr
=
subprocess
.
PIPE
)
out
,
err
=
p
.
communicate
()
out
,
err
=
p
roc
.
communicate
()
if
p
.
returncode
!=
0
:
if
p
roc
.
returncode
!=
0
:
raise
RuntimeError
(
"Error executing `%s': %s"
%
(
" "
.
join
(
cmd
),
err
))
raise
RuntimeError
(
"Error executing `%s': %s"
%
(
" "
.
join
(
cmd
),
err
))
return
out
return
out
def
eintr_wrapper
(
f
,
*
args
):
def
eintr_wrapper
(
f
unc
,
*
args
):
"Wraps some callable with a loop that retries on EINTR."
"Wraps some callable with a loop that retries on EINTR."
while
True
:
while
True
:
try
:
try
:
return
f
(
*
args
)
return
f
unc
(
*
args
)
except
OSError
,
e
:
# pragma: no cover
except
OSError
,
e
x
:
# pragma: no cover
if
e
.
errno
==
errno
.
EINTR
:
if
e
x
.
errno
==
errno
.
EINTR
:
continue
continue
raise
raise
except
IOError
,
e
:
# pragma: no cover
except
IOError
,
e
x
:
# pragma: no cover
if
e
.
errno
==
errno
.
EINTR
:
if
e
x
.
errno
==
errno
.
EINTR
:
continue
continue
raise
raise
def
find_listen_port
(
family
=
socket
.
AF_INET
,
type
=
socket
.
SOCK_STREAM
,
def
find_listen_port
(
family
=
socket
.
AF_INET
,
type
=
socket
.
SOCK_STREAM
,
proto
=
0
,
addr
=
"127.0.0.1"
,
min_port
=
1
,
max_port
=
65535
):
proto
=
0
,
addr
=
"127.0.0.1"
,
min_port
=
1
,
max_port
=
65535
):
s
=
socket
.
socket
(
family
,
type
,
proto
)
s
ock
=
socket
.
socket
(
family
,
type
,
proto
)
for
p
in
range
(
min_port
,
max_port
+
1
):
for
p
ort
in
range
(
min_port
,
max_port
+
1
):
try
:
try
:
s
.
bind
((
addr
,
p
))
s
ock
.
bind
((
addr
,
port
))
return
s
,
p
return
s
ock
,
port
except
socket
.
error
:
except
socket
.
error
:
pass
pass
raise
RuntimeError
(
"Cannot find an usable port in the range specified"
)
raise
RuntimeError
(
"Cannot find an usable port in the range specified"
)
...
@@ -130,36 +143,36 @@ def set_log_level(level):
...
@@ -130,36 +143,36 @@ def set_log_level(level):
assert
level
>
LOG_ERR
and
level
<=
LOG_DEBUG
assert
level
>
LOG_ERR
and
level
<=
LOG_DEBUG
_log_level
=
level
_log_level
=
level
def
set_log_output
(
file
):
def
set_log_output
(
stream
):
"Redirect console messages to the provided stream."
"Redirect console messages to the provided stream."
global
_log_stream
global
_log_stream
assert
hasattr
(
file
,
"write"
)
and
hasattr
(
file
,
"flush"
)
assert
hasattr
(
stream
,
"write"
)
and
hasattr
(
stream
,
"flush"
)
_log_stream
=
file
_log_stream
=
stream
def
log_use_syslog
(
use
=
True
,
ident
=
None
,
logopt
=
0
,
def
log_use_syslog
(
use
=
True
,
ident
=
None
,
logopt
=
0
,
facility
=
syslog
.
LOG_USER
):
facility
=
syslog
.
LOG_USER
):
"Enable or disable the use of syslog for logging messages."
"Enable or disable the use of syslog for logging messages."
global
_log_use_syslog
,
_log_syslog_opts
global
_log_use_syslog
,
_log_syslog_opts
if
not
use
:
_log_syslog_opts
=
(
ident
,
logopt
,
facility
)
if
_log_use_syslog
:
_log_use_syslog
=
use
_init_log
()
def
_init_log
():
if
not
_log_use_syslog
:
syslog
.
closelog
()
syslog
.
closelog
()
_log_use_syslog
=
False
return
return
(
ident
,
logopt
,
facility
)
=
_log_syslog_opts
if
not
ident
:
if
not
ident
:
#ident = os.path.basename(sys.argv[0])
#ident = os.path.basename(sys.argv[0])
ident
=
"nemu"
ident
=
"nemu"
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_use_syslog
=
True
info
(
"Syslog logging started"
)
info
(
"Syslog logging started"
)
def
logger
(
priority
,
message
):
def
logger
(
priority
,
message
):
"Print a log message in syslog, console or both."
"Print a log message in syslog, console or both."
global
_log_use_syslog
,
_log_stream
if
_log_use_syslog
:
if
_log_use_syslog
:
if
os
.
getpid
()
!=
_log_pid
:
if
os
.
getpid
()
!=
_log_pid
:
# Re-init logging
_init_log
()
# Need to tell syslog the new PID.
log_use_syslog
(
_log_use_syslog
,
*
_log_syslog_opts
)
syslog
.
syslog
(
priority
,
message
)
syslog
.
syslog
(
priority
,
message
)
return
return
if
priority
>
_log_level
:
if
priority
>
_log_level
:
...
@@ -180,14 +193,13 @@ def info(message):
...
@@ -180,14 +193,13 @@ def info(message):
def
debug
(
message
):
def
debug
(
message
):
logger
(
LOG_DEBUG
,
message
)
logger
(
LOG_DEBUG
,
message
)
# Used to print extra info in nested exceptions
def
_custom_hook
(
tipe
,
value
,
traceback
):
# pragma: no cover
def
_custom_hook
(
t
,
v
,
tb
):
# pragma: no cover
"""Custom exception hook, to print nested exceptions information."""
if
hasattr
(
v
,
"child_traceback"
):
if
hasattr
(
v
alue
,
"child_traceback"
):
sys
.
stderr
.
write
(
"Nested exception, original traceback "
+
sys
.
stderr
.
write
(
"Nested exception, original traceback "
+
"(most recent call last):
\
n
"
)
"(most recent call last):
\
n
"
)
sys
.
stderr
.
write
(
v
.
child_traceback
+
(
"-"
*
70
)
+
"
\
n
"
)
sys
.
stderr
.
write
(
v
alue
.
child_traceback
+
(
"-"
*
70
)
+
"
\
n
"
)
sys
.
__excepthook__
(
t
,
v
,
tb
)
sys
.
__excepthook__
(
t
ipe
,
value
,
traceback
)
# XXX: somebody kill me, I deserve it :)
sys
.
excepthook
=
_custom_hook
sys
.
excepthook
=
_custom_hook
src/nemu/interface.py
View file @
75f3762b
...
@@ -71,7 +71,7 @@ class NSInterface(Interface):
...
@@ -71,7 +71,7 @@ class NSInterface(Interface):
# Disable auto-configuration
# Disable auto-configuration
# you wish: need to take into account the nonetns mode; plus not
# you wish: need to take into account the nonetns mode; plus not
# touching some pre-existing ifaces
# touching some pre-existing ifaces
#node.system([
sysctl_path
, '-w', 'net.ipv6.conf.%s.autoconf=0' %
#node.system([
SYSCTL_PATH
, '-w', 'net.ipv6.conf.%s.autoconf=0' %
#self.name])
#self.name])
node
.
_add_interface
(
self
)
node
.
_add_interface
(
self
)
...
...
src/nemu/iproute.py
View file @
75f3762b
...
@@ -299,7 +299,7 @@ def get_if_data():
...
@@ -299,7 +299,7 @@ def get_if_data():
In each dictionary, values are interface objects.
In each dictionary, values are interface objects.
"""
"""
ipdata
=
backticks
([
ip_path
,
"-o"
,
"link"
,
"list"
])
ipdata
=
backticks
([
IP_PATH
,
"-o"
,
"link"
,
"list"
])
byidx
=
{}
byidx
=
{}
bynam
=
{}
bynam
=
{}
...
@@ -348,7 +348,7 @@ def create_if_pair(if1, if2):
...
@@ -348,7 +348,7 @@ def create_if_pair(if1, if2):
if
iface
[
i
].
mtu
:
if
iface
[
i
].
mtu
:
cmd
[
i
]
+=
[
"mtu"
,
str
(
iface
[
i
].
mtu
)]
cmd
[
i
]
+=
[
"mtu"
,
str
(
iface
[
i
].
mtu
)]
cmd
=
[
ip_path
,
"link"
,
"add"
]
+
cmd
[
0
]
+
[
"type"
,
"veth"
,
"peer"
]
+
cmd
[
1
]
cmd
=
[
IP_PATH
,
"link"
,
"add"
]
+
cmd
[
0
]
+
[
"type"
,
"veth"
,
"peer"
]
+
cmd
[
1
]
execute
(
cmd
)
execute
(
cmd
)
try
:
try
:
set_if
(
if1
)
set_if
(
if1
)
...
@@ -366,7 +366,7 @@ def create_if_pair(if1, if2):
...
@@ -366,7 +366,7 @@ def create_if_pair(if1, if2):
def
del_if
(
iface
):
def
del_if
(
iface
):
ifname
=
_get_if_name
(
iface
)
ifname
=
_get_if_name
(
iface
)
execute
([
ip_path
,
"link"
,
"del"
,
ifname
])
execute
([
IP_PATH
,
"link"
,
"del"
,
ifname
])
def
set_if
(
iface
,
recover
=
True
):
def
set_if
(
iface
,
recover
=
True
):
def
do_cmds
(
cmds
,
orig_iface
):
def
do_cmds
(
cmds
,
orig_iface
):
...
@@ -383,7 +383,7 @@ def set_if(iface, recover = True):
...
@@ -383,7 +383,7 @@ def set_if(iface, recover = True):
# Name goes first
# Name goes first
if
diff
.
name
:
if
diff
.
name
:
_ils
=
[
ip_path
,
"link"
,
"set"
,
"dev"
]
_ils
=
[
IP_PATH
,
"link"
,
"set"
,
"dev"
]
cmds
=
[
_ils
+
[
orig_iface
.
name
,
"name"
,
diff
.
name
]]
cmds
=
[
_ils
+
[
orig_iface
.
name
,
"name"
,
diff
.
name
]]
if
orig_iface
.
up
:
if
orig_iface
.
up
:
# iface needs to be down
# iface needs to be down
...
@@ -392,7 +392,7 @@ def set_if(iface, recover = True):
...
@@ -392,7 +392,7 @@ def set_if(iface, recover = True):
do_cmds
(
cmds
,
orig_iface
)
do_cmds
(
cmds
,
orig_iface
)
# I need to use the new name after a name change, duh!
# I need to use the new name after a name change, duh!
_ils
=
[
ip_path
,
"link"
,
"set"
,
"dev"
,
diff
.
name
or
orig_iface
.
name
]
_ils
=
[
IP_PATH
,
"link"
,
"set"
,
"dev"
,
diff
.
name
or
orig_iface
.
name
]
cmds
=
[]
cmds
=
[]
if
diff
.
lladdr
:
if
diff
.
lladdr
:
if
orig_iface
.
up
:
if
orig_iface
.
up
:
...
@@ -417,12 +417,12 @@ def set_if(iface, recover = True):
...
@@ -417,12 +417,12 @@ def set_if(iface, recover = True):
def
change_netns
(
iface
,
netns
):
def
change_netns
(
iface
,
netns
):
ifname
=
_get_if_name
(
iface
)
ifname
=
_get_if_name
(
iface
)
execute
([
ip_path
,
"link"
,
"set"
,
"dev"
,
ifname
,
"netns"
,
str
(
netns
)])
execute
([
IP_PATH
,
"link"
,
"set"
,
"dev"
,
ifname
,
"netns"
,
str
(
netns
)])
# Address handling
# Address handling
def
get_addr_data
():
def
get_addr_data
():
ipdata
=
backticks
([
ip_path
,
"-o"
,
"addr"
,
"list"
])
ipdata
=
backticks
([
IP_PATH
,
"-o"
,
"addr"
,
"list"
])
byidx
=
{}
byidx
=
{}
bynam
=
{}
bynam
=
{}
...
@@ -461,7 +461,7 @@ def add_addr(iface, address):
...
@@ -461,7 +461,7 @@ def add_addr(iface, address):
addresses = get_addr_data()[1][ifname]
addresses = get_addr_data()[1][ifname]
assert address not in addresses
assert address not in addresses
cmd = [
ip_path
, "addr", "add", "dev", ifname, "local",
cmd = [
IP_PATH
, "addr", "add", "dev", ifname, "local",
"%s/%d" % (address.address, int(address.prefix_len))]
"%s/%d" % (address.address, int(address.prefix_len))]
if hasattr(address, "broadcast"):
if hasattr(address, "broadcast"):
cmd += ["broadcast", address.broadcast if address.broadcast else "+"]
cmd += ["broadcast", address.broadcast if address.broadcast else "+"]
...
@@ -472,7 +472,7 @@ def del_addr(iface, address):
...
@@ -472,7 +472,7 @@ def del_addr(iface, address):
addresses = get_addr_data()[1][ifname]
addresses = get_addr_data()[1][ifname]
assert address in addresses
assert address in addresses
cmd = [
ip_path
, "addr", "del", "dev", ifname, "local",
cmd = [
IP_PATH
, "addr", "del", "dev", ifname, "local",
"%s/%d" % (address.address, int(address.prefix_len))]
"%s/%d" % (address.address, int(address.prefix_len))]
execute(cmd)
execute(cmd)
...
@@ -546,7 +546,7 @@ def create_bridge(br):
...
@@ -546,7 +546,7 @@ def create_bridge(br):
if isinstance(br, str):
if isinstance(br, str):
br = interface(name = br)
br = interface(name = br)
assert br.name
assert br.name
execute([
brctl_path
, "addbr", br.name])
execute([
BRCTL_PATH
, "addbr", br.name])
try:
try:
set_if(br)
set_if(br)
except:
except:
...
@@ -560,7 +560,7 @@ def create_bridge(br):
...
@@ -560,7 +560,7 @@ def create_bridge(br):
def del_bridge(br):
def del_bridge(br):
brname = _get_if_name(br)
brname = _get_if_name(br)
execute([
brctl_path
, "delbr", brname])
execute([
BRCTL_PATH
, "delbr", brname])
def set_bridge(br, recover = True):
def set_bridge(br, recover = True):
def saveval(fname, val):
def saveval(fname, val):
...
@@ -599,18 +599,18 @@ def set_bridge(br, recover = True):
...
@@ -599,18 +599,18 @@ def set_bridge(br, recover = True):
def
add_bridge_port
(
br
,
iface
):
def
add_bridge_port
(
br
,
iface
):
ifname
=
_get_if_name
(
iface
)
ifname
=
_get_if_name
(
iface
)
brname
=
_get_if_name
(
br
)
brname
=
_get_if_name
(
br
)
execute
([
brctl_path
,
"addif"
,
brname
,
ifname
])
execute
([
BRCTL_PATH
,
"addif"
,
brname
,
ifname
])
def
del_bridge_port
(
br
,
iface
):
def
del_bridge_port
(
br
,
iface
):
ifname
=
_get_if_name
(
iface
)
ifname
=
_get_if_name
(
iface
)
brname
=
_get_if_name
(
br
)
brname
=
_get_if_name
(
br
)
execute
([
brctl_path
,
"delif"
,
brname
,
ifname
])
execute
([
BRCTL_PATH
,
"delif"
,
brname
,
ifname
])
# Routing
# Routing
def
get_all_route_data
():
def
get_all_route_data
():
ipdata
=
backticks
([
ip_path
,
"-o"
,
"route"
,
"list"
])
# "table", "all"
ipdata
=
backticks
([
IP_PATH
,
"-o"
,
"route"
,
"list"
])
# "table", "all"
ipdata
+=
backticks
([
ip_path
,
"-o"
,
"-f"
,
"inet6"
,
"route"
,
"list"
])
ipdata
+=
backticks
([
IP_PATH
,
"-o"
,
"-f"
,
"inet6"
,
"route"
,
"list"
])
ifdata
=
get_if_data
()[
1
]
ifdata
=
get_if_data
()[
1
]
ret
=
[]
ret
=
[]
...
@@ -655,7 +655,7 @@ def del_route(route):
...
@@ -655,7 +655,7 @@ def del_route(route):
_add_del_route("del", route)
_add_del_route("del", route)
def _add_del_route(action, route):
def _add_del_route(action, route):
cmd = [
ip_path
, "route", action]
cmd = [
IP_PATH
, "route", action]
if route.tipe != "unicast":
if route.tipe != "unicast":
cmd += [route.tipe]
cmd += [route.tipe]
if route.prefix:
if route.prefix:
...
@@ -671,7 +671,7 @@ def _add_del_route(action, route):
...
@@ -671,7 +671,7 @@ def _add_del_route(action, route):
# TC stuff
# TC stuff
def get_tc_tree():
def get_tc_tree():
tcdata = backticks([
tc_path
, "qdisc", "show"])
tcdata = backticks([
TC_PATH
, "qdisc", "show"])
data = {}
data = {}
for line in tcdata.split("
\
n
"):
for line in tcdata.split("
\
n
"):
...
@@ -827,7 +827,7 @@ def clear_tc(iface):
...
@@ -827,7 +827,7 @@ def clear_tc(iface):
if tcdata[iface.index] == None:
if tcdata[iface.index] == None:
return
return
# Any other case, we clean
# Any other case, we clean
execute([
tc_path
, "qdisc", "del", "dev", iface.name, "root"])
execute([
TC_PATH
, "qdisc", "del", "dev", iface.name, "root"])
def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
delay_correlation = None, delay_distribution = None,
delay_correlation = None, delay_distribution = None,
...
@@ -843,7 +843,7 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
...
@@ -843,7 +843,7 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
commands = []
commands = []
if tcdata[iface.index] == '
foreign
':
if tcdata[iface.index] == '
foreign
':
# Avoid the overhead of calling tc+ip again
# Avoid the overhead of calling tc+ip again
commands.append([
tc_path
, "qdisc", "del", "dev", iface.name, "root"])
commands.append([
TC_PATH
, "qdisc", "del", "dev", iface.name, "root"])
tcdata[iface.index] = {'
qdiscs
': []}
tcdata[iface.index] = {'
qdiscs
': []}
has_netem = '
netem
' in tcdata[iface.index]['
qdiscs
']
has_netem = '
netem
' in tcdata[iface.index]['
qdiscs
']
...
@@ -859,19 +859,19 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
...
@@ -859,19 +859,19 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
else:
else:
# Too much work to do better :)
# Too much work to do better :)
if has_netem or has_tbf:
if has_netem or has_tbf:
commands.append([
tc_path
, "qdisc", "del", "dev", iface.name,
commands.append([
TC_PATH
, "qdisc", "del", "dev", iface.name,
"root"])
"root"])
cmd = "add"
cmd = "add"
if bandwidth:
if bandwidth:
rate = "%dbit" % int(bandwidth)
rate = "%dbit" % int(bandwidth)
mtu = ifdata[iface.index].mtu
mtu = ifdata[iface.index].mtu
burst = max(mtu, int(bandwidth) /
hz
)
burst = max(mtu, int(bandwidth) /
HZ
)
limit = burst * 2 # FIXME?
limit = burst * 2 # FIXME?
handle = "1:"
handle = "1:"
if cmd == "change":
if cmd == "change":
handle = "%d:" % int(tcdata[iface.index]["qdiscs"]["tbf"])
handle = "%d:" % int(tcdata[iface.index]["qdiscs"]["tbf"])
command = [
tc_path
, "qdisc", cmd, "dev", iface.name, "root", "handle",
command = [
TC_PATH
, "qdisc", cmd, "dev", iface.name, "root", "handle",
handle, "tbf", "rate", rate, "limit", str(limit), "burst",
handle, "tbf", "rate", rate, "limit", str(limit), "burst",
str(burst)]
str(burst)]
commands.append(command)
commands.append(command)
...
@@ -880,7 +880,7 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
...
@@ -880,7 +880,7 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
handle = "2:"
handle = "2:"
if cmd == "change":
if cmd == "change":
handle = "%d:" % int(tcdata[iface.index]["qdiscs"]["netem"])
handle = "%d:" % int(tcdata[iface.index]["qdiscs"]["netem"])
command = [
tc_path
, "qdisc", cmd, "dev", iface.name, "handle", handle]
command = [
TC_PATH
, "qdisc", cmd, "dev", iface.name, "handle", handle]
if bandwidth:
if bandwidth:
parent = "1:"
parent = "1:"
if cmd == "change":
if cmd == "change":
...
...
src/nemu/node.py
View file @
75f3762b
...
@@ -203,8 +203,8 @@ def _start_child(nonetns):
...
@@ -203,8 +203,8 @@ def _start_child(nonetns):
# create new name space
# create new name space
unshare
.
unshare
(
unshare
.
CLONE_NEWNET
)
unshare
.
unshare
(
unshare
.
CLONE_NEWNET
)
# Enable packet forwarding
# Enable packet forwarding
execute
([
sysctl_path
,
'-w'
,
'net.ipv4.ip_forward=1'
])
execute
([
SYSCTL_PATH
,
'-w'
,
'net.ipv4.ip_forward=1'
])
execute
([
sysctl_path
,
'-w'
,
'net.ipv6.conf.default.forwarding=1'
])
execute
([
SYSCTL_PATH
,
'-w'
,
'net.ipv6.conf.default.forwarding=1'
])
srv
.
run
()
srv
.
run
()
except
BaseException
,
e
:
except
BaseException
,
e
:
s
=
"Slave node aborting: %s
\
n
"
%
str
(
e
)
s
=
"Slave node aborting: %s
\
n
"
%
str
(
e
)
...
...
src/nemu/protocol.py
View file @
75f3762b
...
@@ -364,7 +364,7 @@ class Server(object):
...
@@ -364,7 +364,7 @@ class Server(object):
os
.
close
(
fd
)
os
.
close
(
fd
)
# stupid xauth format: needs the 'hostname' for local
# stupid xauth format: needs the 'hostname' for local
# connections
# connections
execute
([
xauth_path
,
"-f"
,
xauth
,
"add"
,
execute
([
XAUTH_PATH
,
"-f"
,
xauth
,
"add"
,
"%s/unix:%d"
%
(
socket
.
gethostname
(),
display
),
"%s/unix:%d"
%
(
socket
.
gethostname
(),
display
),
protoname
,
hexkey
])
protoname
,
hexkey
])
if
user
:
if
user
:
...
@@ -506,7 +506,7 @@ class Server(object):
...
@@ -506,7 +506,7 @@ class Server(object):
self
.
reply
(
200
,
"Done."
)
self
.
reply
(
200
,
"Done."
)
def
do_X11_SET
(
self
,
cmdname
,
protoname
,
hexkey
):
def
do_X11_SET
(
self
,
cmdname
,
protoname
,
hexkey
):
if
not
xauth_path
:
if
not
XAUTH_PATH
:
self
.
reply
(
500
,
"Impossible to forward X: xauth not present"
)
self
.
reply
(
500
,
"Impossible to forward X: xauth not present"
)
return
return
skt
,
port
=
None
,
None
skt
,
port
=
None
,
None
...
@@ -788,9 +788,9 @@ class Client(object):
...
@@ -788,9 +788,9 @@ class Client(object):
raise
RuntimeError
(
"Impossible to forward X: DISPLAY variable not "
raise
RuntimeError
(
"Impossible to forward X: DISPLAY variable not "
"set or invalid"
)
"set or invalid"
)
xauthdpy
,
sock
,
addr
=
xinfo
xauthdpy
,
sock
,
addr
=
xinfo
if
not
xauth_path
:
if
not
XAUTH_PATH
:
raise
RuntimeError
(
"Impossible to forward X: xauth not present"
)
raise
RuntimeError
(
"Impossible to forward X: xauth not present"
)
auth
=
backticks
([
xauth_path
,
"list"
,
xauthdpy
])
auth
=
backticks
([
XAUTH_PATH
,
"list"
,
xauthdpy
])
match
=
re
.
match
(
r"\
S+
\s+(\
S+)
\s+(\
S+)
\n"
,
auth
)
match
=
re
.
match
(
r"\
S+
\s+(\
S+)
\s+(\
S+)
\n"
,
auth
)
if
not
match
:
if
not
match
:
raise
RuntimeError
(
"Impossible to forward X: invalid DISPLAY"
)
raise
RuntimeError
(
"Impossible to forward X: invalid DISPLAY"
)
...
...
test/test_core.py
View file @
75f3762b
...
@@ -230,9 +230,9 @@ class TestGlobal(unittest.TestCase):
...
@@ -230,9 +230,9 @@ 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
(
nemu
.
environ
.
xdpyinfo_path
,
"Test requires xdpyinfo"
)
@
test_util
.
skipUnless
(
nemu
.
environ
.
XDPYINFO_PATH
,
"Test requires xdpyinfo"
)
def
test_run_xdpyinfo
(
self
):
def
test_run_xdpyinfo
(
self
):
xdpy
=
nemu
.
environ
.
xdpyinfo_path
xdpy
=
nemu
.
environ
.
XDPYINFO_PATH
info
=
nemu
.
environ
.
backticks
([
xdpy
])
info
=
nemu
.
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
]
...
@@ -243,9 +243,9 @@ class TestX11(unittest.TestCase):
...
@@ -243,9 +243,9 @@ class TestX11(unittest.TestCase):
@
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
(
nemu
.
environ
.
xdpyinfo_path
,
"Test requires xdpyinfo"
)
@
test_util
.
skipUnless
(
nemu
.
environ
.
XDPYINFO_PATH
,
"Test requires xdpyinfo"
)
def
test_run_xdpyinfo_netns
(
self
):
def
test_run_xdpyinfo_netns
(
self
):
xdpy
=
nemu
.
environ
.
xdpyinfo_path
xdpy
=
nemu
.
environ
.
XDPYINFO_PATH
info
=
nemu
.
environ
.
backticks
([
xdpy
])
info
=
nemu
.
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
]
...
...
test/test_interfaces.py
View file @
75f3762b
...
@@ -141,7 +141,7 @@ class TestInterfaces(unittest.TestCase):
...
@@ -141,7 +141,7 @@ class TestInterfaces(unittest.TestCase):
self
.
assertTrue
(
devs
[
if0
.
name
][
'up'
])
self
.
assertTrue
(
devs
[
if0
.
name
][
'up'
])
# Verify that data is actually read from the kernel
# Verify that data is actually read from the kernel
r
=
node0
.
system
([
ip_path
,
"link"
,
"set"
,
if0
.
name
,
"mtu"
,
"1500"
])
r
=
node0
.
system
([
IP_PATH
,
"link"
,
"set"
,
if0
.
name
,
"mtu"
,
"1500"
])
self
.
assertEquals
(
r
,
0
)
self
.
assertEquals
(
r
,
0
)
devs
=
get_devs_netns
(
node0
)
devs
=
get_devs_netns
(
node0
)
self
.
assertEquals
(
devs
[
if0
.
name
][
'mtu'
],
1500
)
self
.
assertEquals
(
devs
[
if0
.
name
][
'mtu'
],
1500
)
...
@@ -188,7 +188,7 @@ class TestWithDummy(unittest.TestCase):
...
@@ -188,7 +188,7 @@ class TestWithDummy(unittest.TestCase):
node
=
nemu
.
Node
()
node
=
nemu
.
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
)
devs
=
get_devs
()
devs
=
get_devs
()
self
.
assertTrue
(
self
.
dummyname
in
devs
)
self
.
assertTrue
(
self
.
dummyname
in
devs
)
dummyidx
=
devs
[
self
.
dummyname
][
'idx'
]
dummyidx
=
devs
[
self
.
dummyname
][
'idx'
]
...
@@ -213,7 +213,7 @@ class TestWithDummy(unittest.TestCase):
...
@@ -213,7 +213,7 @@ class TestWithDummy(unittest.TestCase):
def
tearDown
(
self
):
def
tearDown
(
self
):
# oops here
# oops here
if
hasattr
(
self
,
'dummyname'
):
if
hasattr
(
self
,
'dummyname'
):
os
.
system
(
"%s link del %s"
%
(
ip_path
,
self
.
dummyname
))
os
.
system
(
"%s link del %s"
%
(
IP_PATH
,
self
.
dummyname
))
# FIXME: Links
# FIXME: Links
...
...
test/test_node.py
View file @
75f3762b
...
@@ -51,7 +51,8 @@ class TestNode(unittest.TestCase):
...
@@ -51,7 +51,8 @@ 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
:
nemu
.
set_cleanup_hooks
(
on_exit
=
True
,
on_signals
=
[])
# TODO: not implemented.
#nemu.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,8 +62,9 @@ class TestNode(unittest.TestCase):
...
@@ -61,8 +62,9 @@ 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
:
nemu
.
set_cleanup_hooks
(
on_exit
=
False
,
# TODO: not implemented.
on_signals
=
[
signal
.
SIGTERM
])
#nemu.set_cleanup_hooks(on_exit = False,
# on_signals = [signal.SIGTERM])
create_stuff
()
create_stuff
()
while
True
:
while
True
:
time
.
sleep
(
10
)
time
.
sleep
(
10
)
...
...
test/test_switch.py
View file @
75f3762b
...
@@ -50,7 +50,7 @@ class TestSwitch(unittest.TestCase):
...
@@ -50,7 +50,7 @@ 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"
)
%
(
nemu
.
environ
.
tc_path
,
i1
.
control
.
name
))
(
nemu
.
environ
.
TC_PATH
,
i1
.
control
.
name
))
tcdata
=
nemu
.
iproute
.
get_tc_data
()[
0
]
tcdata
=
nemu
.
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
...
...
test/test_util.py
View file @
75f3762b
...
@@ -54,11 +54,11 @@ def process_ipcmd(str):
...
@@ -54,11 +54,11 @@ def process_ipcmd(str):
return out
return out
def get_devs():
def get_devs():
outdata = backticks([
ip_path
, "addr", "list"])
outdata = backticks([
IP_PATH
, "addr", "list"])
return process_ipcmd(outdata)
return process_ipcmd(outdata)
def get_devs_netns(node):
def get_devs_netns(node):
out = nemu.subprocess_.backticks_raise(node, [
ip_path
, "addr", "list"])
out = nemu.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