| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from oslo_log import log as logging |
| 14 |
from oslo_utils import excutils |
| 15 |
from pyroute2 import iproute |
| 16 |
from pyroute2.netlink import exceptions as ipexc |
| 17 |
from pyroute2.netlink.rtnl import ifinfmsg |
| 18 |
|
| 19 |
from os_vif import exception |
| 20 |
from os_vif.internal.ip import ip_command |
| 21 |
from os_vif import utils |
| 22 |
|
| 23 |
LOG = logging.getLogger(__name__) |
| 24 |
|
| 25 |
|
| 26 |
class PyRoute2(ip_command.IpCommand): |
| 27 |
|
| 28 |
def _ip_link(self, ip, command, check_exit_code, **kwargs): |
| 29 |
try: |
| 30 |
LOG.debug('pyroute2 command %(command)s, arguments %(args)s' % |
| 31 |
{'command': command, 'args': kwargs}) |
| 32 |
return ip.link(command, **kwargs) |
| 33 |
except ipexc.NetlinkError as e: |
| 34 |
with excutils.save_and_reraise_exception() as ctx: |
| 35 |
if e.code in check_exit_code: |
| 36 |
LOG.error('NetlinkError was raised, code %s, message: %s' % |
| 37 |
(e.code, str(e))) |
| 38 |
ctx.reraise = False |
| 39 |
|
| 40 |
def set(self, device, check_exit_code=None, state=None, mtu=None, |
| 41 |
address=None, promisc=None, master=None): |
| 42 |
check_exit_code = check_exit_code or [] |
| 43 |
with iproute.IPRoute() as ip: |
| 44 |
idx = self.lookup_interface(ip, device) |
| 45 |
args = {'index': idx} |
| 46 |
if state: |
| 47 |
args['state'] = state |
| 48 |
if mtu: |
| 49 |
args['mtu'] = mtu |
| 50 |
if address: |
| 51 |
args['address'] = address |
| 52 |
if promisc is not None: |
| 53 |
flags = ip.link('get', index=idx)[0]['flags'] |
| 54 |
args['flags'] = (utils.set_mask(flags, ifinfmsg.IFF_PROMISC) |
| 55 |
if promisc is True else |
| 56 |
utils.unset_mask(flags, ifinfmsg.IFF_PROMISC)) |
| 57 |
if master: |
| 58 |
args['master'] = self.lookup_interface(ip, master) |
| 59 |
|
| 60 |
if isinstance(check_exit_code, int): |
| 61 |
check_exit_code = [check_exit_code] |
| 62 |
|
| 63 |
return self._ip_link(ip, 'set', check_exit_code, **args) |
| 64 |
|
| 65 |
def lookup_interface(self, ip, link): |
| 66 |
|
| 67 |
|
| 68 |
try: |
| 69 |
idx = ip.link_lookup(ifname=link) |
| 70 |
except ipexc.NetlinkError: |
| 71 |
raise exception.NetworkInterfaceNotFound(interface=link) |
| 72 |
if not len(idx): |
| 73 |
raise exception.NetworkInterfaceNotFound(interface=link) |
| 74 |
return idx[0] |
| 75 |
|
| 76 |
def add(self, device, dev_type, check_exit_code=None, peer=None, link=None, |
| 77 |
vlan_id=None, ageing=None, mode=None, multiqueue=False): |
| 78 |
check_exit_code = check_exit_code or [] |
| 79 |
with iproute.IPRoute() as ip: |
| 80 |
args = {'ifname': device, |
| 81 |
'kind': dev_type} |
| 82 |
if self.TYPE_VLAN == dev_type: |
| 83 |
args['vlan_id'] = vlan_id |
| 84 |
args['link'] = self.lookup_interface(ip, link) |
| 85 |
elif self.TYPE_VETH == dev_type: |
| 86 |
args['peer'] = peer |
| 87 |
elif self.TYPE_BRIDGE == dev_type: |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
args['IFLA_BR_FORWARD_DELAY'] = 0 |
| 94 |
args['IFLA_BR_STP_STATE'] = 0 |
| 95 |
args['IFLA_BR_MCAST_SNOOPING'] = 0 |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if ageing is not None: |
| 104 |
args['IFLA_BR_AGEING_TIME'] = ageing |
| 105 |
elif self.TYPE_TUNTAP == dev_type: |
| 106 |
|
| 107 |
|
| 108 |
tap_mode = mode if mode else 'tap' |
| 109 |
args['mode'] = tap_mode |
| 110 |
|
| 111 |
|
| 112 |
if multiqueue: |
| 113 |
args['multi_queue'] = True |
| 114 |
else: |
| 115 |
raise exception.NetworkInterfaceTypeNotDefined(type=dev_type) |
| 116 |
|
| 117 |
return self._ip_link(ip, 'add', check_exit_code, **args) |
| 118 |
|
| 119 |
def delete(self, device, check_exit_code=None): |
| 120 |
check_exit_code = check_exit_code or [] |
| 121 |
with iproute.IPRoute() as ip: |
| 122 |
idx = self.lookup_interface(ip, device) |
| 123 |
return self._ip_link(ip, 'del', check_exit_code, **{'index': idx}) |
| 124 |
|
| 125 |
def exists(self, device): |
| 126 |
"""Return True if the device exists.""" |
| 127 |
with iproute.IPRoute() as ip: |
| 128 |
try: |
| 129 |
self.lookup_interface(ip, device) |
| 130 |
return True |
| 131 |
except Exception: |
| 132 |
return False |