Commit b8916662 authored by Alina Quereilhac's avatar Alina Quereilhac

TapNodeInterface test running.

parent ca244b1c
......@@ -107,6 +107,7 @@ class NodeInterface(NSInterface):
def __init__(self, node):
"""Create a new interface. `node' is the name space in which this
interface should be put."""
self._slave = None
if1 = netns.iproute.interface(name = self._gen_if_name())
if2 = netns.iproute.interface(name = self._gen_if_name())
ctl, ns = netns.iproute.create_if_pair(if1, if2)
......@@ -176,6 +177,7 @@ class ImportedNodeInterface(NSInterface):
On destruction, the interface will be restored to the original name space and
will try to restore the original state."""
def __init__(self, node, iface, migrate = False):
self._slave = None
self._migrate = migrate
if self._migrate:
iface = node._slave.get_if_data(iface)
......@@ -192,7 +194,7 @@ class ImportedNodeInterface(NSInterface):
super(ImportedNodeInterface, self).__init__(node, iface.index)
def destroy(self): # override: restore as much as possible
if self._slave:
if self._slave:
if self.index in self._slave.get_if_data():
if self._migrate:
self._slave.set_if(self._original_state)
......@@ -210,10 +212,11 @@ class TapNodeInterface(NSInterface):
def __init__(self, node):
"""Create a new tap interface. 'node' is the name space in which this
interface should be put."""
self._fd = None
self._slave = None
iface = netns.iproute.interface(name = self._gen_if_name())
self._fd = netns.iproute.create_tap(iface)
iface, self._fd = netns.iproute.create_tap(iface)
netns.iproute.change_netns(iface.name, node.pid)
iface = node.get_interface(iface.name)
super(TapNodeInterface, self).__init__(node, iface.index)
@property
......@@ -294,6 +297,7 @@ class ImportedInterface(ExternalInterface):
destruction, the code will try to restore the interface to the state it was
in before being imported into netns."""
def __init__(self, iface):
self._original_state = None
iface = netns.iproute.get_if(iface)
self._original_state = iface.copy()
super(ImportedInterface, self).__init__(iface.index)
......
......@@ -899,6 +899,10 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
def create_tap(iface):
"""Creates a tap device and returns the associated file descriptor"""
if isinstance(iface, str):
iface = interface(name = iface)
assert iface.name
IFF_TAP = 0x0002
IFF_NO_PI = 0x1000
TUNSETIFF = 0x400454ca
......@@ -910,5 +914,16 @@ def create_tap(iface):
if err < 0:
os.close(fd)
raise RuntimeError("Could not configure device %s" % iface.name)
return fd
try:
set_if(iface)
except:
(t, v, bt) = sys.exc_info()
try:
os.close(fd)
except:
pass
raise t, v, bt
interfaces = get_if_data()[1]
return interfaces[iface.name], fd
#!/usr/bin/env python
# vim:ts=4:sw=4:et:ai:sts=4
import grp, os, pwd, time, threading, unittest
import grp, os, pwd, select, time, threading, unittest
import netns, test_util
class TestConfigure(unittest.TestCase):
......@@ -99,60 +99,33 @@ class TestGlobal(unittest.TestCase):
@test_util.skipUnless(os.getuid() == 0, "Test requires root privileges")
def test_run_ping_tap(self):
"""This test simulates a point to point connection between two hosts using two tap devices"""
class ChannelThread:
def __init__(self):
self.stop = False
self.thread = None
def _run(self, fd1, fd2):
while not self.stop:
s = os.read(fd1, 65536)
os.write(fd2, s)
def start(self, fd1, fd2):
self.thread = threading.Thread(target = self._run, args=[fd1, fd2])
self.thread.start()
n1 = netns.Node()
n2 = netns.Node()
i1 = n1.add_if()
tap1 = n1.add_tap()
tap2 = n2.add_tap()
i2 = n2.add_if()
i1.up = tap1.up = tap2.up = i2.up = True
tap1.up = tap2.up = True
i1.add_v4_address('10.0.0.1', 24)
tap1.add_v4_address('10.0.1.1', 24)
tap2.add_v4_address('10.0.1.2', 24)
i2.add_v4_address('10.0.2.2', 24)
n2.add_route(prefix = '10.0.0.0', prefix_len = 24, nexthop = '10.0.1.2')
n1.add_route(prefix = '10.0.2.0', prefix_len = 24, nexthop = '10.0.1.1')
# communication forth between the two taps
thread1 = ChannelThread()
thread1.start(tap1.fd, tap2.fd)
# communication back between the two taps
thread2 = ChannelThread()
thread2.start(tap2.fd, tap1.fd)
null = file('/dev/null', 'wb')
a = n1.Popen(['ping', '-qc1', '10.0.2.1'], stdout = null)
self.assertEquals(a.wait(), 0)
a = n1.Popen(['ping', '-qc1', '10.0.1.2'], stdout = null)
print 'jhola'
thread1.stop = True
thread2.stop = True
while(True):
ready = select.select([tap1.fd, tap2.fd], [], [], 0.1)[0]
if ready:
s = os.read(ready[0], 65536)
if ready[0] == tap1.fd:
os.write(tap2.fd, s)
else:
os.write(tap1.fd, s)
if not s:
break
if a.poll() != None:
break
os.write(tap1.fd, "0")
os.write(tap2.fd, "0")
thread1.thread.join()
thread2.thread.join()
print 'lalala'
self.assertEquals(a.wait(), 0)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment