Debian Codegraph @

neutron_taas/services/taas/drivers/linux/sriov_nic_taas.py

raw
1 # Copyright (C) 2018 AT&T
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15
16 from neutron.conf.agent import common
17 from neutron_taas.common import constants as taas_consts
18 from neutron_taas.common import utils as common_utils
19 from neutron_taas.services.taas.agents.extensions import taas as taas_base
20 from neutron_taas.services.taas.drivers.linux import sriov_nic_exceptions \
21 as taas_exc
22 from neutron_taas.services.taas.drivers.linux import sriov_nic_utils \
23 as sriov_utils
24
25 from oslo_config import cfg
26 from oslo_log import helpers as log_helpers
27 from oslo_log import log as logging
28 from oslo_utils import excutils
29 import threading
30
31 LOG = logging.getLogger(__name__)
32
33 TaaS_DRIVER_NAME = 'Taas SRIOV NIC Switch driver'
34
35
36 class SriovNicTaasDriver(taas_base.TaasAgentDriver):
37 def __init__(self):
38 super().__init__()
39 LOG.debug("Initializing Taas SRIOV NIC Switch Driver")
40 self.agent_api = None
41 self.root_helper = common.get_root_helper(cfg.CONF)
42 self.lock = threading.Lock()
43
44 def initialize(self):
45 LOG.debug("Initialize routine called for Taas SRIOV NIC Switch Driver")
46 self.sriov_utils = sriov_utils.SriovNicUtils()
47
48 def consume_api(self, agent_api):
49 self.agent_api = agent_api
50
51 @log_helpers.log_method_call
52 def create_tap_service(self, tap_service_msg):
53 """Create a tap service
54
55 :param tap_service_msg: a dict of the tap_service,
56 taas_id which is the VLAN Id reserved for
57 mirroring for this tap-service and the neutron
58 port of the tap-service:
59 {tap_service: {}, taas_id: VID, port: {}}
60 """
61 ts_port = tap_service_msg['port']
62
63 LOG.debug("SRIOV Driver: Inside create_tap_service: "
64 "Port-id: %(port_id)s",
65 {'port_id': ts_port['id']})
66
67 port_params = self.sriov_utils.get_sriov_port_params(ts_port)
68
69 LOG.info("TaaS SRIOV: create_tap_service RPC invoked for "
70 "port %(id)s, MAC %(ts_port_mac)s, PCI %(ts_pci_slot)s, "
71 "VF-Index %(vf_index)s, PF-Device %(pf_device)s, "
72 "src_vlans %(src_vlans)s; ",
73 {'id': ts_port['id'],
74 'ts_port_mac': port_params['mac'],
75 'ts_pci_slot': port_params['pci_slot'],
76 'vf_index': port_params['vf_index'],
77 'pf_device': port_params['pf_device'],
78 'src_vlans': port_params['src_vlans']})
79
80 @log_helpers.log_method_call
81 def delete_tap_service(self, tap_service_msg):
82 """Delete a tap service
83
84 :param tap_service_msg: a dict of the tap_service,
85 taas_id which is the VLAN Id reserved for
86 mirroring for this tap-service and the neutron
87 port of the tap-service:
88 {tap_service: {}, taas_id: VID, port: {}}
89 """
90 ts_port = tap_service_msg['port']
91
92 LOG.debug("SRIOV Driver: Inside delete_tap_service: "
93 "Port-id: %(port_id)s",
94 {'port_id': ts_port['id']})
95
96 port_params = self.sriov_utils.get_sriov_port_params(ts_port)
97
98 LOG.info("TaaS SRIOV: delete_tap_service RPC invoked for "
99 "port %(id)s, MAC %(ts_port_mac)s, PCI %(ts_pci_slot)s, "
100 "VF-Index %(vf_index)s, PF-Device %(pf_device)s, "
101 "src_vlans %(src_vlans)s; ",
102 {'id': ts_port['id'],
103 'ts_port_mac': port_params['mac'],
104 'ts_pci_slot': port_params['pci_slot'],
105 'vf_index': port_params['vf_index'],
106 'pf_device': port_params['pf_device'],
107 'src_vlans': port_params['src_vlans']})
108
109 @log_helpers.log_method_call
110 def create_tap_flow(self, tap_flow_msg):
111 """Create a tap flow
112
113 :param tap_flow_msg: a dict of the tap_flow, the mac of the port of
114 the tap-flow, taas_id which is the VLAN Id
115 reserved for mirroring for the tap-service
116 associated with this tap-flow, the neutron port
117 of the tap-flow, and the port of the
118 tap-service:
119 {tap_flow: {}, port_mac: '', taas_id: '',
120 port: {}, tap_service_port: {}}
121 """
122 source_port = tap_flow_msg['port']
123 ts_port = tap_flow_msg['tap_service_port']
124 direction = tap_flow_msg['tap_flow']['direction']
125 vlan_filter = tap_flow_msg['tap_flow']['vlan_filter']
126 vf_to_vf_all_vlans = False
127
128 LOG.debug("SRIOV Driver: Inside create_tap_flow: "
129 "SRC Port-id: %(src_port_id)s, "
130 "DEST Port-id: %(dest_port_id)s "
131 "Direction: %(direction)s",
132 {'src_port_id': source_port['id'],
133 'dest_port_id': ts_port['id'],
134 'direction': direction})
135
136 src_port_params = self.sriov_utils.get_sriov_port_params(source_port)
137 ts_port_params = self.sriov_utils.get_sriov_port_params(ts_port)
138
139 LOG.info("TaaS src_port_params "
140 "port %(id)s, MAC %(port_mac)s, PCI %(pci_slot)s, "
141 "VF-Index %(vf_index)s, PF-Device %(pf_device)s, "
142 "src_vlans %(src_vlans)s; ",
143 {'id': source_port['id'],
144 'port_mac': src_port_params['mac'],
145 'pci_slot': src_port_params['pci_slot'],
146 'vf_index': src_port_params['vf_index'],
147 'pf_device': src_port_params['pf_device'],
148 'src_vlans': src_port_params['src_vlans']})
149
150 LOG.info("TaaS ts_port_params "
151 "port %(id)s, MAC %(port_mac)s, PCI %(pci_slot)s, "
152 "VF-Index %(vf_index)s, PF-Device %(pf_device)s, "
153 "VLAN-Filter %(vlan_filter)s, src_vlans %(src_vlans)s; ",
154 {'id': ts_port['id'],
155 'port_mac': ts_port_params['mac'],
156 'pci_slot': ts_port_params['pci_slot'],
157 'vf_index': ts_port_params['vf_index'],
158 'pf_device': ts_port_params['pf_device'],
159 'vlan_filter': vlan_filter,
160 'src_vlans': ts_port_params['src_vlans']})
161
162 # If no VLAN filter configured on source port, then include all vlans
163 if not src_port_params['src_vlans'] or \
164 src_port_params['src_vlans'] == '0':
165 src_port_params['src_vlans'] = taas_consts.VLAN_RANGE
166 LOG.debug("TaaS no src_vlans in src_port")
167
168 # If no VLAN filter configured on probe port, then include all vlans
169 if not vlan_filter:
170 vlan_filter = taas_consts.VLAN_RANGE
171 vf_to_vf_all_vlans = True
172 LOG.debug("VF to VF mirroring for all VLANs. "
173 "Direction %(direction)s",
174 {'direction': direction})
175
176 if not src_port_params['pci_slot']:
177 LOG.error("No PCI Slot for source_port %(id)s with MAC %(mac)s; ",
178 {'id': source_port['id'],
179 'mac': src_port_params['mac'],
180 'source_vlans': src_port_params['src_vlans']})
181 raise taas_exc.PciSlotNotFound(port_id=source_port['id'],
182 mac=src_port_params['mac'])
183
184 if not ts_port_params['pci_slot']:
185 LOG.error("No PCI Slot for ts_port %(id)s with MAC %(mac)s; ",
186 {'id': ts_port['id'], 'mac': ts_port_params['mac'],
187 'vlan_filter': vlan_filter})
188 raise taas_exc.PciSlotNotFound(port_id=ts_port['id'],
189 mac=ts_port_params['mac'])
190
191 if src_port_params['pf_device'] != ts_port_params['pf_device']:
192 LOG.error("SRIOV NIC Driver only supports mirroring b/w "
193 "VF_src %(VF_src)s -> VF_probe %(VF_probe)s on same PF. "
194 "PF_src %(PF_src)s and PF_probe %(PF_probe)s "
195 "are different; ",
196 {'VF_src': src_port_params['vf_index'],
197 'VF_probe': ts_port_params['vf_index'],
198 'PF_src': src_port_params['pf_device'],
199 'PF_probe': ts_port_params['pf_device']})
200 return
201
202 # Fetch common VLAN tags
203 src_vlans_list = sorted(set(common_utils.get_list_from_ranges_str(
204 src_port_params['src_vlans'])))
205 vlan_filter_list = sorted(set(
206 common_utils.get_list_from_ranges_str(vlan_filter)))
207
208 common_vlans_list = list(set(src_vlans_list).intersection(
209 vlan_filter_list))
210 common_vlans_rng_str = common_utils.get_ranges_str_from_list(
211 common_vlans_list)
212
213 LOG.info("TaaS src_vlans_list %(src_vlans_list)s, "
214 "vlan_filter_list %(vlan_filter_list)s, "
215 "common_vlans_list %(common_vlans_list)s, "
216 "common_vlans_rng_str %(common_vlans_rng_str)s; ",
217 {'src_vlans_list': src_vlans_list,
218 'vlan_filter_list': vlan_filter_list,
219 'common_vlans_list': common_vlans_list,
220 'common_vlans_rng_str': common_vlans_rng_str})
221
222 if ts_port_params['pf_device'] and \
223 ts_port_params['vf_index'] and \
224 src_port_params['vf_index']:
225 with self.lock:
226 try:
227 LOG.info("TaaS invoking execute_sysfs_command")
228 self.sriov_utils.execute_sysfs_command(
229 'add',
230 ts_port_params,
231 src_port_params,
232 common_vlans_rng_str,
233 vf_to_vf_all_vlans,
234 direction)
235 except Exception:
236 LOG.error("TaaS error in invoking execute_sysfs_command")
237 with excutils.save_and_reraise_exception():
238 raise taas_exc.SriovNicSwitchDriverInvocationError(
239 tap_service_pf_device=ts_port_params['pf_device'],
240 tap_service_vf_index=ts_port_params['vf_index'],
241 source_vf_index=src_port_params['vf_index'],
242 vlan_filter=common_vlans_rng_str,
243 vf_to_vf_all_vlans=vf_to_vf_all_vlans,
244 direction=direction)
245 return
246
247 @log_helpers.log_method_call
248 def delete_tap_flow(self, tap_flow_msg):
249 """Delete a tap flow
250
251 :param tap_flow_msg: a dict of the tap_flow, the mac of the port of
252 the tap-flow, taas_id which is the VLAN Id
253 reserved for mirroring for the tap-service
254 associated with this tap-flow, the neutron port
255 of the tap-flow, the port of the
256 tap-service, the list of source VLAN IDs, and
257 VLAN filter list.
258 {tap_flow: {}, port_mac: '', taas_id: '',
259 port: {}, tap_service_port: {},
260 source_vlans_list: [], vlan_filter_list: []}
261 """
262 source_port = tap_flow_msg['port']
263 ts_port = tap_flow_msg['tap_service_port']
264 vlan_filter = tap_flow_msg['tap_flow']['vlan_filter']
265 direction = tap_flow_msg['tap_flow']['direction']
266 vf_to_vf_all_vlans = False
267
268 LOG.debug("SRIOV Driver: Inside delete_tap_flow: "
269 "SRC Port-id: %(src_port_id)s, "
270 "DEST Port-id: %(dest_port_id)s "
271 "Direction: %(direction)s",
272 {'src_port_id': source_port['id'],
273 'dest_port_id': ts_port['id'],
274 'direction': direction})
275
276 src_port_params = self.sriov_utils.get_sriov_port_params(source_port)
277 ts_port_params = self.sriov_utils.get_sriov_port_params(ts_port)
278
279 LOG.info("TaaS src_port_params "
280 "port %(id)s, MAC %(port_mac)s, PCI %(pci_slot)s, "
281 "VF-Index %(vf_index)s, PF-Device %(pf_device)s, "
282 "src_vlans %(src_vlans)s; ",
283 {'id': source_port['id'],
284 'port_mac': src_port_params['mac'],
285 'pci_slot': src_port_params['pci_slot'],
286 'vf_index': src_port_params['vf_index'],
287 'pf_device': src_port_params['pf_device'],
288 'src_vlans': src_port_params['src_vlans']})
289
290 LOG.info("TaaS ts_port_params "
291 "port %(id)s, MAC %(port_mac)s, PCI %(pci_slot)s, "
292 "VF-Index %(vf_index)s, PF-Device %(pf_device)s, "
293 "VLAN-Filter %(vlan_filter)s, src_vlans %(src_vlans)s; ",
294 {'id': ts_port['id'],
295 'port_mac': ts_port_params['mac'],
296 'pci_slot': ts_port_params['pci_slot'],
297 'vf_index': ts_port_params['vf_index'],
298 'pf_device': ts_port_params['pf_device'],
299 'vlan_filter': vlan_filter,
300 'src_vlans': ts_port_params['src_vlans']})
301
302 # If no VLAN filter configured on source port, then include all vlans
303 if not src_port_params['src_vlans']:
304 src_port_params['src_vlans'] = taas_consts.VLAN_RANGE
305 LOG.debug("TaaS no src_vlans in src_port")
306
307 # If no VLAN filter configured on probe port, then include all vlans
308 if not vlan_filter:
309 vf_to_vf_all_vlans = True
310 LOG.debug("VF to VF mirroring for all VLANs. "
311 "Direction %(direction)s",
312 {'direction': direction})
313
314 if not src_port_params['pci_slot']:
315 LOG.error("No PCI Slot for source_port %(id)s with MAC %(mac)s; ",
316 {'id': source_port['id'],
317 'mac': src_port_params['mac'],
318 'source_vlans': src_port_params['src_vlans']})
319 raise taas_exc.PciSlotNotFound(port_id=source_port['id'],
320 mac=src_port_params['mac'])
321
322 if not ts_port_params['pci_slot']:
323 LOG.error("No PCI Slot for ts_port %(id)s with MAC %(mac)s; ",
324 {'id': ts_port['id'], 'mac': ts_port_params['mac']})
325 raise taas_exc.PciSlotNotFound(port_id=ts_port['id'],
326 mac=ts_port_params['mac'])
327
328 # Fetch common VLAN tags
329 src_vlans_list = []
330 for src_vlans_str in tap_flow_msg['source_vlans_list']:
331 src_vlans_list.extend(common_utils.get_list_from_ranges_str(
332 src_vlans_str))
333
334 src_vlans_list = sorted(set(src_vlans_list))
335
336 vlan_filter_list = []
337 for vlan_filter_str in tap_flow_msg['vlan_filter_list']:
338 vlan_filter_list.extend(common_utils.get_list_from_ranges_str(
339 vlan_filter_str))
340
341 vlan_filter_list = sorted(set(vlan_filter_list))
342
343 common_vlans_list = \
344 list(set(src_vlans_list).intersection(vlan_filter_list))
345
346 common_vlans_rng_str = \
347 common_utils.get_ranges_str_from_list(common_vlans_list)
348
349 LOG.info("TaaS src_vlans_list %(src_vlans_list)s, "
350 "vlan_filter_list %(vlan_filter_list)s, "
351 "common_vlans_list %(common_vlans_list)s, "
352 "common_vlans_rng_str %(common_vlans_rng_str)s; ",
353 {'src_vlans_list': src_vlans_list,
354 'vlan_filter_list': vlan_filter_list,
355 'common_vlans_list': common_vlans_list,
356 'common_vlans_rng_str': common_vlans_rng_str})
357
358 if ts_port_params['pf_device'] and \
359 ts_port_params['vf_index'] and \
360 src_port_params['vf_index']:
361
362 with self.lock:
363 try:
364 LOG.info("TaaS invoking execute_sysfs_command")
365 self.sriov_utils.execute_sysfs_command('rem',
366 ts_port_params,
367 src_port_params,
368 taas_consts.
369 VLAN_RANGE,
370 False,
371 'BOTH')
372 except Exception:
373 LOG.error("TaaS error in invoking execute_sysfs_command")
374 with excutils.save_and_reraise_exception():
375 raise taas_exc.SriovNicSwitchDriverInvocationError(
376 ts_pf_dev=ts_port_params['pf_device'],
377 ts_vf_index=ts_port_params['vf_index'],
378 source_vf_index=src_port_params['vf_index'],
379 common_vlans_rng_str=taas_consts.VLAN_RANGE,
380 vf_to_vf_all_vlans=vf_to_vf_all_vlans,
381 direction=direction)
382
383 if common_vlans_rng_str:
384 try:
385 LOG.info("TaaS invoking execute_sysfs_command")
386 self.sriov_utils.execute_sysfs_command(
387 'add',
388 ts_port_params,
389 src_port_params,
390 common_vlans_rng_str,
391 False,
392 'BOTH')
393 except Exception:
394 LOG.error("TaaS error in invoking "
395 "execute_sysfs_command")
396 with excutils.save_and_reraise_exception():
397 raise taas_exc.SriovNicSwitchDriverInvocationError(
398 ts_pf_dev=ts_port_params['pf_device'],
399 ts_vf_index=ts_port_params['vf_index'],
400 source_vf_index=src_port_params['vf_index'],
401 common_vlans_rng_str=common_vlans_rng_str,
402 vf_to_vf_all_vlans=vf_to_vf_all_vlans,
403 direction=direction)