Пример #1
0
    def newveth(self,
                iface_id: int = None,
                ifname: str = None,
                mtu: int = None) -> int:
        """
        Create a new interface.

        :param iface_id: id for the new interface
        :param ifname: name for the new interface
        :param mtu: mtu for interface
        :return: nothing
        """
        with self.lock:
            mtu = mtu if mtu is not None else DEFAULT_MTU
            iface_id = iface_id if iface_id is not None else self.next_iface_id(
            )
            ifname = ifname if ifname is not None else 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}"
            name = f"{localname}p"
            veth = Veth(self.session, name, localname, mtu, self.server, self)
            veth.adopt_node(iface_id, ifname, self.up)
            return iface_id
Пример #2
0
    def linknet(self, net):
        """
        Link this bridge with another by creating a veth pair and installing
        each device into each bridge.

        :param core.netns.vnet.LxBrNet net: network to link with
        :return: created interface
        :rtype: Veth
        """
        sessionid = self.session.short_session_id()
        try:
            _id = "%x" % self.id
        except TypeError:
            _id = "%s" % self.id

        try:
            net_id = "%x" % net.id
        except TypeError:
            net_id = "%s" % net.id

        localname = "veth%s.%s.%s" % (_id, net_id, sessionid)
        if len(localname) >= 16:
            raise ValueError("interface local name %s too long" % localname)

        name = "veth%s.%s.%s" % (net_id, _id, sessionid)
        if len(name) >= 16:
            raise ValueError("interface name %s too long" % name)

        netif = Veth(node=None,
                     name=name,
                     localname=localname,
                     mtu=1500,
                     net=self,
                     start=self.up)
        self.attach(netif)
        if net.up:
            # this is similar to net.attach() but uses netif.name instead
            # of localname
            utils.check_cmd(
                [constants.BRCTL_BIN, "addif", net.brname, netif.name])
            utils.check_cmd(
                [constants.IP_BIN, "link", "set", netif.name, "up"])
        i = net.newifindex()
        net._netif[i] = netif
        with net._linked_lock:
            net._linked[netif] = {}
        netif.net = self
        netif.othernet = net
        return netif
Пример #3
0
    def linknet(self, network):
        """
        Link this bridge with another by creating a veth pair and installing
        each device into each bridge.
        """
        session_id = self.session.short_session_id()

        try:
            _id = "%x" % self.id
        except TypeError:
            _id = "%s" % self.id

        try:
            network_id = "%x" % network.id
        except TypeError:
            network_id = "%s" % network.id

        localname = "veth%s.%s.%s" % (_id, network_id, session_id)

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

        name = "veth%s.%s.%s" % (network_id, _id, session_id)
        if len(name) >= 16:
            raise ValueError("interface name %s too long" % name)

        interface = Veth(node=None,
                         name=name,
                         localname=localname,
                         mtu=1500,
                         net=self,
                         start=self.up)
        self.attach(interface)
        if network.up:
            # this is similar to net.attach() but uses netif.name instead of localname
            utils.check_cmd([
                constants.OVS_BIN, "add-port", network.bridge_name,
                interface.name
            ])
            utils.check_cmd(
                [constants.IP_BIN, "link", "set", interface.name, "up"])

        network.attach(interface)
        interface.net = self
        interface.othernet = network
        return interface
Пример #4
0
    def linknet(self, net: CoreNetworkBase) -> CoreInterface:
        """
        Link this bridge with another by creating a veth pair and installing
        each device into each bridge.

        :param net: network to link with
        :return: created interface
        """
        sessionid = self.session.short_session_id()
        try:
            _id = f"{self.id:x}"
        except TypeError:
            _id = str(self.id)
        try:
            net_id = f"{net.id:x}"
        except TypeError:
            net_id = str(net.id)
        localname = f"veth{_id}.{net_id}.{sessionid}"
        name = f"veth{net_id}.{_id}.{sessionid}"
        iface = Veth(self.session, name, localname)
        if self.up:
            iface.startup()
        self.attach(iface)
        if net.up and net.brname:
            iface.net_client.set_iface_master(net.brname, iface.name)
        i = net.next_iface_id()
        net.ifaces[i] = iface
        with net.linked_lock:
            net.linked[iface] = {}
        iface.net = self
        iface.othernet = net
        return iface
Пример #5
0
    def linknet(self, net):
        """
        Link this bridge with another by creating a veth pair and installing
        each device into each bridge.

        :param core.netns.vnet.LxBrNet net: network to link with
        :return: created interface
        :rtype: Veth
        """
        sessionid = self.session.short_session_id()
        try:
            _id = f"{self.id:x}"
        except TypeError:
            _id = str(self.id)

        try:
            net_id = f"{net.id:x}"
        except TypeError:
            net_id = str(net.id)

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

        name = f"veth{net_id}.{_id}.{sessionid}"
        if len(name) >= 16:
            raise ValueError(f"interface name {name} too long")

        netif = Veth(self.session, None, name, localname, start=self.up)
        self.attach(netif)
        if net.up:
            # this is similar to net.attach() but uses netif.name instead of localname
            netif.net_client.create_interface(net.brname, netif.name)
        i = net.newifindex()
        net._netif[i] = netif
        with net._linked_lock:
            net._linked[netif] = {}
        netif.net = self
        netif.othernet = net
        return netif
Пример #6
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
Пример #7
0
    def linknet(self, net: CoreNetworkBase) -> CoreInterface:
        """
        Link this bridge with another by creating a veth pair and installing
        each device into each bridge.

        :param net: network to link with
        :return: created interface
        """
        sessionid = self.session.short_session_id()
        try:
            _id = f"{self.id:x}"
        except TypeError:
            _id = str(self.id)

        try:
            net_id = f"{net.id:x}"
        except TypeError:
            net_id = str(net.id)

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

        name = f"veth{net_id}.{_id}.{sessionid}"
        if len(name) >= 16:
            raise ValueError(f"interface name {name} too long")

        netif = Veth(self.session, None, name, localname, start=self.up)
        self.attach(netif)
        if net.up and net.brname:
            netif.net_client.set_interface_master(net.brname, netif.name)
        i = net.newifindex()
        net._netif[i] = netif
        with net._linked_lock:
            net._linked[netif] = {}
        netif.net = self
        netif.othernet = net
        return netif
Пример #8
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