def flush_ip_addresses(ip_version, device, namespace): family = _IP_VERSION_FAMILY_MAP[ip_version] try: with get_iproute(namespace) as ip: idx = get_link_id(device, namespace) ip.flush_addr(index=idx, family=family) except OSError as e: if e.errno == errno.ENOENT: raise exception.NetworkNamespaceNotFound(netns_name=namespace) raise
def _run_iproute_link(command, device, namespace=None, **kwargs): try: with get_iproute(namespace) as ip: idx = get_link_id(device, namespace) return ip.link(command, index=idx, **kwargs) except NetlinkError as e: _translate_ip_device_exception(e, device, namespace) raise except OSError as e: if e.errno == errno.ENOENT: raise exception.NetworkNamespaceNotFound(netns_name=namespace) raise
def list_ip_routes(namespace, ip_version, device=None, table=None, **kwargs): """List IP routes""" kwargs.update( _make_pyroute2_route_args(namespace, ip_version, None, device, None, table, None, None, None)) try: with get_iproute(namespace) as ip: return make_serializable(ip.route('show', **kwargs)) except OSError as e: if e.errno == errno.ENOENT: raise exception.NetworkNamespaceNotFound(netns_name=namespace) raise
def get_ip_addresses(namespace, **kwargs): """List of IP addresses in a namespace :return: (tuple) IP addresses in a namespace """ try: with get_iproute(namespace) as ip: return make_serializable(ip.get_addr(**kwargs)) except OSError as e: if e.errno == errno.ENOENT: raise exception.NetworkNamespaceNotFound(netns_name=namespace) raise
def get_link_devices(namespace, **kwargs): """List interfaces in a namespace :return: (list) interfaces in a namespace """ try: with get_iproute(namespace) as ip: return make_serializable(ip.get_links(**kwargs)) except OSError as e: if e.errno == errno.ENOENT: raise exception.NetworkNamespaceNotFound(netns_name=namespace) raise
def create_interface(ifname, namespace, kind, **kwargs): # ifname = ifname[:constants.DEVICE_NAME_MAX_LEN] try: with get_iproute(namespace) as ip: physical_interface = kwargs.pop("physical_interface", None) if physical_interface: link_key = "vxlan_link" if kind == "vxlan" else "link" kwargs[link_key] = get_link_id(physical_interface, namespace) return ip.link("add", ifname=ifname, kind=kind, **kwargs) except NetlinkError as e: if e.code == errno.EEXIST: logging.warning("Interface %(device)s already exists.", ifname) else: raise except OSError as e: if e.errno == errno.ENOENT: raise exception.NetworkNamespaceNotFound(netns_name=namespace) raise
def delete_ip_route(namespace, cidr, ip_version, device=None, via=None, table=None, scope=None, **kwargs): """Delete an IP route""" kwargs.update( _make_pyroute2_route_args(namespace, ip_version, cidr, device, via, table, None, scope, None)) try: with get_iproute(namespace) as ip: ip.route('del', **kwargs) except OSError as e: if e.errno == errno.ENOENT: raise exception.NetworkNamespaceNotFound(netns_name=namespace) raise
def add_ip_route(namespace, cidr, ip_version, device=None, via=None, table=None, metric=None, scope=None, **kwargs): """Add an IP route""" kwargs.update( _make_pyroute2_route_args(namespace, ip_version, cidr, device, via, table, metric, scope, 'static')) try: with get_iproute(namespace) as ip: ip.route('replace', **kwargs) except OSError as e: if e.errno == errno.ENOENT: raise exception.NetworkNamespaceNotFound(netns_name=namespace) raise