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.
20 #include <vppinfra/format.h>
21 #include <vlib/vlib.h>
23 #include <vlib/threads.h>
24 #include <vlib/unix/cj.h>
28 #include <rte_config.h>
29 #include <rte_common.h>
31 #include <rte_launch.h>
32 #include <rte_lcore.h>
34 DECLARE_CJ_GLOBAL_LOG;
36 #define FRAME_QUEUE_NELTS 32
41 * Weak definitions of DPDK symbols used in this file.
42 * Needed for linking test programs without DPDK libs.
44 unsigned __thread __attribute__ ((weak)) RTE_PER_LCORE (_lcore_id);
45 struct lcore_config __attribute__ ((weak)) lcore_config[];
46 unsigned __attribute__ ((weak)) rte_socket_id ();
47 int __attribute__ ((weak)) rte_eal_remote_launch ();
55 vlib_thread_main_t vlib_thread_main;
58 os_get_cpu_number (void)
64 len = vec_len (vlib_thread_stacks);
68 /* Get any old stack address. */
71 n = ((uword) sp - (uword) vlib_thread_stacks[0])
72 >> VLIB_LOG2_THREAD_STACK_SIZE;
74 /* "processes" have their own stacks, and they always run in thread 0 */
81 vlib_set_thread_name (char *name)
83 int pthread_setname_np (pthread_t __target_thread, const char *__name);
84 pthread_t thread = pthread_self ();
87 pthread_setname_np (thread, name);
91 sort_registrations_by_no_clone (void *a0, void *a1)
93 vlib_thread_registration_t **tr0 = a0;
94 vlib_thread_registration_t **tr1 = a1;
96 return ((i32) ((*tr0)->no_data_structure_clone)
97 - ((i32) ((*tr1)->no_data_structure_clone)));
101 vlib_sysfs_list_to_bitmap (char *filename)
106 fp = fopen (filename, "r");
111 vec_validate (buffer, 256 - 1);
112 if (fgets ((char *) buffer, 256, fp))
115 unformat_init_string (&in, (char *) buffer,
116 strlen ((char *) buffer));
117 unformat (&in, "%U", unformat_bitmap_list, &r);
127 /* Called early in the init sequence */
130 vlib_thread_init (vlib_main_t * vm)
132 vlib_thread_main_t *tm = &vlib_thread_main;
133 vlib_worker_thread_t *w;
134 vlib_thread_registration_t *tr;
135 u32 n_vlib_mains = 1;
140 /* get bitmaps of active cpu cores and sockets */
141 tm->cpu_core_bitmap =
142 vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
143 tm->cpu_socket_bitmap =
144 vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
146 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
149 for (i = 0; i < tm->skip_cores; i++)
151 uword c = clib_bitmap_first_set (avail_cpu);
153 return clib_error_return (0, "no available cpus to skip");
155 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
158 /* grab cpu for main thread */
161 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
162 if (tm->main_lcore == (u8) ~ 0)
163 return clib_error_return (0, "no available cpus to be used for the"
168 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
169 return clib_error_return (0, "cpu %u is not available to be used"
170 " for the main thread", tm->main_lcore);
172 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
174 /* assume that there is socket 0 only if there is no data from sysfs */
175 if (!tm->cpu_socket_bitmap)
176 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
178 /* pin main thread to main_lcore */
183 CPU_SET (tm->main_lcore, &cpuset);
184 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
188 /* as many threads as stacks... */
189 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
190 CLIB_CACHE_LINE_BYTES);
192 /* Preallocate thread 0 */
193 _vec_len (vlib_worker_threads) = 1;
194 w = vlib_worker_threads;
195 w->thread_mheap = clib_mem_get_heap ();
196 w->thread_stack = vlib_thread_stacks[0];
197 w->dpdk_lcore_id = -1;
198 w->lwp = syscall (SYS_gettid);
199 tm->n_vlib_mains = 1;
201 /* assign threads to cores and set n_vlib_mains */
206 vec_add1 (tm->registrations, tr);
210 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
212 for (i = 0; i < vec_len (tm->registrations); i++)
215 tr = tm->registrations[i];
216 tr->first_index = first_index;
217 first_index += tr->count;
218 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
220 /* construct coremask */
221 if (tr->use_pthreads || !tr->count)
228 clib_bitmap_foreach (c, tr->coremask, ({
229 if (clib_bitmap_get(avail_cpu, c) == 0)
230 return clib_error_return (0, "cpu %u is not available to be used"
231 " for the '%s' thread",c, tr->name);
233 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
240 for (j = 0; j < tr->count; j++)
242 uword c = clib_bitmap_first_set (avail_cpu);
244 return clib_error_return (0,
245 "no available cpus to be used for"
246 " the '%s' thread", tr->name);
248 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
249 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
254 clib_bitmap_free (avail_cpu);
256 tm->n_vlib_mains = n_vlib_mains;
258 vec_validate_aligned (vlib_worker_threads, first_index - 1,
259 CLIB_CACHE_LINE_BYTES);
262 tm->efd.enabled = VLIB_EFD_DISABLED;
263 tm->efd.queue_hi_thresh = ((VLIB_EFD_DEF_WORKER_HI_THRESH_PCT *
264 FRAME_QUEUE_NELTS) / 100);
268 vlib_worker_thread_t *
269 vlib_alloc_thread (vlib_main_t * vm)
271 vlib_worker_thread_t *w;
273 if (vec_len (vlib_worker_threads) >= vec_len (vlib_thread_stacks))
275 clib_warning ("out of worker threads... Quitting...");
278 vec_add2 (vlib_worker_threads, w, 1);
279 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
284 vlib_frame_queue_alloc (int nelts)
286 vlib_frame_queue_t *fq;
288 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
289 memset (fq, 0, sizeof (*fq));
291 fq->vector_threshold = 128; // packets
292 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
296 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
297 fformat (stderr, "WARNING: fq->tail unaligned\n");
298 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
299 fformat (stderr, "WARNING: fq->head unaligned\n");
300 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
301 fformat (stderr, "WARNING: fq->elts unaligned\n");
303 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
304 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
305 sizeof (fq->elts[0]));
306 if (nelts & (nelts - 1))
308 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
316 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
318 vl_msg_api_handler_no_free (void *v)
322 /* Turned off, save as reference material... */
325 vlib_frame_queue_dequeue_internal (int thread_id,
326 vlib_main_t * vm, vlib_node_main_t * nm)
328 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
329 vlib_frame_queue_elt_t *elt;
331 vlib_pending_frame_t *p;
332 vlib_node_runtime_t *r;
333 u32 node_runtime_index;
338 ASSERT (vm == vlib_mains[thread_id]);
342 if (fq->head == fq->tail)
345 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
350 before = clib_cpu_time_now ();
353 node_runtime_index = elt->node_runtime_index;
354 msg_type = elt->msg_type;
358 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
359 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
360 /* note fallthrough... */
361 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
362 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
364 vlib_frame_free (vm, r, f);
366 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
367 vec_add2 (vm->node_main.pending_frames, p, 1);
368 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
369 p->node_runtime_index = elt->node_runtime_index;
370 p->frame_index = vlib_frame_index (vm, f);
371 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
372 fq->dequeue_vectors += (u64) f->n_vectors;
374 case VLIB_FRAME_QUEUE_ELT_API_MSG:
375 vl_msg_api_handler_no_free (f);
378 clib_warning ("bogus frame queue message, type %d", msg_type);
383 fq->dequeue_ticks += clib_cpu_time_now () - before;
384 CLIB_MEMORY_BARRIER ();
393 vlib_frame_queue_dequeue (int thread_id,
394 vlib_main_t * vm, vlib_node_main_t * nm)
396 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
400 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
401 u32 frame_queue_index, vlib_frame_t * frame,
402 vlib_frame_queue_msg_type_t type)
404 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
405 vlib_frame_queue_elt_t *elt;
408 u64 before = clib_cpu_time_now ();
412 new_tail = __sync_add_and_fetch (&fq->tail, 1);
414 /* Wait until a ring slot is available */
415 while (new_tail >= fq->head + fq->nelts)
417 f64 b4 = vlib_time_now_ticks (vm, before);
418 vlib_worker_thread_barrier_check (vm, b4);
419 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
420 // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
423 elt = fq->elts + (new_tail & (fq->nelts - 1));
425 /* this would be very bad... */
430 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
431 save_count = frame->n_vectors;
434 elt->node_runtime_index = node_runtime_index;
435 elt->msg_type = type;
436 CLIB_MEMORY_BARRIER ();
443 /* To be called by vlib worker threads upon startup */
445 vlib_worker_thread_init (vlib_worker_thread_t * w)
447 vlib_thread_main_t *tm = vlib_get_thread_main ();
449 /* worker threads wants no signals. */
453 pthread_sigmask (SIG_SETMASK, &s, 0);
456 clib_mem_set_heap (w->thread_mheap);
458 if (vec_len (tm->thread_prefix) && w->registration->short_name)
460 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
461 w->registration->short_name, w->instance_id, '\0');
462 vlib_set_thread_name ((char *) w->name);
465 if (!w->registration->use_pthreads)
468 /* Initial barrier sync, for both worker and i/o threads */
469 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
471 while (*vlib_worker_threads->wait_at_barrier)
474 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
479 vlib_worker_thread_bootstrap_fn (void *arg)
482 vlib_worker_thread_t *w = arg;
484 w->lwp = syscall (SYS_gettid);
485 w->dpdk_lcore_id = -1;
487 if (w->registration && !w->registration->use_pthreads && rte_socket_id) /* do we really have dpdk linked */
489 unsigned lcore = rte_lcore_id ();
490 lcore = lcore < RTE_MAX_LCORE ? lcore : -1;
491 w->dpdk_lcore_id = lcore;
495 rv = (void *) clib_calljmp
496 ((uword (*)(uword)) w->thread_function,
497 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
498 /* NOTREACHED, we hope */
503 vlib_launch_thread (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
505 void *(*fp_arg) (void *) = fp;
508 if (!w->registration->use_pthreads)
509 if (rte_eal_remote_launch) /* do we have dpdk linked */
510 return rte_eal_remote_launch (fp, (void *) w, lcore_id);
520 CPU_SET (lcore_id, &cpuset);
522 ret = pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w);
524 return pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset);
530 static clib_error_t *
531 start_workers (vlib_main_t * vm)
534 vlib_worker_thread_t *w;
535 vlib_main_t *vm_clone;
537 vlib_frame_queue_t *fq;
538 vlib_thread_main_t *tm = &vlib_thread_main;
539 vlib_thread_registration_t *tr;
540 vlib_node_runtime_t *rt;
541 u32 n_vlib_mains = tm->n_vlib_mains;
542 u32 worker_thread_index;
543 u8 *main_heap = clib_mem_get_per_cpu_heap ();
544 mheap_t *main_heap_header = mheap_header (main_heap);
546 vec_reset_length (vlib_worker_threads);
548 /* Set up the main thread */
549 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
550 w->elog_track.name = "main thread";
551 elog_track_register (&vm->elog_main, &w->elog_track);
553 if (vec_len (tm->thread_prefix))
555 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
556 vlib_set_thread_name ((char *) w->name);
560 w->dpdk_lcore_id = -1;
561 if (rte_socket_id) /* do we really have dpdk linked */
563 unsigned lcore = rte_lcore_id ();
564 w->dpdk_lcore_id = lcore < RTE_MAX_LCORE ? lcore : -1;;
569 * Truth of the matter: we always use at least two
570 * threads. So, make the main heap thread-safe
571 * and make the event log thread-safe.
573 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
575 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
576 vm->elog_main.lock[0] = 0;
578 if (n_vlib_mains > 1)
580 vec_validate (vlib_mains, tm->n_vlib_mains - 1);
581 _vec_len (vlib_mains) = 0;
582 vec_add1 (vlib_mains, vm);
584 vec_validate (vlib_frame_queues, tm->n_vlib_mains - 1);
585 _vec_len (vlib_frame_queues) = 0;
586 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
587 vec_add1 (vlib_frame_queues, fq);
589 vlib_worker_threads->wait_at_barrier =
590 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
591 vlib_worker_threads->workers_at_barrier =
592 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
594 /* Ask for an initial barrier sync */
595 *vlib_worker_threads->workers_at_barrier = 0;
596 *vlib_worker_threads->wait_at_barrier = 1;
598 worker_thread_index = 1;
600 for (i = 0; i < vec_len (tm->registrations); i++)
602 vlib_node_main_t *nm, *nm_clone;
603 vlib_buffer_main_t *bm_clone;
604 vlib_buffer_free_list_t *fl_clone, *fl_orig;
605 vlib_buffer_free_list_t *orig_freelist_pool;
608 tr = tm->registrations[i];
613 for (k = 0; k < tr->count; k++)
615 vec_add2 (vlib_worker_threads, w, 1);
618 mheap_alloc (0 /* use VM */ , tr->mheap_size);
620 w->thread_mheap = main_heap;
621 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
622 w->thread_function = tr->function;
623 w->thread_function_arg = w;
625 w->registration = tr;
628 (char *) format (0, "%s %d", tr->name, k + 1);
629 vec_add1 (w->elog_track.name, 0);
630 elog_track_register (&vm->elog_main, &w->elog_track);
632 if (tr->no_data_structure_clone)
635 /* Allocate "to-worker-N" frame queue */
636 if (tr->frame_queue_nelts)
638 fq = vlib_frame_queue_alloc (tr->frame_queue_nelts);
642 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
645 vec_validate (vlib_frame_queues, worker_thread_index);
646 vlib_frame_queues[worker_thread_index] = fq;
648 /* Fork vlib_global_main et al. Look for bugs here */
649 oldheap = clib_mem_set_heap (w->thread_mheap);
651 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
652 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
654 vm_clone->cpu_index = worker_thread_index;
655 vm_clone->heap_base = w->thread_mheap;
656 vm_clone->mbuf_alloc_list = 0;
657 memset (&vm_clone->random_buffer, 0,
658 sizeof (vm_clone->random_buffer));
660 nm = &vlib_mains[0]->node_main;
661 nm_clone = &vm_clone->node_main;
662 /* fork next frames array, preserving node runtime indices */
663 nm_clone->next_frames = vec_dup (nm->next_frames);
664 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
666 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
667 u32 save_node_runtime_index;
670 save_node_runtime_index = nf->node_runtime_index;
671 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
672 vlib_next_frame_init (nf);
673 nf->node_runtime_index = save_node_runtime_index;
674 nf->flags = save_flags;
677 /* fork the frame dispatch queue */
678 nm_clone->pending_frames = 0;
679 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
680 _vec_len (nm_clone->pending_frames) = 0;
684 for (j = 0; j < vec_len (nm->nodes); j++)
687 n = clib_mem_alloc_no_fail (sizeof (*n));
688 clib_memcpy (n, nm->nodes[j], sizeof (*n));
689 /* none of the copied nodes have enqueue rights given out */
690 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
691 memset (&n->stats_total, 0, sizeof (n->stats_total));
692 memset (&n->stats_last_clear, 0,
693 sizeof (n->stats_last_clear));
694 vec_add1 (nm_clone->nodes, n);
696 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
697 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
699 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
700 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
701 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
702 rt->cpu_index = vm_clone->cpu_index;
704 nm_clone->processes = vec_dup (nm->processes);
706 /* zap the (per worker) frame freelists, etc */
707 nm_clone->frame_sizes = 0;
708 nm_clone->frame_size_hash = 0;
710 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
712 clib_mem_set_heap (oldheap);
713 vec_add1 (vlib_mains, vm_clone);
715 vm_clone->error_main.counters =
716 vec_dup (vlib_mains[0]->error_main.counters);
717 vm_clone->error_main.counters_last_clear =
718 vec_dup (vlib_mains[0]->error_main.counters_last_clear);
720 /* Fork the vlib_buffer_main_t free lists, etc. */
721 bm_clone = vec_dup (vm_clone->buffer_main);
722 vm_clone->buffer_main = bm_clone;
724 orig_freelist_pool = bm_clone->buffer_free_list_pool;
725 bm_clone->buffer_free_list_pool = 0;
728 pool_foreach (fl_orig, orig_freelist_pool,
730 pool_get_aligned (bm_clone->buffer_free_list_pool,
731 fl_clone, CLIB_CACHE_LINE_BYTES);
732 ASSERT (fl_orig - orig_freelist_pool
733 == fl_clone - bm_clone->buffer_free_list_pool);
735 fl_clone[0] = fl_orig[0];
736 fl_clone->aligned_buffers = 0;
737 fl_clone->unaligned_buffers = 0;
738 fl_clone->n_alloc = 0;
742 worker_thread_index++;
748 /* only have non-data-structure copy threads to create... */
749 for (i = 0; i < vec_len (tm->registrations); i++)
751 tr = tm->registrations[i];
753 for (j = 0; j < tr->count; j++)
755 vec_add2 (vlib_worker_threads, w, 1);
758 mheap_alloc (0 /* use VM */ , tr->mheap_size);
760 w->thread_mheap = main_heap;
761 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
762 w->thread_function = tr->function;
763 w->thread_function_arg = w;
766 (char *) format (0, "%s %d", tr->name, j + 1);
767 w->registration = tr;
768 vec_add1 (w->elog_track.name, 0);
769 elog_track_register (&vm->elog_main, &w->elog_track);
774 worker_thread_index = 1;
776 for (i = 0; i < vec_len (tm->registrations); i++)
780 tr = tm->registrations[i];
782 if (tr->use_pthreads || tm->use_pthreads)
784 for (j = 0; j < tr->count; j++)
786 w = vlib_worker_threads + worker_thread_index++;
787 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, 0) <
789 clib_warning ("Couldn't start '%s' pthread ", tr->name);
796 clib_bitmap_foreach (c, tr->coremask, ({
797 w = vlib_worker_threads + worker_thread_index++;
798 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, c) < 0)
799 clib_warning ("Couldn't start DPDK lcore %d", c);
805 vlib_worker_thread_barrier_sync (vm);
806 vlib_worker_thread_barrier_release (vm);
810 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
813 vlib_worker_thread_node_runtime_update (void)
816 vlib_worker_thread_t *w;
818 vlib_node_main_t *nm, *nm_clone;
819 vlib_node_t **old_nodes_clone;
820 vlib_main_t *vm_clone;
821 vlib_node_runtime_t *rt, *old_rt;
824 vlib_node_runtime_sync_stats (vlib_main_t * vm,
825 vlib_node_runtime_t * r,
827 uword n_vectors, uword n_clocks);
829 ASSERT (os_get_cpu_number () == 0);
831 if (vec_len (vlib_mains) == 0)
837 ASSERT (os_get_cpu_number () == 0);
838 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
841 * Scrape all runtime stats, so we don't lose node runtime(s) with
842 * pending counts, or throw away worker / io thread counts.
844 for (j = 0; j < vec_len (nm->nodes); j++)
848 vlib_node_sync_stats (vm, n);
851 for (i = 1; i < vec_len (vlib_mains); i++)
855 vm_clone = vlib_mains[i];
856 nm_clone = &vm_clone->node_main;
858 for (j = 0; j < vec_len (nm_clone->nodes); j++)
860 n = nm_clone->nodes[j];
862 rt = vlib_node_get_runtime (vm_clone, n->index);
863 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
867 for (i = 1; i < vec_len (vlib_mains); i++)
869 vlib_node_runtime_t *rt;
870 w = vlib_worker_threads + i;
871 oldheap = clib_mem_set_heap (w->thread_mheap);
873 vm_clone = vlib_mains[i];
875 /* Re-clone error heap */
876 u64 *old_counters = vm_clone->error_main.counters;
877 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
878 clib_memcpy (&vm_clone->error_main, &vm->error_main,
879 sizeof (vm->error_main));
880 j = vec_len (vm->error_main.counters) - 1;
881 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
882 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
883 vm_clone->error_main.counters = old_counters;
884 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
886 nm_clone = &vm_clone->node_main;
887 vec_free (nm_clone->next_frames);
888 nm_clone->next_frames = vec_dup (nm->next_frames);
890 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
892 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
893 u32 save_node_runtime_index;
896 save_node_runtime_index = nf->node_runtime_index;
897 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
898 vlib_next_frame_init (nf);
899 nf->node_runtime_index = save_node_runtime_index;
900 nf->flags = save_flags;
903 old_nodes_clone = nm_clone->nodes;
907 for (j = 0; j < vec_len (nm->nodes); j++)
909 vlib_node_t *old_n_clone;
910 vlib_node_t *new_n, *new_n_clone;
912 new_n = nm->nodes[j];
913 old_n_clone = old_nodes_clone[j];
915 new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
916 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
917 /* none of the copied nodes have enqueue rights given out */
918 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
920 if (j >= vec_len (old_nodes_clone))
922 /* new node, set to zero */
923 memset (&new_n_clone->stats_total, 0,
924 sizeof (new_n_clone->stats_total));
925 memset (&new_n_clone->stats_last_clear, 0,
926 sizeof (new_n_clone->stats_last_clear));
930 /* Copy stats if the old data is valid */
931 clib_memcpy (&new_n_clone->stats_total,
932 &old_n_clone->stats_total,
933 sizeof (new_n_clone->stats_total));
934 clib_memcpy (&new_n_clone->stats_last_clear,
935 &old_n_clone->stats_last_clear,
936 sizeof (new_n_clone->stats_last_clear));
938 /* keep previous node state */
939 new_n_clone->state = old_n_clone->state;
941 vec_add1 (nm_clone->nodes, new_n_clone);
943 /* Free the old node clone */
944 for (j = 0; j < vec_len (old_nodes_clone); j++)
945 clib_mem_free (old_nodes_clone[j]);
946 vec_free (old_nodes_clone);
948 vec_free (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
950 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
951 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
953 /* clone input node runtime */
954 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
956 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
957 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
959 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
961 rt->cpu_index = vm_clone->cpu_index;
964 for (j = 0; j < vec_len (old_rt); j++)
966 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
967 rt->state = old_rt[j].state;
972 nm_clone->processes = vec_dup (nm->processes);
974 clib_mem_set_heap (oldheap);
976 // vnet_main_fork_fixup (i);
980 static clib_error_t *
981 cpu_config (vlib_main_t * vm, unformat_input_t * input)
983 vlib_thread_registration_t *tr;
985 vlib_thread_main_t *tm = &vlib_thread_main;
991 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
992 tm->n_thread_stacks = 1; /* account for main thread */
998 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1002 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1004 if (unformat (input, "use-pthreads"))
1005 tm->use_pthreads = 1;
1006 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1008 else if (unformat (input, "main-core %u", &tm->main_lcore))
1010 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1012 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1014 p = hash_get_mem (tm->thread_registrations_by_name, name);
1016 return clib_error_return (0, "no such thread type '%s'", name);
1018 tr = (vlib_thread_registration_t *) p[0];
1020 if (tr->use_pthreads)
1021 return clib_error_return (0,
1022 "coremask cannot be set for '%s' threads",
1025 tr->coremask = clib_bitmap_set_multiple
1026 (tr->coremask, 0, coremask, BITS (coremask));
1027 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1029 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1032 p = hash_get_mem (tm->thread_registrations_by_name, name);
1034 return clib_error_return (0, "no such thread type '%s'", name);
1036 tr = (vlib_thread_registration_t *) p[0];
1038 if (tr->use_pthreads)
1039 return clib_error_return (0,
1040 "corelist cannot be set for '%s' threads",
1043 tr->coremask = bitmap;
1044 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1046 else if (unformat (input, "%s %u", &name, &count))
1048 p = hash_get_mem (tm->thread_registrations_by_name, name);
1050 return clib_error_return (0, "no such thread type '%s'", name);
1052 tr = (vlib_thread_registration_t *) p[0];
1053 if (tr->fixed_count)
1054 return clib_error_return
1055 (0, "number of %s threads not configurable", tr->name);
1064 if (!tm->thread_prefix)
1065 tm->thread_prefix = format (0, "vpp");
1069 tm->n_thread_stacks += tr->count;
1070 tm->n_pthreads += tr->count * tr->use_pthreads;
1071 tm->n_eal_threads += tr->count * (tr->use_pthreads == 0);
1078 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1080 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1082 __sync_fetch_and_add_8 (void)
1084 fformat (stderr, "%s called\n", __FUNCTION__);
1089 __sync_add_and_fetch_8 (void)
1091 fformat (stderr, "%s called\n", __FUNCTION__);
1096 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1098 vnet_main_fixup (vlib_fork_fixup_t which)
1103 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1105 vlib_main_t *vm = vlib_get_main ();
1107 if (vlib_mains == 0)
1110 ASSERT (os_get_cpu_number () == 0);
1111 vlib_worker_thread_barrier_sync (vm);
1115 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1116 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1122 vlib_worker_thread_barrier_release (vm);
1126 vlib_worker_thread_barrier_sync (vlib_main_t * vm)
1134 count = vec_len (vlib_mains) - 1;
1136 /* Tolerate recursive calls */
1137 if (++vlib_worker_threads[0].recursion_level > 1)
1140 vlib_worker_threads[0].barrier_sync_count++;
1142 ASSERT (os_get_cpu_number () == 0);
1144 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1146 *vlib_worker_threads->wait_at_barrier = 1;
1147 while (*vlib_worker_threads->workers_at_barrier != count)
1149 if (vlib_time_now (vm) > deadline)
1151 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1158 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1165 if (--vlib_worker_threads[0].recursion_level > 0)
1168 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1170 *vlib_worker_threads->wait_at_barrier = 0;
1172 while (*vlib_worker_threads->workers_at_barrier > 0)
1174 if (vlib_time_now (vm) > deadline)
1176 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1183 * Check the frame queue to see if any frames are available.
1184 * If so, pull the packets off the frames and put them to
1188 vlib_frame_queue_dequeue_internal (vlib_main_t * vm)
1190 u32 thread_id = vm->cpu_index;
1191 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
1192 vlib_frame_queue_elt_t *elt;
1199 vlib_thread_main_t *tm = vlib_get_thread_main ();
1202 ASSERT (vm == vlib_mains[thread_id]);
1204 if (PREDICT_FALSE (tm->handoff_dispatch_node_index == ~0))
1207 * Gather trace data for frame queues
1209 if (PREDICT_FALSE (fq->trace))
1211 frame_queue_trace_t *fqt;
1212 frame_queue_nelt_counter_t *fqh;
1215 fqt = &tm->frame_queue_traces[thread_id];
1217 fqt->nelts = fq->nelts;
1218 fqt->head = fq->head;
1219 fqt->head_hint = fq->head_hint;
1220 fqt->tail = fq->tail;
1221 fqt->threshold = fq->vector_threshold;
1222 fqt->n_in_use = fqt->tail - fqt->head;
1223 if (fqt->n_in_use >= fqt->nelts)
1225 // if beyond max then use max
1226 fqt->n_in_use = fqt->nelts - 1;
1229 /* Record the number of elements in use in the histogram */
1230 fqh = &tm->frame_queue_histogram[thread_id];
1231 fqh->count[fqt->n_in_use]++;
1233 /* Record a snapshot of the elements in use */
1234 for (elix = 0; elix < fqt->nelts; elix++)
1236 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1237 if (1 || elt->valid)
1239 fqt->n_vectors[elix] = elt->n_vectors;
1247 if (fq->head == fq->tail)
1249 fq->head_hint = fq->head;
1253 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1257 fq->head_hint = fq->head;
1261 from = elt->buffer_index;
1262 msg_type = elt->msg_type;
1264 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1265 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1267 f = vlib_get_frame_to_node (vm, tm->handoff_dispatch_node_index);
1269 to = vlib_frame_vector_args (f);
1271 n_left_to_node = elt->n_vectors;
1273 while (n_left_to_node >= 4)
1281 n_left_to_node -= 4;
1284 while (n_left_to_node > 0)
1292 vectors += elt->n_vectors;
1293 f->n_vectors = elt->n_vectors;
1294 vlib_put_frame_to_node (vm, tm->handoff_dispatch_node_index, f);
1298 elt->msg_type = 0xfefefefe;
1299 CLIB_MEMORY_BARRIER ();
1304 * Limit the number of packets pushed into the graph
1306 if (vectors >= fq->vector_threshold)
1308 fq->head_hint = fq->head;
1316 static_always_inline void
1317 vlib_worker_thread_internal (vlib_main_t * vm)
1319 vlib_node_main_t *nm = &vm->node_main;
1320 u64 cpu_time_now = clib_cpu_time_now ();
1324 vlib_worker_thread_barrier_check ();
1326 vlib_frame_queue_dequeue_internal (vm);
1328 vlib_node_runtime_t *n;
1329 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1331 cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1332 VLIB_NODE_STATE_POLLING, /* frame */ 0,
1336 if (_vec_len (nm->pending_frames))
1339 cpu_time_now = clib_cpu_time_now ();
1340 for (i = 0; i < _vec_len (nm->pending_frames); i++)
1342 vlib_pending_frame_t *p;
1344 p = nm->pending_frames + i;
1346 cpu_time_now = dispatch_pending_node (vm, p, cpu_time_now);
1348 _vec_len (nm->pending_frames) = 0;
1350 vlib_increment_main_loop_counter (vm);
1352 /* Record time stamp in case there are no enabled nodes and above
1353 calls do not update time stamp. */
1354 cpu_time_now = clib_cpu_time_now ();
1359 vlib_worker_thread_fn (void *arg)
1361 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1362 vlib_thread_main_t *tm = vlib_get_thread_main ();
1363 vlib_main_t *vm = vlib_get_main ();
1365 ASSERT (vm->cpu_index == os_get_cpu_number ());
1367 vlib_worker_thread_init (w);
1368 clib_time_init (&vm->clib_time);
1369 clib_mem_set_heap (w->thread_mheap);
1371 /* Wait until the dpdk init sequence is complete */
1372 while (tm->worker_thread_release == 0)
1373 vlib_worker_thread_barrier_check ();
1375 vlib_worker_thread_internal (vm);
1379 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1382 .function = vlib_worker_thread_fn,
1387 threads_init (vlib_main_t * vm)
1389 vlib_thread_main_t *tm = vlib_get_thread_main ();
1391 tm->handoff_dispatch_node_index = ~0;
1396 VLIB_INIT_FUNCTION (threads_init);
1399 * fd.io coding-style-patch-verification: ON
1402 * eval: (c-set-style "gnu")