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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vpp/stats/stats.h>
17 #include <vlib/threads.h>
18 #include <vnet/fib/fib_entry.h>
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/fib/ip4_fib.h>
21 #include <vnet/dpo/load_balance.h>
25 stats_main_t stats_main;
27 #include <vnet/ip/ip.h>
29 #include <vpp/api/vpe_msg_enum.h>
32 #define f64_print(a,b)
34 #define vl_typedefs /* define message structures */
35 #include <vpp/api/vpe_all_api_h.h>
38 #define vl_endianfun /* define message structures */
39 #include <vpp/api/vpe_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <vpp/api/vpe_all_api_h.h>
48 #define foreach_stats_msg \
49 _(WANT_STATS, want_stats) \
50 _(VNET_INTERFACE_COUNTERS, vnet_interface_counters) \
51 _(VNET_IP4_FIB_COUNTERS, vnet_ip4_fib_counters) \
52 _(VNET_IP6_FIB_COUNTERS, vnet_ip6_fib_counters) \
53 _(VNET_IP4_NBR_COUNTERS, vnet_ip4_nbr_counters) \
54 _(VNET_IP6_NBR_COUNTERS, vnet_ip6_nbr_counters)
56 /* These constants ensure msg sizes <= 1024, aka ring allocation */
57 #define SIMPLE_COUNTER_BATCH_SIZE 126
58 #define COMBINED_COUNTER_BATCH_SIZE 63
59 #define IP4_FIB_COUNTER_BATCH_SIZE 48
60 #define IP6_FIB_COUNTER_BATCH_SIZE 30
63 #define STATS_RELEASE_DELAY_NS (1000 * 1000 * 5)
67 dslock (stats_main_t * sm, int release_hint, int tag)
70 data_structure_lock_t *l = sm->data_structure_lock;
72 if (PREDICT_FALSE (l == 0))
75 thread_id = os_get_cpu_number ();
76 if (l->lock && l->thread_id == thread_id)
85 while (__sync_lock_test_and_set (&l->lock, 1))
88 l->thread_id = thread_id;
93 stats_dslock_with_hint (int hint, int tag)
95 stats_main_t *sm = &stats_main;
96 dslock (sm, hint, tag);
100 dsunlock (stats_main_t * sm)
103 data_structure_lock_t *l = sm->data_structure_lock;
105 if (PREDICT_FALSE (l == 0))
108 thread_id = os_get_cpu_number ();
109 ASSERT (l->lock && l->thread_id == thread_id);
115 CLIB_MEMORY_BARRIER ();
121 stats_dsunlock (int hint, int tag)
123 stats_main_t *sm = &stats_main;
128 do_simple_interface_counters (stats_main_t * sm)
130 vl_api_vnet_interface_counters_t *mp = 0;
131 vnet_interface_main_t *im = sm->interface_main;
132 api_main_t *am = sm->api_main;
133 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
134 unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
135 vlib_simple_counter_main_t *cm;
136 u32 items_this_message = 0;
141 * Prevent interface registration from expanding / moving the vectors...
142 * That tends never to happen, so we can hold this lock for a while.
144 vnet_interface_counter_lock (im);
146 vec_foreach (cm, im->sw_if_counters)
148 n_counts = vlib_simple_counter_n_counters (cm);
149 for (i = 0; i < n_counts; i++)
153 items_this_message = clib_min (SIMPLE_COUNTER_BATCH_SIZE,
156 mp = vl_msg_api_alloc_as_if_client
157 (sizeof (*mp) + items_this_message * sizeof (v));
158 mp->_vl_msg_id = ntohs (VL_API_VNET_INTERFACE_COUNTERS);
159 mp->vnet_counter_type = cm - im->sw_if_counters;
161 mp->first_sw_if_index = htonl (i);
163 vp = (u64 *) mp->data;
165 v = vlib_get_simple_counter (cm, i);
166 clib_mem_unaligned (vp, u64) = clib_host_to_net_u64 (v);
169 if (mp->count == items_this_message)
171 mp->count = htonl (items_this_message);
172 /* Send to the main thread... */
173 vl_msg_api_send_shmem (q, (u8 *) & mp);
179 vnet_interface_counter_unlock (im);
183 do_combined_interface_counters (stats_main_t * sm)
185 vl_api_vnet_interface_counters_t *mp = 0;
186 vnet_interface_main_t *im = sm->interface_main;
187 api_main_t *am = sm->api_main;
188 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
189 unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
190 vlib_combined_counter_main_t *cm;
191 u32 items_this_message = 0;
192 vlib_counter_t v, *vp = 0;
195 vnet_interface_counter_lock (im);
197 vec_foreach (cm, im->combined_sw_if_counters)
199 n_counts = vlib_combined_counter_n_counters (cm);
200 for (i = 0; i < n_counts; i++)
204 items_this_message = clib_min (COMBINED_COUNTER_BATCH_SIZE,
207 mp = vl_msg_api_alloc_as_if_client
208 (sizeof (*mp) + items_this_message * sizeof (v));
209 mp->_vl_msg_id = ntohs (VL_API_VNET_INTERFACE_COUNTERS);
210 mp->vnet_counter_type = cm - im->combined_sw_if_counters;
212 mp->first_sw_if_index = htonl (i);
214 vp = (vlib_counter_t *) mp->data;
216 vlib_get_combined_counter (cm, i, &v);
217 clib_mem_unaligned (&vp->packets, u64)
218 = clib_host_to_net_u64 (v.packets);
219 clib_mem_unaligned (&vp->bytes, u64) = clib_host_to_net_u64 (v.bytes);
222 if (mp->count == items_this_message)
224 mp->count = htonl (items_this_message);
225 /* Send to the main thread... */
226 vl_msg_api_send_shmem (q, (u8 *) & mp);
232 vnet_interface_counter_unlock (im);
235 /* from .../vnet/vnet/ip/lookup.c. Yuck */
236 typedef CLIB_PACKED (struct
238 ip4_address_t address;
239 u32 address_length: 6;
244 ip46_fib_stats_delay (stats_main_t * sm, u32 sec, u32 nsec)
246 struct timespec _req, *req = &_req;
247 struct timespec _rem, *rem = &_rem;
253 if (nanosleep (req, rem) == 0)
258 clib_unix_warning ("nanosleep");
264 * @brief The context passed when collecting adjacency counters
266 typedef struct ip4_nbr_stats_ctx_t_
269 * The SW IF index all these adjs belong to
274 * A vector of ip4 nbr counters
276 vl_api_ip4_nbr_counter_t *counters;
277 } ip4_nbr_stats_ctx_t;
280 ip4_nbr_stats_cb (adj_index_t ai, void *arg)
282 vl_api_ip4_nbr_counter_t *vl_counter;
283 vlib_counter_t adj_counter;
284 ip4_nbr_stats_ctx_t *ctx;
288 vlib_get_combined_counter (&adjacency_counters, ai, &adj_counter);
290 if (0 != adj_counter.packets)
292 vec_add2 (ctx->counters, vl_counter, 1);
295 vl_counter->packets = clib_host_to_net_u64 (adj_counter.packets);
296 vl_counter->bytes = clib_host_to_net_u64 (adj_counter.bytes);
297 vl_counter->address = adj->sub_type.nbr.next_hop.ip4.as_u32;
298 vl_counter->link_type = adj->ia_link;
300 return (ADJ_WALK_RC_CONTINUE);
303 #define MIN(x,y) (((x)<(y))?(x):(y))
306 ip4_nbr_ship (stats_main_t * sm, ip4_nbr_stats_ctx_t * ctx)
308 api_main_t *am = sm->api_main;
309 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
310 unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
311 vl_api_vnet_ip4_nbr_counters_t *mp = 0;
315 * If the walk context has counters, which may be left over from the last
316 * suspend, then we continue from there.
318 while (0 != vec_len (ctx->counters))
320 u32 n_items = MIN (vec_len (ctx->counters),
321 IP4_FIB_COUNTER_BATCH_SIZE);
324 dslock (sm, 0 /* release hint */ , 1 /* tag */ );
326 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) +
329 (vl_api_ip4_nbr_counter_t)));
330 mp->_vl_msg_id = ntohs (VL_API_VNET_IP4_NBR_COUNTERS);
331 mp->count = ntohl (n_items);
332 mp->sw_if_index = ntohl (ctx->sw_if_index);
337 * copy the counters from the back of the context, then we can easily
338 * 'erase' them by resetting the vector length.
339 * The order we push the stats to the caller is not important.
342 &ctx->counters[vec_len (ctx->counters) - n_items],
343 n_items * sizeof (*ctx->counters));
345 _vec_len (ctx->counters) = vec_len (ctx->counters) - n_items;
350 unix_shared_memory_queue_lock (q);
351 pause = unix_shared_memory_queue_is_full (q);
353 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
354 unix_shared_memory_queue_unlock (q);
358 ip46_fib_stats_delay (sm, 0 /* sec */ ,
359 STATS_RELEASE_DELAY_NS);
364 do_ip4_nbrs (stats_main_t * sm)
366 vnet_main_t *vnm = vnet_get_main ();
367 vnet_interface_main_t *im = &vnm->interface_main;
368 vnet_sw_interface_t *si;
370 ip4_nbr_stats_ctx_t ctx = {
376 pool_foreach (si, im->sw_interfaces,
379 * update the interface we are now concerned with
381 ctx.sw_if_index = si->sw_if_index;
384 * we are about to walk another interface, so we shouldn't have any pending
387 ASSERT(ctx.counters == NULL);
390 * visit each neighbour adjacency on the interface and collect
392 * Because we hold the lock the walk is synchronous, so safe to routing
393 * updates. It's limited in work by the number of adjacenies on an
394 * interface, which is typically not huge.
396 dslock (sm, 0 /* release hint */ , 1 /* tag */ );
397 adj_nbr_walk (si->sw_if_index,
404 * if this interface has some adjacencies with counters then ship them,
405 * else continue to the next interface.
407 if (NULL != ctx.counters)
409 ip4_nbr_ship(sm, &ctx);
416 * @brief The context passed when collecting adjacency counters
418 typedef struct ip6_nbr_stats_ctx_t_
421 * The SW IF index all these adjs belong to
426 * A vector of ip6 nbr counters
428 vl_api_ip6_nbr_counter_t *counters;
429 } ip6_nbr_stats_ctx_t;
432 ip6_nbr_stats_cb (adj_index_t ai,
435 vl_api_ip6_nbr_counter_t *vl_counter;
436 vlib_counter_t adj_counter;
437 ip6_nbr_stats_ctx_t *ctx;
441 vlib_get_combined_counter(&adjacency_counters, ai, &adj_counter);
443 if (0 != adj_counter.packets)
445 vec_add2(ctx->counters, vl_counter, 1);
448 vl_counter->packets = clib_host_to_net_u64(adj_counter.packets);
449 vl_counter->bytes = clib_host_to_net_u64(adj_counter.bytes);
450 vl_counter->address[0] = adj->sub_type.nbr.next_hop.ip6.as_u64[0];
451 vl_counter->address[1] = adj->sub_type.nbr.next_hop.ip6.as_u64[1];
452 vl_counter->link_type = adj->ia_link;
454 return (ADJ_WALK_RC_CONTINUE);
457 #define MIN(x,y) (((x)<(y))?(x):(y))
460 ip6_nbr_ship (stats_main_t * sm,
461 ip6_nbr_stats_ctx_t *ctx)
463 api_main_t *am = sm->api_main;
464 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
465 unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
466 vl_api_vnet_ip6_nbr_counters_t *mp = 0;
470 * If the walk context has counters, which may be left over from the last
471 * suspend, then we continue from there.
473 while (0 != vec_len(ctx->counters))
475 u32 n_items = MIN (vec_len (ctx->counters),
476 IP6_FIB_COUNTER_BATCH_SIZE);
479 dslock (sm, 0 /* release hint */ , 1 /* tag */ );
481 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) +
484 (vl_api_ip6_nbr_counter_t)));
485 mp->_vl_msg_id = ntohs (VL_API_VNET_IP6_NBR_COUNTERS);
486 mp->count = ntohl (n_items);
487 mp->sw_if_index = ntohl (ctx->sw_if_index);
492 * copy the counters from the back of the context, then we can easily
493 * 'erase' them by resetting the vector length.
494 * The order we push the stats to the caller is not important.
497 &ctx->counters[vec_len (ctx->counters) - n_items],
498 n_items * sizeof (*ctx->counters));
500 _vec_len (ctx->counters) = vec_len (ctx->counters) - n_items;
505 unix_shared_memory_queue_lock (q);
506 pause = unix_shared_memory_queue_is_full (q);
508 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
509 unix_shared_memory_queue_unlock (q);
513 ip46_fib_stats_delay (sm, 0 /* sec */ ,
514 STATS_RELEASE_DELAY_NS);
519 do_ip6_nbrs (stats_main_t * sm)
521 vnet_main_t *vnm = vnet_get_main ();
522 vnet_interface_main_t *im = &vnm->interface_main;
523 vnet_sw_interface_t *si;
525 ip6_nbr_stats_ctx_t ctx = {
531 pool_foreach (si, im->sw_interfaces,
534 * update the interface we are now concerned with
536 ctx.sw_if_index = si->sw_if_index;
539 * we are about to walk another interface, so we shouldn't have any pending
542 ASSERT(ctx.counters == NULL);
545 * visit each neighbour adjacency on the interface and collect
547 * Because we hold the lock the walk is synchronous, so safe to routing
548 * updates. It's limited in work by the number of adjacenies on an
549 * interface, which is typically not huge.
551 dslock (sm, 0 /* release hint */ , 1 /* tag */ );
552 adj_nbr_walk (si->sw_if_index,
559 * if this interface has some adjacencies with counters then ship them,
560 * else continue to the next interface.
562 if (NULL != ctx.counters)
564 ip6_nbr_ship(sm, &ctx);
571 do_ip4_fibs (stats_main_t * sm)
573 ip4_main_t *im4 = &ip4_main;
574 api_main_t *am = sm->api_main;
575 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
576 unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
577 static ip4_route_t *routes;
581 ip_lookup_main_t *lm = &im4->lookup_main;
582 static uword *results;
583 vl_api_vnet_ip4_fib_counters_t *mp = 0;
584 u32 items_this_message;
585 vl_api_ip4_fib_counter_t *ctrp = 0;
586 u32 start_at_fib_index = 0;
591 pool_foreach (fib, im4->fibs,
593 /* We may have bailed out due to control-plane activity */
594 while ((fib - im4->fibs) < start_at_fib_index)
597 v4_fib = pool_elt_at_index (im4->v4_fibs, fib->ft_index);
601 items_this_message = IP4_FIB_COUNTER_BATCH_SIZE;
602 mp = vl_msg_api_alloc_as_if_client
604 items_this_message * sizeof (vl_api_ip4_fib_counter_t));
605 mp->_vl_msg_id = ntohs (VL_API_VNET_IP4_FIB_COUNTERS);
607 mp->vrf_id = ntohl (fib->ft_table_id);
608 ctrp = (vl_api_ip4_fib_counter_t *) mp->c;
612 /* happens if the last FIB was empty... */
613 ASSERT (mp->count == 0);
614 mp->vrf_id = ntohl (fib->ft_table_id);
617 dslock (sm, 0 /* release hint */ , 1 /* tag */ );
619 vec_reset_length (routes);
620 vec_reset_length (results);
622 for (i = 0; i < ARRAY_LEN (v4_fib->fib_entry_by_dst_address); i++)
624 uword *hash = v4_fib->fib_entry_by_dst_address[i];
628 x.address_length = i;
630 hash_foreach_pair (p, hash,
632 x.address.data_u32 = p->key;
633 x.index = p->value[0];
635 vec_add1 (routes, x);
636 if (sm->data_structure_lock->release_hint)
638 start_at_fib_index = fib - im4->fibs;
640 ip46_fib_stats_delay (sm, 0 /* sec */,
641 STATS_RELEASE_DELAY_NS);
643 ctrp = (vl_api_ip4_fib_counter_t *)mp->c;
649 vec_foreach (r, routes)
653 vlib_get_combined_counter (&load_balance_main.lbm_to_counters,
657 * seen at least one packet, send it.
662 /* already in net byte order */
663 ctrp->address = r->address.as_u32;
664 ctrp->address_length = r->address_length;
665 ctrp->packets = clib_host_to_net_u64 (c.packets);
666 ctrp->bytes = clib_host_to_net_u64 (c.bytes);
670 if (mp->count == items_this_message)
672 mp->count = htonl (items_this_message);
674 * If the main thread's input queue is stuffed,
675 * drop the data structure lock (which the main thread
676 * may want), and take a pause.
678 unix_shared_memory_queue_lock (q);
679 if (unix_shared_memory_queue_is_full (q))
682 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
683 unix_shared_memory_queue_unlock (q);
685 ip46_fib_stats_delay (sm, 0 /* sec */ ,
686 STATS_RELEASE_DELAY_NS);
689 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
690 unix_shared_memory_queue_unlock (q);
692 items_this_message = IP4_FIB_COUNTER_BATCH_SIZE;
693 mp = vl_msg_api_alloc_as_if_client
695 items_this_message * sizeof (vl_api_ip4_fib_counter_t));
696 mp->_vl_msg_id = ntohs (VL_API_VNET_IP4_FIB_COUNTERS);
698 mp->vrf_id = ntohl (fib->ft_table_id);
699 ctrp = (vl_api_ip4_fib_counter_t *) mp->c;
701 } /* for each (mp or single) adj */
702 if (sm->data_structure_lock->release_hint)
704 start_at_fib_index = fib - im4->fibs;
706 ip46_fib_stats_delay (sm, 0 /* sec */ , STATS_RELEASE_DELAY_NS);
708 ctrp = (vl_api_ip4_fib_counter_t *) mp->c;
711 } /* vec_foreach (routes) */
715 /* Flush any data from this fib */
718 mp->count = htonl (mp->count);
719 vl_msg_api_send_shmem (q, (u8 *) & mp);
725 /* If e.g. the last FIB had no reportable routes, free the buffer */
727 vl_msg_api_free (mp);
732 ip6_address_t address;
740 ip6_route_t **routep;
742 } add_routes_in_fib_arg_t;
745 add_routes_in_fib (BVT (clib_bihash_kv) * kvp, void *arg)
747 add_routes_in_fib_arg_t *ap = arg;
748 stats_main_t *sm = ap->sm;
750 if (sm->data_structure_lock->release_hint)
751 clib_longjmp (&sm->jmp_buf, 1);
753 if (kvp->key[2] >> 32 == ap->fib_index)
757 addr = (ip6_address_t *) kvp;
758 vec_add2 (*ap->routep, r, 1);
759 r->address = addr[0];
760 r->address_length = kvp->key[2] & 0xFF;
761 r->index = kvp->value;
766 do_ip6_fibs (stats_main_t * sm)
768 ip6_main_t *im6 = &ip6_main;
769 api_main_t *am = sm->api_main;
770 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
771 unix_shared_memory_queue_t *q = shmem_hdr->vl_input_queue;
772 static ip6_route_t *routes;
775 static uword *results;
776 vl_api_vnet_ip6_fib_counters_t *mp = 0;
777 u32 items_this_message;
778 vl_api_ip6_fib_counter_t *ctrp = 0;
779 u32 start_at_fib_index = 0;
780 BVT (clib_bihash) * h = &im6->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash;
781 add_routes_in_fib_arg_t _a, *a = &_a;
785 pool_foreach (fib, im6->fibs,
787 /* We may have bailed out due to control-plane activity */
788 while ((fib - im6->fibs) < start_at_fib_index)
793 items_this_message = IP6_FIB_COUNTER_BATCH_SIZE;
794 mp = vl_msg_api_alloc_as_if_client
796 items_this_message * sizeof (vl_api_ip6_fib_counter_t));
797 mp->_vl_msg_id = ntohs (VL_API_VNET_IP6_FIB_COUNTERS);
799 mp->vrf_id = ntohl (fib->ft_table_id);
800 ctrp = (vl_api_ip6_fib_counter_t *) mp->c;
803 dslock (sm, 0 /* release hint */ , 1 /* tag */ );
805 vec_reset_length (routes);
806 vec_reset_length (results);
808 a->fib_index = fib - im6->fibs;
812 if (clib_setjmp (&sm->jmp_buf, 0) == 0)
814 start_at_fib_index = fib - im6->fibs;
815 BV (clib_bihash_foreach_key_value_pair) (h, add_routes_in_fib, a);
820 ip46_fib_stats_delay (sm, 0 /* sec */ ,
821 STATS_RELEASE_DELAY_NS);
823 ctrp = (vl_api_ip6_fib_counter_t *) mp->c;
827 vec_foreach (r, routes)
831 vlib_get_combined_counter (&load_balance_main.lbm_to_counters,
835 * seen at least one packet, send it.
839 /* already in net byte order */
840 ctrp->address[0] = r->address.as_u64[0];
841 ctrp->address[1] = r->address.as_u64[1];
842 ctrp->address_length = (u8) r->address_length;
843 ctrp->packets = clib_host_to_net_u64 (c.packets);
844 ctrp->bytes = clib_host_to_net_u64 (c.bytes);
848 if (mp->count == items_this_message)
850 mp->count = htonl (items_this_message);
852 * If the main thread's input queue is stuffed,
853 * drop the data structure lock (which the main thread
854 * may want), and take a pause.
856 unix_shared_memory_queue_lock (q);
857 if (unix_shared_memory_queue_is_full (q))
860 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
861 unix_shared_memory_queue_unlock (q);
863 ip46_fib_stats_delay (sm, 0 /* sec */ ,
864 STATS_RELEASE_DELAY_NS);
867 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
868 unix_shared_memory_queue_unlock (q);
870 items_this_message = IP6_FIB_COUNTER_BATCH_SIZE;
871 mp = vl_msg_api_alloc_as_if_client
873 items_this_message * sizeof (vl_api_ip6_fib_counter_t));
874 mp->_vl_msg_id = ntohs (VL_API_VNET_IP6_FIB_COUNTERS);
876 mp->vrf_id = ntohl (fib->ft_table_id);
877 ctrp = (vl_api_ip6_fib_counter_t *) mp->c;
881 if (sm->data_structure_lock->release_hint)
883 start_at_fib_index = fib - im6->fibs;
885 ip46_fib_stats_delay (sm, 0 /* sec */ , STATS_RELEASE_DELAY_NS);
887 ctrp = (vl_api_ip6_fib_counter_t *) mp->c;
890 } /* vec_foreach (routes) */
894 /* Flush any data from this fib */
897 mp->count = htonl (mp->count);
898 vl_msg_api_send_shmem (q, (u8 *) & mp);
904 /* If e.g. the last FIB had no reportable routes, free the buffer */
906 vl_msg_api_free (mp);
910 stats_thread_fn (void *arg)
912 stats_main_t *sm = &stats_main;
913 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
914 vlib_thread_main_t *tm = vlib_get_thread_main ();
916 /* stats thread wants no signals. */
920 pthread_sigmask (SIG_SETMASK, &s, 0);
923 if (vec_len (tm->thread_prefix))
924 vlib_set_thread_name ((char *)
925 format (0, "%v_stats%c", tm->thread_prefix, '\0'));
927 clib_mem_set_heap (w->thread_mheap);
931 /* 10 second poll interval */
932 ip46_fib_stats_delay (sm, 10 /* secs */ , 0 /* nsec */ );
934 if (!(sm->enable_poller))
936 do_simple_interface_counters (sm);
937 do_combined_interface_counters (sm);
946 vl_api_vnet_interface_counters_t_handler (vl_api_vnet_interface_counters_t *
949 vpe_client_registration_t *reg;
950 stats_main_t *sm = &stats_main;
951 unix_shared_memory_queue_t *q, *q_prev = NULL;
952 vl_api_vnet_interface_counters_t *mp_copy = NULL;
957 u32 count, sw_if_index;
961 mp_size = sizeof (*mp) + (ntohl (mp->count) *
962 (mp->is_combined ? sizeof (vlib_counter_t) :
966 pool_foreach(reg, sm->stats_registrations,
968 q = vl_api_client_index_to_input_queue (reg->client_index);
971 if (q_prev && (q_prev->cursize < q_prev->maxsize))
973 mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
974 clib_memcpy(mp_copy, mp, mp_size);
975 vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
984 count = ntohl (mp->count);
985 sw_if_index = ntohl (mp->first_sw_if_index);
986 if (mp->is_combined == 0)
989 vp = (u64 *) mp->data;
991 switch (mp->vnet_counter_type)
993 case VNET_INTERFACE_COUNTER_DROP:
994 counter_name = "drop";
996 case VNET_INTERFACE_COUNTER_PUNT:
997 counter_name = "punt";
999 case VNET_INTERFACE_COUNTER_IP4:
1000 counter_name = "ip4";
1002 case VNET_INTERFACE_COUNTER_IP6:
1003 counter_name = "ip6";
1005 case VNET_INTERFACE_COUNTER_RX_NO_BUF:
1006 counter_name = "rx-no-buff";
1008 case VNET_INTERFACE_COUNTER_RX_MISS:
1009 , counter_name = "rx-miss";
1011 case VNET_INTERFACE_COUNTER_RX_ERROR:
1012 , counter_name = "rx-error (fifo-full)";
1014 case VNET_INTERFACE_COUNTER_TX_ERROR:
1015 , counter_name = "tx-error (fifo-full)";
1018 counter_name = "bogus";
1021 for (i = 0; i < count; i++)
1023 v = clib_mem_unaligned (vp, u64);
1024 v = clib_net_to_host_u64 (v);
1026 fformat (stdout, "%U.%s %lld\n", format_vnet_sw_if_index_name,
1027 sm->vnet_main, sw_if_index, counter_name, v);
1035 vp = (vlib_counter_t *) mp->data;
1037 switch (mp->vnet_counter_type)
1039 case VNET_INTERFACE_COUNTER_RX:
1040 counter_name = "rx";
1042 case VNET_INTERFACE_COUNTER_TX:
1043 counter_name = "tx";
1046 counter_name = "bogus";
1049 for (i = 0; i < count; i++)
1051 packets = clib_mem_unaligned (&vp->packets, u64);
1052 packets = clib_net_to_host_u64 (packets);
1053 bytes = clib_mem_unaligned (&vp->bytes, u64);
1054 bytes = clib_net_to_host_u64 (bytes);
1056 fformat (stdout, "%U.%s.packets %lld\n",
1057 format_vnet_sw_if_index_name,
1058 sm->vnet_main, sw_if_index, counter_name, packets);
1059 fformat (stdout, "%U.%s.bytes %lld\n",
1060 format_vnet_sw_if_index_name,
1061 sm->vnet_main, sw_if_index, counter_name, bytes);
1066 if (q_prev && (q_prev->cursize < q_prev->maxsize))
1068 vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1072 vl_msg_api_free (mp);
1077 vl_api_vnet_ip4_fib_counters_t_handler (vl_api_vnet_ip4_fib_counters_t * mp)
1079 vpe_client_registration_t *reg;
1080 stats_main_t *sm = &stats_main;
1081 unix_shared_memory_queue_t *q, *q_prev = NULL;
1082 vl_api_vnet_ip4_fib_counters_t *mp_copy = NULL;
1085 mp_size = sizeof (*mp_copy) +
1086 ntohl (mp->count) * sizeof (vl_api_ip4_fib_counter_t);
1089 pool_foreach(reg, sm->stats_registrations,
1091 q = vl_api_client_index_to_input_queue (reg->client_index);
1094 if (q_prev && (q_prev->cursize < q_prev->maxsize))
1096 mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
1097 clib_memcpy(mp_copy, mp, mp_size);
1098 vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
1105 if (q_prev && (q_prev->cursize < q_prev->maxsize))
1107 vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1111 vl_msg_api_free (mp);
1116 vl_api_vnet_ip4_nbr_counters_t_handler (vl_api_vnet_ip4_nbr_counters_t * mp)
1118 vpe_client_registration_t *reg;
1119 stats_main_t *sm = &stats_main;
1120 unix_shared_memory_queue_t *q, *q_prev = NULL;
1121 vl_api_vnet_ip4_nbr_counters_t *mp_copy = NULL;
1124 mp_size = sizeof (*mp_copy) +
1125 ntohl (mp->count) * sizeof (vl_api_ip4_nbr_counter_t);
1128 pool_foreach(reg, sm->stats_registrations,
1130 q = vl_api_client_index_to_input_queue (reg->client_index);
1133 if (q_prev && (q_prev->cursize < q_prev->maxsize))
1135 mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
1136 clib_memcpy(mp_copy, mp, mp_size);
1137 vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
1144 if (q_prev && (q_prev->cursize < q_prev->maxsize))
1146 vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1150 vl_msg_api_free (mp);
1155 vl_api_vnet_ip6_fib_counters_t_handler (vl_api_vnet_ip6_fib_counters_t * mp)
1157 vpe_client_registration_t *reg;
1158 stats_main_t *sm = &stats_main;
1159 unix_shared_memory_queue_t *q, *q_prev = NULL;
1160 vl_api_vnet_ip6_fib_counters_t *mp_copy = NULL;
1163 mp_size = sizeof (*mp_copy) +
1164 ntohl (mp->count) * sizeof (vl_api_ip6_fib_counter_t);
1167 pool_foreach(reg, sm->stats_registrations,
1169 q = vl_api_client_index_to_input_queue (reg->client_index);
1172 if (q_prev && (q_prev->cursize < q_prev->maxsize))
1174 mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
1175 clib_memcpy(mp_copy, mp, mp_size);
1176 vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
1183 if (q_prev && (q_prev->cursize < q_prev->maxsize))
1185 vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1189 vl_msg_api_free (mp);
1194 vl_api_vnet_ip6_nbr_counters_t_handler (vl_api_vnet_ip6_nbr_counters_t * mp)
1196 vpe_client_registration_t *reg;
1197 stats_main_t *sm = &stats_main;
1198 unix_shared_memory_queue_t *q, *q_prev = NULL;
1199 vl_api_vnet_ip6_nbr_counters_t *mp_copy = NULL;
1202 mp_size = sizeof (*mp_copy) +
1203 ntohl (mp->count) * sizeof (vl_api_ip6_nbr_counter_t);
1206 pool_foreach(reg, sm->stats_registrations,
1208 q = vl_api_client_index_to_input_queue (reg->client_index);
1211 if (q_prev && (q_prev->cursize < q_prev->maxsize))
1213 mp_copy = vl_msg_api_alloc_as_if_client(mp_size);
1214 clib_memcpy(mp_copy, mp, mp_size);
1215 vl_msg_api_send_shmem (q_prev, (u8 *)&mp);
1222 if (q_prev && (q_prev->cursize < q_prev->maxsize))
1224 vl_msg_api_send_shmem (q_prev, (u8 *) & mp);
1228 vl_msg_api_free (mp);
1233 vl_api_want_stats_t_handler (vl_api_want_stats_t * mp)
1235 stats_main_t *sm = &stats_main;
1236 vpe_client_registration_t *rp;
1237 vl_api_want_stats_reply_t *rmp;
1240 unix_shared_memory_queue_t *q;
1242 p = hash_get (sm->stats_registration_hash, mp->client_index);
1245 if (mp->enable_disable)
1247 clib_warning ("pid %d: already enabled...", mp->pid);
1253 rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1254 pool_put (sm->stats_registrations, rp);
1255 hash_unset (sm->stats_registration_hash, mp->client_index);
1259 if (mp->enable_disable == 0)
1261 clib_warning ("pid %d: already disabled...", mp->pid);
1265 pool_get (sm->stats_registrations, rp);
1266 rp->client_index = mp->client_index;
1267 rp->client_pid = mp->pid;
1268 hash_set (sm->stats_registration_hash, rp->client_index,
1269 rp - sm->stats_registrations);
1272 if (pool_elts (sm->stats_registrations))
1273 sm->enable_poller = 1;
1275 sm->enable_poller = 0;
1277 q = vl_api_client_index_to_input_queue (mp->client_index);
1282 rmp = vl_msg_api_alloc (sizeof (*rmp));
1283 rmp->_vl_msg_id = ntohs (VL_API_WANT_STATS_REPLY);
1284 rmp->context = mp->context;
1285 rmp->retval = retval;
1287 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1291 stats_memclnt_delete_callback (u32 client_index)
1293 vpe_client_registration_t *rp;
1294 stats_main_t *sm = &stats_main;
1297 p = hash_get (sm->stats_registration_hash, client_index);
1300 rp = pool_elt_at_index (sm->stats_registrations, p[0]);
1301 pool_put (sm->stats_registrations, rp);
1302 hash_unset (sm->stats_registration_hash, client_index);
1308 #define vl_api_vnet_ip4_fib_counters_t_endian vl_noop_handler
1309 #define vl_api_vnet_ip4_fib_counters_t_print vl_noop_handler
1310 #define vl_api_vnet_ip6_fib_counters_t_endian vl_noop_handler
1311 #define vl_api_vnet_ip6_fib_counters_t_print vl_noop_handler
1312 #define vl_api_vnet_ip4_nbr_counters_t_endian vl_noop_handler
1313 #define vl_api_vnet_ip4_nbr_counters_t_print vl_noop_handler
1314 #define vl_api_vnet_ip6_nbr_counters_t_endian vl_noop_handler
1315 #define vl_api_vnet_ip6_nbr_counters_t_print vl_noop_handler
1317 static clib_error_t *
1318 stats_init (vlib_main_t * vm)
1320 stats_main_t *sm = &stats_main;
1321 api_main_t *am = &api_main;
1322 void *vlib_worker_thread_bootstrap_fn (void *arg);
1325 sm->vnet_main = vnet_get_main ();
1326 sm->interface_main = &vnet_get_main ()->interface_main;
1328 sm->stats_poll_interval_in_seconds = 10;
1329 sm->data_structure_lock =
1330 clib_mem_alloc_aligned (sizeof (data_structure_lock_t),
1331 CLIB_CACHE_LINE_BYTES);
1332 memset (sm->data_structure_lock, 0, sizeof (*sm->data_structure_lock));
1335 vl_msg_api_set_handlers(VL_API_##N, #n, \
1336 vl_api_##n##_t_handler, \
1338 vl_api_##n##_t_endian, \
1339 vl_api_##n##_t_print, \
1340 sizeof(vl_api_##n##_t), 0 /* do NOT trace! */);
1344 /* tell the msg infra not to free these messages... */
1345 am->message_bounce[VL_API_VNET_INTERFACE_COUNTERS] = 1;
1346 am->message_bounce[VL_API_VNET_IP4_FIB_COUNTERS] = 1;
1347 am->message_bounce[VL_API_VNET_IP6_FIB_COUNTERS] = 1;
1348 am->message_bounce[VL_API_VNET_IP4_NBR_COUNTERS] = 1;
1349 am->message_bounce[VL_API_VNET_IP6_NBR_COUNTERS] = 1;
1354 VLIB_INIT_FUNCTION (stats_init);
1357 VLIB_REGISTER_THREAD (stats_thread_reg, static) = {
1359 .function = stats_thread_fn,
1362 .no_data_structure_clone = 1,
1368 * fd.io coding-style-patch-verification: ON
1371 * eval: (c-set-style "gnu")