blob: a946326f2396945552747afa6ca0e12d508cc0b0 [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 */
Damjan Marion858151f2018-07-11 10:51:00 +0200351 if (tm->main_lcore == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 {
Damjan Marion858151f2018-07-11 10:51:00 +0200353 /* if main-lcore is not set, we try to use lcore 1 */
354 if (clib_bitmap_get (avail_cpu, 1))
355 tm->main_lcore = 1;
356 else
357 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400358 if (tm->main_lcore == (u8) ~ 0)
359 return clib_error_return (0, "no available cpus to be used for the"
360 " main thread");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 }
362 else
363 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400364 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
365 return clib_error_return (0, "cpu %u is not available to be used"
366 " for the main thread", tm->main_lcore);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400368 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
370 /* assume that there is socket 0 only if there is no data from sysfs */
371 if (!tm->cpu_socket_bitmap)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400372 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Christophe Fontainefef15b42016-04-09 12:38:49 +0900374 /* pin main thread to main_lcore */
Damjan Marion878c6092017-01-04 13:19:27 +0100375 if (tm->cb.vlib_thread_set_lcore_cb)
376 {
377 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
378 }
Damjan Marion858151f2018-07-11 10:51:00 +0200379 else
380 {
381 cpu_set_t cpuset;
382 CPU_ZERO (&cpuset);
383 CPU_SET (tm->main_lcore, &cpuset);
384 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
385 }
Christophe Fontainefef15b42016-04-09 12:38:49 +0900386
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 /* as many threads as stacks... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400388 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
389 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
391 /* Preallocate thread 0 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400392 _vec_len (vlib_worker_threads) = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 w = vlib_worker_threads;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400394 w->thread_mheap = clib_mem_get_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 w->thread_stack = vlib_thread_stacks[0];
Pavel Kotucek98765202016-10-07 08:37:28 +0200396 w->lcore_id = tm->main_lcore;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400397 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200398 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 tm->n_vlib_mains = 1;
400
Pavel Kotucek1e765832016-09-23 08:54:14 +0200401 if (tm->sched_policy != ~0)
402 {
403 struct sched_param sched_param;
404 if (!sched_getparam (w->lwp, &sched_param))
405 {
406 if (tm->sched_priority != ~0)
407 sched_param.sched_priority = tm->sched_priority;
408 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
409 }
410 }
411
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 /* assign threads to cores and set n_vlib_mains */
413 tr = tm->next;
414
415 while (tr)
416 {
417 vec_add1 (tm->registrations, tr);
418 tr = tr->next;
419 }
420
Dave Barach9b8ffd92016-07-08 08:13:45 -0400421 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
423 for (i = 0; i < vec_len (tm->registrations); i++)
424 {
425 int j;
426 tr = tm->registrations[i];
427 tr->first_index = first_index;
428 first_index += tr->count;
429 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
430
431 /* construct coremask */
432 if (tr->use_pthreads || !tr->count)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400433 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
435 if (tr->coremask)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400436 {
437 uword c;
438 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 clib_bitmap_foreach (c, tr->coremask, ({
440 if (clib_bitmap_get(avail_cpu, c) == 0)
441 return clib_error_return (0, "cpu %u is not available to be used"
442 " for the '%s' thread",c, tr->name);
443
444 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
445 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
Dave Barach9b8ffd92016-07-08 08:13:45 -0400448 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450 {
451 for (j = 0; j < tr->count; j++)
452 {
453 uword c = clib_bitmap_first_set (avail_cpu);
454 if (c == ~0)
455 return clib_error_return (0,
456 "no available cpus to be used for"
457 " the '%s' thread", tr->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458
Dave Barach9b8ffd92016-07-08 08:13:45 -0400459 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
460 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
461 }
462 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463 }
464
Dave Barach9b8ffd92016-07-08 08:13:45 -0400465 clib_bitmap_free (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466
467 tm->n_vlib_mains = n_vlib_mains;
468
Dave Barach9b8ffd92016-07-08 08:13:45 -0400469 vec_validate_aligned (vlib_worker_threads, first_index - 1,
470 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 return 0;
473}
474
Dave Barach9b8ffd92016-07-08 08:13:45 -0400475vlib_frame_queue_t *
476vlib_frame_queue_alloc (int nelts)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400478 vlib_frame_queue_t *fq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479
Dave Barach9b8ffd92016-07-08 08:13:45 -0400480 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 memset (fq, 0, sizeof (*fq));
482 fq->nelts = nelts;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400483 fq->vector_threshold = 128; // packets
484 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485
486 if (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400487 {
488 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
489 fformat (stderr, "WARNING: fq->tail unaligned\n");
490 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
491 fformat (stderr, "WARNING: fq->head unaligned\n");
492 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
493 fformat (stderr, "WARNING: fq->elts unaligned\n");
494
495 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
496 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
497 sizeof (fq->elts[0]));
498 if (nelts & (nelts - 1))
499 {
500 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
501 abort ();
502 }
503 }
504
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 return (fq);
506}
507
508void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400509void
510vl_msg_api_handler_no_free (void *v)
511{
512}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513
514/* Turned off, save as reference material... */
515#if 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400516static inline int
517vlib_frame_queue_dequeue_internal (int thread_id,
518 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519{
520 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
521 vlib_frame_queue_elt_t *elt;
522 vlib_frame_t *f;
523 vlib_pending_frame_t *p;
524 vlib_node_runtime_t *r;
525 u32 node_runtime_index;
526 int msg_type;
527 u64 before;
528 int processed = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529
530 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
532 while (1)
533 {
534 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400535 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
Dave Barach9b8ffd92016-07-08 08:13:45 -0400537 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538
539 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400540 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
Dave Barach9b8ffd92016-07-08 08:13:45 -0400542 before = clib_cpu_time_now ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
544 f = elt->frame;
545 node_runtime_index = elt->node_runtime_index;
546 msg_type = elt->msg_type;
547
548 switch (msg_type)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400549 {
550 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
551 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
552 /* note fallthrough... */
553 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
554 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
555 node_runtime_index);
556 vlib_frame_free (vm, r, f);
557 break;
558 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
559 vec_add2 (vm->node_main.pending_frames, p, 1);
560 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
561 p->node_runtime_index = elt->node_runtime_index;
562 p->frame_index = vlib_frame_index (vm, f);
563 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
564 fq->dequeue_vectors += (u64) f->n_vectors;
565 break;
566 case VLIB_FRAME_QUEUE_ELT_API_MSG:
567 vl_msg_api_handler_no_free (f);
568 break;
569 default:
570 clib_warning ("bogus frame queue message, type %d", msg_type);
571 break;
572 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 elt->valid = 0;
574 fq->dequeues++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400575 fq->dequeue_ticks += clib_cpu_time_now () - before;
576 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577 fq->head++;
578 processed++;
579 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400580 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581 return processed;
582}
583
Dave Barach9b8ffd92016-07-08 08:13:45 -0400584int
585vlib_frame_queue_dequeue (int thread_id,
586 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587{
588 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
589}
590
Dave Barach9b8ffd92016-07-08 08:13:45 -0400591int
592vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
593 u32 frame_queue_index, vlib_frame_t * frame,
594 vlib_frame_queue_msg_type_t type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595{
596 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
597 vlib_frame_queue_elt_t *elt;
598 u32 save_count;
599 u64 new_tail;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400600 u64 before = clib_cpu_time_now ();
601
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602 ASSERT (fq);
603
604 new_tail = __sync_add_and_fetch (&fq->tail, 1);
605
606 /* Wait until a ring slot is available */
607 while (new_tail >= fq->head + fq->nelts)
608 {
609 f64 b4 = vlib_time_now_ticks (vm, before);
610 vlib_worker_thread_barrier_check (vm, b4);
611 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
Damjan Marion586afd72017-04-05 19:18:20 +0200612 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613 }
614
Dave Barach9b8ffd92016-07-08 08:13:45 -0400615 elt = fq->elts + (new_tail & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616
617 /* this would be very bad... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400618 while (elt->valid)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619 {
620 }
621
622 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
623 save_count = frame->n_vectors;
624
625 elt->frame = frame;
626 elt->node_runtime_index = node_runtime_index;
627 elt->msg_type = type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400628 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629 elt->valid = 1;
630
631 return save_count;
632}
633#endif /* 0 */
634
635/* To be called by vlib worker threads upon startup */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400636void
637vlib_worker_thread_init (vlib_worker_thread_t * w)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400639 vlib_thread_main_t *tm = vlib_get_thread_main ();
640
Dave Barachfdf49442016-12-20 12:48:14 -0500641 /*
642 * Note: disabling signals in worker threads as follows
643 * prevents the api post-mortem dump scheme from working
644 * {
645 * sigset_t s;
646 * sigfillset (&s);
647 * pthread_sigmask (SIG_SETMASK, &s, 0);
648 * }
649 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650
651 clib_mem_set_heap (w->thread_mheap);
652
Dave Barach9b8ffd92016-07-08 08:13:45 -0400653 if (vec_len (tm->thread_prefix) && w->registration->short_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
656 w->registration->short_name, w->instance_id, '\0');
657 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658 }
659
660 if (!w->registration->use_pthreads)
661 {
662
663 /* Initial barrier sync, for both worker and i/o threads */
664 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
665
666 while (*vlib_worker_threads->wait_at_barrier)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400667 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
669 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
670 }
671}
672
Dave Barach9b8ffd92016-07-08 08:13:45 -0400673void *
674vlib_worker_thread_bootstrap_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675{
676 void *rv;
677 vlib_worker_thread_t *w = arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400678
679 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200680 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681
Damjan Marionf55f9b82017-05-10 21:06:28 +0200682 __os_thread_index = w - vlib_worker_threads;
Damjan Marion586afd72017-04-05 19:18:20 +0200683
Dave Barach9b8ffd92016-07-08 08:13:45 -0400684 rv = (void *) clib_calljmp
685 ((uword (*)(uword)) w->thread_function,
686 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687 /* NOTREACHED, we hope */
688 return rv;
689}
690
Damjan Marion878c6092017-01-04 13:19:27 +0100691static clib_error_t *
692vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693{
Damjan Marion878c6092017-01-04 13:19:27 +0100694 vlib_thread_main_t *tm = &vlib_thread_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400695 void *(*fp_arg) (void *) = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696
Pavel Kotucek98765202016-10-07 08:37:28 +0200697 w->lcore_id = lcore_id;
Damjan Marion878c6092017-01-04 13:19:27 +0100698 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
699 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400701 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400702 pthread_t worker;
703 cpu_set_t cpuset;
704 CPU_ZERO (&cpuset);
705 CPU_SET (lcore_id, &cpuset);
Christophe Fontainefef15b42016-04-09 12:38:49 +0900706
Damjan Marion878c6092017-01-04 13:19:27 +0100707 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
708 return clib_error_return_unix (0, "pthread_create");
709
710 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
711 return clib_error_return_unix (0, "pthread_setaffinity_np");
712
713 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400714 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715}
716
Dave Barach9b8ffd92016-07-08 08:13:45 -0400717static clib_error_t *
718start_workers (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719{
720 int i, j;
721 vlib_worker_thread_t *w;
722 vlib_main_t *vm_clone;
723 void *oldheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400724 vlib_thread_main_t *tm = &vlib_thread_main;
725 vlib_thread_registration_t *tr;
726 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727 u32 n_vlib_mains = tm->n_vlib_mains;
728 u32 worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400729 u8 *main_heap = clib_mem_get_per_cpu_heap ();
730 mheap_t *main_heap_header = mheap_header (main_heap);
731
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732 vec_reset_length (vlib_worker_threads);
733
734 /* Set up the main thread */
735 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
Dave Barach3e7deb12016-02-05 16:29:53 -0500736 w->elog_track.name = "main thread";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737 elog_track_register (&vm->elog_main, &w->elog_track);
738
Dave Barach9b8ffd92016-07-08 08:13:45 -0400739 if (vec_len (tm->thread_prefix))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400741 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
742 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743 }
744
Dave Barach9b8ffd92016-07-08 08:13:45 -0400745 /*
Dave Barach848191d2016-04-28 16:24:15 -0400746 * Truth of the matter: we always use at least two
Dave Barach9b8ffd92016-07-08 08:13:45 -0400747 * threads. So, make the main heap thread-safe
Dave Barach848191d2016-04-28 16:24:15 -0400748 * and make the event log thread-safe.
749 */
750 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400751 vm->elog_main.lock =
752 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barach848191d2016-04-28 16:24:15 -0400753 vm->elog_main.lock[0] = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400754
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 if (n_vlib_mains > 1)
756 {
Dave Barach80f54e22017-03-08 19:08:56 -0500757 /* Replace hand-crafted length-1 vector with a real vector */
758 vlib_mains = 0;
759
760 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
761 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762 _vec_len (vlib_mains) = 0;
Dave Barach80f54e22017-03-08 19:08:56 -0500763 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764
Dave Barach9b8ffd92016-07-08 08:13:45 -0400765 vlib_worker_threads->wait_at_barrier =
766 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767 vlib_worker_threads->workers_at_barrier =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400768 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100770 vlib_worker_threads->node_reforks_required =
771 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
772
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773 /* Ask for an initial barrier sync */
774 *vlib_worker_threads->workers_at_barrier = 0;
775 *vlib_worker_threads->wait_at_barrier = 1;
776
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100777 /* Without update or refork */
778 *vlib_worker_threads->node_reforks_required = 0;
779 vm->need_vlib_worker_thread_node_runtime_update = 0;
780
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100781 /* init timing */
782 vm->barrier_epoch = 0;
783 vm->barrier_no_close_before = 0;
784
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785 worker_thread_index = 1;
786
Dave Barach9b8ffd92016-07-08 08:13:45 -0400787 for (i = 0; i < vec_len (tm->registrations); i++)
788 {
789 vlib_node_main_t *nm, *nm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400790 vlib_buffer_free_list_t *fl_clone, *fl_orig;
791 vlib_buffer_free_list_t *orig_freelist_pool;
792 int k;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793
Dave Barach9b8ffd92016-07-08 08:13:45 -0400794 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795
Dave Barach9b8ffd92016-07-08 08:13:45 -0400796 if (tr->count == 0)
797 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798
Dave Barach9b8ffd92016-07-08 08:13:45 -0400799 for (k = 0; k < tr->count; k++)
800 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100801 vlib_node_t *n;
802
Dave Barach9b8ffd92016-07-08 08:13:45 -0400803 vec_add2 (vlib_worker_threads, w, 1);
804 if (tr->mheap_size)
805 w->thread_mheap =
806 mheap_alloc (0 /* use VM */ , tr->mheap_size);
807 else
808 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200809
810 w->thread_stack =
811 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400812 w->thread_function = tr->function;
813 w->thread_function_arg = w;
814 w->instance_id = k;
815 w->registration = tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816
Dave Barach9b8ffd92016-07-08 08:13:45 -0400817 w->elog_track.name =
818 (char *) format (0, "%s %d", tr->name, k + 1);
819 vec_add1 (w->elog_track.name, 0);
820 elog_track_register (&vm->elog_main, &w->elog_track);
Bud Grise68adab92016-02-12 10:36:11 -0500821
Dave Barach9b8ffd92016-07-08 08:13:45 -0400822 if (tr->no_data_structure_clone)
823 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824
Dave Barach9b8ffd92016-07-08 08:13:45 -0400825 /* Fork vlib_global_main et al. Look for bugs here */
826 oldheap = clib_mem_set_heap (w->thread_mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200828 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
829 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400830 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831
Damjan Marion586afd72017-04-05 19:18:20 +0200832 vm_clone->thread_index = worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400833 vm_clone->heap_base = w->thread_mheap;
Damjan Marione9f929b2017-03-16 11:32:09 +0100834 vm_clone->init_functions_called =
835 hash_create (0, /* value bytes */ 0);
Dave Barach2877eee2017-12-15 12:22:57 -0500836 vm_clone->pending_rpc_requests = 0;
837 vec_validate (vm_clone->pending_rpc_requests, 0);
838 _vec_len (vm_clone->pending_rpc_requests) = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400839 memset (&vm_clone->random_buffer, 0,
840 sizeof (vm_clone->random_buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841
Dave Barach9b8ffd92016-07-08 08:13:45 -0400842 nm = &vlib_mains[0]->node_main;
843 nm_clone = &vm_clone->node_main;
844 /* fork next frames array, preserving node runtime indices */
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200845 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
846 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400847 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
848 {
849 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
850 u32 save_node_runtime_index;
851 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852
Dave Barach9b8ffd92016-07-08 08:13:45 -0400853 save_node_runtime_index = nf->node_runtime_index;
854 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
855 vlib_next_frame_init (nf);
856 nf->node_runtime_index = save_node_runtime_index;
857 nf->flags = save_flags;
858 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859
Dave Barach9b8ffd92016-07-08 08:13:45 -0400860 /* fork the frame dispatch queue */
861 nm_clone->pending_frames = 0;
862 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
863 _vec_len (nm_clone->pending_frames) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864
Dave Barach9b8ffd92016-07-08 08:13:45 -0400865 /* fork nodes */
866 nm_clone->nodes = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100867
868 /* Allocate all nodes in single block for speed */
869 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
870
Dave Barach9b8ffd92016-07-08 08:13:45 -0400871 for (j = 0; j < vec_len (nm->nodes); j++)
872 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400873 clib_memcpy (n, nm->nodes[j], sizeof (*n));
874 /* none of the copied nodes have enqueue rights given out */
875 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
876 memset (&n->stats_total, 0, sizeof (n->stats_total));
877 memset (&n->stats_last_clear, 0,
878 sizeof (n->stats_last_clear));
879 vec_add1 (nm_clone->nodes, n);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100880 n++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400881 }
882 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200883 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
884 CLIB_CACHE_LINE_BYTES);
JingLiuZTE30af5da2017-07-24 10:53:31 +0800885 vec_foreach (rt,
886 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
Damjan Marione9f929b2017-03-16 11:32:09 +0100887 {
888 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200889 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100890 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100891 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100892 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100893 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
894 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100895 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896
Dave Barach9b8ffd92016-07-08 08:13:45 -0400897 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200898 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
899 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400900 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
Damjan Marione9f929b2017-03-16 11:32:09 +0100901 {
902 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200903 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100904 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100905 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100906 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100907 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
908 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100909 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200911 nm_clone->processes = vec_dup_aligned (nm->processes,
912 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913
Dave Barach9b8ffd92016-07-08 08:13:45 -0400914 /* zap the (per worker) frame freelists, etc */
915 nm_clone->frame_sizes = 0;
Florin Coras93992a92017-05-24 18:03:56 -0700916 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917
Dave Barach9b8ffd92016-07-08 08:13:45 -0400918 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100919
Dave Barach9b8ffd92016-07-08 08:13:45 -0400920 clib_mem_set_heap (oldheap);
Dave Barach80f54e22017-03-08 19:08:56 -0500921 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200923 vm_clone->error_main.counters = vec_dup_aligned
924 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
925 vm_clone->error_main.counters_last_clear = vec_dup_aligned
926 (vlib_mains[0]->error_main.counters_last_clear,
927 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928
Dave Barach9b8ffd92016-07-08 08:13:45 -0400929 /* Fork the vlib_buffer_main_t free lists, etc. */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100930 orig_freelist_pool = vm_clone->buffer_free_list_pool;
931 vm_clone->buffer_free_list_pool = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400932
933 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934 pool_foreach (fl_orig, orig_freelist_pool,
935 ({
Damjan Mariond1274cb2018-03-13 21:32:17 +0100936 pool_get_aligned (vm_clone->buffer_free_list_pool,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937 fl_clone, CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400938 ASSERT (fl_orig - orig_freelist_pool
Damjan Mariond1274cb2018-03-13 21:32:17 +0100939 == fl_clone - vm_clone->buffer_free_list_pool);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940
941 fl_clone[0] = fl_orig[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100942 fl_clone->buffers = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943 fl_clone->n_alloc = 0;
944 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400945/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946
Dave Barach9b8ffd92016-07-08 08:13:45 -0400947 worker_thread_index++;
948 }
949 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950 }
951 else
952 {
953 /* only have non-data-structure copy threads to create... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400954 for (i = 0; i < vec_len (tm->registrations); i++)
955 {
956 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957
Dave Barach9b8ffd92016-07-08 08:13:45 -0400958 for (j = 0; j < tr->count; j++)
959 {
960 vec_add2 (vlib_worker_threads, w, 1);
961 if (tr->mheap_size)
962 w->thread_mheap =
963 mheap_alloc (0 /* use VM */ , tr->mheap_size);
964 else
965 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200966 w->thread_stack =
967 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400968 w->thread_function = tr->function;
969 w->thread_function_arg = w;
970 w->instance_id = j;
971 w->elog_track.name =
972 (char *) format (0, "%s %d", tr->name, j + 1);
973 w->registration = tr;
974 vec_add1 (w->elog_track.name, 0);
975 elog_track_register (&vm->elog_main, &w->elog_track);
976 }
977 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978 }
979
980 worker_thread_index = 1;
981
982 for (i = 0; i < vec_len (tm->registrations); i++)
983 {
Damjan Marion878c6092017-01-04 13:19:27 +0100984 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 int j;
986
987 tr = tm->registrations[i];
988
989 if (tr->use_pthreads || tm->use_pthreads)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400990 {
991 for (j = 0; j < tr->count; j++)
992 {
993 w = vlib_worker_threads + worker_thread_index++;
Damjan Marion878c6092017-01-04 13:19:27 +0100994 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
995 w, 0);
996 if (err)
997 clib_error_report (err);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400998 }
999 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001001 {
1002 uword c;
Damjan Marion878c6092017-01-04 13:19:27 +01001003 /* *INDENT-OFF* */
1004 clib_bitmap_foreach (c, tr->coremask, ({
1005 w = vlib_worker_threads + worker_thread_index++;
1006 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
1007 w, c);
1008 if (err)
1009 clib_error_report (err);
1010 }));
1011 /* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001012 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001014 vlib_worker_thread_barrier_sync (vm);
1015 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016 return 0;
1017}
1018
1019VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
1020
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001021
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001022static inline void
1023worker_thread_node_runtime_update_internal (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024{
1025 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026 vlib_main_t *vm;
1027 vlib_node_main_t *nm, *nm_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028 vlib_main_t *vm_clone;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001029 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030 never_inline void
1031 vlib_node_runtime_sync_stats (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001032 vlib_node_runtime_t * r,
1033 uword n_calls,
1034 uword n_vectors, uword n_clocks);
1035
Damjan Marion586afd72017-04-05 19:18:20 +02001036 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038 vm = vlib_mains[0];
1039 nm = &vm->node_main;
1040
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
1042
Dave Barach9b8ffd92016-07-08 08:13:45 -04001043 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044 * Scrape all runtime stats, so we don't lose node runtime(s) with
1045 * pending counts, or throw away worker / io thread counts.
1046 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001047 for (j = 0; j < vec_len (nm->nodes); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001049 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050 n = nm->nodes[j];
1051 vlib_node_sync_stats (vm, n);
1052 }
1053
1054 for (i = 1; i < vec_len (vlib_mains); i++)
1055 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001056 vlib_node_t *n;
1057
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058 vm_clone = vlib_mains[i];
1059 nm_clone = &vm_clone->node_main;
1060
Dave Barach9b8ffd92016-07-08 08:13:45 -04001061 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1062 {
1063 n = nm_clone->nodes[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064
Dave Barach9b8ffd92016-07-08 08:13:45 -04001065 rt = vlib_node_get_runtime (vm_clone, n->index);
1066 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1067 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068 }
1069
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001070 /* Per-worker clone rebuilds are now done on each thread */
1071}
1072
1073
1074void
1075vlib_worker_thread_node_refork (void)
1076{
1077 vlib_main_t *vm, *vm_clone;
1078 vlib_node_main_t *nm, *nm_clone;
1079 vlib_node_t **old_nodes_clone;
1080 vlib_node_runtime_t *rt, *old_rt;
1081
1082 vlib_node_t *new_n_clone;
1083
1084 int j;
1085
1086 vm = vlib_mains[0];
1087 nm = &vm->node_main;
1088 vm_clone = vlib_get_main ();
1089 nm_clone = &vm_clone->node_main;
1090
1091 /* Re-clone error heap */
1092 u64 *old_counters = vm_clone->error_main.counters;
1093 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1094
1095 clib_memcpy (&vm_clone->error_main, &vm->error_main,
1096 sizeof (vm->error_main));
1097 j = vec_len (vm->error_main.counters) - 1;
1098 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
1099 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
1100 vm_clone->error_main.counters = old_counters;
1101 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1102
1103 nm_clone = &vm_clone->node_main;
1104 vec_free (nm_clone->next_frames);
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001105 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1106 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001107
1108 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001110 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1111 u32 save_node_runtime_index;
1112 u32 save_flags;
Damjan Marionbc20bdf2015-12-17 14:28:18 +01001113
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001114 save_node_runtime_index = nf->node_runtime_index;
1115 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1116 vlib_next_frame_init (nf);
1117 nf->node_runtime_index = save_node_runtime_index;
1118 nf->flags = save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001120
1121 old_nodes_clone = nm_clone->nodes;
1122 nm_clone->nodes = 0;
1123
1124 /* re-fork nodes */
1125
1126 /* Allocate all nodes in single block for speed */
1127 new_n_clone =
1128 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1129 for (j = 0; j < vec_len (nm->nodes); j++)
1130 {
1131 vlib_node_t *old_n_clone;
1132 vlib_node_t *new_n;
1133
1134 new_n = nm->nodes[j];
1135 old_n_clone = old_nodes_clone[j];
1136
1137 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
1138 /* none of the copied nodes have enqueue rights given out */
1139 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1140
1141 if (j >= vec_len (old_nodes_clone))
1142 {
1143 /* new node, set to zero */
1144 memset (&new_n_clone->stats_total, 0,
1145 sizeof (new_n_clone->stats_total));
1146 memset (&new_n_clone->stats_last_clear, 0,
1147 sizeof (new_n_clone->stats_last_clear));
1148 }
1149 else
1150 {
1151 /* Copy stats if the old data is valid */
1152 clib_memcpy (&new_n_clone->stats_total,
1153 &old_n_clone->stats_total,
1154 sizeof (new_n_clone->stats_total));
1155 clib_memcpy (&new_n_clone->stats_last_clear,
1156 &old_n_clone->stats_last_clear,
1157 sizeof (new_n_clone->stats_last_clear));
1158
1159 /* keep previous node state */
1160 new_n_clone->state = old_n_clone->state;
1161 }
1162 vec_add1 (nm_clone->nodes, new_n_clone);
1163 new_n_clone++;
1164 }
1165 /* Free the old node clones */
1166 clib_mem_free (old_nodes_clone[0]);
1167
1168 vec_free (old_nodes_clone);
1169
1170
1171 /* re-clone internal nodes */
1172 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1173 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001174 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1175 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001176
1177 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1178 {
1179 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1180 rt->thread_index = vm_clone->thread_index;
1181 /* copy runtime_data, will be overwritten later for existing rt */
1182 if (n->runtime_data && n->runtime_data_bytes > 0)
1183 clib_memcpy (rt->runtime_data, n->runtime_data,
1184 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1185 n->runtime_data_bytes));
1186 }
1187
1188 for (j = 0; j < vec_len (old_rt); j++)
1189 {
1190 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1191 rt->state = old_rt[j].state;
1192 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
1193 VLIB_NODE_RUNTIME_DATA_SIZE);
1194 }
1195
1196 vec_free (old_rt);
1197
1198 /* re-clone input nodes */
1199 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1200 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001201 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1202 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001203
1204 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1205 {
1206 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1207 rt->thread_index = vm_clone->thread_index;
1208 /* copy runtime_data, will be overwritten later for existing rt */
1209 if (n->runtime_data && n->runtime_data_bytes > 0)
1210 clib_memcpy (rt->runtime_data, n->runtime_data,
1211 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1212 n->runtime_data_bytes));
1213 }
1214
1215 for (j = 0; j < vec_len (old_rt); j++)
1216 {
1217 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1218 rt->state = old_rt[j].state;
1219 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
1220 VLIB_NODE_RUNTIME_DATA_SIZE);
1221 }
1222
1223 vec_free (old_rt);
1224
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001225 nm_clone->processes = vec_dup_aligned (nm->processes,
1226 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001227}
1228
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001229void
1230vlib_worker_thread_node_runtime_update (void)
1231{
1232 /*
1233 * Make a note that we need to do a node runtime update
1234 * prior to releasing the barrier.
1235 */
1236 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237}
1238
Pavel Kotucek1e765832016-09-23 08:54:14 +02001239u32
1240unformat_sched_policy (unformat_input_t * input, va_list * args)
1241{
1242 u32 *r = va_arg (*args, u32 *);
1243
1244 if (0);
1245#define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1246 foreach_sched_policy
1247#undef _
1248 else
1249 return 0;
1250 return 1;
1251}
1252
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253static clib_error_t *
1254cpu_config (vlib_main_t * vm, unformat_input_t * input)
1255{
1256 vlib_thread_registration_t *tr;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001257 uword *p;
1258 vlib_thread_main_t *tm = &vlib_thread_main;
1259 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260 u64 coremask;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001261 uword *bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262 u32 count;
1263
1264 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
Pavel Kotucek1e765832016-09-23 08:54:14 +02001265
Dave Barach9b8ffd92016-07-08 08:13:45 -04001266 tm->n_thread_stacks = 1; /* account for main thread */
Pavel Kotucek1e765832016-09-23 08:54:14 +02001267 tm->sched_policy = ~0;
1268 tm->sched_priority = ~0;
Damjan Marion858151f2018-07-11 10:51:00 +02001269 tm->main_lcore = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001270
1271 tr = tm->next;
1272
1273 while (tr)
1274 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001275 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276 tr = tr->next;
1277 }
1278
Dave Barach9b8ffd92016-07-08 08:13:45 -04001279 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001280 {
Damjan Marionbf741472016-06-13 22:49:44 +02001281 if (unformat (input, "use-pthreads"))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001282 tm->use_pthreads = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001284 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001285 else if (unformat (input, "main-core %u", &tm->main_lcore))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001286 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001288 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001289 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001290 {
1291 p = hash_get_mem (tm->thread_registrations_by_name, name);
1292 if (p == 0)
1293 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001294
Dave Barach9b8ffd92016-07-08 08:13:45 -04001295 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001296
Dave Barach9b8ffd92016-07-08 08:13:45 -04001297 if (tr->use_pthreads)
1298 return clib_error_return (0,
1299 "coremask cannot be set for '%s' threads",
1300 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001301
Dave Barach9b8ffd92016-07-08 08:13:45 -04001302 tr->coremask = clib_bitmap_set_multiple
1303 (tr->coremask, 0, coremask, BITS (coremask));
1304 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1305 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001306 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001307 &bitmap))
1308 {
1309 p = hash_get_mem (tm->thread_registrations_by_name, name);
1310 if (p == 0)
1311 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312
Dave Barach9b8ffd92016-07-08 08:13:45 -04001313 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001314
Dave Barach9b8ffd92016-07-08 08:13:45 -04001315 if (tr->use_pthreads)
1316 return clib_error_return (0,
1317 "corelist cannot be set for '%s' threads",
1318 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001319
Dave Barach9b8ffd92016-07-08 08:13:45 -04001320 tr->coremask = bitmap;
1321 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1322 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001323 else
1324 if (unformat
1325 (input, "scheduler-policy %U", unformat_sched_policy,
1326 &tm->sched_policy))
1327 ;
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001328 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
Pavel Kotucek1e765832016-09-23 08:54:14 +02001329 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001330 else if (unformat (input, "%s %u", &name, &count))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001331 {
1332 p = hash_get_mem (tm->thread_registrations_by_name, name);
1333 if (p == 0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001334 return clib_error_return (0, "no such thread type 3 '%s'", name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001335
1336 tr = (vlib_thread_registration_t *) p[0];
1337 if (tr->fixed_count)
1338 return clib_error_return
1339 (0, "number of %s threads not configurable", tr->name);
1340 tr->count = count;
1341 }
1342 else
1343 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001344 }
1345
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001346 if (tm->sched_priority != ~0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001347 {
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001348 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001349 {
1350 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1351 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1352 if (tm->sched_priority > prio_max)
1353 tm->sched_priority = prio_max;
1354 if (tm->sched_priority < prio_min)
1355 tm->sched_priority = prio_min;
1356 }
1357 else
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001358 {
1359 return clib_error_return
1360 (0,
1361 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1362 tm->sched_priority);
1363 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001364 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365 tr = tm->next;
1366
1367 if (!tm->thread_prefix)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001368 tm->thread_prefix = format (0, "vpp");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001369
1370 while (tr)
1371 {
1372 tm->n_thread_stacks += tr->count;
1373 tm->n_pthreads += tr->count * tr->use_pthreads;
Damjan Marion878c6092017-01-04 13:19:27 +01001374 tm->n_threads += tr->count * (tr->use_pthreads == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001375 tr = tr->next;
1376 }
1377
1378 return 0;
1379}
1380
1381VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1382
Damjan Marion7bee80c2017-04-26 15:32:12 +02001383#if !defined (__x86_64__) && !defined (__i386__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001384void
1385__sync_fetch_and_add_8 (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001386{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001387 fformat (stderr, "%s called\n", __FUNCTION__);
1388 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001389}
Dave Barach9b8ffd92016-07-08 08:13:45 -04001390
1391void
1392__sync_add_and_fetch_8 (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001393{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001394 fformat (stderr, "%s called\n", __FUNCTION__);
1395 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001396}
1397#endif
1398
1399void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -04001400void
1401vnet_main_fixup (vlib_fork_fixup_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001402{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001403}
1404
1405void
1406vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1407{
1408 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001409
1410 if (vlib_mains == 0)
1411 return;
1412
Damjan Marion586afd72017-04-05 19:18:20 +02001413 ASSERT (vlib_get_thread_index () == 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001414 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001415
1416 switch (which)
1417 {
1418 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1419 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1420 break;
1421
1422 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001423 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001425 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001426}
1427
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001428 /*
1429 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1430 * based on a test based heuristic that barrier should be open for at least
1431 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1432 * point it is probably too late to make a difference)
1433 */
1434
1435#ifndef BARRIER_MINIMUM_OPEN_LIMIT
1436#define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1437#endif
1438
1439#ifndef BARRIER_MINIMUM_OPEN_FACTOR
1440#define BARRIER_MINIMUM_OPEN_FACTOR 3
1441#endif
1442
Dave Barach9b8ffd92016-07-08 08:13:45 -04001443void
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001444vlib_worker_thread_barrier_sync_int (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001445{
1446 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001447 f64 now;
1448 f64 t_entry;
1449 f64 t_open;
1450 f64 t_closed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451 u32 count;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001452
Dave Barach80f54e22017-03-08 19:08:56 -05001453 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001454 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001455
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001456 ASSERT (vlib_get_thread_index () == 0);
1457
Ed Warnickecb9cada2015-12-08 15:45:58 -07001458 count = vec_len (vlib_mains) - 1;
1459
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001460 /* Record entry relative to last close */
1461 now = vlib_time_now (vm);
1462 t_entry = now - vm->barrier_epoch;
1463
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464 /* Tolerate recursive calls */
1465 if (++vlib_worker_threads[0].recursion_level > 1)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001466 {
1467 barrier_trace_sync_rec (t_entry);
1468 return;
1469 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001470
Bud Grise42f20062016-03-16 13:09:46 -04001471 vlib_worker_threads[0].barrier_sync_count++;
1472
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001473 /* Enforce minimum barrier open time to minimize packet loss */
1474 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1475 while ((now = vlib_time_now (vm)) < vm->barrier_no_close_before)
1476 ;
1477
1478 /* Record time of closure */
1479 t_open = now - vm->barrier_epoch;
1480 vm->barrier_epoch = now;
1481
1482 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001483
1484 *vlib_worker_threads->wait_at_barrier = 1;
1485 while (*vlib_worker_threads->workers_at_barrier != count)
1486 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001487 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001488 {
1489 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1490 os_panic ();
1491 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001492 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001493
1494 t_closed = now - vm->barrier_epoch;
1495
1496 barrier_trace_sync (t_entry, t_open, t_closed);
1497
Ed Warnickecb9cada2015-12-08 15:45:58 -07001498}
1499
Dave Barach1ddbc012018-06-13 09:26:05 -04001500void vlib_stat_segment_lock (void) __attribute__ ((weak));
1501void
1502vlib_stat_segment_lock (void)
1503{
1504}
1505
1506void vlib_stat_segment_unlock (void) __attribute__ ((weak));
1507void
1508vlib_stat_segment_unlock (void)
1509{
1510}
1511
Dave Barach9b8ffd92016-07-08 08:13:45 -04001512void
1513vlib_worker_thread_barrier_release (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001514{
1515 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001516 f64 now;
1517 f64 minimum_open;
1518 f64 t_entry;
1519 f64 t_closed_total;
1520 f64 t_update_main = 0.0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001521 int refork_needed = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001522
Dave Barach80f54e22017-03-08 19:08:56 -05001523 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001524 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001525
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001526 ASSERT (vlib_get_thread_index () == 0);
1527
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001528
1529 now = vlib_time_now (vm);
1530 t_entry = now - vm->barrier_epoch;
1531
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532 if (--vlib_worker_threads[0].recursion_level > 0)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001533 {
1534 barrier_trace_release_rec (t_entry);
1535 return;
1536 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001537
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001538 /* Update (all) node runtimes before releasing the barrier, if needed */
1539 if (vm->need_vlib_worker_thread_node_runtime_update)
1540 {
Dave Barach1ddbc012018-06-13 09:26:05 -04001541 /*
1542 * Lock stat segment here, so we's safe when
1543 * rebuilding the stat segment node clones from the
1544 * stat thread...
1545 */
1546 vlib_stat_segment_lock ();
1547
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001548 /* Do stats elements on main thread */
1549 worker_thread_node_runtime_update_internal ();
1550 vm->need_vlib_worker_thread_node_runtime_update = 0;
1551
1552 /* Do per thread rebuilds in parallel */
1553 refork_needed = 1;
1554 clib_smp_atomic_add (vlib_worker_threads->node_reforks_required,
1555 (vec_len (vlib_mains) - 1));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001556 now = vlib_time_now (vm);
1557 t_update_main = now - vm->barrier_epoch;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001558 }
1559
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001560 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001561
1562 *vlib_worker_threads->wait_at_barrier = 0;
1563
1564 while (*vlib_worker_threads->workers_at_barrier > 0)
1565 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001566 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001567 {
1568 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1569 os_panic ();
1570 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001571 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001572
1573 /* Wait for reforks before continuing */
1574 if (refork_needed)
1575 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001576 now = vlib_time_now (vm);
1577
1578 deadline = now + BARRIER_SYNC_TIMEOUT;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001579
1580 while (*vlib_worker_threads->node_reforks_required > 0)
1581 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001582 if ((now = vlib_time_now (vm)) > deadline)
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001583 {
1584 fformat (stderr, "%s: worker thread refork deadlock\n",
1585 __FUNCTION__);
1586 os_panic ();
1587 }
1588 }
Dave Barach1ddbc012018-06-13 09:26:05 -04001589 vlib_stat_segment_unlock ();
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001590 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001591
1592 t_closed_total = now - vm->barrier_epoch;
1593
1594 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1595
1596 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1597 {
1598 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1599 }
1600
1601 vm->barrier_no_close_before = now + minimum_open;
1602
1603 /* Record barrier epoch (used to enforce minimum open time) */
1604 vm->barrier_epoch = now;
1605
1606 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1607
Ed Warnickecb9cada2015-12-08 15:45:58 -07001608}
1609
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001610/*
1611 * Check the frame queue to see if any frames are available.
Dave Barach9b8ffd92016-07-08 08:13:45 -04001612 * If so, pull the packets off the frames and put them to
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001613 * the handoff node.
1614 */
Damjan Marione9d52d52017-03-09 15:42:26 +01001615int
1616vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001617{
Damjan Marion586afd72017-04-05 19:18:20 +02001618 u32 thread_id = vm->thread_index;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001619 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001620 vlib_frame_queue_elt_t *elt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001621 u32 *from, *to;
1622 vlib_frame_t *f;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001623 int msg_type;
1624 int processed = 0;
1625 u32 n_left_to_node;
1626 u32 vectors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001627
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001628 ASSERT (fq);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001629 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001630
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001631 if (PREDICT_FALSE (fqm->node_index == ~0))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001632 return 0;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001633 /*
1634 * Gather trace data for frame queues
1635 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001636 if (PREDICT_FALSE (fq->trace))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001637 {
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001638 frame_queue_trace_t *fqt;
1639 frame_queue_nelt_counter_t *fqh;
1640 u32 elix;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001641
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001642 fqt = &fqm->frame_queue_traces[thread_id];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001643
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001644 fqt->nelts = fq->nelts;
1645 fqt->head = fq->head;
1646 fqt->head_hint = fq->head_hint;
1647 fqt->tail = fq->tail;
1648 fqt->threshold = fq->vector_threshold;
1649 fqt->n_in_use = fqt->tail - fqt->head;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001650 if (fqt->n_in_use >= fqt->nelts)
1651 {
1652 // if beyond max then use max
1653 fqt->n_in_use = fqt->nelts - 1;
1654 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001655
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001656 /* Record the number of elements in use in the histogram */
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001657 fqh = &fqm->frame_queue_histogram[thread_id];
Dave Barach9b8ffd92016-07-08 08:13:45 -04001658 fqh->count[fqt->n_in_use]++;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001659
1660 /* Record a snapshot of the elements in use */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001661 for (elix = 0; elix < fqt->nelts; elix++)
1662 {
1663 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1664 if (1 || elt->valid)
1665 {
1666 fqt->n_vectors[elix] = elt->n_vectors;
1667 }
1668 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001669 fqt->written = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001670 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001671
1672 while (1)
1673 {
1674 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001675 {
1676 fq->head_hint = fq->head;
1677 return processed;
1678 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001679
Dave Barach9b8ffd92016-07-08 08:13:45 -04001680 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001681
1682 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001683 {
1684 fq->head_hint = fq->head;
1685 return processed;
1686 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001687
1688 from = elt->buffer_index;
1689 msg_type = elt->msg_type;
1690
1691 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1692 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1693
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001694 f = vlib_get_frame_to_node (vm, fqm->node_index);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001695
1696 to = vlib_frame_vector_args (f);
1697
1698 n_left_to_node = elt->n_vectors;
1699
1700 while (n_left_to_node >= 4)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001701 {
1702 to[0] = from[0];
1703 to[1] = from[1];
1704 to[2] = from[2];
1705 to[3] = from[3];
1706 to += 4;
1707 from += 4;
1708 n_left_to_node -= 4;
1709 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001710
1711 while (n_left_to_node > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001712 {
1713 to[0] = from[0];
1714 to++;
1715 from++;
1716 n_left_to_node--;
1717 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001718
1719 vectors += elt->n_vectors;
1720 f->n_vectors = elt->n_vectors;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001721 vlib_put_frame_to_node (vm, fqm->node_index, f);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001722
1723 elt->valid = 0;
1724 elt->n_vectors = 0;
1725 elt->msg_type = 0xfefefefe;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001726 CLIB_MEMORY_BARRIER ();
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001727 fq->head++;
1728 processed++;
1729
1730 /*
1731 * Limit the number of packets pushed into the graph
1732 */
1733 if (vectors >= fq->vector_threshold)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001734 {
1735 fq->head_hint = fq->head;
1736 return processed;
1737 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001738 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001739 ASSERT (0);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001740 return processed;
1741}
1742
Dave Barach9b8ffd92016-07-08 08:13:45 -04001743void
1744vlib_worker_thread_fn (void *arg)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001745{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001746 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
Damjan Marion878c6092017-01-04 13:19:27 +01001747 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -04001748 vlib_main_t *vm = vlib_get_main ();
Damjan Marione9f929b2017-03-16 11:32:09 +01001749 clib_error_t *e;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001750
Damjan Marion586afd72017-04-05 19:18:20 +02001751 ASSERT (vm->thread_index == vlib_get_thread_index ());
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001752
1753 vlib_worker_thread_init (w);
1754 clib_time_init (&vm->clib_time);
1755 clib_mem_set_heap (w->thread_mheap);
1756
Damjan Marioneb743fa2017-03-20 16:34:15 +01001757 /* Wait until the dpdk init sequence is complete */
1758 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1759 vlib_worker_thread_barrier_check ();
1760
Damjan Marione9f929b2017-03-16 11:32:09 +01001761 e = vlib_call_init_exit_functions
1762 (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1763 if (e)
1764 clib_error_report (e);
1765
Damjan Marione9d52d52017-03-09 15:42:26 +01001766 vlib_worker_loop (vm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001767}
1768
Dave Barach9b8ffd92016-07-08 08:13:45 -04001769/* *INDENT-OFF* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001770VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1771 .name = "workers",
1772 .short_name = "wk",
1773 .function = vlib_worker_thread_fn,
1774};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001775/* *INDENT-ON* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001776
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001777u32
1778vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1779{
1780 vlib_thread_main_t *tm = vlib_get_thread_main ();
1781 vlib_frame_queue_main_t *fqm;
1782 vlib_frame_queue_t *fq;
1783 int i;
1784
1785 if (frame_queue_nelts == 0)
1786 frame_queue_nelts = FRAME_QUEUE_NELTS;
1787
1788 vec_add2 (tm->frame_queue_mains, fqm, 1);
1789
1790 fqm->node_index = node_index;
1791
1792 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1793 _vec_len (fqm->vlib_frame_queues) = 0;
1794 for (i = 0; i < tm->n_vlib_mains; i++)
1795 {
1796 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1797 vec_add1 (fqm->vlib_frame_queues, fq);
1798 }
1799
1800 return (fqm - tm->frame_queue_mains);
1801}
1802
Damjan Marion878c6092017-01-04 13:19:27 +01001803int
1804vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1805{
1806 vlib_thread_main_t *tm = vlib_get_thread_main ();
1807
1808 if (tm->extern_thread_mgmt)
1809 return -1;
1810
1811 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1812 tm->extern_thread_mgmt = 1;
1813 return 0;
1814}
1815
Dave Barach69128d02017-09-26 10:54:34 -04001816void
1817vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1818 args)
1819{
1820 ASSERT (vlib_get_thread_index () == 0);
1821 vlib_process_signal_event (vlib_get_main (), args->node_index,
1822 args->type_opaque, args->data);
1823}
1824
1825void *rpc_call_main_thread_cb_fn;
1826
1827void
1828vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1829{
1830 if (rpc_call_main_thread_cb_fn)
1831 {
1832 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1833 (*fp) (callback, args, arg_size);
1834 }
1835 else
1836 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1837}
1838
Dave Barach9b8ffd92016-07-08 08:13:45 -04001839clib_error_t *
1840threads_init (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001841{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001842 return 0;
1843}
1844
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001845VLIB_INIT_FUNCTION (threads_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001846
1847/*
1848 * fd.io coding-style-patch-verification: ON
1849 *
1850 * Local Variables:
1851 * eval: (c-set-style "gnu")
1852 * End:
1853 */