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();
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 */
80 vlib_set_thread_name (char *name)
82 int pthread_setname_np (pthread_t __target_thread, const char *__name);
83 pthread_t thread = pthread_self();
86 pthread_setname_np(thread, name);
89 static int sort_registrations_by_no_clone (void *a0, void * a1)
91 vlib_thread_registration_t ** tr0 = a0;
92 vlib_thread_registration_t ** tr1 = a1;
94 return ((i32)((*tr0)->no_data_structure_clone)
95 - ((i32)((*tr1)->no_data_structure_clone)));
99 vlib_sysfs_list_to_bitmap(char * filename)
104 fp = fopen (filename, "r");
109 vec_validate (buffer, 256-1);
110 if (fgets ((char *)buffer, 256, fp))
113 unformat_init_string (&in, (char *) buffer, strlen ((char *) buffer));
114 unformat(&in, "%U", unformat_bitmap_list, &r);
124 /* Called early in the init sequence */
127 vlib_thread_init (vlib_main_t * vm)
129 vlib_thread_main_t * tm = &vlib_thread_main;
130 vlib_worker_thread_t * w;
131 vlib_thread_registration_t * tr;
132 u32 n_vlib_mains = 1;
137 /* get bitmaps of active cpu cores and sockets */
138 tm->cpu_core_bitmap =
139 vlib_sysfs_list_to_bitmap("/sys/devices/system/cpu/online");
140 tm->cpu_socket_bitmap =
141 vlib_sysfs_list_to_bitmap("/sys/devices/system/node/online");
143 avail_cpu = clib_bitmap_dup(tm->cpu_core_bitmap);
146 for (i=0; i < tm->skip_cores; i++)
148 uword c = clib_bitmap_first_set(avail_cpu);
150 return clib_error_return (0, "no available cpus to skip");
152 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
155 /* grab cpu for main thread */
158 tm->main_lcore = clib_bitmap_first_set(avail_cpu);
159 if (tm->main_lcore == (u8) ~0)
160 return clib_error_return (0, "no available cpus to be used for the"
165 if (clib_bitmap_get(avail_cpu, tm->main_lcore) == 0)
166 return clib_error_return (0, "cpu %u is not available to be used"
167 " for the main thread", tm->main_lcore);
169 avail_cpu = clib_bitmap_set(avail_cpu, tm->main_lcore, 0);
171 /* assume that there is socket 0 only if there is no data from sysfs */
172 if (!tm->cpu_socket_bitmap)
173 tm->cpu_socket_bitmap = clib_bitmap_set(0, 0, 1);
175 /* pin main thread to main_lcore */
180 CPU_SET(tm->main_lcore, &cpuset);
181 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
185 /* as many threads as stacks... */
186 vec_validate_aligned (vlib_worker_threads, vec_len(vlib_thread_stacks)-1,
187 CLIB_CACHE_LINE_BYTES);
189 /* Preallocate thread 0 */
190 _vec_len(vlib_worker_threads) = 1;
191 w = vlib_worker_threads;
192 w->thread_mheap = clib_mem_get_heap();
193 w->thread_stack = vlib_thread_stacks[0];
194 w->dpdk_lcore_id = -1;
195 w->lwp = syscall(SYS_gettid);
196 tm->n_vlib_mains = 1;
198 /* assign threads to cores and set n_vlib_mains */
203 vec_add1 (tm->registrations, tr);
207 vec_sort_with_function
208 (tm->registrations, sort_registrations_by_no_clone);
210 for (i = 0; i < vec_len (tm->registrations); i++)
213 tr = tm->registrations[i];
214 tr->first_index = first_index;
215 first_index += tr->count;
216 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
218 /* construct coremask */
219 if (tr->use_pthreads || !tr->count)
225 clib_bitmap_foreach (c, tr->coremask, ({
226 if (clib_bitmap_get(avail_cpu, c) == 0)
227 return clib_error_return (0, "cpu %u is not available to be used"
228 " for the '%s' thread",c, tr->name);
230 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
236 for (j=0; j < tr->count; j++)
238 uword c = clib_bitmap_first_set(avail_cpu);
240 return clib_error_return (0, "no available cpus to be used for"
241 " the '%s' thread", tr->name);
243 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
244 tr->coremask = clib_bitmap_set(tr->coremask, c, 1);
249 clib_bitmap_free(avail_cpu);
251 tm->n_vlib_mains = n_vlib_mains;
253 vec_validate_aligned (vlib_worker_threads, first_index-1,
254 CLIB_CACHE_LINE_BYTES);
257 tm->efd.enabled = VLIB_EFD_DISABLED;
258 tm->efd.queue_hi_thresh = ((VLIB_EFD_DEF_WORKER_HI_THRESH_PCT *
259 FRAME_QUEUE_NELTS)/100);
263 vlib_worker_thread_t *
264 vlib_alloc_thread (vlib_main_t * vm)
266 vlib_worker_thread_t * w;
268 if (vec_len(vlib_worker_threads) >= vec_len (vlib_thread_stacks))
270 clib_warning ("out of worker threads... Quitting...");
273 vec_add2 (vlib_worker_threads, w, 1);
274 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
278 vlib_frame_queue_t * vlib_frame_queue_alloc (int nelts)
280 vlib_frame_queue_t * fq;
282 fq = clib_mem_alloc_aligned(sizeof (*fq), CLIB_CACHE_LINE_BYTES);
283 memset (fq, 0, sizeof (*fq));
285 fq->vector_threshold = 128; // packets
286 vec_validate_aligned (fq->elts, nelts-1, CLIB_CACHE_LINE_BYTES);
290 if (((uword)&fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
291 fformat(stderr, "WARNING: fq->tail unaligned\n");
292 if (((uword)&fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
293 fformat(stderr, "WARNING: fq->head unaligned\n");
294 if (((uword)fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
295 fformat(stderr, "WARNING: fq->elts unaligned\n");
297 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
298 fformat(stderr, "WARNING: fq->elts[0] size %d\n",
299 sizeof (fq->elts[0]));
300 if (nelts & (nelts -1))
302 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
310 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
311 void vl_msg_api_handler_no_free (void *v) { }
313 /* Turned off, save as reference material... */
315 static inline int vlib_frame_queue_dequeue_internal (int thread_id,
317 vlib_node_main_t *nm)
319 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
320 vlib_frame_queue_elt_t *elt;
322 vlib_pending_frame_t *p;
323 vlib_node_runtime_t *r;
324 u32 node_runtime_index;
329 ASSERT(vm == vlib_mains[thread_id]);
333 if (fq->head == fq->tail)
336 elt = fq->elts + ((fq->head+1) & (fq->nelts-1));
341 before = clib_cpu_time_now();
344 node_runtime_index = elt->node_runtime_index;
345 msg_type = elt->msg_type;
349 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
350 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
351 /* note fallthrough... */
352 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
353 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
355 vlib_frame_free (vm, r, f);
357 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
358 vec_add2 (vm->node_main.pending_frames, p, 1);
359 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
360 p->node_runtime_index = elt->node_runtime_index;
361 p->frame_index = vlib_frame_index (vm, f);
362 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
363 fq->dequeue_vectors += (u64) f->n_vectors;
365 case VLIB_FRAME_QUEUE_ELT_API_MSG:
366 vl_msg_api_handler_no_free (f);
369 clib_warning ("bogus frame queue message, type %d", msg_type);
374 fq->dequeue_ticks += clib_cpu_time_now() - before;
375 CLIB_MEMORY_BARRIER();
383 int vlib_frame_queue_dequeue (int thread_id,
385 vlib_node_main_t *nm)
387 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
390 int vlib_frame_queue_enqueue (vlib_main_t *vm, u32 node_runtime_index,
391 u32 frame_queue_index, vlib_frame_t *frame,
392 vlib_frame_queue_msg_type_t type)
394 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
395 vlib_frame_queue_elt_t *elt;
398 u64 before = clib_cpu_time_now();
402 new_tail = __sync_add_and_fetch (&fq->tail, 1);
404 /* Wait until a ring slot is available */
405 while (new_tail >= fq->head + fq->nelts)
407 f64 b4 = vlib_time_now_ticks (vm, before);
408 vlib_worker_thread_barrier_check (vm, b4);
409 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
410 // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
413 elt = fq->elts + (new_tail & (fq->nelts-1));
415 /* this would be very bad... */
420 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
421 save_count = frame->n_vectors;
424 elt->node_runtime_index = node_runtime_index;
425 elt->msg_type = type;
426 CLIB_MEMORY_BARRIER();
433 /* To be called by vlib worker threads upon startup */
434 void vlib_worker_thread_init (vlib_worker_thread_t * w)
436 vlib_thread_main_t *tm = vlib_get_thread_main();
438 /* worker threads wants no signals. */
442 pthread_sigmask (SIG_SETMASK, &s, 0);
445 clib_mem_set_heap (w->thread_mheap);
447 if (vec_len(tm->thread_prefix) && w->registration->short_name)
449 w->name = format(0, "%v_%s_%d%c", tm->thread_prefix,
450 w->registration->short_name,
453 vlib_set_thread_name((char *)w->name);
456 if (!w->registration->use_pthreads)
459 /* Initial barrier sync, for both worker and i/o threads */
460 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
462 while (*vlib_worker_threads->wait_at_barrier)
465 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
469 void *vlib_worker_thread_bootstrap_fn (void *arg)
472 vlib_worker_thread_t *w = arg;
474 w->lwp = syscall(SYS_gettid);
475 w->dpdk_lcore_id = -1;
477 if (w->registration && !w->registration->use_pthreads &&
478 rte_socket_id) /* do we really have dpdk linked */
480 unsigned lcore = rte_lcore_id();
481 lcore = lcore < RTE_MAX_LCORE ? lcore : -1;
482 w->dpdk_lcore_id = lcore;
486 rv = (void *) clib_calljmp
487 ((uword (*)(uword)) w->thread_function,
488 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
489 /* NOTREACHED, we hope */
494 vlib_launch_thread (void *fp, vlib_worker_thread_t *w, unsigned lcore_id)
496 void *(*fp_arg)(void *) = fp;
499 if (!w->registration->use_pthreads)
500 if (rte_eal_remote_launch) /* do we have dpdk linked */
501 return rte_eal_remote_launch (fp, (void *)w, lcore_id);
511 CPU_SET(lcore_id, &cpuset);
513 ret = pthread_create (&worker, NULL /* attr */, fp_arg, (void *)w);
515 return pthread_setaffinity_np(worker, sizeof(cpu_set_t), &cpuset);
521 static clib_error_t * start_workers (vlib_main_t * vm)
524 vlib_worker_thread_t *w;
525 vlib_main_t *vm_clone;
527 vlib_frame_queue_t *fq;
528 vlib_thread_main_t * tm = &vlib_thread_main;
529 vlib_thread_registration_t * tr;
530 vlib_node_runtime_t * rt;
531 u32 n_vlib_mains = tm->n_vlib_mains;
532 u32 worker_thread_index;
533 u8 * main_heap = clib_mem_get_per_cpu_heap();
534 mheap_t * main_heap_header = mheap_header (main_heap);
536 vec_reset_length (vlib_worker_threads);
538 /* Set up the main thread */
539 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
540 w->elog_track.name = "main thread";
541 elog_track_register (&vm->elog_main, &w->elog_track);
543 if (vec_len(tm->thread_prefix))
545 w->name = format(0, "%v_main%c", tm->thread_prefix, '\0');
546 vlib_set_thread_name((char *)w->name);
550 w->dpdk_lcore_id = -1;
551 if (rte_socket_id) /* do we really have dpdk linked */
553 unsigned lcore = rte_lcore_id();
554 w->dpdk_lcore_id = lcore < RTE_MAX_LCORE ? lcore : -1;;
559 * Truth of the matter: we always use at least two
560 * threads. So, make the main heap thread-safe
561 * and make the event log thread-safe.
563 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
565 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
566 CLIB_CACHE_LINE_BYTES);
567 vm->elog_main.lock[0] = 0;
569 if (n_vlib_mains > 1)
571 vec_validate (vlib_mains, tm->n_vlib_mains - 1);
572 _vec_len (vlib_mains) = 0;
573 vec_add1 (vlib_mains, vm);
575 vec_validate (vlib_frame_queues, tm->n_vlib_mains - 1);
576 _vec_len (vlib_frame_queues) = 0;
577 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
578 vec_add1 (vlib_frame_queues, fq);
580 vlib_worker_threads->wait_at_barrier =
581 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
582 vlib_worker_threads->workers_at_barrier =
583 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
585 /* Ask for an initial barrier sync */
586 *vlib_worker_threads->workers_at_barrier = 0;
587 *vlib_worker_threads->wait_at_barrier = 1;
589 worker_thread_index = 1;
591 for (i = 0; i < vec_len(tm->registrations); i++)
593 vlib_node_main_t *nm, *nm_clone;
594 vlib_buffer_main_t *bm_clone;
595 vlib_buffer_free_list_t *fl_clone, *fl_orig;
596 vlib_buffer_free_list_t *orig_freelist_pool;
599 tr = tm->registrations[i];
604 for (k = 0; k < tr->count; k++)
606 vec_add2 (vlib_worker_threads, w, 1);
608 w->thread_mheap = mheap_alloc (0 /* use VM */, tr->mheap_size);
610 w->thread_mheap = main_heap;
611 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
612 w->thread_function = tr->function;
613 w->thread_function_arg = w;
615 w->registration = tr;
618 (char *) format (0, "%s %d", tr->name, k+1);
619 vec_add1 (w->elog_track.name, 0);
620 elog_track_register (&vm->elog_main, &w->elog_track);
622 if (tr->no_data_structure_clone)
625 /* Allocate "to-worker-N" frame queue */
626 if (tr->frame_queue_nelts) {
627 fq = vlib_frame_queue_alloc (tr->frame_queue_nelts);
629 fq = vlib_frame_queue_alloc (FRAME_QUEUE_NELTS);
632 vec_validate (vlib_frame_queues, worker_thread_index);
633 vlib_frame_queues[worker_thread_index] = fq;
635 /* Fork vlib_global_main et al. Look for bugs here */
636 oldheap = clib_mem_set_heap (w->thread_mheap);
638 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
639 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
641 vm_clone->cpu_index = worker_thread_index;
642 vm_clone->heap_base = w->thread_mheap;
643 vm_clone->mbuf_alloc_list = 0;
644 memset (&vm_clone->random_buffer, 0, sizeof (vm_clone->random_buffer));
646 nm = &vlib_mains[0]->node_main;
647 nm_clone = &vm_clone->node_main;
648 /* fork next frames array, preserving node runtime indices */
649 nm_clone->next_frames = vec_dup (nm->next_frames);
650 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
652 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
653 u32 save_node_runtime_index;
656 save_node_runtime_index = nf->node_runtime_index;
657 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
658 vlib_next_frame_init (nf);
659 nf->node_runtime_index = save_node_runtime_index;
660 nf->flags = save_flags;
663 /* fork the frame dispatch queue */
664 nm_clone->pending_frames = 0;
665 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
666 _vec_len (nm_clone->pending_frames) = 0;
670 for (j = 0; j < vec_len (nm->nodes); j++)
673 n = clib_mem_alloc_no_fail (sizeof(*n));
674 clib_memcpy (n, nm->nodes[j], sizeof (*n));
675 /* none of the copied nodes have enqueue rights given out */
676 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
677 memset (&n->stats_total, 0, sizeof (n->stats_total));
678 memset (&n->stats_last_clear, 0, sizeof (n->stats_last_clear));
679 vec_add1 (nm_clone->nodes, n);
681 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
682 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
684 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
685 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
686 vec_foreach(rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
687 rt->cpu_index = vm_clone->cpu_index;
689 nm_clone->processes = vec_dup (nm->processes);
691 /* zap the (per worker) frame freelists, etc */
692 nm_clone->frame_sizes = 0;
693 nm_clone->frame_size_hash = 0;
695 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
697 clib_mem_set_heap (oldheap);
698 vec_add1 (vlib_mains, vm_clone);
700 vm_clone->error_main.counters =
701 vec_dup(vlib_mains[0]->error_main.counters);
702 vm_clone->error_main.counters_last_clear =
703 vec_dup(vlib_mains[0]->error_main.counters_last_clear);
705 /* Fork the vlib_buffer_main_t free lists, etc. */
706 bm_clone = vec_dup (vm_clone->buffer_main);
707 vm_clone->buffer_main = bm_clone;
709 orig_freelist_pool = bm_clone->buffer_free_list_pool;
710 bm_clone->buffer_free_list_pool = 0;
712 pool_foreach (fl_orig, orig_freelist_pool,
714 pool_get_aligned (bm_clone->buffer_free_list_pool,
715 fl_clone, CLIB_CACHE_LINE_BYTES);
716 ASSERT (fl_orig - orig_freelist_pool
717 == fl_clone - bm_clone->buffer_free_list_pool);
719 fl_clone[0] = fl_orig[0];
720 fl_clone->aligned_buffers = 0;
721 fl_clone->unaligned_buffers = 0;
722 fl_clone->n_alloc = 0;
725 worker_thread_index++;
731 /* only have non-data-structure copy threads to create... */
732 for (i = 0; i < vec_len(tm->registrations); i++)
734 tr = tm->registrations[i];
736 for (j = 0; j < tr->count; j++)
738 vec_add2 (vlib_worker_threads, w, 1);
740 w->thread_mheap = mheap_alloc (0 /* use VM */, tr->mheap_size);
742 w->thread_mheap = main_heap;
743 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
744 w->thread_function = tr->function;
745 w->thread_function_arg = w;
748 (char *) format (0, "%s %d", tr->name, j+1);
749 w->registration = tr;
750 vec_add1 (w->elog_track.name, 0);
751 elog_track_register (&vm->elog_main, &w->elog_track);
756 worker_thread_index = 1;
758 for (i = 0; i < vec_len (tm->registrations); i++)
762 tr = tm->registrations[i];
764 if (tr->use_pthreads || tm->use_pthreads)
766 for (j = 0; j < tr->count; j++)
768 w = vlib_worker_threads + worker_thread_index++;
769 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, 0) < 0)
770 clib_warning ("Couldn't start '%s' pthread ", tr->name);
776 clib_bitmap_foreach (c, tr->coremask, ({
777 w = vlib_worker_threads + worker_thread_index++;
778 if (vlib_launch_thread (vlib_worker_thread_bootstrap_fn, w, c) < 0)
779 clib_warning ("Couldn't start DPDK lcore %d", c);
784 vlib_worker_thread_barrier_sync(vm);
785 vlib_worker_thread_barrier_release(vm);
789 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
791 void vlib_worker_thread_node_runtime_update(void)
794 vlib_worker_thread_t *w;
796 vlib_node_main_t *nm, *nm_clone;
797 vlib_node_t ** old_nodes_clone;
798 vlib_main_t *vm_clone;
799 vlib_node_runtime_t * rt, * old_rt;
802 vlib_node_runtime_sync_stats (vlib_main_t * vm,
803 vlib_node_runtime_t * r,
808 ASSERT (os_get_cpu_number() == 0);
810 if (vec_len (vlib_mains) == 0)
816 ASSERT (os_get_cpu_number() == 0);
817 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
820 * Scrape all runtime stats, so we don't lose node runtime(s) with
821 * pending counts, or throw away worker / io thread counts.
823 for (j = 0; j < vec_len (nm->nodes); j++)
827 vlib_node_sync_stats (vm, n);
830 for (i = 1; i < vec_len (vlib_mains); i++)
834 vm_clone = vlib_mains[i];
835 nm_clone = &vm_clone->node_main;
837 for (j = 0; j < vec_len (nm_clone->nodes); j++)
839 n = nm_clone->nodes[j];
841 rt = vlib_node_get_runtime (vm_clone, n->index);
842 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
846 for (i = 1; i < vec_len (vlib_mains); i++)
848 vlib_node_runtime_t * rt;
849 w = vlib_worker_threads + i;
850 oldheap = clib_mem_set_heap (w->thread_mheap);
852 vm_clone = vlib_mains[i];
854 /* Re-clone error heap */
855 u64 * old_counters = vm_clone->error_main.counters;
856 u64 * old_counters_all_clear = vm_clone->error_main.counters_last_clear;
857 clib_memcpy (&vm_clone->error_main, &vm->error_main, sizeof (vm->error_main));
858 j = vec_len(vm->error_main.counters) - 1;
859 vec_validate_aligned(old_counters, j, CLIB_CACHE_LINE_BYTES);
860 vec_validate_aligned(old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
861 vm_clone->error_main.counters = old_counters;
862 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
864 nm_clone = &vm_clone->node_main;
865 vec_free (nm_clone->next_frames);
866 nm_clone->next_frames = vec_dup (nm->next_frames);
868 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
870 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
871 u32 save_node_runtime_index;
874 save_node_runtime_index = nf->node_runtime_index;
875 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
876 vlib_next_frame_init (nf);
877 nf->node_runtime_index = save_node_runtime_index;
878 nf->flags = save_flags;
881 old_nodes_clone = nm_clone->nodes;
885 for (j = 0; j < vec_len (nm->nodes); j++) {
886 vlib_node_t *old_n_clone;
887 vlib_node_t *new_n, *new_n_clone;
889 new_n = nm->nodes[j];
890 old_n_clone = old_nodes_clone[j];
892 new_n_clone = clib_mem_alloc_no_fail (sizeof(*new_n_clone));
893 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
894 /* none of the copied nodes have enqueue rights given out */
895 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
897 if (j >= vec_len (old_nodes_clone))
899 /* new node, set to zero */
900 memset (&new_n_clone->stats_total, 0,
901 sizeof (new_n_clone->stats_total));
902 memset (&new_n_clone->stats_last_clear, 0,
903 sizeof (new_n_clone->stats_last_clear));
907 /* Copy stats if the old data is valid */
908 clib_memcpy (&new_n_clone->stats_total,
909 &old_n_clone->stats_total,
910 sizeof (new_n_clone->stats_total));
911 clib_memcpy (&new_n_clone->stats_last_clear,
912 &old_n_clone->stats_last_clear,
913 sizeof (new_n_clone->stats_last_clear));
915 /* keep previous node state */
916 new_n_clone->state = old_n_clone->state;
918 vec_add1 (nm_clone->nodes, new_n_clone);
920 /* Free the old node clone */
921 for (j = 0; j < vec_len(old_nodes_clone); j++)
922 clib_mem_free (old_nodes_clone[j]);
923 vec_free (old_nodes_clone);
925 vec_free (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
927 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
928 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
930 /* clone input node runtime */
931 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
933 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
934 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
936 vec_foreach(rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
938 rt->cpu_index = vm_clone->cpu_index;
941 for (j=0; j < vec_len(old_rt); j++)
943 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
944 rt->state = old_rt[j].state;
949 nm_clone->processes = vec_dup (nm->processes);
951 clib_mem_set_heap (oldheap);
953 // vnet_main_fork_fixup (i);
957 static clib_error_t *
958 cpu_config (vlib_main_t * vm, unformat_input_t * input)
960 vlib_thread_registration_t *tr;
962 vlib_thread_main_t * tm = &vlib_thread_main;
968 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
969 tm->n_thread_stacks = 1; /* account for main thread */
975 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword)tr);
979 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
981 if (unformat (input, "use-pthreads"))
982 tm->use_pthreads = 1;
983 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
985 else if (unformat (input, "main-core %u", &tm->main_lcore))
987 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
989 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
991 p = hash_get_mem (tm->thread_registrations_by_name, name);
993 return clib_error_return (0, "no such thread type '%s'", name);
995 tr = (vlib_thread_registration_t *)p[0];
997 if (tr->use_pthreads)
998 return clib_error_return (0, "coremask cannot be set for '%s' threads",
1001 tr->coremask = clib_bitmap_set_multiple
1002 (tr->coremask, 0, coremask, BITS(coremask));
1003 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1005 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1008 p = hash_get_mem (tm->thread_registrations_by_name, name);
1010 return clib_error_return (0, "no such thread type '%s'", name);
1012 tr = (vlib_thread_registration_t *)p[0];
1014 if (tr->use_pthreads)
1015 return clib_error_return (0, "corelist cannot be set for '%s' threads",
1018 tr->coremask = bitmap;
1019 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1021 else if (unformat (input, "%s %u", &name, &count))
1023 p = hash_get_mem (tm->thread_registrations_by_name, name);
1025 return clib_error_return (0, "no such thread type '%s'", name);
1027 tr = (vlib_thread_registration_t *)p[0];
1028 if (tr->fixed_count)
1029 return clib_error_return
1030 (0, "number of %s threads not configurable", tr->name);
1039 if (!tm->thread_prefix)
1040 tm->thread_prefix = format(0, "vpp");
1044 tm->n_thread_stacks += tr->count;
1045 tm->n_pthreads += tr->count * tr->use_pthreads;
1046 tm->n_eal_threads += tr->count * (tr->use_pthreads == 0);
1053 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1055 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1056 void __sync_fetch_and_add_8 (void)
1058 fformat(stderr, "%s called\n", __FUNCTION__);
1061 void __sync_add_and_fetch_8 (void)
1063 fformat(stderr, "%s called\n", __FUNCTION__);
1068 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1069 void vnet_main_fixup (vlib_fork_fixup_t which) { }
1071 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1073 vlib_main_t * vm = vlib_get_main();
1075 if (vlib_mains == 0)
1078 ASSERT(os_get_cpu_number() == 0);
1079 vlib_worker_thread_barrier_sync(vm);
1083 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1084 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1090 vlib_worker_thread_barrier_release(vm);
1093 void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
1101 count = vec_len (vlib_mains) - 1;
1103 /* Tolerate recursive calls */
1104 if (++vlib_worker_threads[0].recursion_level > 1)
1107 vlib_worker_threads[0].barrier_sync_count++;
1109 ASSERT (os_get_cpu_number() == 0);
1111 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1113 *vlib_worker_threads->wait_at_barrier = 1;
1114 while (*vlib_worker_threads->workers_at_barrier != count)
1116 if (vlib_time_now(vm) > deadline)
1118 fformat(stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1124 void vlib_worker_thread_barrier_release(vlib_main_t * vm)
1131 if (--vlib_worker_threads[0].recursion_level > 0)
1134 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1136 *vlib_worker_threads->wait_at_barrier = 0;
1138 while (*vlib_worker_threads->workers_at_barrier > 0)
1140 if (vlib_time_now(vm) > deadline)
1142 fformat(stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1149 * Check the frame queue to see if any frames are available.
1150 * If so, pull the packets off the frames and put them to
1153 static inline int vlib_frame_queue_dequeue_internal (vlib_main_t *vm)
1155 u32 thread_id = vm->cpu_index;
1156 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
1157 vlib_frame_queue_elt_t *elt;
1164 vlib_thread_main_t *tm = vlib_get_thread_main();
1167 ASSERT(vm == vlib_mains[thread_id]);
1169 if (PREDICT_FALSE (tm->handoff_dispatch_node_index == ~0))
1172 * Gather trace data for frame queues
1174 if (PREDICT_FALSE(fq->trace))
1176 frame_queue_trace_t *fqt;
1177 frame_queue_nelt_counter_t *fqh;
1180 fqt = &tm->frame_queue_traces[thread_id];
1182 fqt->nelts = fq->nelts;
1183 fqt->head = fq->head;
1184 fqt->head_hint = fq->head_hint;
1185 fqt->tail = fq->tail;
1186 fqt->threshold = fq->vector_threshold;
1187 fqt->n_in_use = fqt->tail - fqt->head;
1188 if (fqt->n_in_use >= fqt->nelts){
1189 // if beyond max then use max
1190 fqt->n_in_use = fqt->nelts-1;
1193 /* Record the number of elements in use in the histogram */
1194 fqh = &tm->frame_queue_histogram[thread_id];
1195 fqh->count[ fqt->n_in_use ]++;
1197 /* Record a snapshot of the elements in use */
1198 for (elix=0; elix<fqt->nelts; elix++) {
1199 elt = fq->elts + ((fq->head+1 + elix) & (fq->nelts-1));
1200 if (1 || elt->valid)
1202 fqt->n_vectors[elix] = elt->n_vectors;
1210 if (fq->head == fq->tail)
1212 fq->head_hint = fq->head;
1216 elt = fq->elts + ((fq->head+1) & (fq->nelts-1));
1220 fq->head_hint = fq->head;
1224 from = elt->buffer_index;
1225 msg_type = elt->msg_type;
1227 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1228 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1230 f = vlib_get_frame_to_node (vm, tm->handoff_dispatch_node_index);
1232 to = vlib_frame_vector_args (f);
1234 n_left_to_node = elt->n_vectors;
1236 while (n_left_to_node >= 4)
1244 n_left_to_node -= 4;
1247 while (n_left_to_node > 0)
1255 vectors += elt->n_vectors;
1256 f->n_vectors = elt->n_vectors;
1257 vlib_put_frame_to_node (vm, tm->handoff_dispatch_node_index, f);
1261 elt->msg_type = 0xfefefefe;
1262 CLIB_MEMORY_BARRIER();
1267 * Limit the number of packets pushed into the graph
1269 if (vectors >= fq->vector_threshold)
1271 fq->head_hint = fq->head;
1279 static_always_inline void
1280 vlib_worker_thread_internal (vlib_main_t *vm)
1282 vlib_node_main_t * nm = &vm->node_main;
1283 u64 cpu_time_now = clib_cpu_time_now ();
1287 vlib_worker_thread_barrier_check ();
1289 vlib_frame_queue_dequeue_internal (vm);
1291 vlib_node_runtime_t * n;
1292 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1294 cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1295 VLIB_NODE_STATE_POLLING, /* frame */ 0,
1299 if (_vec_len (nm->pending_frames))
1302 cpu_time_now = clib_cpu_time_now ();
1303 for (i = 0; i < _vec_len (nm->pending_frames); i++) {
1304 vlib_pending_frame_t *p;
1306 p = nm->pending_frames + i;
1308 cpu_time_now = dispatch_pending_node (vm, p, cpu_time_now);
1310 _vec_len (nm->pending_frames) = 0;
1312 vlib_increment_main_loop_counter (vm);
1314 /* Record time stamp in case there are no enabled nodes and above
1315 calls do not update time stamp. */
1316 cpu_time_now = clib_cpu_time_now ();
1320 void vlib_worker_thread_fn (void * arg)
1322 vlib_worker_thread_t * w = (vlib_worker_thread_t *) arg;
1323 vlib_thread_main_t * tm = vlib_get_thread_main();
1324 vlib_main_t * vm = vlib_get_main();
1326 ASSERT(vm->cpu_index == os_get_cpu_number());
1328 vlib_worker_thread_init (w);
1329 clib_time_init (&vm->clib_time);
1330 clib_mem_set_heap (w->thread_mheap);
1332 /* Wait until the dpdk init sequence is complete */
1333 while (tm->worker_thread_release == 0)
1334 vlib_worker_thread_barrier_check ();
1336 vlib_worker_thread_internal(vm);
1339 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1342 .function = vlib_worker_thread_fn,
1345 clib_error_t *threads_init (vlib_main_t *vm)
1347 vlib_thread_main_t * tm = vlib_get_thread_main();
1349 tm->handoff_dispatch_node_index = ~0;
1354 VLIB_INIT_FUNCTION (threads_init);