Arista drivers for ML2 and L3 Service Plugin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

387 lines
16 KiB

  1. # Copyright 2014 Arista Networks, Inc. All rights reserved.
  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. import copy
  15. import traceback
  16. from neutron_lib.agent import topics
  17. from neutron_lib import constants as n_const
  18. from neutron_lib import context as nctx
  19. from neutron_lib.plugins import constants as plugin_constants
  20. from neutron_lib.plugins import directory
  21. from neutron_lib import rpc as n_rpc
  22. from neutron_lib.services import base as service_base
  23. from neutron_lib import worker
  24. from oslo_config import cfg
  25. from oslo_log import helpers as log_helpers
  26. from oslo_log import log as logging
  27. from oslo_service import loopingcall
  28. from oslo_utils import excutils
  29. from neutron.api.rpc.agentnotifiers import l3_rpc_agent_api
  30. from neutron.api.rpc.handlers import l3_rpc
  31. from neutron.db import extraroute_db
  32. from neutron.db import l3_agentschedulers_db
  33. from neutron.db import l3_gwmode_db
  34. from neutron.plugins.ml2.driver_context import NetworkContext # noqa
  35. from networking_arista._i18n import _LE, _LI
  36. from networking_arista.l3Plugin import arista_l3_driver
  37. LOG = logging.getLogger(__name__)
  38. class AristaL3SyncWorker(worker.BaseWorker):
  39. def __init__(self, driver):
  40. self.driver = driver
  41. self._enable_cleanup = driver._enable_cleanup
  42. self._protected_vlans = driver._protected_vlans
  43. self._servers = driver._servers
  44. self._use_vrf = driver._use_vrf
  45. self._loop = None
  46. super(AristaL3SyncWorker, self).__init__(worker_process_count=0)
  47. def start(self):
  48. super(AristaL3SyncWorker, self).start()
  49. if self._loop is None:
  50. self._loop = loopingcall.FixedIntervalLoopingCall(
  51. self.synchronize
  52. )
  53. self._loop.start(interval=cfg.CONF.l3_arista.l3_sync_interval)
  54. def stop(self):
  55. if self._loop is not None:
  56. self._loop.stop()
  57. def wait(self):
  58. if self._loop is not None:
  59. self._loop.wait()
  60. self._loop = None
  61. def reset(self):
  62. self.stop()
  63. self.wait()
  64. self.start()
  65. def get_subnet_info(self, subnet_id):
  66. return self.get_subnet(subnet_id)
  67. def get_routers_and_interfaces(self):
  68. core = directory.get_plugin()
  69. ctx = nctx.get_admin_context()
  70. routers = directory.get_plugin(plugin_constants.L3).get_routers(ctx)
  71. router_interfaces = list()
  72. for r in routers:
  73. ports = core.get_ports(
  74. ctx,
  75. filters={
  76. 'device_id': [r['id']],
  77. 'device_owner': [n_const.DEVICE_OWNER_ROUTER_INTF]}) or []
  78. for p in ports:
  79. router_interface = r.copy()
  80. net_id = p['network_id']
  81. subnet_id = p['fixed_ips'][0]['subnet_id']
  82. subnet = core.get_subnet(ctx, subnet_id)
  83. ml2_db = NetworkContext(self, ctx, {'id': net_id})
  84. seg_id = ml2_db.network_segments[0]['segmentation_id']
  85. router_interface['seg_id'] = seg_id
  86. router_interface['cidr'] = subnet['cidr']
  87. router_interface['gip'] = subnet['gateway_ip']
  88. router_interface['fixed_ip'] = p['fixed_ips'][0]['ip_address']
  89. router_interface['ip_version'] = subnet['ip_version']
  90. router_interface['subnet_id'] = subnet_id
  91. router_interfaces.append(router_interface)
  92. return routers, router_interfaces
  93. def synchronize(self):
  94. """Synchronizes Router DB from Neturon DB with EOS.
  95. Walks through the Neturon Db and ensures that all the routers
  96. created in Netuton DB match with EOS. After creating appropriate
  97. routers, it ensures to add interfaces as well.
  98. Uses idempotent properties of EOS configuration, which means
  99. same commands can be repeated.
  100. """
  101. LOG.info(_LI('Syncing Neutron Router DB <-> EOS'))
  102. # Update vrf creation command support if needed
  103. try:
  104. if self._use_vrf:
  105. self.driver._update_vrf_commands()
  106. routers, router_interfaces = self.get_routers_and_interfaces()
  107. expected_vrfs = set()
  108. if self._use_vrf:
  109. expected_vrfs.update(self.driver._arista_router_name(
  110. r['id'], r['name']) for r in routers)
  111. expected_vlans = set(r['seg_id'] for r in router_interfaces)
  112. if self._enable_cleanup:
  113. LOG.info(_LI('Syncing Neutron Router DB - cleanup'))
  114. self.do_cleanup(expected_vrfs, expected_vlans)
  115. LOG.info(_LI('Syncing Neutron Router DB - creating routers'))
  116. self.create_routers(routers)
  117. LOG.info(_LI('Syncing Neutron Router DB - creating interfaces'))
  118. self.create_router_interfaces(router_interfaces)
  119. LOG.info(_LI('Syncing Neutron Router DB finished'))
  120. except Exception:
  121. exc_str = traceback.format_exc()
  122. LOG.error(_LE("Error during synchronize processing %s"), exc_str)
  123. def get_vrfs(self, server):
  124. ret = self.driver._run_eos_cmds(['show vrf'], server)
  125. if len(ret or []) != 1 or 'vrfs' not in ret[0].keys():
  126. return set()
  127. eos_vrfs = set(vrf for vrf in ret[0]['vrfs'].keys()
  128. if vrf.startswith('__OpenStack__'))
  129. return eos_vrfs
  130. def get_svis(self, server):
  131. ret = self.driver._run_eos_cmds(['show ip interface'], server)
  132. if len(ret or []) != 1 or 'interfaces' not in ret[0].keys():
  133. return set()
  134. eos_svis = set(
  135. int(vlan.strip('Vlan'))
  136. for vlan in ret[0]['interfaces'].keys() if 'Vlan' in vlan)
  137. return eos_svis
  138. def get_vlans(self, server):
  139. ret = self.driver._run_eos_cmds(['show vlan'], server)
  140. if len(ret or []) != 1 or 'vlans' not in ret[0].keys():
  141. return set()
  142. eos_vlans = set(int(vlan) for vlan, info in ret[0]['vlans'].items()
  143. if not info['dynamic'])
  144. return eos_vlans
  145. def do_cleanup(self, expected_vrfs, expected_vlans):
  146. for server in self._servers:
  147. eos_svis = self.get_svis(server)
  148. eos_vlans = self.get_vlans(server)
  149. svis_to_delete = (eos_svis - self._protected_vlans
  150. - expected_vlans)
  151. vlans_to_delete = (eos_vlans - self._protected_vlans
  152. - expected_vlans)
  153. delete_cmds = []
  154. delete_cmds.extend('no interface vlan %s' % svi
  155. for svi in svis_to_delete)
  156. delete_cmds.extend('no vlan %s' % vlan
  157. for vlan in vlans_to_delete)
  158. if self._use_vrf:
  159. eos_vrfs = self.get_vrfs(server)
  160. vrfs_to_delete = eos_vrfs - expected_vrfs
  161. delete_cmds.extend([c.format(vrf)
  162. for c in self.driver.routerDict['delete']
  163. for vrf in vrfs_to_delete])
  164. if delete_cmds:
  165. self.driver._run_config_cmds(delete_cmds, server)
  166. def create_routers(self, routers):
  167. for r in routers:
  168. try:
  169. self.driver.create_router(self, r)
  170. except Exception:
  171. LOG.error(_LE("Error Adding router %(router_id)s "
  172. "on Arista HW"), {'router_id': r})
  173. def create_router_interfaces(self, router_interfaces):
  174. for r in router_interfaces:
  175. try:
  176. self.driver.add_router_interface(self, r)
  177. except Exception:
  178. LOG.error(_LE("Error Adding interface %(subnet_id)s "
  179. "to router %(router_id)s on Arista HW"),
  180. {'subnet_id': r['subnet_id'], 'router_id': r['id']})
  181. class AristaL3ServicePlugin(service_base.ServicePluginBase,
  182. extraroute_db.ExtraRoute_db_mixin,
  183. l3_gwmode_db.L3_NAT_db_mixin,
  184. l3_agentschedulers_db.L3AgentSchedulerDbMixin):
  185. """Implements L3 Router service plugin for Arista hardware.
  186. Creates routers in Arista hardware, manages them, adds/deletes interfaces
  187. to the routes.
  188. """
  189. supported_extension_aliases = ["router", "ext-gw-mode",
  190. "extraroute"]
  191. def __init__(self, driver=None):
  192. super(AristaL3ServicePlugin, self).__init__()
  193. self.driver = driver or arista_l3_driver.AristaL3Driver()
  194. self.setup_rpc()
  195. self.add_worker(AristaL3SyncWorker(self.driver))
  196. def setup_rpc(self):
  197. # RPC support
  198. self.topic = topics.L3PLUGIN
  199. self.conn = n_rpc.Connection()
  200. self.agent_notifiers.update(
  201. {n_const.AGENT_TYPE_L3: l3_rpc_agent_api.L3AgentNotifyAPI()})
  202. self.endpoints = [l3_rpc.L3RpcCallback()]
  203. self.conn.create_consumer(self.topic, self.endpoints,
  204. fanout=False)
  205. self.conn.consume_in_threads()
  206. def get_plugin_type(self):
  207. return plugin_constants.L3
  208. def get_plugin_description(self):
  209. """Returns string description of the plugin."""
  210. return ("Arista L3 Router Service Plugin for Arista Hardware "
  211. "based routing")
  212. @log_helpers.log_method_call
  213. def create_router(self, context, router):
  214. """Create a new router entry in DB, and create it Arista HW."""
  215. # Add router to the DB
  216. new_router = super(AristaL3ServicePlugin, self).create_router(
  217. context,
  218. router)
  219. # create router on the Arista Hw
  220. try:
  221. self.driver.create_router(context, new_router)
  222. return new_router
  223. except Exception:
  224. with excutils.save_and_reraise_exception():
  225. LOG.error(_LE("Error creating router on Arista HW router=%s "),
  226. new_router)
  227. super(AristaL3ServicePlugin, self).delete_router(
  228. context,
  229. new_router['id']
  230. )
  231. @log_helpers.log_method_call
  232. def update_router(self, context, router_id, router):
  233. """Update an existing router in DB, and update it in Arista HW."""
  234. # Read existing router record from DB
  235. original_router = self.get_router(context, router_id)
  236. # Update router DB
  237. new_router = super(AristaL3ServicePlugin, self).update_router(
  238. context, router_id, router)
  239. # Modify router on the Arista Hw
  240. try:
  241. self.driver.update_router(context, router_id,
  242. original_router, new_router)
  243. return new_router
  244. except Exception:
  245. LOG.error(_LE("Error updating router on Arista HW router=%s "),
  246. new_router)
  247. @log_helpers.log_method_call
  248. def delete_router(self, context, router_id):
  249. """Delete an existing router from Arista HW as well as from the DB."""
  250. router = self.get_router(context, router_id)
  251. # Delete router on the Arista Hw
  252. try:
  253. self.driver.delete_router(context, router_id, router)
  254. except Exception as e:
  255. LOG.error(_LE("Error deleting router on Arista HW "
  256. "router %(r)s exception=%(e)s"),
  257. {'r': router, 'e': e})
  258. super(AristaL3ServicePlugin, self).delete_router(context, router_id)
  259. @log_helpers.log_method_call
  260. def add_router_interface(self, context, router_id, interface_info):
  261. """Add a subnet of a network to an existing router."""
  262. new_router = super(AristaL3ServicePlugin, self).add_router_interface(
  263. context, router_id, interface_info)
  264. core = directory.get_plugin()
  265. # Get network info for the subnet that is being added to the router.
  266. # Check if the interface information is by port-id or subnet-id
  267. add_by_port, add_by_sub = self._validate_interface_info(interface_info)
  268. if add_by_sub:
  269. subnet = core.get_subnet(context, interface_info['subnet_id'])
  270. # If we add by subnet and we have no port allocated, assigned
  271. # gateway IP for the interface
  272. fixed_ip = subnet['gateway_ip']
  273. elif add_by_port:
  274. port = core.get_port(context, interface_info['port_id'])
  275. subnet_id = port['fixed_ips'][0]['subnet_id']
  276. fixed_ip = port['fixed_ips'][0]['ip_address']
  277. subnet = core.get_subnet(context, subnet_id)
  278. network_id = subnet['network_id']
  279. # To create SVI's in Arista HW, the segmentation Id is required
  280. # for this network.
  281. ml2_db = NetworkContext(self, context, {'id': network_id})
  282. seg_id = ml2_db.network_segments[0]['segmentation_id']
  283. # Package all the info needed for Hw programming
  284. router = self.get_router(context, router_id)
  285. router_info = copy.deepcopy(new_router)
  286. router_info['seg_id'] = seg_id
  287. router_info['name'] = router['name']
  288. router_info['cidr'] = subnet['cidr']
  289. router_info['gip'] = subnet['gateway_ip']
  290. router_info['fixed_ip'] = fixed_ip
  291. router_info['ip_version'] = subnet['ip_version']
  292. try:
  293. self.driver.add_router_interface(context, router_info)
  294. return new_router
  295. except Exception:
  296. with excutils.save_and_reraise_exception():
  297. LOG.error(_LE("Error Adding subnet %(subnet)s to "
  298. "router %(router_id)s on Arista HW"),
  299. {'subnet': subnet, 'router_id': router_id})
  300. super(AristaL3ServicePlugin, self).remove_router_interface(
  301. context,
  302. router_id,
  303. interface_info)
  304. @log_helpers.log_method_call
  305. def remove_router_interface(self, context, router_id, interface_info):
  306. """Remove a subnet of a network from an existing router."""
  307. router_to_del = (
  308. super(AristaL3ServicePlugin, self).remove_router_interface(
  309. context,
  310. router_id,
  311. interface_info)
  312. )
  313. # Get network information of the subnet that is being removed
  314. core = directory.get_plugin()
  315. subnet = core.get_subnet(context, router_to_del['subnet_id'])
  316. network_id = subnet['network_id']
  317. # For SVI removal from Arista HW, segmentation ID is needed
  318. ml2_db = NetworkContext(self, context, {'id': network_id})
  319. seg_id = ml2_db.network_segments[0]['segmentation_id']
  320. router = self.get_router(context, router_id)
  321. router_info = copy.deepcopy(router_to_del)
  322. router_info['seg_id'] = seg_id
  323. router_info['name'] = router['name']
  324. router_info['ip_version'] = subnet['ip_version']
  325. try:
  326. self.driver.remove_router_interface(context, router_info)
  327. return router_to_del
  328. except Exception as exc:
  329. LOG.error(_LE("Error removing interface %(interface)s from "
  330. "router %(router_id)s on Arista HW"
  331. "Exception =(exc)s"),
  332. {'interface': interface_info, 'router_id': router_id,
  333. 'exc': exc})