udp: add udp encap source port entropy support
[vpp.git] / src / vnet / udp / udp_api.c
index 6ba92bb..ae6b5bb 100644 (file)
 #include <vnet/udp/udp_encap.h>
 #include <vnet/fib/fib_table.h>
 #include <vnet/ip/ip_types_api.h>
+#include <vnet/udp/udp.h>
 
-#include <vnet/vnet_msg_enum.h>
-
-#define vl_typedefs            /* define message structures */
-#include <vnet/vnet_all_api_h.h>
-#undef vl_typedefs
-
-#define vl_endianfun           /* define message structures */
-#include <vnet/vnet_all_api_h.h>
-#undef vl_endianfun
-
-/* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
-#define vl_printfun
-#include <vnet/vnet_all_api_h.h>
-#undef vl_printfun
+#include <vnet/format_fns.h>
+#include <vnet/udp/udp.api_enum.h>
+#include <vnet/udp/udp.api_types.h>
 
+#define REPLY_MSG_ID_BASE udp_main.msg_id_base
 #include <vlibapi/api_helper_macros.h>
 
-#define foreach_udp_api_msg                                                   \
-  _ (UDP_ENCAP_DEL, udp_encap_del)                                            \
-  _ (UDP_ENCAP_ADD, udp_encap_add)                                            \
-  _ (UDP_ENCAP_DUMP, udp_encap_dump)                                          \
-  _ (UDP_DECAP_ADD_DEL, udp_decap_add_del)
-
 static void
 send_udp_encap_details (const udp_encap_t * ue, vl_api_registration_t * reg,
                        u32 context)
@@ -53,7 +37,7 @@ send_udp_encap_details (const udp_encap_t * ue, vl_api_registration_t * reg,
 
   mp = vl_msg_api_alloc (sizeof (*mp));
   clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = ntohs (VL_API_UDP_ENCAP_DETAILS);
+  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_DETAILS);
   mp->context = context;
 
   if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto)
@@ -115,6 +99,7 @@ vl_api_udp_encap_add_t_handler (vl_api_udp_encap_add_t *mp)
 {
   vl_api_udp_encap_add_reply_t *rmp;
   ip46_address_t src_ip, dst_ip;
+  udp_encap_fixup_flags_t flags;
   u32 fib_index, table_id;
   fib_protocol_t fproto;
   ip46_type_t itype;
@@ -135,11 +120,13 @@ vl_api_udp_encap_add_t_handler (vl_api_udp_encap_add_t *mp)
       goto done;
     }
 
-  uei = udp_encap_add_and_lock (fproto, fib_index,
-                               &src_ip, &dst_ip,
+  flags = UDP_ENCAP_FIXUP_NONE;
+  if (mp->udp_encap.src_port == 0)
+    flags |= UDP_ENCAP_FIXUP_UDP_SRC_PORT_ENTROPY;
+
+  uei = udp_encap_add_and_lock (fproto, fib_index, &src_ip, &dst_ip,
                                ntohs (mp->udp_encap.src_port),
-                               ntohs (mp->udp_encap.dst_port),
-                               UDP_ENCAP_FIXUP_NONE);
+                               ntohs (mp->udp_encap.dst_port), flags);
 
 done:
   /* *INDENT-OFF* */
@@ -201,37 +188,22 @@ vl_api_udp_decap_add_del_t_handler (vl_api_udp_decap_add_del_t *mp)
   REPLY_MACRO (VL_API_UDP_DECAP_ADD_DEL_REPLY);
 }
 
-#define vl_msg_name_crc_list
-#include <vnet/udp/udp.api.h>
-#undef vl_msg_name_crc_list
-
-static void
-setup_message_id_table (api_main_t * am)
-{
-#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
-  foreach_vl_msg_name_crc_udp;
-#undef _
-}
-
+#include <vnet/udp/udp.api.c>
 static clib_error_t *
 udp_api_hookup (vlib_main_t * vm)
 {
   api_main_t *am = vlibapi_get_main ();
 
-#define _(N,n)                                                  \
-    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
-                           vl_api_##n##_t_handler,              \
-                           vl_noop_handler,                     \
-                           vl_api_##n##_t_endian,               \
-                           vl_api_##n##_t_print,                \
-                           sizeof(vl_api_##n##_t), 1);
-  foreach_udp_api_msg;
-#undef _
-
   /*
    * Set up the (msg_name, crc, message-id) table
    */
-  setup_message_id_table (am);
+  REPLY_MSG_ID_BASE = setup_message_id_table ();
+
+  /* Mark these APIs as mp safe */
+  vl_api_set_msg_thread_safe (am, REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_ADD, 1);
+  vl_api_set_msg_thread_safe (am, REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_DEL, 1);
+  vl_api_set_msg_thread_safe (am, REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_DUMP,
+                             1);
 
   return 0;
 }