blob: edf5a0e071126e592f8dbf4e6f4a03f4862cf4c7 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 */
Christophe Fontainefef15b42016-04-09 12:38:49 +090015#define _GNU_SOURCE
Christophe Fontainefef15b42016-04-09 12:38:49 +090016
Ed Warnickecb9cada2015-12-08 15:45:58 -070017#include <signal.h>
18#include <math.h>
19#include <vppinfra/format.h>
20#include <vlib/vlib.h>
21
22#include <vlib/threads.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023#include <vlib/unix/cj.h>
24
Ed Warnickecb9cada2015-12-08 15:45:58 -070025DECLARE_CJ_GLOBAL_LOG;
26
27#define FRAME_QUEUE_NELTS 32
28
Dave Barach9b8ffd92016-07-08 08:13:45 -040029u32
30vl (void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -070031{
32 return vec_len (p);
33}
34
Damjan Marion6a7acc22016-12-19 16:28:36 +010035vlib_worker_thread_t *vlib_worker_threads;
Ed Warnickecb9cada2015-12-08 15:45:58 -070036vlib_thread_main_t vlib_thread_main;
37
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010038/*
39 * Barrier tracing can be enabled on a normal build to collect information
40 * on barrier use, including timings and call stacks. Deliberately not
41 * keyed off CLIB_DEBUG, because that can add significant overhead which
42 * imapacts observed timings.
43 */
44
45#ifdef BARRIER_TRACING
46 /*
47 * Output of barrier tracing can be to syslog or elog as suits
48 */
49#ifdef BARRIER_TRACING_ELOG
50static u32
51elog_id_for_msg_name (const char *msg_name)
52{
53 uword *p, r;
54 static uword *h;
55 u8 *name_copy;
56
57 if (!h)
58 h = hash_create_string (0, sizeof (uword));
59
60 p = hash_get_mem (h, msg_name);
61 if (p)
62 return p[0];
63 r = elog_string (&vlib_global_main.elog_main, "%s", msg_name);
64
65 name_copy = format (0, "%s%c", msg_name, 0);
66
67 hash_set_mem (h, name_copy, r);
68
69 return r;
70}
71
72 /*
73 * elog Barrier trace functions, which are nulled out if BARRIER_TRACING isn't
74 * defined
75 */
76
77static inline void
78barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
79{
80 /* *INDENT-OFF* */
81 ELOG_TYPE_DECLARE (e) =
82 {
83 .format = "barrier <%d#%s(O:%dus:%dus)(%dus)",
84 .format_args = "i4T4i4i4i4",
85 };
86 /* *INDENT-ON* */
87 struct
88 {
89 u32 count, caller, t_entry, t_open, t_closed;
90 } *ed = 0;
91
92 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
93 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
94 ed->caller = elog_id_for_msg_name (vlib_worker_threads[0].barrier_caller);
95 ed->t_entry = (int) (1000000.0 * t_entry);
96 ed->t_open = (int) (1000000.0 * t_open);
97 ed->t_closed = (int) (1000000.0 * t_closed);
98}
99
100static inline void
101barrier_trace_sync_rec (f64 t_entry)
102{
103 /* *INDENT-OFF* */
104 ELOG_TYPE_DECLARE (e) =
105 {
106 .format = "barrier <%d(%dus)%s",
107 .format_args = "i4i4T4",
108 };
109 /* *INDENT-ON* */
110 struct
111 {
112 u32 depth, t_entry, caller;
113 } *ed = 0;
114
115 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
116 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
117 ed->t_entry = (int) (1000000.0 * t_entry);
118 ed->caller = elog_id_for_msg_name (vlib_worker_threads[0].barrier_caller);
119}
120
121static inline void
122barrier_trace_release_rec (f64 t_entry)
123{
124 /* *INDENT-OFF* */
125 ELOG_TYPE_DECLARE (e) =
126 {
127 .format = "barrier (%dus)%d>",
128 .format_args = "i4i4",
129 };
130 /* *INDENT-ON* */
131 struct
132 {
133 u32 t_entry, depth;
134 } *ed = 0;
135
136 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
137 ed->t_entry = (int) (1000000.0 * t_entry);
138 ed->depth = (int) vlib_worker_threads[0].recursion_level;
139}
140
141static inline void
142barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
143{
144 /* *INDENT-OFF* */
145 ELOG_TYPE_DECLARE (e) =
146 {
147 .format = "barrier (%dus){%d}(C:%dus)#%d>",
148 .format_args = "i4i4i4i4",
149 };
150 /* *INDENT-ON* */
151 struct
152 {
153 u32 t_entry, t_update_main, t_closed_total, count;
154 } *ed = 0;
155
156 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
157 ed->t_entry = (int) (1000000.0 * t_entry);
158 ed->t_update_main = (int) (1000000.0 * t_update_main);
159 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
160 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
161
162 /* Reset context for next trace */
163 vlib_worker_threads[0].barrier_context = NULL;
164}
165#else
166char barrier_trace[65536];
167char *btp = barrier_trace;
168
169 /*
170 * syslog Barrier trace functions, which are nulled out if BARRIER_TRACING
171 * isn't defined
172 */
173
174
175static inline void
176barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
177{
178 btp += sprintf (btp, "<%u#%s",
179 (unsigned int) vlib_worker_threads[0].barrier_sync_count,
180 vlib_worker_threads[0].barrier_caller);
181
182 if (vlib_worker_threads[0].barrier_context)
183 {
184 btp += sprintf (btp, "[%s]", vlib_worker_threads[0].barrier_context);
185
186 }
187
188 btp += sprintf (btp, "(O:%dus:%dus)(%dus):",
189 (int) (1000000.0 * t_entry),
190 (int) (1000000.0 * t_open), (int) (1000000.0 * t_closed));
191
192}
193
194static inline void
195barrier_trace_sync_rec (f64 t_entry)
196{
197 btp += sprintf (btp, "<%u(%dus)%s:",
198 (int) vlib_worker_threads[0].recursion_level - 1,
199 (int) (1000000.0 * t_entry),
200 vlib_worker_threads[0].barrier_caller);
201}
202
203static inline void
204barrier_trace_release_rec (f64 t_entry)
205{
206 btp += sprintf (btp, ":(%dus)%u>", (int) (1000000.0 * t_entry),
207 (int) vlib_worker_threads[0].recursion_level);
208}
209
210static inline void
211barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
212{
213
214 btp += sprintf (btp, ":(%dus)", (int) (1000000.0 * t_entry));
215 if (t_update_main > 0)
216 {
217 btp += sprintf (btp, "{%dus}", (int) (1000000.0 * t_update_main));
218 }
219
220 btp += sprintf (btp, "(C:%dus)#%u>",
221 (int) (1000000.0 * t_closed_total),
222 (int) vlib_worker_threads[0].barrier_sync_count);
223
224 /* Dump buffer to syslog, and reset for next trace */
225 fformat (stderr, "BTRC %s\n", barrier_trace);
226 btp = barrier_trace;
227 vlib_worker_threads[0].barrier_context = NULL;
228}
229#endif
230#else
231
232 /* Null functions for default case where barrier tracing isn't used */
233static inline void
234barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
235{
236}
237
238static inline void
239barrier_trace_sync_rec (f64 t_entry)
240{
241}
242
243static inline void
244barrier_trace_release_rec (f64 t_entry)
245{
246}
247
248static inline void
249barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
250{
251}
252#endif
253
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254uword
Damjan Marionf55f9b82017-05-10 21:06:28 +0200255os_get_nthreads (void)
Dave Barach01d86c72016-08-08 15:13:42 -0400256{
257 u32 len;
258
259 len = vec_len (vlib_thread_stacks);
260 if (len == 0)
261 return 1;
262 else
263 return len;
264}
265
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266void
267vlib_set_thread_name (char *name)
268{
269 int pthread_setname_np (pthread_t __target_thread, const char *__name);
Dave Barachb2a6e252016-07-27 10:00:58 -0400270 int rv;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400271 pthread_t thread = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
Dave Barach9b8ffd92016-07-08 08:13:45 -0400273 if (thread)
Dave Barachb2a6e252016-07-27 10:00:58 -0400274 {
275 rv = pthread_setname_np (thread, name);
276 if (rv)
Ed Warnicke853e7202016-08-12 11:42:26 -0700277 clib_warning ("pthread_setname_np returned %d", rv);
Dave Barachb2a6e252016-07-27 10:00:58 -0400278 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279}
280
Dave Barach9b8ffd92016-07-08 08:13:45 -0400281static int
282sort_registrations_by_no_clone (void *a0, void *a1)
283{
284 vlib_thread_registration_t **tr0 = a0;
285 vlib_thread_registration_t **tr1 = a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Dave Barach9b8ffd92016-07-08 08:13:45 -0400287 return ((i32) ((*tr0)->no_data_structure_clone)
288 - ((i32) ((*tr1)->no_data_structure_clone)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289}
290
291static uword *
Damjan Marion01914ce2017-09-14 19:04:50 +0200292clib_sysfs_list_to_bitmap (char *filename)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293{
294 FILE *fp;
295 uword *r = 0;
296
297 fp = fopen (filename, "r");
298
299 if (fp != NULL)
300 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400301 u8 *buffer = 0;
302 vec_validate (buffer, 256 - 1);
303 if (fgets ((char *) buffer, 256, fp))
304 {
305 unformat_input_t in;
306 unformat_init_string (&in, (char *) buffer,
307 strlen ((char *) buffer));
Dave Barachb2a6e252016-07-27 10:00:58 -0400308 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
Ed Warnicke853e7202016-08-12 11:42:26 -0700309 clib_warning ("unformat_bitmap_list failed");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400310 unformat_free (&in);
311 }
312 vec_free (buffer);
313 fclose (fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 }
315 return r;
316}
317
318
319/* Called early in the init sequence */
320
321clib_error_t *
322vlib_thread_init (vlib_main_t * vm)
323{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400324 vlib_thread_main_t *tm = &vlib_thread_main;
325 vlib_worker_thread_t *w;
326 vlib_thread_registration_t *tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 u32 n_vlib_mains = 1;
328 u32 first_index = 1;
329 u32 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400330 uword *avail_cpu;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
332 /* get bitmaps of active cpu cores and sockets */
333 tm->cpu_core_bitmap =
Damjan Marion01914ce2017-09-14 19:04:50 +0200334 clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335 tm->cpu_socket_bitmap =
Damjan Marion01914ce2017-09-14 19:04:50 +0200336 clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337
Dave Barach9b8ffd92016-07-08 08:13:45 -0400338 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339
340 /* skip cores */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400341 for (i = 0; i < tm->skip_cores; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343 uword c = clib_bitmap_first_set (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344 if (c == ~0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400345 return clib_error_return (0, "no available cpus to skip");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348 }
349
350 /* grab cpu for main thread */
351 if (!tm->main_lcore)
352 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400353 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
354 if (tm->main_lcore == (u8) ~ 0)
355 return clib_error_return (0, "no available cpus to be used for the"
356 " main thread");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357 }
358 else
359 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
361 return clib_error_return (0, "cpu %u is not available to be used"
362 " for the main thread", tm->main_lcore);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400364 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
366 /* assume that there is socket 0 only if there is no data from sysfs */
367 if (!tm->cpu_socket_bitmap)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400368 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
Christophe Fontainefef15b42016-04-09 12:38:49 +0900370 /* pin main thread to main_lcore */
Damjan Marion878c6092017-01-04 13:19:27 +0100371 if (tm->cb.vlib_thread_set_lcore_cb)
372 {
373 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
374 }
375 else
376 {
377 cpu_set_t cpuset;
378 CPU_ZERO (&cpuset);
379 CPU_SET (tm->main_lcore, &cpuset);
380 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
381 }
Christophe Fontainefef15b42016-04-09 12:38:49 +0900382
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 /* as many threads as stacks... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400384 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
385 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
387 /* Preallocate thread 0 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400388 _vec_len (vlib_worker_threads) = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 w = vlib_worker_threads;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400390 w->thread_mheap = clib_mem_get_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 w->thread_stack = vlib_thread_stacks[0];
Pavel Kotucek98765202016-10-07 08:37:28 +0200392 w->lcore_id = tm->main_lcore;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200394 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 tm->n_vlib_mains = 1;
396
Pavel Kotucek1e765832016-09-23 08:54:14 +0200397 if (tm->sched_policy != ~0)
398 {
399 struct sched_param sched_param;
400 if (!sched_getparam (w->lwp, &sched_param))
401 {
402 if (tm->sched_priority != ~0)
403 sched_param.sched_priority = tm->sched_priority;
404 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
405 }
406 }
407
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 /* assign threads to cores and set n_vlib_mains */
409 tr = tm->next;
410
411 while (tr)
412 {
413 vec_add1 (tm->registrations, tr);
414 tr = tr->next;
415 }
416
Dave Barach9b8ffd92016-07-08 08:13:45 -0400417 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
419 for (i = 0; i < vec_len (tm->registrations); i++)
420 {
421 int j;
422 tr = tm->registrations[i];
423 tr->first_index = first_index;
424 first_index += tr->count;
425 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
426
427 /* construct coremask */
428 if (tr->use_pthreads || !tr->count)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400429 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430
431 if (tr->coremask)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400432 {
433 uword c;
434 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 clib_bitmap_foreach (c, tr->coremask, ({
436 if (clib_bitmap_get(avail_cpu, c) == 0)
437 return clib_error_return (0, "cpu %u is not available to be used"
438 " for the '%s' thread",c, tr->name);
439
440 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
441 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400442/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446 {
447 for (j = 0; j < tr->count; j++)
448 {
449 uword c = clib_bitmap_first_set (avail_cpu);
450 if (c == ~0)
451 return clib_error_return (0,
452 "no available cpus to be used for"
453 " the '%s' thread", tr->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
456 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
457 }
458 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 }
460
Dave Barach9b8ffd92016-07-08 08:13:45 -0400461 clib_bitmap_free (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
463 tm->n_vlib_mains = n_vlib_mains;
464
Dave Barach9b8ffd92016-07-08 08:13:45 -0400465 vec_validate_aligned (vlib_worker_threads, first_index - 1,
466 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468 return 0;
469}
470
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471vlib_frame_queue_t *
472vlib_frame_queue_alloc (int nelts)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474 vlib_frame_queue_t *fq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477 memset (fq, 0, sizeof (*fq));
478 fq->nelts = nelts;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400479 fq->vector_threshold = 128; // packets
480 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481
482 if (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400483 {
484 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
485 fformat (stderr, "WARNING: fq->tail unaligned\n");
486 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
487 fformat (stderr, "WARNING: fq->head unaligned\n");
488 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
489 fformat (stderr, "WARNING: fq->elts unaligned\n");
490
491 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
492 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
493 sizeof (fq->elts[0]));
494 if (nelts & (nelts - 1))
495 {
496 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
497 abort ();
498 }
499 }
500
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501 return (fq);
502}
503
504void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400505void
506vl_msg_api_handler_no_free (void *v)
507{
508}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509
510/* Turned off, save as reference material... */
511#if 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400512static inline int
513vlib_frame_queue_dequeue_internal (int thread_id,
514 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515{
516 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
517 vlib_frame_queue_elt_t *elt;
518 vlib_frame_t *f;
519 vlib_pending_frame_t *p;
520 vlib_node_runtime_t *r;
521 u32 node_runtime_index;
522 int msg_type;
523 u64 before;
524 int processed = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400525
526 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
528 while (1)
529 {
530 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400531 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
Dave Barach9b8ffd92016-07-08 08:13:45 -0400533 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
535 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400536 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537
Dave Barach9b8ffd92016-07-08 08:13:45 -0400538 before = clib_cpu_time_now ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539
540 f = elt->frame;
541 node_runtime_index = elt->node_runtime_index;
542 msg_type = elt->msg_type;
543
544 switch (msg_type)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400545 {
546 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
547 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
548 /* note fallthrough... */
549 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
550 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
551 node_runtime_index);
552 vlib_frame_free (vm, r, f);
553 break;
554 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
555 vec_add2 (vm->node_main.pending_frames, p, 1);
556 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
557 p->node_runtime_index = elt->node_runtime_index;
558 p->frame_index = vlib_frame_index (vm, f);
559 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
560 fq->dequeue_vectors += (u64) f->n_vectors;
561 break;
562 case VLIB_FRAME_QUEUE_ELT_API_MSG:
563 vl_msg_api_handler_no_free (f);
564 break;
565 default:
566 clib_warning ("bogus frame queue message, type %d", msg_type);
567 break;
568 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 elt->valid = 0;
570 fq->dequeues++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400571 fq->dequeue_ticks += clib_cpu_time_now () - before;
572 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 fq->head++;
574 processed++;
575 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400576 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577 return processed;
578}
579
Dave Barach9b8ffd92016-07-08 08:13:45 -0400580int
581vlib_frame_queue_dequeue (int thread_id,
582 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583{
584 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
585}
586
Dave Barach9b8ffd92016-07-08 08:13:45 -0400587int
588vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
589 u32 frame_queue_index, vlib_frame_t * frame,
590 vlib_frame_queue_msg_type_t type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591{
592 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
593 vlib_frame_queue_elt_t *elt;
594 u32 save_count;
595 u64 new_tail;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400596 u64 before = clib_cpu_time_now ();
597
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598 ASSERT (fq);
599
600 new_tail = __sync_add_and_fetch (&fq->tail, 1);
601
602 /* Wait until a ring slot is available */
603 while (new_tail >= fq->head + fq->nelts)
604 {
605 f64 b4 = vlib_time_now_ticks (vm, before);
606 vlib_worker_thread_barrier_check (vm, b4);
607 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
Damjan Marion586afd72017-04-05 19:18:20 +0200608 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609 }
610
Dave Barach9b8ffd92016-07-08 08:13:45 -0400611 elt = fq->elts + (new_tail & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612
613 /* this would be very bad... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400614 while (elt->valid)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615 {
616 }
617
618 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
619 save_count = frame->n_vectors;
620
621 elt->frame = frame;
622 elt->node_runtime_index = node_runtime_index;
623 elt->msg_type = type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400624 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625 elt->valid = 1;
626
627 return save_count;
628}
629#endif /* 0 */
630
631/* To be called by vlib worker threads upon startup */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400632void
633vlib_worker_thread_init (vlib_worker_thread_t * w)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400635 vlib_thread_main_t *tm = vlib_get_thread_main ();
636
Dave Barachfdf49442016-12-20 12:48:14 -0500637 /*
638 * Note: disabling signals in worker threads as follows
639 * prevents the api post-mortem dump scheme from working
640 * {
641 * sigset_t s;
642 * sigfillset (&s);
643 * pthread_sigmask (SIG_SETMASK, &s, 0);
644 * }
645 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
647 clib_mem_set_heap (w->thread_mheap);
648
Dave Barach9b8ffd92016-07-08 08:13:45 -0400649 if (vec_len (tm->thread_prefix) && w->registration->short_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400651 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
652 w->registration->short_name, w->instance_id, '\0');
653 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 }
655
656 if (!w->registration->use_pthreads)
657 {
658
659 /* Initial barrier sync, for both worker and i/o threads */
660 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
661
662 while (*vlib_worker_threads->wait_at_barrier)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400663 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664
665 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
666 }
667}
668
Dave Barach9b8ffd92016-07-08 08:13:45 -0400669void *
670vlib_worker_thread_bootstrap_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671{
672 void *rv;
673 vlib_worker_thread_t *w = arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400674
675 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200676 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677
Damjan Marionf55f9b82017-05-10 21:06:28 +0200678 __os_thread_index = w - vlib_worker_threads;
Damjan Marion586afd72017-04-05 19:18:20 +0200679
Dave Barach9b8ffd92016-07-08 08:13:45 -0400680 rv = (void *) clib_calljmp
681 ((uword (*)(uword)) w->thread_function,
682 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 /* NOTREACHED, we hope */
684 return rv;
685}
686
Damjan Marion878c6092017-01-04 13:19:27 +0100687static clib_error_t *
688vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689{
Damjan Marion878c6092017-01-04 13:19:27 +0100690 vlib_thread_main_t *tm = &vlib_thread_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400691 void *(*fp_arg) (void *) = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692
Pavel Kotucek98765202016-10-07 08:37:28 +0200693 w->lcore_id = lcore_id;
Damjan Marion878c6092017-01-04 13:19:27 +0100694 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
695 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400697 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400698 pthread_t worker;
699 cpu_set_t cpuset;
700 CPU_ZERO (&cpuset);
701 CPU_SET (lcore_id, &cpuset);
Christophe Fontainefef15b42016-04-09 12:38:49 +0900702
Damjan Marion878c6092017-01-04 13:19:27 +0100703 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
704 return clib_error_return_unix (0, "pthread_create");
705
706 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
707 return clib_error_return_unix (0, "pthread_setaffinity_np");
708
709 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400710 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711}
712
Dave Barach9b8ffd92016-07-08 08:13:45 -0400713static clib_error_t *
714start_workers (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715{
716 int i, j;
717 vlib_worker_thread_t *w;
718 vlib_main_t *vm_clone;
719 void *oldheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400720 vlib_thread_main_t *tm = &vlib_thread_main;
721 vlib_thread_registration_t *tr;
722 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723 u32 n_vlib_mains = tm->n_vlib_mains;
724 u32 worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400725 u8 *main_heap = clib_mem_get_per_cpu_heap ();
726 mheap_t *main_heap_header = mheap_header (main_heap);
727
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728 vec_reset_length (vlib_worker_threads);
729
730 /* Set up the main thread */
731 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
Dave Barach3e7deb12016-02-05 16:29:53 -0500732 w->elog_track.name = "main thread";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733 elog_track_register (&vm->elog_main, &w->elog_track);
734
Dave Barach9b8ffd92016-07-08 08:13:45 -0400735 if (vec_len (tm->thread_prefix))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400737 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
738 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739 }
740
Dave Barach9b8ffd92016-07-08 08:13:45 -0400741 /*
Dave Barach848191d2016-04-28 16:24:15 -0400742 * Truth of the matter: we always use at least two
Dave Barach9b8ffd92016-07-08 08:13:45 -0400743 * threads. So, make the main heap thread-safe
Dave Barach848191d2016-04-28 16:24:15 -0400744 * and make the event log thread-safe.
745 */
746 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400747 vm->elog_main.lock =
748 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barach848191d2016-04-28 16:24:15 -0400749 vm->elog_main.lock[0] = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400750
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751 if (n_vlib_mains > 1)
752 {
Dave Barach80f54e22017-03-08 19:08:56 -0500753 /* Replace hand-crafted length-1 vector with a real vector */
754 vlib_mains = 0;
755
756 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
757 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758 _vec_len (vlib_mains) = 0;
Dave Barach80f54e22017-03-08 19:08:56 -0500759 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760
Dave Barach9b8ffd92016-07-08 08:13:45 -0400761 vlib_worker_threads->wait_at_barrier =
762 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763 vlib_worker_threads->workers_at_barrier =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400764 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100766 vlib_worker_threads->node_reforks_required =
767 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
768
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769 /* Ask for an initial barrier sync */
770 *vlib_worker_threads->workers_at_barrier = 0;
771 *vlib_worker_threads->wait_at_barrier = 1;
772
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100773 /* Without update or refork */
774 *vlib_worker_threads->node_reforks_required = 0;
775 vm->need_vlib_worker_thread_node_runtime_update = 0;
776
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100777 /* init timing */
778 vm->barrier_epoch = 0;
779 vm->barrier_no_close_before = 0;
780
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 worker_thread_index = 1;
782
Dave Barach9b8ffd92016-07-08 08:13:45 -0400783 for (i = 0; i < vec_len (tm->registrations); i++)
784 {
785 vlib_node_main_t *nm, *nm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400786 vlib_buffer_free_list_t *fl_clone, *fl_orig;
787 vlib_buffer_free_list_t *orig_freelist_pool;
788 int k;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789
Dave Barach9b8ffd92016-07-08 08:13:45 -0400790 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791
Dave Barach9b8ffd92016-07-08 08:13:45 -0400792 if (tr->count == 0)
793 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700794
Dave Barach9b8ffd92016-07-08 08:13:45 -0400795 for (k = 0; k < tr->count; k++)
796 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100797 vlib_node_t *n;
798
Dave Barach9b8ffd92016-07-08 08:13:45 -0400799 vec_add2 (vlib_worker_threads, w, 1);
800 if (tr->mheap_size)
801 w->thread_mheap =
802 mheap_alloc (0 /* use VM */ , tr->mheap_size);
803 else
804 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200805
806 w->thread_stack =
807 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400808 w->thread_function = tr->function;
809 w->thread_function_arg = w;
810 w->instance_id = k;
811 w->registration = tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812
Dave Barach9b8ffd92016-07-08 08:13:45 -0400813 w->elog_track.name =
814 (char *) format (0, "%s %d", tr->name, k + 1);
815 vec_add1 (w->elog_track.name, 0);
816 elog_track_register (&vm->elog_main, &w->elog_track);
Bud Grise68adab92016-02-12 10:36:11 -0500817
Dave Barach9b8ffd92016-07-08 08:13:45 -0400818 if (tr->no_data_structure_clone)
819 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820
Dave Barach9b8ffd92016-07-08 08:13:45 -0400821 /* Fork vlib_global_main et al. Look for bugs here */
822 oldheap = clib_mem_set_heap (w->thread_mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200824 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
825 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400826 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827
Damjan Marion586afd72017-04-05 19:18:20 +0200828 vm_clone->thread_index = worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400829 vm_clone->heap_base = w->thread_mheap;
830 vm_clone->mbuf_alloc_list = 0;
Damjan Marione9f929b2017-03-16 11:32:09 +0100831 vm_clone->init_functions_called =
832 hash_create (0, /* value bytes */ 0);
Dave Barach2877eee2017-12-15 12:22:57 -0500833 vm_clone->pending_rpc_requests = 0;
834 vec_validate (vm_clone->pending_rpc_requests, 0);
835 _vec_len (vm_clone->pending_rpc_requests) = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400836 memset (&vm_clone->random_buffer, 0,
837 sizeof (vm_clone->random_buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838
Dave Barach9b8ffd92016-07-08 08:13:45 -0400839 nm = &vlib_mains[0]->node_main;
840 nm_clone = &vm_clone->node_main;
841 /* fork next frames array, preserving node runtime indices */
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200842 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
843 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400844 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
845 {
846 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
847 u32 save_node_runtime_index;
848 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850 save_node_runtime_index = nf->node_runtime_index;
851 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
852 vlib_next_frame_init (nf);
853 nf->node_runtime_index = save_node_runtime_index;
854 nf->flags = save_flags;
855 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856
Dave Barach9b8ffd92016-07-08 08:13:45 -0400857 /* fork the frame dispatch queue */
858 nm_clone->pending_frames = 0;
859 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
860 _vec_len (nm_clone->pending_frames) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861
Dave Barach9b8ffd92016-07-08 08:13:45 -0400862 /* fork nodes */
863 nm_clone->nodes = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100864
865 /* Allocate all nodes in single block for speed */
866 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
867
Dave Barach9b8ffd92016-07-08 08:13:45 -0400868 for (j = 0; j < vec_len (nm->nodes); j++)
869 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400870 clib_memcpy (n, nm->nodes[j], sizeof (*n));
871 /* none of the copied nodes have enqueue rights given out */
872 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
873 memset (&n->stats_total, 0, sizeof (n->stats_total));
874 memset (&n->stats_last_clear, 0,
875 sizeof (n->stats_last_clear));
876 vec_add1 (nm_clone->nodes, n);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100877 n++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400878 }
879 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200880 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
881 CLIB_CACHE_LINE_BYTES);
JingLiuZTE30af5da2017-07-24 10:53:31 +0800882 vec_foreach (rt,
883 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
Damjan Marione9f929b2017-03-16 11:32:09 +0100884 {
885 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200886 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100887 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100888 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100889 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100890 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
891 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100892 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893
Dave Barach9b8ffd92016-07-08 08:13:45 -0400894 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200895 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
896 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400897 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
Damjan Marione9f929b2017-03-16 11:32:09 +0100898 {
899 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200900 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100901 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100902 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100903 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100904 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
905 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100906 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200908 nm_clone->processes = vec_dup_aligned (nm->processes,
909 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910
Dave Barach9b8ffd92016-07-08 08:13:45 -0400911 /* zap the (per worker) frame freelists, etc */
912 nm_clone->frame_sizes = 0;
Florin Coras93992a92017-05-24 18:03:56 -0700913 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914
Dave Barach9b8ffd92016-07-08 08:13:45 -0400915 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100916
Dave Barach9b8ffd92016-07-08 08:13:45 -0400917 clib_mem_set_heap (oldheap);
Dave Barach80f54e22017-03-08 19:08:56 -0500918 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200920 vm_clone->error_main.counters = vec_dup_aligned
921 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
922 vm_clone->error_main.counters_last_clear = vec_dup_aligned
923 (vlib_mains[0]->error_main.counters_last_clear,
924 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925
Dave Barach9b8ffd92016-07-08 08:13:45 -0400926 /* Fork the vlib_buffer_main_t free lists, etc. */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100927 orig_freelist_pool = vm_clone->buffer_free_list_pool;
928 vm_clone->buffer_free_list_pool = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400929
930 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 pool_foreach (fl_orig, orig_freelist_pool,
932 ({
Damjan Mariond1274cb2018-03-13 21:32:17 +0100933 pool_get_aligned (vm_clone->buffer_free_list_pool,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934 fl_clone, CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400935 ASSERT (fl_orig - orig_freelist_pool
Damjan Mariond1274cb2018-03-13 21:32:17 +0100936 == fl_clone - vm_clone->buffer_free_list_pool);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937
938 fl_clone[0] = fl_orig[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100939 fl_clone->buffers = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 fl_clone->n_alloc = 0;
941 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400942/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943
Dave Barach9b8ffd92016-07-08 08:13:45 -0400944 worker_thread_index++;
945 }
946 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947 }
948 else
949 {
950 /* only have non-data-structure copy threads to create... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400951 for (i = 0; i < vec_len (tm->registrations); i++)
952 {
953 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954
Dave Barach9b8ffd92016-07-08 08:13:45 -0400955 for (j = 0; j < tr->count; j++)
956 {
957 vec_add2 (vlib_worker_threads, w, 1);
958 if (tr->mheap_size)
959 w->thread_mheap =
960 mheap_alloc (0 /* use VM */ , tr->mheap_size);
961 else
962 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200963 w->thread_stack =
964 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400965 w->thread_function = tr->function;
966 w->thread_function_arg = w;
967 w->instance_id = j;
968 w->elog_track.name =
969 (char *) format (0, "%s %d", tr->name, j + 1);
970 w->registration = tr;
971 vec_add1 (w->elog_track.name, 0);
972 elog_track_register (&vm->elog_main, &w->elog_track);
973 }
974 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975 }
976
977 worker_thread_index = 1;
978
979 for (i = 0; i < vec_len (tm->registrations); i++)
980 {
Damjan Marion878c6092017-01-04 13:19:27 +0100981 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982 int j;
983
984 tr = tm->registrations[i];
985
986 if (tr->use_pthreads || tm->use_pthreads)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400987 {
988 for (j = 0; j < tr->count; j++)
989 {
990 w = vlib_worker_threads + worker_thread_index++;
Damjan Marion878c6092017-01-04 13:19:27 +0100991 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
992 w, 0);
993 if (err)
994 clib_error_report (err);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400995 }
996 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400998 {
999 uword c;
Damjan Marion878c6092017-01-04 13:19:27 +01001000 /* *INDENT-OFF* */
1001 clib_bitmap_foreach (c, tr->coremask, ({
1002 w = vlib_worker_threads + worker_thread_index++;
1003 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
1004 w, c);
1005 if (err)
1006 clib_error_report (err);
1007 }));
1008 /* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001009 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001011 vlib_worker_thread_barrier_sync (vm);
1012 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013 return 0;
1014}
1015
1016VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
1017
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001018
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001019static inline void
1020worker_thread_node_runtime_update_internal (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021{
1022 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023 vlib_main_t *vm;
1024 vlib_node_main_t *nm, *nm_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 vlib_main_t *vm_clone;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001026 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027 never_inline void
1028 vlib_node_runtime_sync_stats (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001029 vlib_node_runtime_t * r,
1030 uword n_calls,
1031 uword n_vectors, uword n_clocks);
1032
Damjan Marion586afd72017-04-05 19:18:20 +02001033 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035 vm = vlib_mains[0];
1036 nm = &vm->node_main;
1037
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
1039
Dave Barach9b8ffd92016-07-08 08:13:45 -04001040 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041 * Scrape all runtime stats, so we don't lose node runtime(s) with
1042 * pending counts, or throw away worker / io thread counts.
1043 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001044 for (j = 0; j < vec_len (nm->nodes); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001046 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047 n = nm->nodes[j];
1048 vlib_node_sync_stats (vm, n);
1049 }
1050
1051 for (i = 1; i < vec_len (vlib_mains); i++)
1052 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001053 vlib_node_t *n;
1054
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055 vm_clone = vlib_mains[i];
1056 nm_clone = &vm_clone->node_main;
1057
Dave Barach9b8ffd92016-07-08 08:13:45 -04001058 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1059 {
1060 n = nm_clone->nodes[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061
Dave Barach9b8ffd92016-07-08 08:13:45 -04001062 rt = vlib_node_get_runtime (vm_clone, n->index);
1063 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1064 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065 }
1066
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001067 /* Per-worker clone rebuilds are now done on each thread */
1068}
1069
1070
1071void
1072vlib_worker_thread_node_refork (void)
1073{
1074 vlib_main_t *vm, *vm_clone;
1075 vlib_node_main_t *nm, *nm_clone;
1076 vlib_node_t **old_nodes_clone;
1077 vlib_node_runtime_t *rt, *old_rt;
1078
1079 vlib_node_t *new_n_clone;
1080
1081 int j;
1082
1083 vm = vlib_mains[0];
1084 nm = &vm->node_main;
1085 vm_clone = vlib_get_main ();
1086 nm_clone = &vm_clone->node_main;
1087
1088 /* Re-clone error heap */
1089 u64 *old_counters = vm_clone->error_main.counters;
1090 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1091
1092 clib_memcpy (&vm_clone->error_main, &vm->error_main,
1093 sizeof (vm->error_main));
1094 j = vec_len (vm->error_main.counters) - 1;
1095 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
1096 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
1097 vm_clone->error_main.counters = old_counters;
1098 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1099
1100 nm_clone = &vm_clone->node_main;
1101 vec_free (nm_clone->next_frames);
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001102 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1103 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001104
1105 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001107 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1108 u32 save_node_runtime_index;
1109 u32 save_flags;
Damjan Marionbc20bdf2015-12-17 14:28:18 +01001110
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001111 save_node_runtime_index = nf->node_runtime_index;
1112 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1113 vlib_next_frame_init (nf);
1114 nf->node_runtime_index = save_node_runtime_index;
1115 nf->flags = save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001117
1118 old_nodes_clone = nm_clone->nodes;
1119 nm_clone->nodes = 0;
1120
1121 /* re-fork nodes */
1122
1123 /* Allocate all nodes in single block for speed */
1124 new_n_clone =
1125 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1126 for (j = 0; j < vec_len (nm->nodes); j++)
1127 {
1128 vlib_node_t *old_n_clone;
1129 vlib_node_t *new_n;
1130
1131 new_n = nm->nodes[j];
1132 old_n_clone = old_nodes_clone[j];
1133
1134 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
1135 /* none of the copied nodes have enqueue rights given out */
1136 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1137
1138 if (j >= vec_len (old_nodes_clone))
1139 {
1140 /* new node, set to zero */
1141 memset (&new_n_clone->stats_total, 0,
1142 sizeof (new_n_clone->stats_total));
1143 memset (&new_n_clone->stats_last_clear, 0,
1144 sizeof (new_n_clone->stats_last_clear));
1145 }
1146 else
1147 {
1148 /* Copy stats if the old data is valid */
1149 clib_memcpy (&new_n_clone->stats_total,
1150 &old_n_clone->stats_total,
1151 sizeof (new_n_clone->stats_total));
1152 clib_memcpy (&new_n_clone->stats_last_clear,
1153 &old_n_clone->stats_last_clear,
1154 sizeof (new_n_clone->stats_last_clear));
1155
1156 /* keep previous node state */
1157 new_n_clone->state = old_n_clone->state;
1158 }
1159 vec_add1 (nm_clone->nodes, new_n_clone);
1160 new_n_clone++;
1161 }
1162 /* Free the old node clones */
1163 clib_mem_free (old_nodes_clone[0]);
1164
1165 vec_free (old_nodes_clone);
1166
1167
1168 /* re-clone internal nodes */
1169 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1170 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001171 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1172 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001173
1174 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1175 {
1176 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1177 rt->thread_index = vm_clone->thread_index;
1178 /* copy runtime_data, will be overwritten later for existing rt */
1179 if (n->runtime_data && n->runtime_data_bytes > 0)
1180 clib_memcpy (rt->runtime_data, n->runtime_data,
1181 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1182 n->runtime_data_bytes));
1183 }
1184
1185 for (j = 0; j < vec_len (old_rt); j++)
1186 {
1187 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1188 rt->state = old_rt[j].state;
1189 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
1190 VLIB_NODE_RUNTIME_DATA_SIZE);
1191 }
1192
1193 vec_free (old_rt);
1194
1195 /* re-clone input nodes */
1196 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1197 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001198 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1199 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001200
1201 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1202 {
1203 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1204 rt->thread_index = vm_clone->thread_index;
1205 /* copy runtime_data, will be overwritten later for existing rt */
1206 if (n->runtime_data && n->runtime_data_bytes > 0)
1207 clib_memcpy (rt->runtime_data, n->runtime_data,
1208 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1209 n->runtime_data_bytes));
1210 }
1211
1212 for (j = 0; j < vec_len (old_rt); j++)
1213 {
1214 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1215 rt->state = old_rt[j].state;
1216 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
1217 VLIB_NODE_RUNTIME_DATA_SIZE);
1218 }
1219
1220 vec_free (old_rt);
1221
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001222 nm_clone->processes = vec_dup_aligned (nm->processes,
1223 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001224}
1225
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001226void
1227vlib_worker_thread_node_runtime_update (void)
1228{
1229 /*
1230 * Make a note that we need to do a node runtime update
1231 * prior to releasing the barrier.
1232 */
1233 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001234}
1235
Pavel Kotucek1e765832016-09-23 08:54:14 +02001236u32
1237unformat_sched_policy (unformat_input_t * input, va_list * args)
1238{
1239 u32 *r = va_arg (*args, u32 *);
1240
1241 if (0);
1242#define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1243 foreach_sched_policy
1244#undef _
1245 else
1246 return 0;
1247 return 1;
1248}
1249
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250static clib_error_t *
1251cpu_config (vlib_main_t * vm, unformat_input_t * input)
1252{
1253 vlib_thread_registration_t *tr;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001254 uword *p;
1255 vlib_thread_main_t *tm = &vlib_thread_main;
1256 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001257 u64 coremask;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001258 uword *bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259 u32 count;
1260
1261 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
Pavel Kotucek1e765832016-09-23 08:54:14 +02001262
Dave Barach9b8ffd92016-07-08 08:13:45 -04001263 tm->n_thread_stacks = 1; /* account for main thread */
Pavel Kotucek1e765832016-09-23 08:54:14 +02001264 tm->sched_policy = ~0;
1265 tm->sched_priority = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266
1267 tr = tm->next;
1268
1269 while (tr)
1270 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001271 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001272 tr = tr->next;
1273 }
1274
Dave Barach9b8ffd92016-07-08 08:13:45 -04001275 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276 {
Damjan Marionbf741472016-06-13 22:49:44 +02001277 if (unformat (input, "use-pthreads"))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001278 tm->use_pthreads = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001279 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001280 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001281 else if (unformat (input, "main-core %u", &tm->main_lcore))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001282 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001284 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001285 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001286 {
1287 p = hash_get_mem (tm->thread_registrations_by_name, name);
1288 if (p == 0)
1289 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290
Dave Barach9b8ffd92016-07-08 08:13:45 -04001291 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001292
Dave Barach9b8ffd92016-07-08 08:13:45 -04001293 if (tr->use_pthreads)
1294 return clib_error_return (0,
1295 "coremask cannot be set for '%s' threads",
1296 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297
Dave Barach9b8ffd92016-07-08 08:13:45 -04001298 tr->coremask = clib_bitmap_set_multiple
1299 (tr->coremask, 0, coremask, BITS (coremask));
1300 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1301 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001303 &bitmap))
1304 {
1305 p = hash_get_mem (tm->thread_registrations_by_name, name);
1306 if (p == 0)
1307 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001308
Dave Barach9b8ffd92016-07-08 08:13:45 -04001309 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001310
Dave Barach9b8ffd92016-07-08 08:13:45 -04001311 if (tr->use_pthreads)
1312 return clib_error_return (0,
1313 "corelist cannot be set for '%s' threads",
1314 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315
Dave Barach9b8ffd92016-07-08 08:13:45 -04001316 tr->coremask = bitmap;
1317 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1318 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001319 else
1320 if (unformat
1321 (input, "scheduler-policy %U", unformat_sched_policy,
1322 &tm->sched_policy))
1323 ;
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001324 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
Pavel Kotucek1e765832016-09-23 08:54:14 +02001325 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001326 else if (unformat (input, "%s %u", &name, &count))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001327 {
1328 p = hash_get_mem (tm->thread_registrations_by_name, name);
1329 if (p == 0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001330 return clib_error_return (0, "no such thread type 3 '%s'", name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001331
1332 tr = (vlib_thread_registration_t *) p[0];
1333 if (tr->fixed_count)
1334 return clib_error_return
1335 (0, "number of %s threads not configurable", tr->name);
1336 tr->count = count;
1337 }
1338 else
1339 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001340 }
1341
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001342 if (tm->sched_priority != ~0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001343 {
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001344 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001345 {
1346 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1347 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1348 if (tm->sched_priority > prio_max)
1349 tm->sched_priority = prio_max;
1350 if (tm->sched_priority < prio_min)
1351 tm->sched_priority = prio_min;
1352 }
1353 else
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001354 {
1355 return clib_error_return
1356 (0,
1357 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1358 tm->sched_priority);
1359 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001360 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001361 tr = tm->next;
1362
1363 if (!tm->thread_prefix)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001364 tm->thread_prefix = format (0, "vpp");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365
1366 while (tr)
1367 {
1368 tm->n_thread_stacks += tr->count;
1369 tm->n_pthreads += tr->count * tr->use_pthreads;
Damjan Marion878c6092017-01-04 13:19:27 +01001370 tm->n_threads += tr->count * (tr->use_pthreads == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371 tr = tr->next;
1372 }
1373
1374 return 0;
1375}
1376
1377VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1378
Damjan Marion7bee80c2017-04-26 15:32:12 +02001379#if !defined (__x86_64__) && !defined (__i386__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001380void
1381__sync_fetch_and_add_8 (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001382{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001383 fformat (stderr, "%s called\n", __FUNCTION__);
1384 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001385}
Dave Barach9b8ffd92016-07-08 08:13:45 -04001386
1387void
1388__sync_add_and_fetch_8 (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001389{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001390 fformat (stderr, "%s called\n", __FUNCTION__);
1391 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001392}
1393#endif
1394
1395void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -04001396void
1397vnet_main_fixup (vlib_fork_fixup_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001398{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001399}
1400
1401void
1402vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1403{
1404 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001405
1406 if (vlib_mains == 0)
1407 return;
1408
Damjan Marion586afd72017-04-05 19:18:20 +02001409 ASSERT (vlib_get_thread_index () == 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001410 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001411
1412 switch (which)
1413 {
1414 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1415 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1416 break;
1417
1418 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001419 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001420 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001421 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001422}
1423
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001424 /*
1425 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1426 * based on a test based heuristic that barrier should be open for at least
1427 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1428 * point it is probably too late to make a difference)
1429 */
1430
1431#ifndef BARRIER_MINIMUM_OPEN_LIMIT
1432#define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1433#endif
1434
1435#ifndef BARRIER_MINIMUM_OPEN_FACTOR
1436#define BARRIER_MINIMUM_OPEN_FACTOR 3
1437#endif
1438
Dave Barach9b8ffd92016-07-08 08:13:45 -04001439void
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001440vlib_worker_thread_barrier_sync_int (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001441{
1442 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001443 f64 now;
1444 f64 t_entry;
1445 f64 t_open;
1446 f64 t_closed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447 u32 count;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001448
Dave Barach80f54e22017-03-08 19:08:56 -05001449 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001450 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001452 ASSERT (vlib_get_thread_index () == 0);
1453
Ed Warnickecb9cada2015-12-08 15:45:58 -07001454 count = vec_len (vlib_mains) - 1;
1455
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001456 /* Record entry relative to last close */
1457 now = vlib_time_now (vm);
1458 t_entry = now - vm->barrier_epoch;
1459
Ed Warnickecb9cada2015-12-08 15:45:58 -07001460 /* Tolerate recursive calls */
1461 if (++vlib_worker_threads[0].recursion_level > 1)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001462 {
1463 barrier_trace_sync_rec (t_entry);
1464 return;
1465 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001466
Bud Grise42f20062016-03-16 13:09:46 -04001467 vlib_worker_threads[0].barrier_sync_count++;
1468
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001469 /* Enforce minimum barrier open time to minimize packet loss */
1470 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1471 while ((now = vlib_time_now (vm)) < vm->barrier_no_close_before)
1472 ;
1473
1474 /* Record time of closure */
1475 t_open = now - vm->barrier_epoch;
1476 vm->barrier_epoch = now;
1477
1478 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001479
1480 *vlib_worker_threads->wait_at_barrier = 1;
1481 while (*vlib_worker_threads->workers_at_barrier != count)
1482 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001483 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001484 {
1485 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1486 os_panic ();
1487 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001488 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001489
1490 t_closed = now - vm->barrier_epoch;
1491
1492 barrier_trace_sync (t_entry, t_open, t_closed);
1493
Ed Warnickecb9cada2015-12-08 15:45:58 -07001494}
1495
Dave Barach9b8ffd92016-07-08 08:13:45 -04001496void
1497vlib_worker_thread_barrier_release (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001498{
1499 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001500 f64 now;
1501 f64 minimum_open;
1502 f64 t_entry;
1503 f64 t_closed_total;
1504 f64 t_update_main = 0.0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001505 int refork_needed = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001506
Dave Barach80f54e22017-03-08 19:08:56 -05001507 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001508 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001509
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001510 ASSERT (vlib_get_thread_index () == 0);
1511
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001512
1513 now = vlib_time_now (vm);
1514 t_entry = now - vm->barrier_epoch;
1515
Ed Warnickecb9cada2015-12-08 15:45:58 -07001516 if (--vlib_worker_threads[0].recursion_level > 0)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001517 {
1518 barrier_trace_release_rec (t_entry);
1519 return;
1520 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001521
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001522 /* Update (all) node runtimes before releasing the barrier, if needed */
1523 if (vm->need_vlib_worker_thread_node_runtime_update)
1524 {
1525 /* Do stats elements on main thread */
1526 worker_thread_node_runtime_update_internal ();
1527 vm->need_vlib_worker_thread_node_runtime_update = 0;
1528
1529 /* Do per thread rebuilds in parallel */
1530 refork_needed = 1;
1531 clib_smp_atomic_add (vlib_worker_threads->node_reforks_required,
1532 (vec_len (vlib_mains) - 1));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001533 now = vlib_time_now (vm);
1534 t_update_main = now - vm->barrier_epoch;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001535 }
1536
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001537 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001538
1539 *vlib_worker_threads->wait_at_barrier = 0;
1540
1541 while (*vlib_worker_threads->workers_at_barrier > 0)
1542 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001543 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001544 {
1545 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1546 os_panic ();
1547 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001548 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001549
1550 /* Wait for reforks before continuing */
1551 if (refork_needed)
1552 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001553 now = vlib_time_now (vm);
1554
1555 deadline = now + BARRIER_SYNC_TIMEOUT;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001556
1557 while (*vlib_worker_threads->node_reforks_required > 0)
1558 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001559 if ((now = vlib_time_now (vm)) > deadline)
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001560 {
1561 fformat (stderr, "%s: worker thread refork deadlock\n",
1562 __FUNCTION__);
1563 os_panic ();
1564 }
1565 }
1566 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001567
1568 t_closed_total = now - vm->barrier_epoch;
1569
1570 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1571
1572 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1573 {
1574 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1575 }
1576
1577 vm->barrier_no_close_before = now + minimum_open;
1578
1579 /* Record barrier epoch (used to enforce minimum open time) */
1580 vm->barrier_epoch = now;
1581
1582 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1583
Ed Warnickecb9cada2015-12-08 15:45:58 -07001584}
1585
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001586/*
1587 * Check the frame queue to see if any frames are available.
Dave Barach9b8ffd92016-07-08 08:13:45 -04001588 * If so, pull the packets off the frames and put them to
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001589 * the handoff node.
1590 */
Damjan Marione9d52d52017-03-09 15:42:26 +01001591int
1592vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001593{
Damjan Marion586afd72017-04-05 19:18:20 +02001594 u32 thread_id = vm->thread_index;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001595 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001596 vlib_frame_queue_elt_t *elt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001597 u32 *from, *to;
1598 vlib_frame_t *f;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001599 int msg_type;
1600 int processed = 0;
1601 u32 n_left_to_node;
1602 u32 vectors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001603
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001604 ASSERT (fq);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001605 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001607 if (PREDICT_FALSE (fqm->node_index == ~0))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001608 return 0;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001609 /*
1610 * Gather trace data for frame queues
1611 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001612 if (PREDICT_FALSE (fq->trace))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001613 {
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001614 frame_queue_trace_t *fqt;
1615 frame_queue_nelt_counter_t *fqh;
1616 u32 elix;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001617
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001618 fqt = &fqm->frame_queue_traces[thread_id];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001619
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001620 fqt->nelts = fq->nelts;
1621 fqt->head = fq->head;
1622 fqt->head_hint = fq->head_hint;
1623 fqt->tail = fq->tail;
1624 fqt->threshold = fq->vector_threshold;
1625 fqt->n_in_use = fqt->tail - fqt->head;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001626 if (fqt->n_in_use >= fqt->nelts)
1627 {
1628 // if beyond max then use max
1629 fqt->n_in_use = fqt->nelts - 1;
1630 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001631
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001632 /* Record the number of elements in use in the histogram */
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001633 fqh = &fqm->frame_queue_histogram[thread_id];
Dave Barach9b8ffd92016-07-08 08:13:45 -04001634 fqh->count[fqt->n_in_use]++;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001635
1636 /* Record a snapshot of the elements in use */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001637 for (elix = 0; elix < fqt->nelts; elix++)
1638 {
1639 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1640 if (1 || elt->valid)
1641 {
1642 fqt->n_vectors[elix] = elt->n_vectors;
1643 }
1644 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001645 fqt->written = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001646 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001647
1648 while (1)
1649 {
1650 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001651 {
1652 fq->head_hint = fq->head;
1653 return processed;
1654 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001655
Dave Barach9b8ffd92016-07-08 08:13:45 -04001656 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001657
1658 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001659 {
1660 fq->head_hint = fq->head;
1661 return processed;
1662 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001663
1664 from = elt->buffer_index;
1665 msg_type = elt->msg_type;
1666
1667 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1668 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1669
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001670 f = vlib_get_frame_to_node (vm, fqm->node_index);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001671
1672 to = vlib_frame_vector_args (f);
1673
1674 n_left_to_node = elt->n_vectors;
1675
1676 while (n_left_to_node >= 4)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001677 {
1678 to[0] = from[0];
1679 to[1] = from[1];
1680 to[2] = from[2];
1681 to[3] = from[3];
1682 to += 4;
1683 from += 4;
1684 n_left_to_node -= 4;
1685 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001686
1687 while (n_left_to_node > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001688 {
1689 to[0] = from[0];
1690 to++;
1691 from++;
1692 n_left_to_node--;
1693 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001694
1695 vectors += elt->n_vectors;
1696 f->n_vectors = elt->n_vectors;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001697 vlib_put_frame_to_node (vm, fqm->node_index, f);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001698
1699 elt->valid = 0;
1700 elt->n_vectors = 0;
1701 elt->msg_type = 0xfefefefe;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001702 CLIB_MEMORY_BARRIER ();
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001703 fq->head++;
1704 processed++;
1705
1706 /*
1707 * Limit the number of packets pushed into the graph
1708 */
1709 if (vectors >= fq->vector_threshold)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001710 {
1711 fq->head_hint = fq->head;
1712 return processed;
1713 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001714 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001715 ASSERT (0);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001716 return processed;
1717}
1718
Dave Barach9b8ffd92016-07-08 08:13:45 -04001719void
1720vlib_worker_thread_fn (void *arg)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001721{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001722 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
Damjan Marion878c6092017-01-04 13:19:27 +01001723 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -04001724 vlib_main_t *vm = vlib_get_main ();
Damjan Marione9f929b2017-03-16 11:32:09 +01001725 clib_error_t *e;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001726
Damjan Marion586afd72017-04-05 19:18:20 +02001727 ASSERT (vm->thread_index == vlib_get_thread_index ());
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001728
1729 vlib_worker_thread_init (w);
1730 clib_time_init (&vm->clib_time);
1731 clib_mem_set_heap (w->thread_mheap);
1732
Damjan Marioneb743fa2017-03-20 16:34:15 +01001733 /* Wait until the dpdk init sequence is complete */
1734 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1735 vlib_worker_thread_barrier_check ();
1736
Damjan Marione9f929b2017-03-16 11:32:09 +01001737 e = vlib_call_init_exit_functions
1738 (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1739 if (e)
1740 clib_error_report (e);
1741
Damjan Marione9d52d52017-03-09 15:42:26 +01001742 vlib_worker_loop (vm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001743}
1744
Dave Barach9b8ffd92016-07-08 08:13:45 -04001745/* *INDENT-OFF* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001746VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1747 .name = "workers",
1748 .short_name = "wk",
1749 .function = vlib_worker_thread_fn,
1750};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001751/* *INDENT-ON* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001752
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001753u32
1754vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1755{
1756 vlib_thread_main_t *tm = vlib_get_thread_main ();
1757 vlib_frame_queue_main_t *fqm;
1758 vlib_frame_queue_t *fq;
1759 int i;
1760
1761 if (frame_queue_nelts == 0)
1762 frame_queue_nelts = FRAME_QUEUE_NELTS;
1763
1764 vec_add2 (tm->frame_queue_mains, fqm, 1);
1765
1766 fqm->node_index = node_index;
1767
1768 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1769 _vec_len (fqm->vlib_frame_queues) = 0;
1770 for (i = 0; i < tm->n_vlib_mains; i++)
1771 {
1772 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1773 vec_add1 (fqm->vlib_frame_queues, fq);
1774 }
1775
1776 return (fqm - tm->frame_queue_mains);
1777}
1778
Damjan Marion878c6092017-01-04 13:19:27 +01001779int
1780vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1781{
1782 vlib_thread_main_t *tm = vlib_get_thread_main ();
1783
1784 if (tm->extern_thread_mgmt)
1785 return -1;
1786
1787 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1788 tm->extern_thread_mgmt = 1;
1789 return 0;
1790}
1791
Dave Barach69128d02017-09-26 10:54:34 -04001792void
1793vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1794 args)
1795{
1796 ASSERT (vlib_get_thread_index () == 0);
1797 vlib_process_signal_event (vlib_get_main (), args->node_index,
1798 args->type_opaque, args->data);
1799}
1800
1801void *rpc_call_main_thread_cb_fn;
1802
1803void
1804vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1805{
1806 if (rpc_call_main_thread_cb_fn)
1807 {
1808 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1809 (*fp) (callback, args, arg_size);
1810 }
1811 else
1812 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1813}
1814
Dave Barach9b8ffd92016-07-08 08:13:45 -04001815clib_error_t *
1816threads_init (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001817{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001818 return 0;
1819}
1820
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001821VLIB_INIT_FUNCTION (threads_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001822
1823/*
1824 * fd.io coding-style-patch-verification: ON
1825 *
1826 * Local Variables:
1827 * eval: (c-set-style "gnu")
1828 * End:
1829 */