How to use the pyroute2.netlink.exceptions.NetlinkError function in pyroute2

To help you get started, we’ve selected a few pyroute2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github svinota / pyroute2 / pyroute2 / ipdb / interfaces.py View on Github external
error.debug = debug
            raise error

        # restore dependent routes for successful rollback
        if commit_phase == 2:
            for route in self.routes:
                with route[0]._direct_state:
                    route[0]['ipdb_scope'] = 'restore'
                try:
                    route[0].commit(transaction=route[1],
                                    commit_phase=2,
                                    commit_mask=2)
                except RuntimeError as x:
                    # RuntimeError is raised due to phase 2, so
                    # an additional check is required
                    if isinstance(x.cause, NetlinkError) and \
                            x.cause.code == errno.EEXIST:
                        pass

        time.sleep(config.commit_barrier)

        # drop all collected errors, if any
        self.errors = []
        return self
github svinota / pyroute2 / pyroute2 / netlink / rtnl / ifinfmsg / proxy.py View on Github external
def manage_tuntap(msg):

    if TUNSETIFF is None:
        raise NetlinkError(errno.EOPNOTSUPP, 'Arch not supported')

    if msg['header']['type'] != RTM_NEWLINK:
        raise NetlinkError(errno.EOPNOTSUPP, 'Unsupported event')

    ifru_flags = 0
    linkinfo = msg.get_attr('IFLA_LINKINFO')
    infodata = linkinfo.get_attr('IFLA_INFO_DATA')

    flags = infodata.get_attr('IFTUN_IFR', None)
    if infodata.get_attr('IFTUN_MODE') == 'tun':
        ifru_flags |= IFT_TUN
    elif infodata.get_attr('IFTUN_MODE') == 'tap':
        ifru_flags |= IFT_TAP
    else:
        raise ValueError('invalid mode')
    if flags is not None:
        if flags['no_pi']:
            ifru_flags |= IFT_NO_PI
        if flags['one_queue']:
github svinota / pyroute2 / pyroute2 / netns / manager.py View on Github external
def create(self, path):
        netnspath = netns._get_netnspath(path)
        try:
            netns.create(netnspath, self.libc)
        except OSError as e:
            raise NetlinkError(e.errno)
        info = self.ipr._dump_one_ns(netnspath, set())
        info['header']['type'] = RTM_NEWNETNS
        info['event'] = 'RTM_NEWNETNS'
        del info['value']
        return info,
github openstack / neutron / neutron / agent / linux / ip_lib.py View on Github external
def get_device_name(index):
        try:
            with privileged.get_iproute(namespace) as ip:
                device = ip.link('get', index=index)
                if device:
                    attrs = device[0].get('attrs', [])
                    for attr in (attr for attr in attrs
                                 if attr[0] == 'IFLA_IFNAME'):
                        return attr[1]
        except netlink_exceptions.NetlinkError as e:
            if e.code == errno.ENODEV:
                return
            raise
github svinota / pyroute2 / pyroute2 / netlink / rtnl / ifinfmsg / proxy.py View on Github external
def manage_tuntap(msg):

    if TUNSETIFF is None:
        raise NetlinkError(errno.EOPNOTSUPP, 'Arch not supported')

    if msg['header']['type'] != RTM_NEWLINK:
        raise NetlinkError(errno.EOPNOTSUPP, 'Unsupported event')

    ifru_flags = 0
    linkinfo = msg.get_attr('IFLA_LINKINFO')
    infodata = linkinfo.get_attr('IFLA_INFO_DATA')

    flags = infodata.get_attr('IFTUN_IFR', None)
    if infodata.get_attr('IFTUN_MODE') == 'tun':
        ifru_flags |= IFT_TUN
    elif infodata.get_attr('IFTUN_MODE') == 'tap':
        ifru_flags |= IFT_TAP
    else:
        raise ValueError('invalid mode')
    if flags is not None:
github openstack / os-vif / os_vif / internal / ip / linux / impl_pyroute2.py View on Github external
def _ip_link(self, ip, command, check_exit_code, **kwargs):
        try:
            LOG.debug('pyroute2 command %(command)s, arguments %(args)s' %
                      {'command': command, 'args': kwargs})
            return ip.link(command, **kwargs)
        except ipexc.NetlinkError as e:
            with excutils.save_and_reraise_exception() as ctx:
                if e.code in check_exit_code:
                    LOG.error('NetlinkError was raised, code %s, message: %s' %
                              (e.code, str(e)))
                    ctx.reraise = False
github svinota / pyroute2 / pyroute2 / netlink / exceptions.py View on Github external
def __init__(self, code, msg=None):
        msg = msg or os.strerror(code)
        super(NetlinkError, self).__init__(code, msg)
        self.code = code
github svinota / pyroute2 / pyroute2 / proxy.py View on Github external
msg += data[8:16]
                        msg += struct.pack('I', 0)
                        # nlmsgerr struct alignment
                        msg += b'\0' * 20
                        return {'verdict': self.policy,
                                'data': msg}
                    else:
                        return ret

                except Exception as e:
                    logging.error(''.join(traceback.format_stack()))
                    logging.error(traceback.format_exc())
                    # errmsg
                    if isinstance(e, (OSError, IOError)):
                        code = e.errno
                    elif isinstance(e, NetlinkError):
                        code = e.code
                    else:
                        code = errno.ECOMM
                    msg = struct.pack('HH', 2, 0)
                    msg += data[8:16]
                    msg += struct.pack('I', code)
                    msg += data
                    msg = struct.pack('I', len(msg) + 4) + msg
                    return {'verdict': 'error',
                            'data': msg}
        return None
github svinota / pyroute2 / pyroute2 / ipdb / interfaces.py View on Github external
# The library can not tell this from the result of
                # an external program.
                #
                # One simple way to work that around is to remove
                # secondaries first.
                rip = sorted(ip2remove,
                             key=lambda x: self['ipaddr'][x]['flags'],
                             reverse=True)
                # 8<--------------------------------------
                for i in rip:
                    # When you remove a primary IP addr, all the
                    # subnetwork can be removed. In this case you
                    # will fail, but it is OK, no need to roll back
                    try:
                        run(nl.addr, 'delete', self['index'], i[0], i[1])
                    except NetlinkError as x:
                        # bypass only errno 99,
                        # 'Cannot assign address'
                        if x.code != errno.EADDRNOTAVAIL:
                            raise
                    except socket.error as x:
                        # bypass illegal IP requests
                        if isinstance(x.args[0], basestring) and \
                                x.args[0].startswith('illegal IP'):
                            continue
                        raise
                ###
                # Add addresses
                # 8<--------------------------------------
                for i in ip2add:
                    # Try to fetch additional address attributes
                    try: