FD.io VPP  v17.10-9-gd594711
Vector Packet Processing
udp_input.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/pg/pg.h>
19 #include <vnet/ip/ip.h>
20 
21 #include <vnet/udp/udp.h>
22 #include <vppinfra/hash.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/elog.h>
25 
26 #include <vnet/udp/udp_packet.h>
27 
28 #include <vlibmemory/api.h>
29 #include "../session/application_interface.h"
30 
32 
33 typedef struct
34 {
39 
40 /* packet trace format function */
41 static u8 *
42 format_udp4_uri_input_trace (u8 * s, va_list * args)
43 {
44  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
45  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
46  udp4_uri_input_trace_t *t = va_arg (*args, udp4_uri_input_trace_t *);
47 
48  s = format (s, "UDP4_URI_INPUT: session %d, disposition %d, thread %d",
49  t->session, t->disposition, t->thread_index);
50  return s;
51 }
52 
53 typedef enum
54 {
58 
59 static char *udp4_uri_input_error_strings[] = {
60 #define _(sym,string) string,
62 #undef _
63 };
64 
65 static uword
67  vlib_node_runtime_t * node, vlib_frame_t * frame)
68 {
69  u32 n_left_from, *from, *to_next;
70  udp4_uri_input_next_t next_index;
73  u32 my_thread_index = vm->thread_index;
74  u8 my_enqueue_epoch;
75  u32 *session_indices_to_enqueue;
76  static u32 serial_number;
77  int i;
78 
79  my_enqueue_epoch = ++smm->current_enqueue_epoch[my_thread_index];
80 
81  from = vlib_frame_vector_args (frame);
82  n_left_from = frame->n_vectors;
83  next_index = node->cached_next_index;
84 
85  while (n_left_from > 0)
86  {
87  u32 n_left_to_next;
88 
89  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
90 
91  while (n_left_from > 0 && n_left_to_next > 0)
92  {
93  u32 bi0;
94  vlib_buffer_t *b0;
96  u32 error0 = SESSION_ERROR_ENQUEUED;
97  udp_header_t *udp0;
98  ip4_header_t *ip0;
99  stream_session_t *s0;
100  svm_fifo_t *f0;
101  u16 udp_len0;
102  u8 *data0;
103 
104  /* speculatively enqueue b0 to the current next frame */
105  bi0 = from[0];
106  to_next[0] = bi0;
107  from += 1;
108  to_next += 1;
109  n_left_from -= 1;
110  n_left_to_next -= 1;
111 
112  b0 = vlib_get_buffer (vm, bi0);
113 
114  /* udp_local hands us a pointer to the udp data */
115 
116  data0 = vlib_buffer_get_current (b0);
117  udp0 = (udp_header_t *) (data0 - sizeof (*udp0));
118 
119  /* $$$$ fixme: udp_local doesn't do ip options correctly anyhow */
120  ip0 = (ip4_header_t *) (((u8 *) udp0) - sizeof (*ip0));
121  s0 = 0;
122 
123  /* lookup session */
125  udp0->dst_port, udp0->src_port,
126  SESSION_TYPE_IP4_UDP);
127 
128  /* no listener */
129  if (PREDICT_FALSE (s0 == 0))
130  {
131  error0 = SESSION_ERROR_NO_LISTENER;
132  goto trace0;
133  }
134 
135  f0 = s0->server_rx_fifo;
136 
137  /* established hit */
138  if (PREDICT_TRUE (s0->session_state == SESSION_STATE_READY))
139  {
140  udp_len0 = clib_net_to_host_u16 (udp0->length);
141 
142  if (PREDICT_FALSE (udp_len0 > svm_fifo_max_enqueue (f0)))
143  {
144  error0 = SESSION_ERROR_FIFO_FULL;
145  goto trace0;
146  }
147 
148  svm_fifo_enqueue_nowait (f0, udp_len0 - sizeof (*udp0),
149  (u8 *) (udp0 + 1));
150 
151  b0->error = node->errors[SESSION_ERROR_ENQUEUED];
152 
153  /* We need to send an RX event on this fifo */
154  if (s0->enqueue_epoch != my_enqueue_epoch)
155  {
156  s0->enqueue_epoch = my_enqueue_epoch;
157 
158  vec_add1 (smm->session_indices_to_enqueue_by_thread
159  [my_thread_index],
160  s0 - smm->sessions[my_thread_index]);
161  }
162  }
163  /* listener hit */
164  else if (s0->session_state == SESSION_STATE_LISTENING)
165  {
166  udp_connection_t *us;
167  int rv;
168 
169  error0 = SESSION_ERROR_NOT_READY;
170 
171  /*
172  * create udp transport session
173  */
174  pool_get (um->udp_sessions[my_thread_index], us);
175 
176  us->mtu = 1024; /* $$$$ policy */
177 
178  us->c_lcl_ip4.as_u32 = ip0->dst_address.as_u32;
179  us->c_rmt_ip4.as_u32 = ip0->src_address.as_u32;
180  us->c_lcl_port = udp0->dst_port;
181  us->c_rmt_port = udp0->src_port;
182  us->c_transport_proto = TRANSPORT_PROTO_UDP;
183  us->c_c_index = us - um->udp_sessions[my_thread_index];
184 
185  /*
186  * create stream session and attach the udp session to it
187  */
188  rv = stream_session_accept (&us->connection, s0->session_index,
189  SESSION_TYPE_IP4_UDP,
190  1 /*notify */ );
191  if (rv)
192  error0 = rv;
193 
194  }
195  else
196  {
197 
198  error0 = SESSION_ERROR_NOT_READY;
199  goto trace0;
200  }
201 
202  trace0:
203  b0->error = node->errors[error0];
204 
206  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
207  {
209  vlib_add_trace (vm, node, b0, sizeof (*t));
210 
211  t->session = ~0;
212  if (s0)
213  t->session = s0 - smm->sessions[my_thread_index];
214  t->disposition = error0;
215  t->thread_index = my_thread_index;
216  }
217 
218  /* verify speculative enqueue, maybe switch current next frame */
219  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
220  to_next, n_left_to_next,
221  bi0, next0);
222  }
223 
224  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
225  }
226 
227  /* Send enqueue events */
228 
229  session_indices_to_enqueue =
230  smm->session_indices_to_enqueue_by_thread[my_thread_index];
231 
232  for (i = 0; i < vec_len (session_indices_to_enqueue); i++)
233  {
234  session_fifo_event_t evt;
236  stream_session_t *s0;
237  application_t *server0;
238 
239  /* Get session */
240  s0 = pool_elt_at_index (smm->sessions[my_thread_index],
241  session_indices_to_enqueue[i]);
242 
243  /* Get session's server */
244  server0 = application_get (s0->app_index);
245 
246  /* Built-in server? Deliver the goods... */
247  if (server0->cb_fns.builtin_server_rx_callback)
248  {
249  server0->cb_fns.builtin_server_rx_callback (s0);
250  continue;
251  }
252 
253  if (svm_fifo_set_event (s0->server_rx_fifo))
254  {
255  /* Fabricate event */
256  evt.fifo = s0->server_rx_fifo;
257  evt.event_type = FIFO_EVENT_APP_RX;
258  evt.event_id = serial_number++;
259 
260  /* Add event to server's event queue */
261  q = server0->event_queue;
262 
263  /* Don't block for lack of space */
264  if (PREDICT_TRUE (q->cursize < q->maxsize))
265  {
266  unix_shared_memory_queue_add (server0->event_queue,
267  (u8 *) & evt,
268  0 /* do wait for mutex */ );
269  }
270  else
271  {
273  SESSION_ERROR_FIFO_FULL, 1);
274  }
275  }
276  /* *INDENT-OFF* */
277  if (1)
278  {
279  ELOG_TYPE_DECLARE (e) =
280  {
281  .format = "evt-enqueue: id %d length %d",
282  .format_args = "i4i4",};
283  struct
284  {
285  u32 data[2];
286  } *ed;
288  ed->data[0] = evt.event_id;
289  ed->data[1] = svm_fifo_max_dequeue (s0->server_rx_fifo);
290  }
291  /* *INDENT-ON* */
292 
293  }
294 
295  vec_reset_length (session_indices_to_enqueue);
296 
297  smm->session_indices_to_enqueue_by_thread[my_thread_index] =
298  session_indices_to_enqueue;
299 
300  return frame->n_vectors;
301 }
302 
304 {
305  .function = udp4_uri_input_node_fn,.name = "udp4-uri-input",.vector_size =
306  sizeof (u32),.format_trace = format_udp4_uri_input_trace,.type =
307  VLIB_NODE_TYPE_INTERNAL,.n_errors =
308  ARRAY_LEN (udp4_uri_input_error_strings),.error_strings =
310  /* edit / add dispositions here */
311  .next_nodes =
312  {
313  [UDP4_URI_INPUT_NEXT_DROP] = "error-drop",}
314 ,};
315 
316 /*
317  * fd.io coding-style-patch-verification: ON
318  *
319  * Local Variables:
320  * eval: (c-set-style "gnu")
321  * End:
322  */
vlib_main_t vlib_global_main
Definition: main.c:1650
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
#define CLIB_UNUSED(x)
Definition: clib.h:79
int stream_session_accept(transport_connection_t *tc, u32 listener_index, u8 sst, u8 notify)
Accept a stream session.
Definition: session.c:594
ip4_address_t src_address
Definition: ip4_packet.h:164
#define PREDICT_TRUE(x)
Definition: clib.h:98
u32 thread_index
Definition: main.h:173
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:518
struct _vlib_node_registration vlib_node_registration_t
static u32 svm_fifo_max_enqueue(svm_fifo_t *f)
Definition: svm_fifo.h:106
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:415
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:225
vlib_node_registration_t udp4_uri_input_node
(constructor) VLIB_REGISTER_NODE (udp4_uri_input_node)
Definition: udp_input.c:31
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
static uword udp4_uri_input_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: udp_input.c:66
struct _svm_fifo svm_fifo_t
static u8 * format_udp4_uri_input_trace(u8 *s, va_list *args)
Definition: udp_input.c:42
ip4_address_t dst_address
Definition: ip4_packet.h:164
static u32 svm_fifo_max_dequeue(svm_fifo_t *f)
Definition: svm_fifo.h:100
struct _stream_session_t stream_session_t
struct _udp_uri_main udp_uri_main_t
int unix_shared_memory_queue_add(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
static session_manager_main_t * vnet_get_session_manager_main()
Definition: session.h:197
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:458
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:193
udp4_uri_input_next_t
Definition: udp_input.c:53
#define ELOG_DATA(em, f)
Definition: elog.h:481
#define PREDICT_FALSE(x)
Definition: clib.h:97
static char * udp4_uri_input_error_strings[]
Definition: udp_input.c:59
struct _session_manager_main session_manager_main_t
Definition: session.h:113
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:216
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:364
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:113
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1158
static u8 svm_fifo_set_event(svm_fifo_t *f)
Sets fifo event flag.
Definition: svm_fifo.h:123
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
u16 n_vectors
Definition: node.h:344
vlib_main_t * vm
Definition: buffer.c:283
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
elog_main_t elog_main
Definition: main.h:155
stream_session_t * stream_session_lookup4(ip4_address_t *lcl, ip4_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto)
Looks up a session based on the 5-tuple passed as argument.
struct _application application_t
#define ARRAY_LEN(x)
Definition: clib.h:59
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:454
#define ELOG_TYPE_DECLARE(f)
Definition: elog.h:439
int svm_fifo_enqueue_nowait(svm_fifo_t *f, u32 max_bytes, u8 *copy_from_here)
Definition: svm_fifo.c:533
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
unsigned int u32
Definition: types.h:88
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:259
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
unsigned short u16
Definition: types.h:57
transport_connection_t connection
Definition: udp.h:31
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
application_t * application_get(u32 index)
Definition: application.c:224
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
#define foreach_session_input_error
Definition: session.h:63
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u16 flags
Copy of main node flags.
Definition: node.h:450
u32 mtu
must be first
Definition: udp.h:34
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:75
static udp_uri_main_t * vnet_get_udp_main()
Definition: udp.h:54
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
struct _unix_shared_memory_queue unix_shared_memory_queue_t