Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 16 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | #include <signal.h> |
| 18 | #include <math.h> |
| 19 | #include <vppinfra/format.h> |
| 20 | #include <vlib/vlib.h> |
| 21 | |
| 22 | #include <vlib/threads.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | #include <vlib/unix/cj.h> |
| 24 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | DECLARE_CJ_GLOBAL_LOG; |
| 26 | |
| 27 | #define FRAME_QUEUE_NELTS 32 |
| 28 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 29 | u32 |
| 30 | vl (void *p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | { |
| 32 | return vec_len (p); |
| 33 | } |
| 34 | |
Damjan Marion | 6a7acc2 | 2016-12-19 16:28:36 +0100 | [diff] [blame] | 35 | vlib_worker_thread_t *vlib_worker_threads; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | vlib_thread_main_t vlib_thread_main; |
| 37 | |
| 38 | uword |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 39 | os_get_nthreads (void) |
Dave Barach | 01d86c7 | 2016-08-08 15:13:42 -0400 | [diff] [blame] | 40 | { |
| 41 | u32 len; |
| 42 | |
| 43 | len = vec_len (vlib_thread_stacks); |
| 44 | if (len == 0) |
| 45 | return 1; |
| 46 | else |
| 47 | return len; |
| 48 | } |
| 49 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | void |
| 51 | vlib_set_thread_name (char *name) |
| 52 | { |
| 53 | int pthread_setname_np (pthread_t __target_thread, const char *__name); |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 54 | int rv; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 55 | pthread_t thread = pthread_self (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 57 | if (thread) |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 58 | { |
| 59 | rv = pthread_setname_np (thread, name); |
| 60 | if (rv) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 61 | clib_warning ("pthread_setname_np returned %d", rv); |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 62 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 65 | static int |
| 66 | sort_registrations_by_no_clone (void *a0, void *a1) |
| 67 | { |
| 68 | vlib_thread_registration_t **tr0 = a0; |
| 69 | vlib_thread_registration_t **tr1 = a1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 71 | return ((i32) ((*tr0)->no_data_structure_clone) |
| 72 | - ((i32) ((*tr1)->no_data_structure_clone))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static uword * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 76 | vlib_sysfs_list_to_bitmap (char *filename) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | { |
| 78 | FILE *fp; |
| 79 | uword *r = 0; |
| 80 | |
| 81 | fp = fopen (filename, "r"); |
| 82 | |
| 83 | if (fp != NULL) |
| 84 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 85 | u8 *buffer = 0; |
| 86 | vec_validate (buffer, 256 - 1); |
| 87 | if (fgets ((char *) buffer, 256, fp)) |
| 88 | { |
| 89 | unformat_input_t in; |
| 90 | unformat_init_string (&in, (char *) buffer, |
| 91 | strlen ((char *) buffer)); |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 92 | if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 93 | clib_warning ("unformat_bitmap_list failed"); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 94 | unformat_free (&in); |
| 95 | } |
| 96 | vec_free (buffer); |
| 97 | fclose (fp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | } |
| 99 | return r; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /* Called early in the init sequence */ |
| 104 | |
| 105 | clib_error_t * |
| 106 | vlib_thread_init (vlib_main_t * vm) |
| 107 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 108 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 109 | vlib_worker_thread_t *w; |
| 110 | vlib_thread_registration_t *tr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | u32 n_vlib_mains = 1; |
| 112 | u32 first_index = 1; |
| 113 | u32 i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 114 | uword *avail_cpu; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | |
| 116 | /* get bitmaps of active cpu cores and sockets */ |
| 117 | tm->cpu_core_bitmap = |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 118 | vlib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | tm->cpu_socket_bitmap = |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 120 | vlib_sysfs_list_to_bitmap ("/sys/devices/system/node/online"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 122 | avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | |
| 124 | /* skip cores */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 125 | for (i = 0; i < tm->skip_cores; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 127 | uword c = clib_bitmap_first_set (avail_cpu); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | if (c == ~0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 129 | return clib_error_return (0, "no available cpus to skip"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 131 | avail_cpu = clib_bitmap_set (avail_cpu, c, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* grab cpu for main thread */ |
| 135 | if (!tm->main_lcore) |
| 136 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 137 | tm->main_lcore = clib_bitmap_first_set (avail_cpu); |
| 138 | if (tm->main_lcore == (u8) ~ 0) |
| 139 | return clib_error_return (0, "no available cpus to be used for the" |
| 140 | " main thread"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | } |
| 142 | else |
| 143 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 144 | if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0) |
| 145 | return clib_error_return (0, "cpu %u is not available to be used" |
| 146 | " for the main thread", tm->main_lcore); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 148 | avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
| 150 | /* assume that there is socket 0 only if there is no data from sysfs */ |
| 151 | if (!tm->cpu_socket_bitmap) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 152 | tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 154 | /* pin main thread to main_lcore */ |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 155 | if (tm->cb.vlib_thread_set_lcore_cb) |
| 156 | { |
| 157 | tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | cpu_set_t cpuset; |
| 162 | CPU_ZERO (&cpuset); |
| 163 | CPU_SET (tm->main_lcore, &cpuset); |
| 164 | pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset); |
| 165 | } |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 166 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | /* as many threads as stacks... */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 168 | vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1, |
| 169 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
| 171 | /* Preallocate thread 0 */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 172 | _vec_len (vlib_worker_threads) = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | w = vlib_worker_threads; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 174 | w->thread_mheap = clib_mem_get_heap (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | w->thread_stack = vlib_thread_stacks[0]; |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 176 | w->lcore_id = tm->main_lcore; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 177 | w->lwp = syscall (SYS_gettid); |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 178 | w->thread_id = pthread_self (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | tm->n_vlib_mains = 1; |
| 180 | |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 181 | if (tm->sched_policy != ~0) |
| 182 | { |
| 183 | struct sched_param sched_param; |
| 184 | if (!sched_getparam (w->lwp, &sched_param)) |
| 185 | { |
| 186 | if (tm->sched_priority != ~0) |
| 187 | sched_param.sched_priority = tm->sched_priority; |
| 188 | sched_setscheduler (w->lwp, tm->sched_policy, &sched_param); |
| 189 | } |
| 190 | } |
| 191 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | /* assign threads to cores and set n_vlib_mains */ |
| 193 | tr = tm->next; |
| 194 | |
| 195 | while (tr) |
| 196 | { |
| 197 | vec_add1 (tm->registrations, tr); |
| 198 | tr = tr->next; |
| 199 | } |
| 200 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 201 | vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
| 203 | for (i = 0; i < vec_len (tm->registrations); i++) |
| 204 | { |
| 205 | int j; |
| 206 | tr = tm->registrations[i]; |
| 207 | tr->first_index = first_index; |
| 208 | first_index += tr->count; |
| 209 | n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0; |
| 210 | |
| 211 | /* construct coremask */ |
| 212 | if (tr->use_pthreads || !tr->count) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 213 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | |
| 215 | if (tr->coremask) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 216 | { |
| 217 | uword c; |
| 218 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | clib_bitmap_foreach (c, tr->coremask, ({ |
| 220 | if (clib_bitmap_get(avail_cpu, c) == 0) |
| 221 | return clib_error_return (0, "cpu %u is not available to be used" |
| 222 | " for the '%s' thread",c, tr->name); |
| 223 | |
| 224 | avail_cpu = clib_bitmap_set(avail_cpu, c, 0); |
| 225 | })); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 226 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 228 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 230 | { |
| 231 | for (j = 0; j < tr->count; j++) |
| 232 | { |
| 233 | uword c = clib_bitmap_first_set (avail_cpu); |
| 234 | if (c == ~0) |
| 235 | return clib_error_return (0, |
| 236 | "no available cpus to be used for" |
| 237 | " the '%s' thread", tr->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 239 | avail_cpu = clib_bitmap_set (avail_cpu, c, 0); |
| 240 | tr->coremask = clib_bitmap_set (tr->coremask, c, 1); |
| 241 | } |
| 242 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 245 | clib_bitmap_free (avail_cpu); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | |
| 247 | tm->n_vlib_mains = n_vlib_mains; |
| 248 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 249 | vec_validate_aligned (vlib_worker_threads, first_index - 1, |
| 250 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 255 | vlib_frame_queue_t * |
| 256 | vlib_frame_queue_alloc (int nelts) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 258 | vlib_frame_queue_t *fq; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 260 | fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | memset (fq, 0, sizeof (*fq)); |
| 262 | fq->nelts = nelts; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 263 | fq->vector_threshold = 128; // packets |
| 264 | vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | |
| 266 | if (1) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 267 | { |
| 268 | if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1)) |
| 269 | fformat (stderr, "WARNING: fq->tail unaligned\n"); |
| 270 | if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1)) |
| 271 | fformat (stderr, "WARNING: fq->head unaligned\n"); |
| 272 | if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1)) |
| 273 | fformat (stderr, "WARNING: fq->elts unaligned\n"); |
| 274 | |
| 275 | if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES) |
| 276 | fformat (stderr, "WARNING: fq->elts[0] size %d\n", |
| 277 | sizeof (fq->elts[0])); |
| 278 | if (nelts & (nelts - 1)) |
| 279 | { |
| 280 | fformat (stderr, "FATAL: nelts MUST be a power of 2\n"); |
| 281 | abort (); |
| 282 | } |
| 283 | } |
| 284 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | return (fq); |
| 286 | } |
| 287 | |
| 288 | void vl_msg_api_handler_no_free (void *) __attribute__ ((weak)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 289 | void |
| 290 | vl_msg_api_handler_no_free (void *v) |
| 291 | { |
| 292 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | |
| 294 | /* Turned off, save as reference material... */ |
| 295 | #if 0 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 296 | static inline int |
| 297 | vlib_frame_queue_dequeue_internal (int thread_id, |
| 298 | vlib_main_t * vm, vlib_node_main_t * nm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | { |
| 300 | vlib_frame_queue_t *fq = vlib_frame_queues[thread_id]; |
| 301 | vlib_frame_queue_elt_t *elt; |
| 302 | vlib_frame_t *f; |
| 303 | vlib_pending_frame_t *p; |
| 304 | vlib_node_runtime_t *r; |
| 305 | u32 node_runtime_index; |
| 306 | int msg_type; |
| 307 | u64 before; |
| 308 | int processed = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 309 | |
| 310 | ASSERT (vm == vlib_mains[thread_id]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | |
| 312 | while (1) |
| 313 | { |
| 314 | if (fq->head == fq->tail) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 315 | return processed; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 317 | elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 318 | |
| 319 | if (!elt->valid) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 320 | return processed; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 322 | before = clib_cpu_time_now (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | |
| 324 | f = elt->frame; |
| 325 | node_runtime_index = elt->node_runtime_index; |
| 326 | msg_type = elt->msg_type; |
| 327 | |
| 328 | switch (msg_type) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 329 | { |
| 330 | case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS: |
| 331 | vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors); |
| 332 | /* note fallthrough... */ |
| 333 | case VLIB_FRAME_QUEUE_ELT_FREE_FRAME: |
| 334 | r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL], |
| 335 | node_runtime_index); |
| 336 | vlib_frame_free (vm, r, f); |
| 337 | break; |
| 338 | case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME: |
| 339 | vec_add2 (vm->node_main.pending_frames, p, 1); |
| 340 | f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH); |
| 341 | p->node_runtime_index = elt->node_runtime_index; |
| 342 | p->frame_index = vlib_frame_index (vm, f); |
| 343 | p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME; |
| 344 | fq->dequeue_vectors += (u64) f->n_vectors; |
| 345 | break; |
| 346 | case VLIB_FRAME_QUEUE_ELT_API_MSG: |
| 347 | vl_msg_api_handler_no_free (f); |
| 348 | break; |
| 349 | default: |
| 350 | clib_warning ("bogus frame queue message, type %d", msg_type); |
| 351 | break; |
| 352 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | elt->valid = 0; |
| 354 | fq->dequeues++; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 355 | fq->dequeue_ticks += clib_cpu_time_now () - before; |
| 356 | CLIB_MEMORY_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | fq->head++; |
| 358 | processed++; |
| 359 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 360 | ASSERT (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | return processed; |
| 362 | } |
| 363 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 364 | int |
| 365 | vlib_frame_queue_dequeue (int thread_id, |
| 366 | vlib_main_t * vm, vlib_node_main_t * nm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | { |
| 368 | return vlib_frame_queue_dequeue_internal (thread_id, vm, nm); |
| 369 | } |
| 370 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 371 | int |
| 372 | vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index, |
| 373 | u32 frame_queue_index, vlib_frame_t * frame, |
| 374 | vlib_frame_queue_msg_type_t type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | { |
| 376 | vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index]; |
| 377 | vlib_frame_queue_elt_t *elt; |
| 378 | u32 save_count; |
| 379 | u64 new_tail; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 380 | u64 before = clib_cpu_time_now (); |
| 381 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | ASSERT (fq); |
| 383 | |
| 384 | new_tail = __sync_add_and_fetch (&fq->tail, 1); |
| 385 | |
| 386 | /* Wait until a ring slot is available */ |
| 387 | while (new_tail >= fq->head + fq->nelts) |
| 388 | { |
| 389 | f64 b4 = vlib_time_now_ticks (vm, before); |
| 390 | vlib_worker_thread_barrier_check (vm, b4); |
| 391 | /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 392 | // vlib_frame_queue_dequeue (vm->thread_index, vm, nm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 395 | elt = fq->elts + (new_tail & (fq->nelts - 1)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | |
| 397 | /* this would be very bad... */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 398 | while (elt->valid) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | { |
| 400 | } |
| 401 | |
| 402 | /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */ |
| 403 | save_count = frame->n_vectors; |
| 404 | |
| 405 | elt->frame = frame; |
| 406 | elt->node_runtime_index = node_runtime_index; |
| 407 | elt->msg_type = type; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 408 | CLIB_MEMORY_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | elt->valid = 1; |
| 410 | |
| 411 | return save_count; |
| 412 | } |
| 413 | #endif /* 0 */ |
| 414 | |
| 415 | /* To be called by vlib worker threads upon startup */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 416 | void |
| 417 | vlib_worker_thread_init (vlib_worker_thread_t * w) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 419 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 420 | |
Dave Barach | fdf4944 | 2016-12-20 12:48:14 -0500 | [diff] [blame] | 421 | /* |
| 422 | * Note: disabling signals in worker threads as follows |
| 423 | * prevents the api post-mortem dump scheme from working |
| 424 | * { |
| 425 | * sigset_t s; |
| 426 | * sigfillset (&s); |
| 427 | * pthread_sigmask (SIG_SETMASK, &s, 0); |
| 428 | * } |
| 429 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 430 | |
| 431 | clib_mem_set_heap (w->thread_mheap); |
| 432 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 433 | if (vec_len (tm->thread_prefix) && w->registration->short_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 435 | w->name = format (0, "%v_%s_%d%c", tm->thread_prefix, |
| 436 | w->registration->short_name, w->instance_id, '\0'); |
| 437 | vlib_set_thread_name ((char *) w->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if (!w->registration->use_pthreads) |
| 441 | { |
| 442 | |
| 443 | /* Initial barrier sync, for both worker and i/o threads */ |
| 444 | clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1); |
| 445 | |
| 446 | while (*vlib_worker_threads->wait_at_barrier) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 447 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | |
| 449 | clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1); |
| 450 | } |
| 451 | } |
| 452 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 453 | void * |
| 454 | vlib_worker_thread_bootstrap_fn (void *arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | { |
| 456 | void *rv; |
| 457 | vlib_worker_thread_t *w = arg; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 458 | |
| 459 | w->lwp = syscall (SYS_gettid); |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 460 | w->thread_id = pthread_self (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 462 | __os_thread_index = w - vlib_worker_threads; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 463 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 464 | rv = (void *) clib_calljmp |
| 465 | ((uword (*)(uword)) w->thread_function, |
| 466 | (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 467 | /* NOTREACHED, we hope */ |
| 468 | return rv; |
| 469 | } |
| 470 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 471 | static clib_error_t * |
| 472 | vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 473 | { |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 474 | vlib_thread_main_t *tm = &vlib_thread_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 475 | void *(*fp_arg) (void *) = fp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 476 | |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 477 | w->lcore_id = lcore_id; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 478 | if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads) |
| 479 | return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 480 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 481 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 482 | pthread_t worker; |
| 483 | cpu_set_t cpuset; |
| 484 | CPU_ZERO (&cpuset); |
| 485 | CPU_SET (lcore_id, &cpuset); |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 486 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 487 | if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w)) |
| 488 | return clib_error_return_unix (0, "pthread_create"); |
| 489 | |
| 490 | if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset)) |
| 491 | return clib_error_return_unix (0, "pthread_setaffinity_np"); |
| 492 | |
| 493 | return 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 494 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 497 | static clib_error_t * |
| 498 | start_workers (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | { |
| 500 | int i, j; |
| 501 | vlib_worker_thread_t *w; |
| 502 | vlib_main_t *vm_clone; |
| 503 | void *oldheap; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 504 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 505 | vlib_thread_registration_t *tr; |
| 506 | vlib_node_runtime_t *rt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | u32 n_vlib_mains = tm->n_vlib_mains; |
| 508 | u32 worker_thread_index; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 509 | u8 *main_heap = clib_mem_get_per_cpu_heap (); |
| 510 | mheap_t *main_heap_header = mheap_header (main_heap); |
| 511 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | vec_reset_length (vlib_worker_threads); |
| 513 | |
| 514 | /* Set up the main thread */ |
| 515 | vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES); |
Dave Barach | 3e7deb1 | 2016-02-05 16:29:53 -0500 | [diff] [blame] | 516 | w->elog_track.name = "main thread"; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | elog_track_register (&vm->elog_main, &w->elog_track); |
| 518 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 519 | if (vec_len (tm->thread_prefix)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 521 | w->name = format (0, "%v_main%c", tm->thread_prefix, '\0'); |
| 522 | vlib_set_thread_name ((char *) w->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 525 | /* |
Dave Barach | 848191d | 2016-04-28 16:24:15 -0400 | [diff] [blame] | 526 | * Truth of the matter: we always use at least two |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 527 | * threads. So, make the main heap thread-safe |
Dave Barach | 848191d | 2016-04-28 16:24:15 -0400 | [diff] [blame] | 528 | * and make the event log thread-safe. |
| 529 | */ |
| 530 | main_heap_header->flags |= MHEAP_FLAG_THREAD_SAFE; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 531 | vm->elog_main.lock = |
| 532 | clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES); |
Dave Barach | 848191d | 2016-04-28 16:24:15 -0400 | [diff] [blame] | 533 | vm->elog_main.lock[0] = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 534 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | if (n_vlib_mains > 1) |
| 536 | { |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 537 | /* Replace hand-crafted length-1 vector with a real vector */ |
| 538 | vlib_mains = 0; |
| 539 | |
| 540 | vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1, |
| 541 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | _vec_len (vlib_mains) = 0; |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 543 | vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 544 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 545 | vlib_worker_threads->wait_at_barrier = |
| 546 | clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | vlib_worker_threads->workers_at_barrier = |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 548 | clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | |
| 550 | /* Ask for an initial barrier sync */ |
| 551 | *vlib_worker_threads->workers_at_barrier = 0; |
| 552 | *vlib_worker_threads->wait_at_barrier = 1; |
| 553 | |
| 554 | worker_thread_index = 1; |
| 555 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 556 | for (i = 0; i < vec_len (tm->registrations); i++) |
| 557 | { |
| 558 | vlib_node_main_t *nm, *nm_clone; |
| 559 | vlib_buffer_main_t *bm_clone; |
| 560 | vlib_buffer_free_list_t *fl_clone, *fl_orig; |
| 561 | vlib_buffer_free_list_t *orig_freelist_pool; |
| 562 | int k; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 564 | tr = tm->registrations[i]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 565 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 566 | if (tr->count == 0) |
| 567 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 568 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 569 | for (k = 0; k < tr->count; k++) |
| 570 | { |
| 571 | vec_add2 (vlib_worker_threads, w, 1); |
| 572 | if (tr->mheap_size) |
| 573 | w->thread_mheap = |
| 574 | mheap_alloc (0 /* use VM */ , tr->mheap_size); |
| 575 | else |
| 576 | w->thread_mheap = main_heap; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 577 | |
| 578 | w->thread_stack = |
| 579 | vlib_thread_stack_init (w - vlib_worker_threads); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 580 | w->thread_function = tr->function; |
| 581 | w->thread_function_arg = w; |
| 582 | w->instance_id = k; |
| 583 | w->registration = tr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 584 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 585 | w->elog_track.name = |
| 586 | (char *) format (0, "%s %d", tr->name, k + 1); |
| 587 | vec_add1 (w->elog_track.name, 0); |
| 588 | elog_track_register (&vm->elog_main, &w->elog_track); |
Bud Grise | 68adab9 | 2016-02-12 10:36:11 -0500 | [diff] [blame] | 589 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 590 | if (tr->no_data_structure_clone) |
| 591 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 593 | /* Fork vlib_global_main et al. Look for bugs here */ |
| 594 | oldheap = clib_mem_set_heap (w->thread_mheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 595 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 596 | vm_clone = clib_mem_alloc (sizeof (*vm_clone)); |
| 597 | clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 599 | vm_clone->thread_index = worker_thread_index; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 600 | vm_clone->heap_base = w->thread_mheap; |
| 601 | vm_clone->mbuf_alloc_list = 0; |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 602 | vm_clone->init_functions_called = |
| 603 | hash_create (0, /* value bytes */ 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 604 | memset (&vm_clone->random_buffer, 0, |
| 605 | sizeof (vm_clone->random_buffer)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 606 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 607 | nm = &vlib_mains[0]->node_main; |
| 608 | nm_clone = &vm_clone->node_main; |
| 609 | /* fork next frames array, preserving node runtime indices */ |
| 610 | nm_clone->next_frames = vec_dup (nm->next_frames); |
| 611 | for (j = 0; j < vec_len (nm_clone->next_frames); j++) |
| 612 | { |
| 613 | vlib_next_frame_t *nf = &nm_clone->next_frames[j]; |
| 614 | u32 save_node_runtime_index; |
| 615 | u32 save_flags; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 616 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 617 | save_node_runtime_index = nf->node_runtime_index; |
| 618 | save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH; |
| 619 | vlib_next_frame_init (nf); |
| 620 | nf->node_runtime_index = save_node_runtime_index; |
| 621 | nf->flags = save_flags; |
| 622 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 623 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 624 | /* fork the frame dispatch queue */ |
| 625 | nm_clone->pending_frames = 0; |
| 626 | vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */ |
| 627 | _vec_len (nm_clone->pending_frames) = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 628 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 629 | /* fork nodes */ |
| 630 | nm_clone->nodes = 0; |
| 631 | for (j = 0; j < vec_len (nm->nodes); j++) |
| 632 | { |
| 633 | vlib_node_t *n; |
| 634 | n = clib_mem_alloc_no_fail (sizeof (*n)); |
| 635 | clib_memcpy (n, nm->nodes[j], sizeof (*n)); |
| 636 | /* none of the copied nodes have enqueue rights given out */ |
| 637 | n->owner_node_index = VLIB_INVALID_NODE_INDEX; |
| 638 | memset (&n->stats_total, 0, sizeof (n->stats_total)); |
| 639 | memset (&n->stats_last_clear, 0, |
| 640 | sizeof (n->stats_last_clear)); |
| 641 | vec_add1 (nm_clone->nodes, n); |
| 642 | } |
| 643 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] = |
| 644 | vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]); |
JingLiuZTE | 30af5da | 2017-07-24 10:53:31 +0800 | [diff] [blame] | 645 | vec_foreach (rt, |
| 646 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 647 | { |
| 648 | vlib_node_t *n = vlib_get_node (vm, rt->node_index); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 649 | rt->thread_index = vm_clone->thread_index; |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 650 | /* copy initial runtime_data from node */ |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 651 | if (n->runtime_data && n->runtime_data_bytes > 0) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 652 | clib_memcpy (rt->runtime_data, n->runtime_data, |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 653 | clib_min (VLIB_NODE_RUNTIME_DATA_SIZE, |
| 654 | n->runtime_data_bytes)); |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 655 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 657 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] = |
| 658 | vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]); |
| 659 | vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 660 | { |
| 661 | vlib_node_t *n = vlib_get_node (vm, rt->node_index); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 662 | rt->thread_index = vm_clone->thread_index; |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 663 | /* copy initial runtime_data from node */ |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 664 | if (n->runtime_data && n->runtime_data_bytes > 0) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 665 | clib_memcpy (rt->runtime_data, n->runtime_data, |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 666 | clib_min (VLIB_NODE_RUNTIME_DATA_SIZE, |
| 667 | n->runtime_data_bytes)); |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 668 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 669 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 670 | nm_clone->processes = vec_dup (nm->processes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 671 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 672 | /* zap the (per worker) frame freelists, etc */ |
| 673 | nm_clone->frame_sizes = 0; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 674 | nm_clone->frame_size_hash = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 675 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 676 | /* Packet trace buffers are guaranteed to be empty, nothing to do here */ |
Damjan Marion | bc20bdf | 2015-12-17 14:28:18 +0100 | [diff] [blame] | 677 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 678 | clib_mem_set_heap (oldheap); |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 679 | vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 680 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 681 | vm_clone->error_main.counters = |
| 682 | vec_dup (vlib_mains[0]->error_main.counters); |
| 683 | vm_clone->error_main.counters_last_clear = |
| 684 | vec_dup (vlib_mains[0]->error_main.counters_last_clear); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 685 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 686 | /* Fork the vlib_buffer_main_t free lists, etc. */ |
| 687 | bm_clone = vec_dup (vm_clone->buffer_main); |
| 688 | vm_clone->buffer_main = bm_clone; |
| 689 | |
| 690 | orig_freelist_pool = bm_clone->buffer_free_list_pool; |
| 691 | bm_clone->buffer_free_list_pool = 0; |
| 692 | |
| 693 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 694 | pool_foreach (fl_orig, orig_freelist_pool, |
| 695 | ({ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 696 | pool_get_aligned (bm_clone->buffer_free_list_pool, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 697 | fl_clone, CLIB_CACHE_LINE_BYTES); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 698 | ASSERT (fl_orig - orig_freelist_pool |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 699 | == fl_clone - bm_clone->buffer_free_list_pool); |
| 700 | |
| 701 | fl_clone[0] = fl_orig[0]; |
Damjan Marion | bd69a5f | 2017-02-05 23:44:42 +0100 | [diff] [blame] | 702 | fl_clone->buffers = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 703 | fl_clone->n_alloc = 0; |
| 704 | })); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 705 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 707 | worker_thread_index++; |
| 708 | } |
| 709 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | } |
| 711 | else |
| 712 | { |
| 713 | /* only have non-data-structure copy threads to create... */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 714 | for (i = 0; i < vec_len (tm->registrations); i++) |
| 715 | { |
| 716 | tr = tm->registrations[i]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 717 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 718 | for (j = 0; j < tr->count; j++) |
| 719 | { |
| 720 | vec_add2 (vlib_worker_threads, w, 1); |
| 721 | if (tr->mheap_size) |
| 722 | w->thread_mheap = |
| 723 | mheap_alloc (0 /* use VM */ , tr->mheap_size); |
| 724 | else |
| 725 | w->thread_mheap = main_heap; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 726 | w->thread_stack = |
| 727 | vlib_thread_stack_init (w - vlib_worker_threads); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 728 | w->thread_function = tr->function; |
| 729 | w->thread_function_arg = w; |
| 730 | w->instance_id = j; |
| 731 | w->elog_track.name = |
| 732 | (char *) format (0, "%s %d", tr->name, j + 1); |
| 733 | w->registration = tr; |
| 734 | vec_add1 (w->elog_track.name, 0); |
| 735 | elog_track_register (&vm->elog_main, &w->elog_track); |
| 736 | } |
| 737 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | worker_thread_index = 1; |
| 741 | |
| 742 | for (i = 0; i < vec_len (tm->registrations); i++) |
| 743 | { |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 744 | clib_error_t *err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 745 | int j; |
| 746 | |
| 747 | tr = tm->registrations[i]; |
| 748 | |
| 749 | if (tr->use_pthreads || tm->use_pthreads) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 750 | { |
| 751 | for (j = 0; j < tr->count; j++) |
| 752 | { |
| 753 | w = vlib_worker_threads + worker_thread_index++; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 754 | err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn, |
| 755 | w, 0); |
| 756 | if (err) |
| 757 | clib_error_report (err); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 758 | } |
| 759 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 760 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 761 | { |
| 762 | uword c; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 763 | /* *INDENT-OFF* */ |
| 764 | clib_bitmap_foreach (c, tr->coremask, ({ |
| 765 | w = vlib_worker_threads + worker_thread_index++; |
| 766 | err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn, |
| 767 | w, c); |
| 768 | if (err) |
| 769 | clib_error_report (err); |
| 770 | })); |
| 771 | /* *INDENT-ON* */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 772 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 773 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 774 | vlib_worker_thread_barrier_sync (vm); |
| 775 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers); |
| 780 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 781 | void |
| 782 | vlib_worker_thread_node_runtime_update (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 783 | { |
| 784 | int i, j; |
| 785 | vlib_worker_thread_t *w; |
| 786 | vlib_main_t *vm; |
| 787 | vlib_node_main_t *nm, *nm_clone; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 788 | vlib_node_t **old_nodes_clone; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | vlib_main_t *vm_clone; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 790 | vlib_node_runtime_t *rt, *old_rt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 791 | void *oldheap; |
| 792 | never_inline void |
| 793 | vlib_node_runtime_sync_stats (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 794 | vlib_node_runtime_t * r, |
| 795 | uword n_calls, |
| 796 | uword n_vectors, uword n_clocks); |
| 797 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 798 | ASSERT (vlib_get_thread_index () == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 799 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 800 | if (vec_len (vlib_mains) == 1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | return; |
| 802 | |
| 803 | vm = vlib_mains[0]; |
| 804 | nm = &vm->node_main; |
| 805 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 806 | ASSERT (vlib_get_thread_index () == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 807 | ASSERT (*vlib_worker_threads->wait_at_barrier == 1); |
| 808 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 809 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 810 | * Scrape all runtime stats, so we don't lose node runtime(s) with |
| 811 | * pending counts, or throw away worker / io thread counts. |
| 812 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 813 | for (j = 0; j < vec_len (nm->nodes); j++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 814 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 815 | vlib_node_t *n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 816 | n = nm->nodes[j]; |
| 817 | vlib_node_sync_stats (vm, n); |
| 818 | } |
| 819 | |
| 820 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 821 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 822 | vlib_node_t *n; |
| 823 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 824 | vm_clone = vlib_mains[i]; |
| 825 | nm_clone = &vm_clone->node_main; |
| 826 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 827 | for (j = 0; j < vec_len (nm_clone->nodes); j++) |
| 828 | { |
| 829 | n = nm_clone->nodes[j]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 830 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 831 | rt = vlib_node_get_runtime (vm_clone, n->index); |
| 832 | vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0); |
| 833 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 837 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 838 | vlib_node_runtime_t *rt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 839 | w = vlib_worker_threads + i; |
| 840 | oldheap = clib_mem_set_heap (w->thread_mheap); |
Damjan Marion | bc20bdf | 2015-12-17 14:28:18 +0100 | [diff] [blame] | 841 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 842 | vm_clone = vlib_mains[i]; |
| 843 | |
| 844 | /* Re-clone error heap */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 845 | u64 *old_counters = vm_clone->error_main.counters; |
| 846 | u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear; |
| 847 | clib_memcpy (&vm_clone->error_main, &vm->error_main, |
| 848 | sizeof (vm->error_main)); |
| 849 | j = vec_len (vm->error_main.counters) - 1; |
| 850 | vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES); |
| 851 | vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | bc20bdf | 2015-12-17 14:28:18 +0100 | [diff] [blame] | 852 | vm_clone->error_main.counters = old_counters; |
| 853 | vm_clone->error_main.counters_last_clear = old_counters_all_clear; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 854 | |
| 855 | nm_clone = &vm_clone->node_main; |
| 856 | vec_free (nm_clone->next_frames); |
| 857 | nm_clone->next_frames = vec_dup (nm->next_frames); |
Damjan Marion | bc20bdf | 2015-12-17 14:28:18 +0100 | [diff] [blame] | 858 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 859 | for (j = 0; j < vec_len (nm_clone->next_frames); j++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 860 | { |
| 861 | vlib_next_frame_t *nf = &nm_clone->next_frames[j]; |
| 862 | u32 save_node_runtime_index; |
| 863 | u32 save_flags; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 864 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 865 | save_node_runtime_index = nf->node_runtime_index; |
| 866 | save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH; |
| 867 | vlib_next_frame_init (nf); |
| 868 | nf->node_runtime_index = save_node_runtime_index; |
| 869 | nf->flags = save_flags; |
| 870 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 871 | |
| 872 | old_nodes_clone = nm_clone->nodes; |
| 873 | nm_clone->nodes = 0; |
| 874 | |
| 875 | /* re-fork nodes */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 876 | for (j = 0; j < vec_len (nm->nodes); j++) |
| 877 | { |
| 878 | vlib_node_t *old_n_clone; |
| 879 | vlib_node_t *new_n, *new_n_clone; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 880 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 881 | new_n = nm->nodes[j]; |
| 882 | old_n_clone = old_nodes_clone[j]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 883 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 884 | new_n_clone = clib_mem_alloc_no_fail (sizeof (*new_n_clone)); |
| 885 | clib_memcpy (new_n_clone, new_n, sizeof (*new_n)); |
| 886 | /* none of the copied nodes have enqueue rights given out */ |
| 887 | new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 888 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 889 | if (j >= vec_len (old_nodes_clone)) |
| 890 | { |
| 891 | /* new node, set to zero */ |
| 892 | memset (&new_n_clone->stats_total, 0, |
| 893 | sizeof (new_n_clone->stats_total)); |
| 894 | memset (&new_n_clone->stats_last_clear, 0, |
| 895 | sizeof (new_n_clone->stats_last_clear)); |
| 896 | } |
| 897 | else |
| 898 | { |
| 899 | /* Copy stats if the old data is valid */ |
| 900 | clib_memcpy (&new_n_clone->stats_total, |
| 901 | &old_n_clone->stats_total, |
| 902 | sizeof (new_n_clone->stats_total)); |
| 903 | clib_memcpy (&new_n_clone->stats_last_clear, |
| 904 | &old_n_clone->stats_last_clear, |
| 905 | sizeof (new_n_clone->stats_last_clear)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 906 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 907 | /* keep previous node state */ |
| 908 | new_n_clone->state = old_n_clone->state; |
| 909 | } |
| 910 | vec_add1 (nm_clone->nodes, new_n_clone); |
| 911 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 912 | /* Free the old node clone */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 913 | for (j = 0; j < vec_len (old_nodes_clone); j++) |
| 914 | clib_mem_free (old_nodes_clone[j]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 915 | vec_free (old_nodes_clone); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 916 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 917 | |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 918 | /* re-clone internal nodes */ |
| 919 | old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 920 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] = |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 921 | vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 922 | |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 923 | vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 924 | { |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 925 | vlib_node_t *n = vlib_get_node (vm, rt->node_index); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 926 | rt->thread_index = vm_clone->thread_index; |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 927 | /* copy runtime_data, will be overwritten later for existing rt */ |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 928 | if (n->runtime_data && n->runtime_data_bytes > 0) |
| 929 | clib_memcpy (rt->runtime_data, n->runtime_data, |
| 930 | clib_min (VLIB_NODE_RUNTIME_DATA_SIZE, |
| 931 | n->runtime_data_bytes)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 932 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 933 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 934 | for (j = 0; j < vec_len (old_rt); j++) |
| 935 | { |
| 936 | rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index); |
| 937 | rt->state = old_rt[j].state; |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 938 | clib_memcpy (rt->runtime_data, old_rt[j].runtime_data, |
| 939 | VLIB_NODE_RUNTIME_DATA_SIZE); |
| 940 | } |
| 941 | |
| 942 | vec_free (old_rt); |
| 943 | |
| 944 | /* re-clone input nodes */ |
| 945 | old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]; |
| 946 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] = |
| 947 | vec_dup (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]); |
| 948 | |
| 949 | vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]) |
| 950 | { |
| 951 | vlib_node_t *n = vlib_get_node (vm, rt->node_index); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 952 | rt->thread_index = vm_clone->thread_index; |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 953 | /* copy runtime_data, will be overwritten later for existing rt */ |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 954 | if (n->runtime_data && n->runtime_data_bytes > 0) |
| 955 | clib_memcpy (rt->runtime_data, n->runtime_data, |
| 956 | clib_min (VLIB_NODE_RUNTIME_DATA_SIZE, |
| 957 | n->runtime_data_bytes)); |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | for (j = 0; j < vec_len (old_rt); j++) |
| 961 | { |
| 962 | rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index); |
| 963 | rt->state = old_rt[j].state; |
| 964 | clib_memcpy (rt->runtime_data, old_rt[j].runtime_data, |
| 965 | VLIB_NODE_RUNTIME_DATA_SIZE); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 966 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 967 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 968 | vec_free (old_rt); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 969 | |
| 970 | nm_clone->processes = vec_dup (nm->processes); |
| 971 | |
| 972 | clib_mem_set_heap (oldheap); |
| 973 | |
| 974 | // vnet_main_fork_fixup (i); |
| 975 | } |
| 976 | } |
| 977 | |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 978 | u32 |
| 979 | unformat_sched_policy (unformat_input_t * input, va_list * args) |
| 980 | { |
| 981 | u32 *r = va_arg (*args, u32 *); |
| 982 | |
| 983 | if (0); |
| 984 | #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f; |
| 985 | foreach_sched_policy |
| 986 | #undef _ |
| 987 | else |
| 988 | return 0; |
| 989 | return 1; |
| 990 | } |
| 991 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 992 | static clib_error_t * |
| 993 | cpu_config (vlib_main_t * vm, unformat_input_t * input) |
| 994 | { |
| 995 | vlib_thread_registration_t *tr; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 996 | uword *p; |
| 997 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 998 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 999 | u64 coremask; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1000 | uword *bitmap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1001 | u32 count; |
| 1002 | |
| 1003 | tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword)); |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1004 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1005 | tm->n_thread_stacks = 1; /* account for main thread */ |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1006 | tm->sched_policy = ~0; |
| 1007 | tm->sched_priority = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1008 | |
| 1009 | tr = tm->next; |
| 1010 | |
| 1011 | while (tr) |
| 1012 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1013 | hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1014 | tr = tr->next; |
| 1015 | } |
| 1016 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1017 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1018 | { |
Damjan Marion | bf74147 | 2016-06-13 22:49:44 +0200 | [diff] [blame] | 1019 | if (unformat (input, "use-pthreads")) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1020 | tm->use_pthreads = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1021 | else if (unformat (input, "thread-prefix %v", &tm->thread_prefix)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1022 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1023 | else if (unformat (input, "main-core %u", &tm->main_lcore)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1024 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1025 | else if (unformat (input, "skip-cores %u", &tm->skip_cores)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1026 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1027 | else if (unformat (input, "coremask-%s %llx", &name, &coremask)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1028 | { |
| 1029 | p = hash_get_mem (tm->thread_registrations_by_name, name); |
| 1030 | if (p == 0) |
| 1031 | return clib_error_return (0, "no such thread type '%s'", name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1032 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1033 | tr = (vlib_thread_registration_t *) p[0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1034 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1035 | if (tr->use_pthreads) |
| 1036 | return clib_error_return (0, |
| 1037 | "coremask cannot be set for '%s' threads", |
| 1038 | name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1039 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1040 | tr->coremask = clib_bitmap_set_multiple |
| 1041 | (tr->coremask, 0, coremask, BITS (coremask)); |
| 1042 | tr->count = clib_bitmap_count_set_bits (tr->coremask); |
| 1043 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1044 | else if (unformat (input, "corelist-%s %U", &name, unformat_bitmap_list, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1045 | &bitmap)) |
| 1046 | { |
| 1047 | p = hash_get_mem (tm->thread_registrations_by_name, name); |
| 1048 | if (p == 0) |
| 1049 | return clib_error_return (0, "no such thread type '%s'", name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1050 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1051 | tr = (vlib_thread_registration_t *) p[0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1052 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1053 | if (tr->use_pthreads) |
| 1054 | return clib_error_return (0, |
| 1055 | "corelist cannot be set for '%s' threads", |
| 1056 | name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1057 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1058 | tr->coremask = bitmap; |
| 1059 | tr->count = clib_bitmap_count_set_bits (tr->coremask); |
| 1060 | } |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1061 | else |
| 1062 | if (unformat |
| 1063 | (input, "scheduler-policy %U", unformat_sched_policy, |
| 1064 | &tm->sched_policy)) |
| 1065 | ; |
Pavel Kotucek | c08a1ed | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1066 | else if (unformat (input, "scheduler-priority %u", &tm->sched_priority)) |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1067 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1068 | else if (unformat (input, "%s %u", &name, &count)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1069 | { |
| 1070 | p = hash_get_mem (tm->thread_registrations_by_name, name); |
| 1071 | if (p == 0) |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1072 | return clib_error_return (0, "no such thread type 3 '%s'", name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1073 | |
| 1074 | tr = (vlib_thread_registration_t *) p[0]; |
| 1075 | if (tr->fixed_count) |
| 1076 | return clib_error_return |
| 1077 | (0, "number of %s threads not configurable", tr->name); |
| 1078 | tr->count = count; |
| 1079 | } |
| 1080 | else |
| 1081 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Pavel Kotucek | c08a1ed | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1084 | if (tm->sched_priority != ~0) |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1085 | { |
Pavel Kotucek | c08a1ed | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1086 | if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR) |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1087 | { |
| 1088 | u32 prio_max = sched_get_priority_max (tm->sched_policy); |
| 1089 | u32 prio_min = sched_get_priority_min (tm->sched_policy); |
| 1090 | if (tm->sched_priority > prio_max) |
| 1091 | tm->sched_priority = prio_max; |
| 1092 | if (tm->sched_priority < prio_min) |
| 1093 | tm->sched_priority = prio_min; |
| 1094 | } |
| 1095 | else |
Pavel Kotucek | c08a1ed | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1096 | { |
| 1097 | return clib_error_return |
| 1098 | (0, |
| 1099 | "scheduling priority (%d) is not allowed for `normal` scheduling policy", |
| 1100 | tm->sched_priority); |
| 1101 | } |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1102 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1103 | tr = tm->next; |
| 1104 | |
| 1105 | if (!tm->thread_prefix) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1106 | tm->thread_prefix = format (0, "vpp"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1107 | |
| 1108 | while (tr) |
| 1109 | { |
| 1110 | tm->n_thread_stacks += tr->count; |
| 1111 | tm->n_pthreads += tr->count * tr->use_pthreads; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1112 | tm->n_threads += tr->count * (tr->use_pthreads == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1113 | tr = tr->next; |
| 1114 | } |
| 1115 | |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu"); |
| 1120 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 1121 | #if !defined (__x86_64__) && !defined (__i386__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1122 | void |
| 1123 | __sync_fetch_and_add_8 (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1124 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1125 | fformat (stderr, "%s called\n", __FUNCTION__); |
| 1126 | abort (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1127 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1128 | |
| 1129 | void |
| 1130 | __sync_add_and_fetch_8 (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1131 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1132 | fformat (stderr, "%s called\n", __FUNCTION__); |
| 1133 | abort (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1134 | } |
| 1135 | #endif |
| 1136 | |
| 1137 | void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1138 | void |
| 1139 | vnet_main_fixup (vlib_fork_fixup_t which) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1140 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | void |
| 1144 | vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which) |
| 1145 | { |
| 1146 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1147 | |
| 1148 | if (vlib_mains == 0) |
| 1149 | return; |
| 1150 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1151 | ASSERT (vlib_get_thread_index () == 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1152 | vlib_worker_thread_barrier_sync (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1153 | |
| 1154 | switch (which) |
| 1155 | { |
| 1156 | case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX: |
| 1157 | vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX); |
| 1158 | break; |
| 1159 | |
| 1160 | default: |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1161 | ASSERT (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1162 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1163 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1166 | void |
| 1167 | vlib_worker_thread_barrier_sync (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1168 | { |
| 1169 | f64 deadline; |
| 1170 | u32 count; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1171 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 1172 | if (vec_len (vlib_mains) < 2) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1173 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1174 | |
| 1175 | count = vec_len (vlib_mains) - 1; |
| 1176 | |
| 1177 | /* Tolerate recursive calls */ |
| 1178 | if (++vlib_worker_threads[0].recursion_level > 1) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1179 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1180 | |
Bud Grise | 42f2006 | 2016-03-16 13:09:46 -0400 | [diff] [blame] | 1181 | vlib_worker_threads[0].barrier_sync_count++; |
| 1182 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1183 | ASSERT (vlib_get_thread_index () == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1184 | |
| 1185 | deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT; |
| 1186 | |
| 1187 | *vlib_worker_threads->wait_at_barrier = 1; |
| 1188 | while (*vlib_worker_threads->workers_at_barrier != count) |
| 1189 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1190 | if (vlib_time_now (vm) > deadline) |
| 1191 | { |
| 1192 | fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__); |
| 1193 | os_panic (); |
| 1194 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1198 | void |
| 1199 | vlib_worker_thread_barrier_release (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1200 | { |
| 1201 | f64 deadline; |
| 1202 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 1203 | if (vec_len (vlib_mains) < 2) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1204 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1205 | |
| 1206 | if (--vlib_worker_threads[0].recursion_level > 0) |
| 1207 | return; |
| 1208 | |
| 1209 | deadline = vlib_time_now (vm) + BARRIER_SYNC_TIMEOUT; |
| 1210 | |
| 1211 | *vlib_worker_threads->wait_at_barrier = 0; |
| 1212 | |
| 1213 | while (*vlib_worker_threads->workers_at_barrier > 0) |
| 1214 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1215 | if (vlib_time_now (vm) > deadline) |
| 1216 | { |
| 1217 | fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__); |
| 1218 | os_panic (); |
| 1219 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1223 | /* |
| 1224 | * Check the frame queue to see if any frames are available. |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1225 | * If so, pull the packets off the frames and put them to |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1226 | * the handoff node. |
| 1227 | */ |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1228 | int |
| 1229 | vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1230 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1231 | u32 thread_id = vm->thread_index; |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1232 | vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id]; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1233 | vlib_frame_queue_elt_t *elt; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1234 | u32 *from, *to; |
| 1235 | vlib_frame_t *f; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1236 | int msg_type; |
| 1237 | int processed = 0; |
| 1238 | u32 n_left_to_node; |
| 1239 | u32 vectors = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1240 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1241 | ASSERT (fq); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1242 | ASSERT (vm == vlib_mains[thread_id]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1243 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1244 | if (PREDICT_FALSE (fqm->node_index == ~0)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1245 | return 0; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1246 | /* |
| 1247 | * Gather trace data for frame queues |
| 1248 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1249 | if (PREDICT_FALSE (fq->trace)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1250 | { |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1251 | frame_queue_trace_t *fqt; |
| 1252 | frame_queue_nelt_counter_t *fqh; |
| 1253 | u32 elix; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1254 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1255 | fqt = &fqm->frame_queue_traces[thread_id]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1256 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1257 | fqt->nelts = fq->nelts; |
| 1258 | fqt->head = fq->head; |
| 1259 | fqt->head_hint = fq->head_hint; |
| 1260 | fqt->tail = fq->tail; |
| 1261 | fqt->threshold = fq->vector_threshold; |
| 1262 | fqt->n_in_use = fqt->tail - fqt->head; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1263 | if (fqt->n_in_use >= fqt->nelts) |
| 1264 | { |
| 1265 | // if beyond max then use max |
| 1266 | fqt->n_in_use = fqt->nelts - 1; |
| 1267 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1268 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1269 | /* Record the number of elements in use in the histogram */ |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1270 | fqh = &fqm->frame_queue_histogram[thread_id]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1271 | fqh->count[fqt->n_in_use]++; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1272 | |
| 1273 | /* Record a snapshot of the elements in use */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1274 | for (elix = 0; elix < fqt->nelts; elix++) |
| 1275 | { |
| 1276 | elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1)); |
| 1277 | if (1 || elt->valid) |
| 1278 | { |
| 1279 | fqt->n_vectors[elix] = elt->n_vectors; |
| 1280 | } |
| 1281 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1282 | fqt->written = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1283 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1284 | |
| 1285 | while (1) |
| 1286 | { |
| 1287 | if (fq->head == fq->tail) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1288 | { |
| 1289 | fq->head_hint = fq->head; |
| 1290 | return processed; |
| 1291 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1292 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1293 | elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1)); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1294 | |
| 1295 | if (!elt->valid) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1296 | { |
| 1297 | fq->head_hint = fq->head; |
| 1298 | return processed; |
| 1299 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1300 | |
| 1301 | from = elt->buffer_index; |
| 1302 | msg_type = elt->msg_type; |
| 1303 | |
| 1304 | ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME); |
| 1305 | ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE); |
| 1306 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1307 | f = vlib_get_frame_to_node (vm, fqm->node_index); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1308 | |
| 1309 | to = vlib_frame_vector_args (f); |
| 1310 | |
| 1311 | n_left_to_node = elt->n_vectors; |
| 1312 | |
| 1313 | while (n_left_to_node >= 4) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1314 | { |
| 1315 | to[0] = from[0]; |
| 1316 | to[1] = from[1]; |
| 1317 | to[2] = from[2]; |
| 1318 | to[3] = from[3]; |
| 1319 | to += 4; |
| 1320 | from += 4; |
| 1321 | n_left_to_node -= 4; |
| 1322 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1323 | |
| 1324 | while (n_left_to_node > 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1325 | { |
| 1326 | to[0] = from[0]; |
| 1327 | to++; |
| 1328 | from++; |
| 1329 | n_left_to_node--; |
| 1330 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1331 | |
| 1332 | vectors += elt->n_vectors; |
| 1333 | f->n_vectors = elt->n_vectors; |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1334 | vlib_put_frame_to_node (vm, fqm->node_index, f); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1335 | |
| 1336 | elt->valid = 0; |
| 1337 | elt->n_vectors = 0; |
| 1338 | elt->msg_type = 0xfefefefe; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1339 | CLIB_MEMORY_BARRIER (); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1340 | fq->head++; |
| 1341 | processed++; |
| 1342 | |
| 1343 | /* |
| 1344 | * Limit the number of packets pushed into the graph |
| 1345 | */ |
| 1346 | if (vectors >= fq->vector_threshold) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1347 | { |
| 1348 | fq->head_hint = fq->head; |
| 1349 | return processed; |
| 1350 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1351 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1352 | ASSERT (0); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1353 | return processed; |
| 1354 | } |
| 1355 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1356 | void |
| 1357 | vlib_worker_thread_fn (void *arg) |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1358 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1359 | vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1360 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1361 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 1362 | clib_error_t *e; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1363 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1364 | ASSERT (vm->thread_index == vlib_get_thread_index ()); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1365 | |
| 1366 | vlib_worker_thread_init (w); |
| 1367 | clib_time_init (&vm->clib_time); |
| 1368 | clib_mem_set_heap (w->thread_mheap); |
| 1369 | |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 1370 | /* Wait until the dpdk init sequence is complete */ |
| 1371 | while (tm->extern_thread_mgmt && tm->worker_thread_release == 0) |
| 1372 | vlib_worker_thread_barrier_check (); |
| 1373 | |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 1374 | e = vlib_call_init_exit_functions |
| 1375 | (vm, vm->worker_init_function_registrations, 1 /* call_once */ ); |
| 1376 | if (e) |
| 1377 | clib_error_report (e); |
| 1378 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1379 | vlib_worker_loop (vm); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1380 | } |
| 1381 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1382 | /* *INDENT-OFF* */ |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1383 | VLIB_REGISTER_THREAD (worker_thread_reg, static) = { |
| 1384 | .name = "workers", |
| 1385 | .short_name = "wk", |
| 1386 | .function = vlib_worker_thread_fn, |
| 1387 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1388 | /* *INDENT-ON* */ |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1389 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1390 | u32 |
| 1391 | vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts) |
| 1392 | { |
| 1393 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 1394 | vlib_frame_queue_main_t *fqm; |
| 1395 | vlib_frame_queue_t *fq; |
| 1396 | int i; |
| 1397 | |
| 1398 | if (frame_queue_nelts == 0) |
| 1399 | frame_queue_nelts = FRAME_QUEUE_NELTS; |
| 1400 | |
| 1401 | vec_add2 (tm->frame_queue_mains, fqm, 1); |
| 1402 | |
| 1403 | fqm->node_index = node_index; |
| 1404 | |
| 1405 | vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1); |
| 1406 | _vec_len (fqm->vlib_frame_queues) = 0; |
| 1407 | for (i = 0; i < tm->n_vlib_mains; i++) |
| 1408 | { |
| 1409 | fq = vlib_frame_queue_alloc (frame_queue_nelts); |
| 1410 | vec_add1 (fqm->vlib_frame_queues, fq); |
| 1411 | } |
| 1412 | |
| 1413 | return (fqm - tm->frame_queue_mains); |
| 1414 | } |
| 1415 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1416 | |
| 1417 | int |
| 1418 | vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb) |
| 1419 | { |
| 1420 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 1421 | |
| 1422 | if (tm->extern_thread_mgmt) |
| 1423 | return -1; |
| 1424 | |
| 1425 | tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb; |
| 1426 | tm->extern_thread_mgmt = 1; |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1430 | clib_error_t * |
| 1431 | threads_init (vlib_main_t * vm) |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1432 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1433 | return 0; |
| 1434 | } |
| 1435 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1436 | VLIB_INIT_FUNCTION (threads_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1437 | |
| 1438 | /* |
| 1439 | * fd.io coding-style-patch-verification: ON |
| 1440 | * |
| 1441 | * Local Variables: |
| 1442 | * eval: (c-set-style "gnu") |
| 1443 | * End: |
| 1444 | */ |