dns, dhcp: on-demand udp port registration

Change-Id: I8bf411adc6c5f4caa349d161174b544d2de3ad1d
Signed-off-by: Dave Barach <dave@barachs.net>
diff --git a/src/vnet/dhcp/client.c b/src/vnet/dhcp/client.c
index 3233305..1c52b0c 100644
--- a/src/vnet/dhcp/client.c
+++ b/src/vnet/dhcp/client.c
@@ -907,6 +907,7 @@
 
   if (a->is_add)
     {
+      dhcp_maybe_register_udp_ports ();
       pool_get (dcm->clients, c);
       clib_memset (c, 0, sizeof (*c));
       c->state = DHCP_DISCOVER;
diff --git a/src/vnet/dhcp/dhcp4_proxy_node.c b/src/vnet/dhcp/dhcp4_proxy_node.c
index 97ed3b5..99e5a73 100644
--- a/src/vnet/dhcp/dhcp4_proxy_node.c
+++ b/src/vnet/dhcp/dhcp4_proxy_node.c
@@ -736,6 +736,24 @@
 };
 /* *INDENT-ON* */
 
+void
+dhcp_maybe_register_udp_ports (void)
+{
+  dhcp_proxy_main_t *dm = &dhcp_proxy_main;
+  vlib_main_t *vm = dm->vlib_main;
+
+  if (dm->udp_ports_registered)
+    return;
+
+  udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client,
+			 dhcp_proxy_to_client_node.index, 1 /* is_ip4 */ );
+
+  udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server,
+			 dhcp_proxy_to_server_node.index, 1 /* is_ip4 */ );
+
+  dm->udp_ports_registered = 1;
+}
+
 static clib_error_t *
 dhcp4_proxy_init (vlib_main_t * vm)
 {
@@ -744,12 +762,7 @@
 
   error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
   dm->error_drop_node_index = error_drop_node->index;
-
-  udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_client,
-			 dhcp_proxy_to_client_node.index, 1 /* is_ip4 */ );
-
-  udp_register_dst_port (vm, UDP_DST_PORT_dhcp_to_server,
-			 dhcp_proxy_to_server_node.index, 1 /* is_ip4 */ );
+  dm->vlib_main = vm;
 
   return 0;
 }
@@ -777,6 +790,8 @@
   if (ip46_address_is_zero (src_addr))
     return VNET_API_ERROR_INVALID_SRC_ADDRESS;
 
+  dhcp_maybe_register_udp_ports ();
+
   rx_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
 						    rx_table_id,
 						    FIB_SOURCE_DHCP);
diff --git a/src/vnet/dhcp/dhcp_proxy.h b/src/vnet/dhcp/dhcp_proxy.h
index ec43769..f8fc290 100644
--- a/src/vnet/dhcp/dhcp_proxy.h
+++ b/src/vnet/dhcp/dhcp_proxy.h
@@ -146,11 +146,23 @@
 
   /* hash lookup specific vrf_id -> option 82 vss suboption  */
   u32 *vss_index_by_rx_fib_index[DHCP_N_PROTOS];
+
+  /* udp ports have been registered */
+  int udp_ports_registered;
+
+  /* convenience */
+  vlib_main_t *vlib_main;
+
 } dhcp_proxy_main_t;
 
 extern dhcp_proxy_main_t dhcp_proxy_main;
 
 /**
+ * @brief Register the dhcp client and server ports, if not already done
+ */
+void dhcp_maybe_register_udp_ports (void);
+
+/**
  * @brief Send the details of a proxy session to the API client during a dump
  */
 void dhcp_send_details (fib_protocol_t proto,
diff --git a/src/vnet/dns/dns.c b/src/vnet/dns/dns.c
index 54b75aa..93b7fb8 100644
--- a/src/vnet/dns/dns.c
+++ b/src/vnet/dns/dns.c
@@ -72,6 +72,7 @@
 {
   vlib_thread_main_t *tm = &vlib_thread_main;
   u32 n_vlib_mains = tm->n_vlib_mains;
+  vlib_main_t *vm = dm->vlib_main;
 
   if (is_enable)
     {
@@ -79,6 +80,23 @@
 	  && (vec_len (dm->ip6_name_servers) == 0))
 	return VNET_API_ERROR_NO_NAME_SERVERS;
 
+      if (dm->udp_ports_registered == 0)
+	{
+	  udp_register_dst_port (vm, UDP_DST_PORT_dns_reply,
+				 dns46_reply_node.index, 1 /* is_ip4 */ );
+
+	  udp_register_dst_port (vm, UDP_DST_PORT_dns_reply6,
+				 dns46_reply_node.index, 0 /* is_ip4 */ );
+
+	  udp_register_dst_port (vm, UDP_DST_PORT_dns,
+				 dns4_request_node.index, 1 /* is_ip4 */ );
+
+	  udp_register_dst_port (vm, UDP_DST_PORT_dns6,
+				 dns6_request_node.index, 0 /* is_ip4 */ );
+
+	  dm->udp_ports_registered = 1;
+	}
+
       if (dm->cache_entry_by_name == 0)
 	{
 	  if (n_vlib_mains > 1)
@@ -1597,17 +1615,6 @@
   dm->max_ttl_in_seconds = 86400;
   dm->random_seed = 0xDEADDABE;
 
-  udp_register_dst_port (vm, UDP_DST_PORT_dns_reply, dns46_reply_node.index,
-			 1 /* is_ip4 */ );
-
-  udp_register_dst_port (vm, UDP_DST_PORT_dns_reply6, dns46_reply_node.index,
-			 0 /* is_ip4 */ );
-
-  udp_register_dst_port (vm, UDP_DST_PORT_dns, dns4_request_node.index,
-			 1 /* is_ip4 */ );
-
-  udp_register_dst_port (vm, UDP_DST_PORT_dns6, dns6_request_node.index,
-			 0 /* is_ip4 */ );
   return 0;
 }
 
diff --git a/src/vnet/dns/dns.h b/src/vnet/dns/dns.h
index 59a61ed..c747e2e 100644
--- a/src/vnet/dns/dns.h
+++ b/src/vnet/dns/dns.h
@@ -101,6 +101,9 @@
   /** enable / disable flag */
   int is_enabled;
 
+  /** udp port registration complete */
+  int udp_ports_registered;
+
   /** upstream name servers, e.g. 8.8.8.8 */
   ip4_address_t *ip4_name_servers;
   ip6_address_t *ip6_name_servers;