vlib: add description field in plugin registration
[vpp.git] / src / plugins / snat / snat.c
index 1cf2b0c..70b6a6e 100644 (file)
@@ -19,7 +19,6 @@
 #include <vnet/ip/ip.h>
 #include <vnet/ip/ip4.h>
 #include <vnet/plugin/plugin.h>
-#include <vlibapi/api.h>
 #include <snat/snat.h>
 #include <snat/snat_ipfix_logging.h>
 #include <snat/snat_det.h>
@@ -48,6 +47,9 @@ snat_main_t snat_main;
 
 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
 
+#define REPLY_MSG_ID_BASE sm->msg_id_base
+#include <vlibapi/api_helper_macros.h>
+
 /* Get the API version number */
 #define vl_api_version(n,v) static u32 api_version=(v);
 #include <snat/snat_all_api_h.h>
@@ -60,45 +62,6 @@ snat_main_t snat_main;
     vec_free (s);                               \
     return handle;
 
-/* 
- * A handy macro to set up a message reply.
- * Assumes that the following variables are available:
- * mp - pointer to request message
- * rmp - pointer to reply message type
- * rv - return value
- */
-
-#define REPLY_MACRO(t)                                          \
-do {                                                            \
-    unix_shared_memory_queue_t * q =                            \
-    vl_api_client_index_to_input_queue (mp->client_index);      \
-    if (!q)                                                     \
-        return;                                                 \
-                                                                \
-    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
-    rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
-    rmp->context = mp->context;                                 \
-    rmp->retval = ntohl(rv);                                    \
-                                                                \
-    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
-} while(0);
-
-#define REPLY_MACRO2(t, body)                                   \
-do {                                                            \
-    unix_shared_memory_queue_t * q =                            \
-    vl_api_client_index_to_input_queue (mp->client_index);      \
-    if (!q)                                                     \
-        return;                                                 \
-                                                                \
-    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
-    rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
-    rmp->context = mp->context;                                 \
-    rmp->retval = ntohl(rv);                                    \
-    do {body;} while (0);                                       \
-    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
-} while(0);
-
-
 /* Hook up input features */
 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
   .arc_name = "ip4-unicast",
@@ -144,71 +107,10 @@ VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
 /* *INDENT-OFF* */
 VLIB_PLUGIN_REGISTER () = {
     .version = VPP_BUILD_VER,
+    .description = "Network Address Translation",
 };
 /* *INDENT-ON* */
 
-/*$$$$$ move to an installed header file */
-#if (1 || CLIB_DEBUG > 0)       /* "trust, but verify" */
-
-#define VALIDATE_SW_IF_INDEX(mp)                               \
- do { u32 __sw_if_index = ntohl(mp->sw_if_index);              \
-    vnet_main_t *__vnm = vnet_get_main();                       \
-    if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
-                           __sw_if_index)) {                    \
-        rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
-        goto bad_sw_if_index;                                   \
-    }                                                           \
-} while(0);
-
-#define BAD_SW_IF_INDEX_LABEL                   \
-do {                                            \
-bad_sw_if_index:                                \
-    ;                                           \
-} while (0);
-
-#define VALIDATE_RX_SW_IF_INDEX(mp)                            \
- do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index);                \
-    vnet_main_t *__vnm = vnet_get_main();                       \
-    if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
-                           __rx_sw_if_index)) {                        \
-        rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
-        goto bad_rx_sw_if_index;                               \
-    }                                                           \
-} while(0);
-
-#define BAD_RX_SW_IF_INDEX_LABEL               \
-do {                                            \
-bad_rx_sw_if_index:                            \
-    ;                                           \
-} while (0);
-
-#define VALIDATE_TX_SW_IF_INDEX(mp)                            \
- do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index);                \
-    vnet_main_t *__vnm = vnet_get_main();                       \
-    if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
-                           __tx_sw_if_index)) {                        \
-        rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
-        goto bad_tx_sw_if_index;                               \
-    }                                                           \
-} while(0);
-
-#define BAD_TX_SW_IF_INDEX_LABEL               \
-do {                                            \
-bad_tx_sw_if_index:                            \
-    ;                                           \
-} while (0);
-
-#else
-
-#define VALIDATE_SW_IF_INDEX(mp)
-#define BAD_SW_IF_INDEX_LABEL
-#define VALIDATE_RX_SW_IF_INDEX(mp)
-#define BAD_RX_SW_IF_INDEX_LABEL
-#define VALIDATE_TX_SW_IF_INDEX(mp)
-#define BAD_TX_SW_IF_INDEX_LABEL
-
-#endif  /* CLIB_DEBUG > 0 */
-
 /**
  * @brief Add/del NAT address to FIB.
  *
@@ -1539,6 +1441,8 @@ vl_api_snat_user_session_dump_t_handler
   q = vl_api_client_index_to_input_queue (mp->client_index);
   if (q == 0)
     return;
+  if (!mp->is_ip4)
+    return;
 
   clib_memcpy (&ukey.addr, mp->ip_address, 4);
   ukey.fib_index = ip4_fib_index_from_table_id (ntohl(mp->vrf_id));
@@ -1620,7 +1524,7 @@ vl_api_snat_det_forward_t_handler
   snat_main_t * sm = &snat_main;
   vl_api_snat_det_forward_reply_t * rmp;
   int rv = 0;
-  u16 lo_port = 0;
+  u16 lo_port = 0, hi_port = 0;
   snat_det_map_t * dm;
   ip4_address_t in_addr, out_addr;
 
@@ -1634,12 +1538,13 @@ vl_api_snat_det_forward_t_handler
     }
 
   snat_det_forward(dm, &in_addr, &out_addr, &lo_port);
+  hi_port = lo_port + dm->ports_per_host - 1;
 
 send_reply:
   REPLY_MACRO2(VL_API_SNAT_DET_FORWARD_REPLY,
   ({
     rmp->out_port_lo = ntohs(lo_port);
-    rmp->out_port_hi = ntohs(lo_port + dm->ports_per_host - 1);
+    rmp->out_port_hi = ntohs(hi_port);
     rmp->is_ip4 = 1;
     memset(rmp->out_addr, 0, 16);
     clib_memcpy(rmp->out_addr, &out_addr, 4);
@@ -1700,6 +1605,55 @@ static void *vl_api_snat_det_reverse_t_print
   FINISH;
 }
 
+static void
+sent_snat_det_map_details
+(snat_det_map_t * m, unix_shared_memory_queue_t * q, u32 context)
+{
+  vl_api_snat_det_map_details_t *rmp;
+  snat_main_t * sm = &snat_main;
+
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id = ntohs (VL_API_SNAT_DET_MAP_DETAILS+sm->msg_id_base);
+  rmp->is_ip4 = 1;
+  clib_memcpy (rmp->in_addr, &m->in_addr, 4);
+  rmp->in_plen = m->in_plen;
+  clib_memcpy (rmp->out_addr, &m->out_addr, 4);
+  rmp->out_plen = m->out_plen;
+  rmp->sharing_ratio = htonl (m->sharing_ratio);
+  rmp->ports_per_host = htons (m->ports_per_host);
+  rmp->ses_num = htonl (m->ses_num);
+  rmp->context = context;
+
+  vl_msg_api_send_shmem (q, (u8 *) & rmp);
+}
+
+static void
+vl_api_snat_det_map_dump_t_handler
+(vl_api_snat_det_map_dump_t * mp)
+{
+  unix_shared_memory_queue_t *q;
+  snat_main_t * sm = &snat_main;
+  snat_det_map_t * m;
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (q == 0)
+    return;
+
+  vec_foreach(m, sm->det_maps)
+    sent_snat_det_map_details(m, q, mp->context);
+}
+
+static void * vl_api_snat_det_map_dump_t_print
+(vl_api_snat_det_map_dump_t *mp, void * handle)
+{
+  u8 * s;
+
+  s = format (0, "SCRIPT: snat_det_map_dump ");
+
+  FINISH;
+}
+
 /* List of message types that this plugin understands */
 #define foreach_snat_plugin_api_msg                                     \
 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range)                       \
@@ -1719,7 +1673,8 @@ _(SNAT_USER_DUMP, snat_user_dump)                                       \
 _(SNAT_USER_SESSION_DUMP, snat_user_session_dump)                       \
 _(SNAT_ADD_DET_MAP, snat_add_det_map)                                   \
 _(SNAT_DET_FORWARD, snat_det_forward)                                   \
-_(SNAT_DET_REVERSE, snat_det_reverse)
+_(SNAT_DET_REVERSE, snat_det_reverse)                                   \
+_(SNAT_DET_MAP_DUMP, snat_det_map_dump)
 
 /* Set up the API message handling tables */
 static clib_error_t *
@@ -2604,6 +2559,9 @@ snat_config (vlib_main_t * vm, unformat_input_t * input)
       if (!static_mapping_only ||
           (static_mapping_only && static_mapping_connection_tracking))
         {
+          sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
+          sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
+
           clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
                                 user_memory_size);
 
@@ -2621,6 +2579,11 @@ snat_config (vlib_main_t * vm, unformat_input_t * input)
           clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
                                 user_memory_size);
         }
+      else
+        {
+          sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
+          sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
+        }
       clib_bihash_init_8_8 (&sm->static_mapping_by_local,
                             "static_mapping_by_local", static_mapping_buckets,
                             static_mapping_memory_size);