sr: new messages created to return packet statistics in sr localsid details
[vpp.git] / src / vnet / srv6 / sr_api.c
index 9bb1a16..1b95125 100644 (file)
 #include <vnet/api_errno.h>
 #include <vnet/feature/feature.h>
 #include <vnet/fib/fib_table.h>
-
 #include <vnet/ip/ip_types_api.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/srv6/sr.api_enum.h>
+#include <vnet/srv6/sr.api_types.h>
 
+#define REPLY_MSG_ID_BASE sr_main.msg_id_base
 #include <vlibapi/api_helper_macros.h>
 
-#define foreach_vpe_api_msg                             \
-_(SR_LOCALSID_ADD_DEL, sr_localsid_add_del)             \
-_(SR_POLICY_ADD, sr_policy_add)                         \
-_(SR_POLICY_MOD, sr_policy_mod)                         \
-_(SR_POLICY_DEL, sr_policy_del)                         \
-_(SR_STEERING_ADD_DEL, sr_steering_add_del)             \
-_(SR_SET_ENCAP_SOURCE, sr_set_encap_source)             \
-_(SR_SET_ENCAP_HOP_LIMIT, sr_set_encap_hop_limit)       \
-_(SR_LOCALSIDS_DUMP, sr_localsids_dump)                 \
-_(SR_POLICIES_DUMP, sr_policies_dump)                   \
-_(SR_STEERING_POL_DUMP, sr_steering_pol_dump)
-
 static void vl_api_sr_localsid_add_del_t_handler
   (vl_api_sr_localsid_add_del_t * mp)
 {
@@ -238,10 +214,10 @@ static void send_sr_localsid_details
 
   rmp = vl_msg_api_alloc (sizeof (*rmp));
   clib_memset (rmp, 0, sizeof (*rmp));
-  rmp->_vl_msg_id = ntohs (VL_API_SR_LOCALSIDS_DETAILS);
+  rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_LOCALSIDS_DETAILS);
   ip6_address_encode (&t->localsid, rmp->addr);
   rmp->end_psp = t->end_psp;
-  rmp->behavior = htons (t->behavior);
+  rmp->behavior = t->behavior;
   rmp->fib_table = htonl (t->fib_table);
   rmp->vlan_index = htonl (t->vlan_index);
   ip_address_encode (&t->next_hop, IP46_TYPE_ANY, &rmp->xconnect_nh_addr);
@@ -272,13 +248,80 @@ static void vl_api_sr_localsids_dump_t_handler
     return;
 
   /* *INDENT-OFF* */
-  pool_foreach (t, sm->localsids,
-  ({
+  pool_foreach (t, sm->localsids)
+   {
     send_sr_localsid_details(t, reg, mp->context);
-  }));
+  }
   /* *INDENT-ON* */
 }
 
+static void
+send_sr_localsid_with_packet_stats_details (int local_sid_index,
+                                           ip6_sr_localsid_t *t,
+                                           vl_api_registration_t *reg,
+                                           u32 context)
+{
+  vl_api_sr_localsids_with_packet_stats_details_t *rmp;
+  vlib_counter_t good_traffic, bad_traffic;
+  ip6_sr_main_t *sm = &sr_main;
+
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  clib_memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id =
+    ntohs (REPLY_MSG_ID_BASE + VL_API_SR_LOCALSIDS_WITH_PACKET_STATS_DETAILS);
+  ip6_address_encode (&t->localsid, rmp->addr);
+  rmp->end_psp = t->end_psp;
+  rmp->behavior = t->behavior;
+  rmp->fib_table = htonl (t->fib_table);
+  rmp->vlan_index = htonl (t->vlan_index);
+  ip_address_encode (&t->next_hop, IP46_TYPE_ANY, &rmp->xconnect_nh_addr);
+
+  if (t->behavior == SR_BEHAVIOR_T || t->behavior == SR_BEHAVIOR_DT6)
+    rmp->xconnect_iface_or_vrf_table =
+      htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP6));
+  else if (t->behavior == SR_BEHAVIOR_DT4)
+    rmp->xconnect_iface_or_vrf_table =
+      htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP4));
+  else
+    rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
+
+  rmp->context = context;
+  vlib_get_combined_counter (&(sm->sr_ls_valid_counters), local_sid_index,
+                            &good_traffic);
+  vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), local_sid_index,
+                            &bad_traffic);
+  rmp->good_traffic_bytes = clib_host_to_net_u64 (good_traffic.bytes);
+  rmp->good_traffic_pkt_count = clib_host_to_net_u64 (good_traffic.packets);
+  rmp->bad_traffic_bytes = clib_host_to_net_u64 (bad_traffic.bytes);
+  rmp->bad_traffic_pkt_count = clib_host_to_net_u64 (bad_traffic.packets);
+  vl_api_send_msg (reg, (u8 *) rmp);
+}
+
+static void
+vl_api_sr_localsids_with_packet_stats_dump_t_handler (
+  vl_api_sr_localsids_with_packet_stats_dump_t *mp)
+{
+  vl_api_registration_t *reg;
+  ip6_sr_main_t *sm = &sr_main;
+  ip6_sr_localsid_t **localsid_list = 0;
+  ip6_sr_localsid_t *t;
+  int i;
+
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+
+  pool_foreach (t, sm->localsids)
+    {
+      vec_add1 (localsid_list, t);
+    }
+  for (i = 0; i < vec_len (localsid_list); i++)
+    {
+      t = localsid_list[i];
+      send_sr_localsid_with_packet_stats_details (i, t, reg, mp->context);
+    }
+}
+
 static void send_sr_policies_details
   (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
 {
@@ -298,7 +341,7 @@ static void send_sr_policies_details
                vec_len (t->segments_lists) *
                sizeof (vl_api_srv6_sid_list_t)));
 
-  rmp->_vl_msg_id = ntohs (VL_API_SR_POLICIES_DETAILS);
+  rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_POLICIES_DETAILS);
   ip6_address_encode (&t->bsid, rmp->bsid);
   rmp->is_encap = t->is_encap;
   rmp->is_spray = t->type;
@@ -314,7 +357,7 @@ static void send_sr_policies_details
 
     api_sid_list->num_sids = vec_len (segment_list->segments);
     api_sid_list->weight = htonl (segment_list->weight);
-
+    slidx = 0;
     vec_foreach (segment, segment_list->segments)
     {
       ip6_address_encode (segment, api_sid_list->sids[slidx++]);
@@ -337,10 +380,79 @@ vl_api_sr_policies_dump_t_handler (vl_api_sr_policies_dump_t * mp)
     return;
 
   /* *INDENT-OFF* */
-  pool_foreach (t, sm->sr_policies,
-  ({
+  pool_foreach (t, sm->sr_policies)
+   {
     send_sr_policies_details(t, reg, mp->context);
-  }));
+  }
+  /* *INDENT-ON* */
+}
+
+
+
+static void send_sr_policies_details_with_sl_index
+  (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
+{
+  vl_api_sr_policies_with_sl_index_details_t *rmp;
+  ip6_sr_main_t *sm = &sr_main;
+
+  u32 *sl_index, slidx = 0;
+  ip6_sr_sl_t *segment_list = 0;
+  ip6_address_t *segment;
+  vl_api_srv6_sid_list_with_sl_index_t *api_sid_list;
+
+  rmp = vl_msg_api_alloc (sizeof (*rmp) +
+                         vec_len (t->segments_lists) *
+                         sizeof (vl_api_srv6_sid_list_with_sl_index_t));
+  clib_memset (rmp, 0,
+              (sizeof (*rmp) +
+               vec_len (t->segments_lists) *
+               sizeof (vl_api_srv6_sid_list_with_sl_index_t)));
+
+  rmp->_vl_msg_id =
+    ntohs (REPLY_MSG_ID_BASE + VL_API_SR_POLICIES_WITH_SL_INDEX_DETAILS);
+  ip6_address_encode (&t->bsid, rmp->bsid);
+  rmp->is_encap = t->is_encap;
+  rmp->is_spray = t->type;
+  rmp->fib_table = htonl (t->fib_table);
+  rmp->num_sid_lists = vec_len (t->segments_lists);
+
+  /* Fill in all the segments lists */
+  vec_foreach (sl_index, t->segments_lists)
+  {
+    segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
+
+    api_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
+    api_sid_list->sl_index = htonl (*sl_index);
+    api_sid_list->num_sids = vec_len (segment_list->segments);
+    api_sid_list->weight = htonl (segment_list->weight);
+    slidx = 0;
+    vec_foreach (segment, segment_list->segments)
+    {
+      ip6_address_encode (segment, api_sid_list->sids[slidx++]);
+    }
+  }
+
+  rmp->context = context;
+  vl_api_send_msg (reg, (u8 *) rmp);
+}
+
+static void
+  vl_api_sr_policies_with_sl_index_dump_t_handler
+  (vl_api_sr_policies_with_sl_index_dump_t * mp)
+{
+  vl_api_registration_t *reg;
+  ip6_sr_main_t *sm = &sr_main;
+  ip6_sr_policy_t *t;
+
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+
+  /* *INDENT-OFF* */
+  pool_foreach (t, sm->sr_policies)
+   {
+    send_sr_policies_details_with_sl_index(t, reg, mp->context);
+  }
   /* *INDENT-ON* */
 }
 
@@ -352,7 +464,7 @@ static void send_sr_steering_pol_details
 
   rmp = vl_msg_api_alloc (sizeof (*rmp));
   clib_memset (rmp, 0, sizeof (*rmp));
-  rmp->_vl_msg_id = ntohs (VL_API_SR_STEERING_POL_DETAILS);
+  rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_STEERING_POL_DETAILS);
 
   //Get the SR policy BSID
   ip6_sr_policy_t *p;
@@ -384,51 +496,21 @@ static void vl_api_sr_steering_pol_dump_t_handler
     return;
 
   /* *INDENT-OFF* */
-  pool_foreach (t, sm->steer_policies,
-  ({
+  pool_foreach (t, sm->steer_policies)
+   {
     send_sr_steering_pol_details(t, reg, mp->context);
-  }));
+  }
   /* *INDENT-ON* */
 }
 
-/*
- * sr_api_hookup
- * Add vpe's API message handlers to the table.
- * vlib has already mapped shared memory and
- * added the client registration handlers.
- * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
- */
-#define vl_msg_name_crc_list
-#include <vnet/vnet_all_api_h.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_sr;
-#undef _
-}
-
+#include <vnet/srv6/sr.api.c>
 static clib_error_t *
 sr_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_vpe_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 ();
 
   return 0;
 }