FD.io VPP  v18.01-8-g0eacf49
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 
31 static char *udp_error_strings[] = {
32 #define udp_error(n,s) s,
33 #include "udp_error.def"
34 #undef udp_error
35 };
36 
37 typedef struct
38 {
43 
44 /* packet trace format function */
45 static u8 *
46 format_udp_input_trace (u8 * s, va_list * args)
47 {
48  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50  udp_input_trace_t *t = va_arg (*args, udp_input_trace_t *);
51 
52  s = format (s, "UDP_INPUT: connection %d, disposition %d, thread %d",
54  return s;
55 }
56 
57 #define foreach_udp_input_next \
58  _ (DROP, "error-drop")
59 
60 typedef enum
61 {
62 #define _(s, n) UDP_INPUT_NEXT_##s,
64 #undef _
67 
68 always_inline void
69 udp_input_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
70 {
71  if (PREDICT_TRUE (!val))
72  return;
73 
74  if (is_ip4)
75  vlib_node_increment_counter (vm, udp4_input_node.index, evt, val);
76  else
77  vlib_node_increment_counter (vm, udp6_input_node.index, evt, val);
78 }
79 
82  vlib_frame_t * frame, u8 is_ip4)
83 {
84  u32 n_left_from, *from, *to_next;
85  u32 next_index, errors;
86  u32 my_thread_index = vm->thread_index;
87 
88  from = vlib_frame_vector_args (frame);
89  n_left_from = frame->n_vectors;
90  next_index = node->cached_next_index;
91 
92  while (n_left_from > 0)
93  {
94  u32 n_left_to_next;
95 
96  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
97 
98  while (n_left_from > 0 && n_left_to_next > 0)
99  {
100  u32 bi0, fib_index0;
101  vlib_buffer_t *b0;
102  u32 next0 = UDP_INPUT_NEXT_DROP;
103  u32 error0 = UDP_ERROR_ENQUEUED;
104  udp_header_t *udp0;
105  ip4_header_t *ip40;
106  ip6_header_t *ip60;
107  u8 *data0;
108  stream_session_t *s0;
109  transport_connection_t *tc0 = 0;
110  udp_connection_t *child0, *new_uc0;
111  int written0;
112 
113  /* speculatively enqueue b0 to the current next frame */
114  bi0 = from[0];
115  to_next[0] = bi0;
116  from += 1;
117  to_next += 1;
118  n_left_from -= 1;
119  n_left_to_next -= 1;
120 
121  b0 = vlib_get_buffer (vm, bi0);
122 
123  /* udp_local hands us a pointer to the udp data */
124  data0 = vlib_buffer_get_current (b0);
125  udp0 = (udp_header_t *) (data0 - sizeof (*udp0));
126  fib_index0 = vnet_buffer (b0)->ip.fib_index;
127 
128  if (is_ip4)
129  {
130  /* $$$$ fixme: udp_local doesn't do ip options correctly anyhow */
131  ip40 = (ip4_header_t *) (((u8 *) udp0) - sizeof (*ip40));
132  s0 = session_lookup_safe4 (fib_index0, &ip40->dst_address,
133  &ip40->src_address, udp0->dst_port,
135  }
136  else
137  {
138  ip60 = (ip6_header_t *) (((u8 *) udp0) - sizeof (*ip60));
139  s0 = session_lookup_safe6 (fib_index0, &ip60->dst_address,
140  &ip60->src_address, udp0->dst_port,
142  }
143 
144  if (PREDICT_FALSE (s0 == 0))
145  {
146  error0 = UDP_ERROR_NO_LISTENER;
147  goto trace0;
148  }
149 
150  if (PREDICT_TRUE (s0->session_state == SESSION_STATE_READY))
151  {
152  tc0 = session_get_transport (s0);
153  }
154  else if (s0->session_state == SESSION_STATE_CONNECTING_READY)
155  {
156  /*
157  * Clone the transport. It will be cleaned up with the
158  * session once we notify the session layer.
159  */
160  new_uc0 = udp_conenction_clone_safe (s0->connection_index,
161  s0->thread_index);
162  ASSERT (s0->session_index == new_uc0->c_s_index);
163 
164  /*
165  * Drop the 'lock' on pool resize
166  */
167  session_pool_remove_peeker (s0->thread_index);
169  s0->thread_index, &s0);
170  tc0 = &new_uc0->connection;
171  }
172  else if (s0->session_state == SESSION_STATE_LISTENING)
173  {
174  tc0 = listen_session_get_transport (s0);
175 
176  child0 = udp_connection_alloc (my_thread_index);
177  if (is_ip4)
178  {
179  ip_set (&child0->c_lcl_ip, &ip40->dst_address, 1);
180  ip_set (&child0->c_rmt_ip, &ip40->src_address, 1);
181  }
182  else
183  {
184  ip_set (&child0->c_lcl_ip, &ip60->dst_address, 0);
185  ip_set (&child0->c_rmt_ip, &ip60->src_address, 0);
186  }
187  child0->c_lcl_port = udp0->dst_port;
188  child0->c_rmt_port = udp0->src_port;
189  child0->c_is_ip4 = is_ip4;
190  child0->mtu = 1460; /* $$$$ policy */
191 
193  (&child0->connection, tc0->s_index, 1))
194  {
195  error0 = UDP_ERROR_CREATE_SESSION;
196  goto trace0;
197  }
198  s0 = session_get (child0->c_s_index, child0->c_thread_index);
199  s0->session_state = SESSION_STATE_READY;
200  tc0 = &child0->connection;
201 
202  error0 = UDP_ERROR_LISTENER;
203  }
204  else
205  {
206  error0 = UDP_ERROR_NOT_READY;
207  goto trace0;
208  }
209 
210  written0 = session_enqueue_dgram_connection (s0, b0, tc0->proto,
211  1 /* queue evt */ );
212  if (PREDICT_FALSE (written0 < 0))
213  {
214  error0 = UDP_ERROR_FIFO_FULL;
215  goto trace0;
216  }
217 
218  trace0:
219  b0->error = node->errors[error0];
220 
222  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
223  {
224  udp_input_trace_t *t = vlib_add_trace (vm, node, b0,
225  sizeof (*t));
226 
227  t->connection = tc0 ? tc0->c_index : ~0;
228  t->disposition = error0;
229  t->thread_index = my_thread_index;
230  }
231 
232  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
233  to_next, n_left_to_next,
234  bi0, next0);
235  }
236 
237  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
238  }
239 
241  my_thread_index);
242  udp_input_inc_counter (vm, is_ip4, UDP_ERROR_EVENT_FIFO_FULL, errors);
243  return frame->n_vectors;
244 }
245 
248 
249 static uword
251  vlib_frame_t * frame)
252 {
253  return udp46_input_inline (vm, node, frame, 1);
254 }
255 
256 /* *INDENT-OFF* */
258 {
259  .function = udp4_input,
260  .name = "udp4-input",
261  .vector_size = sizeof (u32),
262  .format_trace = format_udp_input_trace,
263  .type = VLIB_NODE_TYPE_INTERNAL,
264  .n_errors = ARRAY_LEN (udp_error_strings),
265  .error_strings = udp_error_strings,
266  .n_next_nodes = UDP_INPUT_N_NEXT,
267  .next_nodes = {
268 #define _(s, n) [UDP_INPUT_NEXT_##s] = n,
270 #undef _
271  },
272 };
273 /* *INDENT-ON* */
274 
275 static uword
277  vlib_frame_t * frame)
278 {
279  return udp46_input_inline (vm, node, frame, 0);
280 }
281 
282 /* *INDENT-OFF* */
284 {
285  .function = udp6_input,
286  .name = "udp6-input",
287  .vector_size = sizeof (u32),
288  .format_trace = format_udp_input_trace,
289  .type = VLIB_NODE_TYPE_INTERNAL,
290  .n_errors = ARRAY_LEN (udp_error_strings),
291  .error_strings = udp_error_strings,
292  .n_next_nodes = UDP_INPUT_N_NEXT,
293  .next_nodes = {
294 #define _(s, n) [UDP_INPUT_NEXT_##s] = n,
296 #undef _
297  },
298 };
299 /* *INDENT-ON* */
300 
301 /*
302  * fd.io coding-style-patch-verification: ON
303  *
304  * Local Variables:
305  * eval: (c-set-style "gnu")
306  * End:
307  */
#define CLIB_UNUSED(x)
Definition: clib.h:79
ip4_address_t src_address
Definition: ip4_packet.h:164
struct _transport_connection transport_connection_t
#define PREDICT_TRUE(x)
Definition: clib.h:106
void ip_set(ip46_address_t *dst, void *src, u8 is_ip4)
Definition: ip.c:76
u32 thread_index
Definition: main.h:173
udp_input_next_t
Definition: udp_input.c:60
struct _vlib_node_registration vlib_node_registration_t
transport_connection_t * session_get_transport(stream_session_t *s)
Definition: session.c:1026
static void session_pool_remove_peeker(u32 thread_index)
Definition: session.h:318
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
ip6_address_t src_address
Definition: ip6_packet.h:341
static void udp_input_inc_counter(vlib_main_t *vm, u8 is_ip4, u8 evt, u8 val)
Definition: udp_input.c:69
vlib_node_registration_t udp6_input_node
(constructor) VLIB_REGISTER_NODE (udp6_input_node)
Definition: udp_input.c:247
#define always_inline
Definition: clib.h:92
ip4_address_t dst_address
Definition: ip4_packet.h:164
static uword udp4_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: udp_input.c:250
struct _stream_session_t stream_session_t
int session_dgram_connect_notify(transport_connection_t *tc, u32 old_thread_index, stream_session_t **new_session)
Move dgram session to the right thread.
Definition: session.c:641
udp_connection_t * udp_connection_alloc(u32 thread_index)
Definition: udp.c:26
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:195
stream_session_t * session_lookup_safe6(u32 fib_index, ip6_address_t *lcl, ip6_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto)
Lookup session with ip6 and transport layer information.
static uword udp46_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u8 is_ip4)
Definition: udp_input.c:81
#define PREDICT_FALSE(x)
Definition: clib.h:105
#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:218
int stream_session_accept(transport_connection_t *tc, u32 listener_index, u8 notify)
Accept a stream session.
Definition: session.c:761
#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 stream_session_t * session_get(u32 si, u32 thread_index)
Definition: session.h:229
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
stream_session_t * session_lookup_safe4(u32 fib_index, ip4_address_t *lcl, ip4_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto)
Lookup session with ip4 and transport layer information.
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
#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
int session_enqueue_dgram_connection(stream_session_t *s, vlib_buffer_t *b, u8 proto, u8 queue_event)
Definition: session.c:359
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
static u8 * format_udp_input_trace(u8 *s, va_list *args)
Definition: udp_input.c:46
static char * udp_error_strings[]
Definition: udp_input.c:31
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:259
static udp_connection_t * udp_conenction_clone_safe(u32 connection_index, u32 thread_index)
Definition: udp.h:210
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
#define foreach_udp_input_next
Definition: udp_input.c:57
vlib_node_registration_t udp4_input_node
(constructor) VLIB_REGISTER_NODE (udp4_input_node)
Definition: udp_input.c:246
transport_connection_t connection
Definition: udp.h:39
transport_connection_t * listen_session_get_transport(stream_session_t *s)
Definition: session.c:1039
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
#define vnet_buffer(b)
Definition: buffer.h:326
int session_manager_flush_enqueue_events(u8 transport_proto, u32 thread_index)
Flushes queue of sessions that are to be notified of new data enqueued events.
Definition: session.c:510
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
static uword udp6_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: udp_input.c:276
u16 flags
Copy of main node flags.
Definition: node.h:450
u32 mtu
must be first
Definition: udp.h:41
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 vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
ip6_address_t dst_address
Definition: ip6_packet.h:341