| 1 |
# Copyright (C) 2018 AT&T |
| 2 |
# Copyright (C) 2015 Ericsson AB |
| 3 |
# Copyright (c) 2015 Gigamon |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 |
# not use this file except in compliance with the License. You may obtain |
| 7 |
# a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 |
# License for the specific language governing permissions and limitations |
| 15 |
# under the License. |
| 16 |
|
| 17 |
|
| 18 |
from neutron import manager |
| 19 |
from neutron_taas.services.taas.drivers.linux \ |
| 20 |
import ovs_constants as taas_ovs_consts |
| 21 |
|
| 22 |
from neutron_taas.common import topics |
| 23 |
from neutron_taas.services.taas.agents import taas_agent_api as api |
| 24 |
|
| 25 |
from neutron_lib.api.definitions import portbindings |
| 26 |
from neutron_lib import constants |
| 27 |
from neutron_lib import context as neutron_context |
| 28 |
from neutron_lib import rpc as n_rpc |
| 29 |
from oslo_config import cfg |
| 30 |
from oslo_log import helpers as log_helpers |
| 31 |
from oslo_log import log as logging |
| 32 |
import oslo_messaging as messaging |
| 33 |
from oslo_service import service |
| 34 |
|
| 35 |
LOG = logging.getLogger(__name__) |
| 36 |
|
| 37 |
|
| 38 |
class TaasPluginApi(api.TaasPluginApiMixin): |
| 39 |
|
| 40 |
def __init__(self, topic, host): |
| 41 |
super().__init__(topic, host) |
| 42 |
target = messaging.Target(topic=topic, version='1.0') |
| 43 |
self.client = n_rpc.get_client(target) |
| 44 |
|
| 45 |
def sync_tap_resources(self, sync_tap_res, host): |
| 46 |
"""Send Rpc to plugin to recreate pre-existing tap resources.""" |
| 47 |
LOG.debug("In RPC Call for Sync Tap Resources: Host=%s, MSG=%s", |
| 48 |
host, sync_tap_res) |
| 49 |
|
| 50 |
context = neutron_context.get_admin_context() |
| 51 |
|
| 52 |
cctxt = self.client.prepare(fanout=False) |
| 53 |
cctxt.cast(context, 'sync_tap_resources', sync_tap_res=sync_tap_res, |
| 54 |
host=host) |
| 55 |
|
| 56 |
def set_tap_service_status(self, msg, status, host): |
| 57 |
LOG.debug("In RPC Call for set tap service status: Host=%s, MSG=%s, " |
| 58 |
"Status=%s", host, msg, status) |
| 59 |
|
| 60 |
context = neutron_context.get_admin_context() |
| 61 |
|
| 62 |
cctxt = self.client.prepare(fanout=False) |
| 63 |
cctxt.cast(context, 'set_tap_service_status', msg=msg, status=status, |
| 64 |
host=host) |
| 65 |
|
| 66 |
def set_tap_flow_status(self, msg, status, host): |
| 67 |
LOG.debug("In RPC Call for set tap flow status: Host=%s, MSG=%s, " |
| 68 |
"Status=%s", host, msg, status) |
| 69 |
|
| 70 |
context = neutron_context.get_admin_context() |
| 71 |
|
| 72 |
cctxt = self.client.prepare(fanout=False) |
| 73 |
cctxt.cast(context, 'set_tap_flow_status', msg=msg, status=status, |
| 74 |
host=host) |
| 75 |
|
| 76 |
@log_helpers.log_method_call |
| 77 |
def set_tap_mirror_status(self, msg, status, host): |
| 78 |
LOG.debug("In RPC Call for set Tap Mirror status") |
| 79 |
# TODO(lajoskatona): We can have a status field to indicate that the |
| 80 |
# Neutron port is UP, is it useful? |
| 81 |
|
| 82 |
|
| 83 |
class TaasAgentRpcCallback(api.TaasAgentRpcCallbackMixin): |
| 84 |
|
| 85 |
def __init__(self, conf, driver_type): |
| 86 |
|
| 87 |
LOG.debug("TaaS Agent initialize called") |
| 88 |
|
| 89 |
self.conf = conf |
| 90 |
self.driver_type = driver_type |
| 91 |
|
| 92 |
super().__init__() |
| 93 |
|
| 94 |
def initialize(self): |
| 95 |
self.taas_driver = manager.NeutronManager.load_class_for_provider( |
| 96 |
'neutron_taas.taas.agent_drivers', self.driver_type)() |
| 97 |
self.taas_driver.consume_api(self.agent_api) |
| 98 |
self.taas_driver.initialize() |
| 99 |
self.func_dict = { |
| 100 |
'create_tap_service': { |
| 101 |
'msg_name': 'tap_service', |
| 102 |
'set_status_func_name': 'set_tap_service_status', |
| 103 |
'fail_status': constants.ERROR, |
| 104 |
'succ_status': constants.ACTIVE}, |
| 105 |
'create_tap_flow': { |
| 106 |
'msg_name': 'tap_flow', |
| 107 |
'set_status_func_name': 'set_tap_flow_status', |
| 108 |
'fail_status': constants.ERROR, |
| 109 |
'succ_status': constants.ACTIVE}, |
| 110 |
'delete_tap_service': { |
| 111 |
'msg_name': 'tap_service', |
| 112 |
'set_status_func_name': 'set_tap_service_status', |
| 113 |
'fail_status': constants.PENDING_DELETE, |
| 114 |
'succ_status': constants.INACTIVE}, |
| 115 |
'delete_tap_flow': { |
| 116 |
'msg_name': 'tap_flow', |
| 117 |
'set_status_func_name': 'set_tap_flow_status', |
| 118 |
'fail_status': constants.PENDING_DELETE, |
| 119 |
'succ_status': constants.INACTIVE}, |
| 120 |
'create_tap_mirror': { |
| 121 |
'msg_name': 'tap_mirror', |
| 122 |
'set_status_func_name': 'set_tap_mirror_status', |
| 123 |
'fail_status': constants.ERROR, |
| 124 |
'succ_status': constants.ACTIVE}, |
| 125 |
'delete_tap_mirror': { |
| 126 |
'msg_name': 'tap_mirror', |
| 127 |
'set_status_func_name': 'set_tap_mirror_status', |
| 128 |
'fail_status': constants.PENDING_DELETE, |
| 129 |
'succ_status': constants.INACTIVE}, |
| 130 |
'periodic_tasks': { |
| 131 |
'msg_name': 'periodic_tasks', |
| 132 |
} |
| 133 |
} |
| 134 |
self.portbind_drivers_map = {portbindings.VNIC_DIRECT: 'sriov', |
| 135 |
portbindings.VNIC_NORMAL: 'ovs'} |
| 136 |
self._taas_rpc_setup() |
| 137 |
TaasAgentService(self).start(self.taas_plugin_rpc, self.conf.host) |
| 138 |
|
| 139 |
def consume_api(self, agent_api): |
| 140 |
self.agent_api = agent_api |
| 141 |
|
| 142 |
def _invoke_driver_for_plugin_api(self, context, args, func_name): |
| 143 |
LOG.debug("Invoking Driver for %(func_name)s from agent", |
| 144 |
{'func_name': func_name}) |
| 145 |
|
| 146 |
status_msg = {} |
| 147 |
if func_name != 'periodic_tasks': |
| 148 |
func_dict = self.func_dict[func_name] |
| 149 |
status_msg = {'id': args[func_dict['msg_name']]['id']} |
| 150 |
|
| 151 |
try: |
| 152 |
driver_func = getattr(self.taas_driver, func_name) |
| 153 |
driver_func(args) |
| 154 |
except Exception: |
| 155 |
LOG.error("Failed to invoke the driver") |
| 156 |
rpc_func = getattr( |
| 157 |
self.taas_plugin_rpc, |
| 158 |
self.func_dict[func_name]['set_status_func_name']) |
| 159 |
rpc_func(status_msg, self.func_dict[func_name]['fail_status'], |
| 160 |
self.conf.host) |
| 161 |
return |
| 162 |
|
| 163 |
if func_name != 'periodic_tasks': |
| 164 |
rpc_func = getattr( |
| 165 |
self.taas_plugin_rpc, |
| 166 |
self.func_dict[func_name]['set_status_func_name']) |
| 167 |
rpc_func(status_msg, self.func_dict[func_name]['succ_status'], |
| 168 |
self.conf.host) |
| 169 |
|
| 170 |
def create_tap_service(self, context, tap_service_msg, host): |
| 171 |
"""Handle Rpc from plugin to create a tap_service.""" |
| 172 |
if not self._driver_and_host_verification( |
| 173 |
host, tap_service_msg['port']): |
| 174 |
LOG.debug("RPC Call for Create Tap Serv. Either Host value [%s]" |
| 175 |
"(received in RPC) doesn't match the host value " |
| 176 |
"stored in agent [%s], or incompatible driver type. " |
| 177 |
"Ignoring the message.", host, self.conf.host) |
| 178 |
return |
| 179 |
LOG.debug("In RPC Call for Create Tap Service: MSG=%s", |
| 180 |
tap_service_msg) |
| 181 |
|
| 182 |
return self._invoke_driver_for_plugin_api( |
| 183 |
context, |
| 184 |
tap_service_msg, |
| 185 |
'create_tap_service') |
| 186 |
|
| 187 |
def create_tap_flow(self, context, tap_flow_msg, host): |
| 188 |
if not self._driver_and_host_verification(host, tap_flow_msg['port']): |
| 189 |
LOG.debug("RPC Call for Create Tap Flow. Either Host value [%s]" |
| 190 |
"(received in RPC) doesn't match the host value " |
| 191 |
"stored in agent [%s], or incompatible driver type. " |
| 192 |
"Ignoring the message.", host, self.conf.host) |
| 193 |
return |
| 194 |
LOG.debug("In RPC Call for Create Tap Flow: MSG=%s", tap_flow_msg) |
| 195 |
|
| 196 |
return self._invoke_driver_for_plugin_api( |
| 197 |
context, |
| 198 |
tap_flow_msg, |
| 199 |
'create_tap_flow') |
| 200 |
|
| 201 |
def delete_tap_service(self, context, tap_service_msg, host): |
| 202 |
# |
| 203 |
# Cleanup operations must be performed by all hosts |
| 204 |
# where the source and/or destination ports associated |
| 205 |
# with this tap service were residing. |
| 206 |
# |
| 207 |
if not self._is_driver_port_type_compatible(tap_service_msg['port']): |
| 208 |
LOG.debug("RPC Call for Delete Tap Service. Incompatible driver " |
| 209 |
"type. Ignoring the message. Host=[%s]", host) |
| 210 |
return |
| 211 |
LOG.debug("In RPC Call for Delete Tap Service: MSG=%s", |
| 212 |
tap_service_msg) |
| 213 |
|
| 214 |
return self._invoke_driver_for_plugin_api( |
| 215 |
context, |
| 216 |
tap_service_msg, |
| 217 |
'delete_tap_service') |
| 218 |
|
| 219 |
def delete_tap_flow(self, context, tap_flow_msg, host): |
| 220 |
if not self._driver_and_host_verification(host, tap_flow_msg['port']): |
| 221 |
LOG.debug("RPC Call for Delete Tap Flow. Either Host value [%s]" |
| 222 |
"(received in RPC) doesn't match the host value " |
| 223 |
"stored in agent [%s], or incompatible driver type. " |
| 224 |
"Ignoring the message.", host, self.conf.host) |
| 225 |
return |
| 226 |
LOG.debug("In RPC Call for Delete Tap Flow: MSG=%s", tap_flow_msg) |
| 227 |
|
| 228 |
return self._invoke_driver_for_plugin_api( |
| 229 |
context, |
| 230 |
tap_flow_msg, |
| 231 |
'delete_tap_flow') |
| 232 |
|
| 233 |
@log_helpers.log_method_call |
| 234 |
def create_tap_mirror(self, context, tap_mirror_msg, host): |
| 235 |
"""Handle Rpc from plugin to create a tap_mirror.""" |
| 236 |
if not self._driver_and_host_verification( |
| 237 |
host, tap_mirror_msg['port']): |
| 238 |
LOG.debug("RPC Call for Create Tap Mirror. Either Host value [%s]" |
| 239 |
"(received in RPC) doesn't match the host value " |
| 240 |
"stored in agent [%s], or incompatible driver type. " |
| 241 |
"Ignoring the message.", host, self.conf.host) |
| 242 |
return |
| 243 |
LOG.debug("In RPC Call for Creae Tap Mirror: MSG=%s", tap_mirror_msg) |
| 244 |
|
| 245 |
return self._invoke_driver_for_plugin_api( |
| 246 |
context, |
| 247 |
tap_mirror_msg, |
| 248 |
'create_tap_mirror') |
| 249 |
|
| 250 |
@log_helpers.log_method_call |
| 251 |
def delete_tap_mirror(self, context, tap_mirror_msg, host): |
| 252 |
"""....""" |
| 253 |
if not self._driver_and_host_verification(host, |
| 254 |
tap_mirror_msg['port']): |
| 255 |
LOG.debug("RPC Call for Delete Tap Mirror. Either Host value [%s]" |
| 256 |
"(received in RPC) doesn't match the host value " |
| 257 |
"stored in agent [%s], or incompatible driver type. " |
| 258 |
"Ignoring the message.", host, self.conf.host) |
| 259 |
return |
| 260 |
LOG.debug("In RPC Call for Delete Tap Mirror: MSG=%s", tap_mirror_msg) |
| 261 |
|
| 262 |
return self._invoke_driver_for_plugin_api( |
| 263 |
context, |
| 264 |
tap_mirror_msg, |
| 265 |
'delete_tap_mirror') |
| 266 |
|
| 267 |
def _taas_rpc_setup(self): |
| 268 |
# setup RPC to msg taas plugin |
| 269 |
self.taas_plugin_rpc = TaasPluginApi( |
| 270 |
topics.TAAS_PLUGIN, self.conf.host) |
| 271 |
|
| 272 |
endpoints = [self] |
| 273 |
conn = n_rpc.Connection() |
| 274 |
conn.create_consumer(topics.TAAS_AGENT, endpoints, fanout=False) |
| 275 |
conn.consume_in_threads() |
| 276 |
|
| 277 |
def periodic_tasks(self): |
| 278 |
return self._invoke_driver_for_plugin_api( |
| 279 |
context=None, |
| 280 |
args=None, |
| 281 |
func_name='periodic_tasks') |
| 282 |
|
| 283 |
def get_driver_type(self): |
| 284 |
return self.driver_type |
| 285 |
|
| 286 |
def _is_driver_port_type_compatible(self, port): |
| 287 |
return ( |
| 288 |
port.get(portbindings.VNIC_TYPE) in self.portbind_drivers_map and |
| 289 |
self.portbind_drivers_map[port.get(portbindings.VNIC_TYPE)] == |
| 290 |
self.driver_type) |
| 291 |
|
| 292 |
def _driver_and_host_verification(self, host, port): |
| 293 |
return ((host == self.conf.host) and |
| 294 |
self._is_driver_port_type_compatible(port)) |
| 295 |
|
| 296 |
|
| 297 |
class TaasAgentService(service.Service): |
| 298 |
def __init__(self, driver): |
| 299 |
super().__init__() |
| 300 |
self.driver = driver |
| 301 |
|
| 302 |
def start(self, taas_plugin_rpc, host): |
| 303 |
super().start() |
| 304 |
|
| 305 |
if self.driver.get_driver_type() == \ |
| 306 |
taas_ovs_consts.EXTENSION_DRIVER_TYPE: |
| 307 |
self.tg.add_timer( |
| 308 |
int(cfg.CONF.taas_agent_periodic_interval), |
| 309 |
self.driver.periodic_tasks, |
| 310 |
None |
| 311 |
) |
| 312 |
|
| 313 |
# Indicate the TaaS plugin to recreate the taas resources |
| 314 |
rpc_msg = {'host_id': host} |
| 315 |
taas_plugin_rpc.sync_tap_resources(rpc_msg, host) |