Commit fa067176 authored by Martín Ferrari's avatar Martín Ferrari

Style adjustments

parent 8de4982b
......@@ -4,8 +4,11 @@ BUILDDIR = build
DISTDIR = dist
# stupid distutils, it's broken in so many ways
SUBBUILDDIR = $(shell python -c 'import distutils.util, sys; print "lib.%s-%s" % (distutils.util.get_platform(), sys.version[0:3])')
PYTHON25 := $(shell python -c 'import sys; v = sys.version_info; print (1 if v[0] <= 2 and v[1] <= 5 else 0)')
SUBBUILDDIR = $(shell python -c 'import distutils.util, sys; \
print "lib.%s-%s" % (distutils.util.get_platform(), \
sys.version[0:3])')
PYTHON25 := $(shell python -c 'import sys; v = sys.version_info; \
print (1 if v[0] <= 2 and v[1] <= 5 else 0)')
ifeq ($(PYTHON25),0)
BUILDDIR := $(BUILDDIR)/$(SUBBUILDDIR)
......
......@@ -29,8 +29,8 @@ if1 = b.add_if(mtu = 1492)
# for using with a tun device, to connect to the outside world
if2 = b.import_if('tun0')
# each Switch is a linux bridge, all the parameters are applied to the associated
# interfaces as tc qdiscs.
# each Switch is a linux bridge, all the parameters are applied to the
# associated interfaces as tc qdiscs.
switch0 = netns.Switch(bandwidth = 100 * 1024 * 1024,
delay = 0.01, delay_jitter = 0.001,
delay_correlation = 0.25, delay_distribution = 'normal',
......
......@@ -6,7 +6,8 @@ from distutils.core import setup, Extension, Command
setup(
name = 'netns',
version = '0.1',
description = 'A framework for creating emulated networks in a single host and run experiments on them',
description = '''A framework for creating emulated networks in a
single host and run experiments on them''',
# long_description = longdesc,
author = 'Martin Ferrari',
author_email = 'martin.ferrari@gmail.com',
......
......@@ -142,14 +142,14 @@ class NodeInterface(NSInterface):
class P2PInterface(NSInterface):
"""Class to create and handle point-to-point interfaces between name
spaces, without using Switch objects. Those do not allow any kind of traffic
shaping.
spaces, without using Switch objects. Those do not allow any kind of
traffic shaping.
As two interfaces need to be created, instead of using the class
constructor, use the P2PInterface.create_pair() static method."""
@staticmethod
def create_pair(node1, node2):
"""Create and return a pair of connected P2PInterface objects, assigned
to name spaces represented by `node1' and `node2'."""
"""Create and return a pair of connected P2PInterface objects,
assigned to name spaces represented by `node1' and `node2'."""
if1 = netns.iproute.interface(name = P2PInterface._gen_if_name())
if2 = netns.iproute.interface(name = P2PInterface._gen_if_name())
pair = netns.iproute.create_if_pair(if1, if2)
......@@ -182,10 +182,10 @@ class P2PInterface(NSInterface):
class ImportedNodeInterface(NSInterface):
"""Class to handle already existing interfaces inside a name space:
real devices, tun devices, etc.
The flag 'migrate' in the constructor indicates that the interface was migrated
inside the name space.
On destruction, the interface will be restored to the original name space and
will try to restore the original state."""
The flag 'migrate' in the constructor indicates that the interface was
migrated inside the name space.
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
......@@ -241,7 +241,8 @@ class TapNodeInterface(NSInterface):
pass
class ExternalInterface(Interface):
"""Add user-facing methods for interfaces that run in the main namespace."""
"""Add user-facing methods for interfaces that run in the main
namespace."""
@property
def control(self):
# This is *the* control interface
......@@ -301,11 +302,11 @@ class SlaveInterface(ExternalInterface):
class ImportedInterface(ExternalInterface):
"""Class to handle already existing interfaces. Analogous to
ImportedNodeInterface, this class only differs in that the interface is not
migrated inside the name space. This kind of interfaces can only be
ImportedNodeInterface, this class only differs in that the interface is
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
destruction, the code will try to restore the interface to the state it was
in before being imported into netns."""
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)
......@@ -329,7 +330,8 @@ class Switch(ExternalInterface):
def __init__(self, **args):
"""Creates a new Switch object, which models a linux bridge device.
Parameters are passed to the set_parameters() method after creation."""
Parameters are passed to the set_parameters() method after
creation."""
iface = netns.iproute.create_bridge(self._gen_br_name())
super(Switch, self).__init__(iface.index)
......
# vim:ts=4:sw=4:et:ai:sts=4
import copy, os, re, socket, subprocess, sys, struct
from fcntl import ioctl
import copy, fcntl, os, re, socket, struct, subprocess, sys
from netns.environ import *
# helpers
......@@ -182,8 +181,9 @@ class bridge(interface):
class address(object):
"""Class for internal use. It is mostly a data container used to easily
pass information around; with some convenience methods. __eq__ and __hash__
are defined just to be able to easily find duplicated addresses."""
pass information around; with some convenience methods. __eq__ and
__hash__ are defined just to be able to easily find duplicated
addresses."""
# broadcast is not taken into account for differentiating addresses
def __eq__(self, o):
if not isinstance(o, address):
......@@ -502,8 +502,8 @@ def _sysfs_read_br(brname):
ports = os.listdir(p2))
def get_bridge_data():
# brctl stinks too much; it is better to directly use sysfs, it is probably
# stable by now
# brctl stinks too much; it is better to directly use sysfs, it is
# probably stable by now
byidx = {}
bynam = {}
ports = {}
......@@ -912,7 +912,7 @@ def create_tap(iface):
fd = os.open("/dev/net/tun", os.O_RDWR)
if fd == -1:
raise RuntimeError("Could not open /dev/net/tun")
err = ioctl(fd, TUNSETIFF, struct.pack("16sH", iface.name, mode))
err = fcntl.ioctl(fd, TUNSETIFF, struct.pack("16sH", iface.name, mode))
if err < 0:
os.close(fd)
raise RuntimeError("Could not configure device %s" % iface.name)
......
......@@ -3,7 +3,7 @@
import os, socket, sys, traceback, unshare, weakref
from netns.environ import *
import netns.protocol, netns.subprocess_, netns.interface
import netns.interface, netns.protocol, netns.subprocess_
__all__ = ['Node', 'get_nodes', 'import_if']
......@@ -19,8 +19,8 @@ class Node(object):
"""Create a new node in the emulation. Implemented as a separate
process in a new network name space. Requires root privileges to run.
If keepns is true, the network name space is not created and can be run
as a normal user, for testing. If debug is true, details of the
If keepns is true, the network name space is not created and can be
run as a normal user, for testing. If debug is true, details of the
communication protocol are printed on stderr."""
# Initialize attributes, in case something fails during __init__
......@@ -119,7 +119,8 @@ class Node(object):
ifaces = self._slave.get_if_data()
for i in ifaces:
if i not in self._interfaces:
iface = netns.interface.ImportedNodeInterface(self, i, migrate = True)
iface = netns.interface.ImportedNodeInterface(self, i,
migrate = True)
self._auto_interfaces.append(iface) # keep it referenced!
self._interfaces[i] = iface
# by the way, clean up _interfaces
......
......@@ -63,8 +63,8 @@ _proc_commands = {
}
class Server(object):
"""Class that implements the communication protocol and dispatches calls to
the required functions. Also works as the main loop for the slave
"""Class that implements the communication protocol and dispatches calls
to the required functions. Also works as the main loop for the slave
process."""
def __init__(self, rfd, wfd, debug = 0):
# Dictionary of valid commands
......
#!/usr/bin/env python
# vim:ts=4:sw=4:et:ai:sts=4
import grp, os, pwd, select, time, threading, unittest
import grp, os, pwd, select, time, unittest
import netns, test_util
class TestConfigure(unittest.TestCase):
......@@ -86,8 +86,10 @@ class TestGlobal(unittest.TestCase):
i2b.add_v4_address('10.0.1.1', 24)
i3.add_v4_address('10.0.1.2', 24)
n1.add_route(prefix = '10.0.1.0', prefix_len = 24, nexthop = '10.0.0.2')
n3.add_route(prefix = '10.0.0.0', prefix_len = 24, nexthop = '10.0.1.1')
n1.add_route(prefix = '10.0.1.0', prefix_len = 24,
nexthop = '10.0.0.2')
n3.add_route(prefix = '10.0.0.0', prefix_len = 24,
nexthop = '10.0.1.1')
null = file('/dev/null', 'wb')
a1 = n1.Popen(['ping', '-qc1', '10.0.1.2'], stdout = null)
......@@ -97,7 +99,8 @@ 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"""
"""This test simulates a point to point connection between two hosts
using two tap devices"""
n1 = netns.Node()
n2 = netns.Node()
......@@ -128,7 +131,8 @@ class TestGlobal(unittest.TestCase):
@test_util.skipUnless(os.getuid() == 0, "Test requires root privileges")
def test_run_ping_tap_routing(self):
"""This test simulates a point to point connection between two hosts using two tap devices"""
"""This test simulates a point to point connection between two hosts
using two tap devices"""
n1 = netns.Node()
n2 = netns.Node()
n3 = netns.Node()
......
......@@ -22,13 +22,17 @@ class TestSwitch(unittest.TestCase):
l.mtu = 3000
ifdata = netns.iproute.get_if_data()[0]
self.assertEquals(ifdata[l.index].mtu, 3000)
self.assertEquals(ifdata[i1.control.index].mtu, 3000, "MTU propagation")
self.assertEquals(ifdata[i2.control.index].mtu, 3000, "MTU propagation")
self.assertEquals(ifdata[i1.control.index].mtu, 3000,
"MTU propagation")
self.assertEquals(ifdata[i2.control.index].mtu, 3000,
"MTU propagation")
i1.mtu = i2.mtu = 3000
self.assertEquals(ifdata[l.index].up, False)
self.assertEquals(ifdata[i1.control.index].up, False, "UP propagation")
self.assertEquals(ifdata[i2.control.index].up, False, "UP propagation")
self.assertEquals(ifdata[i1.control.index].up, False,
"UP propagation")
self.assertEquals(ifdata[i2.control.index].up, False,
"UP propagation")
l.up = True
ifdata = netns.iproute.get_if_data()[0]
......
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