DSLite: Implement new API call DSLITE_ADDRESS_DUMP.
[vpp.git] / src / plugins / nat / nat_api.c
index f3d353f..d226ad6 100644 (file)
@@ -21,6 +21,7 @@
 #include <nat/nat.h>
 #include <nat/nat_det.h>
 #include <nat/nat64.h>
+#include <nat/nat66.h>
 #include <nat/dslite.h>
 #include <nat/nat_reass.h>
 #include <vlibapi/api.h>
@@ -696,6 +697,7 @@ static void
   u32 vrf_id, external_sw_if_index;
   int rv = 0;
   snat_protocol_t proto;
+  u8 *tag = 0;
 
   memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
   memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
@@ -707,11 +709,16 @@ static void
   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
   external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
   proto = ip_proto_to_snat_proto (mp->protocol);
+  mp->tag[sizeof (mp->tag) - 1] = 0;
+  tag = format (0, "%s", mp->tag);
+  vec_terminate_c_string (tag);
 
   rv = snat_add_static_mapping (local_addr, external_addr, local_port,
                                external_port, vrf_id, mp->addr_only,
                                external_sw_if_index, proto, mp->is_add,
-                               mp->twice_nat);
+                               mp->twice_nat, mp->out2in_only, tag);
+
+  vec_free (tag);
 
   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
 }
@@ -732,7 +739,8 @@ static void *vl_api_nat44_add_del_static_mapping_t_print
                clib_net_to_host_u16 (mp->local_port),
                clib_net_to_host_u16 (mp->external_port));
 
-  s = format (s, "twice_nat %d ", mp->twice_nat);
+  s = format (s, "twice_nat %d out2in_only %d ",
+             mp->twice_nat, mp->out2in_only);
 
   if (mp->vrf_id != ~0)
     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
@@ -757,13 +765,19 @@ send_nat44_static_mapping_details (snat_static_mapping_t * m,
   rmp->addr_only = m->addr_only;
   clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
   clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
-  rmp->local_port = htons (m->local_port);
-  rmp->external_port = htons (m->external_port);
   rmp->external_sw_if_index = ~0;
   rmp->vrf_id = htonl (m->vrf_id);
-  rmp->protocol = snat_proto_to_ip_proto (m->proto);
   rmp->context = context;
   rmp->twice_nat = m->twice_nat;
+  rmp->out2in_only = m->out2in_only;
+  if (m->addr_only == 0)
+    {
+      rmp->protocol = snat_proto_to_ip_proto (m->proto);
+      rmp->external_port = htons (m->external_port);
+      rmp->local_port = htons (m->local_port);
+    }
+  if (m->tag)
+    strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
 
   vl_api_send_msg (reg, (u8 *) rmp);
 }
@@ -782,13 +796,18 @@ send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
   rmp->addr_only = m->addr_only;
   clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
-  rmp->local_port = htons (m->l_port);
-  rmp->external_port = htons (m->e_port);
   rmp->external_sw_if_index = htonl (m->sw_if_index);
   rmp->vrf_id = htonl (m->vrf_id);
-  rmp->protocol = snat_proto_to_ip_proto (m->proto);
   rmp->context = context;
   rmp->twice_nat = m->twice_nat;
+  if (m->addr_only == 0)
+    {
+      rmp->protocol = snat_proto_to_ip_proto (m->proto);
+      rmp->external_port = htons (m->e_port);
+      rmp->local_port = htons (m->l_port);
+    }
+  if (m->tag)
+    strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
 
   vl_api_send_msg (reg, (u8 *) rmp);
 }
@@ -845,6 +864,7 @@ static void
   u32 vrf_id, sw_if_index;
   int rv = 0;
   snat_protocol_t proto = ~0;
+  u8 *tag = 0;
 
   if (mp->addr_only == 0)
     {
@@ -857,11 +877,15 @@ static void
     addr.as_u32 = 0;
   else
     memcpy (&addr.as_u8, mp->ip_address, 4);
-
+  mp->tag[sizeof (mp->tag) - 1] = 0;
+  tag = format (0, "%s", mp->tag);
+  vec_terminate_c_string (tag);
 
   rv =
     snat_add_static_mapping (addr, addr, port, port, vrf_id, mp->addr_only,
-                            sw_if_index, proto, mp->is_add, 0);
+                            sw_if_index, proto, mp->is_add, 0, 0, tag);
+
+  vec_free (tag);
 
   REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
 }
@@ -906,6 +930,8 @@ send_nat44_identity_mapping_details (snat_static_mapping_t * m,
   rmp->vrf_id = htonl (m->vrf_id);
   rmp->protocol = snat_proto_to_ip_proto (m->proto);
   rmp->context = context;
+  if (m->tag)
+    strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
 
   vl_api_send_msg (reg, (u8 *) rmp);
 }
@@ -928,6 +954,8 @@ send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
   rmp->vrf_id = htonl (m->vrf_id);
   rmp->protocol = snat_proto_to_ip_proto (m->proto);
   rmp->context = context;
+  if (m->tag)
+    strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
 
   vl_api_send_msg (reg, (u8 *) rmp);
 }
@@ -1162,7 +1190,7 @@ vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
   ip.src_address.as_u32 = ukey.addr.as_u32;
   ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
   key.key = ukey.as_u64;
-  if (sm->num_workers)
+  if (sm->num_workers > 1)
     tsm =
       vec_elt_at_index (sm->per_thread_data,
                        sm->worker_in2out_cb (&ip, ukey.fib_index));
@@ -1236,19 +1264,24 @@ static void
   nat44_lb_addr_port_t *locals = 0;
   ip4_address_t e_addr;
   snat_protocol_t proto;
+  u8 *tag = 0;
 
   locals = unformat_nat44_lb_addr_port (mp->locals, mp->local_num);
   clib_memcpy (&e_addr, mp->external_addr, 4);
   proto = ip_proto_to_snat_proto (mp->protocol);
+  mp->tag[sizeof (mp->tag) - 1] = 0;
+  tag = format (0, "%s", mp->tag);
+  vec_terminate_c_string (tag);
 
   rv =
     nat44_add_del_lb_static_mapping (e_addr,
                                     clib_net_to_host_u16 (mp->external_port),
                                     proto, clib_net_to_host_u32 (mp->vrf_id),
                                     locals, mp->is_add, mp->twice_nat,
-                                    mp->out2in_only);
+                                    mp->out2in_only, tag);
 
   vec_free (locals);
+  vec_free (tag);
 
   REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
 }
@@ -1259,7 +1292,7 @@ static void *vl_api_nat44_add_del_lb_static_mapping_t_print
   u8 *s;
 
   s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
-  s = format (s, "is_add %d twice_nat %d out2in_only ",
+  s = format (s, "is_add %d twice_nat %d out2in_only %d ",
              mp->is_add, mp->twice_nat, mp->out2in_only);
 
   FINISH;
@@ -1289,6 +1322,8 @@ send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
   rmp->context = context;
   rmp->twice_nat = m->twice_nat;
   rmp->out2in_only = m->out2in_only;
+  if (m->tag)
+    strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
 
   locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
   vec_foreach (ap, m->locals)
@@ -2546,6 +2581,54 @@ send_reply:
   REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
 }
 
+static void
+send_dslite_address_details (snat_address_t * ap,
+                            vl_api_registration_t * reg, u32 context)
+{
+  vl_api_dslite_address_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_DSLITE_ADDRESS_DETAILS + sm->msg_id_base);
+  clib_memcpy (rmp->ip_address, &(ap->addr), 4);
+  rmp->context = context;
+
+  vl_api_send_msg (reg, (u8 *) rmp);
+}
+
+static void
+vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
+{
+  vl_api_registration_t *reg;
+  dslite_main_t *dm = &dslite_main;
+  snat_address_t *ap;
+
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+
+  /* *INDENT-OFF* */
+  vec_foreach (ap, dm->addr_pool)
+    {
+      send_dslite_address_details (ap, reg, mp->context);
+    }
+  /* *INDENT-ON* */
+}
+
+static void *
+vl_api_dslite_address_dump_t_print (vl_api_dslite_address_dump_t * mp,
+                                   void *handle)
+{
+  u8 *s;
+
+  s = format (0, "SCRIPT: dslite_address_dump ");
+
+  FINISH;
+}
+
 static void *vl_api_dslite_add_del_pool_addr_range_t_print
   (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
 {
@@ -2560,6 +2643,192 @@ static void *vl_api_dslite_add_del_pool_addr_range_t_print
 }
 
 
+/*************/
+/*** NAT66 ***/
+/*************/
+
+static void
+vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
+                                         mp)
+{
+  snat_main_t *sm = &snat_main;
+  vl_api_nat66_add_del_interface_reply_t *rmp;
+  int rv = 0;
+
+  VALIDATE_SW_IF_INDEX (mp);
+
+  rv =
+    nat66_interface_add_del (ntohl (mp->sw_if_index), mp->is_inside,
+                            mp->is_add);
+
+  BAD_SW_IF_INDEX_LABEL;
+
+  REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
+}
+
+static void *
+vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
+                                       void *handle)
+{
+  u8 *s;
+
+  s = format (0, "SCRIPT: nat66_add_del_interface ");
+  s = format (s, "sw_if_index %d %s %s",
+             clib_host_to_net_u32 (mp->sw_if_index),
+             mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
+
+  FINISH;
+}
+
+static void
+  vl_api_nat66_add_del_static_mapping_t_handler
+  (vl_api_nat66_add_del_static_mapping_t * mp)
+{
+  snat_main_t *sm = &snat_main;
+  vl_api_nat66_add_del_static_mapping_reply_t *rmp;
+  ip6_address_t l_addr, e_addr;
+  int rv = 0;
+
+  memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
+  memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
+
+  rv =
+    nat66_static_mapping_add_del (&l_addr, &e_addr,
+                                 clib_net_to_host_u32 (mp->vrf_id),
+                                 mp->is_add);
+
+  REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
+}
+
+static void *vl_api_nat66_add_del_static_mapping_t_print
+  (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
+{
+  u8 *s;
+
+  s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
+  s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
+             format_ip6_address, mp->local_ip_address,
+             format_ip6_address, mp->external_ip_address,
+             clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
+
+  FINISH;
+}
+
+typedef struct nat66_api_walk_ctx_t_
+{
+  svm_queue_t *q;
+  u32 context;
+} nat66_api_walk_ctx_t;
+
+static int
+nat66_api_interface_walk (snat_interface_t * i, void *arg)
+{
+  vl_api_nat66_interface_details_t *rmp;
+  snat_main_t *sm = &snat_main;
+  nat66_api_walk_ctx_t *ctx = arg;
+
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
+  rmp->sw_if_index = ntohl (i->sw_if_index);
+  rmp->is_inside = nat_interface_is_inside (i);
+  rmp->context = ctx->context;
+
+  vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
+
+  return 0;
+}
+
+static void
+vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
+{
+  svm_queue_t *q;
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (q == 0)
+    return;
+
+  nat66_api_walk_ctx_t ctx = {
+    .q = q,
+    .context = mp->context,
+  };
+
+  nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
+}
+
+static void *
+vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
+                                    void *handle)
+{
+  u8 *s;
+
+  s = format (0, "SCRIPT: nat66_interface_dump ");
+
+  FINISH;
+}
+
+static int
+nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
+{
+  vl_api_nat66_static_mapping_details_t *rmp;
+  nat66_main_t *nm = &nat66_main;
+  snat_main_t *sm = &snat_main;
+  nat66_api_walk_ctx_t *ctx = arg;
+  fib_table_t *fib;
+  vlib_counter_t vc;
+
+  fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
+  if (!fib)
+    return -1;
+
+  vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
+
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id =
+    ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
+  clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
+  clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
+  rmp->vrf_id = ntohl (fib->ft_table_id);
+  rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
+  rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
+  rmp->context = ctx->context;
+
+  vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
+
+  return 0;
+}
+
+static void
+vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
+                                           * mp)
+{
+  svm_queue_t *q;
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (q == 0)
+    return;
+
+  nat66_api_walk_ctx_t ctx = {
+    .q = q,
+    .context = mp->context,
+  };
+
+  nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
+}
+
+static void *
+vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
+                                         mp, void *handle)
+{
+  u8 *s;
+
+  s = format (0, "SCRIPT: nat66_static_mapping_dump ");
+
+  FINISH;
+}
+
+
 /* List of message types that this plugin understands */
 #define foreach_snat_plugin_api_msg                                     \
 _(NAT_CONTROL_PING, nat_control_ping)                                   \
@@ -2613,10 +2882,15 @@ _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix)                           \
 _(NAT64_PREFIX_DUMP, nat64_prefix_dump)                                 \
 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr)           \
 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range)       \
+_(DSLITE_ADDRESS_DUMP, dslite_address_dump)                            \
 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr)                           \
 _(DSLITE_GET_AFTR_ADDR, dslite_get_aftr_addr)                           \
 _(DSLITE_SET_B4_ADDR, dslite_set_b4_addr)                               \
-_(DSLITE_GET_B4_ADDR, dslite_get_b4_addr)
+_(DSLITE_GET_B4_ADDR, dslite_get_b4_addr)                               \
+_(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface)                     \
+_(NAT66_INTERFACE_DUMP, nat66_interface_dump)                           \
+_(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping)           \
+_(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
 
 /* Set up the API message handling tables */
 static clib_error_t *