FD.io VPP  v18.10-34-gcce845e
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 <vlibmemory/api.h>
17 #include <vlib/vlib.h>
18 
19 #include <vppinfra/hash.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/elog.h>
22 
23 #include <vnet/vnet.h>
24 #include <vnet/pg/pg.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/udp/udp.h>
27 #include <vnet/udp/udp_packet.h>
28 #include <vnet/session/session.h>
29 
30 static char *udp_error_strings[] = {
31 #define udp_error(n,s) s,
32 #include "udp_error.def"
33 #undef udp_error
34 };
35 
36 typedef struct
37 {
42 
43 /* packet trace format function */
44 static u8 *
45 format_udp_input_trace (u8 * s, va_list * args)
46 {
47  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49  udp_input_trace_t *t = va_arg (*args, udp_input_trace_t *);
50 
51  s = format (s, "UDP_INPUT: connection %d, disposition %d, thread %d",
53  return s;
54 }
55 
56 #define foreach_udp_input_next \
57  _ (DROP, "error-drop")
58 
59 typedef enum
60 {
61 #define _(s, n) UDP_INPUT_NEXT_##s,
63 #undef _
66 
67 always_inline void
68 udp_input_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
69 {
70  if (PREDICT_TRUE (!val))
71  return;
72 
73  if (is_ip4)
74  vlib_node_increment_counter (vm, udp4_input_node.index, evt, val);
75  else
76  vlib_node_increment_counter (vm, udp6_input_node.index, evt, val);
77 }
78 
81  vlib_frame_t * frame, u8 is_ip4)
82 {
83  u32 n_left_from, *from, *to_next;
84  u32 next_index, errors;
85  u32 my_thread_index = vm->thread_index;
86 
87  from = vlib_frame_vector_args (frame);
88  n_left_from = frame->n_vectors;
89  next_index = node->cached_next_index;
90 
91  while (n_left_from > 0)
92  {
93  u32 n_left_to_next;
94 
95  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
96 
97  while (n_left_from > 0 && n_left_to_next > 0)
98  {
99  u32 bi0, fib_index0;
100  vlib_buffer_t *b0;
101  u32 next0 = UDP_INPUT_NEXT_DROP;
102  u32 error0 = UDP_ERROR_ENQUEUED;
103  udp_header_t *udp0;
104  ip4_header_t *ip40;
105  ip6_header_t *ip60;
106  u8 *data0;
107  stream_session_t *s0;
108  udp_connection_t *uc0, *child0, *new_uc0;
110  int wrote0;
111  void *rmt_addr, *lcl_addr;
112  session_dgram_hdr_t hdr0;
113 
114  /* speculatively enqueue b0 to the current next frame */
115  bi0 = from[0];
116  to_next[0] = bi0;
117  from += 1;
118  to_next += 1;
119  n_left_from -= 1;
120  n_left_to_next -= 1;
121 
122  b0 = vlib_get_buffer (vm, bi0);
123 
124  /* udp_local hands us a pointer to the udp data */
125  data0 = vlib_buffer_get_current (b0);
126  udp0 = (udp_header_t *) (data0 - sizeof (*udp0));
127  fib_index0 = vnet_buffer (b0)->ip.fib_index;
128 
129  if (is_ip4)
130  {
131  /* TODO: must fix once udp_local does ip options correctly */
132  ip40 = (ip4_header_t *) (((u8 *) udp0) - sizeof (*ip40));
133  s0 = session_lookup_safe4 (fib_index0, &ip40->dst_address,
134  &ip40->src_address, udp0->dst_port,
136  lcl_addr = &ip40->dst_address;
137  rmt_addr = &ip40->src_address;
138 
139  }
140  else
141  {
142  ip60 = (ip6_header_t *) (((u8 *) udp0) - sizeof (*ip60));
143  s0 = session_lookup_safe6 (fib_index0, &ip60->dst_address,
144  &ip60->src_address, udp0->dst_port,
146  lcl_addr = &ip60->dst_address;
147  rmt_addr = &ip60->src_address;
148  }
149 
150  if (PREDICT_FALSE (!s0))
151  {
152  error0 = UDP_ERROR_NO_LISTENER;
153  goto trace0;
154  }
155 
156  if (s0->session_state == SESSION_STATE_OPENED)
157  {
158  /* TODO optimization: move cl session to right thread
159  * However, since such a move would affect the session handle,
160  * which we pass 'raw' to the app, we'd also have notify the
161  * app of the change or change the way we pass handles to apps.
162  */
163  tc0 = session_get_transport (s0);
165  if (uc0->is_connected)
166  {
167  /*
168  * Clone the transport. It will be cleaned up with the
169  * session once we notify the session layer.
170  */
171  new_uc0 = udp_connection_clone_safe (s0->connection_index,
172  s0->thread_index);
173  ASSERT (s0->session_index == new_uc0->c_s_index);
174 
175  /*
176  * Drop the 'lock' on pool resize
177  */
178  session_pool_remove_peeker (s0->thread_index);
180  s0->thread_index, &s0);
181  tc0 = &new_uc0->connection;
182  }
183  }
184  else if (s0->session_state == SESSION_STATE_READY)
185  {
186  tc0 = session_get_transport (s0);
188  }
189  else if (s0->session_state == SESSION_STATE_LISTENING)
190  {
191  tc0 = listen_session_get_transport (s0);
193  if (uc0->is_connected)
194  {
195  child0 = udp_connection_alloc (my_thread_index);
196  if (is_ip4)
197  {
198  ip_set (&child0->c_lcl_ip, &ip40->dst_address, 1);
199  ip_set (&child0->c_rmt_ip, &ip40->src_address, 1);
200  }
201  else
202  {
203  ip_set (&child0->c_lcl_ip, &ip60->dst_address, 0);
204  ip_set (&child0->c_rmt_ip, &ip60->src_address, 0);
205  }
206  child0->c_lcl_port = udp0->dst_port;
207  child0->c_rmt_port = udp0->src_port;
208  child0->c_is_ip4 = is_ip4;
209 
210  if (stream_session_accept (&child0->connection,
211  tc0->s_index, 1))
212  {
213  error0 = UDP_ERROR_CREATE_SESSION;
214  goto trace0;
215  }
216  s0 =
217  session_get (child0->c_s_index, child0->c_thread_index);
218  s0->session_state = SESSION_STATE_READY;
219  tc0 = &child0->connection;
221  error0 = UDP_ERROR_LISTENER;
222  }
223  }
224  else
225  {
226  error0 = UDP_ERROR_NOT_READY;
227  goto trace0;
228  }
229 
230  if (!uc0->is_connected)
231  {
232  if (svm_fifo_max_enqueue (s0->server_rx_fifo)
233  < b0->current_length + sizeof (session_dgram_hdr_t))
234  {
235  error0 = UDP_ERROR_FIFO_FULL;
236  goto trace0;
237  }
238  hdr0.data_length = b0->current_length;
239  hdr0.data_offset = 0;
240  ip_set (&hdr0.lcl_ip, lcl_addr, is_ip4);
241  ip_set (&hdr0.rmt_ip, rmt_addr, is_ip4);
242  hdr0.lcl_port = udp0->dst_port;
243  hdr0.rmt_port = udp0->src_port;
244  hdr0.is_ip4 = is_ip4;
245 
246  clib_spinlock_lock (&uc0->rx_lock);
247  wrote0 = session_enqueue_dgram_connection (s0, &hdr0, b0,
249  1 /* queue evt */ );
251  ASSERT (wrote0 > 0);
252 
253  if (s0->session_state != SESSION_STATE_LISTENING)
254  session_pool_remove_peeker (s0->thread_index);
255  }
256  else
257  {
258  if (svm_fifo_max_enqueue (s0->server_rx_fifo)
259  < b0->current_length)
260  {
261  error0 = UDP_ERROR_FIFO_FULL;
262  goto trace0;
263  }
264  wrote0 = session_enqueue_stream_connection (tc0, b0, 0,
265  1 /* queue evt */ ,
266  1 /* in order */ );
267  ASSERT (wrote0 > 0);
268  }
269 
270  trace0:
271 
272  b0->error = node->errors[error0];
273 
275  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
276  {
277  udp_input_trace_t *t = vlib_add_trace (vm, node, b0,
278  sizeof (*t));
279 
280  t->connection = s0 ? s0->connection_index : ~0;
281  t->disposition = error0;
282  t->thread_index = my_thread_index;
283  }
284 
285  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
286  to_next, n_left_to_next,
287  bi0, next0);
288  }
289 
290  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
291  }
292 
294  udp_input_inc_counter (vm, is_ip4, UDP_ERROR_EVENT_FIFO_FULL, errors);
295  return frame->n_vectors;
296 }
297 
298 
299 static uword
301  vlib_frame_t * frame)
302 {
303  return udp46_input_inline (vm, node, frame, 1);
304 }
305 
306 /* *INDENT-OFF* */
308 {
309  .function = udp4_input,
310  .name = "udp4-input",
311  .vector_size = sizeof (u32),
312  .format_trace = format_udp_input_trace,
313  .type = VLIB_NODE_TYPE_INTERNAL,
314  .n_errors = ARRAY_LEN (udp_error_strings),
315  .error_strings = udp_error_strings,
316  .n_next_nodes = UDP_INPUT_N_NEXT,
317  .next_nodes = {
318 #define _(s, n) [UDP_INPUT_NEXT_##s] = n,
320 #undef _
321  },
322 };
323 /* *INDENT-ON* */
324 
325 static uword
327  vlib_frame_t * frame)
328 {
329  return udp46_input_inline (vm, node, frame, 0);
330 }
331 
332 /* *INDENT-OFF* */
334 {
335  .function = udp6_input,
336  .name = "udp6-input",
337  .vector_size = sizeof (u32),
338  .format_trace = format_udp_input_trace,
339  .type = VLIB_NODE_TYPE_INTERNAL,
340  .n_errors = ARRAY_LEN (udp_error_strings),
341  .error_strings = udp_error_strings,
342  .n_next_nodes = UDP_INPUT_N_NEXT,
343  .next_nodes = {
344 #define _(s, n) [UDP_INPUT_NEXT_##s] = n,
346 #undef _
347  },
348 };
349 /* *INDENT-ON* */
350 
351 /*
352  * fd.io coding-style-patch-verification: ON
353  *
354  * Local Variables:
355  * eval: (c-set-style "gnu")
356  * End:
357  */
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:89
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:74
#define CLIB_UNUSED(x)
Definition: clib.h:81
ip4_address_t src_address
Definition: ip4_packet.h:169
struct _transport_connection transport_connection_t
#define PREDICT_TRUE(x)
Definition: clib.h:108
static udp_connection_t * udp_get_connection_from_transport(transport_connection_t *tc)
Definition: udp.h:170
void ip_set(ip46_address_t *dst, void *src, u8 is_ip4)
Definition: ip.c:90
u32 thread_index
Definition: main.h:179
udp_input_next_t
Definition: udp_input.c:59
transport_connection_t * session_get_transport(stream_session_t *s)
Definition: session.c:1289
static u32 svm_fifo_max_enqueue(svm_fifo_t *f)
Definition: svm_fifo.h:132
static void session_pool_remove_peeker(u32 thread_index)
Definition: session.h:445
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
int session_enqueue_stream_connection(transport_connection_t *tc, vlib_buffer_t *b, u32 offset, u8 queue_event, u8 is_in_order)
Definition: session.c:355
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:472
ip6_address_t src_address
Definition: ip6_packet.h:378
unsigned char u8
Definition: types.h:56
static void udp_input_inc_counter(vlib_main_t *vm, u8 is_ip4, u8 evt, u8 val)
Definition: udp_input.c:68
vlib_node_registration_t udp6_input_node
(constructor) VLIB_REGISTER_NODE (udp6_input_node)
Definition: udp_input.c:333
#define always_inline
Definition: clib.h:94
ip4_address_t dst_address
Definition: ip4_packet.h:169
static uword udp4_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: udp_input.c:300
int session_enqueue_dgram_connection(stream_session_t *s, session_dgram_hdr_t *hdr, vlib_buffer_t *b, u8 proto, u8 queue_event)
Definition: session.c:411
unsigned int u32
Definition: types.h:88
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:698
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
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:205
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:80
#define PREDICT_FALSE(x)
Definition: clib.h:107
#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:855
#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:138
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1176
static stream_session_t * session_get(u32 si, u32 thread_index)
Definition: session.h:314
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:155
u16 n_vectors
Definition: node.h:401
vlib_main_t * vm
Definition: buffer.c:294
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 ARRAY_LEN(x)
Definition: clib.h:61
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:455
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:513
#define ASSERT(truth)
static u8 * format_udp_input_trace(u8 *s, va_list *args)
Definition: udp_input.c:45
static char * udp_error_strings[]
Definition: udp_input.c:30
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:57
#define foreach_udp_input_next
Definition: udp_input.c:56
vlib_node_registration_t udp4_input_node
(constructor) VLIB_REGISTER_NODE (udp4_input_node)
Definition: udp_input.c:307
transport_connection_t connection
must be first
Definition: udp.h:41
clib_spinlock_t rx_lock
rx fifo lock
Definition: udp.h:42
transport_connection_t * listen_session_get_transport(stream_session_t *s)
Definition: session.c:1302
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
int session_manager_flush_all_enqueue_events(u8 transport_proto)
Definition: session.c:569
#define vnet_buffer(b)
Definition: buffer.h:344
static uword udp6_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: udp_input.c:326
u16 flags
Copy of main node flags.
Definition: node.h:507
static udp_connection_t * udp_connection_clone_safe(u32 connection_index, u32 thread_index)
Definition: udp.h:215
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:310
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:116
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:58
u8 is_connected
connected mode
Definition: udp.h:43
ip6_address_t dst_address
Definition: ip6_packet.h:378