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);
85 pthread_t thread = pthread_self ();
89 rv = pthread_setname_np (thread, name);
91 clib_warning ("pthread_setname_np returned %d", rv);
96 sort_registrations_by_no_clone (void *a0, void *a1)
98 vlib_thread_registration_t **tr0 = a0;
99 vlib_thread_registration_t **tr1 = a1;
101 return ((i32) ((*tr0)->no_data_structure_clone)
102 - ((i32) ((*tr1)->no_data_structure_clone)));
106 vlib_sysfs_list_to_bitmap (char *filename)
111 fp = fopen (filename, "r");
116 vec_validate (buffer, 256 - 1);
117 if (fgets ((char *) buffer, 256, fp))
120 unformat_init_string (&in, (char *) buffer,
121 strlen ((char *) buffer));
122 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
123 clib_warning ("unformat_bitmap_list failed");
133 /* Called early in the init sequence */
136 vlib_thread_init (vlib_main_t * vm)
138 vlib_thread_main_t *tm = &vlib_thread_main;
139 vlib_worker_thread_t *w;
140 vlib_thread_registration_t *tr;
141 u32 n_vlib_mains = 1;
146 /* get bitmaps of active cpu cores and sockets */
147 tm->cpu_core_bitmap =
148 vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
149 tm->cpu_socket_bitmap =
150 vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
152 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
155 for (i = 0; i < tm->skip_cores; i++)
157 uword c = clib_bitmap_first_set (avail_cpu);
159 return clib_error_return (0, "no available cpus to skip");
161 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
164 /* grab cpu for main thread */
167 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
168 if (tm->main_lcore == (u8) ~ 0)
169 return clib_error_return (0, "no available cpus to be used for the"
174 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
175 return clib_error_return (0, "cpu %u is not available to be used"
176 " for the main thread", tm->main_lcore);
178 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
180 /* assume that there is socket 0 only if there is no data from sysfs */
181 if (!tm->cpu_socket_bitmap)
182 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
184 /* pin main thread to main_lcore */
189 CPU_SET (tm->main_lcore, &cpuset);
190 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
194 /* as many threads as stacks... */
195 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
196 CLIB_CACHE_LINE_BYTES);
198 /* Preallocate thread 0 */
199 _vec_len (vlib_worker_threads) = 1;
200 w = vlib_worker_threads;
201 w->thread_mheap = clib_mem_get_heap ();
202 w->thread_stack = vlib_thread_stacks[0];
203 w->dpdk_lcore_id = -1;
204 w->lwp = syscall (SYS_gettid);
205 tm->n_vlib_mains = 1;
207 /* assign threads to cores and set n_vlib_mains */
212 vec_add1 (tm->registrations, tr);
216 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
218 for (i = 0; i < vec_len (tm->registrations); i++)
221 tr = tm->registrations[i];
222 tr->first_index = first_index;
223 first_index += tr->count;
224 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
226 /* construct coremask */
227 if (tr->use_pthreads || !tr->count)
234 clib_bitmap_foreach (c, tr->coremask, ({
235 if (clib_bitmap_get(avail_cpu, c) == 0)
236 return clib_error_return (0, "cpu %u is not available to be used"
237 " for the '%s' thread",c, tr->name);
239 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
246 for (j = 0; j < tr->count; j++)
248 uword c = clib_bitmap_first_set (avail_cpu);
250 return clib_error_return (0,
251 "no available cpus to be used for"
252 " the '%s' thread", tr->name);
254 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
255 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
260 clib_bitmap_free (avail_cpu);
262 tm->n_vlib_mains = n_vlib_mains;
264 vec_validate_aligned (vlib_worker_threads, first_index - 1,
265 CLIB_CACHE_LINE_BYTES);
268 tm->efd.enabled = VLIB_EFD_DISABLED;
269 tm->efd.queue_hi_thresh = ((VLIB_EFD_DEF_WORKER_HI_THRESH_PCT *
270 FRAME_QUEUE_NELTS) / 100);
274 vlib_worker_thread_t *
275 vlib_alloc_thread (vlib_main_t * vm)
277 vlib_worker_thread_t *w;
279 if (vec_len (vlib_worker_threads) >= vec_len (vlib_thread_stacks))
281 clib_warning ("out of worker threads... Quitting...");
284 vec_add2 (vlib_worker_threads, w, 1);
285 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
290 vlib_frame_queue_alloc (int nelts)
292 vlib_frame_queue_t *fq;
294 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
295 memset (fq, 0, sizeof (*fq));
297 fq->vector_threshold = 128; // packets
298 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
302 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
303 fformat (stderr, "WARNING: fq->tail unaligned\n");
304 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
305 fformat (stderr, "WARNING: fq->head unaligned\n");
306 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
307 fformat (stderr, "WARNING: fq->elts unaligned\n");
309 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
310 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
311 sizeof (fq->elts[0]));
312 if (nelts & (nelts - 1))
314 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
322 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
324 vl_msg_api_handler_no_free (void *v)
328 /* Turned off, save as reference material... */
331 vlib_frame_queue_dequeue_internal (int thread_id,
332 vlib_main_t * vm, vlib_node_main_t * nm)
334 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
335 vlib_frame_queue_elt_t *elt;
337 vlib_pending_frame_t *p;
338 vlib_node_runtime_t *r;
339 u32 node_runtime_index;
344 ASSERT (vm == vlib_mains[thread_id]);
348 if (fq->head == fq->tail)
351 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
356 before = clib_cpu_time_now ();
359 node_runtime_index = elt->node_runtime_index;
360 msg_type = elt->msg_type;
364 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
365 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
366 /* note fallthrough... */
367 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
368 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
370 vlib_frame_free (vm, r, f);
372 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
373 vec_add2 (vm->node_main.pending_frames, p, 1);
374 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
375 p->node_runtime_index = elt->node_runtime_index;
376 p->frame_index = vlib_frame_index (vm, f);
377 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
378 fq->dequeue_vectors += (u64) f->n_vectors;
380 case VLIB_FRAME_QUEUE_ELT_API_MSG:
381 vl_msg_api_handler_no_free (f);
384 clib_warning ("bogus frame queue message, type %d", msg_type);
389 fq->dequeue_ticks += clib_cpu_time_now () - before;
390 CLIB_MEMORY_BARRIER ();
399 vlib_frame_queue_dequeue (int thread_id,
400 vlib_main_t * vm, vlib_node_main_t * nm)
402 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
406 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
407 u32 frame_queue_index, vlib_frame_t * frame,
408 vlib_frame_queue_msg_type_t type)
410 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
411 vlib_frame_queue_elt_t *elt;
414 u64 before = clib_cpu_time_now ();
418 new_tail = __sync_add_and_fetch (&fq->tail, 1);
420 /* Wait until a ring slot is available */
421 while (new_tail >= fq->head + fq->nelts)
423 f64 b4 = vlib_time_now_ticks (vm, before);
424 vlib_worker_thread_barrier_check (vm, b4);
425 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
426 // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
429 elt = fq->elts + (new_tail & (fq->nelts - 1));
431 /* this would be very bad... */
436 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
437 save_count = frame->n_vectors;
440 elt->node_runtime_index = node_runtime_index;
441 elt->msg_type = type;
442 CLIB_MEMORY_BARRIER ();
449 /* To be called by vlib worker threads upon startup */
451 vlib_worker_thread_init (vlib_worker_thread_t * w)
453 vlib_thread_main_t *tm = vlib_get_thread_main ();
455 /* worker threads wants no signals. */
459 pthread_sigmask (SIG_SETMASK, &s, 0);
462 clib_mem_set_heap (w->thread_mheap);
464 if (vec_len (tm->thread_prefix) && w->registration->short_name)
466 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
467 w->registration->short_name, w->instance_id, '\0');
468 vlib_set_thread_name ((char *) w->name);
471 if (!w->registration->use_pthreads)
474 /* Initial barrier sync, for both worker and i/o threads */
475 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
477 while (*vlib_worker_threads->wait_at_barrier)
480 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
485 vlib_worker_thread_bootstrap_fn (void *arg)
488 vlib_worker_thread_t *w = arg;
490 w->lwp = syscall (SYS_gettid);
491 w->dpdk_lcore_id = -1;
493 if (w->registration && !w->registration->use_pthreads && rte_socket_id) /* do we really have dpdk linked */
495 unsigned lcore = rte_lcore_id ();
496 lcore = lcore < RTE_MAX_LCORE ? lcore : -1;
497 w->dpdk_lcore_id = lcore;
501 rv = (void *) clib_calljmp
502 ((uword (*)(uword)) w->thread_function,
503 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
504 /* NOTREACHED, we hope */
509 vlib_launch_thread (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
511 void *(*fp_arg) (void *) = fp;
514 if (!w->registration->use_pthreads)
515 if (rte_eal_remote_launch) /* do we have dpdk linked */
516 return rte_eal_remote_launch (fp, (void *) w, lcore_id);
526 CPU_SET (lcore_id, &cpuset);
528 ret = pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w);
530 return pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset);
536 static clib_error_t *
537 start_workers (vlib_main_t * vm)
540 vlib_worker_thread_t *w;
541 vlib_main_t *vm_clone;
543 vlib_frame_queue_t *fq;
544 vlib_thread_main_t *tm = &vlib_thread_main;
545 vlib_thread_registration_t *tr;
546 vlib_node_runtime_t *rt;
547 u32 n_vlib_mains = tm->n_vlib_mains;
548 u32 worker_thread_index;
549 u8 *main_heap = clib_mem_get_per_cpu_heap ();
550 mheap_t *main_heap_header = mheap_header (main_heap);
552 vec_reset_length (vlib_worker_threads);
554 /* Set up the main thread */
555 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
556 w->elog_track.name = "main thread";
557 elog_track_register (&vm->elog_main, &w->elog_track);
559 if (vec_len (tm->thread_prefix))
561 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
562 vlib_set_thread_name ((char *) w->name);
566 w->dpdk_lcore_id = -1;
567 if (rte_socket_id) /* do we really have dpdk linked */
569 unsigned lcore = rte_lcore_id ();
570 w->dpdk_lcore_id = lcore < RTE_MAX_LCORE ? lcore : -1;;
575 * Truth of the matter: we always use at least two
576 * threads. So, make the main heap thread-safe
577 * and make the event log thread-safe.
579 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
581 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
582 vm->elog_main.lock[0] = 0;
584 if (n_vlib_mains > 1)
586 vec_validate (vlib_mains, tm->n_vlib_mains - 1);
587 _vec_len (vlib_mains) = 0;
588 vec_add1 (vlib_mains, vm);
590 vec_validate (vlib_frame_queues, tm->n_vlib_mains - 1);
591 _vec_len (vlib_frame_queues) = 0;
592 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
593 vec_add1 (vlib_frame_queues, fq);
595 vlib_worker_threads->wait_at_barrier =
596 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
597 vlib_worker_threads->workers_at_barrier =
598 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
600 /* Ask for an initial barrier sync */
601 *vlib_worker_threads->workers_at_barrier = 0;
602 *vlib_worker_threads->wait_at_barrier = 1;
604 worker_thread_index = 1;
606 for (i = 0; i < vec_len (tm->registrations); i++)
608 vlib_node_main_t *nm, *nm_clone;
609 vlib_buffer_main_t *bm_clone;
610 vlib_buffer_free_list_t *fl_clone, *fl_orig;
611 vlib_buffer_free_list_t *orig_freelist_pool;
614 tr = tm->registrations[i];
619 for (k = 0; k < tr->count; k++)
621 vec_add2 (vlib_worker_threads, w, 1);
624 mheap_alloc (0 /* use VM */ , tr->mheap_size);
626 w->thread_mheap = main_heap;
627 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
628 w->thread_function = tr->function;
629 w->thread_function_arg = w;
631 w->registration = tr;
634 (char *) format (0, "%s %d", tr->name, k + 1);
635 vec_add1 (w->elog_track.name, 0);
636 elog_track_register (&vm->elog_main, &w->elog_track);
638 if (tr->no_data_structure_clone)
641 /* Allocate "to-worker-N" frame queue */
642 if (tr->frame_queue_nelts)
644 fq = vlib_frame_queue_alloc (tr->frame_queue_nelts);
648 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
651 vec_validate (vlib_frame_queues, worker_thread_index);
652 vlib_frame_queues[worker_thread_index] = fq;
654 /* Fork vlib_global_main et al. Look for bugs here */
655 oldheap = clib_mem_set_heap (w->thread_mheap);
657 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
658 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
660 vm_clone->cpu_index = worker_thread_index;
661 vm_clone->heap_base = w->thread_mheap;
662 vm_clone->mbuf_alloc_list = 0;
663 memset (&vm_clone->random_buffer, 0,
664 sizeof (vm_clone->random_buffer));
666 nm = &vlib_mains[0]->node_main;
667 nm_clone = &vm_clone->node_main;
668 /* fork next frames array, preserving node runtime indices */
669 nm_clone->next_frames = vec_dup (nm->next_frames);
670 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
672 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
673 u32 save_node_runtime_index;
676 save_node_runtime_index = nf->node_runtime_index;
677 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
678 vlib_next_frame_init (nf);
679 nf->node_runtime_index = save_node_runtime_index;
680 nf->flags = save_flags;
683 /* fork the frame dispatch queue */
684 nm_clone->pending_frames = 0;
685 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
686 _vec_len (nm_clone->pending_frames) = 0;
690 for (j = 0; j < vec_len (nm->nodes); j++)
693 n = clib_mem_alloc_no_fail (sizeof (*n));
694 clib_memcpy (n, nm->nodes[j], sizeof (*n));
695 /* none of the copied nodes have enqueue rights given out */
696 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
697 memset (&n->stats_total, 0, sizeof (n->stats_total));
698 memset (&n->stats_last_clear, 0,
699 sizeof (n->stats_last_clear));
700 vec_add1 (nm_clone->nodes, n);
702 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
703 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
705 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
706 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
707 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
708 rt->cpu_index = vm_clone->cpu_index;
710 nm_clone->processes = vec_dup (nm->processes);
712 /* zap the (per worker) frame freelists, etc */
713 nm_clone->frame_sizes = 0;
714 nm_clone->frame_size_hash = 0;
716 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
718 clib_mem_set_heap (oldheap);
719 vec_add1 (vlib_mains, vm_clone);
721 vm_clone->error_main.counters =
722 vec_dup (vlib_mains[0]->error_main.counters);
723 vm_clone->error_main.counters_last_clear =
724 vec_dup (vlib_mains[0]->error_main.counters_last_clear);
726 /* Fork the vlib_buffer_main_t free lists, etc. */
727 bm_clone = vec_dup (vm_clone->buffer_main);
728 vm_clone->buffer_main = bm_clone;
730 orig_freelist_pool = bm_clone->buffer_free_list_pool;
731 bm_clone->buffer_free_list_pool = 0;
734 pool_foreach (fl_orig, orig_freelist_pool,
736 pool_get_aligned (bm_clone->buffer_free_list_pool,
737 fl_clone, CLIB_CACHE_LINE_BYTES);
738 ASSERT (fl_orig - orig_freelist_pool
739 == fl_clone - bm_clone->buffer_free_list_pool);
741 fl_clone[0] = fl_orig[0];
742 fl_clone->aligned_buffers = 0;
743 fl_clone->unaligned_buffers = 0;
744 fl_clone->n_alloc = 0;
748 worker_thread_index++;
754 /* only have non-data-structure copy threads to create... */
755 for (i = 0; i < vec_len (tm->registrations); i++)
757 tr = tm->registrations[i];
759 for (j = 0; j < tr->count; j++)
761 vec_add2 (vlib_worker_threads, w, 1);
764 mheap_alloc (0 /* use VM */ , tr->mheap_size);
766 w->thread_mheap = main_heap;
767 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
768 w->thread_function = tr->function;
769 w->thread_function_arg = w;
772 (char *) format (0, "%s %d", tr->name, j + 1);
773 w->registration = tr;
774 vec_add1 (w->elog_track.name, 0);
775 elog_track_register (&vm->elog_main, &w->elog_track);
780 worker_thread_index = 1;
782 for (i = 0; i < vec_len (tm->registrations); i++)
786 tr = tm->registrations[i];
788 if (tr->use_pthreads || tm->use_pthreads)
790 for (j = 0; j < tr->count; j++)
792 w = vlib_worker_threads + worker_thread_index++;
793 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, 0) <
795 clib_warning ("Couldn't start '%s' pthread ", tr->name);
802 clib_bitmap_foreach (c, tr->coremask, ({
803 w = vlib_worker_threads + worker_thread_index++;
804 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, c) < 0)
805 clib_warning ("Couldn't start DPDK lcore %d", c);
811 vlib_worker_thread_barrier_sync (vm);
812 vlib_worker_thread_barrier_release (vm);
816 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
819 vlib_worker_thread_node_runtime_update (void)
822 vlib_worker_thread_t *w;
824 vlib_node_main_t *nm, *nm_clone;
825 vlib_node_t **old_nodes_clone;
826 vlib_main_t *vm_clone;
827 vlib_node_runtime_t *rt, *old_rt;
830 vlib_node_runtime_sync_stats (vlib_main_t * vm,
831 vlib_node_runtime_t * r,
833 uword n_vectors, uword n_clocks);
835 ASSERT (os_get_cpu_number () == 0);
837 if (vec_len (vlib_mains) == 0)
843 ASSERT (os_get_cpu_number () == 0);
844 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
847 * Scrape all runtime stats, so we don't lose node runtime(s) with
848 * pending counts, or throw away worker / io thread counts.
850 for (j = 0; j < vec_len (nm->nodes); j++)
854 vlib_node_sync_stats (vm, n);
857 for (i = 1; i < vec_len (vlib_mains); i++)
861 vm_clone = vlib_mains[i];
862 nm_clone = &vm_clone->node_main;
864 for (j = 0; j < vec_len (nm_clone->nodes); j++)
866 n = nm_clone->nodes[j];
868 rt = vlib_node_get_runtime (vm_clone, n->index);
869 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
873 for (i = 1; i < vec_len (vlib_mains); i++)
875 vlib_node_runtime_t *rt;
876 w = vlib_worker_threads + i;
877 oldheap = clib_mem_set_heap (w->thread_mheap);
879 vm_clone = vlib_mains[i];
881 /* Re-clone error heap */
882 u64 *old_counters = vm_clone->error_main.counters;
883 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
884 clib_memcpy (&vm_clone->error_main, &vm->error_main,
885 sizeof (vm->error_main));
886 j = vec_len (vm->error_main.counters) - 1;
887 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
888 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
889 vm_clone->error_main.counters = old_counters;
890 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
892 nm_clone = &vm_clone->node_main;
893 vec_free (nm_clone->next_frames);
894 nm_clone->next_frames = vec_dup (nm->next_frames);
896 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
898 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
899 u32 save_node_runtime_index;
902 save_node_runtime_index = nf->node_runtime_index;
903 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
904 vlib_next_frame_init (nf);
905 nf->node_runtime_index = save_node_runtime_index;
906 nf->flags = save_flags;
909 old_nodes_clone = nm_clone->nodes;
913 for (j = 0; j < vec_len (nm->nodes); j++)
915 vlib_node_t *old_n_clone;
916 vlib_node_t *new_n, *new_n_clone;
918 new_n = nm->nodes[j];
919 old_n_clone = old_nodes_clone[j];
921 new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
922 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
923 /* none of the copied nodes have enqueue rights given out */
924 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
926 if (j >= vec_len (old_nodes_clone))
928 /* new node, set to zero */
929 memset (&new_n_clone->stats_total, 0,
930 sizeof (new_n_clone->stats_total));
931 memset (&new_n_clone->stats_last_clear, 0,
932 sizeof (new_n_clone->stats_last_clear));
936 /* Copy stats if the old data is valid */
937 clib_memcpy (&new_n_clone->stats_total,
938 &old_n_clone->stats_total,
939 sizeof (new_n_clone->stats_total));
940 clib_memcpy (&new_n_clone->stats_last_clear,
941 &old_n_clone->stats_last_clear,
942 sizeof (new_n_clone->stats_last_clear));
944 /* keep previous node state */
945 new_n_clone->state = old_n_clone->state;
947 vec_add1 (nm_clone->nodes, new_n_clone);
949 /* Free the old node clone */
950 for (j = 0; j < vec_len (old_nodes_clone); j++)
951 clib_mem_free (old_nodes_clone[j]);
952 vec_free (old_nodes_clone);
954 vec_free (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
956 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
957 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
959 /* clone input node runtime */
960 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
962 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
963 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
965 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
967 rt->cpu_index = vm_clone->cpu_index;
970 for (j = 0; j < vec_len (old_rt); j++)
972 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
973 rt->state = old_rt[j].state;
978 nm_clone->processes = vec_dup (nm->processes);
980 clib_mem_set_heap (oldheap);
982 // vnet_main_fork_fixup (i);
986 static clib_error_t *
987 cpu_config (vlib_main_t * vm, unformat_input_t * input)
989 vlib_thread_registration_t *tr;
991 vlib_thread_main_t *tm = &vlib_thread_main;
997 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
998 tm->n_thread_stacks = 1; /* account for main thread */
1004 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1008 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1010 if (unformat (input, "use-pthreads"))
1011 tm->use_pthreads = 1;
1012 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1014 else if (unformat (input, "main-core %u", &tm->main_lcore))
1016 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1018 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1020 p = hash_get_mem (tm->thread_registrations_by_name, name);
1022 return clib_error_return (0, "no such thread type '%s'", name);
1024 tr = (vlib_thread_registration_t *) p[0];
1026 if (tr->use_pthreads)
1027 return clib_error_return (0,
1028 "coremask cannot be set for '%s' threads",
1031 tr->coremask = clib_bitmap_set_multiple
1032 (tr->coremask, 0, coremask, BITS (coremask));
1033 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1035 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1038 p = hash_get_mem (tm->thread_registrations_by_name, name);
1040 return clib_error_return (0, "no such thread type '%s'", name);
1042 tr = (vlib_thread_registration_t *) p[0];
1044 if (tr->use_pthreads)
1045 return clib_error_return (0,
1046 "corelist cannot be set for '%s' threads",
1049 tr->coremask = bitmap;
1050 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1052 else if (unformat (input, "%s %u", &name, &count))
1054 p = hash_get_mem (tm->thread_registrations_by_name, name);
1056 return clib_error_return (0, "no such thread type '%s'", name);
1058 tr = (vlib_thread_registration_t *) p[0];
1059 if (tr->fixed_count)
1060 return clib_error_return
1061 (0, "number of %s threads not configurable", tr->name);
1070 if (!tm->thread_prefix)
1071 tm->thread_prefix = format (0, "vpp");
1075 tm->n_thread_stacks += tr->count;
1076 tm->n_pthreads += tr->count * tr->use_pthreads;
1077 tm->n_eal_threads += tr->count * (tr->use_pthreads == 0);
1084 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1086 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1088 __sync_fetch_and_add_8 (void)
1090 fformat (stderr, "%s called\n", __FUNCTION__);
1095 __sync_add_and_fetch_8 (void)
1097 fformat (stderr, "%s called\n", __FUNCTION__);
1102 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1104 vnet_main_fixup (vlib_fork_fixup_t which)
1109 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1111 vlib_main_t *vm = vlib_get_main ();
1113 if (vlib_mains == 0)
1116 ASSERT (os_get_cpu_number () == 0);
1117 vlib_worker_thread_barrier_sync (vm);
1121 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1122 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1128 vlib_worker_thread_barrier_release (vm);
1132 vlib_worker_thread_barrier_sync (vlib_main_t * vm)
1140 count = vec_len (vlib_mains) - 1;
1142 /* Tolerate recursive calls */
1143 if (++vlib_worker_threads[0].recursion_level > 1)
1146 vlib_worker_threads[0].barrier_sync_count++;
1148 ASSERT (os_get_cpu_number () == 0);
1150 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1152 *vlib_worker_threads->wait_at_barrier = 1;
1153 while (*vlib_worker_threads->workers_at_barrier != count)
1155 if (vlib_time_now (vm) > deadline)
1157 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1164 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1171 if (--vlib_worker_threads[0].recursion_level > 0)
1174 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1176 *vlib_worker_threads->wait_at_barrier = 0;
1178 while (*vlib_worker_threads->workers_at_barrier > 0)
1180 if (vlib_time_now (vm) > deadline)
1182 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1189 * Check the frame queue to see if any frames are available.
1190 * If so, pull the packets off the frames and put them to
1194 vlib_frame_queue_dequeue_internal (vlib_main_t * vm)
1196 u32 thread_id = vm->cpu_index;
1197 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
1198 vlib_frame_queue_elt_t *elt;
1205 vlib_thread_main_t *tm = vlib_get_thread_main ();
1208 ASSERT (vm == vlib_mains[thread_id]);
1210 if (PREDICT_FALSE (tm->handoff_dispatch_node_index == ~0))
1213 * Gather trace data for frame queues
1215 if (PREDICT_FALSE (fq->trace))
1217 frame_queue_trace_t *fqt;
1218 frame_queue_nelt_counter_t *fqh;
1221 fqt = &tm->frame_queue_traces[thread_id];
1223 fqt->nelts = fq->nelts;
1224 fqt->head = fq->head;
1225 fqt->head_hint = fq->head_hint;
1226 fqt->tail = fq->tail;
1227 fqt->threshold = fq->vector_threshold;
1228 fqt->n_in_use = fqt->tail - fqt->head;
1229 if (fqt->n_in_use >= fqt->nelts)
1231 // if beyond max then use max
1232 fqt->n_in_use = fqt->nelts - 1;
1235 /* Record the number of elements in use in the histogram */
1236 fqh = &tm->frame_queue_histogram[thread_id];
1237 fqh->count[fqt->n_in_use]++;
1239 /* Record a snapshot of the elements in use */
1240 for (elix = 0; elix < fqt->nelts; elix++)
1242 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1243 if (1 || elt->valid)
1245 fqt->n_vectors[elix] = elt->n_vectors;
1253 if (fq->head == fq->tail)
1255 fq->head_hint = fq->head;
1259 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1263 fq->head_hint = fq->head;
1267 from = elt->buffer_index;
1268 msg_type = elt->msg_type;
1270 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1271 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1273 f = vlib_get_frame_to_node (vm, tm->handoff_dispatch_node_index);
1275 to = vlib_frame_vector_args (f);
1277 n_left_to_node = elt->n_vectors;
1279 while (n_left_to_node >= 4)
1287 n_left_to_node -= 4;
1290 while (n_left_to_node > 0)
1298 vectors += elt->n_vectors;
1299 f->n_vectors = elt->n_vectors;
1300 vlib_put_frame_to_node (vm, tm->handoff_dispatch_node_index, f);
1304 elt->msg_type = 0xfefefefe;
1305 CLIB_MEMORY_BARRIER ();
1310 * Limit the number of packets pushed into the graph
1312 if (vectors >= fq->vector_threshold)
1314 fq->head_hint = fq->head;
1322 static_always_inline void
1323 vlib_worker_thread_internal (vlib_main_t * vm)
1325 vlib_node_main_t *nm = &vm->node_main;
1326 u64 cpu_time_now = clib_cpu_time_now ();
1330 vlib_worker_thread_barrier_check ();
1332 vlib_frame_queue_dequeue_internal (vm);
1334 vlib_node_runtime_t *n;
1335 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1337 cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1338 VLIB_NODE_STATE_POLLING, /* frame */ 0,
1342 if (_vec_len (nm->pending_frames))
1345 cpu_time_now = clib_cpu_time_now ();
1346 for (i = 0; i < _vec_len (nm->pending_frames); i++)
1348 vlib_pending_frame_t *p;
1350 p = nm->pending_frames + i;
1352 cpu_time_now = dispatch_pending_node (vm, p, cpu_time_now);
1354 _vec_len (nm->pending_frames) = 0;
1356 vlib_increment_main_loop_counter (vm);
1358 /* Record time stamp in case there are no enabled nodes and above
1359 calls do not update time stamp. */
1360 cpu_time_now = clib_cpu_time_now ();
1365 vlib_worker_thread_fn (void *arg)
1367 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1368 vlib_thread_main_t *tm = vlib_get_thread_main ();
1369 vlib_main_t *vm = vlib_get_main ();
1371 ASSERT (vm->cpu_index == os_get_cpu_number ());
1373 vlib_worker_thread_init (w);
1374 clib_time_init (&vm->clib_time);
1375 clib_mem_set_heap (w->thread_mheap);
1377 /* Wait until the dpdk init sequence is complete */
1378 while (tm->worker_thread_release == 0)
1379 vlib_worker_thread_barrier_check ();
1381 vlib_worker_thread_internal (vm);
1385 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1388 .function = vlib_worker_thread_fn,
1393 threads_init (vlib_main_t * vm)
1395 vlib_thread_main_t *tm = vlib_get_thread_main ();
1397 tm->handoff_dispatch_node_index = ~0;
1402 VLIB_INIT_FUNCTION (threads_init);
1405 * fd.io coding-style-patch-verification: ON
1408 * eval: (c-set-style "gnu")