api: vat2 and json autogeneration for api messages
[vpp.git] / src / plugins / dns / dns.c
index 6ae0ff1..de1862d 100644 (file)
@@ -14,7 +14,7 @@
  */
 
 #include <vnet/vnet.h>
-#include <vnet/udp/udp.h>
+#include <vnet/udp/udp_local.h>
 #include <vnet/plugin/plugin.h>
 #include <vnet/fib/fib_table.h>
 #include <dns/dns.h>
 #include <stdbool.h>
 
 /* define message IDs */
-#include <dns/dns_msg_enum.h>
-
-/* define message structures */
-#define vl_typedefs
-#include <dns/dns_all_api_h.h>
-#undef vl_typedefs
-
-/* define generated endian-swappers */
-#define vl_endianfun
-#include <dns/dns_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 <dns/dns_all_api_h.h>
-#undef vl_printfun
-
-/* Get the API version number */
-#define vl_api_version(n,v) static u32 api_version=(v);
-#include <dns/dns_all_api_h.h>
-#undef vl_api_version
+#include <dns/dns.api_enum.h>
+#include <dns/dns.api_types.h>
 
 #define REPLY_MSG_ID_BASE dm->msg_id_base
 #include <vlibapi/api_helper_macros.h>
 
 /* Macro to finish up custom dump fns */
+#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
 #define FINISH                                  \
     vec_add1 (s, 0);                            \
     vl_print (handle, (char *)s);               \
@@ -68,7 +49,7 @@ dns_cache_clear (dns_main_t * dm)
   if (dm->is_enabled == 0)
     return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED;
 
-  dns_cache_lock (dm);
+  dns_cache_lock (dm, 1);
 
   /* *INDENT-OFF* */
   pool_foreach (ep, dm->entries,
@@ -721,7 +702,7 @@ dns_delete_by_name (dns_main_t * dm, u8 * name)
   if (dm->is_enabled == 0)
     return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED;
 
-  dns_cache_lock (dm);
+  dns_cache_lock (dm, 2);
   p = hash_get_mem (dm->cache_entry_by_name, name);
   if (!p)
     {
@@ -755,7 +736,7 @@ delete_random_entry (dns_main_t * dm)
     return VNET_API_ERROR_UNSPECIFIED;
 #endif
 
-  dns_cache_lock (dm);
+  dns_cache_lock (dm, 3);
   limit = pool_elts (dm->entries);
   start_index = random_u32 (&dm->random_seed) % limit;
 
@@ -792,7 +773,7 @@ dns_add_static_entry (dns_main_t * dm, u8 * name, u8 * dns_reply_data)
   if (dm->is_enabled == 0)
     return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED;
 
-  dns_cache_lock (dm);
+  dns_cache_lock (dm, 4);
   p = hash_get_mem (dm->cache_entry_by_name, name);
   if (p)
     {
@@ -816,6 +797,8 @@ dns_add_static_entry (dns_main_t * dm, u8 * name, u8 * dns_reply_data)
 
   /* Note: consumes the name vector */
   ep->name = name;
+  /* make sure it NULL-terminated as hash_set_mem will use strlen() */
+  vec_terminate_c_string (ep->name);
   hash_set_mem (dm->cache_entry_by_name, ep->name, ep - dm->entries);
   ep->flags = DNS_CACHE_ENTRY_FLAG_VALID | DNS_CACHE_ENTRY_FLAG_STATIC;
   ep->dns_response = dns_reply_data;
@@ -844,7 +827,7 @@ vnet_dns_resolve_name (dns_main_t * dm, u8 * name, dns_pending_request_t * t,
   if (name[0] == 0)
     return VNET_API_ERROR_INVALID_VALUE;
 
-  dns_cache_lock (dm);
+  dns_cache_lock (dm, 5);
 search_again:
   p = hash_get_mem (dm->cache_entry_by_name, name);
   if (p)
@@ -903,9 +886,8 @@ search_again:
              name = ep->cname;
              goto search_again;
            }
-
-         /* Note: caller must drop the lock! */
          *retp = ep;
+         dns_cache_unlock (dm);
          return (0);
        }
       else
@@ -1577,25 +1559,6 @@ vl_api_dns_resolve_ip_t_handler (vl_api_dns_resolve_ip_t * mp)
   dns_cache_unlock (dm);
 }
 
-#define vl_msg_name_crc_list
-#include <dns/dns_all_api_h.h>
-#undef vl_msg_name_crc_list
-
-static void
-setup_message_id_table (dns_main_t * dm)
-{
-#define _(id,n,crc) \
-  vl_msg_api_add_msg_name_crc (dm->api_main, #n "_" #crc, dm->msg_id_base + id);
-  foreach_vl_msg_name_crc_dns;
-#undef _
-}
-
-#define foreach_dns_plugin_api_msg                      \
-_(DNS_ENABLE_DISABLE, dns_enable_disable)               \
-_(DNS_NAME_SERVER_ADD_DEL, dns_name_server_add_del)     \
-_(DNS_RESOLVE_NAME, dns_resolve_name)                  \
-_(DNS_RESOLVE_IP, dns_resolve_ip)
-
 static clib_error_t *
 dns_config_fn (vlib_main_t * vm, unformat_input_t * input)
 {
@@ -2133,7 +2096,7 @@ format_dns_cache (u8 * s, va_list * args)
       return s;
     }
 
-  dns_cache_lock (dm);
+  dns_cache_lock (dm, 6);
 
   if (name)
     {
@@ -2736,7 +2699,7 @@ test_dns_expire_command_fn (vlib_main_t * vm,
   else
     return clib_error_return (0, "no name provided");
 
-  dns_cache_lock (dm);
+  dns_cache_lock (dm, 7);
 
   p = hash_get_mem (dm->cache_entry_by_name, name);
   if (!p)
@@ -2794,7 +2757,7 @@ vnet_send_dns4_reply (dns_main_t * dm, dns_pending_request_t * pr,
   u8 *reply;
   vl_api_dns_resolve_name_reply_t _rnr, *rnr = &_rnr;
   vl_api_dns_resolve_ip_reply_t _rir, *rir = &_rir;
-  u32 ttl, tmp;
+  u32 ttl = 64, tmp;
   u32 qp_offset;
   dns_query_t *qp;
   dns_rr_t *rr;
@@ -3029,115 +2992,21 @@ found_src_address:
     }
 }
 
-static void *vl_api_dns_enable_disable_t_print
-  (vl_api_dns_enable_disable_t * mp, void *handle)
-{
-  u8 *s;
-
-  s = format (0, "SCRIPT: dns_enable_disable ");
-  s = format (s, "%s ", mp->enable ? "enable" : "disable");
-
-  FINISH;
-}
-
-static void *vl_api_dns_name_server_add_del_t_print
-  (vl_api_dns_name_server_add_del_t * mp, void *handle)
-{
-  u8 *s;
-
-  s = format (0, "SCRIPT: dns_name_server_add_del ");
-  if (mp->is_ip6)
-    s = format (s, "%U ", format_ip6_address,
-               (ip6_address_t *) mp->server_address);
-  else
-    s = format (s, "%U ", format_ip4_address,
-               (ip4_address_t *) mp->server_address);
-
-  if (mp->is_add == 0)
-    s = format (s, "del ");
-
-  FINISH;
-}
-
-static void *vl_api_dns_resolve_name_t_print
-  (vl_api_dns_resolve_name_t * mp, void *handle)
-{
-  u8 *s;
-
-  s = format (0, "SCRIPT: dns_resolve_name ");
-  s = format (s, "%s ", mp->name);
-  FINISH;
-}
-
-static void *vl_api_dns_resolve_ip_t_print
-  (vl_api_dns_resolve_ip_t * mp, void *handle)
-{
-  u8 *s;
-
-  s = format (0, "SCRIPT: dns_resolve_ip ");
-  if (mp->is_ip6)
-    s = format (s, "%U ", format_ip6_address, mp->address);
-  else
-    s = format (s, "%U ", format_ip4_address, mp->address);
-  FINISH;
-}
-
-static void
-dns_custom_dump_configure (dns_main_t * dm)
-{
-#define _(n,f) dm->api_main->msg_print_handlers \
-  [VL_API_##n + dm->msg_id_base]                \
-    = (void *) vl_api_##f##_t_print;
-  foreach_dns_plugin_api_msg;
-#undef _
-}
-
-/* Set up the API message handling tables */
-static clib_error_t *
-dns_plugin_api_hookup (vlib_main_t * vm)
-{
-  dns_main_t *dmp = &dns_main;
-#define _(N,n)                                                  \
-    vl_msg_api_set_handlers((VL_API_##N + dmp->msg_id_base),    \
-                           #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_dns_plugin_api_msg;
-#undef _
-
-  return 0;
-}
-
+#include <dns/dns.api.c>
 static clib_error_t *
 dns_init (vlib_main_t * vm)
 {
   dns_main_t *dm = &dns_main;
-  u8 *name;
 
   dm->vlib_main = vm;
   dm->vnet_main = vnet_get_main ();
   dm->name_cache_size = 1000;
   dm->max_ttl_in_seconds = 86400;
   dm->random_seed = 0xDEADDABE;
-  dm->api_main = &api_main;
-
-  name = format (0, "dns_%08x%c", api_version, 0);
+  dm->api_main = vlibapi_get_main ();
 
   /* Ask for a correctly-sized block of API message decode slots */
-  dm->msg_id_base = vl_msg_api_get_msg_ids
-    ((char *) name, VL_MSG_FIRST_AVAILABLE);
-
-  (void) dns_plugin_api_hookup (vm);
-
-  /* Add our API messages to the global name_crc hash table */
-  setup_message_id_table (dm);
-
-  dns_custom_dump_configure (dm);
-
-  vec_free (name);
+  dm->msg_id_base = setup_message_id_table ();
 
   return 0;
 }