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
35df66e0
Commit
35df66e0
authored
Jul 08, 2010
by
Martín Ferrari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove some not needed stuff
parent
ec459ae1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
109 deletions
+87
-109
src/netns/iproute.py
src/netns/iproute.py
+87
-109
No files found.
src/netns/iproute.py
View file @
35df66e0
...
@@ -2,60 +2,34 @@
...
@@ -2,60 +2,34 @@
import
re
,
socket
,
subprocess
,
sys
import
re
,
socket
,
subprocess
,
sys
class
interfaceflags
(
object
):
@
classmethod
def
parse
(
cls
,
string
):
l
=
string
.
split
(
","
)
up
=
"UP"
in
l
no_carrier
=
"NO_CARRIER"
in
l
loopback
=
"LOOPBACK"
in
l
broadcast
=
"BROADCAST"
in
l
multicast
=
"MULTICAST"
in
l
return
cls
(
up
,
no_carrier
,
loopback
,
broadcast
,
multicast
)
def
__init__
(
self
,
up
=
None
,
no_carrier
=
None
,
loopback
=
None
,
broadcast
=
None
,
multicast
=
None
):
self
.
up
=
up
self
.
no_carrier
=
no_carrier
self
.
loopback
=
loopback
self
.
broadcast
=
broadcast
self
.
multicast
=
multicast
def
__repr__
(
self
):
s
=
"%s.%s(up = %s, no_carrier = %s, loopback = %s, broadcast = %s, "
s
+=
"multicast = %s)"
return
s
%
(
self
.
__module__
,
self
.
__class__
.
__name__
,
self
.
up
,
self
.
no_carrier
,
self
.
loopback
,
self
.
broadcast
,
self
.
multicast
)
def
__sub__
(
self
,
o
):
"""Compare flags and return a new object with just the flags that
differ set (with the value they have in self). The no-carrier,
broadcast, and loopback flags are ignored"""
up
=
None
if
self
.
up
==
o
.
up
else
self
.
up
#no_carrier = (None if self.no_carrier == o.no_carrier else
# self.no_carrier)
#loopback = None if self.loopback == o.loopback else self.loopback
#broadcast = None if self.broadcast == o.broadcast else self.broadcast
multicast
=
None
if
self
.
multicast
==
o
.
multicast
else
self
.
multicast
return
self
.
__class__
(
up
,
None
,
None
,
None
,
multicast
)
def
__eq__
(
self
,
o
):
return
(
self
.
up
==
o
.
up
and
self
.
loopback
==
o
.
loopback
and
self
.
broadcast
==
o
.
broadcast
and
self
.
multicast
==
o
.
multicast
)
class
interface
(
object
):
class
interface
(
object
):
def
__init__
(
self
,
index
=
None
,
name
=
None
,
flags
=
None
,
mtu
=
None
,
@
classmethod
qdisc
=
None
,
tipe
=
None
,
lladdr
=
None
,
broadcast
=
None
,
def
parse_ip
(
cls
,
line
):
match
=
re
.
search
(
r'^(\
d+): (
\S+): <(\
S+)> m
tu (\
d+) qdisc
\S+'
+
r'.*link/\
S+ ([
0-9a-f:]+) brd ([0-9a-f:]+)'
,
line
)
flags
=
match
.
group
(
3
).
split
(
","
)
return
cls
(
index
=
match
.
group
(
1
),
name
=
match
.
group
(
2
),
up
=
"UP"
in
flags
,
mtu
=
match
.
group
(
4
),
lladdr
=
match
.
group
(
5
),
arp
=
not
(
"NOARP"
in
flags
),
broadcast
=
match
.
group
(
6
),
multicast
=
"MULTICAST"
in
flags
)
def
__init__
(
self
,
index
=
None
,
name
=
None
,
up
=
None
,
mtu
=
None
,
lladdr
=
None
,
broadcast
=
None
,
multicast
=
None
,
arp
=
None
,
addresses
=
None
):
addresses
=
None
):
self
.
index
=
int
(
index
)
if
index
else
None
self
.
index
=
int
(
index
)
if
index
else
None
self
.
name
=
name
self
.
name
=
name
self
.
flags
=
flags
self
.
up
=
up
self
.
mtu
=
int
(
mtu
)
if
mtu
else
None
self
.
mtu
=
int
(
mtu
)
if
mtu
else
None
self
.
qdisc
=
qdisc
self
.
type
=
tipe
self
.
lladdr
=
lladdr
self
.
lladdr
=
lladdr
self
.
broadcast
=
broadcast
self
.
broadcast
=
broadcast
if
addresses
:
self
.
multicast
=
multicast
self
.
addresses
=
addresses
self
.
arp
=
arp
else
:
self
.
addresses
=
addresses
if
addresses
else
[]
self
.
addresses
=
[]
def
_set_addresses
(
self
,
value
):
def
_set_addresses
(
self
,
value
):
if
value
==
None
:
if
value
==
None
:
...
@@ -71,28 +45,29 @@ class interface(object):
...
@@ -71,28 +45,29 @@ class interface(object):
addresses
=
property
(
_get_addresses
,
_set_addresses
)
addresses
=
property
(
_get_addresses
,
_set_addresses
)
def
__repr__
(
self
):
def
__repr__
(
self
):
s
=
"%s.%s(index = %s, name = %s,
flags = %s, mtu = %s, qdisc
= %s, "
s
=
"%s.%s(index = %s, name = %s,
up = %s, mtu = %s, lladdr
= %s, "
s
+=
"
tipe = %s, lladdr = %s, broadcast
= %s, addresses = %s)"
s
+=
"
broadcast = %s, multicast = %s, arp
= %s, addresses = %s)"
return
s
%
(
self
.
__module__
,
self
.
__class__
.
__name__
,
return
s
%
(
self
.
__module__
,
self
.
__class__
.
__name__
,
self
.
index
.
__repr__
(),
self
.
name
.
__repr__
(),
self
.
index
.
__repr__
(),
self
.
name
.
__repr__
(),
self
.
flags
.
__repr__
(),
self
.
mtu
.
__repr__
(),
self
.
up
.
__repr__
(),
self
.
mtu
.
__repr__
(),
self
.
qdisc
.
__repr__
(),
self
.
type
.
__repr__
(),
self
.
lladdr
.
__repr__
(),
self
.
broadcast
.
__repr__
(),
self
.
lladdr
.
__repr__
(),
self
.
broadcast
.
__repr__
(),
self
.
multicast
.
__repr__
(),
self
.
arp
.
__repr__
(),
self
.
addresses
.
__repr__
())
self
.
addresses
.
__repr__
())
def
__sub__
(
self
,
o
):
def
__sub__
(
self
,
o
):
"""Compare attributes and return a new object with just the attributes
"""Compare attributes and return a new object with just the attributes
that differ set (with the value they have in the first operand). The
that differ set (with the value they have in the first operand). The
index remains equal to the first operand; type and qdisc are
index remains equal to the first operand."""
ignored."""
name
=
None
if
self
.
name
==
o
.
name
else
self
.
name
name
=
None
if
self
.
name
==
o
.
name
else
self
.
name
flags
=
None
if
self
.
flags
==
o
.
flags
else
self
.
flags
-
o
.
flags
up
=
None
if
self
.
up
==
o
.
up
else
self
.
up
mtu
=
None
if
self
.
mtu
==
o
.
mtu
else
self
.
mtu
mtu
=
None
if
self
.
mtu
==
o
.
mtu
else
self
.
mtu
lladdr
=
None
if
self
.
lladdr
==
o
.
lladdr
else
self
.
lladdr
lladdr
=
None
if
self
.
lladdr
==
o
.
lladdr
else
self
.
lladdr
broadcast
=
None
if
self
.
broadcast
==
o
.
broadcast
else
self
.
broadcast
broadcast
=
None
if
self
.
broadcast
==
o
.
broadcast
else
self
.
broadcast
multicast
=
None
if
self
.
multicast
==
o
.
multicast
else
self
.
multicast
arp
=
None
if
self
.
arp
==
o
.
arp
else
self
.
arp
addresses
=
None
if
self
.
addresses
==
o
.
addresses
else
self
.
addresses
addresses
=
None
if
self
.
addresses
==
o
.
addresses
else
self
.
addresses
return
self
.
__class__
(
self
.
index
,
name
,
flags
,
mtu
,
None
,
None
,
return
self
.
__class__
(
self
.
index
,
name
,
up
,
mtu
,
lladdr
,
broadcast
,
lladdr
,
broadcast
,
addresses
)
multicast
,
arp
,
addresses
)
class
address
(
object
):
class
address
(
object
):
@
property
@
property
...
@@ -102,6 +77,23 @@ class address(object):
...
@@ -102,6 +77,23 @@ class address(object):
@
property
@
property
def
family
(
self
):
return
self
.
_family
def
family
(
self
):
return
self
.
_family
@
classmethod
def
parse_ip
(
cls
,
line
):
match
=
re
.
search
(
r'^inet ([0-9.]+)/(\
d+)(?:
brd ([0-9.]+))?'
,
line
)
if
match
!=
None
:
return
ipv4address
(
address
=
match
.
group
(
1
),
prefix_len
=
match
.
group
(
2
),
broadcast
=
match
.
group
(
3
))
match
=
re
.
search
(
r'^inet6 ([0-9a-f:]+)/(\
d+)
', line)
if match != None:
return ipv6address(
address = match.group(1),
prefix_len = match.group(2))
raise RuntimeError("Problems parsing ip command output")
class ipv4address(address):
class ipv4address(address):
def __init__(self, address, prefix_len, broadcast):
def __init__(self, address, prefix_len, broadcast):
self._address = address
self._address = address
...
@@ -119,6 +111,8 @@ class ipv4address(address):
...
@@ -119,6 +111,8 @@ class ipv4address(address):
self.broadcast.__repr__())
self.broadcast.__repr__())
def __eq__(self, o):
def __eq__(self, o):
if not isinstance(o, address):
return False
return (self.address == o.address and
return (self.address == o.address and
self.prefix_len == o.prefix_len and
self.prefix_len == o.prefix_len and
self.broadcast == o.broadcast)
self.broadcast == o.broadcast)
...
@@ -139,6 +133,8 @@ class ipv6address(address):
...
@@ -139,6 +133,8 @@ class ipv6address(address):
self.address.__repr__(), self.prefix_len)
self.address.__repr__(), self.prefix_len)
def __eq__(self, o):
def __eq__(self, o):
if not isinstance(o, address):
return False
return (self.address == o.address and self.prefix_len == o.prefix_len)
return (self.address == o.address and self.prefix_len == o.prefix_len)
def __hash__(self):
def __hash__(self):
...
@@ -158,7 +154,7 @@ def get_if_data():
...
@@ -158,7 +154,7 @@ def get_if_data():
ipdata = ipcmd.communicate()[0]
ipdata = ipcmd.communicate()[0]
assert ipcmd.wait() == 0
assert ipcmd.wait() == 0
curidx
=
name
=
None
curidx = None
byidx = {}
byidx = {}
bynam = {}
bynam = {}
for line in ipdata.split("\n"):
for line in ipdata.split("\n"):
...
@@ -167,43 +163,17 @@ def get_if_data():
...
@@ -167,43 +163,17 @@ def get_if_data():
match = re.search(r'
^
(
\
d
+
):
\
s
+
(.
*
)
', line)
match = re.search(r'
^
(
\
d
+
):
\
s
+
(.
*
)
', line)
if curidx != int(match.group(1)):
if curidx != int(match.group(1)):
curidx = int(match.group(1))
curidx = int(match.group(1))
i = interface.parse_ip(line)
match
=
re
.
search
(
r'^(\
d+): (
\S+): <(\
S+)> m
tu (\
d+) qdisc (
\S+)'
+
byidx[curidx] = bynam[i.name] = i
r'.*link/(\
S+) ([
0-9a-f:]+) brd ([0-9a-f:]+)'
,
line
)
name
=
match
.
group
(
2
)
byidx
[
curidx
]
=
bynam
[
name
]
=
interface
(
index
=
curidx
,
name
=
name
,
flags
=
interfaceflags
.
parse
(
match
.
group
(
3
)),
mtu
=
match
.
group
(
4
),
qdisc
=
match
.
group
(
5
),
tipe
=
match
.
group
(
6
),
lladdr
=
match
.
group
(
7
),
broadcast
=
match
.
group
(
8
),
)
continue
continue
# Assume curidx is defined
# Assume curidx is defined
assert curidx != None
assert curidx != None
match
=
re
.
search
((
"^%s: %s"
%
(
curidx
,
name
))
+
r'\
s+(.*)$
', line)
match = re.search(("^%s: %s" % (curidx, byidx[curidx].name)) +
r'
\
s
+
(.
*
)
$
', line)
line = match.group(1)
line = match.group(1)
byidx[curidx].addresses += [address.parse_ip(line)]
match = re.search(r'
^
inet
([
0
-
9.
]
+
)
/
(
\
d
+
)(
?
:
brd
([
0
-
9.
]
+
))
?
', line)
if match != None:
byidx[curidx].addresses += [ipv4address(
address = match.group(1),
prefix_len = match.group(2),
broadcast = match.group(3))]
continue
match = re.search(r'
^
inet6
([
0
-
9
a
-
f
:]
+
)
/
(
\
d
+
)
', line)
if match != None:
byidx[curidx].addresses += [ipv6address(
address = match.group(1),
prefix_len = match.group(2))]
continue
raise RuntimeError("Problems parsing ip command output")
return byidx, bynam
return byidx, bynam
def create_if_pair(if1, if2):
def create_if_pair(if1, if2):
...
@@ -214,11 +184,11 @@ def create_if_pair(if1, if2):
...
@@ -214,11 +184,11 @@ def create_if_pair(if1, if2):
for i in (0, 1):
for i in (0, 1):
cmd[i] = ["name", iface[i].name]
cmd[i] = ["name", iface[i].name]
if iface[i].lladdr:
if iface[i].lladdr:
cmd[i]
.expand(["address", iface[i].lladdr])
cmd[i]
+= ["address", iface[i].lladdr]
if iface[i].broadcast:
if iface[i].broadcast:
cmd[i]
.expand(["broadcast", iface[i].broadcast])
cmd[i]
+= ["broadcast", iface[i].broadcast]
if iface[i].mtu:
if iface[i].mtu:
cmd[i]
.expand(["mtu", str(iface[i].mtu)])
cmd[i]
+= ["mtu", str(iface[i].mtu)]
cmd = ["ip", "link", "add"] + cmd[0] + ["type", "veth", "peer"] + cmd[1]
cmd = ["ip", "link", "add"] + cmd[0] + ["type", "veth", "peer"] + cmd[1]
execute(cmd)
execute(cmd)
...
@@ -244,24 +214,24 @@ def set_if(iface, recover = True):
...
@@ -244,24 +214,24 @@ def set_if(iface, recover = True):
interface = get_real_if(iface)
interface = get_real_if(iface)
_ils = ["ip", "link", "set", "dev", interface.name]
_ils = ["ip", "link", "set", "dev", interface.name]
diff = iface - interface
diff = iface - interface
c
omman
ds = []
c
m
ds = []
if diff.name:
if diff.name:
c
omman
ds.append(_ils + ["name", diff.name])
c
m
ds.append(_ils + ["name", diff.name])
if diff.mtu:
if diff.mtu:
c
omman
ds.append(_ils + ["mtu", str(diff.mtu)])
c
m
ds.append(_ils + ["mtu", str(diff.mtu)])
if diff.lladdr:
if diff.lladdr:
c
omman
ds.append(_ils + ["address", diff.lladdr])
c
m
ds.append(_ils + ["address", diff.lladdr])
if diff.broadcast:
if diff.broadcast:
c
omman
ds.append(_ils + ["broadcast", diff.broadcast])
c
m
ds.append(_ils + ["broadcast", diff.broadcast])
if diff.
flags
:
if diff.
up != None
:
if diff.flags.up != None:
cmds.append(_ils + ["up" if diff.up else "down"])
commands.append(_ils + ["up" if diff.flags.up else "down"])
if diff.multicast != None:
if diff.flags.multicast != None:
cmds.append(_ils + ["multicast", "on" if diff.multicast else "off"])
commands.append(_ils + ["multicast",
if diff.arp != None:
"on" if diff.flags.multicast
else "off"])
cmds.append(_ils + ["arp", "on" if diff.arp
else "off"])
#print c
omman
ds
#print c
m
ds
for c in c
omman
ds:
for c in c
m
ds:
try:
try:
execute(c)
execute(c)
except:
except:
...
@@ -311,6 +281,8 @@ def set_addr(iface, recover = True):
...
@@ -311,6 +281,8 @@ def set_addr(iface, recover = True):
raise
raise
return get_real_if(iface)
return get_real_if(iface)
# Useful stuff
def execute(cmd):
def execute(cmd):
print " ".join(cmd)#; return
print " ".join(cmd)#; return
null = open('
/
dev
/
null
', '
r
+
')
null = open('
/
dev
/
null
', '
r
+
')
...
@@ -320,8 +292,14 @@ def execute(cmd):
...
@@ -320,8 +292,14 @@ def execute(cmd):
raise RuntimeError("Error executing `%s'
:
%
s
" % ("
".join(cmd), err))
raise RuntimeError("Error executing `%s'
:
%
s
" % ("
".join(cmd), err))
def get_real_if(iface):
def get_real_if(iface):
if iface.index != None:
ifdata = get_if_data()
return get_if_data()[0][iface.index]
if isinstance(iface, interface):
else:
if iface.index != None:
return get_if_data()[1][iface.name]
return ifdata[0][iface.index]
else:
return ifdata[1][iface.name]
if isinstance(iface, int):
return ifdata[0][iface]
return ifdata[1][iface]
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