FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
input.c
Go to the documentation of this file.
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 #include <unistd.h>
45 
46 /* FIXME autoconf */
47 #define HAVE_LINUX_EPOLL
48 
49 #ifdef HAVE_LINUX_EPOLL
50 
51 #include <sys/epoll.h>
52 
53 typedef struct
54 {
55  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
56  int epoll_fd;
57  struct epoll_event *epoll_events;
59 
60  /* Statistics. */
64 
65 static linux_epoll_main_t *linux_epoll_mains = 0;
66 
67 static void
69 {
71  linux_epoll_main_t *em = vec_elt_at_index (linux_epoll_mains,
73  struct epoll_event e = { 0 };
74  int op, add_del = 0;
75 
76  e.events = EPOLLIN;
78  e.events |= EPOLLOUT;
80  e.events |= EPOLLET;
81  e.data.u32 = f - fm->file_pool;
82 
83  op = -1;
84 
85  switch (update_type)
86  {
88  op = EPOLL_CTL_ADD;
89  add_del = 1;
90  break;
91 
93  op = EPOLL_CTL_MOD;
94  break;
95 
97  op = EPOLL_CTL_DEL;
98  add_del = -1;
99  break;
100 
101  default:
102  clib_warning ("unknown update_type %d", update_type);
103  return;
104  }
105 
106  /* 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 
118  if (epoll_ctl (em->epoll_fd, op, f->file_descriptor, &e) < 0)
119  {
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  }
131 }
132 
135  vlib_frame_t * frame, u32 thread_index)
136 {
137  unix_main_t *um = &unix_main;
139  linux_epoll_main_t *em = vec_elt_at_index (linux_epoll_mains, thread_index);
140  struct epoll_event *e;
141  int n_fds_ready;
142  int is_main = (thread_index == 0);
143 
144  {
145  vlib_node_main_t *nm = &vm->node_main;
146  u32 ticks_until_expiration;
147  f64 timeout;
148  int timeout_ms = 0, max_timeout_ms = 10;
149  f64 vector_rate = vlib_last_vectors_per_main_loop (vm);
150 
151  /* If we're not working very hard, decide how long to sleep */
152  if (is_main && vector_rate < 2 && vm->api_queue_nonempty == 0
153  && nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] == 0)
154  {
155  ticks_until_expiration = TW (tw_timer_first_expires_in_ticks)
156  ((TWT (tw_timer_wheel) *) nm->timing_wheel);
157 
158  /* Nothing on the fast wheel, sleep 10ms */
159  if (ticks_until_expiration == TW_SLOTS_PER_RING)
160  {
161  timeout = 10e-3;
162  timeout_ms = max_timeout_ms;
163  }
164  else
165  {
166  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  }
176  }
177  node->input_main_loops_per_call = 0;
178  }
179  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  }
186  else /* busy */
187  {
188  /* Don't come back for a respectable number of dispatch cycles */
189  node->input_main_loops_per_call = 1024;
190  }
191 
192  /* Allow any signal to wakeup our sleep. */
193  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);
200 
201  /* 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  }
215  }
216 
217  if (n_fds_ready < 0)
218  {
219  if (unix_error_is_fatal (errno))
220  vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait"));
221 
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;
233  clib_error_t *errors[4];
234  int n_errors = 0;
235 
236  if (PREDICT_TRUE (!(e->events & EPOLLERR)))
237  {
238  if (e->events & EPOLLIN)
239  {
240  errors[n_errors] = f->read_function (f);
241  f->read_events++;
242  n_errors += errors[n_errors] != 0;
243  }
244  if (e->events & EPOLLOUT)
245  {
246  errors[n_errors] = f->write_function (f);
247  f->write_events++;
248  n_errors += errors[n_errors] != 0;
249  }
250  }
251  else
252  {
253  if (f->error_function)
254  {
255  errors[n_errors] = f->error_function (f);
256  f->error_events++;
257  n_errors += errors[n_errors] != 0;
258  }
259  else
260  close (f->file_descriptor);
261  }
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 
273 static uword
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 
285 /* *INDENT-OFF* */
287  .function = linux_epoll_input,
288  .type = VLIB_NODE_TYPE_PRE_INPUT,
289  .name = "unix-epoll-input",
290 };
291 /* *INDENT-ON* */
292 
293 clib_error_t *
295 {
296  linux_epoll_main_t *em;
299 
300 
301  vec_validate_aligned (linux_epoll_mains, tm->n_vlib_mains,
303 
304  vec_foreach (em, linux_epoll_mains)
305  {
306  /* Allocate some events. */
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  }
318 
320 
321  return 0;
322 }
323 
325 
326 #endif /* HAVE_LINUX_EPOLL */
327 
328 static clib_error_t *
330 {
332 }
333 
335 
336 /*
337  * fd.io coding-style-patch-verification: ON
338  *
339  * Local Variables:
340  * eval: (c-set-style "gnu")
341  * End:
342  */
#define UNIX_FILE_EVENT_EDGE_TRIGGERED
Definition: file.h:58
unix_main_t unix_main
Definition: main.c:62
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
#define clib_min(x, y)
Definition: clib.h:340
static void vlib_panic_with_error(vlib_main_t *vm, clib_error_t *error)
Definition: main.h:272
static clib_error_t * unix_input_init(vlib_main_t *vm)
Definition: input.c:329
static vlib_node_registration_t linux_epoll_input_node
(constructor) VLIB_REGISTER_NODE (linux_epoll_input_node)
Definition: input.c:286
#define PREDICT_TRUE(x)
Definition: clib.h:106
u32 file_descriptor
Definition: file.h:54
int i
u32 polling_thread_index
Definition: file.h:61
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:445
static u32 vlib_last_vectors_per_main_loop(vlib_main_t *vm)
Definition: main.h:300
clib_file_function_t * read_function
Definition: file.h:67
u32 input_main_loops_per_call
For input nodes: decremented on each main loop interation until it reaches zero and function is calle...
Definition: node.h:439
clib_file_t * file_pool
Definition: file.h:88
#define static_always_inline
Definition: clib.h:93
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
clib_file_update_type_t
Definition: file.h:78
static uword linux_epoll_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: input.c:274
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
clib_file_main_t file_main
Definition: main.c:63
unsigned long u64
Definition: types.h:89
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:237
#define vlib_call_init_function(vm, x)
Definition: init.h:162
#define VLIB_FRAME_SIZE
Definition: node.h:328
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:461
#define clib_error_return_unix(e, args...)
Definition: error.h:102
#define TW_SLOTS_PER_RING
#define TWT(a)
u32 flags
Definition: file.h:56
void(* file_update)(clib_file_t *file, clib_file_update_type_t update_type)
Definition: file.h:90
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:294
static void linux_epoll_file_update(clib_file_t *f, clib_file_update_type_t update_type)
Definition: input.c:68
#define clib_warning(format, args...)
Definition: error.h:59
#define ARRAY_LEN(x)
Definition: clib.h:59
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
static word unix_error_is_fatal(word error)
Definition: error.h:118
clib_error_t * linux_epoll_input_init(vlib_main_t *vm)
Definition: input.c:294
static_always_inline uword linux_epoll_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u32 thread_index)
Definition: input.c:134
#define UNIX_FILE_DATA_AVAILABLE_TO_WRITE
Definition: file.h:57
#define clib_max(x, y)
Definition: clib.h:333
u64 uword
Definition: types.h:112
struct epoll_event * epoll_events
Definition: input.c:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
u32 input_node_counts_by_state[VLIB_N_NODE_STATE]
Definition: node.h:693
u64 read_events
Definition: file.h:73
vlib_node_main_t node_main
Definition: main.h:132
#define clib_unix_warning(format, args...)
Definition: error.h:68
u64 epoll_files_ready
Definition: input.c:61
clib_file_function_t * error_function
Definition: file.h:67
u64 write_events
Definition: file.h:74
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
static void unix_save_error(unix_main_t *um, clib_error_t *error)
Definition: unix.h:112
u64 error_events
Definition: file.h:75
#define vec_foreach(var, vec)
Vector iterator.
Definition: file.h:51
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
#define TW(a)
clib_file_function_t * write_function
Definition: file.h:67
void * timing_wheel
Definition: node.h:669