Use thread local storage for thread index
[vpp.git] / src / vlib / threads.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #define _GNU_SOURCE
16
17 #include <signal.h>
18 #include <math.h>
19 #include <vppinfra/format.h>
20 #include <vlib/vlib.h>
21
22 #include <vlib/threads.h>
23 #include <vlib/unix/cj.h>
24
25 DECLARE_CJ_GLOBAL_LOG;
26
27 #define FRAME_QUEUE_NELTS 32
28
29 u32
30 vl (void *p)
31 {
32   return vec_len (p);
33 }
34
35 vlib_worker_thread_t *vlib_worker_threads;
36 vlib_thread_main_t vlib_thread_main;
37
38 __thread uword vlib_thread_index = 0;
39
40 uword
41 os_get_cpu_number (void)
42 {
43   return vlib_thread_index;
44 }
45
46 uword
47 os_get_ncpus (void)
48 {
49   u32 len;
50
51   len = vec_len (vlib_thread_stacks);
52   if (len == 0)
53     return 1;
54   else
55     return len;
56 }
57
58 void
59 vlib_set_thread_name (char *name)
60 {
61   int pthread_setname_np (pthread_t __target_thread, const char *__name);
62   int rv;
63   pthread_t thread = pthread_self ();
64
65   if (thread)
66     {
67       rv = pthread_setname_np (thread, name);
68       if (rv)
69         clib_warning ("pthread_setname_np returned %d", rv);
70     }
71 }
72
73 static int
74 sort_registrations_by_no_clone (void *a0, void *a1)
75 {
76   vlib_thread_registration_t **tr0 = a0;
77   vlib_thread_registration_t **tr1 = a1;
78
79   return ((i32) ((*tr0)->no_data_structure_clone)
80           - ((i32) ((*tr1)->no_data_structure_clone)));
81 }
82
83 static uword *
84 vlib_sysfs_list_to_bitmap (char *filename)
85 {
86   FILE *fp;
87   uword *r = 0;
88
89   fp = fopen (filename, "r");
90
91   if (fp != NULL)
92     {
93       u8 *buffer = 0;
94       vec_validate (buffer, 256 - 1);
95       if (fgets ((char *) buffer, 256, fp))
96         {
97           unformat_input_t in;
98           unformat_init_string (&in, (char *) buffer,
99                                 strlen ((char *) buffer));
100           if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
101             clib_warning ("unformat_bitmap_list failed");
102           unformat_free (&in);
103         }
104       vec_free (buffer);
105       fclose (fp);
106     }
107   return r;
108 }
109
110
111 /* Called early in the init sequence */
112
113 clib_error_t *
114 vlib_thread_init (vlib_main_t * vm)
115 {
116   vlib_thread_main_t *tm = &vlib_thread_main;
117   vlib_worker_thread_t *w;
118   vlib_thread_registration_t *tr;
119   u32 n_vlib_mains = 1;
120   u32 first_index = 1;
121   u32 i;
122   uword *avail_cpu;
123
124   /* get bitmaps of active cpu cores and sockets */
125   tm->cpu_core_bitmap =
126     vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
127   tm->cpu_socket_bitmap =
128     vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
129
130   avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
131
132   /* skip cores */
133   for (i = 0; i < tm->skip_cores; i++)
134     {
135       uword c = clib_bitmap_first_set (avail_cpu);
136       if (c == ~0)
137         return clib_error_return (0, "no available cpus to skip");
138
139       avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
140     }
141
142   /* grab cpu for main thread */
143   if (!tm->main_lcore)
144     {
145       tm->main_lcore = clib_bitmap_first_set (avail_cpu);
146       if (tm->main_lcore == (u8) ~ 0)
147         return clib_error_return (0, "no available cpus to be used for the"
148                                   " main thread");
149     }
150   else
151     {
152       if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
153         return clib_error_return (0, "cpu %u is not available to be used"
154                                   " for the main thread", tm->main_lcore);
155     }
156   avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
157
158   /* assume that there is socket 0 only if there is no data from sysfs */
159   if (!tm->cpu_socket_bitmap)
160     tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
161
162   /* pin main thread to main_lcore  */
163   if (tm->cb.vlib_thread_set_lcore_cb)
164     {
165       tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
166     }
167   else
168     {
169       cpu_set_t cpuset;
170       CPU_ZERO (&cpuset);
171       CPU_SET (tm->main_lcore, &cpuset);
172       pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
173     }
174
175   /* as many threads as stacks... */
176   vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
177                         CLIB_CACHE_LINE_BYTES);
178
179   /* Preallocate thread 0 */
180   _vec_len (vlib_worker_threads) = 1;
181   w = vlib_worker_threads;
182   w->thread_mheap = clib_mem_get_heap ();
183   w->thread_stack = vlib_thread_stacks[0];
184   w->lcore_id = tm->main_lcore;
185   w->lwp = syscall (SYS_gettid);
186   w->thread_id = pthread_self ();
187   tm->n_vlib_mains = 1;
188
189   if (tm->sched_policy != ~0)
190     {
191       struct sched_param sched_param;
192       if (!sched_getparam (w->lwp, &sched_param))
193         {
194           if (tm->sched_priority != ~0)
195             sched_param.sched_priority = tm->sched_priority;
196           sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
197         }
198     }
199
200   /* assign threads to cores and set n_vlib_mains */
201   tr = tm->next;
202
203   while (tr)
204     {
205       vec_add1 (tm->registrations, tr);
206       tr = tr->next;
207     }
208
209   vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
210
211   for (i = 0; i < vec_len (tm->registrations); i++)
212     {
213       int j;
214       tr = tm->registrations[i];
215       tr->first_index = first_index;
216       first_index += tr->count;
217       n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
218
219       /* construct coremask */
220       if (tr->use_pthreads || !tr->count)
221         continue;
222
223       if (tr->coremask)
224         {
225           uword c;
226           /* *INDENT-OFF* */
227           clib_bitmap_foreach (c, tr->coremask, ({
228             if (clib_bitmap_get(avail_cpu, c) == 0)
229               return clib_error_return (0, "cpu %u is not available to be used"
230                                         " for the '%s' thread",c, tr->name);
231
232             avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
233           }));
234 /* *INDENT-ON* */
235
236         }
237       else
238         {
239           for (j = 0; j < tr->count; j++)
240             {
241               uword c = clib_bitmap_first_set (avail_cpu);
242               if (c == ~0)
243                 return clib_error_return (0,
244                                           "no available cpus to be used for"
245                                           " the '%s' thread", tr->name);
246
247               avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
248               tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
249             }
250         }
251     }
252
253   clib_bitmap_free (avail_cpu);
254
255   tm->n_vlib_mains = n_vlib_mains;
256
257   vec_validate_aligned (vlib_worker_threads, first_index - 1,
258                         CLIB_CACHE_LINE_BYTES);
259
260   return 0;
261 }
262
263 vlib_frame_queue_t *
264 vlib_frame_queue_alloc (int nelts)
265 {
266   vlib_frame_queue_t *fq;
267
268   fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
269   memset (fq, 0, sizeof (*fq));
270   fq->nelts = nelts;
271   fq->vector_threshold = 128;   // packets
272   vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
273
274   if (1)
275     {
276       if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
277         fformat (stderr, "WARNING: fq->tail unaligned\n");
278       if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
279         fformat (stderr, "WARNING: fq->head unaligned\n");
280       if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
281         fformat (stderr, "WARNING: fq->elts unaligned\n");
282
283       if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
284         fformat (stderr, "WARNING: fq->elts[0] size %d\n",
285                  sizeof (fq->elts[0]));
286       if (nelts & (nelts - 1))
287         {
288           fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
289           abort ();
290         }
291     }
292
293   return (fq);
294 }
295
296 void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
297 void
298 vl_msg_api_handler_no_free (void *v)
299 {
300 }
301
302 /* Turned off, save as reference material... */
303 #if 0
304 static inline int
305 vlib_frame_queue_dequeue_internal (int thread_id,
306                                    vlib_main_t * vm, vlib_node_main_t * nm)
307 {
308   vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
309   vlib_frame_queue_elt_t *elt;
310   vlib_frame_t *f;
311   vlib_pending_frame_t *p;
312   vlib_node_runtime_t *r;
313   u32 node_runtime_index;
314   int msg_type;
315   u64 before;
316   int processed = 0;
317
318   ASSERT (vm == vlib_mains[thread_id]);
319
320   while (1)
321     {
322       if (fq->head == fq->tail)
323         return processed;
324
325       elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
326
327       if (!elt->valid)
328         return processed;
329
330       before = clib_cpu_time_now ();
331
332       f = elt->frame;
333       node_runtime_index = elt->node_runtime_index;
334       msg_type = elt->msg_type;
335
336       switch (msg_type)
337         {
338         case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
339           vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
340           /* note fallthrough... */
341         case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
342           r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
343                                 node_runtime_index);
344           vlib_frame_free (vm, r, f);
345           break;
346         case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
347           vec_add2 (vm->node_main.pending_frames, p, 1);
348           f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
349           p->node_runtime_index = elt->node_runtime_index;
350           p->frame_index = vlib_frame_index (vm, f);
351           p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
352           fq->dequeue_vectors += (u64) f->n_vectors;
353           break;
354         case VLIB_FRAME_QUEUE_ELT_API_MSG:
355           vl_msg_api_handler_no_free (f);
356           break;
357         default:
358           clib_warning ("bogus frame queue message, type %d", msg_type);
359           break;
360         }
361       elt->valid = 0;
362       fq->dequeues++;
363       fq->dequeue_ticks += clib_cpu_time_now () - before;
364       CLIB_MEMORY_BARRIER ();
365       fq->head++;
366       processed++;
367     }
368   ASSERT (0);
369   return processed;
370 }
371
372 int
373 vlib_frame_queue_dequeue (int thread_id,
374                           vlib_main_t * vm, vlib_node_main_t * nm)
375 {
376   return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
377 }
378
379 int
380 vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
381                           u32 frame_queue_index, vlib_frame_t * frame,
382                           vlib_frame_queue_msg_type_t type)
383 {
384   vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
385   vlib_frame_queue_elt_t *elt;
386   u32 save_count;
387   u64 new_tail;
388   u64 before = clib_cpu_time_now ();
389
390   ASSERT (fq);
391
392   new_tail = __sync_add_and_fetch (&fq->tail, 1);
393
394   /* Wait until a ring slot is available */
395   while (new_tail >= fq->head + fq->nelts)
396     {
397       f64 b4 = vlib_time_now_ticks (vm, before);
398       vlib_worker_thread_barrier_check (vm, b4);
399       /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
400       // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
401     }
402
403   elt = fq->elts + (new_tail & (fq->nelts - 1));
404
405   /* this would be very bad... */
406   while (elt->valid)
407     {
408     }
409
410   /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
411   save_count = frame->n_vectors;
412
413   elt->frame = frame;
414   elt->node_runtime_index = node_runtime_index;
415   elt->msg_type = type;
416   CLIB_MEMORY_BARRIER ();
417   elt->valid = 1;
418
419   return save_count;
420 }
421 #endif /* 0 */
422
423 /* To be called by vlib worker threads upon startup */
424 void
425 vlib_worker_thread_init (vlib_worker_thread_t * w)
426 {
427   vlib_thread_main_t *tm = vlib_get_thread_main ();
428
429   /*
430    * Note: disabling signals in worker threads as follows
431    * prevents the api post-mortem dump scheme from working
432    * {
433    *    sigset_t s;
434    *    sigfillset (&s);
435    *    pthread_sigmask (SIG_SETMASK, &s, 0);
436    *  }
437    */
438
439   clib_mem_set_heap (w->thread_mheap);
440
441   if (vec_len (tm->thread_prefix) && w->registration->short_name)
442     {
443       w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
444                         w->registration->short_name, w->instance_id, '\0');
445       vlib_set_thread_name ((char *) w->name);
446     }
447
448   if (!w->registration->use_pthreads)
449     {
450
451       /* Initial barrier sync, for both worker and i/o threads */
452       clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
453
454       while (*vlib_worker_threads->wait_at_barrier)
455         ;
456
457       clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
458     }
459 }
460
461 void *
462 vlib_worker_thread_bootstrap_fn (void *arg)
463 {
464   void *rv;
465   vlib_worker_thread_t *w = arg;
466
467   w->lwp = syscall (SYS_gettid);
468   w->thread_id = pthread_self ();
469
470   vlib_thread_index = w - vlib_worker_threads;
471
472   rv = (void *) clib_calljmp
473     ((uword (*)(uword)) w->thread_function,
474      (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
475   /* NOTREACHED, we hope */
476   return rv;
477 }
478
479 static clib_error_t *
480 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
481 {
482   vlib_thread_main_t *tm = &vlib_thread_main;
483   void *(*fp_arg) (void *) = fp;
484
485   w->lcore_id = lcore_id;
486   if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
487     return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
488   else
489     {
490       pthread_t worker;
491       cpu_set_t cpuset;
492       CPU_ZERO (&cpuset);
493       CPU_SET (lcore_id, &cpuset);
494
495       if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
496         return clib_error_return_unix (0, "pthread_create");
497
498       if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
499         return clib_error_return_unix (0, "pthread_setaffinity_np");
500
501       return 0;
502     }
503 }
504
505 static clib_error_t *
506 start_workers (vlib_main_t * vm)
507 {
508   int i, j;
509   vlib_worker_thread_t *w;
510   vlib_main_t *vm_clone;
511   void *oldheap;
512   vlib_thread_main_t *tm = &vlib_thread_main;
513   vlib_thread_registration_t *tr;
514   vlib_node_runtime_t *rt;
515   u32 n_vlib_mains = tm->n_vlib_mains;
516   u32 worker_thread_index;
517   u8 *main_heap = clib_mem_get_per_cpu_heap ();
518   mheap_t *main_heap_header = mheap_header (main_heap);
519
520   vec_reset_length (vlib_worker_threads);
521
522   /* Set up the main thread */
523   vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
524   w->elog_track.name = "main thread";
525   elog_track_register (&vm->elog_main, &w->elog_track);
526
527   if (vec_len (tm->thread_prefix))
528     {
529       w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
530       vlib_set_thread_name ((char *) w->name);
531     }
532
533   /*
534    * Truth of the matter: we always use at least two
535    * threads. So, make the main heap thread-safe
536    * and make the event log thread-safe.
537    */
538   main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
539   vm->elog_main.lock =
540     clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
541   vm->elog_main.lock[0] = 0;
542
543   if (n_vlib_mains > 1)
544     {
545       /* Replace hand-crafted length-1 vector with a real vector */
546       vlib_mains = 0;
547
548       vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
549                             CLIB_CACHE_LINE_BYTES);
550       _vec_len (vlib_mains) = 0;
551       vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
552
553       vlib_worker_threads->wait_at_barrier =
554         clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
555       vlib_worker_threads->workers_at_barrier =
556         clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
557
558       /* Ask for an initial barrier sync */
559       *vlib_worker_threads->workers_at_barrier = 0;
560       *vlib_worker_threads->wait_at_barrier = 1;
561
562       worker_thread_index = 1;
563
564       for (i = 0; i < vec_len (tm->registrations); i++)
565         {
566           vlib_node_main_t *nm, *nm_clone;
567           vlib_buffer_main_t *bm_clone;
568           vlib_buffer_free_list_t *fl_clone, *fl_orig;
569           vlib_buffer_free_list_t *orig_freelist_pool;
570           int k;
571
572           tr = tm->registrations[i];
573
574           if (tr->count == 0)
575             continue;
576
577           for (k = 0; k < tr->count; k++)
578             {
579               vec_add2 (vlib_worker_threads, w, 1);
580               if (tr->mheap_size)
581                 w->thread_mheap =
582                   mheap_alloc (0 /* use VM */ , tr->mheap_size);
583               else
584                 w->thread_mheap = main_heap;
585
586               w->thread_stack =
587                 vlib_thread_stack_init (w - vlib_worker_threads);
588               w->thread_function = tr->function;
589               w->thread_function_arg = w;
590               w->instance_id = k;
591               w->registration = tr;
592
593               w->elog_track.name =
594                 (char *) format (0, "%s %d", tr->name, k + 1);
595               vec_add1 (w->elog_track.name, 0);
596               elog_track_register (&vm->elog_main, &w->elog_track);
597
598               if (tr->no_data_structure_clone)
599                 continue;
600
601               /* Fork vlib_global_main et al. Look for bugs here */
602               oldheap = clib_mem_set_heap (w->thread_mheap);
603
604               vm_clone = clib_mem_alloc (sizeof (*vm_clone));
605               clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
606
607               vm_clone->thread_index = worker_thread_index;
608               vm_clone->heap_base = w->thread_mheap;
609               vm_clone->mbuf_alloc_list = 0;
610               vm_clone->init_functions_called =
611                 hash_create (0, /* value bytes */ 0);
612               memset (&vm_clone->random_buffer, 0,
613                       sizeof (vm_clone->random_buffer));
614
615               nm = &vlib_mains[0]->node_main;
616               nm_clone = &vm_clone->node_main;
617               /* fork next frames array, preserving node runtime indices */
618               nm_clone->next_frames = vec_dup (nm->next_frames);
619               for (j = 0; j < vec_len (nm_clone->next_frames); j++)
620                 {
621                   vlib_next_frame_t *nf = &nm_clone->next_frames[j];
622                   u32 save_node_runtime_index;
623                   u32 save_flags;
624
625                   save_node_runtime_index = nf->node_runtime_index;
626                   save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
627                   vlib_next_frame_init (nf);
628                   nf->node_runtime_index = save_node_runtime_index;
629                   nf->flags = save_flags;
630                 }
631
632               /* fork the frame dispatch queue */
633               nm_clone->pending_frames = 0;
634               vec_validate (nm_clone->pending_frames, 10);      /* $$$$$?????? */
635               _vec_len (nm_clone->pending_frames) = 0;
636
637               /* fork nodes */
638               nm_clone->nodes = 0;
639               for (j = 0; j < vec_len (nm->nodes); j++)
640                 {
641                   vlib_node_t *n;
642                   n = clib_mem_alloc_no_fail (sizeof (*n));
643                   clib_memcpy (n, nm->nodes[j], sizeof (*n));
644                   /* none of the copied nodes have enqueue rights given out */
645                   n->owner_node_index = VLIB_INVALID_NODE_INDEX;
646                   memset (&n->stats_total, 0, sizeof (n->stats_total));
647                   memset (&n->stats_last_clear, 0,
648                           sizeof (n->stats_last_clear));
649                   vec_add1 (nm_clone->nodes, n);
650                 }
651               nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
652                 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
653               vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
654               {
655                 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
656                 rt->thread_index = vm_clone->thread_index;
657                 /* copy initial runtime_data from node */
658                 if (n->runtime_data && n->runtime_data_bytes > 0)
659                   clib_memcpy (rt->runtime_data, n->runtime_data,
660                                clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
661                                          n->runtime_data_bytes));
662               }
663
664               nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
665                 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
666               vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
667               {
668                 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
669                 rt->thread_index = vm_clone->thread_index;
670                 /* copy initial runtime_data from node */
671                 if (n->runtime_data && n->runtime_data_bytes > 0)
672                   clib_memcpy (rt->runtime_data, n->runtime_data,
673                                clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
674                                          n->runtime_data_bytes));
675               }
676
677               nm_clone->processes = vec_dup (nm->processes);
678
679               /* zap the (per worker) frame freelists, etc */
680               nm_clone->frame_sizes = 0;
681               nm_clone->frame_size_hash = 0;
682
683               /* Packet trace buffers are guaranteed to be empty, nothing to do here */
684
685               clib_mem_set_heap (oldheap);
686               vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
687
688               vm_clone->error_main.counters =
689                 vec_dup (vlib_mains[0]->error_main.counters);
690               vm_clone->error_main.counters_last_clear =
691                 vec_dup (vlib_mains[0]->error_main.counters_last_clear);
692
693               /* Fork the vlib_buffer_main_t free lists, etc. */
694               bm_clone = vec_dup (vm_clone->buffer_main);
695               vm_clone->buffer_main = bm_clone;
696
697               orig_freelist_pool = bm_clone->buffer_free_list_pool;
698               bm_clone->buffer_free_list_pool = 0;
699
700             /* *INDENT-OFF* */
701             pool_foreach (fl_orig, orig_freelist_pool,
702                           ({
703                             pool_get_aligned (bm_clone->buffer_free_list_pool,
704                                               fl_clone, CLIB_CACHE_LINE_BYTES);
705                             ASSERT (fl_orig - orig_freelist_pool
706                                     == fl_clone - bm_clone->buffer_free_list_pool);
707
708                             fl_clone[0] = fl_orig[0];
709                             fl_clone->buffers = 0;
710                             fl_clone->n_alloc = 0;
711                           }));
712 /* *INDENT-ON* */
713
714               worker_thread_index++;
715             }
716         }
717     }
718   else
719     {
720       /* only have non-data-structure copy threads to create... */
721       for (i = 0; i < vec_len (tm->registrations); i++)
722         {
723           tr = tm->registrations[i];
724
725           for (j = 0; j < tr->count; j++)
726             {
727               vec_add2 (vlib_worker_threads, w, 1);
728               if (tr->mheap_size)
729                 w->thread_mheap =
730                   mheap_alloc (0 /* use VM */ , tr->mheap_size);
731               else
732                 w->thread_mheap = main_heap;
733               w->thread_stack =
734                 vlib_thread_stack_init (w - vlib_worker_threads);
735               w->thread_function = tr->function;
736               w->thread_function_arg = w;
737               w->instance_id = j;
738               w->elog_track.name =
739                 (char *) format (0, "%s %d", tr->name, j + 1);
740               w->registration = tr;
741               vec_add1 (w->elog_track.name, 0);
742               elog_track_register (&vm->elog_main, &w->elog_track);
743             }
744         }
745     }
746
747   worker_thread_index = 1;
748
749   for (i = 0; i < vec_len (tm->registrations); i++)
750     {
751       clib_error_t *err;
752       int j;
753
754       tr = tm->registrations[i];
755
756       if (tr->use_pthreads || tm->use_pthreads)
757         {
758           for (j = 0; j < tr->count; j++)
759             {
760               w = vlib_worker_threads + worker_thread_index++;
761               err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
762                                             w, 0);
763               if (err)
764                 clib_error_report (err);
765             }
766         }
767       else
768         {
769           uword c;
770           /* *INDENT-OFF* */
771           clib_bitmap_foreach (c, tr->coremask, ({
772             w = vlib_worker_threads + worker_thread_index++;
773             err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
774                                           w, c);
775             if (err)
776               clib_error_report (err);
777           }));
778           /* *INDENT-ON* */
779         }
780     }
781   vlib_worker_thread_barrier_sync (vm);
782   vlib_worker_thread_barrier_release (vm);
783   return 0;
784 }
785
786 VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
787
788 void
789 vlib_worker_thread_node_runtime_update (void)
790 {
791   int i, j;
792   vlib_worker_thread_t *w;
793   vlib_main_t *vm;
794   vlib_node_main_t *nm, *nm_clone;
795   vlib_node_t **old_nodes_clone;
796   vlib_main_t *vm_clone;
797   vlib_node_runtime_t *rt, *old_rt;
798   void *oldheap;
799   never_inline void
800     vlib_node_runtime_sync_stats (vlib_main_t * vm,
801                                   vlib_node_runtime_t * r,
802                                   uword n_calls,
803                                   uword n_vectors, uword n_clocks);
804
805   ASSERT (vlib_get_thread_index () == 0);
806
807   if (vec_len (vlib_mains) == 1)
808     return;
809
810   vm = vlib_mains[0];
811   nm = &vm->node_main;
812
813   ASSERT (vlib_get_thread_index () == 0);
814   ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
815
816   /*
817    * Scrape all runtime stats, so we don't lose node runtime(s) with
818    * pending counts, or throw away worker / io thread counts.
819    */
820   for (j = 0; j < vec_len (nm->nodes); j++)
821     {
822       vlib_node_t *n;
823       n = nm->nodes[j];
824       vlib_node_sync_stats (vm, n);
825     }
826
827   for (i = 1; i < vec_len (vlib_mains); i++)
828     {
829       vlib_node_t *n;
830
831       vm_clone = vlib_mains[i];
832       nm_clone = &vm_clone->node_main;
833
834       for (j = 0; j < vec_len (nm_clone->nodes); j++)
835         {
836           n = nm_clone->nodes[j];
837
838           rt = vlib_node_get_runtime (vm_clone, n->index);
839           vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
840         }
841     }
842
843   for (i = 1; i < vec_len (vlib_mains); i++)
844     {
845       vlib_node_runtime_t *rt;
846       w = vlib_worker_threads + i;
847       oldheap = clib_mem_set_heap (w->thread_mheap);
848
849       vm_clone = vlib_mains[i];
850
851       /* Re-clone error heap */
852       u64 *old_counters = vm_clone->error_main.counters;
853       u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
854       clib_memcpy (&vm_clone->error_main, &vm->error_main,
855                    sizeof (vm->error_main));
856       j = vec_len (vm->error_main.counters) - 1;
857       vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
858       vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
859       vm_clone->error_main.counters = old_counters;
860       vm_clone->error_main.counters_last_clear = old_counters_all_clear;
861
862       nm_clone = &vm_clone->node_main;
863       vec_free (nm_clone->next_frames);
864       nm_clone->next_frames = vec_dup (nm->next_frames);
865
866       for (j = 0; j < vec_len (nm_clone->next_frames); j++)
867         {
868           vlib_next_frame_t *nf = &nm_clone->next_frames[j];
869           u32 save_node_runtime_index;
870           u32 save_flags;
871
872           save_node_runtime_index = nf->node_runtime_index;
873           save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
874           vlib_next_frame_init (nf);
875           nf->node_runtime_index = save_node_runtime_index;
876           nf->flags = save_flags;
877         }
878
879       old_nodes_clone = nm_clone->nodes;
880       nm_clone->nodes = 0;
881
882       /* re-fork nodes */
883       for (j = 0; j < vec_len (nm->nodes); j++)
884         {
885           vlib_node_t *old_n_clone;
886           vlib_node_t *new_n, *new_n_clone;
887
888           new_n = nm->nodes[j];
889           old_n_clone = old_nodes_clone[j];
890
891           new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
892           clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
893           /* none of the copied nodes have enqueue rights given out */
894           new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
895
896           if (j >= vec_len (old_nodes_clone))
897             {
898               /* new node, set to zero */
899               memset (&new_n_clone->stats_total, 0,
900                       sizeof (new_n_clone->stats_total));
901               memset (&new_n_clone->stats_last_clear, 0,
902                       sizeof (new_n_clone->stats_last_clear));
903             }
904           else
905             {
906               /* Copy stats if the old data is valid */
907               clib_memcpy (&new_n_clone->stats_total,
908                            &old_n_clone->stats_total,
909                            sizeof (new_n_clone->stats_total));
910               clib_memcpy (&new_n_clone->stats_last_clear,
911                            &old_n_clone->stats_last_clear,
912                            sizeof (new_n_clone->stats_last_clear));
913
914               /* keep previous node state */
915               new_n_clone->state = old_n_clone->state;
916             }
917           vec_add1 (nm_clone->nodes, new_n_clone);
918         }
919       /* Free the old node clone */
920       for (j = 0; j < vec_len (old_nodes_clone); j++)
921         clib_mem_free (old_nodes_clone[j]);
922       vec_free (old_nodes_clone);
923
924
925       /* re-clone internal nodes */
926       old_rt = 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]);
929
930       vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
931       {
932         vlib_node_t *n = vlib_get_node (vm, rt->node_index);
933         rt->thread_index = vm_clone->thread_index;
934         /* copy runtime_data, will be overwritten later for existing rt */
935         if (n->runtime_data && n->runtime_data_bytes > 0)
936           clib_memcpy (rt->runtime_data, n->runtime_data,
937                        clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
938                                  n->runtime_data_bytes));
939       }
940
941       for (j = 0; j < vec_len (old_rt); j++)
942         {
943           rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
944           rt->state = old_rt[j].state;
945           clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
946                        VLIB_NODE_RUNTIME_DATA_SIZE);
947         }
948
949       vec_free (old_rt);
950
951       /* re-clone input nodes */
952       old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
953       nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
954         vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
955
956       vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
957       {
958         vlib_node_t *n = vlib_get_node (vm, rt->node_index);
959         rt->thread_index = vm_clone->thread_index;
960         /* copy runtime_data, will be overwritten later for existing rt */
961         if (n->runtime_data && n->runtime_data_bytes > 0)
962           clib_memcpy (rt->runtime_data, n->runtime_data,
963                        clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
964                                  n->runtime_data_bytes));
965       }
966
967       for (j = 0; j < vec_len (old_rt); j++)
968         {
969           rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
970           rt->state = old_rt[j].state;
971           clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
972                        VLIB_NODE_RUNTIME_DATA_SIZE);
973         }
974
975       vec_free (old_rt);
976
977       nm_clone->processes = vec_dup (nm->processes);
978
979       clib_mem_set_heap (oldheap);
980
981       // vnet_main_fork_fixup (i);
982     }
983 }
984
985 u32
986 unformat_sched_policy (unformat_input_t * input, va_list * args)
987 {
988   u32 *r = va_arg (*args, u32 *);
989
990   if (0);
991 #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
992   foreach_sched_policy
993 #undef _
994     else
995     return 0;
996   return 1;
997 }
998
999 static clib_error_t *
1000 cpu_config (vlib_main_t * vm, unformat_input_t * input)
1001 {
1002   vlib_thread_registration_t *tr;
1003   uword *p;
1004   vlib_thread_main_t *tm = &vlib_thread_main;
1005   u8 *name;
1006   u64 coremask;
1007   uword *bitmap;
1008   u32 count;
1009
1010   tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
1011
1012   tm->n_thread_stacks = 1;      /* account for main thread */
1013   tm->sched_policy = ~0;
1014   tm->sched_priority = ~0;
1015
1016   tr = tm->next;
1017
1018   while (tr)
1019     {
1020       hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
1021       tr = tr->next;
1022     }
1023
1024   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1025     {
1026       if (unformat (input, "use-pthreads"))
1027         tm->use_pthreads = 1;
1028       else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
1029         ;
1030       else if (unformat (input, "main-core %u", &tm->main_lcore))
1031         ;
1032       else if (unformat (input, "skip-cores %u", &tm->skip_cores))
1033         ;
1034       else if (unformat (input, "coremask-%s %llx", &name, &coremask))
1035         {
1036           p = hash_get_mem (tm->thread_registrations_by_name, name);
1037           if (p == 0)
1038             return clib_error_return (0, "no such thread type '%s'", name);
1039
1040           tr = (vlib_thread_registration_t *) p[0];
1041
1042           if (tr->use_pthreads)
1043             return clib_error_return (0,
1044                                       "coremask cannot be set for '%s' threads",
1045                                       name);
1046
1047           tr->coremask = clib_bitmap_set_multiple
1048             (tr->coremask, 0, coremask, BITS (coremask));
1049           tr->count = clib_bitmap_count_set_bits (tr->coremask);
1050         }
1051       else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
1052                          &bitmap))
1053         {
1054           p = hash_get_mem (tm->thread_registrations_by_name, name);
1055           if (p == 0)
1056             return clib_error_return (0, "no such thread type '%s'", name);
1057
1058           tr = (vlib_thread_registration_t *) p[0];
1059
1060           if (tr->use_pthreads)
1061             return clib_error_return (0,
1062                                       "corelist cannot be set for '%s' threads",
1063                                       name);
1064
1065           tr->coremask = bitmap;
1066           tr->count = clib_bitmap_count_set_bits (tr->coremask);
1067         }
1068       else
1069         if (unformat
1070             (input, "scheduler-policy %U", unformat_sched_policy,
1071              &tm->sched_policy))
1072         ;
1073       else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
1074         ;
1075       else if (unformat (input, "%s %u", &name, &count))
1076         {
1077           p = hash_get_mem (tm->thread_registrations_by_name, name);
1078           if (p == 0)
1079             return clib_error_return (0, "no such thread type 3 '%s'", name);
1080
1081           tr = (vlib_thread_registration_t *) p[0];
1082           if (tr->fixed_count)
1083             return clib_error_return
1084               (0, "number of %s threads not configurable", tr->name);
1085           tr->count = count;
1086         }
1087       else
1088         break;
1089     }
1090
1091   if (tm->sched_priority != ~0)
1092     {
1093       if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
1094         {
1095           u32 prio_max = sched_get_priority_max (tm->sched_policy);
1096           u32 prio_min = sched_get_priority_min (tm->sched_policy);
1097           if (tm->sched_priority > prio_max)
1098             tm->sched_priority = prio_max;
1099           if (tm->sched_priority < prio_min)
1100             tm->sched_priority = prio_min;
1101         }
1102       else
1103         {
1104           return clib_error_return
1105             (0,
1106              "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1107              tm->sched_priority);
1108         }
1109     }
1110   tr = tm->next;
1111
1112   if (!tm->thread_prefix)
1113     tm->thread_prefix = format (0, "vpp");
1114
1115   while (tr)
1116     {
1117       tm->n_thread_stacks += tr->count;
1118       tm->n_pthreads += tr->count * tr->use_pthreads;
1119       tm->n_threads += tr->count * (tr->use_pthreads == 0);
1120       tr = tr->next;
1121     }
1122
1123   return 0;
1124 }
1125
1126 VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1127
1128 #if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
1129 void
1130 __sync_fetch_and_add_8 (void)
1131 {
1132   fformat (stderr, "%s called\n", __FUNCTION__);
1133   abort ();
1134 }
1135
1136 void
1137 __sync_add_and_fetch_8 (void)
1138 {
1139   fformat (stderr, "%s called\n", __FUNCTION__);
1140   abort ();
1141 }
1142 #endif
1143
1144 void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
1145 void
1146 vnet_main_fixup (vlib_fork_fixup_t which)
1147 {
1148 }
1149
1150 void
1151 vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1152 {
1153   vlib_main_t *vm = vlib_get_main ();
1154
1155   if (vlib_mains == 0)
1156     return;
1157
1158   ASSERT (vlib_get_thread_index () == 0);
1159   vlib_worker_thread_barrier_sync (vm);
1160
1161   switch (which)
1162     {
1163     case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1164       vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1165       break;
1166
1167     default:
1168       ASSERT (0);
1169     }
1170   vlib_worker_thread_barrier_release (vm);
1171 }
1172
1173 void
1174 vlib_worker_thread_barrier_sync (vlib_main_t * vm)
1175 {
1176   f64 deadline;
1177   u32 count;
1178
1179   if (vec_len (vlib_mains) < 2)
1180     return;
1181
1182   count = vec_len (vlib_mains) - 1;
1183
1184   /* Tolerate recursive calls */
1185   if (++vlib_worker_threads[0].recursion_level > 1)
1186     return;
1187
1188   vlib_worker_threads[0].barrier_sync_count++;
1189
1190   ASSERT (vlib_get_thread_index () == 0);
1191
1192   deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1193
1194   *vlib_worker_threads->wait_at_barrier = 1;
1195   while (*vlib_worker_threads->workers_at_barrier != count)
1196     {
1197       if (vlib_time_now (vm) > deadline)
1198         {
1199           fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1200           os_panic ();
1201         }
1202     }
1203 }
1204
1205 void
1206 vlib_worker_thread_barrier_release (vlib_main_t * vm)
1207 {
1208   f64 deadline;
1209
1210   if (vec_len (vlib_mains) < 2)
1211     return;
1212
1213   if (--vlib_worker_threads[0].recursion_level > 0)
1214     return;
1215
1216   deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1217
1218   *vlib_worker_threads->wait_at_barrier = 0;
1219
1220   while (*vlib_worker_threads->workers_at_barrier > 0)
1221     {
1222       if (vlib_time_now (vm) > deadline)
1223         {
1224           fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1225           os_panic ();
1226         }
1227     }
1228 }
1229
1230 /*
1231  * Check the frame queue to see if any frames are available.
1232  * If so, pull the packets off the frames and put them to
1233  * the handoff node.
1234  */
1235 int
1236 vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
1237 {
1238   u32 thread_id = vm->thread_index;
1239   vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
1240   vlib_frame_queue_elt_t *elt;
1241   u32 *from, *to;
1242   vlib_frame_t *f;
1243   int msg_type;
1244   int processed = 0;
1245   u32 n_left_to_node;
1246   u32 vectors = 0;
1247
1248   ASSERT (fq);
1249   ASSERT (vm == vlib_mains[thread_id]);
1250
1251   if (PREDICT_FALSE (fqm->node_index == ~0))
1252     return 0;
1253   /*
1254    * Gather trace data for frame queues
1255    */
1256   if (PREDICT_FALSE (fq->trace))
1257     {
1258       frame_queue_trace_t *fqt;
1259       frame_queue_nelt_counter_t *fqh;
1260       u32 elix;
1261
1262       fqt = &fqm->frame_queue_traces[thread_id];
1263
1264       fqt->nelts = fq->nelts;
1265       fqt->head = fq->head;
1266       fqt->head_hint = fq->head_hint;
1267       fqt->tail = fq->tail;
1268       fqt->threshold = fq->vector_threshold;
1269       fqt->n_in_use = fqt->tail - fqt->head;
1270       if (fqt->n_in_use >= fqt->nelts)
1271         {
1272           // if beyond max then use max
1273           fqt->n_in_use = fqt->nelts - 1;
1274         }
1275
1276       /* Record the number of elements in use in the histogram */
1277       fqh = &fqm->frame_queue_histogram[thread_id];
1278       fqh->count[fqt->n_in_use]++;
1279
1280       /* Record a snapshot of the elements in use */
1281       for (elix = 0; elix < fqt->nelts; elix++)
1282         {
1283           elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1284           if (1 || elt->valid)
1285             {
1286               fqt->n_vectors[elix] = elt->n_vectors;
1287             }
1288         }
1289       fqt->written = 1;
1290     }
1291
1292   while (1)
1293     {
1294       if (fq->head == fq->tail)
1295         {
1296           fq->head_hint = fq->head;
1297           return processed;
1298         }
1299
1300       elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
1301
1302       if (!elt->valid)
1303         {
1304           fq->head_hint = fq->head;
1305           return processed;
1306         }
1307
1308       from = elt->buffer_index;
1309       msg_type = elt->msg_type;
1310
1311       ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1312       ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1313
1314       f = vlib_get_frame_to_node (vm, fqm->node_index);
1315
1316       to = vlib_frame_vector_args (f);
1317
1318       n_left_to_node = elt->n_vectors;
1319
1320       while (n_left_to_node >= 4)
1321         {
1322           to[0] = from[0];
1323           to[1] = from[1];
1324           to[2] = from[2];
1325           to[3] = from[3];
1326           to += 4;
1327           from += 4;
1328           n_left_to_node -= 4;
1329         }
1330
1331       while (n_left_to_node > 0)
1332         {
1333           to[0] = from[0];
1334           to++;
1335           from++;
1336           n_left_to_node--;
1337         }
1338
1339       vectors += elt->n_vectors;
1340       f->n_vectors = elt->n_vectors;
1341       vlib_put_frame_to_node (vm, fqm->node_index, f);
1342
1343       elt->valid = 0;
1344       elt->n_vectors = 0;
1345       elt->msg_type = 0xfefefefe;
1346       CLIB_MEMORY_BARRIER ();
1347       fq->head++;
1348       processed++;
1349
1350       /*
1351        * Limit the number of packets pushed into the graph
1352        */
1353       if (vectors >= fq->vector_threshold)
1354         {
1355           fq->head_hint = fq->head;
1356           return processed;
1357         }
1358     }
1359   ASSERT (0);
1360   return processed;
1361 }
1362
1363 void
1364 vlib_worker_thread_fn (void *arg)
1365 {
1366   vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
1367   vlib_thread_main_t *tm = vlib_get_thread_main ();
1368   vlib_main_t *vm = vlib_get_main ();
1369   clib_error_t *e;
1370
1371   ASSERT (vm->thread_index == vlib_get_thread_index ());
1372
1373   vlib_worker_thread_init (w);
1374   clib_time_init (&vm->clib_time);
1375   clib_mem_set_heap (w->thread_mheap);
1376
1377   /* Wait until the dpdk init sequence is complete */
1378   while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1379     vlib_worker_thread_barrier_check ();
1380
1381   e = vlib_call_init_exit_functions
1382     (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1383   if (e)
1384     clib_error_report (e);
1385
1386   vlib_worker_loop (vm);
1387 }
1388
1389 /* *INDENT-OFF* */
1390 VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1391   .name = "workers",
1392   .short_name = "wk",
1393   .function = vlib_worker_thread_fn,
1394 };
1395 /* *INDENT-ON* */
1396
1397 u32
1398 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1399 {
1400   vlib_thread_main_t *tm = vlib_get_thread_main ();
1401   vlib_frame_queue_main_t *fqm;
1402   vlib_frame_queue_t *fq;
1403   int i;
1404
1405   if (frame_queue_nelts == 0)
1406     frame_queue_nelts = FRAME_QUEUE_NELTS;
1407
1408   vec_add2 (tm->frame_queue_mains, fqm, 1);
1409
1410   fqm->node_index = node_index;
1411
1412   vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1413   _vec_len (fqm->vlib_frame_queues) = 0;
1414   for (i = 0; i < tm->n_vlib_mains; i++)
1415     {
1416       fq = vlib_frame_queue_alloc (frame_queue_nelts);
1417       vec_add1 (fqm->vlib_frame_queues, fq);
1418     }
1419
1420   return (fqm - tm->frame_queue_mains);
1421 }
1422
1423
1424 int
1425 vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1426 {
1427   vlib_thread_main_t *tm = vlib_get_thread_main ();
1428
1429   if (tm->extern_thread_mgmt)
1430     return -1;
1431
1432   tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1433   tm->extern_thread_mgmt = 1;
1434   return 0;
1435 }
1436
1437 clib_error_t *
1438 threads_init (vlib_main_t * vm)
1439 {
1440   return 0;
1441 }
1442
1443 VLIB_INIT_FUNCTION (threads_init);
1444
1445 /*
1446  * fd.io coding-style-patch-verification: ON
1447  *
1448  * Local Variables:
1449  * eval: (c-set-style "gnu")
1450  * End:
1451  */