Browse Source

Add ffho_net functions to get Lookback IPs and Router-ID

  Use ffho_net module to compute Loopback IPs (IPv4 + IPv6) as well as the
  Router ID (which basicly is the IPv4 Loopback IP). Use this functions in
  templates which computed these values in Jinja beforehand.

  From now on a pillar node entry does not require 'prefixes' for the 'lo'
  iface to be specified manually. They are computed as well, if missing.

Signed-off-by: Maximilian Wilhelm <max@rfc2324.org>
Maximilian Wilhelm 7 years ago
parent
commit
913ef41ccf
3 changed files with 62 additions and 11 deletions
  1. 56 0
      _modules/ffho_net.py
  2. 2 2
      network/interfaces/interfaces.tmpl
  3. 4 9
      snmpd/snmpd.conf

+ 56 - 0
_modules/ffho_net.py

@@ -52,6 +52,13 @@ GRE_FFRL_attrs = {
 	'ttl'    : '64',
 }
 
+
+# The IPv4/IPv6 prefix use for Loopback IPs
+loopback_prefix = {
+	'v4' : '10.132.255.',
+	'v6' : '2a03:2260:2342:ffff::',
+}
+
 ################################################################################
 #                              Internal functions                              #
 #                                                                              #
@@ -560,6 +567,25 @@ def _generate_ffrl_gre_tunnels (ifaces):
 			except KeyError:
 				pass
 
+def _generate_loopback_ips (ifaces, node_config, node_id):
+	v4_ip = "%s/32"  % get_loopback_ip (node_config, node_id, 'v4')
+	v6_ip = "%s/128" % get_loopback_ip (node_config, node_id, 'v6')
+
+	# Interface lo already present?
+	if 'lo' not in ifaces:
+		ifaces['lo'] = { 'prefixes' : [] }
+
+	# Add 'prefixes' list if not present
+	if 'prefixes' not in ifaces['lo']:
+		ifaces['lo']['prefixes'] = []
+
+	prefixes = ifaces['lo']['prefixes']
+	if v4_ip not in prefixes:
+		prefixes.append (v4_ip)
+
+	if v6_ip not in prefixes:
+		prefixes.append (v6_ip)
+
 
 ################################################################################
 #                              Public functions                                #
@@ -573,6 +599,7 @@ def _generate_ffrl_gre_tunnels (ifaces):
 #   * VRFs
 #   * B.A.T.M.A.N. instances and interfaces
 #   * VXLAN interfaces to connect B.A.T.M.A.N. sites
+#   * Loopback IPs derived from numeric node ID
 #
 # @param: node_config	Pillar node configuration (as dict)
 # @param: sites_config	Pillar sites configuration (as dict)
@@ -614,6 +641,9 @@ def get_interface_config (node_config, sites_config, node_id = ""):
 		if interface.startswith ('veth_'):
 			_update_veth_config (interface, config)
 
+	# Auto generate Loopback IPs IFF not present
+	_generate_loopback_ips (ifaces, node_config, node_id)
+
 	# Auto generated VRF devices for any VRF found in ifaces and not already configured.
 	_generate_vrfs (ifaces)
 
@@ -764,6 +794,32 @@ def get_node_iface_ips (node_config, iface_name):
 	return ips
 
 
+#
+# Get the lookback IP of the given node for the given proto
+#
+# @param node_config:	Pillar node configuration (as dict)
+# @param node_id:	Minion name / Pillar node configuration key
+# @param proto:		{ 'v4', 'v6' }
+def get_loopback_ip (node_config, node_id, proto):
+	if proto not in [ 'v4', 'v6' ]:
+		raise Exception ("get_loopback_ip(): Invalid proto: \"%s\"." % proto)
+
+	if not proto in loopback_prefix:
+		raise Exception ("get_loopback_ip(): No loopback_prefix configured for IP%s in ffno_net module!" % proto)
+
+	if not 'id' in node_config:
+		raise Exception ("get_loopback_ip(): No 'id' configured in pillar for node \"%s\"!" % node_id)
+
+
+	return "%s%s" % (loopback_prefix.get (proto), node_config.get ('id'))
+
+#
+# Get the router id (read: IPv4 Lo-IP) out of the given node config.
+def get_router_id (node_config, node_id):
+	return get_loopback_ip (node_config, node_id, 'v4')
+
+
+
 # Compute minions OSPF interface configuration according to FFHO routing policy
 # See https://wiki.ffho.net/infrastruktur:vlans for information about Vlans
 def get_ospf_interface_config (node_config, grains_id):

+ 2 - 2
network/interfaces/interfaces.tmpl

@@ -5,13 +5,14 @@
 {%- set node_config = salt['pillar.get']('nodes:' ~ grains['id'], {}) %}
 {%- set node_id = grains['id'] %}
 {%- set sites_config = salt['pillar.get']('sites', {}) %}
+{%- set ifaces = salt['ffho_net.get_interface_config'](node_config, sites_config) %}
 
 #source /etc/network/interfaces.d/*
 
 {#
  # Configure any secondary IPs / router_id / infra-srv IPs on loopback, if present
  #}
-{%- set lo_prefixes = salt['pillar.get']('nodes:' ~ grains['id'] ~ ':ifaces:lo:prefixes', []) %}
+{%- set lo_prefixes = ifaces['lo']['prefixes'] %}
 
 {#- Add user/infra-srv VPN IPs, too #}
 {%- for vpn in ['user-srv', 'infra-srv'] %}
@@ -43,7 +44,6 @@ iface lo
 {#
  # Set up ifaces dict with configured and generated interfaces.
  #}
-{%- set ifaces = salt['ffho_net.get_interface_config'](node_config, sites_config) %}
 {%- for interface in ifaces|sort if interface not in ['lo'] %}
   {%- set config = ifaces.get (interface) %}
 

+ 4 - 9
snmpd/snmpd.conf

@@ -1,6 +1,7 @@
 {%- import "globals.sls" as globals with context %}
+{%- set node_config = salt['pillar.get'] ('nodes:' ~ grains['id']) %}
 {%- set ro_community = salt['pillar.get'] ('ffho:snmp:ro_community', "Configure ffho:snmp:ro_community in pillar...") %}
-{%- set sys_location = salt['pillar.get'] ('nodes:' ~ grains['id'] ~ ':sysLocation', "Please fix sysLocation...") %}
+{%- set sys_location = node_config.get ('sysLocation', "Please fix sysLocation...") %}
 #
 # /etc/snmp/snmpd.conf (Salt managed)
 #
@@ -10,14 +11,8 @@ agentAddress  udp:127.0.0.1:161
 agentAddress  udp6:[::1]:161
 
 # Listen for connections on Loopback-IPs
-{%- for ip in salt['pillar.get']('nodes:' ~ grains['id'] ~ ':ifaces:lo:prefixes', []) %}
-  {%- if ip.startswith ('10.132.255.') %}
-agentAddress  udp:{{ ip.split ('/')[0] }}:161
-  {%- endif %}
-  {%- if ip.startswith ('2a03:2260:2342:ffff::') %}
-agentAddress  udp6:[{{ ip.split ('/')[0] }}]:161
-  {%- endif %}
-{%- endfor %}
+agentAddress  udp:{{ salt['ffho_net.get_loopback_ip'] (node_config, grains['id'], 'v4') }}:161
+agentAddress  udp6:[{{ salt['ffho_net.get_loopback_ip'] (node_config, grains['id'], 'v6') }}]:161
 
 
 rocommunity   {{ ro_community }}	127.0.0.1