当前位置: 首页>>代码示例>>Python>>正文


Python IPRoute.link方法代码示例

本文整理汇总了Python中pyroute2.IPRoute.link方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.link方法的具体用法?Python IPRoute.link怎么用?Python IPRoute.link使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyroute2.IPRoute的用法示例。


在下文中一共展示了IPRoute.link方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: delete_interface

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
    def delete_interface(self):
        '''
        Delete the interface.
        Deletes both VLAN Tag interface and MACVlan interface
        '''
        # Find the VLAN interface interface from the MACVlan interface
        link = self.get_link()
        if link is None:
            return

        vlan_idx = None
        for i in link[0]['attrs']:
            if (i[0] == 'IFLA_LINK'):
                vlan_idx = i[1]
                break

        if vlan_idx is None:
            raise Error(CNI_ERROR_DEL_VLAN_INTF,
                        'Error finding vlan interface. Interface inside ' +
                        ' container ' + self.cni.container_ifname)

        # Delete the VLAN Tag interface.
        # It will delete the interface inside container also
        try:
            iproute = IPRoute()
            iproute.link('del', index=vlan_idx)
        except NetlinkError as e:
            raise Error(CNI_ERROR_DEL_VLAN_INTF,
                        'Error deleting VLAN interface. Parent interface ' +
                        self.host_ifname + ' vlan-tag ' + self.vlan_tag +
                        ' vlan-ifindex ' + str(vlan_idx) +
                        ' code ' + str(e.code) + ' message ' + e.message)
        return
开发者ID:Juniper,项目名称:contrail-controller,代码行数:35,代码来源:macvlan.py

示例2: create_veth_pair

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
def create_veth_pair(name):
    ip = IPRoute()
    peers = ('{}0'.format(name), '{}1'.format(name))
    LOG.info('creating veth pair {}'.format(peers))
    ip.link('add', kind='veth', ifname=peers[0], peer=peers[1])
    link_up(peers[0])
    link_up(peers[1])
开发者ID:durgesh-rane,项目名称:simple-ostinato,代码行数:9,代码来源:utils.py

示例3: delete_veth_pair

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
def delete_veth_pair(name):
    ip = IPRoute()
    peers = ('{}0'.format(name), '{}1'.format(name))
    LOG.info('deleting veth pair {}'.format(peers))
    link_down(peers[0])
    link_down(peers[1])
    ip.link('del', index=ip.link_lookup(ifname=peers[0])[0])
开发者ID:durgesh-rane,项目名称:simple-ostinato,代码行数:9,代码来源:utils.py

示例4: BasicTest

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
class BasicTest(object):

    def setup(self):
        require_user('root')
        self.ip = IPRoute()
        self.ip.link('add',
                     index=0,
                     ifname='dummyX',
                     linkinfo={'attrs': [['IFLA_INFO_KIND', 'dummy']]})
        self.interface = self.ip.link_lookup(ifname='dummyX')[0]

    def teardown(self):
        self.ip.link('delete', index=self.interface)
        self.ip.release()

    def get_qdiscs(self):
        return [x for x in self.ip.get_qdiscs() if
                x['index'] == self.interface]

    def get_qdisc(self):
        # get qdiscs list and filter out our interface
        qds = self.get_qdiscs()
        if qds:
            return qds[0]
        else:
            return None
开发者ID:rgacogne,项目名称:pyroute2,代码行数:28,代码来源:test_tc.py

示例5: create_interface

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
    def create_interface(self):
        '''
        Create MACVlan interface
        Creates VLAN interface first based on VLAN tag for sub-interface
        then create macvlan interface above the vlan interface
        '''

        iproute = IPRoute()
        # Ensure the host interface is present
        host_if = iproute.link_lookup(ifname=self.host_ifname)
        if len(host_if) == 0:
            raise CniError(CNI_ERROR_ADD_VLAN_INTF,
                           'Error creating vlan interface' + ' host interface' +
                           self.host_ifname + ' not found')
            return

        # Create vlan interface if not present
        vlan_if = iproute.link_lookup(ifname=self.vlan_ifname)
        if len(vlan_if) == 0:
            try:
                iproute.link("add", ifname=self.vlan_ifname, kind='vlan',
                             vlan_id=self.vlan_tag, link=host_if[0])
            except NetlinkError as e:
                if e.code != errno.EEXIST:
                    raise CniError(CNI_ERROR_ADD_VETH,
                                   'Error creating vlan interface. ' +
                                   'Host interface ' + self.host_ifname +
                                   'vlan id ' + str(self.vlan_tag) +
                                   'vlan ifname ' + self.vlan_ifname +
                                   ' code ' + str(e.code) +
                                   ' message ' + e.message)
            vlan_if = iproute.link_lookup(ifname=self.vlan_ifname)

        # Create MACVlan interface if not present
        cn_ifname = self.vlan_ifname + '-ns'
        cn_if = iproute.link_lookup(ifname=cn_ifname)
        if len(cn_if) == 0:
            try:
                iproute.link("add", ifname=cn_ifname, kind='macvlan',
                             link=vlan_if[0], macvlan_mode="vepa")
            except NetlinkError as e:
                if e.code != errno.EEXIST:
                    raise CniError(CNI_ERROR_ADD_VETH,
                                   'Error creating macvlan interface ' +
                                   ' vlan iterface ' + self.vlan_ifname +
                                   ' macvlan interface ' + cn_ifname +
                                   ' code ' + str(e.code) +
                                   ' message ' + e.message)
            cn_if = iproute.link_lookup(ifname=self.vlan_ifname)

        # Move one end of pair inside container
        self.move_link(cn_ifname)
        return
开发者ID:Juniper,项目名称:contrail-controller,代码行数:55,代码来源:cni.py

示例6: configure_interface

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
 def configure_interface(self, ip4, plen, gw):
     '''
     Configure the interface inside container with,
     - IP Address
     - Default gateway
     - Link-up
     '''
     # Configure interface inside the container
     self.configure_link(ip4, plen, gw)
     # Set link-up for vlan interface on host-os
     iproute = IPRoute()
     idx = iproute.link_lookup(ifname=self.vlan_ifname)[0]
     iproute.link('set', index=idx, state='up')
     return
开发者ID:Juniper,项目名称:contrail-controller,代码行数:16,代码来源:cni.py

示例7: delete_link

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
 def delete_link(self):
     '''
     Delete interface inside the container
     '''
     with CniNamespace(self.container_netns, logger):
         iproute = IPRoute()
         iface = iproute.link_lookup(ifname=self.container_ifname)
         if len(iface) == 0:
             return
         try:
             iproute.link('del', index=iface[0])
         except NetlinkError as e:
             raise CniError(CNI_ERROR_DEL_NS_INTF,
                            'Error deleting interface inside container ' +
                            self.container_ifname +
                            ' code ' + str(e.code) + ' message ' + e.message)
     return
开发者ID:Juniper,项目名称:contrail-controller,代码行数:19,代码来源:cni.py

示例8: condUnshareNet

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
def condUnshareNet(unshare_net=True):
    if USE_NSPAWN and unshare_net:
        try:
            unshare(CLONE_NEWNET)
            # Set up loopback interface and add default route via loopback in the namespace.
            # Missing default route may confuse some software, see
            # https://github.com/rpm-software-management/mock/issues/113
            ipr = IPRoute()
            dev = ipr.link_lookup(ifname='lo')[0]

            ipr.link('set', index=dev, state='up')
            ipr.route("add", dst="default", gateway="127.0.0.1")
        except exception.UnshareFailed:
            # IPC and UTS ns are supported since the same kernel version. If this
            # fails, there had to be a warning already
            pass
        # pylint: disable=bare-except
        except Exception as e:
            getLog().warning("network namespace setup failed: %s", e)
开发者ID:xsuchy,项目名称:mock,代码行数:21,代码来源:util.py

示例9: configure_link

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
    def configure_link(self, ip4_address, plen, gateway):
        '''
        Configure following attributes for interface inside the container
        - Link-up
        - IP Address
        - Default gateway
        '''
        @staticmethod
        def _intf_error(e, ifname, message):
            raise Error(CNI_ERROR_CONFIG_NS_INTF, message + ifname +
                        ' code ' + str(e.code) + ' message ' + e.message)
            return

        with CniNamespace(self.cni.container_netns):
            iproute = IPRoute()
            intf = iproute.link_lookup(ifname=self.cni.container_ifname)
            if len(intf) == 0:
                raise Error(CNI_ERROR_CONFIG_NS_INTF,
                            'Error finding interface ' +
                            self.cni.container_ifname + ' inside container')
            idx_ns = intf[0]
            try:
                iproute.link('set', index=idx_ns, state='up')
            except NetlinkError as e:
                _intf_error(e, self.cni.container_ifname,
                            'Error setting link state for interface ' +
                            'inside container')
            try:
                iproute.addr('add', index=idx_ns, address=ip4_address,
                              prefixlen=plen)
            except NetlinkError as e:
                if e.code != errno.EEXIST:
                    _intf_error(e, self.cni.container_ifname,
                                'Error setting ip-address for interface ' +
                                'inside container')
            try:
                iproute.route('add', dst='0.0.0.0/0', gateway=gateway)
            except NetlinkError as e:
                if e.code != errno.EEXIST:
                    _intf_error(e, self.cni.container_ifname,
                                'Error adding default route inside container')
        return
开发者ID:Juniper,项目名称:contrail-controller,代码行数:44,代码来源:interface.py

示例10: create_interface

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
    def create_interface(self):
        '''
        Create veth-pair
        Creates veth-pair in the container-namespace first and then moves
        one end of the pair to host-os namespace
        '''
        # If the host end of veth is already present in host-os, it means
        # interface create was already done. There is nothing to do
        iproute = IPRoute()
        iface = iproute.link_lookup(ifname=self.host_ifname)
        if len(iface) != 0:
            return

        host_ifindex = None
        with CniNamespace(self.cni.container_netns):
            # Create veth pairs if not already created inside namespace
            # One end of pair is named host_ifname and the other end of pair
            # is set a container_ifname
            ns_iproute = IPRoute()
            ns_iface = ns_iproute.link_lookup(ifname=self.cni.container_ifname)
            if len(ns_iface) == 0:
                try:
                    ns_iproute.link_create(ifname=self.cni.container_ifname,
                                        peer=self.host_ifname, kind='veth',
                                        address=self.container_mac)
                except NetlinkError as e:
                    if e.code != errno.EEXIST:
                        raise Error(CNI_ERROR_VETH_ADD,
                                    'Error creating veth device ' +
                                    self.host_ifname + ' code ' +
                                    str(e.code) + ' message ' + e.message)

            # We must move the host part of veth pair to host-namespace
            # Get the interface-index. We will move to host namespace once
            # we exit container-name space and go to host-namespace
            host_ifindex = ns_iproute.link_lookup(ifname=self.host_ifname)[0]

        if host_ifindex is not None:
            ns_iproute.link('set', index=host_ifindex, net_ns_pid=self.pid)

        return
开发者ID:Juniper,项目名称:contrail-controller,代码行数:43,代码来源:veth.py

示例11: get_link

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
 def get_link(self):
     '''
     Get link information for the interface inside the container
     '''
     link = None
     with CniNamespace(self.container_netns, logger):
         iproute = IPRoute()
         iface = iproute.link_lookup(ifname=self.container_ifname)
         if len(iface) != 0:
             idx = iface[0]
             link = iproute.link("get", index=idx)
     return link
开发者ID:Juniper,项目名称:contrail-controller,代码行数:14,代码来源:cni.py

示例12: move_link

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
    def move_link(self, ifname):
        '''
        Move interface inside a container and rename to self.container_ifname
        '''
        # Get index of interface being configured
        iproute = IPRoute()
        intf = iproute.link_lookup(ifname=ifname)
        if len(intf) == 0:
            return
        idx = intf[0]

        # Move interface inside container.
        # If the interface rename failed, the interface with temporary name
        # can be present inside interface
        iproute.link('set', index=idx, net_ns_pid=self.container_pid)

        # Rename the interface
        with CniNamespace(self.container_netns, logger):
            ip_ns = IPRoute()
            if ifname != self.container_ifname:
                ip_ns.link("set", index=idx, address=self.mac,
                           ifname=self.container_ifname)
        return
开发者ID:Juniper,项目名称:contrail-controller,代码行数:25,代码来源:cni.py

示例13: BasicTest

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
class BasicTest(object):

    def setup(self):
        require_user('root')
        self.ip = IPRoute()
        self.ifname = uifname()
        self.ip.link_create(ifname=self.ifname, kind='dummy')
        self.interface = self.ip.link_lookup(ifname=self.ifname)[0]

    def teardown(self):
        self.ip.link('delete', index=self.interface)
        self.ip.close()

    def get_qdiscs(self):
        return [x for x in self.ip.get_qdiscs() if
                x['index'] == self.interface]

    def get_qdisc(self):
        # get qdiscs list and filter out our interface
        qds = self.get_qdiscs()
        if qds:
            return qds[0]
        else:
            return None
开发者ID:GateprotectGmbH,项目名称:pyroute2,代码行数:26,代码来源:test_tc.py

示例14: TestRule

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
class TestRule(object):

    def setup(self):
        require_user('root')
        self.ip = IPRoute()
        self.ifname = uifname()
        self.ip.link('add', ifname=self.ifname, kind='dummy')
        self.interface = self.ip.link_lookup(ifname=self.ifname)[0]

    def teardown(self):
        self.ip.link('delete', index=self.interface)
        self.ip.close()

    def test_basic(self):
        self.ip.rule('add', 10, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32000 and
                    x.get_attr('FRA_TABLE') == 10]) == 1
        self.ip.rule('delete', 10, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32000 and
                    x.get_attr('FRA_TABLE') == 10]) == 0

    def test_fwmark(self):
        self.ip.rule('add', 15, 32006, fwmark=10)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK')]) == 1
        self.ip.rule('delete', 15, 32006, fwmark=10)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK')]) == 0

    def test_fwmark_mask_normalized(self):
        self.ip.rule('add', 15, 32006, fwmark=10, fwmask=20)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK') and
                    x.get_attr('FRA_FWMASK')]) == 1
        self.ip.rule('delete', 15, 32006, fwmark=10, fwmask=20)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK') and
                    x.get_attr('FRA_FWMASK')]) == 0

    def test_fwmark_mask_raw(self):
        self.ip.rule('add', 15, 32006, fwmark=10, FRA_FWMASK=20)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK') and
                    x.get_attr('FRA_FWMASK')]) == 1
        self.ip.rule('delete', 15, 32006, fwmark=10, FRA_FWMASK=20)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK') and
                    x.get_attr('FRA_FWMASK')]) == 0

    def test_bad_table(self):
        try:
            self.ip.rule('add', -1, 32000)
        except Exception:
            pass

    def test_big_table(self):
        self.ip.rule('add', 1024, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32000 and
                    x.get_attr('FRA_TABLE') == 1024]) == 1
        self.ip.rule('delete', 1024, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32000 and
                    x.get_attr('FRA_TABLE') == 1024]) == 0

    def test_src_dst(self):
        self.ip.rule('add', 17, 32005,
                     src='10.0.0.0', src_len=24,
                     dst='10.1.0.0', dst_len=24)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32005 and
                    x.get_attr('FRA_TABLE') == 17 and
                    x.get_attr('FRA_SRC') == '10.0.0.0' and
                    x.get_attr('FRA_DST') == '10.1.0.0' and
                    x['src_len'] == 24 and
                    x['dst_len'] == 24]) == 1
        self.ip.rule('delete', 17, 32005,
                     src='10.0.0.0', src_len=24,
                     dst='10.1.0.0', dst_len=24)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32005 and
                    x.get_attr('FRA_TABLE') == 17 and
                    x.get_attr('FRA_SRC') == '10.0.0.0' and
                    x.get_attr('FRA_DST') == '10.1.0.0' and
                    x['src_len'] == 24 and
                    x['dst_len'] == 24]) == 0
开发者ID:0xD3ADB33F,项目名称:pyroute2,代码行数:102,代码来源:test_rule.py

示例15: TestIPRoute

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link [as 别名]
class TestIPRoute(object):

    def setup(self):
        self.ip = IPRoute()
        self.ap = AddrPool()
        self.iftmp = 'pr2x{0}'
        try:
            self.dev, idx = self.create()
            self.ifaces = [idx]
        except IndexError:
            pass

    def get_ifname(self):
        return self.iftmp.format(self.ap.alloc())

    def create(self, kind='dummy'):
        name = self.get_ifname()
        create_link(name, kind=kind)
        idx = self.ip.link_lookup(ifname=name)[0]
        return (name, idx)

    def teardown(self):
        if hasattr(self, 'ifaces'):
            for dev in self.ifaces:
                try:
                    self.ip.link('delete', index=dev)
                except:
                    pass
        self.ip.close()

    def _test_nla_operators(self):
        require_user('root')
        self.ip.addr('add', self.ifaces[0], address='172.16.0.1', mask=24)
        self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24)
        r = [x for x in self.ip.get_addr() if x['index'] == self.ifaces[0]]
        complement = r[0] - r[1]
        intersection = r[0] & r[1]

        assert complement.get_attr('IFA_ADDRESS') == '172.16.0.1'
        assert complement.get_attr('IFA_LABEL') is None
        assert complement['prefixlen'] == 0
        assert complement['index'] == 0

        assert intersection.get_attr('IFA_ADDRESS') is None
        assert intersection.get_attr('IFA_LABEL') == self.dev
        assert intersection['prefixlen'] == 24
        assert intersection['index'] == self.ifaces[0]

    def test_add_addr(self):
        require_user('root')
        self.ip.addr('add', self.ifaces[0], address='172.16.0.1', mask=24)
        assert '172.16.0.1/24' in get_ip_addr()

    def _create(self, kind):
        name = self.get_ifname()
        self.ip.link_create(ifname=name, kind=kind)
        devs = self.ip.link_lookup(ifname=name)
        assert devs
        self.ifaces.extend(devs)

    def test_create_dummy(self):
        require_user('root')
        self._create('dummy')

    def test_create_bond(self):
        require_user('root')
        self._create('bond')

    def test_create_bridge(self):
        require_user('root')
        self._create('bridge')

    def test_neigh_real_links(self):
        links = set([x['index'] for x in self.ip.get_links()])
        neigh = set([x['ifindex'] for x in self.ip.get_neighbors()])
        assert neigh < links

    def test_mass_ipv6(self):
        #
        # Achtung! This test is time consuming.
        # It is really time consuming, I'm not not
        # kidding you. Beware.
        #
        require_user('root')
        base = 'fdb3:84e5:4ff4:55e4::{0}'
        limit = int(os.environ.get('PYROUTE2_SLIMIT', '0x800'), 16)

        # add addresses
        for idx in range(limit):
            self.ip.addr('add', self.ifaces[0],
                         base.format(hex(idx)[2:]), 48)

        # assert addresses in two steps, to ease debug
        addrs = self.ip.get_addr(10)
        assert len(addrs) >= limit

        # clean up addresses
        #
        # it is not required, but if you don't do that,
        # you'll get this on the interface removal:
#.........这里部分代码省略.........
开发者ID:jazzmes,项目名称:pyroute2,代码行数:103,代码来源:test_ipr.py


注:本文中的pyroute2.IPRoute.link方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。