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