blob: 713e1927d1f29ad39b41cfb43bdb8688970f1429 [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;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400182 uword *avail_cpu;
Damjan Marion8973b072022-03-01 15:51:18 +0100183 u32 stats_num_worker_threads_dir_index;
184
185 stats_num_worker_threads_dir_index =
186 vlib_stats_add_gauge ("/sys/num_worker_threads");
187 ASSERT (stats_num_worker_threads_dir_index != ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
189 /* get bitmaps of active cpu cores and sockets */
Damjan Marion4b661402024-02-29 16:14:27 +0100190 tm->cpu_core_bitmap = os_get_online_cpu_core_bitmap ();
191 tm->cpu_socket_bitmap = os_get_online_cpu_node_bitmap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Dave Barach9b8ffd92016-07-08 08:13:45 -0400193 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
195 /* skip cores */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400196 for (i = 0; i < tm->skip_cores; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 uword c = clib_bitmap_first_set (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 if (c == ~0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400200 return clib_error_return (0, "no available cpus to skip");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Dave Barach9b8ffd92016-07-08 08:13:45 -0400202 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203 }
204
205 /* grab cpu for main thread */
Benoît Ganne8b153de2021-12-09 18:24:21 +0100206 if (tm->main_lcore != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400208 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
209 return clib_error_return (0, "cpu %u is not available to be used"
210 " for the main thread", tm->main_lcore);
Benoît Ganne8b153de2021-12-09 18:24:21 +0100211 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
214 /* assume that there is socket 0 only if there is no data from sysfs */
215 if (!tm->cpu_socket_bitmap)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400216 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217
Christophe Fontainefef15b42016-04-09 12:38:49 +0900218 /* pin main thread to main_lcore */
Benoît Ganne8b153de2021-12-09 18:24:21 +0100219 if (tm->main_lcore != ~0)
220 {
221 cpu_set_t cpuset;
222 CPU_ZERO (&cpuset);
223 CPU_SET (tm->main_lcore, &cpuset);
hsandid832342e2023-12-20 15:41:54 +0100224 if (pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t),
225 &cpuset))
226 {
227 return clib_error_return (0, "could not pin main thread to cpu %u",
228 tm->main_lcore);
229 }
Benoît Ganne8b153de2021-12-09 18:24:21 +0100230 }
Christophe Fontainefef15b42016-04-09 12:38:49 +0900231
Dave Barach2180bac2019-05-10 15:25:10 -0400232 /* Set up thread 0 */
233 vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion8bea5892022-04-04 22:40:45 +0200234 vec_set_len (vlib_worker_threads, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 w = vlib_worker_threads;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400236 w->thread_mheap = clib_mem_get_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 w->thread_stack = vlib_thread_stacks[0];
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200238 w->cpu_id = tm->main_lcore;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400239 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200240 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 tm->n_vlib_mains = 1;
242
Jon Loeligerf617b142020-01-30 18:37:47 -0600243 vlib_get_thread_core_numa (w, w->cpu_id);
244
Pavel Kotucek1e765832016-09-23 08:54:14 +0200245 if (tm->sched_policy != ~0)
246 {
247 struct sched_param sched_param;
248 if (!sched_getparam (w->lwp, &sched_param))
249 {
250 if (tm->sched_priority != ~0)
251 sched_param.sched_priority = tm->sched_priority;
252 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
253 }
254 }
255
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256 /* assign threads to cores and set n_vlib_mains */
257 tr = tm->next;
258
259 while (tr)
260 {
261 vec_add1 (tm->registrations, tr);
262 tr = tr->next;
263 }
264
Dave Barach9b8ffd92016-07-08 08:13:45 -0400265 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266
267 for (i = 0; i < vec_len (tm->registrations); i++)
268 {
269 int j;
270 tr = tm->registrations[i];
271 tr->first_index = first_index;
272 first_index += tr->count;
273 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
274
275 /* construct coremask */
276 if (tr->use_pthreads || !tr->count)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400277 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
279 if (tr->coremask)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400280 {
281 uword c;
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100282 clib_bitmap_foreach (c, tr->coremask) {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283 if (clib_bitmap_get(avail_cpu, c) == 0)
284 return clib_error_return (0, "cpu %u is not available to be used"
285 " for the '%s' thread",c, tr->name);
286
287 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100288 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400289 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400291 {
292 for (j = 0; j < tr->count; j++)
293 {
Vladimir Isaev2ed42042020-03-17 12:56:31 +0300294 /* Do not use CPU 0 by default - leave it to the host and IRQs */
295 uword avail_c0 = clib_bitmap_get (avail_cpu, 0);
296 avail_cpu = clib_bitmap_set (avail_cpu, 0, 0);
297
Dave Barach9b8ffd92016-07-08 08:13:45 -0400298 uword c = clib_bitmap_first_set (avail_cpu);
Vladimir Isaev2ed42042020-03-17 12:56:31 +0300299 /* Use CPU 0 as a last resort */
300 if (c == ~0 && avail_c0)
301 {
302 c = 0;
303 avail_c0 = 0;
304 }
305
Dave Barach9b8ffd92016-07-08 08:13:45 -0400306 if (c == ~0)
307 return clib_error_return (0,
308 "no available cpus to be used for"
hsandid832342e2023-12-20 15:41:54 +0100309 " the '%s' thread #%u",
310 tr->name, tr->count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
Vladimir Isaev2ed42042020-03-17 12:56:31 +0300312 avail_cpu = clib_bitmap_set (avail_cpu, 0, avail_c0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400313 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
314 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
315 }
316 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317 }
318
Dave Barach9b8ffd92016-07-08 08:13:45 -0400319 clib_bitmap_free (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
321 tm->n_vlib_mains = n_vlib_mains;
Damjan Marion8973b072022-03-01 15:51:18 +0100322 vlib_stats_set_gauge (stats_num_worker_threads_dir_index, n_vlib_mains - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323
Dave Barach2180bac2019-05-10 15:25:10 -0400324 /*
325 * Allocate the remaining worker threads, and thread stack vector slots
326 * from now on, calls to os_get_nthreads() will return the correct
327 * answer.
328 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400329 vec_validate_aligned (vlib_worker_threads, first_index - 1,
330 CLIB_CACHE_LINE_BYTES);
Dave Barach2180bac2019-05-10 15:25:10 -0400331 vec_validate (vlib_thread_stacks, vec_len (vlib_worker_threads) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 return 0;
333}
334
Dave Barach9b8ffd92016-07-08 08:13:45 -0400335vlib_frame_queue_t *
336vlib_frame_queue_alloc (int nelts)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400338 vlib_frame_queue_t *fq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339
Dave Barach9b8ffd92016-07-08 08:13:45 -0400340 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400341 clib_memset (fq, 0, sizeof (*fq));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 fq->nelts = nelts;
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200343 fq->vector_threshold = 2 * VLIB_FRAME_SIZE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400344 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200346 if (nelts & (nelts - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347 {
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200348 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
349 abort ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400350 }
351
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 return (fq);
353}
354
355void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356void
357vl_msg_api_handler_no_free (void *v)
358{
359}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361/* To be called by vlib worker threads upon startup */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400362void
363vlib_worker_thread_init (vlib_worker_thread_t * w)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400365 vlib_thread_main_t *tm = vlib_get_thread_main ();
366
Dave Barachfdf49442016-12-20 12:48:14 -0500367 /*
368 * Note: disabling signals in worker threads as follows
369 * prevents the api post-mortem dump scheme from working
370 * {
371 * sigset_t s;
372 * sigfillset (&s);
373 * pthread_sigmask (SIG_SETMASK, &s, 0);
374 * }
375 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376
377 clib_mem_set_heap (w->thread_mheap);
378
Dave Barach9b8ffd92016-07-08 08:13:45 -0400379 if (vec_len (tm->thread_prefix) && w->registration->short_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400381 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
382 w->registration->short_name, w->instance_id, '\0');
383 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 }
385
386 if (!w->registration->use_pthreads)
387 {
388
389 /* Initial barrier sync, for both worker and i/o threads */
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000390 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
392 while (*vlib_worker_threads->wait_at_barrier)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000395 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396 }
397}
398
Dave Barach9b8ffd92016-07-08 08:13:45 -0400399void *
400vlib_worker_thread_bootstrap_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 vlib_worker_thread_t *w = arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400403
404 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200405 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
Damjan Marionf55f9b82017-05-10 21:06:28 +0200407 __os_thread_index = w - vlib_worker_threads;
Damjan Marion586afd72017-04-05 19:18:20 +0200408
Damjan Marion7bf23172022-03-28 01:47:33 +0200409 if (CLIB_DEBUG > 0)
410 {
411 void *frame_addr = __builtin_frame_address (0);
412 if (frame_addr < (void *) w->thread_stack ||
413 frame_addr > (void *) w->thread_stack + VLIB_THREAD_STACK_SIZE)
414 {
415 /* heap is not set yet */
416 fprintf (stderr, "thread stack is not set properly\n");
417 exit (1);
418 }
419 }
Dave Barach37579c32021-07-27 09:27:07 -0400420
Damjan Marion7bf23172022-03-28 01:47:33 +0200421 w->thread_function (arg);
422
423 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424}
425
Dave Baracha690fdb2020-01-21 12:34:55 -0500426void
427vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id)
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200428{
Lijian.Zhang4fbb9da2020-02-14 15:16:49 +0800429 clib_bitmap_t *nbmp = 0, *cbmp = 0;
Damjan Marion3eb6cbe2024-02-12 19:44:58 +0000430 int node, core_id = -1, numa_id = -1;
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200431
Damjan Marion3eb6cbe2024-02-12 19:44:58 +0000432 core_id = os_get_cpu_phys_core_id (cpu_id);
Damjan Marion4b661402024-02-29 16:14:27 +0100433 nbmp = os_get_online_cpu_node_bitmap ();
Damjan Marion3eb6cbe2024-02-12 19:44:58 +0000434
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100435 clib_bitmap_foreach (node, nbmp) {
Damjan Marion3eb6cbe2024-02-12 19:44:58 +0000436 cbmp = os_get_cpu_on_node_bitmap (node);
437 if (clib_bitmap_get (cbmp, cpu_id))
438 numa_id = node;
439 vec_reset_length (cbmp);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100440 }
Damjan Marion4b661402024-02-29 16:14:27 +0100441
Lijian.Zhang4fbb9da2020-02-14 15:16:49 +0800442 vec_free (nbmp);
443 vec_free (cbmp);
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200444
445 w->core_id = core_id;
Dave Baracha690fdb2020-01-21 12:34:55 -0500446 w->numa_id = numa_id;
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200447}
448
Damjan Marion878c6092017-01-04 13:19:27 +0100449static clib_error_t *
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200450vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200452 clib_mem_main_t *mm = &clib_mem_main;
Damjan Marion878c6092017-01-04 13:19:27 +0100453 vlib_thread_main_t *tm = &vlib_thread_main;
Damjan Mariona2b7a022021-12-28 20:32:20 +0100454 pthread_t worker;
Damjan Marion7bf23172022-03-28 01:47:33 +0200455 pthread_attr_t attr;
Damjan Mariona2b7a022021-12-28 20:32:20 +0100456 cpu_set_t cpuset;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400457 void *(*fp_arg) (void *) = fp;
Dave Baracha690fdb2020-01-21 12:34:55 -0500458 void *numa_heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200460 w->cpu_id = cpu_id;
Dave Baracha690fdb2020-01-21 12:34:55 -0500461 vlib_get_thread_core_numa (w, cpu_id);
Dave Baracha690fdb2020-01-21 12:34:55 -0500462
463 /* Set up NUMA-bound heap if indicated */
Damjan Marion57d1ec02020-09-16 21:15:44 +0200464 if (mm->per_numa_mheaps[w->numa_id] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -0500465 {
466 /* If the user requested a NUMA heap, create it... */
467 if (tm->numa_heap_size)
468 {
Damjan Marion2bc1af52020-10-02 15:01:12 +0200469 clib_mem_set_numa_affinity (w->numa_id, 1 /* force */ );
470 numa_heap = clib_mem_create_heap (0 /* DIY */ , tm->numa_heap_size,
471 1 /* is_locked */ ,
472 "numa %u heap", w->numa_id);
473 clib_mem_set_default_numa_affinity ();
Damjan Marion57d1ec02020-09-16 21:15:44 +0200474 mm->per_numa_mheaps[w->numa_id] = numa_heap;
Dave Baracha690fdb2020-01-21 12:34:55 -0500475 }
476 else
477 {
478 /* Or, use the main heap */
Damjan Marion57d1ec02020-09-16 21:15:44 +0200479 mm->per_numa_mheaps[w->numa_id] = w->thread_mheap;
Dave Baracha690fdb2020-01-21 12:34:55 -0500480 }
481 }
482
Dave Barach9b8ffd92016-07-08 08:13:45 -0400483 CPU_ZERO (&cpuset);
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200484 CPU_SET (cpu_id, &cpuset);
Christophe Fontainefef15b42016-04-09 12:38:49 +0900485
Damjan Marion7bf23172022-03-28 01:47:33 +0200486 if (pthread_attr_init (&attr))
487 return clib_error_return_unix (0, "pthread_attr_init");
488
489 if (pthread_attr_setstack (&attr, w->thread_stack,
490 VLIB_THREAD_STACK_SIZE))
491 return clib_error_return_unix (0, "pthread_attr_setstack");
492
493 if (pthread_create (&worker, &attr, fp_arg, (void *) w))
Damjan Marion878c6092017-01-04 13:19:27 +0100494 return clib_error_return_unix (0, "pthread_create");
495
496 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
497 return clib_error_return_unix (0, "pthread_setaffinity_np");
498
Damjan Marion7bf23172022-03-28 01:47:33 +0200499 if (pthread_attr_destroy (&attr))
500 return clib_error_return_unix (0, "pthread_attr_destroy");
501
Damjan Marion878c6092017-01-04 13:19:27 +0100502 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503}
504
Dave Barach9b8ffd92016-07-08 08:13:45 -0400505static clib_error_t *
506start_workers (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100508 vlib_global_main_t *vgm = vlib_get_global_main ();
Damjan Marion66c85832022-03-14 13:04:38 +0100509 vlib_main_t *fvm = vlib_get_first_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 int i, j;
511 vlib_worker_thread_t *w;
512 vlib_main_t *vm_clone;
513 void *oldheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400514 vlib_thread_main_t *tm = &vlib_thread_main;
515 vlib_thread_registration_t *tr;
516 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 u32 n_vlib_mains = tm->n_vlib_mains;
518 u32 worker_thread_index;
Damjan Marion66c85832022-03-14 13:04:38 +0100519 u32 stats_err_entry_index = fvm->error_main.stats_err_entry_index;
Damjan Marionbfa75d62020-10-06 17:46:06 +0200520 clib_mem_heap_t *main_heap = clib_mem_get_per_cpu_heap ();
Ole Troana606d922021-05-05 09:23:17 +0200521 vlib_stats_register_mem_heap (main_heap);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400522
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 vec_reset_length (vlib_worker_threads);
524
525 /* Set up the main thread */
526 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
Dave Barach3e7deb12016-02-05 16:29:53 -0500527 w->elog_track.name = "main thread";
Damjan Marionf553a2c2021-03-26 13:45:37 +0100528 elog_track_register (vlib_get_elog_main (), &w->elog_track);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530 if (vec_len (tm->thread_prefix))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400532 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
533 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 }
535
Damjan Marionfd8deb42021-03-06 12:26:28 +0100536 vgm->elog_main.lock =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400537 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100538 vgm->elog_main.lock[0] = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400539
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000540 clib_callback_data_init (&vm->vlib_node_runtime_perf_callbacks,
541 &vm->worker_thread_main_loop_callback_lock);
542
Damjan Marionfd8deb42021-03-06 12:26:28 +0100543 vec_validate_aligned (vgm->vlib_mains, n_vlib_mains - 1,
544 CLIB_CACHE_LINE_BYTES);
Damjan Marion8bea5892022-04-04 22:40:45 +0200545 vec_set_len (vgm->vlib_mains, 0);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100546 vec_add1_aligned (vgm->vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
Dave Barach1c556d12020-10-02 11:35:44 -0400547
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 if (n_vlib_mains > 1)
549 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400550 vlib_worker_threads->wait_at_barrier =
551 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552 vlib_worker_threads->workers_at_barrier =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400553 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100555 vlib_worker_threads->node_reforks_required =
556 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
557
Dave Barachf6c68d72018-11-01 08:12:52 -0400558 /* We'll need the rpc vector lock... */
559 clib_spinlock_init (&vm->pending_rpc_lock);
560
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 /* Ask for an initial barrier sync */
562 *vlib_worker_threads->workers_at_barrier = 0;
563 *vlib_worker_threads->wait_at_barrier = 1;
564
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100565 /* Without update or refork */
566 *vlib_worker_threads->node_reforks_required = 0;
Damjan Marionfd8deb42021-03-06 12:26:28 +0100567 vgm->need_vlib_worker_thread_node_runtime_update = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100568
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100569 /* init timing */
570 vm->barrier_epoch = 0;
571 vm->barrier_no_close_before = 0;
572
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 worker_thread_index = 1;
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000574 clib_spinlock_init (&vm->worker_thread_main_loop_callback_lock);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
Dave Barach9b8ffd92016-07-08 08:13:45 -0400576 for (i = 0; i < vec_len (tm->registrations); i++)
577 {
578 vlib_node_main_t *nm, *nm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400579 int k;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Dave Barach9b8ffd92016-07-08 08:13:45 -0400581 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582
Dave Barach9b8ffd92016-07-08 08:13:45 -0400583 if (tr->count == 0)
584 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Dave Barach9b8ffd92016-07-08 08:13:45 -0400586 for (k = 0; k < tr->count; k++)
587 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100588 vlib_node_t *n;
Damjan Marion66c85832022-03-14 13:04:38 +0100589 u64 **c;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100590
Dave Barach9b8ffd92016-07-08 08:13:45 -0400591 vec_add2 (vlib_worker_threads, w, 1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400592 /* Currently unused, may not really work */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400593 if (tr->mheap_size)
Damjan Marion4537c302020-09-28 19:03:37 +0200594 w->thread_mheap = clib_mem_create_heap (0, tr->mheap_size,
595 /* unlocked */ 0,
596 "%s%d heap",
597 tr->name, k);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400598 else
599 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200600
601 w->thread_stack =
602 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400603 w->thread_function = tr->function;
604 w->thread_function_arg = w;
605 w->instance_id = k;
606 w->registration = tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
Dave Barach9b8ffd92016-07-08 08:13:45 -0400608 w->elog_track.name =
609 (char *) format (0, "%s %d", tr->name, k + 1);
610 vec_add1 (w->elog_track.name, 0);
Damjan Marionf553a2c2021-03-26 13:45:37 +0100611 elog_track_register (vlib_get_elog_main (), &w->elog_track);
Bud Grise68adab92016-02-12 10:36:11 -0500612
Dave Barach9b8ffd92016-07-08 08:13:45 -0400613 if (tr->no_data_structure_clone)
614 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615
Dave Barach9b8ffd92016-07-08 08:13:45 -0400616 /* Fork vlib_global_main et al. Look for bugs here */
617 oldheap = clib_mem_set_heap (w->thread_mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200619 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
620 CLIB_CACHE_LINE_BYTES);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100621 clib_memcpy (vm_clone, vlib_get_first_main (),
622 sizeof (*vm_clone));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
Damjan Marion586afd72017-04-05 19:18:20 +0200624 vm_clone->thread_index = worker_thread_index;
Dave Barach2877eee2017-12-15 12:22:57 -0500625 vm_clone->pending_rpc_requests = 0;
626 vec_validate (vm_clone->pending_rpc_requests, 0);
Damjan Marion8bea5892022-04-04 22:40:45 +0200627 vec_set_len (vm_clone->pending_rpc_requests, 0);
Dave Barachb7b92992018-10-17 10:38:51 -0400628 clib_memset (&vm_clone->random_buffer, 0,
629 sizeof (vm_clone->random_buffer));
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000630 clib_spinlock_init
631 (&vm_clone->worker_thread_main_loop_callback_lock);
632 clib_callback_data_init
633 (&vm_clone->vlib_node_runtime_perf_callbacks,
634 &vm_clone->worker_thread_main_loop_callback_lock);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
Damjan Marionfd8deb42021-03-06 12:26:28 +0100636 nm = &vlib_get_first_main ()->node_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400637 nm_clone = &vm_clone->node_main;
638 /* fork next frames array, preserving node runtime indices */
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200639 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
640 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400641 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
642 {
643 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
644 u32 save_node_runtime_index;
645 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
Dave Barach9b8ffd92016-07-08 08:13:45 -0400647 save_node_runtime_index = nf->node_runtime_index;
648 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
649 vlib_next_frame_init (nf);
650 nf->node_runtime_index = save_node_runtime_index;
651 nf->flags = save_flags;
652 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
Dave Barach9b8ffd92016-07-08 08:13:45 -0400654 /* fork the frame dispatch queue */
655 nm_clone->pending_frames = 0;
Dave Barach53fe4a72019-01-26 09:50:26 -0500656 vec_validate (nm_clone->pending_frames, 10);
Damjan Marion8bea5892022-04-04 22:40:45 +0200657 vec_set_len (nm_clone->pending_frames, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
Dave Barach9b8ffd92016-07-08 08:13:45 -0400659 /* fork nodes */
660 nm_clone->nodes = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100661
662 /* Allocate all nodes in single block for speed */
663 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
664
Dave Barach9b8ffd92016-07-08 08:13:45 -0400665 for (j = 0; j < vec_len (nm->nodes); j++)
666 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400667 clib_memcpy (n, nm->nodes[j], sizeof (*n));
668 /* none of the copied nodes have enqueue rights given out */
669 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
Dave Barachb7b92992018-10-17 10:38:51 -0400670 clib_memset (&n->stats_total, 0, sizeof (n->stats_total));
671 clib_memset (&n->stats_last_clear, 0,
672 sizeof (n->stats_last_clear));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400673 vec_add1 (nm_clone->nodes, n);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100674 n++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400675 }
676 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200677 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
678 CLIB_CACHE_LINE_BYTES);
JingLiuZTE30af5da2017-07-24 10:53:31 +0800679 vec_foreach (rt,
680 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
Damjan Marione9f929b2017-03-16 11:32:09 +0100681 {
682 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marione9f929b2017-03-16 11:32:09 +0100683 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100684 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100685 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100686 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
687 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100688 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689
Dave Barach9b8ffd92016-07-08 08:13:45 -0400690 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200691 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
692 CLIB_CACHE_LINE_BYTES);
Damjan Marion94100532020-11-06 23:25:57 +0100693 clib_interrupt_init (
Damjan Marioncc8249c2023-07-23 14:24:22 +0200694 &nm_clone->input_node_interrupts,
Damjan Marion94100532020-11-06 23:25:57 +0100695 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]));
Damjan Marioncc8249c2023-07-23 14:24:22 +0200696 clib_interrupt_init (
697 &nm_clone->pre_input_node_interrupts,
698 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT]));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400699 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
Damjan Marione9f929b2017-03-16 11:32:09 +0100700 {
701 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marione9f929b2017-03-16 11:32:09 +0100702 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100703 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100704 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100705 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
706 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100707 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708
Dave Barach53fe4a72019-01-26 09:50:26 -0500709 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
710 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
711 CLIB_CACHE_LINE_BYTES);
712 vec_foreach (rt,
713 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
714 {
715 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Dave Barach53fe4a72019-01-26 09:50:26 -0500716 /* copy initial runtime_data from node */
717 if (n->runtime_data && n->runtime_data_bytes > 0)
718 clib_memcpy (rt->runtime_data, n->runtime_data,
719 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
720 n->runtime_data_bytes));
721 }
722
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200723 nm_clone->processes = vec_dup_aligned (nm->processes,
724 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725
Dave Barach593eedf2019-03-10 09:44:51 -0400726 /* Create per-thread frame freelist */
Damjan Marionb32bd702021-12-23 17:05:02 +0100727 nm_clone->frame_sizes = 0;
Dave Barach687c9022019-07-23 10:22:31 -0400728 nm_clone->node_by_error = nm->node_by_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729
Dave Barach9b8ffd92016-07-08 08:13:45 -0400730 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100731
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732 clib_mem_set_heap (oldheap);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100733 vec_add1_aligned (vgm->vlib_mains, vm_clone,
734 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735
Ole Troan233e4682019-05-16 15:01:34 +0200736 /* Switch to the stats segment ... */
Damjan Marion66c85832022-03-14 13:04:38 +0100737 vlib_stats_validate (stats_err_entry_index, worker_thread_index,
738 vec_len (fvm->error_main.counters) - 1);
739 c = vlib_stats_get_entry_data_pointer (stats_err_entry_index);
740 vm_clone->error_main.counters = c[worker_thread_index];
Ole Troan233e4682019-05-16 15:01:34 +0200741
Damjan Marionfd8deb42021-03-06 12:26:28 +0100742 vm_clone->error_main.counters_last_clear = vec_dup_aligned (
743 vlib_get_first_main ()->error_main.counters_last_clear,
744 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745
Dave Barach9b8ffd92016-07-08 08:13:45 -0400746 worker_thread_index++;
747 }
748 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 }
750 else
751 {
752 /* only have non-data-structure copy threads to create... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400753 for (i = 0; i < vec_len (tm->registrations); i++)
754 {
755 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756
Dave Barach9b8ffd92016-07-08 08:13:45 -0400757 for (j = 0; j < tr->count; j++)
758 {
759 vec_add2 (vlib_worker_threads, w, 1);
760 if (tr->mheap_size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400761 {
Damjan Marion4537c302020-09-28 19:03:37 +0200762 w->thread_mheap = clib_mem_create_heap (0, tr->mheap_size,
763 /* locked */ 0,
764 "%s%d heap",
765 tr->name, j);
Dave Barach6a5adc32018-07-04 10:56:23 -0400766 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400767 else
768 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200769 w->thread_stack =
770 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400771 w->thread_function = tr->function;
772 w->thread_function_arg = w;
773 w->instance_id = j;
774 w->elog_track.name =
775 (char *) format (0, "%s %d", tr->name, j + 1);
776 w->registration = tr;
777 vec_add1 (w->elog_track.name, 0);
Damjan Marionf553a2c2021-03-26 13:45:37 +0100778 elog_track_register (vlib_get_elog_main (), &w->elog_track);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400779 }
780 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 }
782
783 worker_thread_index = 1;
784
785 for (i = 0; i < vec_len (tm->registrations); i++)
786 {
Damjan Marion878c6092017-01-04 13:19:27 +0100787 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788 int j;
789
790 tr = tm->registrations[i];
791
792 if (tr->use_pthreads || tm->use_pthreads)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400793 {
794 for (j = 0; j < tr->count; j++)
795 {
hsandid832342e2023-12-20 15:41:54 +0100796
Dave Barach9b8ffd92016-07-08 08:13:45 -0400797 w = vlib_worker_threads + worker_thread_index++;
Damjan Marion878c6092017-01-04 13:19:27 +0100798 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
799 w, 0);
800 if (err)
hsandid832342e2023-12-20 15:41:54 +0100801 clib_unix_error ("%U, thread %s init on cpu %d failed",
802 format_clib_error, err, tr->name, 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400803 }
804 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400806 {
807 uword c;
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100808 clib_bitmap_foreach (c, tr->coremask) {
Damjan Marion878c6092017-01-04 13:19:27 +0100809 w = vlib_worker_threads + worker_thread_index++;
810 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
811 w, c);
812 if (err)
hsandid832342e2023-12-20 15:41:54 +0100813 clib_unix_error ("%U, thread %s init on cpu %d failed",
814 format_clib_error, err, tr->name, c);
815 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400816 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400818 vlib_worker_thread_barrier_sync (vm);
Damjan Marion321bd102022-06-01 00:45:18 +0200819 {
820 clib_error_t *err;
821 err = vlib_call_init_exit_functions (
822 vm, &vgm->num_workers_change_function_registrations, 1 /* call_once */,
823 1 /* is_global */);
824 if (err)
825 clib_error_report (err);
826 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400827 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 return 0;
829}
830
831VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
832
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100833
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100834static inline void
835worker_thread_node_runtime_update_internal (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836{
837 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838 vlib_main_t *vm;
839 vlib_node_main_t *nm, *nm_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840 vlib_main_t *vm_clone;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100841 vlib_node_runtime_t *rt;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400842
Damjan Marion586afd72017-04-05 19:18:20 +0200843 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100845 vm = vlib_get_first_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846 nm = &vm->node_main;
847
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
849
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851 * Scrape all runtime stats, so we don't lose node runtime(s) with
852 * pending counts, or throw away worker / io thread counts.
853 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400854 for (j = 0; j < vec_len (nm->nodes); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400856 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857 n = nm->nodes[j];
858 vlib_node_sync_stats (vm, n);
859 }
860
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100861 for (i = 1; i < vlib_get_n_threads (); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400863 vlib_node_t *n;
864
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100865 vm_clone = vlib_get_main_by_index (i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700866 nm_clone = &vm_clone->node_main;
867
Dave Barach9b8ffd92016-07-08 08:13:45 -0400868 for (j = 0; j < vec_len (nm_clone->nodes); j++)
869 {
870 n = nm_clone->nodes[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700871
Dave Barach9b8ffd92016-07-08 08:13:45 -0400872 rt = vlib_node_get_runtime (vm_clone, n->index);
873 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
874 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875 }
876
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100877 /* Per-worker clone rebuilds are now done on each thread */
878}
879
880
881void
882vlib_worker_thread_node_refork (void)
883{
884 vlib_main_t *vm, *vm_clone;
885 vlib_node_main_t *nm, *nm_clone;
886 vlib_node_t **old_nodes_clone;
887 vlib_node_runtime_t *rt, *old_rt;
Damjan Marion66c85832022-03-14 13:04:38 +0100888 u64 **c;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100889
890 vlib_node_t *new_n_clone;
891
892 int j;
893
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100894 vm = vlib_get_first_main ();
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100895 nm = &vm->node_main;
896 vm_clone = vlib_get_main ();
897 nm_clone = &vm_clone->node_main;
898
899 /* Re-clone error heap */
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100900 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
901
Dave Barach178cf492018-11-13 16:34:13 -0500902 clib_memcpy_fast (&vm_clone->error_main, &vm->error_main,
903 sizeof (vm->error_main));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100904 j = vec_len (vm->error_main.counters) - 1;
Ole Troan233e4682019-05-16 15:01:34 +0200905
Damjan Marion66c85832022-03-14 13:04:38 +0100906 c = vlib_stats_get_entry_data_pointer (vm->error_main.stats_err_entry_index);
907 vm_clone->error_main.counters = c[vm_clone->thread_index];
Ole Troan233e4682019-05-16 15:01:34 +0200908
909 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100910 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
911
Dmitry Valter9f5b3692022-09-05 15:30:18 +0000912 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
913 {
914 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
915 if ((nf->flags & VLIB_FRAME_IS_ALLOCATED) && nf->frame != NULL)
916 {
917 vlib_frame_t *f = nf->frame;
918 nf->frame = NULL;
919 vlib_frame_free (vm_clone, f);
920 }
921 }
922
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100923 vec_free (nm_clone->next_frames);
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200924 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
925 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100926
927 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100929 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
930 u32 save_node_runtime_index;
931 u32 save_flags;
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100932
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100933 save_node_runtime_index = nf->node_runtime_index;
934 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
935 vlib_next_frame_init (nf);
936 nf->node_runtime_index = save_node_runtime_index;
937 nf->flags = save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100939
940 old_nodes_clone = nm_clone->nodes;
941 nm_clone->nodes = 0;
942
943 /* re-fork nodes */
944
945 /* Allocate all nodes in single block for speed */
946 new_n_clone =
947 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
948 for (j = 0; j < vec_len (nm->nodes); j++)
949 {
Benoît Ganne5517bd32019-08-30 16:20:12 +0200950 vlib_node_t *new_n = nm->nodes[j];
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100951
Dave Barach178cf492018-11-13 16:34:13 -0500952 clib_memcpy_fast (new_n_clone, new_n, sizeof (*new_n));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100953 /* none of the copied nodes have enqueue rights given out */
954 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
955
956 if (j >= vec_len (old_nodes_clone))
957 {
958 /* new node, set to zero */
Dave Barachb7b92992018-10-17 10:38:51 -0400959 clib_memset (&new_n_clone->stats_total, 0,
960 sizeof (new_n_clone->stats_total));
961 clib_memset (&new_n_clone->stats_last_clear, 0,
962 sizeof (new_n_clone->stats_last_clear));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100963 }
964 else
965 {
Benoît Ganne5517bd32019-08-30 16:20:12 +0200966 vlib_node_t *old_n_clone = old_nodes_clone[j];
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100967 /* Copy stats if the old data is valid */
Dave Barach178cf492018-11-13 16:34:13 -0500968 clib_memcpy_fast (&new_n_clone->stats_total,
969 &old_n_clone->stats_total,
970 sizeof (new_n_clone->stats_total));
971 clib_memcpy_fast (&new_n_clone->stats_last_clear,
972 &old_n_clone->stats_last_clear,
973 sizeof (new_n_clone->stats_last_clear));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100974
975 /* keep previous node state */
976 new_n_clone->state = old_n_clone->state;
Maxime Peimdde163d2021-04-15 13:52:12 +0200977 new_n_clone->flags = old_n_clone->flags;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100978 }
979 vec_add1 (nm_clone->nodes, new_n_clone);
980 new_n_clone++;
981 }
982 /* Free the old node clones */
983 clib_mem_free (old_nodes_clone[0]);
984
985 vec_free (old_nodes_clone);
986
987
988 /* re-clone internal nodes */
989 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
990 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200991 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
992 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100993
994 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
995 {
996 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100997 /* copy runtime_data, will be overwritten later for existing rt */
998 if (n->runtime_data && n->runtime_data_bytes > 0)
Dave Barach178cf492018-11-13 16:34:13 -0500999 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1000 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1001 n->runtime_data_bytes));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001002 }
1003
1004 for (j = 0; j < vec_len (old_rt); j++)
1005 {
1006 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1007 rt->state = old_rt[j].state;
Maxime Peimdde163d2021-04-15 13:52:12 +02001008 rt->flags = old_rt[j].flags;
Dave Barach178cf492018-11-13 16:34:13 -05001009 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1010 VLIB_NODE_RUNTIME_DATA_SIZE);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001011 }
1012
1013 vec_free (old_rt);
1014
1015 /* re-clone input nodes */
1016 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1017 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001018 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1019 CLIB_CACHE_LINE_BYTES);
Damjan Marion94100532020-11-06 23:25:57 +01001020 clib_interrupt_resize (
Damjan Marioncc8249c2023-07-23 14:24:22 +02001021 &nm_clone->input_node_interrupts,
Damjan Marion94100532020-11-06 23:25:57 +01001022 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]));
Damjan Marioncc8249c2023-07-23 14:24:22 +02001023 clib_interrupt_resize (
1024 &nm_clone->pre_input_node_interrupts,
1025 vec_len (nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT]));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001026
1027 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1028 {
1029 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001030 /* copy runtime_data, will be overwritten later for existing rt */
1031 if (n->runtime_data && n->runtime_data_bytes > 0)
Dave Barach178cf492018-11-13 16:34:13 -05001032 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1033 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1034 n->runtime_data_bytes));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001035 }
1036
1037 for (j = 0; j < vec_len (old_rt); j++)
1038 {
1039 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1040 rt->state = old_rt[j].state;
Maxime Peimdde163d2021-04-15 13:52:12 +02001041 rt->flags = old_rt[j].flags;
Dave Barach178cf492018-11-13 16:34:13 -05001042 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1043 VLIB_NODE_RUNTIME_DATA_SIZE);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001044 }
1045
1046 vec_free (old_rt);
1047
Dave Barach53fe4a72019-01-26 09:50:26 -05001048 /* re-clone pre-input nodes */
1049 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT];
1050 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
1051 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
1052 CLIB_CACHE_LINE_BYTES);
1053
1054 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1055 {
1056 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Dave Barach53fe4a72019-01-26 09:50:26 -05001057 /* copy runtime_data, will be overwritten later for existing rt */
1058 if (n->runtime_data && n->runtime_data_bytes > 0)
1059 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1060 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1061 n->runtime_data_bytes));
1062 }
1063
1064 for (j = 0; j < vec_len (old_rt); j++)
1065 {
1066 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1067 rt->state = old_rt[j].state;
Maxime Peimdde163d2021-04-15 13:52:12 +02001068 rt->flags = old_rt[j].flags;
Dave Barach53fe4a72019-01-26 09:50:26 -05001069 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1070 VLIB_NODE_RUNTIME_DATA_SIZE);
1071 }
1072
1073 vec_free (old_rt);
1074
Vladislav Grishenko29733502021-09-25 21:00:59 +05001075 vec_free (nm_clone->processes);
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001076 nm_clone->processes = vec_dup_aligned (nm->processes,
1077 CLIB_CACHE_LINE_BYTES);
Dave Barach687c9022019-07-23 10:22:31 -04001078 nm_clone->node_by_error = nm->node_by_error;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001079}
1080
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001081void
1082vlib_worker_thread_node_runtime_update (void)
1083{
1084 /*
1085 * Make a note that we need to do a node runtime update
1086 * prior to releasing the barrier.
1087 */
1088 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089}
1090
Pavel Kotucek1e765832016-09-23 08:54:14 +02001091u32
1092unformat_sched_policy (unformat_input_t * input, va_list * args)
1093{
1094 u32 *r = va_arg (*args, u32 *);
1095
1096 if (0);
1097#define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1098 foreach_sched_policy
1099#undef _
1100 else
1101 return 0;
1102 return 1;
1103}
1104
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105static clib_error_t *
1106cpu_config (vlib_main_t * vm, unformat_input_t * input)
1107{
1108 vlib_thread_registration_t *tr;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001109 uword *p;
1110 vlib_thread_main_t *tm = &vlib_thread_main;
1111 u8 *name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001112 uword *bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113 u32 count;
1114
1115 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
Pavel Kotucek1e765832016-09-23 08:54:14 +02001116
Dave Barach9b8ffd92016-07-08 08:13:45 -04001117 tm->n_thread_stacks = 1; /* account for main thread */
Pavel Kotucek1e765832016-09-23 08:54:14 +02001118 tm->sched_policy = ~0;
1119 tm->sched_priority = ~0;
Damjan Marion858151f2018-07-11 10:51:00 +02001120 tm->main_lcore = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121
1122 tr = tm->next;
1123
1124 while (tr)
1125 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001126 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127 tr = tr->next;
1128 }
1129
Dave Barach9b8ffd92016-07-08 08:13:45 -04001130 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131 {
Damjan Marionbf741472016-06-13 22:49:44 +02001132 if (unformat (input, "use-pthreads"))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001133 tm->use_pthreads = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001135 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001136 else if (unformat (input, "main-core %u", &tm->main_lcore))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001137 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001139 ;
Dave Baracha690fdb2020-01-21 12:34:55 -05001140 else if (unformat (input, "numa-heap-size %U",
1141 unformat_memory_size, &tm->numa_heap_size))
1142 ;
Yi Hee4a9eb72018-07-17 14:18:41 +08001143 else if (unformat (input, "coremask-%s %U", &name,
1144 unformat_bitmap_mask, &bitmap) ||
1145 unformat (input, "corelist-%s %U", &name,
1146 unformat_bitmap_list, &bitmap))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001147 {
1148 p = hash_get_mem (tm->thread_registrations_by_name, name);
1149 if (p == 0)
1150 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151
Dave Barach9b8ffd92016-07-08 08:13:45 -04001152 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153
Dave Barach9b8ffd92016-07-08 08:13:45 -04001154 if (tr->use_pthreads)
1155 return clib_error_return (0,
1156 "corelist cannot be set for '%s' threads",
1157 name);
Vladimir Isaev18a4a372020-03-17 12:30:11 +03001158 if (tr->count)
1159 return clib_error_return
1160 (0, "core placement of '%s' threads is already configured",
1161 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162
Dave Barach9b8ffd92016-07-08 08:13:45 -04001163 tr->coremask = bitmap;
1164 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1165 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001166 else
1167 if (unformat
1168 (input, "scheduler-policy %U", unformat_sched_policy,
1169 &tm->sched_policy))
1170 ;
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001171 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
Pavel Kotucek1e765832016-09-23 08:54:14 +02001172 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173 else if (unformat (input, "%s %u", &name, &count))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001174 {
1175 p = hash_get_mem (tm->thread_registrations_by_name, name);
1176 if (p == 0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001177 return clib_error_return (0, "no such thread type 3 '%s'", name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001178
1179 tr = (vlib_thread_registration_t *) p[0];
Vladimir Isaev18a4a372020-03-17 12:30:11 +03001180
Dave Barach9b8ffd92016-07-08 08:13:45 -04001181 if (tr->fixed_count)
1182 return clib_error_return
Vladimir Isaev18a4a372020-03-17 12:30:11 +03001183 (0, "number of '%s' threads not configurable", name);
1184 if (tr->count)
1185 return clib_error_return
1186 (0, "number of '%s' threads is already configured", name);
1187
Dave Barach9b8ffd92016-07-08 08:13:45 -04001188 tr->count = count;
1189 }
1190 else
1191 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192 }
1193
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001194 if (tm->sched_priority != ~0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001195 {
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001196 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001197 {
1198 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1199 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1200 if (tm->sched_priority > prio_max)
1201 tm->sched_priority = prio_max;
1202 if (tm->sched_priority < prio_min)
1203 tm->sched_priority = prio_min;
1204 }
1205 else
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001206 {
1207 return clib_error_return
1208 (0,
1209 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1210 tm->sched_priority);
1211 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001212 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001213 tr = tm->next;
1214
1215 if (!tm->thread_prefix)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001216 tm->thread_prefix = format (0, "vpp");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217
1218 while (tr)
1219 {
1220 tm->n_thread_stacks += tr->count;
1221 tm->n_pthreads += tr->count * tr->use_pthreads;
Damjan Marion878c6092017-01-04 13:19:27 +01001222 tm->n_threads += tr->count * (tr->use_pthreads == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223 tr = tr->next;
1224 }
1225
1226 return 0;
1227}
1228
1229VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1230
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001231 /*
1232 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1233 * based on a test based heuristic that barrier should be open for at least
1234 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1235 * point it is probably too late to make a difference)
1236 */
1237
1238#ifndef BARRIER_MINIMUM_OPEN_LIMIT
1239#define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1240#endif
1241
1242#ifndef BARRIER_MINIMUM_OPEN_FACTOR
1243#define BARRIER_MINIMUM_OPEN_FACTOR 3
1244#endif
1245
Dave Barach9b8ffd92016-07-08 08:13:45 -04001246void
Dave Barachc602b382019-06-03 19:48:22 -04001247vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm)
1248{
1249 f64 deadline;
1250 f64 now = vlib_time_now (vm);
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001251 u32 count = vlib_get_n_threads () - 1;
Dave Barachc602b382019-06-03 19:48:22 -04001252
1253 /* No worker threads? */
1254 if (count == 0)
1255 return;
1256
1257 deadline = now + BARRIER_SYNC_TIMEOUT;
1258 *vlib_worker_threads->wait_at_barrier = 1;
1259 while (*vlib_worker_threads->workers_at_barrier != count)
1260 {
1261 if ((now = vlib_time_now (vm)) > deadline)
1262 {
1263 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1264 os_panic ();
1265 }
1266 CLIB_PAUSE ();
1267 }
1268 *vlib_worker_threads->wait_at_barrier = 0;
1269}
1270
Neale Ranns42845dd2020-05-26 13:12:17 +00001271/**
1272 * Return true if the wroker thread barrier is held
1273 */
1274u8
1275vlib_worker_thread_barrier_held (void)
1276{
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001277 if (vlib_get_n_threads () < 2)
Neale Ranns42845dd2020-05-26 13:12:17 +00001278 return (1);
1279
1280 return (*vlib_worker_threads->wait_at_barrier == 1);
1281}
1282
Dave Barachc602b382019-06-03 19:48:22 -04001283void
Damjan Marion8343ee52019-02-26 17:15:48 +01001284vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, const char *func_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001285{
1286 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001287 f64 now;
1288 f64 t_entry;
1289 f64 t_open;
1290 f64 t_closed;
Dave Barach9ae190e2019-04-23 10:07:24 -04001291 f64 max_vector_rate;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001292 u32 count;
Dave Barach9ae190e2019-04-23 10:07:24 -04001293 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001294
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001295 if (vlib_get_n_threads () < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001296 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001298 ASSERT (vlib_get_thread_index () == 0);
1299
Damjan Marion8343ee52019-02-26 17:15:48 +01001300 vlib_worker_threads[0].barrier_caller = func_name;
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001301 count = vlib_get_n_threads () - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001303 /* Record entry relative to last close */
1304 now = vlib_time_now (vm);
1305 t_entry = now - vm->barrier_epoch;
1306
Ed Warnickecb9cada2015-12-08 15:45:58 -07001307 /* Tolerate recursive calls */
1308 if (++vlib_worker_threads[0].recursion_level > 1)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001309 {
1310 barrier_trace_sync_rec (t_entry);
1311 return;
1312 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001313
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +00001314 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
1315 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
1316 vm->clib_time.last_cpu_time, 0 /* enter */ );
1317
Dave Barach9ae190e2019-04-23 10:07:24 -04001318 /*
1319 * Need data to decide if we're working hard enough to honor
1320 * the barrier hold-down timer.
1321 */
1322 max_vector_rate = 0.0;
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001323 for (i = 1; i < vlib_get_n_threads (); i++)
1324 {
1325 vlib_main_t *ovm = vlib_get_main_by_index (i);
1326 max_vector_rate = clib_max (max_vector_rate,
1327 (f64) vlib_last_vectors_per_main_loop (ovm));
1328 }
Dave Barach9ae190e2019-04-23 10:07:24 -04001329
Bud Grise42f20062016-03-16 13:09:46 -04001330 vlib_worker_threads[0].barrier_sync_count++;
1331
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001332 /* Enforce minimum barrier open time to minimize packet loss */
1333 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001334
Dave Barach9ae190e2019-04-23 10:07:24 -04001335 /*
1336 * If any worker thread seems busy, which we define
1337 * as a vector rate above 10, we enforce the barrier hold-down timer
1338 */
1339 if (max_vector_rate > 10.0)
Dave Barach36feebb2018-09-07 11:12:27 -04001340 {
Dave Barach9ae190e2019-04-23 10:07:24 -04001341 while (1)
Dave Barach36feebb2018-09-07 11:12:27 -04001342 {
Dave Barach9ae190e2019-04-23 10:07:24 -04001343 now = vlib_time_now (vm);
1344 /* Barrier hold-down timer expired? */
1345 if (now >= vm->barrier_no_close_before)
1346 break;
1347 if ((vm->barrier_no_close_before - now)
1348 > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1349 {
1350 clib_warning
1351 ("clock change: would have waited for %.4f seconds",
1352 (vm->barrier_no_close_before - now));
1353 break;
1354 }
Dave Barach36feebb2018-09-07 11:12:27 -04001355 }
1356 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001357 /* Record time of closure */
1358 t_open = now - vm->barrier_epoch;
1359 vm->barrier_epoch = now;
1360
1361 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001362
1363 *vlib_worker_threads->wait_at_barrier = 1;
1364 while (*vlib_worker_threads->workers_at_barrier != count)
1365 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001366 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001367 {
1368 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1369 os_panic ();
1370 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001372
1373 t_closed = now - vm->barrier_epoch;
1374
1375 barrier_trace_sync (t_entry, t_open, t_closed);
1376
Ed Warnickecb9cada2015-12-08 15:45:58 -07001377}
1378
Dave Barach9b8ffd92016-07-08 08:13:45 -04001379void
1380vlib_worker_thread_barrier_release (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001381{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001382 vlib_global_main_t *vgm = vlib_get_global_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001383 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001384 f64 now;
1385 f64 minimum_open;
1386 f64 t_entry;
1387 f64 t_closed_total;
1388 f64 t_update_main = 0.0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001389 int refork_needed = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001391 if (vlib_get_n_threads () < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001392 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001393
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001394 ASSERT (vlib_get_thread_index () == 0);
1395
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001396
1397 now = vlib_time_now (vm);
1398 t_entry = now - vm->barrier_epoch;
1399
Ed Warnickecb9cada2015-12-08 15:45:58 -07001400 if (--vlib_worker_threads[0].recursion_level > 0)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001401 {
1402 barrier_trace_release_rec (t_entry);
1403 return;
1404 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001405
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001406 /* Update (all) node runtimes before releasing the barrier, if needed */
Damjan Marionfd8deb42021-03-06 12:26:28 +01001407 if (vgm->need_vlib_worker_thread_node_runtime_update)
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001408 {
Dave Barach1ddbc012018-06-13 09:26:05 -04001409 /*
1410 * Lock stat segment here, so we's safe when
1411 * rebuilding the stat segment node clones from the
1412 * stat thread...
1413 */
Damjan Marion8973b072022-03-01 15:51:18 +01001414 vlib_stats_segment_lock ();
Dave Barach1ddbc012018-06-13 09:26:05 -04001415
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001416 /* Do stats elements on main thread */
1417 worker_thread_node_runtime_update_internal ();
Damjan Marionfd8deb42021-03-06 12:26:28 +01001418 vgm->need_vlib_worker_thread_node_runtime_update = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001419
1420 /* Do per thread rebuilds in parallel */
1421 refork_needed = 1;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +00001422 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001423 (vlib_get_n_threads () - 1));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001424 now = vlib_time_now (vm);
1425 t_update_main = now - vm->barrier_epoch;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001426 }
1427
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001428 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001429
Dave Baracha4324a92019-02-19 17:05:30 -05001430 /*
1431 * Note when we let go of the barrier.
1432 * Workers can use this to derive a reasonably accurate
1433 * time offset. See vlib_time_now(...)
1434 */
1435 vm->time_last_barrier_release = vlib_time_now (vm);
1436 CLIB_MEMORY_STORE_BARRIER ();
1437
Ed Warnickecb9cada2015-12-08 15:45:58 -07001438 *vlib_worker_threads->wait_at_barrier = 0;
1439
1440 while (*vlib_worker_threads->workers_at_barrier > 0)
1441 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001442 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001443 {
1444 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1445 os_panic ();
1446 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001448
1449 /* Wait for reforks before continuing */
1450 if (refork_needed)
1451 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001452 now = vlib_time_now (vm);
1453
1454 deadline = now + BARRIER_SYNC_TIMEOUT;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001455
1456 while (*vlib_worker_threads->node_reforks_required > 0)
1457 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001458 if ((now = vlib_time_now (vm)) > deadline)
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001459 {
1460 fformat (stderr, "%s: worker thread refork deadlock\n",
1461 __FUNCTION__);
1462 os_panic ();
1463 }
1464 }
Damjan Marion8973b072022-03-01 15:51:18 +01001465 vlib_stats_segment_unlock ();
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001466 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001467
1468 t_closed_total = now - vm->barrier_epoch;
1469
1470 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1471
1472 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1473 {
1474 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1475 }
1476
1477 vm->barrier_no_close_before = now + minimum_open;
1478
1479 /* Record barrier epoch (used to enforce minimum open time) */
1480 vm->barrier_epoch = now;
1481
1482 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1483
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +00001484 if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
1485 clib_call_callbacks (vm->barrier_perf_callbacks, vm,
1486 vm->clib_time.last_cpu_time, 1 /* leave */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07001487}
1488
Florin Coras4b208302022-03-30 13:50:19 -07001489static void
1490vlib_worker_sync_rpc (void *args)
1491{
1492 ASSERT (vlib_thread_is_main_w_barrier ());
1493 vlib_worker_threads->wait_before_barrier = 0;
1494}
1495
1496void
1497vlib_workers_sync (void)
1498{
1499 if (PREDICT_FALSE (!vlib_num_workers ()))
1500 return;
1501
1502 if (!(*vlib_worker_threads->wait_at_barrier) &&
1503 !clib_atomic_swap_rel_n (&vlib_worker_threads->wait_before_barrier, 1))
1504 {
1505 u32 thread_index = vlib_get_thread_index ();
1506 vlib_rpc_call_main_thread (vlib_worker_sync_rpc, (u8 *) &thread_index,
1507 sizeof (thread_index));
Florin Corase060b0a2024-02-01 21:13:10 -08001508 vlib_worker_flush_pending_rpc_requests (vlib_get_main ());
Florin Coras4b208302022-03-30 13:50:19 -07001509 }
1510
1511 /* Wait until main thread asks for barrier */
1512 while (!(*vlib_worker_threads->wait_at_barrier))
1513 ;
1514
1515 /* Stop before barrier and make sure all threads are either
1516 * at worker barrier or the barrier before it */
1517 clib_atomic_fetch_add (&vlib_worker_threads->workers_before_barrier, 1);
1518 while (vlib_num_workers () > (*vlib_worker_threads->workers_at_barrier +
1519 vlib_worker_threads->workers_before_barrier))
1520 ;
1521}
1522
1523void
1524vlib_workers_continue (void)
1525{
1526 if (PREDICT_FALSE (!vlib_num_workers ()))
1527 return;
1528
1529 clib_atomic_fetch_add (&vlib_worker_threads->done_work_before_barrier, 1);
1530
1531 /* Wait until all workers are done with work before barrier */
1532 while (vlib_worker_threads->done_work_before_barrier <
1533 vlib_worker_threads->workers_before_barrier)
1534 ;
1535
1536 clib_atomic_fetch_add (&vlib_worker_threads->done_work_before_barrier, -1);
1537 clib_atomic_fetch_add (&vlib_worker_threads->workers_before_barrier, -1);
1538}
1539
Neale Ranns42845dd2020-05-26 13:12:17 +00001540/**
1541 * Wait until each of the workers has been once around the track
1542 */
1543void
1544vlib_worker_wait_one_loop (void)
1545{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001546 vlib_global_main_t *vgm = vlib_get_global_main ();
Neale Ranns42845dd2020-05-26 13:12:17 +00001547 ASSERT (vlib_get_thread_index () == 0);
1548
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001549 if (vlib_get_n_threads () < 2)
Neale Ranns42845dd2020-05-26 13:12:17 +00001550 return;
1551
1552 if (vlib_worker_thread_barrier_held ())
1553 return;
1554
1555 u32 *counts = 0;
1556 u32 ii;
1557
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001558 vec_validate (counts, vlib_get_n_threads () - 1);
Neale Ranns42845dd2020-05-26 13:12:17 +00001559
1560 /* record the current loop counts */
Damjan Marionfd8deb42021-03-06 12:26:28 +01001561 vec_foreach_index (ii, vgm->vlib_mains)
1562 counts[ii] = vgm->vlib_mains[ii]->main_loop_count;
Neale Ranns42845dd2020-05-26 13:12:17 +00001563
1564 /* spin until each changes, apart from the main thread, or we'd be
1565 * a while */
1566 for (ii = 1; ii < vec_len (counts); ii++)
1567 {
Damjan Marionfd8deb42021-03-06 12:26:28 +01001568 while (counts[ii] == vgm->vlib_mains[ii]->main_loop_count)
Neale Ranns42845dd2020-05-26 13:12:17 +00001569 CLIB_PAUSE ();
1570 }
1571
1572 vec_free (counts);
1573 return;
1574}
1575
Dave Barach9b8ffd92016-07-08 08:13:45 -04001576void
Florin Coras4cadd3b2024-02-01 20:46:15 -08001577vlib_worker_flush_pending_rpc_requests (vlib_main_t *vm)
1578{
1579 vlib_main_t *vm_global = vlib_get_first_main ();
1580
1581 ASSERT (vm != vm_global);
1582
1583 clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
1584 vec_append (vm_global->pending_rpc_requests, vm->pending_rpc_requests);
1585 vec_reset_length (vm->pending_rpc_requests);
1586 clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
1587}
1588
1589void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001590vlib_worker_thread_fn (void *arg)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001591{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001592 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -04001593 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001594 vlib_main_t *vm = vlib_get_main ();
Damjan Marione9f929b2017-03-16 11:32:09 +01001595 clib_error_t *e;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001596
Damjan Marion586afd72017-04-05 19:18:20 +02001597 ASSERT (vm->thread_index == vlib_get_thread_index ());
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001598
1599 vlib_worker_thread_init (w);
1600 clib_time_init (&vm->clib_time);
1601 clib_mem_set_heap (w->thread_mheap);
1602
Damjan Marionfd8deb42021-03-06 12:26:28 +01001603 vm->worker_init_functions_called = hash_create (0, 0);
1604
1605 e = vlib_call_init_exit_functions_no_sort (
1606 vm, &vgm->worker_init_function_registrations, 1 /* call_once */,
1607 0 /* is_global */);
Damjan Marione9f929b2017-03-16 11:32:09 +01001608 if (e)
1609 clib_error_report (e);
1610
Damjan Marione9d52d52017-03-09 15:42:26 +01001611 vlib_worker_loop (vm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001612}
1613
1614VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1615 .name = "workers",
1616 .short_name = "wk",
1617 .function = vlib_worker_thread_fn,
1618};
1619
Mohammed Hawarie7149262022-05-18 10:08:47 +02001620extern clib_march_fn_registration
1621 *vlib_frame_queue_dequeue_with_aux_fn_march_fn_registrations;
1622extern clib_march_fn_registration
1623 *vlib_frame_queue_dequeue_fn_march_fn_registrations;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001624u32
1625vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1626{
1627 vlib_thread_main_t *tm = vlib_get_thread_main ();
Mohammed Hawarie7149262022-05-18 10:08:47 +02001628 vlib_main_t *vm = vlib_get_main ();
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001629 vlib_frame_queue_main_t *fqm;
1630 vlib_frame_queue_t *fq;
Mohammed Hawarie7149262022-05-18 10:08:47 +02001631 vlib_node_t *node;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001632 int i;
Elias Rudberg368104d2020-04-16 16:01:52 +02001633 u32 num_threads;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001634
1635 if (frame_queue_nelts == 0)
dongjuan88752482019-06-04 10:59:02 +08001636 frame_queue_nelts = FRAME_QUEUE_MAX_NELTS;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001637
Elias Rudberg368104d2020-04-16 16:01:52 +02001638 num_threads = 1 /* main thread */ + tm->n_threads;
1639 ASSERT (frame_queue_nelts >= 8 + num_threads);
Damjan Marion78fd7e82018-07-20 18:47:05 +02001640
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001641 vec_add2 (tm->frame_queue_mains, fqm, 1);
1642
Mohammed Hawarie7149262022-05-18 10:08:47 +02001643 node = vlib_get_node (vm, fqm->node_index);
1644 ASSERT (node);
1645 if (node->aux_offset)
1646 {
1647 fqm->frame_queue_dequeue_fn =
1648 CLIB_MARCH_FN_VOID_POINTER (vlib_frame_queue_dequeue_with_aux_fn);
1649 }
1650 else
1651 {
1652 fqm->frame_queue_dequeue_fn =
1653 CLIB_MARCH_FN_VOID_POINTER (vlib_frame_queue_dequeue_fn);
1654 }
1655
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001656 fqm->node_index = node_index;
Damjan Marion78fd7e82018-07-20 18:47:05 +02001657 fqm->frame_queue_nelts = frame_queue_nelts;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001658
1659 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
Damjan Marion8bea5892022-04-04 22:40:45 +02001660 vec_set_len (fqm->vlib_frame_queues, 0);
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001661 for (i = 0; i < tm->n_vlib_mains; i++)
1662 {
1663 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1664 vec_add1 (fqm->vlib_frame_queues, fq);
1665 }
1666
1667 return (fqm - tm->frame_queue_mains);
1668}
1669
Dave Barach69128d02017-09-26 10:54:34 -04001670void
1671vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1672 args)
1673{
1674 ASSERT (vlib_get_thread_index () == 0);
1675 vlib_process_signal_event (vlib_get_main (), args->node_index,
1676 args->type_opaque, args->data);
1677}
1678
1679void *rpc_call_main_thread_cb_fn;
1680
1681void
1682vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1683{
1684 if (rpc_call_main_thread_cb_fn)
1685 {
1686 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1687 (*fp) (callback, args, arg_size);
1688 }
1689 else
1690 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1691}
1692
Dave Barach9b8ffd92016-07-08 08:13:45 -04001693clib_error_t *
1694threads_init (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001695{
Benoît Ganne8b153de2021-12-09 18:24:21 +01001696 const vlib_thread_main_t *tm = vlib_get_thread_main ();
1697
1698 if (tm->main_lcore == ~0 && tm->n_vlib_mains > 1)
1699 return clib_error_return (0, "Configuration error, a main core must "
1700 "be specified when using worker threads");
1701
Ed Warnickecb9cada2015-12-08 15:45:58 -07001702 return 0;
1703}
1704
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001705VLIB_INIT_FUNCTION (threads_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001706
Dave Baracha4324a92019-02-19 17:05:30 -05001707static clib_error_t *
1708show_clock_command_fn (vlib_main_t * vm,
1709 unformat_input_t * input, vlib_cli_command_t * cmd)
1710{
Dave Barachc25048b2020-01-29 18:05:24 -05001711 int verbose = 0;
Dave Barach19718002020-03-11 10:31:36 -04001712 clib_timebase_t _tb, *tb = &_tb;
Dave Baracha4324a92019-02-19 17:05:30 -05001713
Dave Barachc25048b2020-01-29 18:05:24 -05001714 (void) unformat (input, "verbose %=", &verbose, 1);
Dave Baracha4324a92019-02-19 17:05:30 -05001715
Dave Barach19718002020-03-11 10:31:36 -04001716 clib_timebase_init (tb, 0 /* GMT */ , CLIB_TIMEBASE_DAYLIGHT_NONE,
1717 &vm->clib_time);
1718
1719 vlib_cli_output (vm, "%U, %U GMT", format_clib_time, &vm->clib_time,
1720 verbose, format_clib_timebase_time,
1721 clib_timebase_now (tb));
Dave Baracha4324a92019-02-19 17:05:30 -05001722
Dave Baracha4324a92019-02-19 17:05:30 -05001723 vlib_cli_output (vm, "Time last barrier release %.9f",
1724 vm->time_last_barrier_release);
1725
Benoît Ganne56eccdb2021-08-20 09:18:31 +02001726 foreach_vlib_main ()
Dave Baracha4324a92019-02-19 17:05:30 -05001727 {
Benoît Ganne56eccdb2021-08-20 09:18:31 +02001728 vlib_cli_output (vm, "%d: %U", this_vlib_main->thread_index,
1729 format_clib_time, &this_vlib_main->clib_time, verbose);
Dave Barachc25048b2020-01-29 18:05:24 -05001730
Benoît Ganne56eccdb2021-08-20 09:18:31 +02001731 vlib_cli_output (vm, "Thread %d offset %.9f error %.9f",
1732 this_vlib_main->thread_index,
1733 this_vlib_main->time_offset,
1734 vm->time_last_barrier_release -
1735 this_vlib_main->time_last_barrier_release);
Dave Baracha4324a92019-02-19 17:05:30 -05001736 }
1737 return 0;
1738}
1739
Dave Baracha4324a92019-02-19 17:05:30 -05001740VLIB_CLI_COMMAND (f_command, static) =
1741{
1742 .path = "show clock",
1743 .short_help = "show clock",
1744 .function = show_clock_command_fn,
1745};
Dave Baracha4324a92019-02-19 17:05:30 -05001746
Dave Barachab1a50c2020-10-06 14:08:16 -04001747vlib_thread_main_t *
1748vlib_get_thread_main_not_inline (void)
1749{
1750 return vlib_get_thread_main ();
1751}
1752
Dave Barach9b8ffd92016-07-08 08:13:45 -04001753/*
1754 * fd.io coding-style-patch-verification: ON
1755 *
1756 * Local Variables:
1757 * eval: (c-set-style "gnu")
1758 * End:
1759 */