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