+typedef struct udp_encap_stat_t_
+{
+ u32 ue_id;
+ u64 stats[2];
+} udp_encap_stat_t;
+
+typedef struct udp_encap_stats_walk_t_
+{
+ udp_encap_stat_t *stats;
+} udp_encap_stats_walk_t;
+
+static int
+udp_encap_stats_walk_cb (index_t uei, void *arg)
+{
+ udp_encap_stats_walk_t *ctx = arg;
+ udp_encap_stat_t *stat;
+ udp_encap_t *ue;
+
+ ue = udp_encap_get (uei);
+ vec_add2 (ctx->stats, stat, 1);
+
+ stat->ue_id = uei;
+ udp_encap_get_stats (ue->ue_id, &stat->stats[0], &stat->stats[1]);
+
+ return (1);
+}
+
+static void
+udp_encap_ship (udp_encap_stats_walk_t * ctx)
+{
+ vl_api_vnet_udp_encap_counters_t *mp;
+ vl_shmem_hdr_t *shmem_hdr;
+ stats_main_t *sm;
+ api_main_t *am;
+ svm_queue_t *q;
+
+ mp = NULL;
+ sm = &stats_main;
+ am = sm->api_main;
+ shmem_hdr = am->shmem_hdr;
+ q = shmem_hdr->vl_input_queue;
+
+ /*
+ * If the walk context has counters, which may be left over from the last
+ * suspend, then we continue from there.
+ */
+ while (0 != vec_len (ctx->stats))
+ {
+ u32 n_items = MIN (vec_len (ctx->stats),
+ UDP_ENCAP_COUNTER_BATCH_SIZE);
+ u8 pause = 0;
+
+ dslock (sm, 0 /* release hint */ , 1 /* tag */ );
+
+ mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) +
+ (n_items *
+ sizeof
+ (vl_api_udp_encap_counter_t)));
+ mp->_vl_msg_id = ntohs (VL_API_VNET_UDP_ENCAP_COUNTERS);
+ mp->count = ntohl (n_items);
+
+ /*
+ * copy the counters from the back of the context, then we can easily
+ * 'erase' them by resetting the vector length.
+ * The order we push the stats to the caller is not important.
+ */
+ clib_memcpy (mp->c,
+ &ctx->stats[vec_len (ctx->stats) - n_items],
+ n_items * sizeof (*ctx->stats));
+
+ _vec_len (ctx->stats) = vec_len (ctx->stats) - n_items;
+
+ /*
+ * send to the shm q
+ */
+ svm_queue_lock (q);
+ pause = svm_queue_is_full (q);
+
+ vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
+ svm_queue_unlock (q);
+ dsunlock (sm);
+
+ if (pause)
+ ip46_fib_stats_delay (sm, 0 /* sec */ ,
+ STATS_RELEASE_DELAY_NS);
+ }
+}
+
+static void
+do_udp_encap_counters (stats_main_t * sm)
+{
+ udp_encap_stat_t *stat;
+
+ udp_encap_stats_walk_t ctx = {
+ .stats = NULL,
+ };
+
+ dslock (sm, 0 /* release hint */ , 1 /* tag */ );
+ udp_encap_walk (udp_encap_stats_walk_cb, &ctx);
+ dsunlock (sm);
+
+ udp_encap_ship (&ctx);
+}
+
+int
+stats_set_poller_delay (u32 poller_delay_sec)
+{
+ stats_main_t *sm = &stats_main;
+ if (!poller_delay_sec)
+ {
+ return VNET_API_ERROR_INVALID_ARGUMENT;
+ }
+ else
+ {
+ sm->stats_poll_interval_in_seconds = poller_delay_sec;
+ return 0;
+ }
+}
+
+/*
+ * Accept connection on the socket and exchange the fd for the shared
+ * memory segment.
+ */
+static clib_error_t *
+stats_socket_accept_ready (clib_file_t * uf)
+{
+ stats_main_t *sm = &stats_main;
+ ssvm_private_t *ssvmp = &sm->stat_segment;
+ clib_error_t *err;
+ clib_socket_t client = { 0 };
+
+ err = clib_socket_accept (sm->socket, &client);
+ if (err)
+ {
+ clib_error_report (err);
+ return err;
+ }
+
+ /* Send the fd across and close */
+ err = clib_socket_sendmsg (&client, 0, 0, &ssvmp->fd, 1);
+ if (err)
+ clib_error_report (err);
+ clib_socket_close (&client);
+
+ return 0;
+}
+
+static void
+stats_segment_socket_init (void)
+{
+ stats_main_t *sm = &stats_main;
+ clib_error_t *error;
+ clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
+
+ s->config = (char *) sm->socket_name;
+ s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
+ CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
+ if ((error = clib_socket_init (s)))
+ {
+ clib_error_report (error);
+ return;
+ }
+
+ clib_file_t template = { 0 };
+ clib_file_main_t *fm = &file_main;
+ template.read_function = stats_socket_accept_ready;
+ template.file_descriptor = s->fd;
+ template.description =
+ format (0, "stats segment listener %s", STAT_SEGMENT_SOCKET_FILE);
+ clib_file_add (fm, &template);
+
+ sm->socket = s;
+}
+
+static clib_error_t *
+stats_config (vlib_main_t * vm, unformat_input_t * input)
+{
+ stats_main_t *sm = &stats_main;
+ u32 sec;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "socket-name %s", &sm->socket_name))
+ ;
+ else if (unformat (input, "default"))
+ sm->socket_name = format (0, "%s", STAT_SEGMENT_SOCKET_FILE);
+ else if (unformat (input, "interval %u", &sec))
+ {
+ int rv = stats_set_poller_delay (sec);
+ if (rv)
+ {
+ return clib_error_return (0,
+ "`stats_set_poller_delay' API call failed, rv=%d:%U",
+ (int) rv, format_vnet_api_errno, rv);
+ }
+ }
+ else
+ {
+ return clib_error_return (0, "unknown input '%U'",
+ format_unformat_error, input);
+ }
+ }
+
+ if (sm->socket_name)
+ stats_segment_socket_init ();
+
+ return 0;
+}
+
+/* stats { ... } configuration. */
+/*?
+ *
+ * @cfgcmd{interval, <seconds>}
+ * Configure stats poller delay to be @c seconds.
+ *
+?*/
+VLIB_CONFIG_FUNCTION (stats_config, "stats");
+
+static void
+ vl_api_stats_get_poller_delay_t_handler
+ (vl_api_stats_get_poller_delay_t * mp)
+{
+ stats_main_t *sm = &stats_main;
+ vl_api_registration_t *reg;
+ reg = vl_api_client_index_to_registration (mp->client_index);
+ if (!reg)
+ return;
+ vl_api_stats_get_poller_delay_reply_t *rmp;
+
+ rmp = vl_msg_api_alloc (sizeof (*rmp));
+ rmp->_vl_msg_id = ntohs (VL_API_WANT_PER_INTERFACE_SIMPLE_STATS_REPLY);
+ rmp->context = mp->context;
+ rmp->retval = 0;
+ rmp->delay = clib_host_to_net_u32 (sm->stats_poll_interval_in_seconds);
+
+ vl_api_send_msg (reg, (u8 *) rmp);
+
+}
+