FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
threads.c
Go to the documentation of this file.
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 #define _GNU_SOURCE
16 
17 #include <signal.h>
18 #include <math.h>
19 #include <vppinfra/format.h>
20 #include <vlib/vlib.h>
21 
22 #include <vlib/threads.h>
23 #include <vlib/unix/cj.h>
24 
26 
27 #define FRAME_QUEUE_NELTS 32
28 
29 u32
30 vl (void *p)
31 {
32  return vec_len (p);
33 }
34 
37 
38 uword
40 {
41  void *sp;
42  uword n;
43  u32 len;
44 
46  if (len == 0)
47  return 0;
48 
49  /* Get any old stack address. */
50  sp = &sp;
51 
52  n = ((uword) sp - (uword) vlib_thread_stacks[0])
54 
55  /* "processes" have their own stacks, and they always run in thread 0 */
56  n = n >= len ? 0 : n;
57 
58  return n;
59 }
60 
61 uword
63 {
64  u32 len;
65 
67  if (len == 0)
68  return 1;
69  else
70  return len;
71 }
72 
73 void
75 {
76  int pthread_setname_np (pthread_t __target_thread, const char *__name);
77  int rv;
78  pthread_t thread = pthread_self ();
79 
80  if (thread)
81  {
82  rv = pthread_setname_np (thread, name);
83  if (rv)
84  clib_warning ("pthread_setname_np returned %d", rv);
85  }
86 }
87 
88 static int
89 sort_registrations_by_no_clone (void *a0, void *a1)
90 {
91  vlib_thread_registration_t **tr0 = a0;
92  vlib_thread_registration_t **tr1 = a1;
93 
94  return ((i32) ((*tr0)->no_data_structure_clone)
95  - ((i32) ((*tr1)->no_data_structure_clone)));
96 }
97 
98 static uword *
99 vlib_sysfs_list_to_bitmap (char *filename)
100 {
101  FILE *fp;
102  uword *r = 0;
103 
104  fp = fopen (filename, "r");
105 
106  if (fp != NULL)
107  {
108  u8 *buffer = 0;
109  vec_validate (buffer, 256 - 1);
110  if (fgets ((char *) buffer, 256, fp))
111  {
112  unformat_input_t in;
113  unformat_init_string (&in, (char *) buffer,
114  strlen ((char *) buffer));
115  if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
116  clib_warning ("unformat_bitmap_list failed");
117  unformat_free (&in);
118  }
119  vec_free (buffer);
120  fclose (fp);
121  }
122  return r;
123 }
124 
125 
126 /* Called early in the init sequence */
127 
128 clib_error_t *
130 {
134  u32 n_vlib_mains = 1;
135  u32 first_index = 1;
136  u32 i;
137  uword *avail_cpu;
138 
139  /* get bitmaps of active cpu cores and sockets */
140  tm->cpu_core_bitmap =
141  vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
142  tm->cpu_socket_bitmap =
143  vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
144 
145  avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
146 
147  /* skip cores */
148  for (i = 0; i < tm->skip_cores; i++)
149  {
150  uword c = clib_bitmap_first_set (avail_cpu);
151  if (c == ~0)
152  return clib_error_return (0, "no available cpus to skip");
153 
154  avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
155  }
156 
157  /* grab cpu for main thread */
158  if (!tm->main_lcore)
159  {
160  tm->main_lcore = clib_bitmap_first_set (avail_cpu);
161  if (tm->main_lcore == (u8) ~ 0)
162  return clib_error_return (0, "no available cpus to be used for the"
163  " main thread");
164  }
165  else
166  {
167  if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
168  return clib_error_return (0, "cpu %u is not available to be used"
169  " for the main thread", tm->main_lcore);
170  }
171  avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
172 
173  /* assume that there is socket 0 only if there is no data from sysfs */
174  if (!tm->cpu_socket_bitmap)
175  tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
176 
177  /* pin main thread to main_lcore */
179  {
181  }
182  else
183  {
184  cpu_set_t cpuset;
185  CPU_ZERO (&cpuset);
186  CPU_SET (tm->main_lcore, &cpuset);
187  pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
188  }
189 
190  /* as many threads as stacks... */
191  vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
193 
194  /* Preallocate thread 0 */
195  _vec_len (vlib_worker_threads) = 1;
199  w->lcore_id = tm->main_lcore;
200  w->lwp = syscall (SYS_gettid);
201  w->thread_id = pthread_self ();
202  tm->n_vlib_mains = 1;
203 
204  if (tm->sched_policy != ~0)
205  {
206  struct sched_param sched_param;
207  if (!sched_getparam (w->lwp, &sched_param))
208  {
209  if (tm->sched_priority != ~0)
210  sched_param.sched_priority = tm->sched_priority;
211  sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
212  }
213  }
214 
215  /* assign threads to cores and set n_vlib_mains */
216  tr = tm->next;
217 
218  while (tr)
219  {
220  vec_add1 (tm->registrations, tr);
221  tr = tr->next;
222  }
223 
225 
226  for (i = 0; i < vec_len (tm->registrations); i++)
227  {
228  int j;
229  tr = tm->registrations[i];
230  tr->first_index = first_index;
231  first_index += tr->count;
232  n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
233 
234  /* construct coremask */
235  if (tr->use_pthreads || !tr->count)
236  continue;
237 
238  if (tr->coremask)
239  {
240  uword c;
241  /* *INDENT-OFF* */
242  clib_bitmap_foreach (c, tr->coremask, ({
243  if (clib_bitmap_get(avail_cpu, c) == 0)
244  return clib_error_return (0, "cpu %u is not available to be used"
245  " for the '%s' thread",c, tr->name);
246 
247  avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
248  }));
249 /* *INDENT-ON* */
250 
251  }
252  else
253  {
254  for (j = 0; j < tr->count; j++)
255  {
256  uword c = clib_bitmap_first_set (avail_cpu);
257  if (c == ~0)
258  return clib_error_return (0,
259  "no available cpus to be used for"
260  " the '%s' thread", tr->name);
261 
262  avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
263  tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
264  }
265  }
266  }
267 
268  clib_bitmap_free (avail_cpu);
269 
270  tm->n_vlib_mains = n_vlib_mains;
271 
272  vec_validate_aligned (vlib_worker_threads, first_index - 1,
274 
275  return 0;
276 }
277 
280 {
282 
283  if (vec_len (vlib_worker_threads) >= vec_len (vlib_thread_stacks))
284  {
285  clib_warning ("out of worker threads... Quitting...");
286  exit (1);
287  }
288  vec_add2 (vlib_worker_threads, w, 1);
290  return w;
291 }
292 
295 {
296  vlib_frame_queue_t *fq;
297 
298  fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
299  memset (fq, 0, sizeof (*fq));
300  fq->nelts = nelts;
301  fq->vector_threshold = 128; // packets
303 
304  if (1)
305  {
306  if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
307  fformat (stderr, "WARNING: fq->tail unaligned\n");
308  if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
309  fformat (stderr, "WARNING: fq->head unaligned\n");
310  if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
311  fformat (stderr, "WARNING: fq->elts unaligned\n");
312 
313  if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
314  fformat (stderr, "WARNING: fq->elts[0] size %d\n",
315  sizeof (fq->elts[0]));
316  if (nelts & (nelts - 1))
317  {
318  fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
319  abort ();
320  }
321  }
322 
323  return (fq);
324 }
325 
326 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
327 void
329 {
330 }
331 
332 /* Turned off, save as reference material... */
333 #if 0
334 static inline int
335 vlib_frame_queue_dequeue_internal (int thread_id,
336  vlib_main_t * vm, vlib_node_main_t * nm)
337 {
338  vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
340  vlib_frame_t *f;
343  u32 node_runtime_index;
344  int msg_type;
345  u64 before;
346  int processed = 0;
347 
348  ASSERT (vm == vlib_mains[thread_id]);
349 
350  while (1)
351  {
352  if (fq->head == fq->tail)
353  return processed;
354 
355  elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
356 
357  if (!elt->valid)
358  return processed;
359 
360  before = clib_cpu_time_now ();
361 
362  f = elt->frame;
363  node_runtime_index = elt->node_runtime_index;
364  msg_type = elt->msg_type;
365 
366  switch (msg_type)
367  {
368  case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
370  /* note fallthrough... */
371  case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
373  node_runtime_index);
374  vlib_frame_free (vm, r, f);
375  break;
377  vec_add2 (vm->node_main.pending_frames, p, 1);
379  p->node_runtime_index = elt->node_runtime_index;
380  p->frame_index = vlib_frame_index (vm, f);
382  fq->dequeue_vectors += (u64) f->n_vectors;
383  break;
384  case VLIB_FRAME_QUEUE_ELT_API_MSG:
386  break;
387  default:
388  clib_warning ("bogus frame queue message, type %d", msg_type);
389  break;
390  }
391  elt->valid = 0;
392  fq->dequeues++;
393  fq->dequeue_ticks += clib_cpu_time_now () - before;
395  fq->head++;
396  processed++;
397  }
398  ASSERT (0);
399  return processed;
400 }
401 
402 int
403 vlib_frame_queue_dequeue (int thread_id,
404  vlib_main_t * vm, vlib_node_main_t * nm)
405 {
406  return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
407 }
408 
409 int
410 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
411  u32 frame_queue_index, vlib_frame_t * frame,
413 {
414  vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
416  u32 save_count;
417  u64 new_tail;
418  u64 before = clib_cpu_time_now ();
419 
420  ASSERT (fq);
421 
422  new_tail = __sync_add_and_fetch (&fq->tail, 1);
423 
424  /* Wait until a ring slot is available */
425  while (new_tail >= fq->head + fq->nelts)
426  {
427  f64 b4 = vlib_time_now_ticks (vm, before);
429  /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
430  // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
431  }
432 
433  elt = fq->elts + (new_tail & (fq->nelts - 1));
434 
435  /* this would be very bad... */
436  while (elt->valid)
437  {
438  }
439 
440  /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
441  save_count = frame->n_vectors;
442 
443  elt->frame = frame;
444  elt->node_runtime_index = node_runtime_index;
445  elt->msg_type = type;
447  elt->valid = 1;
448 
449  return save_count;
450 }
451 #endif /* 0 */
452 
453 /* To be called by vlib worker threads upon startup */
454 void
456 {
458 
459  /*
460  * Note: disabling signals in worker threads as follows
461  * prevents the api post-mortem dump scheme from working
462  * {
463  * sigset_t s;
464  * sigfillset (&s);
465  * pthread_sigmask (SIG_SETMASK, &s, 0);
466  * }
467  */
468 
470 
471  if (vec_len (tm->thread_prefix) && w->registration->short_name)
472  {
473  w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
474  w->registration->short_name, w->instance_id, '\0');
475  vlib_set_thread_name ((char *) w->name);
476  }
477 
478  if (!w->registration->use_pthreads)
479  {
480 
481  /* Initial barrier sync, for both worker and i/o threads */
482  clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
483 
484  while (*vlib_worker_threads->wait_at_barrier)
485  ;
486 
487  clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
488  }
489 }
490 
491 void *
493 {
494  void *rv;
495  vlib_worker_thread_t *w = arg;
496 
497  w->lwp = syscall (SYS_gettid);
498  w->thread_id = pthread_self ();
499 
500  rv = (void *) clib_calljmp
501  ((uword (*)(uword)) w->thread_function,
503  /* NOTREACHED, we hope */
504  return rv;
505 }
506 
507 static clib_error_t *
508 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
509 {
511  void *(*fp_arg) (void *) = fp;
512 
513  w->lcore_id = lcore_id;
515  return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
516  else
517  {
518  pthread_t worker;
519  cpu_set_t cpuset;
520  CPU_ZERO (&cpuset);
521  CPU_SET (lcore_id, &cpuset);
522 
523  if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
524  return clib_error_return_unix (0, "pthread_create");
525 
526  if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
527  return clib_error_return_unix (0, "pthread_setaffinity_np");
528 
529  return 0;
530  }
531 }
532 
533 static clib_error_t *
535 {
536  int i, j;
538  vlib_main_t *vm_clone;
539  void *oldheap;
543  u32 n_vlib_mains = tm->n_vlib_mains;
544  u32 worker_thread_index;
545  u8 *main_heap = clib_mem_get_per_cpu_heap ();
546  mheap_t *main_heap_header = mheap_header (main_heap);
547 
548  vec_reset_length (vlib_worker_threads);
549 
550  /* Set up the main thread */
551  vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
552  w->elog_track.name = "main thread";
554 
555  if (vec_len (tm->thread_prefix))
556  {
557  w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
558  vlib_set_thread_name ((char *) w->name);
559  }
560 
561  /*
562  * Truth of the matter: we always use at least two
563  * threads. So, make the main heap thread-safe
564  * and make the event log thread-safe.
565  */
566  main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
567  vm->elog_main.lock =
569  vm->elog_main.lock[0] = 0;
570 
571  if (n_vlib_mains > 1)
572  {
573  /* Replace hand-crafted length-1 vector with a real vector */
574  vlib_mains = 0;
575 
578  _vec_len (vlib_mains) = 0;
580 
581  vlib_worker_threads->wait_at_barrier =
583  vlib_worker_threads->workers_at_barrier =
585 
586  /* Ask for an initial barrier sync */
587  *vlib_worker_threads->workers_at_barrier = 0;
588  *vlib_worker_threads->wait_at_barrier = 1;
589 
590  worker_thread_index = 1;
591 
592  for (i = 0; i < vec_len (tm->registrations); i++)
593  {
594  vlib_node_main_t *nm, *nm_clone;
595  vlib_buffer_main_t *bm_clone;
596  vlib_buffer_free_list_t *fl_clone, *fl_orig;
597  vlib_buffer_free_list_t *orig_freelist_pool;
598  int k;
599 
600  tr = tm->registrations[i];
601 
602  if (tr->count == 0)
603  continue;
604 
605  for (k = 0; k < tr->count; k++)
606  {
607  vec_add2 (vlib_worker_threads, w, 1);
608  if (tr->mheap_size)
609  w->thread_mheap =
610  mheap_alloc (0 /* use VM */ , tr->mheap_size);
611  else
612  w->thread_mheap = main_heap;
614  w->thread_function = tr->function;
615  w->thread_function_arg = w;
616  w->instance_id = k;
617  w->registration = tr;
618 
619  w->elog_track.name =
620  (char *) format (0, "%s %d", tr->name, k + 1);
621  vec_add1 (w->elog_track.name, 0);
623 
624  if (tr->no_data_structure_clone)
625  continue;
626 
627  /* Fork vlib_global_main et al. Look for bugs here */
628  oldheap = clib_mem_set_heap (w->thread_mheap);
629 
630  vm_clone = clib_mem_alloc (sizeof (*vm_clone));
631  clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
632 
633  vm_clone->cpu_index = worker_thread_index;
634  vm_clone->heap_base = w->thread_mheap;
635  vm_clone->mbuf_alloc_list = 0;
636  vm_clone->init_functions_called =
637  hash_create (0, /* value bytes */ 0);
638  memset (&vm_clone->random_buffer, 0,
639  sizeof (vm_clone->random_buffer));
640 
641  nm = &vlib_mains[0]->node_main;
642  nm_clone = &vm_clone->node_main;
643  /* fork next frames array, preserving node runtime indices */
644  nm_clone->next_frames = vec_dup (nm->next_frames);
645  for (j = 0; j < vec_len (nm_clone->next_frames); j++)
646  {
647  vlib_next_frame_t *nf = &nm_clone->next_frames[j];
648  u32 save_node_runtime_index;
649  u32 save_flags;
650 
651  save_node_runtime_index = nf->node_runtime_index;
652  save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
654  nf->node_runtime_index = save_node_runtime_index;
655  nf->flags = save_flags;
656  }
657 
658  /* fork the frame dispatch queue */
659  nm_clone->pending_frames = 0;
660  vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
661  _vec_len (nm_clone->pending_frames) = 0;
662 
663  /* fork nodes */
664  nm_clone->nodes = 0;
665  for (j = 0; j < vec_len (nm->nodes); j++)
666  {
667  vlib_node_t *n;
668  n = clib_mem_alloc_no_fail (sizeof (*n));
669  clib_memcpy (n, nm->nodes[j], sizeof (*n));
670  /* none of the copied nodes have enqueue rights given out */
672  memset (&n->stats_total, 0, sizeof (n->stats_total));
673  memset (&n->stats_last_clear, 0,
674  sizeof (n->stats_last_clear));
675  vec_add1 (nm_clone->nodes, n);
676  }
680  {
681  vlib_node_t *n = vlib_get_node (vm, rt->node_index);
682  rt->cpu_index = vm_clone->cpu_index;
683  /* copy initial runtime_data from node */
684  if (n->runtime_data && n->runtime_data_bytes > 0)
687  n->runtime_data_bytes));
688  }
689 
693  {
694  vlib_node_t *n = vlib_get_node (vm, rt->node_index);
695  rt->cpu_index = vm_clone->cpu_index;
696  /* copy initial runtime_data from node */
697  if (n->runtime_data && n->runtime_data_bytes > 0)
700  n->runtime_data_bytes));
701  }
702 
703  nm_clone->processes = vec_dup (nm->processes);
704 
705  /* zap the (per worker) frame freelists, etc */
706  nm_clone->frame_sizes = 0;
707  nm_clone->frame_size_hash = 0;
708 
709  /* Packet trace buffers are guaranteed to be empty, nothing to do here */
710 
711  clib_mem_set_heap (oldheap);
713 
714  vm_clone->error_main.counters =
715  vec_dup (vlib_mains[0]->error_main.counters);
716  vm_clone->error_main.counters_last_clear =
717  vec_dup (vlib_mains[0]->error_main.counters_last_clear);
718 
719  /* Fork the vlib_buffer_main_t free lists, etc. */
720  bm_clone = vec_dup (vm_clone->buffer_main);
721  vm_clone->buffer_main = bm_clone;
722 
723  orig_freelist_pool = bm_clone->buffer_free_list_pool;
724  bm_clone->buffer_free_list_pool = 0;
725 
726  /* *INDENT-OFF* */
727  pool_foreach (fl_orig, orig_freelist_pool,
728  ({
730  fl_clone, CLIB_CACHE_LINE_BYTES);
731  ASSERT (fl_orig - orig_freelist_pool
732  == fl_clone - bm_clone->buffer_free_list_pool);
733 
734  fl_clone[0] = fl_orig[0];
735  fl_clone->buffers = 0;
736  fl_clone->n_alloc = 0;
737  }));
738 /* *INDENT-ON* */
739 
740  worker_thread_index++;
741  }
742  }
743  }
744  else
745  {
746  /* only have non-data-structure copy threads to create... */
747  for (i = 0; i < vec_len (tm->registrations); i++)
748  {
749  tr = tm->registrations[i];
750 
751  for (j = 0; j < tr->count; j++)
752  {
753  vec_add2 (vlib_worker_threads, w, 1);
754  if (tr->mheap_size)
755  w->thread_mheap =
756  mheap_alloc (0 /* use VM */ , tr->mheap_size);
757  else
758  w->thread_mheap = main_heap;
760  w->thread_function = tr->function;
761  w->thread_function_arg = w;
762  w->instance_id = j;
763  w->elog_track.name =
764  (char *) format (0, "%s %d", tr->name, j + 1);
765  w->registration = tr;
766  vec_add1 (w->elog_track.name, 0);
768  }
769  }
770  }
771 
772  worker_thread_index = 1;
773 
774  for (i = 0; i < vec_len (tm->registrations); i++)
775  {
776  clib_error_t *err;
777  int j;
778 
779  tr = tm->registrations[i];
780 
781  if (tr->use_pthreads || tm->use_pthreads)
782  {
783  for (j = 0; j < tr->count; j++)
784  {
785  w = vlib_worker_threads + worker_thread_index++;
787  w, 0);
788  if (err)
789  clib_error_report (err);
790  }
791  }
792  else
793  {
794  uword c;
795  /* *INDENT-OFF* */
796  clib_bitmap_foreach (c, tr->coremask, ({
797  w = vlib_worker_threads + worker_thread_index++;
798  err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
799  w, c);
800  if (err)
801  clib_error_report (err);
802  }));
803  /* *INDENT-ON* */
804  }
805  }
808  return 0;
809 }
810 
812 
813 void
815 {
816  int i, j;
818  vlib_main_t *vm;
819  vlib_node_main_t *nm, *nm_clone;
820  vlib_node_t **old_nodes_clone;
821  vlib_main_t *vm_clone;
822  vlib_node_runtime_t *rt, *old_rt;
823  void *oldheap;
824  never_inline void
827  uword n_calls,
828  uword n_vectors, uword n_clocks);
829 
830  ASSERT (os_get_cpu_number () == 0);
831 
832  if (vec_len (vlib_mains) == 1)
833  return;
834 
835  vm = vlib_mains[0];
836  nm = &vm->node_main;
837 
838  ASSERT (os_get_cpu_number () == 0);
839  ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
840 
841  /*
842  * Scrape all runtime stats, so we don't lose node runtime(s) with
843  * pending counts, or throw away worker / io thread counts.
844  */
845  for (j = 0; j < vec_len (nm->nodes); j++)
846  {
847  vlib_node_t *n;
848  n = nm->nodes[j];
849  vlib_node_sync_stats (vm, n);
850  }
851 
852  for (i = 1; i < vec_len (vlib_mains); i++)
853  {
854  vlib_node_t *n;
855 
856  vm_clone = vlib_mains[i];
857  nm_clone = &vm_clone->node_main;
858 
859  for (j = 0; j < vec_len (nm_clone->nodes); j++)
860  {
861  n = nm_clone->nodes[j];
862 
863  rt = vlib_node_get_runtime (vm_clone, n->index);
864  vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
865  }
866  }
867 
868  for (i = 1; i < vec_len (vlib_mains); i++)
869  {
871  w = vlib_worker_threads + i;
872  oldheap = clib_mem_set_heap (w->thread_mheap);
873 
874  vm_clone = vlib_mains[i];
875 
876  /* Re-clone error heap */
877  u64 *old_counters = vm_clone->error_main.counters;
878  u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
879  clib_memcpy (&vm_clone->error_main, &vm->error_main,
880  sizeof (vm->error_main));
881  j = vec_len (vm->error_main.counters) - 1;
882  vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
883  vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
884  vm_clone->error_main.counters = old_counters;
885  vm_clone->error_main.counters_last_clear = old_counters_all_clear;
886 
887  nm_clone = &vm_clone->node_main;
888  vec_free (nm_clone->next_frames);
889  nm_clone->next_frames = vec_dup (nm->next_frames);
890 
891  for (j = 0; j < vec_len (nm_clone->next_frames); j++)
892  {
893  vlib_next_frame_t *nf = &nm_clone->next_frames[j];
894  u32 save_node_runtime_index;
895  u32 save_flags;
896 
897  save_node_runtime_index = nf->node_runtime_index;
898  save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
900  nf->node_runtime_index = save_node_runtime_index;
901  nf->flags = save_flags;
902  }
903 
904  old_nodes_clone = nm_clone->nodes;
905  nm_clone->nodes = 0;
906 
907  /* re-fork nodes */
908  for (j = 0; j < vec_len (nm->nodes); j++)
909  {
910  vlib_node_t *old_n_clone;
911  vlib_node_t *new_n, *new_n_clone;
912 
913  new_n = nm->nodes[j];
914  old_n_clone = old_nodes_clone[j];
915 
916  new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
917  clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
918  /* none of the copied nodes have enqueue rights given out */
920 
921  if (j >= vec_len (old_nodes_clone))
922  {
923  /* new node, set to zero */
924  memset (&new_n_clone->stats_total, 0,
925  sizeof (new_n_clone->stats_total));
926  memset (&new_n_clone->stats_last_clear, 0,
927  sizeof (new_n_clone->stats_last_clear));
928  }
929  else
930  {
931  /* Copy stats if the old data is valid */
932  clib_memcpy (&new_n_clone->stats_total,
933  &old_n_clone->stats_total,
934  sizeof (new_n_clone->stats_total));
935  clib_memcpy (&new_n_clone->stats_last_clear,
936  &old_n_clone->stats_last_clear,
937  sizeof (new_n_clone->stats_last_clear));
938 
939  /* keep previous node state */
940  new_n_clone->state = old_n_clone->state;
941  }
942  vec_add1 (nm_clone->nodes, new_n_clone);
943  }
944  /* Free the old node clone */
945  for (j = 0; j < vec_len (old_nodes_clone); j++)
946  clib_mem_free (old_nodes_clone[j]);
947  vec_free (old_nodes_clone);
948 
949 
950  /* re-clone internal nodes */
951  old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
954 
956  {
957  vlib_node_t *n = vlib_get_node (vm, rt->node_index);
958  rt->cpu_index = vm_clone->cpu_index;
959  /* copy runtime_data, will be overwritten later for existing rt */
960  if (n->runtime_data && n->runtime_data_bytes > 0)
963  n->runtime_data_bytes));
964  }
965 
966  for (j = 0; j < vec_len (old_rt); j++)
967  {
968  rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
969  rt->state = old_rt[j].state;
970  clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
972  }
973 
974  vec_free (old_rt);
975 
976  /* re-clone input nodes */
977  old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
980 
982  {
983  vlib_node_t *n = vlib_get_node (vm, rt->node_index);
984  rt->cpu_index = vm_clone->cpu_index;
985  /* copy runtime_data, will be overwritten later for existing rt */
986  if (n->runtime_data && n->runtime_data_bytes > 0)
989  n->runtime_data_bytes));
990  }
991 
992  for (j = 0; j < vec_len (old_rt); j++)
993  {
994  rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
995  rt->state = old_rt[j].state;
996  clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
998  }
999 
1000  vec_free (old_rt);
1001 
1002  nm_clone->processes = vec_dup (nm->processes);
1003 
1004  clib_mem_set_heap (oldheap);
1005 
1006  // vnet_main_fork_fixup (i);
1007  }
1008 }
1009 
1010 u32
1011 unformat_sched_policy (unformat_input_t * input, va_list * args)
1012 {
1013  u32 *r = va_arg (*args, u32 *);
1014 
1015  if (0);
1016 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1018 #undef _
1019  else
1020  return 0;
1021  return 1;
1022 }
1023 
1024 static clib_error_t *
1026 {
1028  uword *p;
1030  u8 *name;
1031  u64 coremask;
1032  uword *bitmap;
1033  u32 count;
1034 
1036 
1037  tm->n_thread_stacks = 1; /* account for main thread */
1038  tm->sched_policy = ~0;
1039  tm->sched_priority = ~0;
1040 
1041  tr = tm->next;
1042 
1043  while (tr)
1044  {
1046  tr = tr->next;
1047  }
1048 
1049  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1050  {
1051  if (unformat (input, "use-pthreads"))
1052  tm->use_pthreads = 1;
1053  else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1054  ;
1055  else if (unformat (input, "main-core %u", &tm->main_lcore))
1056  ;
1057  else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1058  ;
1059  else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1060  {
1062  if (p == 0)
1063  return clib_error_return (0, "no such thread type '%s'", name);
1064 
1065  tr = (vlib_thread_registration_t *) p[0];
1066 
1067  if (tr->use_pthreads)
1068  return clib_error_return (0,
1069  "coremask cannot be set for '%s' threads",
1070  name);
1071 
1073  (tr->coremask, 0, coremask, BITS (coremask));
1075  }
1076  else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1077  &bitmap))
1078  {
1080  if (p == 0)
1081  return clib_error_return (0, "no such thread type '%s'", name);
1082 
1083  tr = (vlib_thread_registration_t *) p[0];
1084 
1085  if (tr->use_pthreads)
1086  return clib_error_return (0,
1087  "corelist cannot be set for '%s' threads",
1088  name);
1089 
1090  tr->coremask = bitmap;
1092  }
1093  else
1094  if (unformat
1095  (input, "scheduler-policy %U", unformat_sched_policy,
1096  &tm->sched_policy))
1097  ;
1098  else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1099  ;
1100  else if (unformat (input, "%s %u", &name, &count))
1101  {
1103  if (p == 0)
1104  return clib_error_return (0, "no such thread type 3 '%s'", name);
1105 
1106  tr = (vlib_thread_registration_t *) p[0];
1107  if (tr->fixed_count)
1108  return clib_error_return
1109  (0, "number of %s threads not configurable", tr->name);
1110  tr->count = count;
1111  }
1112  else
1113  break;
1114  }
1115 
1116  if (tm->sched_priority != ~0)
1117  {
1118  if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1119  {
1120  u32 prio_max = sched_get_priority_max (tm->sched_policy);
1121  u32 prio_min = sched_get_priority_min (tm->sched_policy);
1122  if (tm->sched_priority > prio_max)
1123  tm->sched_priority = prio_max;
1124  if (tm->sched_priority < prio_min)
1125  tm->sched_priority = prio_min;
1126  }
1127  else
1128  {
1129  return clib_error_return
1130  (0,
1131  "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1132  tm->sched_priority);
1133  }
1134  }
1135  tr = tm->next;
1136 
1137  if (!tm->thread_prefix)
1138  tm->thread_prefix = format (0, "vpp");
1139 
1140  while (tr)
1141  {
1142  tm->n_thread_stacks += tr->count;
1143  tm->n_pthreads += tr->count * tr->use_pthreads;
1144  tm->n_threads += tr->count * (tr->use_pthreads == 0);
1145  tr = tr->next;
1146  }
1147 
1148  return 0;
1149 }
1150 
1152 
1153 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1154 void
1155 __sync_fetch_and_add_8 (void)
1156 {
1157  fformat (stderr, "%s called\n", __FUNCTION__);
1158  abort ();
1159 }
1160 
1161 void
1162 __sync_add_and_fetch_8 (void)
1163 {
1164  fformat (stderr, "%s called\n", __FUNCTION__);
1165  abort ();
1166 }
1167 #endif
1168 
1169 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1170 void
1172 {
1173 }
1174 
1175 void
1177 {
1178  vlib_main_t *vm = vlib_get_main ();
1179 
1180  if (vlib_mains == 0)
1181  return;
1182 
1183  ASSERT (os_get_cpu_number () == 0);
1185 
1186  switch (which)
1187  {
1190  break;
1191 
1192  default:
1193  ASSERT (0);
1194  }
1196 }
1197 
1198 void
1200 {
1201  f64 deadline;
1202  u32 count;
1203 
1204  if (vec_len (vlib_mains) < 2)
1205  return;
1206 
1207  count = vec_len (vlib_mains) - 1;
1208 
1209  /* Tolerate recursive calls */
1210  if (++vlib_worker_threads[0].recursion_level > 1)
1211  return;
1212 
1213  vlib_worker_threads[0].barrier_sync_count++;
1214 
1215  ASSERT (os_get_cpu_number () == 0);
1216 
1217  deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1218 
1219  *vlib_worker_threads->wait_at_barrier = 1;
1220  while (*vlib_worker_threads->workers_at_barrier != count)
1221  {
1222  if (vlib_time_now (vm) > deadline)
1223  {
1224  fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1225  os_panic ();
1226  }
1227  }
1228 }
1229 
1230 void
1232 {
1233  f64 deadline;
1234 
1235  if (vec_len (vlib_mains) < 2)
1236  return;
1237 
1238  if (--vlib_worker_threads[0].recursion_level > 0)
1239  return;
1240 
1241  deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1242 
1243  *vlib_worker_threads->wait_at_barrier = 0;
1244 
1245  while (*vlib_worker_threads->workers_at_barrier > 0)
1246  {
1247  if (vlib_time_now (vm) > deadline)
1248  {
1249  fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1250  os_panic ();
1251  }
1252  }
1253 }
1254 
1255 /*
1256  * Check the frame queue to see if any frames are available.
1257  * If so, pull the packets off the frames and put them to
1258  * the handoff node.
1259  */
1260 int
1262 {
1263  u32 thread_id = vm->cpu_index;
1264  vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1266  u32 *from, *to;
1267  vlib_frame_t *f;
1268  int msg_type;
1269  int processed = 0;
1270  u32 n_left_to_node;
1271  u32 vectors = 0;
1272 
1273  ASSERT (fq);
1274  ASSERT (vm == vlib_mains[thread_id]);
1275 
1276  if (PREDICT_FALSE (fqm->node_index == ~0))
1277  return 0;
1278  /*
1279  * Gather trace data for frame queues
1280  */
1281  if (PREDICT_FALSE (fq->trace))
1282  {
1283  frame_queue_trace_t *fqt;
1285  u32 elix;
1286 
1287  fqt = &fqm->frame_queue_traces[thread_id];
1288 
1289  fqt->nelts = fq->nelts;
1290  fqt->head = fq->head;
1291  fqt->head_hint = fq->head_hint;
1292  fqt->tail = fq->tail;
1293  fqt->threshold = fq->vector_threshold;
1294  fqt->n_in_use = fqt->tail - fqt->head;
1295  if (fqt->n_in_use >= fqt->nelts)
1296  {
1297  // if beyond max then use max
1298  fqt->n_in_use = fqt->nelts - 1;
1299  }
1300 
1301  /* Record the number of elements in use in the histogram */
1302  fqh = &fqm->frame_queue_histogram[thread_id];
1303  fqh->count[fqt->n_in_use]++;
1304 
1305  /* Record a snapshot of the elements in use */
1306  for (elix = 0; elix < fqt->nelts; elix++)
1307  {
1308  elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1309  if (1 || elt->valid)
1310  {
1311  fqt->n_vectors[elix] = elt->n_vectors;
1312  }
1313  }
1314  fqt->written = 1;
1315  }
1316 
1317  while (1)
1318  {
1319  if (fq->head == fq->tail)
1320  {
1321  fq->head_hint = fq->head;
1322  return processed;
1323  }
1324 
1325  elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1326 
1327  if (!elt->valid)
1328  {
1329  fq->head_hint = fq->head;
1330  return processed;
1331  }
1332 
1333  from = elt->buffer_index;
1334  msg_type = elt->msg_type;
1335 
1337  ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1338 
1339  f = vlib_get_frame_to_node (vm, fqm->node_index);
1340 
1341  to = vlib_frame_vector_args (f);
1342 
1343  n_left_to_node = elt->n_vectors;
1344 
1345  while (n_left_to_node >= 4)
1346  {
1347  to[0] = from[0];
1348  to[1] = from[1];
1349  to[2] = from[2];
1350  to[3] = from[3];
1351  to += 4;
1352  from += 4;
1353  n_left_to_node -= 4;
1354  }
1355 
1356  while (n_left_to_node > 0)
1357  {
1358  to[0] = from[0];
1359  to++;
1360  from++;
1361  n_left_to_node--;
1362  }
1363 
1364  vectors += elt->n_vectors;
1365  f->n_vectors = elt->n_vectors;
1366  vlib_put_frame_to_node (vm, fqm->node_index, f);
1367 
1368  elt->valid = 0;
1369  elt->n_vectors = 0;
1370  elt->msg_type = 0xfefefefe;
1372  fq->head++;
1373  processed++;
1374 
1375  /*
1376  * Limit the number of packets pushed into the graph
1377  */
1378  if (vectors >= fq->vector_threshold)
1379  {
1380  fq->head_hint = fq->head;
1381  return processed;
1382  }
1383  }
1384  ASSERT (0);
1385  return processed;
1386 }
1387 
1388 void
1390 {
1393  vlib_main_t *vm = vlib_get_main ();
1394  clib_error_t *e;
1395 
1396  ASSERT (vm->cpu_index == os_get_cpu_number ());
1397 
1399  clib_time_init (&vm->clib_time);
1401 
1402  /* Wait until the dpdk init sequence is complete */
1403  while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1405 
1407  (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1408  if (e)
1409  clib_error_report (e);
1410 
1411  vlib_worker_loop (vm);
1412 }
1413 
1414 /* *INDENT-OFF* */
1415 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1416  .name = "workers",
1417  .short_name = "wk",
1418  .function = vlib_worker_thread_fn,
1419 };
1420 /* *INDENT-ON* */
1421 
1422 u32
1423 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1424 {
1427  vlib_frame_queue_t *fq;
1428  int i;
1429 
1430  if (frame_queue_nelts == 0)
1431  frame_queue_nelts = FRAME_QUEUE_NELTS;
1432 
1433  vec_add2 (tm->frame_queue_mains, fqm, 1);
1434 
1435  fqm->node_index = node_index;
1436 
1438  _vec_len (fqm->vlib_frame_queues) = 0;
1439  for (i = 0; i < tm->n_vlib_mains; i++)
1440  {
1441  fq = vlib_frame_queue_alloc (frame_queue_nelts);
1442  vec_add1 (fqm->vlib_frame_queues, fq);
1443  }
1444 
1445  return (fqm - tm->frame_queue_mains);
1446 }
1447 
1448 
1449 int
1451 {
1453 
1454  if (tm->extern_thread_mgmt)
1455  return -1;
1456 
1458  tm->extern_thread_mgmt = 1;
1459  return 0;
1460 }
1461 
1462 clib_error_t *
1464 {
1465  return 0;
1466 }
1467 
1469 
1470 /*
1471  * fd.io coding-style-patch-verification: ON
1472  *
1473  * Local Variables:
1474  * eval: (c-set-style "gnu")
1475  * End:
1476  */
_vlib_init_function_list_elt_t * worker_init_function_registrations
Definition: main.h:165
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:187
u32 vl(void *p)
Definition: threads.c:30
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
clib_error_t *(* vlib_thread_set_lcore_cb)(u32 thread, u16 lcore)
Definition: threads.h:265
#define clib_min(x, y)
Definition: clib.h:332
vlib_process_t ** processes
Definition: node.h:673
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:290
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:463
int vlib_frame_queue_enqueue(vlib_main_t *vm, u32 node_runtime_index, u32 frame_queue_index, vlib_frame_t *frame, vlib_frame_queue_msg_type_t type)
#define VLIB_MAIN_LOOP_ENTER_FUNCTION(x)
Definition: init.h:114
word elog_track_register(elog_main_t *em, elog_track_t *t)
Definition: elog.c:190
void * mheap_alloc(void *memory, uword size)
Definition: mheap.c:953
#define NULL
Definition: clib.h:55
u32 index
Definition: node.h:237
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:185
#define vec_add2_aligned(V, P, N, A)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:573
clib_error_t * threads_init(vlib_main_t *vm)
Definition: threads.c:1463
void os_panic(void)
Definition: unix-misc.c:172
u32 vlib_frame_queue_main_init(u32 node_index, u32 frame_queue_nelts)
Definition: threads.c:1423
void * thread_function_arg
Definition: threads.h:98
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
static int sort_registrations_by_no_clone(void *a0, void *a1)
Definition: threads.c:89
static u64 clib_cpu_time_now(void)
Definition: time.h:73
frame_queue_trace_t * frame_queue_traces
Definition: threads.h:149
elog_track_t elog_track
Definition: threads.h:100
#define VLIB_FRAME_NO_FREE_AFTER_DISPATCH
Definition: node.h:365
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:561
static mheap_t * mheap_header(u8 *v)
vlib_buffer_main_t * buffer_main
Definition: main.h:104
void vnet_main_fixup(vlib_fork_fixup_t which)
Definition: threads.c:1171
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
#define hash_set_mem(h, key, value)
Definition: hash.h:274
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
clib_time_t clib_time
Definition: main.h:62
void vlib_worker_thread_fn(void *arg)
Definition: threads.c:1389
u32 unformat_sched_policy(unformat_input_t *input, va_list *args)
Definition: threads.c:1011
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:447
struct vlib_thread_registration_ * next
Definition: threads.h:31
#define MHEAP_FLAG_THREAD_SAFE
u32 buffer_index[VLIB_FRAME_SIZE]
Definition: threads.h:82
void * runtime_data
Definition: node.h:243
volatile u32 valid
Definition: threads.h:76
vlib_main_t ** vlib_mains
Definition: buffer.c:285
#define clib_bitmap_dup(v)
Duplicate a bitmap.
Definition: bitmap.h:87
void vlib_frame_free(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_frame_t *f)
Definition: main.c:216
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
u8 state
Definition: node.h:265
u64 * counters_last_clear
Definition: error.h:81
static void vlib_worker_thread_barrier_check(void)
Definition: threads.h:214
#define VLIB_LOG2_THREAD_STACK_SIZE
Definition: threads.h:65
vlib_thread_registration_t * next
Definition: threads.h:271
#define vec_add1_aligned(V, E, A)
Add 1 element to end of vector (alignment specified).
Definition: vec.h:532
vlib_node_stats_t stats_last_clear
Definition: node.h:231
#define clib_smp_atomic_add(addr, increment)
Definition: smp.h:46
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:376
void vlib_worker_thread_node_runtime_update(void)
Definition: threads.c:814
u64 count[FRAME_QUEUE_MAX_NELTS]
Definition: node.h:717
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
never_inline void vlib_node_runtime_sync_stats(vlib_main_t *vm, vlib_node_runtime_t *r, uword n_calls, uword n_vectors, uword n_clocks)
Definition: main.c:545
vlib_worker_thread_t * vlib_alloc_thread(vlib_main_t *vm)
Definition: threads.c:279
vlib_frame_queue_msg_type_t
Definition: threads.h:68
vlib_node_t ** nodes
Definition: node.h:633
int i32
Definition: types.h:81
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:111
uword * lock
Definition: elog.h:162
uword * cpu_core_bitmap
Definition: threads.h:308
clib_error_t * vlib_call_init_exit_functions(vlib_main_t *vm, _vlib_init_function_list_elt_t *head, int call_once)
Definition: init.c:43
u32 cpu_index
Definition: main.h:159
unsigned long u64
Definition: types.h:89
vlib_frame_queue_elt_t * elts
Definition: threads.h:138
vlib_node_runtime_t * nodes_by_type[VLIB_N_NODE_TYPE]
Definition: node.h:643
void vlib_set_thread_name(char *name)
Definition: threads.c:74
void vl_msg_api_handler_no_free(void *)
Definition: threads.c:328
#define hash_create_string(elts, value_bytes)
Definition: hash.h:652
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1022
vlib_fork_fixup_t
Definition: threads.h:205
#define BARRIER_SYNC_TIMEOUT
Definition: threads.h:187
#define VLIB_NODE_RUNTIME_DATA_SIZE
Definition: node.h:474
VLIB_REGISTER_THREAD(worker_thread_reg, static)
int extern_thread_mgmt
Definition: threads.h:327
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:35
#define clib_bitmap_foreach(i, ai, body)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
void * thread_mheap
Definition: threads.h:95
u32 next_frame_index
Definition: node.h:406
vlib_node_stats_t stats_total
Definition: node.h:227
volatile u64 head
Definition: threads.h:125
u16 state
Input node state.
Definition: node.h:451
void vlib_node_sync_stats(vlib_main_t *vm, vlib_node_t *n)
Definition: main.c:575
static uword clib_bitmap_first_set(uword *ai)
Return the lowest numbered set bit in a bitmap.
Definition: bitmap.h:385
vlib_error_main_t error_main
Definition: main.h:124
static u32 vlib_frame_index(vlib_main_t *vm, vlib_frame_t *f)
Definition: node_funcs.h:231
vlib_thread_callbacks_t cb
Definition: threads.h:326
int vlib_thread_cb_register(struct vlib_main_t *vm, vlib_thread_callbacks_t *cb)
Definition: threads.c:1450
#define v
Definition: acl.c:246
struct _unformat_input_t unformat_input_t
char * name
Definition: elog.h:106
#define clib_error_return_unix(e, args...)
Definition: error.h:114
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:374
static void * clib_mem_get_per_cpu_heap(void)
Definition: mem.h:55
static clib_error_t * vlib_launch_thread_int(void *fp, vlib_worker_thread_t *w, unsigned lcore_id)
Definition: threads.c:508
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define VLIB_FRAME_PENDING
Definition: node.h:376
#define VLIB_FRAME_SIZE
Definition: node.h:328
u32 node_index
Node index.
Definition: node.h:436
uword * init_functions_called
Definition: main.h:156
#define VLIB_FRAME_FREE_AFTER_DISPATCH
Definition: node.h:379
void clib_time_init(clib_time_t *c)
Definition: time.c:175
uword * frame_size_hash
Definition: node.h:688
vlib_thread_main_t vlib_thread_main
Definition: threads.c:36
word fformat(FILE *f, char *fmt,...)
Definition: format.c:452
void(* thread_function)(void *)
Definition: threads.h:97
static clib_error_t * cpu_config(vlib_main_t *vm, unformat_input_t *input)
Definition: threads.c:1025
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:169
i32 n_vectors[FRAME_QUEUE_MAX_NELTS]
Definition: node.h:712
u64 * counters
Definition: error.h:78
u32 owner_node_index
Definition: node.h:308
vlib_frame_queue_t * vlib_frame_queue_alloc(int nelts)
Definition: threads.c:294
uword os_get_ncpus(void)
Definition: threads.c:62
volatile u64 tail
Definition: threads.h:117
#define clib_mem_alloc_no_fail(size)
Definition: mem.h:153
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:140
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
svmdb_client_t * c
u16 n_vectors
Definition: node.h:344
vlib_frame_queue_t ** vlib_frame_queues
Definition: threads.h:146
vlib_main_t * vm
Definition: buffer.c:276
u32 node_runtime_index
Definition: node.h:400
vlib_pending_frame_t * pending_frames
Definition: node.h:658
vlib_thread_function_t * function
Definition: threads.h:36
int vlib_frame_queue_dequeue(vlib_main_t *vm, vlib_frame_queue_main_t *fqm)
Definition: threads.c:1261
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
void * heap_base
Definition: main.h:101
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:223
#define clib_warning(format, args...)
Definition: error.h:59
static vlib_node_runtime_t * vlib_node_get_runtime(vlib_main_t *vm, u32 node_index)
Get node runtime by node index.
Definition: node_funcs.h:88
#define clib_memcpy(a, b, c)
Definition: string.h:69
void vlib_worker_loop(vlib_main_t *vm)
Definition: main.c:1612
elog_main_t elog_main
Definition: main.h:141
frame_queue_nelt_counter_t * frame_queue_histogram
Definition: threads.h:150
static uword * clib_bitmap_set_multiple(uword *bitmap, uword i, uword value, uword n_bits)
sets the ith through ith + n_bits bits in a bitmap
Definition: bitmap.h:275
void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
Definition: threads.c:1199
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Definition: bitmap.h:197
u16 cpu_index
CPU this node runs on.
Definition: node.h:461
static void vlib_next_frame_init(vlib_next_frame_t *nf)
Definition: node.h:389
void vlib_worker_thread_init(vlib_worker_thread_t *w)
Definition: threads.c:455
static void * clib_mem_get_heap(void)
Definition: mem.h:217
volatile u32 * wait_at_barrier
Definition: threads.h:90
#define FRAME_QUEUE_NELTS
Definition: threads.c:27
#define never_inline
Definition: clib.h:81
#define hash_create(elts, value_bytes)
Definition: hash.h:658
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
vlib_frame_queue_main_t * frame_queue_mains
Definition: threads.h:314
u16 flags
Definition: node.h:335
static void clib_mem_free(void *p)
Definition: mem.h:176
#define clib_error_report(e)
Definition: error.h:125
#define clib_bitmap_free(v)
Free a bitmap.
Definition: bitmap.h:92
uword * thread_registrations_by_name
Definition: threads.h:276
static void * clib_mem_alloc(uword size)
Definition: mem.h:109
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 uword
Definition: types.h:112
static uword clib_bitmap_count_set_bits(uword *ai)
Return the number of set bits in a bitmap.
Definition: bitmap.h:441
void ** mbuf_alloc_list
Definition: main.h:161
static clib_error_t * start_workers(vlib_main_t *vm)
Definition: threads.c:534
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
#define VLIB_PENDING_FRAME_NO_NEXT_FRAME
Definition: node.h:409
DECLARE_CJ_GLOBAL_LOG
Definition: threads.c:25
vlib_node_main_t node_main
Definition: main.h:115
static uword unformat_bitmap_list(unformat_input_t *input, va_list *va)
unformat a list of bit ranges into a bitmap (eg "0-3,5-7,11" )
Definition: bitmap.h:693
vlib_buffer_free_list_t * buffer_free_list_pool
Definition: buffer.h:389
vlib_next_frame_t * next_frames
Definition: node.h:655
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:960
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:253
uword os_get_cpu_number(void)
Definition: threads.c:39
volatile u64 head_hint
Definition: threads.h:134
#define VLIB_THREAD_STACK_SIZE
Definition: threads.h:66
vlib_frame_size_t * frame_sizes
Definition: node.h:691
clib_error_t *(* vlib_launch_thread_cb)(void *fp, vlib_worker_thread_t *w, unsigned lcore_id)
Definition: threads.h:263
#define hash_get_mem(h, key)
Definition: hash.h:268
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:117
volatile u32 * workers_at_barrier
Definition: threads.h:91
uword clib_calljmp(uword(*func)(uword func_arg), uword func_arg, void *stack)
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1231
#define VLIB_INVALID_NODE_INDEX
Definition: node.h:325
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
static f64 vlib_time_now_ticks(vlib_main_t *vm, u64 n)
Definition: main.h:191
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
#define vec_foreach(var, vec)
Vector iterator.
void * vlib_worker_thread_bootstrap_fn(void *arg)
Definition: threads.c:492
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:101
u32 node_runtime_index
Definition: node.h:359
uword * cpu_socket_bitmap
Definition: threads.h:311
static uword * vlib_sysfs_list_to_bitmap(char *filename)
Definition: threads.c:99
#define foreach_sched_policy
Definition: threads.h:246
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:196
vlib_thread_registration_t ** registrations
Definition: threads.h:274
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u8 ** vlib_thread_stacks
Definition: main.c:497
pthread_t thread_id
Definition: threads.h:108
vlib_thread_registration_t * registration
Definition: threads.h:102
#define BITS(x)
Definition: clib.h:58
volatile u32 worker_thread_release
Definition: threads.h:317
void vlib_worker_thread_fork_fixup(vlib_fork_fixup_t which)
Definition: threads.c:1176
clib_random_buffer_t random_buffer
Definition: main.h:153
u8 runtime_data_bytes
Definition: node.h:268
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
clib_error_t * vlib_thread_init(vlib_main_t *vm)
Definition: threads.c:129