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 | */ |
| 15 | /* |
| 16 | * input.c: Unix file input |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vlib/vlib.h> |
| 41 | #include <vlib/unix/unix.h> |
| 42 | #include <signal.h> |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 43 | #include <unistd.h> |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 44 | #include <vppinfra/tw_timer_1t_3w_1024sl_ov.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | |
| 46 | /* FIXME autoconf */ |
| 47 | #define HAVE_LINUX_EPOLL |
| 48 | |
| 49 | #ifdef HAVE_LINUX_EPOLL |
| 50 | |
| 51 | #include <sys/epoll.h> |
| 52 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Dave Barach | eb987d3 | 2018-05-03 08:26:39 -0400 | [diff] [blame] | 55 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | int epoll_fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 57 | struct epoll_event *epoll_events; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 58 | int n_epoll_fds; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | |
| 60 | /* Statistics. */ |
| 61 | u64 epoll_files_ready; |
| 62 | u64 epoll_waits; |
| 63 | } linux_epoll_main_t; |
| 64 | |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 65 | static linux_epoll_main_t *linux_epoll_mains = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
| 67 | static void |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 68 | linux_epoll_file_update (clib_file_t * f, clib_file_update_type_t update_type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 70 | clib_file_main_t *fm = &file_main; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 71 | linux_epoll_main_t *em = vec_elt_at_index (linux_epoll_mains, |
| 72 | f->polling_thread_index); |
| 73 | struct epoll_event e = { 0 }; |
| 74 | int op, add_del = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | |
| 76 | e.events = EPOLLIN; |
| 77 | if (f->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE) |
| 78 | e.events |= EPOLLOUT; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 79 | if (f->flags & UNIX_FILE_EVENT_EDGE_TRIGGERED) |
| 80 | e.events |= EPOLLET; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 81 | e.data.u32 = f - fm->file_pool; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
Dave Barach | a1a093d | 2017-03-02 13:13:23 -0500 | [diff] [blame] | 83 | op = -1; |
| 84 | |
| 85 | switch (update_type) |
| 86 | { |
| 87 | case UNIX_FILE_UPDATE_ADD: |
| 88 | op = EPOLL_CTL_ADD; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 89 | add_del = 1; |
Dave Barach | a1a093d | 2017-03-02 13:13:23 -0500 | [diff] [blame] | 90 | break; |
| 91 | |
| 92 | case UNIX_FILE_UPDATE_MODIFY: |
| 93 | op = EPOLL_CTL_MOD; |
| 94 | break; |
| 95 | |
| 96 | case UNIX_FILE_UPDATE_DELETE: |
| 97 | op = EPOLL_CTL_DEL; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 98 | add_del = -1; |
Dave Barach | a1a093d | 2017-03-02 13:13:23 -0500 | [diff] [blame] | 99 | break; |
| 100 | |
| 101 | default: |
| 102 | clib_warning ("unknown update_type %d", update_type); |
| 103 | return; |
| 104 | } |
| 105 | |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 106 | /* worker threads open epoll fd only if needed */ |
| 107 | if (update_type == UNIX_FILE_UPDATE_ADD && em->epoll_fd == -1) |
| 108 | { |
| 109 | em->epoll_fd = epoll_create (1); |
| 110 | if (em->epoll_fd < 0) |
| 111 | { |
| 112 | clib_unix_warning ("epoll_create"); |
| 113 | return; |
| 114 | } |
| 115 | em->n_epoll_fds = 0; |
| 116 | } |
| 117 | |
Dave Barach | a1a093d | 2017-03-02 13:13:23 -0500 | [diff] [blame] | 118 | if (epoll_ctl (em->epoll_fd, op, f->file_descriptor, &e) < 0) |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 119 | { |
| 120 | clib_unix_warning ("epoll_ctl"); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | em->n_epoll_fds += add_del; |
| 125 | |
| 126 | if (em->n_epoll_fds == 0) |
| 127 | { |
| 128 | close (em->epoll_fd); |
| 129 | em->epoll_fd = -1; |
| 130 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 133 | static_always_inline uword |
| 134 | linux_epoll_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 135 | vlib_frame_t * frame, u32 thread_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 137 | unix_main_t *um = &unix_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 138 | clib_file_main_t *fm = &file_main; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 139 | linux_epoll_main_t *em = vec_elt_at_index (linux_epoll_mains, thread_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 140 | struct epoll_event *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | int n_fds_ready; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 142 | int is_main = (thread_index == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
| 144 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 145 | vlib_node_main_t *nm = &vm->node_main; |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 146 | u32 ticks_until_expiration; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | f64 timeout; |
Dave Barach | 9ae190e | 2019-04-23 10:07:24 -0400 | [diff] [blame] | 148 | f64 now; |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 149 | int timeout_ms = 0, max_timeout_ms = 10; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | f64 vector_rate = vlib_last_vectors_per_main_loop (vm); |
| 151 | |
Dave Barach | 9ae190e | 2019-04-23 10:07:24 -0400 | [diff] [blame] | 152 | if (is_main == 0) |
| 153 | now = vlib_time_now (vm); |
| 154 | |
Dave Barach | 85aa490 | 2018-06-14 18:52:46 -0400 | [diff] [blame] | 155 | /* |
| 156 | * If we've been asked for a fixed-sleep between main loop polls, |
| 157 | * do so right away. |
| 158 | */ |
| 159 | if (PREDICT_FALSE (is_main && um->poll_sleep_usec)) |
| 160 | { |
| 161 | struct timespec ts, tsrem; |
| 162 | timeout = 0; |
| 163 | timeout_ms = 0; |
| 164 | node->input_main_loops_per_call = 0; |
| 165 | ts.tv_sec = 0; |
| 166 | ts.tv_nsec = 1000 * um->poll_sleep_usec; |
| 167 | |
| 168 | while (nanosleep (&ts, &tsrem) < 0) |
| 169 | { |
| 170 | ts = tsrem; |
| 171 | } |
| 172 | } |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 173 | /* If we're not working very hard, decide how long to sleep */ |
Dave Barach | 85aa490 | 2018-06-14 18:52:46 -0400 | [diff] [blame] | 174 | else if (is_main && vector_rate < 2 && vm->api_queue_nonempty == 0 |
| 175 | && nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | { |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 177 | ticks_until_expiration = TW (tw_timer_first_expires_in_ticks) |
| 178 | ((TWT (tw_timer_wheel) *) nm->timing_wheel); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 179 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 180 | /* Nothing on the fast wheel, sleep 10ms */ |
| 181 | if (ticks_until_expiration == TW_SLOTS_PER_RING) |
Damjan Marion | 18bc907 | 2016-12-07 14:07:54 +0100 | [diff] [blame] | 182 | { |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 183 | timeout = 10e-3; |
| 184 | timeout_ms = max_timeout_ms; |
Damjan Marion | 18bc907 | 2016-12-07 14:07:54 +0100 | [diff] [blame] | 185 | } |
| 186 | else |
| 187 | { |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 188 | timeout = (f64) ticks_until_expiration *1e-5; |
| 189 | if (timeout < 1e-3) |
| 190 | timeout_ms = 0; |
| 191 | else |
| 192 | { |
| 193 | timeout_ms = timeout * 1e3; |
| 194 | /* Must be between 1 and 10 ms. */ |
| 195 | timeout_ms = clib_max (1, timeout_ms); |
| 196 | timeout_ms = clib_min (max_timeout_ms, timeout_ms); |
| 197 | } |
Damjan Marion | 18bc907 | 2016-12-07 14:07:54 +0100 | [diff] [blame] | 198 | } |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 199 | node->input_main_loops_per_call = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | } |
Dave Barach | 9ae190e | 2019-04-23 10:07:24 -0400 | [diff] [blame] | 201 | else if (is_main == 0 && vector_rate < 2 |
| 202 | && (vlib_global_main.time_last_barrier_release + 0.5 < now) |
| 203 | && nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] == 0) |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 204 | { |
| 205 | timeout = 10e-3; |
| 206 | timeout_ms = max_timeout_ms; |
| 207 | node->input_main_loops_per_call = 0; |
| 208 | } |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 209 | else /* busy */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | { |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 211 | /* Don't come back for a respectable number of dispatch cycles */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | node->input_main_loops_per_call = 1024; |
| 213 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | |
| 215 | /* Allow any signal to wakeup our sleep. */ |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 216 | if (is_main || em->epoll_fd != -1) |
| 217 | { |
| 218 | static sigset_t unblock_all_signals; |
| 219 | n_fds_ready = epoll_pwait (em->epoll_fd, |
| 220 | em->epoll_events, |
| 221 | vec_len (em->epoll_events), |
| 222 | timeout_ms, &unblock_all_signals); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 223 | |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 224 | /* This kludge is necessary to run over absurdly old kernels */ |
| 225 | if (n_fds_ready < 0 && errno == ENOSYS) |
| 226 | { |
| 227 | n_fds_ready = epoll_wait (em->epoll_fd, |
| 228 | em->epoll_events, |
| 229 | vec_len (em->epoll_events), timeout_ms); |
| 230 | } |
Damjan Marion | 29c0b33 | 2019-01-28 13:41:27 +0100 | [diff] [blame] | 231 | |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 232 | } |
| 233 | else |
| 234 | { |
Dave Barach | 9ae190e | 2019-04-23 10:07:24 -0400 | [diff] [blame] | 235 | /* |
| 236 | * Worker thread, no epoll fd's, sleep for 100us at a time |
| 237 | * and check for a barrier sync request |
| 238 | */ |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 239 | if (timeout_ms) |
Dave Barach | 9ae190e | 2019-04-23 10:07:24 -0400 | [diff] [blame] | 240 | { |
| 241 | struct timespec ts, tsrem; |
| 242 | f64 limit = now + (f64) timeout_ms * 1e-3; |
| 243 | |
| 244 | while (vlib_time_now (vm) < limit) |
| 245 | { |
| 246 | /* Sleep for 100us at a time */ |
| 247 | ts.tv_sec = 0; |
| 248 | ts.tv_nsec = 1000 * 100; |
| 249 | |
| 250 | while (nanosleep (&ts, &tsrem) < 0) |
| 251 | ts = tsrem; |
| 252 | if (*vlib_worker_threads->wait_at_barrier) |
| 253 | goto done; |
| 254 | } |
| 255 | } |
Damjan Marion | 29c0b33 | 2019-01-28 13:41:27 +0100 | [diff] [blame] | 256 | goto done; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 257 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (n_fds_ready < 0) |
| 261 | { |
| 262 | if (unix_error_is_fatal (errno)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 263 | vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait")); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | |
| 265 | /* non fatal error (e.g. EINTR). */ |
Damjan Marion | 29c0b33 | 2019-01-28 13:41:27 +0100 | [diff] [blame] | 266 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | em->epoll_waits += 1; |
| 270 | em->epoll_files_ready += n_fds_ready; |
| 271 | |
| 272 | for (e = em->epoll_events; e < em->epoll_events + n_fds_ready; e++) |
| 273 | { |
| 274 | u32 i = e->data.u32; |
Florin Coras | c67078e | 2019-04-18 10:04:03 -0700 | [diff] [blame] | 275 | clib_file_t *f = pool_elt_at_index (fm->file_pool, i); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 276 | clib_error_t *errors[4]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | int n_errors = 0; |
| 278 | |
Steven | 97f592f | 2018-09-08 14:06:16 -0700 | [diff] [blame] | 279 | if (PREDICT_FALSE (pool_is_free (fm->file_pool, f))) |
| 280 | { |
| 281 | /* |
| 282 | * Under rare scenerop, epoll may still post us events for the |
| 283 | * deleted file descriptor. We just deal with it and throw away the |
| 284 | * events for the corresponding file descriptor. |
| 285 | */ |
| 286 | if (e->events & EPOLLIN) |
| 287 | { |
| 288 | errors[n_errors] = |
| 289 | clib_error_return (0, "epoll event EPOLLIN dropped due " |
| 290 | "to free index %u", i); |
| 291 | n_errors++; |
| 292 | } |
| 293 | if (e->events & EPOLLOUT) |
| 294 | { |
| 295 | errors[n_errors] = |
| 296 | clib_error_return (0, "epoll event EPOLLOUT dropped due " |
| 297 | "to free index %u", i); |
| 298 | n_errors++; |
| 299 | } |
| 300 | if (e->events & EPOLLERR) |
| 301 | { |
| 302 | errors[n_errors] = |
| 303 | clib_error_return (0, "epoll event EPOLLERR dropped due " |
| 304 | "to free index %u", i); |
| 305 | n_errors++; |
| 306 | } |
| 307 | } |
| 308 | else if (PREDICT_TRUE (!(e->events & EPOLLERR))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | { |
| 310 | if (e->events & EPOLLIN) |
| 311 | { |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 312 | f->read_events++; |
Artem Belov | 32b07c3 | 2019-04-18 07:30:43 +0000 | [diff] [blame] | 313 | errors[n_errors] = f->read_function (f); |
Florin Coras | c67078e | 2019-04-18 10:04:03 -0700 | [diff] [blame] | 314 | /* Make sure f is valid if the file pool moves */ |
Florin Coras | 81f54a5 | 2019-04-22 09:49:10 -0700 | [diff] [blame] | 315 | if (pool_is_free_index (fm->file_pool, i)) |
| 316 | continue; |
Florin Coras | c67078e | 2019-04-18 10:04:03 -0700 | [diff] [blame] | 317 | f = pool_elt_at_index (fm->file_pool, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 318 | n_errors += errors[n_errors] != 0; |
| 319 | } |
| 320 | if (e->events & EPOLLOUT) |
| 321 | { |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 322 | f->write_events++; |
Artem Belov | 32b07c3 | 2019-04-18 07:30:43 +0000 | [diff] [blame] | 323 | errors[n_errors] = f->write_function (f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | n_errors += errors[n_errors] != 0; |
| 325 | } |
| 326 | } |
| 327 | else |
| 328 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 329 | if (f->error_function) |
| 330 | { |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 331 | f->error_events++; |
Artem Belov | 32b07c3 | 2019-04-18 07:30:43 +0000 | [diff] [blame] | 332 | errors[n_errors] = f->error_function (f); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 333 | n_errors += errors[n_errors] != 0; |
| 334 | } |
Ole Troan | 4b12b3c | 2016-01-27 23:37:58 +0200 | [diff] [blame] | 335 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 336 | close (f->file_descriptor); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | ASSERT (n_errors < ARRAY_LEN (errors)); |
| 340 | for (i = 0; i < n_errors; i++) |
| 341 | { |
| 342 | unix_save_error (um, errors[i]); |
| 343 | } |
| 344 | } |
| 345 | |
Damjan Marion | 29c0b33 | 2019-01-28 13:41:27 +0100 | [diff] [blame] | 346 | done: |
| 347 | if (PREDICT_FALSE (vm->cpu_id != clib_get_current_cpu_id ())) |
| 348 | { |
| 349 | vm->cpu_id = clib_get_current_cpu_id (); |
| 350 | vm->numa_node = clib_get_current_numa_node (); |
| 351 | } |
| 352 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 356 | static uword |
| 357 | linux_epoll_input (vlib_main_t * vm, |
| 358 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 359 | { |
| 360 | u32 thread_index = vlib_get_thread_index (); |
| 361 | |
| 362 | if (thread_index == 0) |
| 363 | return linux_epoll_input_inline (vm, node, frame, 0); |
| 364 | else |
| 365 | return linux_epoll_input_inline (vm, node, frame, thread_index); |
| 366 | } |
| 367 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 368 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | VLIB_REGISTER_NODE (linux_epoll_input_node,static) = { |
| 370 | .function = linux_epoll_input, |
| 371 | .type = VLIB_NODE_TYPE_PRE_INPUT, |
| 372 | .name = "unix-epoll-input", |
| 373 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 374 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | |
| 376 | clib_error_t * |
| 377 | linux_epoll_input_init (vlib_main_t * vm) |
| 378 | { |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 379 | linux_epoll_main_t *em; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 380 | clib_file_main_t *fm = &file_main; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 381 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 382 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 384 | vec_validate_aligned (linux_epoll_mains, tm->n_vlib_mains, |
| 385 | CLIB_CACHE_LINE_BYTES); |
| 386 | |
| 387 | vec_foreach (em, linux_epoll_mains) |
| 388 | { |
| 389 | /* Allocate some events. */ |
| 390 | vec_resize (em->epoll_events, VLIB_FRAME_SIZE); |
| 391 | |
| 392 | if (linux_epoll_mains == em) |
| 393 | { |
| 394 | em->epoll_fd = epoll_create (1); |
| 395 | if (em->epoll_fd < 0) |
| 396 | return clib_error_return_unix (0, "epoll_create"); |
| 397 | } |
| 398 | else |
| 399 | em->epoll_fd = -1; |
| 400 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 402 | fm->file_update = linux_epoll_file_update; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | VLIB_INIT_FUNCTION (linux_epoll_input_init); |
| 408 | |
| 409 | #endif /* HAVE_LINUX_EPOLL */ |
| 410 | |
| 411 | static clib_error_t * |
| 412 | unix_input_init (vlib_main_t * vm) |
| 413 | { |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame^] | 414 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame^] | 417 | /* *INDENT-OFF* */ |
| 418 | VLIB_INIT_FUNCTION (unix_input_init) = |
| 419 | { |
| 420 | .runs_before = VLIB_INITS ("linux_epoll_input_init"), |
| 421 | }; |
| 422 | /* *INDENT-ON* */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * fd.io coding-style-patch-verification: ON |
| 426 | * |
| 427 | * Local Variables: |
| 428 | * eval: (c-set-style "gnu") |
| 429 | * End: |
| 430 | */ |