FD.io VPP  v21.06-1-gbb7418cf9
Vector Packet Processing
stat_segment.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 
16 #include <vppinfra/mem.h>
17 #include <vlib/vlib.h>
18 #include <vlib/unix/unix.h>
19 #include "stat_segment.h"
20 #include <vnet/vnet.h>
21 #include <vnet/devices/devices.h> /* vnet_get_aggregate_rx_packets */
23 
25 
26 /*
27  * Used only by VPP writers
28  */
29 void
31 {
34  sm->shared_header->in_progress = 1;
35 }
36 
37 void
39 {
41  sm->shared_header->epoch++;
42  sm->shared_header->in_progress = 0;
44 }
45 
46 /*
47  * Change heap to the stats shared memory segment
48  */
49 void *
51 {
53 
54  sm->last = old;
55  ASSERT (sm && sm->shared_header);
56  return clib_mem_set_heap (sm->heap);
57 }
58 
59 static u32
61 {
64  hash_pair_t *hp;
65 
66  /* Must be called in the context of the main heap */
67  ASSERT (clib_mem_get_heap () != sm->heap);
68 
69  hp = hash_get_pair (sm->directory_vector_by_name, name);
70  if (hp)
71  {
72  index = hp->value[0];
73  }
74 
75  return index;
76 }
77 
78 static void
80 {
82 
83  /* Must be called in the context of the main heap */
84  ASSERT (clib_mem_get_heap () != sm->heap);
85 
86  hash_set (sm->directory_vector_by_name, format (0, "%s%c", name, 0), index);
87 }
88 
89 static u32
91 {
93  u32 next_vector_index = vec_len (sm->directory_vector);
94 
95  ssize_t i;
97  {
99  {
100  next_vector_index = i;
101  break;
102  }
103  }
104 
105  return next_vector_index;
106 }
107 
108 static u32
110 {
112 
113  ASSERT (clib_mem_get_heap () == sm->heap);
114 
116 
117  clib_mem_set_heap (oldheap);
118  create_hash_index ((u8 *) e->name, index);
119  clib_mem_set_heap (sm->heap);
120 
121  vec_validate (sm->directory_vector, index);
122  sm->directory_vector[index] = *e;
123 
124  return index;
125 }
126 
127 static void
129 {
132 
133  ASSERT (clib_mem_get_heap () == sm->heap);
134 
135  if (index > vec_len (sm->directory_vector))
136  return;
137 
138  e = &sm->directory_vector[index];
139 
140  clib_mem_set_heap (oldheap);
142  clib_mem_set_heap (sm->heap);
143 
144  memset (e, 0, sizeof (*e));
146 }
147 
148 /*
149  * Called from main heap
150  */
151 void
152 vlib_stats_delete_cm (void *cm_arg)
153 {
157 
158  /* Not all counters have names / hash-table entries */
159  if (!cm->name && !cm->stat_segment_name)
160  {
161  return;
162  }
164 
165  /* Lookup hash-table is on the main heap */
166  char *stat_segment_name =
167  cm->stat_segment_name ? cm->stat_segment_name : cm->name;
168  u32 index = lookup_hash_index ((u8 *) stat_segment_name);
169 
170  e = &sm->directory_vector[index];
172 
173  void *oldheap = clib_mem_set_heap (sm->heap); /* Enter stats segment */
174  clib_mem_set_heap (oldheap); /* Exit stats segment */
175 
176  memset (e, 0, sizeof (*e));
178 
180 }
181 
182 void
183 vlib_stats_pop_heap (void *cm_arg, void *oldheap, u32 cindex,
185 {
188  stat_segment_shared_header_t *shared_header = sm->shared_header;
189  char *stat_segment_name;
191 
192  /* Not all counters have names / hash-table entries */
193  if (!cm->name && !cm->stat_segment_name)
194  {
195  clib_mem_set_heap (oldheap);
196  return;
197  }
198 
199  ASSERT (shared_header);
200 
202 
203  /* Lookup hash-table is on the main heap */
204  stat_segment_name =
205  cm->stat_segment_name ? cm->stat_segment_name : cm->name;
206 
207  clib_mem_set_heap (oldheap); /* Exit stats segment */
208  u32 vector_index = lookup_hash_index ((u8 *) stat_segment_name);
209  /* Back to stats segment */
210  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */
211 
212 
213  /* Update the vector */
214  if (vector_index == STAT_SEGMENT_INDEX_INVALID)
215  { /* New */
216  strncpy (e.name, stat_segment_name, 128 - 1);
217  e.type = type;
218  vector_index = vlib_stats_create_counter (&e, oldheap);
219  }
220 
221  stat_segment_directory_entry_t *ep = &sm->directory_vector[vector_index];
222  ep->data = cm->counters;
223 
224  /* Reset the client hash table pointer, since it WILL change! */
225  shared_header->directory_vector = sm->directory_vector;
226 
228  clib_mem_set_heap (oldheap);
229 }
230 
231 u8 *
232 format_vlib_stats_symlink (u8 *s, va_list *args)
233 {
234  char *input = va_arg (*args, char *);
235  char *modified_input = vec_dup (input);
236  int i;
237  u8 *result;
238 
239  for (i = 0; i < strlen (modified_input); i++)
240  if (modified_input[i] == '/')
241  modified_input[i] = '_';
242 
243  result = format (s, "%s", modified_input);
244  vec_free (modified_input);
245  return result;
246 }
247 
248 void
249 vlib_stats_register_symlink (void *oldheap, u8 *name, u32 index1, u32 index2,
250  u8 lock)
251 {
253  stat_segment_shared_header_t *shared_header = sm->shared_header;
255 
256  ASSERT (shared_header);
257 
258  if (lock)
260  clib_mem_set_heap (oldheap); /* Exit stats segment */
261  u32 vector_index = lookup_hash_index (name);
262  /* Back to stats segment */
263  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */
264 
265  if (vector_index == STAT_SEGMENT_INDEX_INVALID)
266  {
267  memcpy (e.name, name, vec_len (name));
268  e.name[vec_len (name)] = '\0';
270  e.index1 = index1;
271  e.index2 = index2;
272  vector_index = vlib_stats_create_counter (&e, oldheap);
273 
274  /* Warn clients to refresh any pointers they might be holding */
275  shared_header->directory_vector = sm->directory_vector;
276  }
277 
278  if (lock)
280 }
281 
282 void
283 vlib_stats_rename_symlink (void *oldheap, u64 index, u8 *new_name)
284 {
287 
288  ASSERT (clib_mem_get_heap () == sm->heap);
289 
290  if (index > vec_len (sm->directory_vector))
291  return;
292 
293  e = &sm->directory_vector[index];
294 
295  clib_mem_set_heap (oldheap);
297  clib_mem_set_heap (sm->heap);
298 
299  strncpy (e->name, (char *) new_name, 128 - 1);
300  clib_mem_set_heap (oldheap);
301  hash_set (sm->directory_vector_by_name, &e->name, index);
302  clib_mem_set_heap (sm->heap);
303 }
304 
305 void
306 vlib_stats_register_error_index (void *oldheap, u8 * name, u64 * em_vec,
307  u64 index)
308 {
310  stat_segment_shared_header_t *shared_header = sm->shared_header;
312 
313  ASSERT (shared_header);
314 
316  clib_mem_set_heap (oldheap); /* Exit stats segment */
317  u32 vector_index = lookup_hash_index (name);
318  /* Back to stats segment */
319  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */
320 
321  if (vector_index == STAT_SEGMENT_INDEX_INVALID)
322  {
323  memcpy (e.name, name, vec_len (name));
324  e.name[vec_len (name)] = '\0';
326  e.index = index;
327  vector_index = vlib_stats_create_counter (&e, oldheap);
328 
329  /* Warn clients to refresh any pointers they might be holding */
330  shared_header->directory_vector = sm->directory_vector;
331  }
332 
334 }
335 
336 /*
337  * Creates a two dimensional vector with the maximum valid index specified in
338  * both dimensions as arguments.
339  * Must be called on the stat segment heap.
340  */
341 static void
343  u32 max2)
344 {
345  counter_t **counters = ep->data;
346  int i;
348  for (i = 0; i <= max1; i++)
349  vec_validate_aligned (counters[i], max2, CLIB_CACHE_LINE_BYTES);
350 
351  ep->data = counters;
352 }
353 
354 static void
356 {
358  ASSERT (tm->n_vlib_mains > 0);
360 }
361 
362 void
363 vlib_stats_pop_heap2 (u64 * error_vector, u32 thread_index, void *oldheap,
364  int lock)
365 {
367  stat_segment_shared_header_t *shared_header = sm->shared_header;
368 
369  ASSERT (shared_header);
370 
371  if (lock)
373 
374  /* Reset the client hash table pointer, since it WILL change! */
375  vec_validate (sm->error_vector, thread_index);
376  sm->error_vector[thread_index] = error_vector;
377 
378  shared_header->error_vector = sm->error_vector;
379  shared_header->directory_vector = sm->directory_vector;
380 
381  if (lock)
383  clib_mem_set_heap (oldheap);
384 }
385 
386 /*
387  * Create a new entry and add name to directory hash.
388  * Returns ~0 if name exists.
389  * Called from main heap.
390  * The name is either C-string or nul-terminated vector
391  */
392 u32
394 {
396  stat_segment_shared_header_t *shared_header = sm->shared_header;
397  void *oldheap;
399 
400  ASSERT (shared_header);
401 
402  u32 vector_index = lookup_hash_index (name);
403  if (vector_index != STAT_SEGMENT_INDEX_INVALID) /* Already registered */
404  return ~0;
405 
406  memset (&e, 0, sizeof (e));
407  e.type = t;
408  strcpy_s (e.name, sizeof (e.name), (char *) name);
409 
410  oldheap = vlib_stats_push_heap (NULL);
412  vector_index = vlib_stats_create_counter (&e, oldheap);
413 
414  shared_header->directory_vector = sm->directory_vector;
415 
417  clib_mem_set_heap (oldheap);
418 
419  return vector_index;
420 }
421 
422 clib_error_t *
424 {
426  stat_segment_shared_header_t *shared_header;
427  void *oldheap;
428  uword memory_size, sys_page_sz;
429  int mfd;
430  char *mem_name = "stat segment";
431  void *heap, *memaddr;
432 
433  memory_size = sm->memory_size;
434  if (memory_size == 0)
435  memory_size = STAT_SEGMENT_DEFAULT_SIZE;
436 
439 
440  mfd = clib_mem_vm_create_fd (sm->log2_page_sz, mem_name);
441 
442  if (mfd == -1)
443  return clib_error_return (0, "stat segment memory fd failure: %U",
445  /* Set size */
446  if ((ftruncate (mfd, memory_size)) == -1)
447  {
448  close (mfd);
449  return clib_error_return (0, "stat segment ftruncate failure");
450  }
451 
452  memaddr = clib_mem_vm_map_shared (0, memory_size, mfd, 0, mem_name);
453 
454  if (memaddr == CLIB_MEM_VM_MAP_FAILED)
455  return clib_error_return (0, "stat segment mmap failure");
456 
457  sys_page_sz = clib_mem_get_page_size ();
458 
459  heap =
460  clib_mem_create_heap (((u8 *) memaddr) + sys_page_sz,
461  memory_size - sys_page_sz, 1 /* locked */, mem_name);
462  sm->heap = heap;
463  sm->memfd = mfd;
464 
466  sm->shared_header = shared_header = memaddr;
467 
468  shared_header->version = STAT_SEGMENT_VERSION;
469  shared_header->base = memaddr;
470 
473 
474  oldheap = clib_mem_set_heap (sm->heap);
475 
476  /* Set up the name to counter-vector hash table */
477  sm->directory_vector = 0;
478 
479  shared_header->epoch = 1;
480 
481  /* Scalar stats and node counters */
483 #define _(E,t,n,p) \
484  strcpy(sm->directory_vector[STAT_COUNTER_##E].name, #p "/" #n); \
485  sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
487 #undef _
488  /* Save the vector in the shared segment, for clients */
489  shared_header->directory_vector = sm->directory_vector;
490 
491  clib_mem_set_heap (oldheap);
492 
494 
495  return 0;
496 }
497 
498 static int
499 name_sort_cmp (void *a1, void *a2)
500 {
503 
504  return strcmp ((char *) n1->name, (char *) n2->name);
505 }
506 
507 static u8 *
508 format_stat_dir_entry (u8 * s, va_list * args)
509 {
511  va_arg (*args, stat_segment_directory_entry_t *);
512  char *type_name;
513  char *format_string;
514 
515  format_string = "%-74s %-10s %10lld";
516 
517  switch (ep->type)
518  {
520  type_name = "ScalarPtr";
521  break;
522 
525  type_name = "CMainPtr";
526  break;
527 
529  type_name = "ErrIndex";
530  break;
531 
533  type_name = "NameVector";
534  break;
535 
536  case STAT_DIR_TYPE_EMPTY:
537  type_name = "empty";
538  break;
539 
541  type_name = "Symlink";
542  break;
543 
544  default:
545  type_name = "illegal!";
546  break;
547  }
548 
549  return format (s, format_string, ep->name, type_name, 0);
550 }
551 
552 static clib_error_t *
554  unformat_input_t * input,
555  vlib_cli_command_t * cmd)
556 {
559  int i;
560 
561  int verbose = 0;
562 
563  if (unformat (input, "verbose"))
564  verbose = 1;
565 
566  /* Lock even as reader, as this command doesn't handle epoch changes */
568  show_data = vec_dup (sm->directory_vector);
570 
572 
573  vlib_cli_output (vm, "%-74s %10s %10s", "Name", "Type", "Value");
574 
575  for (i = 0; i < vec_len (show_data); i++)
576  {
578 
579  if (ep->type == STAT_DIR_TYPE_EMPTY)
580  continue;
581 
582  vlib_cli_output (vm, "%-100U", format_stat_dir_entry,
583  vec_elt_at_index (show_data, i));
584  }
585 
586  if (verbose)
587  {
588  ASSERT (sm->heap);
589  vlib_cli_output (vm, "%U", format_clib_mem_heap, sm->heap,
590  0 /* verbose */ );
591  }
592 
593  return 0;
594 }
595 
596 /* *INDENT-OFF* */
597 VLIB_CLI_COMMAND (show_stat_segment_command, static) =
598 {
599  .path = "show statistics segment",
600  .short_help = "show statistics segment [verbose]",
601  .function = show_stat_segment_command_fn,
602 };
603 /* *INDENT-ON* */
604 
605 /*
606  * Node performance counters:
607  * total_calls [threads][node-index]
608  * total_vectors
609  * total_calls
610  * total suspends
611  */
612 
613 static inline void
615 {
616  vlib_main_t **stat_vms = 0;
617  vlib_node_t ***node_dups = 0;
618  int i, j;
619  static u32 no_max_nodes = 0;
620 
621  vlib_node_get_nodes (0 /* vm, for barrier sync */,
622  (u32) ~0 /* all threads */, 1 /* include stats */,
623  0 /* barrier sync */, &node_dups, &stat_vms);
624 
625  u32 l = vec_len (node_dups[0]);
626  u8 *symlink_name = 0;
627 
628  /*
629  * Extend performance nodes if necessary
630  */
631  if (l > no_max_nodes)
632  {
633  void *oldheap = clib_mem_set_heap (sm->heap);
635 
644 
645  vec_validate (sm->nodes, l - 1);
648  ep->data = sm->nodes;
649 
650  /* Update names dictionary */
651  vlib_node_t **nodes = node_dups[0];
652  int i;
653  for (i = 0; i < vec_len (nodes); i++)
654  {
655  vlib_node_t *n = nodes[i];
656  u8 *s = 0;
657  s = format (s, "%v%c", n->name, 0);
658  if (sm->nodes[n->index])
659  vec_free (sm->nodes[n->index]);
660  sm->nodes[n->index] = s;
661 
662 #define _(E, t, name, p) \
663  vec_reset_length (symlink_name); \
664  symlink_name = format (symlink_name, "/nodes/%U/" #name "%c", \
665  format_vlib_stats_symlink, s, 0); \
666  vlib_stats_register_symlink (oldheap, symlink_name, STAT_COUNTER_##E, \
667  n->index, 0 /* don't lock */);
669 #undef _
670  }
671  vec_free (symlink_name);
673  clib_mem_set_heap (oldheap);
674  no_max_nodes = l;
675  }
676 
677  for (j = 0; j < vec_len (node_dups); j++)
678  {
679  vlib_node_t **nodes = node_dups[j];
680 
681  for (i = 0; i < vec_len (nodes); i++)
682  {
683  counter_t **counters;
684  counter_t *c;
685  vlib_node_t *n = nodes[i];
686 
687  if (j == 0)
688  {
689  if (strncmp ((char *) sm->nodes[n->index], (char *) n->name,
690  strlen ((char *) sm->nodes[n->index])))
691  {
692  u8 *s = 0;
693  u32 vector_index;
694  u8 *symlink_new_name = 0;
695  void *oldheap = clib_mem_set_heap (sm->heap);
697  s = format (s, "%v%c", n->name, 0);
698 #define _(E, t, name, p) \
699  vec_reset_length (symlink_name); \
700  symlink_name = format (symlink_name, "/nodes/%U/" #name "%c", \
701  format_vlib_stats_symlink, sm->nodes[n->index], 0); \
702  clib_mem_set_heap (oldheap); /* Exit stats segment */ \
703  vector_index = lookup_hash_index ((u8 *) symlink_name); \
704  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */ \
705  vec_reset_length (symlink_new_name); \
706  symlink_new_name = format (symlink_new_name, "/nodes/%U/" #name "%c", \
707  format_vlib_stats_symlink, s, 0); \
708  vlib_stats_rename_symlink (oldheap, vector_index, symlink_new_name);
710 #undef _
711  vec_free (symlink_name);
712  vec_free (symlink_new_name);
713  vec_free (sm->nodes[n->index]);
714  sm->nodes[n->index] = s;
716  clib_mem_set_heap (oldheap);
717  }
718  }
719 
721  c = counters[j];
723 
725  c = counters[j];
727 
729  c = counters[j];
730  c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
731 
733  c = counters[j];
735  }
736  vec_free (node_dups[j]);
737  }
738  vec_free (node_dups);
739  vec_free (stat_vms);
740 }
741 
742 static void
744 {
745  u64 input_packets;
746  f64 dt, now;
747  static int num_worker_threads_set;
748 
749  /*
750  * Set once at the beginning of time.
751  * Can't do this from the init routine, which happens before
752  * start_workers sets up vlib_mains...
753  */
754  if (PREDICT_FALSE (num_worker_threads_set == 0))
755  {
757  ASSERT (tm->n_vlib_mains > 0);
760  tm->n_vlib_mains - 1;
761  num_worker_threads_set = 1;
762  }
763 
764  /*
765  * Compute the aggregate input rate
766  */
767  now = vlib_time_now (vm);
769  input_packets = vnet_get_aggregate_rx_packets ();
771  (f64) (input_packets - sm->last_input_packets) / dt;
773  sm->last_input_packets = input_packets;
776 
777  if (sm->node_counters_enabled)
779 
780  /* *INDENT-OFF* */
782  pool_foreach (g, sm->gauges)
783  {
785  }
786  /* *INDENT-ON* */
787 
788  /* Heartbeat, so clients detect we're still here */
790 }
791 
792 /*
793  * Accept connection on the socket and exchange the fd for the shared
794  * memory segment.
795  */
796 static clib_error_t *
798 {
800  clib_error_t *err;
801  clib_socket_t client = { 0 };
802 
803  err = clib_socket_accept (sm->socket, &client);
804  if (err)
805  {
806  clib_error_report (err);
807  return err;
808  }
809 
810  /* Send the fd across and close */
811  err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
812  if (err)
813  clib_error_report (err);
814  clib_socket_close (&client);
815 
816  return 0;
817 }
818 
819 static clib_error_t *
821 {
825 
826  memset (s, 0, sizeof (clib_socket_t));
827  s->config = (char *) sm->socket_name;
830 
831  if ((error = clib_socket_init (s)))
832  return error;
833 
834  clib_file_t template = { 0 };
836  template.file_descriptor = s->fd;
837  template.description = format (0, "stats segment listener %s", s->config);
838  clib_file_add (&file_main, &template);
839 
840  sm->socket = s;
841 
842  return 0;
843 }
844 
845 static clib_error_t *
847 {
848  /*
849  * cleanup the listener socket on exit.
850  */
852  unlink ((char *) sm->socket_name);
853  return 0;
854 }
855 
857 
858 /* Overrides weak reference in vlib:node_cli.c */
859 f64
861 {
862  return stat_segment_main.update_interval;
863 }
864 
865 static uword
867  vlib_frame_t * f)
868 {
870 
871  while (1)
872  {
873  do_stat_segment_updates (vm, sm);
875  }
876  return 0; /* or not */
877 }
878 
879 /*
880  * Add a data provider (via callback) for a given stats entry.
881  * TODO: Add support for per-provider interval.
882  */
883 void
885  u32 caller_index, u32 interval)
886 {
889 
890  pool_get (sm->gauges, gauge);
891  gauge->fn = update_fn;
892  gauge->caller_index = caller_index;
893  gauge->directory_index = vector_index;
894 
895  return;
896 }
897 
898 /*
899  * Create an scalar entry with a data provider.
900  * Deprecated, replace with stat_segment_new_entry + stat_segment_pool_add
901  */
902 clib_error_t *
904  u32 caller_index)
905 {
908 
910  if (vector_index == ~0) /* Already registered */
911  return clib_error_return (0, "%v is already registered", name);
912 
913  pool_get (sm->gauges, gauge);
914  gauge->fn = update_fn;
915  gauge->caller_index = caller_index;
916  gauge->directory_index = vector_index;
917 
918  return NULL;
919 }
920 
921 clib_error_t *
923 {
924  ASSERT (vlib_get_thread_index () == 0);
925 
927  if (vector_index == ~0) /* Already registered */
928  return clib_error_return (0, "%v is already registered", name);
929  *index = vector_index;
930  return 0;
931 }
932 
933 clib_error_t *
935 {
937  stat_segment_shared_header_t *shared_header = sm->shared_header;
939  void *oldheap;
940 
941  ASSERT (shared_header);
942 
943  if (index > vec_len (sm->directory_vector))
944  return clib_error_return (0, "%u index does not exist", index);
945 
946  e = &sm->directory_vector[index];
948  return clib_error_return (0, "%u index cannot be deleted", index);
949 
950  oldheap = vlib_stats_push_heap (NULL);
952 
953  vlib_stats_delete_counter (index, oldheap);
954 
956  clib_mem_set_heap (oldheap);
957 
958  return 0;
959 }
960 
961 void
963 {
965 
966  ASSERT (index < vec_len (sm->directory_vector));
968 }
969 
970 static clib_error_t *
972 {
974  sm->update_interval = 10.0;
975 
977  {
978  if (unformat (input, "socket-name %s", &sm->socket_name))
979  ;
980  /* DEPRECATE: default (does nothing) */
981  else if (unformat (input, "default"))
982  ;
983  else if (unformat (input, "size %U",
985  ;
986  else if (unformat (input, "page-size %U",
988  ;
989  else if (unformat (input, "per-node-counters on"))
990  sm->node_counters_enabled = 1;
991  else if (unformat (input, "per-node-counters off"))
992  sm->node_counters_enabled = 0;
993  else if (unformat (input, "update-interval %f", &sm->update_interval))
994  ;
995  else
996  return clib_error_return (0, "unknown input `%U'",
997  format_unformat_error, input);
998  }
999 
1000  /*
1001  * NULL-terminate socket name string
1002  * clib_socket_init()->socket_config() use C str*
1003  */
1004  if (vec_len (sm->socket_name))
1006 
1007  return 0;
1008 }
1009 
1011 
1012 static clib_error_t *
1014 {
1016  vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
1017  vnet_sw_interface_t *si_sup =
1019  vnet_hw_interface_t *hi_sup;
1020  u8 *s = 0;
1021  u8 *symlink_name = 0;
1022  u32 vector_index;
1023 
1024  void *oldheap = vlib_stats_push_heap (sm->interfaces);
1026 
1027  vec_validate (sm->interfaces, sw_if_index);
1028 
1030  hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
1031 
1032  s = format (s, "%v", hi_sup->name);
1034  s = format (s, ".%d", si->sub.id);
1035  s = format (s, "%c", 0);
1036 
1037  if (is_add)
1038  {
1039  sm->interfaces[sw_if_index] = s;
1040 #define _(E, n, p) \
1041  clib_mem_set_heap (oldheap); /* Exit stats segment */ \
1042  vector_index = lookup_hash_index ((u8 *) "/" #p "/" #n); \
1043  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */ \
1044  vec_reset_length (symlink_name); \
1045  symlink_name = format (symlink_name, "/interfaces/%U/" #n "%c", \
1046  format_vlib_stats_symlink, s, 0); \
1047  vlib_stats_register_symlink (oldheap, symlink_name, vector_index, \
1048  sw_if_index, 0 /* don't lock */);
1051 #undef _
1052 
1053  vec_free (symlink_name);
1054  }
1055  else
1056  {
1057  vec_free (sm->interfaces[sw_if_index]);
1058  sm->interfaces[sw_if_index] = 0;
1059 #define _(E, n, p) \
1060  vec_reset_length (symlink_name); \
1061  symlink_name = format (symlink_name, "/interfaces/%U/" #n "%c", \
1062  format_vlib_stats_symlink, s, 0); \
1063  clib_mem_set_heap (oldheap); /* Exit stats segment */ \
1064  vector_index = lookup_hash_index ((u8 *) symlink_name); \
1065  clib_mem_set_heap (sm->heap); /* Re-enter stat segment */ \
1066  vlib_stats_delete_counter (vector_index, oldheap);
1069 #undef _
1070 
1071  vec_free (symlink_name);
1072  }
1073 
1076  ep->data = sm->interfaces;
1077 
1079  clib_mem_set_heap (oldheap);
1080 
1081  return 0;
1082 }
1083 
1085 
1087 {
1088 .function = stat_segment_collector_process,
1089 .name = "statseg-collector-process",
1090 .type = VLIB_NODE_TYPE_PROCESS,
1091 };
1092 
1093 static clib_error_t *
1095 {
1097 
1098  /* set default socket file name when statseg config stanza is empty. */
1099  if (!vec_len (sm->socket_name))
1100  sm->socket_name = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
1102  return stats_segment_socket_init ();
1103 }
1104 
1106  .runs_after = VLIB_INITS ("unix_input_init"),
1107 };
1108 
1109 /*
1110  * fd.io coding-style-patch-verification: ON
1111  *
1112  * Local Variables:
1113  * eval: (c-set-style "gnu")
1114  * End:
1115  */
static clib_error_t * statseg_config(vlib_main_t *vm, unformat_input_t *input)
Definition: stat_segment.c:971
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:524
__clib_export int clib_mem_vm_create_fd(clib_mem_page_sz_t log2_page_size, char *fmt,...)
Definition: mem.c:263
void vlib_node_get_nodes(vlib_main_t *vm, u32 max_threads, int include_stats, int barrier_sync, vlib_node_t ****node_dupsp, vlib_main_t ***stat_vmsp)
Get list of nodes.
Definition: node.c:603
#define CLIB_MEM_VM_MAP_FAILED
Definition: mem.h:54
#define hash_set(h, key, value)
Definition: hash.h:255
vnet_interface_output_runtime_t * rt
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:121
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:82
void vlib_stats_pop_heap2(u64 *error_vector, u32 thread_index, void *oldheap, int lock)
Definition: stat_segment.c:363
stat_segment_gauges_pool_t * gauges
Definition: stat_segment.h:76
void vlib_stats_register_mem_heap(clib_mem_heap_t *heap)
#define hash_unset(h, key)
Definition: hash.h:261
static u64 vnet_get_aggregate_rx_packets(void)
Definition: devices.h:74
stat_segment_shared_header_t * shared_header
Definition: stat_segment.h:96
uint64_t index
static clib_error_t * stats_socket_accept_ready(clib_file_t *uf)
Definition: stat_segment.c:797
stat_segment_main_t stat_segment_main
Definition: stat_segment.c:24
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
clib_mem_heap_t * clib_mem_create_heap(void *base, uword size, int is_locked, char *fmt,...)
Definition: mem_dlmalloc.c:536
u32 thread_index
stat_segment_directory_entry_t * directory_vector
Definition: stat_segment.h:80
unsigned long u64
Definition: types.h:89
__clib_export void * clib_mem_vm_map_shared(void *base, uword size, int fd, uword offset, char *fmt,...)
Definition: mem.c:59
__clib_export errno_t strcpy_s(char *__restrict__ dest, rsize_t dmax, const char *__restrict__ src)
copy src string to dest string
Definition: string.c:272
vnet_feature_config_main_t * cm
u32 index
Definition: node.h:270
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
static clib_mem_heap_t * clib_mem_set_heap(clib_mem_heap_t *heap)
Definition: mem.h:365
static void stat_validate_counter_vector(stat_segment_directory_entry_t *ep, u32 max)
Definition: stat_segment.c:355
#define STAT_SEGMENT_DEFAULT_SIZE
Definition: stat_segment.h:58
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define vec_terminate_c_string(V)
(If necessary) NULL terminate a vector containing a c-string.
Definition: vec.h:1133
void * vlib_stats_push_heap(void *old)
Definition: stat_segment.c:50
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
string name[64]
Definition: fib.api:25
#define foreach_stat_segment_counter_name
Definition: stat_segment.h:46
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:535
#define vec_foreach_index_backwards(var, v)
Iterate over vector indices (reverse).
void * data
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
static_always_inline uword clib_mem_get_page_size(void)
Definition: mem.h:468
unsigned char u8
Definition: types.h:56
void(* stat_segment_update_fn)(stat_segment_directory_entry_t *e, u32 i)
Definition: stat_segment.h:65
__clib_export clib_error_t * clib_socket_init(clib_socket_t *s)
Definition: socket.c:390
void vlib_stats_register_symlink(void *oldheap, u8 *name, u32 index1, u32 index2, u8 lock)
Definition: stat_segment.c:249
clib_file_function_t * read_function
Definition: file.h:67
double f64
Definition: types.h:142
uword value[0]
Definition: hash.h:165
unsigned int u32
Definition: types.h:88
void stat_segment_poll_add(u32 vector_index, stat_segment_update_fn update_fn, u32 caller_index, u32 interval)
Definition: stat_segment.c:884
static void create_hash_index(u8 *name, u32 index)
Definition: stat_segment.c:79
#define CLIB_SOCKET_F_IS_SERVER
Definition: socket.h:58
static u32 vlib_stats_get_next_vector_index()
Definition: stat_segment.c:90
vlib_frame_t * f
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:486
vlib_node_stats_t stats_last_clear
Definition: node.h:264
if(node->flags &VLIB_NODE_FLAG_TRACE) vnet_interface_output_trace(vm
uint64_t value
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
static u32 vlib_stats_create_counter(stat_segment_directory_entry_t *e, void *oldheap)
Definition: stat_segment.c:109
void vlib_stats_pop_heap(void *cm_arg, void *oldheap, u32 cindex, stat_directory_type_t type)
Definition: stat_segment.c:183
static char * vlib_unix_get_runtime_dir(void)
Definition: unix.h:151
static clib_error_t * clib_socket_sendmsg(clib_socket_t *s, void *msg, int msglen, int fds[], int num_fds)
Definition: socket.h:151
uint64_t counter_t
64bit counters
Definition: counter_types.h:22
description fragment has unexpected format
Definition: map.api:433
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static vnet_sw_interface_t * vnet_get_sup_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
#define clib_error_return(e, args...)
Definition: error.h:99
clib_file_main_t file_main
Definition: main.c:63
#define hash_get_pair(h, key)
Definition: hash.h:252
stat_segment_update_fn fn
Definition: stat_segment.h:69
static clib_error_t * statseg_init(vlib_main_t *vm)
A collection of simple counters.
Definition: counter.h:57
static u32 lookup_hash_index(u8 *name)
Definition: stat_segment.c:60
static clib_error_t * stats_segment_socket_init(void)
Definition: stat_segment.c:820
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:65
char * name
The counter collection&#39;s name.
Definition: counter.h:60
vl_api_fib_path_type_t type
Definition: fib_types.api:123
uint32_t index1
static void vlib_stats_delete_counter(u32 index, void *oldheap)
Definition: stat_segment.c:128
Definition: cJSON.c:88
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
vlib_node_stats_t stats_total
Definition: node.h:260
vnet_sub_interface_t sub
Definition: interface.h:892
stat_directory_type_t
f64 time_last_runtime_stats_clear
Definition: node.h:731
static clib_error_t * clib_socket_close(clib_socket_t *sock)
Definition: socket.h:175
static u8 * format_stat_dir_entry(u8 *s, va_list *args)
Definition: stat_segment.c:508
void vlib_stat_segment_unlock(void)
Definition: stat_segment.c:38
struct _unformat_input_t unformat_input_t
u64 memory_size
Definition: vhost_user.h:124
static clib_error_t * stats_segment_socket_exit(vlib_main_t *vm)
Definition: stat_segment.c:846
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:444
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
u8 * name
Definition: node.h:254
#define foreach_simple_interface_counter_name
Definition: interface.h:935
volatile uint64_t ** error_vector
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:220
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
#define foreach_combined_interface_counter_name
Definition: interface.h:946
svmdb_client_t * c
clib_error_t * stat_segment_register_gauge(u8 *name, stat_segment_update_fn update_fn, u32 caller_index)
Definition: stat_segment.c:903
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:208
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
u32 index
Definition: flow_types.api:221
__clib_export clib_error_t * clib_socket_accept(clib_socket_t *server, clib_socket_t *client)
Definition: socket.c:542
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:177
unformat_function_t unformat_log2_page_size
Definition: format.h:294
static vlib_node_registration_t stat_segment_collector
(constructor) VLIB_REGISTER_NODE (stat_segment_collector)
void vlib_stat_segment_lock(void)
Definition: stat_segment.c:30
VNET_SW_INTERFACE_ADD_DEL_FUNCTION(statseg_sw_interface_add_del)
void stat_provider_register_vector_rate(u32 num_workers)
#define STAT_SEGMENT_SOCKET_FILENAME
Definition: stat_client.h:33
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
static clib_mem_heap_t * clib_mem_get_heap(void)
Definition: mem.h:359
char name[128]
clib_error_t * stat_segment_deregister_state_counter(u32 index)
Definition: stat_segment.c:934
u8 value
Definition: qos.api:54
#define ASSERT(truth)
u8 * format_clib_mem_heap(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:422
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
vnet_sw_interface_t * si
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:96
f64 vlib_get_stat_segment_update_rate(void)
Definition: stat_segment.c:860
volatile stat_segment_directory_entry_t * directory_vector
void vlib_stats_rename_symlink(void *oldheap, u64 index, u8 *new_name)
Definition: stat_segment.c:283
u16 interval
Definition: vrrp.api:34
#define clib_error_report(e)
Definition: error.h:113
struct _socket_t clib_socket_t
u8 * format_vlib_stats_symlink(u8 *s, va_list *args)
Definition: stat_segment.c:232
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
#define CLIB_SOCKET_F_SEQPACKET
Definition: socket.h:63
#define STAT_SEGMENT_VERSION
Definition: stat_segment.h:61
volatile u64 ** error_vector
Definition: stat_segment.h:81
static_always_inline clib_error_t * clib_mem_get_last_error(void)
Definition: mem.h:553
static uword stat_segment_collector_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: stat_segment.c:866
u32 stat_segment_new_entry(u8 *name, stat_directory_type_t t)
Definition: stat_segment.c:393
uword * directory_vector_by_name
Definition: stat_segment.h:79
char * stat_segment_name
Name in stat segment directory.
Definition: counter.h:61
static void stat_validate_counter_vector2(stat_segment_directory_entry_t *ep, u32 max1, u32 max2)
Definition: stat_segment.c:342
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_node_main_t node_main
Definition: main.h:171
#define CLIB_SOCKET_F_PASSCRED
Definition: socket.h:64
u64 uword
Definition: types.h:112
static void update_node_counters(stat_segment_main_t *sm)
Definition: stat_segment.c:614
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1098
static void do_stat_segment_updates(vlib_main_t *vm, stat_segment_main_t *sm)
Definition: stat_segment.c:743
#define foreach_stat_segment_node_counter_name
Definition: stat_segment.h:40
static int name_sort_cmp(void *a1, void *a2)
Definition: stat_segment.c:499
static clib_error_t * statseg_sw_interface_add_del(vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
static clib_error_t * show_stat_segment_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: stat_segment.c:553
unformat_function_t unformat_memory_size
Definition: format.h:288
uint32_t index2
clib_socket_t * socket
Definition: stat_segment.h:89
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
counter_t ** counters
Per-thread u64 non-atomic counters.
Definition: counter.h:59
vnet_sw_interface_type_t type
Definition: interface.h:870
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:56
f64 now
Definition: file.h:51
void vlib_stats_delete_cm(void *cm_arg)
Definition: stat_segment.c:152
void vlib_stats_register_error_index(void *oldheap, u8 *name, u64 *em_vec, u64 index)
Definition: stat_segment.c:306
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
#define CLIB_SOCKET_F_ALLOW_GROUP_WRITE
Definition: socket.h:62
clib_error_t * vlib_map_stat_segment_init(void)
Definition: stat_segment.c:423
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
#define VLIB_INITS(...)
Definition: init.h:352
#define STAT_SEGMENT_INDEX_INVALID
Definition: stat_segment.h:63
clib_mem_page_sz_t log2_page_sz
Definition: stat_segment.h:92
clib_error_t * stat_segment_register_state_counter(u8 *name, u32 *index)
Definition: stat_segment.c:922
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
void stat_segment_set_state_counter(u32 index, u64 value)
Definition: stat_segment.c:962
clib_spinlock_t * stat_segment_lockp
Definition: stat_segment.h:88
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
stat_directory_type_t type