Debian Codegraph @

os_vif/internal/ip/linux/impl_pyroute2.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
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 # TODO(sean-k-mooney): remove try block after we raise
67 # the min pyroute2 version above 0.5.12
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 # NOTE(sean-k-mooney): the keys are defined in the pyroute2
89 # codebase but are not documented. see the nla_map field
90 # in the bridge_data class located in the
91 # pyroute2.netlink.rtnl.ifinfmsg module for mode details
92 # https://github.com/svinota/pyroute2/blob/3ba9cdde34b2346ef8c2f8ba17cef5dbeb4c6d52/pyroute2/netlink/rtnl/ifinfmsg/__init__.py#L776-L820
93 args['IFLA_BR_FORWARD_DELAY'] = 0 # set no delay
94 args['IFLA_BR_STP_STATE'] = 0 # disable spanning tree
95 args['IFLA_BR_MCAST_SNOOPING'] = 0 # disable snooping
96 # NOTE(sean-k-mooney): we conditionally enable mac ageing as
97 # this code is shared between the ovs and linux bridge
98 # plugins. For linux bridge we want to allow the default
99 # ageing of 300 seconds, whereas for ovs with the ip-tables
100 # firewall we want to disable ageing. None was chosen as
101 # the default value of ageing to allow the caller to determine
102 # what policy to use and keep this code generic.
103 if ageing is not None:
104 args['IFLA_BR_AGEING_TIME'] = ageing
105 elif self.TYPE_TUNTAP == dev_type:
106 # Set mode (default to 'tap' if not specified)
107 # This matches the 'ip tuntap add' command behavior
108 tap_mode = mode if mode else 'tap'
109 args['mode'] = tap_mode
110 # Enable multiqueue if requested
111 # This sets the IFF_MULTI_QUEUE flag in IFTUN_IFR
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