Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from pyroute2.common import map_namespace
from pyroute2.netlink import nlmsg
from pyroute2.netlink import nla
FR_ACT_UNSPEC = 0
FR_ACT_TO_TBL = 1
FR_ACT_GOTO = 2
FR_ACT_NOP = 3
FR_ACT_BLACKHOLE = 6
FR_ACT_UNREACHABLE = 7
FR_ACT_PROHIBIT = 8
(FR_ACT_NAMES, FR_ACT_VALUES) = map_namespace('FR_ACT', globals())
class fibmsg(nlmsg):
'''
IP rule message
C structure::
struct fib_rule_hdr {
__u8 family;
__u8 dst_len;
__u8 src_len;
__u8 tos;
__u8 table;
__u8 res1; /* reserved */
__u8 res2; /* reserved */
__u8 action;
__u32 flags;
};
def decode(self):
nla_base.decode(self)
data_length = len(self['value'])
element_size = struct.calcsize(self.fmt)
array_size = data_length // element_size
trail = (data_length % element_size) or -data_length
data = self['value'][:-trail]
fmt = '%s%i%s' % (self.fmt[:-1], array_size, self.fmt[-1:])
self.value = struct.unpack(fmt, data)
array_size = data_length // element_size
trail = (data_length % element_size) or -data_length
data = self['value'][:-trail]
fmt = '%s%i%s' % (self.fmt[:-1], array_size, self.fmt[-1:])
self.value = struct.unpack(fmt, data)
class cdata(nla_base):
'''
Binary data
'''
__slots__ = ()
fields = [('value', 's')]
class string(nla_base):
'''
UTF-8 string.
'''
__slots__ = ()
sql_type = 'TEXT'
fields = [('value', 's')]
def encode(self):
if isinstance(self['value'], str) and sys.version[0] == '3':
self['value'] = bytes(self['value'], 'utf-8')
nla_base.encode(self)
def decode(self):
nla_base.decode(self)
def test_rename_plus_ipv6(self):
require_user('root')
mtu = 1280 # mtu must be >= 1280 if you plan to use IPv6
txqlen = 2000
nsid = str(uuid4())
ipdb_main = IPDB()
ipdb_test = IPDB(nl=NetNS(nsid))
if1 = uifname()
if2 = uifname()
if3 = uifname()
# create
ipdb_main.create(kind='veth',
ifname=if1,
peer=if2,
mtu=mtu,
txqlen=txqlen).commit()
# move
with ipdb_main.interfaces[if2] as veth:
veth.net_ns_fd = nsid
# set it up
foo = str(uuid4())
bar = str(uuid4())
ifA = uifname()
ifB = uifname()
netnsmod.create(foo)
netnsmod.create(bar)
with IPDB(nl=NetNS(foo)) as ip:
ip.create(ifname=ifA,
kind='veth',
peer={'ifname': ifB,
'net_ns_fd': bar}).commit()
assert ifA in ip.interfaces.keys()
assert ifB not in ip.interfaces.keys()
with IPDB(nl=NetNS(bar)) as ip:
assert ifA not in ip.interfaces.keys()
assert ifB in ip.interfaces.keys()
ip.interfaces[ifB].remove().commit()
assert ifA not in ip.interfaces.keys()
assert ifB not in ip.interfaces.keys()
with IPDB(nl=NetNS(foo)) as ip:
assert ifA not in ip.interfaces.keys()
assert ifB not in ip.interfaces.keys()
netnsmod.remove(foo)
netnsmod.remove(bar)
def test_veth_peer_attrs(self):
require_user('root')
ifA = self.get_ifname()
ns = str(uuid.uuid4())
addr_ext = '06:00:00:00:02:02'
addr_int = '06:00:00:00:02:03'
with IPDB(nl=NetNS(ns)) as nsdb:
veth = self.ip.create(**{'ifname': 'x' + ifA,
'kind': 'veth',
'address': addr_ext,
'peer': {'ifname': ifA,
'address': addr_int,
'net_ns_fd': ns}})
veth.commit()
assert nsdb.interfaces[ifA]['address'] == addr_int
assert self.ip.interfaces['x' + ifA]['address'] == addr_ext
with veth:
veth.remove()
netns.remove(ns)
def test_commit_barrier(self):
require_user('root')
ifname = uifname()
# barrier 0
try:
ip = IPDB()
config.commit_barrier = 0
ts1 = time.time()
ip.create(ifname=ifname, kind='dummy').commit()
ts2 = time.time()
assert 0 < (ts2 - ts1) < 2
except:
raise
finally:
config.commit_barrier = 0.2
ip.interfaces[ifname].remove().commit()
ip.release()
def test_rename_plus_ipv6(self):
require_user('root')
mtu = 1280 # mtu must be >= 1280 if you plan to use IPv6
txqlen = 2000
nsid = str(uuid4())
ipdb_main = IPDB()
ipdb_test = IPDB(nl=NetNS(nsid))
if1 = uifname()
if2 = uifname()
if3 = uifname()
# create
ipdb_main.create(kind='veth',
ifname=if1,
peer=if2,
mtu=mtu,
txqlen=txqlen).commit()
# move
with ipdb_main.interfaces[if2] as veth:
veth.net_ns_fd = nsid
# set it up
with ipdb_test.interfaces[if2] as veth:
veth.add_ip('fdb3:84e5:4ff4:55e4::1/64')
def test_bridge_deps(self):
self.if_br0 = uifname()
self.if_br0p0 = uifname()
self.if_br0p1 = uifname()
ifaddr1 = self.ifaddr()
ifaddr2 = self.ifaddr()
router = self.ifaddr()
dst = str(self.ipnets[1].network)
(self
.interfaces
.append(self
.ndb
.interfaces
.create(ifname=self.if_br0,
kind='bridge',
state='up')
.commit()['index']))
(self
.interfaces
def test_view_constraints_pipeline(self):
ifname = uifname()
ifaddr = self.ifaddr()
(self
.ndb
.interfaces
.constraint('target', self.netns)
.create(ifname=ifname, kind='dummy')
.set('address', '00:11:22:33:44:55')
.set('state', 'up')
.ipaddr
.create(address=ifaddr, prefixlen=24)
.commit())
self._assert_test_view(ifname, ifaddr)