ea10e4fc3542d9e714b010213572311812b74b92
[vpp.git] / vlib / vlib / unix / input.c
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>
43
44 /* FIXME autoconf */
45 #define HAVE_LINUX_EPOLL
46
47 #ifdef HAVE_LINUX_EPOLL
48
49 #include <sys/epoll.h>
50
51 typedef struct {
52   int epoll_fd;
53   struct epoll_event * epoll_events;
54
55   /* Statistics. */
56   u64 epoll_files_ready;
57   u64 epoll_waits;
58 } linux_epoll_main_t;
59
60 static linux_epoll_main_t linux_epoll_main;
61
62 static void
63 linux_epoll_file_update (unix_file_t * f,
64                          unix_file_update_type_t update_type)
65 {
66   unix_main_t * um = &unix_main;
67   linux_epoll_main_t * em = &linux_epoll_main;
68   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;
75   e.data.u32 = f - um->file_pool;
76
77   if (epoll_ctl (em->epoll_fd,
78                  (update_type == UNIX_FILE_UPDATE_ADD
79                   ? EPOLL_CTL_ADD
80                   : (update_type == UNIX_FILE_UPDATE_MODIFY
81                      ? EPOLL_CTL_MOD
82                      : EPOLL_CTL_DEL)),
83                  f->file_descriptor,
84                  &e) < 0)
85     clib_warning ("epoll_ctl");
86 }
87
88 static uword
89 linux_epoll_input (vlib_main_t * vm,
90                    vlib_node_runtime_t * node,
91                    vlib_frame_t * frame)
92 {
93   unix_main_t * um = &unix_main;
94   linux_epoll_main_t * em = &linux_epoll_main;
95   struct epoll_event * e;
96   int n_fds_ready;
97
98   {
99     vlib_node_main_t * nm = &vm->node_main;
100     u64 t = nm->cpu_time_next_process_ready;
101     f64 timeout;
102     int timeout_ms, max_timeout_ms = 10;
103     f64 vector_rate = vlib_last_vectors_per_main_loop (vm);
104
105     if (t == ~0ULL)
106       {
107         timeout = 10e-3;
108         timeout_ms = max_timeout_ms;
109       }
110     else
111       {
112         timeout =
113           (((i64) t - (i64) clib_cpu_time_now ())
114            * vm->clib_time.seconds_per_clock)
115           /* subtract off some slop time */ - 50e-6;
116         timeout_ms = timeout * 1e3;
117         
118         /* Must be between 1 and 10 ms. */
119         timeout_ms = clib_max (1, timeout_ms);
120         timeout_ms = clib_min (max_timeout_ms, timeout_ms);
121       }
122
123     /* If we still have input nodes polling (e.g. vnet packet generator)
124        don't sleep. */
125     if (nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] > 0)
126       timeout_ms = 0;
127
128     if (vector_rate > 1)
129       {
130         /* When busy don't wait & only epoll for input every 8 times
131            through main loop. */
132         timeout_ms = 0;
133         node->input_main_loops_per_call = 1024;
134       }
135     else
136       /* We're not busy; go to sleep for a while. */
137       node->input_main_loops_per_call = 0;
138
139     /* Allow any signal to wakeup our sleep. */
140     {
141       static sigset_t unblock_all_signals;
142       n_fds_ready = epoll_pwait (em->epoll_fd,
143                                  em->epoll_events,
144                                  vec_len (em->epoll_events),
145                                  timeout_ms,
146                                  &unblock_all_signals);
147       
148       /* This kludge is necessary to run over absurdly old kernels */
149       if (n_fds_ready < 0 && errno == ENOSYS)
150         {
151           n_fds_ready = epoll_wait (em->epoll_fd,
152                                     em->epoll_events,
153                                     vec_len (em->epoll_events),
154                                     timeout_ms);
155         }
156     }
157   }
158
159   if (n_fds_ready < 0)
160     {
161       if (unix_error_is_fatal (errno))
162         vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait"));
163
164       /* non fatal error (e.g. EINTR). */
165       return 0;
166     }
167
168   em->epoll_waits += 1;
169   em->epoll_files_ready += n_fds_ready;
170
171   for (e = em->epoll_events; e < em->epoll_events + n_fds_ready; e++)
172     {
173       u32 i = e->data.u32;
174       unix_file_t * f = pool_elt_at_index (um->file_pool, i);
175       clib_error_t * errors[4];
176       int n_errors = 0;
177
178       if (PREDICT_TRUE (! (e->events & EPOLLERR)))
179         {
180           if (e->events & EPOLLIN)
181             {
182               errors[n_errors] = f->read_function (f);
183               n_errors += errors[n_errors] != 0;
184             }
185           if (e->events & EPOLLOUT)
186             {
187               errors[n_errors] = f->write_function (f);
188               n_errors += errors[n_errors] != 0;
189             }
190         }
191       else
192         {
193           if (f->error_function)
194             {
195               errors[n_errors] = f->error_function (f);
196               n_errors += errors[n_errors] != 0;
197             }
198         }
199
200       ASSERT (n_errors < ARRAY_LEN (errors));
201       for (i = 0; i < n_errors; i++)
202         {
203           unix_save_error (um, errors[i]);
204         }
205     }
206
207   return 0;
208 }
209
210 VLIB_REGISTER_NODE (linux_epoll_input_node,static) = {
211   .function = linux_epoll_input,
212   .type = VLIB_NODE_TYPE_PRE_INPUT,
213   .name = "unix-epoll-input",
214 };
215
216 clib_error_t *
217 linux_epoll_input_init (vlib_main_t * vm)
218 {
219   linux_epoll_main_t * em = &linux_epoll_main;
220   unix_main_t * um = &unix_main;
221   
222   /* Allocate some events. */
223   vec_resize (em->epoll_events, VLIB_FRAME_SIZE);
224
225   em->epoll_fd = epoll_create (vec_len (em->epoll_events));
226   if (em->epoll_fd < 0)
227     return clib_error_return_unix (0, "epoll_create");
228
229   um->file_update = linux_epoll_file_update;
230
231   return 0;
232 }
233
234 VLIB_INIT_FUNCTION (linux_epoll_input_init);
235
236 #endif /* HAVE_LINUX_EPOLL */
237
238 static clib_error_t *
239 unix_input_init (vlib_main_t * vm)
240 {
241   return vlib_call_init_function (vm, linux_epoll_input_init);
242 }
243
244 VLIB_INIT_FUNCTION (unix_input_init);