blob: 22fa5f12ecd262313b004a7303950ed0e77d98eb [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
Christophe Fontainefef15b42016-04-09 12:38:49 +090015#define _GNU_SOURCE
Christophe Fontainefef15b42016-04-09 12:38:49 +090016
Ed Warnickecb9cada2015-12-08 15:45:58 -070017#include <signal.h>
18#include <math.h>
19#include <vppinfra/format.h>
Mohsin Kazmi5d64c782018-09-11 20:27:09 +020020#include <vppinfra/linux/sysfs.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021#include <vlib/vlib.h>
22
23#include <vlib/threads.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024#include <vlib/unix/cj.h>
25
Ole Troan233e4682019-05-16 15:01:34 +020026#include <vlib/stat_weak_inlines.h>
27
Ed Warnickecb9cada2015-12-08 15:45:58 -070028DECLARE_CJ_GLOBAL_LOG;
29
Damjan Marion78fd7e82018-07-20 18:47:05 +020030#define FRAME_QUEUE_NELTS 64
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
Dave Barach9b8ffd92016-07-08 08:13:45 -040032u32
33vl (void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -070034{
35 return vec_len (p);
36}
37
Damjan Marion6a7acc22016-12-19 16:28:36 +010038vlib_worker_thread_t *vlib_worker_threads;
Ed Warnickecb9cada2015-12-08 15:45:58 -070039vlib_thread_main_t vlib_thread_main;
40
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010041/*
42 * Barrier tracing can be enabled on a normal build to collect information
43 * on barrier use, including timings and call stacks. Deliberately not
44 * keyed off CLIB_DEBUG, because that can add significant overhead which
45 * imapacts observed timings.
46 */
47
Dave Barachc3a06552018-10-01 09:25:32 -040048u32
49elog_global_id_for_msg_name (const char *msg_name)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010050{
51 uword *p, r;
52 static uword *h;
53 u8 *name_copy;
54
55 if (!h)
56 h = hash_create_string (0, sizeof (uword));
57
58 p = hash_get_mem (h, msg_name);
59 if (p)
60 return p[0];
61 r = elog_string (&vlib_global_main.elog_main, "%s", msg_name);
62
63 name_copy = format (0, "%s%c", msg_name, 0);
64
65 hash_set_mem (h, name_copy, r);
66
67 return r;
68}
69
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010070static inline void
71barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed)
72{
Dave Barach88c6e002018-09-30 15:54:06 -040073 if (!vlib_worker_threads->barrier_elog_enabled)
74 return;
75
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010076 /* *INDENT-OFF* */
77 ELOG_TYPE_DECLARE (e) =
78 {
Dave Barach88c6e002018-09-30 15:54:06 -040079 .format = "bar-trace-%s-#%d",
80 .format_args = "T4i4",
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010081 };
82 /* *INDENT-ON* */
83 struct
84 {
Dave Barach88c6e002018-09-30 15:54:06 -040085 u32 caller, count, t_entry, t_open, t_closed;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010086 } *ed = 0;
87
88 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
89 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
Dave Barachc3a06552018-10-01 09:25:32 -040090 ed->caller = elog_global_id_for_msg_name
91 (vlib_worker_threads[0].barrier_caller);
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +010092 ed->t_entry = (int) (1000000.0 * t_entry);
93 ed->t_open = (int) (1000000.0 * t_open);
94 ed->t_closed = (int) (1000000.0 * t_closed);
95}
96
97static inline void
98barrier_trace_sync_rec (f64 t_entry)
99{
Dave Barach88c6e002018-09-30 15:54:06 -0400100 if (!vlib_worker_threads->barrier_elog_enabled)
101 return;
102
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100103 /* *INDENT-OFF* */
104 ELOG_TYPE_DECLARE (e) =
105 {
Dave Barach88c6e002018-09-30 15:54:06 -0400106 .format = "bar-syncrec-%s-#%d",
107 .format_args = "T4i4",
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100108 };
109 /* *INDENT-ON* */
110 struct
111 {
Dave Barach88c6e002018-09-30 15:54:06 -0400112 u32 caller, depth;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100113 } *ed = 0;
114
115 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
116 ed->depth = (int) vlib_worker_threads[0].recursion_level - 1;
Dave Barachc3a06552018-10-01 09:25:32 -0400117 ed->caller = elog_global_id_for_msg_name
118 (vlib_worker_threads[0].barrier_caller);
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100119}
120
121static inline void
122barrier_trace_release_rec (f64 t_entry)
123{
Dave Barach88c6e002018-09-30 15:54:06 -0400124 if (!vlib_worker_threads->barrier_elog_enabled)
125 return;
126
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100127 /* *INDENT-OFF* */
128 ELOG_TYPE_DECLARE (e) =
129 {
Dave Barach88c6e002018-09-30 15:54:06 -0400130 .format = "bar-relrrec-#%d",
131 .format_args = "i4",
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100132 };
133 /* *INDENT-ON* */
134 struct
135 {
Dave Barach88c6e002018-09-30 15:54:06 -0400136 u32 depth;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100137 } *ed = 0;
138
139 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100140 ed->depth = (int) vlib_worker_threads[0].recursion_level;
141}
142
143static inline void
144barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main)
145{
Dave Barach88c6e002018-09-30 15:54:06 -0400146 if (!vlib_worker_threads->barrier_elog_enabled)
147 return;
148
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100149 /* *INDENT-OFF* */
150 ELOG_TYPE_DECLARE (e) =
151 {
Dave Barach88c6e002018-09-30 15:54:06 -0400152 .format = "bar-rel-#%d-e%d-u%d-t%d",
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100153 .format_args = "i4i4i4i4",
154 };
155 /* *INDENT-ON* */
156 struct
157 {
Dave Barach88c6e002018-09-30 15:54:06 -0400158 u32 count, t_entry, t_update_main, t_closed_total;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100159 } *ed = 0;
160
161 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
162 ed->t_entry = (int) (1000000.0 * t_entry);
163 ed->t_update_main = (int) (1000000.0 * t_update_main);
164 ed->t_closed_total = (int) (1000000.0 * t_closed_total);
165 ed->count = (int) vlib_worker_threads[0].barrier_sync_count;
166
167 /* Reset context for next trace */
168 vlib_worker_threads[0].barrier_context = NULL;
169}
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100170
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171uword
Damjan Marionf55f9b82017-05-10 21:06:28 +0200172os_get_nthreads (void)
Dave Barach01d86c72016-08-08 15:13:42 -0400173{
Dave Barach2180bac2019-05-10 15:25:10 -0400174 return vec_len (vlib_thread_stacks);
Dave Barach01d86c72016-08-08 15:13:42 -0400175}
176
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177void
178vlib_set_thread_name (char *name)
179{
180 int pthread_setname_np (pthread_t __target_thread, const char *__name);
Dave Barachb2a6e252016-07-27 10:00:58 -0400181 int rv;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400182 pthread_t thread = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
Dave Barach9b8ffd92016-07-08 08:13:45 -0400184 if (thread)
Dave Barachb2a6e252016-07-27 10:00:58 -0400185 {
186 rv = pthread_setname_np (thread, name);
187 if (rv)
Ed Warnicke853e7202016-08-12 11:42:26 -0700188 clib_warning ("pthread_setname_np returned %d", rv);
Dave Barachb2a6e252016-07-27 10:00:58 -0400189 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190}
191
Dave Barach9b8ffd92016-07-08 08:13:45 -0400192static int
193sort_registrations_by_no_clone (void *a0, void *a1)
194{
195 vlib_thread_registration_t **tr0 = a0;
196 vlib_thread_registration_t **tr1 = a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 return ((i32) ((*tr0)->no_data_structure_clone)
199 - ((i32) ((*tr1)->no_data_structure_clone)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200}
201
202static uword *
Damjan Marion01914ce2017-09-14 19:04:50 +0200203clib_sysfs_list_to_bitmap (char *filename)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204{
205 FILE *fp;
206 uword *r = 0;
207
208 fp = fopen (filename, "r");
209
210 if (fp != NULL)
211 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400212 u8 *buffer = 0;
213 vec_validate (buffer, 256 - 1);
214 if (fgets ((char *) buffer, 256, fp))
215 {
216 unformat_input_t in;
217 unformat_init_string (&in, (char *) buffer,
218 strlen ((char *) buffer));
Dave Barachb2a6e252016-07-27 10:00:58 -0400219 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
Ed Warnicke853e7202016-08-12 11:42:26 -0700220 clib_warning ("unformat_bitmap_list failed");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400221 unformat_free (&in);
222 }
223 vec_free (buffer);
224 fclose (fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 }
226 return r;
227}
228
229
230/* Called early in the init sequence */
231
232clib_error_t *
233vlib_thread_init (vlib_main_t * vm)
234{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400235 vlib_thread_main_t *tm = &vlib_thread_main;
236 vlib_worker_thread_t *w;
237 vlib_thread_registration_t *tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238 u32 n_vlib_mains = 1;
239 u32 first_index = 1;
240 u32 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400241 uword *avail_cpu;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
243 /* get bitmaps of active cpu cores and sockets */
244 tm->cpu_core_bitmap =
Damjan Marion01914ce2017-09-14 19:04:50 +0200245 clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 tm->cpu_socket_bitmap =
Damjan Marion01914ce2017-09-14 19:04:50 +0200247 clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Dave Barach9b8ffd92016-07-08 08:13:45 -0400249 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
251 /* skip cores */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400252 for (i = 0; i < tm->skip_cores; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400254 uword c = clib_bitmap_first_set (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 if (c == ~0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400256 return clib_error_return (0, "no available cpus to skip");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
Dave Barach9b8ffd92016-07-08 08:13:45 -0400258 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 }
260
261 /* grab cpu for main thread */
Damjan Marion858151f2018-07-11 10:51:00 +0200262 if (tm->main_lcore == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 {
Damjan Marion858151f2018-07-11 10:51:00 +0200264 /* if main-lcore is not set, we try to use lcore 1 */
265 if (clib_bitmap_get (avail_cpu, 1))
266 tm->main_lcore = 1;
267 else
268 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400269 if (tm->main_lcore == (u8) ~ 0)
270 return clib_error_return (0, "no available cpus to be used for the"
271 " main thread");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 }
273 else
274 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400275 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
276 return clib_error_return (0, "cpu %u is not available to be used"
277 " for the main thread", tm->main_lcore);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400279 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
281 /* assume that there is socket 0 only if there is no data from sysfs */
282 if (!tm->cpu_socket_bitmap)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400283 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Christophe Fontainefef15b42016-04-09 12:38:49 +0900285 /* pin main thread to main_lcore */
Damjan Marion878c6092017-01-04 13:19:27 +0100286 if (tm->cb.vlib_thread_set_lcore_cb)
287 {
288 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
289 }
Damjan Marion858151f2018-07-11 10:51:00 +0200290 else
291 {
292 cpu_set_t cpuset;
293 CPU_ZERO (&cpuset);
294 CPU_SET (tm->main_lcore, &cpuset);
295 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
296 }
Christophe Fontainefef15b42016-04-09 12:38:49 +0900297
Dave Barach2180bac2019-05-10 15:25:10 -0400298 /* Set up thread 0 */
299 vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400300 _vec_len (vlib_worker_threads) = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 w = vlib_worker_threads;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400302 w->thread_mheap = clib_mem_get_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 w->thread_stack = vlib_thread_stacks[0];
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200304 w->cpu_id = tm->main_lcore;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400305 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200306 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307 tm->n_vlib_mains = 1;
308
Pavel Kotucek1e765832016-09-23 08:54:14 +0200309 if (tm->sched_policy != ~0)
310 {
311 struct sched_param sched_param;
312 if (!sched_getparam (w->lwp, &sched_param))
313 {
314 if (tm->sched_priority != ~0)
315 sched_param.sched_priority = tm->sched_priority;
316 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
317 }
318 }
319
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 /* assign threads to cores and set n_vlib_mains */
321 tr = tm->next;
322
323 while (tr)
324 {
325 vec_add1 (tm->registrations, tr);
326 tr = tr->next;
327 }
328
Dave Barach9b8ffd92016-07-08 08:13:45 -0400329 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
331 for (i = 0; i < vec_len (tm->registrations); i++)
332 {
333 int j;
334 tr = tm->registrations[i];
335 tr->first_index = first_index;
336 first_index += tr->count;
337 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
338
339 /* construct coremask */
340 if (tr->use_pthreads || !tr->count)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400341 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
343 if (tr->coremask)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400344 {
345 uword c;
346 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347 clib_bitmap_foreach (c, tr->coremask, ({
348 if (clib_bitmap_get(avail_cpu, c) == 0)
349 return clib_error_return (0, "cpu %u is not available to be used"
350 " for the '%s' thread",c, tr->name);
351
352 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
353 }));
Dave Barach2180bac2019-05-10 15:25:10 -0400354 /* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400357 {
358 for (j = 0; j < tr->count; j++)
359 {
360 uword c = clib_bitmap_first_set (avail_cpu);
361 if (c == ~0)
362 return clib_error_return (0,
363 "no available cpus to be used for"
364 " the '%s' thread", tr->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
Dave Barach9b8ffd92016-07-08 08:13:45 -0400366 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
367 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
368 }
369 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 }
371
Dave Barach9b8ffd92016-07-08 08:13:45 -0400372 clib_bitmap_free (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
374 tm->n_vlib_mains = n_vlib_mains;
375
Dave Barach2180bac2019-05-10 15:25:10 -0400376 /*
377 * Allocate the remaining worker threads, and thread stack vector slots
378 * from now on, calls to os_get_nthreads() will return the correct
379 * answer.
380 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400381 vec_validate_aligned (vlib_worker_threads, first_index - 1,
382 CLIB_CACHE_LINE_BYTES);
Dave Barach2180bac2019-05-10 15:25:10 -0400383 vec_validate (vlib_thread_stacks, vec_len (vlib_worker_threads) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 return 0;
385}
386
Dave Barach9b8ffd92016-07-08 08:13:45 -0400387vlib_frame_queue_t *
388vlib_frame_queue_alloc (int nelts)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400390 vlib_frame_queue_t *fq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
Dave Barach9b8ffd92016-07-08 08:13:45 -0400392 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400393 clib_memset (fq, 0, sizeof (*fq));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 fq->nelts = nelts;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400395 fq->vector_threshold = 128; // packets
396 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397
398 if (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400399 {
400 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
401 fformat (stderr, "WARNING: fq->tail unaligned\n");
402 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
403 fformat (stderr, "WARNING: fq->head unaligned\n");
404 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
405 fformat (stderr, "WARNING: fq->elts unaligned\n");
406
407 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
408 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
409 sizeof (fq->elts[0]));
410 if (nelts & (nelts - 1))
411 {
412 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
413 abort ();
414 }
415 }
416
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 return (fq);
418}
419
420void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400421void
422vl_msg_api_handler_no_free (void *v)
423{
424}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
426/* Turned off, save as reference material... */
427#if 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400428static inline int
429vlib_frame_queue_dequeue_internal (int thread_id,
430 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431{
432 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
433 vlib_frame_queue_elt_t *elt;
434 vlib_frame_t *f;
435 vlib_pending_frame_t *p;
436 vlib_node_runtime_t *r;
437 u32 node_runtime_index;
438 int msg_type;
439 u64 before;
440 int processed = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400441
442 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
444 while (1)
445 {
446 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400447 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448
Dave Barach9b8ffd92016-07-08 08:13:45 -0400449 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
451 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400452 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454 before = clib_cpu_time_now ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455
456 f = elt->frame;
457 node_runtime_index = elt->node_runtime_index;
458 msg_type = elt->msg_type;
459
460 switch (msg_type)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400461 {
462 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
463 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
464 /* note fallthrough... */
465 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
466 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
467 node_runtime_index);
468 vlib_frame_free (vm, r, f);
469 break;
470 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
471 vec_add2 (vm->node_main.pending_frames, p, 1);
472 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
473 p->node_runtime_index = elt->node_runtime_index;
474 p->frame_index = vlib_frame_index (vm, f);
475 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
476 fq->dequeue_vectors += (u64) f->n_vectors;
477 break;
478 case VLIB_FRAME_QUEUE_ELT_API_MSG:
479 vl_msg_api_handler_no_free (f);
480 break;
481 default:
482 clib_warning ("bogus frame queue message, type %d", msg_type);
483 break;
484 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 elt->valid = 0;
486 fq->dequeues++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400487 fq->dequeue_ticks += clib_cpu_time_now () - before;
488 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489 fq->head++;
490 processed++;
491 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400492 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 return processed;
494}
495
Dave Barach9b8ffd92016-07-08 08:13:45 -0400496int
497vlib_frame_queue_dequeue (int thread_id,
498 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499{
500 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
501}
502
Dave Barach9b8ffd92016-07-08 08:13:45 -0400503int
504vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
505 u32 frame_queue_index, vlib_frame_t * frame,
506 vlib_frame_queue_msg_type_t type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507{
508 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
509 vlib_frame_queue_elt_t *elt;
510 u32 save_count;
511 u64 new_tail;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400512 u64 before = clib_cpu_time_now ();
513
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 ASSERT (fq);
515
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000516 new_tail = clib_atomic_add_fetch (&fq->tail, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517
518 /* Wait until a ring slot is available */
519 while (new_tail >= fq->head + fq->nelts)
520 {
521 f64 b4 = vlib_time_now_ticks (vm, before);
522 vlib_worker_thread_barrier_check (vm, b4);
523 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
Damjan Marion586afd72017-04-05 19:18:20 +0200524 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 }
526
Dave Barach9b8ffd92016-07-08 08:13:45 -0400527 elt = fq->elts + (new_tail & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
529 /* this would be very bad... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530 while (elt->valid)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 {
532 }
533
534 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
535 save_count = frame->n_vectors;
536
537 elt->frame = frame;
538 elt->node_runtime_index = node_runtime_index;
539 elt->msg_type = type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400540 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 elt->valid = 1;
542
543 return save_count;
544}
545#endif /* 0 */
546
547/* To be called by vlib worker threads upon startup */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400548void
549vlib_worker_thread_init (vlib_worker_thread_t * w)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400551 vlib_thread_main_t *tm = vlib_get_thread_main ();
552
Dave Barachfdf49442016-12-20 12:48:14 -0500553 /*
554 * Note: disabling signals in worker threads as follows
555 * prevents the api post-mortem dump scheme from working
556 * {
557 * sigset_t s;
558 * sigfillset (&s);
559 * pthread_sigmask (SIG_SETMASK, &s, 0);
560 * }
561 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
563 clib_mem_set_heap (w->thread_mheap);
564
Dave Barach9b8ffd92016-07-08 08:13:45 -0400565 if (vec_len (tm->thread_prefix) && w->registration->short_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400567 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
568 w->registration->short_name, w->instance_id, '\0');
569 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 }
571
572 if (!w->registration->use_pthreads)
573 {
574
575 /* Initial barrier sync, for both worker and i/o threads */
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000576 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
578 while (*vlib_worker_threads->wait_at_barrier)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400579 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000581 clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 }
583}
584
Dave Barach9b8ffd92016-07-08 08:13:45 -0400585void *
586vlib_worker_thread_bootstrap_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587{
588 void *rv;
589 vlib_worker_thread_t *w = arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400590
591 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200592 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Damjan Marionf55f9b82017-05-10 21:06:28 +0200594 __os_thread_index = w - vlib_worker_threads;
Damjan Marion586afd72017-04-05 19:18:20 +0200595
Dave Barach9b8ffd92016-07-08 08:13:45 -0400596 rv = (void *) clib_calljmp
597 ((uword (*)(uword)) w->thread_function,
598 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 /* NOTREACHED, we hope */
600 return rv;
601}
602
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200603static void
604vlib_get_thread_core_socket (vlib_worker_thread_t * w, unsigned cpu_id)
605{
606 const char *sys_cpu_path = "/sys/devices/system/cpu/cpu";
607 u8 *p = 0;
608 int core_id = -1, socket_id = -1;
609
610 p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, cpu_id, 0);
611 clib_sysfs_read ((char *) p, "%d", &core_id);
612 vec_reset_length (p);
613 p =
614 format (p, "%s%u/topology/physical_package_id%c", sys_cpu_path, cpu_id,
615 0);
616 clib_sysfs_read ((char *) p, "%d", &socket_id);
617 vec_free (p);
618
619 w->core_id = core_id;
620 w->socket_id = socket_id;
621}
622
Damjan Marion878c6092017-01-04 13:19:27 +0100623static clib_error_t *
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200624vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625{
Damjan Marion878c6092017-01-04 13:19:27 +0100626 vlib_thread_main_t *tm = &vlib_thread_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400627 void *(*fp_arg) (void *) = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200629 w->cpu_id = cpu_id;
630 vlib_get_thread_core_socket (w, cpu_id);
Damjan Marion878c6092017-01-04 13:19:27 +0100631 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200632 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, cpu_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400634 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400635 pthread_t worker;
636 cpu_set_t cpuset;
637 CPU_ZERO (&cpuset);
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200638 CPU_SET (cpu_id, &cpuset);
Christophe Fontainefef15b42016-04-09 12:38:49 +0900639
Damjan Marion878c6092017-01-04 13:19:27 +0100640 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
641 return clib_error_return_unix (0, "pthread_create");
642
643 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
644 return clib_error_return_unix (0, "pthread_setaffinity_np");
645
646 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400647 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648}
649
Dave Barach9b8ffd92016-07-08 08:13:45 -0400650static clib_error_t *
651start_workers (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652{
653 int i, j;
654 vlib_worker_thread_t *w;
655 vlib_main_t *vm_clone;
656 void *oldheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657 vlib_thread_main_t *tm = &vlib_thread_main;
658 vlib_thread_registration_t *tr;
659 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660 u32 n_vlib_mains = tm->n_vlib_mains;
661 u32 worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400662 u8 *main_heap = clib_mem_get_per_cpu_heap ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400663
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 vec_reset_length (vlib_worker_threads);
665
666 /* Set up the main thread */
667 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
Dave Barach3e7deb12016-02-05 16:29:53 -0500668 w->elog_track.name = "main thread";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669 elog_track_register (&vm->elog_main, &w->elog_track);
670
Dave Barach9b8ffd92016-07-08 08:13:45 -0400671 if (vec_len (tm->thread_prefix))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400673 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
674 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675 }
676
Dave Barach9b8ffd92016-07-08 08:13:45 -0400677 vm->elog_main.lock =
678 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barach848191d2016-04-28 16:24:15 -0400679 vm->elog_main.lock[0] = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400680
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681 if (n_vlib_mains > 1)
682 {
Dave Barach80f54e22017-03-08 19:08:56 -0500683 /* Replace hand-crafted length-1 vector with a real vector */
684 vlib_mains = 0;
685
686 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
687 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688 _vec_len (vlib_mains) = 0;
Dave Barach80f54e22017-03-08 19:08:56 -0500689 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690
Dave Barach9b8ffd92016-07-08 08:13:45 -0400691 vlib_worker_threads->wait_at_barrier =
692 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693 vlib_worker_threads->workers_at_barrier =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400694 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100696 vlib_worker_threads->node_reforks_required =
697 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
698
Dave Barachf6c68d72018-11-01 08:12:52 -0400699 /* We'll need the rpc vector lock... */
700 clib_spinlock_init (&vm->pending_rpc_lock);
701
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702 /* Ask for an initial barrier sync */
703 *vlib_worker_threads->workers_at_barrier = 0;
704 *vlib_worker_threads->wait_at_barrier = 1;
705
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100706 /* Without update or refork */
707 *vlib_worker_threads->node_reforks_required = 0;
708 vm->need_vlib_worker_thread_node_runtime_update = 0;
709
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100710 /* init timing */
711 vm->barrier_epoch = 0;
712 vm->barrier_no_close_before = 0;
713
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714 worker_thread_index = 1;
715
Dave Barach9b8ffd92016-07-08 08:13:45 -0400716 for (i = 0; i < vec_len (tm->registrations); i++)
717 {
718 vlib_node_main_t *nm, *nm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400719 int k;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
Dave Barach9b8ffd92016-07-08 08:13:45 -0400721 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
Dave Barach9b8ffd92016-07-08 08:13:45 -0400723 if (tr->count == 0)
724 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725
Dave Barach9b8ffd92016-07-08 08:13:45 -0400726 for (k = 0; k < tr->count; k++)
727 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100728 vlib_node_t *n;
729
Dave Barach9b8ffd92016-07-08 08:13:45 -0400730 vec_add2 (vlib_worker_threads, w, 1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400731 /* Currently unused, may not really work */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732 if (tr->mheap_size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400733 {
734#if USE_DLMALLOC == 0
735 w->thread_mheap =
736 mheap_alloc (0 /* use VM */ , tr->mheap_size);
737#else
738 w->thread_mheap = create_mspace (tr->mheap_size,
739 0 /* unlocked */ );
740#endif
741 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400742 else
743 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200744
745 w->thread_stack =
746 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400747 w->thread_function = tr->function;
748 w->thread_function_arg = w;
749 w->instance_id = k;
750 w->registration = tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
Dave Barach9b8ffd92016-07-08 08:13:45 -0400752 w->elog_track.name =
753 (char *) format (0, "%s %d", tr->name, k + 1);
754 vec_add1 (w->elog_track.name, 0);
755 elog_track_register (&vm->elog_main, &w->elog_track);
Bud Grise68adab92016-02-12 10:36:11 -0500756
Dave Barach9b8ffd92016-07-08 08:13:45 -0400757 if (tr->no_data_structure_clone)
758 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759
Dave Barach9b8ffd92016-07-08 08:13:45 -0400760 /* Fork vlib_global_main et al. Look for bugs here */
761 oldheap = clib_mem_set_heap (w->thread_mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200763 vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone),
764 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400765 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
Damjan Marion586afd72017-04-05 19:18:20 +0200767 vm_clone->thread_index = worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400768 vm_clone->heap_base = w->thread_mheap;
Dave Barach6a5adc32018-07-04 10:56:23 -0400769 vm_clone->heap_aligned_base = (void *)
770 (((uword) w->thread_mheap) & ~(VLIB_FRAME_ALIGN - 1));
Damjan Marione9f929b2017-03-16 11:32:09 +0100771 vm_clone->init_functions_called =
772 hash_create (0, /* value bytes */ 0);
Dave Barach2877eee2017-12-15 12:22:57 -0500773 vm_clone->pending_rpc_requests = 0;
774 vec_validate (vm_clone->pending_rpc_requests, 0);
775 _vec_len (vm_clone->pending_rpc_requests) = 0;
Dave Barachb7b92992018-10-17 10:38:51 -0400776 clib_memset (&vm_clone->random_buffer, 0,
777 sizeof (vm_clone->random_buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778
Dave Barach9b8ffd92016-07-08 08:13:45 -0400779 nm = &vlib_mains[0]->node_main;
780 nm_clone = &vm_clone->node_main;
781 /* fork next frames array, preserving node runtime indices */
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200782 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
783 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400784 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
785 {
786 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
787 u32 save_node_runtime_index;
788 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789
Dave Barach9b8ffd92016-07-08 08:13:45 -0400790 save_node_runtime_index = nf->node_runtime_index;
791 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
792 vlib_next_frame_init (nf);
793 nf->node_runtime_index = save_node_runtime_index;
794 nf->flags = save_flags;
795 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796
Dave Barach9b8ffd92016-07-08 08:13:45 -0400797 /* fork the frame dispatch queue */
798 nm_clone->pending_frames = 0;
Dave Barach53fe4a72019-01-26 09:50:26 -0500799 vec_validate (nm_clone->pending_frames, 10);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400800 _vec_len (nm_clone->pending_frames) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801
Dave Barach9b8ffd92016-07-08 08:13:45 -0400802 /* fork nodes */
803 nm_clone->nodes = 0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100804
805 /* Allocate all nodes in single block for speed */
806 n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n));
807
Dave Barach9b8ffd92016-07-08 08:13:45 -0400808 for (j = 0; j < vec_len (nm->nodes); j++)
809 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400810 clib_memcpy (n, nm->nodes[j], sizeof (*n));
811 /* none of the copied nodes have enqueue rights given out */
812 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
Dave Barachb7b92992018-10-17 10:38:51 -0400813 clib_memset (&n->stats_total, 0, sizeof (n->stats_total));
814 clib_memset (&n->stats_last_clear, 0,
815 sizeof (n->stats_last_clear));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400816 vec_add1 (nm_clone->nodes, n);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100817 n++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400818 }
819 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200820 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
821 CLIB_CACHE_LINE_BYTES);
JingLiuZTE30af5da2017-07-24 10:53:31 +0800822 vec_foreach (rt,
823 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
Damjan Marione9f929b2017-03-16 11:32:09 +0100824 {
825 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200826 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100827 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100828 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100829 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100830 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
831 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100832 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833
Dave Barach9b8ffd92016-07-08 08:13:45 -0400834 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200835 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
836 CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400837 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
Damjan Marione9f929b2017-03-16 11:32:09 +0100838 {
839 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200840 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100841 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100842 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100843 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100844 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
845 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100846 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700847
Dave Barach53fe4a72019-01-26 09:50:26 -0500848 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
849 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
850 CLIB_CACHE_LINE_BYTES);
851 vec_foreach (rt,
852 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
853 {
854 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
855 rt->thread_index = vm_clone->thread_index;
856 /* copy initial runtime_data from node */
857 if (n->runtime_data && n->runtime_data_bytes > 0)
858 clib_memcpy (rt->runtime_data, n->runtime_data,
859 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
860 n->runtime_data_bytes));
861 }
862
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200863 nm_clone->processes = vec_dup_aligned (nm->processes,
864 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865
Dave Barach593eedf2019-03-10 09:44:51 -0400866 /* Create per-thread frame freelist */
867 nm_clone->frame_sizes = vec_new (vlib_frame_size_t, 1);
868#ifdef VLIB_SUPPORTS_ARBITRARY_SCALAR_SIZES
Florin Coras93992a92017-05-24 18:03:56 -0700869 nm_clone->frame_size_hash = hash_create (0, sizeof (uword));
Dave Barach593eedf2019-03-10 09:44:51 -0400870#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700871
Dave Barach9b8ffd92016-07-08 08:13:45 -0400872 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100873
Dave Barach9b8ffd92016-07-08 08:13:45 -0400874 clib_mem_set_heap (oldheap);
Dave Barach80f54e22017-03-08 19:08:56 -0500875 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876
Ole Troan233e4682019-05-16 15:01:34 +0200877 /* Switch to the stats segment ... */
878 void *oldheap = vlib_stats_push_heap (0);
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200879 vm_clone->error_main.counters = vec_dup_aligned
880 (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES);
Ole Troan233e4682019-05-16 15:01:34 +0200881 vlib_stats_pop_heap2 (vm_clone->error_main.counters,
882 worker_thread_index, oldheap, 1);
883
Damjan Marionbe3f4d52018-03-27 21:06:10 +0200884 vm_clone->error_main.counters_last_clear = vec_dup_aligned
885 (vlib_mains[0]->error_main.counters_last_clear,
886 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887
Dave Barach9b8ffd92016-07-08 08:13:45 -0400888 worker_thread_index++;
889 }
890 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891 }
892 else
893 {
894 /* only have non-data-structure copy threads to create... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400895 for (i = 0; i < vec_len (tm->registrations); i++)
896 {
897 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898
Dave Barach9b8ffd92016-07-08 08:13:45 -0400899 for (j = 0; j < tr->count; j++)
900 {
901 vec_add2 (vlib_worker_threads, w, 1);
902 if (tr->mheap_size)
Dave Barach6a5adc32018-07-04 10:56:23 -0400903 {
904#if USE_DLMALLOC == 0
905 w->thread_mheap =
906 mheap_alloc (0 /* use VM */ , tr->mheap_size);
907#else
908 w->thread_mheap =
909 create_mspace (tr->mheap_size, 0 /* locked */ );
910#endif
911 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400912 else
913 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200914 w->thread_stack =
915 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400916 w->thread_function = tr->function;
917 w->thread_function_arg = w;
918 w->instance_id = j;
919 w->elog_track.name =
920 (char *) format (0, "%s %d", tr->name, j + 1);
921 w->registration = tr;
922 vec_add1 (w->elog_track.name, 0);
923 elog_track_register (&vm->elog_main, &w->elog_track);
924 }
925 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926 }
927
928 worker_thread_index = 1;
929
930 for (i = 0; i < vec_len (tm->registrations); i++)
931 {
Damjan Marion878c6092017-01-04 13:19:27 +0100932 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933 int j;
934
935 tr = tm->registrations[i];
936
937 if (tr->use_pthreads || tm->use_pthreads)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400938 {
939 for (j = 0; j < tr->count; j++)
940 {
941 w = vlib_worker_threads + worker_thread_index++;
Damjan Marion878c6092017-01-04 13:19:27 +0100942 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
943 w, 0);
944 if (err)
945 clib_error_report (err);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400946 }
947 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400949 {
950 uword c;
Damjan Marion878c6092017-01-04 13:19:27 +0100951 /* *INDENT-OFF* */
952 clib_bitmap_foreach (c, tr->coremask, ({
953 w = vlib_worker_threads + worker_thread_index++;
954 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
955 w, c);
956 if (err)
957 clib_error_report (err);
958 }));
959 /* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400960 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400962 vlib_worker_thread_barrier_sync (vm);
963 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964 return 0;
965}
966
967VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
968
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100969
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100970static inline void
971worker_thread_node_runtime_update_internal (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972{
973 int i, j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974 vlib_main_t *vm;
975 vlib_node_main_t *nm, *nm_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976 vlib_main_t *vm_clone;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +0100977 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978 never_inline void
979 vlib_node_runtime_sync_stats (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400980 vlib_node_runtime_t * r,
981 uword n_calls,
982 uword n_vectors, uword n_clocks);
983
Damjan Marion586afd72017-04-05 19:18:20 +0200984 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986 vm = vlib_mains[0];
987 nm = &vm->node_main;
988
Ed Warnickecb9cada2015-12-08 15:45:58 -0700989 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
990
Dave Barach9b8ffd92016-07-08 08:13:45 -0400991 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992 * Scrape all runtime stats, so we don't lose node runtime(s) with
993 * pending counts, or throw away worker / io thread counts.
994 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400995 for (j = 0; j < vec_len (nm->nodes); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700996 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400997 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998 n = nm->nodes[j];
999 vlib_node_sync_stats (vm, n);
1000 }
1001
1002 for (i = 1; i < vec_len (vlib_mains); i++)
1003 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001004 vlib_node_t *n;
1005
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 vm_clone = vlib_mains[i];
1007 nm_clone = &vm_clone->node_main;
1008
Dave Barach9b8ffd92016-07-08 08:13:45 -04001009 for (j = 0; j < vec_len (nm_clone->nodes); j++)
1010 {
1011 n = nm_clone->nodes[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012
Dave Barach9b8ffd92016-07-08 08:13:45 -04001013 rt = vlib_node_get_runtime (vm_clone, n->index);
1014 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
1015 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016 }
1017
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001018 /* Per-worker clone rebuilds are now done on each thread */
1019}
1020
1021
1022void
1023vlib_worker_thread_node_refork (void)
1024{
1025 vlib_main_t *vm, *vm_clone;
1026 vlib_node_main_t *nm, *nm_clone;
1027 vlib_node_t **old_nodes_clone;
1028 vlib_node_runtime_t *rt, *old_rt;
1029
1030 vlib_node_t *new_n_clone;
1031
1032 int j;
1033
1034 vm = vlib_mains[0];
1035 nm = &vm->node_main;
1036 vm_clone = vlib_get_main ();
1037 nm_clone = &vm_clone->node_main;
1038
1039 /* Re-clone error heap */
1040 u64 *old_counters = vm_clone->error_main.counters;
1041 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
1042
Dave Barach178cf492018-11-13 16:34:13 -05001043 clib_memcpy_fast (&vm_clone->error_main, &vm->error_main,
1044 sizeof (vm->error_main));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001045 j = vec_len (vm->error_main.counters) - 1;
Ole Troan233e4682019-05-16 15:01:34 +02001046
1047 /* Switch to the stats segment ... */
1048 void *oldheap = vlib_stats_push_heap (0);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001049 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001050 vm_clone->error_main.counters = old_counters;
Ole Troan233e4682019-05-16 15:01:34 +02001051 vlib_stats_pop_heap2 (vm_clone->error_main.counters, vm_clone->thread_index,
1052 oldheap, 0);
1053
1054 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001055 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
1056
1057 nm_clone = &vm_clone->node_main;
1058 vec_free (nm_clone->next_frames);
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001059 nm_clone->next_frames = vec_dup_aligned (nm->next_frames,
1060 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001061
1062 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063 {
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001064 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
1065 u32 save_node_runtime_index;
1066 u32 save_flags;
Damjan Marionbc20bdf2015-12-17 14:28:18 +01001067
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001068 save_node_runtime_index = nf->node_runtime_index;
1069 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
1070 vlib_next_frame_init (nf);
1071 nf->node_runtime_index = save_node_runtime_index;
1072 nf->flags = save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001074
1075 old_nodes_clone = nm_clone->nodes;
1076 nm_clone->nodes = 0;
1077
1078 /* re-fork nodes */
1079
1080 /* Allocate all nodes in single block for speed */
1081 new_n_clone =
1082 clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone));
1083 for (j = 0; j < vec_len (nm->nodes); j++)
1084 {
1085 vlib_node_t *old_n_clone;
1086 vlib_node_t *new_n;
1087
1088 new_n = nm->nodes[j];
1089 old_n_clone = old_nodes_clone[j];
1090
Dave Barach178cf492018-11-13 16:34:13 -05001091 clib_memcpy_fast (new_n_clone, new_n, sizeof (*new_n));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001092 /* none of the copied nodes have enqueue rights given out */
1093 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
1094
1095 if (j >= vec_len (old_nodes_clone))
1096 {
1097 /* new node, set to zero */
Dave Barachb7b92992018-10-17 10:38:51 -04001098 clib_memset (&new_n_clone->stats_total, 0,
1099 sizeof (new_n_clone->stats_total));
1100 clib_memset (&new_n_clone->stats_last_clear, 0,
1101 sizeof (new_n_clone->stats_last_clear));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001102 }
1103 else
1104 {
1105 /* Copy stats if the old data is valid */
Dave Barach178cf492018-11-13 16:34:13 -05001106 clib_memcpy_fast (&new_n_clone->stats_total,
1107 &old_n_clone->stats_total,
1108 sizeof (new_n_clone->stats_total));
1109 clib_memcpy_fast (&new_n_clone->stats_last_clear,
1110 &old_n_clone->stats_last_clear,
1111 sizeof (new_n_clone->stats_last_clear));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001112
1113 /* keep previous node state */
1114 new_n_clone->state = old_n_clone->state;
1115 }
1116 vec_add1 (nm_clone->nodes, new_n_clone);
1117 new_n_clone++;
1118 }
1119 /* Free the old node clones */
1120 clib_mem_free (old_nodes_clone[0]);
1121
1122 vec_free (old_nodes_clone);
1123
1124
1125 /* re-clone internal nodes */
1126 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
1127 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001128 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1129 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001130
1131 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
1132 {
1133 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1134 rt->thread_index = vm_clone->thread_index;
1135 /* copy runtime_data, will be overwritten later for existing rt */
1136 if (n->runtime_data && n->runtime_data_bytes > 0)
Dave Barach178cf492018-11-13 16:34:13 -05001137 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1138 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1139 n->runtime_data_bytes));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001140 }
1141
1142 for (j = 0; j < vec_len (old_rt); j++)
1143 {
1144 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1145 rt->state = old_rt[j].state;
Dave Barach178cf492018-11-13 16:34:13 -05001146 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1147 VLIB_NODE_RUNTIME_DATA_SIZE);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001148 }
1149
1150 vec_free (old_rt);
1151
1152 /* re-clone input nodes */
1153 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
1154 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001155 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1156 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001157
1158 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1159 {
1160 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1161 rt->thread_index = vm_clone->thread_index;
1162 /* copy runtime_data, will be overwritten later for existing rt */
1163 if (n->runtime_data && n->runtime_data_bytes > 0)
Dave Barach178cf492018-11-13 16:34:13 -05001164 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1165 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1166 n->runtime_data_bytes));
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001167 }
1168
1169 for (j = 0; j < vec_len (old_rt); j++)
1170 {
1171 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1172 rt->state = old_rt[j].state;
Dave Barach178cf492018-11-13 16:34:13 -05001173 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1174 VLIB_NODE_RUNTIME_DATA_SIZE);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001175 }
1176
1177 vec_free (old_rt);
1178
Dave Barach53fe4a72019-01-26 09:50:26 -05001179 /* re-clone pre-input nodes */
1180 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT];
1181 nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT] =
1182 vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT],
1183 CLIB_CACHE_LINE_BYTES);
1184
1185 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1186 {
1187 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
1188 rt->thread_index = vm_clone->thread_index;
1189 /* copy runtime_data, will be overwritten later for existing rt */
1190 if (n->runtime_data && n->runtime_data_bytes > 0)
1191 clib_memcpy_fast (rt->runtime_data, n->runtime_data,
1192 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
1193 n->runtime_data_bytes));
1194 }
1195
1196 for (j = 0; j < vec_len (old_rt); j++)
1197 {
1198 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
1199 rt->state = old_rt[j].state;
1200 clib_memcpy_fast (rt->runtime_data, old_rt[j].runtime_data,
1201 VLIB_NODE_RUNTIME_DATA_SIZE);
1202 }
1203
1204 vec_free (old_rt);
1205
Damjan Marionbe3f4d52018-03-27 21:06:10 +02001206 nm_clone->processes = vec_dup_aligned (nm->processes,
1207 CLIB_CACHE_LINE_BYTES);
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001208}
1209
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001210void
1211vlib_worker_thread_node_runtime_update (void)
1212{
1213 /*
1214 * Make a note that we need to do a node runtime update
1215 * prior to releasing the barrier.
1216 */
1217 vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218}
1219
Pavel Kotucek1e765832016-09-23 08:54:14 +02001220u32
1221unformat_sched_policy (unformat_input_t * input, va_list * args)
1222{
1223 u32 *r = va_arg (*args, u32 *);
1224
1225 if (0);
1226#define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
1227 foreach_sched_policy
1228#undef _
1229 else
1230 return 0;
1231 return 1;
1232}
1233
Ed Warnickecb9cada2015-12-08 15:45:58 -07001234static clib_error_t *
1235cpu_config (vlib_main_t * vm, unformat_input_t * input)
1236{
1237 vlib_thread_registration_t *tr;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001238 uword *p;
1239 vlib_thread_main_t *tm = &vlib_thread_main;
1240 u8 *name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001241 uword *bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 u32 count;
1243
1244 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
Pavel Kotucek1e765832016-09-23 08:54:14 +02001245
Dave Barach9b8ffd92016-07-08 08:13:45 -04001246 tm->n_thread_stacks = 1; /* account for main thread */
Pavel Kotucek1e765832016-09-23 08:54:14 +02001247 tm->sched_policy = ~0;
1248 tm->sched_priority = ~0;
Damjan Marion858151f2018-07-11 10:51:00 +02001249 tm->main_lcore = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250
1251 tr = tm->next;
1252
1253 while (tr)
1254 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001255 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256 tr = tr->next;
1257 }
1258
Dave Barach9b8ffd92016-07-08 08:13:45 -04001259 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260 {
Damjan Marionbf741472016-06-13 22:49:44 +02001261 if (unformat (input, "use-pthreads"))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001262 tm->use_pthreads = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001264 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265 else if (unformat (input, "main-core %u", &tm->main_lcore))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001266 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001267 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001268 ;
Yi Hee4a9eb72018-07-17 14:18:41 +08001269 else if (unformat (input, "coremask-%s %U", &name,
1270 unformat_bitmap_mask, &bitmap) ||
1271 unformat (input, "corelist-%s %U", &name,
1272 unformat_bitmap_list, &bitmap))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001273 {
1274 p = hash_get_mem (tm->thread_registrations_by_name, name);
1275 if (p == 0)
1276 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001277
Dave Barach9b8ffd92016-07-08 08:13:45 -04001278 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001279
Dave Barach9b8ffd92016-07-08 08:13:45 -04001280 if (tr->use_pthreads)
1281 return clib_error_return (0,
1282 "corelist cannot be set for '%s' threads",
1283 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001284
Dave Barach9b8ffd92016-07-08 08:13:45 -04001285 tr->coremask = bitmap;
1286 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1287 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001288 else
1289 if (unformat
1290 (input, "scheduler-policy %U", unformat_sched_policy,
1291 &tm->sched_policy))
1292 ;
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001293 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
Pavel Kotucek1e765832016-09-23 08:54:14 +02001294 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001295 else if (unformat (input, "%s %u", &name, &count))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001296 {
1297 p = hash_get_mem (tm->thread_registrations_by_name, name);
1298 if (p == 0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001299 return clib_error_return (0, "no such thread type 3 '%s'", name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001300
1301 tr = (vlib_thread_registration_t *) p[0];
1302 if (tr->fixed_count)
1303 return clib_error_return
1304 (0, "number of %s threads not configurable", tr->name);
1305 tr->count = count;
1306 }
1307 else
1308 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309 }
1310
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001311 if (tm->sched_priority != ~0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001312 {
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001313 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001314 {
1315 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1316 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1317 if (tm->sched_priority > prio_max)
1318 tm->sched_priority = prio_max;
1319 if (tm->sched_priority < prio_min)
1320 tm->sched_priority = prio_min;
1321 }
1322 else
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001323 {
1324 return clib_error_return
1325 (0,
1326 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1327 tm->sched_priority);
1328 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001329 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001330 tr = tm->next;
1331
1332 if (!tm->thread_prefix)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001333 tm->thread_prefix = format (0, "vpp");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001334
1335 while (tr)
1336 {
1337 tm->n_thread_stacks += tr->count;
1338 tm->n_pthreads += tr->count * tr->use_pthreads;
Damjan Marion878c6092017-01-04 13:19:27 +01001339 tm->n_threads += tr->count * (tr->use_pthreads == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001340 tr = tr->next;
1341 }
1342
1343 return 0;
1344}
1345
1346VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1347
Ed Warnickecb9cada2015-12-08 15:45:58 -07001348void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -04001349void
1350vnet_main_fixup (vlib_fork_fixup_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001352}
1353
1354void
1355vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1356{
1357 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001358
1359 if (vlib_mains == 0)
1360 return;
1361
Damjan Marion586afd72017-04-05 19:18:20 +02001362 ASSERT (vlib_get_thread_index () == 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001363 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001364
1365 switch (which)
1366 {
1367 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1368 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1369 break;
1370
1371 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001372 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001373 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001374 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001375}
1376
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001377 /*
1378 * Enforce minimum open time to minimize packet loss due to Rx overflow,
1379 * based on a test based heuristic that barrier should be open for at least
1380 * 3 time as long as it is closed (with an upper bound of 1ms because by that
1381 * point it is probably too late to make a difference)
1382 */
1383
1384#ifndef BARRIER_MINIMUM_OPEN_LIMIT
1385#define BARRIER_MINIMUM_OPEN_LIMIT 0.001
1386#endif
1387
1388#ifndef BARRIER_MINIMUM_OPEN_FACTOR
1389#define BARRIER_MINIMUM_OPEN_FACTOR 3
1390#endif
1391
Dave Barach9b8ffd92016-07-08 08:13:45 -04001392void
Damjan Marion8343ee52019-02-26 17:15:48 +01001393vlib_worker_thread_barrier_sync_int (vlib_main_t * vm, const char *func_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001394{
1395 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001396 f64 now;
1397 f64 t_entry;
1398 f64 t_open;
1399 f64 t_closed;
Dave Barach9ae190e2019-04-23 10:07:24 -04001400 f64 max_vector_rate;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401 u32 count;
Dave Barach9ae190e2019-04-23 10:07:24 -04001402 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001403
Dave Barach80f54e22017-03-08 19:08:56 -05001404 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001405 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001406
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001407 ASSERT (vlib_get_thread_index () == 0);
1408
Damjan Marion8343ee52019-02-26 17:15:48 +01001409 vlib_worker_threads[0].barrier_caller = func_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410 count = vec_len (vlib_mains) - 1;
1411
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001412 /* Record entry relative to last close */
1413 now = vlib_time_now (vm);
1414 t_entry = now - vm->barrier_epoch;
1415
Ed Warnickecb9cada2015-12-08 15:45:58 -07001416 /* Tolerate recursive calls */
1417 if (++vlib_worker_threads[0].recursion_level > 1)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001418 {
1419 barrier_trace_sync_rec (t_entry);
1420 return;
1421 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001422
Dave Barach9ae190e2019-04-23 10:07:24 -04001423 /*
1424 * Need data to decide if we're working hard enough to honor
1425 * the barrier hold-down timer.
1426 */
1427 max_vector_rate = 0.0;
1428 for (i = 1; i < vec_len (vlib_mains); i++)
1429 max_vector_rate =
1430 clib_max (max_vector_rate,
1431 vlib_last_vectors_per_main_loop_as_f64 (vlib_mains[i]));
1432
Bud Grise42f20062016-03-16 13:09:46 -04001433 vlib_worker_threads[0].barrier_sync_count++;
1434
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001435 /* Enforce minimum barrier open time to minimize packet loss */
1436 ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001437
Dave Barach9ae190e2019-04-23 10:07:24 -04001438 /*
1439 * If any worker thread seems busy, which we define
1440 * as a vector rate above 10, we enforce the barrier hold-down timer
1441 */
1442 if (max_vector_rate > 10.0)
Dave Barach36feebb2018-09-07 11:12:27 -04001443 {
Dave Barach9ae190e2019-04-23 10:07:24 -04001444 while (1)
Dave Barach36feebb2018-09-07 11:12:27 -04001445 {
Dave Barach9ae190e2019-04-23 10:07:24 -04001446 now = vlib_time_now (vm);
1447 /* Barrier hold-down timer expired? */
1448 if (now >= vm->barrier_no_close_before)
1449 break;
1450 if ((vm->barrier_no_close_before - now)
1451 > (2.0 * BARRIER_MINIMUM_OPEN_LIMIT))
1452 {
1453 clib_warning
1454 ("clock change: would have waited for %.4f seconds",
1455 (vm->barrier_no_close_before - now));
1456 break;
1457 }
Dave Barach36feebb2018-09-07 11:12:27 -04001458 }
1459 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001460 /* Record time of closure */
1461 t_open = now - vm->barrier_epoch;
1462 vm->barrier_epoch = now;
1463
1464 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001465
1466 *vlib_worker_threads->wait_at_barrier = 1;
1467 while (*vlib_worker_threads->workers_at_barrier != count)
1468 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001469 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001470 {
1471 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1472 os_panic ();
1473 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001474 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001475
1476 t_closed = now - vm->barrier_epoch;
1477
1478 barrier_trace_sync (t_entry, t_open, t_closed);
1479
Ed Warnickecb9cada2015-12-08 15:45:58 -07001480}
1481
Dave Barach9b8ffd92016-07-08 08:13:45 -04001482void
1483vlib_worker_thread_barrier_release (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001484{
1485 f64 deadline;
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001486 f64 now;
1487 f64 minimum_open;
1488 f64 t_entry;
1489 f64 t_closed_total;
1490 f64 t_update_main = 0.0;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001491 int refork_needed = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001492
Dave Barach80f54e22017-03-08 19:08:56 -05001493 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001494 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001495
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001496 ASSERT (vlib_get_thread_index () == 0);
1497
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001498
1499 now = vlib_time_now (vm);
1500 t_entry = now - vm->barrier_epoch;
1501
Ed Warnickecb9cada2015-12-08 15:45:58 -07001502 if (--vlib_worker_threads[0].recursion_level > 0)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001503 {
1504 barrier_trace_release_rec (t_entry);
1505 return;
1506 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001508 /* Update (all) node runtimes before releasing the barrier, if needed */
1509 if (vm->need_vlib_worker_thread_node_runtime_update)
1510 {
Dave Barach1ddbc012018-06-13 09:26:05 -04001511 /*
1512 * Lock stat segment here, so we's safe when
1513 * rebuilding the stat segment node clones from the
1514 * stat thread...
1515 */
1516 vlib_stat_segment_lock ();
1517
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001518 /* Do stats elements on main thread */
1519 worker_thread_node_runtime_update_internal ();
1520 vm->need_vlib_worker_thread_node_runtime_update = 0;
1521
1522 /* Do per thread rebuilds in parallel */
1523 refork_needed = 1;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +00001524 clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
1525 (vec_len (vlib_mains) - 1));
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001526 now = vlib_time_now (vm);
1527 t_update_main = now - vm->barrier_epoch;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001528 }
1529
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001530 deadline = now + BARRIER_SYNC_TIMEOUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001531
Dave Baracha4324a92019-02-19 17:05:30 -05001532 /*
1533 * Note when we let go of the barrier.
1534 * Workers can use this to derive a reasonably accurate
1535 * time offset. See vlib_time_now(...)
1536 */
1537 vm->time_last_barrier_release = vlib_time_now (vm);
1538 CLIB_MEMORY_STORE_BARRIER ();
1539
Ed Warnickecb9cada2015-12-08 15:45:58 -07001540 *vlib_worker_threads->wait_at_barrier = 0;
1541
1542 while (*vlib_worker_threads->workers_at_barrier > 0)
1543 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001544 if ((now = vlib_time_now (vm)) > deadline)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001545 {
1546 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1547 os_panic ();
1548 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001549 }
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001550
1551 /* Wait for reforks before continuing */
1552 if (refork_needed)
1553 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001554 now = vlib_time_now (vm);
1555
1556 deadline = now + BARRIER_SYNC_TIMEOUT;
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001557
1558 while (*vlib_worker_threads->node_reforks_required > 0)
1559 {
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001560 if ((now = vlib_time_now (vm)) > deadline)
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001561 {
1562 fformat (stderr, "%s: worker thread refork deadlock\n",
1563 __FUNCTION__);
1564 os_panic ();
1565 }
1566 }
Dave Barach1ddbc012018-06-13 09:26:05 -04001567 vlib_stat_segment_unlock ();
Colin Tregenza Dancer21596182017-09-04 15:27:49 +01001568 }
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001569
1570 t_closed_total = now - vm->barrier_epoch;
1571
1572 minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR;
1573
1574 if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT)
1575 {
1576 minimum_open = BARRIER_MINIMUM_OPEN_LIMIT;
1577 }
1578
1579 vm->barrier_no_close_before = now + minimum_open;
1580
1581 /* Record barrier epoch (used to enforce minimum open time) */
1582 vm->barrier_epoch = now;
1583
1584 barrier_trace_release (t_entry, t_closed_total, t_update_main);
1585
Ed Warnickecb9cada2015-12-08 15:45:58 -07001586}
1587
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001588/*
1589 * Check the frame queue to see if any frames are available.
Dave Barach9b8ffd92016-07-08 08:13:45 -04001590 * If so, pull the packets off the frames and put them to
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001591 * the handoff node.
1592 */
Damjan Marione9d52d52017-03-09 15:42:26 +01001593int
1594vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001595{
Damjan Marion586afd72017-04-05 19:18:20 +02001596 u32 thread_id = vm->thread_index;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001597 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001598 vlib_frame_queue_elt_t *elt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001599 u32 *from, *to;
1600 vlib_frame_t *f;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001601 int msg_type;
1602 int processed = 0;
1603 u32 n_left_to_node;
1604 u32 vectors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001605
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001606 ASSERT (fq);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001607 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001608
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001609 if (PREDICT_FALSE (fqm->node_index == ~0))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001610 return 0;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001611 /*
1612 * Gather trace data for frame queues
1613 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001614 if (PREDICT_FALSE (fq->trace))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001615 {
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001616 frame_queue_trace_t *fqt;
1617 frame_queue_nelt_counter_t *fqh;
1618 u32 elix;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001619
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001620 fqt = &fqm->frame_queue_traces[thread_id];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001621
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001622 fqt->nelts = fq->nelts;
1623 fqt->head = fq->head;
1624 fqt->head_hint = fq->head_hint;
1625 fqt->tail = fq->tail;
1626 fqt->threshold = fq->vector_threshold;
1627 fqt->n_in_use = fqt->tail - fqt->head;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001628 if (fqt->n_in_use >= fqt->nelts)
1629 {
1630 // if beyond max then use max
1631 fqt->n_in_use = fqt->nelts - 1;
1632 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001633
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001634 /* Record the number of elements in use in the histogram */
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001635 fqh = &fqm->frame_queue_histogram[thread_id];
Dave Barach9b8ffd92016-07-08 08:13:45 -04001636 fqh->count[fqt->n_in_use]++;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001637
1638 /* Record a snapshot of the elements in use */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001639 for (elix = 0; elix < fqt->nelts; elix++)
1640 {
1641 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1642 if (1 || elt->valid)
1643 {
1644 fqt->n_vectors[elix] = elt->n_vectors;
1645 }
1646 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001647 fqt->written = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001648 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001649
1650 while (1)
1651 {
1652 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001653 {
1654 fq->head_hint = fq->head;
1655 return processed;
1656 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001657
Dave Barach9b8ffd92016-07-08 08:13:45 -04001658 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001659
1660 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001661 {
1662 fq->head_hint = fq->head;
1663 return processed;
1664 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001665
1666 from = elt->buffer_index;
1667 msg_type = elt->msg_type;
1668
1669 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1670 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1671
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001672 f = vlib_get_frame_to_node (vm, fqm->node_index);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001673
1674 to = vlib_frame_vector_args (f);
1675
1676 n_left_to_node = elt->n_vectors;
1677
1678 while (n_left_to_node >= 4)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001679 {
1680 to[0] = from[0];
1681 to[1] = from[1];
1682 to[2] = from[2];
1683 to[3] = from[3];
1684 to += 4;
1685 from += 4;
1686 n_left_to_node -= 4;
1687 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001688
1689 while (n_left_to_node > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001690 {
1691 to[0] = from[0];
1692 to++;
1693 from++;
1694 n_left_to_node--;
1695 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001696
1697 vectors += elt->n_vectors;
1698 f->n_vectors = elt->n_vectors;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001699 vlib_put_frame_to_node (vm, fqm->node_index, f);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001700
1701 elt->valid = 0;
1702 elt->n_vectors = 0;
1703 elt->msg_type = 0xfefefefe;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001704 CLIB_MEMORY_BARRIER ();
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001705 fq->head++;
1706 processed++;
1707
1708 /*
1709 * Limit the number of packets pushed into the graph
1710 */
1711 if (vectors >= fq->vector_threshold)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001712 {
1713 fq->head_hint = fq->head;
1714 return processed;
1715 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001716 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001717 ASSERT (0);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001718 return processed;
1719}
1720
Dave Barach9b8ffd92016-07-08 08:13:45 -04001721void
1722vlib_worker_thread_fn (void *arg)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001723{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001724 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
Damjan Marion878c6092017-01-04 13:19:27 +01001725 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -04001726 vlib_main_t *vm = vlib_get_main ();
Damjan Marione9f929b2017-03-16 11:32:09 +01001727 clib_error_t *e;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001728
Damjan Marion586afd72017-04-05 19:18:20 +02001729 ASSERT (vm->thread_index == vlib_get_thread_index ());
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001730
1731 vlib_worker_thread_init (w);
1732 clib_time_init (&vm->clib_time);
1733 clib_mem_set_heap (w->thread_mheap);
1734
Damjan Marioneb743fa2017-03-20 16:34:15 +01001735 /* Wait until the dpdk init sequence is complete */
1736 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1737 vlib_worker_thread_barrier_check ();
1738
Damjan Marione9f929b2017-03-16 11:32:09 +01001739 e = vlib_call_init_exit_functions
Dave Barachf8d50682019-05-14 18:01:44 -04001740 (vm, &vm->worker_init_function_registrations, 1 /* call_once */ );
Damjan Marione9f929b2017-03-16 11:32:09 +01001741 if (e)
1742 clib_error_report (e);
1743
Damjan Marione9d52d52017-03-09 15:42:26 +01001744 vlib_worker_loop (vm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001745}
1746
Dave Barach9b8ffd92016-07-08 08:13:45 -04001747/* *INDENT-OFF* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001748VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1749 .name = "workers",
1750 .short_name = "wk",
1751 .function = vlib_worker_thread_fn,
1752};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001753/* *INDENT-ON* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001754
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001755u32
1756vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1757{
1758 vlib_thread_main_t *tm = vlib_get_thread_main ();
1759 vlib_frame_queue_main_t *fqm;
1760 vlib_frame_queue_t *fq;
1761 int i;
1762
1763 if (frame_queue_nelts == 0)
1764 frame_queue_nelts = FRAME_QUEUE_NELTS;
1765
Damjan Marion78fd7e82018-07-20 18:47:05 +02001766 ASSERT (frame_queue_nelts >= 8);
1767
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001768 vec_add2 (tm->frame_queue_mains, fqm, 1);
1769
1770 fqm->node_index = node_index;
Damjan Marion78fd7e82018-07-20 18:47:05 +02001771 fqm->frame_queue_nelts = frame_queue_nelts;
1772 fqm->queue_hi_thresh = frame_queue_nelts - 2;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001773
1774 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
Damjan Marion78fd7e82018-07-20 18:47:05 +02001775 vec_validate (fqm->per_thread_data, tm->n_vlib_mains - 1);
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001776 _vec_len (fqm->vlib_frame_queues) = 0;
1777 for (i = 0; i < tm->n_vlib_mains; i++)
1778 {
Damjan Marion78fd7e82018-07-20 18:47:05 +02001779 vlib_frame_queue_per_thread_data_t *ptd;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001780 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1781 vec_add1 (fqm->vlib_frame_queues, fq);
Damjan Marion78fd7e82018-07-20 18:47:05 +02001782
1783 ptd = vec_elt_at_index (fqm->per_thread_data, i);
1784 vec_validate (ptd->handoff_queue_elt_by_thread_index,
1785 tm->n_vlib_mains - 1);
1786 vec_validate_init_empty (ptd->congested_handoff_queue_by_thread_index,
1787 tm->n_vlib_mains - 1,
1788 (vlib_frame_queue_t *) (~0));
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001789 }
1790
1791 return (fqm - tm->frame_queue_mains);
1792}
1793
Damjan Marion878c6092017-01-04 13:19:27 +01001794int
1795vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1796{
1797 vlib_thread_main_t *tm = vlib_get_thread_main ();
1798
1799 if (tm->extern_thread_mgmt)
1800 return -1;
1801
1802 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1803 tm->extern_thread_mgmt = 1;
1804 return 0;
1805}
1806
Dave Barach69128d02017-09-26 10:54:34 -04001807void
1808vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
1809 args)
1810{
1811 ASSERT (vlib_get_thread_index () == 0);
1812 vlib_process_signal_event (vlib_get_main (), args->node_index,
1813 args->type_opaque, args->data);
1814}
1815
1816void *rpc_call_main_thread_cb_fn;
1817
1818void
1819vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size)
1820{
1821 if (rpc_call_main_thread_cb_fn)
1822 {
1823 void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn;
1824 (*fp) (callback, args, arg_size);
1825 }
1826 else
1827 clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!");
1828}
1829
Dave Barach9b8ffd92016-07-08 08:13:45 -04001830clib_error_t *
1831threads_init (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001832{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001833 return 0;
1834}
1835
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001836VLIB_INIT_FUNCTION (threads_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001837
Dave Baracha4324a92019-02-19 17:05:30 -05001838
1839static clib_error_t *
1840show_clock_command_fn (vlib_main_t * vm,
1841 unformat_input_t * input, vlib_cli_command_t * cmd)
1842{
1843 int i;
1844 f64 now;
1845
1846 now = vlib_time_now (vm);
1847
1848 vlib_cli_output (vm, "Time now %.9f", now);
1849
1850 if (vec_len (vlib_mains) == 1)
1851 return 0;
1852
1853 vlib_cli_output (vm, "Time last barrier release %.9f",
1854 vm->time_last_barrier_release);
1855
1856 for (i = 1; i < vec_len (vlib_mains); i++)
1857 {
1858 if (vlib_mains[i] == 0)
1859 continue;
1860 vlib_cli_output (vm, "Thread %d offset %.9f error %.9f", i,
1861 vlib_mains[i]->time_offset,
1862 vm->time_last_barrier_release -
1863 vlib_mains[i]->time_last_barrier_release);
1864 }
1865 return 0;
1866}
1867
1868/* *INDENT-OFF* */
1869VLIB_CLI_COMMAND (f_command, static) =
1870{
1871 .path = "show clock",
1872 .short_help = "show clock",
1873 .function = show_clock_command_fn,
1874};
1875/* *INDENT-ON* */
1876
Dave Barach9b8ffd92016-07-08 08:13:45 -04001877/*
1878 * fd.io coding-style-patch-verification: ON
1879 *
1880 * Local Variables:
1881 * eval: (c-set-style "gnu")
1882 * End:
1883 */