blob: e5a82429a27ac5431d7aac1905e98edc0224a447 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Corasc5df8c72019-04-08 07:42:30 -07002 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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
Florin Coras7fb0fe12018-04-09 09:24:52 -070016#include <vlibmemory/api.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050017#include <vlib/vlib.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050018
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vppinfra/hash.h>
20#include <vppinfra/error.h>
21#include <vppinfra/elog.h>
22
Florin Coras7fb0fe12018-04-09 09:24:52 -070023#include <vnet/vnet.h>
24#include <vnet/pg/pg.h>
25#include <vnet/ip/ip.h>
26#include <vnet/udp/udp.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050027#include <vnet/udp/udp_packet.h>
Florin Coras7fb0fe12018-04-09 09:24:52 -070028#include <vnet/session/session.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050029
Florin Coras3cbc04b2017-10-02 00:18:51 -070030static char *udp_error_strings[] = {
31#define udp_error(n,s) s,
32#include "udp_error.def"
33#undef udp_error
34};
Dave Barach68b0fb02017-02-28 15:15:56 -050035
36typedef struct
37{
Florin Coras3cbc04b2017-10-02 00:18:51 -070038 u32 connection;
Dave Barach68b0fb02017-02-28 15:15:56 -050039 u32 disposition;
40 u32 thread_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -070041} udp_input_trace_t;
Dave Barach68b0fb02017-02-28 15:15:56 -050042
43/* packet trace format function */
44static u8 *
Florin Coras3cbc04b2017-10-02 00:18:51 -070045format_udp_input_trace (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -050046{
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 *);
Florin Coras3cbc04b2017-10-02 00:18:51 -070049 udp_input_trace_t *t = va_arg (*args, udp_input_trace_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -050050
Florin Coras3cbc04b2017-10-02 00:18:51 -070051 s = format (s, "UDP_INPUT: connection %d, disposition %d, thread %d",
52 t->connection, t->disposition, t->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -050053 return s;
54}
55
Florin Coras3cbc04b2017-10-02 00:18:51 -070056#define foreach_udp_input_next \
57 _ (DROP, "error-drop")
58
Dave Barach68b0fb02017-02-28 15:15:56 -050059typedef enum
60{
Florin Coras3cbc04b2017-10-02 00:18:51 -070061#define _(s, n) UDP_INPUT_NEXT_##s,
62 foreach_udp_input_next
Dave Barach68b0fb02017-02-28 15:15:56 -050063#undef _
Florin Coras3cbc04b2017-10-02 00:18:51 -070064 UDP_INPUT_N_NEXT,
65} udp_input_next_t;
Dave Barach68b0fb02017-02-28 15:15:56 -050066
Florin Coras3cbc04b2017-10-02 00:18:51 -070067always_inline void
68udp_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
79always_inline uword
80udp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
81 vlib_frame_t * frame, u8 is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -050082{
83 u32 n_left_from, *from, *to_next;
Florin Coras3cbc04b2017-10-02 00:18:51 -070084 u32 next_index, errors;
Damjan Marion586afd72017-04-05 19:18:20 +020085 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050086
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 {
Florin Coras3cbc04b2017-10-02 00:18:51 -070099 u32 bi0, fib_index0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500100 vlib_buffer_t *b0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700101 u32 next0 = UDP_INPUT_NEXT_DROP;
102 u32 error0 = UDP_ERROR_ENQUEUED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500103 udp_header_t *udp0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700104 ip4_header_t *ip40;
105 ip6_header_t *ip60;
Dave Barach68b0fb02017-02-28 15:15:56 -0500106 u8 *data0;
Florin Coras288eaab2019-02-03 15:26:14 -0800107 session_t *s0;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700108 udp_connection_t *uc0, *child0, *new_uc0;
109 transport_connection_t *tc0;
110 int wrote0;
111 void *rmt_addr, *lcl_addr;
112 session_dgram_hdr_t hdr0;
Aloys Augustinb3392332019-08-06 16:09:01 +0200113 u8 queue_event = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500114
115 /* speculatively enqueue b0 to the current next frame */
116 bi0 = from[0];
117 to_next[0] = bi0;
118 from += 1;
119 to_next += 1;
120 n_left_from -= 1;
121 n_left_to_next -= 1;
122
123 b0 = vlib_get_buffer (vm, bi0);
124
125 /* udp_local hands us a pointer to the udp data */
Dave Barach68b0fb02017-02-28 15:15:56 -0500126 data0 = vlib_buffer_get_current (b0);
127 udp0 = (udp_header_t *) (data0 - sizeof (*udp0));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700128 fib_index0 = vnet_buffer (b0)->ip.fib_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500129
Florin Coras3cbc04b2017-10-02 00:18:51 -0700130 if (is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -0500131 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700132 /* TODO: must fix once udp_local does ip options correctly */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700133 ip40 = (ip4_header_t *) (((u8 *) udp0) - sizeof (*ip40));
134 s0 = session_lookup_safe4 (fib_index0, &ip40->dst_address,
135 &ip40->src_address, udp0->dst_port,
136 udp0->src_port, TRANSPORT_PROTO_UDP);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700137 lcl_addr = &ip40->dst_address;
138 rmt_addr = &ip40->src_address;
139
Dave Barach68b0fb02017-02-28 15:15:56 -0500140 }
141 else
142 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700143 ip60 = (ip6_header_t *) (((u8 *) udp0) - sizeof (*ip60));
144 s0 = session_lookup_safe6 (fib_index0, &ip60->dst_address,
145 &ip60->src_address, udp0->dst_port,
146 udp0->src_port, TRANSPORT_PROTO_UDP);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700147 lcl_addr = &ip60->dst_address;
148 rmt_addr = &ip60->src_address;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700149 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500150
Florin Coras7fb0fe12018-04-09 09:24:52 -0700151 if (PREDICT_FALSE (!s0))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700152 {
153 error0 = UDP_ERROR_NO_LISTENER;
154 goto trace0;
155 }
156
Florin Coras7fb0fe12018-04-09 09:24:52 -0700157 if (s0->session_state == SESSION_STATE_OPENED)
158 {
159 /* TODO optimization: move cl session to right thread
160 * However, since such a move would affect the session handle,
161 * which we pass 'raw' to the app, we'd also have notify the
162 * app of the change or change the way we pass handles to apps.
163 */
164 tc0 = session_get_transport (s0);
165 uc0 = udp_get_connection_from_transport (tc0);
Florin Corasc7542392019-07-22 08:08:43 -0700166 if (uc0->flags & UDP_CONN_F_CONNECTED)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700167 {
Nathan Skrzypczakf8475a72019-08-05 14:20:54 +0200168 if (s0->thread_index != vlib_get_thread_index ())
169 {
170 /*
171 * Clone the transport. It will be cleaned up with the
172 * session once we notify the session layer.
173 */
174 new_uc0 =
175 udp_connection_clone_safe (s0->connection_index,
176 s0->thread_index);
177 ASSERT (s0->session_index == new_uc0->c_s_index);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700178
Nathan Skrzypczakf8475a72019-08-05 14:20:54 +0200179 /*
180 * Drop the 'lock' on pool resize
181 */
182 session_pool_remove_peeker (s0->thread_index);
183 session_dgram_connect_notify (&new_uc0->connection,
184 s0->thread_index, &s0);
185 tc0 = &new_uc0->connection;
Aloys Augustinb3392332019-08-06 16:09:01 +0200186 uc0 = new_uc0;
187 queue_event = 0;
Nathan Skrzypczakf8475a72019-08-05 14:20:54 +0200188 }
189 else
190 s0->session_state = SESSION_STATE_READY;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700191 }
192 }
193 else if (s0->session_state == SESSION_STATE_READY)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700194 {
195 tc0 = session_get_transport (s0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700196 uc0 = udp_get_connection_from_transport (tc0);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700197 }
198 else if (s0->session_state == SESSION_STATE_LISTENING)
199 {
200 tc0 = listen_session_get_transport (s0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700201 uc0 = udp_get_connection_from_transport (tc0);
Florin Corasc7542392019-07-22 08:08:43 -0700202 if (uc0->flags & UDP_CONN_F_CONNECTED)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700203 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700204 child0 = udp_connection_alloc (my_thread_index);
205 if (is_ip4)
206 {
207 ip_set (&child0->c_lcl_ip, &ip40->dst_address, 1);
208 ip_set (&child0->c_rmt_ip, &ip40->src_address, 1);
209 }
210 else
211 {
212 ip_set (&child0->c_lcl_ip, &ip60->dst_address, 0);
213 ip_set (&child0->c_rmt_ip, &ip60->src_address, 0);
214 }
215 child0->c_lcl_port = udp0->dst_port;
216 child0->c_rmt_port = udp0->src_port;
217 child0->c_is_ip4 = is_ip4;
Nathan Skrzypczak14e2e2a2019-05-03 13:54:44 +0200218 child0->c_fib_index = tc0->fib_index;
Florin Corasc7542392019-07-22 08:08:43 -0700219 child0->flags |= UDP_CONN_F_CONNECTED;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700220
Florin Corasc9940fc2019-02-05 20:55:11 -0800221 if (session_stream_accept (&child0->connection,
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200222 tc0->s_index, tc0->thread_index,
223 1))
Florin Coras7fb0fe12018-04-09 09:24:52 -0700224 {
225 error0 = UDP_ERROR_CREATE_SESSION;
226 goto trace0;
227 }
Florin Coras9cbf6812019-08-06 18:28:49 -0700228 s0 = session_get (child0->c_s_index,
229 child0->c_thread_index);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700230 s0->session_state = SESSION_STATE_READY;
231 tc0 = &child0->connection;
232 uc0 = udp_get_connection_from_transport (tc0);
233 error0 = UDP_ERROR_LISTENER;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700234 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700235 }
236 else
237 {
238 error0 = UDP_ERROR_NOT_READY;
239 goto trace0;
240 }
241
Florin Coras7fb0fe12018-04-09 09:24:52 -0700242
Aloys Augustinde38b382019-04-23 17:18:38 +0200243 if (svm_fifo_max_enqueue_prod (s0->rx_fifo)
244 < b0->current_length + sizeof (session_dgram_hdr_t))
Florin Coras7fb0fe12018-04-09 09:24:52 -0700245 {
Aloys Augustinde38b382019-04-23 17:18:38 +0200246 error0 = UDP_ERROR_FIFO_FULL;
247 goto trace0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500248 }
Aloys Augustinde38b382019-04-23 17:18:38 +0200249 hdr0.data_length = b0->current_length;
250 hdr0.data_offset = 0;
251 ip_set (&hdr0.lcl_ip, lcl_addr, is_ip4);
252 ip_set (&hdr0.rmt_ip, rmt_addr, is_ip4);
253 hdr0.lcl_port = udp0->dst_port;
254 hdr0.rmt_port = udp0->src_port;
255 hdr0.is_ip4 = is_ip4;
256
257 clib_spinlock_lock (&uc0->rx_lock);
Florin Coras9cbf6812019-08-06 18:28:49 -0700258 /* If session is owned by another thread and rx event needed,
259 * enqueue event now while we still have the peeker lock */
260 if (s0->thread_index != my_thread_index)
261 {
262 wrote0 = session_enqueue_dgram_connection (s0, &hdr0, b0,
263 TRANSPORT_PROTO_UDP,
264 /* queue event */ 0);
265 if (queue_event && !svm_fifo_has_event (s0->rx_fifo))
266 session_enqueue_notify (s0);
267 }
268 else
269 {
270 wrote0 = session_enqueue_dgram_connection (s0, &hdr0, b0,
271 TRANSPORT_PROTO_UDP,
272 queue_event);
273 }
Aloys Augustinde38b382019-04-23 17:18:38 +0200274 clib_spinlock_unlock (&uc0->rx_lock);
275 ASSERT (wrote0 > 0);
276
277 if (s0->session_state != SESSION_STATE_LISTENING)
278 session_pool_remove_peeker (s0->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500279
280 trace0:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700281
Dave Barach68b0fb02017-02-28 15:15:56 -0500282 b0->error = node->errors[error0];
283
284 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
285 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
286 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700287 udp_input_trace_t *t = vlib_add_trace (vm, node, b0,
288 sizeof (*t));
Dave Barach68b0fb02017-02-28 15:15:56 -0500289
Florin Coras7fb0fe12018-04-09 09:24:52 -0700290 t->connection = s0 ? s0->connection_index : ~0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500291 t->disposition = error0;
292 t->thread_index = my_thread_index;
293 }
294
Dave Barach68b0fb02017-02-28 15:15:56 -0500295 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
296 to_next, n_left_to_next,
297 bi0, next0);
298 }
299
300 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
301 }
302
Florin Coras9cbf6812019-08-06 18:28:49 -0700303 errors = session_main_flush_enqueue_events (TRANSPORT_PROTO_UDP,
304 my_thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700305 udp_input_inc_counter (vm, is_ip4, UDP_ERROR_EVENT_FIFO_FULL, errors);
Dave Barach68b0fb02017-02-28 15:15:56 -0500306 return frame->n_vectors;
307}
308
Florin Coras3cbc04b2017-10-02 00:18:51 -0700309
310static uword
311udp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
312 vlib_frame_t * frame)
Dave Barach68b0fb02017-02-28 15:15:56 -0500313{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700314 return udp46_input_inline (vm, node, frame, 1);
315}
316
317/* *INDENT-OFF* */
318VLIB_REGISTER_NODE (udp4_input_node) =
319{
320 .function = udp4_input,
321 .name = "udp4-input",
322 .vector_size = sizeof (u32),
323 .format_trace = format_udp_input_trace,
324 .type = VLIB_NODE_TYPE_INTERNAL,
325 .n_errors = ARRAY_LEN (udp_error_strings),
326 .error_strings = udp_error_strings,
327 .n_next_nodes = UDP_INPUT_N_NEXT,
328 .next_nodes = {
329#define _(s, n) [UDP_INPUT_NEXT_##s] = n,
330 foreach_udp_input_next
331#undef _
332 },
333};
334/* *INDENT-ON* */
335
336static uword
337udp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
338 vlib_frame_t * frame)
339{
340 return udp46_input_inline (vm, node, frame, 0);
341}
342
343/* *INDENT-OFF* */
344VLIB_REGISTER_NODE (udp6_input_node) =
345{
346 .function = udp6_input,
347 .name = "udp6-input",
348 .vector_size = sizeof (u32),
349 .format_trace = format_udp_input_trace,
350 .type = VLIB_NODE_TYPE_INTERNAL,
351 .n_errors = ARRAY_LEN (udp_error_strings),
352 .error_strings = udp_error_strings,
353 .n_next_nodes = UDP_INPUT_N_NEXT,
354 .next_nodes = {
355#define _(s, n) [UDP_INPUT_NEXT_##s] = n,
356 foreach_udp_input_next
357#undef _
358 },
359};
360/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500361
362/*
363 * fd.io coding-style-patch-verification: ON
364 *
365 * Local Variables:
366 * eval: (c-set-style "gnu")
367 * End:
368 */