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> |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 43 | #include <vppinfra/tw_timer_1t_3w_1024sl_ov.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | |
| 45 | /* FIXME autoconf */ |
| 46 | #define HAVE_LINUX_EPOLL |
| 47 | |
| 48 | #ifdef HAVE_LINUX_EPOLL |
| 49 | |
| 50 | #include <sys/epoll.h> |
| 51 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 52 | typedef struct |
| 53 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | int epoll_fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 55 | struct epoll_event *epoll_events; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
| 57 | /* Statistics. */ |
| 58 | u64 epoll_files_ready; |
| 59 | u64 epoll_waits; |
| 60 | } linux_epoll_main_t; |
| 61 | |
| 62 | static linux_epoll_main_t linux_epoll_main; |
| 63 | |
| 64 | static void |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 65 | linux_epoll_file_update (unix_file_t * f, unix_file_update_type_t update_type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 67 | unix_main_t *um = &unix_main; |
| 68 | linux_epoll_main_t *em = &linux_epoll_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | struct epoll_event e; |
Dave Barach | a1a093d | 2017-03-02 13:13:23 -0500 | [diff] [blame] | 70 | int op; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | |
| 72 | memset (&e, 0, sizeof (e)); |
| 73 | |
| 74 | e.events = EPOLLIN; |
| 75 | if (f->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE) |
| 76 | e.events |= EPOLLOUT; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 77 | if (f->flags & UNIX_FILE_EVENT_EDGE_TRIGGERED) |
| 78 | e.events |= EPOLLET; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | e.data.u32 = f - um->file_pool; |
| 80 | |
Dave Barach | a1a093d | 2017-03-02 13:13:23 -0500 | [diff] [blame] | 81 | op = -1; |
| 82 | |
| 83 | switch (update_type) |
| 84 | { |
| 85 | case UNIX_FILE_UPDATE_ADD: |
| 86 | op = EPOLL_CTL_ADD; |
| 87 | break; |
| 88 | |
| 89 | case UNIX_FILE_UPDATE_MODIFY: |
| 90 | op = EPOLL_CTL_MOD; |
| 91 | break; |
| 92 | |
| 93 | case UNIX_FILE_UPDATE_DELETE: |
| 94 | op = EPOLL_CTL_DEL; |
| 95 | break; |
| 96 | |
| 97 | default: |
| 98 | clib_warning ("unknown update_type %d", update_type); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | if (epoll_ctl (em->epoll_fd, op, f->file_descriptor, &e) < 0) |
| 103 | clib_unix_warning ("epoll_ctl"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static uword |
| 107 | linux_epoll_input (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 108 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 110 | unix_main_t *um = &unix_main; |
| 111 | linux_epoll_main_t *em = &linux_epoll_main; |
| 112 | struct epoll_event *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | int n_fds_ready; |
| 114 | |
| 115 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 116 | vlib_node_main_t *nm = &vm->node_main; |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 117 | u32 ticks_until_expiration; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | f64 timeout; |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 119 | int timeout_ms = 0, max_timeout_ms = 10; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | f64 vector_rate = vlib_last_vectors_per_main_loop (vm); |
| 121 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 122 | /* If we're not working very hard, decide how long to sleep */ |
| 123 | if (vector_rate < 2 && vm->api_queue_nonempty == 0 |
| 124 | && nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | { |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 126 | ticks_until_expiration = TW (tw_timer_first_expires_in_ticks) |
| 127 | ((TWT (tw_timer_wheel) *) nm->timing_wheel); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 128 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 129 | /* Nothing on the fast wheel, sleep 10ms */ |
| 130 | if (ticks_until_expiration == TW_SLOTS_PER_RING) |
Damjan Marion | 18bc907 | 2016-12-07 14:07:54 +0100 | [diff] [blame] | 131 | { |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 132 | timeout = 10e-3; |
| 133 | timeout_ms = max_timeout_ms; |
Damjan Marion | 18bc907 | 2016-12-07 14:07:54 +0100 | [diff] [blame] | 134 | } |
| 135 | else |
| 136 | { |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 137 | timeout = (f64) ticks_until_expiration *1e-5; |
| 138 | if (timeout < 1e-3) |
| 139 | timeout_ms = 0; |
| 140 | else |
| 141 | { |
| 142 | timeout_ms = timeout * 1e3; |
| 143 | /* Must be between 1 and 10 ms. */ |
| 144 | timeout_ms = clib_max (1, timeout_ms); |
| 145 | timeout_ms = clib_min (max_timeout_ms, timeout_ms); |
| 146 | } |
Damjan Marion | 18bc907 | 2016-12-07 14:07:54 +0100 | [diff] [blame] | 147 | } |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 148 | node->input_main_loops_per_call = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | } |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 150 | else /* busy */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | { |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 152 | /* Don't come back for a respectable number of dispatch cycles */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | node->input_main_loops_per_call = 1024; |
| 154 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | |
| 156 | /* Allow any signal to wakeup our sleep. */ |
| 157 | { |
| 158 | static sigset_t unblock_all_signals; |
| 159 | n_fds_ready = epoll_pwait (em->epoll_fd, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 160 | em->epoll_events, |
| 161 | vec_len (em->epoll_events), |
| 162 | timeout_ms, &unblock_all_signals); |
| 163 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | /* This kludge is necessary to run over absurdly old kernels */ |
| 165 | if (n_fds_ready < 0 && errno == ENOSYS) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 166 | { |
| 167 | n_fds_ready = epoll_wait (em->epoll_fd, |
| 168 | em->epoll_events, |
| 169 | vec_len (em->epoll_events), timeout_ms); |
| 170 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | if (n_fds_ready < 0) |
| 175 | { |
| 176 | if (unix_error_is_fatal (errno)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 177 | vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait")); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | |
| 179 | /* non fatal error (e.g. EINTR). */ |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | em->epoll_waits += 1; |
| 184 | em->epoll_files_ready += n_fds_ready; |
| 185 | |
| 186 | for (e = em->epoll_events; e < em->epoll_events + n_fds_ready; e++) |
| 187 | { |
| 188 | u32 i = e->data.u32; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 189 | unix_file_t *f = pool_elt_at_index (um->file_pool, i); |
| 190 | clib_error_t *errors[4]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | int n_errors = 0; |
| 192 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 193 | if (PREDICT_TRUE (!(e->events & EPOLLERR))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | { |
| 195 | if (e->events & EPOLLIN) |
| 196 | { |
| 197 | errors[n_errors] = f->read_function (f); |
| 198 | n_errors += errors[n_errors] != 0; |
| 199 | } |
| 200 | if (e->events & EPOLLOUT) |
| 201 | { |
| 202 | errors[n_errors] = f->write_function (f); |
| 203 | n_errors += errors[n_errors] != 0; |
| 204 | } |
| 205 | } |
| 206 | else |
| 207 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 208 | if (f->error_function) |
| 209 | { |
| 210 | errors[n_errors] = f->error_function (f); |
| 211 | n_errors += errors[n_errors] != 0; |
| 212 | } |
Ole Troan | 4b12b3c | 2016-01-27 23:37:58 +0200 | [diff] [blame] | 213 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 214 | close (f->file_descriptor); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | ASSERT (n_errors < ARRAY_LEN (errors)); |
| 218 | for (i = 0; i < n_errors; i++) |
| 219 | { |
| 220 | unix_save_error (um, errors[i]); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 227 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | VLIB_REGISTER_NODE (linux_epoll_input_node,static) = { |
| 229 | .function = linux_epoll_input, |
| 230 | .type = VLIB_NODE_TYPE_PRE_INPUT, |
| 231 | .name = "unix-epoll-input", |
| 232 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 233 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | |
| 235 | clib_error_t * |
| 236 | linux_epoll_input_init (vlib_main_t * vm) |
| 237 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 238 | linux_epoll_main_t *em = &linux_epoll_main; |
| 239 | unix_main_t *um = &unix_main; |
| 240 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | /* Allocate some events. */ |
| 242 | vec_resize (em->epoll_events, VLIB_FRAME_SIZE); |
| 243 | |
| 244 | em->epoll_fd = epoll_create (vec_len (em->epoll_events)); |
| 245 | if (em->epoll_fd < 0) |
| 246 | return clib_error_return_unix (0, "epoll_create"); |
| 247 | |
| 248 | um->file_update = linux_epoll_file_update; |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | VLIB_INIT_FUNCTION (linux_epoll_input_init); |
| 254 | |
| 255 | #endif /* HAVE_LINUX_EPOLL */ |
| 256 | |
| 257 | static clib_error_t * |
| 258 | unix_input_init (vlib_main_t * vm) |
| 259 | { |
| 260 | return vlib_call_init_function (vm, linux_epoll_input_init); |
| 261 | } |
| 262 | |
| 263 | VLIB_INIT_FUNCTION (unix_input_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * fd.io coding-style-patch-verification: ON |
| 267 | * |
| 268 | * Local Variables: |
| 269 | * eval: (c-set-style "gnu") |
| 270 | * End: |
| 271 | */ |