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.
19 #include <vppinfra/format.h>
20 #include <vlib/vlib.h>
22 #include <vlib/threads.h>
23 #include <vlib/unix/cj.h>
27 #include <rte_config.h>
28 #include <rte_common.h>
30 #include <rte_launch.h>
31 #include <rte_lcore.h>
33 DECLARE_CJ_GLOBAL_LOG;
35 #define FRAME_QUEUE_NELTS 32
40 * Weak definitions of DPDK symbols used in this file.
41 * Needed for linking test programs without DPDK libs.
43 unsigned __thread __attribute__ ((weak)) RTE_PER_LCORE (_lcore_id);
44 struct lcore_config __attribute__ ((weak)) lcore_config[];
45 unsigned __attribute__ ((weak)) rte_socket_id ();
46 int __attribute__ ((weak)) rte_eal_remote_launch ();
54 vlib_thread_main_t vlib_thread_main;
57 os_get_cpu_number (void)
63 len = vec_len (vlib_thread_stacks);
67 /* Get any old stack address. */
70 n = ((uword) sp - (uword) vlib_thread_stacks[0])
71 >> VLIB_LOG2_THREAD_STACK_SIZE;
73 /* "processes" have their own stacks, and they always run in thread 0 */
84 len = vec_len (vlib_thread_stacks);
92 vlib_set_thread_name (char *name)
94 int pthread_setname_np (pthread_t __target_thread, const char *__name);
96 pthread_t thread = pthread_self ();
100 rv = pthread_setname_np (thread, name);
102 clib_warning ("pthread_setname_np returned %d", rv);
107 sort_registrations_by_no_clone (void *a0, void *a1)
109 vlib_thread_registration_t **tr0 = a0;
110 vlib_thread_registration_t **tr1 = a1;
112 return ((i32) ((*tr0)->no_data_structure_clone)
113 - ((i32) ((*tr1)->no_data_structure_clone)));
117 vlib_sysfs_list_to_bitmap (char *filename)
122 fp = fopen (filename, "r");
127 vec_validate (buffer, 256 - 1);
128 if (fgets ((char *) buffer, 256, fp))
131 unformat_init_string (&in, (char *) buffer,
132 strlen ((char *) buffer));
133 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
134 clib_warning ("unformat_bitmap_list failed");
144 /* Called early in the init sequence */
147 vlib_thread_init (vlib_main_t * vm)
149 vlib_thread_main_t *tm = &vlib_thread_main;
150 vlib_worker_thread_t *w;
151 vlib_thread_registration_t *tr;
152 u32 n_vlib_mains = 1;
157 /* get bitmaps of active cpu cores and sockets */
158 tm->cpu_core_bitmap =
159 vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
160 tm->cpu_socket_bitmap =
161 vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
163 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
166 for (i = 0; i < tm->skip_cores; i++)
168 uword c = clib_bitmap_first_set (avail_cpu);
170 return clib_error_return (0, "no available cpus to skip");
172 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
175 /* grab cpu for main thread */
178 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
179 if (tm->main_lcore == (u8) ~ 0)
180 return clib_error_return (0, "no available cpus to be used for the"
185 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
186 return clib_error_return (0, "cpu %u is not available to be used"
187 " for the main thread", tm->main_lcore);
189 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
191 /* assume that there is socket 0 only if there is no data from sysfs */
192 if (!tm->cpu_socket_bitmap)
193 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
195 /* pin main thread to main_lcore */
200 CPU_SET (tm->main_lcore, &cpuset);
201 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
205 /* as many threads as stacks... */
206 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
207 CLIB_CACHE_LINE_BYTES);
209 /* Preallocate thread 0 */
210 _vec_len (vlib_worker_threads) = 1;
211 w = vlib_worker_threads;
212 w->thread_mheap = clib_mem_get_heap ();
213 w->thread_stack = vlib_thread_stacks[0];
214 w->lcore_id = tm->main_lcore;
215 w->lwp = syscall (SYS_gettid);
216 w->thread_id = pthread_self ();
217 tm->n_vlib_mains = 1;
219 if (tm->sched_policy != ~0)
221 struct sched_param sched_param;
222 if (!sched_getparam (w->lwp, &sched_param))
224 if (tm->sched_priority != ~0)
225 sched_param.sched_priority = tm->sched_priority;
226 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
230 /* assign threads to cores and set n_vlib_mains */
235 vec_add1 (tm->registrations, tr);
239 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
241 for (i = 0; i < vec_len (tm->registrations); i++)
244 tr = tm->registrations[i];
245 tr->first_index = first_index;
246 first_index += tr->count;
247 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
249 /* construct coremask */
250 if (tr->use_pthreads || !tr->count)
257 clib_bitmap_foreach (c, tr->coremask, ({
258 if (clib_bitmap_get(avail_cpu, c) == 0)
259 return clib_error_return (0, "cpu %u is not available to be used"
260 " for the '%s' thread",c, tr->name);
262 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
269 for (j = 0; j < tr->count; j++)
271 uword c = clib_bitmap_first_set (avail_cpu);
273 return clib_error_return (0,
274 "no available cpus to be used for"
275 " the '%s' thread", tr->name);
277 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
278 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
283 clib_bitmap_free (avail_cpu);
285 tm->n_vlib_mains = n_vlib_mains;
287 vec_validate_aligned (vlib_worker_threads, first_index - 1,
288 CLIB_CACHE_LINE_BYTES);
291 tm->efd.enabled = VLIB_EFD_DISABLED;
292 tm->efd.queue_hi_thresh = ((VLIB_EFD_DEF_WORKER_HI_THRESH_PCT *
293 FRAME_QUEUE_NELTS) / 100);
297 vlib_worker_thread_t *
298 vlib_alloc_thread (vlib_main_t * vm)
300 vlib_worker_thread_t *w;
302 if (vec_len (vlib_worker_threads) >= vec_len (vlib_thread_stacks))
304 clib_warning ("out of worker threads... Quitting...");
307 vec_add2 (vlib_worker_threads, w, 1);
308 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
313 vlib_frame_queue_alloc (int nelts)
315 vlib_frame_queue_t *fq;
317 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
318 memset (fq, 0, sizeof (*fq));
320 fq->vector_threshold = 128; // packets
321 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
325 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
326 fformat (stderr, "WARNING: fq->tail unaligned\n");
327 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
328 fformat (stderr, "WARNING: fq->head unaligned\n");
329 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
330 fformat (stderr, "WARNING: fq->elts unaligned\n");
332 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
333 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
334 sizeof (fq->elts[0]));
335 if (nelts & (nelts - 1))
337 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
345 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
347 vl_msg_api_handler_no_free (void *v)
351 /* Turned off, save as reference material... */
354 vlib_frame_queue_dequeue_internal (int thread_id,
355 vlib_main_t * vm, vlib_node_main_t * nm)
357 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
358 vlib_frame_queue_elt_t *elt;
360 vlib_pending_frame_t *p;
361 vlib_node_runtime_t *r;
362 u32 node_runtime_index;
367 ASSERT (vm == vlib_mains[thread_id]);
371 if (fq->head == fq->tail)
374 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
379 before = clib_cpu_time_now ();
382 node_runtime_index = elt->node_runtime_index;
383 msg_type = elt->msg_type;
387 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
388 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
389 /* note fallthrough... */
390 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
391 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
393 vlib_frame_free (vm, r, f);
395 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
396 vec_add2 (vm->node_main.pending_frames, p, 1);
397 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
398 p->node_runtime_index = elt->node_runtime_index;
399 p->frame_index = vlib_frame_index (vm, f);
400 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
401 fq->dequeue_vectors += (u64) f->n_vectors;
403 case VLIB_FRAME_QUEUE_ELT_API_MSG:
404 vl_msg_api_handler_no_free (f);
407 clib_warning ("bogus frame queue message, type %d", msg_type);
412 fq->dequeue_ticks += clib_cpu_time_now () - before;
413 CLIB_MEMORY_BARRIER ();
422 vlib_frame_queue_dequeue (int thread_id,
423 vlib_main_t * vm, vlib_node_main_t * nm)
425 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
429 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
430 u32 frame_queue_index, vlib_frame_t * frame,
431 vlib_frame_queue_msg_type_t type)
433 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
434 vlib_frame_queue_elt_t *elt;
437 u64 before = clib_cpu_time_now ();
441 new_tail = __sync_add_and_fetch (&fq->tail, 1);
443 /* Wait until a ring slot is available */
444 while (new_tail >= fq->head + fq->nelts)
446 f64 b4 = vlib_time_now_ticks (vm, before);
447 vlib_worker_thread_barrier_check (vm, b4);
448 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
449 // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
452 elt = fq->elts + (new_tail & (fq->nelts - 1));
454 /* this would be very bad... */
459 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
460 save_count = frame->n_vectors;
463 elt->node_runtime_index = node_runtime_index;
464 elt->msg_type = type;
465 CLIB_MEMORY_BARRIER ();
472 /* To be called by vlib worker threads upon startup */
474 vlib_worker_thread_init (vlib_worker_thread_t * w)
476 vlib_thread_main_t *tm = vlib_get_thread_main ();
478 /* worker threads wants no signals. */
482 pthread_sigmask (SIG_SETMASK, &s, 0);
485 clib_mem_set_heap (w->thread_mheap);
487 if (vec_len (tm->thread_prefix) && w->registration->short_name)
489 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
490 w->registration->short_name, w->instance_id, '\0');
491 vlib_set_thread_name ((char *) w->name);
494 if (!w->registration->use_pthreads)
497 /* Initial barrier sync, for both worker and i/o threads */
498 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
500 while (*vlib_worker_threads->wait_at_barrier)
503 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
508 vlib_worker_thread_bootstrap_fn (void *arg)
511 vlib_worker_thread_t *w = arg;
513 w->lwp = syscall (SYS_gettid);
514 w->thread_id = pthread_self ();
516 rv = (void *) clib_calljmp
517 ((uword (*)(uword)) w->thread_function,
518 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
519 /* NOTREACHED, we hope */
524 vlib_launch_thread (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
526 void *(*fp_arg) (void *) = fp;
528 w->lcore_id = lcore_id;
530 if (!w->registration->use_pthreads)
531 if (rte_eal_remote_launch) /* do we have dpdk linked */
532 return rte_eal_remote_launch (fp, (void *) w, lcore_id);
542 CPU_SET (lcore_id, &cpuset);
544 ret = pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w);
546 return pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset);
552 static clib_error_t *
553 start_workers (vlib_main_t * vm)
556 vlib_worker_thread_t *w;
557 vlib_main_t *vm_clone;
559 vlib_frame_queue_t *fq;
560 vlib_thread_main_t *tm = &vlib_thread_main;
561 vlib_thread_registration_t *tr;
562 vlib_node_runtime_t *rt;
563 u32 n_vlib_mains = tm->n_vlib_mains;
564 u32 worker_thread_index;
565 u8 *main_heap = clib_mem_get_per_cpu_heap ();
566 mheap_t *main_heap_header = mheap_header (main_heap);
568 vec_reset_length (vlib_worker_threads);
570 /* Set up the main thread */
571 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
572 w->elog_track.name = "main thread";
573 elog_track_register (&vm->elog_main, &w->elog_track);
575 if (vec_len (tm->thread_prefix))
577 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
578 vlib_set_thread_name ((char *) w->name);
582 * Truth of the matter: we always use at least two
583 * threads. So, make the main heap thread-safe
584 * and make the event log thread-safe.
586 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
588 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
589 vm->elog_main.lock[0] = 0;
591 if (n_vlib_mains > 1)
593 vec_validate (vlib_mains, tm->n_vlib_mains - 1);
594 _vec_len (vlib_mains) = 0;
595 vec_add1 (vlib_mains, vm);
597 vec_validate (vlib_frame_queues, tm->n_vlib_mains - 1);
598 _vec_len (vlib_frame_queues) = 0;
599 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
600 vec_add1 (vlib_frame_queues, fq);
602 vlib_worker_threads->wait_at_barrier =
603 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
604 vlib_worker_threads->workers_at_barrier =
605 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
607 /* Ask for an initial barrier sync */
608 *vlib_worker_threads->workers_at_barrier = 0;
609 *vlib_worker_threads->wait_at_barrier = 1;
611 worker_thread_index = 1;
613 for (i = 0; i < vec_len (tm->registrations); i++)
615 vlib_node_main_t *nm, *nm_clone;
616 vlib_buffer_main_t *bm_clone;
617 vlib_buffer_free_list_t *fl_clone, *fl_orig;
618 vlib_buffer_free_list_t *orig_freelist_pool;
621 tr = tm->registrations[i];
626 for (k = 0; k < tr->count; k++)
628 vec_add2 (vlib_worker_threads, w, 1);
631 mheap_alloc (0 /* use VM */ , tr->mheap_size);
633 w->thread_mheap = main_heap;
634 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
635 w->thread_function = tr->function;
636 w->thread_function_arg = w;
638 w->registration = tr;
641 (char *) format (0, "%s %d", tr->name, k + 1);
642 vec_add1 (w->elog_track.name, 0);
643 elog_track_register (&vm->elog_main, &w->elog_track);
645 if (tr->no_data_structure_clone)
648 /* Allocate "to-worker-N" frame queue */
649 if (tr->frame_queue_nelts)
651 fq = vlib_frame_queue_alloc (tr->frame_queue_nelts);
655 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
658 vec_validate (vlib_frame_queues, worker_thread_index);
659 vlib_frame_queues[worker_thread_index] = fq;
661 /* Fork vlib_global_main et al. Look for bugs here */
662 oldheap = clib_mem_set_heap (w->thread_mheap);
664 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
665 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
667 vm_clone->cpu_index = worker_thread_index;
668 vm_clone->heap_base = w->thread_mheap;
669 vm_clone->mbuf_alloc_list = 0;
670 memset (&vm_clone->random_buffer, 0,
671 sizeof (vm_clone->random_buffer));
673 nm = &vlib_mains[0]->node_main;
674 nm_clone = &vm_clone->node_main;
675 /* fork next frames array, preserving node runtime indices */
676 nm_clone->next_frames = vec_dup (nm->next_frames);
677 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
679 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
680 u32 save_node_runtime_index;
683 save_node_runtime_index = nf->node_runtime_index;
684 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
685 vlib_next_frame_init (nf);
686 nf->node_runtime_index = save_node_runtime_index;
687 nf->flags = save_flags;
690 /* fork the frame dispatch queue */
691 nm_clone->pending_frames = 0;
692 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
693 _vec_len (nm_clone->pending_frames) = 0;
697 for (j = 0; j < vec_len (nm->nodes); j++)
700 n = clib_mem_alloc_no_fail (sizeof (*n));
701 clib_memcpy (n, nm->nodes[j], sizeof (*n));
702 /* none of the copied nodes have enqueue rights given out */
703 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
704 memset (&n->stats_total, 0, sizeof (n->stats_total));
705 memset (&n->stats_last_clear, 0,
706 sizeof (n->stats_last_clear));
707 vec_add1 (nm_clone->nodes, n);
709 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
710 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
712 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
713 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
714 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
715 rt->cpu_index = vm_clone->cpu_index;
717 nm_clone->processes = vec_dup (nm->processes);
719 /* zap the (per worker) frame freelists, etc */
720 nm_clone->frame_sizes = 0;
721 nm_clone->frame_size_hash = 0;
723 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
725 clib_mem_set_heap (oldheap);
726 vec_add1 (vlib_mains, vm_clone);
728 vm_clone->error_main.counters =
729 vec_dup (vlib_mains[0]->error_main.counters);
730 vm_clone->error_main.counters_last_clear =
731 vec_dup (vlib_mains[0]->error_main.counters_last_clear);
733 /* Fork the vlib_buffer_main_t free lists, etc. */
734 bm_clone = vec_dup (vm_clone->buffer_main);
735 vm_clone->buffer_main = bm_clone;
737 orig_freelist_pool = bm_clone->buffer_free_list_pool;
738 bm_clone->buffer_free_list_pool = 0;
741 pool_foreach (fl_orig, orig_freelist_pool,
743 pool_get_aligned (bm_clone->buffer_free_list_pool,
744 fl_clone, CLIB_CACHE_LINE_BYTES);
745 ASSERT (fl_orig - orig_freelist_pool
746 == fl_clone - bm_clone->buffer_free_list_pool);
748 fl_clone[0] = fl_orig[0];
749 fl_clone->aligned_buffers = 0;
750 fl_clone->unaligned_buffers = 0;
751 fl_clone->n_alloc = 0;
755 worker_thread_index++;
761 /* only have non-data-structure copy threads to create... */
762 for (i = 0; i < vec_len (tm->registrations); i++)
764 tr = tm->registrations[i];
766 for (j = 0; j < tr->count; j++)
768 vec_add2 (vlib_worker_threads, w, 1);
771 mheap_alloc (0 /* use VM */ , tr->mheap_size);
773 w->thread_mheap = main_heap;
774 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
775 w->thread_function = tr->function;
776 w->thread_function_arg = w;
779 (char *) format (0, "%s %d", tr->name, j + 1);
780 w->registration = tr;
781 vec_add1 (w->elog_track.name, 0);
782 elog_track_register (&vm->elog_main, &w->elog_track);
787 worker_thread_index = 1;
789 for (i = 0; i < vec_len (tm->registrations); i++)
793 tr = tm->registrations[i];
795 if (tr->use_pthreads || tm->use_pthreads)
797 for (j = 0; j < tr->count; j++)
799 w = vlib_worker_threads + worker_thread_index++;
800 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, 0) <
802 clib_warning ("Couldn't start '%s' pthread ", tr->name);
809 clib_bitmap_foreach (c, tr->coremask, ({
810 w = vlib_worker_threads + worker_thread_index++;
811 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, c) < 0)
812 clib_warning ("Couldn't start DPDK lcore %d", c);
818 vlib_worker_thread_barrier_sync (vm);
819 vlib_worker_thread_barrier_release (vm);
823 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
826 vlib_worker_thread_node_runtime_update (void)
829 vlib_worker_thread_t *w;
831 vlib_node_main_t *nm, *nm_clone;
832 vlib_node_t **old_nodes_clone;
833 vlib_main_t *vm_clone;
834 vlib_node_runtime_t *rt, *old_rt;
837 vlib_node_runtime_sync_stats (vlib_main_t * vm,
838 vlib_node_runtime_t * r,
840 uword n_vectors, uword n_clocks);
842 ASSERT (os_get_cpu_number () == 0);
844 if (vec_len (vlib_mains) == 0)
850 ASSERT (os_get_cpu_number () == 0);
851 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
854 * Scrape all runtime stats, so we don't lose node runtime(s) with
855 * pending counts, or throw away worker / io thread counts.
857 for (j = 0; j < vec_len (nm->nodes); j++)
861 vlib_node_sync_stats (vm, n);
864 for (i = 1; i < vec_len (vlib_mains); i++)
868 vm_clone = vlib_mains[i];
869 nm_clone = &vm_clone->node_main;
871 for (j = 0; j < vec_len (nm_clone->nodes); j++)
873 n = nm_clone->nodes[j];
875 rt = vlib_node_get_runtime (vm_clone, n->index);
876 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
880 for (i = 1; i < vec_len (vlib_mains); i++)
882 vlib_node_runtime_t *rt;
883 w = vlib_worker_threads + i;
884 oldheap = clib_mem_set_heap (w->thread_mheap);
886 vm_clone = vlib_mains[i];
888 /* Re-clone error heap */
889 u64 *old_counters = vm_clone->error_main.counters;
890 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
891 clib_memcpy (&vm_clone->error_main, &vm->error_main,
892 sizeof (vm->error_main));
893 j = vec_len (vm->error_main.counters) - 1;
894 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
895 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
896 vm_clone->error_main.counters = old_counters;
897 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
899 nm_clone = &vm_clone->node_main;
900 vec_free (nm_clone->next_frames);
901 nm_clone->next_frames = vec_dup (nm->next_frames);
903 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
905 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
906 u32 save_node_runtime_index;
909 save_node_runtime_index = nf->node_runtime_index;
910 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
911 vlib_next_frame_init (nf);
912 nf->node_runtime_index = save_node_runtime_index;
913 nf->flags = save_flags;
916 old_nodes_clone = nm_clone->nodes;
920 for (j = 0; j < vec_len (nm->nodes); j++)
922 vlib_node_t *old_n_clone;
923 vlib_node_t *new_n, *new_n_clone;
925 new_n = nm->nodes[j];
926 old_n_clone = old_nodes_clone[j];
928 new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
929 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
930 /* none of the copied nodes have enqueue rights given out */
931 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
933 if (j >= vec_len (old_nodes_clone))
935 /* new node, set to zero */
936 memset (&new_n_clone->stats_total, 0,
937 sizeof (new_n_clone->stats_total));
938 memset (&new_n_clone->stats_last_clear, 0,
939 sizeof (new_n_clone->stats_last_clear));
943 /* Copy stats if the old data is valid */
944 clib_memcpy (&new_n_clone->stats_total,
945 &old_n_clone->stats_total,
946 sizeof (new_n_clone->stats_total));
947 clib_memcpy (&new_n_clone->stats_last_clear,
948 &old_n_clone->stats_last_clear,
949 sizeof (new_n_clone->stats_last_clear));
951 /* keep previous node state */
952 new_n_clone->state = old_n_clone->state;
954 vec_add1 (nm_clone->nodes, new_n_clone);
956 /* Free the old node clone */
957 for (j = 0; j < vec_len (old_nodes_clone); j++)
958 clib_mem_free (old_nodes_clone[j]);
959 vec_free (old_nodes_clone);
961 vec_free (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
963 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
964 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
966 /* clone input node runtime */
967 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
969 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
970 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
972 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
974 rt->cpu_index = vm_clone->cpu_index;
977 for (j = 0; j < vec_len (old_rt); j++)
979 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
980 rt->state = old_rt[j].state;
985 nm_clone->processes = vec_dup (nm->processes);
987 clib_mem_set_heap (oldheap);
989 // vnet_main_fork_fixup (i);
994 unformat_sched_policy (unformat_input_t * input, va_list * args)
996 u32 *r = va_arg (*args, u32 *);
999 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1000 foreach_sched_policy
1007 static clib_error_t *
1008 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1010 vlib_thread_registration_t *tr;
1012 vlib_thread_main_t *tm = &vlib_thread_main;
1018 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1020 tm->n_thread_stacks = 1; /* account for main thread */
1021 tm->sched_policy = ~0;
1022 tm->sched_priority = ~0;
1028 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1032 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1034 if (unformat (input, "use-pthreads"))
1035 tm->use_pthreads = 1;
1036 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1038 else if (unformat (input, "main-core %u", &tm->main_lcore))
1040 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1042 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1044 p = hash_get_mem (tm->thread_registrations_by_name, name);
1046 return clib_error_return (0, "no such thread type '%s'", name);
1048 tr = (vlib_thread_registration_t *) p[0];
1050 if (tr->use_pthreads)
1051 return clib_error_return (0,
1052 "coremask cannot be set for '%s' threads",
1055 tr->coremask = clib_bitmap_set_multiple
1056 (tr->coremask, 0, coremask, BITS (coremask));
1057 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1059 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1062 p = hash_get_mem (tm->thread_registrations_by_name, name);
1064 return clib_error_return (0, "no such thread type '%s'", name);
1066 tr = (vlib_thread_registration_t *) p[0];
1068 if (tr->use_pthreads)
1069 return clib_error_return (0,
1070 "corelist cannot be set for '%s' threads",
1073 tr->coremask = bitmap;
1074 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1078 (input, "scheduler-policy %U", unformat_sched_policy,
1081 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1083 else if (unformat (input, "%s %u", &name, &count))
1085 p = hash_get_mem (tm->thread_registrations_by_name, name);
1087 return clib_error_return (0, "no such thread type 3 '%s'", name);
1089 tr = (vlib_thread_registration_t *) p[0];
1090 if (tr->fixed_count)
1091 return clib_error_return
1092 (0, "number of %s threads not configurable", tr->name);
1099 if (tm->sched_priority != ~0)
1101 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1103 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1104 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1105 if (tm->sched_priority > prio_max)
1106 tm->sched_priority = prio_max;
1107 if (tm->sched_priority < prio_min)
1108 tm->sched_priority = prio_min;
1112 return clib_error_return
1114 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1115 tm->sched_priority);
1120 if (!tm->thread_prefix)
1121 tm->thread_prefix = format (0, "vpp");
1125 tm->n_thread_stacks += tr->count;
1126 tm->n_pthreads += tr->count * tr->use_pthreads;
1127 tm->n_eal_threads += tr->count * (tr->use_pthreads == 0);
1134 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1136 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1138 __sync_fetch_and_add_8 (void)
1140 fformat (stderr, "%s called\n", __FUNCTION__);
1145 __sync_add_and_fetch_8 (void)
1147 fformat (stderr, "%s called\n", __FUNCTION__);
1152 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1154 vnet_main_fixup (vlib_fork_fixup_t which)
1159 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1161 vlib_main_t *vm = vlib_get_main ();
1163 if (vlib_mains == 0)
1166 ASSERT (os_get_cpu_number () == 0);
1167 vlib_worker_thread_barrier_sync (vm);
1171 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1172 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1178 vlib_worker_thread_barrier_release (vm);
1182 vlib_worker_thread_barrier_sync (vlib_main_t * vm)
1190 count = vec_len (vlib_mains) - 1;
1192 /* Tolerate recursive calls */
1193 if (++vlib_worker_threads[0].recursion_level > 1)
1196 vlib_worker_threads[0].barrier_sync_count++;
1198 ASSERT (os_get_cpu_number () == 0);
1200 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1202 *vlib_worker_threads->wait_at_barrier = 1;
1203 while (*vlib_worker_threads->workers_at_barrier != count)
1205 if (vlib_time_now (vm) > deadline)
1207 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1214 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1221 if (--vlib_worker_threads[0].recursion_level > 0)
1224 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1226 *vlib_worker_threads->wait_at_barrier = 0;
1228 while (*vlib_worker_threads->workers_at_barrier > 0)
1230 if (vlib_time_now (vm) > deadline)
1232 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1239 * Check the frame queue to see if any frames are available.
1240 * If so, pull the packets off the frames and put them to
1244 vlib_frame_queue_dequeue_internal (vlib_main_t * vm)
1246 u32 thread_id = vm->cpu_index;
1247 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
1248 vlib_frame_queue_elt_t *elt;
1255 vlib_thread_main_t *tm = vlib_get_thread_main ();
1258 ASSERT (vm == vlib_mains[thread_id]);
1260 if (PREDICT_FALSE (tm->handoff_dispatch_node_index == ~0))
1263 * Gather trace data for frame queues
1265 if (PREDICT_FALSE (fq->trace))
1267 frame_queue_trace_t *fqt;
1268 frame_queue_nelt_counter_t *fqh;
1271 fqt = &tm->frame_queue_traces[thread_id];
1273 fqt->nelts = fq->nelts;
1274 fqt->head = fq->head;
1275 fqt->head_hint = fq->head_hint;
1276 fqt->tail = fq->tail;
1277 fqt->threshold = fq->vector_threshold;
1278 fqt->n_in_use = fqt->tail - fqt->head;
1279 if (fqt->n_in_use >= fqt->nelts)
1281 // if beyond max then use max
1282 fqt->n_in_use = fqt->nelts - 1;
1285 /* Record the number of elements in use in the histogram */
1286 fqh = &tm->frame_queue_histogram[thread_id];
1287 fqh->count[fqt->n_in_use]++;
1289 /* Record a snapshot of the elements in use */
1290 for (elix = 0; elix < fqt->nelts; elix++)
1292 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1293 if (1 || elt->valid)
1295 fqt->n_vectors[elix] = elt->n_vectors;
1303 if (fq->head == fq->tail)
1305 fq->head_hint = fq->head;
1309 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1313 fq->head_hint = fq->head;
1317 from = elt->buffer_index;
1318 msg_type = elt->msg_type;
1320 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1321 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1323 f = vlib_get_frame_to_node (vm, tm->handoff_dispatch_node_index);
1325 to = vlib_frame_vector_args (f);
1327 n_left_to_node = elt->n_vectors;
1329 while (n_left_to_node >= 4)
1337 n_left_to_node -= 4;
1340 while (n_left_to_node > 0)
1348 vectors += elt->n_vectors;
1349 f->n_vectors = elt->n_vectors;
1350 vlib_put_frame_to_node (vm, tm->handoff_dispatch_node_index, f);
1354 elt->msg_type = 0xfefefefe;
1355 CLIB_MEMORY_BARRIER ();
1360 * Limit the number of packets pushed into the graph
1362 if (vectors >= fq->vector_threshold)
1364 fq->head_hint = fq->head;
1372 static_always_inline void
1373 vlib_worker_thread_internal (vlib_main_t * vm)
1375 vlib_node_main_t *nm = &vm->node_main;
1376 u64 cpu_time_now = clib_cpu_time_now ();
1378 vec_alloc (nm->pending_interrupt_node_runtime_indices, 32);
1382 vlib_worker_thread_barrier_check ();
1384 vlib_frame_queue_dequeue_internal (vm);
1386 vlib_node_runtime_t *n;
1387 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1389 cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1390 VLIB_NODE_STATE_POLLING, /* frame */ 0,
1394 /* Next handle interrupts. */
1396 uword l = _vec_len (nm->pending_interrupt_node_runtime_indices);
1400 _vec_len (nm->pending_interrupt_node_runtime_indices) = 0;
1401 for (i = 0; i < l; i++)
1403 n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1405 pending_interrupt_node_runtime_indices
1408 dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1409 VLIB_NODE_STATE_INTERRUPT,
1416 if (_vec_len (nm->pending_frames))
1419 cpu_time_now = clib_cpu_time_now ();
1420 for (i = 0; i < _vec_len (nm->pending_frames); i++)
1422 vlib_pending_frame_t *p;
1424 p = nm->pending_frames + i;
1426 cpu_time_now = dispatch_pending_node (vm, p, cpu_time_now);
1428 _vec_len (nm->pending_frames) = 0;
1430 vlib_increment_main_loop_counter (vm);
1432 /* Record time stamp in case there are no enabled nodes and above
1433 calls do not update time stamp. */
1434 cpu_time_now = clib_cpu_time_now ();
1439 vlib_worker_thread_fn (void *arg)
1441 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1442 vlib_thread_main_t *tm = vlib_get_thread_main ();
1443 vlib_main_t *vm = vlib_get_main ();
1445 ASSERT (vm->cpu_index == os_get_cpu_number ());
1447 vlib_worker_thread_init (w);
1448 clib_time_init (&vm->clib_time);
1449 clib_mem_set_heap (w->thread_mheap);
1451 /* Wait until the dpdk init sequence is complete */
1452 while (tm->worker_thread_release == 0)
1453 vlib_worker_thread_barrier_check ();
1455 vlib_worker_thread_internal (vm);
1459 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1462 .function = vlib_worker_thread_fn,
1467 threads_init (vlib_main_t * vm)
1469 vlib_thread_main_t *tm = vlib_get_thread_main ();
1471 tm->handoff_dispatch_node_index = ~0;
1476 VLIB_INIT_FUNCTION (threads_init);
1479 * fd.io coding-style-patch-verification: ON
1482 * eval: (c-set-style "gnu")