blob: 523ab029bd4b856197b3ae8b1e49dde56e758f50 [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>
Florin Coras7fb0fe12018-04-09 09:24:52 -070024#include <vnet/ip/ip.h>
25#include <vnet/udp/udp.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050026#include <vnet/udp/udp_packet.h>
Florin Coras7fb0fe12018-04-09 09:24:52 -070027#include <vnet/session/session.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050028
Florin Coras3cbc04b2017-10-02 00:18:51 -070029static char *udp_error_strings[] = {
30#define udp_error(n,s) s,
31#include "udp_error.def"
32#undef udp_error
33};
Dave Barach68b0fb02017-02-28 15:15:56 -050034
35typedef struct
36{
Florin Coras3cbc04b2017-10-02 00:18:51 -070037 u32 connection;
Dave Barach68b0fb02017-02-28 15:15:56 -050038 u32 disposition;
39 u32 thread_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -070040} udp_input_trace_t;
Dave Barach68b0fb02017-02-28 15:15:56 -050041
42/* packet trace format function */
43static u8 *
Florin Coras3cbc04b2017-10-02 00:18:51 -070044format_udp_input_trace (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -050045{
46 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
47 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Coras3cbc04b2017-10-02 00:18:51 -070048 udp_input_trace_t *t = va_arg (*args, udp_input_trace_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -050049
Florin Coras3cbc04b2017-10-02 00:18:51 -070050 s = format (s, "UDP_INPUT: connection %d, disposition %d, thread %d",
51 t->connection, t->disposition, t->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -050052 return s;
53}
54
Florin Coras3cbc04b2017-10-02 00:18:51 -070055#define foreach_udp_input_next \
56 _ (DROP, "error-drop")
57
Dave Barach68b0fb02017-02-28 15:15:56 -050058typedef enum
59{
Florin Coras3cbc04b2017-10-02 00:18:51 -070060#define _(s, n) UDP_INPUT_NEXT_##s,
61 foreach_udp_input_next
Dave Barach68b0fb02017-02-28 15:15:56 -050062#undef _
Florin Coras3cbc04b2017-10-02 00:18:51 -070063 UDP_INPUT_N_NEXT,
64} udp_input_next_t;
Dave Barach68b0fb02017-02-28 15:15:56 -050065
Florin Coras3cbc04b2017-10-02 00:18:51 -070066always_inline void
67udp_input_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
68{
Florin Coras3cbc04b2017-10-02 00:18:51 -070069 if (is_ip4)
70 vlib_node_increment_counter (vm, udp4_input_node.index, evt, val);
71 else
72 vlib_node_increment_counter (vm, udp6_input_node.index, evt, val);
73}
74
Florin Corase759bb52020-04-08 01:55:39 +000075#define udp_store_err_counters(vm, is_ip4, cnts) \
76{ \
77 int i; \
78 for (i = 0; i < UDP_N_ERROR; i++) \
79 if (cnts[i]) \
80 udp_input_inc_counter(vm, is_ip4, i, cnts[i]); \
81}
82
83#define udp_inc_err_counter(cnts, err, val) \
84{ \
85 cnts[err] += val; \
86}
87
88static void
89udp_trace_buffer (vlib_main_t * vm, vlib_node_runtime_t * node,
90 vlib_buffer_t * b, session_t * s, u16 error0)
91{
92 udp_input_trace_t *t;
93
94 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_IS_TRACED)))
95 return;
96
97 t = vlib_add_trace (vm, node, b, sizeof (*t));
98 t->connection = s ? s->connection_index : ~0;
99 t->disposition = error0;
Florin Coras92bbfc22020-04-09 14:20:52 +0000100 t->thread_index = s ? s->thread_index : vm->thread_index;
Florin Corase759bb52020-04-08 01:55:39 +0000101}
102
103static udp_connection_t *
104udp_connection_accept (udp_connection_t * listener, session_dgram_hdr_t * hdr,
105 u32 thread_index)
106{
107 udp_connection_t *uc;
108
109 uc = udp_connection_alloc (thread_index);
110 ip_copy (&uc->c_lcl_ip, &hdr->lcl_ip, hdr->is_ip4);
111 ip_copy (&uc->c_rmt_ip, &hdr->rmt_ip, hdr->is_ip4);
112 uc->c_lcl_port = hdr->lcl_port;
113 uc->c_rmt_port = hdr->rmt_port;
114 uc->c_is_ip4 = hdr->is_ip4;
115 uc->c_fib_index = listener->c_fib_index;
116 uc->mss = listener->mss;
117 uc->flags |= UDP_CONN_F_CONNECTED;
118
119 if (session_dgram_accept (&uc->connection, listener->c_s_index,
120 listener->c_thread_index))
121 {
122 udp_connection_free (uc);
123 return 0;
124 }
125 udp_connection_share_port (clib_net_to_host_u16
126 (uc->c_lcl_port), uc->c_is_ip4);
127 return uc;
128}
129
130static void
131udp_connection_enqueue (udp_connection_t * uc0, session_t * s0,
132 session_dgram_hdr_t * hdr0, u32 thread_index,
133 vlib_buffer_t * b, u8 queue_event, u32 * error0)
134{
135 int wrote0;
136
137 clib_spinlock_lock (&uc0->rx_lock);
138
139 if (svm_fifo_max_enqueue_prod (s0->rx_fifo)
140 < hdr0->data_length + sizeof (session_dgram_hdr_t))
141 {
142 *error0 = UDP_ERROR_FIFO_FULL;
143 goto unlock_rx_lock;
144 }
145
146 /* If session is owned by another thread and rx event needed,
147 * enqueue event now while we still have the peeker lock */
148 if (s0->thread_index != thread_index)
149 {
150 wrote0 = session_enqueue_dgram_connection (s0, hdr0, b,
151 TRANSPORT_PROTO_UDP,
152 /* queue event */ 0);
153 if (queue_event && !svm_fifo_has_event (s0->rx_fifo))
154 session_enqueue_notify (s0);
155 }
156 else
157 {
158 wrote0 = session_enqueue_dgram_connection (s0, hdr0, b,
159 TRANSPORT_PROTO_UDP,
160 queue_event);
161 }
162 ASSERT (wrote0 > 0);
163
164unlock_rx_lock:
165
166 clib_spinlock_unlock (&uc0->rx_lock);
167}
168
169always_inline session_t *
170udp_parse_and_lookup_buffer (vlib_buffer_t * b, session_dgram_hdr_t * hdr,
171 u8 is_ip4)
172{
173 udp_header_t *udp;
174 u32 fib_index;
175 session_t *s;
176
177 /* udp_local hands us a pointer to the udp data */
178 udp = (udp_header_t *) (vlib_buffer_get_current (b) - sizeof (*udp));
179 fib_index = vnet_buffer (b)->ip.fib_index;
180
181 hdr->data_offset = 0;
182 hdr->lcl_port = udp->dst_port;
183 hdr->rmt_port = udp->src_port;
184 hdr->is_ip4 = is_ip4;
185
186 if (is_ip4)
187 {
188 ip4_header_t *ip4;
189
190 /* TODO: must fix once udp_local does ip options correctly */
191 ip4 = (ip4_header_t *) (((u8 *) udp) - sizeof (*ip4));
192 ip_set (&hdr->lcl_ip, &ip4->dst_address, 1);
193 ip_set (&hdr->rmt_ip, &ip4->src_address, 1);
194 hdr->data_length = clib_net_to_host_u16 (ip4->length);
195 hdr->data_length -= sizeof (ip4_header_t) + sizeof (udp_header_t);
196 s = session_lookup_safe4 (fib_index, &ip4->dst_address,
197 &ip4->src_address, udp->dst_port,
198 udp->src_port, TRANSPORT_PROTO_UDP);
199 }
200 else
201 {
202 ip6_header_t *ip60;
203
204 ip60 = (ip6_header_t *) (((u8 *) udp) - sizeof (*ip60));
205 ip_set (&hdr->lcl_ip, &ip60->dst_address, 0);
206 ip_set (&hdr->rmt_ip, &ip60->src_address, 0);
207 hdr->data_length = clib_net_to_host_u16 (ip60->payload_length);
208 hdr->data_length -= sizeof (udp_header_t);
209 s = session_lookup_safe6 (fib_index, &ip60->dst_address,
210 &ip60->src_address, udp->dst_port,
211 udp->src_port, TRANSPORT_PROTO_UDP);
212 }
213
214 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
215 b->current_length = hdr->data_length;
216 else
217 b->total_length_not_including_first_buffer = hdr->data_length
218 - b->current_length;
219
220 return s;
221}
222
Florin Coras3cbc04b2017-10-02 00:18:51 -0700223always_inline uword
224udp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
225 vlib_frame_t * frame, u8 is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -0500226{
Florin Corase759bb52020-04-08 01:55:39 +0000227 u32 n_left_from, *from, errors, *first_buffer;
228 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
229 u16 err_counters[UDP_N_ERROR] = { 0 };
230 u32 thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500231
Aloys Augustin8fadb652019-09-24 18:57:50 +0200232 from = first_buffer = vlib_frame_vector_args (frame);
Dave Barach68b0fb02017-02-28 15:15:56 -0500233 n_left_from = frame->n_vectors;
Florin Corase759bb52020-04-08 01:55:39 +0000234 vlib_get_buffers (vm, from, bufs, n_left_from);
235
236 b = bufs;
Dave Barach68b0fb02017-02-28 15:15:56 -0500237
238 while (n_left_from > 0)
239 {
Aloys Augustin8fadb652019-09-24 18:57:50 +0200240 u32 error0 = UDP_ERROR_ENQUEUED;
Aloys Augustin8fadb652019-09-24 18:57:50 +0200241 session_dgram_hdr_t hdr0;
Florin Corase759bb52020-04-08 01:55:39 +0000242 udp_connection_t *uc0;
243 session_t *s0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500244
Florin Corase759bb52020-04-08 01:55:39 +0000245 s0 = udp_parse_and_lookup_buffer (b[0], &hdr0, is_ip4);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200246 if (PREDICT_FALSE (!s0))
247 {
248 error0 = UDP_ERROR_NO_LISTENER;
Florin Corase759bb52020-04-08 01:55:39 +0000249 goto done;
Aloys Augustin8fadb652019-09-24 18:57:50 +0200250 }
251
Florin Corase759bb52020-04-08 01:55:39 +0000252 /*
253 * If session exists pool peeker lock is taken at this point unless
254 * the session is already on the right thread or is a listener
255 */
256
Aloys Augustin8fadb652019-09-24 18:57:50 +0200257 if (s0->session_state == SESSION_STATE_OPENED)
258 {
Florin Corase759bb52020-04-08 01:55:39 +0000259 u8 queue_event = 1;
260 uc0 = udp_connection_from_transport (session_get_transport (s0));
Aloys Augustin8fadb652019-09-24 18:57:50 +0200261 if (uc0->flags & UDP_CONN_F_CONNECTED)
262 {
Florin Corase759bb52020-04-08 01:55:39 +0000263 if (s0->thread_index != thread_index)
Aloys Augustin8fadb652019-09-24 18:57:50 +0200264 {
265 /*
266 * Clone the transport. It will be cleaned up with the
267 * session once we notify the session layer.
268 */
Florin Corase759bb52020-04-08 01:55:39 +0000269 uc0 = udp_connection_clone_safe (s0->connection_index,
270 s0->thread_index);
271 ASSERT (s0->session_index == uc0->c_s_index);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200272
273 /*
Florin Corase759bb52020-04-08 01:55:39 +0000274 * Drop the peeker lock on pool resize and ask session
275 * layer for a new session.
Aloys Augustin8fadb652019-09-24 18:57:50 +0200276 */
277 session_pool_remove_peeker (s0->thread_index);
Florin Corase759bb52020-04-08 01:55:39 +0000278 session_dgram_connect_notify (&uc0->connection,
Aloys Augustin8fadb652019-09-24 18:57:50 +0200279 s0->thread_index, &s0);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200280 queue_event = 0;
281 }
282 else
283 s0->session_state = SESSION_STATE_READY;
284 }
Florin Corase759bb52020-04-08 01:55:39 +0000285 udp_connection_enqueue (uc0, s0, &hdr0, thread_index, b[0],
286 queue_event, &error0);
287 session_pool_remove_peeker (s0->thread_index);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200288 }
289 else if (s0->session_state == SESSION_STATE_READY)
290 {
Florin Corase759bb52020-04-08 01:55:39 +0000291 uc0 = udp_connection_from_transport (session_get_transport (s0));
292 udp_connection_enqueue (uc0, s0, &hdr0, thread_index, b[0], 1,
293 &error0);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200294 }
295 else if (s0->session_state == SESSION_STATE_LISTENING)
296 {
Florin Corase759bb52020-04-08 01:55:39 +0000297 uc0 = udp_connection_from_transport (session_get_transport (s0));
Aloys Augustin8fadb652019-09-24 18:57:50 +0200298 if (uc0->flags & UDP_CONN_F_CONNECTED)
299 {
Florin Corase759bb52020-04-08 01:55:39 +0000300 uc0 = udp_connection_accept (uc0, &hdr0, thread_index);
301 if (!uc0)
Aloys Augustin8fadb652019-09-24 18:57:50 +0200302 {
303 error0 = UDP_ERROR_CREATE_SESSION;
Florin Corase759bb52020-04-08 01:55:39 +0000304 goto done;
Aloys Augustin8fadb652019-09-24 18:57:50 +0200305 }
Florin Corase759bb52020-04-08 01:55:39 +0000306 s0 = session_get (uc0->c_s_index, uc0->c_thread_index);
307 error0 = UDP_ERROR_ACCEPT;
Aloys Augustin8fadb652019-09-24 18:57:50 +0200308 }
Florin Corase759bb52020-04-08 01:55:39 +0000309 udp_connection_enqueue (uc0, s0, &hdr0, thread_index, b[0], 1,
310 &error0);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200311 }
312 else
313 {
314 error0 = UDP_ERROR_NOT_READY;
Florin Corase759bb52020-04-08 01:55:39 +0000315 session_pool_remove_peeker (s0->thread_index);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200316 }
317
Florin Corase759bb52020-04-08 01:55:39 +0000318 done:
Andreas Schultz063f2b82020-04-16 16:18:57 +0200319 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
320 udp_trace_buffer (vm, node, b[0], s0, error0);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200321
Florin Corase759bb52020-04-08 01:55:39 +0000322 b += 1;
323 n_left_from -= 1;
Florin Corasba78e232020-04-06 21:28:59 +0000324
Florin Corase759bb52020-04-08 01:55:39 +0000325 udp_inc_err_counter (err_counters, error0, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500326 }
327
Aloys Augustin8fadb652019-09-24 18:57:50 +0200328 vlib_buffer_free (vm, first_buffer, frame->n_vectors);
Florin Coras9cbf6812019-08-06 18:28:49 -0700329 errors = session_main_flush_enqueue_events (TRANSPORT_PROTO_UDP,
Florin Corase759bb52020-04-08 01:55:39 +0000330 thread_index);
331 err_counters[UDP_ERROR_MQ_FULL] = errors;
332 udp_store_err_counters (vm, is_ip4, err_counters);
Dave Barach68b0fb02017-02-28 15:15:56 -0500333 return frame->n_vectors;
334}
335
Florin Coras3cbc04b2017-10-02 00:18:51 -0700336static uword
337udp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
338 vlib_frame_t * frame)
Dave Barach68b0fb02017-02-28 15:15:56 -0500339{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700340 return udp46_input_inline (vm, node, frame, 1);
341}
342
343/* *INDENT-OFF* */
344VLIB_REGISTER_NODE (udp4_input_node) =
345{
346 .function = udp4_input,
347 .name = "udp4-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* */
361
362static uword
363udp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
364 vlib_frame_t * frame)
365{
366 return udp46_input_inline (vm, node, frame, 0);
367}
368
369/* *INDENT-OFF* */
370VLIB_REGISTER_NODE (udp6_input_node) =
371{
372 .function = udp6_input,
373 .name = "udp6-input",
374 .vector_size = sizeof (u32),
375 .format_trace = format_udp_input_trace,
376 .type = VLIB_NODE_TYPE_INTERNAL,
377 .n_errors = ARRAY_LEN (udp_error_strings),
378 .error_strings = udp_error_strings,
379 .n_next_nodes = UDP_INPUT_N_NEXT,
380 .next_nodes = {
381#define _(s, n) [UDP_INPUT_NEXT_##s] = n,
382 foreach_udp_input_next
383#undef _
384 },
385};
386/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500387
388/*
389 * fd.io coding-style-patch-verification: ON
390 *
391 * Local Variables:
392 * eval: (c-set-style "gnu")
393 * End:
394 */