Commit 5100b8e3 authored by zhifan huang's avatar zhifan huang

2 node connect direct test

parent f2d64ed7
import subprocess import subprocess
import time import time
import random
import weakref
import sys
class NetManager(object):
"""contain all the nemu object created, so they can live more time"""
def __init__(self):
self.object = []
self.registrys = {}
class Device(object):
app1 = subprocess.Popen(['unshare', '-n'], stdin=subprocess.PIPE) _id = 0
app2 = subprocess.Popen(['unshare', '-n'], stdin=subprocess.PIPE)
print("pid, 1:{}, 2:{}".format(app1.pid, app2.pid)) @classmethod
subprocess.check_call(['ip', 'link', 'add', 'veth0', 'netns', str(app1.pid) ,'type', 'veth', 'peer', 'veth1', 'netns', str(app2.pid)]) def get_id(cls):
cls._id += 1
return cls._id
subprocess.check_call(['nsenter', '-t', str(app1.pid), '-n'] + ['ip', 'link', 'set', 'up', 'veth0']) def __init__(self, type, name=None):
"""
type: device type, str
name: device name, str. if not set, generate one name
"""
# name if name else .....
self.type = type
self.name = name or "{}-{}".format(type, self.get_id())
self.ips = []
self._up = False
subprocess.check_call(['nsenter', '-t', str(app2.pid), '-n'] + ['ip', 'link', 'set', 'up', 'veth1']) @property
subprocess.check_call(['nsenter', '-t', str(app1.pid), '-n'] + ['ip', 'addr', 'add', 'dev', 'veth0', '10.1.1.1/24']) def up(self):
subprocess.check_call(['nsenter', '-t', str(app2.pid), '-n'] + ['ip', 'addr', 'add', 'dev', 'veth1', '10.1.1.2/24']) "bool value control device up or not"
# subprocess.check_call(['nsenter', '-t', str(app1.pid), '-n','ip', 'route', 'add', '10.1.1.0/24', 'via', '10.1.1.1']) return self._up
# 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"])
print("good bye!") @up.setter
\ No newline at end of file def up(self, value):
value = "up" if value else "down"
self.net.run(['ip', 'link', 'set', 'up', self.name])
def add_ip4(self, address, prefix):
ip = "{}/{}".format(address, prefix)
self.ips.append(address)
self.net.run(['ip', 'addr', 'add', ip, 'dev', self.name])
class Netns(object):
"""a network namespace"""
def __init__(self):
self.devices = []
self.app = subprocess.Popen(['unshare', '-n'], stdin=subprocess.PIPE)
self.pid = self.app.pid
lo = Device("lo", name="lo")
self.add_device(lo)
lo.up = True
self.run(['sysctl', '-w', 'net.ipv4.ip_forward=1'])
self.run(['sysctl', '-w', 'net.ipv6.conf.default.forwarding=1'], stderr=sys.stderr)
def Popen(self, cmd, stdout=sys.stdout, stderr=sys.stderr,**kw):
""" a wrapper for subprocess.Popen"""
return subprocess.Popen(['nsenter', '-t', str(self.pid), '-n'] + cmd, stdout=stdout, stderr=stderr, **kw)
def run(self, cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,**kw):
""" wrapper for subprocess.checkout"""
subprocess.check_call(['nsenter', '-t', str(self.pid), '-n'] + cmd, stdout=stdout, stderr=stderr, **kw)
def add_device(self, dev):
self.devices.append(dev)
dev.net = weakref.proxy(self)
@staticmethod
def connect_direct(node1, node2):
"""connect 2 node by veth"""
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
@property
def ip(self):
return self.out.ips[-1]
@property
def out(self):
return self.devices[-1]
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 net_simple():
nm = NetManager()
node1 = Netns()
node2 = Netns()
Netns.connect_direct(node1, node2)
# subprocess.check_call(['nsenter', '-t', str(node2.pid), '-n'] + ["ping", '-c', '1', "10.1.1.1"])
nm.registrys[node1] = [node2]
return nm
if __name__ == "__main__":
main()
print("good bye!")
...@@ -16,7 +16,8 @@ WORK_DIR = Path(__file__).parent.resolve() / "temp_net_test" ...@@ -16,7 +16,8 @@ WORK_DIR = Path(__file__).parent.resolve() / "temp_net_test"
DH_FILE = WORK_DIR / "dh2048.pem" DH_FILE = WORK_DIR / "dh2048.pem"
RE6STNET = "re6stnet" RE6STNET = "re6stnet"
RE6ST_REGISTRY = "re6st-registry" #RE6ST_REGISTRY = "re6st-registry"
RE6ST_REGISTRY = "python -m re6st.cli.registry"
RE6ST_CONF = "re6st-conf" RE6ST_CONF = "re6st-conf"
def initial(): def initial():
...@@ -67,6 +68,10 @@ class Re6stRegistry(object): ...@@ -67,6 +68,10 @@ class Re6stRegistry(object):
self.run() self.run()
# wait the servcice started
while(not self.log.exists()):
time.sleep(0.1)
@classmethod @classmethod
def generate_name(cls): def generate_name(cls):
cls.registry_seq += 1 cls.registry_seq += 1
...@@ -94,6 +99,12 @@ class Re6stRegistry(object): ...@@ -94,6 +99,12 @@ class Re6stRegistry(object):
self.proc = self.node.Popen(cmd, stdout=PIPE, stderr=PIPE) self.proc = self.node.Popen(cmd, stdout=PIPE, stderr=PIPE)
def __del__(self):
try:
self.proc.terminate()
except:
pass
class Re6stNode(object): class Re6stNode(object):
"""class run a re6stnet service on a namespace""" """class run a re6stnet service on a namespace"""
node_seq = -1 node_seq = -1
...@@ -149,7 +160,7 @@ class Re6stNode(object): ...@@ -149,7 +160,7 @@ class Re6stNode(object):
def create_node(self): def create_node(self):
"""create necessary file for node""" """create necessary file for node"""
sys.stderr.write("create file for node {}".format(self.name)) sys.stderr.write("---create file for node {}---\n".format(self.name))
cmd = "{script} --registry {registry_url} --email {email}" cmd = "{script} --registry {registry_url} --email {email}"
cmd = cmd.format(script=RE6ST_CONF, registry_url=self.registry.url, cmd = cmd.format(script=RE6ST_CONF, registry_url=self.registry.url,
email=self.email).split() email=self.email).split()
...@@ -186,3 +197,9 @@ class Re6stNode(object): ...@@ -186,3 +197,9 @@ class Re6stNode(object):
console=self.console).split() console=self.console).split()
cmd += args cmd += args
self.proc = self.node.Popen(cmd, stdout=PIPE, stderr=PIPE) self.proc = self.node.Popen(cmd, stdout=PIPE, stderr=PIPE)
def __del__(self):
try:
self.proc.terminate()
except:
pass
...@@ -7,9 +7,9 @@ import sys ...@@ -7,9 +7,9 @@ import sys
import os import os
from pathlib2 import Path from pathlib2 import Path
from network_build import * import my_net
PING_PATH = Path(__file__).parent.resolve() / "ping.py" PING_PATH = str(Path(__file__).parent.resolve() / "ping.py")
def wait_stable(nodes, timeout=180): def wait_stable(nodes, timeout=180):
sys.stderr.write("wait stalbe\n") sys.stderr.write("wait stalbe\n")
...@@ -19,7 +19,7 @@ def wait_stable(nodes, timeout=180): ...@@ -19,7 +19,7 @@ def wait_stable(nodes, timeout=180):
for node in nodes: for node in nodes:
sub_ips = set(ips) - {node.ip6} sub_ips = set(ips) - {node.ip6}
node.ping_proc = node.node.Popen( node.ping_proc = node.node.Popen(
"python {} --retry -a {} ".format(PING_PATH, ' '.join(sub_ips)),shell=True) ["python", PING_PATH, '--retry', '-a'] + list(sub_ips))
unfinished = list(nodes) unfinished = list(nodes)
# check all the node network can ping each other # check all the node network can ping each other
while len(unfinished): while len(unfinished):
...@@ -41,8 +41,7 @@ def ping_test(nodes): ...@@ -41,8 +41,7 @@ def ping_test(nodes):
for node in nodes: for node in nodes:
sub_ips = set(ips) - {node.ip6} sub_ips = set(ips) - {node.ip6}
node.ping_proc = node.node.Popen( node.ping_proc = node.node.Popen(
"python {} -a {} ".format(PING_PATH, ' '.join(sub_ips)), ["python", PING_PATH, '--retry', '-a'] + list(sub_ips), stdout=subprocess.PIPE)
shell=True, stdout=subprocess.PIPE)
out, _ = node.ping_proc.communicate() out, _ = node.ping_proc.communicate()
unreached = [ ips[addr] for addr in out.split()] unreached = [ ips[addr] for addr in out.split()]
if unreached: if unreached:
...@@ -78,19 +77,20 @@ class TestPing(unittest.TestCase): ...@@ -78,19 +77,20 @@ class TestPing(unittest.TestCase):
"""create a network demo, test the connectivity by ping """create a network demo, test the connectivity by ping
wait the network stable then ping 3 times wait the network stable then ping 3 times
""" """
nm = network_direct() nm = my_net.net_simple()
net = nm.registrys net = nm.registrys
nodes = [] nodes = []
registrys = [] registrys = []
for registry in net: for registry in net:
reg = re6st_wrap.Re6stRegistry(registry, "2001:db8:42::", recreate=False) reg = re6st_wrap.Re6stRegistry(registry, "2001:db8:42::", recreate=True)
reg_node = re6st_wrap.Re6stNode(registry, reg, name=reg.name) reg_node = re6st_wrap.Re6stNode(registry, reg, name=reg.name)
registrys.append(registry) registrys.append(registry)
reg_node.run("--gateway", "--disable-proto", "none", "--ip", registry.ip) reg_node.run("--gateway", "--disable-proto", "none", "--ip", registry.ip)
nodes.append(reg_node) nodes.append(reg_node)
for m in net[registry]: for m in net[registry]:
print("add nodes")
node = re6st_wrap.Re6stNode(m, reg) node = re6st_wrap.Re6stNode(m, reg)
node.run("-i" + m.iface.name) node.run("-i" + m.out.name)
nodes.append(node) nodes.append(node)
wait_stable(nodes) wait_stable(nodes)
......
...@@ -95,7 +95,7 @@ setup( ...@@ -95,7 +95,7 @@ setup(
install_requires = ['pyOpenSSL >= 0.13', 'miniupnpc'], install_requires = ['pyOpenSSL >= 0.13', 'miniupnpc'],
extras_require = { extras_require = {
'geoip': ['geoip2'], 'geoip': ['geoip2'],
'test': ['mock', 'pathlib2', 'nemu', 'python-unshare', 'passfd'] 'test': ['mock', 'pathlib2', 'multiping']
}, },
#dependency_links = [ #dependency_links = [
# "http://miniupnp.free.fr/files/download.php?file=miniupnpc-1.7.20120714.tar.gz#egg=miniupnpc-1.7", # "http://miniupnp.free.fr/files/download.php?file=miniupnpc-1.7.20120714.tar.gz#egg=miniupnpc-1.7",
......
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