Exemplo n.º 1
0
    def newnetif(self,
                 net=None,
                 addrlist=None,
                 hwaddr=None,
                 ifindex=None,
                 ifname=None):
        """
        Create a new network interface.

        :param net: network to associate with
        :param list addrlist: addresses to add on the interface
        :param core.misc.ipaddress.MacAddress hwaddr: hardware address to set for interface
        :param int ifindex: index of interface to create
        :param str ifname: name for interface
        :return: interface index
        :rtype: int
        """
        self.lock.acquire()
        try:
            # TODO: see if you can move this to emane specific code
            if nodeutils.is_node(net, NodeTypes.EMANE):
                ifindex = self.newtuntap(ifindex=ifindex,
                                         ifname=ifname,
                                         net=net)
                # TUN/TAP is not ready for addressing yet; the device may
                #   take some time to appear, and installing it into a
                #   namespace after it has been bound removes addressing;
                #   save addresses with the interface now
                self.attachnet(ifindex, net)
                netif = self.netif(ifindex)
                netif.sethwaddr(hwaddr)
                for addr in utils.maketuple(addrlist):
                    netif.addaddr(addr)
                return ifindex
            else:
                ifindex = self.newveth(ifindex=ifindex, ifname=ifname, net=net)

            if net is not None:
                self.attachnet(ifindex, net)

            if hwaddr:
                self.sethwaddr(ifindex, hwaddr)

            if addrlist:
                for addr in utils.maketuple(addrlist):
                    self.addaddr(ifindex, addr)

            self.ifup(ifindex)
            return ifindex
        finally:
            self.lock.release()
Exemplo n.º 2
0
    def newnetif(self,
                 net=None,
                 addrlist=None,
                 hwaddr=None,
                 ifindex=None,
                 ifname=None):
        """
        Add a network interface. If we are attaching to a CoreNs3Net, this
        will be a TunTap. Otherwise dispatch to CoreNode.newnetif().
        """
        if not addrlist:
            addrlist = []

        if not isinstance(net, CoreNs3Net):
            return CoreNode.newnetif(self, net, addrlist, hwaddr, ifindex,
                                     ifname)
        ifindex = self.newtuntap(ifindex=ifindex, ifname=ifname, net=net)
        self.attachnet(ifindex, net)
        netif = self.netif(ifindex)
        netif.sethwaddr(hwaddr)
        for addr in maketuple(addrlist):
            netif.addaddr(addr)

        addrstr = netif.addrlist[0]
        (addr, mask) = addrstr.split('/')
        tap = net._tapdevs[netif]
        tap.SetAttribute(
            "IpAddress",
            ns.network.Ipv4AddressValue(ns.network.Ipv4Address(addr)))
        tap.SetAttribute(
            "Netmask",
            ns.network.Ipv4MaskValue(ns.network.Ipv4Mask("/" + mask)))
        ns.core.Simulator.Schedule(ns.core.Time('0'), netif.install)
        return ifindex
Exemplo n.º 3
0
    def newnetif(self, net=None, addrlist=None, hwaddr=None, ifindex=None, ifname=None):
        """
        This is called when linking with another node. Since this node
        represents an interface, we do not create another object here,
        but attach ourselves to the given network.

        :param core.coreobj.PyCoreNet net: new network instance
        :param list[str] addrlist: address list
        :param str hwaddr: hardware address
        :param int ifindex: interface index
        :param str ifname: interface name
        :return:
        """
        with self.lock:
            if ifindex is None:
                ifindex = 0

            if self.net is not None:
                raise ValueError("RJ45 nodes support at most 1 network interface")

            self._netif[ifindex] = self
            # PyCoreNetIf.node is self
            self.node = self
            self.ifindex = ifindex

            if net is not None:
                self.attachnet(net)

            if addrlist:
                for addr in utils.maketuple(addrlist):
                    self.addaddr(addr)

            return ifindex
Exemplo n.º 4
0
 def adoptnetif(self, netif, ifindex, hwaddr, addrlist):
     """
     The broker builds a GreTap tunnel device to this physical node.
     When a link message is received linking this node to another part of
     the emulation, no new interface is created; instead, adopt the
     GreTap netif as the node interface.
     """
     netif.name = "gt%d" % ifindex
     netif.node = self
     self.addnetif(netif, ifindex)
     # use a more reasonable name, e.g. "gt0" instead of "gt.56286.150"
     if self.up:
         self.cmd([
             constants.IP_BIN, "link", "set", "dev", netif.localname, "down"
         ])
         self.cmd([
             constants.IP_BIN, "link", "set", netif.localname, "name",
             netif.name
         ])
     netif.localname = netif.name
     if hwaddr:
         self.sethwaddr(ifindex, hwaddr)
     for addr in utils.maketuple(addrlist):
         self.addaddr(ifindex, addr)
     if self.up:
         self.cmd([
             constants.IP_BIN, "link", "set", "dev", netif.localname, "up"
         ])
Exemplo n.º 5
0
    def __init__(self, routers, logfile, debugs=()):
        """
        Create a QuaggaConf instance.

        :param list routers: routers
        :param str logfile: log file name
        :param debugs: debug options
        """
        routers = "\n!\n".join(map(str, utils.maketuple(routers)))
        if debugs:
            debugs = "\n".join(utils.maketuple(debugs))
        else:
            debugs = "! no debugs"
        forwarding = "ip forwarding\nipv6 forwarding"
        Conf.__init__(self,
                      logfile=logfile,
                      debugs=debugs,
                      routers=routers,
                      forwarding=forwarding)
Exemplo n.º 6
0
 def newnetif(self,
              net=None,
              addrlist=[],
              hwaddr=None,
              ifindex=None,
              ifname=None):
     self.lock.acquire()
     try:
         ifindex = self.newveth(ifindex=ifindex, ifname=ifname, net=net)
         if net is not None:
             self.attachnet(ifindex, net)
         if hwaddr:
             self.sethwaddr(ifindex, hwaddr)
         for addr in utils.maketuple(addrlist):
             self.addaddr(ifindex, addr)
         self.ifup(ifindex)
         return ifindex
     finally:
         self.lock.release()
Exemplo n.º 7
0
    def __init__(self,
                 ospf6ifs,
                 area,
                 routerid,
                 redistribute="! no redistribute"):
        """
        Create a QuaggaOSPF6 instance.

        :param list ospf6ifs: ospf6 interfaces
        :param area: area
        :param routerid: router id
        :param str redistribute: redistribute value
        """
        ospf6ifs = utils.maketuple(ospf6ifs)
        interfaces = "\n!\n".join(map(str, ospf6ifs))
        ospfifs = "\n  ".join(
            map(lambda x: "interface %s area %s" % (x.name(), area), ospf6ifs))
        Conf.__init__(self,
                      interfaces=interfaces,
                      routerid=routerid,
                      ospfifs=ospfifs,
                      redistribute=redistribute)
Exemplo n.º 8
0
 def newnetif(self, net = None, addrlist = [], hwaddr = None,
              ifindex = None, ifname = None):
     ''' Add a network interface. If we are attaching to a CoreNs3Net, this
     will be a TunTap. Otherwise dispatch to CoreNode.newnetif().
     '''
     if not isinstance(net, CoreNs3Net):
         return CoreNode.newnetif(self, net, addrlist, hwaddr, ifindex, 
                                 ifname)
     ifindex = self.newtuntap(ifindex = ifindex, ifname = ifname, net = net) 
     self.attachnet(ifindex, net)
     netif = self.netif(ifindex)
     netif.sethwaddr(hwaddr)
     for addr in maketuple(addrlist):
         netif.addaddr(addr)
     
     addrstr = netif.addrlist[0]
     (addr, mask) = addrstr.split('/')
     tap = net._tapdevs[netif]
     tap.SetAttribute("IpAddress",
                      ns.network.Ipv4AddressValue(ns.network.Ipv4Address(addr)))
     tap.SetAttribute("Netmask",
                      ns.network.Ipv4MaskValue(ns.network.Ipv4Mask("/" + mask)))
     ns.core.Simulator.Schedule(ns.core.Time('0'), netif.install)
     return ifindex
Exemplo n.º 9
0
    def newnetif(self,
                 net=None,
                 addrlist=[],
                 hwaddr=None,
                 ifindex=None,
                 ifname=None):
        logger.warn("XEN PVM newnetif(ifindex=%s, ifname=%s) called", ifindex,
                    ifname)

        self.lock.acquire()

        if not self.up:
            self.lock.release()
            raise Exception("Can't access add veth as VM disk isn't ready")

        if self.booted:
            self.lock.release()
            raise Exception("Can't access add veth as VM is already running")

        try:
            if nodeutils.is_node(net, NodeTypes.EMANE):
                raise Exception("Xen PVM doesn't yet support Emane nets")

                # ifindex = self.newtuntap(ifindex = ifindex, ifname = ifname,
                #                          net = net)
                # # TUN/TAP is not ready for addressing yet; the device may
                # #   take some time to appear, and installing it into a
                # #   namespace after it has been bound removes addressing;
                # #   save addresses with the interface now
                # self.attachnet(ifindex, net)
                # netif = self.netif(ifindex)
                # netif.sethwaddr(hwaddr)
                # for addr in maketuple(addrlist):
                #     netif.addaddr(addr)
                # return ifindex
            else:
                ifindex = self.newveth(ifindex=ifindex,
                                       ifname=ifname,
                                       net=net,
                                       hwaddr=hwaddr)
            if net is not None:
                self.attachnet(ifindex, net)

            rulefile = os.path.join(self.getconfigitem("mount_path"),
                                    self.etcdir,
                                    "udev/rules.d/70-persistent-net.rules")
            f = self.openpausednodefile(rulefile, "a")
            f.write(
                "\n# Xen PVM virtual interface #%s %s with MAC address %s\n" %
                (ifindex, self.ifname(ifindex), hwaddr))
            # Using MAC address as we"re now loading PVM net driver "early"
            # OLD: Would like to use MAC address, but udev isn"t working with paravirtualized NICs.  Perhaps the "set hw address" isn"t triggering a rescan.
            f.write(
                'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="%s", KERNEL=="eth*", NAME="%s"\n'
                % (hwaddr, self.ifname(ifindex)))
            # f.write("SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", DEVPATH=="/devices/vif-%s/?*", KERNEL=="eth*", NAME="%s"\n" % (ifindex, self.ifname(ifindex)))
            f.close()

            if hwaddr:
                self.sethwaddr(ifindex, hwaddr)
            for addr in utils.maketuple(addrlist):
                self.addaddr(ifindex, addr)
            # self.ifup(ifindex)
            return ifindex
        finally:
            self.lock.release()