Commit 1c601542 authored by zhifan huang's avatar zhifan huang

add demo

parent 6b4aec14
......@@ -3,6 +3,8 @@ from subprocess import PIPE
import weakref
import sys
import logging
import time
import ipaddress
class NetManager(object):
"""contain all the nemu object created, so they can live more time"""
......@@ -46,14 +48,6 @@ class Device(object):
self.ips.append(address)
self.net.run(['ip', 'addr', 'add', ip, 'dev', self.name])
class Bridge(Device):
def __init__(self, netns, name=None):
super(Bridge, self).__init__("bridge", name)
netns.add_device(self)
netns.run(['ip', 'link', 'add', self.name, 'type', 'bridge'])
self.up = True
class Netns(object):
"""a network namespace"""
......@@ -69,10 +63,7 @@ class Netns(object):
self.devices = []
self.app = subprocess.Popen(['unshare', '-n'], stdin=PIPE)
self.pid = self.app.pid
lo = Device("lo", name="lo")
self.add_device(lo)
lo.up = True
self.add_device_lo()
self.run(['sysctl', '-w', 'net.ipv4.ip_forward=1'], stdout=PIPE)
self.run(['sysctl', '-w', 'net.ipv6.conf.default.forwarding=1'], stdout=PIPE)
......@@ -91,63 +82,75 @@ class Netns(object):
def add_device(self, dev):
self.devices.append(dev)
dev.net = weakref.proxy(self)
def add_device_lo(self):
lo = Device("lo", name="lo")
self.add_device(lo)
lo.up = True
def add_device_bridge(self):
""" create a bridge in the netns"""
br = Device("bridge")
self.add_device(br)
self.bridge = br
self.run(['ip', 'link', 'add', br.name, 'type', 'bridge'])
br.up = True
def add_route(self, net, *args):
self.run(['ip', 'route', 'add', net] + list(args))
@staticmethod
def connect_direct(node1, node2):
"""connect 2 node by veth"""
"""create veths between 2 netns
no difference if node1 and node2 changed
node1(self): Netns
node2: Netns
"""
dev1 = Device("veth")
dev2 = Device("veth")
node1.add_device(dev1)
node2.add_device(dev2)
subprocess.check_call(['ip', 'link', 'add', dev1.name, 'netns', str(node1.pid) ,'type', 'veth', 'peer', dev2.name, 'netns', str(node2.pid)])
dev1.add_ip4("10.1.1.1", prefix=24)
dev2.add_ip4("10.1.1.2", prefix=24)
dev1.up = dev2.up = True
return dev1, dev2
def connect_bridge(self, bridge):
dev1 = Device("veth")
dev2 = Device("veth")
def connect_router(self, router):
"""create veths between 2 netns, and set one veth to the bridge in router
router: Netns
"""
if not hasattr(router, "bridge"):
raise("router should have a bridge")
dev1, dev2 = self.connect_direct(router)
self.add_device(dev1)
bridge.net.add_device(dev2)
router.add_device(dev2)
subprocess.check_call(['ip', 'link', 'add', dev1.name, 'netns', str(self.pid) ,'type', 'veth', 'peer', dev2.name, 'netns', str(bridge.net.pid)])
bridge.net.run(['ip', 'link', 'set', dev2.name, 'master', bridge.name])
dev1.up = dev2.up = True
router.run(['ip', 'link', 'set', dev2.name, 'master', router.bridge.name])
return dev1, dev2
def main():
app1 = Netns()
app2 = Netns()
print("pid, 1:{}, 2:{}".format(app1.pid, app2.pid))
Netns.connect_direct(app1, app2)
# subprocess.check_call(['nsenter', '-t', str(app1.pid), '-n','ip', 'route', 'add', '10.1.1.0/24', 'via', '10.1.1.1'])
# subprocess.check_call(['ip', 'route', 'add', '10.1.1.0/24', 'via', '10.1.1.2'])
subprocess.check_call(['nsenter', '-t', str(app2.pid), '-n'] + ["ping", "10.1.1.1"])
def connectible_test(nm):
"""test each node can ping to their registry
nm: NetManger
"""
for reg in nm.registrys:
for node in nm.registrys[reg]:
app0 = node.Popen(["ping", "-c", "1", reg.ip], stdout=subprocess.PIPE)
app0 = node.Popen(["ping", "-c", "1", reg.ip], stdout=PIPE)
ret = app0.wait()
assert ret == 0, "network construct failed"
assert ret == 0, "network construct failed {} to {}".format(node.ip, reg.ip)
logging.debug("each node can ping to their registry")
def net_simple():
""" registry .1 ------ .2 node
10.1.1
"""
nm = NetManager()
node1 = Netns()
node2 = Netns()
Netns.connect_direct(node1, node2)
dev1, dev2 = node1.connect_direct(node2)
dev1.add_ip4("10.1.1.1", prefix=24)
dev2.add_ip4("10.1.1.2", prefix=24)
nm.registrys[node1] = [node2]
connectible_test(nm)
......@@ -156,18 +159,19 @@ def net_simple():
def net_route():
"""netns connect by a route(bridge)"""
nm = NetManager()
router = Netns()
br = Bridge(router)
router.add_device_bridge()
registry = Netns()
node1 = Netns()
node2 = Netns()
veth_r, _ = registry.connect_bridge(br)
veth_n1, _ = node1.connect_bridge(br)
veth_n2, _ = node2.connect_bridge(br)
veth_r = registry.connect_router(router)
veth_n1, _ = node1.connect_router(router)
veth_n2, _ = node2.connect_router(router)
veth_r.add_ip4("192.168.1.1", 24)
veth_n1.add_ip4("192.168.1.2", 24)
......@@ -179,7 +183,101 @@ def net_route():
connectible_test(nm)
return nm
def net_demo():
# Underlying network:
#
# registry .2------ ------.2 registry2
# | |
# 10.0.0| |10.3.0
# .1 | |.1
# ---------------Internet----------------
# |.1 |.1 |.1
# |10.1.0 |10.2.0 |
# |.2 |.2 |
# gateway1 gateway2 s3:10.0.1
# |.1 |.1 |.2 |.3 |.4
# s1:10.1.1 --s2:10.2.1-- m6 m7 m8
# |.2 |.3 |.2 |.3 |.4 |.5
# m1 m2 m3 m4 m5 m10
#
nm = NetManager()
internet = Netns()
gateway1 = Netns()
router1 = Netns()
gateway2 = Netns()
router2 = Netns()
router3 = Netns()
registry = Netns()
node1 = Netns()
node2 = Netns()
node3 = Netns()
node4 = Netns()
node5 = Netns()
node6 = Netns()
node7 = Netns()
node8 = Netns()
router1.add_device_bridge()
router2.add_device_bridge()
router3.add_device_bridge()
veth_re, veth_it1 = registry.connect_direct(internet)
veth_g1_1, veth_it2 = gateway1.connect_direct(internet)
veth_g2_1, veth_it3 = gateway2.connect_direct(internet)
veth_it1.add_ip4("10.0.0.1", 24)
veth_re.add_ip4("10.0.0.2", 24)
registry.add_route("10.0.0.0/8", 'via', "10.0.0.1")
veth_it2.add_ip4("10.1.0.1", 24)
veth_g1_1.add_ip4("10.1.0.2", 24)
gateway1.add_route("10.0.0.0/8", 'via', "10.1.0.1")
ip = ipaddress.ip_address(u"10.1.1.1")
for node in [gateway1, node1, node2]:
dev, _ = node.connect_router(router1)
dev.add_ip4(str(ip), 24)
ip += 1
for node in [node1, node2]:
node.add_route("10.0.0.0/8", 'via', "10.1.1.1")
veth_it3.add_ip4("10.2.0.1", 24)
veth_g2_1.add_ip4("10.2.0.2", 24)
gateway2.add_route("10.0.0.0/8", 'via', "10.2.0.1")
ip = ipaddress.ip_address(u"10.2.1.1")
for node in [gateway2, node3, node4, node5]:
dev, _ = node.connect_router(router2)
dev.add_ip4(str(ip), 24)
ip += 1
for node in [node3, node4, node5]:
node.add_route("10.0.0.0/8", 'via', "10.2.1.1")
ip = ipaddress.ip_address(u"10.0.1.1")
for node in [internet, node6, node7, node8]:
dev, _ = node.connect_router(router3)
dev.add_ip4(str(ip), 24)
ip += 1
for node in [node6, node7, node8]:
node.add_route("10.0.0.0/8", 'via', "10.0.1.1")
internet.add_route("10.1.0.0/16", 'via', "10.1.0.2")
internet.add_route("10.2.0.0/16", 'via', "10.2.0.2")
# time.sleep(100000)
nm.object += [internet, gateway1, gateway2, router1, router2, router3]
nm.registrys[registry] = [node1, node2, node3, node4, node5, node6, node7, node8]
connectible_test(nm)
return nm
if __name__ == "__main__":
net_route()
net_demo()
print("good bye!")
......@@ -38,7 +38,7 @@ def ip_to_serial(ip6):
class Re6stRegistry(object):
"""class run a re6st-registry service on a namespace"""
registry_seq = -1
registry_seq = 0
def __init__(self, node, ip6, recreate=False):
self.node = node
......@@ -117,7 +117,7 @@ class Re6stRegistry(object):
class Re6stNode(object):
"""class run a re6stnet service on a namespace"""
node_seq = -1
node_seq = 0
def __init__(self, node, registry, name=None, recreate=False):
"""
......
......@@ -89,7 +89,7 @@ class TestPing(unittest.TestCase):
"""create a network demo, test the connectivity by ping
wait the network stable then ping 3 times
"""
nm = my_net.net_simple()
nm = my_net.net_demo()
nodes, registrys = deploy_re6st(nm)
wait_stable(nodes)
......@@ -98,17 +98,17 @@ class TestPing(unittest.TestCase):
self.assertFalse(ping_test(nodes), "N.{} ping test failed".format(i))
def test_routeur(self):
"""create a network demo, test the connectivity by ping
wait the network stable then ping 3 times
"""
nm = my_net.net_route()
nodes, registrys = deploy_re6st(nm)
# def test_routeur(self):
# """create a network demo, test the connectivity by ping
# wait the network stable then ping 3 times
# """
# nm = my_net.net_route()
# nodes, registrys = deploy_re6st(nm)
wait_stable(nodes)
for i in range(3):
time.sleep(20)
self.assertFalse(ping_test(nodes), "N.{} ping test failed".format(i))
# wait_stable(nodes)
# for i in range(3):
# time.sleep(20)
# self.assertFalse(ping_test(nodes), "N.{} ping test failed".format(i))
if __name__ == "__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