blob: e3ea3c9cb478c69b9669ba7db3ee9f6988850d1e [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
38uword
39os_get_cpu_number (void)
40{
Dave Barach9b8ffd92016-07-08 08:13:45 -040041 void *sp;
Ed Warnickecb9cada2015-12-08 15:45:58 -070042 uword n;
43 u32 len;
44
45 len = vec_len (vlib_thread_stacks);
46 if (len == 0)
47 return 0;
48
49 /* Get any old stack address. */
50 sp = &sp;
51
Dave Barach9b8ffd92016-07-08 08:13:45 -040052 n = ((uword) sp - (uword) vlib_thread_stacks[0])
53 >> VLIB_LOG2_THREAD_STACK_SIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
55 /* "processes" have their own stacks, and they always run in thread 0 */
56 n = n >= len ? 0 : n;
57
58 return n;
59}
60
Dave Barach01d86c72016-08-08 15:13:42 -040061uword
62os_get_ncpus (void)
63{
64 u32 len;
65
66 len = vec_len (vlib_thread_stacks);
67 if (len == 0)
68 return 1;
69 else
70 return len;
71}
72
Ed Warnickecb9cada2015-12-08 15:45:58 -070073void
74vlib_set_thread_name (char *name)
75{
76 int pthread_setname_np (pthread_t __target_thread, const char *__name);
Dave Barachb2a6e252016-07-27 10:00:58 -040077 int rv;
Dave Barach9b8ffd92016-07-08 08:13:45 -040078 pthread_t thread = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
Dave Barach9b8ffd92016-07-08 08:13:45 -040080 if (thread)
Dave Barachb2a6e252016-07-27 10:00:58 -040081 {
82 rv = pthread_setname_np (thread, name);
83 if (rv)
Ed Warnicke853e7202016-08-12 11:42:26 -070084 clib_warning ("pthread_setname_np returned %d", rv);
Dave Barachb2a6e252016-07-27 10:00:58 -040085 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070086}
87
Dave Barach9b8ffd92016-07-08 08:13:45 -040088static int
89sort_registrations_by_no_clone (void *a0, void *a1)
90{
91 vlib_thread_registration_t **tr0 = a0;
92 vlib_thread_registration_t **tr1 = a1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Dave Barach9b8ffd92016-07-08 08:13:45 -040094 return ((i32) ((*tr0)->no_data_structure_clone)
95 - ((i32) ((*tr1)->no_data_structure_clone)));
Ed Warnickecb9cada2015-12-08 15:45:58 -070096}
97
98static uword *
Dave Barach9b8ffd92016-07-08 08:13:45 -040099vlib_sysfs_list_to_bitmap (char *filename)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100{
101 FILE *fp;
102 uword *r = 0;
103
104 fp = fopen (filename, "r");
105
106 if (fp != NULL)
107 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400108 u8 *buffer = 0;
109 vec_validate (buffer, 256 - 1);
110 if (fgets ((char *) buffer, 256, fp))
111 {
112 unformat_input_t in;
113 unformat_init_string (&in, (char *) buffer,
114 strlen ((char *) buffer));
Dave Barachb2a6e252016-07-27 10:00:58 -0400115 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
Ed Warnicke853e7202016-08-12 11:42:26 -0700116 clib_warning ("unformat_bitmap_list failed");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117 unformat_free (&in);
118 }
119 vec_free (buffer);
120 fclose (fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 }
122 return r;
123}
124
125
126/* Called early in the init sequence */
127
128clib_error_t *
129vlib_thread_init (vlib_main_t * vm)
130{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400131 vlib_thread_main_t *tm = &vlib_thread_main;
132 vlib_worker_thread_t *w;
133 vlib_thread_registration_t *tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 u32 n_vlib_mains = 1;
135 u32 first_index = 1;
136 u32 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137 uword *avail_cpu;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
139 /* get bitmaps of active cpu cores and sockets */
140 tm->cpu_core_bitmap =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400141 vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 tm->cpu_socket_bitmap =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400143 vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Dave Barach9b8ffd92016-07-08 08:13:45 -0400145 avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147 /* skip cores */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148 for (i = 0; i < tm->skip_cores; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400150 uword c = clib_bitmap_first_set (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 if (c == ~0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152 return clib_error_return (0, "no available cpus to skip");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Dave Barach9b8ffd92016-07-08 08:13:45 -0400154 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155 }
156
157 /* grab cpu for main thread */
158 if (!tm->main_lcore)
159 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 tm->main_lcore = clib_bitmap_first_set (avail_cpu);
161 if (tm->main_lcore == (u8) ~ 0)
162 return clib_error_return (0, "no available cpus to be used for the"
163 " main thread");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 }
165 else
166 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167 if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0)
168 return clib_error_return (0, "cpu %u is not available to be used"
169 " for the main thread", tm->main_lcore);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400171 avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
173 /* assume that there is socket 0 only if there is no data from sysfs */
174 if (!tm->cpu_socket_bitmap)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400175 tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
Christophe Fontainefef15b42016-04-09 12:38:49 +0900177 /* pin main thread to main_lcore */
Damjan Marion878c6092017-01-04 13:19:27 +0100178 if (tm->cb.vlib_thread_set_lcore_cb)
179 {
180 tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore);
181 }
182 else
183 {
184 cpu_set_t cpuset;
185 CPU_ZERO (&cpuset);
186 CPU_SET (tm->main_lcore, &cpuset);
187 pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);
188 }
Christophe Fontainefef15b42016-04-09 12:38:49 +0900189
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190 /* as many threads as stacks... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400191 vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1,
192 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
194 /* Preallocate thread 0 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400195 _vec_len (vlib_worker_threads) = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 w = vlib_worker_threads;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400197 w->thread_mheap = clib_mem_get_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198 w->thread_stack = vlib_thread_stacks[0];
Pavel Kotucek98765202016-10-07 08:37:28 +0200199 w->lcore_id = tm->main_lcore;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400200 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200201 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 tm->n_vlib_mains = 1;
203
Pavel Kotucek1e765832016-09-23 08:54:14 +0200204 if (tm->sched_policy != ~0)
205 {
206 struct sched_param sched_param;
207 if (!sched_getparam (w->lwp, &sched_param))
208 {
209 if (tm->sched_priority != ~0)
210 sched_param.sched_priority = tm->sched_priority;
211 sched_setscheduler (w->lwp, tm->sched_policy, &sched_param);
212 }
213 }
214
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 /* assign threads to cores and set n_vlib_mains */
216 tr = tm->next;
217
218 while (tr)
219 {
220 vec_add1 (tm->registrations, tr);
221 tr = tr->next;
222 }
223
Dave Barach9b8ffd92016-07-08 08:13:45 -0400224 vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
226 for (i = 0; i < vec_len (tm->registrations); i++)
227 {
228 int j;
229 tr = tm->registrations[i];
230 tr->first_index = first_index;
231 first_index += tr->count;
232 n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0;
233
234 /* construct coremask */
235 if (tr->use_pthreads || !tr->count)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400236 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
238 if (tr->coremask)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400239 {
240 uword c;
241 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 clib_bitmap_foreach (c, tr->coremask, ({
243 if (clib_bitmap_get(avail_cpu, c) == 0)
244 return clib_error_return (0, "cpu %u is not available to be used"
245 " for the '%s' thread",c, tr->name);
246
247 avail_cpu = clib_bitmap_set(avail_cpu, c, 0);
248 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400249/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Dave Barach9b8ffd92016-07-08 08:13:45 -0400251 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400253 {
254 for (j = 0; j < tr->count; j++)
255 {
256 uword c = clib_bitmap_first_set (avail_cpu);
257 if (c == ~0)
258 return clib_error_return (0,
259 "no available cpus to be used for"
260 " the '%s' thread", tr->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Dave Barach9b8ffd92016-07-08 08:13:45 -0400262 avail_cpu = clib_bitmap_set (avail_cpu, c, 0);
263 tr->coremask = clib_bitmap_set (tr->coremask, c, 1);
264 }
265 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266 }
267
Dave Barach9b8ffd92016-07-08 08:13:45 -0400268 clib_bitmap_free (avail_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
270 tm->n_vlib_mains = n_vlib_mains;
271
Dave Barach9b8ffd92016-07-08 08:13:45 -0400272 vec_validate_aligned (vlib_worker_threads, first_index - 1,
273 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 return 0;
276}
277
278vlib_worker_thread_t *
279vlib_alloc_thread (vlib_main_t * vm)
280{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400281 vlib_worker_thread_t *w;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Dave Barach9b8ffd92016-07-08 08:13:45 -0400283 if (vec_len (vlib_worker_threads) >= vec_len (vlib_thread_stacks))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 {
285 clib_warning ("out of worker threads... Quitting...");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400286 exit (1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 }
288 vec_add2 (vlib_worker_threads, w, 1);
289 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
290 return w;
291}
292
Dave Barach9b8ffd92016-07-08 08:13:45 -0400293vlib_frame_queue_t *
294vlib_frame_queue_alloc (int nelts)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400296 vlib_frame_queue_t *fq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297
Dave Barach9b8ffd92016-07-08 08:13:45 -0400298 fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299 memset (fq, 0, sizeof (*fq));
300 fq->nelts = nelts;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400301 fq->vector_threshold = 128; // packets
302 vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303
304 if (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400305 {
306 if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1))
307 fformat (stderr, "WARNING: fq->tail unaligned\n");
308 if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1))
309 fformat (stderr, "WARNING: fq->head unaligned\n");
310 if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1))
311 fformat (stderr, "WARNING: fq->elts unaligned\n");
312
313 if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES)
314 fformat (stderr, "WARNING: fq->elts[0] size %d\n",
315 sizeof (fq->elts[0]));
316 if (nelts & (nelts - 1))
317 {
318 fformat (stderr, "FATAL: nelts MUST be a power of 2\n");
319 abort ();
320 }
321 }
322
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323 return (fq);
324}
325
326void vl_msg_api_handler_no_free (void *) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400327void
328vl_msg_api_handler_no_free (void *v)
329{
330}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
332/* Turned off, save as reference material... */
333#if 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400334static inline int
335vlib_frame_queue_dequeue_internal (int thread_id,
336 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337{
338 vlib_frame_queue_t *fq = vlib_frame_queues[thread_id];
339 vlib_frame_queue_elt_t *elt;
340 vlib_frame_t *f;
341 vlib_pending_frame_t *p;
342 vlib_node_runtime_t *r;
343 u32 node_runtime_index;
344 int msg_type;
345 u64 before;
346 int processed = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347
348 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
350 while (1)
351 {
352 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400353 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
357 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400358 return processed;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360 before = clib_cpu_time_now ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
362 f = elt->frame;
363 node_runtime_index = elt->node_runtime_index;
364 msg_type = elt->msg_type;
365
366 switch (msg_type)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367 {
368 case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS:
369 vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
370 /* note fallthrough... */
371 case VLIB_FRAME_QUEUE_ELT_FREE_FRAME:
372 r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
373 node_runtime_index);
374 vlib_frame_free (vm, r, f);
375 break;
376 case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME:
377 vec_add2 (vm->node_main.pending_frames, p, 1);
378 f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH);
379 p->node_runtime_index = elt->node_runtime_index;
380 p->frame_index = vlib_frame_index (vm, f);
381 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
382 fq->dequeue_vectors += (u64) f->n_vectors;
383 break;
384 case VLIB_FRAME_QUEUE_ELT_API_MSG:
385 vl_msg_api_handler_no_free (f);
386 break;
387 default:
388 clib_warning ("bogus frame queue message, type %d", msg_type);
389 break;
390 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 elt->valid = 0;
392 fq->dequeues++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 fq->dequeue_ticks += clib_cpu_time_now () - before;
394 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 fq->head++;
396 processed++;
397 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400398 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 return processed;
400}
401
Dave Barach9b8ffd92016-07-08 08:13:45 -0400402int
403vlib_frame_queue_dequeue (int thread_id,
404 vlib_main_t * vm, vlib_node_main_t * nm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405{
406 return vlib_frame_queue_dequeue_internal (thread_id, vm, nm);
407}
408
Dave Barach9b8ffd92016-07-08 08:13:45 -0400409int
410vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
411 u32 frame_queue_index, vlib_frame_t * frame,
412 vlib_frame_queue_msg_type_t type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413{
414 vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index];
415 vlib_frame_queue_elt_t *elt;
416 u32 save_count;
417 u64 new_tail;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400418 u64 before = clib_cpu_time_now ();
419
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420 ASSERT (fq);
421
422 new_tail = __sync_add_and_fetch (&fq->tail, 1);
423
424 /* Wait until a ring slot is available */
425 while (new_tail >= fq->head + fq->nelts)
426 {
427 f64 b4 = vlib_time_now_ticks (vm, before);
428 vlib_worker_thread_barrier_check (vm, b4);
429 /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */
430 // vlib_frame_queue_dequeue (vm->cpu_index, vm, nm);
431 }
432
Dave Barach9b8ffd92016-07-08 08:13:45 -0400433 elt = fq->elts + (new_tail & (fq->nelts - 1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
435 /* this would be very bad... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400436 while (elt->valid)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437 {
438 }
439
440 /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */
441 save_count = frame->n_vectors;
442
443 elt->frame = frame;
444 elt->node_runtime_index = node_runtime_index;
445 elt->msg_type = type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 elt->valid = 1;
448
449 return save_count;
450}
451#endif /* 0 */
452
453/* To be called by vlib worker threads upon startup */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454void
455vlib_worker_thread_init (vlib_worker_thread_t * w)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400457 vlib_thread_main_t *tm = vlib_get_thread_main ();
458
Dave Barachfdf49442016-12-20 12:48:14 -0500459 /*
460 * Note: disabling signals in worker threads as follows
461 * prevents the api post-mortem dump scheme from working
462 * {
463 * sigset_t s;
464 * sigfillset (&s);
465 * pthread_sigmask (SIG_SETMASK, &s, 0);
466 * }
467 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
469 clib_mem_set_heap (w->thread_mheap);
470
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471 if (vec_len (tm->thread_prefix) && w->registration->short_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400473 w->name = format (0, "%v_%s_%d%c", tm->thread_prefix,
474 w->registration->short_name, w->instance_id, '\0');
475 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476 }
477
478 if (!w->registration->use_pthreads)
479 {
480
481 /* Initial barrier sync, for both worker and i/o threads */
482 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
483
484 while (*vlib_worker_threads->wait_at_barrier)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400485 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486
487 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
488 }
489}
490
Dave Barach9b8ffd92016-07-08 08:13:45 -0400491void *
492vlib_worker_thread_bootstrap_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493{
494 void *rv;
495 vlib_worker_thread_t *w = arg;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400496
497 w->lwp = syscall (SYS_gettid);
Pavel Kotucek98765202016-10-07 08:37:28 +0200498 w->thread_id = pthread_self ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499
Dave Barach9b8ffd92016-07-08 08:13:45 -0400500 rv = (void *) clib_calljmp
501 ((uword (*)(uword)) w->thread_function,
502 (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 /* NOTREACHED, we hope */
504 return rv;
505}
506
Damjan Marion878c6092017-01-04 13:19:27 +0100507static clib_error_t *
508vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509{
Damjan Marion878c6092017-01-04 13:19:27 +0100510 vlib_thread_main_t *tm = &vlib_thread_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400511 void *(*fp_arg) (void *) = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512
Pavel Kotucek98765202016-10-07 08:37:28 +0200513 w->lcore_id = lcore_id;
Damjan Marion878c6092017-01-04 13:19:27 +0100514 if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads)
515 return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400517 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400518 pthread_t worker;
519 cpu_set_t cpuset;
520 CPU_ZERO (&cpuset);
521 CPU_SET (lcore_id, &cpuset);
Christophe Fontainefef15b42016-04-09 12:38:49 +0900522
Damjan Marion878c6092017-01-04 13:19:27 +0100523 if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w))
524 return clib_error_return_unix (0, "pthread_create");
525
526 if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset))
527 return clib_error_return_unix (0, "pthread_setaffinity_np");
528
529 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531}
532
Dave Barach9b8ffd92016-07-08 08:13:45 -0400533static clib_error_t *
534start_workers (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535{
536 int i, j;
537 vlib_worker_thread_t *w;
538 vlib_main_t *vm_clone;
539 void *oldheap;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400540 vlib_thread_main_t *tm = &vlib_thread_main;
541 vlib_thread_registration_t *tr;
542 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 u32 n_vlib_mains = tm->n_vlib_mains;
544 u32 worker_thread_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400545 u8 *main_heap = clib_mem_get_per_cpu_heap ();
546 mheap_t *main_heap_header = mheap_header (main_heap);
547
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 vec_reset_length (vlib_worker_threads);
549
550 /* Set up the main thread */
551 vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES);
Dave Barach3e7deb12016-02-05 16:29:53 -0500552 w->elog_track.name = "main thread";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 elog_track_register (&vm->elog_main, &w->elog_track);
554
Dave Barach9b8ffd92016-07-08 08:13:45 -0400555 if (vec_len (tm->thread_prefix))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400557 w->name = format (0, "%v_main%c", tm->thread_prefix, '\0');
558 vlib_set_thread_name ((char *) w->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559 }
560
Dave Barach9b8ffd92016-07-08 08:13:45 -0400561 /*
Dave Barach848191d2016-04-28 16:24:15 -0400562 * Truth of the matter: we always use at least two
Dave Barach9b8ffd92016-07-08 08:13:45 -0400563 * threads. So, make the main heap thread-safe
Dave Barach848191d2016-04-28 16:24:15 -0400564 * and make the event log thread-safe.
565 */
566 main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400567 vm->elog_main.lock =
568 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barach848191d2016-04-28 16:24:15 -0400569 vm->elog_main.lock[0] = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400570
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571 if (n_vlib_mains > 1)
572 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 vec_validate (vlib_mains, tm->n_vlib_mains - 1);
574 _vec_len (vlib_mains) = 0;
575 vec_add1 (vlib_mains, vm);
576
Dave Barach9b8ffd92016-07-08 08:13:45 -0400577 vlib_worker_threads->wait_at_barrier =
578 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579 vlib_worker_threads->workers_at_barrier =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400580 clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
582 /* Ask for an initial barrier sync */
583 *vlib_worker_threads->workers_at_barrier = 0;
584 *vlib_worker_threads->wait_at_barrier = 1;
585
586 worker_thread_index = 1;
587
Dave Barach9b8ffd92016-07-08 08:13:45 -0400588 for (i = 0; i < vec_len (tm->registrations); i++)
589 {
590 vlib_node_main_t *nm, *nm_clone;
591 vlib_buffer_main_t *bm_clone;
592 vlib_buffer_free_list_t *fl_clone, *fl_orig;
593 vlib_buffer_free_list_t *orig_freelist_pool;
594 int k;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595
Dave Barach9b8ffd92016-07-08 08:13:45 -0400596 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597
Dave Barach9b8ffd92016-07-08 08:13:45 -0400598 if (tr->count == 0)
599 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600
Dave Barach9b8ffd92016-07-08 08:13:45 -0400601 for (k = 0; k < tr->count; k++)
602 {
603 vec_add2 (vlib_worker_threads, w, 1);
604 if (tr->mheap_size)
605 w->thread_mheap =
606 mheap_alloc (0 /* use VM */ , tr->mheap_size);
607 else
608 w->thread_mheap = main_heap;
609 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
610 w->thread_function = tr->function;
611 w->thread_function_arg = w;
612 w->instance_id = k;
613 w->registration = tr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
Dave Barach9b8ffd92016-07-08 08:13:45 -0400615 w->elog_track.name =
616 (char *) format (0, "%s %d", tr->name, k + 1);
617 vec_add1 (w->elog_track.name, 0);
618 elog_track_register (&vm->elog_main, &w->elog_track);
Bud Grise68adab92016-02-12 10:36:11 -0500619
Dave Barach9b8ffd92016-07-08 08:13:45 -0400620 if (tr->no_data_structure_clone)
621 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622
Dave Barach9b8ffd92016-07-08 08:13:45 -0400623 /* Fork vlib_global_main et al. Look for bugs here */
624 oldheap = clib_mem_set_heap (w->thread_mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625
Dave Barach9b8ffd92016-07-08 08:13:45 -0400626 vm_clone = clib_mem_alloc (sizeof (*vm_clone));
627 clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628
Dave Barach9b8ffd92016-07-08 08:13:45 -0400629 vm_clone->cpu_index = worker_thread_index;
630 vm_clone->heap_base = w->thread_mheap;
631 vm_clone->mbuf_alloc_list = 0;
632 memset (&vm_clone->random_buffer, 0,
633 sizeof (vm_clone->random_buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634
Dave Barach9b8ffd92016-07-08 08:13:45 -0400635 nm = &vlib_mains[0]->node_main;
636 nm_clone = &vm_clone->node_main;
637 /* fork next frames array, preserving node runtime indices */
638 nm_clone->next_frames = vec_dup (nm->next_frames);
639 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
640 {
641 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
642 u32 save_node_runtime_index;
643 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644
Dave Barach9b8ffd92016-07-08 08:13:45 -0400645 save_node_runtime_index = nf->node_runtime_index;
646 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
647 vlib_next_frame_init (nf);
648 nf->node_runtime_index = save_node_runtime_index;
649 nf->flags = save_flags;
650 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651
Dave Barach9b8ffd92016-07-08 08:13:45 -0400652 /* fork the frame dispatch queue */
653 nm_clone->pending_frames = 0;
654 vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */
655 _vec_len (nm_clone->pending_frames) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657 /* fork nodes */
658 nm_clone->nodes = 0;
659 for (j = 0; j < vec_len (nm->nodes); j++)
660 {
661 vlib_node_t *n;
662 n = clib_mem_alloc_no_fail (sizeof (*n));
663 clib_memcpy (n, nm->nodes[j], sizeof (*n));
664 /* none of the copied nodes have enqueue rights given out */
665 n->owner_node_index = VLIB_INVALID_NODE_INDEX;
666 memset (&n->stats_total, 0, sizeof (n->stats_total));
667 memset (&n->stats_last_clear, 0,
668 sizeof (n->stats_last_clear));
669 vec_add1 (nm_clone->nodes, n);
670 }
671 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] =
672 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Dave Barach9b8ffd92016-07-08 08:13:45 -0400674 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
675 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
676 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
677 rt->cpu_index = vm_clone->cpu_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
Dave Barach9b8ffd92016-07-08 08:13:45 -0400679 nm_clone->processes = vec_dup (nm->processes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
Dave Barach9b8ffd92016-07-08 08:13:45 -0400681 /* zap the (per worker) frame freelists, etc */
682 nm_clone->frame_sizes = 0;
683 nm_clone->frame_size_hash = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684
Dave Barach9b8ffd92016-07-08 08:13:45 -0400685 /* Packet trace buffers are guaranteed to be empty, nothing to do here */
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100686
Dave Barach9b8ffd92016-07-08 08:13:45 -0400687 clib_mem_set_heap (oldheap);
688 vec_add1 (vlib_mains, vm_clone);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689
Dave Barach9b8ffd92016-07-08 08:13:45 -0400690 vm_clone->error_main.counters =
691 vec_dup (vlib_mains[0]->error_main.counters);
692 vm_clone->error_main.counters_last_clear =
693 vec_dup (vlib_mains[0]->error_main.counters_last_clear);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694
Dave Barach9b8ffd92016-07-08 08:13:45 -0400695 /* Fork the vlib_buffer_main_t free lists, etc. */
696 bm_clone = vec_dup (vm_clone->buffer_main);
697 vm_clone->buffer_main = bm_clone;
698
699 orig_freelist_pool = bm_clone->buffer_free_list_pool;
700 bm_clone->buffer_free_list_pool = 0;
701
702 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703 pool_foreach (fl_orig, orig_freelist_pool,
704 ({
Dave Barach9b8ffd92016-07-08 08:13:45 -0400705 pool_get_aligned (bm_clone->buffer_free_list_pool,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706 fl_clone, CLIB_CACHE_LINE_BYTES);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400707 ASSERT (fl_orig - orig_freelist_pool
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708 == fl_clone - bm_clone->buffer_free_list_pool);
709
710 fl_clone[0] = fl_orig[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100711 fl_clone->buffers = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712 fl_clone->n_alloc = 0;
713 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400714/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715
Dave Barach9b8ffd92016-07-08 08:13:45 -0400716 worker_thread_index++;
717 }
718 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719 }
720 else
721 {
722 /* only have non-data-structure copy threads to create... */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400723 for (i = 0; i < vec_len (tm->registrations); i++)
724 {
725 tr = tm->registrations[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Dave Barach9b8ffd92016-07-08 08:13:45 -0400727 for (j = 0; j < tr->count; j++)
728 {
729 vec_add2 (vlib_worker_threads, w, 1);
730 if (tr->mheap_size)
731 w->thread_mheap =
732 mheap_alloc (0 /* use VM */ , tr->mheap_size);
733 else
734 w->thread_mheap = main_heap;
735 w->thread_stack = vlib_thread_stacks[w - vlib_worker_threads];
736 w->thread_function = tr->function;
737 w->thread_function_arg = w;
738 w->instance_id = j;
739 w->elog_track.name =
740 (char *) format (0, "%s %d", tr->name, j + 1);
741 w->registration = tr;
742 vec_add1 (w->elog_track.name, 0);
743 elog_track_register (&vm->elog_main, &w->elog_track);
744 }
745 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746 }
747
748 worker_thread_index = 1;
749
750 for (i = 0; i < vec_len (tm->registrations); i++)
751 {
Damjan Marion878c6092017-01-04 13:19:27 +0100752 clib_error_t *err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753 int j;
754
755 tr = tm->registrations[i];
756
757 if (tr->use_pthreads || tm->use_pthreads)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400758 {
759 for (j = 0; j < tr->count; j++)
760 {
761 w = vlib_worker_threads + worker_thread_index++;
Damjan Marion878c6092017-01-04 13:19:27 +0100762 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
763 w, 0);
764 if (err)
765 clib_error_report (err);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400766 }
767 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400769 {
770 uword c;
Damjan Marion878c6092017-01-04 13:19:27 +0100771 /* *INDENT-OFF* */
772 clib_bitmap_foreach (c, tr->coremask, ({
773 w = vlib_worker_threads + worker_thread_index++;
774 err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn,
775 w, c);
776 if (err)
777 clib_error_report (err);
778 }));
779 /* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400780 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400782 vlib_worker_thread_barrier_sync (vm);
783 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784 return 0;
785}
786
787VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers);
788
Dave Barach9b8ffd92016-07-08 08:13:45 -0400789void
790vlib_worker_thread_node_runtime_update (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791{
792 int i, j;
793 vlib_worker_thread_t *w;
794 vlib_main_t *vm;
795 vlib_node_main_t *nm, *nm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400796 vlib_node_t **old_nodes_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 vlib_main_t *vm_clone;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400798 vlib_node_runtime_t *rt, *old_rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 void *oldheap;
800 never_inline void
801 vlib_node_runtime_sync_stats (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400802 vlib_node_runtime_t * r,
803 uword n_calls,
804 uword n_vectors, uword n_clocks);
805
806 ASSERT (os_get_cpu_number () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807
808 if (vec_len (vlib_mains) == 0)
809 return;
810
811 vm = vlib_mains[0];
812 nm = &vm->node_main;
813
Dave Barach9b8ffd92016-07-08 08:13:45 -0400814 ASSERT (os_get_cpu_number () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815 ASSERT (*vlib_worker_threads->wait_at_barrier == 1);
816
Dave Barach9b8ffd92016-07-08 08:13:45 -0400817 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818 * Scrape all runtime stats, so we don't lose node runtime(s) with
819 * pending counts, or throw away worker / io thread counts.
820 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400821 for (j = 0; j < vec_len (nm->nodes); j++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400823 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824 n = nm->nodes[j];
825 vlib_node_sync_stats (vm, n);
826 }
827
828 for (i = 1; i < vec_len (vlib_mains); i++)
829 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400830 vlib_node_t *n;
831
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832 vm_clone = vlib_mains[i];
833 nm_clone = &vm_clone->node_main;
834
Dave Barach9b8ffd92016-07-08 08:13:45 -0400835 for (j = 0; j < vec_len (nm_clone->nodes); j++)
836 {
837 n = nm_clone->nodes[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838
Dave Barach9b8ffd92016-07-08 08:13:45 -0400839 rt = vlib_node_get_runtime (vm_clone, n->index);
840 vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0);
841 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842 }
843
844 for (i = 1; i < vec_len (vlib_mains); i++)
845 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400846 vlib_node_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700847 w = vlib_worker_threads + i;
848 oldheap = clib_mem_set_heap (w->thread_mheap);
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100849
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850 vm_clone = vlib_mains[i];
851
852 /* Re-clone error heap */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400853 u64 *old_counters = vm_clone->error_main.counters;
854 u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear;
855 clib_memcpy (&vm_clone->error_main, &vm->error_main,
856 sizeof (vm->error_main));
857 j = vec_len (vm->error_main.counters) - 1;
858 vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES);
859 vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES);
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100860 vm_clone->error_main.counters = old_counters;
861 vm_clone->error_main.counters_last_clear = old_counters_all_clear;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862
863 nm_clone = &vm_clone->node_main;
864 vec_free (nm_clone->next_frames);
865 nm_clone->next_frames = vec_dup (nm->next_frames);
Damjan Marionbc20bdf2015-12-17 14:28:18 +0100866
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867 for (j = 0; j < vec_len (nm_clone->next_frames); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400868 {
869 vlib_next_frame_t *nf = &nm_clone->next_frames[j];
870 u32 save_node_runtime_index;
871 u32 save_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872
Dave Barach9b8ffd92016-07-08 08:13:45 -0400873 save_node_runtime_index = nf->node_runtime_index;
874 save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH;
875 vlib_next_frame_init (nf);
876 nf->node_runtime_index = save_node_runtime_index;
877 nf->flags = save_flags;
878 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879
880 old_nodes_clone = nm_clone->nodes;
881 nm_clone->nodes = 0;
882
883 /* re-fork nodes */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400884 for (j = 0; j < vec_len (nm->nodes); j++)
885 {
886 vlib_node_t *old_n_clone;
887 vlib_node_t *new_n, *new_n_clone;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
Dave Barach9b8ffd92016-07-08 08:13:45 -0400889 new_n = nm->nodes[j];
890 old_n_clone = old_nodes_clone[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891
Dave Barach9b8ffd92016-07-08 08:13:45 -0400892 new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone));
893 clib_memcpy (new_n_clone, new_n, sizeof (*new_n));
894 /* none of the copied nodes have enqueue rights given out */
895 new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896
Dave Barach9b8ffd92016-07-08 08:13:45 -0400897 if (j >= vec_len (old_nodes_clone))
898 {
899 /* new node, set to zero */
900 memset (&new_n_clone->stats_total, 0,
901 sizeof (new_n_clone->stats_total));
902 memset (&new_n_clone->stats_last_clear, 0,
903 sizeof (new_n_clone->stats_last_clear));
904 }
905 else
906 {
907 /* Copy stats if the old data is valid */
908 clib_memcpy (&new_n_clone->stats_total,
909 &old_n_clone->stats_total,
910 sizeof (new_n_clone->stats_total));
911 clib_memcpy (&new_n_clone->stats_last_clear,
912 &old_n_clone->stats_last_clear,
913 sizeof (new_n_clone->stats_last_clear));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914
Dave Barach9b8ffd92016-07-08 08:13:45 -0400915 /* keep previous node state */
916 new_n_clone->state = old_n_clone->state;
917 }
918 vec_add1 (nm_clone->nodes, new_n_clone);
919 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920 /* Free the old node clone */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400921 for (j = 0; j < vec_len (old_nodes_clone); j++)
922 clib_mem_free (old_nodes_clone[j]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923 vec_free (old_nodes_clone);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400924
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 vec_free (nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]);
926
927 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
930 /* clone input node runtime */
931 old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT];
932
933 nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] =
Dave Barach9b8ffd92016-07-08 08:13:45 -0400934 vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935
Dave Barach9b8ffd92016-07-08 08:13:45 -0400936 vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT])
937 {
938 rt->cpu_index = vm_clone->cpu_index;
939 }
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;
945 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946
Dave Barach9b8ffd92016-07-08 08:13:45 -0400947 vec_free (old_rt);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948
949 nm_clone->processes = vec_dup (nm->processes);
950
951 clib_mem_set_heap (oldheap);
952
953 // vnet_main_fork_fixup (i);
954 }
955}
956
Pavel Kotucek1e765832016-09-23 08:54:14 +0200957u32
958unformat_sched_policy (unformat_input_t * input, va_list * args)
959{
960 u32 *r = va_arg (*args, u32 *);
961
962 if (0);
963#define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f;
964 foreach_sched_policy
965#undef _
966 else
967 return 0;
968 return 1;
969}
970
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971static clib_error_t *
972cpu_config (vlib_main_t * vm, unformat_input_t * input)
973{
974 vlib_thread_registration_t *tr;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400975 uword *p;
976 vlib_thread_main_t *tm = &vlib_thread_main;
977 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978 u64 coremask;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400979 uword *bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980 u32 count;
981
982 tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword));
Pavel Kotucek1e765832016-09-23 08:54:14 +0200983
Dave Barach9b8ffd92016-07-08 08:13:45 -0400984 tm->n_thread_stacks = 1; /* account for main thread */
Pavel Kotucek1e765832016-09-23 08:54:14 +0200985 tm->sched_policy = ~0;
986 tm->sched_priority = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987
988 tr = tm->next;
989
990 while (tr)
991 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400992 hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993 tr = tr->next;
994 }
995
Dave Barach9b8ffd92016-07-08 08:13:45 -0400996 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997 {
Damjan Marionbf741472016-06-13 22:49:44 +0200998 if (unformat (input, "use-pthreads"))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400999 tm->use_pthreads = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000 else if (unformat (input, "thread-prefix %v", &tm->thread_prefix))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001001 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002 else if (unformat (input, "main-core %u", &tm->main_lcore))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001003 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004 else if (unformat (input, "skip-cores %u", &tm->skip_cores))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001005 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 else if (unformat (input, "coremask-%s %llx", &name, &coremask))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001007 {
1008 p = hash_get_mem (tm->thread_registrations_by_name, name);
1009 if (p == 0)
1010 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011
Dave Barach9b8ffd92016-07-08 08:13:45 -04001012 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013
Dave Barach9b8ffd92016-07-08 08:13:45 -04001014 if (tr->use_pthreads)
1015 return clib_error_return (0,
1016 "coremask cannot be set for '%s' threads",
1017 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018
Dave Barach9b8ffd92016-07-08 08:13:45 -04001019 tr->coremask = clib_bitmap_set_multiple
1020 (tr->coremask, 0, coremask, BITS (coremask));
1021 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1022 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023 else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001024 &bitmap))
1025 {
1026 p = hash_get_mem (tm->thread_registrations_by_name, name);
1027 if (p == 0)
1028 return clib_error_return (0, "no such thread type '%s'", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029
Dave Barach9b8ffd92016-07-08 08:13:45 -04001030 tr = (vlib_thread_registration_t *) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031
Dave Barach9b8ffd92016-07-08 08:13:45 -04001032 if (tr->use_pthreads)
1033 return clib_error_return (0,
1034 "corelist cannot be set for '%s' threads",
1035 name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036
Dave Barach9b8ffd92016-07-08 08:13:45 -04001037 tr->coremask = bitmap;
1038 tr->count = clib_bitmap_count_set_bits (tr->coremask);
1039 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001040 else
1041 if (unformat
1042 (input, "scheduler-policy %U", unformat_sched_policy,
1043 &tm->sched_policy))
1044 ;
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001045 else if (unformat (input, "scheduler-priority %u", &tm->sched_priority))
Pavel Kotucek1e765832016-09-23 08:54:14 +02001046 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047 else if (unformat (input, "%s %u", &name, &count))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001048 {
1049 p = hash_get_mem (tm->thread_registrations_by_name, name);
1050 if (p == 0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001051 return clib_error_return (0, "no such thread type 3 '%s'", name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001052
1053 tr = (vlib_thread_registration_t *) p[0];
1054 if (tr->fixed_count)
1055 return clib_error_return
1056 (0, "number of %s threads not configurable", tr->name);
1057 tr->count = count;
1058 }
1059 else
1060 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061 }
1062
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001063 if (tm->sched_priority != ~0)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001064 {
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001065 if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR)
Pavel Kotucek1e765832016-09-23 08:54:14 +02001066 {
1067 u32 prio_max = sched_get_priority_max (tm->sched_policy);
1068 u32 prio_min = sched_get_priority_min (tm->sched_policy);
1069 if (tm->sched_priority > prio_max)
1070 tm->sched_priority = prio_max;
1071 if (tm->sched_priority < prio_min)
1072 tm->sched_priority = prio_min;
1073 }
1074 else
Pavel Kotucekc08a1ed2016-09-23 08:54:14 +02001075 {
1076 return clib_error_return
1077 (0,
1078 "scheduling priority (%d) is not allowed for `normal` scheduling policy",
1079 tm->sched_priority);
1080 }
Pavel Kotucek1e765832016-09-23 08:54:14 +02001081 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082 tr = tm->next;
1083
1084 if (!tm->thread_prefix)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001085 tm->thread_prefix = format (0, "vpp");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001086
1087 while (tr)
1088 {
1089 tm->n_thread_stacks += tr->count;
1090 tm->n_pthreads += tr->count * tr->use_pthreads;
Damjan Marion878c6092017-01-04 13:19:27 +01001091 tm->n_threads += tr->count * (tr->use_pthreads == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092 tr = tr->next;
1093 }
1094
1095 return 0;
1096}
1097
1098VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu");
1099
Christophe Fontainefef15b42016-04-09 12:38:49 +09001100#if !defined (__x86_64__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001101void
1102__sync_fetch_and_add_8 (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001104 fformat (stderr, "%s called\n", __FUNCTION__);
1105 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106}
Dave Barach9b8ffd92016-07-08 08:13:45 -04001107
1108void
1109__sync_add_and_fetch_8 (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001110{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001111 fformat (stderr, "%s called\n", __FUNCTION__);
1112 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113}
1114#endif
1115
1116void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak));
Dave Barach9b8ffd92016-07-08 08:13:45 -04001117void
1118vnet_main_fixup (vlib_fork_fixup_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001120}
1121
1122void
1123vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which)
1124{
1125 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126
1127 if (vlib_mains == 0)
1128 return;
1129
Dave Barach9b8ffd92016-07-08 08:13:45 -04001130 ASSERT (os_get_cpu_number () == 0);
1131 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132
1133 switch (which)
1134 {
1135 case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX:
1136 vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX);
1137 break;
1138
1139 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001140 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001142 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143}
1144
Dave Barach9b8ffd92016-07-08 08:13:45 -04001145void
1146vlib_worker_thread_barrier_sync (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147{
1148 f64 deadline;
1149 u32 count;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001150
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151 if (!vlib_mains)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001152 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153
1154 count = vec_len (vlib_mains) - 1;
1155
1156 /* Tolerate recursive calls */
1157 if (++vlib_worker_threads[0].recursion_level > 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001158 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159
Bud Grise42f20062016-03-16 13:09:46 -04001160 vlib_worker_threads[0].barrier_sync_count++;
1161
Dave Barach9b8ffd92016-07-08 08:13:45 -04001162 ASSERT (os_get_cpu_number () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001163
1164 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1165
1166 *vlib_worker_threads->wait_at_barrier = 1;
1167 while (*vlib_worker_threads->workers_at_barrier != count)
1168 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001169 if (vlib_time_now (vm) > deadline)
1170 {
1171 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1172 os_panic ();
1173 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001174 }
1175}
1176
Dave Barach9b8ffd92016-07-08 08:13:45 -04001177void
1178vlib_worker_thread_barrier_release (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179{
1180 f64 deadline;
1181
1182 if (!vlib_mains)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001183 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184
1185 if (--vlib_worker_threads[0].recursion_level > 0)
1186 return;
1187
1188 deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT;
1189
1190 *vlib_worker_threads->wait_at_barrier = 0;
1191
1192 while (*vlib_worker_threads->workers_at_barrier > 0)
1193 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001194 if (vlib_time_now (vm) > deadline)
1195 {
1196 fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__);
1197 os_panic ();
1198 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199 }
1200}
1201
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001202/*
1203 * Check the frame queue to see if any frames are available.
Dave Barach9b8ffd92016-07-08 08:13:45 -04001204 * If so, pull the packets off the frames and put them to
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001205 * the handoff node.
1206 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001207static inline int
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001208vlib_frame_queue_dequeue_internal (vlib_main_t * vm,
1209 vlib_frame_queue_main_t * fqm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001210{
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001211 u32 thread_id = vm->cpu_index;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001212 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001213 vlib_frame_queue_elt_t *elt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001214 u32 *from, *to;
1215 vlib_frame_t *f;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001216 int msg_type;
1217 int processed = 0;
1218 u32 n_left_to_node;
1219 u32 vectors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001220
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001221 ASSERT (fq);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001222 ASSERT (vm == vlib_mains[thread_id]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001224 if (PREDICT_FALSE (fqm->node_index == ~0))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001225 return 0;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001226 /*
1227 * Gather trace data for frame queues
1228 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001229 if (PREDICT_FALSE (fq->trace))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230 {
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001231 frame_queue_trace_t *fqt;
1232 frame_queue_nelt_counter_t *fqh;
1233 u32 elix;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001234
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001235 fqt = &fqm->frame_queue_traces[thread_id];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001236
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001237 fqt->nelts = fq->nelts;
1238 fqt->head = fq->head;
1239 fqt->head_hint = fq->head_hint;
1240 fqt->tail = fq->tail;
1241 fqt->threshold = fq->vector_threshold;
1242 fqt->n_in_use = fqt->tail - fqt->head;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001243 if (fqt->n_in_use >= fqt->nelts)
1244 {
1245 // if beyond max then use max
1246 fqt->n_in_use = fqt->nelts - 1;
1247 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001249 /* Record the number of elements in use in the histogram */
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001250 fqh = &fqm->frame_queue_histogram[thread_id];
Dave Barach9b8ffd92016-07-08 08:13:45 -04001251 fqh->count[fqt->n_in_use]++;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001252
1253 /* Record a snapshot of the elements in use */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001254 for (elix = 0; elix < fqt->nelts; elix++)
1255 {
1256 elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
1257 if (1 || elt->valid)
1258 {
1259 fqt->n_vectors[elix] = elt->n_vectors;
1260 }
1261 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001262 fqt->written = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001264
1265 while (1)
1266 {
1267 if (fq->head == fq->tail)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001268 {
1269 fq->head_hint = fq->head;
1270 return processed;
1271 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001272
Dave Barach9b8ffd92016-07-08 08:13:45 -04001273 elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001274
1275 if (!elt->valid)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001276 {
1277 fq->head_hint = fq->head;
1278 return processed;
1279 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001280
1281 from = elt->buffer_index;
1282 msg_type = elt->msg_type;
1283
1284 ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
1285 ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
1286
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001287 f = vlib_get_frame_to_node (vm, fqm->node_index);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001288
1289 to = vlib_frame_vector_args (f);
1290
1291 n_left_to_node = elt->n_vectors;
1292
1293 while (n_left_to_node >= 4)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001294 {
1295 to[0] = from[0];
1296 to[1] = from[1];
1297 to[2] = from[2];
1298 to[3] = from[3];
1299 to += 4;
1300 from += 4;
1301 n_left_to_node -= 4;
1302 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001303
1304 while (n_left_to_node > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001305 {
1306 to[0] = from[0];
1307 to++;
1308 from++;
1309 n_left_to_node--;
1310 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001311
1312 vectors += elt->n_vectors;
1313 f->n_vectors = elt->n_vectors;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001314 vlib_put_frame_to_node (vm, fqm->node_index, f);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001315
1316 elt->valid = 0;
1317 elt->n_vectors = 0;
1318 elt->msg_type = 0xfefefefe;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001319 CLIB_MEMORY_BARRIER ();
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001320 fq->head++;
1321 processed++;
1322
1323 /*
1324 * Limit the number of packets pushed into the graph
1325 */
1326 if (vectors >= fq->vector_threshold)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001327 {
1328 fq->head_hint = fq->head;
1329 return processed;
1330 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001331 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001332 ASSERT (0);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001333 return processed;
1334}
1335
1336static_always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001337vlib_worker_thread_internal (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001338{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001339 vlib_node_main_t *nm = &vm->node_main;
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001340 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001341 u64 cpu_time_now = clib_cpu_time_now ();
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001342 vlib_frame_queue_main_t *fqm;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001343
Damjan Marion5d447a92016-09-06 22:30:16 +02001344 vec_alloc (nm->pending_interrupt_node_runtime_indices, 32);
1345
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001346 while (1)
1347 {
1348 vlib_worker_thread_barrier_check ();
1349
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001350 vec_foreach (fqm, tm->frame_queue_mains)
1351 vlib_frame_queue_dequeue_internal (vm, fqm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001352
Dave Barach9b8ffd92016-07-08 08:13:45 -04001353 vlib_node_runtime_t *n;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001354 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT])
Dave Barach9b8ffd92016-07-08 08:13:45 -04001355 {
1356 cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1357 VLIB_NODE_STATE_POLLING, /* frame */ 0,
1358 cpu_time_now);
1359 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001360
Damjan Marion5d447a92016-09-06 22:30:16 +02001361 /* Next handle interrupts. */
1362 {
1363 uword l = _vec_len (nm->pending_interrupt_node_runtime_indices);
1364 uword i;
1365 if (l > 0)
1366 {
1367 _vec_len (nm->pending_interrupt_node_runtime_indices) = 0;
1368 for (i = 0; i < l; i++)
1369 {
1370 n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1371 nm->
1372 pending_interrupt_node_runtime_indices
1373 [i]);
1374 cpu_time_now =
1375 dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1376 VLIB_NODE_STATE_INTERRUPT,
1377 /* frame */ 0,
1378 cpu_time_now);
1379 }
1380 }
1381 }
1382
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001383 if (_vec_len (nm->pending_frames))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001384 {
1385 int i;
1386 cpu_time_now = clib_cpu_time_now ();
1387 for (i = 0; i < _vec_len (nm->pending_frames); i++)
1388 {
1389 vlib_pending_frame_t *p;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001390
Dave Barach9b8ffd92016-07-08 08:13:45 -04001391 p = nm->pending_frames + i;
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001392
Dave Barach9b8ffd92016-07-08 08:13:45 -04001393 cpu_time_now = dispatch_pending_node (vm, p, cpu_time_now);
1394 }
1395 _vec_len (nm->pending_frames) = 0;
1396 }
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001397 vlib_increment_main_loop_counter (vm);
1398
1399 /* Record time stamp in case there are no enabled nodes and above
1400 calls do not update time stamp. */
1401 cpu_time_now = clib_cpu_time_now ();
1402 }
1403}
1404
Dave Barach9b8ffd92016-07-08 08:13:45 -04001405void
1406vlib_worker_thread_fn (void *arg)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001407{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001408 vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg;
Damjan Marion878c6092017-01-04 13:19:27 +01001409 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -04001410 vlib_main_t *vm = vlib_get_main ();
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001411
Dave Barach9b8ffd92016-07-08 08:13:45 -04001412 ASSERT (vm->cpu_index == os_get_cpu_number ());
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001413
1414 vlib_worker_thread_init (w);
1415 clib_time_init (&vm->clib_time);
1416 clib_mem_set_heap (w->thread_mheap);
1417
1418 /* Wait until the dpdk init sequence is complete */
Damjan Marion878c6092017-01-04 13:19:27 +01001419 while (tm->extern_thread_mgmt && tm->worker_thread_release == 0)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001420 vlib_worker_thread_barrier_check ();
1421
Dave Barach9b8ffd92016-07-08 08:13:45 -04001422 vlib_worker_thread_internal (vm);
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001423}
1424
Dave Barach9b8ffd92016-07-08 08:13:45 -04001425/* *INDENT-OFF* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001426VLIB_REGISTER_THREAD (worker_thread_reg, static) = {
1427 .name = "workers",
1428 .short_name = "wk",
1429 .function = vlib_worker_thread_fn,
1430};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001431/* *INDENT-ON* */
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001432
Damjan Marionaaef1eb2016-11-08 17:37:01 +01001433u32
1434vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts)
1435{
1436 vlib_thread_main_t *tm = vlib_get_thread_main ();
1437 vlib_frame_queue_main_t *fqm;
1438 vlib_frame_queue_t *fq;
1439 int i;
1440
1441 if (frame_queue_nelts == 0)
1442 frame_queue_nelts = FRAME_QUEUE_NELTS;
1443
1444 vec_add2 (tm->frame_queue_mains, fqm, 1);
1445
1446 fqm->node_index = node_index;
1447
1448 vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
1449 _vec_len (fqm->vlib_frame_queues) = 0;
1450 for (i = 0; i < tm->n_vlib_mains; i++)
1451 {
1452 fq = vlib_frame_queue_alloc (frame_queue_nelts);
1453 vec_add1 (fqm->vlib_frame_queues, fq);
1454 }
1455
1456 return (fqm - tm->frame_queue_mains);
1457}
1458
Damjan Marion878c6092017-01-04 13:19:27 +01001459
1460int
1461vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb)
1462{
1463 vlib_thread_main_t *tm = vlib_get_thread_main ();
1464
1465 if (tm->extern_thread_mgmt)
1466 return -1;
1467
1468 tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb;
1469 tm->extern_thread_mgmt = 1;
1470 return 0;
1471}
1472
Dave Barach9b8ffd92016-07-08 08:13:45 -04001473clib_error_t *
1474threads_init (vlib_main_t * vm)
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001475{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476 return 0;
1477}
1478
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001479VLIB_INIT_FUNCTION (threads_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001480
1481/*
1482 * fd.io coding-style-patch-verification: ON
1483 *
1484 * Local Variables:
1485 * eval: (c-set-style "gnu")
1486 * End:
1487 */