blob: 321e443dee9ab5cf817d2343a7ad08f5ce18b2c3 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Marionceab7882018-01-19 20:56:12 +010043#include <unistd.h>
Dave Barach5c20a012017-06-13 08:48:31 -040044#include <vppinfra/tw_timer_1t_3w_1024sl_ov.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
46/* FIXME autoconf */
47#define HAVE_LINUX_EPOLL
48
49#ifdef HAVE_LINUX_EPOLL
50
51#include <sys/epoll.h>
52
Dave Barach9b8ffd92016-07-08 08:13:45 -040053typedef struct
54{
Dave Baracheb987d32018-05-03 08:26:39 -040055 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070056 int epoll_fd;
Dave Barach9b8ffd92016-07-08 08:13:45 -040057 struct epoll_event *epoll_events;
Damjan Marionceab7882018-01-19 20:56:12 +010058 int n_epoll_fds;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
60 /* Statistics. */
61 u64 epoll_files_ready;
62 u64 epoll_waits;
63} linux_epoll_main_t;
64
Damjan Marionceab7882018-01-19 20:56:12 +010065static linux_epoll_main_t *linux_epoll_mains = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
67static void
Dave Barach59b25652017-09-10 15:04:27 -040068linux_epoll_file_update (clib_file_t * f, clib_file_update_type_t update_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
Damjan Marion56dd5432017-09-08 19:52:02 +020070 clib_file_main_t *fm = &file_main;
Damjan Marionceab7882018-01-19 20:56:12 +010071 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 Warnickecb9cada2015-12-08 15:45:58 -070075
76 e.events = EPOLLIN;
77 if (f->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE)
78 e.events |= EPOLLOUT;
Dave Barach9b8ffd92016-07-08 08:13:45 -040079 if (f->flags & UNIX_FILE_EVENT_EDGE_TRIGGERED)
80 e.events |= EPOLLET;
Damjan Marion56dd5432017-09-08 19:52:02 +020081 e.data.u32 = f - fm->file_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
Dave Baracha1a093d2017-03-02 13:13:23 -050083 op = -1;
84
85 switch (update_type)
86 {
87 case UNIX_FILE_UPDATE_ADD:
88 op = EPOLL_CTL_ADD;
Damjan Marionceab7882018-01-19 20:56:12 +010089 add_del = 1;
Dave Baracha1a093d2017-03-02 13:13:23 -050090 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 Marionceab7882018-01-19 20:56:12 +010098 add_del = -1;
Dave Baracha1a093d2017-03-02 13:13:23 -050099 break;
100
101 default:
102 clib_warning ("unknown update_type %d", update_type);
103 return;
104 }
105
Damjan Marionceab7882018-01-19 20:56:12 +0100106 /* 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 Baracha1a093d2017-03-02 13:13:23 -0500118 if (epoll_ctl (em->epoll_fd, op, f->file_descriptor, &e) < 0)
Damjan Marionceab7882018-01-19 20:56:12 +0100119 {
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 Warnickecb9cada2015-12-08 15:45:58 -0700131}
132
Damjan Marionceab7882018-01-19 20:56:12 +0100133static_always_inline uword
134linux_epoll_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
135 vlib_frame_t * frame, u32 thread_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +0200138 clib_file_main_t *fm = &file_main;
Damjan Marionceab7882018-01-19 20:56:12 +0100139 linux_epoll_main_t *em = vec_elt_at_index (linux_epoll_mains, thread_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400140 struct epoll_event *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 int n_fds_ready;
Damjan Marionceab7882018-01-19 20:56:12 +0100142 int is_main = (thread_index == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
144 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400145 vlib_node_main_t *nm = &vm->node_main;
Dave Barach5c20a012017-06-13 08:48:31 -0400146 u32 ticks_until_expiration;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 f64 timeout;
Dave Barach5c20a012017-06-13 08:48:31 -0400148 int timeout_ms = 0, max_timeout_ms = 10;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 f64 vector_rate = vlib_last_vectors_per_main_loop (vm);
150
Dave Barach5c20a012017-06-13 08:48:31 -0400151 /* If we're not working very hard, decide how long to sleep */
Damjan Marionceab7882018-01-19 20:56:12 +0100152 if (is_main && vector_rate < 2 && vm->api_queue_nonempty == 0
Dave Barach5c20a012017-06-13 08:48:31 -0400153 && nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 {
Dave Barach5c20a012017-06-13 08:48:31 -0400155 ticks_until_expiration = TW (tw_timer_first_expires_in_ticks)
156 ((TWT (tw_timer_wheel) *) nm->timing_wheel);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400157
Dave Barach5c20a012017-06-13 08:48:31 -0400158 /* Nothing on the fast wheel, sleep 10ms */
159 if (ticks_until_expiration == TW_SLOTS_PER_RING)
Damjan Marion18bc9072016-12-07 14:07:54 +0100160 {
Dave Barach5c20a012017-06-13 08:48:31 -0400161 timeout = 10e-3;
162 timeout_ms = max_timeout_ms;
Damjan Marion18bc9072016-12-07 14:07:54 +0100163 }
164 else
165 {
Dave Barach5c20a012017-06-13 08:48:31 -0400166 timeout = (f64) ticks_until_expiration *1e-5;
167 if (timeout < 1e-3)
168 timeout_ms = 0;
169 else
170 {
171 timeout_ms = timeout * 1e3;
172 /* Must be between 1 and 10 ms. */
173 timeout_ms = clib_max (1, timeout_ms);
174 timeout_ms = clib_min (max_timeout_ms, timeout_ms);
175 }
Damjan Marion18bc9072016-12-07 14:07:54 +0100176 }
Dave Barach5c20a012017-06-13 08:48:31 -0400177 node->input_main_loops_per_call = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 }
Damjan Marionceab7882018-01-19 20:56:12 +0100179 else if (is_main == 0 && vector_rate < 2 &&
180 nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] == 0)
181 {
182 timeout = 10e-3;
183 timeout_ms = max_timeout_ms;
184 node->input_main_loops_per_call = 0;
185 }
Dave Barach5c20a012017-06-13 08:48:31 -0400186 else /* busy */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 {
Dave Barach5c20a012017-06-13 08:48:31 -0400188 /* Don't come back for a respectable number of dispatch cycles */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 node->input_main_loops_per_call = 1024;
190 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
192 /* Allow any signal to wakeup our sleep. */
Damjan Marionceab7882018-01-19 20:56:12 +0100193 if (is_main || em->epoll_fd != -1)
194 {
195 static sigset_t unblock_all_signals;
196 n_fds_ready = epoll_pwait (em->epoll_fd,
197 em->epoll_events,
198 vec_len (em->epoll_events),
199 timeout_ms, &unblock_all_signals);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400200
Damjan Marionceab7882018-01-19 20:56:12 +0100201 /* This kludge is necessary to run over absurdly old kernels */
202 if (n_fds_ready < 0 && errno == ENOSYS)
203 {
204 n_fds_ready = epoll_wait (em->epoll_fd,
205 em->epoll_events,
206 vec_len (em->epoll_events), timeout_ms);
207 }
208 }
209 else
210 {
211 if (timeout_ms)
212 usleep (timeout_ms * 1000);
213 return 0;
214 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 }
216
217 if (n_fds_ready < 0)
218 {
219 if (unix_error_is_fatal (errno))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400220 vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait"));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
222 /* non fatal error (e.g. EINTR). */
223 return 0;
224 }
225
226 em->epoll_waits += 1;
227 em->epoll_files_ready += n_fds_ready;
228
229 for (e = em->epoll_events; e < em->epoll_events + n_fds_ready; e++)
230 {
231 u32 i = e->data.u32;
Damjan Marion56dd5432017-09-08 19:52:02 +0200232 clib_file_t *f = pool_elt_at_index (fm->file_pool, i);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400233 clib_error_t *errors[4];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 int n_errors = 0;
235
Dave Barach9b8ffd92016-07-08 08:13:45 -0400236 if (PREDICT_TRUE (!(e->events & EPOLLERR)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 {
238 if (e->events & EPOLLIN)
239 {
240 errors[n_errors] = f->read_function (f);
Damjan Marionceab7882018-01-19 20:56:12 +0100241 f->read_events++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 n_errors += errors[n_errors] != 0;
243 }
244 if (e->events & EPOLLOUT)
245 {
246 errors[n_errors] = f->write_function (f);
Damjan Marionceab7882018-01-19 20:56:12 +0100247 f->write_events++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 n_errors += errors[n_errors] != 0;
249 }
250 }
251 else
252 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400253 if (f->error_function)
254 {
255 errors[n_errors] = f->error_function (f);
Damjan Marionceab7882018-01-19 20:56:12 +0100256 f->error_events++;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400257 n_errors += errors[n_errors] != 0;
258 }
Ole Troan4b12b3c2016-01-27 23:37:58 +0200259 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400260 close (f->file_descriptor);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 }
262
263 ASSERT (n_errors < ARRAY_LEN (errors));
264 for (i = 0; i < n_errors; i++)
265 {
266 unix_save_error (um, errors[i]);
267 }
268 }
269
270 return 0;
271}
272
Damjan Marionceab7882018-01-19 20:56:12 +0100273static uword
274linux_epoll_input (vlib_main_t * vm,
275 vlib_node_runtime_t * node, vlib_frame_t * frame)
276{
277 u32 thread_index = vlib_get_thread_index ();
278
279 if (thread_index == 0)
280 return linux_epoll_input_inline (vm, node, frame, 0);
281 else
282 return linux_epoll_input_inline (vm, node, frame, thread_index);
283}
284
Dave Barach9b8ffd92016-07-08 08:13:45 -0400285/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286VLIB_REGISTER_NODE (linux_epoll_input_node,static) = {
287 .function = linux_epoll_input,
288 .type = VLIB_NODE_TYPE_PRE_INPUT,
289 .name = "unix-epoll-input",
290};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400291/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
293clib_error_t *
294linux_epoll_input_init (vlib_main_t * vm)
295{
Damjan Marionceab7882018-01-19 20:56:12 +0100296 linux_epoll_main_t *em;
Damjan Marion56dd5432017-09-08 19:52:02 +0200297 clib_file_main_t *fm = &file_main;
Damjan Marionceab7882018-01-19 20:56:12 +0100298 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400299
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
Damjan Marionceab7882018-01-19 20:56:12 +0100301 vec_validate_aligned (linux_epoll_mains, tm->n_vlib_mains,
302 CLIB_CACHE_LINE_BYTES);
303
304 vec_foreach (em, linux_epoll_mains)
305 {
306 /* Allocate some events. */
307 vec_resize (em->epoll_events, VLIB_FRAME_SIZE);
308
309 if (linux_epoll_mains == em)
310 {
311 em->epoll_fd = epoll_create (1);
312 if (em->epoll_fd < 0)
313 return clib_error_return_unix (0, "epoll_create");
314 }
315 else
316 em->epoll_fd = -1;
317 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
Damjan Marion56dd5432017-09-08 19:52:02 +0200319 fm->file_update = linux_epoll_file_update;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
321 return 0;
322}
323
324VLIB_INIT_FUNCTION (linux_epoll_input_init);
325
326#endif /* HAVE_LINUX_EPOLL */
327
328static clib_error_t *
329unix_input_init (vlib_main_t * vm)
330{
331 return vlib_call_init_function (vm, linux_epoll_input_init);
332}
333
334VLIB_INIT_FUNCTION (unix_input_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400335
336/*
337 * fd.io coding-style-patch-verification: ON
338 *
339 * Local Variables:
340 * eval: (c-set-style "gnu")
341 * End:
342 */