Unregister UDPC port only when owned
[vpp.git] / src / vnet / udp / udp.c
index 7674ef2..d5166d9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Copyright (c) 2016-2019 Cisco and/or its affiliates.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
@@ -22,6 +22,8 @@
 #include <vnet/dpo/load_balance.h>
 #include <vnet/fib/ip4_fib.h>
 
+udp_main_t udp_main;
+
 udp_connection_t *
 udp_connection_alloc (u32 thread_index)
 {
@@ -93,6 +95,7 @@ udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
   listener->c_proto = TRANSPORT_PROTO_UDP;
   listener->c_s_index = session_index;
   listener->c_fib_index = lcl->fib_index;
+  listener->owns_port = 1;
   clib_spinlock_init (&listener->rx_lock);
 
   node_index = lcl->is_ip4 ? udp4_input_node.index : udp6_input_node.index;
@@ -166,8 +169,9 @@ udp_session_close (u32 connection_index, u32 thread_index)
   uc = udp_connection_get (connection_index, thread_index);
   if (uc)
     {
-      udp_unregister_dst_port (vm, clib_net_to_host_u16 (uc->c_lcl_port),
-                              uc->c_is_ip4);
+      if (uc->owns_port || !uc->is_connected)
+       udp_unregister_dst_port (vm, clib_net_to_host_u16 (uc->c_lcl_port),
+                                uc->c_is_ip4);
       session_transport_delete_notify (&uc->connection);
       udp_connection_free (uc);
     }
@@ -300,6 +304,7 @@ udp_open_connection (transport_endpoint_cfg_t * rmt)
   uc->c_is_ip4 = rmt->is_ip4;
   uc->c_proto = TRANSPORT_PROTO_UDP;
   uc->c_fib_index = rmt->fib_index;
+  uc->owns_port = 1;
 
   return uc->c_c_index;
 }
@@ -318,9 +323,9 @@ udp_session_get_half_open (u32 conn_index)
 
 /* *INDENT-OFF* */
 const static transport_proto_vft_t udp_proto = {
-  .bind = udp_session_bind,
-  .open = udp_open_connection,
-  .unbind = udp_session_unbind,
+  .start_listen = udp_session_bind,
+  .connect = udp_open_connection,
+  .stop_listen = udp_session_unbind,
   .push_header = udp_push_header,
   .get_connection = udp_session_get,
   .get_listener = udp_session_get_listener,
@@ -342,9 +347,11 @@ int
 udpc_connection_open (transport_endpoint_cfg_t * rmt)
 {
   udp_connection_t *uc;
+  /* Reproduce the logic of udp_open_connection to find the correct thread */
+  u32 thread_index = vlib_num_workers ()? 1 : vlib_get_main ()->thread_index;
   u32 uc_index;
   uc_index = udp_open_connection (rmt);
-  uc = udp_connection_get (uc_index, vlib_get_thread_index ());
+  uc = udp_connection_get (uc_index, thread_index);
   uc->is_connected = 1;
   return uc_index;
 }
@@ -362,9 +369,9 @@ udpc_connection_listen (u32 session_index, transport_endpoint_t * lcl)
 
 /* *INDENT-OFF* */
 const static transport_proto_vft_t udpc_proto = {
-  .bind = udpc_connection_listen,
-  .open = udpc_connection_open,
-  .unbind = udp_session_unbind,
+  .start_listen = udpc_connection_listen,
+  .stop_listen = udp_session_unbind,
+  .connect = udpc_connection_open,
   .push_header = udp_push_header,
   .get_connection = udp_session_get,
   .get_listener = udp_session_get_listener,
@@ -376,7 +383,7 @@ const static transport_proto_vft_t udpc_proto = {
   .format_connection = format_udp_session,
   .format_half_open = format_udp_half_open_session,
   .format_listener = format_udp_listener_session,
-  .tx_type = TRANSPORT_TX_DEQUEUE,
+  .tx_type = TRANSPORT_TX_DGRAM,
   .service_type = TRANSPORT_SERVICE_CL,
 };
 /* *INDENT-ON* */