Exemplo n.º 1
0
 def test_create_from_path(self):
     ns_dir = tempfile.mkdtemp()
     # Create namespace
     ns_name = str(uuid4())
     nspath = '%s/%s' % (ns_dir, ns_name)
     temp_ns = NetNS(nspath)
     temp_ns.close()
     fd = open(nspath)
     self._test_create(nspath, fd.fileno())
     fd.close()
     netnsmod.remove(nspath)
     assert ns_name not in netnsmod.listnetns()
     assert ns_name not in netnsmod.listnetns(ns_dir)
Exemplo n.º 2
0
 def test_create_from_path(self):
     ns_dir = tempfile.mkdtemp()
     # Create namespace
     ns_name = str(uuid4())
     nspath = '%s/%s' % (ns_dir, ns_name)
     temp_ns = NetNS(nspath)
     temp_ns.close()
     fd = open(nspath)
     self._test_create(nspath, fd.fileno())
     fd.close()
     netnsmod.remove(nspath)
     assert ns_name not in netnsmod.listnetns()
     assert ns_name not in netnsmod.listnetns(ns_dir)
Exemplo n.º 3
0
 def post(self, nspath):
     if nspath in netns.listnetns():
         abort(500, "Namespace already exists")
     else:
         mynet = NetNS(nspath)
         mynet.close()
         return("Namespace successfully created")
Exemplo n.º 4
0
    def _create_endpoint(self, request, response):
        logger.debug('endpoint.create')

        network_id = request['NetworkID']
        # if no CreateNetwork request received
        namespace = 'sdns' + network_id[:6]
        if namespace not in netns.listnetns():
            self._create_namespace(namespace)
        endpoint_id = request['EndpointID']
        address = request['Interface']['Address']
        ip = IPRoute()

        veth0_name = 'veth%s0' % endpoint_id[:6]
        veth1_name = 'veth%s1' % endpoint_id[:6]
        ip.link('add', ifname=veth0_name, kind='veth', peer=veth1_name)
        veth0 = ip.link_lookup(ifname=veth0_name)[0]
        veth1 = ip.link_lookup(ifname=veth1_name)[0]
        ip.link('set', index=veth0, mtu=1450)
        ip.link('set', index=veth1, mtu=1450)
        ip_addr, mask = address.split('/')
        ip.addr('add', index=veth1, address=ip_addr, mask=int(mask))

        ip.link('set', index=veth0, net_ns_fd=namespace)
        ns = NetNS(namespace)
        ns.link('set', index=veth0, state='up')
        bridge = ns.link_lookup(ifname='shoutbr0')[0]
        ns.link('set', index=veth0, master=bridge)

        ip.close()
        ns.close()
        return {'Interface': {}}
Exemplo n.º 5
0
def create_static_arp():
    arp_table = []

    # Populate our static arp table
    for ns in netns.listnetns():
        arp_table.append({
            "host": ns,
            "mac": get_mac_addr(ns, 'bat0'),
            "ip": get_ip_addr(ns, 'bat0')
        })

    # Assign it to all hosts. Skip self.
    for ns in netns.listnetns():
        for arp in arp_table:
            if arp["host"] != ns:
                os.system("ip netns exec {} arp -s {} {}".format(
                    ns, arp['ip'], arp['mac']))
Exemplo n.º 6
0
 def delete(self, nspath):
     if nspath not in netns.listnetns():
         abort(404, "Namespace not found")
     else:
         with NetNS(nspath) as n:
             n.remove()
             n.close()
         return("Namespace successfully removed")
Exemplo n.º 7
0
def list_network_namespaces(**kwargs):
    """List all network namespace entries.

    :param kwargs: Callers add any filters they use as kwargs
    """
    if cfg.CONF.AGENT.use_helper_for_ns_read:
        return privileged.list_netns(**kwargs)
    else:
        return netns.listnetns(**kwargs)
Exemplo n.º 8
0
def list_network_namespaces(**kwargs):
    """List all network namespace entries.

    :param kwargs: Callers add any filters they use as kwargs
    """
    if cfg.CONF.AGENT.use_helper_for_ns_read:
        return privileged.list_netns(**kwargs)
    else:
        return netns.listnetns(**kwargs)
Exemplo n.º 9
0
    def _do_delete_network(self, request, response):
        logger.debug('network.delete')

        network_id = request['NetworkID']
        namespace = 'sdns' + network_id[:6]
        if namespace in netns.listnetns():
            self._delete_namespace(namespace)

        return {}
Exemplo n.º 10
0
    def test_create(self):
        require_user('root')

        nsid = str(uuid4())
        ipdb_main = IPDB()
        ipdb_test = IPDB(nl=NetNS(nsid))
        if1 = uifname()
        if2 = uifname()

        # create VETH pair
        ipdb_main.create(ifname=if1, kind='veth', peer=if2).commit()

        # move the peer to netns
        with ipdb_main.interfaces[if2] as veth:
            veth.net_ns_fd = nsid

        # assign addresses
        with ipdb_main.interfaces[if1] as veth:
            veth.add_ip('172.16.200.1/24')
            veth.up()

        with ipdb_test.interfaces[if2] as veth:
            veth.add_ip('172.16.200.2/24')
            veth.up()

        # ping peer
        try:
            with open('/dev/null', 'w') as fnull:
                subprocess.check_call(['ping', '-c', '1', '172.16.200.2'],
                                      stdout=fnull,
                                      stderr=fnull)
            ret_ping = True
        except Exception:
            ret_ping = False

        # check ARP
        time.sleep(0.5)
        ret_arp = '172.16.200.1' in list(ipdb_test.interfaces[if2].neighbours)
        # ret_arp = list(ipdb_test.interfaces.v0p1.neighbours)

        # cleanup
        ipdb_main.interfaces[if1].remove().commit()
        ipdb_main.release()
        ipdb_test.release()
        netnsmod.remove(nsid)

        assert ret_ping
        assert ret_arp
        assert nsid not in netnsmod.listnetns()
Exemplo n.º 11
0
def destroy_ns(ns_name):
    """
    Deletes a network namespace

    :param ns_name: The name of the network
    :return: Returns True of the namespace was deleted or did not exist.
    """

    if ns_name not in netns.listnetns():
        log.warning("Namespace %s not found! Doing nothing." % ns_name)
        return True

    netns.remove(ns_name)

    return True
Exemplo n.º 12
0
    def test_create(self):
        require_user('root')

        nsid = str(uuid4())
        ipdb_main = IPDB()
        ipdb_test = IPDB(nl=NetNS(nsid))
        if1 = uifname()
        if2 = uifname()

        # create VETH pair
        ipdb_main.create(ifname=if1, kind='veth', peer=if2).commit()

        # move the peer to netns
        with ipdb_main.interfaces[if2] as veth:
            veth.net_ns_fd = nsid

        # assign addresses
        with ipdb_main.interfaces[if1] as veth:
            veth.add_ip('172.16.200.1/24')
            veth.up()

        with ipdb_test.interfaces[if2] as veth:
            veth.add_ip('172.16.200.2/24')
            veth.up()

        # ping peer
        try:
            with open('/dev/null', 'w') as fnull:
                subprocess.check_call(['ping', '-c', '1', '172.16.200.2'],
                                      stdout=fnull, stderr=fnull)
            ret_ping = True
        except Exception:
            ret_ping = False

        # check ARP
        time.sleep(0.5)
        ret_arp = '172.16.200.1' in list(ipdb_test.interfaces[if2].neighbours)
        # ret_arp = list(ipdb_test.interfaces.v0p1.neighbours)

        # cleanup
        ipdb_main.interfaces[if1].remove().commit()
        ipdb_main.release()
        ipdb_test.release()
        netnsmod.remove(nsid)

        assert ret_ping
        assert ret_arp
        assert nsid not in netnsmod.listnetns()
Exemplo n.º 13
0
 def test_attach(self):
     ns_name = str(uuid4())
     pid = os.fork()
     if pid == 0:
         # child
         while True:
             time.sleep(.1)
     else:
         # parent
         try:
             self._test_create(ns_name, pid=pid)
         finally:
             os.kill(int(pid), signal.SIGTERM)
             os.waitpid(pid, 0)
     netnsmod.remove(ns_name)
     assert ns_name not in netnsmod.listnetns()
Exemplo n.º 14
0
  def __removePvd(self, phyIfaceName, pvdId):
    pvd = self.pvds.get((phyIfaceName, pvdId))
    if (pvd):
      # remove the network namespace associated with the PvD (this in turn removes the PvD network configuration as well)
      if (pvd.netnsName in netns.listnetns()):
        netns.remove(pvd.netnsName)
      # remove the directory containing PvD-related DNS information
      (dnsConfDir, dnsConfFile) = self.__getDnsConfPath(pvd.netnsName)
      if (os.path.exists(dnsConfDir)):
        shutil.rmtree(dnsConfDir, True)
      # remove the PvD record from the PvD manager's log
      del self.pvds[(phyIfaceName, pvdId)]
      LOG.info('PvD {0} received through {1} REMOVED, network namespace {2} deleted, DNS directory {3} deleted, type {4}'.format(pvd.pvdId, pvd.phyIfaceName, pvd.netnsName, dnsConfDir, pvd.pvdInfo.pvdType))
      self.pvdserver.stateChanged ("deleted", pvdId)

    else:
      raise Exception('There is no PvD {0} configured on {1}'.format(pvdInfo.pvdId, phyIfaceName))
Exemplo n.º 15
0
def get_metrics(netns_list=None):
    """Return Metrics for contract values.

    This function will return the highest value of "nf_conntrack_count" from
    all available namespaces.

    :returns: ``dict``
    """
    for key in METRICS.keys():
        METRICS[key]['value'] = get_value(METRICS[key]['path'])

    ns_count = [METRICS['nf_conntrack_count']['value']]
    for ns in netns_list or netns.listnetns():
        with NetNsExec(ns=ns):
            ns_count.append(
                get_value(path='/proc/sys/net/netfilter/nf_conntrack_count'))

    METRICS['nf_conntrack_count']['value'] = max(ns_count)

    return METRICS
Exemplo n.º 16
0
def get_metrics(netns_list=None):
    """Return Metrics for contract values.

    This function will return the highest value of "nf_conntrack_count" from
    all available namespaces.

    :returns: ``dict``
    """
    for key in METRICS.keys():
        METRICS[key]['value'] = get_value(METRICS[key]['path'])

    ns_count = [METRICS['nf_conntrack_count']['value']]
    for ns in netns_list or netns.listnetns():
        with NetNsExec(ns=ns):
            ns_count.append(
                get_value(
                    path='/proc/sys/net/netfilter/nf_conntrack_count'
                )
            )

    METRICS['nf_conntrack_count']['value'] = max(ns_count)

    return METRICS
Exemplo n.º 17
0
 def test_create(self):
     ns_name = str(uuid4())
     self._test_create(ns_name)
     netnsmod.remove(ns_name)
     assert ns_name not in netnsmod.listnetns()
Exemplo n.º 18
0
def main():
    parser = argparse.ArgumentParser(
        description='Configure namespace hosts for simulation.')
    parser.add_argument('--cleanup', action='store_true', default=False)
    parser.add_argument('--nhosts', action='store', type=int)
    parser.add_argument('--batv', action='store_true', default=False)
    parser.add_argument('--hop_penalty', action='store', type=int)
    parser.add_argument('--ogm_interval', action='store', type=int)
    parser.add_argument('--static_arp', action='store_true', default=False)
    parser.add_argument('--gateway', nargs="+")
    parser.add_argument('--verbose', action='store_true', default=False)

    arguments = parser.parse_args()

    GW_IP = "192.168.3.1"

    ip_pool = ip_pool_generator()

    if arguments.cleanup:
        print("Cleaning up...")
        cleanup()

        print("\n-----\nDone!")
    else:

        os.system("modprobe batman-adv")

        if arguments.batv:
            os.system("batctl ra BATMAN_V")
        else:
            os.system("batctl ra BATMAN_IV")

        for i in range(0, arguments.nhosts):
            name = "host{}".format(i)
            tap = "tap{}".format(i)
            bat = "bat0"
            ip = next(ip_pool)
            create_namespace(name)
            create_TAP(name, tap)
            create_batman_interface(name, tap, bat, ip)

            if arguments.hop_penalty:
                set_hop_penalty(name, bat, arguments.hop_penalty)

            if arguments.ogm_interval:
                set_ogm_interval(name, bat, arguments.ogm_interval)

        if arguments.gateway:
            # setup "GW router"
            create_gw_router("gwrouter0", arguments.gateway, GW_IP)

            # set servers
            for gw in arguments.gateway:
                if arguments.verbose:
                    print("Setting {} as server".format(gw))
                set_gateway_mode(gw, 'server')

            # set clients
            for ns in netns.listnetns():
                if ns not in arguments.gateway:
                    if arguments.verbose:
                        print("Setting {} as client".format(ns))
                    set_gateway_mode(ns, 'client')

        if arguments.static_arp:
            create_static_arp()
Exemplo n.º 19
0
 def is_ns_exists(self, ns_name):
     return ns_name in netns.listnetns()
Exemplo n.º 20
0
	def is_exist(self, nsname = 'ns'):
		if nsname in netns.listnetns():
			return True
		else:
			return False
Exemplo n.º 21
0
 def __getNetnsName(self):
   netnsName = None
   while (not netnsName or netnsName in netns.listnetns()):
     self.__netnsIdGenerator += 1;
     netnsName = self.__NETNS_PREFIX + str(self.__netnsIdGenerator)
   return netnsName
Exemplo n.º 22
0
 def get(self):
     return netns.listnetns()
Exemplo n.º 23
0
 def get(self, nspath):
     if nspath in netns.listnetns():
         return NetnsDao(nspath)
     else:
         abort(404, "Namespace " + nspath + " does not exist")
Exemplo n.º 24
0
 def test_create(self):
     ns_name = str(uuid4())
     self._test_create(ns_name)
     netnsmod.remove(ns_name)
     assert ns_name not in netnsmod.listnetns()
Exemplo n.º 25
0
 def put(self, nspath):
     if nspath not in netns.listnetns():
         abort(404, "Namespace not found")
     else:
         pass
Exemplo n.º 26
0
def get_nic_mac_ip(os_version):
    """
    data = {
        ["mac1"]:[
            {"eth0": [ip1, ip2]},
            {"eth0:0": [ip3]}
            {"eth0.1": [ip4]}
            ],
        ["mac2"]:...,
    }
    :return: data
    """
    nic_data, nic_mac, alias_nic = dict(), dict(), list()

    for nic_name, snic_addr_list in psutil.net_if_addrs().items():
        # print(nic_name, snic_addr_list)
        ip_list, mac = list(), ""
        if re.search(r'Windows', os_version, re.I):
            nic_name = nic_name.decode('GBK')
            for item in snic_addr_list:
                if item.family == 2 and item.address != "127.0.0.1":
                    ip_list.append(item.address)
                    # item.netmask
                if item.family == -1 and item.address != "00:00:00:00:00:00":
                    mac = item.address
            if mac == "00-00-00-00-00-00-00-E0" and not ip_list:
                continue
        else:
            if not re.match('en|eth|em|br|bond|tun|team', nic_name, re.I):
                continue
            for item in snic_addr_list:
                # snicaddr(family=2, address='172.20.1.47', netmask='255.255.255.0', broadcast='172.20.1.255', ptp=None)
                if item.family == 2 and item.address != "127.0.0.1":
                    ip_list.append(item.address)
                    # item.netmask
                if item.family == 17 and item.address != "00:00:00:00:00:00":
                    mac = item.address
            if ":" in nic_name and mac == "":
                alias_nic.append([nic_name, ip_list])
                continue
        nic_mac[nic_name] = mac
        if mac not in nic_data:
            nic_data[mac] = [{nic_name: ip_list}]
        else:
            nic_data[mac].append({nic_name: ip_list})

    if not re.search(r'Windows', os_version, re.I):
        # 获取其他 net namespace 的网卡
        from pyroute2 import netns, NetNS, IPDB
        for ns in netns.listnetns():
            ip_db = IPDB(nl=NetNS(ns))
            for nic_name, snic_addr in ip_db.interfaces.items():
                if nic_name in [1, 10, "lo"]:
                    continue
                mac = snic_addr['address']
                ip_list = list()
                for i in snic_addr['ipaddr']:
                    # (('10.0.0.1', 24), )
                    ip_list.append(i[0])
                if mac not in nic_data:
                    nic_data[mac] = [{nic_name: ip_list}]
                else:
                    nic_data[mac].append({nic_name: ip_list})
    for nic in alias_nic:
        # ['eth0:0', ['x.x.x.x']]
        root_nic_name = nic[0].split(":")[0]
        nic_data[nic_mac[root_nic_name]].append({nic[0]: nic[1]})
    return nic_data
Exemplo n.º 27
0
def create_ns(br_name, ns_name, ns_ip, ns_netmask, if_description, add_if):
    """
    :param br_name:
    :param ns_name:
    :param ns_ip:
    :param ns_netmask:
    :param if_description:
    :param add_if:
    :return:
    """

    # start the main network settings database:
    ipdb = IPDB()

    # Check if the bridge exists
    if br_name not in ipdb.interfaces.keys():
        log.fatal("Could not find bridge %s!" % br_name)
        sys.exit(-1)

    # veth naming
    ifname = "%s_%s_p%%d" % (ns_name, if_description)

    if (ifname % 0) in ipdb.interfaces.keys():
        log.fatal("%s already exists as interface!" % (ifname % 0))
        return False

    log.debug("Creating veth pair %s - %s for namespace %s." %
              (ifname % 0, ifname % 1, ns_name))

    if ns_name in netns.listnetns() and not add_if:
        log.warning("Namespace %s already exists!" % ns_name)

    # Create/configure a network namespace
    ipdb_ns = IPDB(nl=NetNS(ns_name))

    # Make sure lo is up in the namespace
    ipdb_ns.interfaces["lo"].up().commit()

    # Create veth pair
    ipdb.create(kind='veth', ifname=(ifname % 0), peer=(ifname % 1)).commit()

    # Move peer veth into the netns
    with ipdb.interfaces[(ifname % 1)] as veth:
        veth.net_ns_fd = ns_name

    with ipdb.interfaces[(ifname % 0)] as veth:
        veth.up()

    # wait for iface to be moved to ns
    time.sleep(0.1)

    ns_ip = '%s/%s' % (ns_ip, ns_netmask)

    log.debug("Assigning IP %s to %s." % (ns_ip, (ifname % 1)))

    with ipdb_ns.interfaces[(ifname % 1)] as veth:
        veth.add_ip(ns_ip)
        veth.up()

    log.debug("Adding port %s to bridge %s." % ((ifname % 0), br_name))

    ipdb.interfaces[br_name].add_port((ifname % 0))

    ipdb.commit()

    return True
Exemplo n.º 28
0
from pyroute2 import netns

#netns.create("custom")
print(netns.listnetns())

Exemplo n.º 29
0
def list_netns(**kwargs):
    """List network namespaces.

    Caller requires raised priveleges to list namespaces
    """
    return netns.listnetns(**kwargs)
Exemplo n.º 30
0
 def list(self):
     print(netns.listnetns())
Exemplo n.º 31
0
def main(argv):
    if (len(argv) !=  2):
        print('Usage: python pvdtest_veth.py PVD_ID')
        print('PVD_ID := name of the interface that contains PvD-related network configuration')
        sys.exit()

    # Get user-entered PVD ID
    pvdId = sys.argv[1]
    
    # Create IPRoute object for manipulation with a default network namespace
    ipMain = IPRoute()

    # Create a PvD-related network namespace
    pvdNetnsName = getPvdNetnsName(pvdId);
    if (pvdNetnsName in netns.listnetns()):
        netns.remove(pvdNetnsName)
    netns.create(pvdNetnsName)

    # Create IPRoute object for manipulation with a PvD-related network namespace
    netns.setns(pvdNetnsName)
    ipPvd = IPRoute()

    # Activate loopback interface in a PvD-related network namespace
    loIndex = ipPvd.link_lookup(ifname='lo')[0]
    ipPvd.link_up(loIndex)

    # Get addresses from a PvD-related network interface
    pvdIfIndex = ipMain.link_lookup(ifname=getPvdIfName(pvdId))[0]
    pvdAddresses = ipMain.get_addr(index=pvdIfIndex)

    # Get current routes
    pvdRoutes = ipMain.get_routes()

    # Create bridge
    bridge = getPvdBridgeName(pvdId)
    ipMain.link_create(ifname=bridge, kind='bridge')

    # Create veth interface
    (veth0, veth1) = getPvdVethNames(pvdId)
    ipMain.link_create(ifname=veth0, kind='veth', peer=veth1)

    # Move one end of the veth interafce to a PvD-related network namespace
    veth1Index = ipMain.link_lookup(ifname=veth1)[0]
    ipMain.link('set', index=veth1Index, net_ns_fd=pvdNetnsName)

    # Shut down and remove addresses from the PvD interface before adding it to the bridge
    ipMain.link_down(pvdIfIndex)
    ipMain.flush_addr(pvdIfIndex)

    # Make a bridge between PvD interface and one end of veth interface
    veth0Index = ipMain.link_lookup(ifname=veth0)[0]
    bridgeIndex = ipMain.link_lookup(ifname=bridge)[0]
    ipMain.link('set', index=veth0Index, master=bridgeIndex)
    ipMain.link('set', index=pvdIfIndex, master=bridgeIndex)

    # Activate bridge and connected interfaces
    ipMain.link_up(pvdIfIndex)
    ipMain.link_up(veth0Index)
    ipMain.link_up(bridgeIndex)
    ipPvd.link_up(veth1Index)

    # Configure bridge and another end of the veth interface with PvD-related network parameters + add routes
    ipMain.flush_routes()
    ipPvd.flush_routes()
    for address in pvdAddresses:
        ipAddress = broadcastAddress = netmask = addrFamily = None
        for attr in address['attrs']:
            if attr[0] == 'IFA_ADDRESS':
                ipAddress = attr[1]
            if attr[0] == 'IFA_BROADCAST':
                broadcastAddress = attr[1]
        netmask = address['prefixlen']
        addrFamily = address['family']
        # Configure bridge
        try:
            ipMain.addr('add', index=bridgeIndex, family=addrFamily, address=ipAddress, broadcast=broadcastAddress, mask=netmask)
        except:
            pass
        # Configure veth
        try:
            ipPvd.addr('add', index=veth1Index, family=addrFamily, address=ipAddress, broadcast=broadcastAddress, mask=netmask)
        except:
            pass
        # Configure routes
        # Some routes are added during interface IP address/netmask configuration, skip them if already there
        ipNetwork = IPNetwork(ipAddress + '/' + str(netmask))
        try:
            ipMain.route('add', dst=str(ipNetwork.network), mask=netmask, oif=bridgeIndex, src=ipAddress, rtproto='RTPROT_STATIC', rtscope='RT_SCOPE_LINK')
        except:
            pass
        try:
            ipPvd.route('add', dst=str(ipNetwork.network), mask=netmask, oif=veth1Index, src=ipAddress, rtproto='RTPROT_STATIC', rtscope='RT_SCOPE_LINK')
        except:
            pass

    # Fing gateway(s) and add default routes
    defGateways = []
    for route in pvdRoutes:
        for attr in route['attrs']:
            if (attr[0] == 'RTA_GATEWAY' and attr[1] not in defGateways):
                defGateways.append(attr[1])
    if (len(defGateways) > 0):
        ipMain.route('add', dst='0.0.0.0', oif=bridgeIndex, gateway=defGateways[0], rtproto='RTPROT_STATIC')
        ipPvd.route('add', dst='0.0.0.0', oif=veth1Index, gateway=defGateways[0], rtproto='RTPROT_STATIC')
Exemplo n.º 32
0
 def get_namespaces_list():
     return netns.listnetns()
Exemplo n.º 33
0
def list_netns(**kwargs):
    """List network namespaces.

    Caller requires raised priveleges to list namespaces
    """
    return netns.listnetns(**kwargs)
Exemplo n.º 34
0
 def del_namespace(self):
     if self.name in netns.listnetns():
         netns.remove(self.name)
Exemplo n.º 35
0
 def get(self, nspath):
     if nspath in netns.listnetns():
         return NetnsDao(nspath).interfaces
     else:
         abort(404, "Namespace does not exist")
Exemplo n.º 36
0
 def list(self):
     print (netns.listnetns())