FD.io VPP  v16.06
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 
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. */
59 
61 
62 static void
64  unix_file_update_type_t update_type)
65 {
66  unix_main_t * um = &unix_main;
68  struct epoll_event e;
69 
70  memset (&e, 0, sizeof (e));
71 
72  e.events = EPOLLIN;
74  e.events |= EPOLLOUT;
76  e.events |= EPOLLET;
77  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
84  : EPOLL_CTL_DEL)),
85  f->file_descriptor,
86  &e) < 0)
87  clib_warning ("epoll_ctl");
88 }
89 
90 static uword
92  vlib_node_runtime_t * node,
93  vlib_frame_t * frame)
94 {
95  unix_main_t * um = &unix_main;
97  struct epoll_event * e;
98  int n_fds_ready;
99 
100  {
101  vlib_node_main_t * nm = &vm->node_main;
103  f64 timeout;
104  int timeout_ms, max_timeout_ms = 10;
105  f64 vector_rate = vlib_last_vectors_per_main_loop (vm);
106 
107  if (t == ~0ULL)
108  {
109  timeout = 10e-3;
110  timeout_ms = max_timeout_ms;
111  }
112  else
113  {
114  timeout =
115  (((i64) t - (i64) clib_cpu_time_now ())
117  /* subtract off some slop time */ - 50e-6;
118  timeout_ms = timeout * 1e3;
119 
120  /* Must be between 1 and 10 ms. */
121  timeout_ms = clib_max (1, timeout_ms);
122  timeout_ms = clib_min (max_timeout_ms, timeout_ms);
123  }
124 
125  /* If we still have input nodes polling (e.g. vnet packet generator)
126  don't sleep. */
127  if (nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] > 0)
128  timeout_ms = 0;
129 
130  /*
131  * When busy: don't wait & only epoll for input
132  * every 1024 times through main loop.
133  */
134  if (vector_rate > 1 || vm->api_queue_nonempty)
135  {
136  timeout_ms = 0;
137  node->input_main_loops_per_call = 1024;
138  }
139  else
140  /* We're not busy; go to sleep for a while. */
141  node->input_main_loops_per_call = 0;
142 
143  /* Allow any signal to wakeup our sleep. */
144  {
145  static sigset_t unblock_all_signals;
146  n_fds_ready = epoll_pwait (em->epoll_fd,
147  em->epoll_events,
148  vec_len (em->epoll_events),
149  timeout_ms,
150  &unblock_all_signals);
151 
152  /* This kludge is necessary to run over absurdly old kernels */
153  if (n_fds_ready < 0 && errno == ENOSYS)
154  {
155  n_fds_ready = epoll_wait (em->epoll_fd,
156  em->epoll_events,
157  vec_len (em->epoll_events),
158  timeout_ms);
159  }
160  }
161  }
162 
163  if (n_fds_ready < 0)
164  {
165  if (unix_error_is_fatal (errno))
166  vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait"));
167 
168  /* non fatal error (e.g. EINTR). */
169  return 0;
170  }
171 
172  em->epoll_waits += 1;
173  em->epoll_files_ready += n_fds_ready;
174 
175  for (e = em->epoll_events; e < em->epoll_events + n_fds_ready; e++)
176  {
177  u32 i = e->data.u32;
178  unix_file_t * f = pool_elt_at_index (um->file_pool, i);
179  clib_error_t * errors[4];
180  int n_errors = 0;
181 
182  if (PREDICT_TRUE (! (e->events & EPOLLERR)))
183  {
184  if (e->events & EPOLLIN)
185  {
186  errors[n_errors] = f->read_function (f);
187  n_errors += errors[n_errors] != 0;
188  }
189  if (e->events & EPOLLOUT)
190  {
191  errors[n_errors] = f->write_function (f);
192  n_errors += errors[n_errors] != 0;
193  }
194  }
195  else
196  {
197  if (f->error_function)
198  {
199  errors[n_errors] = f->error_function (f);
200  n_errors += errors[n_errors] != 0;
201  }
202  else
203  close(f->file_descriptor);
204  }
205 
206  ASSERT (n_errors < ARRAY_LEN (errors));
207  for (i = 0; i < n_errors; i++)
208  {
209  unix_save_error (um, errors[i]);
210  }
211  }
212 
213  return 0;
214 }
215 
217  .function = linux_epoll_input,
218  .type = VLIB_NODE_TYPE_PRE_INPUT,
219  .name = "unix-epoll-input",
220 };
221 
222 clib_error_t *
224 {
226  unix_main_t * um = &unix_main;
227 
228  /* Allocate some events. */
230 
231  em->epoll_fd = epoll_create (vec_len (em->epoll_events));
232  if (em->epoll_fd < 0)
233  return clib_error_return_unix (0, "epoll_create");
234 
236 
237  return 0;
238 }
239 
241 
242 #endif /* HAVE_LINUX_EPOLL */
243 
244 static clib_error_t *
246 {
248 }
249 
unix_file_t * file_pool
Definition: unix.h:85
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
#define clib_min(x, y)
Definition: clib.h:295
u32 flags
Definition: unix.h:53
unix_file_function_t * read_function
Definition: unix.h:61
#define PREDICT_TRUE(x)
Definition: clib.h:98
static uword linux_epoll_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: input.c:91
u64 cpu_time_next_process_ready
Definition: node.h:606
clib_time_t clib_time
Definition: main.h:61
u32 input_main_loops_per_call
Definition: node.h:404
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
vlib_node_registration_t linux_epoll_input_node
(constructor) VLIB_REGISTER_NODE (linux_epoll_input_node)
Definition: input.c:216
#define clib_warning(format, args...)
Definition: error.h:59
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:199
#define vlib_call_init_function(vm, x)
Definition: init.h:159
#define pool_elt_at_index(p, i)
Definition: pool.h:346
u32 file_descriptor
Definition: unix.h:51
unix_file_function_t * error_function
Definition: unix.h:61
#define UNIX_FILE_EVENT_EDGE_TRIGGERED
Definition: unix.h:55
#define clib_error_return_unix(e, args...)
Definition: error.h:115
#define VLIB_FRAME_SIZE
Definition: node.h:292
f64 seconds_per_clock
Definition: time.h:56
always_inline word unix_error_is_fatal(word error)
Definition: error.h:130
long i64
Definition: types.h:82
clib_error_t * linux_epoll_input_init(vlib_main_t *vm)
Definition: input.c:223
static clib_error_t * unix_input_init(vlib_main_t *vm)
Definition: input.c:245
always_inline u32 vlib_last_vectors_per_main_loop(vlib_main_t *vm)
Definition: main.h:249
#define ARRAY_LEN(x)
Definition: clib.h:59
always_inline void vlib_panic_with_error(vlib_main_t *vm, clib_error_t *error)
Definition: main.h:223
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
unix_main_t unix_main
Definition: main.c:57
#define clib_max(x, y)
Definition: clib.h:288
u64 uword
Definition: types.h:112
struct epoll_event * epoll_events
Definition: input.c:53
static linux_epoll_main_t linux_epoll_main
Definition: input.c:60
unix_file_function_t * write_function
Definition: unix.h:61
#define UNIX_FILE_DATA_AVAILABLE_TO_WRITE
Definition: unix.h:54
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:140
u32 input_node_counts_by_state[VLIB_N_NODE_STATE]
Definition: node.h:622
vlib_node_main_t node_main
Definition: main.h:115
Definition: unix.h:49
u64 epoll_files_ready
Definition: input.c:56
volatile u32 api_queue_nonempty
Definition: main.h:173
always_inline void unix_save_error(unix_main_t *um, clib_error_t *error)
Definition: unix.h:163
void(* file_update)(unix_file_t *file, unix_file_update_type_t update_type)
Definition: unix.h:90
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
static void linux_epoll_file_update(unix_file_t *f, unix_file_update_type_t update_type)
Definition: input.c:63
unix_file_update_type_t
Definition: unix.h:69
always_inline u64 clib_cpu_time_now(void)
Definition: time.h:71