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