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>
25 DECLARE_CJ_GLOBAL_LOG;
27 #define FRAME_QUEUE_NELTS 32
35 vlib_worker_thread_t *vlib_worker_threads;
36 vlib_thread_main_t vlib_thread_main;
39 os_get_cpu_number (void)
45 len = vec_len (vlib_thread_stacks);
49 /* Get any old stack address. */
52 n = ((uword) sp - (uword) vlib_thread_stacks[0])
53 >> VLIB_LOG2_THREAD_STACK_SIZE;
55 /* "processes" have their own stacks, and they always run in thread 0 */
66 len = vec_len (vlib_thread_stacks);
74 vlib_set_thread_name (char *name)
76 int pthread_setname_np (pthread_t __target_thread, const char *__name);
78 pthread_t thread = pthread_self ();
82 rv = pthread_setname_np (thread, name);
84 clib_warning ("pthread_setname_np returned %d", rv);
89 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,
114 strlen ((char *) buffer));
115 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
116 clib_warning ("unformat_bitmap_list failed");
126 /* Called early in the init sequence */
129 vlib_thread_init (vlib_main_t * vm)
131 vlib_thread_main_t *tm = &vlib_thread_main;
132 vlib_worker_thread_t *w;
133 vlib_thread_registration_t *tr;
134 u32 n_vlib_mains = 1;
139 /* get bitmaps of active cpu cores and sockets */
140 tm->cpu_core_bitmap =
141 vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
142 tm->cpu_socket_bitmap =
143 vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
145 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
148 for (i = 0; i < tm->skip_cores; i++)
150 uword c = clib_bitmap_first_set (avail_cpu);
152 return clib_error_return (0, "no available cpus to skip");
154 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
157 /* grab cpu for main thread */
160 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
161 if (tm->main_lcore == (u8) ~ 0)
162 return clib_error_return (0, "no available cpus to be used for the"
167 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
168 return clib_error_return (0, "cpu %u is not available to be used"
169 " for the main thread", tm->main_lcore);
171 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
173 /* assume that there is socket 0 only if there is no data from sysfs */
174 if (!tm->cpu_socket_bitmap)
175 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
177 /* pin main thread to main_lcore */
178 if (tm->cb.vlib_thread_set_lcore_cb)
180 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
186 CPU_SET (tm->main_lcore, &cpuset);
187 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
190 /* as many threads as stacks... */
191 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
192 CLIB_CACHE_LINE_BYTES);
194 /* Preallocate thread 0 */
195 _vec_len (vlib_worker_threads) = 1;
196 w = vlib_worker_threads;
197 w->thread_mheap = clib_mem_get_heap ();
198 w->thread_stack = vlib_thread_stacks[0];
199 w->lcore_id = tm->main_lcore;
200 w->lwp = syscall (SYS_gettid);
201 w->thread_id = pthread_self ();
202 tm->n_vlib_mains = 1;
204 if (tm->sched_policy != ~0)
206 struct sched_param sched_param;
207 if (!sched_getparam (w->lwp, &sched_param))
209 if (tm->sched_priority != ~0)
210 sched_param.sched_priority = tm->sched_priority;
211 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
215 /* assign threads to cores and set n_vlib_mains */
220 vec_add1 (tm->registrations, tr);
224 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
226 for (i = 0; i < vec_len (tm->registrations); i++)
229 tr = tm->registrations[i];
230 tr->first_index = first_index;
231 first_index += tr->count;
232 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
234 /* construct coremask */
235 if (tr->use_pthreads || !tr->count)
242 clib_bitmap_foreach (c, tr->coremask, ({
243 if (clib_bitmap_get(avail_cpu, c) == 0)
244 return clib_error_return (0, "cpu %u is not available to be used"
245 " for the '%s' thread",c, tr->name);
247 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
254 for (j = 0; j < tr->count; j++)
256 uword c = clib_bitmap_first_set (avail_cpu);
258 return clib_error_return (0,
259 "no available cpus to be used for"
260 " the '%s' thread", tr->name);
262 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
263 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
268 clib_bitmap_free (avail_cpu);
270 tm->n_vlib_mains = n_vlib_mains;
272 vec_validate_aligned (vlib_worker_threads, first_index - 1,
273 CLIB_CACHE_LINE_BYTES);
278 vlib_worker_thread_t *
279 vlib_alloc_thread (vlib_main_t * vm)
281 vlib_worker_thread_t *w;
283 if (vec_len (vlib_worker_threads) >= vec_len (vlib_thread_stacks))
285 clib_warning ("out of worker threads... Quitting...");
288 vec_add2 (vlib_worker_threads, w, 1);
289 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
294 vlib_frame_queue_alloc (int nelts)
296 vlib_frame_queue_t *fq;
298 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
299 memset (fq, 0, sizeof (*fq));
301 fq->vector_threshold = 128; // packets
302 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
306 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
307 fformat (stderr, "WARNING: fq->tail unaligned\n");
308 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
309 fformat (stderr, "WARNING: fq->head unaligned\n");
310 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
311 fformat (stderr, "WARNING: fq->elts unaligned\n");
313 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
314 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
315 sizeof (fq->elts[0]));
316 if (nelts & (nelts - 1))
318 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
326 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
328 vl_msg_api_handler_no_free (void *v)
332 /* Turned off, save as reference material... */
335 vlib_frame_queue_dequeue_internal (int thread_id,
336 vlib_main_t * vm, vlib_node_main_t * nm)
338 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
339 vlib_frame_queue_elt_t *elt;
341 vlib_pending_frame_t *p;
342 vlib_node_runtime_t *r;
343 u32 node_runtime_index;
348 ASSERT (vm == vlib_mains[thread_id]);
352 if (fq->head == fq->tail)
355 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
360 before = clib_cpu_time_now ();
363 node_runtime_index = elt->node_runtime_index;
364 msg_type = elt->msg_type;
368 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
369 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
370 /* note fallthrough... */
371 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
372 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
374 vlib_frame_free (vm, r, f);
376 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
377 vec_add2 (vm->node_main.pending_frames, p, 1);
378 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
379 p->node_runtime_index = elt->node_runtime_index;
380 p->frame_index = vlib_frame_index (vm, f);
381 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
382 fq->dequeue_vectors += (u64) f->n_vectors;
384 case VLIB_FRAME_QUEUE_ELT_API_MSG:
385 vl_msg_api_handler_no_free (f);
388 clib_warning ("bogus frame queue message, type %d", msg_type);
393 fq->dequeue_ticks += clib_cpu_time_now () - before;
394 CLIB_MEMORY_BARRIER ();
403 vlib_frame_queue_dequeue (int thread_id,
404 vlib_main_t * vm, vlib_node_main_t * nm)
406 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
410 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
411 u32 frame_queue_index, vlib_frame_t * frame,
412 vlib_frame_queue_msg_type_t type)
414 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
415 vlib_frame_queue_elt_t *elt;
418 u64 before = clib_cpu_time_now ();
422 new_tail = __sync_add_and_fetch (&fq->tail, 1);
424 /* Wait until a ring slot is available */
425 while (new_tail >= fq->head + fq->nelts)
427 f64 b4 = vlib_time_now_ticks (vm, before);
428 vlib_worker_thread_barrier_check (vm, b4);
429 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
430 // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
433 elt = fq->elts + (new_tail & (fq->nelts - 1));
435 /* this would be very bad... */
440 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
441 save_count = frame->n_vectors;
444 elt->node_runtime_index = node_runtime_index;
445 elt->msg_type = type;
446 CLIB_MEMORY_BARRIER ();
453 /* To be called by vlib worker threads upon startup */
455 vlib_worker_thread_init (vlib_worker_thread_t * w)
457 vlib_thread_main_t *tm = vlib_get_thread_main ();
460 * Note: disabling signals in worker threads as follows
461 * prevents the api post-mortem dump scheme from working
465 * pthread_sigmask (SIG_SETMASK, &s, 0);
469 clib_mem_set_heap (w->thread_mheap);
471 if (vec_len (tm->thread_prefix) && w->registration->short_name)
473 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
474 w->registration->short_name, w->instance_id, '\0');
475 vlib_set_thread_name ((char *) w->name);
478 if (!w->registration->use_pthreads)
481 /* Initial barrier sync, for both worker and i/o threads */
482 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
484 while (*vlib_worker_threads->wait_at_barrier)
487 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
492 vlib_worker_thread_bootstrap_fn (void *arg)
495 vlib_worker_thread_t *w = arg;
497 w->lwp = syscall (SYS_gettid);
498 w->thread_id = pthread_self ();
500 rv = (void *) clib_calljmp
501 ((uword (*)(uword)) w->thread_function,
502 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
503 /* NOTREACHED, we hope */
507 static clib_error_t *
508 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
510 vlib_thread_main_t *tm = &vlib_thread_main;
511 void *(*fp_arg) (void *) = fp;
513 w->lcore_id = lcore_id;
514 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
515 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
521 CPU_SET (lcore_id, &cpuset);
523 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
524 return clib_error_return_unix (0, "pthread_create");
526 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
527 return clib_error_return_unix (0, "pthread_setaffinity_np");
533 static clib_error_t *
534 start_workers (vlib_main_t * vm)
537 vlib_worker_thread_t *w;
538 vlib_main_t *vm_clone;
540 vlib_thread_main_t *tm = &vlib_thread_main;
541 vlib_thread_registration_t *tr;
542 vlib_node_runtime_t *rt;
543 u32 n_vlib_mains = tm->n_vlib_mains;
544 u32 worker_thread_index;
545 u8 *main_heap = clib_mem_get_per_cpu_heap ();
546 mheap_t *main_heap_header = mheap_header (main_heap);
548 vec_reset_length (vlib_worker_threads);
550 /* Set up the main thread */
551 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
552 w->elog_track.name = "main thread";
553 elog_track_register (&vm->elog_main, &w->elog_track);
555 if (vec_len (tm->thread_prefix))
557 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
558 vlib_set_thread_name ((char *) w->name);
562 * Truth of the matter: we always use at least two
563 * threads. So, make the main heap thread-safe
564 * and make the event log thread-safe.
566 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
568 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
569 vm->elog_main.lock[0] = 0;
571 if (n_vlib_mains > 1)
573 vec_validate (vlib_mains, tm->n_vlib_mains - 1);
574 _vec_len (vlib_mains) = 0;
575 vec_add1 (vlib_mains, vm);
577 vlib_worker_threads->wait_at_barrier =
578 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
579 vlib_worker_threads->workers_at_barrier =
580 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
582 /* Ask for an initial barrier sync */
583 *vlib_worker_threads->workers_at_barrier = 0;
584 *vlib_worker_threads->wait_at_barrier = 1;
586 worker_thread_index = 1;
588 for (i = 0; i < vec_len (tm->registrations); i++)
590 vlib_node_main_t *nm, *nm_clone;
591 vlib_buffer_main_t *bm_clone;
592 vlib_buffer_free_list_t *fl_clone, *fl_orig;
593 vlib_buffer_free_list_t *orig_freelist_pool;
596 tr = tm->registrations[i];
601 for (k = 0; k < tr->count; k++)
603 vec_add2 (vlib_worker_threads, w, 1);
606 mheap_alloc (0 /* use VM */ , tr->mheap_size);
608 w->thread_mheap = main_heap;
609 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
610 w->thread_function = tr->function;
611 w->thread_function_arg = w;
613 w->registration = tr;
616 (char *) format (0, "%s %d", tr->name, k + 1);
617 vec_add1 (w->elog_track.name, 0);
618 elog_track_register (&vm->elog_main, &w->elog_track);
620 if (tr->no_data_structure_clone)
623 /* Fork vlib_global_main et al. Look for bugs here */
624 oldheap = clib_mem_set_heap (w->thread_mheap);
626 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
627 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
629 vm_clone->cpu_index = worker_thread_index;
630 vm_clone->heap_base = w->thread_mheap;
631 vm_clone->mbuf_alloc_list = 0;
632 memset (&vm_clone->random_buffer, 0,
633 sizeof (vm_clone->random_buffer));
635 nm = &vlib_mains[0]->node_main;
636 nm_clone = &vm_clone->node_main;
637 /* fork next frames array, preserving node runtime indices */
638 nm_clone->next_frames = vec_dup (nm->next_frames);
639 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
641 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
642 u32 save_node_runtime_index;
645 save_node_runtime_index = nf->node_runtime_index;
646 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
647 vlib_next_frame_init (nf);
648 nf->node_runtime_index = save_node_runtime_index;
649 nf->flags = save_flags;
652 /* fork the frame dispatch queue */
653 nm_clone->pending_frames = 0;
654 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
655 _vec_len (nm_clone->pending_frames) = 0;
659 for (j = 0; j < vec_len (nm->nodes); j++)
662 n = clib_mem_alloc_no_fail (sizeof (*n));
663 clib_memcpy (n, nm->nodes[j], sizeof (*n));
664 /* none of the copied nodes have enqueue rights given out */
665 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
666 memset (&n->stats_total, 0, sizeof (n->stats_total));
667 memset (&n->stats_last_clear, 0,
668 sizeof (n->stats_last_clear));
669 vec_add1 (nm_clone->nodes, n);
671 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
672 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
674 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
675 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
676 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
677 rt->cpu_index = vm_clone->cpu_index;
679 nm_clone->processes = vec_dup (nm->processes);
681 /* zap the (per worker) frame freelists, etc */
682 nm_clone->frame_sizes = 0;
683 nm_clone->frame_size_hash = 0;
685 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
687 clib_mem_set_heap (oldheap);
688 vec_add1 (vlib_mains, vm_clone);
690 vm_clone->error_main.counters =
691 vec_dup (vlib_mains[0]->error_main.counters);
692 vm_clone->error_main.counters_last_clear =
693 vec_dup (vlib_mains[0]->error_main.counters_last_clear);
695 /* Fork the vlib_buffer_main_t free lists, etc. */
696 bm_clone = vec_dup (vm_clone->buffer_main);
697 vm_clone->buffer_main = bm_clone;
699 orig_freelist_pool = bm_clone->buffer_free_list_pool;
700 bm_clone->buffer_free_list_pool = 0;
703 pool_foreach (fl_orig, orig_freelist_pool,
705 pool_get_aligned (bm_clone->buffer_free_list_pool,
706 fl_clone, CLIB_CACHE_LINE_BYTES);
707 ASSERT (fl_orig - orig_freelist_pool
708 == fl_clone - bm_clone->buffer_free_list_pool);
710 fl_clone[0] = fl_orig[0];
711 fl_clone->aligned_buffers = 0;
712 fl_clone->unaligned_buffers = 0;
713 fl_clone->n_alloc = 0;
717 worker_thread_index++;
723 /* only have non-data-structure copy threads to create... */
724 for (i = 0; i < vec_len (tm->registrations); i++)
726 tr = tm->registrations[i];
728 for (j = 0; j < tr->count; j++)
730 vec_add2 (vlib_worker_threads, w, 1);
733 mheap_alloc (0 /* use VM */ , tr->mheap_size);
735 w->thread_mheap = main_heap;
736 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
737 w->thread_function = tr->function;
738 w->thread_function_arg = w;
741 (char *) format (0, "%s %d", tr->name, j + 1);
742 w->registration = tr;
743 vec_add1 (w->elog_track.name, 0);
744 elog_track_register (&vm->elog_main, &w->elog_track);
749 worker_thread_index = 1;
751 for (i = 0; i < vec_len (tm->registrations); i++)
756 tr = tm->registrations[i];
758 if (tr->use_pthreads || tm->use_pthreads)
760 for (j = 0; j < tr->count; j++)
762 w = vlib_worker_threads + worker_thread_index++;
763 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
766 clib_error_report (err);
773 clib_bitmap_foreach (c, tr->coremask, ({
774 w = vlib_worker_threads + worker_thread_index++;
775 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
778 clib_error_report (err);
783 vlib_worker_thread_barrier_sync (vm);
784 vlib_worker_thread_barrier_release (vm);
788 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
791 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,
805 uword n_vectors, uword n_clocks);
807 ASSERT (os_get_cpu_number () == 0);
809 if (vec_len (vlib_mains) == 0)
815 ASSERT (os_get_cpu_number () == 0);
816 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
819 * Scrape all runtime stats, so we don't lose node runtime(s) with
820 * pending counts, or throw away worker / io thread counts.
822 for (j = 0; j < vec_len (nm->nodes); j++)
826 vlib_node_sync_stats (vm, n);
829 for (i = 1; i < vec_len (vlib_mains); i++)
833 vm_clone = vlib_mains[i];
834 nm_clone = &vm_clone->node_main;
836 for (j = 0; j < vec_len (nm_clone->nodes); j++)
838 n = nm_clone->nodes[j];
840 rt = vlib_node_get_runtime (vm_clone, n->index);
841 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
845 for (i = 1; i < vec_len (vlib_mains); i++)
847 vlib_node_runtime_t *rt;
848 w = vlib_worker_threads + i;
849 oldheap = clib_mem_set_heap (w->thread_mheap);
851 vm_clone = vlib_mains[i];
853 /* Re-clone error heap */
854 u64 *old_counters = vm_clone->error_main.counters;
855 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
856 clib_memcpy (&vm_clone->error_main, &vm->error_main,
857 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++)
887 vlib_node_t *old_n_clone;
888 vlib_node_t *new_n, *new_n_clone;
890 new_n = nm->nodes[j];
891 old_n_clone = old_nodes_clone[j];
893 new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
894 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
895 /* none of the copied nodes have enqueue rights given out */
896 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
898 if (j >= vec_len (old_nodes_clone))
900 /* new node, set to zero */
901 memset (&new_n_clone->stats_total, 0,
902 sizeof (new_n_clone->stats_total));
903 memset (&new_n_clone->stats_last_clear, 0,
904 sizeof (new_n_clone->stats_last_clear));
908 /* Copy stats if the old data is valid */
909 clib_memcpy (&new_n_clone->stats_total,
910 &old_n_clone->stats_total,
911 sizeof (new_n_clone->stats_total));
912 clib_memcpy (&new_n_clone->stats_last_clear,
913 &old_n_clone->stats_last_clear,
914 sizeof (new_n_clone->stats_last_clear));
916 /* keep previous node state */
917 new_n_clone->state = old_n_clone->state;
919 vec_add1 (nm_clone->nodes, new_n_clone);
921 /* Free the old node clone */
922 for (j = 0; j < vec_len (old_nodes_clone); j++)
923 clib_mem_free (old_nodes_clone[j]);
924 vec_free (old_nodes_clone);
926 vec_free (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
928 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
929 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
931 /* clone input node runtime */
932 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
934 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
935 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
937 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
939 rt->cpu_index = vm_clone->cpu_index;
942 for (j = 0; j < vec_len (old_rt); j++)
944 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
945 rt->state = old_rt[j].state;
950 nm_clone->processes = vec_dup (nm->processes);
952 clib_mem_set_heap (oldheap);
954 // vnet_main_fork_fixup (i);
959 unformat_sched_policy (unformat_input_t * input, va_list * args)
961 u32 *r = va_arg (*args, u32 *);
964 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
972 static clib_error_t *
973 cpu_config (vlib_main_t * vm, unformat_input_t * input)
975 vlib_thread_registration_t *tr;
977 vlib_thread_main_t *tm = &vlib_thread_main;
983 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
985 tm->n_thread_stacks = 1; /* account for main thread */
986 tm->sched_policy = ~0;
987 tm->sched_priority = ~0;
993 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
997 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
999 if (unformat (input, "use-pthreads"))
1000 tm->use_pthreads = 1;
1001 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1003 else if (unformat (input, "main-core %u", &tm->main_lcore))
1005 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1007 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1009 p = hash_get_mem (tm->thread_registrations_by_name, name);
1011 return clib_error_return (0, "no such thread type '%s'", name);
1013 tr = (vlib_thread_registration_t *) p[0];
1015 if (tr->use_pthreads)
1016 return clib_error_return (0,
1017 "coremask cannot be set for '%s' threads",
1020 tr->coremask = clib_bitmap_set_multiple
1021 (tr->coremask, 0, coremask, BITS (coremask));
1022 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1024 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1027 p = hash_get_mem (tm->thread_registrations_by_name, name);
1029 return clib_error_return (0, "no such thread type '%s'", name);
1031 tr = (vlib_thread_registration_t *) p[0];
1033 if (tr->use_pthreads)
1034 return clib_error_return (0,
1035 "corelist cannot be set for '%s' threads",
1038 tr->coremask = bitmap;
1039 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1043 (input, "scheduler-policy %U", unformat_sched_policy,
1046 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1048 else if (unformat (input, "%s %u", &name, &count))
1050 p = hash_get_mem (tm->thread_registrations_by_name, name);
1052 return clib_error_return (0, "no such thread type 3 '%s'", name);
1054 tr = (vlib_thread_registration_t *) p[0];
1055 if (tr->fixed_count)
1056 return clib_error_return
1057 (0, "number of %s threads not configurable", tr->name);
1064 if (tm->sched_priority != ~0)
1066 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1068 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1069 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1070 if (tm->sched_priority > prio_max)
1071 tm->sched_priority = prio_max;
1072 if (tm->sched_priority < prio_min)
1073 tm->sched_priority = prio_min;
1077 return clib_error_return
1079 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1080 tm->sched_priority);
1085 if (!tm->thread_prefix)
1086 tm->thread_prefix = format (0, "vpp");
1090 tm->n_thread_stacks += tr->count;
1091 tm->n_pthreads += tr->count * tr->use_pthreads;
1092 tm->n_threads += tr->count * (tr->use_pthreads == 0);
1099 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1101 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1103 __sync_fetch_and_add_8 (void)
1105 fformat (stderr, "%s called\n", __FUNCTION__);
1110 __sync_add_and_fetch_8 (void)
1112 fformat (stderr, "%s called\n", __FUNCTION__);
1117 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1119 vnet_main_fixup (vlib_fork_fixup_t which)
1124 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1126 vlib_main_t *vm = vlib_get_main ();
1128 if (vlib_mains == 0)
1131 ASSERT (os_get_cpu_number () == 0);
1132 vlib_worker_thread_barrier_sync (vm);
1136 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1137 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1143 vlib_worker_thread_barrier_release (vm);
1147 vlib_worker_thread_barrier_sync (vlib_main_t * vm)
1155 count = vec_len (vlib_mains) - 1;
1157 /* Tolerate recursive calls */
1158 if (++vlib_worker_threads[0].recursion_level > 1)
1161 vlib_worker_threads[0].barrier_sync_count++;
1163 ASSERT (os_get_cpu_number () == 0);
1165 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1167 *vlib_worker_threads->wait_at_barrier = 1;
1168 while (*vlib_worker_threads->workers_at_barrier != count)
1170 if (vlib_time_now (vm) > deadline)
1172 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1179 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1186 if (--vlib_worker_threads[0].recursion_level > 0)
1189 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1191 *vlib_worker_threads->wait_at_barrier = 0;
1193 while (*vlib_worker_threads->workers_at_barrier > 0)
1195 if (vlib_time_now (vm) > deadline)
1197 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1204 * Check the frame queue to see if any frames are available.
1205 * If so, pull the packets off the frames and put them to
1209 vlib_frame_queue_dequeue_internal (vlib_main_t * vm,
1210 vlib_frame_queue_main_t * fqm)
1212 u32 thread_id = vm->cpu_index;
1213 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1214 vlib_frame_queue_elt_t *elt;
1223 ASSERT (vm == vlib_mains[thread_id]);
1225 if (PREDICT_FALSE (fqm->node_index == ~0))
1228 * Gather trace data for frame queues
1230 if (PREDICT_FALSE (fq->trace))
1232 frame_queue_trace_t *fqt;
1233 frame_queue_nelt_counter_t *fqh;
1236 fqt = &fqm->frame_queue_traces[thread_id];
1238 fqt->nelts = fq->nelts;
1239 fqt->head = fq->head;
1240 fqt->head_hint = fq->head_hint;
1241 fqt->tail = fq->tail;
1242 fqt->threshold = fq->vector_threshold;
1243 fqt->n_in_use = fqt->tail - fqt->head;
1244 if (fqt->n_in_use >= fqt->nelts)
1246 // if beyond max then use max
1247 fqt->n_in_use = fqt->nelts - 1;
1250 /* Record the number of elements in use in the histogram */
1251 fqh = &fqm->frame_queue_histogram[thread_id];
1252 fqh->count[fqt->n_in_use]++;
1254 /* Record a snapshot of the elements in use */
1255 for (elix = 0; elix < fqt->nelts; elix++)
1257 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1258 if (1 || elt->valid)
1260 fqt->n_vectors[elix] = elt->n_vectors;
1268 if (fq->head == fq->tail)
1270 fq->head_hint = fq->head;
1274 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1278 fq->head_hint = fq->head;
1282 from = elt->buffer_index;
1283 msg_type = elt->msg_type;
1285 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1286 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1288 f = vlib_get_frame_to_node (vm, fqm->node_index);
1290 to = vlib_frame_vector_args (f);
1292 n_left_to_node = elt->n_vectors;
1294 while (n_left_to_node >= 4)
1302 n_left_to_node -= 4;
1305 while (n_left_to_node > 0)
1313 vectors += elt->n_vectors;
1314 f->n_vectors = elt->n_vectors;
1315 vlib_put_frame_to_node (vm, fqm->node_index, f);
1319 elt->msg_type = 0xfefefefe;
1320 CLIB_MEMORY_BARRIER ();
1325 * Limit the number of packets pushed into the graph
1327 if (vectors >= fq->vector_threshold)
1329 fq->head_hint = fq->head;
1337 static_always_inline void
1338 vlib_worker_thread_internal (vlib_main_t * vm)
1340 vlib_node_main_t *nm = &vm->node_main;
1341 vlib_thread_main_t *tm = vlib_get_thread_main ();
1342 u64 cpu_time_now = clib_cpu_time_now ();
1343 vlib_frame_queue_main_t *fqm;
1345 vec_alloc (nm->pending_interrupt_node_runtime_indices, 32);
1349 vlib_worker_thread_barrier_check ();
1351 vec_foreach (fqm, tm->frame_queue_mains)
1352 vlib_frame_queue_dequeue_internal (vm, fqm);
1354 vlib_node_runtime_t *n;
1355 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1357 cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1358 VLIB_NODE_STATE_POLLING, /* frame */ 0,
1362 /* Next handle interrupts. */
1364 uword l = _vec_len (nm->pending_interrupt_node_runtime_indices);
1368 _vec_len (nm->pending_interrupt_node_runtime_indices) = 0;
1369 for (i = 0; i < l; i++)
1371 n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1373 pending_interrupt_node_runtime_indices
1376 dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1377 VLIB_NODE_STATE_INTERRUPT,
1384 if (_vec_len (nm->pending_frames))
1387 cpu_time_now = clib_cpu_time_now ();
1388 for (i = 0; i < _vec_len (nm->pending_frames); i++)
1390 vlib_pending_frame_t *p;
1392 p = nm->pending_frames + i;
1394 cpu_time_now = dispatch_pending_node (vm, p, cpu_time_now);
1396 _vec_len (nm->pending_frames) = 0;
1398 vlib_increment_main_loop_counter (vm);
1400 /* Record time stamp in case there are no enabled nodes and above
1401 calls do not update time stamp. */
1402 cpu_time_now = clib_cpu_time_now ();
1407 vlib_worker_thread_fn (void *arg)
1409 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1410 vlib_thread_main_t *tm = vlib_get_thread_main ();
1411 vlib_main_t *vm = vlib_get_main ();
1413 ASSERT (vm->cpu_index == os_get_cpu_number ());
1415 vlib_worker_thread_init (w);
1416 clib_time_init (&vm->clib_time);
1417 clib_mem_set_heap (w->thread_mheap);
1419 /* Wait until the dpdk init sequence is complete */
1420 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1421 vlib_worker_thread_barrier_check ();
1423 vlib_worker_thread_internal (vm);
1427 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1430 .function = vlib_worker_thread_fn,
1435 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1437 vlib_thread_main_t *tm = vlib_get_thread_main ();
1438 vlib_frame_queue_main_t *fqm;
1439 vlib_frame_queue_t *fq;
1442 if (frame_queue_nelts == 0)
1443 frame_queue_nelts = FRAME_QUEUE_NELTS;
1445 vec_add2 (tm->frame_queue_mains, fqm, 1);
1447 fqm->node_index = node_index;
1449 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1450 _vec_len (fqm->vlib_frame_queues) = 0;
1451 for (i = 0; i < tm->n_vlib_mains; i++)
1453 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1454 vec_add1 (fqm->vlib_frame_queues, fq);
1457 return (fqm - tm->frame_queue_mains);
1462 vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1464 vlib_thread_main_t *tm = vlib_get_thread_main ();
1466 if (tm->extern_thread_mgmt)
1469 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1470 tm->extern_thread_mgmt = 1;
1475 threads_init (vlib_main_t * vm)
1480 VLIB_INIT_FUNCTION (threads_init);
1483 * fd.io coding-style-patch-verification: ON
1486 * eval: (c-set-style "gnu")