blob: 07096ed27dc25b3da7712380188570a44c46ac4d [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>
43
44/* FIXME autoconf */
45#define HAVE_LINUX_EPOLL
46
47#ifdef HAVE_LINUX_EPOLL
48
49#include <sys/epoll.h>
50
Dave Barach9b8ffd92016-07-08 08:13:45 -040051typedef struct
52{
Ed Warnickecb9cada2015-12-08 15:45:58 -070053 int epoll_fd;
Dave Barach9b8ffd92016-07-08 08:13:45 -040054 struct epoll_event *epoll_events;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
56 /* Statistics. */
57 u64 epoll_files_ready;
58 u64 epoll_waits;
59} linux_epoll_main_t;
60
61static linux_epoll_main_t linux_epoll_main;
62
63static void
Dave Barach9b8ffd92016-07-08 08:13:45 -040064linux_epoll_file_update (unix_file_t * f, unix_file_update_type_t update_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
Dave Barach9b8ffd92016-07-08 08:13:45 -040066 unix_main_t *um = &unix_main;
67 linux_epoll_main_t *em = &linux_epoll_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 struct epoll_event e;
69
70 memset (&e, 0, sizeof (e));
71
72 e.events = EPOLLIN;
73 if (f->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE)
74 e.events |= EPOLLOUT;
Dave Barach9b8ffd92016-07-08 08:13:45 -040075 if (f->flags & UNIX_FILE_EVENT_EDGE_TRIGGERED)
76 e.events |= EPOLLET;
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 e.data.u32 = f - um->file_pool;
78
79 if (epoll_ctl (em->epoll_fd,
80 (update_type == UNIX_FILE_UPDATE_ADD
81 ? EPOLL_CTL_ADD
82 : (update_type == UNIX_FILE_UPDATE_MODIFY
83 ? EPOLL_CTL_MOD
Dave Barach9b8ffd92016-07-08 08:13:45 -040084 : EPOLL_CTL_DEL)), f->file_descriptor, &e) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 clib_warning ("epoll_ctl");
86}
87
88static uword
89linux_epoll_input (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -040090 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -070091{
Dave Barach9b8ffd92016-07-08 08:13:45 -040092 unix_main_t *um = &unix_main;
93 linux_epoll_main_t *em = &linux_epoll_main;
94 struct epoll_event *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 int n_fds_ready;
96
97 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040098 vlib_node_main_t *nm = &vm->node_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 u64 t = nm->cpu_time_next_process_ready;
100 f64 timeout;
101 int timeout_ms, max_timeout_ms = 10;
102 f64 vector_rate = vlib_last_vectors_per_main_loop (vm);
103
104 if (t == ~0ULL)
105 {
106 timeout = 10e-3;
107 timeout_ms = max_timeout_ms;
108 }
109 else
110 {
111 timeout =
112 (((i64) t - (i64) clib_cpu_time_now ())
113 * vm->clib_time.seconds_per_clock)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400114 /* subtract off some slop time */ - 50e-6;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400115
Damjan Marion18bc9072016-12-07 14:07:54 +0100116 if (timeout < 1e3)
117 {
118 /* We have event happenning in less than 1 ms so
119 don't allow epoll to wait */
120 timeout_ms = 0;
121 }
122 else
123 {
124 timeout_ms = timeout * 1e3;
125
126 /* Must be between 1 and 10 ms. */
127 timeout_ms = clib_max (1, timeout_ms);
128 timeout_ms = clib_min (max_timeout_ms, timeout_ms);
129 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 }
131
132 /* If we still have input nodes polling (e.g. vnet packet generator)
133 don't sleep. */
134 if (nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] > 0)
135 timeout_ms = 0;
136
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137 /*
138 * When busy: don't wait & only epoll for input
139 * every 1024 times through main loop.
Dave Barachdae88b92016-04-19 09:38:35 -0400140 */
141 if (vector_rate > 1 || vm->api_queue_nonempty)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 timeout_ms = 0;
144 node->input_main_loops_per_call = 1024;
145 }
146 else
147 /* We're not busy; go to sleep for a while. */
148 node->input_main_loops_per_call = 0;
149
150 /* Allow any signal to wakeup our sleep. */
151 {
152 static sigset_t unblock_all_signals;
153 n_fds_ready = epoll_pwait (em->epoll_fd,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400154 em->epoll_events,
155 vec_len (em->epoll_events),
156 timeout_ms, &unblock_all_signals);
157
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 /* This kludge is necessary to run over absurdly old kernels */
159 if (n_fds_ready < 0 && errno == ENOSYS)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 {
161 n_fds_ready = epoll_wait (em->epoll_fd,
162 em->epoll_events,
163 vec_len (em->epoll_events), timeout_ms);
164 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 }
166 }
167
168 if (n_fds_ready < 0)
169 {
170 if (unix_error_is_fatal (errno))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400171 vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait"));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
173 /* non fatal error (e.g. EINTR). */
174 return 0;
175 }
176
177 em->epoll_waits += 1;
178 em->epoll_files_ready += n_fds_ready;
179
180 for (e = em->epoll_events; e < em->epoll_events + n_fds_ready; e++)
181 {
182 u32 i = e->data.u32;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400183 unix_file_t *f = pool_elt_at_index (um->file_pool, i);
184 clib_error_t *errors[4];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 int n_errors = 0;
186
Dave Barach9b8ffd92016-07-08 08:13:45 -0400187 if (PREDICT_TRUE (!(e->events & EPOLLERR)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188 {
189 if (e->events & EPOLLIN)
190 {
191 errors[n_errors] = f->read_function (f);
192 n_errors += errors[n_errors] != 0;
193 }
194 if (e->events & EPOLLOUT)
195 {
196 errors[n_errors] = f->write_function (f);
197 n_errors += errors[n_errors] != 0;
198 }
199 }
200 else
201 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400202 if (f->error_function)
203 {
204 errors[n_errors] = f->error_function (f);
205 n_errors += errors[n_errors] != 0;
206 }
Ole Troan4b12b3c2016-01-27 23:37:58 +0200207 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400208 close (f->file_descriptor);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209 }
210
211 ASSERT (n_errors < ARRAY_LEN (errors));
212 for (i = 0; i < n_errors; i++)
213 {
214 unix_save_error (um, errors[i]);
215 }
216 }
217
218 return 0;
219}
220
Dave Barach9b8ffd92016-07-08 08:13:45 -0400221/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222VLIB_REGISTER_NODE (linux_epoll_input_node,static) = {
223 .function = linux_epoll_input,
224 .type = VLIB_NODE_TYPE_PRE_INPUT,
225 .name = "unix-epoll-input",
226};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400227/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
229clib_error_t *
230linux_epoll_input_init (vlib_main_t * vm)
231{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400232 linux_epoll_main_t *em = &linux_epoll_main;
233 unix_main_t *um = &unix_main;
234
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 /* Allocate some events. */
236 vec_resize (em->epoll_events, VLIB_FRAME_SIZE);
237
238 em->epoll_fd = epoll_create (vec_len (em->epoll_events));
239 if (em->epoll_fd < 0)
240 return clib_error_return_unix (0, "epoll_create");
241
242 um->file_update = linux_epoll_file_update;
243
244 return 0;
245}
246
247VLIB_INIT_FUNCTION (linux_epoll_input_init);
248
249#endif /* HAVE_LINUX_EPOLL */
250
251static clib_error_t *
252unix_input_init (vlib_main_t * vm)
253{
254 return vlib_call_init_function (vm, linux_epoll_input_init);
255}
256
257VLIB_INIT_FUNCTION (unix_input_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400258
259/*
260 * fd.io coding-style-patch-verification: ON
261 *
262 * Local Variables:
263 * eval: (c-set-style "gnu")
264 * End:
265 */