Allow individual stats API and introduce stats.api
[vpp.git] / src / vpp / stats / stats.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vpp/stats/stats.h>
16 #include <signal.h>
17 #include <vnet/fib/ip4_fib.h>
18 #include <vnet/fib/fib_entry.h>
19 #include <vnet/dpo/load_balance.h>
20
21 #define STATS_DEBUG 0
22
23 stats_main_t stats_main;
24
25 #include <vnet/ip/ip.h>
26
27 #include <vpp/api/vpe_msg_enum.h>
28
29 #define f64_endian(a)
30 #define f64_print(a,b)
31
32 #define vl_typedefs             /* define message structures */
33 #include <vpp/api/vpe_all_api_h.h>
34 #undef vl_typedefs
35
36 #define vl_endianfun            /* define message structures */
37 #include <vpp/api/vpe_all_api_h.h>
38 #undef vl_endianfun
39
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #define vl_printfun
43 #include <vpp/api/vpe_all_api_h.h>
44 #undef vl_printfun
45
46 #define foreach_stats_msg                                               \
47 _(WANT_STATS, want_stats)                                               \
48 _(VNET_INTERFACE_SIMPLE_COUNTERS, vnet_interface_simple_counters)       \
49 _(WANT_INTERFACE_SIMPLE_STATS, want_interface_simple_stats)     \
50 _(VNET_INTERFACE_COMBINED_COUNTERS, vnet_interface_combined_counters)   \
51 _(WANT_INTERFACE_COMBINED_STATS, want_interface_combined_stats) \
52 _(VNET_IP4_FIB_COUNTERS, vnet_ip4_fib_counters)                         \
53 _(WANT_IP4_FIB_STATS, want_ip4_fib_stats)            \
54 _(VNET_IP6_FIB_COUNTERS, vnet_ip6_fib_counters)                         \
55 _(WANT_IP6_FIB_STATS, want_ip6_fib_stats)        \
56 _(VNET_IP4_NBR_COUNTERS, vnet_ip4_nbr_counters)                         \
57 _(WANT_IP4_NBR_STATS, want_ip4_nbr_stats)            \
58 _(VNET_IP6_NBR_COUNTERS, vnet_ip6_nbr_counters) \
59 _(WANT_IP6_NBR_STATS, want_ip6_nbr_stats) \
60 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)
61
62
63 /* These constants ensure msg sizes <= 1024, aka ring allocation */
64 #define SIMPLE_COUNTER_BATCH_SIZE       126
65 #define COMBINED_COUNTER_BATCH_SIZE     63
66 #define IP4_FIB_COUNTER_BATCH_SIZE      48
67 #define IP6_FIB_COUNTER_BATCH_SIZE      30
68
69 /* 5ms */
70 #define STATS_RELEASE_DELAY_NS (1000 * 1000 * 5)
71 /*                              ns/us  us/ms        */
72
73 u8 *
74 format_vnet_interface_combined_counters (u8 * s, va_list * args)
75 {
76   stats_main_t *sm = &stats_main;
77   vl_api_vnet_interface_combined_counters_t *mp =
78     va_arg (*args, vl_api_vnet_interface_combined_counters_t *);
79
80   char *counter_name;
81   u32 count, sw_if_index;
82   int i;
83   count = ntohl (mp->count);
84   sw_if_index = ntohl (mp->first_sw_if_index);
85
86   vlib_counter_t *vp;
87   u64 packets, bytes;
88   vp = (vlib_counter_t *) mp->data;
89
90   switch (mp->vnet_counter_type)
91     {
92     case VNET_INTERFACE_COUNTER_RX:
93       counter_name = "rx";
94       break;
95     case VNET_INTERFACE_COUNTER_TX:
96       counter_name = "tx";
97       break;
98     default:
99       counter_name = "bogus";
100       break;
101     }
102   for (i = 0; i < count; i++)
103     {
104       packets = clib_mem_unaligned (&vp->packets, u64);
105       packets = clib_net_to_host_u64 (packets);
106       bytes = clib_mem_unaligned (&vp->bytes, u64);
107       bytes = clib_net_to_host_u64 (bytes);
108       vp++;
109       s = format (s, "%U.%s.packets %lld\n",
110                   format_vnet_sw_if_index_name,
111                   sm->vnet_main, sw_if_index, counter_name, packets);
112       s = format (s, "%U.%s.bytes %lld\n",
113                   format_vnet_sw_if_index_name,
114                   sm->vnet_main, sw_if_index, counter_name, bytes);
115       sw_if_index++;
116     }
117   return s;
118 }
119
120 u8 *
121 format_vnet_interface_simple_counters (u8 * s, va_list * args)
122 {
123   stats_main_t *sm = &stats_main;
124   vl_api_vnet_interface_simple_counters_t *mp =
125     va_arg (*args, vl_api_vnet_interface_simple_counters_t *);
126   char *counter_name;
127   u32 count, sw_if_index;
128   count = ntohl (mp->count);
129   sw_if_index = ntohl (mp->first_sw_if_index);
130   u64 *vp, v;
131   vp = (u64 *) mp->data;
132   int i;
133
134   switch (mp->vnet_counter_type)
135     {
136     case VNET_INTERFACE_COUNTER_DROP:
137       counter_name = "drop";
138       break;
139     case VNET_INTERFACE_COUNTER_PUNT:
140       counter_name = "punt";
141       break;
142     case VNET_INTERFACE_COUNTER_IP4:
143       counter_name = "ip4";
144       break;
145     case VNET_INTERFACE_COUNTER_IP6:
146       counter_name = "ip6";
147       break;
148     case VNET_INTERFACE_COUNTER_RX_NO_BUF:
149       counter_name = "rx-no-buff";
150       break;
151     case VNET_INTERFACE_COUNTER_RX_MISS:
152       counter_name = "rx-miss";
153       break;
154     case VNET_INTERFACE_COUNTER_RX_ERROR:
155       counter_name = "rx-error (fifo-full)";
156       break;
157     case VNET_INTERFACE_COUNTER_TX_ERROR:
158       counter_name = "tx-error (fifo-full)";
159       break;
160     default:
161       counter_name = "bogus";
162       break;
163     }
164   for (i = 0; i < count; i++)
165     {
166       v = clib_mem_unaligned (vp, u64);
167       v = clib_net_to_host_u64 (v);
168       vp++;
169       s = format (s, "%U.%s %lld\n", format_vnet_sw_if_index_name,
170                   sm->vnet_main, sw_if_index, counter_name, v);
171       sw_if_index++;
172     }
173
174   return s;
175 }
176
177 void
178 dslock (stats_main_t * sm, int release_hint, int tag)
179 {
180   u32 thread_index;
181   data_structure_lock_t *l = sm->data_structure_lock;
182
183   if (PREDICT_FALSE (l == 0))
184     return;
185
186   thread_index = vlib_get_thread_index ();
187   if (l->lock && l->thread_index == thread_index)
188     {
189       l->count++;
190       return;
191     }
192
193   if (release_hint)
194     l->release_hint++;
195
196   while (__sync_lock_test_and_set (&l->lock, 1))
197     /* zzzz */ ;
198   l->tag = tag;
199   l->thread_index = thread_index;
200   l->count = 1;
201 }
202
203 void
204 stats_dslock_with_hint (int hint, int tag)
205 {
206   stats_main_t *sm = &stats_main;
207   dslock (sm, hint, tag);
208 }
209
210 void
211 dsunlock (stats_main_t * sm)
212 {
213   u32 thread_index;
214   data_structure_lock_t *l = sm->data_structure_lock;
215
216   if (PREDICT_FALSE (l == 0))
217     return;
218
219   thread_index = vlib_get_thread_index ();
220   ASSERT (l->lock && l->thread_index == thread_index);
221   l->count--;
222   if (l->count == 0)
223     {
224       l->tag = -l->tag;
225       l->release_hint = 0;
226       CLIB_MEMORY_BARRIER ();
227       l->lock = 0;
228     }
229 }
230
231 void
232 stats_dsunlock (int hint, int tag)
233 {
234   stats_main_t *sm = &stats_main;
235   dsunlock (sm);
236 }
237
238 static void
239 do_simple_interface_counters (stats_main_t * sm)
240 {
241   vl_api_vnet_interface_simple_counters_t *mp = 0;
242   vnet_interface_main_t *im = sm->interface_main;
243   api_main_t *am = sm->api_main;
244   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
245   unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
246   vlib_simple_counter_main_t *cm;
247   u32 items_this_message = 0;
248   u64 v, *vp = 0;
249   int i, n_counts;
250
251   /*
252    * Prevent interface registration from expanding / moving the vectors...
253    * That tends never to happen, so we can hold this lock for a while.
254    */
255   vnet_interface_counter_lock (im);
256
257   vec_foreach (cm, im->sw_if_counters)
258   {
259     n_counts = vlib_simple_counter_n_counters (cm);
260     for (i = 0; i < n_counts; i++)
261       {
262         if (mp == 0)
263           {
264             items_this_message = clib_min (SIMPLE_COUNTER_BATCH_SIZE,
265                                            n_counts - i);
266
267             mp = vl_msg_api_alloc_as_if_client
268               (sizeof (*mp) + items_this_message * sizeof (v));
269             mp->_vl_msg_id = ntohs (VL_API_VNET_INTERFACE_SIMPLE_COUNTERS);
270             mp->vnet_counter_type = cm - im->sw_if_counters;
271             mp->first_sw_if_index = htonl (i);
272             mp->count = 0;
273             vp = (u64 *) mp->data;
274           }
275         v = vlib_get_simple_counter (cm, i);
276         clib_mem_unaligned (vp, u64) = clib_host_to_net_u64 (v);
277         vp++;
278         mp->count++;
279         if (mp->count == items_this_message)
280           {
281             mp->count = htonl (items_this_message);
282             /* Send to the main thread... */
283             vl_msg_api_send_shmem (q, (u8 *) & mp);
284             mp = 0;
285           }
286       }
287     ASSERT (mp == 0);
288   }
289   vnet_interface_counter_unlock (im);
290 }
291
292 static void
293 do_combined_interface_counters (stats_main_t * sm)
294 {
295   vl_api_vnet_interface_combined_counters_t *mp = 0;
296   vnet_interface_main_t *im = sm->interface_main;
297   api_main_t *am = sm->api_main;
298   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
299   unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
300   vlib_combined_counter_main_t *cm;
301   u32 items_this_message = 0;
302   vlib_counter_t v, *vp = 0;
303   int i, n_counts;
304
305   vnet_interface_counter_lock (im);
306
307   vec_foreach (cm, im->combined_sw_if_counters)
308   {
309     n_counts = vlib_combined_counter_n_counters (cm);
310     for (i = 0; i < n_counts; i++)
311       {
312         if (mp == 0)
313           {
314             items_this_message = clib_min (COMBINED_COUNTER_BATCH_SIZE,
315                                            n_counts - i);
316
317             mp = vl_msg_api_alloc_as_if_client
318               (sizeof (*mp) + items_this_message * sizeof (v));
319             mp->_vl_msg_id = ntohs (VL_API_VNET_INTERFACE_COMBINED_COUNTERS);
320             mp->vnet_counter_type = cm - im->combined_sw_if_counters;
321             mp->first_sw_if_index = htonl (i);
322             mp->count = 0;
323             vp = (vlib_counter_t *) mp->data;
324           }
325         vlib_get_combined_counter (cm, i, &v);
326         clib_mem_unaligned (&vp->packets, u64)
327           = clib_host_to_net_u64 (v.packets);
328         clib_mem_unaligned (&vp->bytes, u64) = clib_host_to_net_u64 (v.bytes);
329         vp++;
330         mp->count++;
331         if (mp->count == items_this_message)
332           {
333             mp->count = htonl (items_this_message);
334             /* Send to the main thread... */
335             vl_msg_api_send_shmem (q, (u8 *) & mp);
336             mp = 0;
337           }
338       }
339     ASSERT (mp == 0);
340   }
341   vnet_interface_counter_unlock (im);
342 }
343
344 static void
345 ip46_fib_stats_delay (stats_main_t * sm, u32 sec, u32 nsec)
346 {
347   struct timespec _req, *req = &_req;
348   struct timespec _rem, *rem = &_rem;
349
350   req->tv_sec = sec;
351   req->tv_nsec = nsec;
352   while (1)
353     {
354       if (nanosleep (req, rem) == 0)
355         break;
356       *req = *rem;
357       if (errno == EINTR)
358         continue;
359       clib_unix_warning ("nanosleep");
360       break;
361     }
362 }
363
364 /**
365  * @brief The context passed when collecting adjacency counters
366  */
367 typedef struct ip4_nbr_stats_ctx_t_
368 {
369   /**
370    * The SW IF index all these adjs belong to
371    */
372   u32 sw_if_index;
373
374   /**
375    * A vector of ip4 nbr counters
376    */
377   vl_api_ip4_nbr_counter_t *counters;
378 } ip4_nbr_stats_ctx_t;
379
380 static adj_walk_rc_t
381 ip4_nbr_stats_cb (adj_index_t ai, void *arg)
382 {
383   vl_api_ip4_nbr_counter_t *vl_counter;
384   vlib_counter_t adj_counter;
385   ip4_nbr_stats_ctx_t *ctx;
386   ip_adjacency_t *adj;
387
388   ctx = arg;
389   vlib_get_combined_counter (&adjacency_counters, ai, &adj_counter);
390
391   if (0 != adj_counter.packets)
392     {
393       vec_add2 (ctx->counters, vl_counter, 1);
394       adj = adj_get (ai);
395
396       vl_counter->packets = clib_host_to_net_u64 (adj_counter.packets);
397       vl_counter->bytes = clib_host_to_net_u64 (adj_counter.bytes);
398       vl_counter->address = adj->sub_type.nbr.next_hop.ip4.as_u32;
399       vl_counter->link_type = adj->ia_link;
400     }
401   return (ADJ_WALK_RC_CONTINUE);
402 }
403
404 #define MIN(x,y) (((x)<(y))?(x):(y))
405
406 static void
407 ip4_nbr_ship (stats_main_t * sm, ip4_nbr_stats_ctx_t * ctx)
408 {
409   api_main_t *am = sm->api_main;
410   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
411   unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
412   vl_api_vnet_ip4_nbr_counters_t *mp = 0;
413   int first = 0;
414
415   /*
416    * If the walk context has counters, which may be left over from the last
417    * suspend, then we continue from there.
418    */
419   while (0 != vec_len (ctx->counters))
420     {
421       u32 n_items = MIN (vec_len (ctx->counters),
422                          IP4_FIB_COUNTER_BATCH_SIZE);
423       u8 pause = 0;
424
425       dslock (sm, 0 /* release hint */ , 1 /* tag */ );
426
427       mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) +
428                                           (n_items *
429                                            sizeof
430                                            (vl_api_ip4_nbr_counter_t)));
431       mp->_vl_msg_id = ntohs (VL_API_VNET_IP4_NBR_COUNTERS);
432       mp->count = ntohl (n_items);
433       mp->sw_if_index = ntohl (ctx->sw_if_index);
434       mp->begin = first;
435       first = 0;
436
437       /*
438        * copy the counters from the back of the context, then we can easily
439        * 'erase' them by resetting the vector length.
440        * The order we push the stats to the caller is not important.
441        */
442       clib_memcpy (mp->c,
443                    &ctx->counters[vec_len (ctx->counters) - n_items],
444                    n_items * sizeof (*ctx->counters));
445
446       _vec_len (ctx->counters) = vec_len (ctx->counters) - n_items;
447
448       /*
449        * send to the shm q
450        */
451       unix_shared_memory_queue_lock (q);
452       pause = unix_shared_memory_queue_is_full (q);
453
454       vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
455       unix_shared_memory_queue_unlock (q);
456       dsunlock (sm);
457
458       if (pause)
459         ip46_fib_stats_delay (sm, 0 /* sec */ ,
460                               STATS_RELEASE_DELAY_NS);
461     }
462 }
463
464 static void
465 do_ip4_nbrs (stats_main_t * sm)
466 {
467   vnet_main_t *vnm = vnet_get_main ();
468   vnet_interface_main_t *im = &vnm->interface_main;
469   vnet_sw_interface_t *si;
470
471   ip4_nbr_stats_ctx_t ctx = {
472     .sw_if_index = 0,
473     .counters = NULL,
474   };
475
476   /* *INDENT-OFF* */
477   pool_foreach (si, im->sw_interfaces,
478   ({
479     /*
480      * update the interface we are now concerned with
481      */
482     ctx.sw_if_index = si->sw_if_index;
483
484     /*
485      * we are about to walk another interface, so we shouldn't have any pending
486      * stats to export.
487      */
488     ASSERT(ctx.counters == NULL);
489
490     /*
491      * visit each neighbour adjacency on the interface and collect
492      * its current stats.
493      * Because we hold the lock the walk is synchronous, so safe to routing
494      * updates. It's limited in work by the number of adjacenies on an
495      * interface, which is typically not huge.
496      */
497     dslock (sm, 0 /* release hint */ , 1 /* tag */ );
498     adj_nbr_walk (si->sw_if_index,
499                   FIB_PROTOCOL_IP4,
500                   ip4_nbr_stats_cb,
501                   &ctx);
502     dsunlock (sm);
503
504     /*
505      * if this interface has some adjacencies with counters then ship them,
506      * else continue to the next interface.
507      */
508     if (NULL != ctx.counters)
509       {
510         ip4_nbr_ship(sm, &ctx);
511       }
512   }));
513   /* *INDENT-OFF* */
514 }
515
516 /**
517  * @brief The context passed when collecting adjacency counters
518  */
519 typedef struct ip6_nbr_stats_ctx_t_
520 {
521   /**
522    * The SW IF index all these adjs belong to
523    */
524   u32 sw_if_index;
525
526   /**
527    * A vector of ip6 nbr counters
528    */
529   vl_api_ip6_nbr_counter_t *counters;
530 } ip6_nbr_stats_ctx_t;
531
532 static adj_walk_rc_t
533 ip6_nbr_stats_cb (adj_index_t ai,
534                   void *arg)
535 {
536   vl_api_ip6_nbr_counter_t *vl_counter;
537   vlib_counter_t adj_counter;
538   ip6_nbr_stats_ctx_t *ctx;
539   ip_adjacency_t *adj;
540
541   ctx = arg;
542   vlib_get_combined_counter(&adjacency_counters, ai, &adj_counter);
543
544   if (0 != adj_counter.packets)
545     {
546       vec_add2(ctx->counters, vl_counter, 1);
547       adj = adj_get(ai);
548
549       vl_counter->packets = clib_host_to_net_u64(adj_counter.packets);
550       vl_counter->bytes   = clib_host_to_net_u64(adj_counter.bytes);
551       vl_counter->address[0] = adj->sub_type.nbr.next_hop.ip6.as_u64[0];
552       vl_counter->address[1] = adj->sub_type.nbr.next_hop.ip6.as_u64[1];
553       vl_counter->link_type = adj->ia_link;
554     }
555   return (ADJ_WALK_RC_CONTINUE);
556 }
557
558 #define MIN(x,y) (((x)<(y))?(x):(y))
559
560 static void
561 ip6_nbr_ship (stats_main_t * sm,
562               ip6_nbr_stats_ctx_t *ctx)
563 {
564   api_main_t *am = sm->api_main;
565   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
566   unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
567   vl_api_vnet_ip6_nbr_counters_t *mp = 0;
568   int first = 0;
569
570   /*
571    * If the walk context has counters, which may be left over from the last
572    * suspend, then we continue from there.
573    */
574   while (0 != vec_len(ctx->counters))
575     {
576       u32 n_items = MIN (vec_len (ctx->counters),
577                          IP6_FIB_COUNTER_BATCH_SIZE);
578       u8 pause = 0;
579
580       dslock (sm, 0 /* release hint */ , 1 /* tag */ );
581
582       mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) +
583                                           (n_items *
584                                            sizeof
585                                            (vl_api_ip6_nbr_counter_t)));
586       mp->_vl_msg_id = ntohs (VL_API_VNET_IP6_NBR_COUNTERS);
587       mp->count = ntohl (n_items);
588       mp->sw_if_index = ntohl (ctx->sw_if_index);
589       mp->begin = first;
590       first = 0;
591
592       /*
593        * copy the counters from the back of the context, then we can easily
594        * 'erase' them by resetting the vector length.
595        * The order we push the stats to the caller is not important.
596        */
597       clib_memcpy (mp->c,
598                    &ctx->counters[vec_len (ctx->counters) - n_items],
599                    n_items * sizeof (*ctx->counters));
600
601       _vec_len (ctx->counters) = vec_len (ctx->counters) - n_items;
602
603       /*
604        * send to the shm q
605        */
606       unix_shared_memory_queue_lock (q);
607       pause = unix_shared_memory_queue_is_full (q);
608
609       vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
610       unix_shared_memory_queue_unlock (q);
611       dsunlock (sm);
612
613       if (pause)
614         ip46_fib_stats_delay (sm, 0 /* sec */ ,
615                               STATS_RELEASE_DELAY_NS);
616     }
617 }
618
619 static void
620 do_ip6_nbrs (stats_main_t * sm)
621 {
622   vnet_main_t *vnm = vnet_get_main ();
623   vnet_interface_main_t *im = &vnm->interface_main;
624   vnet_sw_interface_t *si;
625
626   ip6_nbr_stats_ctx_t ctx = {
627     .sw_if_index = 0,
628     .counters = NULL,
629   };
630
631   /* *INDENT-OFF* */
632   pool_foreach (si, im->sw_interfaces,
633   ({
634     /*
635      * update the interface we are now concerned with
636      */
637     ctx.sw_if_index = si->sw_if_index;
638
639     /*
640      * we are about to walk another interface, so we shouldn't have any pending
641      * stats to export.
642      */
643     ASSERT(ctx.counters == NULL);
644
645     /*
646      * visit each neighbour adjacency on the interface and collect
647      * its current stats.
648      * Because we hold the lock the walk is synchronous, so safe to routing
649      * updates. It's limited in work by the number of adjacenies on an
650      * interface, which is typically not huge.
651      */
652     dslock (sm, 0 /* release hint */ , 1 /* tag */ );
653     adj_nbr_walk (si->sw_if_index,
654                   FIB_PROTOCOL_IP6,
655                   ip6_nbr_stats_cb,
656                   &ctx);
657     dsunlock (sm);
658
659     /*
660      * if this interface has some adjacencies with counters then ship them,
661      * else continue to the next interface.
662      */
663     if (NULL != ctx.counters)
664       {
665         ip6_nbr_ship(sm, &ctx);
666       }
667   }));
668   /* *INDENT-OFF* */
669 }
670
671 static void
672 do_ip4_fibs (stats_main_t * sm)
673 {
674   ip4_main_t *im4 = &ip4_main;
675   api_main_t *am = sm->api_main;
676   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
677   unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
678   ip4_route_t *r;
679   fib_table_t *fib;
680   ip4_fib_t *v4_fib;
681   do_ip46_fibs_t *do_fibs;
682   vl_api_vnet_ip4_fib_counters_t *mp = 0;
683   u32 items_this_message;
684   vl_api_ip4_fib_counter_t *ctrp = 0;
685   u32 start_at_fib_index = 0;
686   int i, j, k;
687
688   do_fibs = &sm->do_ip46_fibs;
689
690 again:
691   vec_reset_length (do_fibs->fibs);
692   /* *INDENT-OFF* */
693   pool_foreach (fib, im4->fibs,
694                 ({vec_add1(do_fibs->fibs,fib);}));
695
696   /* *INDENT-ON* */
697
698   for (j = 0; j < vec_len (do_fibs->fibs); j++)
699     {
700       fib = do_fibs->fibs[j];
701       /* We may have bailed out due to control-plane activity */
702       while ((fib - im4->fibs) < start_at_fib_index)
703         continue;
704
705       v4_fib = pool_elt_at_index (im4->v4_fibs, fib->ft_index);
706
707       if (mp == 0)
708         {
709           items_this_message = IP4_FIB_COUNTER_BATCH_SIZE;
710           mp = vl_msg_api_alloc_as_if_client
711             (sizeof (*mp) +
712              items_this_message * sizeof (vl_api_ip4_fib_counter_t));
713           mp->_vl_msg_id = ntohs (VL_API_VNET_IP4_FIB_COUNTERS);
714           mp->count = 0;
715           mp->vrf_id = ntohl (fib->ft_table_id);
716           ctrp = (vl_api_ip4_fib_counter_t *) mp->c;
717         }
718       else
719         {
720           /* happens if the last FIB was empty... */
721           ASSERT (mp->count == 0);
722           mp->vrf_id = ntohl (fib->ft_table_id);
723         }
724
725       dslock (sm, 0 /* release hint */ , 1 /* tag */ );
726
727       vec_reset_length (do_fibs->ip4routes);
728       vec_reset_length (do_fibs->results);
729
730       for (i = 0; i < ARRAY_LEN (v4_fib->fib_entry_by_dst_address); i++)
731         {
732           uword *hash = v4_fib->fib_entry_by_dst_address[i];
733           hash_pair_t *p;
734           ip4_route_t x;
735
736           vec_reset_length (do_fibs->pvec);
737
738           x.address_length = i;
739
740           hash_foreach_pair (p, hash, (
741                                         {
742                                         vec_add1 (do_fibs->pvec, p);}
743                              ));
744           for (k = 0; k < vec_len (do_fibs->pvec); k++)
745             {
746               p = do_fibs->pvec[k];
747               x.address.data_u32 = p->key;
748               x.index = p->value[0];
749
750               vec_add1 (do_fibs->ip4routes, x);
751               if (sm->data_structure_lock->release_hint)
752                 {
753                   start_at_fib_index = fib - im4->fibs;
754                   dsunlock (sm);
755                   ip46_fib_stats_delay (sm, 0 /* sec */ ,
756                                         STATS_RELEASE_DELAY_NS);
757                   mp->count = 0;
758                   ctrp = (vl_api_ip4_fib_counter_t *) mp->c;
759                   goto again;
760                 }
761             }
762         }
763
764       vec_foreach (r, do_fibs->ip4routes)
765       {
766         vlib_counter_t c;
767         const dpo_id_t *dpo_id;
768         u32 index;
769
770         dpo_id = fib_entry_contribute_ip_forwarding (r->index);
771         index = (u32) dpo_id->dpoi_index;
772
773         vlib_get_combined_counter (&load_balance_main.lbm_to_counters,
774                                    index, &c);
775         /*
776          * If it has actually
777          * seen at least one packet, send it.
778          */
779         if (c.packets > 0)
780           {
781
782             /* already in net byte order */
783             ctrp->address = r->address.as_u32;
784             ctrp->address_length = r->address_length;
785             ctrp->packets = clib_host_to_net_u64 (c.packets);
786             ctrp->bytes = clib_host_to_net_u64 (c.bytes);
787             mp->count++;
788             ctrp++;
789
790             if (mp->count == items_this_message)
791               {
792                 mp->count = htonl (items_this_message);
793                 /*
794                  * If the main thread's input queue is stuffed,
795                  * drop the data structure lock (which the main thread
796                  * may want), and take a pause.
797                  */
798                 unix_shared_memory_queue_lock (q);
799                 if (unix_shared_memory_queue_is_full (q))
800                   {
801                     dsunlock (sm);
802                     vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
803                     unix_shared_memory_queue_unlock (q);
804                     mp = 0;
805                     ip46_fib_stats_delay (sm, 0 /* sec */ ,
806                                           STATS_RELEASE_DELAY_NS);
807                     goto again;
808                   }
809                 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
810                 unix_shared_memory_queue_unlock (q);
811
812                 items_this_message = IP4_FIB_COUNTER_BATCH_SIZE;
813                 mp = vl_msg_api_alloc_as_if_client
814                   (sizeof (*mp) +
815                    items_this_message * sizeof (vl_api_ip4_fib_counter_t));
816                 mp->_vl_msg_id = ntohs (VL_API_VNET_IP4_FIB_COUNTERS);
817                 mp->count = 0;
818                 mp->vrf_id = ntohl (fib->ft_table_id);
819                 ctrp = (vl_api_ip4_fib_counter_t *) mp->c;
820               }
821           }                     /* for each (mp or single) adj */
822         if (sm->data_structure_lock->release_hint)
823           {
824             start_at_fib_index = fib - im4->fibs;
825             dsunlock (sm);
826             ip46_fib_stats_delay (sm, 0 /* sec */ , STATS_RELEASE_DELAY_NS);
827             mp->count = 0;
828             ctrp = (vl_api_ip4_fib_counter_t *) mp->c;
829             goto again;
830           }
831       }                         /* vec_foreach (routes) */
832
833       dsunlock (sm);
834
835       /* Flush any data from this fib */
836       if (mp->count)
837         {
838           mp->count = htonl (mp->count);
839           vl_msg_api_send_shmem (q, (u8 *) & mp);
840           mp = 0;
841         }
842     }
843
844   /* If e.g. the last FIB had no reportable routes, free the buffer */
845   if (mp)
846     vl_msg_api_free (mp);
847 }
848
849 typedef struct
850 {
851   u32 fib_index;
852   ip6_route_t **routep;
853   stats_main_t *sm;
854 } add_routes_in_fib_arg_t;
855
856 static void
857 add_routes_in_fib (BVT (clib_bihash_kv) * kvp, void *arg)
858 {
859   add_routes_in_fib_arg_t *ap = arg;
860   stats_main_t *sm = ap->sm;
861
862   if (sm->data_structure_lock->release_hint)
863     clib_longjmp (&sm->jmp_buf, 1);
864
865   if (kvp->key[2] >> 32 == ap->fib_index)
866     {
867       ip6_address_t *addr;
868       ip6_route_t *r;
869       addr = (ip6_address_t *) kvp;
870       vec_add2 (*ap->routep, r, 1);
871       r->address = addr[0];
872       r->address_length = kvp->key[2] & 0xFF;
873       r->index = kvp->value;
874     }
875 }
876
877 static void
878 do_ip6_fibs (stats_main_t * sm)
879 {
880   ip6_main_t *im6 = &ip6_main;
881   api_main_t *am = sm->api_main;
882   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
883   unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
884   ip6_route_t *r;
885   fib_table_t *fib;
886   do_ip46_fibs_t *do_fibs;
887   vl_api_vnet_ip6_fib_counters_t *mp = 0;
888   u32 items_this_message;
889   vl_api_ip6_fib_counter_t *ctrp = 0;
890   u32 start_at_fib_index = 0;
891   BVT (clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash;
892   add_routes_in_fib_arg_t _a, *a = &_a;
893   int i;
894
895   do_fibs = &sm->do_ip46_fibs;
896 again:
897   vec_reset_length (do_fibs->fibs);
898   /* *INDENT-OFF* */
899   pool_foreach (fib, im6->fibs,
900                 ({vec_add1(do_fibs->fibs,fib);}));
901   /* *INDENT-ON* */
902
903
904   for (i = 0; i < vec_len (do_fibs->fibs); i++)
905     {
906       fib = do_fibs->fibs[i];
907       /* We may have bailed out due to control-plane activity */
908       while ((fib - im6->fibs) < start_at_fib_index)
909         continue;
910
911       if (mp == 0)
912         {
913           items_this_message = IP6_FIB_COUNTER_BATCH_SIZE;
914           mp = vl_msg_api_alloc_as_if_client
915             (sizeof (*mp) +
916              items_this_message * sizeof (vl_api_ip6_fib_counter_t));
917           mp->_vl_msg_id = ntohs (VL_API_VNET_IP6_FIB_COUNTERS);
918           mp->count = 0;
919           mp->vrf_id = ntohl (fib->ft_table_id);
920           ctrp = (vl_api_ip6_fib_counter_t *) mp->c;
921         }
922
923       dslock (sm, 0 /* release hint */ , 1 /* tag */ );
924
925       vec_reset_length (do_fibs->ip6routes);
926       vec_reset_length (do_fibs->results);
927
928       a->fib_index = fib - im6->fibs;
929       a->routep = &do_fibs->ip6routes;
930       a->sm = sm;
931
932       if (clib_setjmp (&sm->jmp_buf, 0) == 0)
933         {
934           start_at_fib_index = fib - im6->fibs;
935           BV (clib_bihash_foreach_key_value_pair) (h, add_routes_in_fib, a);
936         }
937       else
938         {
939           dsunlock (sm);
940           ip46_fib_stats_delay (sm, 0 /* sec */ ,
941                                 STATS_RELEASE_DELAY_NS);
942           mp->count = 0;
943           ctrp = (vl_api_ip6_fib_counter_t *) mp->c;
944           goto again;
945         }
946
947       vec_foreach (r, do_fibs->ip6routes)
948       {
949         vlib_counter_t c;
950
951         vlib_get_combined_counter (&load_balance_main.lbm_to_counters,
952                                    r->index, &c);
953         /*
954          * If it has actually
955          * seen at least one packet, send it.
956          */
957         if (c.packets > 0)
958           {
959             /* already in net byte order */
960             ctrp->address[0] = r->address.as_u64[0];
961             ctrp->address[1] = r->address.as_u64[1];
962             ctrp->address_length = (u8) r->address_length;
963             ctrp->packets = clib_host_to_net_u64 (c.packets);
964             ctrp->bytes = clib_host_to_net_u64 (c.bytes);
965             mp->count++;
966             ctrp++;
967
968             if (mp->count == items_this_message)
969               {
970                 mp->count = htonl (items_this_message);
971                 /*
972                  * If the main thread's input queue is stuffed,
973                  * drop the data structure lock (which the main thread
974                  * may want), and take a pause.
975                  */
976                 unix_shared_memory_queue_lock (q);
977                 if (unix_shared_memory_queue_is_full (q))
978                   {
979                     dsunlock (sm);
980                     vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
981                     unix_shared_memory_queue_unlock (q);
982                     mp = 0;
983                     ip46_fib_stats_delay (sm, 0 /* sec */ ,
984                                           STATS_RELEASE_DELAY_NS);
985                     goto again;
986                   }
987                 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
988                 unix_shared_memory_queue_unlock (q);
989
990                 items_this_message = IP6_FIB_COUNTER_BATCH_SIZE;
991                 mp = vl_msg_api_alloc_as_if_client
992                   (sizeof (*mp) +
993                    items_this_message * sizeof (vl_api_ip6_fib_counter_t));
994                 mp->_vl_msg_id = ntohs (VL_API_VNET_IP6_FIB_COUNTERS);
995                 mp->count = 0;
996                 mp->vrf_id = ntohl (fib->ft_table_id);
997                 ctrp = (vl_api_ip6_fib_counter_t *) mp->c;
998               }
999           }
1000
1001         if (sm->data_structure_lock->release_hint)
1002           {
1003             start_at_fib_index = fib - im6->fibs;
1004             dsunlock (sm);
1005             ip46_fib_stats_delay (sm, 0 /* sec */ , STATS_RELEASE_DELAY_NS);
1006             mp->count = 0;
1007             ctrp = (vl_api_ip6_fib_counter_t *) mp->c;
1008             goto again;
1009           }
1010       }                         /* vec_foreach (routes) */
1011
1012       dsunlock (sm);
1013
1014       /* Flush any data from this fib */
1015       if (mp->count)
1016         {
1017           mp->count = htonl (mp->count);
1018           vl_msg_api_send_shmem (q, (u8 *) & mp);
1019           mp = 0;
1020         }
1021     }
1022
1023   /* If e.g. the last FIB had no reportable routes, free the buffer */
1024   if (mp)
1025     vl_msg_api_free (mp);
1026 }
1027
1028 static void
1029 stats_thread_fn (void *arg)
1030 {
1031   stats_main_t *sm = &stats_main;
1032   vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1033   vlib_thread_main_t *tm = vlib_get_thread_main ();
1034
1035   /* stats thread wants no signals. */
1036   {
1037     sigset_t s;
1038     sigfillset (&s);
1039     pthread_sigmask (SIG_SETMASK, &s, 0);
1040   }
1041
1042   if (vec_len (tm->thread_prefix))
1043     vlib_set_thread_name ((char *)
1044                           format (0, "%v_stats%c", tm->thread_prefix, '\0'));
1045
1046   clib_mem_set_heap (w->thread_mheap);
1047
1048   while (1)
1049     {
1050       /* 10 second poll interval */
1051       ip46_fib_stats_delay (sm, 10 /* secs */ , 0 /* nsec */ );
1052
1053       if (!(sm->enable_poller))
1054         continue;
1055       do_simple_interface_counters (sm);
1056       do_combined_interface_counters (sm);
1057       do_ip4_fibs (sm);
1058       do_ip6_fibs (sm);
1059       do_ip4_nbrs (sm);
1060       do_ip6_nbrs (sm);
1061     }
1062 }
1063
1064 static void
1065   vl_api_vnet_interface_simple_counters_t_handler
1066   (vl_api_vnet_interface_simple_counters_t * mp)
1067 {
1068   vpe_client_stats_registration_t *reg;
1069   stats_main_t *sm = &stats_main;
1070   unix_shared_memory_queue_t *q, *q_prev = NULL;
1071   vl_api_vnet_interface_simple_counters_t *mp_copy = NULL;
1072   u32 mp_size;
1073   int i;
1074
1075   mp_size = sizeof (*mp) + (ntohl (mp->count) * sizeof (u64));
1076
1077   /* *INDENT-OFF* */
1078   vec_reset_length(sm->regs);
1079   pool_foreach(reg, sm->stats_registrations,
1080                ({
1081              vec_add1(sm->regs,reg);
1082                }));
1083   /* *INDENT-ON* */
1084   for (i = 0; i < vec_len (sm->regs); i++)
1085     {
1086       reg = sm->regs[i];
1087       if (reg->stats_registrations & INTERFACE_SIMPLE_COUNTERS)
1088         {
1089           q = vl_api_client_index_to_input_queue (reg->client.client_index);
1090           if (q)
1091             {
1092               if (q_prev && (q_prev->cursize < q_prev->maxsize))
1093                 {
1094                   mp_copy = vl_msg_api_alloc_as_if_client (mp_size);
1095                   clib_memcpy (mp_copy, mp, mp_size);
1096                   vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1097                   mp = mp_copy;
1098                 }
1099               q_prev = q;
1100             }
1101         }
1102     }
1103 #if STATS_DEBUG > 0
1104   fformat (stdout, "%U\n", format_vnet_simple_counters, mp);
1105 #endif
1106
1107   if (q_prev && (q_prev->cursize < q_prev->maxsize))
1108     {
1109       vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1110     }
1111   else
1112     {
1113       vl_msg_api_free (mp);
1114     }
1115 }
1116
1117 static void
1118   vl_api_vnet_interface_combined_counters_t_handler
1119   (vl_api_vnet_interface_combined_counters_t * mp)
1120 {
1121   vpe_client_stats_registration_t *reg;
1122   stats_main_t *sm = &stats_main;
1123   unix_shared_memory_queue_t *q, *q_prev = NULL;
1124   vl_api_vnet_interface_combined_counters_t *mp_copy = NULL;
1125   u32 mp_size;
1126
1127   mp_size = sizeof (*mp) + (ntohl (mp->count) * sizeof (vlib_counter_t));
1128
1129   /* *INDENT-OFF* */
1130   pool_foreach(reg, sm->stats_registrations,
1131                ({
1132              if (reg->stats_registrations & INTERFACE_COMBINED_COUNTERS)
1133                {
1134                  q = vl_api_client_index_to_input_queue (reg->client.client_index);
1135                  if (q)
1136                    {
1137                      if (q_prev && (q_prev->cursize < q_prev->maxsize))
1138                        {
1139                          mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
1140                          clib_memcpy(mp_copy, mp, mp_size);
1141                          vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
1142                          mp = mp_copy;
1143                        }
1144                      q_prev = q;
1145                    }
1146                }
1147                }));
1148   /* *INDENT-ON* */
1149
1150 #if STATS_DEBUG > 0
1151   fformat (stdout, "%U\n", format_vnet_combined_counters, mp);
1152 #endif
1153
1154   if (q_prev && (q_prev->cursize < q_prev->maxsize))
1155     {
1156       vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1157     }
1158   else
1159     {
1160       vl_msg_api_free (mp);
1161     }
1162 }
1163
1164 static void
1165 vl_api_vnet_ip4_fib_counters_t_handler (vl_api_vnet_ip4_fib_counters_t * mp)
1166 {
1167   vpe_client_stats_registration_t *reg;
1168   stats_main_t *sm = &stats_main;
1169   unix_shared_memory_queue_t *q, *q_prev = NULL;
1170   vl_api_vnet_ip4_fib_counters_t *mp_copy = NULL;
1171   u32 mp_size;
1172
1173   mp_size = sizeof (*mp_copy) +
1174     ntohl (mp->count) * sizeof (vl_api_ip4_fib_counter_t);
1175
1176   /* *INDENT-OFF* */
1177   pool_foreach(reg, sm->stats_registrations,
1178   ({
1179     if (reg->stats_registrations & IP4_FIB_COUNTERS)
1180       {
1181         q = vl_api_client_index_to_input_queue (reg->client.client_index);
1182         if (q)
1183           {
1184             if (q_prev && (q_prev->cursize < q_prev->maxsize))
1185               {
1186                 mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
1187                 clib_memcpy(mp_copy, mp, mp_size);
1188                 vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
1189                 mp = mp_copy;
1190               }
1191             q_prev = q;
1192           }
1193       }
1194   }));
1195   /* *INDENT-ON* */
1196   if (q_prev && (q_prev->cursize < q_prev->maxsize))
1197     {
1198       vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1199     }
1200   else
1201     {
1202       vl_msg_api_free (mp);
1203     }
1204 }
1205
1206 static void
1207 vl_api_vnet_ip4_nbr_counters_t_handler (vl_api_vnet_ip4_nbr_counters_t * mp)
1208 {
1209   vpe_client_stats_registration_t *reg;
1210   stats_main_t *sm = &stats_main;
1211   unix_shared_memory_queue_t *q, *q_prev = NULL;
1212   vl_api_vnet_ip4_nbr_counters_t *mp_copy = NULL;
1213   u32 mp_size;
1214
1215   mp_size = sizeof (*mp_copy) +
1216     ntohl (mp->count) * sizeof (vl_api_ip4_nbr_counter_t);
1217
1218   /* *INDENT-OFF* */
1219   pool_foreach(reg, sm->stats_registrations,
1220   ({
1221     if (reg->stats_registrations & IP4_NBR_COUNTERS)
1222       {
1223         q = vl_api_client_index_to_input_queue (reg->client.client_index);
1224         if (q)
1225           {
1226             if (q_prev && (q_prev->cursize < q_prev->maxsize))
1227               {
1228                 mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
1229                 clib_memcpy(mp_copy, mp, mp_size);
1230                 vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
1231                 mp = mp_copy;
1232               }
1233             q_prev = q;
1234           }
1235       }
1236   }));
1237   /* *INDENT-ON* */
1238   if (q_prev && (q_prev->cursize < q_prev->maxsize))
1239     {
1240       vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1241     }
1242   else
1243     {
1244       vl_msg_api_free (mp);
1245     }
1246 }
1247
1248 static void
1249 vl_api_vnet_ip6_fib_counters_t_handler (vl_api_vnet_ip6_fib_counters_t * mp)
1250 {
1251   vpe_client_stats_registration_t *reg;
1252   stats_main_t *sm = &stats_main;
1253   unix_shared_memory_queue_t *q, *q_prev = NULL;
1254   vl_api_vnet_ip6_fib_counters_t *mp_copy = NULL;
1255   u32 mp_size;
1256
1257   mp_size = sizeof (*mp_copy) +
1258     ntohl (mp->count) * sizeof (vl_api_ip6_fib_counter_t);
1259
1260   /* *INDENT-OFF* */
1261   pool_foreach(reg, sm->stats_registrations,
1262   ({
1263     if (reg->stats_registrations & IP6_FIB_COUNTERS)
1264       {
1265         q = vl_api_client_index_to_input_queue (reg->client.client_index);
1266         if (q)
1267           {
1268             if (q_prev && (q_prev->cursize < q_prev->maxsize))
1269               {
1270                 mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
1271                 clib_memcpy(mp_copy, mp, mp_size);
1272                 vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
1273                 mp = mp_copy;
1274               }
1275             q_prev = q;
1276           }
1277       }
1278       }));
1279   /* *INDENT-ON* */
1280   if (q_prev && (q_prev->cursize < q_prev->maxsize))
1281     {
1282       vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1283     }
1284   else
1285     {
1286       vl_msg_api_free (mp);
1287     }
1288 }
1289
1290 static void
1291 vl_api_vnet_ip6_nbr_counters_t_handler (vl_api_vnet_ip6_nbr_counters_t * mp)
1292 {
1293   vpe_client_stats_registration_t *reg;
1294   stats_main_t *sm = &stats_main;
1295   unix_shared_memory_queue_t *q, *q_prev = NULL;
1296   vl_api_vnet_ip6_nbr_counters_t *mp_copy = NULL;
1297   u32 mp_size;
1298
1299   mp_size = sizeof (*mp_copy) +
1300     ntohl (mp->count) * sizeof (vl_api_ip6_nbr_counter_t);
1301
1302   /* *INDENT-OFF* */
1303   pool_foreach(reg, sm->stats_registrations,
1304   ({
1305     if (reg->stats_registrations & IP6_NBR_COUNTERS)
1306       {
1307         q = vl_api_client_index_to_input_queue (reg->client.client_index);
1308         if (q)
1309           {
1310             if (q_prev && (q_prev->cursize < q_prev->maxsize))
1311               {
1312                 mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
1313                 clib_memcpy(mp_copy, mp, mp_size);
1314                 vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
1315                 mp = mp_copy;
1316               }
1317             q_prev = q;
1318           }
1319       }
1320   }));
1321   /* *INDENT-ON* */
1322   if (q_prev && (q_prev->cursize < q_prev->maxsize))
1323     {
1324       vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1325     }
1326   else
1327     {
1328       vl_msg_api_free (mp);
1329     }
1330 }
1331
1332 static void
1333 vl_api_want_stats_t_handler (vl_api_want_stats_t * mp)
1334 {
1335   stats_main_t *sm = &stats_main;
1336   vpe_client_stats_registration_t *rp;
1337   vl_api_want_stats_reply_t *rmp;
1338   uword *p;
1339   i32 retval = 0;
1340   unix_shared_memory_queue_t *q;
1341
1342   p = hash_get (sm->stats_registration_hash, mp->client_index);
1343   if (p)
1344     {
1345       if (mp->enable_disable)
1346         {
1347           clib_warning ("pid %d: already enabled...", mp->pid);
1348           retval = -2;
1349           goto reply;
1350         }
1351       else
1352         {
1353           rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1354           pool_put (sm->stats_registrations, rp);
1355           hash_unset (sm->stats_registration_hash, mp->client_index);
1356           goto reply;
1357         }
1358     }
1359   if (mp->enable_disable == 0)
1360     {
1361       clib_warning ("pid %d: already disabled...", mp->pid);
1362       retval = -3;
1363       goto reply;
1364     }
1365   pool_get (sm->stats_registrations, rp);
1366   rp->client.client_index = mp->client_index;
1367   rp->client.client_pid = mp->pid;
1368   rp->stats_registrations |= INTERFACE_SIMPLE_COUNTERS;
1369   rp->stats_registrations |= INTERFACE_COMBINED_COUNTERS;
1370   rp->stats_registrations |= IP4_FIB_COUNTERS;
1371   rp->stats_registrations |= IP4_NBR_COUNTERS;
1372   rp->stats_registrations |= IP6_FIB_COUNTERS;
1373   rp->stats_registrations |= IP6_NBR_COUNTERS;
1374
1375   hash_set (sm->stats_registration_hash, rp->client.client_index,
1376             rp - sm->stats_registrations);
1377
1378 reply:
1379   if (pool_elts (sm->stats_registrations))
1380     sm->enable_poller = 1;
1381   else
1382     sm->enable_poller = 0;
1383
1384   q = vl_api_client_index_to_input_queue (mp->client_index);
1385
1386   if (!q)
1387     return;
1388
1389   rmp = vl_msg_api_alloc (sizeof (*rmp));
1390   rmp->_vl_msg_id = ntohs (VL_API_WANT_STATS_REPLY);
1391   rmp->context = mp->context;
1392   rmp->retval = retval;
1393
1394   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1395 }
1396
1397 static void
1398   vl_api_want_interface_simple_stats_t_handler
1399   (vl_api_want_interface_simple_stats_t * mp)
1400 {
1401   stats_main_t *sm = &stats_main;
1402   vpe_client_stats_registration_t *rp;
1403   vl_api_want_interface_simple_stats_reply_t *rmp;
1404   uword *p;
1405   i32 retval = 0;
1406   unix_shared_memory_queue_t *q;
1407
1408   p = hash_get (sm->stats_registration_hash, mp->client_index);
1409
1410   /* Disable case */
1411   if (mp->enable_disable == 0)
1412     {
1413       if (!p)                   // No client to disable
1414         {
1415           clib_warning ("pid %d: already disabled for stats...", mp->pid);
1416           retval = 0;
1417           goto reply;
1418         }
1419
1420       rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1421       if (!rp->stats_registrations & INTERFACE_SIMPLE_COUNTERS) // Client but doesn't want this.
1422         {
1423           clib_warning
1424             ("pid %d: already disabled for interface simple stats...",
1425              mp->pid);
1426           retval = 0;
1427           goto reply;
1428         }
1429       else
1430         {
1431           rp->stats_registrations &= ~(INTERFACE_SIMPLE_COUNTERS);      // Clear flag
1432           if (rp->stats_registrations == 0)     // Client isn't listening to anything else
1433             {
1434               pool_put (sm->stats_registrations, rp);
1435               hash_unset (sm->stats_registration_hash, mp->client_index);
1436             }
1437           goto reply;
1438         }
1439     }
1440   /* Enable case */
1441   /* Get client from pool */
1442   if (p)
1443     rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1444
1445   if (!p || !rp)                // Doesn't exist, make a new entry
1446     {
1447       pool_get (sm->stats_registrations, rp);
1448       rp->client.client_index = mp->client_index;
1449       rp->client.client_pid = mp->pid;
1450     }
1451   rp->stats_registrations |= INTERFACE_SIMPLE_COUNTERS;
1452   hash_set (sm->stats_registration_hash, rp->client.client_index,
1453             rp - sm->stats_registrations);
1454
1455 reply:
1456   if (pool_elts (sm->stats_registrations))      // Someone wants something, somewhere so enable globally for now.
1457     sm->enable_poller = 1;
1458   else
1459     sm->enable_poller = 0;
1460
1461   q = vl_api_client_index_to_input_queue (mp->client_index);
1462
1463   if (!q)
1464     return;
1465
1466   rmp = vl_msg_api_alloc (sizeof (*rmp));
1467   rmp->_vl_msg_id = ntohs (VL_API_WANT_INTERFACE_SIMPLE_STATS_REPLY);
1468   rmp->context = mp->context;
1469   rmp->retval = retval;
1470
1471   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1472 }
1473
1474 static void
1475   vl_api_want_interface_combined_stats_t_handler
1476   (vl_api_want_interface_combined_stats_t * mp)
1477 {
1478   stats_main_t *sm = &stats_main;
1479   vpe_client_stats_registration_t *rp;
1480   vl_api_want_interface_combined_stats_reply_t *rmp;
1481   uword *p;
1482   i32 retval = 0;
1483   unix_shared_memory_queue_t *q;
1484
1485   p = hash_get (sm->stats_registration_hash, mp->client_index);
1486
1487   /* Disable case */
1488   if (mp->enable_disable == 0)
1489     {
1490       if (!p)                   // No client to disable
1491         {
1492           clib_warning ("pid %d: already disabled for stats...", mp->pid);
1493           retval = 0;
1494           goto reply;
1495         }
1496
1497       rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1498       if (!rp->stats_registrations & INTERFACE_COMBINED_COUNTERS)       // Client but doesn't want this.
1499         {
1500           clib_warning
1501             ("pid %d: already disabled for interface COMBINED stats...",
1502              mp->pid);
1503           retval = 0;
1504           goto reply;
1505         }
1506       else
1507         {
1508           rp->stats_registrations &= ~(INTERFACE_COMBINED_COUNTERS);    // Clear flag
1509           if (rp->stats_registrations == 0)     // Client isn't listening to anything else
1510             {
1511               pool_put (sm->stats_registrations, rp);
1512               hash_unset (sm->stats_registration_hash, mp->client_index);
1513             }
1514           goto reply;
1515         }
1516     }
1517   /* Enable case */
1518   /* Get client from pool */
1519   if (p)
1520     rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1521
1522   if (!p || !rp)                // Doesn't exist, make a new entry
1523     {
1524       pool_get (sm->stats_registrations, rp);
1525       rp->client.client_index = mp->client_index;
1526       rp->client.client_pid = mp->pid;
1527     }
1528   rp->stats_registrations |= INTERFACE_COMBINED_COUNTERS;
1529   hash_set (sm->stats_registration_hash, rp->client.client_index,
1530             rp - sm->stats_registrations);
1531
1532 reply:
1533   if (pool_elts (sm->stats_registrations))      // Someone wants something, somewhere so enable globally for now.
1534     sm->enable_poller = 1;
1535   else
1536     sm->enable_poller = 0;
1537
1538   q = vl_api_client_index_to_input_queue (mp->client_index);
1539
1540   if (!q)
1541     return;
1542
1543   rmp = vl_msg_api_alloc (sizeof (*rmp));
1544   rmp->_vl_msg_id = ntohs (VL_API_WANT_INTERFACE_COMBINED_STATS_REPLY);
1545   rmp->context = mp->context;
1546   rmp->retval = retval;
1547
1548   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1549 }
1550
1551 static void
1552 vl_api_want_ip4_fib_stats_t_handler (vl_api_want_ip4_fib_stats_t * mp)
1553 {
1554   stats_main_t *sm = &stats_main;
1555   vpe_client_stats_registration_t *rp;
1556   vl_api_want_ip4_fib_stats_reply_t *rmp;
1557   uword *p;
1558   i32 retval = 0;
1559   unix_shared_memory_queue_t *q;
1560
1561   p = hash_get (sm->stats_registration_hash, mp->client_index);
1562
1563   /* Disable case */
1564   /*
1565      $$$ FIXME: need std return codes. Still undecided if enabling already
1566      enabled (and similar for disabled) is really a -'ve error condition or
1567      if 0 is sufficient
1568    */
1569   if (mp->enable_disable == 0)
1570     {
1571       if (!p)                   // No client to disable
1572         {
1573           clib_warning ("pid %d: already disabled for stats...", mp->pid);
1574           retval = -3;
1575           goto reply;
1576         }
1577
1578       rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1579       if (!rp->stats_registrations & IP4_FIB_COUNTERS)  // Client but doesn't want this.
1580         {
1581           clib_warning ("pid %d: already disabled for interface ip4 fib...",
1582                         mp->pid);
1583           retval = -2;
1584           goto reply;
1585         }
1586       else
1587         {
1588           rp->stats_registrations &= ~(IP4_FIB_COUNTERS);       // Clear flag
1589           if (rp->stats_registrations == 0)     // Client isn't listening to anything else
1590             {
1591               pool_put (sm->stats_registrations, rp);
1592               hash_unset (sm->stats_registration_hash, mp->client_index);
1593             }
1594           goto reply;
1595         }
1596     }
1597   /* Enable case */
1598   /* Get client from pool */
1599   if (p)
1600     rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1601
1602   if (!p || !rp)                // Doesn't exist, make a new entry
1603     {
1604       pool_get (sm->stats_registrations, rp);
1605       rp->client.client_index = mp->client_index;
1606       rp->client.client_pid = mp->pid;
1607     }
1608   rp->stats_registrations |= IP4_FIB_COUNTERS;
1609   hash_set (sm->stats_registration_hash, rp->client.client_index,
1610             rp - sm->stats_registrations);
1611
1612 reply:
1613   if (pool_elts (sm->stats_registrations))      // Someone wants something, somewhere so enable globally for now.
1614     sm->enable_poller = 1;
1615   else
1616     sm->enable_poller = 0;
1617
1618   q = vl_api_client_index_to_input_queue (mp->client_index);
1619
1620   if (!q)
1621     return;
1622
1623   rmp = vl_msg_api_alloc (sizeof (*rmp));
1624   rmp->_vl_msg_id = ntohs (VL_API_WANT_IP4_FIB_STATS_REPLY);
1625   rmp->context = mp->context;
1626   rmp->retval = retval;
1627
1628   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1629 }
1630
1631 static void
1632 vl_api_want_ip6_fib_stats_t_handler (vl_api_want_ip6_fib_stats_t * mp)
1633 {
1634   stats_main_t *sm = &stats_main;
1635   vpe_client_stats_registration_t *rp;
1636   vl_api_want_ip6_fib_stats_reply_t *rmp;
1637   uword *p;
1638   i32 retval = 0;
1639   unix_shared_memory_queue_t *q;
1640
1641   p = hash_get (sm->stats_registration_hash, mp->client_index);
1642
1643   /* Disable case */
1644   /*
1645      $$$ FIXME: need std return codes. Still undecided if enabling already
1646      enabled (and similar for disabled) is really a -'ve error condition or
1647      if 0 is sufficient
1648    */
1649   if (mp->enable_disable == 0)
1650     {
1651       if (!p)                   // No client to disable
1652         {
1653           clib_warning ("pid %d: already disabled for stats...", mp->pid);
1654           retval = -3;
1655           goto reply;
1656         }
1657
1658       rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1659       if (!rp->stats_registrations & IP6_FIB_COUNTERS)  // Client but doesn't want this.
1660         {
1661           clib_warning ("pid %d: already disabled for interface ip6 fib...",
1662                         mp->pid);
1663           retval = -2;
1664           goto reply;
1665         }
1666       else
1667         {
1668           rp->stats_registrations &= ~(IP6_FIB_COUNTERS);       // Clear flag
1669           if (rp->stats_registrations == 0)     // Client isn't listening to anything else
1670             {
1671               pool_put (sm->stats_registrations, rp);
1672               hash_unset (sm->stats_registration_hash, mp->client_index);
1673             }
1674           goto reply;
1675         }
1676     }
1677   /* Enable case */
1678   /* Get client from pool */
1679   if (p)
1680     rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1681
1682   if (!p || !rp)                // Doesn't exist, make a new entry
1683     {
1684       pool_get (sm->stats_registrations, rp);
1685       rp->client.client_index = mp->client_index;
1686       rp->client.client_pid = mp->pid;
1687     }
1688   rp->stats_registrations |= IP6_FIB_COUNTERS;
1689   hash_set (sm->stats_registration_hash, rp->client.client_index,
1690             rp - sm->stats_registrations);
1691
1692 reply:
1693   if (pool_elts (sm->stats_registrations))      // Someone wants something, somewhere so enable globally for now.
1694     sm->enable_poller = 1;
1695   else
1696     sm->enable_poller = 0;
1697
1698   q = vl_api_client_index_to_input_queue (mp->client_index);
1699
1700   if (!q)
1701     return;
1702
1703   rmp = vl_msg_api_alloc (sizeof (*rmp));
1704   rmp->_vl_msg_id = ntohs (VL_API_WANT_IP6_FIB_STATS_REPLY);
1705   rmp->context = mp->context;
1706   rmp->retval = retval;
1707
1708   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1709 }
1710
1711 /* FIXME - NBR stats broken - this will be fixed in subsequent patch */
1712 static void
1713 vl_api_want_ip4_nbr_stats_t_handler (vl_api_want_ip4_nbr_stats_t * mp)
1714 {
1715 }
1716
1717 static void
1718 vl_api_want_ip6_nbr_stats_t_handler (vl_api_want_ip6_nbr_stats_t * mp)
1719 {
1720 }
1721
1722 static void
1723 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
1724 {
1725   stats_main_t *sm = &stats_main;
1726   vnet_interface_main_t *im = sm->interface_main;
1727   vl_api_vnet_get_summary_stats_reply_t *rmp;
1728   vlib_combined_counter_main_t *cm;
1729   vlib_counter_t v;
1730   int i, which;
1731   u64 total_pkts[VLIB_N_RX_TX];
1732   u64 total_bytes[VLIB_N_RX_TX];
1733
1734   unix_shared_memory_queue_t *q =
1735     vl_api_client_index_to_input_queue (mp->client_index);
1736
1737   if (!q)
1738     return;
1739
1740   rmp = vl_msg_api_alloc (sizeof (*rmp));
1741   rmp->_vl_msg_id = ntohs (VL_API_VNET_GET_SUMMARY_STATS_REPLY);
1742   rmp->context = mp->context;
1743   rmp->retval = 0;
1744
1745   memset (total_pkts, 0, sizeof (total_pkts));
1746   memset (total_bytes, 0, sizeof (total_bytes));
1747
1748   vnet_interface_counter_lock (im);
1749
1750   vec_foreach (cm, im->combined_sw_if_counters)
1751   {
1752     which = cm - im->combined_sw_if_counters;
1753
1754     for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1755       {
1756         vlib_get_combined_counter (cm, i, &v);
1757         total_pkts[which] += v.packets;
1758         total_bytes[which] += v.bytes;
1759       }
1760   }
1761   vnet_interface_counter_unlock (im);
1762
1763   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
1764   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
1765   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
1766   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
1767   rmp->vector_rate =
1768     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
1769
1770   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1771 }
1772
1773 int
1774 stats_memclnt_delete_callback (u32 client_index)
1775 {
1776   vpe_client_stats_registration_t *rp;
1777   stats_main_t *sm = &stats_main;
1778   uword *p;
1779
1780   p = hash_get (sm->stats_registration_hash, client_index);
1781   if (p)
1782     {
1783       rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1784       pool_put (sm->stats_registrations, rp);
1785       hash_unset (sm->stats_registration_hash, client_index);
1786     }
1787
1788   return 0;
1789 }
1790
1791 #define vl_api_vnet_interface_simple_counters_t_endian vl_noop_handler
1792 #define vl_api_vnet_interface_simple_counters_t_print vl_noop_handler
1793 #define vl_api_vnet_interface_combined_counters_t_endian vl_noop_handler
1794 #define vl_api_vnet_interface_combined_counters_t_print vl_noop_handler
1795 #define vl_api_vnet_ip4_fib_counters_t_endian vl_noop_handler
1796 #define vl_api_vnet_ip4_fib_counters_t_print vl_noop_handler
1797 #define vl_api_vnet_ip6_fib_counters_t_endian vl_noop_handler
1798 #define vl_api_vnet_ip6_fib_counters_t_print vl_noop_handler
1799 #define vl_api_vnet_ip4_nbr_counters_t_endian vl_noop_handler
1800 #define vl_api_vnet_ip4_nbr_counters_t_print vl_noop_handler
1801 #define vl_api_vnet_ip6_nbr_counters_t_endian vl_noop_handler
1802 #define vl_api_vnet_ip6_nbr_counters_t_print vl_noop_handler
1803
1804 static clib_error_t *
1805 stats_init (vlib_main_t * vm)
1806 {
1807   stats_main_t *sm = &stats_main;
1808   api_main_t *am = &api_main;
1809   void *vlib_worker_thread_bootstrap_fn (void *arg);
1810
1811   sm->vlib_main = vm;
1812   sm->vnet_main = vnet_get_main ();
1813   sm->interface_main = &vnet_get_main ()->interface_main;
1814   sm->api_main = am;
1815   sm->stats_poll_interval_in_seconds = 10;
1816   sm->data_structure_lock =
1817     clib_mem_alloc_aligned (sizeof (data_structure_lock_t),
1818                             CLIB_CACHE_LINE_BYTES);
1819   memset (sm->data_structure_lock, 0, sizeof (*sm->data_structure_lock));
1820
1821 #define _(N,n)                                                  \
1822     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1823                            vl_api_##n##_t_handler,              \
1824                            vl_noop_handler,                     \
1825                            vl_api_##n##_t_endian,               \
1826                            vl_api_##n##_t_print,                \
1827                            sizeof(vl_api_##n##_t), 0 /* do NOT trace! */);
1828   foreach_stats_msg;
1829 #undef _
1830
1831   /* tell the msg infra not to free these messages... */
1832   am->message_bounce[VL_API_VNET_INTERFACE_SIMPLE_COUNTERS] = 1;
1833   am->message_bounce[VL_API_VNET_INTERFACE_COMBINED_COUNTERS] = 1;
1834   am->message_bounce[VL_API_VNET_IP4_FIB_COUNTERS] = 1;
1835   am->message_bounce[VL_API_VNET_IP6_FIB_COUNTERS] = 1;
1836   am->message_bounce[VL_API_VNET_IP4_NBR_COUNTERS] = 1;
1837   am->message_bounce[VL_API_VNET_IP6_NBR_COUNTERS] = 1;
1838
1839   return 0;
1840 }
1841
1842 VLIB_INIT_FUNCTION (stats_init);
1843
1844 /* *INDENT-OFF* */
1845 VLIB_REGISTER_THREAD (stats_thread_reg, static) = {
1846   .name = "stats",
1847   .function = stats_thread_fn,
1848   .fixed_count = 1,
1849   .count = 1,
1850   .no_data_structure_clone = 1,
1851   .use_pthreads = 1,
1852 };
1853 /* *INDENT-ON* */
1854
1855 /*
1856  * fd.io coding-style-patch-verification: ON
1857  *
1858  * Local Variables:
1859  * eval: (c-set-style "gnu")
1860  * End:
1861  */