OpenStack Networking (Neutron)
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.

iptables_driver.py 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. # Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
  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. from oslo_config import cfg
  15. from oslo_log import helpers as log_helpers
  16. from oslo_log import log as logging
  17. from neutron.agent.common import utils as common_utils
  18. from neutron.agent.l3 import dvr_snat_ns
  19. from neutron.agent.l3 import namespaces
  20. from neutron.agent.linux import ip_lib
  21. from neutron.agent.linux import iptables_manager
  22. from neutron.common import constants
  23. from neutron.common import ipv6_utils
  24. from neutron.conf.agent import common as config
  25. from neutron.services.metering.drivers import abstract_driver
  26. LOG = logging.getLogger(__name__)
  27. NS_PREFIX = 'qrouter-'
  28. WRAP_NAME = 'neutron-meter'
  29. EXTERNAL_DEV_PREFIX = 'qg-'
  30. ROUTER_2_FIP_DEV_PREFIX = namespaces.ROUTER_2_FIP_DEV_PREFIX
  31. TOP_CHAIN = WRAP_NAME + "-FORWARD"
  32. RULE = '-r-'
  33. LABEL = '-l-'
  34. config.register_interface_driver_opts_helper(cfg.CONF)
  35. config.register_interface_opts()
  36. class IptablesManagerTransaction(object):
  37. __transactions = {}
  38. def __init__(self, im):
  39. self.im = im
  40. transaction = self.__transactions.get(im, 0)
  41. transaction += 1
  42. self.__transactions[im] = transaction
  43. def __enter__(self):
  44. return self.im
  45. def __exit__(self, type, value, traceback):
  46. transaction = self.__transactions.get(self.im)
  47. if transaction == 1:
  48. self.im.apply()
  49. del self.__transactions[self.im]
  50. else:
  51. transaction -= 1
  52. self.__transactions[self.im] = transaction
  53. class RouterWithMetering(object):
  54. def __init__(self, conf, router):
  55. self.conf = conf
  56. self.id = router['id']
  57. self.router = router
  58. # TODO(cbrandily): deduplicate ns_name generation in metering/l3
  59. self.ns_name = NS_PREFIX + self.id
  60. self.iptables_manager = None
  61. self.snat_iptables_manager = None
  62. if self.router['distributed']:
  63. # If distributed routers then we need to apply the
  64. # metering agent label rules in the snat namespace as well.
  65. snat_ns_name = dvr_snat_ns.SnatNamespace.get_snat_ns_name(
  66. self.id)
  67. # Check for namespace existence before we assign the
  68. # snat_iptables_manager
  69. if ip_lib.network_namespace_exists(snat_ns_name):
  70. self.snat_iptables_manager = iptables_manager.IptablesManager(
  71. namespace=snat_ns_name,
  72. binary_name=WRAP_NAME,
  73. state_less=True,
  74. use_ipv6=ipv6_utils.is_enabled_and_bind_by_default())
  75. # Check of namespace existence before we assign the iptables_manager
  76. # NOTE(Swami): If distributed routers, all external traffic on a
  77. # compute node will flow through the rfp interface in the router
  78. # namespace.
  79. if ip_lib.network_namespace_exists(self.ns_name):
  80. self.iptables_manager = iptables_manager.IptablesManager(
  81. namespace=self.ns_name,
  82. binary_name=WRAP_NAME,
  83. state_less=True,
  84. use_ipv6=ipv6_utils.is_enabled_and_bind_by_default())
  85. self.metering_labels = {}
  86. class IptablesMeteringDriver(abstract_driver.MeteringAbstractDriver):
  87. def __init__(self, plugin, conf):
  88. self.plugin = plugin
  89. self.conf = conf or cfg.CONF
  90. self.routers = {}
  91. self.driver = common_utils.load_interface_driver(self.conf)
  92. def _update_router(self, router):
  93. r = self.routers.get(router['id'],
  94. RouterWithMetering(self.conf, router))
  95. r.router = router
  96. self.routers[r.id] = r
  97. return r
  98. @log_helpers.log_method_call
  99. def update_routers(self, context, routers):
  100. # disassociate removed routers
  101. router_ids = set(router['id'] for router in routers)
  102. for router_id, rm in self.routers.items():
  103. if router_id not in router_ids:
  104. self._process_disassociate_metering_label(rm.router)
  105. for router in routers:
  106. old_gw_port_id = None
  107. old_rm = self.routers.get(router['id'])
  108. if old_rm:
  109. old_gw_port_id = old_rm.router['gw_port_id']
  110. gw_port_id = router['gw_port_id']
  111. if gw_port_id != old_gw_port_id:
  112. if old_rm:
  113. if router.get('distributed'):
  114. old_rm_im = old_rm.snat_iptables_manager
  115. else:
  116. old_rm_im = old_rm.iptables_manager
  117. with IptablesManagerTransaction(old_rm_im):
  118. self._process_disassociate_metering_label(router)
  119. if gw_port_id:
  120. self._process_associate_metering_label(router)
  121. elif gw_port_id:
  122. self._process_associate_metering_label(router)
  123. @log_helpers.log_method_call
  124. def remove_router(self, context, router_id):
  125. if router_id in self.routers:
  126. del self.routers[router_id]
  127. def get_external_device_names(self, rm):
  128. gw_port_id = rm.router.get('gw_port_id')
  129. if not gw_port_id:
  130. return None, None
  131. # NOTE (Swami): External device 'qg' should be used on the
  132. # Router namespace if the router is legacy and should be used on
  133. # SNAT namespace if the router is distributed.
  134. ext_dev = (EXTERNAL_DEV_PREFIX +
  135. gw_port_id)[:self.driver.DEV_NAME_LEN]
  136. ext_snat_dev = (ROUTER_2_FIP_DEV_PREFIX +
  137. rm.id)[:self.driver.DEV_NAME_LEN]
  138. return ext_dev, ext_snat_dev
  139. def _process_metering_label_rules(self, rules, label_chain,
  140. rules_chain, ext_dev, im):
  141. if not ext_dev:
  142. return
  143. for rule in rules:
  144. self._add_rule_to_chain(ext_dev, rule, im,
  145. label_chain, rules_chain)
  146. def _process_metering_label_rule_add(self, rule, ext_dev,
  147. label_chain, rules_chain, im):
  148. self._add_rule_to_chain(ext_dev, rule, im, label_chain, rules_chain)
  149. def _process_metering_label_rule_delete(self, rule, ext_dev,
  150. label_chain, rules_chain, im):
  151. self._remove_rule_from_chain(ext_dev, rule, im,
  152. label_chain, rules_chain)
  153. def _add_rule_to_chain(self, ext_dev, rule, im,
  154. label_chain, rules_chain):
  155. ipt_rule = self._prepare_rule(ext_dev, rule, label_chain)
  156. if rule['excluded']:
  157. im.ipv4['filter'].add_rule(rules_chain, ipt_rule,
  158. wrap=False, top=True)
  159. else:
  160. im.ipv4['filter'].add_rule(rules_chain, ipt_rule,
  161. wrap=False, top=False)
  162. def _remove_rule_from_chain(self, ext_dev, rule, im,
  163. label_chain, rules_chain):
  164. ipt_rule = self._prepare_rule(ext_dev, rule, label_chain)
  165. if rule['excluded']:
  166. im.ipv4['filter'].remove_rule(rules_chain, ipt_rule,
  167. wrap=False, top=True)
  168. else:
  169. im.ipv4['filter'].remove_rule(rules_chain, ipt_rule,
  170. wrap=False, top=False)
  171. def _prepare_rule(self, ext_dev, rule, label_chain):
  172. remote_ip = rule['remote_ip_prefix']
  173. if rule['direction'] == 'egress':
  174. dir_opt = '-s %s -o %s' % (remote_ip, ext_dev)
  175. else:
  176. dir_opt = '-d %s -i %s' % (remote_ip, ext_dev)
  177. if rule['excluded']:
  178. ipt_rule = '%s -j RETURN' % dir_opt
  179. else:
  180. ipt_rule = '%s -j %s' % (dir_opt, label_chain)
  181. return ipt_rule
  182. def _process_ns_specific_metering_label(self, router, ext_dev, im):
  183. '''Process metering label based on the associated namespaces.'''
  184. rm = self.routers.get(router['id'])
  185. with IptablesManagerTransaction(im):
  186. labels = router.get(constants.METERING_LABEL_KEY, [])
  187. for label in labels:
  188. label_id = label['id']
  189. label_chain = iptables_manager.get_chain_name(
  190. WRAP_NAME + LABEL + label_id, wrap=False)
  191. rules_chain = iptables_manager.get_chain_name(
  192. WRAP_NAME + RULE + label_id, wrap=False)
  193. exists = rm.metering_labels.get(label_id)
  194. if not exists:
  195. self._create_metering_label_chain(rm,
  196. label_chain,
  197. rules_chain)
  198. rm.metering_labels[label_id] = label
  199. rules = label.get('rules')
  200. if rules:
  201. self._process_metering_label_rules(
  202. rules, label_chain, rules_chain, ext_dev, im)
  203. def _process_associate_metering_label(self, router):
  204. self._update_router(router)
  205. rm = self.routers.get(router['id'])
  206. ext_dev, ext_snat_dev = self.get_external_device_names(rm)
  207. for (im, dev) in [(rm.iptables_manager, ext_dev),
  208. (rm.snat_iptables_manager, ext_snat_dev)]:
  209. if im:
  210. self._process_ns_specific_metering_label(router, dev, im)
  211. def _process_ns_specific_disassociate_metering_label(self, router, im):
  212. '''Disassociate metering label based on specific namespaces.'''
  213. rm = self.routers.get(router['id'])
  214. with IptablesManagerTransaction(im):
  215. labels = router.get(constants.METERING_LABEL_KEY, [])
  216. for label in labels:
  217. label_id = label['id']
  218. if label_id not in rm.metering_labels:
  219. continue
  220. label_chain = iptables_manager.get_chain_name(
  221. WRAP_NAME + LABEL + label_id, wrap=False)
  222. rules_chain = iptables_manager.get_chain_name(
  223. WRAP_NAME + RULE + label_id, wrap=False)
  224. im.ipv4['filter'].remove_chain(label_chain, wrap=False)
  225. im.ipv4['filter'].remove_chain(rules_chain, wrap=False)
  226. def _process_disassociate_metering_label(self, router):
  227. rm = self.routers.get(router['id'])
  228. if not rm:
  229. return
  230. for im in [rm.iptables_manager, rm.snat_iptables_manager]:
  231. if im:
  232. self._process_ns_specific_disassociate_metering_label(
  233. router, im)
  234. labels = router.get(constants.METERING_LABEL_KEY, [])
  235. for label in labels:
  236. label_id = label['id']
  237. del rm.metering_labels[label_id]
  238. @log_helpers.log_method_call
  239. def add_metering_label(self, context, routers):
  240. for router in routers:
  241. self._process_associate_metering_label(router)
  242. @log_helpers.log_method_call
  243. def add_metering_label_rule(self, context, routers):
  244. for router in routers:
  245. self._add_metering_label_rule(router)
  246. @log_helpers.log_method_call
  247. def remove_metering_label_rule(self, context, routers):
  248. for router in routers:
  249. self._remove_metering_label_rule(router)
  250. @log_helpers.log_method_call
  251. def update_metering_label_rules(self, context, routers):
  252. for router in routers:
  253. self._update_metering_label_rules(router)
  254. def _add_metering_label_rule(self, router):
  255. self._process_metering_rule_action(router, 'create')
  256. def _remove_metering_label_rule(self, router):
  257. self._process_metering_rule_action(router, 'delete')
  258. def _create_metering_label_chain(self, rm, label_chain, rules_chain):
  259. rm.iptables_manager.ipv4['filter'].add_chain(label_chain, wrap=False)
  260. rm.iptables_manager.ipv4['filter'].add_chain(rules_chain, wrap=False)
  261. rm.iptables_manager.ipv4['filter'].add_rule(
  262. TOP_CHAIN, '-j ' + rules_chain, wrap=False)
  263. rm.iptables_manager.ipv4['filter'].add_rule(
  264. label_chain, '', wrap=False)
  265. def _process_metering_rule_action_based_on_ns(
  266. self, router, action, ext_dev, im):
  267. '''Process metering rule actions based specific namespaces.'''
  268. rm = self.routers.get(router['id'])
  269. with IptablesManagerTransaction(im):
  270. labels = router.get(constants.METERING_LABEL_KEY, [])
  271. for label in labels:
  272. label_id = label['id']
  273. label_chain = iptables_manager.get_chain_name(
  274. WRAP_NAME + LABEL + label_id, wrap=False)
  275. rules_chain = iptables_manager.get_chain_name(
  276. WRAP_NAME + RULE + label_id, wrap=False)
  277. exists = rm.metering_labels.get(label_id)
  278. if action == 'create' and not exists:
  279. self._create_metering_label_chain(rm,
  280. label_chain,
  281. rules_chain)
  282. rm.metering_labels[label_id] = label
  283. rule = label.get('rule')
  284. if rule:
  285. if action == 'create':
  286. self._process_metering_label_rule_add(
  287. rule, ext_dev, label_chain, rules_chain, im)
  288. elif action == 'delete':
  289. self._process_metering_label_rule_delete(
  290. rule, ext_dev, label_chain, rules_chain, im)
  291. def _process_metering_rule_action(self, router, action):
  292. rm = self.routers.get(router['id'])
  293. if not rm:
  294. return
  295. ext_dev, ext_snat_dev = self.get_external_device_names(rm)
  296. for (im, dev) in [(rm.iptables_manager, ext_dev),
  297. (rm.snat_iptables_manager, ext_snat_dev)]:
  298. if im and dev:
  299. self._process_metering_rule_action_based_on_ns(
  300. router, action, dev, im)
  301. def _update_metering_label_rules_based_on_ns(self, router, ext_dev, im):
  302. '''Update metering lable rules based on namespace.'''
  303. with IptablesManagerTransaction(im):
  304. labels = router.get(constants.METERING_LABEL_KEY, [])
  305. for label in labels:
  306. label_id = label['id']
  307. label_chain = iptables_manager.get_chain_name(
  308. WRAP_NAME + LABEL + label_id, wrap=False)
  309. rules_chain = iptables_manager.get_chain_name(
  310. WRAP_NAME + RULE + label_id, wrap=False)
  311. im.ipv4['filter'].empty_chain(rules_chain, wrap=False)
  312. rules = label.get('rules')
  313. if rules:
  314. self._process_metering_label_rules(
  315. rules, label_chain, rules_chain, ext_dev, im)
  316. def _update_metering_label_rules(self, router):
  317. rm = self.routers.get(router['id'])
  318. if not rm:
  319. return
  320. ext_dev, ext_snat_dev = self.get_external_device_names(rm)
  321. for (im, dev) in [(rm.iptables_manager, ext_dev),
  322. (rm.snat_iptables_manager, ext_snat_dev)]:
  323. if im and dev:
  324. self._update_metering_label_rules_based_on_ns(router, dev, im)
  325. @log_helpers.log_method_call
  326. def remove_metering_label(self, context, routers):
  327. for router in routers:
  328. self._process_disassociate_metering_label(router)
  329. @log_helpers.log_method_call
  330. def get_traffic_counters(self, context, routers):
  331. accs = {}
  332. routers_to_reconfigure = set()
  333. for router in routers:
  334. rm = self.routers.get(router['id'])
  335. if not rm:
  336. continue
  337. for label_id in rm.metering_labels:
  338. try:
  339. chain = iptables_manager.get_chain_name(WRAP_NAME +
  340. LABEL +
  341. label_id,
  342. wrap=False)
  343. chain_acc = rm.iptables_manager.get_traffic_counters(
  344. chain, wrap=False, zero=True)
  345. except RuntimeError:
  346. LOG.exception('Failed to get traffic counters, '
  347. 'router: %s', router)
  348. routers_to_reconfigure.add(router['id'])
  349. continue
  350. if not chain_acc:
  351. continue
  352. acc = accs.get(label_id, {'pkts': 0, 'bytes': 0})
  353. acc['pkts'] += chain_acc['pkts']
  354. acc['bytes'] += chain_acc['bytes']
  355. accs[label_id] = acc
  356. for router_id in routers_to_reconfigure:
  357. del self.routers[router_id]
  358. return accs