blob: 9112538724805ff0a42e4eef49308f2c222fbd92 [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>
Mohsin Kazmi5d64c782018-09-11 20:27:09 +020020#include <vppinfra/linux/sysfs.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021#include <vlib/vlib.h>
22
23#include <vlib/threads.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024#include <vlib/unix/cj.h>
25
Ole Troan233e4682019-05-16 15:01:34 +020026#include <vlib/stat_weak_inlines.h>
27
Ed Warnickecb9cada2015-12-08 15:45:58 -070028DECLARE_CJ_GLOBAL_LOG;
29
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
Dave Barach9b8ffd92016-07-08 08:13:45 -040031u32
32vl (void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -070033{
34 return vec_len (p);
35}
36
Damjan Marion6a7acc22016-12-19 16:28:36 +010037vlib_worker_thread_t *vlib_worker_threads;
Ed Warnickecb9cada2015-12-08 15:45:58 -070038vlib_thread_main_t vlib_thread_main;
39
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010040/*
41 * Barrier tracing can be enabled on a normal build to collect information
42 * on barrier use, including timings and call stacks. Deliberately not
43 * keyed off CLIB_DEBUG, because that can add significant overhead which
44 * imapacts observed timings.
45 */
46
Dave Barachc3a06552018-10-01 09:25:32 -040047u32
48elog_global_id_for_msg_name (const char *msg_name)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010049{
50 uword *p, r;
51 static uword *h;
52 u8 *name_copy;
53
54 if (!h)
55 h = hash_create_string (0, sizeof (uword));
56
57 p = hash_get_mem (h, msg_name);
58 if (p)
59 return p[0];
60 r = elog_string (&vlib_global_main.elog_main, "%s", msg_name);
61
62 name_copy = format (0, "%s%c", msg_name, 0);
63
64 hash_set_mem (h, name_copy, r);
65
66 return r;
67}
68
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010069static inline void
70barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
71{
Dave Barach88c6e002018-09-30 15:54:06 -040072 if (!vlib_worker_threads->barrier_elog_enabled)
73 return;
74
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010075 /* *INDENT-OFF* */
76 ELOG_TYPE_DECLARE (e) =
77 {
Dave Barach88c6e002018-09-30 15:54:06 -040078 .format = "bar-trace-%s-#%d",
79 .format_args = "T4i4",
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010080 };
81 /* *INDENT-ON* */
82 struct
83 {
Dave Barach88c6e002018-09-30 15:54:06 -040084 u32 caller, count, t_entry, t_open, t_closed;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010085 } *ed = 0;
86
87 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
88 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
Dave Barachc3a06552018-10-01 09:25:32 -040089 ed->caller = elog_global_id_for_msg_name
90 (vlib_worker_threads[0].barrier_caller);
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010091 ed->t_entry = (int) (1000000.0 * t_entry);
92 ed->t_open = (int) (1000000.0 * t_open);
93 ed->t_closed = (int) (1000000.0 * t_closed);
94}
95
96static inline void
97barrier_trace_sync_rec (f64 t_entry)
98{
Dave Barach88c6e002018-09-30 15:54:06 -040099 if (!vlib_worker_threads->barrier_elog_enabled)
100 return;
101
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100102 /* *INDENT-OFF* */
103 ELOG_TYPE_DECLARE (e) =
104 {
Dave Barach88c6e002018-09-30 15:54:06 -0400105 .format = "bar-syncrec-%s-#%d",
106 .format_args = "T4i4",
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100107 };
108 /* *INDENT-ON* */
109 struct
110 {
Dave Barach88c6e002018-09-30 15:54:06 -0400111 u32 caller, depth;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100112 } *ed = 0;
113
114 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
115 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
Dave Barachc3a06552018-10-01 09:25:32 -0400116 ed->caller = elog_global_id_for_msg_name
117 (vlib_worker_threads[0].barrier_caller);
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100118}
119
120static inline void
121barrier_trace_release_rec (f64 t_entry)
122{
Dave Barach88c6e002018-09-30 15:54:06 -0400123 if (!vlib_worker_threads->barrier_elog_enabled)
124 return;
125
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100126 /* *INDENT-OFF* */
127 ELOG_TYPE_DECLARE (e) =
128 {
Dave Barach88c6e002018-09-30 15:54:06 -0400129 .format = "bar-relrrec-#%d",
130 .format_args = "i4",
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100131 };
132 /* *INDENT-ON* */
133 struct
134 {
Dave Barach88c6e002018-09-30 15:54:06 -0400135 u32 depth;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100136 } *ed = 0;
137
138 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100139 ed->depth = (int) vlib_worker_threads[0].recursion_level;
140}
141
142static inline void
143barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
144{
Dave Barach88c6e002018-09-30 15:54:06 -0400145 if (!vlib_worker_threads->barrier_elog_enabled)
146 return;
147
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100148 /* *INDENT-OFF* */
149 ELOG_TYPE_DECLARE (e) =
150 {
Dave Barach88c6e002018-09-30 15:54:06 -0400151 .format = "bar-rel-#%d-e%d-u%d-t%d",
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100152 .format_args = "i4i4i4i4",
153 };
154 /* *INDENT-ON* */
155 struct
156 {
Dave Barach88c6e002018-09-30 15:54:06 -0400157 u32 count, t_entry, t_update_main, t_closed_total;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100158 } *ed = 0;
159
160 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
161 ed->t_entry = (int) (1000000.0 * t_entry);
162 ed->t_update_main = (int) (1000000.0 * t_update_main);
163 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
164 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
165
166 /* Reset context for next trace */
167 vlib_worker_threads[0].barrier_context = NULL;
168}
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100169
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170uword
Damjan Marionf55f9b82017-05-10 21:06:28 +0200171os_get_nthreads (void)
Dave Barach01d86c72016-08-08 15:13:42 -0400172{
Dave Barach2180bac2019-05-10 15:25:10 -0400173 return vec_len (vlib_thread_stacks);
Dave Barach01d86c72016-08-08 15:13:42 -0400174}
175
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176void
177vlib_set_thread_name (char *name)
178{
179 int pthread_setname_np (pthread_t __target_thread, const char *__name);
Dave Barachb2a6e252016-07-27 10:00:58 -0400180 int rv;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400181 pthread_t thread = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Dave Barach9b8ffd92016-07-08 08:13:45 -0400183 if (thread)
Dave Barachb2a6e252016-07-27 10:00:58 -0400184 {
185 rv = pthread_setname_np (thread, name);
186 if (rv)
Ed Warnicke853e7202016-08-12 11:42:26 -0700187 clib_warning ("pthread_setname_np returned %d", rv);
Dave Barachb2a6e252016-07-27 10:00:58 -0400188 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189}
190
Dave Barach9b8ffd92016-07-08 08:13:45 -0400191static int
192sort_registrations_by_no_clone (void *a0, void *a1)
193{
194 vlib_thread_registration_t **tr0 = a0;
195 vlib_thread_registration_t **tr1 = a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Dave Barach9b8ffd92016-07-08 08:13:45 -0400197 return ((i32) ((*tr0)->no_data_structure_clone)
198 - ((i32) ((*tr1)->no_data_structure_clone)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199}
200
201static uword *
Damjan Marion01914ce2017-09-14 19:04:50 +0200202clib_sysfs_list_to_bitmap (char *filename)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203{
204 FILE *fp;
205 uword *r = 0;
206
207 fp = fopen (filename, "r");
208
209 if (fp != NULL)
210 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400211 u8 *buffer = 0;
212 vec_validate (buffer, 256 - 1);
213 if (fgets ((char *) buffer, 256, fp))
214 {
215 unformat_input_t in;
216 unformat_init_string (&in, (char *) buffer,
217 strlen ((char *) buffer));
Dave Barachb2a6e252016-07-27 10:00:58 -0400218 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
Ed Warnicke853e7202016-08-12 11:42:26 -0700219 clib_warning ("unformat_bitmap_list failed");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400220 unformat_free (&in);
221 }
222 vec_free (buffer);
223 fclose (fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 }
225 return r;
226}
227
228
229/* Called early in the init sequence */
230
231clib_error_t *
232vlib_thread_init (vlib_main_t * vm)
233{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400234 vlib_thread_main_t *tm = &vlib_thread_main;
235 vlib_worker_thread_t *w;
236 vlib_thread_registration_t *tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 u32 n_vlib_mains = 1;
238 u32 first_index = 1;
239 u32 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400240 uword *avail_cpu;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
242 /* get bitmaps of active cpu cores and sockets */
243 tm->cpu_core_bitmap =
Damjan Marion01914ce2017-09-14 19:04:50 +0200244 clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245 tm->cpu_socket_bitmap =
Damjan Marion01914ce2017-09-14 19:04:50 +0200246 clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Dave Barach9b8ffd92016-07-08 08:13:45 -0400248 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
250 /* skip cores */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400251 for (i = 0; i < tm->skip_cores; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400253 uword c = clib_bitmap_first_set (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254 if (c == ~0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400255 return clib_error_return (0, "no available cpus to skip");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
Dave Barach9b8ffd92016-07-08 08:13:45 -0400257 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 }
259
260 /* grab cpu for main thread */
Damjan Marion858151f2018-07-11 10:51:00 +0200261 if (tm->main_lcore == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 {
Damjan Marion858151f2018-07-11 10:51:00 +0200263 /* if main-lcore is not set, we try to use lcore 1 */
264 if (clib_bitmap_get (avail_cpu, 1))
265 tm->main_lcore = 1;
266 else
267 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400268 if (tm->main_lcore == (u8) ~ 0)
269 return clib_error_return (0, "no available cpus to be used for the"
270 " main thread");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 }
272 else
273 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400274 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
275 return clib_error_return (0, "cpu %u is not available to be used"
276 " for the main thread", tm->main_lcore);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400278 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
280 /* assume that there is socket 0 only if there is no data from sysfs */
281 if (!tm->cpu_socket_bitmap)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400282 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
Christophe Fontainefef15b42016-04-09 12:38:49 +0900284 /* pin main thread to main_lcore */
Damjan Marion878c6092017-01-04 13:19:27 +0100285 if (tm->cb.vlib_thread_set_lcore_cb)
286 {
287 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
288 }
Damjan Marion858151f2018-07-11 10:51:00 +0200289 else
290 {
291 cpu_set_t cpuset;
292 CPU_ZERO (&cpuset);
293 CPU_SET (tm->main_lcore, &cpuset);
294 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
295 }
Christophe Fontainefef15b42016-04-09 12:38:49 +0900296
Dave Barach2180bac2019-05-10 15:25:10 -0400297 /* Set up thread 0 */
298 vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400299 _vec_len (vlib_worker_threads) = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300 w = vlib_worker_threads;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400301 w->thread_mheap = clib_mem_get_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 w->thread_stack = vlib_thread_stacks[0];
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200303 w->cpu_id = tm->main_lcore;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400304 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200305 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 tm->n_vlib_mains = 1;
307
Pavel Kotucek1e765832016-09-23 08:54:14 +0200308 if (tm->sched_policy != ~0)
309 {
310 struct sched_param sched_param;
311 if (!sched_getparam (w->lwp, &sched_param))
312 {
313 if (tm->sched_priority != ~0)
314 sched_param.sched_priority = tm->sched_priority;
315 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
316 }
317 }
318
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 /* assign threads to cores and set n_vlib_mains */
320 tr = tm->next;
321
322 while (tr)
323 {
324 vec_add1 (tm->registrations, tr);
325 tr = tr->next;
326 }
327
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
330 for (i = 0; i < vec_len (tm->registrations); i++)
331 {
332 int j;
333 tr = tm->registrations[i];
334 tr->first_index = first_index;
335 first_index += tr->count;
336 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
337
338 /* construct coremask */
339 if (tr->use_pthreads || !tr->count)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400340 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341
342 if (tr->coremask)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343 {
344 uword c;
345 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346 clib_bitmap_foreach (c, tr->coremask, ({
347 if (clib_bitmap_get(avail_cpu, c) == 0)
348 return clib_error_return (0, "cpu %u is not available to be used"
349 " for the '%s' thread",c, tr->name);
350
351 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
352 }));
Dave Barach2180bac2019-05-10 15:25:10 -0400353 /* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356 {
357 for (j = 0; j < tr->count; j++)
358 {
359 uword c = clib_bitmap_first_set (avail_cpu);
360 if (c == ~0)
361 return clib_error_return (0,
362 "no available cpus to be used for"
363 " the '%s' thread", tr->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364
Dave Barach9b8ffd92016-07-08 08:13:45 -0400365 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
366 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
367 }
368 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 }
370
Dave Barach9b8ffd92016-07-08 08:13:45 -0400371 clib_bitmap_free (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372
373 tm->n_vlib_mains = n_vlib_mains;
374
Dave Barach2180bac2019-05-10 15:25:10 -0400375 /*
376 * Allocate the remaining worker threads, and thread stack vector slots
377 * from now on, calls to os_get_nthreads() will return the correct
378 * answer.
379 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400380 vec_validate_aligned (vlib_worker_threads, first_index - 1,
381 CLIB_CACHE_LINE_BYTES);
Dave Barach2180bac2019-05-10 15:25:10 -0400382 vec_validate (vlib_thread_stacks, vec_len (vlib_worker_threads) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 return 0;
384}
385
Dave Barach9b8ffd92016-07-08 08:13:45 -0400386vlib_frame_queue_t *
387vlib_frame_queue_alloc (int nelts)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400389 vlib_frame_queue_t *fq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Dave Barach9b8ffd92016-07-08 08:13:45 -0400391 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400392 clib_memset (fq, 0, sizeof (*fq));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 fq->nelts = nelts;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400394 fq->vector_threshold = 128; // packets
395 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
397 if (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400398 {
399 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
400 fformat (stderr, "WARNING: fq->tail unaligned\n");
401 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
402 fformat (stderr, "WARNING: fq->head unaligned\n");
403 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
404 fformat (stderr, "WARNING: fq->elts unaligned\n");
405
406 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
407 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
408 sizeof (fq->elts[0]));
409 if (nelts & (nelts - 1))
410 {
411 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
412 abort ();
413 }
414 }
415
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 return (fq);
417}
418
419void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400420void
421vl_msg_api_handler_no_free (void *v)
422{
423}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
425/* Turned off, save as reference material... */
426#if 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400427static inline int
428vlib_frame_queue_dequeue_internal (int thread_id,
429 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430{
431 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
432 vlib_frame_queue_elt_t *elt;
433 vlib_frame_t *f;
434 vlib_pending_frame_t *p;
435 vlib_node_runtime_t *r;
436 u32 node_runtime_index;
437 int msg_type;
438 u64 before;
439 int processed = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400440
441 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
443 while (1)
444 {
445 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
Dave Barach9b8ffd92016-07-08 08:13:45 -0400448 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449
450 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400451 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452
Dave Barach9b8ffd92016-07-08 08:13:45 -0400453 before = clib_cpu_time_now ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454
455 f = elt->frame;
456 node_runtime_index = elt->node_runtime_index;
457 msg_type = elt->msg_type;
458
459 switch (msg_type)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400460 {
461 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
462 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
463 /* note fallthrough... */
464 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
465 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
466 node_runtime_index);
467 vlib_frame_free (vm, r, f);
468 break;
469 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
470 vec_add2 (vm->node_main.pending_frames, p, 1);
471 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
472 p->node_runtime_index = elt->node_runtime_index;
473 p->frame_index = vlib_frame_index (vm, f);
474 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
475 fq->dequeue_vectors += (u64) f->n_vectors;
476 break;
477 case VLIB_FRAME_QUEUE_ELT_API_MSG:
478 vl_msg_api_handler_no_free (f);
479 break;
480 default:
481 clib_warning ("bogus frame queue message, type %d", msg_type);
482 break;
483 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484 elt->valid = 0;
485 fq->dequeues++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400486 fq->dequeue_ticks += clib_cpu_time_now () - before;
487 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 fq->head++;
489 processed++;
490 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400491 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 return processed;
493}
494
Dave Barach9b8ffd92016-07-08 08:13:45 -0400495int
496vlib_frame_queue_dequeue (int thread_id,
497 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498{
499 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
500}
501
Dave Barach9b8ffd92016-07-08 08:13:45 -0400502int
503vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
504 u32 frame_queue_index, vlib_frame_t * frame,
505 vlib_frame_queue_msg_type_t type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506{
507 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
508 vlib_frame_queue_elt_t *elt;
509 u32 save_count;
510 u64 new_tail;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400511 u64 before = clib_cpu_time_now ();
512
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513 ASSERT (fq);
514
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000515 new_tail = clib_atomic_add_fetch (&fq->tail, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516
517 /* Wait until a ring slot is available */
518 while (new_tail >= fq->head + fq->nelts)
519 {
520 f64 b4 = vlib_time_now_ticks (vm, before);
521 vlib_worker_thread_barrier_check (vm, b4);
522 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
Damjan Marion586afd72017-04-05 19:18:20 +0200523 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 }
525
Dave Barach9b8ffd92016-07-08 08:13:45 -0400526 elt = fq->elts + (new_tail & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
528 /* this would be very bad... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529 while (elt->valid)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530 {
531 }
532
533 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
534 save_count = frame->n_vectors;
535
536 elt->frame = frame;
537 elt->node_runtime_index = node_runtime_index;
538 elt->msg_type = type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400539 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 elt->valid = 1;
541
542 return save_count;
543}
544#endif /* 0 */
545
546/* To be called by vlib worker threads upon startup */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400547void
548vlib_worker_thread_init (vlib_worker_thread_t * w)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400550 vlib_thread_main_t *tm = vlib_get_thread_main ();
551
Dave Barachfdf49442016-12-20 12:48:14 -0500552 /*
553 * Note: disabling signals in worker threads as follows
554 * prevents the api post-mortem dump scheme from working
555 * {
556 * sigset_t s;
557 * sigfillset (&s);
558 * pthread_sigmask (SIG_SETMASK, &s, 0);
559 * }
560 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561
562 clib_mem_set_heap (w->thread_mheap);
563
Dave Barach9b8ffd92016-07-08 08:13:45 -0400564 if (vec_len (tm->thread_prefix) && w->registration->short_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400566 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
567 w->registration->short_name, w->instance_id, '\0');
568 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 }
570
571 if (!w->registration->use_pthreads)
572 {
573
574 /* Initial barrier sync, for both worker and i/o threads */
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000575 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
577 while (*vlib_worker_threads->wait_at_barrier)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400578 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000580 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581 }
582}
583
Dave Barach9b8ffd92016-07-08 08:13:45 -0400584void *
585vlib_worker_thread_bootstrap_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586{
587 void *rv;
588 vlib_worker_thread_t *w = arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400589
590 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200591 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
Damjan Marionf55f9b82017-05-10 21:06:28 +0200593 __os_thread_index = w - vlib_worker_threads;
Damjan Marion586afd72017-04-05 19:18:20 +0200594
Dave Barach9b8ffd92016-07-08 08:13:45 -0400595 rv = (void *) clib_calljmp
596 ((uword (*)(uword)) w->thread_function,
597 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598 /* NOTREACHED, we hope */
599 return rv;
600}
601
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200602static void
603vlib_get_thread_core_socket (vlib_worker_thread_t * w, unsigned cpu_id)
604{
605 const char *sys_cpu_path = "/sys/devices/system/cpu/cpu";
606 u8 *p = 0;
607 int core_id = -1, socket_id = -1;
608
609 p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, cpu_id, 0);
610 clib_sysfs_read ((char *) p, "%d", &core_id);
611 vec_reset_length (p);
612 p =
613 format (p, "%s%u/topology/physical_package_id%c", sys_cpu_path, cpu_id,
614 0);
615 clib_sysfs_read ((char *) p, "%d", &socket_id);
616 vec_free (p);
617
618 w->core_id = core_id;
619 w->socket_id = socket_id;
620}
621
Damjan Marion878c6092017-01-04 13:19:27 +0100622static clib_error_t *
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200623vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624{
Damjan Marion878c6092017-01-04 13:19:27 +0100625 vlib_thread_main_t *tm = &vlib_thread_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400626 void *(*fp_arg) (void *) = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200628 w->cpu_id = cpu_id;
629 vlib_get_thread_core_socket (w, cpu_id);
Damjan Marion878c6092017-01-04 13:19:27 +0100630 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200631 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, cpu_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400633 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400634 pthread_t worker;
635 cpu_set_t cpuset;
636 CPU_ZERO (&cpuset);
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200637 CPU_SET (cpu_id, &cpuset);
Christophe Fontainefef15b42016-04-09 12:38:49 +0900638
Damjan Marion878c6092017-01-04 13:19:27 +0100639 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
640 return clib_error_return_unix (0, "pthread_create");
641
642 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
643 return clib_error_return_unix (0, "pthread_setaffinity_np");
644
645 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400646 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647}
648
Dave Barach9b8ffd92016-07-08 08:13:45 -0400649static clib_error_t *
650start_workers (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651{
652 int i, j;
653 vlib_worker_thread_t *w;
654 vlib_main_t *vm_clone;
655 void *oldheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400656 vlib_thread_main_t *tm = &vlib_thread_main;
657 vlib_thread_registration_t *tr;
658 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659 u32 n_vlib_mains = tm->n_vlib_mains;
660 u32 worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400661 u8 *main_heap = clib_mem_get_per_cpu_heap ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400662
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663 vec_reset_length (vlib_worker_threads);
664
665 /* Set up the main thread */
666 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
Dave Barach3e7deb12016-02-05 16:29:53 -0500667 w->elog_track.name = "main thread";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 elog_track_register (&vm->elog_main, &w->elog_track);
669
Dave Barach9b8ffd92016-07-08 08:13:45 -0400670 if (vec_len (tm->thread_prefix))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400672 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
673 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674 }
675
Dave Barach9b8ffd92016-07-08 08:13:45 -0400676 vm->elog_main.lock =
677 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barach848191d2016-04-28 16:24:15 -0400678 vm->elog_main.lock[0] = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400679
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680 if (n_vlib_mains > 1)
681 {
Dave Barach80f54e22017-03-08 19:08:56 -0500682 /* Replace hand-crafted length-1 vector with a real vector */
683 vlib_mains = 0;
684
685 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
686 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687 _vec_len (vlib_mains) = 0;
Dave Barach80f54e22017-03-08 19:08:56 -0500688 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689
Dave Barach9b8ffd92016-07-08 08:13:45 -0400690 vlib_worker_threads->wait_at_barrier =
691 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 vlib_worker_threads->workers_at_barrier =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400693 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100695 vlib_worker_threads->node_reforks_required =
696 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
697
Dave Barachf6c68d72018-11-01 08:12:52 -0400698 /* We'll need the rpc vector lock... */
699 clib_spinlock_init (&vm->pending_rpc_lock);
700
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701 /* Ask for an initial barrier sync */
702 *vlib_worker_threads->workers_at_barrier = 0;
703 *vlib_worker_threads->wait_at_barrier = 1;
704
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100705 /* Without update or refork */
706 *vlib_worker_threads->node_reforks_required = 0;
707 vm->need_vlib_worker_thread_node_runtime_update = 0;
708
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100709 /* init timing */
710 vm->barrier_epoch = 0;
711 vm->barrier_no_close_before = 0;
712
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 worker_thread_index = 1;
714
Dave Barach9b8ffd92016-07-08 08:13:45 -0400715 for (i = 0; i < vec_len (tm->registrations); i++)
716 {
717 vlib_node_main_t *nm, *nm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400718 int k;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719
Dave Barach9b8ffd92016-07-08 08:13:45 -0400720 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721
Dave Barach9b8ffd92016-07-08 08:13:45 -0400722 if (tr->count == 0)
723 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724
Dave Barach9b8ffd92016-07-08 08:13:45 -0400725 for (k = 0; k < tr->count; k++)
726 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100727 vlib_node_t *n;
728
Dave Barach9b8ffd92016-07-08 08:13:45 -0400729 vec_add2 (vlib_worker_threads, w, 1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400730 /* Currently unused, may not really work */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400731 if (tr->mheap_size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400732 {
733#if USE_DLMALLOC == 0
734 w->thread_mheap =
735 mheap_alloc (0 /* use VM */ , tr->mheap_size);
736#else
737 w->thread_mheap = create_mspace (tr->mheap_size,
738 0 /* unlocked */ );
739#endif
740 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400741 else
742 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200743
744 w->thread_stack =
745 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400746 w->thread_function = tr->function;
747 w->thread_function_arg = w;
748 w->instance_id = k;
749 w->registration = tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
Dave Barach9b8ffd92016-07-08 08:13:45 -0400751 w->elog_track.name =
752 (char *) format (0, "%s %d", tr->name, k + 1);
753 vec_add1 (w->elog_track.name, 0);
754 elog_track_register (&vm->elog_main, &w->elog_track);
Bud Grise68adab92016-02-12 10:36:11 -0500755
Dave Barach9b8ffd92016-07-08 08:13:45 -0400756 if (tr->no_data_structure_clone)
757 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758
Dave Barach9b8ffd92016-07-08 08:13:45 -0400759 /* Fork vlib_global_main et al. Look for bugs here */
760 oldheap = clib_mem_set_heap (w->thread_mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200762 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
763 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400764 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765
Damjan Marion586afd72017-04-05 19:18:20 +0200766 vm_clone->thread_index = worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400767 vm_clone->heap_base = w->thread_mheap;
Dave Barach6a5adc32018-07-04 10:56:23 -0400768 vm_clone->heap_aligned_base = (void *)
769 (((uword) w->thread_mheap) & ~(VLIB_FRAME_ALIGN - 1));
Damjan Marione9f929b2017-03-16 11:32:09 +0100770 vm_clone->init_functions_called =
771 hash_create (0, /* value bytes */ 0);
Dave Barach2877eee2017-12-15 12:22:57 -0500772 vm_clone->pending_rpc_requests = 0;
773 vec_validate (vm_clone->pending_rpc_requests, 0);
774 _vec_len (vm_clone->pending_rpc_requests) = 0;
Dave Barachb7b92992018-10-17 10:38:51 -0400775 clib_memset (&vm_clone->random_buffer, 0,
776 sizeof (vm_clone->random_buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777
Dave Barach9b8ffd92016-07-08 08:13:45 -0400778 nm = &vlib_mains[0]->node_main;
779 nm_clone = &vm_clone->node_main;
780 /* fork next frames array, preserving node runtime indices */
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200781 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
782 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400783 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
784 {
785 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
786 u32 save_node_runtime_index;
787 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
Dave Barach9b8ffd92016-07-08 08:13:45 -0400789 save_node_runtime_index = nf->node_runtime_index;
790 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
791 vlib_next_frame_init (nf);
792 nf->node_runtime_index = save_node_runtime_index;
793 nf->flags = save_flags;
794 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795
Dave Barach9b8ffd92016-07-08 08:13:45 -0400796 /* fork the frame dispatch queue */
797 nm_clone->pending_frames = 0;
Dave Barach53fe4a72019-01-26 09:50:26 -0500798 vec_validate (nm_clone->pending_frames, 10);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400799 _vec_len (nm_clone->pending_frames) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800
Dave Barach9b8ffd92016-07-08 08:13:45 -0400801 /* fork nodes */
802 nm_clone->nodes = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100803
804 /* Allocate all nodes in single block for speed */
805 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
806
Dave Barach9b8ffd92016-07-08 08:13:45 -0400807 for (j = 0; j < vec_len (nm->nodes); j++)
808 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400809 clib_memcpy (n, nm->nodes[j], sizeof (*n));
810 /* none of the copied nodes have enqueue rights given out */
811 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
Dave Barachb7b92992018-10-17 10:38:51 -0400812 clib_memset (&n->stats_total, 0, sizeof (n->stats_total));
813 clib_memset (&n->stats_last_clear, 0,
814 sizeof (n->stats_last_clear));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400815 vec_add1 (nm_clone->nodes, n);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100816 n++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400817 }
818 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200819 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
820 CLIB_CACHE_LINE_BYTES);
JingLiuZTE30af5da2017-07-24 10:53:31 +0800821 vec_foreach (rt,
822 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
Damjan Marione9f929b2017-03-16 11:32:09 +0100823 {
824 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200825 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100826 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100827 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100828 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100829 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
830 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100831 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832
Dave Barach9b8ffd92016-07-08 08:13:45 -0400833 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200834 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
835 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400836 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
Damjan Marione9f929b2017-03-16 11:32:09 +0100837 {
838 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200839 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100840 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100841 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100842 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100843 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
844 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100845 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846
Dave Barach53fe4a72019-01-26 09:50:26 -0500847 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
848 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
849 CLIB_CACHE_LINE_BYTES);
850 vec_foreach (rt,
851 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
852 {
853 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
854 rt->thread_index = vm_clone->thread_index;
855 /* copy initial runtime_data from node */
856 if (n->runtime_data && n->runtime_data_bytes > 0)
857 clib_memcpy (rt->runtime_data, n->runtime_data,
858 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
859 n->runtime_data_bytes));
860 }
861
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200862 nm_clone->processes = vec_dup_aligned (nm->processes,
863 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864
Dave Barach593eedf2019-03-10 09:44:51 -0400865 /* Create per-thread frame freelist */
866 nm_clone->frame_sizes = vec_new (vlib_frame_size_t, 1);
867#ifdef VLIB_SUPPORTS_ARBITRARY_SCALAR_SIZES
Florin Coras93992a92017-05-24 18:03:56 -0700868 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
Dave Barach593eedf2019-03-10 09:44:51 -0400869#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870
Dave Barach9b8ffd92016-07-08 08:13:45 -0400871 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100872
Dave Barach9b8ffd92016-07-08 08:13:45 -0400873 clib_mem_set_heap (oldheap);
Dave Barach80f54e22017-03-08 19:08:56 -0500874 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875
Ole Troan233e4682019-05-16 15:01:34 +0200876 /* Switch to the stats segment ... */
877 void *oldheap = vlib_stats_push_heap (0);
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200878 vm_clone->error_main.counters = vec_dup_aligned
879 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
Ole Troan233e4682019-05-16 15:01:34 +0200880 vlib_stats_pop_heap2 (vm_clone->error_main.counters,
881 worker_thread_index, oldheap, 1);
882
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200883 vm_clone->error_main.counters_last_clear = vec_dup_aligned
884 (vlib_mains[0]->error_main.counters_last_clear,
885 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886
Dave Barach9b8ffd92016-07-08 08:13:45 -0400887 worker_thread_index++;
888 }
889 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890 }
891 else
892 {
893 /* only have non-data-structure copy threads to create... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400894 for (i = 0; i < vec_len (tm->registrations); i++)
895 {
896 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897
Dave Barach9b8ffd92016-07-08 08:13:45 -0400898 for (j = 0; j < tr->count; j++)
899 {
900 vec_add2 (vlib_worker_threads, w, 1);
901 if (tr->mheap_size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400902 {
903#if USE_DLMALLOC == 0
904 w->thread_mheap =
905 mheap_alloc (0 /* use VM */ , tr->mheap_size);
906#else
907 w->thread_mheap =
908 create_mspace (tr->mheap_size, 0 /* locked */ );
909#endif
910 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400911 else
912 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200913 w->thread_stack =
914 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400915 w->thread_function = tr->function;
916 w->thread_function_arg = w;
917 w->instance_id = j;
918 w->elog_track.name =
919 (char *) format (0, "%s %d", tr->name, j + 1);
920 w->registration = tr;
921 vec_add1 (w->elog_track.name, 0);
922 elog_track_register (&vm->elog_main, &w->elog_track);
923 }
924 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 }
926
927 worker_thread_index = 1;
928
929 for (i = 0; i < vec_len (tm->registrations); i++)
930 {
Damjan Marion878c6092017-01-04 13:19:27 +0100931 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932 int j;
933
934 tr = tm->registrations[i];
935
936 if (tr->use_pthreads || tm->use_pthreads)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400937 {
938 for (j = 0; j < tr->count; j++)
939 {
940 w = vlib_worker_threads + worker_thread_index++;
Damjan Marion878c6092017-01-04 13:19:27 +0100941 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
942 w, 0);
943 if (err)
944 clib_error_report (err);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400945 }
946 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400948 {
949 uword c;
Damjan Marion878c6092017-01-04 13:19:27 +0100950 /* *INDENT-OFF* */
951 clib_bitmap_foreach (c, tr->coremask, ({
952 w = vlib_worker_threads + worker_thread_index++;
953 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
954 w, c);
955 if (err)
956 clib_error_report (err);
957 }));
958 /* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400959 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400961 vlib_worker_thread_barrier_sync (vm);
962 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963 return 0;
964}
965
966VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
967
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100968
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100969static inline void
970worker_thread_node_runtime_update_internal (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971{
972 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973 vlib_main_t *vm;
974 vlib_node_main_t *nm, *nm_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975 vlib_main_t *vm_clone;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100976 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977 never_inline void
978 vlib_node_runtime_sync_stats (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400979 vlib_node_runtime_t * r,
980 uword n_calls,
981 uword n_vectors, uword n_clocks);
982
Damjan Marion586afd72017-04-05 19:18:20 +0200983 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 vm = vlib_mains[0];
986 nm = &vm->node_main;
987
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
989
Dave Barach9b8ffd92016-07-08 08:13:45 -0400990 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991 * Scrape all runtime stats, so we don't lose node runtime(s) with
992 * pending counts, or throw away worker / io thread counts.
993 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400994 for (j = 0; j < vec_len (nm->nodes); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400996 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997 n = nm->nodes[j];
998 vlib_node_sync_stats (vm, n);
999 }
1000
1001 for (i = 1; i < vec_len (vlib_mains); i++)
1002 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001003 vlib_node_t *n;
1004
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005 vm_clone = vlib_mains[i];
1006 nm_clone = &vm_clone->node_main;
1007
Dave Barach9b8ffd92016-07-08 08:13:45 -04001008 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1009 {
1010 n = nm_clone->nodes[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011
Dave Barach9b8ffd92016-07-08 08:13:45 -04001012 rt = vlib_node_get_runtime (vm_clone, n->index);
1013 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1014 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015 }
1016
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001017 /* Per-worker clone rebuilds are now done on each thread */
1018}
1019
1020
1021void
1022vlib_worker_thread_node_refork (void)
1023{
1024 vlib_main_t *vm, *vm_clone;
1025 vlib_node_main_t *nm, *nm_clone;
1026 vlib_node_t **old_nodes_clone;
1027 vlib_node_runtime_t *rt, *old_rt;
1028
1029 vlib_node_t *new_n_clone;
1030
1031 int j;
1032
1033 vm = vlib_mains[0];
1034 nm = &vm->node_main;
1035 vm_clone = vlib_get_main ();
1036 nm_clone = &vm_clone->node_main;
1037
1038 /* Re-clone error heap */
1039 u64 *old_counters = vm_clone->error_main.counters;
1040 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1041
Dave Barach178cf492018-11-13 16:34:13 -05001042 clib_memcpy_fast (&vm_clone->error_main, &vm->error_main,
1043 sizeof (vm->error_main));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001044 j = vec_len (vm->error_main.counters) - 1;
Ole Troan233e4682019-05-16 15:01:34 +02001045
1046 /* Switch to the stats segment ... */
1047 void *oldheap = vlib_stats_push_heap (0);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001048 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001049 vm_clone->error_main.counters = old_counters;
Ole Troan233e4682019-05-16 15:01:34 +02001050 vlib_stats_pop_heap2 (vm_clone->error_main.counters, vm_clone->thread_index,
1051 oldheap, 0);
1052
1053 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001054 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1055
1056 nm_clone = &vm_clone->node_main;
1057 vec_free (nm_clone->next_frames);
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001058 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1059 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001060
1061 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001063 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1064 u32 save_node_runtime_index;
1065 u32 save_flags;
Damjan Marionbc20bdf2015-12-17 14:28:18 +01001066
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001067 save_node_runtime_index = nf->node_runtime_index;
1068 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1069 vlib_next_frame_init (nf);
1070 nf->node_runtime_index = save_node_runtime_index;
1071 nf->flags = save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001073
1074 old_nodes_clone = nm_clone->nodes;
1075 nm_clone->nodes = 0;
1076
1077 /* re-fork nodes */
1078
1079 /* Allocate all nodes in single block for speed */
1080 new_n_clone =
1081 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1082 for (j = 0; j < vec_len (nm->nodes); j++)
1083 {
1084 vlib_node_t *old_n_clone;
1085 vlib_node_t *new_n;
1086
1087 new_n = nm->nodes[j];
1088 old_n_clone = old_nodes_clone[j];
1089
Dave Barach178cf492018-11-13 16:34:13 -05001090 clib_memcpy_fast (new_n_clone, new_n, sizeof (*new_n));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001091 /* none of the copied nodes have enqueue rights given out */
1092 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1093
1094 if (j >= vec_len (old_nodes_clone))
1095 {
1096 /* new node, set to zero */
Dave Barachb7b92992018-10-17 10:38:51 -04001097 clib_memset (&new_n_clone->stats_total, 0,
1098 sizeof (new_n_clone->stats_total));
1099 clib_memset (&new_n_clone->stats_last_clear, 0,
1100 sizeof (new_n_clone->stats_last_clear));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001101 }
1102 else
1103 {
1104 /* Copy stats if the old data is valid */
Dave Barach178cf492018-11-13 16:34:13 -05001105 clib_memcpy_fast (&new_n_clone->stats_total,
1106 &old_n_clone->stats_total,
1107 sizeof (new_n_clone->stats_total));
1108 clib_memcpy_fast (&new_n_clone->stats_last_clear,
1109 &old_n_clone->stats_last_clear,
1110 sizeof (new_n_clone->stats_last_clear));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001111
1112 /* keep previous node state */
1113 new_n_clone->state = old_n_clone->state;
1114 }
1115 vec_add1 (nm_clone->nodes, new_n_clone);
1116 new_n_clone++;
1117 }
1118 /* Free the old node clones */
1119 clib_mem_free (old_nodes_clone[0]);
1120
1121 vec_free (old_nodes_clone);
1122
1123
1124 /* re-clone internal nodes */
1125 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1126 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001127 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1128 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001129
1130 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1131 {
1132 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1133 rt->thread_index = vm_clone->thread_index;
1134 /* copy runtime_data, will be overwritten later for existing rt */
1135 if (n->runtime_data && n->runtime_data_bytes > 0)
Dave Barach178cf492018-11-13 16:34:13 -05001136 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1137 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1138 n->runtime_data_bytes));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001139 }
1140
1141 for (j = 0; j < vec_len (old_rt); j++)
1142 {
1143 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1144 rt->state = old_rt[j].state;
Dave Barach178cf492018-11-13 16:34:13 -05001145 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1146 VLIB_NODE_RUNTIME_DATA_SIZE);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001147 }
1148
1149 vec_free (old_rt);
1150
1151 /* re-clone input nodes */
1152 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1153 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001154 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1155 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001156
1157 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1158 {
1159 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1160 rt->thread_index = vm_clone->thread_index;
1161 /* copy runtime_data, will be overwritten later for existing rt */
1162 if (n->runtime_data && n->runtime_data_bytes > 0)
Dave Barach178cf492018-11-13 16:34:13 -05001163 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1164 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1165 n->runtime_data_bytes));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001166 }
1167
1168 for (j = 0; j < vec_len (old_rt); j++)
1169 {
1170 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1171 rt->state = old_rt[j].state;
Dave Barach178cf492018-11-13 16:34:13 -05001172 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1173 VLIB_NODE_RUNTIME_DATA_SIZE);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001174 }
1175
1176 vec_free (old_rt);
1177
Dave Barach53fe4a72019-01-26 09:50:26 -05001178 /* re-clone pre-input nodes */
1179 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT];
1180 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
1181 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
1182 CLIB_CACHE_LINE_BYTES);
1183
1184 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1185 {
1186 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1187 rt->thread_index = vm_clone->thread_index;
1188 /* copy runtime_data, will be overwritten later for existing rt */
1189 if (n->runtime_data && n->runtime_data_bytes > 0)
1190 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1191 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1192 n->runtime_data_bytes));
1193 }
1194
1195 for (j = 0; j < vec_len (old_rt); j++)
1196 {
1197 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1198 rt->state = old_rt[j].state;
1199 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1200 VLIB_NODE_RUNTIME_DATA_SIZE);
1201 }
1202
1203 vec_free (old_rt);
1204
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001205 nm_clone->processes = vec_dup_aligned (nm->processes,
1206 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001207}
1208
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001209void
1210vlib_worker_thread_node_runtime_update (void)
1211{
1212 /*
1213 * Make a note that we need to do a node runtime update
1214 * prior to releasing the barrier.
1215 */
1216 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217}
1218
Pavel Kotucek1e765832016-09-23 08:54:14 +02001219u32
1220unformat_sched_policy (unformat_input_t * input, va_list * args)
1221{
1222 u32 *r = va_arg (*args, u32 *);
1223
1224 if (0);
1225#define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1226 foreach_sched_policy
1227#undef _
1228 else
1229 return 0;
1230 return 1;
1231}
1232
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233static clib_error_t *
1234cpu_config (vlib_main_t * vm, unformat_input_t * input)
1235{
1236 vlib_thread_registration_t *tr;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001237 uword *p;
1238 vlib_thread_main_t *tm = &vlib_thread_main;
1239 u8 *name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001240 uword *bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241 u32 count;
1242
1243 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
Pavel Kotucek1e765832016-09-23 08:54:14 +02001244
Dave Barach9b8ffd92016-07-08 08:13:45 -04001245 tm->n_thread_stacks = 1; /* account for main thread */
Pavel Kotucek1e765832016-09-23 08:54:14 +02001246 tm->sched_policy = ~0;
1247 tm->sched_priority = ~0;
Damjan Marion858151f2018-07-11 10:51:00 +02001248 tm->main_lcore = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249
1250 tr = tm->next;
1251
1252 while (tr)
1253 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001254 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255 tr = tr->next;
1256 }
1257
Dave Barach9b8ffd92016-07-08 08:13:45 -04001258 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259 {
Damjan Marionbf741472016-06-13 22:49:44 +02001260 if (unformat (input, "use-pthreads"))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001261 tm->use_pthreads = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001263 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001264 else if (unformat (input, "main-core %u", &tm->main_lcore))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001265 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001267 ;
Yi Hee4a9eb72018-07-17 14:18:41 +08001268 else if (unformat (input, "coremask-%s %U", &name,
1269 unformat_bitmap_mask, &bitmap) ||
1270 unformat (input, "corelist-%s %U", &name,
1271 unformat_bitmap_list, &bitmap))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001272 {
1273 p = hash_get_mem (tm->thread_registrations_by_name, name);
1274 if (p == 0)
1275 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276
Dave Barach9b8ffd92016-07-08 08:13:45 -04001277 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001278
Dave Barach9b8ffd92016-07-08 08:13:45 -04001279 if (tr->use_pthreads)
1280 return clib_error_return (0,
1281 "corelist cannot be set for '%s' threads",
1282 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283
Dave Barach9b8ffd92016-07-08 08:13:45 -04001284 tr->coremask = bitmap;
1285 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1286 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001287 else
1288 if (unformat
1289 (input, "scheduler-policy %U", unformat_sched_policy,
1290 &tm->sched_policy))
1291 ;
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001292 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
Pavel Kotucek1e765832016-09-23 08:54:14 +02001293 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001294 else if (unformat (input, "%s %u", &name, &count))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001295 {
1296 p = hash_get_mem (tm->thread_registrations_by_name, name);
1297 if (p == 0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001298 return clib_error_return (0, "no such thread type 3 '%s'", name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001299
1300 tr = (vlib_thread_registration_t *) p[0];
1301 if (tr->fixed_count)
1302 return clib_error_return
1303 (0, "number of %s threads not configurable", tr->name);
1304 tr->count = count;
1305 }
1306 else
1307 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001308 }
1309
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001310 if (tm->sched_priority != ~0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001311 {
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001312 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001313 {
1314 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1315 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1316 if (tm->sched_priority > prio_max)
1317 tm->sched_priority = prio_max;
1318 if (tm->sched_priority < prio_min)
1319 tm->sched_priority = prio_min;
1320 }
1321 else
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001322 {
1323 return clib_error_return
1324 (0,
1325 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1326 tm->sched_priority);
1327 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001328 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001329 tr = tm->next;
1330
1331 if (!tm->thread_prefix)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001332 tm->thread_prefix = format (0, "vpp");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001333
1334 while (tr)
1335 {
1336 tm->n_thread_stacks += tr->count;
1337 tm->n_pthreads += tr->count * tr->use_pthreads;
Damjan Marion878c6092017-01-04 13:19:27 +01001338 tm->n_threads += tr->count * (tr->use_pthreads == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001339 tr = tr->next;
1340 }
1341
1342 return 0;
1343}
1344
1345VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1346
Ed Warnickecb9cada2015-12-08 15:45:58 -07001347void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -04001348void
1349vnet_main_fixup (vlib_fork_fixup_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001350{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001351}
1352
1353void
1354vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1355{
1356 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001357
1358 if (vlib_mains == 0)
1359 return;
1360
Damjan Marion586afd72017-04-05 19:18:20 +02001361 ASSERT (vlib_get_thread_index () == 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001362 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001363
1364 switch (which)
1365 {
1366 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1367 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1368 break;
1369
1370 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001371 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001372 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001373 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374}
1375
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001376 /*
1377 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1378 * based on a test based heuristic that barrier should be open for at least
1379 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1380 * point it is probably too late to make a difference)
1381 */
1382
1383#ifndef BARRIER_MINIMUM_OPEN_LIMIT
1384#define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1385#endif
1386
1387#ifndef BARRIER_MINIMUM_OPEN_FACTOR
1388#define BARRIER_MINIMUM_OPEN_FACTOR 3
1389#endif
1390
Dave Barach9b8ffd92016-07-08 08:13:45 -04001391void
Dave Barachc602b382019-06-03 19:48:22 -04001392vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm)
1393{
1394 f64 deadline;
1395 f64 now = vlib_time_now (vm);
1396 u32 count = vec_len (vlib_mains) - 1;
1397
1398 /* No worker threads? */
1399 if (count == 0)
1400 return;
1401
1402 deadline = now + BARRIER_SYNC_TIMEOUT;
1403 *vlib_worker_threads->wait_at_barrier = 1;
1404 while (*vlib_worker_threads->workers_at_barrier != count)
1405 {
1406 if ((now = vlib_time_now (vm)) > deadline)
1407 {
1408 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1409 os_panic ();
1410 }
1411 CLIB_PAUSE ();
1412 }
1413 *vlib_worker_threads->wait_at_barrier = 0;
1414}
1415
1416void
Damjan Marion8343ee52019-02-26 17:15:48 +01001417vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, const char *func_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001418{
1419 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001420 f64 now;
1421 f64 t_entry;
1422 f64 t_open;
1423 f64 t_closed;
Dave Barach9ae190e2019-04-23 10:07:24 -04001424 f64 max_vector_rate;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001425 u32 count;
Dave Barach9ae190e2019-04-23 10:07:24 -04001426 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001427
Dave Barach80f54e22017-03-08 19:08:56 -05001428 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001429 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001430
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001431 ASSERT (vlib_get_thread_index () == 0);
1432
Damjan Marion8343ee52019-02-26 17:15:48 +01001433 vlib_worker_threads[0].barrier_caller = func_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001434 count = vec_len (vlib_mains) - 1;
1435
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001436 /* Record entry relative to last close */
1437 now = vlib_time_now (vm);
1438 t_entry = now - vm->barrier_epoch;
1439
Ed Warnickecb9cada2015-12-08 15:45:58 -07001440 /* Tolerate recursive calls */
1441 if (++vlib_worker_threads[0].recursion_level > 1)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001442 {
1443 barrier_trace_sync_rec (t_entry);
1444 return;
1445 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001446
Dave Barach9ae190e2019-04-23 10:07:24 -04001447 /*
1448 * Need data to decide if we're working hard enough to honor
1449 * the barrier hold-down timer.
1450 */
1451 max_vector_rate = 0.0;
1452 for (i = 1; i < vec_len (vlib_mains); i++)
1453 max_vector_rate =
1454 clib_max (max_vector_rate,
1455 vlib_last_vectors_per_main_loop_as_f64 (vlib_mains[i]));
1456
Bud Grise42f20062016-03-16 13:09:46 -04001457 vlib_worker_threads[0].barrier_sync_count++;
1458
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001459 /* Enforce minimum barrier open time to minimize packet loss */
1460 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001461
Dave Barach9ae190e2019-04-23 10:07:24 -04001462 /*
1463 * If any worker thread seems busy, which we define
1464 * as a vector rate above 10, we enforce the barrier hold-down timer
1465 */
1466 if (max_vector_rate > 10.0)
Dave Barach36feebb2018-09-07 11:12:27 -04001467 {
Dave Barach9ae190e2019-04-23 10:07:24 -04001468 while (1)
Dave Barach36feebb2018-09-07 11:12:27 -04001469 {
Dave Barach9ae190e2019-04-23 10:07:24 -04001470 now = vlib_time_now (vm);
1471 /* Barrier hold-down timer expired? */
1472 if (now >= vm->barrier_no_close_before)
1473 break;
1474 if ((vm->barrier_no_close_before - now)
1475 > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1476 {
1477 clib_warning
1478 ("clock change: would have waited for %.4f seconds",
1479 (vm->barrier_no_close_before - now));
1480 break;
1481 }
Dave Barach36feebb2018-09-07 11:12:27 -04001482 }
1483 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001484 /* Record time of closure */
1485 t_open = now - vm->barrier_epoch;
1486 vm->barrier_epoch = now;
1487
1488 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001489
1490 *vlib_worker_threads->wait_at_barrier = 1;
1491 while (*vlib_worker_threads->workers_at_barrier != count)
1492 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001493 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001494 {
1495 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1496 os_panic ();
1497 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001498 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001499
1500 t_closed = now - vm->barrier_epoch;
1501
1502 barrier_trace_sync (t_entry, t_open, t_closed);
1503
Ed Warnickecb9cada2015-12-08 15:45:58 -07001504}
1505
Dave Barach9b8ffd92016-07-08 08:13:45 -04001506void
1507vlib_worker_thread_barrier_release (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001508{
1509 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001510 f64 now;
1511 f64 minimum_open;
1512 f64 t_entry;
1513 f64 t_closed_total;
1514 f64 t_update_main = 0.0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001515 int refork_needed = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001516
Dave Barach80f54e22017-03-08 19:08:56 -05001517 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001518 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001520 ASSERT (vlib_get_thread_index () == 0);
1521
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001522
1523 now = vlib_time_now (vm);
1524 t_entry = now - vm->barrier_epoch;
1525
Ed Warnickecb9cada2015-12-08 15:45:58 -07001526 if (--vlib_worker_threads[0].recursion_level > 0)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001527 {
1528 barrier_trace_release_rec (t_entry);
1529 return;
1530 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001531
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001532 /* Update (all) node runtimes before releasing the barrier, if needed */
1533 if (vm->need_vlib_worker_thread_node_runtime_update)
1534 {
Dave Barach1ddbc012018-06-13 09:26:05 -04001535 /*
1536 * Lock stat segment here, so we's safe when
1537 * rebuilding the stat segment node clones from the
1538 * stat thread...
1539 */
1540 vlib_stat_segment_lock ();
1541
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001542 /* Do stats elements on main thread */
1543 worker_thread_node_runtime_update_internal ();
1544 vm->need_vlib_worker_thread_node_runtime_update = 0;
1545
1546 /* Do per thread rebuilds in parallel */
1547 refork_needed = 1;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +00001548 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
1549 (vec_len (vlib_mains) - 1));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001550 now = vlib_time_now (vm);
1551 t_update_main = now - vm->barrier_epoch;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001552 }
1553
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001554 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001555
Dave Baracha4324a92019-02-19 17:05:30 -05001556 /*
1557 * Note when we let go of the barrier.
1558 * Workers can use this to derive a reasonably accurate
1559 * time offset. See vlib_time_now(...)
1560 */
1561 vm->time_last_barrier_release = vlib_time_now (vm);
1562 CLIB_MEMORY_STORE_BARRIER ();
1563
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564 *vlib_worker_threads->wait_at_barrier = 0;
1565
1566 while (*vlib_worker_threads->workers_at_barrier > 0)
1567 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001568 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001569 {
1570 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1571 os_panic ();
1572 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001573 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001574
1575 /* Wait for reforks before continuing */
1576 if (refork_needed)
1577 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001578 now = vlib_time_now (vm);
1579
1580 deadline = now + BARRIER_SYNC_TIMEOUT;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001581
1582 while (*vlib_worker_threads->node_reforks_required > 0)
1583 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001584 if ((now = vlib_time_now (vm)) > deadline)
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001585 {
1586 fformat (stderr, "%s: worker thread refork deadlock\n",
1587 __FUNCTION__);
1588 os_panic ();
1589 }
1590 }
Dave Barach1ddbc012018-06-13 09:26:05 -04001591 vlib_stat_segment_unlock ();
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001592 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001593
1594 t_closed_total = now - vm->barrier_epoch;
1595
1596 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1597
1598 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1599 {
1600 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1601 }
1602
1603 vm->barrier_no_close_before = now + minimum_open;
1604
1605 /* Record barrier epoch (used to enforce minimum open time) */
1606 vm->barrier_epoch = now;
1607
1608 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1609
Ed Warnickecb9cada2015-12-08 15:45:58 -07001610}
1611
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001612/*
1613 * Check the frame queue to see if any frames are available.
Dave Barach9b8ffd92016-07-08 08:13:45 -04001614 * If so, pull the packets off the frames and put them to
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001615 * the handoff node.
1616 */
Damjan Marione9d52d52017-03-09 15:42:26 +01001617int
1618vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001619{
Damjan Marion586afd72017-04-05 19:18:20 +02001620 u32 thread_id = vm->thread_index;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001621 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001622 vlib_frame_queue_elt_t *elt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001623 u32 *from, *to;
1624 vlib_frame_t *f;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001625 int msg_type;
1626 int processed = 0;
1627 u32 n_left_to_node;
1628 u32 vectors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001629
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001630 ASSERT (fq);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001631 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001632
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001633 if (PREDICT_FALSE (fqm->node_index == ~0))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001634 return 0;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001635 /*
1636 * Gather trace data for frame queues
1637 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001638 if (PREDICT_FALSE (fq->trace))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001639 {
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001640 frame_queue_trace_t *fqt;
1641 frame_queue_nelt_counter_t *fqh;
1642 u32 elix;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001643
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001644 fqt = &fqm->frame_queue_traces[thread_id];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001645
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001646 fqt->nelts = fq->nelts;
1647 fqt->head = fq->head;
1648 fqt->head_hint = fq->head_hint;
1649 fqt->tail = fq->tail;
1650 fqt->threshold = fq->vector_threshold;
1651 fqt->n_in_use = fqt->tail - fqt->head;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001652 if (fqt->n_in_use >= fqt->nelts)
1653 {
1654 // if beyond max then use max
1655 fqt->n_in_use = fqt->nelts - 1;
1656 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001657
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001658 /* Record the number of elements in use in the histogram */
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001659 fqh = &fqm->frame_queue_histogram[thread_id];
Dave Barach9b8ffd92016-07-08 08:13:45 -04001660 fqh->count[fqt->n_in_use]++;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001661
1662 /* Record a snapshot of the elements in use */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001663 for (elix = 0; elix < fqt->nelts; elix++)
1664 {
1665 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1666 if (1 || elt->valid)
1667 {
1668 fqt->n_vectors[elix] = elt->n_vectors;
1669 }
1670 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001671 fqt->written = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001672 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001673
1674 while (1)
1675 {
Dave Baracha638c182019-06-21 18:24:07 -04001676 vlib_buffer_t *b;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001677 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001678 {
1679 fq->head_hint = fq->head;
1680 return processed;
1681 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001682
Dave Barach9b8ffd92016-07-08 08:13:45 -04001683 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001684
1685 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001686 {
1687 fq->head_hint = fq->head;
1688 return processed;
1689 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001690
1691 from = elt->buffer_index;
1692 msg_type = elt->msg_type;
1693
1694 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1695 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1696
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001697 f = vlib_get_frame_to_node (vm, fqm->node_index);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001698
Dave Baracha638c182019-06-21 18:24:07 -04001699 /* If the first vector is traced, set the frame trace flag */
1700 b = vlib_get_buffer (vm, from[0]);
1701 if (b->flags & VLIB_BUFFER_IS_TRACED)
1702 f->frame_flags |= VLIB_NODE_FLAG_TRACE;
1703
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001704 to = vlib_frame_vector_args (f);
1705
1706 n_left_to_node = elt->n_vectors;
1707
1708 while (n_left_to_node >= 4)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001709 {
1710 to[0] = from[0];
1711 to[1] = from[1];
1712 to[2] = from[2];
1713 to[3] = from[3];
1714 to += 4;
1715 from += 4;
1716 n_left_to_node -= 4;
1717 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001718
1719 while (n_left_to_node > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001720 {
1721 to[0] = from[0];
1722 to++;
1723 from++;
1724 n_left_to_node--;
1725 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001726
1727 vectors += elt->n_vectors;
1728 f->n_vectors = elt->n_vectors;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001729 vlib_put_frame_to_node (vm, fqm->node_index, f);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001730
1731 elt->valid = 0;
1732 elt->n_vectors = 0;
1733 elt->msg_type = 0xfefefefe;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001734 CLIB_MEMORY_BARRIER ();
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001735 fq->head++;
1736 processed++;
1737
1738 /*
1739 * Limit the number of packets pushed into the graph
1740 */
1741 if (vectors >= fq->vector_threshold)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001742 {
1743 fq->head_hint = fq->head;
1744 return processed;
1745 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001746 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001747 ASSERT (0);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001748 return processed;
1749}
1750
Dave Barach9b8ffd92016-07-08 08:13:45 -04001751void
1752vlib_worker_thread_fn (void *arg)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001753{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001754 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
Damjan Marion878c6092017-01-04 13:19:27 +01001755 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -04001756 vlib_main_t *vm = vlib_get_main ();
Damjan Marione9f929b2017-03-16 11:32:09 +01001757 clib_error_t *e;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001758
Damjan Marion586afd72017-04-05 19:18:20 +02001759 ASSERT (vm->thread_index == vlib_get_thread_index ());
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001760
1761 vlib_worker_thread_init (w);
1762 clib_time_init (&vm->clib_time);
1763 clib_mem_set_heap (w->thread_mheap);
1764
Dave Barachc602b382019-06-03 19:48:22 -04001765 e = vlib_call_init_exit_functions_no_sort
Dave Barachf8d50682019-05-14 18:01:44 -04001766 (vm, &vm->worker_init_function_registrations, 1 /* call_once */ );
Damjan Marione9f929b2017-03-16 11:32:09 +01001767 if (e)
1768 clib_error_report (e);
1769
Dave Barachc602b382019-06-03 19:48:22 -04001770 /* Wait until the dpdk init sequence is complete */
1771 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1772 vlib_worker_thread_barrier_check ();
1773
Damjan Marione9d52d52017-03-09 15:42:26 +01001774 vlib_worker_loop (vm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001775}
1776
Dave Barach9b8ffd92016-07-08 08:13:45 -04001777/* *INDENT-OFF* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001778VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1779 .name = "workers",
1780 .short_name = "wk",
1781 .function = vlib_worker_thread_fn,
1782};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001783/* *INDENT-ON* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001784
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001785u32
1786vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1787{
1788 vlib_thread_main_t *tm = vlib_get_thread_main ();
1789 vlib_frame_queue_main_t *fqm;
1790 vlib_frame_queue_t *fq;
1791 int i;
1792
1793 if (frame_queue_nelts == 0)
dongjuan88752482019-06-04 10:59:02 +08001794 frame_queue_nelts = FRAME_QUEUE_MAX_NELTS;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001795
Damjan Marion78fd7e82018-07-20 18:47:05 +02001796 ASSERT (frame_queue_nelts >= 8);
1797
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001798 vec_add2 (tm->frame_queue_mains, fqm, 1);
1799
1800 fqm->node_index = node_index;
Damjan Marion78fd7e82018-07-20 18:47:05 +02001801 fqm->frame_queue_nelts = frame_queue_nelts;
1802 fqm->queue_hi_thresh = frame_queue_nelts - 2;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001803
1804 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
Damjan Marion78fd7e82018-07-20 18:47:05 +02001805 vec_validate (fqm->per_thread_data, tm->n_vlib_mains - 1);
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001806 _vec_len (fqm->vlib_frame_queues) = 0;
1807 for (i = 0; i < tm->n_vlib_mains; i++)
1808 {
Damjan Marion78fd7e82018-07-20 18:47:05 +02001809 vlib_frame_queue_per_thread_data_t *ptd;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001810 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1811 vec_add1 (fqm->vlib_frame_queues, fq);
Damjan Marion78fd7e82018-07-20 18:47:05 +02001812
1813 ptd = vec_elt_at_index (fqm->per_thread_data, i);
1814 vec_validate (ptd->handoff_queue_elt_by_thread_index,
1815 tm->n_vlib_mains - 1);
1816 vec_validate_init_empty (ptd->congested_handoff_queue_by_thread_index,
1817 tm->n_vlib_mains - 1,
1818 (vlib_frame_queue_t *) (~0));
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001819 }
1820
1821 return (fqm - tm->frame_queue_mains);
1822}
1823
Damjan Marion878c6092017-01-04 13:19:27 +01001824int
1825vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1826{
1827 vlib_thread_main_t *tm = vlib_get_thread_main ();
1828
1829 if (tm->extern_thread_mgmt)
1830 return -1;
1831
1832 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1833 tm->extern_thread_mgmt = 1;
1834 return 0;
1835}
1836
Dave Barach69128d02017-09-26 10:54:34 -04001837void
1838vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1839 args)
1840{
1841 ASSERT (vlib_get_thread_index () == 0);
1842 vlib_process_signal_event (vlib_get_main (), args->node_index,
1843 args->type_opaque, args->data);
1844}
1845
1846void *rpc_call_main_thread_cb_fn;
1847
1848void
1849vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1850{
1851 if (rpc_call_main_thread_cb_fn)
1852 {
1853 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1854 (*fp) (callback, args, arg_size);
1855 }
1856 else
1857 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1858}
1859
Dave Barach9b8ffd92016-07-08 08:13:45 -04001860clib_error_t *
1861threads_init (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001862{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001863 return 0;
1864}
1865
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001866VLIB_INIT_FUNCTION (threads_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001867
Dave Baracha4324a92019-02-19 17:05:30 -05001868
1869static clib_error_t *
1870show_clock_command_fn (vlib_main_t * vm,
1871 unformat_input_t * input, vlib_cli_command_t * cmd)
1872{
1873 int i;
1874 f64 now;
1875
1876 now = vlib_time_now (vm);
1877
1878 vlib_cli_output (vm, "Time now %.9f", now);
1879
1880 if (vec_len (vlib_mains) == 1)
1881 return 0;
1882
1883 vlib_cli_output (vm, "Time last barrier release %.9f",
1884 vm->time_last_barrier_release);
1885
1886 for (i = 1; i < vec_len (vlib_mains); i++)
1887 {
1888 if (vlib_mains[i] == 0)
1889 continue;
1890 vlib_cli_output (vm, "Thread %d offset %.9f error %.9f", i,
1891 vlib_mains[i]->time_offset,
1892 vm->time_last_barrier_release -
1893 vlib_mains[i]->time_last_barrier_release);
1894 }
1895 return 0;
1896}
1897
1898/* *INDENT-OFF* */
1899VLIB_CLI_COMMAND (f_command, static) =
1900{
1901 .path = "show clock",
1902 .short_help = "show clock",
1903 .function = show_clock_command_fn,
1904};
1905/* *INDENT-ON* */
1906
Dave Barach9b8ffd92016-07-08 08:13:45 -04001907/*
1908 * fd.io coding-style-patch-verification: ON
1909 *
1910 * Local Variables:
1911 * eval: (c-set-style "gnu")
1912 * End:
1913 */