PageRenderTime 30ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/neutron/agent/l3/dvr_local_router.py

https://github.com/openstack/neutron
Python | 924 lines | 896 code | 9 blank | 19 comment | 1 complexity | 7019067d9f35d437bec6a1f60f1c5aec MD5 | raw file
  1. # Copyright (c) 2015 OpenStack Foundation
  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 binascii
  15. import collections
  16. import netaddr
  17. from neutron_lib import constants as lib_constants
  18. from oslo_log import log as logging
  19. from oslo_utils import excutils
  20. from pyroute2.netlink import exceptions \
  21. as pyroute2_exc # pylint: disable=no-name-in-module
  22. from neutron.agent.l3 import dvr_fip_ns
  23. from neutron.agent.l3 import dvr_router_base
  24. from neutron.agent.linux import ip_lib
  25. from neutron.common import utils as common_utils
  26. from neutron.privileged.agent.linux import ip_lib as priv_ip_lib
  27. LOG = logging.getLogger(__name__)
  28. # xor-folding mask used for IPv6 rule index
  29. MASK_30 = 0x3fffffff
  30. # Tracks the arp entry cache
  31. Arp_entry = collections.namedtuple(
  32. 'Arp_entry', 'ip mac subnet_id operation')
  33. class DvrLocalRouter(dvr_router_base.DvrRouterBase):
  34. def __init__(self, host, *args, **kwargs):
  35. super(DvrLocalRouter, self).__init__(host, *args, **kwargs)
  36. self.floating_ips_dict = {}
  37. # Linklocal subnet for router and floating IP namespace link
  38. self.rtr_fip_subnet = None
  39. self.rtr_fip_connect = False
  40. self.fip_ns = None
  41. self._pending_arp_set = set()
  42. self._load_used_fip_information()
  43. def _load_used_fip_information(self):
  44. """Load FIP from the FipRulePriorityAllocator state file.
  45. If, for any reason, the FIP is not stored in the state file, this
  46. method reads the namespace "ip rule" list and search for the
  47. corresponding fixed IP of the FIP. If present, this "ip rule" is
  48. (1) deleted, (2) a new rule priority is allocated and (3) the "ip rule"
  49. is written again with the new assigned priority.
  50. At the end of the method, all existing "ip rule" registers in
  51. FIP_RT_TBL table (where FIP rules are stored) that don't match with
  52. any register memoized in self._rule_priorities is deleted.
  53. """
  54. ex_gw_port = self.get_ex_gw_port()
  55. if not ex_gw_port:
  56. return
  57. fip_ns = self.agent.get_fip_ns(ex_gw_port['network_id'])
  58. for fip in self.get_floating_ips():
  59. floating_ip = fip['floating_ip_address']
  60. fixed_ip = fip['fixed_ip_address']
  61. if not fixed_ip:
  62. continue
  63. rule_pr = fip_ns.lookup_rule_priority(floating_ip)
  64. if rule_pr:
  65. self.floating_ips_dict[floating_ip] = (fixed_ip, rule_pr)
  66. continue
  67. rule_pr = fip_ns.allocate_rule_priority(floating_ip)
  68. ip_lib.add_ip_rule(self.ns_name, fixed_ip,
  69. table=dvr_fip_ns.FIP_RT_TBL,
  70. priority=rule_pr)
  71. self.floating_ips_dict[floating_ip] = (fixed_ip, rule_pr)
  72. self._cleanup_unused_fip_ip_rules()
  73. def _cleanup_unused_fip_ip_rules(self):
  74. if not self.router_namespace.exists():
  75. # It could be a new router, thus the namespace is not created yet.
  76. return
  77. ip_rules = ip_lib.list_ip_rules(self.ns_name,
  78. lib_constants.IP_VERSION_4)
  79. ip_rules = [ipr for ipr in ip_rules
  80. if ipr['table'] == dvr_fip_ns.FIP_RT_TBL]
  81. for ip_rule in ip_rules:
  82. for fixed_ip, rule_pr in self.floating_ips_dict.values():
  83. if (ip_rule['from'] == fixed_ip and
  84. ip_rule['priority'] == rule_pr):
  85. break
  86. else:
  87. ip_lib.delete_ip_rule(self.ns_name, ip_rule['from'],
  88. table=dvr_fip_ns.FIP_RT_TBL,
  89. priority=ip_rule['priority'])
  90. def migrate_centralized_floating_ip(self, fip, interface_name, device):
  91. # Remove the centralized fip first and then add fip to the host
  92. ip_cidr = common_utils.ip_to_cidr(fip['floating_ip_address'])
  93. self.floating_ip_removed_dist(ip_cidr)
  94. # Now add the floating_ip to the current host
  95. return self.floating_ip_added_dist(fip, ip_cidr)
  96. def floating_forward_rules(self, fip):
  97. """Override this function defined in router_info for dvr routers."""
  98. if not self.fip_ns:
  99. return []
  100. if fip.get(lib_constants.DVR_SNAT_BOUND):
  101. return []
  102. # For dvr_no_external node should not process any floating IP
  103. # iptables rules.
  104. if (self.agent_conf.agent_mode ==
  105. lib_constants.L3_AGENT_MODE_DVR_NO_EXTERNAL):
  106. return []
  107. fixed_ip = fip['fixed_ip_address']
  108. floating_ip = fip['floating_ip_address']
  109. rtr_2_fip_name = self.fip_ns.get_rtr_ext_device_name(self.router_id)
  110. dnat_from_floatingip_to_fixedip = (
  111. 'PREROUTING', '-d %s/32 -i %s -j DNAT --to-destination %s' % (
  112. floating_ip, rtr_2_fip_name, fixed_ip))
  113. to_source = '-s %s/32 -j SNAT --to-source %s' % (fixed_ip, floating_ip)
  114. if self.iptables_manager.random_fully:
  115. to_source += ' --random-fully'
  116. snat_from_fixedip_to_floatingip = ('float-snat', to_source)
  117. return [dnat_from_floatingip_to_fixedip,
  118. snat_from_fixedip_to_floatingip]
  119. def floating_mangle_rules(self, floating_ip, fixed_ip, internal_mark):
  120. if not self.fip_ns:
  121. return []
  122. rtr_2_fip_name = self.fip_ns.get_rtr_ext_device_name(self.router_id)
  123. mark_traffic_to_floating_ip = (
  124. 'floatingip', '-d %s/32 -i %s -j MARK --set-xmark %s' % (
  125. floating_ip, rtr_2_fip_name, internal_mark))
  126. mark_traffic_from_fixed_ip = (
  127. 'FORWARD', '-s %s/32 -j $float-snat' % fixed_ip)
  128. return [mark_traffic_to_floating_ip, mark_traffic_from_fixed_ip]
  129. def add_centralized_floatingip(self, fip, fip_cidr):
  130. """Implements floatingip in centralized network node.
  131. This is a dummy function and is overridden in dvr_edge_router.py
  132. to add the floatingip function to the snat namespace.
  133. """
  134. def remove_centralized_floatingip(self, fip_cidr):
  135. """Removes floatingip from centralized network node.
  136. This is a dummy function and is overridden in dvr_edge_router.py
  137. to remove the floatingip function from the snat namespace.
  138. """
  139. def floating_ip_added_dist(self, fip, fip_cidr):
  140. """Add floating IP to respective namespace based on agent mode."""
  141. if fip.get(lib_constants.DVR_SNAT_BOUND):
  142. return self.add_centralized_floatingip(fip, fip_cidr)
  143. if not self._check_if_floatingip_bound_to_host(fip):
  144. # TODO(Swami): Need to figure out what status
  145. # should be returned when the floating IP is
  146. # not destined for this agent and if the floating
  147. # IP is configured in a different compute host.
  148. # This should not happen once we fix the server
  149. # side code, but still a check to make sure if
  150. # the floating IP is intended for this host should
  151. # be done.
  152. return
  153. # dvr_no_external host should not process any floating IP route rules.
  154. if (self.agent_conf.agent_mode ==
  155. lib_constants.L3_AGENT_MODE_DVR_NO_EXTERNAL):
  156. return
  157. floating_ip = fip['floating_ip_address']
  158. fixed_ip = fip['fixed_ip_address']
  159. self._add_floating_ip_rule(floating_ip, fixed_ip)
  160. fip_2_rtr_name = self.fip_ns.get_int_device_name(self.router_id)
  161. # Add routing rule in fip namespace
  162. fip_ns_name = self.fip_ns.get_name()
  163. if self.rtr_fip_subnet is None:
  164. self.rtr_fip_subnet = self.fip_ns.local_subnets.allocate(
  165. self.router_id)
  166. rtr_2_fip, __ = self.rtr_fip_subnet.get_pair()
  167. device = ip_lib.IPDevice(fip_2_rtr_name, namespace=fip_ns_name)
  168. device.route.add_route(fip_cidr, str(rtr_2_fip.ip))
  169. interface_name = (
  170. self.fip_ns.get_ext_device_name(
  171. self.fip_ns.agent_gateway_port['id']))
  172. ip_lib.send_ip_addr_adv_notif(fip_ns_name,
  173. interface_name,
  174. floating_ip)
  175. return lib_constants.FLOATINGIP_STATUS_ACTIVE
  176. def _add_floating_ip_rule(self, floating_ip, fixed_ip):
  177. rule_pr = self.fip_ns.allocate_rule_priority(floating_ip)
  178. self.floating_ips_dict[floating_ip] = (fixed_ip, rule_pr)
  179. ip_lib.add_ip_rule(namespace=self.ns_name, ip=fixed_ip,
  180. table=dvr_fip_ns.FIP_RT_TBL,
  181. priority=int(str(rule_pr)))
  182. def _remove_floating_ip_rule(self, floating_ip):
  183. if floating_ip in self.floating_ips_dict:
  184. fixed_ip, rule_pr = self.floating_ips_dict[floating_ip]
  185. ip_lib.delete_ip_rule(self.ns_name, ip=fixed_ip,
  186. table=dvr_fip_ns.FIP_RT_TBL,
  187. priority=int(str(rule_pr)))
  188. self.fip_ns.deallocate_rule_priority(floating_ip)
  189. else:
  190. LOG.error('Floating IP %s not stored in this agent. Because of '
  191. 'the initialization method '
  192. '"_load_used_fip_information", all floating IPs should '
  193. 'be memoized in the local memory.', floating_ip)
  194. def floating_ip_removed_dist(self, fip_cidr):
  195. """Remove floating IP from FIP namespace."""
  196. centralized_fip_cidrs = self.get_centralized_fip_cidr_set()
  197. if fip_cidr in centralized_fip_cidrs:
  198. self.remove_centralized_floatingip(fip_cidr)
  199. return
  200. floating_ip = fip_cidr.split('/')[0]
  201. fip_2_rtr_name = self.fip_ns.get_int_device_name(self.router_id)
  202. if self.rtr_fip_subnet is None:
  203. self.rtr_fip_subnet = self.fip_ns.local_subnets.lookup(
  204. self.router_id)
  205. if self.rtr_fip_subnet:
  206. rtr_2_fip, fip_2_rtr = self.rtr_fip_subnet.get_pair()
  207. fip_ns_name = self.fip_ns.get_name()
  208. self._remove_floating_ip_rule(floating_ip)
  209. device = ip_lib.IPDevice(fip_2_rtr_name, namespace=fip_ns_name)
  210. device.route.delete_route(fip_cidr, via=str(rtr_2_fip.ip))
  211. return device
  212. def floating_ip_moved_dist(self, fip):
  213. """Handle floating IP move between fixed IPs."""
  214. floating_ip = fip['floating_ip_address']
  215. self._remove_floating_ip_rule(floating_ip)
  216. self._add_floating_ip_rule(floating_ip, fip['fixed_ip_address'])
  217. def add_floating_ip(self, fip, interface_name, device):
  218. # Special Handling for DVR - update FIP namespace
  219. ip_cidr = common_utils.ip_to_cidr(fip['floating_ip_address'])
  220. return self.floating_ip_added_dist(fip, ip_cidr)
  221. def remove_floating_ip(self, device, ip_cidr):
  222. fip_2_rtr_device = self.floating_ip_removed_dist(ip_cidr)
  223. if fip_2_rtr_device:
  224. fip_2_rtr_device.delete_conntrack_state(ip_cidr)
  225. def move_floating_ip(self, fip):
  226. self.floating_ip_moved_dist(fip)
  227. return lib_constants.FLOATINGIP_STATUS_ACTIVE
  228. def _get_internal_port(self, subnet_id):
  229. """Return internal router port based on subnet_id."""
  230. router_ports = self.router.get(lib_constants.INTERFACE_KEY, [])
  231. for port in router_ports:
  232. fips = port['fixed_ips']
  233. for f in fips:
  234. if f['subnet_id'] == subnet_id:
  235. return port
  236. def _cache_arp_entry(self, ip, mac, subnet_id, operation):
  237. """Cache the arp entries if device not ready."""
  238. arp_entry_tuple = Arp_entry(ip=ip,
  239. mac=mac,
  240. subnet_id=subnet_id,
  241. operation=operation)
  242. self._pending_arp_set.add(arp_entry_tuple)
  243. def _process_arp_cache_for_internal_port(self, subnet_id):
  244. """Function to process the cached arp entries."""
  245. arp_remove = set()
  246. device, device_exists = self.get_arp_related_dev(subnet_id)
  247. for arp_entry in self._pending_arp_set:
  248. if subnet_id == arp_entry.subnet_id:
  249. try:
  250. state = self._update_arp_entry(
  251. arp_entry.ip, arp_entry.mac,
  252. arp_entry.subnet_id, arp_entry.operation,
  253. device=device,
  254. device_exists=device_exists)
  255. except Exception:
  256. state = False
  257. if state:
  258. # If the arp update was successful, then
  259. # go ahead and add it to the remove set
  260. arp_remove.add(arp_entry)
  261. self._pending_arp_set -= arp_remove
  262. def _delete_arp_cache_for_internal_port(self, subnet_id):
  263. """Function to delete the cached arp entries."""
  264. arp_delete = set()
  265. for arp_entry in self._pending_arp_set:
  266. if subnet_id == arp_entry.subnet_id:
  267. arp_delete.add(arp_entry)
  268. self._pending_arp_set -= arp_delete
  269. def _update_arp_entry(
  270. self, ip, mac, subnet_id, operation, device,
  271. device_exists=True):
  272. """Add or delete arp entry into router namespace for the subnet."""
  273. try:
  274. if device_exists:
  275. if operation == 'add':
  276. device.neigh.add(ip, mac)
  277. elif operation == 'delete':
  278. device.neigh.delete(ip, mac)
  279. return True
  280. else:
  281. if operation == 'add':
  282. LOG.warning("Device %s does not exist so ARP entry "
  283. "cannot be updated, will cache "
  284. "information to be applied later "
  285. "when the device exists",
  286. device)
  287. self._cache_arp_entry(ip, mac, subnet_id, operation)
  288. return False
  289. except Exception:
  290. with excutils.save_and_reraise_exception():
  291. LOG.exception("DVR: Failed updating arp entry")
  292. def get_arp_related_dev(self, subnet_id):
  293. port = self._get_internal_port(subnet_id)
  294. # update arp entry only if the subnet is attached to the router
  295. if not port:
  296. return None, False
  297. interface_name = self.get_internal_device_name(port['id'])
  298. device = ip_lib.IPDevice(interface_name, namespace=self.ns_name)
  299. device_exists = device.exists()
  300. return device, device_exists
  301. def _set_subnet_arp_info(self, subnet):
  302. """Set ARP info retrieved from Plugin for existing ports."""
  303. # TODO(Carl) Can we eliminate the need to make this RPC while
  304. # processing a router.
  305. subnet_ports = self.agent.get_ports_by_subnet(subnet['id'])
  306. ignored_device_owners = (
  307. lib_constants.ROUTER_INTERFACE_OWNERS +
  308. tuple(common_utils.get_dvr_allowed_address_pair_device_owners()))
  309. device, device_exists = self.get_arp_related_dev(subnet['id'])
  310. subnet_ip_version = netaddr.IPNetwork(subnet['cidr']).version
  311. for p in subnet_ports:
  312. if p['device_owner'] not in ignored_device_owners:
  313. for fixed_ip in p['fixed_ips']:
  314. if fixed_ip['subnet_id'] == subnet['id']:
  315. self._update_arp_entry(fixed_ip['ip_address'],
  316. p['mac_address'],
  317. subnet['id'],
  318. 'add',
  319. device=device,
  320. device_exists=device_exists)
  321. for allowed_address_pair in p.get('allowed_address_pairs', []):
  322. if ('/' not in str(allowed_address_pair['ip_address']) or
  323. common_utils.is_cidr_host(
  324. allowed_address_pair['ip_address'])):
  325. ip_address = common_utils.cidr_to_ip(
  326. allowed_address_pair['ip_address'])
  327. ip_version = common_utils.get_ip_version(ip_address)
  328. if ip_version == subnet_ip_version:
  329. self._update_arp_entry(
  330. ip_address,
  331. allowed_address_pair['mac_address'],
  332. subnet['id'],
  333. 'add',
  334. device=device,
  335. device_exists=device_exists)
  336. # subnet_ports does not have snat port if the port is still unbound
  337. # by the time this function is called. So ensure to add arp entry
  338. # for snat port if port details are updated in router info.
  339. for p in self.get_snat_interfaces():
  340. for fixed_ip in p['fixed_ips']:
  341. if fixed_ip['subnet_id'] == subnet['id']:
  342. self._update_arp_entry(fixed_ip['ip_address'],
  343. p['mac_address'],
  344. subnet['id'],
  345. 'add',
  346. device=device,
  347. device_exists=device_exists)
  348. self._process_arp_cache_for_internal_port(subnet['id'])
  349. @staticmethod
  350. def _get_snat_idx(ip_cidr):
  351. """Generate index for DVR snat rules and route tables.
  352. The index value has to be 32 bits or less but more than the system
  353. generated entries i.e. 32768. For IPv4 use the numeric value of the
  354. cidr. For IPv6 generate a crc32 bit hash and xor-fold to 30 bits.
  355. Use the freed range to extend smaller values so that they become
  356. greater than system generated entries.
  357. """
  358. net = netaddr.IPNetwork(ip_cidr)
  359. if net.version == 6:
  360. if isinstance(ip_cidr, str):
  361. ip_cidr = ip_cidr.encode() # Needed for Python 3.x
  362. # the crc32 & 0xffffffff is for Python 2.6 and 3.0 compatibility
  363. snat_idx = binascii.crc32(ip_cidr) & 0xffffffff
  364. # xor-fold the hash to reserve upper range to extend smaller values
  365. snat_idx = (snat_idx >> 30) ^ (snat_idx & MASK_30)
  366. if snat_idx < 32768:
  367. snat_idx = snat_idx + MASK_30
  368. else:
  369. snat_idx = net.value
  370. return snat_idx
  371. def _delete_gateway_device_if_exists(self, ns_ip_device, gw_ip_addr,
  372. snat_idx):
  373. try:
  374. ns_ip_device.route.delete_gateway(gw_ip_addr, table=snat_idx)
  375. except priv_ip_lib.NetworkInterfaceNotFound:
  376. pass
  377. def _stale_ip_rule_cleanup(self, namespace, ns_ipd, ip_version):
  378. ip_rules_list = ip_lib.list_ip_rules(namespace, ip_version)
  379. snat_table_list = []
  380. for ip_rule in ip_rules_list:
  381. snat_table = ip_rule['table']
  382. priority = ip_rule['priority']
  383. if snat_table in ['local', 'default', 'main']:
  384. continue
  385. if (ip_version == lib_constants.IP_VERSION_4 and
  386. snat_table in range(dvr_fip_ns.FIP_PR_START,
  387. dvr_fip_ns.FIP_PR_END)):
  388. continue
  389. gateway_cidr = ip_rule['from']
  390. ip_lib.delete_ip_rule(namespace, ip=gateway_cidr, table=snat_table,
  391. priority=priority)
  392. snat_table_list.append(snat_table)
  393. for tb in snat_table_list:
  394. ns_ipd.route.flush(ip_version, table=tb)
  395. def gateway_redirect_cleanup(self, rtr_interface):
  396. ns_ipd = ip_lib.IPDevice(rtr_interface, namespace=self.ns_name)
  397. self._stale_ip_rule_cleanup(self.ns_name, ns_ipd,
  398. lib_constants.IP_VERSION_4)
  399. self._stale_ip_rule_cleanup(self.ns_name, ns_ipd,
  400. lib_constants.IP_VERSION_6)
  401. def _snat_redirect_modify(self, gateway, sn_port, sn_int, is_add):
  402. """Adds or removes rules and routes for SNAT redirection."""
  403. cmd = ['net.ipv4.conf.%s.send_redirects=0' % sn_int]
  404. try:
  405. ns_ipd = ip_lib.IPDevice(sn_int, namespace=self.ns_name)
  406. for port_fixed_ip in sn_port['fixed_ips']:
  407. # Iterate and find the gateway IP address matching
  408. # the IP version
  409. port_ip_addr = port_fixed_ip['ip_address']
  410. port_ip_vers = netaddr.IPAddress(port_ip_addr).version
  411. for gw_fixed_ip in gateway['fixed_ips']:
  412. gw_ip_addr = gw_fixed_ip['ip_address']
  413. if netaddr.IPAddress(gw_ip_addr).version == port_ip_vers:
  414. sn_port_cidr = common_utils.ip_to_cidr(
  415. port_ip_addr, port_fixed_ip['prefixlen'])
  416. snat_idx = self._get_snat_idx(sn_port_cidr)
  417. if is_add:
  418. ns_ipd.route.add_gateway(gw_ip_addr,
  419. table=snat_idx)
  420. ip_lib.add_ip_rule(namespace=self.ns_name,
  421. ip=sn_port_cidr,
  422. table=snat_idx,
  423. priority=snat_idx)
  424. ip_lib.sysctl(cmd, namespace=self.ns_name)
  425. else:
  426. self._delete_gateway_device_if_exists(ns_ipd,
  427. gw_ip_addr,
  428. snat_idx)
  429. ip_lib.delete_ip_rule(self.ns_name,
  430. ip=sn_port_cidr,
  431. table=snat_idx,
  432. priority=snat_idx)
  433. except Exception:
  434. if is_add:
  435. exc = 'DVR: error adding redirection logic'
  436. else:
  437. exc = ('DVR: snat remove failed to clear the rule '
  438. 'and device')
  439. LOG.exception(exc)
  440. def _snat_redirect_add(self, gateway, sn_port, sn_int):
  441. """Adds rules and routes for SNAT redirection."""
  442. self._snat_redirect_modify(gateway, sn_port, sn_int, is_add=True)
  443. def _snat_redirect_remove(self, gateway, sn_port, sn_int):
  444. """Removes rules and routes for SNAT redirection."""
  445. self._snat_redirect_modify(gateway, sn_port, sn_int, is_add=False)
  446. def internal_network_added(self, port):
  447. super(DvrLocalRouter, self).internal_network_added(port)
  448. # NOTE: The following function _set_subnet_arp_info
  449. # should be called to dynamically populate the arp
  450. # entries for the dvr services ports into the router
  451. # namespace. This does not have dependency on the
  452. # external_gateway port or the agent_mode.
  453. ex_gw_port = self.get_ex_gw_port()
  454. for subnet in port['subnets']:
  455. self._set_subnet_arp_info(subnet)
  456. if ex_gw_port:
  457. # Check for address_scopes here if gateway exists.
  458. address_scopes_match = self._check_if_address_scopes_match(
  459. port, ex_gw_port)
  460. if (address_scopes_match and
  461. (self.agent_conf.agent_mode in
  462. [lib_constants.L3_AGENT_MODE_DVR,
  463. lib_constants.L3_AGENT_MODE_DVR_SNAT])):
  464. self._add_interface_routing_rule_to_router_ns(port)
  465. self._add_interface_route_to_fip_ns(port)
  466. self._snat_redirect_add_from_port(port)
  467. def _snat_redirect_add_from_port(self, port):
  468. ex_gw_port = self.get_ex_gw_port()
  469. if not ex_gw_port:
  470. return
  471. address_scopes_match = self._check_if_address_scopes_match(
  472. port, ex_gw_port)
  473. if (address_scopes_match and
  474. (self.agent_conf.agent_mode in
  475. [lib_constants.L3_AGENT_MODE_DVR,
  476. lib_constants.L3_AGENT_MODE_DVR_SNAT])):
  477. return
  478. sn_port = self.get_snat_port_for_internal_port(port)
  479. if not sn_port:
  480. return
  481. interface_name = self.get_internal_device_name(port['id'])
  482. self._snat_redirect_add(sn_port, port, interface_name)
  483. def _dvr_internal_network_removed(self, port):
  484. # Clean up the cached arp entries related to the port subnet
  485. for subnet in port['subnets']:
  486. self._delete_arp_cache_for_internal_port(subnet)
  487. if not self.ex_gw_port:
  488. return
  489. # Delete DVR address_scope static route for the removed interface
  490. # Check for address_scopes here.
  491. address_scopes_match = self._check_if_address_scopes_match(
  492. port, self.ex_gw_port)
  493. if (address_scopes_match and
  494. (self.agent_conf.agent_mode in
  495. [lib_constants.L3_AGENT_MODE_DVR,
  496. lib_constants.L3_AGENT_MODE_DVR_SNAT])):
  497. self._delete_interface_route_in_fip_ns(port)
  498. self._delete_interface_routing_rule_in_router_ns(port)
  499. # If address scopes match there is no need to cleanup the
  500. # snat redirect rules, hence return here.
  501. return
  502. sn_port = self.get_snat_port_for_internal_port(port, self.snat_ports)
  503. if not sn_port:
  504. return
  505. # DVR handling code for SNAT
  506. interface_name = self.get_internal_device_name(port['id'])
  507. self._snat_redirect_remove(sn_port, port, interface_name)
  508. def internal_network_removed(self, port):
  509. self._dvr_internal_network_removed(port)
  510. super(DvrLocalRouter, self).internal_network_removed(port)
  511. def get_floating_agent_gw_interface(self, ext_net_id):
  512. """Filter Floating Agent GW port for the external network."""
  513. fip_ports = self.router.get(
  514. lib_constants.FLOATINGIP_AGENT_INTF_KEY, [])
  515. return next(
  516. (p for p in fip_ports if p['network_id'] == ext_net_id), None)
  517. def get_snat_external_device_interface_name(self, port_id):
  518. pass
  519. def get_external_device_interface_name(self, ex_gw_port):
  520. fip_int = self.fip_ns.get_int_device_name(self.router_id)
  521. if ip_lib.device_exists(fip_int, namespace=self.fip_ns.get_name()):
  522. return self.fip_ns.get_rtr_ext_device_name(self.router_id)
  523. def enable_snat_redirect_rules(self, ex_gw_port):
  524. for p in self.internal_ports:
  525. gateway = self.get_snat_port_for_internal_port(p)
  526. if not gateway:
  527. continue
  528. address_scopes_match = self._check_if_address_scopes_match(
  529. p, ex_gw_port)
  530. if (not address_scopes_match or
  531. (self.agent_conf.agent_mode ==
  532. lib_constants.L3_AGENT_MODE_DVR_NO_EXTERNAL)):
  533. internal_dev = self.get_internal_device_name(p['id'])
  534. self._snat_redirect_add(gateway, p, internal_dev)
  535. def disable_snat_redirect_rules(self, ex_gw_port):
  536. for p in self.internal_ports:
  537. gateway = self.get_snat_port_for_internal_port(
  538. p, self.snat_ports)
  539. if not gateway:
  540. continue
  541. address_scopes_match = self._check_if_address_scopes_match(
  542. p, ex_gw_port)
  543. if (not address_scopes_match or
  544. (self.agent_conf.agent_mode ==
  545. lib_constants.L3_AGENT_MODE_DVR_NO_EXTERNAL)):
  546. internal_dev = self.get_internal_device_name(p['id'])
  547. self._snat_redirect_remove(gateway, p, internal_dev)
  548. def external_gateway_added(self, ex_gw_port, interface_name):
  549. # TODO(Carl) Refactor external_gateway_added/updated/removed to use
  550. # super class implementation where possible. Looks like preserve_ips,
  551. # and ns_name are the key differences.
  552. cmd = ['net.ipv4.conf.all.send_redirects=0']
  553. ip_lib.sysctl(cmd, namespace=self.ns_name)
  554. self.enable_snat_redirect_rules(ex_gw_port)
  555. for port in self.get_snat_interfaces():
  556. for ip in port['fixed_ips']:
  557. subnet_id = ip['subnet_id']
  558. device, device_exists = self.get_arp_related_dev(subnet_id)
  559. self._update_arp_entry(ip['ip_address'],
  560. port['mac_address'],
  561. subnet_id,
  562. 'add',
  563. device=device,
  564. device_exists=device_exists)
  565. def external_gateway_updated(self, ex_gw_port, interface_name):
  566. pass
  567. def process_floating_ip_nat_rules(self):
  568. """Configure NAT rules for the router's floating IPs.
  569. Configures iptables rules for the floating ips of the given router
  570. """
  571. # Clear out all iptables rules for floating ips
  572. self.iptables_manager.ipv4['nat'].clear_rules_by_tag('floating_ip')
  573. floating_ips = self.get_floating_ips()
  574. # Loop once to ensure that floating ips are configured.
  575. for fip in floating_ips:
  576. # If floating IP is snat_bound, then the iptables rule should
  577. # not be installed to qrouter namespace, since the mixed snat
  578. # namespace may already install it.
  579. if fip.get(lib_constants.DVR_SNAT_BOUND):
  580. continue
  581. # Rebuild iptables rules for the floating ip.
  582. for chain, rule in self.floating_forward_rules(fip):
  583. self.iptables_manager.ipv4['nat'].add_rule(
  584. chain, rule, tag='floating_ip')
  585. self.iptables_manager.apply()
  586. def external_gateway_removed(self, ex_gw_port, interface_name):
  587. # TODO(Carl) Should this be calling process_snat_dnat_for_fip?
  588. self.process_floating_ip_nat_rules()
  589. if self.fip_ns:
  590. to_fip_interface_name = (
  591. self.get_external_device_interface_name(ex_gw_port))
  592. self.process_floating_ip_addresses(to_fip_interface_name)
  593. # Remove the router to fip namespace connection after the
  594. # gateway is removed.
  595. self.fip_ns.delete_rtr_2_fip_link(self)
  596. self.rtr_fip_connect = False
  597. # NOTE:_snat_redirect_remove should be only called when the
  598. # gateway is cleared and should not be called when the gateway
  599. # is moved or rescheduled.
  600. if not self.router.get('gw_port'):
  601. self.disable_snat_redirect_rules(ex_gw_port)
  602. def _handle_router_snat_rules(self, ex_gw_port, interface_name):
  603. """Configures NAT rules for Floating IPs for DVR."""
  604. self.iptables_manager.ipv4['nat'].empty_chain('POSTROUTING')
  605. self.iptables_manager.ipv4['nat'].empty_chain('snat')
  606. ex_gw_port = self.get_ex_gw_port()
  607. if not ex_gw_port:
  608. return
  609. ext_device_name = self.get_external_device_interface_name(ex_gw_port)
  610. floatingips = self.get_floating_ips()
  611. if not ext_device_name or not floatingips:
  612. # Without router to fip device, or without any floating ip,
  613. # the snat rules should not be added
  614. return
  615. # Add back the jump to float-snat
  616. self.iptables_manager.ipv4['nat'].add_rule('snat', '-j $float-snat')
  617. rule = self._prevent_snat_for_internal_traffic_rule(ext_device_name)
  618. self.iptables_manager.ipv4['nat'].add_rule(*rule)
  619. def _get_address_scope_mark(self):
  620. # Prepare address scope iptables rule for internal ports
  621. internal_ports = self.router.get(lib_constants.INTERFACE_KEY, [])
  622. ports_scopemark = self._get_port_devicename_scopemark(
  623. internal_ports, self.get_internal_device_name)
  624. # DVR local router will use rfp port as external port
  625. ext_port = self.get_ex_gw_port()
  626. if not ext_port:
  627. return ports_scopemark
  628. ext_device_name = self.get_external_device_interface_name(ext_port)
  629. if not ext_device_name:
  630. return ports_scopemark
  631. ext_scope_mark = self._get_port_devicename_scopemark(
  632. [ext_port], self.get_internal_device_name,
  633. interface_name=ext_device_name)
  634. for ip_version in (lib_constants.IP_VERSION_4,
  635. lib_constants.IP_VERSION_6):
  636. ports_scopemark[ip_version].update(
  637. ext_scope_mark[ip_version])
  638. return ports_scopemark
  639. def _check_if_floatingip_bound_to_host(self, fip):
  640. """Check if the floating IP is bound to this host."""
  641. return self.host in (fip.get('host'), fip.get('dest_host'))
  642. def process_external(self):
  643. if self.agent_conf.agent_mode != (
  644. lib_constants.L3_AGENT_MODE_DVR_NO_EXTERNAL):
  645. ex_gw_port = self.get_ex_gw_port()
  646. if ex_gw_port:
  647. self.create_dvr_external_gateway_on_agent(ex_gw_port)
  648. self.connect_rtr_2_fip()
  649. super(DvrLocalRouter, self).process_external()
  650. def _check_rtr_2_fip_connect(self):
  651. """Checks if the rtr to fip connect exists, if not sets to false."""
  652. fip_ns_name = self.fip_ns.get_name()
  653. if ip_lib.network_namespace_exists(fip_ns_name):
  654. fip_2_rtr_name = self.fip_ns.get_int_device_name(self.router_id)
  655. if not ip_lib.device_exists(fip_2_rtr_name, namespace=fip_ns_name):
  656. self.rtr_fip_connect = False
  657. def connect_rtr_2_fip(self):
  658. self._check_rtr_2_fip_connect()
  659. if self.fip_ns.agent_gateway_port and not self.rtr_fip_connect:
  660. ex_gw_port = self.get_ex_gw_port()
  661. self.fip_ns.create_rtr_2_fip_link(self)
  662. self.set_address_scope_interface_routes(ex_gw_port)
  663. self.rtr_fip_connect = True
  664. self.routes_updated([], self.router['routes'])
  665. def _check_if_address_scopes_match(self, int_port, ex_gw_port):
  666. """Checks and returns the matching state for v4 or v6 scopes."""
  667. int_port_addr_scopes = int_port.get('address_scopes', {})
  668. ext_port_addr_scopes = ex_gw_port.get('address_scopes', {})
  669. key = (
  670. lib_constants.IP_VERSION_6 if self._port_has_ipv6_subnet(int_port)
  671. else lib_constants.IP_VERSION_4)
  672. # NOTE: DVR does not support IPv6 for the floating namespace yet, so
  673. # until we fix it, we probably should use the snat redirect path for
  674. # the ports that have IPv6 address configured.
  675. int_port_addr_value = int_port_addr_scopes.get(str(key))
  676. # If the address scope of the interface is none, then don't need
  677. # to compare and just return.
  678. if int_port_addr_value is None:
  679. return False
  680. if ((key != lib_constants.IP_VERSION_6) and
  681. int_port_addr_scopes.get(str(key)) in
  682. ext_port_addr_scopes.values()):
  683. return True
  684. return False
  685. def _delete_interface_route_in_fip_ns(self, router_port):
  686. rtr_2_fip_ip, fip_2_rtr_name = self.get_rtr_fip_ip_and_interface_name()
  687. fip_ns_name = self.fip_ns.get_name()
  688. if ip_lib.network_namespace_exists(fip_ns_name):
  689. device = ip_lib.IPDevice(fip_2_rtr_name, namespace=fip_ns_name)
  690. if not device.exists():
  691. return
  692. for subnet in router_port['subnets']:
  693. rtr_port_cidr = subnet['cidr']
  694. device.route.delete_route(rtr_port_cidr, via=str(rtr_2_fip_ip))
  695. def _add_interface_route_to_fip_ns(self, router_port):
  696. rtr_2_fip_ip, fip_2_rtr_name = self.get_rtr_fip_ip_and_interface_name()
  697. fip_ns_name = self.fip_ns.get_name()
  698. if ip_lib.network_namespace_exists(fip_ns_name):
  699. device = ip_lib.IPDevice(fip_2_rtr_name, namespace=fip_ns_name)
  700. if not device.exists():
  701. return
  702. for subnet in router_port['subnets']:
  703. rtr_port_cidr = subnet['cidr']
  704. device.route.add_route(rtr_port_cidr, str(rtr_2_fip_ip))
  705. def _add_interface_routing_rule_to_router_ns(self, router_port):
  706. for subnet in router_port['subnets']:
  707. rtr_port_cidr = subnet['cidr']
  708. ip_lib.add_ip_rule(namespace=self.ns_name, ip=rtr_port_cidr,
  709. table=dvr_fip_ns.FIP_RT_TBL,
  710. priority=dvr_fip_ns.FAST_PATH_EXIT_PR)
  711. def _delete_interface_routing_rule_in_router_ns(self, router_port):
  712. for subnet in router_port['subnets']:
  713. rtr_port_cidr = subnet['cidr']
  714. ip_lib.delete_ip_rule(self.ns_name, ip=rtr_port_cidr,
  715. table=dvr_fip_ns.FIP_RT_TBL,
  716. priority=dvr_fip_ns.FAST_PATH_EXIT_PR)
  717. def get_rtr_fip_ip_and_interface_name(self):
  718. """Function that returns the router to fip interface name and ip."""
  719. if self.rtr_fip_subnet is None:
  720. self.rtr_fip_subnet = self.fip_ns.local_subnets.allocate(
  721. self.router_id)
  722. rtr_2_fip, __ = self.rtr_fip_subnet.get_pair()
  723. fip_2_rtr_name = self.fip_ns.get_int_device_name(self.router_id)
  724. return rtr_2_fip.ip, fip_2_rtr_name
  725. def set_address_scope_interface_routes(self, ex_gw_port):
  726. """Sets routing rules for router interfaces if addr scopes match."""
  727. for port in self.internal_ports:
  728. if self._check_if_address_scopes_match(port, ex_gw_port):
  729. self._add_interface_routing_rule_to_router_ns(port)
  730. self._add_interface_route_to_fip_ns(port)
  731. def create_dvr_external_gateway_on_agent(self, ex_gw_port):
  732. fip_agent_port = self.get_floating_agent_gw_interface(
  733. ex_gw_port['network_id'])
  734. if not fip_agent_port:
  735. fip_agent_port = self.agent.plugin_rpc.get_agent_gateway_port(
  736. self.agent.context, ex_gw_port['network_id'])
  737. LOG.debug("FloatingIP agent gateway port received from the "
  738. "plugin: %s", fip_agent_port)
  739. self.fip_ns.create_or_update_gateway_port(fip_agent_port)
  740. def update_routing_table(self, operation, route):
  741. # TODO(Swami): The static routes should be added to the
  742. # specific namespace based on the availability of the
  743. # network interfaces. In the case of DVR the static routes
  744. # for local internal router networks can be added to the
  745. # snat_namespace and router_namespace but should not be
  746. # added to the fip namespace. Likewise the static routes
  747. # for the external router networks should only be added to
  748. # the snat_namespace and fip_namespace.
  749. # The current code adds static routes to all namespaces in
  750. # order to reduce the complexity. This should be revisited
  751. # later.
  752. if self.fip_ns and self.fip_ns.agent_gateway_port:
  753. fip_ns_name = self.fip_ns.get_name()
  754. agent_gw_port = self.fip_ns.agent_gateway_port
  755. route_apply = self._check_if_route_applicable_to_fip_namespace(
  756. route, agent_gw_port)
  757. if route_apply:
  758. if self.rtr_fip_subnet is None:
  759. self.rtr_fip_subnet = self.fip_ns.local_subnets.allocate(
  760. self.router_id)
  761. rtr_2_fip, fip_2_rtr = self.rtr_fip_subnet.get_pair()
  762. tbl_index = self._get_snat_idx(fip_2_rtr)
  763. self._update_fip_route_table_with_next_hop_routes(
  764. operation, route, fip_ns_name, tbl_index)
  765. super(DvrLocalRouter, self).update_routing_table(operation, route)
  766. def _update_fip_route_table_with_next_hop_routes(self, operation, route,
  767. fip_ns_name, tbl_index):
  768. cmd = (ip_lib.add_ip_route if operation == 'replace' else
  769. ip_lib.delete_ip_route)
  770. try:
  771. cmd(fip_ns_name, route['destination'], via=route['nexthop'],
  772. table=tbl_index, proto='boot')
  773. except priv_ip_lib.NetworkNamespaceNotFound:
  774. LOG.debug("The FIP namespace %(ns)s does not exist for "
  775. "router %(id)s",
  776. {'ns': fip_ns_name, 'id': self.router_id})
  777. except (OSError, pyroute2_exc.NetlinkError):
  778. pass
  779. def _check_if_route_applicable_to_fip_namespace(self, route,
  780. agent_gateway_port):
  781. ip_cidrs = common_utils.fixed_ip_cidrs(agent_gateway_port['fixed_ips'])
  782. nexthop_cidr = netaddr.IPAddress(route['nexthop'])
  783. for gw_cidr in ip_cidrs:
  784. gw_subnet_cidr = netaddr.IPNetwork(gw_cidr)
  785. # NOTE: In the case of DVR routers apply the extra routes
  786. # on the FIP namespace only if it is associated with the
  787. # external agent gateway subnets.
  788. if nexthop_cidr in gw_subnet_cidr:
  789. return True
  790. return False
  791. def get_router_cidrs(self, device):
  792. """As no floatingip will be set on the rfp device. Get floatingip from
  793. the route of fip namespace.
  794. """
  795. if not self.fip_ns:
  796. return set()
  797. fip_ns_name = self.fip_ns.get_name()
  798. fip_2_rtr_name = self.fip_ns.get_int_device_name(self.router_id)
  799. device = ip_lib.IPDevice(fip_2_rtr_name, namespace=fip_ns_name)
  800. if not device.exists():
  801. return set()
  802. if self.rtr_fip_subnet is None:
  803. self.rtr_fip_subnet = self.fip_ns.local_subnets.allocate(
  804. self.router_id)
  805. rtr_2_fip, _fip_2_rtr = self.rtr_fip_subnet.get_pair()
  806. exist_routes = device.route.list_routes(
  807. lib_constants.IP_VERSION_4, via=str(rtr_2_fip.ip))
  808. return {common_utils.ip_to_cidr(route['cidr'])
  809. for route in exist_routes}
  810. def process(self):
  811. ex_gw_port = self.get_ex_gw_port()
  812. if ex_gw_port:
  813. self.fip_ns = self.agent.get_fip_ns(ex_gw_port['network_id'])
  814. self.fip_ns.scan_fip_ports(self)
  815. super(DvrLocalRouter, self).process()