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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * input.c: Unix file input
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vlib/vlib.h>
41 #include <vlib/unix/unix.h>
44 #include <vppinfra/tw_timer_1t_3w_1024sl_ov.h>
47 #define HAVE_LINUX_EPOLL
49 #ifdef HAVE_LINUX_EPOLL
51 #include <sys/epoll.h>
55 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
57 struct epoll_event *epoll_events;
61 u64 epoll_files_ready;
65 static linux_epoll_main_t *linux_epoll_mains = 0;
68 linux_epoll_file_update (clib_file_t * f, clib_file_update_type_t update_type)
70 clib_file_main_t *fm = &file_main;
71 linux_epoll_main_t *em = vec_elt_at_index (linux_epoll_mains,
72 f->polling_thread_index);
73 struct epoll_event e = { 0 };
77 if (f->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE)
79 if (f->flags & UNIX_FILE_EVENT_EDGE_TRIGGERED)
81 e.data.u32 = f - fm->file_pool;
87 case UNIX_FILE_UPDATE_ADD:
92 case UNIX_FILE_UPDATE_MODIFY:
96 case UNIX_FILE_UPDATE_DELETE:
102 clib_warning ("unknown update_type %d", update_type);
106 /* worker threads open epoll fd only if needed */
107 if (update_type == UNIX_FILE_UPDATE_ADD && em->epoll_fd == -1)
109 em->epoll_fd = epoll_create (1);
110 if (em->epoll_fd < 0)
112 clib_unix_warning ("epoll_create");
118 if (epoll_ctl (em->epoll_fd, op, f->file_descriptor, &e) < 0)
120 clib_unix_warning ("epoll_ctl");
124 em->n_epoll_fds += add_del;
126 if (em->n_epoll_fds == 0)
128 close (em->epoll_fd);
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)
137 unix_main_t *um = &unix_main;
138 clib_file_main_t *fm = &file_main;
139 linux_epoll_main_t *em = vec_elt_at_index (linux_epoll_mains, thread_index);
140 struct epoll_event *e;
142 int is_main = (thread_index == 0);
145 vlib_node_main_t *nm = &vm->node_main;
146 u32 ticks_until_expiration;
148 int timeout_ms = 0, max_timeout_ms = 10;
149 f64 vector_rate = vlib_last_vectors_per_main_loop (vm);
152 * If we've been asked for a fixed-sleep between main loop polls,
155 if (PREDICT_FALSE (is_main && um->poll_sleep_usec))
157 struct timespec ts, tsrem;
160 node->input_main_loops_per_call = 0;
162 ts.tv_nsec = 1000 * um->poll_sleep_usec;
164 while (nanosleep (&ts, &tsrem) < 0)
169 /* If we're not working very hard, decide how long to sleep */
170 else if (is_main && vector_rate < 2 && vm->api_queue_nonempty == 0
171 && nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] == 0)
173 ticks_until_expiration = TW (tw_timer_first_expires_in_ticks)
174 ((TWT (tw_timer_wheel) *) nm->timing_wheel);
176 /* Nothing on the fast wheel, sleep 10ms */
177 if (ticks_until_expiration == TW_SLOTS_PER_RING)
180 timeout_ms = max_timeout_ms;
184 timeout = (f64) ticks_until_expiration *1e-5;
189 timeout_ms = timeout * 1e3;
190 /* Must be between 1 and 10 ms. */
191 timeout_ms = clib_max (1, timeout_ms);
192 timeout_ms = clib_min (max_timeout_ms, timeout_ms);
195 node->input_main_loops_per_call = 0;
197 else if (is_main == 0 && vector_rate < 2 &&
198 nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] == 0)
201 timeout_ms = max_timeout_ms;
202 node->input_main_loops_per_call = 0;
206 /* Don't come back for a respectable number of dispatch cycles */
207 node->input_main_loops_per_call = 1024;
210 /* Allow any signal to wakeup our sleep. */
211 if (is_main || em->epoll_fd != -1)
213 static sigset_t unblock_all_signals;
214 n_fds_ready = epoll_pwait (em->epoll_fd,
216 vec_len (em->epoll_events),
217 timeout_ms, &unblock_all_signals);
219 /* This kludge is necessary to run over absurdly old kernels */
220 if (n_fds_ready < 0 && errno == ENOSYS)
222 n_fds_ready = epoll_wait (em->epoll_fd,
224 vec_len (em->epoll_events), timeout_ms);
230 usleep (timeout_ms * 1000);
237 if (unix_error_is_fatal (errno))
238 vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait"));
240 /* non fatal error (e.g. EINTR). */
244 em->epoll_waits += 1;
245 em->epoll_files_ready += n_fds_ready;
247 for (e = em->epoll_events; e < em->epoll_events + n_fds_ready; e++)
250 clib_file_t *f = fm->file_pool + i;
251 clib_error_t *errors[4];
254 if (PREDICT_FALSE (pool_is_free (fm->file_pool, f)))
257 * Under rare scenerop, epoll may still post us events for the
258 * deleted file descriptor. We just deal with it and throw away the
259 * events for the corresponding file descriptor.
261 if (e->events & EPOLLIN)
264 clib_error_return (0, "epoll event EPOLLIN dropped due "
265 "to free index %u", i);
268 if (e->events & EPOLLOUT)
271 clib_error_return (0, "epoll event EPOLLOUT dropped due "
272 "to free index %u", i);
275 if (e->events & EPOLLERR)
278 clib_error_return (0, "epoll event EPOLLERR dropped due "
279 "to free index %u", i);
283 else if (PREDICT_TRUE (!(e->events & EPOLLERR)))
285 if (e->events & EPOLLIN)
287 errors[n_errors] = f->read_function (f);
289 n_errors += errors[n_errors] != 0;
291 if (e->events & EPOLLOUT)
293 errors[n_errors] = f->write_function (f);
295 n_errors += errors[n_errors] != 0;
300 if (f->error_function)
302 errors[n_errors] = f->error_function (f);
304 n_errors += errors[n_errors] != 0;
307 close (f->file_descriptor);
310 ASSERT (n_errors < ARRAY_LEN (errors));
311 for (i = 0; i < n_errors; i++)
313 unix_save_error (um, errors[i]);
321 linux_epoll_input (vlib_main_t * vm,
322 vlib_node_runtime_t * node, vlib_frame_t * frame)
324 u32 thread_index = vlib_get_thread_index ();
326 if (thread_index == 0)
327 return linux_epoll_input_inline (vm, node, frame, 0);
329 return linux_epoll_input_inline (vm, node, frame, thread_index);
333 VLIB_REGISTER_NODE (linux_epoll_input_node,static) = {
334 .function = linux_epoll_input,
335 .type = VLIB_NODE_TYPE_PRE_INPUT,
336 .name = "unix-epoll-input",
341 linux_epoll_input_init (vlib_main_t * vm)
343 linux_epoll_main_t *em;
344 clib_file_main_t *fm = &file_main;
345 vlib_thread_main_t *tm = vlib_get_thread_main ();
348 vec_validate_aligned (linux_epoll_mains, tm->n_vlib_mains,
349 CLIB_CACHE_LINE_BYTES);
351 vec_foreach (em, linux_epoll_mains)
353 /* Allocate some events. */
354 vec_resize (em->epoll_events, VLIB_FRAME_SIZE);
356 if (linux_epoll_mains == em)
358 em->epoll_fd = epoll_create (1);
359 if (em->epoll_fd < 0)
360 return clib_error_return_unix (0, "epoll_create");
366 fm->file_update = linux_epoll_file_update;
371 VLIB_INIT_FUNCTION (linux_epoll_input_init);
373 #endif /* HAVE_LINUX_EPOLL */
375 static clib_error_t *
376 unix_input_init (vlib_main_t * vm)
378 return vlib_call_init_function (vm, linux_epoll_input_init);
381 VLIB_INIT_FUNCTION (unix_input_init);
384 * fd.io coding-style-patch-verification: ON
387 * eval: (c-set-style "gnu")