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->dpdk_lcore_id = -1;
215 w->lwp = syscall (SYS_gettid);
216 tm->n_vlib_mains = 1;
218 if (tm->sched_policy != ~0)
220 struct sched_param sched_param;
221 if (!sched_getparam (w->lwp, &sched_param))
223 if (tm->sched_priority != ~0)
224 sched_param.sched_priority = tm->sched_priority;
225 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
229 /* assign threads to cores and set n_vlib_mains */
234 vec_add1 (tm->registrations, tr);
238 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
240 for (i = 0; i < vec_len (tm->registrations); i++)
243 tr = tm->registrations[i];
244 tr->first_index = first_index;
245 first_index += tr->count;
246 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
248 /* construct coremask */
249 if (tr->use_pthreads || !tr->count)
256 clib_bitmap_foreach (c, tr->coremask, ({
257 if (clib_bitmap_get(avail_cpu, c) == 0)
258 return clib_error_return (0, "cpu %u is not available to be used"
259 " for the '%s' thread",c, tr->name);
261 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
268 for (j = 0; j < tr->count; j++)
270 uword c = clib_bitmap_first_set (avail_cpu);
272 return clib_error_return (0,
273 "no available cpus to be used for"
274 " the '%s' thread", tr->name);
276 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
277 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
282 clib_bitmap_free (avail_cpu);
284 tm->n_vlib_mains = n_vlib_mains;
286 vec_validate_aligned (vlib_worker_threads, first_index - 1,
287 CLIB_CACHE_LINE_BYTES);
290 tm->efd.enabled = VLIB_EFD_DISABLED;
291 tm->efd.queue_hi_thresh = ((VLIB_EFD_DEF_WORKER_HI_THRESH_PCT *
292 FRAME_QUEUE_NELTS) / 100);
296 vlib_worker_thread_t *
297 vlib_alloc_thread (vlib_main_t * vm)
299 vlib_worker_thread_t *w;
301 if (vec_len (vlib_worker_threads) >= vec_len (vlib_thread_stacks))
303 clib_warning ("out of worker threads... Quitting...");
306 vec_add2 (vlib_worker_threads, w, 1);
307 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
312 vlib_frame_queue_alloc (int nelts)
314 vlib_frame_queue_t *fq;
316 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
317 memset (fq, 0, sizeof (*fq));
319 fq->vector_threshold = 128; // packets
320 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
324 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
325 fformat (stderr, "WARNING: fq->tail unaligned\n");
326 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
327 fformat (stderr, "WARNING: fq->head unaligned\n");
328 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
329 fformat (stderr, "WARNING: fq->elts unaligned\n");
331 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
332 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
333 sizeof (fq->elts[0]));
334 if (nelts & (nelts - 1))
336 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
344 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
346 vl_msg_api_handler_no_free (void *v)
350 /* Turned off, save as reference material... */
353 vlib_frame_queue_dequeue_internal (int thread_id,
354 vlib_main_t * vm, vlib_node_main_t * nm)
356 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
357 vlib_frame_queue_elt_t *elt;
359 vlib_pending_frame_t *p;
360 vlib_node_runtime_t *r;
361 u32 node_runtime_index;
366 ASSERT (vm == vlib_mains[thread_id]);
370 if (fq->head == fq->tail)
373 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
378 before = clib_cpu_time_now ();
381 node_runtime_index = elt->node_runtime_index;
382 msg_type = elt->msg_type;
386 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
387 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
388 /* note fallthrough... */
389 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
390 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
392 vlib_frame_free (vm, r, f);
394 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
395 vec_add2 (vm->node_main.pending_frames, p, 1);
396 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
397 p->node_runtime_index = elt->node_runtime_index;
398 p->frame_index = vlib_frame_index (vm, f);
399 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
400 fq->dequeue_vectors += (u64) f->n_vectors;
402 case VLIB_FRAME_QUEUE_ELT_API_MSG:
403 vl_msg_api_handler_no_free (f);
406 clib_warning ("bogus frame queue message, type %d", msg_type);
411 fq->dequeue_ticks += clib_cpu_time_now () - before;
412 CLIB_MEMORY_BARRIER ();
421 vlib_frame_queue_dequeue (int thread_id,
422 vlib_main_t * vm, vlib_node_main_t * nm)
424 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
428 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
429 u32 frame_queue_index, vlib_frame_t * frame,
430 vlib_frame_queue_msg_type_t type)
432 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
433 vlib_frame_queue_elt_t *elt;
436 u64 before = clib_cpu_time_now ();
440 new_tail = __sync_add_and_fetch (&fq->tail, 1);
442 /* Wait until a ring slot is available */
443 while (new_tail >= fq->head + fq->nelts)
445 f64 b4 = vlib_time_now_ticks (vm, before);
446 vlib_worker_thread_barrier_check (vm, b4);
447 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
448 // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
451 elt = fq->elts + (new_tail & (fq->nelts - 1));
453 /* this would be very bad... */
458 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
459 save_count = frame->n_vectors;
462 elt->node_runtime_index = node_runtime_index;
463 elt->msg_type = type;
464 CLIB_MEMORY_BARRIER ();
471 /* To be called by vlib worker threads upon startup */
473 vlib_worker_thread_init (vlib_worker_thread_t * w)
475 vlib_thread_main_t *tm = vlib_get_thread_main ();
477 /* worker threads wants no signals. */
481 pthread_sigmask (SIG_SETMASK, &s, 0);
484 clib_mem_set_heap (w->thread_mheap);
486 if (vec_len (tm->thread_prefix) && w->registration->short_name)
488 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
489 w->registration->short_name, w->instance_id, '\0');
490 vlib_set_thread_name ((char *) w->name);
493 if (!w->registration->use_pthreads)
496 /* Initial barrier sync, for both worker and i/o threads */
497 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
499 while (*vlib_worker_threads->wait_at_barrier)
502 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
507 vlib_worker_thread_bootstrap_fn (void *arg)
510 vlib_worker_thread_t *w = arg;
512 w->lwp = syscall (SYS_gettid);
513 w->dpdk_lcore_id = -1;
515 if (w->registration && !w->registration->use_pthreads && rte_socket_id) /* do we really have dpdk linked */
517 unsigned lcore = rte_lcore_id ();
518 lcore = lcore < RTE_MAX_LCORE ? lcore : -1;
519 w->dpdk_lcore_id = lcore;
523 rv = (void *) clib_calljmp
524 ((uword (*)(uword)) w->thread_function,
525 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
526 /* NOTREACHED, we hope */
531 vlib_launch_thread (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
533 void *(*fp_arg) (void *) = fp;
536 if (!w->registration->use_pthreads)
537 if (rte_eal_remote_launch) /* do we have dpdk linked */
538 return rte_eal_remote_launch (fp, (void *) w, lcore_id);
548 CPU_SET (lcore_id, &cpuset);
550 ret = pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w);
552 return pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset);
558 static clib_error_t *
559 start_workers (vlib_main_t * vm)
562 vlib_worker_thread_t *w;
563 vlib_main_t *vm_clone;
565 vlib_frame_queue_t *fq;
566 vlib_thread_main_t *tm = &vlib_thread_main;
567 vlib_thread_registration_t *tr;
568 vlib_node_runtime_t *rt;
569 u32 n_vlib_mains = tm->n_vlib_mains;
570 u32 worker_thread_index;
571 u8 *main_heap = clib_mem_get_per_cpu_heap ();
572 mheap_t *main_heap_header = mheap_header (main_heap);
574 vec_reset_length (vlib_worker_threads);
576 /* Set up the main thread */
577 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
578 w->elog_track.name = "main thread";
579 elog_track_register (&vm->elog_main, &w->elog_track);
581 if (vec_len (tm->thread_prefix))
583 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
584 vlib_set_thread_name ((char *) w->name);
588 w->dpdk_lcore_id = -1;
589 if (rte_socket_id) /* do we really have dpdk linked */
591 unsigned lcore = rte_lcore_id ();
592 w->dpdk_lcore_id = lcore < RTE_MAX_LCORE ? lcore : -1;;
597 * Truth of the matter: we always use at least two
598 * threads. So, make the main heap thread-safe
599 * and make the event log thread-safe.
601 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
603 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
604 vm->elog_main.lock[0] = 0;
606 if (n_vlib_mains > 1)
608 vec_validate (vlib_mains, tm->n_vlib_mains - 1);
609 _vec_len (vlib_mains) = 0;
610 vec_add1 (vlib_mains, vm);
612 vec_validate (vlib_frame_queues, tm->n_vlib_mains - 1);
613 _vec_len (vlib_frame_queues) = 0;
614 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
615 vec_add1 (vlib_frame_queues, fq);
617 vlib_worker_threads->wait_at_barrier =
618 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
619 vlib_worker_threads->workers_at_barrier =
620 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
622 /* Ask for an initial barrier sync */
623 *vlib_worker_threads->workers_at_barrier = 0;
624 *vlib_worker_threads->wait_at_barrier = 1;
626 worker_thread_index = 1;
628 for (i = 0; i < vec_len (tm->registrations); i++)
630 vlib_node_main_t *nm, *nm_clone;
631 vlib_buffer_main_t *bm_clone;
632 vlib_buffer_free_list_t *fl_clone, *fl_orig;
633 vlib_buffer_free_list_t *orig_freelist_pool;
636 tr = tm->registrations[i];
641 for (k = 0; k < tr->count; k++)
643 vec_add2 (vlib_worker_threads, w, 1);
646 mheap_alloc (0 /* use VM */ , tr->mheap_size);
648 w->thread_mheap = main_heap;
649 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
650 w->thread_function = tr->function;
651 w->thread_function_arg = w;
653 w->registration = tr;
656 (char *) format (0, "%s %d", tr->name, k + 1);
657 vec_add1 (w->elog_track.name, 0);
658 elog_track_register (&vm->elog_main, &w->elog_track);
660 if (tr->no_data_structure_clone)
663 /* Allocate "to-worker-N" frame queue */
664 if (tr->frame_queue_nelts)
666 fq = vlib_frame_queue_alloc (tr->frame_queue_nelts);
670 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
673 vec_validate (vlib_frame_queues, worker_thread_index);
674 vlib_frame_queues[worker_thread_index] = fq;
676 /* Fork vlib_global_main et al. Look for bugs here */
677 oldheap = clib_mem_set_heap (w->thread_mheap);
679 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
680 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
682 vm_clone->cpu_index = worker_thread_index;
683 vm_clone->heap_base = w->thread_mheap;
684 vm_clone->mbuf_alloc_list = 0;
685 memset (&vm_clone->random_buffer, 0,
686 sizeof (vm_clone->random_buffer));
688 nm = &vlib_mains[0]->node_main;
689 nm_clone = &vm_clone->node_main;
690 /* fork next frames array, preserving node runtime indices */
691 nm_clone->next_frames = vec_dup (nm->next_frames);
692 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
694 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
695 u32 save_node_runtime_index;
698 save_node_runtime_index = nf->node_runtime_index;
699 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
700 vlib_next_frame_init (nf);
701 nf->node_runtime_index = save_node_runtime_index;
702 nf->flags = save_flags;
705 /* fork the frame dispatch queue */
706 nm_clone->pending_frames = 0;
707 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
708 _vec_len (nm_clone->pending_frames) = 0;
712 for (j = 0; j < vec_len (nm->nodes); j++)
715 n = clib_mem_alloc_no_fail (sizeof (*n));
716 clib_memcpy (n, nm->nodes[j], sizeof (*n));
717 /* none of the copied nodes have enqueue rights given out */
718 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
719 memset (&n->stats_total, 0, sizeof (n->stats_total));
720 memset (&n->stats_last_clear, 0,
721 sizeof (n->stats_last_clear));
722 vec_add1 (nm_clone->nodes, n);
724 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
725 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
727 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
728 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
729 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
730 rt->cpu_index = vm_clone->cpu_index;
732 nm_clone->processes = vec_dup (nm->processes);
734 /* zap the (per worker) frame freelists, etc */
735 nm_clone->frame_sizes = 0;
736 nm_clone->frame_size_hash = 0;
738 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
740 clib_mem_set_heap (oldheap);
741 vec_add1 (vlib_mains, vm_clone);
743 vm_clone->error_main.counters =
744 vec_dup (vlib_mains[0]->error_main.counters);
745 vm_clone->error_main.counters_last_clear =
746 vec_dup (vlib_mains[0]->error_main.counters_last_clear);
748 /* Fork the vlib_buffer_main_t free lists, etc. */
749 bm_clone = vec_dup (vm_clone->buffer_main);
750 vm_clone->buffer_main = bm_clone;
752 orig_freelist_pool = bm_clone->buffer_free_list_pool;
753 bm_clone->buffer_free_list_pool = 0;
756 pool_foreach (fl_orig, orig_freelist_pool,
758 pool_get_aligned (bm_clone->buffer_free_list_pool,
759 fl_clone, CLIB_CACHE_LINE_BYTES);
760 ASSERT (fl_orig - orig_freelist_pool
761 == fl_clone - bm_clone->buffer_free_list_pool);
763 fl_clone[0] = fl_orig[0];
764 fl_clone->aligned_buffers = 0;
765 fl_clone->unaligned_buffers = 0;
766 fl_clone->n_alloc = 0;
770 worker_thread_index++;
776 /* only have non-data-structure copy threads to create... */
777 for (i = 0; i < vec_len (tm->registrations); i++)
779 tr = tm->registrations[i];
781 for (j = 0; j < tr->count; j++)
783 vec_add2 (vlib_worker_threads, w, 1);
786 mheap_alloc (0 /* use VM */ , tr->mheap_size);
788 w->thread_mheap = main_heap;
789 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
790 w->thread_function = tr->function;
791 w->thread_function_arg = w;
794 (char *) format (0, "%s %d", tr->name, j + 1);
795 w->registration = tr;
796 vec_add1 (w->elog_track.name, 0);
797 elog_track_register (&vm->elog_main, &w->elog_track);
802 worker_thread_index = 1;
804 for (i = 0; i < vec_len (tm->registrations); i++)
808 tr = tm->registrations[i];
810 if (tr->use_pthreads || tm->use_pthreads)
812 for (j = 0; j < tr->count; j++)
814 w = vlib_worker_threads + worker_thread_index++;
815 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, 0) <
817 clib_warning ("Couldn't start '%s' pthread ", tr->name);
824 clib_bitmap_foreach (c, tr->coremask, ({
825 w = vlib_worker_threads + worker_thread_index++;
826 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, c) < 0)
827 clib_warning ("Couldn't start DPDK lcore %d", c);
833 vlib_worker_thread_barrier_sync (vm);
834 vlib_worker_thread_barrier_release (vm);
838 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
841 vlib_worker_thread_node_runtime_update (void)
844 vlib_worker_thread_t *w;
846 vlib_node_main_t *nm, *nm_clone;
847 vlib_node_t **old_nodes_clone;
848 vlib_main_t *vm_clone;
849 vlib_node_runtime_t *rt, *old_rt;
852 vlib_node_runtime_sync_stats (vlib_main_t * vm,
853 vlib_node_runtime_t * r,
855 uword n_vectors, uword n_clocks);
857 ASSERT (os_get_cpu_number () == 0);
859 if (vec_len (vlib_mains) == 0)
865 ASSERT (os_get_cpu_number () == 0);
866 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
869 * Scrape all runtime stats, so we don't lose node runtime(s) with
870 * pending counts, or throw away worker / io thread counts.
872 for (j = 0; j < vec_len (nm->nodes); j++)
876 vlib_node_sync_stats (vm, n);
879 for (i = 1; i < vec_len (vlib_mains); i++)
883 vm_clone = vlib_mains[i];
884 nm_clone = &vm_clone->node_main;
886 for (j = 0; j < vec_len (nm_clone->nodes); j++)
888 n = nm_clone->nodes[j];
890 rt = vlib_node_get_runtime (vm_clone, n->index);
891 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
895 for (i = 1; i < vec_len (vlib_mains); i++)
897 vlib_node_runtime_t *rt;
898 w = vlib_worker_threads + i;
899 oldheap = clib_mem_set_heap (w->thread_mheap);
901 vm_clone = vlib_mains[i];
903 /* Re-clone error heap */
904 u64 *old_counters = vm_clone->error_main.counters;
905 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
906 clib_memcpy (&vm_clone->error_main, &vm->error_main,
907 sizeof (vm->error_main));
908 j = vec_len (vm->error_main.counters) - 1;
909 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
910 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
911 vm_clone->error_main.counters = old_counters;
912 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
914 nm_clone = &vm_clone->node_main;
915 vec_free (nm_clone->next_frames);
916 nm_clone->next_frames = vec_dup (nm->next_frames);
918 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
920 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
921 u32 save_node_runtime_index;
924 save_node_runtime_index = nf->node_runtime_index;
925 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
926 vlib_next_frame_init (nf);
927 nf->node_runtime_index = save_node_runtime_index;
928 nf->flags = save_flags;
931 old_nodes_clone = nm_clone->nodes;
935 for (j = 0; j < vec_len (nm->nodes); j++)
937 vlib_node_t *old_n_clone;
938 vlib_node_t *new_n, *new_n_clone;
940 new_n = nm->nodes[j];
941 old_n_clone = old_nodes_clone[j];
943 new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
944 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
945 /* none of the copied nodes have enqueue rights given out */
946 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
948 if (j >= vec_len (old_nodes_clone))
950 /* new node, set to zero */
951 memset (&new_n_clone->stats_total, 0,
952 sizeof (new_n_clone->stats_total));
953 memset (&new_n_clone->stats_last_clear, 0,
954 sizeof (new_n_clone->stats_last_clear));
958 /* Copy stats if the old data is valid */
959 clib_memcpy (&new_n_clone->stats_total,
960 &old_n_clone->stats_total,
961 sizeof (new_n_clone->stats_total));
962 clib_memcpy (&new_n_clone->stats_last_clear,
963 &old_n_clone->stats_last_clear,
964 sizeof (new_n_clone->stats_last_clear));
966 /* keep previous node state */
967 new_n_clone->state = old_n_clone->state;
969 vec_add1 (nm_clone->nodes, new_n_clone);
971 /* Free the old node clone */
972 for (j = 0; j < vec_len (old_nodes_clone); j++)
973 clib_mem_free (old_nodes_clone[j]);
974 vec_free (old_nodes_clone);
976 vec_free (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
978 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
979 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
981 /* clone input node runtime */
982 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
984 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
985 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
987 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
989 rt->cpu_index = vm_clone->cpu_index;
992 for (j = 0; j < vec_len (old_rt); j++)
994 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
995 rt->state = old_rt[j].state;
1000 nm_clone->processes = vec_dup (nm->processes);
1002 clib_mem_set_heap (oldheap);
1004 // vnet_main_fork_fixup (i);
1009 unformat_sched_policy (unformat_input_t * input, va_list * args)
1011 u32 *r = va_arg (*args, u32 *);
1014 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1015 foreach_sched_policy
1022 static clib_error_t *
1023 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1025 vlib_thread_registration_t *tr;
1027 vlib_thread_main_t *tm = &vlib_thread_main;
1033 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1035 tm->n_thread_stacks = 1; /* account for main thread */
1036 tm->sched_policy = ~0;
1037 tm->sched_priority = ~0;
1043 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1047 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1049 if (unformat (input, "use-pthreads"))
1050 tm->use_pthreads = 1;
1051 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1053 else if (unformat (input, "main-core %u", &tm->main_lcore))
1055 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1057 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1059 p = hash_get_mem (tm->thread_registrations_by_name, name);
1061 return clib_error_return (0, "no such thread type '%s'", name);
1063 tr = (vlib_thread_registration_t *) p[0];
1065 if (tr->use_pthreads)
1066 return clib_error_return (0,
1067 "coremask cannot be set for '%s' threads",
1070 tr->coremask = clib_bitmap_set_multiple
1071 (tr->coremask, 0, coremask, BITS (coremask));
1072 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1074 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1077 p = hash_get_mem (tm->thread_registrations_by_name, name);
1079 return clib_error_return (0, "no such thread type '%s'", name);
1081 tr = (vlib_thread_registration_t *) p[0];
1083 if (tr->use_pthreads)
1084 return clib_error_return (0,
1085 "corelist cannot be set for '%s' threads",
1088 tr->coremask = bitmap;
1089 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1093 (input, "scheduler-policy %U", unformat_sched_policy,
1096 else if (unformat (input, "scheduler-prio %u", &tm->sched_priority))
1098 else if (unformat (input, "%s %u", &name, &count))
1100 p = hash_get_mem (tm->thread_registrations_by_name, name);
1102 return clib_error_return (0, "no such thread type 3 '%s'", name);
1104 tr = (vlib_thread_registration_t *) p[0];
1105 if (tr->fixed_count)
1106 return clib_error_return
1107 (0, "number of %s threads not configurable", tr->name);
1114 if (tm->sched_policy != ~0)
1116 if (tm->sched_priority != ~0
1117 && (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR))
1119 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1120 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1121 if (tm->sched_priority > prio_max)
1122 tm->sched_priority = prio_max;
1123 if (tm->sched_priority < prio_min)
1124 tm->sched_priority = prio_min;
1127 tm->sched_priority = 0;
1131 if (!tm->thread_prefix)
1132 tm->thread_prefix = format (0, "vpp");
1136 tm->n_thread_stacks += tr->count;
1137 tm->n_pthreads += tr->count * tr->use_pthreads;
1138 tm->n_eal_threads += tr->count * (tr->use_pthreads == 0);
1145 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1147 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1149 __sync_fetch_and_add_8 (void)
1151 fformat (stderr, "%s called\n", __FUNCTION__);
1156 __sync_add_and_fetch_8 (void)
1158 fformat (stderr, "%s called\n", __FUNCTION__);
1163 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1165 vnet_main_fixup (vlib_fork_fixup_t which)
1170 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1172 vlib_main_t *vm = vlib_get_main ();
1174 if (vlib_mains == 0)
1177 ASSERT (os_get_cpu_number () == 0);
1178 vlib_worker_thread_barrier_sync (vm);
1182 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1183 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1189 vlib_worker_thread_barrier_release (vm);
1193 vlib_worker_thread_barrier_sync (vlib_main_t * vm)
1201 count = vec_len (vlib_mains) - 1;
1203 /* Tolerate recursive calls */
1204 if (++vlib_worker_threads[0].recursion_level > 1)
1207 vlib_worker_threads[0].barrier_sync_count++;
1209 ASSERT (os_get_cpu_number () == 0);
1211 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1213 *vlib_worker_threads->wait_at_barrier = 1;
1214 while (*vlib_worker_threads->workers_at_barrier != count)
1216 if (vlib_time_now (vm) > deadline)
1218 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1225 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1232 if (--vlib_worker_threads[0].recursion_level > 0)
1235 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1237 *vlib_worker_threads->wait_at_barrier = 0;
1239 while (*vlib_worker_threads->workers_at_barrier > 0)
1241 if (vlib_time_now (vm) > deadline)
1243 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1250 * Check the frame queue to see if any frames are available.
1251 * If so, pull the packets off the frames and put them to
1255 vlib_frame_queue_dequeue_internal (vlib_main_t * vm)
1257 u32 thread_id = vm->cpu_index;
1258 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
1259 vlib_frame_queue_elt_t *elt;
1266 vlib_thread_main_t *tm = vlib_get_thread_main ();
1269 ASSERT (vm == vlib_mains[thread_id]);
1271 if (PREDICT_FALSE (tm->handoff_dispatch_node_index == ~0))
1274 * Gather trace data for frame queues
1276 if (PREDICT_FALSE (fq->trace))
1278 frame_queue_trace_t *fqt;
1279 frame_queue_nelt_counter_t *fqh;
1282 fqt = &tm->frame_queue_traces[thread_id];
1284 fqt->nelts = fq->nelts;
1285 fqt->head = fq->head;
1286 fqt->head_hint = fq->head_hint;
1287 fqt->tail = fq->tail;
1288 fqt->threshold = fq->vector_threshold;
1289 fqt->n_in_use = fqt->tail - fqt->head;
1290 if (fqt->n_in_use >= fqt->nelts)
1292 // if beyond max then use max
1293 fqt->n_in_use = fqt->nelts - 1;
1296 /* Record the number of elements in use in the histogram */
1297 fqh = &tm->frame_queue_histogram[thread_id];
1298 fqh->count[fqt->n_in_use]++;
1300 /* Record a snapshot of the elements in use */
1301 for (elix = 0; elix < fqt->nelts; elix++)
1303 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1304 if (1 || elt->valid)
1306 fqt->n_vectors[elix] = elt->n_vectors;
1314 if (fq->head == fq->tail)
1316 fq->head_hint = fq->head;
1320 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1324 fq->head_hint = fq->head;
1328 from = elt->buffer_index;
1329 msg_type = elt->msg_type;
1331 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1332 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1334 f = vlib_get_frame_to_node (vm, tm->handoff_dispatch_node_index);
1336 to = vlib_frame_vector_args (f);
1338 n_left_to_node = elt->n_vectors;
1340 while (n_left_to_node >= 4)
1348 n_left_to_node -= 4;
1351 while (n_left_to_node > 0)
1359 vectors += elt->n_vectors;
1360 f->n_vectors = elt->n_vectors;
1361 vlib_put_frame_to_node (vm, tm->handoff_dispatch_node_index, f);
1365 elt->msg_type = 0xfefefefe;
1366 CLIB_MEMORY_BARRIER ();
1371 * Limit the number of packets pushed into the graph
1373 if (vectors >= fq->vector_threshold)
1375 fq->head_hint = fq->head;
1383 static_always_inline void
1384 vlib_worker_thread_internal (vlib_main_t * vm)
1386 vlib_node_main_t *nm = &vm->node_main;
1387 u64 cpu_time_now = clib_cpu_time_now ();
1389 vec_alloc (nm->pending_interrupt_node_runtime_indices, 32);
1393 vlib_worker_thread_barrier_check ();
1395 vlib_frame_queue_dequeue_internal (vm);
1397 vlib_node_runtime_t *n;
1398 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1400 cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1401 VLIB_NODE_STATE_POLLING, /* frame */ 0,
1405 /* Next handle interrupts. */
1407 uword l = _vec_len (nm->pending_interrupt_node_runtime_indices);
1411 _vec_len (nm->pending_interrupt_node_runtime_indices) = 0;
1412 for (i = 0; i < l; i++)
1414 n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1416 pending_interrupt_node_runtime_indices
1419 dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1420 VLIB_NODE_STATE_INTERRUPT,
1427 if (_vec_len (nm->pending_frames))
1430 cpu_time_now = clib_cpu_time_now ();
1431 for (i = 0; i < _vec_len (nm->pending_frames); i++)
1433 vlib_pending_frame_t *p;
1435 p = nm->pending_frames + i;
1437 cpu_time_now = dispatch_pending_node (vm, p, cpu_time_now);
1439 _vec_len (nm->pending_frames) = 0;
1441 vlib_increment_main_loop_counter (vm);
1443 /* Record time stamp in case there are no enabled nodes and above
1444 calls do not update time stamp. */
1445 cpu_time_now = clib_cpu_time_now ();
1450 vlib_worker_thread_fn (void *arg)
1452 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1453 vlib_thread_main_t *tm = vlib_get_thread_main ();
1454 vlib_main_t *vm = vlib_get_main ();
1456 ASSERT (vm->cpu_index == os_get_cpu_number ());
1458 vlib_worker_thread_init (w);
1459 clib_time_init (&vm->clib_time);
1460 clib_mem_set_heap (w->thread_mheap);
1462 /* Wait until the dpdk init sequence is complete */
1463 while (tm->worker_thread_release == 0)
1464 vlib_worker_thread_barrier_check ();
1466 vlib_worker_thread_internal (vm);
1470 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1473 .function = vlib_worker_thread_fn,
1478 threads_init (vlib_main_t * vm)
1480 vlib_thread_main_t *tm = vlib_get_thread_main ();
1482 tm->handoff_dispatch_node_index = ~0;
1487 VLIB_INIT_FUNCTION (threads_init);
1490 * fd.io coding-style-patch-verification: ON
1493 * eval: (c-set-style "gnu")