blob: c17fde4bfecd5f525c9c69a29db16f7f333b0b8b [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
Damjan Marion7752b392018-07-03 17:24:45 +0200340 /* by default we skip core 0, unless it is the only one available */
Damjan Marion9e300dd2018-07-03 22:24:32 +0200341 if (tm->skip_cores == ~0)
342 tm->skip_cores = (clib_bitmap_count_set_bits (avail_cpu) < 2) ? 0 : 1;
Damjan Marion7752b392018-07-03 17:24:45 +0200343
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344 /* skip cores */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400345 for (i = 0; i < tm->skip_cores; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347 uword c = clib_bitmap_first_set (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348 if (c == ~0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400349 return clib_error_return (0, "no available cpus to skip");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
Dave Barach9b8ffd92016-07-08 08:13:45 -0400351 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 }
353
354 /* grab cpu for main thread */
355 if (!tm->main_lcore)
356 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400357 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
358 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 }
Christophe Fontainefef15b42016-04-09 12:38:49 +0900379
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 /* as many threads as stacks... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400381 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
382 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383
384 /* Preallocate thread 0 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400385 _vec_len (vlib_worker_threads) = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386 w = vlib_worker_threads;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400387 w->thread_mheap = clib_mem_get_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 w->thread_stack = vlib_thread_stacks[0];
Pavel Kotucek98765202016-10-07 08:37:28 +0200389 w->lcore_id = tm->main_lcore;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400390 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200391 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 tm->n_vlib_mains = 1;
393
Pavel Kotucek1e765832016-09-23 08:54:14 +0200394 if (tm->sched_policy != ~0)
395 {
396 struct sched_param sched_param;
397 if (!sched_getparam (w->lwp, &sched_param))
398 {
399 if (tm->sched_priority != ~0)
400 sched_param.sched_priority = tm->sched_priority;
401 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
402 }
403 }
404
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 /* assign threads to cores and set n_vlib_mains */
406 tr = tm->next;
407
408 while (tr)
409 {
410 vec_add1 (tm->registrations, tr);
411 tr = tr->next;
412 }
413
Dave Barach9b8ffd92016-07-08 08:13:45 -0400414 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415
416 for (i = 0; i < vec_len (tm->registrations); i++)
417 {
418 int j;
419 tr = tm->registrations[i];
420 tr->first_index = first_index;
421 first_index += tr->count;
422 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
423
424 /* construct coremask */
425 if (tr->use_pthreads || !tr->count)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400426 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
428 if (tr->coremask)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400429 {
430 uword c;
431 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 clib_bitmap_foreach (c, tr->coremask, ({
433 if (clib_bitmap_get(avail_cpu, c) == 0)
434 return clib_error_return (0, "cpu %u is not available to be used"
435 " for the '%s' thread",c, tr->name);
436
437 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
438 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400439/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440
Dave Barach9b8ffd92016-07-08 08:13:45 -0400441 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400443 {
444 for (j = 0; j < tr->count; j++)
445 {
446 uword c = clib_bitmap_first_set (avail_cpu);
447 if (c == ~0)
448 return clib_error_return (0,
449 "no available cpus to be used for"
450 " the '%s' thread", tr->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451
Dave Barach9b8ffd92016-07-08 08:13:45 -0400452 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
453 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
454 }
455 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 }
457
Dave Barach9b8ffd92016-07-08 08:13:45 -0400458 clib_bitmap_free (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459
460 tm->n_vlib_mains = n_vlib_mains;
461
Dave Barach9b8ffd92016-07-08 08:13:45 -0400462 vec_validate_aligned (vlib_worker_threads, first_index - 1,
463 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465 return 0;
466}
467
Dave Barach9b8ffd92016-07-08 08:13:45 -0400468vlib_frame_queue_t *
469vlib_frame_queue_alloc (int nelts)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471 vlib_frame_queue_t *fq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
Dave Barach9b8ffd92016-07-08 08:13:45 -0400473 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474 memset (fq, 0, sizeof (*fq));
475 fq->nelts = nelts;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476 fq->vector_threshold = 128; // packets
477 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478
479 if (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400480 {
481 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
482 fformat (stderr, "WARNING: fq->tail unaligned\n");
483 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
484 fformat (stderr, "WARNING: fq->head unaligned\n");
485 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
486 fformat (stderr, "WARNING: fq->elts unaligned\n");
487
488 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
489 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
490 sizeof (fq->elts[0]));
491 if (nelts & (nelts - 1))
492 {
493 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
494 abort ();
495 }
496 }
497
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 return (fq);
499}
500
501void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400502void
503vl_msg_api_handler_no_free (void *v)
504{
505}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
507/* Turned off, save as reference material... */
508#if 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400509static inline int
510vlib_frame_queue_dequeue_internal (int thread_id,
511 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512{
513 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
514 vlib_frame_queue_elt_t *elt;
515 vlib_frame_t *f;
516 vlib_pending_frame_t *p;
517 vlib_node_runtime_t *r;
518 u32 node_runtime_index;
519 int msg_type;
520 u64 before;
521 int processed = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400522
523 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
525 while (1)
526 {
527 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400528 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
532 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400533 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
Dave Barach9b8ffd92016-07-08 08:13:45 -0400535 before = clib_cpu_time_now ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
537 f = elt->frame;
538 node_runtime_index = elt->node_runtime_index;
539 msg_type = elt->msg_type;
540
541 switch (msg_type)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400542 {
543 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
544 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
545 /* note fallthrough... */
546 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
547 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
548 node_runtime_index);
549 vlib_frame_free (vm, r, f);
550 break;
551 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
552 vec_add2 (vm->node_main.pending_frames, p, 1);
553 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
554 p->node_runtime_index = elt->node_runtime_index;
555 p->frame_index = vlib_frame_index (vm, f);
556 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
557 fq->dequeue_vectors += (u64) f->n_vectors;
558 break;
559 case VLIB_FRAME_QUEUE_ELT_API_MSG:
560 vl_msg_api_handler_no_free (f);
561 break;
562 default:
563 clib_warning ("bogus frame queue message, type %d", msg_type);
564 break;
565 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 elt->valid = 0;
567 fq->dequeues++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400568 fq->dequeue_ticks += clib_cpu_time_now () - before;
569 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 fq->head++;
571 processed++;
572 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400573 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574 return processed;
575}
576
Dave Barach9b8ffd92016-07-08 08:13:45 -0400577int
578vlib_frame_queue_dequeue (int thread_id,
579 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580{
581 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
582}
583
Dave Barach9b8ffd92016-07-08 08:13:45 -0400584int
585vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
586 u32 frame_queue_index, vlib_frame_t * frame,
587 vlib_frame_queue_msg_type_t type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588{
589 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
590 vlib_frame_queue_elt_t *elt;
591 u32 save_count;
592 u64 new_tail;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400593 u64 before = clib_cpu_time_now ();
594
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595 ASSERT (fq);
596
597 new_tail = __sync_add_and_fetch (&fq->tail, 1);
598
599 /* Wait until a ring slot is available */
600 while (new_tail >= fq->head + fq->nelts)
601 {
602 f64 b4 = vlib_time_now_ticks (vm, before);
603 vlib_worker_thread_barrier_check (vm, b4);
604 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
Damjan Marion586afd72017-04-05 19:18:20 +0200605 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606 }
607
Dave Barach9b8ffd92016-07-08 08:13:45 -0400608 elt = fq->elts + (new_tail & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609
610 /* this would be very bad... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400611 while (elt->valid)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612 {
613 }
614
615 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
616 save_count = frame->n_vectors;
617
618 elt->frame = frame;
619 elt->node_runtime_index = node_runtime_index;
620 elt->msg_type = type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400621 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622 elt->valid = 1;
623
624 return save_count;
625}
626#endif /* 0 */
627
628/* To be called by vlib worker threads upon startup */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400629void
630vlib_worker_thread_init (vlib_worker_thread_t * w)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400632 vlib_thread_main_t *tm = vlib_get_thread_main ();
633
Dave Barachfdf49442016-12-20 12:48:14 -0500634 /*
635 * Note: disabling signals in worker threads as follows
636 * prevents the api post-mortem dump scheme from working
637 * {
638 * sigset_t s;
639 * sigfillset (&s);
640 * pthread_sigmask (SIG_SETMASK, &s, 0);
641 * }
642 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
644 clib_mem_set_heap (w->thread_mheap);
645
Dave Barach9b8ffd92016-07-08 08:13:45 -0400646 if (vec_len (tm->thread_prefix) && w->registration->short_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400648 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
649 w->registration->short_name, w->instance_id, '\0');
650 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651 }
652
653 if (!w->registration->use_pthreads)
654 {
655
656 /* Initial barrier sync, for both worker and i/o threads */
657 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
658
659 while (*vlib_worker_threads->wait_at_barrier)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400660 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661
662 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
663 }
664}
665
Dave Barach9b8ffd92016-07-08 08:13:45 -0400666void *
667vlib_worker_thread_bootstrap_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668{
669 void *rv;
670 vlib_worker_thread_t *w = arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400671
672 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200673 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674
Damjan Marionf55f9b82017-05-10 21:06:28 +0200675 __os_thread_index = w - vlib_worker_threads;
Damjan Marion586afd72017-04-05 19:18:20 +0200676
Dave Barach9b8ffd92016-07-08 08:13:45 -0400677 rv = (void *) clib_calljmp
678 ((uword (*)(uword)) w->thread_function,
679 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680 /* NOTREACHED, we hope */
681 return rv;
682}
683
Damjan Marion878c6092017-01-04 13:19:27 +0100684static clib_error_t *
685vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686{
Damjan Marion878c6092017-01-04 13:19:27 +0100687 vlib_thread_main_t *tm = &vlib_thread_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400688 void *(*fp_arg) (void *) = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689
Pavel Kotucek98765202016-10-07 08:37:28 +0200690 w->lcore_id = lcore_id;
Damjan Marion878c6092017-01-04 13:19:27 +0100691 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
692 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400694 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400695 pthread_t worker;
696 cpu_set_t cpuset;
697 CPU_ZERO (&cpuset);
698 CPU_SET (lcore_id, &cpuset);
Christophe Fontainefef15b42016-04-09 12:38:49 +0900699
Damjan Marion878c6092017-01-04 13:19:27 +0100700 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
701 return clib_error_return_unix (0, "pthread_create");
702
703 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
704 return clib_error_return_unix (0, "pthread_setaffinity_np");
705
706 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400707 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708}
709
Dave Barach9b8ffd92016-07-08 08:13:45 -0400710static clib_error_t *
711start_workers (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712{
713 int i, j;
714 vlib_worker_thread_t *w;
715 vlib_main_t *vm_clone;
716 void *oldheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400717 vlib_thread_main_t *tm = &vlib_thread_main;
718 vlib_thread_registration_t *tr;
719 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720 u32 n_vlib_mains = tm->n_vlib_mains;
721 u32 worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400722 u8 *main_heap = clib_mem_get_per_cpu_heap ();
723 mheap_t *main_heap_header = mheap_header (main_heap);
724
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725 vec_reset_length (vlib_worker_threads);
726
727 /* Set up the main thread */
728 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
Dave Barach3e7deb12016-02-05 16:29:53 -0500729 w->elog_track.name = "main thread";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730 elog_track_register (&vm->elog_main, &w->elog_track);
731
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732 if (vec_len (tm->thread_prefix))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400734 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
735 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736 }
737
Dave Barach9b8ffd92016-07-08 08:13:45 -0400738 /*
Dave Barach848191d2016-04-28 16:24:15 -0400739 * Truth of the matter: we always use at least two
Dave Barach9b8ffd92016-07-08 08:13:45 -0400740 * threads. So, make the main heap thread-safe
Dave Barach848191d2016-04-28 16:24:15 -0400741 * and make the event log thread-safe.
742 */
743 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400744 vm->elog_main.lock =
745 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barach848191d2016-04-28 16:24:15 -0400746 vm->elog_main.lock[0] = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400747
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748 if (n_vlib_mains > 1)
749 {
Dave Barach80f54e22017-03-08 19:08:56 -0500750 /* Replace hand-crafted length-1 vector with a real vector */
751 vlib_mains = 0;
752
753 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
754 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 _vec_len (vlib_mains) = 0;
Dave Barach80f54e22017-03-08 19:08:56 -0500756 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757
Dave Barach9b8ffd92016-07-08 08:13:45 -0400758 vlib_worker_threads->wait_at_barrier =
759 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760 vlib_worker_threads->workers_at_barrier =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400761 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100763 vlib_worker_threads->node_reforks_required =
764 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
765
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766 /* Ask for an initial barrier sync */
767 *vlib_worker_threads->workers_at_barrier = 0;
768 *vlib_worker_threads->wait_at_barrier = 1;
769
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100770 /* Without update or refork */
771 *vlib_worker_threads->node_reforks_required = 0;
772 vm->need_vlib_worker_thread_node_runtime_update = 0;
773
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100774 /* init timing */
775 vm->barrier_epoch = 0;
776 vm->barrier_no_close_before = 0;
777
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778 worker_thread_index = 1;
779
Dave Barach9b8ffd92016-07-08 08:13:45 -0400780 for (i = 0; i < vec_len (tm->registrations); i++)
781 {
782 vlib_node_main_t *nm, *nm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400783 vlib_buffer_free_list_t *fl_clone, *fl_orig;
784 vlib_buffer_free_list_t *orig_freelist_pool;
785 int k;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786
Dave Barach9b8ffd92016-07-08 08:13:45 -0400787 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
Dave Barach9b8ffd92016-07-08 08:13:45 -0400789 if (tr->count == 0)
790 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791
Dave Barach9b8ffd92016-07-08 08:13:45 -0400792 for (k = 0; k < tr->count; k++)
793 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100794 vlib_node_t *n;
795
Dave Barach9b8ffd92016-07-08 08:13:45 -0400796 vec_add2 (vlib_worker_threads, w, 1);
797 if (tr->mheap_size)
798 w->thread_mheap =
799 mheap_alloc (0 /* use VM */ , tr->mheap_size);
800 else
801 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200802
803 w->thread_stack =
804 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400805 w->thread_function = tr->function;
806 w->thread_function_arg = w;
807 w->instance_id = k;
808 w->registration = tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809
Dave Barach9b8ffd92016-07-08 08:13:45 -0400810 w->elog_track.name =
811 (char *) format (0, "%s %d", tr->name, k + 1);
812 vec_add1 (w->elog_track.name, 0);
813 elog_track_register (&vm->elog_main, &w->elog_track);
Bud Grise68adab92016-02-12 10:36:11 -0500814
Dave Barach9b8ffd92016-07-08 08:13:45 -0400815 if (tr->no_data_structure_clone)
816 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817
Dave Barach9b8ffd92016-07-08 08:13:45 -0400818 /* Fork vlib_global_main et al. Look for bugs here */
819 oldheap = clib_mem_set_heap (w->thread_mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200821 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
822 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400823 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824
Damjan Marion586afd72017-04-05 19:18:20 +0200825 vm_clone->thread_index = worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400826 vm_clone->heap_base = w->thread_mheap;
Damjan Marione9f929b2017-03-16 11:32:09 +0100827 vm_clone->init_functions_called =
828 hash_create (0, /* value bytes */ 0);
Dave Barach2877eee2017-12-15 12:22:57 -0500829 vm_clone->pending_rpc_requests = 0;
830 vec_validate (vm_clone->pending_rpc_requests, 0);
831 _vec_len (vm_clone->pending_rpc_requests) = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400832 memset (&vm_clone->random_buffer, 0,
833 sizeof (vm_clone->random_buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834
Dave Barach9b8ffd92016-07-08 08:13:45 -0400835 nm = &vlib_mains[0]->node_main;
836 nm_clone = &vm_clone->node_main;
837 /* fork next frames array, preserving node runtime indices */
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200838 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
839 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400840 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
841 {
842 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
843 u32 save_node_runtime_index;
844 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845
Dave Barach9b8ffd92016-07-08 08:13:45 -0400846 save_node_runtime_index = nf->node_runtime_index;
847 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
848 vlib_next_frame_init (nf);
849 nf->node_runtime_index = save_node_runtime_index;
850 nf->flags = save_flags;
851 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852
Dave Barach9b8ffd92016-07-08 08:13:45 -0400853 /* fork the frame dispatch queue */
854 nm_clone->pending_frames = 0;
855 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
856 _vec_len (nm_clone->pending_frames) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857
Dave Barach9b8ffd92016-07-08 08:13:45 -0400858 /* fork nodes */
859 nm_clone->nodes = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100860
861 /* Allocate all nodes in single block for speed */
862 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
863
Dave Barach9b8ffd92016-07-08 08:13:45 -0400864 for (j = 0; j < vec_len (nm->nodes); j++)
865 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400866 clib_memcpy (n, nm->nodes[j], sizeof (*n));
867 /* none of the copied nodes have enqueue rights given out */
868 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
869 memset (&n->stats_total, 0, sizeof (n->stats_total));
870 memset (&n->stats_last_clear, 0,
871 sizeof (n->stats_last_clear));
872 vec_add1 (nm_clone->nodes, n);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100873 n++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400874 }
875 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200876 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
877 CLIB_CACHE_LINE_BYTES);
JingLiuZTE30af5da2017-07-24 10:53:31 +0800878 vec_foreach (rt,
879 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
Damjan Marione9f929b2017-03-16 11:32:09 +0100880 {
881 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200882 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100883 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100884 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100885 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100886 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
887 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100888 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889
Dave Barach9b8ffd92016-07-08 08:13:45 -0400890 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200891 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
892 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400893 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
Damjan Marione9f929b2017-03-16 11:32:09 +0100894 {
895 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200896 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100897 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100898 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100899 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100900 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
901 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100902 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200904 nm_clone->processes = vec_dup_aligned (nm->processes,
905 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906
Dave Barach9b8ffd92016-07-08 08:13:45 -0400907 /* zap the (per worker) frame freelists, etc */
908 nm_clone->frame_sizes = 0;
Florin Coras93992a92017-05-24 18:03:56 -0700909 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910
Dave Barach9b8ffd92016-07-08 08:13:45 -0400911 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100912
Dave Barach9b8ffd92016-07-08 08:13:45 -0400913 clib_mem_set_heap (oldheap);
Dave Barach80f54e22017-03-08 19:08:56 -0500914 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700915
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200916 vm_clone->error_main.counters = vec_dup_aligned
917 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
918 vm_clone->error_main.counters_last_clear = vec_dup_aligned
919 (vlib_mains[0]->error_main.counters_last_clear,
920 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921
Dave Barach9b8ffd92016-07-08 08:13:45 -0400922 /* Fork the vlib_buffer_main_t free lists, etc. */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100923 orig_freelist_pool = vm_clone->buffer_free_list_pool;
924 vm_clone->buffer_free_list_pool = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400925
926 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927 pool_foreach (fl_orig, orig_freelist_pool,
928 ({
Damjan Mariond1274cb2018-03-13 21:32:17 +0100929 pool_get_aligned (vm_clone->buffer_free_list_pool,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930 fl_clone, CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400931 ASSERT (fl_orig - orig_freelist_pool
Damjan Mariond1274cb2018-03-13 21:32:17 +0100932 == fl_clone - vm_clone->buffer_free_list_pool);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933
934 fl_clone[0] = fl_orig[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100935 fl_clone->buffers = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936 fl_clone->n_alloc = 0;
937 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400938/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939
Dave Barach9b8ffd92016-07-08 08:13:45 -0400940 worker_thread_index++;
941 }
942 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943 }
944 else
945 {
946 /* only have non-data-structure copy threads to create... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400947 for (i = 0; i < vec_len (tm->registrations); i++)
948 {
949 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950
Dave Barach9b8ffd92016-07-08 08:13:45 -0400951 for (j = 0; j < tr->count; j++)
952 {
953 vec_add2 (vlib_worker_threads, w, 1);
954 if (tr->mheap_size)
955 w->thread_mheap =
956 mheap_alloc (0 /* use VM */ , tr->mheap_size);
957 else
958 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200959 w->thread_stack =
960 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400961 w->thread_function = tr->function;
962 w->thread_function_arg = w;
963 w->instance_id = j;
964 w->elog_track.name =
965 (char *) format (0, "%s %d", tr->name, j + 1);
966 w->registration = tr;
967 vec_add1 (w->elog_track.name, 0);
968 elog_track_register (&vm->elog_main, &w->elog_track);
969 }
970 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 }
972
973 worker_thread_index = 1;
974
975 for (i = 0; i < vec_len (tm->registrations); i++)
976 {
Damjan Marion878c6092017-01-04 13:19:27 +0100977 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978 int j;
979
980 tr = tm->registrations[i];
981
982 if (tr->use_pthreads || tm->use_pthreads)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400983 {
984 for (j = 0; j < tr->count; j++)
985 {
986 w = vlib_worker_threads + worker_thread_index++;
Damjan Marion878c6092017-01-04 13:19:27 +0100987 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
988 w, 0);
989 if (err)
990 clib_error_report (err);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400991 }
992 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400994 {
995 uword c;
Damjan Marion878c6092017-01-04 13:19:27 +0100996 /* *INDENT-OFF* */
997 clib_bitmap_foreach (c, tr->coremask, ({
998 w = vlib_worker_threads + worker_thread_index++;
999 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
1000 w, c);
1001 if (err)
1002 clib_error_report (err);
1003 }));
1004 /* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001005 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001007 vlib_worker_thread_barrier_sync (vm);
1008 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009 return 0;
1010}
1011
1012VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
1013
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001014
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001015static inline void
1016worker_thread_node_runtime_update_internal (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017{
1018 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019 vlib_main_t *vm;
1020 vlib_node_main_t *nm, *nm_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021 vlib_main_t *vm_clone;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001022 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023 never_inline void
1024 vlib_node_runtime_sync_stats (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001025 vlib_node_runtime_t * r,
1026 uword n_calls,
1027 uword n_vectors, uword n_clocks);
1028
Damjan Marion586afd72017-04-05 19:18:20 +02001029 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031 vm = vlib_mains[0];
1032 nm = &vm->node_main;
1033
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
1035
Dave Barach9b8ffd92016-07-08 08:13:45 -04001036 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037 * Scrape all runtime stats, so we don't lose node runtime(s) with
1038 * pending counts, or throw away worker / io thread counts.
1039 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001040 for (j = 0; j < vec_len (nm->nodes); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001042 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043 n = nm->nodes[j];
1044 vlib_node_sync_stats (vm, n);
1045 }
1046
1047 for (i = 1; i < vec_len (vlib_mains); i++)
1048 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001049 vlib_node_t *n;
1050
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051 vm_clone = vlib_mains[i];
1052 nm_clone = &vm_clone->node_main;
1053
Dave Barach9b8ffd92016-07-08 08:13:45 -04001054 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1055 {
1056 n = nm_clone->nodes[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057
Dave Barach9b8ffd92016-07-08 08:13:45 -04001058 rt = vlib_node_get_runtime (vm_clone, n->index);
1059 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1060 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061 }
1062
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001063 /* Per-worker clone rebuilds are now done on each thread */
1064}
1065
1066
1067void
1068vlib_worker_thread_node_refork (void)
1069{
1070 vlib_main_t *vm, *vm_clone;
1071 vlib_node_main_t *nm, *nm_clone;
1072 vlib_node_t **old_nodes_clone;
1073 vlib_node_runtime_t *rt, *old_rt;
1074
1075 vlib_node_t *new_n_clone;
1076
1077 int j;
1078
1079 vm = vlib_mains[0];
1080 nm = &vm->node_main;
1081 vm_clone = vlib_get_main ();
1082 nm_clone = &vm_clone->node_main;
1083
1084 /* Re-clone error heap */
1085 u64 *old_counters = vm_clone->error_main.counters;
1086 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1087
1088 clib_memcpy (&vm_clone->error_main, &vm->error_main,
1089 sizeof (vm->error_main));
1090 j = vec_len (vm->error_main.counters) - 1;
1091 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
1092 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
1093 vm_clone->error_main.counters = old_counters;
1094 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1095
1096 nm_clone = &vm_clone->node_main;
1097 vec_free (nm_clone->next_frames);
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001098 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1099 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001100
1101 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001103 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1104 u32 save_node_runtime_index;
1105 u32 save_flags;
Damjan Marionbc20bdf2015-12-17 14:28:18 +01001106
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001107 save_node_runtime_index = nf->node_runtime_index;
1108 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1109 vlib_next_frame_init (nf);
1110 nf->node_runtime_index = save_node_runtime_index;
1111 nf->flags = save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001113
1114 old_nodes_clone = nm_clone->nodes;
1115 nm_clone->nodes = 0;
1116
1117 /* re-fork nodes */
1118
1119 /* Allocate all nodes in single block for speed */
1120 new_n_clone =
1121 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1122 for (j = 0; j < vec_len (nm->nodes); j++)
1123 {
1124 vlib_node_t *old_n_clone;
1125 vlib_node_t *new_n;
1126
1127 new_n = nm->nodes[j];
1128 old_n_clone = old_nodes_clone[j];
1129
1130 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
1131 /* none of the copied nodes have enqueue rights given out */
1132 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1133
1134 if (j >= vec_len (old_nodes_clone))
1135 {
1136 /* new node, set to zero */
1137 memset (&new_n_clone->stats_total, 0,
1138 sizeof (new_n_clone->stats_total));
1139 memset (&new_n_clone->stats_last_clear, 0,
1140 sizeof (new_n_clone->stats_last_clear));
1141 }
1142 else
1143 {
1144 /* Copy stats if the old data is valid */
1145 clib_memcpy (&new_n_clone->stats_total,
1146 &old_n_clone->stats_total,
1147 sizeof (new_n_clone->stats_total));
1148 clib_memcpy (&new_n_clone->stats_last_clear,
1149 &old_n_clone->stats_last_clear,
1150 sizeof (new_n_clone->stats_last_clear));
1151
1152 /* keep previous node state */
1153 new_n_clone->state = old_n_clone->state;
1154 }
1155 vec_add1 (nm_clone->nodes, new_n_clone);
1156 new_n_clone++;
1157 }
1158 /* Free the old node clones */
1159 clib_mem_free (old_nodes_clone[0]);
1160
1161 vec_free (old_nodes_clone);
1162
1163
1164 /* re-clone internal nodes */
1165 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1166 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001167 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1168 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001169
1170 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1171 {
1172 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1173 rt->thread_index = vm_clone->thread_index;
1174 /* copy runtime_data, will be overwritten later for existing rt */
1175 if (n->runtime_data && n->runtime_data_bytes > 0)
1176 clib_memcpy (rt->runtime_data, n->runtime_data,
1177 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1178 n->runtime_data_bytes));
1179 }
1180
1181 for (j = 0; j < vec_len (old_rt); j++)
1182 {
1183 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1184 rt->state = old_rt[j].state;
1185 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
1186 VLIB_NODE_RUNTIME_DATA_SIZE);
1187 }
1188
1189 vec_free (old_rt);
1190
1191 /* re-clone input nodes */
1192 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1193 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001194 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1195 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001196
1197 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1198 {
1199 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1200 rt->thread_index = vm_clone->thread_index;
1201 /* copy runtime_data, will be overwritten later for existing rt */
1202 if (n->runtime_data && n->runtime_data_bytes > 0)
1203 clib_memcpy (rt->runtime_data, n->runtime_data,
1204 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1205 n->runtime_data_bytes));
1206 }
1207
1208 for (j = 0; j < vec_len (old_rt); j++)
1209 {
1210 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1211 rt->state = old_rt[j].state;
1212 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
1213 VLIB_NODE_RUNTIME_DATA_SIZE);
1214 }
1215
1216 vec_free (old_rt);
1217
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001218 nm_clone->processes = vec_dup_aligned (nm->processes,
1219 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001220}
1221
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001222void
1223vlib_worker_thread_node_runtime_update (void)
1224{
1225 /*
1226 * Make a note that we need to do a node runtime update
1227 * prior to releasing the barrier.
1228 */
1229 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230}
1231
Pavel Kotucek1e765832016-09-23 08:54:14 +02001232u32
1233unformat_sched_policy (unformat_input_t * input, va_list * args)
1234{
1235 u32 *r = va_arg (*args, u32 *);
1236
1237 if (0);
1238#define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1239 foreach_sched_policy
1240#undef _
1241 else
1242 return 0;
1243 return 1;
1244}
1245
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246static clib_error_t *
1247cpu_config (vlib_main_t * vm, unformat_input_t * input)
1248{
1249 vlib_thread_registration_t *tr;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001250 uword *p;
1251 vlib_thread_main_t *tm = &vlib_thread_main;
1252 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253 u64 coremask;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001254 uword *bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255 u32 count;
1256
1257 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
Pavel Kotucek1e765832016-09-23 08:54:14 +02001258
Dave Barach9b8ffd92016-07-08 08:13:45 -04001259 tm->n_thread_stacks = 1; /* account for main thread */
Pavel Kotucek1e765832016-09-23 08:54:14 +02001260 tm->sched_policy = ~0;
1261 tm->sched_priority = ~0;
Damjan Marion7752b392018-07-03 17:24:45 +02001262 tm->skip_cores = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263
1264 tr = tm->next;
1265
1266 while (tr)
1267 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001268 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269 tr = tr->next;
1270 }
1271
Dave Barach9b8ffd92016-07-08 08:13:45 -04001272 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273 {
Damjan Marionbf741472016-06-13 22:49:44 +02001274 if (unformat (input, "use-pthreads"))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001275 tm->use_pthreads = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001277 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001278 else if (unformat (input, "main-core %u", &tm->main_lcore))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001279 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001280 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001281 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001282 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001283 {
1284 p = hash_get_mem (tm->thread_registrations_by_name, name);
1285 if (p == 0)
1286 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287
Dave Barach9b8ffd92016-07-08 08:13:45 -04001288 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001289
Dave Barach9b8ffd92016-07-08 08:13:45 -04001290 if (tr->use_pthreads)
1291 return clib_error_return (0,
1292 "coremask cannot be set for '%s' threads",
1293 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001294
Dave Barach9b8ffd92016-07-08 08:13:45 -04001295 tr->coremask = clib_bitmap_set_multiple
1296 (tr->coremask, 0, coremask, BITS (coremask));
1297 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1298 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001299 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001300 &bitmap))
1301 {
1302 p = hash_get_mem (tm->thread_registrations_by_name, name);
1303 if (p == 0)
1304 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001305
Dave Barach9b8ffd92016-07-08 08:13:45 -04001306 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001307
Dave Barach9b8ffd92016-07-08 08:13:45 -04001308 if (tr->use_pthreads)
1309 return clib_error_return (0,
1310 "corelist cannot be set for '%s' threads",
1311 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312
Dave Barach9b8ffd92016-07-08 08:13:45 -04001313 tr->coremask = bitmap;
1314 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1315 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001316 else
1317 if (unformat
1318 (input, "scheduler-policy %U", unformat_sched_policy,
1319 &tm->sched_policy))
1320 ;
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001321 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
Pavel Kotucek1e765832016-09-23 08:54:14 +02001322 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001323 else if (unformat (input, "%s %u", &name, &count))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001324 {
1325 p = hash_get_mem (tm->thread_registrations_by_name, name);
1326 if (p == 0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001327 return clib_error_return (0, "no such thread type 3 '%s'", name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001328
1329 tr = (vlib_thread_registration_t *) p[0];
1330 if (tr->fixed_count)
1331 return clib_error_return
1332 (0, "number of %s threads not configurable", tr->name);
1333 tr->count = count;
1334 }
1335 else
1336 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001337 }
1338
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001339 if (tm->sched_priority != ~0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001340 {
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001341 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001342 {
1343 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1344 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1345 if (tm->sched_priority > prio_max)
1346 tm->sched_priority = prio_max;
1347 if (tm->sched_priority < prio_min)
1348 tm->sched_priority = prio_min;
1349 }
1350 else
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001351 {
1352 return clib_error_return
1353 (0,
1354 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1355 tm->sched_priority);
1356 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001357 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001358 tr = tm->next;
1359
1360 if (!tm->thread_prefix)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001361 tm->thread_prefix = format (0, "vpp");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001362
1363 while (tr)
1364 {
1365 tm->n_thread_stacks += tr->count;
1366 tm->n_pthreads += tr->count * tr->use_pthreads;
Damjan Marion878c6092017-01-04 13:19:27 +01001367 tm->n_threads += tr->count * (tr->use_pthreads == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368 tr = tr->next;
1369 }
1370
1371 return 0;
1372}
1373
1374VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1375
Damjan Marion7bee80c2017-04-26 15:32:12 +02001376#if !defined (__x86_64__) && !defined (__i386__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001377void
1378__sync_fetch_and_add_8 (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001379{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001380 fformat (stderr, "%s called\n", __FUNCTION__);
1381 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001382}
Dave Barach9b8ffd92016-07-08 08:13:45 -04001383
1384void
1385__sync_add_and_fetch_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}
1390#endif
1391
1392void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -04001393void
1394vnet_main_fixup (vlib_fork_fixup_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001395{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001396}
1397
1398void
1399vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1400{
1401 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001402
1403 if (vlib_mains == 0)
1404 return;
1405
Damjan Marion586afd72017-04-05 19:18:20 +02001406 ASSERT (vlib_get_thread_index () == 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001407 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001408
1409 switch (which)
1410 {
1411 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1412 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1413 break;
1414
1415 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001416 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001417 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001418 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001419}
1420
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001421 /*
1422 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1423 * based on a test based heuristic that barrier should be open for at least
1424 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1425 * point it is probably too late to make a difference)
1426 */
1427
1428#ifndef BARRIER_MINIMUM_OPEN_LIMIT
1429#define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1430#endif
1431
1432#ifndef BARRIER_MINIMUM_OPEN_FACTOR
1433#define BARRIER_MINIMUM_OPEN_FACTOR 3
1434#endif
1435
Dave Barach9b8ffd92016-07-08 08:13:45 -04001436void
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001437vlib_worker_thread_barrier_sync_int (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001438{
1439 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001440 f64 now;
1441 f64 t_entry;
1442 f64 t_open;
1443 f64 t_closed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001444 u32 count;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001445
Dave Barach80f54e22017-03-08 19:08:56 -05001446 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001447 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001448
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001449 ASSERT (vlib_get_thread_index () == 0);
1450
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451 count = vec_len (vlib_mains) - 1;
1452
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001453 /* Record entry relative to last close */
1454 now = vlib_time_now (vm);
1455 t_entry = now - vm->barrier_epoch;
1456
Ed Warnickecb9cada2015-12-08 15:45:58 -07001457 /* Tolerate recursive calls */
1458 if (++vlib_worker_threads[0].recursion_level > 1)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001459 {
1460 barrier_trace_sync_rec (t_entry);
1461 return;
1462 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463
Bud Grise42f20062016-03-16 13:09:46 -04001464 vlib_worker_threads[0].barrier_sync_count++;
1465
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001466 /* Enforce minimum barrier open time to minimize packet loss */
1467 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
1468 while ((now = vlib_time_now (vm)) < vm->barrier_no_close_before)
1469 ;
1470
1471 /* Record time of closure */
1472 t_open = now - vm->barrier_epoch;
1473 vm->barrier_epoch = now;
1474
1475 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476
1477 *vlib_worker_threads->wait_at_barrier = 1;
1478 while (*vlib_worker_threads->workers_at_barrier != count)
1479 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001480 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001481 {
1482 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1483 os_panic ();
1484 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001485 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001486
1487 t_closed = now - vm->barrier_epoch;
1488
1489 barrier_trace_sync (t_entry, t_open, t_closed);
1490
Ed Warnickecb9cada2015-12-08 15:45:58 -07001491}
1492
Dave Barach1ddbc012018-06-13 09:26:05 -04001493void vlib_stat_segment_lock (void) __attribute__ ((weak));
1494void
1495vlib_stat_segment_lock (void)
1496{
1497}
1498
1499void vlib_stat_segment_unlock (void) __attribute__ ((weak));
1500void
1501vlib_stat_segment_unlock (void)
1502{
1503}
1504
Dave Barach9b8ffd92016-07-08 08:13:45 -04001505void
1506vlib_worker_thread_barrier_release (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507{
1508 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001509 f64 now;
1510 f64 minimum_open;
1511 f64 t_entry;
1512 f64 t_closed_total;
1513 f64 t_update_main = 0.0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001514 int refork_needed = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001515
Dave Barach80f54e22017-03-08 19:08:56 -05001516 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001517 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001518
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001519 ASSERT (vlib_get_thread_index () == 0);
1520
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001521
1522 now = vlib_time_now (vm);
1523 t_entry = now - vm->barrier_epoch;
1524
Ed Warnickecb9cada2015-12-08 15:45:58 -07001525 if (--vlib_worker_threads[0].recursion_level > 0)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001526 {
1527 barrier_trace_release_rec (t_entry);
1528 return;
1529 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001530
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001531 /* Update (all) node runtimes before releasing the barrier, if needed */
1532 if (vm->need_vlib_worker_thread_node_runtime_update)
1533 {
Dave Barach1ddbc012018-06-13 09:26:05 -04001534 /*
1535 * Lock stat segment here, so we's safe when
1536 * rebuilding the stat segment node clones from the
1537 * stat thread...
1538 */
1539 vlib_stat_segment_lock ();
1540
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001541 /* Do stats elements on main thread */
1542 worker_thread_node_runtime_update_internal ();
1543 vm->need_vlib_worker_thread_node_runtime_update = 0;
1544
1545 /* Do per thread rebuilds in parallel */
1546 refork_needed = 1;
1547 clib_smp_atomic_add (vlib_worker_threads->node_reforks_required,
1548 (vec_len (vlib_mains) - 1));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001549 now = vlib_time_now (vm);
1550 t_update_main = now - vm->barrier_epoch;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001551 }
1552
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001553 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001554
1555 *vlib_worker_threads->wait_at_barrier = 0;
1556
1557 while (*vlib_worker_threads->workers_at_barrier > 0)
1558 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001559 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001560 {
1561 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1562 os_panic ();
1563 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001565
1566 /* Wait for reforks before continuing */
1567 if (refork_needed)
1568 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001569 now = vlib_time_now (vm);
1570
1571 deadline = now + BARRIER_SYNC_TIMEOUT;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001572
1573 while (*vlib_worker_threads->node_reforks_required > 0)
1574 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001575 if ((now = vlib_time_now (vm)) > deadline)
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001576 {
1577 fformat (stderr, "%s: worker thread refork deadlock\n",
1578 __FUNCTION__);
1579 os_panic ();
1580 }
1581 }
Dave Barach1ddbc012018-06-13 09:26:05 -04001582 vlib_stat_segment_unlock ();
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001583 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001584
1585 t_closed_total = now - vm->barrier_epoch;
1586
1587 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1588
1589 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1590 {
1591 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1592 }
1593
1594 vm->barrier_no_close_before = now + minimum_open;
1595
1596 /* Record barrier epoch (used to enforce minimum open time) */
1597 vm->barrier_epoch = now;
1598
1599 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1600
Ed Warnickecb9cada2015-12-08 15:45:58 -07001601}
1602
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001603/*
1604 * Check the frame queue to see if any frames are available.
Dave Barach9b8ffd92016-07-08 08:13:45 -04001605 * If so, pull the packets off the frames and put them to
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001606 * the handoff node.
1607 */
Damjan Marione9d52d52017-03-09 15:42:26 +01001608int
1609vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001610{
Damjan Marion586afd72017-04-05 19:18:20 +02001611 u32 thread_id = vm->thread_index;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001612 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001613 vlib_frame_queue_elt_t *elt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001614 u32 *from, *to;
1615 vlib_frame_t *f;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001616 int msg_type;
1617 int processed = 0;
1618 u32 n_left_to_node;
1619 u32 vectors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001620
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001621 ASSERT (fq);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001622 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001623
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001624 if (PREDICT_FALSE (fqm->node_index == ~0))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001625 return 0;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001626 /*
1627 * Gather trace data for frame queues
1628 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001629 if (PREDICT_FALSE (fq->trace))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001630 {
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001631 frame_queue_trace_t *fqt;
1632 frame_queue_nelt_counter_t *fqh;
1633 u32 elix;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001634
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001635 fqt = &fqm->frame_queue_traces[thread_id];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001636
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001637 fqt->nelts = fq->nelts;
1638 fqt->head = fq->head;
1639 fqt->head_hint = fq->head_hint;
1640 fqt->tail = fq->tail;
1641 fqt->threshold = fq->vector_threshold;
1642 fqt->n_in_use = fqt->tail - fqt->head;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001643 if (fqt->n_in_use >= fqt->nelts)
1644 {
1645 // if beyond max then use max
1646 fqt->n_in_use = fqt->nelts - 1;
1647 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001648
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001649 /* Record the number of elements in use in the histogram */
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001650 fqh = &fqm->frame_queue_histogram[thread_id];
Dave Barach9b8ffd92016-07-08 08:13:45 -04001651 fqh->count[fqt->n_in_use]++;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001652
1653 /* Record a snapshot of the elements in use */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001654 for (elix = 0; elix < fqt->nelts; elix++)
1655 {
1656 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1657 if (1 || elt->valid)
1658 {
1659 fqt->n_vectors[elix] = elt->n_vectors;
1660 }
1661 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001662 fqt->written = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001663 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001664
1665 while (1)
1666 {
1667 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001668 {
1669 fq->head_hint = fq->head;
1670 return processed;
1671 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001672
Dave Barach9b8ffd92016-07-08 08:13:45 -04001673 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001674
1675 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001676 {
1677 fq->head_hint = fq->head;
1678 return processed;
1679 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001680
1681 from = elt->buffer_index;
1682 msg_type = elt->msg_type;
1683
1684 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1685 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1686
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001687 f = vlib_get_frame_to_node (vm, fqm->node_index);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001688
1689 to = vlib_frame_vector_args (f);
1690
1691 n_left_to_node = elt->n_vectors;
1692
1693 while (n_left_to_node >= 4)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001694 {
1695 to[0] = from[0];
1696 to[1] = from[1];
1697 to[2] = from[2];
1698 to[3] = from[3];
1699 to += 4;
1700 from += 4;
1701 n_left_to_node -= 4;
1702 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001703
1704 while (n_left_to_node > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001705 {
1706 to[0] = from[0];
1707 to++;
1708 from++;
1709 n_left_to_node--;
1710 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001711
1712 vectors += elt->n_vectors;
1713 f->n_vectors = elt->n_vectors;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001714 vlib_put_frame_to_node (vm, fqm->node_index, f);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001715
1716 elt->valid = 0;
1717 elt->n_vectors = 0;
1718 elt->msg_type = 0xfefefefe;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001719 CLIB_MEMORY_BARRIER ();
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001720 fq->head++;
1721 processed++;
1722
1723 /*
1724 * Limit the number of packets pushed into the graph
1725 */
1726 if (vectors >= fq->vector_threshold)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001727 {
1728 fq->head_hint = fq->head;
1729 return processed;
1730 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001731 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001732 ASSERT (0);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001733 return processed;
1734}
1735
Dave Barach9b8ffd92016-07-08 08:13:45 -04001736void
1737vlib_worker_thread_fn (void *arg)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001738{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001739 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
Damjan Marion878c6092017-01-04 13:19:27 +01001740 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -04001741 vlib_main_t *vm = vlib_get_main ();
Damjan Marione9f929b2017-03-16 11:32:09 +01001742 clib_error_t *e;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001743
Damjan Marion586afd72017-04-05 19:18:20 +02001744 ASSERT (vm->thread_index == vlib_get_thread_index ());
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001745
1746 vlib_worker_thread_init (w);
1747 clib_time_init (&vm->clib_time);
1748 clib_mem_set_heap (w->thread_mheap);
1749
Damjan Marioneb743fa2017-03-20 16:34:15 +01001750 /* Wait until the dpdk init sequence is complete */
1751 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1752 vlib_worker_thread_barrier_check ();
1753
Damjan Marione9f929b2017-03-16 11:32:09 +01001754 e = vlib_call_init_exit_functions
1755 (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1756 if (e)
1757 clib_error_report (e);
1758
Damjan Marione9d52d52017-03-09 15:42:26 +01001759 vlib_worker_loop (vm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001760}
1761
Dave Barach9b8ffd92016-07-08 08:13:45 -04001762/* *INDENT-OFF* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001763VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1764 .name = "workers",
1765 .short_name = "wk",
1766 .function = vlib_worker_thread_fn,
1767};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001768/* *INDENT-ON* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001769
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001770u32
1771vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1772{
1773 vlib_thread_main_t *tm = vlib_get_thread_main ();
1774 vlib_frame_queue_main_t *fqm;
1775 vlib_frame_queue_t *fq;
1776 int i;
1777
1778 if (frame_queue_nelts == 0)
1779 frame_queue_nelts = FRAME_QUEUE_NELTS;
1780
1781 vec_add2 (tm->frame_queue_mains, fqm, 1);
1782
1783 fqm->node_index = node_index;
1784
1785 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1786 _vec_len (fqm->vlib_frame_queues) = 0;
1787 for (i = 0; i < tm->n_vlib_mains; i++)
1788 {
1789 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1790 vec_add1 (fqm->vlib_frame_queues, fq);
1791 }
1792
1793 return (fqm - tm->frame_queue_mains);
1794}
1795
Damjan Marion878c6092017-01-04 13:19:27 +01001796int
1797vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1798{
1799 vlib_thread_main_t *tm = vlib_get_thread_main ();
1800
1801 if (tm->extern_thread_mgmt)
1802 return -1;
1803
1804 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1805 tm->extern_thread_mgmt = 1;
1806 return 0;
1807}
1808
Dave Barach69128d02017-09-26 10:54:34 -04001809void
1810vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1811 args)
1812{
1813 ASSERT (vlib_get_thread_index () == 0);
1814 vlib_process_signal_event (vlib_get_main (), args->node_index,
1815 args->type_opaque, args->data);
1816}
1817
1818void *rpc_call_main_thread_cb_fn;
1819
1820void
1821vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1822{
1823 if (rpc_call_main_thread_cb_fn)
1824 {
1825 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1826 (*fp) (callback, args, arg_size);
1827 }
1828 else
1829 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1830}
1831
Dave Barach9b8ffd92016-07-08 08:13:45 -04001832clib_error_t *
1833threads_init (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001834{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001835 return 0;
1836}
1837
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001838VLIB_INIT_FUNCTION (threads_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001839
1840/*
1841 * fd.io coding-style-patch-verification: ON
1842 *
1843 * Local Variables:
1844 * eval: (c-set-style "gnu")
1845 * End:
1846 */