blob: 3994afc2cea68f0cb73788f85a68a748e55f9cfe [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>
Dave Barach19718002020-03-11 10:31:36 -040020#include <vppinfra/time_range.h>
Damjan Marion94100532020-11-06 23:25:57 +010021#include <vppinfra/interrupt.h>
Damjan Marion4b661402024-02-29 16:14:27 +010022#include <vppinfra/bitmap.h>
23#include <vppinfra/unix.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024#include <vlib/vlib.h>
25
26#include <vlib/threads.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
Damjan Marion8973b072022-03-01 15:51:18 +010028#include <vlib/stats/stats.h>
Ole Troan233e4682019-05-16 15:01:34 +020029
Dave Barach9b8ffd92016-07-08 08:13:45 -040030u32
31vl (void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -070032{
33 return vec_len (p);
34}
35
Damjan Marion6a7acc22016-12-19 16:28:36 +010036vlib_worker_thread_t *vlib_worker_threads;
Ed Warnickecb9cada2015-12-08 15:45:58 -070037vlib_thread_main_t vlib_thread_main;
38
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010039/*
40 * Barrier tracing can be enabled on a normal build to collect information
41 * on barrier use, including timings and call stacks. Deliberately not
42 * keyed off CLIB_DEBUG, because that can add significant overhead which
43 * imapacts observed timings.
44 */
45
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010046static inline void
47barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
48{
Dave Barach88c6e002018-09-30 15:54:06 -040049 if (!vlib_worker_threads->barrier_elog_enabled)
50 return;
51
Damjan Marion14361002021-03-11 12:17:33 +010052 ELOG_TYPE_DECLARE (e) = {
53 .format = "bar-trace-%s-#%d",
54 .format_args = "T4i4",
55 };
56
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010057 struct
58 {
Dave Barach88c6e002018-09-30 15:54:06 -040059 u32 caller, count, t_entry, t_open, t_closed;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010060 } *ed = 0;
61
62 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
63 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
Dave Barachb09f4d02019-07-15 16:00:03 -040064 ed->caller = elog_string (&vlib_global_main.elog_main,
65 (char *) vlib_worker_threads[0].barrier_caller);
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010066 ed->t_entry = (int) (1000000.0 * t_entry);
67 ed->t_open = (int) (1000000.0 * t_open);
68 ed->t_closed = (int) (1000000.0 * t_closed);
69}
70
71static inline void
72barrier_trace_sync_rec (f64 t_entry)
73{
Dave Barach88c6e002018-09-30 15:54:06 -040074 if (!vlib_worker_threads->barrier_elog_enabled)
75 return;
76
Damjan Marion14361002021-03-11 12:17:33 +010077 ELOG_TYPE_DECLARE (e) = {
78 .format = "bar-syncrec-%s-#%d",
79 .format_args = "T4i4",
80 };
81
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010082 struct
83 {
Dave Barach88c6e002018-09-30 15:54:06 -040084 u32 caller, depth;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010085 } *ed = 0;
86
87 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
88 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
Dave Barachb09f4d02019-07-15 16:00:03 -040089 ed->caller = elog_string (&vlib_global_main.elog_main,
90 (char *) vlib_worker_threads[0].barrier_caller);
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010091}
92
93static inline void
94barrier_trace_release_rec (f64 t_entry)
95{
Dave Barach88c6e002018-09-30 15:54:06 -040096 if (!vlib_worker_threads->barrier_elog_enabled)
97 return;
98
Damjan Marion14361002021-03-11 12:17:33 +010099 ELOG_TYPE_DECLARE (e) = {
100 .format = "bar-relrrec-#%d",
101 .format_args = "i4",
102 };
103
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100104 struct
105 {
Dave Barach88c6e002018-09-30 15:54:06 -0400106 u32 depth;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100107 } *ed = 0;
108
109 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100110 ed->depth = (int) vlib_worker_threads[0].recursion_level;
111}
112
113static inline void
114barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
115{
Dave Barach88c6e002018-09-30 15:54:06 -0400116 if (!vlib_worker_threads->barrier_elog_enabled)
117 return;
118
Damjan Marion14361002021-03-11 12:17:33 +0100119 ELOG_TYPE_DECLARE (e) = {
120 .format = "bar-rel-#%d-e%d-u%d-t%d",
121 .format_args = "i4i4i4i4",
122 };
123
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100124 struct
125 {
Dave Barach88c6e002018-09-30 15:54:06 -0400126 u32 count, t_entry, t_update_main, t_closed_total;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100127 } *ed = 0;
128
129 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
130 ed->t_entry = (int) (1000000.0 * t_entry);
131 ed->t_update_main = (int) (1000000.0 * t_update_main);
132 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
133 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
134
135 /* Reset context for next trace */
136 vlib_worker_threads[0].barrier_context = NULL;
137}
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100138
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139uword
Damjan Marionf55f9b82017-05-10 21:06:28 +0200140os_get_nthreads (void)
Dave Barach01d86c72016-08-08 15:13:42 -0400141{
Dave Barach2180bac2019-05-10 15:25:10 -0400142 return vec_len (vlib_thread_stacks);
Dave Barach01d86c72016-08-08 15:13:42 -0400143}
144
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145void
146vlib_set_thread_name (char *name)
147{
148 int pthread_setname_np (pthread_t __target_thread, const char *__name);
Dave Barachb2a6e252016-07-27 10:00:58 -0400149 int rv;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400150 pthread_t thread = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152 if (thread)
Dave Barachb2a6e252016-07-27 10:00:58 -0400153 {
154 rv = pthread_setname_np (thread, name);
155 if (rv)
Ed Warnicke853e7202016-08-12 11:42:26 -0700156 clib_warning ("pthread_setname_np returned %d", rv);
Dave Barachb2a6e252016-07-27 10:00:58 -0400157 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158}
159
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160static int
161sort_registrations_by_no_clone (void *a0, void *a1)
162{
163 vlib_thread_registration_t **tr0 = a0;
164 vlib_thread_registration_t **tr1 = a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Dave Barach9b8ffd92016-07-08 08:13:45 -0400166 return ((i32) ((*tr0)->no_data_structure_clone)
167 - ((i32) ((*tr1)->no_data_structure_clone)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168}
169
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
171/* Called early in the init sequence */
172
173clib_error_t *
174vlib_thread_init (vlib_main_t * vm)
175{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 vlib_thread_main_t *tm = &vlib_thread_main;
177 vlib_worker_thread_t *w;
178 vlib_thread_registration_t *tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 u32 n_vlib_mains = 1;
180 u32 first_index = 1;
181 u32 i;
hsandid71c32a82024-03-25 17:51:31 +0100182 pid_t pid;
183 uword *avail_cpu, *affinity_cpu;
184 uword n_cpus;
Damjan Marion8973b072022-03-01 15:51:18 +0100185 u32 stats_num_worker_threads_dir_index;
186
187 stats_num_worker_threads_dir_index =
188 vlib_stats_add_gauge ("/sys/num_worker_threads");
189 ASSERT (stats_num_worker_threads_dir_index != ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
191 /* get bitmaps of active cpu cores and sockets */
Damjan Marion4b661402024-02-29 16:14:27 +0100192 tm->cpu_core_bitmap = os_get_online_cpu_core_bitmap ();
193 tm->cpu_socket_bitmap = os_get_online_cpu_node_bitmap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
hsandid71c32a82024-03-25 17:51:31 +0100195 /* get bitmap of active cpu cores vpp has affinity to */
196 pid = getpid ();
197 tm->cpu_affinity_bitmap = os_get_cpu_affinity_bitmap (pid);
198
199 /* if fetching affinity fails, return online cpu core bmp */
200 if (tm->cpu_affinity_bitmap == 0)
201 tm->cpu_affinity_bitmap = os_get_online_cpu_core_bitmap ();
202
Dave Barach9b8ffd92016-07-08 08:13:45 -0400203 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
hsandid71c32a82024-03-25 17:51:31 +0100204 affinity_cpu = clib_bitmap_dup (tm->cpu_affinity_bitmap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
206 /* skip cores */
hsandid71c32a82024-03-25 17:51:31 +0100207 n_cpus = clib_bitmap_count_set_bits (avail_cpu);
208 if (tm->skip_cores >= n_cpus)
209 return clib_error_return (0, "skip-core greater than available cpus");
210 n_cpus = clib_bitmap_count_set_bits (affinity_cpu);
211 if (tm->skip_cores >= n_cpus)
212 return clib_error_return (0, "skip-core greater than affinity cpus");
213
Dave Barach9b8ffd92016-07-08 08:13:45 -0400214 for (i = 0; i < tm->skip_cores; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 {
hsandid71c32a82024-03-25 17:51:31 +0100216 uword c;
217 c = clib_bitmap_first_set (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218 if (c == ~0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400219 return clib_error_return (0, "no available cpus to skip");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Dave Barach9b8ffd92016-07-08 08:13:45 -0400221 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
hsandid71c32a82024-03-25 17:51:31 +0100222
223 c = clib_bitmap_first_set (affinity_cpu);
224 if (c == ~0)
225 return clib_error_return (0, "no available env cpus to skip");
226
227 affinity_cpu = clib_bitmap_set (affinity_cpu, c, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 }
229
230 /* grab cpu for main thread */
Benoît Ganne8b153de2021-12-09 18:24:21 +0100231 if (tm->main_lcore != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400233 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
234 return clib_error_return (0, "cpu %u is not available to be used"
235 " for the main thread", tm->main_lcore);
Benoît Ganne8b153de2021-12-09 18:24:21 +0100236 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
hsandid71c32a82024-03-25 17:51:31 +0100237 affinity_cpu = clib_bitmap_set (affinity_cpu, tm->main_lcore, 0);
238 }
239 /* if auto enabled, grab first cpu vpp has affinity to for main thread */
240 else if (tm->use_main_core_auto)
241 {
242 uword c = clib_bitmap_first_set (affinity_cpu);
243 if (c != ~0)
244 tm->main_lcore = c;
245
246 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
247 affinity_cpu = clib_bitmap_set (affinity_cpu, tm->main_lcore, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
250 /* assume that there is socket 0 only if there is no data from sysfs */
251 if (!tm->cpu_socket_bitmap)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400252 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
Christophe Fontainefef15b42016-04-09 12:38:49 +0900254 /* pin main thread to main_lcore */
Benoît Ganne8b153de2021-12-09 18:24:21 +0100255 if (tm->main_lcore != ~0)
256 {
257 cpu_set_t cpuset;
258 CPU_ZERO (&cpuset);
259 CPU_SET (tm->main_lcore, &cpuset);
hsandid832342e2023-12-20 15:41:54 +0100260 if (pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t),
261 &cpuset))
262 {
263 return clib_error_return (0, "could not pin main thread to cpu %u",
264 tm->main_lcore);
265 }
Benoît Ganne8b153de2021-12-09 18:24:21 +0100266 }
Christophe Fontainefef15b42016-04-09 12:38:49 +0900267
Dave Barach2180bac2019-05-10 15:25:10 -0400268 /* Set up thread 0 */
269 vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion8bea5892022-04-04 22:40:45 +0200270 vec_set_len (vlib_worker_threads, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 w = vlib_worker_threads;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400272 w->thread_mheap = clib_mem_get_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 w->thread_stack = vlib_thread_stacks[0];
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200274 w->cpu_id = tm->main_lcore;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400275 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200276 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277 tm->n_vlib_mains = 1;
278
Jon Loeligerf617b142020-01-30 18:37:47 -0600279 vlib_get_thread_core_numa (w, w->cpu_id);
280
Pavel Kotucek1e765832016-09-23 08:54:14 +0200281 if (tm->sched_policy != ~0)
282 {
283 struct sched_param sched_param;
284 if (!sched_getparam (w->lwp, &sched_param))
285 {
286 if (tm->sched_priority != ~0)
287 sched_param.sched_priority = tm->sched_priority;
288 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
289 }
290 }
291
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292 /* assign threads to cores and set n_vlib_mains */
293 tr = tm->next;
294
295 while (tr)
296 {
297 vec_add1 (tm->registrations, tr);
298 tr = tr->next;
299 }
300
Dave Barach9b8ffd92016-07-08 08:13:45 -0400301 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
303 for (i = 0; i < vec_len (tm->registrations); i++)
304 {
305 int j;
306 tr = tm->registrations[i];
307 tr->first_index = first_index;
308 first_index += tr->count;
309 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
310
311 /* construct coremask */
312 if (tr->use_pthreads || !tr->count)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400313 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
315 if (tr->coremask)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400316 {
317 uword c;
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100318 clib_bitmap_foreach (c, tr->coremask) {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 if (clib_bitmap_get(avail_cpu, c) == 0)
320 return clib_error_return (0, "cpu %u is not available to be used"
321 " for the '%s' thread",c, tr->name);
322
323 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100324 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400325 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400327 {
hsandid71c32a82024-03-25 17:51:31 +0100328 /* for automatic pinning, use cpu affinity list */
329 uword n_env_cpu = 0;
330 n_env_cpu = clib_bitmap_count_set_bits (affinity_cpu);
331
332 if (n_env_cpu < tr->count)
333 return clib_error_return (0,
334 "no available cpus to be used for"
335 " the '%s' thread #%u",
336 tr->name, n_env_cpu);
337
Dave Barach9b8ffd92016-07-08 08:13:45 -0400338 for (j = 0; j < tr->count; j++)
339 {
Vladimir Isaev2ed42042020-03-17 12:56:31 +0300340 /* Do not use CPU 0 by default - leave it to the host and IRQs */
hsandid71c32a82024-03-25 17:51:31 +0100341 uword avail_c0 = clib_bitmap_get (affinity_cpu, 0);
342 affinity_cpu = clib_bitmap_set (affinity_cpu, 0, 0);
Vladimir Isaev2ed42042020-03-17 12:56:31 +0300343
hsandid71c32a82024-03-25 17:51:31 +0100344 uword c = clib_bitmap_first_set (affinity_cpu);
Vladimir Isaev2ed42042020-03-17 12:56:31 +0300345 /* Use CPU 0 as a last resort */
346 if (c == ~0 && avail_c0)
347 {
348 c = 0;
349 avail_c0 = 0;
350 }
351
Dave Barach9b8ffd92016-07-08 08:13:45 -0400352 if (c == ~0)
353 return clib_error_return (0,
354 "no available cpus to be used for"
hsandid832342e2023-12-20 15:41:54 +0100355 " the '%s' thread #%u",
356 tr->name, tr->count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357
hsandid71c32a82024-03-25 17:51:31 +0100358 affinity_cpu = clib_bitmap_set (affinity_cpu, 0, avail_c0);
359 affinity_cpu = clib_bitmap_set (affinity_cpu, c, 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
361 }
362 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363 }
364
Dave Barach9b8ffd92016-07-08 08:13:45 -0400365 clib_bitmap_free (avail_cpu);
hsandid71c32a82024-03-25 17:51:31 +0100366 clib_bitmap_free (affinity_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367
368 tm->n_vlib_mains = n_vlib_mains;
Damjan Marion8973b072022-03-01 15:51:18 +0100369 vlib_stats_set_gauge (stats_num_worker_threads_dir_index, n_vlib_mains - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
Dave Barach2180bac2019-05-10 15:25:10 -0400371 /*
372 * Allocate the remaining worker threads, and thread stack vector slots
373 * from now on, calls to os_get_nthreads() will return the correct
374 * answer.
375 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400376 vec_validate_aligned (vlib_worker_threads, first_index - 1,
377 CLIB_CACHE_LINE_BYTES);
Dave Barach2180bac2019-05-10 15:25:10 -0400378 vec_validate (vlib_thread_stacks, vec_len (vlib_worker_threads) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 return 0;
380}
381
Dave Barach9b8ffd92016-07-08 08:13:45 -0400382vlib_frame_queue_t *
383vlib_frame_queue_alloc (int nelts)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400385 vlib_frame_queue_t *fq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
Dave Barach9b8ffd92016-07-08 08:13:45 -0400387 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400388 clib_memset (fq, 0, sizeof (*fq));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 fq->nelts = nelts;
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200390 fq->vector_threshold = 2 * VLIB_FRAME_SIZE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400391 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200393 if (nelts & (nelts - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400394 {
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200395 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
396 abort ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400397 }
398
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 return (fq);
400}
401
402void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400403void
404vl_msg_api_handler_no_free (void *v)
405{
406}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408/* To be called by vlib worker threads upon startup */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400409void
410vlib_worker_thread_init (vlib_worker_thread_t * w)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400412 vlib_thread_main_t *tm = vlib_get_thread_main ();
413
Dave Barachfdf49442016-12-20 12:48:14 -0500414 /*
415 * Note: disabling signals in worker threads as follows
416 * prevents the api post-mortem dump scheme from working
417 * {
418 * sigset_t s;
419 * sigfillset (&s);
420 * pthread_sigmask (SIG_SETMASK, &s, 0);
421 * }
422 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
424 clib_mem_set_heap (w->thread_mheap);
425
Dave Barach9b8ffd92016-07-08 08:13:45 -0400426 if (vec_len (tm->thread_prefix) && w->registration->short_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400428 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
429 w->registration->short_name, w->instance_id, '\0');
430 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 }
432
433 if (!w->registration->use_pthreads)
434 {
435
436 /* Initial barrier sync, for both worker and i/o threads */
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000437 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
439 while (*vlib_worker_threads->wait_at_barrier)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400440 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000442 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 }
444}
445
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446void *
447vlib_worker_thread_bootstrap_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 vlib_worker_thread_t *w = arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450
451 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200452 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453
Damjan Marionf55f9b82017-05-10 21:06:28 +0200454 __os_thread_index = w - vlib_worker_threads;
Damjan Marion586afd72017-04-05 19:18:20 +0200455
Damjan Marion7bf23172022-03-28 01:47:33 +0200456 if (CLIB_DEBUG > 0)
457 {
458 void *frame_addr = __builtin_frame_address (0);
459 if (frame_addr < (void *) w->thread_stack ||
460 frame_addr > (void *) w->thread_stack + VLIB_THREAD_STACK_SIZE)
461 {
462 /* heap is not set yet */
463 fprintf (stderr, "thread stack is not set properly\n");
464 exit (1);
465 }
466 }
Dave Barach37579c32021-07-27 09:27:07 -0400467
Damjan Marion7bf23172022-03-28 01:47:33 +0200468 w->thread_function (arg);
469
470 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471}
472
Dave Baracha690fdb2020-01-21 12:34:55 -0500473void
474vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id)
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200475{
Lijian.Zhang4fbb9da2020-02-14 15:16:49 +0800476 clib_bitmap_t *nbmp = 0, *cbmp = 0;
Damjan Marion3eb6cbe2024-02-12 19:44:58 +0000477 int node, core_id = -1, numa_id = -1;
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200478
Damjan Marion3eb6cbe2024-02-12 19:44:58 +0000479 core_id = os_get_cpu_phys_core_id (cpu_id);
Damjan Marion4b661402024-02-29 16:14:27 +0100480 nbmp = os_get_online_cpu_node_bitmap ();
Damjan Marion3eb6cbe2024-02-12 19:44:58 +0000481
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100482 clib_bitmap_foreach (node, nbmp) {
Damjan Marion3eb6cbe2024-02-12 19:44:58 +0000483 cbmp = os_get_cpu_on_node_bitmap (node);
484 if (clib_bitmap_get (cbmp, cpu_id))
485 numa_id = node;
486 vec_reset_length (cbmp);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100487 }
Damjan Marion4b661402024-02-29 16:14:27 +0100488
Lijian.Zhang4fbb9da2020-02-14 15:16:49 +0800489 vec_free (nbmp);
490 vec_free (cbmp);
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200491
492 w->core_id = core_id;
Dave Baracha690fdb2020-01-21 12:34:55 -0500493 w->numa_id = numa_id;
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200494}
495
Damjan Marion878c6092017-01-04 13:19:27 +0100496static clib_error_t *
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200497vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200499 clib_mem_main_t *mm = &clib_mem_main;
Damjan Marion878c6092017-01-04 13:19:27 +0100500 vlib_thread_main_t *tm = &vlib_thread_main;
Damjan Mariona2b7a022021-12-28 20:32:20 +0100501 pthread_t worker;
Damjan Marion7bf23172022-03-28 01:47:33 +0200502 pthread_attr_t attr;
Damjan Mariona2b7a022021-12-28 20:32:20 +0100503 cpu_set_t cpuset;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400504 void *(*fp_arg) (void *) = fp;
Dave Baracha690fdb2020-01-21 12:34:55 -0500505 void *numa_heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200507 w->cpu_id = cpu_id;
Dave Baracha690fdb2020-01-21 12:34:55 -0500508 vlib_get_thread_core_numa (w, cpu_id);
Dave Baracha690fdb2020-01-21 12:34:55 -0500509
510 /* Set up NUMA-bound heap if indicated */
Damjan Marion57d1ec02020-09-16 21:15:44 +0200511 if (mm->per_numa_mheaps[w->numa_id] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -0500512 {
513 /* If the user requested a NUMA heap, create it... */
514 if (tm->numa_heap_size)
515 {
Damjan Marion2bc1af52020-10-02 15:01:12 +0200516 clib_mem_set_numa_affinity (w->numa_id, 1 /* force */ );
517 numa_heap = clib_mem_create_heap (0 /* DIY */ , tm->numa_heap_size,
518 1 /* is_locked */ ,
519 "numa %u heap", w->numa_id);
520 clib_mem_set_default_numa_affinity ();
Damjan Marion57d1ec02020-09-16 21:15:44 +0200521 mm->per_numa_mheaps[w->numa_id] = numa_heap;
Dave Baracha690fdb2020-01-21 12:34:55 -0500522 }
523 else
524 {
525 /* Or, use the main heap */
Damjan Marion57d1ec02020-09-16 21:15:44 +0200526 mm->per_numa_mheaps[w->numa_id] = w->thread_mheap;
Dave Baracha690fdb2020-01-21 12:34:55 -0500527 }
528 }
529
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530 CPU_ZERO (&cpuset);
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200531 CPU_SET (cpu_id, &cpuset);
Christophe Fontainefef15b42016-04-09 12:38:49 +0900532
Damjan Marion7bf23172022-03-28 01:47:33 +0200533 if (pthread_attr_init (&attr))
534 return clib_error_return_unix (0, "pthread_attr_init");
535
536 if (pthread_attr_setstack (&attr, w->thread_stack,
537 VLIB_THREAD_STACK_SIZE))
538 return clib_error_return_unix (0, "pthread_attr_setstack");
539
540 if (pthread_create (&worker, &attr, fp_arg, (void *) w))
Damjan Marion878c6092017-01-04 13:19:27 +0100541 return clib_error_return_unix (0, "pthread_create");
542
543 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
544 return clib_error_return_unix (0, "pthread_setaffinity_np");
545
Damjan Marion7bf23172022-03-28 01:47:33 +0200546 if (pthread_attr_destroy (&attr))
547 return clib_error_return_unix (0, "pthread_attr_destroy");
548
Damjan Marion878c6092017-01-04 13:19:27 +0100549 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550}
551
Dave Barach9b8ffd92016-07-08 08:13:45 -0400552static clib_error_t *
553start_workers (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100555 vlib_global_main_t *vgm = vlib_get_global_main ();
Damjan Marion66c85832022-03-14 13:04:38 +0100556 vlib_main_t *fvm = vlib_get_first_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 int i, j;
558 vlib_worker_thread_t *w;
559 vlib_main_t *vm_clone;
560 void *oldheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400561 vlib_thread_main_t *tm = &vlib_thread_main;
562 vlib_thread_registration_t *tr;
563 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564 u32 n_vlib_mains = tm->n_vlib_mains;
565 u32 worker_thread_index;
Damjan Marion66c85832022-03-14 13:04:38 +0100566 u32 stats_err_entry_index = fvm->error_main.stats_err_entry_index;
Damjan Marionbfa75d62020-10-06 17:46:06 +0200567 clib_mem_heap_t *main_heap = clib_mem_get_per_cpu_heap ();
Ole Troana606d922021-05-05 09:23:17 +0200568 vlib_stats_register_mem_heap (main_heap);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400569
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 vec_reset_length (vlib_worker_threads);
571
572 /* Set up the main thread */
573 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
Dave Barach3e7deb12016-02-05 16:29:53 -0500574 w->elog_track.name = "main thread";
Damjan Marionf553a2c2021-03-26 13:45:37 +0100575 elog_track_register (vlib_get_elog_main (), &w->elog_track);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
Dave Barach9b8ffd92016-07-08 08:13:45 -0400577 if (vec_len (tm->thread_prefix))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400579 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
580 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581 }
582
Damjan Marionfd8deb42021-03-06 12:26:28 +0100583 vgm->elog_main.lock =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400584 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100585 vgm->elog_main.lock[0] = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400586
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000587 clib_callback_data_init (&vm->vlib_node_runtime_perf_callbacks,
588 &vm->worker_thread_main_loop_callback_lock);
589
Damjan Marionfd8deb42021-03-06 12:26:28 +0100590 vec_validate_aligned (vgm->vlib_mains, n_vlib_mains - 1,
591 CLIB_CACHE_LINE_BYTES);
Damjan Marion8bea5892022-04-04 22:40:45 +0200592 vec_set_len (vgm->vlib_mains, 0);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100593 vec_add1_aligned (vgm->vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
Dave Barach1c556d12020-10-02 11:35:44 -0400594
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595 if (n_vlib_mains > 1)
596 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400597 vlib_worker_threads->wait_at_barrier =
598 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 vlib_worker_threads->workers_at_barrier =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400600 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100602 vlib_worker_threads->node_reforks_required =
603 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
604
Dave Barachf6c68d72018-11-01 08:12:52 -0400605 /* We'll need the rpc vector lock... */
606 clib_spinlock_init (&vm->pending_rpc_lock);
607
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 /* Ask for an initial barrier sync */
609 *vlib_worker_threads->workers_at_barrier = 0;
610 *vlib_worker_threads->wait_at_barrier = 1;
611
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100612 /* Without update or refork */
613 *vlib_worker_threads->node_reforks_required = 0;
Damjan Marionfd8deb42021-03-06 12:26:28 +0100614 vgm->need_vlib_worker_thread_node_runtime_update = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100615
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100616 /* init timing */
617 vm->barrier_epoch = 0;
618 vm->barrier_no_close_before = 0;
619
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620 worker_thread_index = 1;
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000621 clib_spinlock_init (&vm->worker_thread_main_loop_callback_lock);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622
Dave Barach9b8ffd92016-07-08 08:13:45 -0400623 for (i = 0; i < vec_len (tm->registrations); i++)
624 {
625 vlib_node_main_t *nm, *nm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400626 int k;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627
Dave Barach9b8ffd92016-07-08 08:13:45 -0400628 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629
Dave Barach9b8ffd92016-07-08 08:13:45 -0400630 if (tr->count == 0)
631 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632
Dave Barach9b8ffd92016-07-08 08:13:45 -0400633 for (k = 0; k < tr->count; k++)
634 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100635 vlib_node_t *n;
Damjan Marion66c85832022-03-14 13:04:38 +0100636 u64 **c;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100637
Dave Barach9b8ffd92016-07-08 08:13:45 -0400638 vec_add2 (vlib_worker_threads, w, 1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400639 /* Currently unused, may not really work */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400640 if (tr->mheap_size)
Damjan Marion4537c302020-09-28 19:03:37 +0200641 w->thread_mheap = clib_mem_create_heap (0, tr->mheap_size,
642 /* unlocked */ 0,
643 "%s%d heap",
644 tr->name, k);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400645 else
646 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200647
648 w->thread_stack =
649 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400650 w->thread_function = tr->function;
651 w->thread_function_arg = w;
652 w->instance_id = k;
653 w->registration = tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655 w->elog_track.name =
656 (char *) format (0, "%s %d", tr->name, k + 1);
657 vec_add1 (w->elog_track.name, 0);
Damjan Marionf553a2c2021-03-26 13:45:37 +0100658 elog_track_register (vlib_get_elog_main (), &w->elog_track);
Bud Grise68adab92016-02-12 10:36:11 -0500659
Dave Barach9b8ffd92016-07-08 08:13:45 -0400660 if (tr->no_data_structure_clone)
661 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662
Dave Barach9b8ffd92016-07-08 08:13:45 -0400663 /* Fork vlib_global_main et al. Look for bugs here */
664 oldheap = clib_mem_set_heap (w->thread_mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200666 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
667 CLIB_CACHE_LINE_BYTES);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100668 clib_memcpy (vm_clone, vlib_get_first_main (),
669 sizeof (*vm_clone));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670
Damjan Marion586afd72017-04-05 19:18:20 +0200671 vm_clone->thread_index = worker_thread_index;
Dave Barach2877eee2017-12-15 12:22:57 -0500672 vm_clone->pending_rpc_requests = 0;
673 vec_validate (vm_clone->pending_rpc_requests, 0);
Damjan Marion8bea5892022-04-04 22:40:45 +0200674 vec_set_len (vm_clone->pending_rpc_requests, 0);
Dave Barachb7b92992018-10-17 10:38:51 -0400675 clib_memset (&vm_clone->random_buffer, 0,
676 sizeof (vm_clone->random_buffer));
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000677 clib_spinlock_init
678 (&vm_clone->worker_thread_main_loop_callback_lock);
679 clib_callback_data_init
680 (&vm_clone->vlib_node_runtime_perf_callbacks,
681 &vm_clone->worker_thread_main_loop_callback_lock);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682
Damjan Marionfd8deb42021-03-06 12:26:28 +0100683 nm = &vlib_get_first_main ()->node_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400684 nm_clone = &vm_clone->node_main;
685 /* fork next frames array, preserving node runtime indices */
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200686 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
687 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400688 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
689 {
690 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
691 u32 save_node_runtime_index;
692 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693
Dave Barach9b8ffd92016-07-08 08:13:45 -0400694 save_node_runtime_index = nf->node_runtime_index;
695 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
696 vlib_next_frame_init (nf);
697 nf->node_runtime_index = save_node_runtime_index;
698 nf->flags = save_flags;
699 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
Dave Barach9b8ffd92016-07-08 08:13:45 -0400701 /* fork the frame dispatch queue */
702 nm_clone->pending_frames = 0;
Dave Barach53fe4a72019-01-26 09:50:26 -0500703 vec_validate (nm_clone->pending_frames, 10);
Damjan Marion8bea5892022-04-04 22:40:45 +0200704 vec_set_len (nm_clone->pending_frames, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Dave Barach9b8ffd92016-07-08 08:13:45 -0400706 /* fork nodes */
707 nm_clone->nodes = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100708
709 /* Allocate all nodes in single block for speed */
710 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
711
Dave Barach9b8ffd92016-07-08 08:13:45 -0400712 for (j = 0; j < vec_len (nm->nodes); j++)
713 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400714 clib_memcpy (n, nm->nodes[j], sizeof (*n));
715 /* none of the copied nodes have enqueue rights given out */
716 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
Dave Barachb7b92992018-10-17 10:38:51 -0400717 clib_memset (&n->stats_total, 0, sizeof (n->stats_total));
718 clib_memset (&n->stats_last_clear, 0,
719 sizeof (n->stats_last_clear));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400720 vec_add1 (nm_clone->nodes, n);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100721 n++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400722 }
723 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200724 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
725 CLIB_CACHE_LINE_BYTES);
JingLiuZTE30af5da2017-07-24 10:53:31 +0800726 vec_foreach (rt,
727 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
Damjan Marione9f929b2017-03-16 11:32:09 +0100728 {
729 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marione9f929b2017-03-16 11:32:09 +0100730 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100731 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100732 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100733 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
734 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100735 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736
Dave Barach9b8ffd92016-07-08 08:13:45 -0400737 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200738 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
739 CLIB_CACHE_LINE_BYTES);
Damjan Marion94100532020-11-06 23:25:57 +0100740 clib_interrupt_init (
Damjan Marioncc8249c2023-07-23 14:24:22 +0200741 &nm_clone->input_node_interrupts,
Damjan Marion94100532020-11-06 23:25:57 +0100742 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]));
Damjan Marioncc8249c2023-07-23 14:24:22 +0200743 clib_interrupt_init (
744 &nm_clone->pre_input_node_interrupts,
745 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT]));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400746 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
Damjan Marione9f929b2017-03-16 11:32:09 +0100747 {
748 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marione9f929b2017-03-16 11:32:09 +0100749 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100750 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100751 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100752 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
753 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100754 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755
Dave Barach53fe4a72019-01-26 09:50:26 -0500756 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
757 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
758 CLIB_CACHE_LINE_BYTES);
759 vec_foreach (rt,
760 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
761 {
762 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Dave Barach53fe4a72019-01-26 09:50:26 -0500763 /* copy initial runtime_data from node */
764 if (n->runtime_data && n->runtime_data_bytes > 0)
765 clib_memcpy (rt->runtime_data, n->runtime_data,
766 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
767 n->runtime_data_bytes));
768 }
769
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200770 nm_clone->processes = vec_dup_aligned (nm->processes,
771 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Dave Barach593eedf2019-03-10 09:44:51 -0400773 /* Create per-thread frame freelist */
Damjan Marionb32bd702021-12-23 17:05:02 +0100774 nm_clone->frame_sizes = 0;
Dave Barach687c9022019-07-23 10:22:31 -0400775 nm_clone->node_by_error = nm->node_by_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776
Dave Barach9b8ffd92016-07-08 08:13:45 -0400777 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100778
Dave Barach9b8ffd92016-07-08 08:13:45 -0400779 clib_mem_set_heap (oldheap);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100780 vec_add1_aligned (vgm->vlib_mains, vm_clone,
781 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782
Ole Troan233e4682019-05-16 15:01:34 +0200783 /* Switch to the stats segment ... */
Damjan Marion66c85832022-03-14 13:04:38 +0100784 vlib_stats_validate (stats_err_entry_index, worker_thread_index,
785 vec_len (fvm->error_main.counters) - 1);
786 c = vlib_stats_get_entry_data_pointer (stats_err_entry_index);
787 vm_clone->error_main.counters = c[worker_thread_index];
Ole Troan233e4682019-05-16 15:01:34 +0200788
Damjan Marionfd8deb42021-03-06 12:26:28 +0100789 vm_clone->error_main.counters_last_clear = vec_dup_aligned (
790 vlib_get_first_main ()->error_main.counters_last_clear,
791 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792
Dave Barach9b8ffd92016-07-08 08:13:45 -0400793 worker_thread_index++;
794 }
795 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 }
797 else
798 {
799 /* only have non-data-structure copy threads to create... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400800 for (i = 0; i < vec_len (tm->registrations); i++)
801 {
802 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803
Dave Barach9b8ffd92016-07-08 08:13:45 -0400804 for (j = 0; j < tr->count; j++)
805 {
806 vec_add2 (vlib_worker_threads, w, 1);
807 if (tr->mheap_size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400808 {
Damjan Marion4537c302020-09-28 19:03:37 +0200809 w->thread_mheap = clib_mem_create_heap (0, tr->mheap_size,
810 /* locked */ 0,
811 "%s%d heap",
812 tr->name, j);
Dave Barach6a5adc32018-07-04 10:56:23 -0400813 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400814 else
815 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200816 w->thread_stack =
817 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400818 w->thread_function = tr->function;
819 w->thread_function_arg = w;
820 w->instance_id = j;
821 w->elog_track.name =
822 (char *) format (0, "%s %d", tr->name, j + 1);
823 w->registration = tr;
824 vec_add1 (w->elog_track.name, 0);
Damjan Marionf553a2c2021-03-26 13:45:37 +0100825 elog_track_register (vlib_get_elog_main (), &w->elog_track);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400826 }
827 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 }
829
830 worker_thread_index = 1;
831
832 for (i = 0; i < vec_len (tm->registrations); i++)
833 {
Damjan Marion878c6092017-01-04 13:19:27 +0100834 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835 int j;
836
837 tr = tm->registrations[i];
838
839 if (tr->use_pthreads || tm->use_pthreads)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400840 {
841 for (j = 0; j < tr->count; j++)
842 {
hsandid832342e2023-12-20 15:41:54 +0100843
Dave Barach9b8ffd92016-07-08 08:13:45 -0400844 w = vlib_worker_threads + worker_thread_index++;
Damjan Marion878c6092017-01-04 13:19:27 +0100845 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
846 w, 0);
847 if (err)
hsandid832342e2023-12-20 15:41:54 +0100848 clib_unix_error ("%U, thread %s init on cpu %d failed",
849 format_clib_error, err, tr->name, 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850 }
851 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400853 {
854 uword c;
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100855 clib_bitmap_foreach (c, tr->coremask) {
Damjan Marion878c6092017-01-04 13:19:27 +0100856 w = vlib_worker_threads + worker_thread_index++;
857 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
858 w, c);
859 if (err)
hsandid832342e2023-12-20 15:41:54 +0100860 clib_unix_error ("%U, thread %s init on cpu %d failed",
861 format_clib_error, err, tr->name, c);
862 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400863 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400865 vlib_worker_thread_barrier_sync (vm);
Damjan Marion321bd102022-06-01 00:45:18 +0200866 {
867 clib_error_t *err;
868 err = vlib_call_init_exit_functions (
869 vm, &vgm->num_workers_change_function_registrations, 1 /* call_once */,
870 1 /* is_global */);
871 if (err)
872 clib_error_report (err);
873 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400874 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875 return 0;
876}
877
878VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
879
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100880
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100881static inline void
882worker_thread_node_runtime_update_internal (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883{
884 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885 vlib_main_t *vm;
886 vlib_node_main_t *nm, *nm_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887 vlib_main_t *vm_clone;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100888 vlib_node_runtime_t *rt;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400889
Damjan Marion586afd72017-04-05 19:18:20 +0200890 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100892 vm = vlib_get_first_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893 nm = &vm->node_main;
894
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
896
Dave Barach9b8ffd92016-07-08 08:13:45 -0400897 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 * Scrape all runtime stats, so we don't lose node runtime(s) with
899 * pending counts, or throw away worker / io thread counts.
900 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400901 for (j = 0; j < vec_len (nm->nodes); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400903 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904 n = nm->nodes[j];
905 vlib_node_sync_stats (vm, n);
906 }
907
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100908 for (i = 1; i < vlib_get_n_threads (); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400910 vlib_node_t *n;
911
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100912 vm_clone = vlib_get_main_by_index (i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913 nm_clone = &vm_clone->node_main;
914
Dave Barach9b8ffd92016-07-08 08:13:45 -0400915 for (j = 0; j < vec_len (nm_clone->nodes); j++)
916 {
917 n = nm_clone->nodes[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918
Dave Barach9b8ffd92016-07-08 08:13:45 -0400919 rt = vlib_node_get_runtime (vm_clone, n->index);
920 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
921 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922 }
923
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100924 /* Per-worker clone rebuilds are now done on each thread */
925}
926
927
928void
929vlib_worker_thread_node_refork (void)
930{
931 vlib_main_t *vm, *vm_clone;
932 vlib_node_main_t *nm, *nm_clone;
933 vlib_node_t **old_nodes_clone;
934 vlib_node_runtime_t *rt, *old_rt;
Damjan Marion66c85832022-03-14 13:04:38 +0100935 u64 **c;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100936
937 vlib_node_t *new_n_clone;
938
939 int j;
940
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100941 vm = vlib_get_first_main ();
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100942 nm = &vm->node_main;
943 vm_clone = vlib_get_main ();
944 nm_clone = &vm_clone->node_main;
945
946 /* Re-clone error heap */
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100947 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
948
Dave Barach178cf492018-11-13 16:34:13 -0500949 clib_memcpy_fast (&vm_clone->error_main, &vm->error_main,
950 sizeof (vm->error_main));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100951 j = vec_len (vm->error_main.counters) - 1;
Ole Troan233e4682019-05-16 15:01:34 +0200952
Damjan Marion66c85832022-03-14 13:04:38 +0100953 c = vlib_stats_get_entry_data_pointer (vm->error_main.stats_err_entry_index);
954 vm_clone->error_main.counters = c[vm_clone->thread_index];
Ole Troan233e4682019-05-16 15:01:34 +0200955
956 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100957 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
958
Dmitry Valter9f5b3692022-09-05 15:30:18 +0000959 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
960 {
961 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
962 if ((nf->flags & VLIB_FRAME_IS_ALLOCATED) && nf->frame != NULL)
963 {
964 vlib_frame_t *f = nf->frame;
965 nf->frame = NULL;
966 vlib_frame_free (vm_clone, f);
967 }
968 }
969
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100970 vec_free (nm_clone->next_frames);
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200971 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
972 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100973
974 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100976 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
977 u32 save_node_runtime_index;
978 u32 save_flags;
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100979
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100980 save_node_runtime_index = nf->node_runtime_index;
981 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
982 vlib_next_frame_init (nf);
983 nf->node_runtime_index = save_node_runtime_index;
984 nf->flags = save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100986
987 old_nodes_clone = nm_clone->nodes;
988 nm_clone->nodes = 0;
989
990 /* re-fork nodes */
991
992 /* Allocate all nodes in single block for speed */
993 new_n_clone =
994 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
995 for (j = 0; j < vec_len (nm->nodes); j++)
996 {
Benoît Ganne5517bd32019-08-30 16:20:12 +0200997 vlib_node_t *new_n = nm->nodes[j];
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100998
Dave Barach178cf492018-11-13 16:34:13 -0500999 clib_memcpy_fast (new_n_clone, new_n, sizeof (*new_n));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001000 /* none of the copied nodes have enqueue rights given out */
1001 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1002
1003 if (j >= vec_len (old_nodes_clone))
1004 {
1005 /* new node, set to zero */
Dave Barachb7b92992018-10-17 10:38:51 -04001006 clib_memset (&new_n_clone->stats_total, 0,
1007 sizeof (new_n_clone->stats_total));
1008 clib_memset (&new_n_clone->stats_last_clear, 0,
1009 sizeof (new_n_clone->stats_last_clear));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001010 }
1011 else
1012 {
Benoît Ganne5517bd32019-08-30 16:20:12 +02001013 vlib_node_t *old_n_clone = old_nodes_clone[j];
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001014 /* Copy stats if the old data is valid */
Dave Barach178cf492018-11-13 16:34:13 -05001015 clib_memcpy_fast (&new_n_clone->stats_total,
1016 &old_n_clone->stats_total,
1017 sizeof (new_n_clone->stats_total));
1018 clib_memcpy_fast (&new_n_clone->stats_last_clear,
1019 &old_n_clone->stats_last_clear,
1020 sizeof (new_n_clone->stats_last_clear));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001021
1022 /* keep previous node state */
1023 new_n_clone->state = old_n_clone->state;
Maxime Peimdde163d2021-04-15 13:52:12 +02001024 new_n_clone->flags = old_n_clone->flags;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001025 }
1026 vec_add1 (nm_clone->nodes, new_n_clone);
1027 new_n_clone++;
1028 }
1029 /* Free the old node clones */
1030 clib_mem_free (old_nodes_clone[0]);
1031
1032 vec_free (old_nodes_clone);
1033
1034
1035 /* re-clone internal nodes */
1036 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1037 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001038 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1039 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001040
1041 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1042 {
1043 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001044 /* copy runtime_data, will be overwritten later for existing rt */
1045 if (n->runtime_data && n->runtime_data_bytes > 0)
Dave Barach178cf492018-11-13 16:34:13 -05001046 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1047 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1048 n->runtime_data_bytes));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001049 }
1050
1051 for (j = 0; j < vec_len (old_rt); j++)
1052 {
1053 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1054 rt->state = old_rt[j].state;
Maxime Peimdde163d2021-04-15 13:52:12 +02001055 rt->flags = old_rt[j].flags;
Dave Barach178cf492018-11-13 16:34:13 -05001056 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1057 VLIB_NODE_RUNTIME_DATA_SIZE);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001058 }
1059
1060 vec_free (old_rt);
1061
1062 /* re-clone input nodes */
1063 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1064 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001065 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1066 CLIB_CACHE_LINE_BYTES);
Damjan Marion94100532020-11-06 23:25:57 +01001067 clib_interrupt_resize (
Damjan Marioncc8249c2023-07-23 14:24:22 +02001068 &nm_clone->input_node_interrupts,
Damjan Marion94100532020-11-06 23:25:57 +01001069 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]));
Damjan Marioncc8249c2023-07-23 14:24:22 +02001070 clib_interrupt_resize (
1071 &nm_clone->pre_input_node_interrupts,
1072 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT]));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001073
1074 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1075 {
1076 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001077 /* copy runtime_data, will be overwritten later for existing rt */
1078 if (n->runtime_data && n->runtime_data_bytes > 0)
Dave Barach178cf492018-11-13 16:34:13 -05001079 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1080 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1081 n->runtime_data_bytes));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001082 }
1083
1084 for (j = 0; j < vec_len (old_rt); j++)
1085 {
1086 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1087 rt->state = old_rt[j].state;
Maxime Peimdde163d2021-04-15 13:52:12 +02001088 rt->flags = old_rt[j].flags;
Dave Barach178cf492018-11-13 16:34:13 -05001089 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1090 VLIB_NODE_RUNTIME_DATA_SIZE);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001091 }
1092
1093 vec_free (old_rt);
1094
Dave Barach53fe4a72019-01-26 09:50:26 -05001095 /* re-clone pre-input nodes */
1096 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT];
1097 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
1098 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
1099 CLIB_CACHE_LINE_BYTES);
1100
1101 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1102 {
1103 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Dave Barach53fe4a72019-01-26 09:50:26 -05001104 /* copy runtime_data, will be overwritten later for existing rt */
1105 if (n->runtime_data && n->runtime_data_bytes > 0)
1106 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1107 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1108 n->runtime_data_bytes));
1109 }
1110
1111 for (j = 0; j < vec_len (old_rt); j++)
1112 {
1113 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1114 rt->state = old_rt[j].state;
Maxime Peimdde163d2021-04-15 13:52:12 +02001115 rt->flags = old_rt[j].flags;
Dave Barach53fe4a72019-01-26 09:50:26 -05001116 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1117 VLIB_NODE_RUNTIME_DATA_SIZE);
1118 }
1119
1120 vec_free (old_rt);
1121
Vladislav Grishenko29733502021-09-25 21:00:59 +05001122 vec_free (nm_clone->processes);
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001123 nm_clone->processes = vec_dup_aligned (nm->processes,
1124 CLIB_CACHE_LINE_BYTES);
Dave Barach687c9022019-07-23 10:22:31 -04001125 nm_clone->node_by_error = nm->node_by_error;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001126}
1127
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001128void
1129vlib_worker_thread_node_runtime_update (void)
1130{
1131 /*
1132 * Make a note that we need to do a node runtime update
1133 * prior to releasing the barrier.
1134 */
1135 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001136}
1137
Pavel Kotucek1e765832016-09-23 08:54:14 +02001138u32
1139unformat_sched_policy (unformat_input_t * input, va_list * args)
1140{
1141 u32 *r = va_arg (*args, u32 *);
1142
1143 if (0);
1144#define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1145 foreach_sched_policy
1146#undef _
1147 else
1148 return 0;
1149 return 1;
1150}
1151
Ed Warnickecb9cada2015-12-08 15:45:58 -07001152static clib_error_t *
1153cpu_config (vlib_main_t * vm, unformat_input_t * input)
1154{
1155 vlib_thread_registration_t *tr;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001156 uword *p;
1157 vlib_thread_main_t *tm = &vlib_thread_main;
1158 u8 *name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001159 uword *bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160 u32 count;
1161
1162 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
Pavel Kotucek1e765832016-09-23 08:54:14 +02001163
Dave Barach9b8ffd92016-07-08 08:13:45 -04001164 tm->n_thread_stacks = 1; /* account for main thread */
Pavel Kotucek1e765832016-09-23 08:54:14 +02001165 tm->sched_policy = ~0;
1166 tm->sched_priority = ~0;
Damjan Marion858151f2018-07-11 10:51:00 +02001167 tm->main_lcore = ~0;
hsandid71c32a82024-03-25 17:51:31 +01001168 tm->use_main_core_auto = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169
1170 tr = tm->next;
1171
1172 while (tr)
1173 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001174 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175 tr = tr->next;
1176 }
1177
Dave Barach9b8ffd92016-07-08 08:13:45 -04001178 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179 {
Damjan Marionbf741472016-06-13 22:49:44 +02001180 if (unformat (input, "use-pthreads"))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001181 tm->use_pthreads = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001182 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001183 ;
hsandid71c32a82024-03-25 17:51:31 +01001184 else if (unformat (input, "main-core auto"))
1185 tm->use_main_core_auto = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186 else if (unformat (input, "main-core %u", &tm->main_lcore))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001187 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001189 ;
Dave Baracha690fdb2020-01-21 12:34:55 -05001190 else if (unformat (input, "numa-heap-size %U",
1191 unformat_memory_size, &tm->numa_heap_size))
1192 ;
Yi Hee4a9eb72018-07-17 14:18:41 +08001193 else if (unformat (input, "coremask-%s %U", &name,
1194 unformat_bitmap_mask, &bitmap) ||
1195 unformat (input, "corelist-%s %U", &name,
1196 unformat_bitmap_list, &bitmap))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001197 {
1198 p = hash_get_mem (tm->thread_registrations_by_name, name);
1199 if (p == 0)
1200 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201
Dave Barach9b8ffd92016-07-08 08:13:45 -04001202 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001203
Dave Barach9b8ffd92016-07-08 08:13:45 -04001204 if (tr->use_pthreads)
1205 return clib_error_return (0,
1206 "corelist cannot be set for '%s' threads",
1207 name);
Vladimir Isaev18a4a372020-03-17 12:30:11 +03001208 if (tr->count)
1209 return clib_error_return
1210 (0, "core placement of '%s' threads is already configured",
1211 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212
Dave Barach9b8ffd92016-07-08 08:13:45 -04001213 tr->coremask = bitmap;
1214 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1215 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001216 else
1217 if (unformat
1218 (input, "scheduler-policy %U", unformat_sched_policy,
1219 &tm->sched_policy))
1220 ;
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001221 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
Pavel Kotucek1e765832016-09-23 08:54:14 +02001222 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223 else if (unformat (input, "%s %u", &name, &count))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001224 {
1225 p = hash_get_mem (tm->thread_registrations_by_name, name);
1226 if (p == 0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001227 return clib_error_return (0, "no such thread type 3 '%s'", name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001228
1229 tr = (vlib_thread_registration_t *) p[0];
Vladimir Isaev18a4a372020-03-17 12:30:11 +03001230
Dave Barach9b8ffd92016-07-08 08:13:45 -04001231 if (tr->fixed_count)
1232 return clib_error_return
Vladimir Isaev18a4a372020-03-17 12:30:11 +03001233 (0, "number of '%s' threads not configurable", name);
1234 if (tr->count)
1235 return clib_error_return
1236 (0, "number of '%s' threads is already configured", name);
1237
Dave Barach9b8ffd92016-07-08 08:13:45 -04001238 tr->count = count;
1239 }
1240 else
1241 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 }
1243
hsandid71c32a82024-03-25 17:51:31 +01001244 if (tm->main_lcore != ~0 && tm->use_main_core_auto)
1245 {
1246 return clib_error_return (
1247 0, "cannot set both 'main-core %u' and 'main-core auto'",
1248 tm->main_lcore);
1249 }
1250
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001251 if (tm->sched_priority != ~0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001252 {
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001253 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001254 {
1255 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1256 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1257 if (tm->sched_priority > prio_max)
1258 tm->sched_priority = prio_max;
1259 if (tm->sched_priority < prio_min)
1260 tm->sched_priority = prio_min;
1261 }
1262 else
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001263 {
1264 return clib_error_return
1265 (0,
1266 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1267 tm->sched_priority);
1268 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001269 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001270 tr = tm->next;
1271
1272 if (!tm->thread_prefix)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001273 tm->thread_prefix = format (0, "vpp");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001274
1275 while (tr)
1276 {
1277 tm->n_thread_stacks += tr->count;
1278 tm->n_pthreads += tr->count * tr->use_pthreads;
Damjan Marion878c6092017-01-04 13:19:27 +01001279 tm->n_threads += tr->count * (tr->use_pthreads == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001280 tr = tr->next;
1281 }
1282
1283 return 0;
1284}
1285
1286VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1287
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001288 /*
1289 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1290 * based on a test based heuristic that barrier should be open for at least
1291 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1292 * point it is probably too late to make a difference)
1293 */
1294
1295#ifndef BARRIER_MINIMUM_OPEN_LIMIT
1296#define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1297#endif
1298
1299#ifndef BARRIER_MINIMUM_OPEN_FACTOR
1300#define BARRIER_MINIMUM_OPEN_FACTOR 3
1301#endif
1302
Dave Barach9b8ffd92016-07-08 08:13:45 -04001303void
Dave Barachc602b382019-06-03 19:48:22 -04001304vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm)
1305{
1306 f64 deadline;
1307 f64 now = vlib_time_now (vm);
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001308 u32 count = vlib_get_n_threads () - 1;
Dave Barachc602b382019-06-03 19:48:22 -04001309
1310 /* No worker threads? */
1311 if (count == 0)
1312 return;
1313
1314 deadline = now + BARRIER_SYNC_TIMEOUT;
1315 *vlib_worker_threads->wait_at_barrier = 1;
1316 while (*vlib_worker_threads->workers_at_barrier != count)
1317 {
1318 if ((now = vlib_time_now (vm)) > deadline)
1319 {
1320 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1321 os_panic ();
1322 }
1323 CLIB_PAUSE ();
1324 }
1325 *vlib_worker_threads->wait_at_barrier = 0;
1326}
1327
Neale Ranns42845dd2020-05-26 13:12:17 +00001328/**
1329 * Return true if the wroker thread barrier is held
1330 */
1331u8
1332vlib_worker_thread_barrier_held (void)
1333{
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001334 if (vlib_get_n_threads () < 2)
Neale Ranns42845dd2020-05-26 13:12:17 +00001335 return (1);
1336
1337 return (*vlib_worker_threads->wait_at_barrier == 1);
1338}
1339
Dave Barachc602b382019-06-03 19:48:22 -04001340void
Damjan Marion8343ee52019-02-26 17:15:48 +01001341vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, const char *func_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001342{
1343 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001344 f64 now;
1345 f64 t_entry;
1346 f64 t_open;
1347 f64 t_closed;
Dave Barach9ae190e2019-04-23 10:07:24 -04001348 f64 max_vector_rate;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001349 u32 count;
Dave Barach9ae190e2019-04-23 10:07:24 -04001350 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001351
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001352 if (vlib_get_n_threads () < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001353 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001354
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001355 ASSERT (vlib_get_thread_index () == 0);
1356
Damjan Marion8343ee52019-02-26 17:15:48 +01001357 vlib_worker_threads[0].barrier_caller = func_name;
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001358 count = vlib_get_n_threads () - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001359
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001360 /* Record entry relative to last close */
1361 now = vlib_time_now (vm);
1362 t_entry = now - vm->barrier_epoch;
1363
Ed Warnickecb9cada2015-12-08 15:45:58 -07001364 /* Tolerate recursive calls */
1365 if (++vlib_worker_threads[0].recursion_level > 1)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001366 {
1367 barrier_trace_sync_rec (t_entry);
1368 return;
1369 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001370
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +00001371 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
1372 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
1373 vm->clib_time.last_cpu_time, 0 /* enter */ );
1374
Dave Barach9ae190e2019-04-23 10:07:24 -04001375 /*
1376 * Need data to decide if we're working hard enough to honor
1377 * the barrier hold-down timer.
1378 */
1379 max_vector_rate = 0.0;
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001380 for (i = 1; i < vlib_get_n_threads (); i++)
1381 {
1382 vlib_main_t *ovm = vlib_get_main_by_index (i);
1383 max_vector_rate = clib_max (max_vector_rate,
1384 (f64) vlib_last_vectors_per_main_loop (ovm));
1385 }
Dave Barach9ae190e2019-04-23 10:07:24 -04001386
Bud Grise42f20062016-03-16 13:09:46 -04001387 vlib_worker_threads[0].barrier_sync_count++;
1388
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001389 /* Enforce minimum barrier open time to minimize packet loss */
1390 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001391
Dave Barach9ae190e2019-04-23 10:07:24 -04001392 /*
1393 * If any worker thread seems busy, which we define
1394 * as a vector rate above 10, we enforce the barrier hold-down timer
1395 */
1396 if (max_vector_rate > 10.0)
Dave Barach36feebb2018-09-07 11:12:27 -04001397 {
Dave Barach9ae190e2019-04-23 10:07:24 -04001398 while (1)
Dave Barach36feebb2018-09-07 11:12:27 -04001399 {
Dave Barach9ae190e2019-04-23 10:07:24 -04001400 now = vlib_time_now (vm);
1401 /* Barrier hold-down timer expired? */
1402 if (now >= vm->barrier_no_close_before)
1403 break;
1404 if ((vm->barrier_no_close_before - now)
1405 > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1406 {
1407 clib_warning
1408 ("clock change: would have waited for %.4f seconds",
1409 (vm->barrier_no_close_before - now));
1410 break;
1411 }
Dave Barach36feebb2018-09-07 11:12:27 -04001412 }
1413 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001414 /* Record time of closure */
1415 t_open = now - vm->barrier_epoch;
1416 vm->barrier_epoch = now;
1417
1418 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001419
1420 *vlib_worker_threads->wait_at_barrier = 1;
1421 while (*vlib_worker_threads->workers_at_barrier != count)
1422 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001423 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001424 {
1425 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1426 os_panic ();
1427 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001428 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001429
1430 t_closed = now - vm->barrier_epoch;
1431
1432 barrier_trace_sync (t_entry, t_open, t_closed);
1433
Ed Warnickecb9cada2015-12-08 15:45:58 -07001434}
1435
Dave Barach9b8ffd92016-07-08 08:13:45 -04001436void
1437vlib_worker_thread_barrier_release (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001438{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001439 vlib_global_main_t *vgm = vlib_get_global_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001440 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001441 f64 now;
1442 f64 minimum_open;
1443 f64 t_entry;
1444 f64 t_closed_total;
1445 f64 t_update_main = 0.0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001446 int refork_needed = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001448 if (vlib_get_n_threads () < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001449 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001450
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001451 ASSERT (vlib_get_thread_index () == 0);
1452
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001453
1454 now = vlib_time_now (vm);
1455 t_entry = now - vm->barrier_epoch;
1456
Ed Warnickecb9cada2015-12-08 15:45:58 -07001457 if (--vlib_worker_threads[0].recursion_level > 0)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001458 {
1459 barrier_trace_release_rec (t_entry);
1460 return;
1461 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001463 /* Update (all) node runtimes before releasing the barrier, if needed */
Damjan Marionfd8deb42021-03-06 12:26:28 +01001464 if (vgm->need_vlib_worker_thread_node_runtime_update)
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001465 {
Dave Barach1ddbc012018-06-13 09:26:05 -04001466 /*
1467 * Lock stat segment here, so we's safe when
1468 * rebuilding the stat segment node clones from the
1469 * stat thread...
1470 */
Damjan Marion8973b072022-03-01 15:51:18 +01001471 vlib_stats_segment_lock ();
Dave Barach1ddbc012018-06-13 09:26:05 -04001472
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001473 /* Do stats elements on main thread */
1474 worker_thread_node_runtime_update_internal ();
Damjan Marionfd8deb42021-03-06 12:26:28 +01001475 vgm->need_vlib_worker_thread_node_runtime_update = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001476
1477 /* Do per thread rebuilds in parallel */
1478 refork_needed = 1;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +00001479 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001480 (vlib_get_n_threads () - 1));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001481 now = vlib_time_now (vm);
1482 t_update_main = now - vm->barrier_epoch;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001483 }
1484
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001485 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001486
Dave Baracha4324a92019-02-19 17:05:30 -05001487 /*
1488 * Note when we let go of the barrier.
1489 * Workers can use this to derive a reasonably accurate
1490 * time offset. See vlib_time_now(...)
1491 */
1492 vm->time_last_barrier_release = vlib_time_now (vm);
1493 CLIB_MEMORY_STORE_BARRIER ();
1494
Ed Warnickecb9cada2015-12-08 15:45:58 -07001495 *vlib_worker_threads->wait_at_barrier = 0;
1496
1497 while (*vlib_worker_threads->workers_at_barrier > 0)
1498 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001499 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001500 {
1501 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1502 os_panic ();
1503 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001504 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001505
1506 /* Wait for reforks before continuing */
1507 if (refork_needed)
1508 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001509 now = vlib_time_now (vm);
1510
1511 deadline = now + BARRIER_SYNC_TIMEOUT;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001512
1513 while (*vlib_worker_threads->node_reforks_required > 0)
1514 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001515 if ((now = vlib_time_now (vm)) > deadline)
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001516 {
1517 fformat (stderr, "%s: worker thread refork deadlock\n",
1518 __FUNCTION__);
1519 os_panic ();
1520 }
1521 }
Damjan Marion8973b072022-03-01 15:51:18 +01001522 vlib_stats_segment_unlock ();
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001523 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001524
1525 t_closed_total = now - vm->barrier_epoch;
1526
1527 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1528
1529 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1530 {
1531 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1532 }
1533
1534 vm->barrier_no_close_before = now + minimum_open;
1535
1536 /* Record barrier epoch (used to enforce minimum open time) */
1537 vm->barrier_epoch = now;
1538
1539 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1540
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +00001541 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
1542 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
1543 vm->clib_time.last_cpu_time, 1 /* leave */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07001544}
1545
Florin Coras4b208302022-03-30 13:50:19 -07001546static void
1547vlib_worker_sync_rpc (void *args)
1548{
1549 ASSERT (vlib_thread_is_main_w_barrier ());
1550 vlib_worker_threads->wait_before_barrier = 0;
1551}
1552
1553void
1554vlib_workers_sync (void)
1555{
1556 if (PREDICT_FALSE (!vlib_num_workers ()))
1557 return;
1558
1559 if (!(*vlib_worker_threads->wait_at_barrier) &&
1560 !clib_atomic_swap_rel_n (&vlib_worker_threads->wait_before_barrier, 1))
1561 {
1562 u32 thread_index = vlib_get_thread_index ();
1563 vlib_rpc_call_main_thread (vlib_worker_sync_rpc, (u8 *) &thread_index,
1564 sizeof (thread_index));
Florin Corase060b0a2024-02-01 21:13:10 -08001565 vlib_worker_flush_pending_rpc_requests (vlib_get_main ());
Florin Coras4b208302022-03-30 13:50:19 -07001566 }
1567
1568 /* Wait until main thread asks for barrier */
1569 while (!(*vlib_worker_threads->wait_at_barrier))
1570 ;
1571
1572 /* Stop before barrier and make sure all threads are either
1573 * at worker barrier or the barrier before it */
1574 clib_atomic_fetch_add (&vlib_worker_threads->workers_before_barrier, 1);
1575 while (vlib_num_workers () > (*vlib_worker_threads->workers_at_barrier +
1576 vlib_worker_threads->workers_before_barrier))
1577 ;
1578}
1579
1580void
1581vlib_workers_continue (void)
1582{
1583 if (PREDICT_FALSE (!vlib_num_workers ()))
1584 return;
1585
1586 clib_atomic_fetch_add (&vlib_worker_threads->done_work_before_barrier, 1);
1587
1588 /* Wait until all workers are done with work before barrier */
1589 while (vlib_worker_threads->done_work_before_barrier <
1590 vlib_worker_threads->workers_before_barrier)
1591 ;
1592
1593 clib_atomic_fetch_add (&vlib_worker_threads->done_work_before_barrier, -1);
1594 clib_atomic_fetch_add (&vlib_worker_threads->workers_before_barrier, -1);
1595}
1596
Neale Ranns42845dd2020-05-26 13:12:17 +00001597/**
1598 * Wait until each of the workers has been once around the track
1599 */
1600void
1601vlib_worker_wait_one_loop (void)
1602{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001603 vlib_global_main_t *vgm = vlib_get_global_main ();
Neale Ranns42845dd2020-05-26 13:12:17 +00001604 ASSERT (vlib_get_thread_index () == 0);
1605
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001606 if (vlib_get_n_threads () < 2)
Neale Ranns42845dd2020-05-26 13:12:17 +00001607 return;
1608
1609 if (vlib_worker_thread_barrier_held ())
1610 return;
1611
1612 u32 *counts = 0;
1613 u32 ii;
1614
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001615 vec_validate (counts, vlib_get_n_threads () - 1);
Neale Ranns42845dd2020-05-26 13:12:17 +00001616
1617 /* record the current loop counts */
Damjan Marionfd8deb42021-03-06 12:26:28 +01001618 vec_foreach_index (ii, vgm->vlib_mains)
1619 counts[ii] = vgm->vlib_mains[ii]->main_loop_count;
Neale Ranns42845dd2020-05-26 13:12:17 +00001620
1621 /* spin until each changes, apart from the main thread, or we'd be
1622 * a while */
1623 for (ii = 1; ii < vec_len (counts); ii++)
1624 {
Damjan Marionfd8deb42021-03-06 12:26:28 +01001625 while (counts[ii] == vgm->vlib_mains[ii]->main_loop_count)
Neale Ranns42845dd2020-05-26 13:12:17 +00001626 CLIB_PAUSE ();
1627 }
1628
1629 vec_free (counts);
1630 return;
1631}
1632
Dave Barach9b8ffd92016-07-08 08:13:45 -04001633void
Florin Coras4cadd3b2024-02-01 20:46:15 -08001634vlib_worker_flush_pending_rpc_requests (vlib_main_t *vm)
1635{
1636 vlib_main_t *vm_global = vlib_get_first_main ();
1637
1638 ASSERT (vm != vm_global);
1639
1640 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
1641 vec_append (vm_global->pending_rpc_requests, vm->pending_rpc_requests);
1642 vec_reset_length (vm->pending_rpc_requests);
1643 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
1644}
1645
1646void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001647vlib_worker_thread_fn (void *arg)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001648{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001649 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -04001650 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001651 vlib_main_t *vm = vlib_get_main ();
Damjan Marione9f929b2017-03-16 11:32:09 +01001652 clib_error_t *e;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001653
Damjan Marion586afd72017-04-05 19:18:20 +02001654 ASSERT (vm->thread_index == vlib_get_thread_index ());
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001655
1656 vlib_worker_thread_init (w);
1657 clib_time_init (&vm->clib_time);
1658 clib_mem_set_heap (w->thread_mheap);
1659
Damjan Marionfd8deb42021-03-06 12:26:28 +01001660 vm->worker_init_functions_called = hash_create (0, 0);
1661
1662 e = vlib_call_init_exit_functions_no_sort (
1663 vm, &vgm->worker_init_function_registrations, 1 /* call_once */,
1664 0 /* is_global */);
Damjan Marione9f929b2017-03-16 11:32:09 +01001665 if (e)
1666 clib_error_report (e);
1667
Damjan Marione9d52d52017-03-09 15:42:26 +01001668 vlib_worker_loop (vm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001669}
1670
1671VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1672 .name = "workers",
1673 .short_name = "wk",
1674 .function = vlib_worker_thread_fn,
1675};
1676
Mohammed Hawarie7149262022-05-18 10:08:47 +02001677extern clib_march_fn_registration
1678 *vlib_frame_queue_dequeue_with_aux_fn_march_fn_registrations;
1679extern clib_march_fn_registration
1680 *vlib_frame_queue_dequeue_fn_march_fn_registrations;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001681u32
1682vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1683{
1684 vlib_thread_main_t *tm = vlib_get_thread_main ();
Mohammed Hawarie7149262022-05-18 10:08:47 +02001685 vlib_main_t *vm = vlib_get_main ();
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001686 vlib_frame_queue_main_t *fqm;
1687 vlib_frame_queue_t *fq;
Mohammed Hawarie7149262022-05-18 10:08:47 +02001688 vlib_node_t *node;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001689 int i;
Elias Rudberg368104d2020-04-16 16:01:52 +02001690 u32 num_threads;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001691
1692 if (frame_queue_nelts == 0)
dongjuan88752482019-06-04 10:59:02 +08001693 frame_queue_nelts = FRAME_QUEUE_MAX_NELTS;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001694
Elias Rudberg368104d2020-04-16 16:01:52 +02001695 num_threads = 1 /* main thread */ + tm->n_threads;
1696 ASSERT (frame_queue_nelts >= 8 + num_threads);
Damjan Marion78fd7e82018-07-20 18:47:05 +02001697
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001698 vec_add2 (tm->frame_queue_mains, fqm, 1);
1699
Mohammed Hawarie7149262022-05-18 10:08:47 +02001700 node = vlib_get_node (vm, fqm->node_index);
1701 ASSERT (node);
1702 if (node->aux_offset)
1703 {
1704 fqm->frame_queue_dequeue_fn =
1705 CLIB_MARCH_FN_VOID_POINTER (vlib_frame_queue_dequeue_with_aux_fn);
1706 }
1707 else
1708 {
1709 fqm->frame_queue_dequeue_fn =
1710 CLIB_MARCH_FN_VOID_POINTER (vlib_frame_queue_dequeue_fn);
1711 }
1712
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001713 fqm->node_index = node_index;
Damjan Marion78fd7e82018-07-20 18:47:05 +02001714 fqm->frame_queue_nelts = frame_queue_nelts;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001715
1716 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
Damjan Marion8bea5892022-04-04 22:40:45 +02001717 vec_set_len (fqm->vlib_frame_queues, 0);
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001718 for (i = 0; i < tm->n_vlib_mains; i++)
1719 {
1720 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1721 vec_add1 (fqm->vlib_frame_queues, fq);
1722 }
1723
1724 return (fqm - tm->frame_queue_mains);
1725}
1726
Dave Barach69128d02017-09-26 10:54:34 -04001727void
1728vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1729 args)
1730{
1731 ASSERT (vlib_get_thread_index () == 0);
1732 vlib_process_signal_event (vlib_get_main (), args->node_index,
1733 args->type_opaque, args->data);
1734}
1735
1736void *rpc_call_main_thread_cb_fn;
1737
1738void
1739vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1740{
1741 if (rpc_call_main_thread_cb_fn)
1742 {
1743 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1744 (*fp) (callback, args, arg_size);
1745 }
1746 else
1747 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1748}
1749
Dave Barach9b8ffd92016-07-08 08:13:45 -04001750clib_error_t *
1751threads_init (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001752{
Benoît Ganne8b153de2021-12-09 18:24:21 +01001753 const vlib_thread_main_t *tm = vlib_get_thread_main ();
1754
1755 if (tm->main_lcore == ~0 && tm->n_vlib_mains > 1)
1756 return clib_error_return (0, "Configuration error, a main core must "
1757 "be specified when using worker threads");
1758
Ed Warnickecb9cada2015-12-08 15:45:58 -07001759 return 0;
1760}
1761
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001762VLIB_INIT_FUNCTION (threads_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001763
Dave Baracha4324a92019-02-19 17:05:30 -05001764static clib_error_t *
1765show_clock_command_fn (vlib_main_t * vm,
1766 unformat_input_t * input, vlib_cli_command_t * cmd)
1767{
Dave Barachc25048b2020-01-29 18:05:24 -05001768 int verbose = 0;
Dave Barach19718002020-03-11 10:31:36 -04001769 clib_timebase_t _tb, *tb = &_tb;
Dave Baracha4324a92019-02-19 17:05:30 -05001770
Dave Barachc25048b2020-01-29 18:05:24 -05001771 (void) unformat (input, "verbose %=", &verbose, 1);
Dave Baracha4324a92019-02-19 17:05:30 -05001772
Dave Barach19718002020-03-11 10:31:36 -04001773 clib_timebase_init (tb, 0 /* GMT */ , CLIB_TIMEBASE_DAYLIGHT_NONE,
1774 &vm->clib_time);
1775
1776 vlib_cli_output (vm, "%U, %U GMT", format_clib_time, &vm->clib_time,
1777 verbose, format_clib_timebase_time,
1778 clib_timebase_now (tb));
Dave Baracha4324a92019-02-19 17:05:30 -05001779
Dave Baracha4324a92019-02-19 17:05:30 -05001780 vlib_cli_output (vm, "Time last barrier release %.9f",
1781 vm->time_last_barrier_release);
1782
Benoît Ganne56eccdb2021-08-20 09:18:31 +02001783 foreach_vlib_main ()
Dave Baracha4324a92019-02-19 17:05:30 -05001784 {
Benoît Ganne56eccdb2021-08-20 09:18:31 +02001785 vlib_cli_output (vm, "%d: %U", this_vlib_main->thread_index,
1786 format_clib_time, &this_vlib_main->clib_time, verbose);
Dave Barachc25048b2020-01-29 18:05:24 -05001787
Benoît Ganne56eccdb2021-08-20 09:18:31 +02001788 vlib_cli_output (vm, "Thread %d offset %.9f error %.9f",
1789 this_vlib_main->thread_index,
1790 this_vlib_main->time_offset,
1791 vm->time_last_barrier_release -
1792 this_vlib_main->time_last_barrier_release);
Dave Baracha4324a92019-02-19 17:05:30 -05001793 }
1794 return 0;
1795}
1796
Dave Baracha4324a92019-02-19 17:05:30 -05001797VLIB_CLI_COMMAND (f_command, static) =
1798{
1799 .path = "show clock",
1800 .short_help = "show clock",
1801 .function = show_clock_command_fn,
1802};
Dave Baracha4324a92019-02-19 17:05:30 -05001803
Dave Barachab1a50c2020-10-06 14:08:16 -04001804vlib_thread_main_t *
1805vlib_get_thread_main_not_inline (void)
1806{
1807 return vlib_get_thread_main ();
1808}
1809
Dave Barach9b8ffd92016-07-08 08:13:45 -04001810/*
1811 * fd.io coding-style-patch-verification: ON
1812 *
1813 * Local Variables:
1814 * eval: (c-set-style "gnu")
1815 * End:
1816 */