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

        :param int ifindex: index for the new interface
        :param str ifname: name for the new interface
        :param core.nodes.base.CoreNetworkBase net: network to associate interface with
        :return: nothing
        """
        with self.lock:
            if ifindex is None:
                ifindex = self.newifindex()

            if ifname is None:
                ifname = "eth%d" % ifindex

            sessionid = self.session.short_session_id()

            try:
                suffix = "%x.%s.%s" % (self.id, ifindex, sessionid)
            except TypeError:
                suffix = "%s.%s.%s" % (self.id, ifindex, sessionid)

            localname = "veth" + suffix
            if len(localname) >= 16:
                raise ValueError("interface local name (%s) too long" % localname)

            name = localname + "p"
            if len(name) >= 16:
                raise ValueError("interface name (%s) too long" % name)

            veth = Veth(node=self, name=name, localname=localname, net=net, start=self.up)

            if self.up:
                utils.check_cmd([constants.IP_BIN, "link", "set", veth.name, "netns", str(self.pid)])
                self.check_cmd([constants.IP_BIN, "link", "set", veth.name, "name", ifname])

            veth.name = ifname

            if self.up:
                # TODO: potentially find better way to query interface ID
                # retrieve interface information
                output = self.check_cmd(["ip", "link", "show", veth.name])
                logging.debug("interface command output: %s", output)
                output = output.split("\n")
                veth.flow_id = int(output[0].strip().split(":")[0]) + 1
                logging.debug("interface flow index: %s - %s", veth.name, veth.flow_id)
                # TODO: mimic packed hwaddr
                # veth.hwaddr = MacAddress.from_string(output[1].strip().split()[1])
                logging.debug("interface mac: %s - %s", veth.name, veth.hwaddr)

            try:
                self.addnetif(veth, ifindex)
            except ValueError as e:
                veth.shutdown()
                del veth
                raise e

            return ifindex
Exemplo n.º 2
0
    def newveth(self, iface_id: int = None, ifname: str = None) -> int:
        """
        Create a new interface.

        :param iface_id: id for the new interface
        :param ifname: name for the new interface
        :return: nothing
        """
        with self.lock:
            if iface_id is None:
                iface_id = self.next_iface_id()

            if ifname is None:
                ifname = f"eth{iface_id}"

            sessionid = self.session.short_session_id()

            try:
                suffix = f"{self.id:x}.{iface_id}.{sessionid}"
            except TypeError:
                suffix = f"{self.id}.{iface_id}.{sessionid}"

            localname = f"veth{suffix}"
            if len(localname) >= 16:
                raise ValueError(
                    f"interface local name ({localname}) too long")

            name = localname + "p"
            if len(name) >= 16:
                raise ValueError(f"interface name ({name}) too long")

            veth = Veth(self.session,
                        self,
                        name,
                        localname,
                        start=self.up,
                        server=self.server)

            if self.up:
                self.net_client.device_ns(veth.name, str(self.pid))
                self.node_net_client.device_name(veth.name, ifname)
                self.node_net_client.checksums_off(ifname)

            veth.name = ifname

            if self.up:
                flow_id = self.node_net_client.get_ifindex(veth.name)
                veth.flow_id = int(flow_id)
                logging.debug("interface flow index: %s - %s", veth.name,
                              veth.flow_id)
                mac = self.node_net_client.get_mac(veth.name)
                logging.debug("interface mac: %s - %s", veth.name, mac)
                veth.set_mac(mac)

            try:
                # add network interface to the node. If unsuccessful, destroy the
                # network interface and raise exception.
                self.add_iface(veth, iface_id)
            except ValueError as e:
                veth.shutdown()
                del veth
                raise e

            return iface_id