blob: 4a111f8db52ab34338a9b3ed78b86254f5f7159d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
Christophe Fontainefef15b42016-04-09 12:38:49 +090015#define _GNU_SOURCE
Christophe Fontainefef15b42016-04-09 12:38:49 +090016
Ed Warnickecb9cada2015-12-08 15:45:58 -070017#include <signal.h>
18#include <math.h>
19#include <vppinfra/format.h>
20#include <vlib/vlib.h>
21
22#include <vlib/threads.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023#include <vlib/unix/cj.h>
24
Ed Warnickecb9cada2015-12-08 15:45:58 -070025DECLARE_CJ_GLOBAL_LOG;
26
27#define FRAME_QUEUE_NELTS 32
28
Dave Barach9b8ffd92016-07-08 08:13:45 -040029u32
30vl (void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -070031{
32 return vec_len (p);
33}
34
Damjan Marion6a7acc22016-12-19 16:28:36 +010035vlib_worker_thread_t *vlib_worker_threads;
Ed Warnickecb9cada2015-12-08 15:45:58 -070036vlib_thread_main_t vlib_thread_main;
37
Damjan Marion586afd72017-04-05 19:18:20 +020038__thread uword vlib_thread_index = 0;
39
Ed Warnickecb9cada2015-12-08 15:45:58 -070040uword
41os_get_cpu_number (void)
42{
Damjan Marion586afd72017-04-05 19:18:20 +020043 return vlib_thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070044}
45
Dave Barach01d86c72016-08-08 15:13:42 -040046uword
47os_get_ncpus (void)
48{
49 u32 len;
50
51 len = vec_len (vlib_thread_stacks);
52 if (len == 0)
53 return 1;
54 else
55 return len;
56}
57
Ed Warnickecb9cada2015-12-08 15:45:58 -070058void
59vlib_set_thread_name (char *name)
60{
61 int pthread_setname_np (pthread_t __target_thread, const char *__name);
Dave Barachb2a6e252016-07-27 10:00:58 -040062 int rv;
Dave Barach9b8ffd92016-07-08 08:13:45 -040063 pthread_t thread = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070064
Dave Barach9b8ffd92016-07-08 08:13:45 -040065 if (thread)
Dave Barachb2a6e252016-07-27 10:00:58 -040066 {
67 rv = pthread_setname_np (thread, name);
68 if (rv)
Ed Warnicke853e7202016-08-12 11:42:26 -070069 clib_warning ("pthread_setname_np returned %d", rv);
Dave Barachb2a6e252016-07-27 10:00:58 -040070 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070071}
72
Dave Barach9b8ffd92016-07-08 08:13:45 -040073static int
74sort_registrations_by_no_clone (void *a0, void *a1)
75{
76 vlib_thread_registration_t **tr0 = a0;
77 vlib_thread_registration_t **tr1 = a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
Dave Barach9b8ffd92016-07-08 08:13:45 -040079 return ((i32) ((*tr0)->no_data_structure_clone)
80 - ((i32) ((*tr1)->no_data_structure_clone)));
Ed Warnickecb9cada2015-12-08 15:45:58 -070081}
82
83static uword *
Dave Barach9b8ffd92016-07-08 08:13:45 -040084vlib_sysfs_list_to_bitmap (char *filename)
Ed Warnickecb9cada2015-12-08 15:45:58 -070085{
86 FILE *fp;
87 uword *r = 0;
88
89 fp = fopen (filename, "r");
90
91 if (fp != NULL)
92 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040093 u8 *buffer = 0;
94 vec_validate (buffer, 256 - 1);
95 if (fgets ((char *) buffer, 256, fp))
96 {
97 unformat_input_t in;
98 unformat_init_string (&in, (char *) buffer,
99 strlen ((char *) buffer));
Dave Barachb2a6e252016-07-27 10:00:58 -0400100 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
Ed Warnicke853e7202016-08-12 11:42:26 -0700101 clib_warning ("unformat_bitmap_list failed");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400102 unformat_free (&in);
103 }
104 vec_free (buffer);
105 fclose (fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 }
107 return r;
108}
109
110
111/* Called early in the init sequence */
112
113clib_error_t *
114vlib_thread_init (vlib_main_t * vm)
115{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400116 vlib_thread_main_t *tm = &vlib_thread_main;
117 vlib_worker_thread_t *w;
118 vlib_thread_registration_t *tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 u32 n_vlib_mains = 1;
120 u32 first_index = 1;
121 u32 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400122 uword *avail_cpu;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
124 /* get bitmaps of active cpu cores and sockets */
125 tm->cpu_core_bitmap =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400126 vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127 tm->cpu_socket_bitmap =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400128 vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Dave Barach9b8ffd92016-07-08 08:13:45 -0400130 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
132 /* skip cores */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400133 for (i = 0; i < tm->skip_cores; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400135 uword c = clib_bitmap_first_set (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 if (c == ~0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137 return clib_error_return (0, "no available cpus to skip");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
Dave Barach9b8ffd92016-07-08 08:13:45 -0400139 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 }
141
142 /* grab cpu for main thread */
143 if (!tm->main_lcore)
144 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400145 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
146 if (tm->main_lcore == (u8) ~ 0)
147 return clib_error_return (0, "no available cpus to be used for the"
148 " main thread");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 }
150 else
151 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
153 return clib_error_return (0, "cpu %u is not available to be used"
154 " for the main thread", tm->main_lcore);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
158 /* assume that there is socket 0 only if there is no data from sysfs */
159 if (!tm->cpu_socket_bitmap)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Christophe Fontainefef15b42016-04-09 12:38:49 +0900162 /* pin main thread to main_lcore */
Damjan Marion878c6092017-01-04 13:19:27 +0100163 if (tm->cb.vlib_thread_set_lcore_cb)
164 {
165 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
166 }
167 else
168 {
169 cpu_set_t cpuset;
170 CPU_ZERO (&cpuset);
171 CPU_SET (tm->main_lcore, &cpuset);
172 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
173 }
Christophe Fontainefef15b42016-04-09 12:38:49 +0900174
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 /* as many threads as stacks... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
177 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
179 /* Preallocate thread 0 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400180 _vec_len (vlib_worker_threads) = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 w = vlib_worker_threads;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400182 w->thread_mheap = clib_mem_get_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 w->thread_stack = vlib_thread_stacks[0];
Pavel Kotucek98765202016-10-07 08:37:28 +0200184 w->lcore_id = tm->main_lcore;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400185 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200186 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 tm->n_vlib_mains = 1;
188
Pavel Kotucek1e765832016-09-23 08:54:14 +0200189 if (tm->sched_policy != ~0)
190 {
191 struct sched_param sched_param;
192 if (!sched_getparam (w->lwp, &sched_param))
193 {
194 if (tm->sched_priority != ~0)
195 sched_param.sched_priority = tm->sched_priority;
196 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
197 }
198 }
199
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200 /* assign threads to cores and set n_vlib_mains */
201 tr = tm->next;
202
203 while (tr)
204 {
205 vec_add1 (tm->registrations, tr);
206 tr = tr->next;
207 }
208
Dave Barach9b8ffd92016-07-08 08:13:45 -0400209 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
211 for (i = 0; i < vec_len (tm->registrations); i++)
212 {
213 int j;
214 tr = tm->registrations[i];
215 tr->first_index = first_index;
216 first_index += tr->count;
217 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
218
219 /* construct coremask */
220 if (tr->use_pthreads || !tr->count)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400221 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
223 if (tr->coremask)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400224 {
225 uword c;
226 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227 clib_bitmap_foreach (c, tr->coremask, ({
228 if (clib_bitmap_get(avail_cpu, c) == 0)
229 return clib_error_return (0, "cpu %u is not available to be used"
230 " for the '%s' thread",c, tr->name);
231
232 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
233 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400234/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
Dave Barach9b8ffd92016-07-08 08:13:45 -0400236 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400238 {
239 for (j = 0; j < tr->count; j++)
240 {
241 uword c = clib_bitmap_first_set (avail_cpu);
242 if (c == ~0)
243 return clib_error_return (0,
244 "no available cpus to be used for"
245 " the '%s' thread", tr->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Dave Barach9b8ffd92016-07-08 08:13:45 -0400247 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
248 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
249 }
250 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251 }
252
Dave Barach9b8ffd92016-07-08 08:13:45 -0400253 clib_bitmap_free (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
255 tm->n_vlib_mains = n_vlib_mains;
256
Dave Barach9b8ffd92016-07-08 08:13:45 -0400257 vec_validate_aligned (vlib_worker_threads, first_index - 1,
258 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 return 0;
261}
262
Dave Barach9b8ffd92016-07-08 08:13:45 -0400263vlib_frame_queue_t *
264vlib_frame_queue_alloc (int nelts)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400266 vlib_frame_queue_t *fq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
Dave Barach9b8ffd92016-07-08 08:13:45 -0400268 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269 memset (fq, 0, sizeof (*fq));
270 fq->nelts = nelts;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400271 fq->vector_threshold = 128; // packets
272 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
274 if (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400275 {
276 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
277 fformat (stderr, "WARNING: fq->tail unaligned\n");
278 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
279 fformat (stderr, "WARNING: fq->head unaligned\n");
280 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
281 fformat (stderr, "WARNING: fq->elts unaligned\n");
282
283 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
284 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
285 sizeof (fq->elts[0]));
286 if (nelts & (nelts - 1))
287 {
288 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
289 abort ();
290 }
291 }
292
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 return (fq);
294}
295
296void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400297void
298vl_msg_api_handler_no_free (void *v)
299{
300}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
302/* Turned off, save as reference material... */
303#if 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400304static inline int
305vlib_frame_queue_dequeue_internal (int thread_id,
306 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307{
308 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
309 vlib_frame_queue_elt_t *elt;
310 vlib_frame_t *f;
311 vlib_pending_frame_t *p;
312 vlib_node_runtime_t *r;
313 u32 node_runtime_index;
314 int msg_type;
315 u64 before;
316 int processed = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400317
318 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
320 while (1)
321 {
322 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400323 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Dave Barach9b8ffd92016-07-08 08:13:45 -0400325 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
327 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Dave Barach9b8ffd92016-07-08 08:13:45 -0400330 before = clib_cpu_time_now ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
332 f = elt->frame;
333 node_runtime_index = elt->node_runtime_index;
334 msg_type = elt->msg_type;
335
336 switch (msg_type)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400337 {
338 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
339 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
340 /* note fallthrough... */
341 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
342 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
343 node_runtime_index);
344 vlib_frame_free (vm, r, f);
345 break;
346 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
347 vec_add2 (vm->node_main.pending_frames, p, 1);
348 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
349 p->node_runtime_index = elt->node_runtime_index;
350 p->frame_index = vlib_frame_index (vm, f);
351 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
352 fq->dequeue_vectors += (u64) f->n_vectors;
353 break;
354 case VLIB_FRAME_QUEUE_ELT_API_MSG:
355 vl_msg_api_handler_no_free (f);
356 break;
357 default:
358 clib_warning ("bogus frame queue message, type %d", msg_type);
359 break;
360 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 elt->valid = 0;
362 fq->dequeues++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400363 fq->dequeue_ticks += clib_cpu_time_now () - before;
364 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365 fq->head++;
366 processed++;
367 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400368 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 return processed;
370}
371
Dave Barach9b8ffd92016-07-08 08:13:45 -0400372int
373vlib_frame_queue_dequeue (int thread_id,
374 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375{
376 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
377}
378
Dave Barach9b8ffd92016-07-08 08:13:45 -0400379int
380vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
381 u32 frame_queue_index, vlib_frame_t * frame,
382 vlib_frame_queue_msg_type_t type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383{
384 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
385 vlib_frame_queue_elt_t *elt;
386 u32 save_count;
387 u64 new_tail;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400388 u64 before = clib_cpu_time_now ();
389
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390 ASSERT (fq);
391
392 new_tail = __sync_add_and_fetch (&fq->tail, 1);
393
394 /* Wait until a ring slot is available */
395 while (new_tail >= fq->head + fq->nelts)
396 {
397 f64 b4 = vlib_time_now_ticks (vm, before);
398 vlib_worker_thread_barrier_check (vm, b4);
399 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
Damjan Marion586afd72017-04-05 19:18:20 +0200400 // vlib_frame_queue_dequeue (vm->thread_index, vm, nm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401 }
402
Dave Barach9b8ffd92016-07-08 08:13:45 -0400403 elt = fq->elts + (new_tail & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404
405 /* this would be very bad... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400406 while (elt->valid)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 {
408 }
409
410 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
411 save_count = frame->n_vectors;
412
413 elt->frame = frame;
414 elt->node_runtime_index = node_runtime_index;
415 elt->msg_type = type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400416 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 elt->valid = 1;
418
419 return save_count;
420}
421#endif /* 0 */
422
423/* To be called by vlib worker threads upon startup */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400424void
425vlib_worker_thread_init (vlib_worker_thread_t * w)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400427 vlib_thread_main_t *tm = vlib_get_thread_main ();
428
Dave Barachfdf49442016-12-20 12:48:14 -0500429 /*
430 * Note: disabling signals in worker threads as follows
431 * prevents the api post-mortem dump scheme from working
432 * {
433 * sigset_t s;
434 * sigfillset (&s);
435 * pthread_sigmask (SIG_SETMASK, &s, 0);
436 * }
437 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
439 clib_mem_set_heap (w->thread_mheap);
440
Dave Barach9b8ffd92016-07-08 08:13:45 -0400441 if (vec_len (tm->thread_prefix) && w->registration->short_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400443 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
444 w->registration->short_name, w->instance_id, '\0');
445 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 }
447
448 if (!w->registration->use_pthreads)
449 {
450
451 /* Initial barrier sync, for both worker and i/o threads */
452 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
453
454 while (*vlib_worker_threads->wait_at_barrier)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
457 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
458 }
459}
460
Dave Barach9b8ffd92016-07-08 08:13:45 -0400461void *
462vlib_worker_thread_bootstrap_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463{
464 void *rv;
465 vlib_worker_thread_t *w = arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400466
467 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200468 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
Damjan Marion586afd72017-04-05 19:18:20 +0200470 vlib_thread_index = w - vlib_worker_threads;
471
Dave Barach9b8ffd92016-07-08 08:13:45 -0400472 rv = (void *) clib_calljmp
473 ((uword (*)(uword)) w->thread_function,
474 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475 /* NOTREACHED, we hope */
476 return rv;
477}
478
Damjan Marion878c6092017-01-04 13:19:27 +0100479static clib_error_t *
480vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481{
Damjan Marion878c6092017-01-04 13:19:27 +0100482 vlib_thread_main_t *tm = &vlib_thread_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400483 void *(*fp_arg) (void *) = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484
Pavel Kotucek98765202016-10-07 08:37:28 +0200485 w->lcore_id = lcore_id;
Damjan Marion878c6092017-01-04 13:19:27 +0100486 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
487 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400489 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400490 pthread_t worker;
491 cpu_set_t cpuset;
492 CPU_ZERO (&cpuset);
493 CPU_SET (lcore_id, &cpuset);
Christophe Fontainefef15b42016-04-09 12:38:49 +0900494
Damjan Marion878c6092017-01-04 13:19:27 +0100495 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
496 return clib_error_return_unix (0, "pthread_create");
497
498 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
499 return clib_error_return_unix (0, "pthread_setaffinity_np");
500
501 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400502 }
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{
508 int i, j;
509 vlib_worker_thread_t *w;
510 vlib_main_t *vm_clone;
511 void *oldheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400512 vlib_thread_main_t *tm = &vlib_thread_main;
513 vlib_thread_registration_t *tr;
514 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 u32 n_vlib_mains = tm->n_vlib_mains;
516 u32 worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400517 u8 *main_heap = clib_mem_get_per_cpu_heap ();
518 mheap_t *main_heap_header = mheap_header (main_heap);
519
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 vec_reset_length (vlib_worker_threads);
521
522 /* Set up the main thread */
523 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
Dave Barach3e7deb12016-02-05 16:29:53 -0500524 w->elog_track.name = "main thread";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 elog_track_register (&vm->elog_main, &w->elog_track);
526
Dave Barach9b8ffd92016-07-08 08:13:45 -0400527 if (vec_len (tm->thread_prefix))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
530 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 }
532
Dave Barach9b8ffd92016-07-08 08:13:45 -0400533 /*
Dave Barach848191d2016-04-28 16:24:15 -0400534 * Truth of the matter: we always use at least two
Dave Barach9b8ffd92016-07-08 08:13:45 -0400535 * threads. So, make the main heap thread-safe
Dave Barach848191d2016-04-28 16:24:15 -0400536 * and make the event log thread-safe.
537 */
538 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400539 vm->elog_main.lock =
540 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barach848191d2016-04-28 16:24:15 -0400541 vm->elog_main.lock[0] = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400542
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 if (n_vlib_mains > 1)
544 {
Dave Barach80f54e22017-03-08 19:08:56 -0500545 /* Replace hand-crafted length-1 vector with a real vector */
546 vlib_mains = 0;
547
548 vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1,
549 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 _vec_len (vlib_mains) = 0;
Dave Barach80f54e22017-03-08 19:08:56 -0500551 vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552
Dave Barach9b8ffd92016-07-08 08:13:45 -0400553 vlib_worker_threads->wait_at_barrier =
554 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555 vlib_worker_threads->workers_at_barrier =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400556 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557
558 /* Ask for an initial barrier sync */
559 *vlib_worker_threads->workers_at_barrier = 0;
560 *vlib_worker_threads->wait_at_barrier = 1;
561
562 worker_thread_index = 1;
563
Dave Barach9b8ffd92016-07-08 08:13:45 -0400564 for (i = 0; i < vec_len (tm->registrations); i++)
565 {
566 vlib_node_main_t *nm, *nm_clone;
567 vlib_buffer_main_t *bm_clone;
568 vlib_buffer_free_list_t *fl_clone, *fl_orig;
569 vlib_buffer_free_list_t *orig_freelist_pool;
570 int k;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571
Dave Barach9b8ffd92016-07-08 08:13:45 -0400572 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573
Dave Barach9b8ffd92016-07-08 08:13:45 -0400574 if (tr->count == 0)
575 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
Dave Barach9b8ffd92016-07-08 08:13:45 -0400577 for (k = 0; k < tr->count; k++)
578 {
579 vec_add2 (vlib_worker_threads, w, 1);
580 if (tr->mheap_size)
581 w->thread_mheap =
582 mheap_alloc (0 /* use VM */ , tr->mheap_size);
583 else
584 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200585
586 w->thread_stack =
587 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400588 w->thread_function = tr->function;
589 w->thread_function_arg = w;
590 w->instance_id = k;
591 w->registration = tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
Dave Barach9b8ffd92016-07-08 08:13:45 -0400593 w->elog_track.name =
594 (char *) format (0, "%s %d", tr->name, k + 1);
595 vec_add1 (w->elog_track.name, 0);
596 elog_track_register (&vm->elog_main, &w->elog_track);
Bud Grise68adab92016-02-12 10:36:11 -0500597
Dave Barach9b8ffd92016-07-08 08:13:45 -0400598 if (tr->no_data_structure_clone)
599 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600
Dave Barach9b8ffd92016-07-08 08:13:45 -0400601 /* Fork vlib_global_main et al. Look for bugs here */
602 oldheap = clib_mem_set_heap (w->thread_mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
Dave Barach9b8ffd92016-07-08 08:13:45 -0400604 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
605 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606
Damjan Marion586afd72017-04-05 19:18:20 +0200607 vm_clone->thread_index = worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400608 vm_clone->heap_base = w->thread_mheap;
609 vm_clone->mbuf_alloc_list = 0;
Damjan Marione9f929b2017-03-16 11:32:09 +0100610 vm_clone->init_functions_called =
611 hash_create (0, /* value bytes */ 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400612 memset (&vm_clone->random_buffer, 0,
613 sizeof (vm_clone->random_buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
Dave Barach9b8ffd92016-07-08 08:13:45 -0400615 nm = &vlib_mains[0]->node_main;
616 nm_clone = &vm_clone->node_main;
617 /* fork next frames array, preserving node runtime indices */
618 nm_clone->next_frames = vec_dup (nm->next_frames);
619 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
620 {
621 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
622 u32 save_node_runtime_index;
623 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
Dave Barach9b8ffd92016-07-08 08:13:45 -0400625 save_node_runtime_index = nf->node_runtime_index;
626 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
627 vlib_next_frame_init (nf);
628 nf->node_runtime_index = save_node_runtime_index;
629 nf->flags = save_flags;
630 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
Dave Barach9b8ffd92016-07-08 08:13:45 -0400632 /* fork the frame dispatch queue */
633 nm_clone->pending_frames = 0;
634 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
635 _vec_len (nm_clone->pending_frames) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636
Dave Barach9b8ffd92016-07-08 08:13:45 -0400637 /* fork nodes */
638 nm_clone->nodes = 0;
639 for (j = 0; j < vec_len (nm->nodes); j++)
640 {
641 vlib_node_t *n;
642 n = clib_mem_alloc_no_fail (sizeof (*n));
643 clib_memcpy (n, nm->nodes[j], sizeof (*n));
644 /* none of the copied nodes have enqueue rights given out */
645 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
646 memset (&n->stats_total, 0, sizeof (n->stats_total));
647 memset (&n->stats_last_clear, 0,
648 sizeof (n->stats_last_clear));
649 vec_add1 (nm_clone->nodes, n);
650 }
651 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
652 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
Damjan Marione9f929b2017-03-16 11:32:09 +0100653 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
654 {
655 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200656 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100657 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100658 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100659 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100660 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
661 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100662 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663
Dave Barach9b8ffd92016-07-08 08:13:45 -0400664 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
665 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
666 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
Damjan Marione9f929b2017-03-16 11:32:09 +0100667 {
668 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200669 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100670 /* copy initial runtime_data from node */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100671 if (n->runtime_data && n->runtime_data_bytes > 0)
Damjan Marione9f929b2017-03-16 11:32:09 +0100672 clib_memcpy (rt->runtime_data, n->runtime_data,
Damjan Marionb6f93a12017-03-16 17:46:41 +0100673 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
674 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100675 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Dave Barach9b8ffd92016-07-08 08:13:45 -0400677 nm_clone->processes = vec_dup (nm->processes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
Dave Barach9b8ffd92016-07-08 08:13:45 -0400679 /* zap the (per worker) frame freelists, etc */
680 nm_clone->frame_sizes = 0;
681 nm_clone->frame_size_hash = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682
Dave Barach9b8ffd92016-07-08 08:13:45 -0400683 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100684
Dave Barach9b8ffd92016-07-08 08:13:45 -0400685 clib_mem_set_heap (oldheap);
Dave Barach80f54e22017-03-08 19:08:56 -0500686 vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687
Dave Barach9b8ffd92016-07-08 08:13:45 -0400688 vm_clone->error_main.counters =
689 vec_dup (vlib_mains[0]->error_main.counters);
690 vm_clone->error_main.counters_last_clear =
691 vec_dup (vlib_mains[0]->error_main.counters_last_clear);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692
Dave Barach9b8ffd92016-07-08 08:13:45 -0400693 /* Fork the vlib_buffer_main_t free lists, etc. */
694 bm_clone = vec_dup (vm_clone->buffer_main);
695 vm_clone->buffer_main = bm_clone;
696
697 orig_freelist_pool = bm_clone->buffer_free_list_pool;
698 bm_clone->buffer_free_list_pool = 0;
699
700 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701 pool_foreach (fl_orig, orig_freelist_pool,
702 ({
Dave Barach9b8ffd92016-07-08 08:13:45 -0400703 pool_get_aligned (bm_clone->buffer_free_list_pool,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 fl_clone, CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400705 ASSERT (fl_orig - orig_freelist_pool
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706 == fl_clone - bm_clone->buffer_free_list_pool);
707
708 fl_clone[0] = fl_orig[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100709 fl_clone->buffers = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 fl_clone->n_alloc = 0;
711 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400712/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713
Dave Barach9b8ffd92016-07-08 08:13:45 -0400714 worker_thread_index++;
715 }
716 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717 }
718 else
719 {
720 /* only have non-data-structure copy threads to create... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400721 for (i = 0; i < vec_len (tm->registrations); i++)
722 {
723 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724
Dave Barach9b8ffd92016-07-08 08:13:45 -0400725 for (j = 0; j < tr->count; j++)
726 {
727 vec_add2 (vlib_worker_threads, w, 1);
728 if (tr->mheap_size)
729 w->thread_mheap =
730 mheap_alloc (0 /* use VM */ , tr->mheap_size);
731 else
732 w->thread_mheap = main_heap;
Damjan Marion586afd72017-04-05 19:18:20 +0200733 w->thread_stack =
734 vlib_thread_stack_init (w - vlib_worker_threads);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400735 w->thread_function = tr->function;
736 w->thread_function_arg = w;
737 w->instance_id = j;
738 w->elog_track.name =
739 (char *) format (0, "%s %d", tr->name, j + 1);
740 w->registration = tr;
741 vec_add1 (w->elog_track.name, 0);
742 elog_track_register (&vm->elog_main, &w->elog_track);
743 }
744 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 }
746
747 worker_thread_index = 1;
748
749 for (i = 0; i < vec_len (tm->registrations); i++)
750 {
Damjan Marion878c6092017-01-04 13:19:27 +0100751 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752 int j;
753
754 tr = tm->registrations[i];
755
756 if (tr->use_pthreads || tm->use_pthreads)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400757 {
758 for (j = 0; j < tr->count; j++)
759 {
760 w = vlib_worker_threads + worker_thread_index++;
Damjan Marion878c6092017-01-04 13:19:27 +0100761 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
762 w, 0);
763 if (err)
764 clib_error_report (err);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400765 }
766 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400768 {
769 uword c;
Damjan Marion878c6092017-01-04 13:19:27 +0100770 /* *INDENT-OFF* */
771 clib_bitmap_foreach (c, tr->coremask, ({
772 w = vlib_worker_threads + worker_thread_index++;
773 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
774 w, c);
775 if (err)
776 clib_error_report (err);
777 }));
778 /* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400779 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400781 vlib_worker_thread_barrier_sync (vm);
782 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783 return 0;
784}
785
786VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
787
Dave Barach9b8ffd92016-07-08 08:13:45 -0400788void
789vlib_worker_thread_node_runtime_update (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790{
791 int i, j;
792 vlib_worker_thread_t *w;
793 vlib_main_t *vm;
794 vlib_node_main_t *nm, *nm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400795 vlib_node_t **old_nodes_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 vlib_main_t *vm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400797 vlib_node_runtime_t *rt, *old_rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798 void *oldheap;
799 never_inline void
800 vlib_node_runtime_sync_stats (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400801 vlib_node_runtime_t * r,
802 uword n_calls,
803 uword n_vectors, uword n_clocks);
804
Damjan Marion586afd72017-04-05 19:18:20 +0200805 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806
Dave Barach80f54e22017-03-08 19:08:56 -0500807 if (vec_len (vlib_mains) == 1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808 return;
809
810 vm = vlib_mains[0];
811 nm = &vm->node_main;
812
Damjan Marion586afd72017-04-05 19:18:20 +0200813 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
815
Dave Barach9b8ffd92016-07-08 08:13:45 -0400816 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817 * Scrape all runtime stats, so we don't lose node runtime(s) with
818 * pending counts, or throw away worker / io thread counts.
819 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400820 for (j = 0; j < vec_len (nm->nodes); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400822 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823 n = nm->nodes[j];
824 vlib_node_sync_stats (vm, n);
825 }
826
827 for (i = 1; i < vec_len (vlib_mains); i++)
828 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400829 vlib_node_t *n;
830
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831 vm_clone = vlib_mains[i];
832 nm_clone = &vm_clone->node_main;
833
Dave Barach9b8ffd92016-07-08 08:13:45 -0400834 for (j = 0; j < vec_len (nm_clone->nodes); j++)
835 {
836 n = nm_clone->nodes[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837
Dave Barach9b8ffd92016-07-08 08:13:45 -0400838 rt = vlib_node_get_runtime (vm_clone, n->index);
839 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
840 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841 }
842
843 for (i = 1; i < vec_len (vlib_mains); i++)
844 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400845 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846 w = vlib_worker_threads + i;
847 oldheap = clib_mem_set_heap (w->thread_mheap);
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100848
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849 vm_clone = vlib_mains[i];
850
851 /* Re-clone error heap */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400852 u64 *old_counters = vm_clone->error_main.counters;
853 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
854 clib_memcpy (&vm_clone->error_main, &vm->error_main,
855 sizeof (vm->error_main));
856 j = vec_len (vm->error_main.counters) - 1;
857 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
858 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100859 vm_clone->error_main.counters = old_counters;
860 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861
862 nm_clone = &vm_clone->node_main;
863 vec_free (nm_clone->next_frames);
864 nm_clone->next_frames = vec_dup (nm->next_frames);
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100865
Ed Warnickecb9cada2015-12-08 15:45:58 -0700866 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400867 {
868 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
869 u32 save_node_runtime_index;
870 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700871
Dave Barach9b8ffd92016-07-08 08:13:45 -0400872 save_node_runtime_index = nf->node_runtime_index;
873 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
874 vlib_next_frame_init (nf);
875 nf->node_runtime_index = save_node_runtime_index;
876 nf->flags = save_flags;
877 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878
879 old_nodes_clone = nm_clone->nodes;
880 nm_clone->nodes = 0;
881
882 /* re-fork nodes */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400883 for (j = 0; j < vec_len (nm->nodes); j++)
884 {
885 vlib_node_t *old_n_clone;
886 vlib_node_t *new_n, *new_n_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887
Dave Barach9b8ffd92016-07-08 08:13:45 -0400888 new_n = nm->nodes[j];
889 old_n_clone = old_nodes_clone[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890
Dave Barach9b8ffd92016-07-08 08:13:45 -0400891 new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
892 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
893 /* none of the copied nodes have enqueue rights given out */
894 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895
Dave Barach9b8ffd92016-07-08 08:13:45 -0400896 if (j >= vec_len (old_nodes_clone))
897 {
898 /* new node, set to zero */
899 memset (&new_n_clone->stats_total, 0,
900 sizeof (new_n_clone->stats_total));
901 memset (&new_n_clone->stats_last_clear, 0,
902 sizeof (new_n_clone->stats_last_clear));
903 }
904 else
905 {
906 /* Copy stats if the old data is valid */
907 clib_memcpy (&new_n_clone->stats_total,
908 &old_n_clone->stats_total,
909 sizeof (new_n_clone->stats_total));
910 clib_memcpy (&new_n_clone->stats_last_clear,
911 &old_n_clone->stats_last_clear,
912 sizeof (new_n_clone->stats_last_clear));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913
Dave Barach9b8ffd92016-07-08 08:13:45 -0400914 /* keep previous node state */
915 new_n_clone->state = old_n_clone->state;
916 }
917 vec_add1 (nm_clone->nodes, new_n_clone);
918 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 /* Free the old node clone */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400920 for (j = 0; j < vec_len (old_nodes_clone); j++)
921 clib_mem_free (old_nodes_clone[j]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922 vec_free (old_nodes_clone);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400923
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924
Damjan Marione9f929b2017-03-16 11:32:09 +0100925 /* re-clone internal nodes */
926 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400928 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929
Damjan Marione9f929b2017-03-16 11:32:09 +0100930 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
Dave Barach9b8ffd92016-07-08 08:13:45 -0400931 {
Damjan Marione9f929b2017-03-16 11:32:09 +0100932 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200933 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100934 /* copy runtime_data, will be overwritten later for existing rt */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100935 if (n->runtime_data && n->runtime_data_bytes > 0)
936 clib_memcpy (rt->runtime_data, n->runtime_data,
937 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
938 n->runtime_data_bytes));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400939 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940
Dave Barach9b8ffd92016-07-08 08:13:45 -0400941 for (j = 0; j < vec_len (old_rt); j++)
942 {
943 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
944 rt->state = old_rt[j].state;
Damjan Marione9f929b2017-03-16 11:32:09 +0100945 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
946 VLIB_NODE_RUNTIME_DATA_SIZE);
947 }
948
949 vec_free (old_rt);
950
951 /* re-clone input nodes */
952 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
953 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
954 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
955
956 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
957 {
958 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
Damjan Marion586afd72017-04-05 19:18:20 +0200959 rt->thread_index = vm_clone->thread_index;
Damjan Marione9f929b2017-03-16 11:32:09 +0100960 /* copy runtime_data, will be overwritten later for existing rt */
Damjan Marionb6f93a12017-03-16 17:46:41 +0100961 if (n->runtime_data && n->runtime_data_bytes > 0)
962 clib_memcpy (rt->runtime_data, n->runtime_data,
963 clib_min (VLIB_NODE_RUNTIME_DATA_SIZE,
964 n->runtime_data_bytes));
Damjan Marione9f929b2017-03-16 11:32:09 +0100965 }
966
967 for (j = 0; j < vec_len (old_rt); j++)
968 {
969 rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index);
970 rt->state = old_rt[j].state;
971 clib_memcpy (rt->runtime_data, old_rt[j].runtime_data,
972 VLIB_NODE_RUNTIME_DATA_SIZE);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400973 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974
Dave Barach9b8ffd92016-07-08 08:13:45 -0400975 vec_free (old_rt);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976
977 nm_clone->processes = vec_dup (nm->processes);
978
979 clib_mem_set_heap (oldheap);
980
981 // vnet_main_fork_fixup (i);
982 }
983}
984
Pavel Kotucek1e765832016-09-23 08:54:14 +0200985u32
986unformat_sched_policy (unformat_input_t * input, va_list * args)
987{
988 u32 *r = va_arg (*args, u32 *);
989
990 if (0);
991#define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
992 foreach_sched_policy
993#undef _
994 else
995 return 0;
996 return 1;
997}
998
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999static clib_error_t *
1000cpu_config (vlib_main_t * vm, unformat_input_t * input)
1001{
1002 vlib_thread_registration_t *tr;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001003 uword *p;
1004 vlib_thread_main_t *tm = &vlib_thread_main;
1005 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 u64 coremask;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001007 uword *bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008 u32 count;
1009
1010 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
Pavel Kotucek1e765832016-09-23 08:54:14 +02001011
Dave Barach9b8ffd92016-07-08 08:13:45 -04001012 tm->n_thread_stacks = 1; /* account for main thread */
Pavel Kotucek1e765832016-09-23 08:54:14 +02001013 tm->sched_policy = ~0;
1014 tm->sched_priority = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015
1016 tr = tm->next;
1017
1018 while (tr)
1019 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001020 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021 tr = tr->next;
1022 }
1023
Dave Barach9b8ffd92016-07-08 08:13:45 -04001024 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 {
Damjan Marionbf741472016-06-13 22:49:44 +02001026 if (unformat (input, "use-pthreads"))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001027 tm->use_pthreads = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001029 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030 else if (unformat (input, "main-core %u", &tm->main_lcore))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001031 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001033 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001035 {
1036 p = hash_get_mem (tm->thread_registrations_by_name, name);
1037 if (p == 0)
1038 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039
Dave Barach9b8ffd92016-07-08 08:13:45 -04001040 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041
Dave Barach9b8ffd92016-07-08 08:13:45 -04001042 if (tr->use_pthreads)
1043 return clib_error_return (0,
1044 "coremask cannot be set for '%s' threads",
1045 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046
Dave Barach9b8ffd92016-07-08 08:13:45 -04001047 tr->coremask = clib_bitmap_set_multiple
1048 (tr->coremask, 0, coremask, BITS (coremask));
1049 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1050 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001052 &bitmap))
1053 {
1054 p = hash_get_mem (tm->thread_registrations_by_name, name);
1055 if (p == 0)
1056 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057
Dave Barach9b8ffd92016-07-08 08:13:45 -04001058 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059
Dave Barach9b8ffd92016-07-08 08:13:45 -04001060 if (tr->use_pthreads)
1061 return clib_error_return (0,
1062 "corelist cannot be set for '%s' threads",
1063 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064
Dave Barach9b8ffd92016-07-08 08:13:45 -04001065 tr->coremask = bitmap;
1066 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1067 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001068 else
1069 if (unformat
1070 (input, "scheduler-policy %U", unformat_sched_policy,
1071 &tm->sched_policy))
1072 ;
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001073 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
Pavel Kotucek1e765832016-09-23 08:54:14 +02001074 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075 else if (unformat (input, "%s %u", &name, &count))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001076 {
1077 p = hash_get_mem (tm->thread_registrations_by_name, name);
1078 if (p == 0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001079 return clib_error_return (0, "no such thread type 3 '%s'", name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001080
1081 tr = (vlib_thread_registration_t *) p[0];
1082 if (tr->fixed_count)
1083 return clib_error_return
1084 (0, "number of %s threads not configurable", tr->name);
1085 tr->count = count;
1086 }
1087 else
1088 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089 }
1090
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001091 if (tm->sched_priority != ~0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001092 {
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001093 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001094 {
1095 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1096 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1097 if (tm->sched_priority > prio_max)
1098 tm->sched_priority = prio_max;
1099 if (tm->sched_priority < prio_min)
1100 tm->sched_priority = prio_min;
1101 }
1102 else
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001103 {
1104 return clib_error_return
1105 (0,
1106 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1107 tm->sched_priority);
1108 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001109 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001110 tr = tm->next;
1111
1112 if (!tm->thread_prefix)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001113 tm->thread_prefix = format (0, "vpp");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114
1115 while (tr)
1116 {
1117 tm->n_thread_stacks += tr->count;
1118 tm->n_pthreads += tr->count * tr->use_pthreads;
Damjan Marion878c6092017-01-04 13:19:27 +01001119 tm->n_threads += tr->count * (tr->use_pthreads == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120 tr = tr->next;
1121 }
1122
1123 return 0;
1124}
1125
1126VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1127
Christophe Fontainefef15b42016-04-09 12:38:49 +09001128#if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001129void
1130__sync_fetch_and_add_8 (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001132 fformat (stderr, "%s called\n", __FUNCTION__);
1133 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134}
Dave Barach9b8ffd92016-07-08 08:13:45 -04001135
1136void
1137__sync_add_and_fetch_8 (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001139 fformat (stderr, "%s called\n", __FUNCTION__);
1140 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141}
1142#endif
1143
1144void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -04001145void
1146vnet_main_fixup (vlib_fork_fixup_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001148}
1149
1150void
1151vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1152{
1153 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154
1155 if (vlib_mains == 0)
1156 return;
1157
Damjan Marion586afd72017-04-05 19:18:20 +02001158 ASSERT (vlib_get_thread_index () == 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001159 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160
1161 switch (which)
1162 {
1163 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1164 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1165 break;
1166
1167 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001168 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001170 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171}
1172
Dave Barach9b8ffd92016-07-08 08:13:45 -04001173void
1174vlib_worker_thread_barrier_sync (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175{
1176 f64 deadline;
1177 u32 count;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001178
Dave Barach80f54e22017-03-08 19:08:56 -05001179 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001180 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001181
1182 count = vec_len (vlib_mains) - 1;
1183
1184 /* Tolerate recursive calls */
1185 if (++vlib_worker_threads[0].recursion_level > 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001186 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001187
Bud Grise42f20062016-03-16 13:09:46 -04001188 vlib_worker_threads[0].barrier_sync_count++;
1189
Damjan Marion586afd72017-04-05 19:18:20 +02001190 ASSERT (vlib_get_thread_index () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191
1192 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1193
1194 *vlib_worker_threads->wait_at_barrier = 1;
1195 while (*vlib_worker_threads->workers_at_barrier != count)
1196 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001197 if (vlib_time_now (vm) > deadline)
1198 {
1199 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1200 os_panic ();
1201 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001202 }
1203}
1204
Dave Barach9b8ffd92016-07-08 08:13:45 -04001205void
1206vlib_worker_thread_barrier_release (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001207{
1208 f64 deadline;
1209
Dave Barach80f54e22017-03-08 19:08:56 -05001210 if (vec_len (vlib_mains) < 2)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001211 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212
1213 if (--vlib_worker_threads[0].recursion_level > 0)
1214 return;
1215
1216 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1217
1218 *vlib_worker_threads->wait_at_barrier = 0;
1219
1220 while (*vlib_worker_threads->workers_at_barrier > 0)
1221 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001222 if (vlib_time_now (vm) > deadline)
1223 {
1224 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1225 os_panic ();
1226 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001227 }
1228}
1229
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001230/*
1231 * Check the frame queue to see if any frames are available.
Dave Barach9b8ffd92016-07-08 08:13:45 -04001232 * If so, pull the packets off the frames and put them to
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001233 * the handoff node.
1234 */
Damjan Marione9d52d52017-03-09 15:42:26 +01001235int
1236vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237{
Damjan Marion586afd72017-04-05 19:18:20 +02001238 u32 thread_id = vm->thread_index;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001239 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001240 vlib_frame_queue_elt_t *elt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001241 u32 *from, *to;
1242 vlib_frame_t *f;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001243 int msg_type;
1244 int processed = 0;
1245 u32 n_left_to_node;
1246 u32 vectors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001247
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001248 ASSERT (fq);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001249 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001251 if (PREDICT_FALSE (fqm->node_index == ~0))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001252 return 0;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001253 /*
1254 * Gather trace data for frame queues
1255 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001256 if (PREDICT_FALSE (fq->trace))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001257 {
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001258 frame_queue_trace_t *fqt;
1259 frame_queue_nelt_counter_t *fqh;
1260 u32 elix;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001261
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001262 fqt = &fqm->frame_queue_traces[thread_id];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001264 fqt->nelts = fq->nelts;
1265 fqt->head = fq->head;
1266 fqt->head_hint = fq->head_hint;
1267 fqt->tail = fq->tail;
1268 fqt->threshold = fq->vector_threshold;
1269 fqt->n_in_use = fqt->tail - fqt->head;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001270 if (fqt->n_in_use >= fqt->nelts)
1271 {
1272 // if beyond max then use max
1273 fqt->n_in_use = fqt->nelts - 1;
1274 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001275
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001276 /* Record the number of elements in use in the histogram */
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001277 fqh = &fqm->frame_queue_histogram[thread_id];
Dave Barach9b8ffd92016-07-08 08:13:45 -04001278 fqh->count[fqt->n_in_use]++;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001279
1280 /* Record a snapshot of the elements in use */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001281 for (elix = 0; elix < fqt->nelts; elix++)
1282 {
1283 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1284 if (1 || elt->valid)
1285 {
1286 fqt->n_vectors[elix] = elt->n_vectors;
1287 }
1288 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001289 fqt->written = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001291
1292 while (1)
1293 {
1294 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001295 {
1296 fq->head_hint = fq->head;
1297 return processed;
1298 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001299
Dave Barach9b8ffd92016-07-08 08:13:45 -04001300 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001301
1302 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001303 {
1304 fq->head_hint = fq->head;
1305 return processed;
1306 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001307
1308 from = elt->buffer_index;
1309 msg_type = elt->msg_type;
1310
1311 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1312 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1313
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001314 f = vlib_get_frame_to_node (vm, fqm->node_index);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001315
1316 to = vlib_frame_vector_args (f);
1317
1318 n_left_to_node = elt->n_vectors;
1319
1320 while (n_left_to_node >= 4)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001321 {
1322 to[0] = from[0];
1323 to[1] = from[1];
1324 to[2] = from[2];
1325 to[3] = from[3];
1326 to += 4;
1327 from += 4;
1328 n_left_to_node -= 4;
1329 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001330
1331 while (n_left_to_node > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001332 {
1333 to[0] = from[0];
1334 to++;
1335 from++;
1336 n_left_to_node--;
1337 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001338
1339 vectors += elt->n_vectors;
1340 f->n_vectors = elt->n_vectors;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001341 vlib_put_frame_to_node (vm, fqm->node_index, f);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001342
1343 elt->valid = 0;
1344 elt->n_vectors = 0;
1345 elt->msg_type = 0xfefefefe;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001346 CLIB_MEMORY_BARRIER ();
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001347 fq->head++;
1348 processed++;
1349
1350 /*
1351 * Limit the number of packets pushed into the graph
1352 */
1353 if (vectors >= fq->vector_threshold)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001354 {
1355 fq->head_hint = fq->head;
1356 return processed;
1357 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001358 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001359 ASSERT (0);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001360 return processed;
1361}
1362
Dave Barach9b8ffd92016-07-08 08:13:45 -04001363void
1364vlib_worker_thread_fn (void *arg)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001365{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001366 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
Damjan Marion878c6092017-01-04 13:19:27 +01001367 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -04001368 vlib_main_t *vm = vlib_get_main ();
Damjan Marione9f929b2017-03-16 11:32:09 +01001369 clib_error_t *e;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001370
Damjan Marion586afd72017-04-05 19:18:20 +02001371 ASSERT (vm->thread_index == vlib_get_thread_index ());
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001372
1373 vlib_worker_thread_init (w);
1374 clib_time_init (&vm->clib_time);
1375 clib_mem_set_heap (w->thread_mheap);
1376
Damjan Marioneb743fa2017-03-20 16:34:15 +01001377 /* Wait until the dpdk init sequence is complete */
1378 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
1379 vlib_worker_thread_barrier_check ();
1380
Damjan Marione9f929b2017-03-16 11:32:09 +01001381 e = vlib_call_init_exit_functions
1382 (vm, vm->worker_init_function_registrations, 1 /* call_once */ );
1383 if (e)
1384 clib_error_report (e);
1385
Damjan Marione9d52d52017-03-09 15:42:26 +01001386 vlib_worker_loop (vm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001387}
1388
Dave Barach9b8ffd92016-07-08 08:13:45 -04001389/* *INDENT-OFF* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001390VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1391 .name = "workers",
1392 .short_name = "wk",
1393 .function = vlib_worker_thread_fn,
1394};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001395/* *INDENT-ON* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001396
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001397u32
1398vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1399{
1400 vlib_thread_main_t *tm = vlib_get_thread_main ();
1401 vlib_frame_queue_main_t *fqm;
1402 vlib_frame_queue_t *fq;
1403 int i;
1404
1405 if (frame_queue_nelts == 0)
1406 frame_queue_nelts = FRAME_QUEUE_NELTS;
1407
1408 vec_add2 (tm->frame_queue_mains, fqm, 1);
1409
1410 fqm->node_index = node_index;
1411
1412 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1413 _vec_len (fqm->vlib_frame_queues) = 0;
1414 for (i = 0; i < tm->n_vlib_mains; i++)
1415 {
1416 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1417 vec_add1 (fqm->vlib_frame_queues, fq);
1418 }
1419
1420 return (fqm - tm->frame_queue_mains);
1421}
1422
Damjan Marion878c6092017-01-04 13:19:27 +01001423
1424int
1425vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1426{
1427 vlib_thread_main_t *tm = vlib_get_thread_main ();
1428
1429 if (tm->extern_thread_mgmt)
1430 return -1;
1431
1432 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1433 tm->extern_thread_mgmt = 1;
1434 return 0;
1435}
1436
Dave Barach9b8ffd92016-07-08 08:13:45 -04001437clib_error_t *
1438threads_init (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001439{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001440 return 0;
1441}
1442
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001443VLIB_INIT_FUNCTION (threads_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001444
1445/*
1446 * fd.io coding-style-patch-verification: ON
1447 *
1448 * Local Variables:
1449 * eval: (c-set-style "gnu")
1450 * End:
1451 */