ffho_net.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. #!/usr/bin/python
  2. import re
  3. mac_prefix = "f2"
  4. vrf_info = {
  5. 'vrf_external' : {
  6. 'table' : 1023,
  7. 'fwmark' : [ '0x1', '0x1023' ],
  8. },
  9. }
  10. sites = None
  11. def _get_site_no (sites_config, site_name):
  12. global sites
  13. if sites == None:
  14. sites = {}
  15. for site in sites_config:
  16. if site.startswith ("_"):
  17. continue
  18. sites[site] = sites_config[site].get ("site_no", -2)
  19. return sites.get (site_name, -1)
  20. #
  21. # Generate a MAC address after the format f2:dd:dd:ss:nn:nn where
  22. # dd:dd is the hexadecimal reprensentation of the nodes device_id
  23. # ff:ff representing the gluon nodes
  24. #
  25. # ss is the hexadecimal reprensentation of the site_id the interface is connected to
  26. #
  27. # nn:nn is the decimal representation of the network the interface is connected to, with
  28. # 00:00 being the dummy interface
  29. # 00:0f being the VEth internal side interface
  30. # 00:e0 being an external instance dummy interface
  31. # 00:e1 being an inter-gw-vpn interface
  32. # 00:e4 being an nodes fastd tunnel interface of IPv4 transport
  33. # 00:e6 being an nodes fastd tunnel interface of IPv6 transport
  34. # 00:ef being an extenral instance VEth interface side
  35. # 02:xx being a connection to local Vlan 2xx
  36. # 1b:24 being the ibss 2.4GHz bssid
  37. # 1b:05 being the ibss 5GHz bssid
  38. # ff:ff being the gluon next-node interface
  39. # xx:xx being a VXLAN tunnel for site ss, with xx being a the underlay VLAN ID (1xyz, 2xyz)
  40. def gen_batman_iface_mac (site_no, device_no, network):
  41. net_type_map = {
  42. 'dummy' : "00:00",
  43. 'int2ext' : "00:0f",
  44. 'dummy-e' : "00:e0",
  45. 'intergw' : "00:e1",
  46. 'nodes4' : "00:e4",
  47. 'nodes6' : "00:e6",
  48. 'ext2int' : "00:ef",
  49. }
  50. # Well-known network type?
  51. if network in net_type_map:
  52. last = net_type_map[network]
  53. elif type (network) == int:
  54. last = re.sub (r'(\d{2})(\d{2})', '\g<1>:\g<2>', "%04d" % network)
  55. else:
  56. last = "ee:ee"
  57. # Convert device_no to hex, format number to 4 digits with leading zeros and : betwwen 2nd and 3rd digit
  58. device_no_hex = re.sub (r'([0-9a-fA-F]{2})([0-9a-fA-F]{2})', '\g<1>:\g<2>', "%04x" % int (device_no))
  59. # Format site_no to two digit number with leading zero
  60. site_no_hex = "%02d" % int (site_no)
  61. return "%s:%s:%s:%s" % (mac_prefix, device_no_hex, site_no_hex, last)
  62. #
  63. # Default parameters added to any given bonding/bridge interface,
  64. # if not specified at the interface configuration.
  65. default_bond_config = {
  66. 'bond-mode': '802.3ad',
  67. 'bond-min-links': '1',
  68. 'bond-xmit-hash-policy': 'layer3+4'
  69. }
  70. default_bridge_config = {
  71. 'bridge-fd' : '0',
  72. 'bridge-stp' : 'no'
  73. }
  74. #
  75. # Hop penalty to set if none is explicitly specified
  76. # Check if one of these roles is configured for any given node, use first match.
  77. default_hop_penalty_by_role = {
  78. 'bbr' : 5,
  79. 'bras' : 50,
  80. 'batman_gw' : 50,
  81. }
  82. batman_role_evaluation_order = [ 'bbr', 'batman_gw', 'bras' ]
  83. # Gather B.A.T.M.A.N. related config options for real batman devices (e.g. bat0)
  84. # as well as for batman member interfaces (e.g. eth0.100, fastd ifaces etc.)
  85. def _update_batman_config (node_config, iface, sites_config):
  86. try:
  87. node_batman_hop_penalty = int (node_config['batman']['hop-penalty'])
  88. except KeyError,ValueError:
  89. node_batman_hop_penalty = None
  90. iface_config = node_config['ifaces'][iface]
  91. iface_type = iface_config.get ('type', 'inet')
  92. batman_config = {}
  93. for item, value in iface_config.items ():
  94. if item.startswith ('batman-'):
  95. batman_config[item] = value
  96. iface_config.pop (item)
  97. # B.A.T.M.A.N. device (e.g. bat0)
  98. if iface_type == 'batman':
  99. if 'batman-hop-penalty' not in batman_config:
  100. # If there's a hop penalty set for the node, but not for the interface
  101. # apply the nodes hop penalty
  102. if node_batman_hop_penalty:
  103. batman_config['batman-hop-penalty'] = node_batman_hop_penalty
  104. # If there's no hop penalty set for the node, use a default hop penalty
  105. # for the roles the node might have, if any
  106. else:
  107. node_roles = node_config.get ('roles', [])
  108. for role in batman_role_evaluation_order:
  109. if role in node_roles:
  110. batman_config['batman-hop-penalty'] = default_hop_penalty_by_role[role]
  111. # If batman ifaces were specified as a list - which they should -
  112. # generate a sorted list of interface names as string representation
  113. if 'batman-ifaces' in batman_config and type (batman_config['batman-ifaces']) == list:
  114. batman_iface_str = " ".join (sorted (batman_config['batman-ifaces']))
  115. batman_config['batman-ifaces'] = batman_iface_str
  116. # B.A.T.M.A.N. member interface (e.g. eth.100, fastd ifaces, etc.)
  117. elif iface_type == 'batman_iface':
  118. # Generate unique MAC address for every batman iface, as B.A.T.M.A.N.
  119. # will get puzzled with multiple interfaces having the same MAC and
  120. # do nasty things.
  121. site = iface_config.get ('site')
  122. site_no = _get_site_no (sites_config, site)
  123. device_no = node_config.get ('id')
  124. network = 1234
  125. # Generate a unique BATMAN-MAC for this interfaces
  126. match = re.search (r'^vlan(\d+)', iface)
  127. if match:
  128. network = int (match.group (1))
  129. iface_config['hwaddress'] = gen_batman_iface_mac (site_no, device_no, network)
  130. iface_config['batman'] = batman_config
  131. # Mangle bond specific config items with default values and store them in
  132. # separate sub-dict for easier access and configuration.
  133. def _update_bond_config (config):
  134. bond_config = default_bond_config.copy ()
  135. for item, value in config.items ():
  136. if item.startswith ('bond-'):
  137. bond_config[item] = value
  138. config.pop (item)
  139. if bond_config['bond-mode'] not in ['2', 'balance-xor', '4', '802.3ad']:
  140. bond_config.pop ('bond-xmit-hash-policy')
  141. config['bond'] = bond_config
  142. # Mangle bridge specific config items with default values and store them in
  143. # separate sub-dict for easier access and configuration.
  144. def _update_bridge_config (config):
  145. bridge_config = default_bridge_config.copy ()
  146. for item, value in config.items ():
  147. if item.startswith ('bridge-'):
  148. bridge_config[item] = value
  149. config.pop (item)
  150. # Fix and salt mangled string interpretation back to real string.
  151. if type (value) == bool:
  152. bridge_config[item] = "yes" if value else "no"
  153. # If bridge ports were specified as a list - which they should -
  154. # generate a sorted list of interface names as string representation
  155. if 'bridge-ports' in bridge_config and type (bridge_config['bridge-ports']) == list:
  156. bridge_ports_str = " ".join (sorted (bridge_config['bridge-ports']))
  157. bridge_config['bridge-ports'] = bridge_ports_str
  158. config['bridge'] = bridge_config
  159. # Move vlan specific config items into a sub-dict for easier access and pretty-printing
  160. # in the configuration file
  161. def _update_vlan_config (config):
  162. vlan_config = {}
  163. for item, value in config.items ():
  164. if item.startswith ('vlan-'):
  165. vlan_config[item] = value
  166. config.pop (item)
  167. config['vlan'] = vlan_config
  168. # Generate configuration entries for any batman related interfaces not
  169. # configured explicitly, but asked for implicitly by role batman and a
  170. # (list of) site(s) specified in the node config.
  171. def _generate_batman_interface_config (node_config, ifaces, sites_config):
  172. # No role 'batman', nothing to do
  173. roles = node_config.get ('roles', [])
  174. if 'batman' not in roles:
  175. return
  176. # Should there be a 2nd external BATMAN instance?
  177. batman_ext = 'batman_ext' in roles or 'bras' in roles
  178. device_no = node_config.get ('id', -1)
  179. for site in node_config.get ('sites', []):
  180. site_no = _get_site_no (sites_config, site)
  181. # Predefine interface names for regular/external BATMAN instance
  182. # and possible VEth link pair for connecting both instances.
  183. bat_site_if = "bat-%s" % site
  184. dummy_site_if = "dummy-%s" % site
  185. bat_site_if_ext = "bat-%s-ext" % site
  186. dummy_site_if_ext = "dummy-%s-e" % site
  187. int2ext_site_if = "i2e-%s" % site
  188. ext2int_site_if = "e2i-%s" % site
  189. site_ifaces = {
  190. # Regular BATMAN interface, always present
  191. bat_site_if : {
  192. 'type' : 'batman',
  193. # int2ext_site_if will be added automagically if requred
  194. 'batman-ifaces' : [ dummy_site_if ],
  195. 'batman-ifaces-ignore-regex': '.*_.*',
  196. },
  197. # Dummy interface always present in regular BATMAN instance
  198. dummy_site_if : {
  199. 'link-type' : 'dummy',
  200. 'hwaddress' : gen_batman_iface_mac (site_no, device_no, 'dummy'),
  201. },
  202. # Optional 2nd "external" BATMAN instance
  203. bat_site_if_ext : {
  204. 'type' : 'batman',
  205. 'batman-ifaces' : [ dummy_site_if_ext, ext2int_site_if ],
  206. 'batman-ifaces-ignore-regex': '.*_.*',
  207. 'ext_only' : True,
  208. },
  209. # Optional dummy interface always present in 2nd "external" BATMAN instance
  210. dummy_site_if_ext : {
  211. 'link-type' : 'dummy',
  212. 'hwaddress' : gen_batman_iface_mac (site_no, device_no, 'dummy-e'),
  213. 'ext_only' : True,
  214. },
  215. # Optional VEth interface pair - internal side
  216. int2ext_site_if : {
  217. 'link-type' : 'veth',
  218. 'veth-peer-name' : ext2int_site_if,
  219. 'hwaddress' : gen_batman_iface_mac (site_no, device_no, 'int2ext'),
  220. 'ext_only' : True,
  221. },
  222. # Optional VEth interface pair - "external" side
  223. ext2int_site_if : {
  224. 'link-type' : 'veth',
  225. 'veth-peer-name' : int2ext_site_if,
  226. 'hwaddress' : gen_batman_iface_mac (site_no, device_no, 'ext2int'),
  227. 'ext_only' : True,
  228. },
  229. }
  230. for iface, iface_config_tmpl in site_ifaces.items ():
  231. # Ignore any interface only relevant when role batman_ext is set
  232. # but it isn't
  233. if not batman_ext and iface_config_tmpl.get ('ext_only', False):
  234. continue
  235. # Remove ext_only key so we don't leak it into ifaces dict
  236. if 'ext_only' in iface_config_tmpl:
  237. del iface_config_tmpl['ext_only']
  238. # If there is no trace of the desired iface config yet...
  239. if iface not in ifaces:
  240. # ... just place our template there.
  241. ifaces[iface] = iface_config_tmpl
  242. # If there should be an 2nd external BATMAN instance make sure
  243. # the internal side of the VEth iface pair is connected to the
  244. # internal BATMAN instance.
  245. if batman_ext and iface == bat_site_if:
  246. iface_config_tmpl['batman-ifaces'].append (int2ext_site_if)
  247. # If there already is an interface configuration try to enhance it with
  248. # meaningful values from our template and force correct hwaddress to be
  249. # used.
  250. else:
  251. iface_config = ifaces[iface]
  252. # Force hwaddress to be what we expect.
  253. if 'hwaddress' in iface_config_tmpl:
  254. iface_config['hwaddress'] = iface_config_tmpl['hwaddress']
  255. # Copy every attribute of the config template missing in iface config
  256. for attr in iface_config_tmpl:
  257. if attr not in iface_config:
  258. iface_config[attr] = iface_config_tmpl[attr]
  259. # Make sure there is a bridge present for every site where a mesh_breakout
  260. # interface should be configured.
  261. for iface, config in ifaces.items ():
  262. iface_type = config.get ('type', 'inet')
  263. if iface_type not in ['mesh_breakout', 'batman_iface']:
  264. continue
  265. site = config.get ('site')
  266. site_bridge = "br-%s" % site
  267. batman_site_if = "bat-%s" % site
  268. if iface_type == 'mesh_breakout':
  269. # If the bridge has already been defined (with an IP maybe) make
  270. # sure that the corresbonding batman device is part of the bridge-
  271. # ports.
  272. if site_bridge in ifaces:
  273. bridge_config = ifaces.get (site_bridge)
  274. # If there already is/are (a) bridge-port(s) defined, add
  275. # the batman and the breakout interfaces if not present...
  276. bridge_ports = bridge_config.get ('bridge-ports', None)
  277. if bridge_ports:
  278. for dev in (batman_site_if, iface):
  279. if not dev in bridge_ports:
  280. if type (bridge_ports) == list:
  281. bridge_ports.append (dev)
  282. else:
  283. bridge_config['bridge-ports'] += ' ' + dev
  284. # ...if there is no bridge-port defined yet, just used
  285. # the batman and breakout iface.
  286. else:
  287. bridge_config['bridge-ports'] = [ iface, batman_site_if ]
  288. # If the bridge isn't present alltogether, add it.
  289. else:
  290. ifaces[site_bridge] = {
  291. 'bridge-ports' : [ iface, batman_site_if ],
  292. }
  293. elif iface_type == 'batman_iface':
  294. batman_ifaces = ifaces[bat_site_if]['batman-ifaces']
  295. if iface not in batman_ifaces:
  296. if type (batman_ifaces) == list:
  297. batman_ifaces.append (iface)
  298. else:
  299. batman_ifaces += ' ' + iface
  300. ## Generate VXLAN tunnels for every configured batman peer for every site
  301. ## configured on this and the peer node.
  302. #def _generate_vxlan_interface_config_complex (node_config, ifaces, node_id, nodes_config):
  303. # # No role 'batman', nothing to do
  304. # if 'batman' not in node_config.get ('roles', []):
  305. # return
  306. #
  307. # # No batman peers configred, nothing to do
  308. # try:
  309. # peers = node_config['batman']['peers']
  310. # if type (peers) != list:
  311. # return
  312. # except KeyError:
  313. # return
  314. #
  315. # # Sites configured on this node. Nothing to do, if none.
  316. # my_sites = node_config.get ('sites', [])
  317. # if len (my_sites) == 0:
  318. # return
  319. #
  320. # device_no = node_config.get ('id', -1)
  321. #
  322. # # ...
  323. # for peer in peers:
  324. # try:
  325. # # Try to get node config of peer
  326. # peer_config = nodes_config.get (peer)
  327. #
  328. # # Not a batman node?
  329. # if not 'batman' in peer_config['roles']:
  330. # continue
  331. #
  332. # # Verify we are in peers list of peer
  333. # peers_of_peer = peer_config['batman']['peers']
  334. # if type (peers_of_peer) != list:
  335. # continue
  336. # if node_id not in peers_of_peer:
  337. # continue
  338. #
  339. # # Get sites configured on peers
  340. # sites_of_peer = peer_config.get ('sites')
  341. # except KeyError:
  342. # continue
  343. #
  344. # for site in my_sites:
  345. # if site not in sites_of_peer:
  346. # continue
  347. #
  348. # # Build tunnel here
  349. def _generate_vxlan_interface_config (node_config, ifaces, sites_config):
  350. # No role 'batman', nothing to do
  351. if 'batman' not in node_config.get ('roles', []):
  352. return
  353. # Sites configured on this node. Nothing to do, if none.
  354. my_sites = node_config.get ('sites', [])
  355. if len (my_sites) == 0:
  356. return
  357. # As we're still here we can now safely assume that a B.A.T.M.A.N.
  358. # device has been configured for every site specified in sites list.
  359. device_no = node_config.get ('id', -1)
  360. for iface, iface_config in ifaces.items ():
  361. batman_connect_sites = iface_config.get ('batman_connect_sites', [])
  362. # If we got a string, convert it to a list with a single element
  363. if type (batman_connect_sites) == str:
  364. batman_connect_sites = [ batman_connect_sites ]
  365. # If the string 'all' is part of the list, blindly use all sites configured for this node
  366. if 'all' in batman_connect_sites:
  367. batman_connect_sites = my_sites
  368. for site in batman_connect_sites:
  369. # Silenty ignore sites not configured on this node
  370. if site not in my_sites:
  371. continue
  372. # iface_name := vx_<last 5 chars of underlay iface>_<site> stripped to 15 chars
  373. vx_iface = "vx_%s_%s" % (re.sub ('vlan', 'v', iface)[-5:], site)[:15]
  374. site_no = _get_site_no (sites_config, site)
  375. vni = 100 + site_no
  376. bat_iface = "bat-%s" % site
  377. try:
  378. iface_id = int (re.sub ('vlan', '', iface))
  379. # Gather interface specific mcast address.
  380. # The address is derived from the vlan-id of the underlying interface,
  381. # assuming that it in fact is a vlan interface.
  382. # Mangle the vlan-id into two 2 digit values, eliminating any leading zeros.
  383. iface_id_4digit = "%04d" % iface_id
  384. octet2 = int (iface_id_4digit[0:2])
  385. octet3 = int (iface_id_4digit[2:4])
  386. mcast_ip = "225.%s.%s.%s" % (octet2, octet3, site_no)
  387. vni = octet2 * 256 * 256 + octet3 * 256 + site_no
  388. except ValueError:
  389. iface_id = 9999
  390. mcast_ip = "225.0.0.%s" % site_no
  391. vni = site_no
  392. # bail out if VXLAN tunnel already configured
  393. if vx_iface in ifaces:
  394. continue
  395. # If there's no batman interface for this site, there's no point
  396. # in setting up a VXLAN interfaces
  397. if bat_iface not in ifaces:
  398. continue
  399. # Add the VXLAN interface
  400. ifaces[vx_iface] = {
  401. 'vxlan' : {
  402. 'vxlan-id' : vni,
  403. 'vxlan-svcnodeip' : mcast_ip,
  404. 'vxlan-physdev' : iface,
  405. },
  406. 'hwaddress' : gen_batman_iface_mac (site_no, device_no, iface_id),
  407. }
  408. # If the batman interface for this site doesn't have any interfaces
  409. # set up - which basicly cannot happen - add this VXLAN tunnel as
  410. # the first in the list.
  411. if not 'batman-ifaces' in ifaces[bat_iface]:
  412. ifaces[bat_iface]['batman-ifaces'] = [ vx_iface ]
  413. continue
  414. # In the hope there already are interfaces for batman set up already
  415. # add this VXLAN tunnel to the list
  416. batman_ifaces = ifaces[bat_iface]['batman-ifaces']
  417. if vx_iface not in batman_ifaces:
  418. if type (batman_ifaces) == list:
  419. batman_ifaces.append (vx_iface)
  420. else:
  421. batman_ifaces += ' ' + vx_iface
  422. def _generate_vrfs (ifaces):
  423. for iface, iface_config in ifaces.items ():
  424. vrf = iface_config.get ('vrf', None)
  425. if vrf and vrf not in ifaces:
  426. conf = vrf_info.get (vrf, {})
  427. table = conf.get ('table', 1234)
  428. fwmark = conf.get ('fwmark', None)
  429. ifaces[vrf] = {
  430. 'vrf-table' : table,
  431. }
  432. # Create ip rule's for any fwmarks defined
  433. if fwmark:
  434. up = []
  435. # Make sure we are dealing with a list even if there is only one mark to be set up
  436. if type (fwmark) in (str, int):
  437. fwmark = [ fwmark ]
  438. # Create ip rule entries for IPv4 and IPv6 for every fwmark
  439. for mark in fwmark:
  440. up.append ("ip rule add fwmark %s table %s" % (mark, table))
  441. up.append ("ip -6 rule add fwmark %s table %s" % (mark, table))
  442. ifaces[vrf]['up'] = up
  443. GRE_FFRL_attrs = {
  444. 'mode' : 'gre',
  445. 'method' : 'tunnel',
  446. 'mtu' : '1400',
  447. 'ttl' : '64',
  448. }
  449. def _generate_ffrl_gre_tunnels (ifaces):
  450. for iface, iface_config in ifaces.items ():
  451. # We only care for GRE_FFRL type interfaces
  452. if iface_config.get ('type', '') != 'GRE_FFRL':
  453. continue
  454. # Copy default values to interface config
  455. for attr, val in GRE_FFRL_attrs.items ():
  456. if not attr in iface_config:
  457. iface_config[attr] = val
  458. # Guesstimate local IPv4 tunnel endpoint address from tunnel-physdev
  459. if not 'local' in iface_config and 'tunnel-physdev' in iface_config:
  460. try:
  461. physdev_prefixes = [p.split ('/')[0] for p in ifaces[iface_config['tunnel-physdev']]['prefixes'] if '.' in p]
  462. if len (physdev_prefixes) == 1:
  463. iface_config['local'] = physdev_prefixes[0]
  464. except KeyError:
  465. pass
  466. def get_interface_config (node_config, sites_config):
  467. # Get config of this node and dict of all configured ifaces
  468. ifaces = node_config.get ('ifaces', {})
  469. # Generate configuration entries for any batman related interfaces not
  470. # configured explicitly, but asked for implicitly by role <batman> and
  471. # a (list of) site(s) specified in the node config.
  472. _generate_batman_interface_config (node_config, ifaces, sites_config)
  473. # Generate VXLAN tunnels for every interfaces specifying 'batman_connect_sites'
  474. _generate_vxlan_interface_config (node_config, ifaces, sites_config)
  475. # Enhance ifaces configuration with some meaningful defaults for
  476. # bonding, bridge and vlan interfaces, MAC address for batman ifaces, etc.
  477. for interface, config in ifaces.items ():
  478. iface_type = config.get ('type', 'inet')
  479. if 'batman-ifaces' in config or iface_type.startswith ('batman'):
  480. _update_batman_config (node_config, interface, sites_config)
  481. if 'bond-slaves' in config:
  482. _update_bond_config (config)
  483. # FIXME: This maybe will not match on bridges without any member ports configured!
  484. if 'bridge-ports' in config or interface.startswith ('br-'):
  485. _update_bridge_config (config)
  486. if 'vlan-raw-device' in config or 'vlan-id' in config:
  487. _update_vlan_config (config)
  488. # Auto generated VRF devices for any VRF found in ifaces and not already configured.
  489. _generate_vrfs (ifaces)
  490. # Pimp GRE_FFRL type inteface configuration with default values
  491. _generate_ffrl_gre_tunnels (ifaces)
  492. # Drop any config parameters used in node interface configuration not
  493. # relevant anymore for config file generation.
  494. for interface, config in ifaces.items ():
  495. for key in [ 'batman_connect_sites', 'ospf', 'site', 'type' ]:
  496. if key in config:
  497. config.pop (key)
  498. # This leaves 'auto', 'prefixes' and 'desc' as keys which should not be directly
  499. # printed into the remaining configuration. These are handled within the jinja
  500. # interface template.
  501. return ifaces
  502. # Generate entries for /etc/bat-hosts for every batman interface we will configure on any node.
  503. # For readability purposes superflous/redundant information is being stripped/supressed.
  504. # As these names will only show up in batctl calls with a specific site, site_names in interfaces
  505. # are stripped. Dummy interfaces are stripped as well.
  506. def gen_bat_hosts (nodes_config, sites_config):
  507. bat_hosts = {}
  508. for node_id in sorted (nodes_config.keys ()):
  509. node_config = nodes_config.get (node_id)
  510. node_name = node_id.split ('.')[0]
  511. ifaces = get_interface_config (node_config, sites_config)
  512. for iface in sorted (ifaces):
  513. iface_config = ifaces.get (iface)
  514. hwaddress = iface_config.get ('hwaddress', None)
  515. if hwaddress == None:
  516. continue
  517. entry_name = node_name
  518. match = re.search (r'^dummy-([^-]+)(-e)?$', iface)
  519. if match:
  520. if match.group (2):
  521. entry_name += "-e"
  522. else:
  523. entry_name += "/%s" % re.sub (r'^(vx_.*|i2e|e2i)[_-](.*)$', '\g<1>', iface)
  524. bat_hosts[hwaddress] = entry_name
  525. if 'fastd' in node_config.get ('roles', []):
  526. device_no = node_config.get ('id')
  527. for site in node_config.get ('sites', []):
  528. site_no = _get_site_no (sites_config, site)
  529. for network in ('intergw', 'nodes4', 'nodes6'):
  530. hwaddress = gen_batman_iface_mac (site_no, device_no, network)
  531. bat_hosts[hwaddress] = "%s/%s" % (node_name, network)
  532. return bat_hosts
  533. # Generate eBGP session parameters for FFRL Transit from nodes pillar information.
  534. def get_ffrl_bgp_config (ifaces, proto):
  535. from ipcalc import IP
  536. _generate_ffrl_gre_tunnels (ifaces)
  537. sessions = {}
  538. for iface in sorted (ifaces):
  539. # We only care for GRE tunnels to the FFRL Backbone
  540. if not iface.startswith ('gre_ffrl_'):
  541. continue
  542. iface_config = ifaces.get (iface)
  543. # Search for IPv4/IPv6 prefix as defined by proto parameter
  544. local = None
  545. neighbor = None
  546. for prefix in iface_config.get ('prefixes', []):
  547. if (proto == 'v4' and '.' in prefix) or (proto == 'v6' and ':' in prefix):
  548. local = prefix.split ('/')[0]
  549. # Calculate neighbor IP as <local IP> - 1
  550. if proto == 'v4':
  551. neighbor = str (IP (int (IP (local)) - 1, version = 4))
  552. else:
  553. neighbor = str (IP (int (IP (local)) - 1, version = 6))
  554. break
  555. # Strip gre_ prefix iface name and use it as identifier for the eBGP session.
  556. name = re.sub ('gre_ffrl_', 'ffrl_', iface)
  557. sessions[name] = {
  558. 'local' : local,
  559. 'neighbor' : neighbor,
  560. 'bgp_local_pref' : iface_config.get ('bgp_local_pref', None),
  561. }
  562. return sessions