blob: 296a874523d72cf80804866a4eea622922ea1e41 [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{
Florin Coras3cbc04b2017-10-02 00:18:51 -070070 if (is_ip4)
71 vlib_node_increment_counter (vm, udp4_input_node.index, evt, val);
72 else
73 vlib_node_increment_counter (vm, udp6_input_node.index, evt, val);
74}
75
Florin Corase759bb52020-04-08 01:55:39 +000076#define udp_store_err_counters(vm, is_ip4, cnts) \
77{ \
78 int i; \
79 for (i = 0; i < UDP_N_ERROR; i++) \
80 if (cnts[i]) \
81 udp_input_inc_counter(vm, is_ip4, i, cnts[i]); \
82}
83
84#define udp_inc_err_counter(cnts, err, val) \
85{ \
86 cnts[err] += val; \
87}
88
89static void
90udp_trace_buffer (vlib_main_t * vm, vlib_node_runtime_t * node,
91 vlib_buffer_t * b, session_t * s, u16 error0)
92{
93 udp_input_trace_t *t;
94
95 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_IS_TRACED)))
96 return;
97
98 t = vlib_add_trace (vm, node, b, sizeof (*t));
99 t->connection = s ? s->connection_index : ~0;
100 t->disposition = error0;
Florin Coras92bbfc22020-04-09 14:20:52 +0000101 t->thread_index = s ? s->thread_index : vm->thread_index;
Florin Corase759bb52020-04-08 01:55:39 +0000102}
103
104static udp_connection_t *
105udp_connection_accept (udp_connection_t * listener, session_dgram_hdr_t * hdr,
106 u32 thread_index)
107{
108 udp_connection_t *uc;
109
110 uc = udp_connection_alloc (thread_index);
111 ip_copy (&uc->c_lcl_ip, &hdr->lcl_ip, hdr->is_ip4);
112 ip_copy (&uc->c_rmt_ip, &hdr->rmt_ip, hdr->is_ip4);
113 uc->c_lcl_port = hdr->lcl_port;
114 uc->c_rmt_port = hdr->rmt_port;
115 uc->c_is_ip4 = hdr->is_ip4;
116 uc->c_fib_index = listener->c_fib_index;
117 uc->mss = listener->mss;
118 uc->flags |= UDP_CONN_F_CONNECTED;
119
120 if (session_dgram_accept (&uc->connection, listener->c_s_index,
121 listener->c_thread_index))
122 {
123 udp_connection_free (uc);
124 return 0;
125 }
126 udp_connection_share_port (clib_net_to_host_u16
127 (uc->c_lcl_port), uc->c_is_ip4);
128 return uc;
129}
130
131static void
132udp_connection_enqueue (udp_connection_t * uc0, session_t * s0,
133 session_dgram_hdr_t * hdr0, u32 thread_index,
134 vlib_buffer_t * b, u8 queue_event, u32 * error0)
135{
136 int wrote0;
137
138 clib_spinlock_lock (&uc0->rx_lock);
139
140 if (svm_fifo_max_enqueue_prod (s0->rx_fifo)
141 < hdr0->data_length + sizeof (session_dgram_hdr_t))
142 {
143 *error0 = UDP_ERROR_FIFO_FULL;
144 goto unlock_rx_lock;
145 }
146
147 /* If session is owned by another thread and rx event needed,
148 * enqueue event now while we still have the peeker lock */
149 if (s0->thread_index != thread_index)
150 {
151 wrote0 = session_enqueue_dgram_connection (s0, hdr0, b,
152 TRANSPORT_PROTO_UDP,
153 /* queue event */ 0);
154 if (queue_event && !svm_fifo_has_event (s0->rx_fifo))
155 session_enqueue_notify (s0);
156 }
157 else
158 {
159 wrote0 = session_enqueue_dgram_connection (s0, hdr0, b,
160 TRANSPORT_PROTO_UDP,
161 queue_event);
162 }
163 ASSERT (wrote0 > 0);
164
165unlock_rx_lock:
166
167 clib_spinlock_unlock (&uc0->rx_lock);
168}
169
170always_inline session_t *
171udp_parse_and_lookup_buffer (vlib_buffer_t * b, session_dgram_hdr_t * hdr,
172 u8 is_ip4)
173{
174 udp_header_t *udp;
175 u32 fib_index;
176 session_t *s;
177
178 /* udp_local hands us a pointer to the udp data */
179 udp = (udp_header_t *) (vlib_buffer_get_current (b) - sizeof (*udp));
180 fib_index = vnet_buffer (b)->ip.fib_index;
181
182 hdr->data_offset = 0;
183 hdr->lcl_port = udp->dst_port;
184 hdr->rmt_port = udp->src_port;
185 hdr->is_ip4 = is_ip4;
186
187 if (is_ip4)
188 {
189 ip4_header_t *ip4;
190
191 /* TODO: must fix once udp_local does ip options correctly */
192 ip4 = (ip4_header_t *) (((u8 *) udp) - sizeof (*ip4));
193 ip_set (&hdr->lcl_ip, &ip4->dst_address, 1);
194 ip_set (&hdr->rmt_ip, &ip4->src_address, 1);
195 hdr->data_length = clib_net_to_host_u16 (ip4->length);
196 hdr->data_length -= sizeof (ip4_header_t) + sizeof (udp_header_t);
197 s = session_lookup_safe4 (fib_index, &ip4->dst_address,
198 &ip4->src_address, udp->dst_port,
199 udp->src_port, TRANSPORT_PROTO_UDP);
200 }
201 else
202 {
203 ip6_header_t *ip60;
204
205 ip60 = (ip6_header_t *) (((u8 *) udp) - sizeof (*ip60));
206 ip_set (&hdr->lcl_ip, &ip60->dst_address, 0);
207 ip_set (&hdr->rmt_ip, &ip60->src_address, 0);
208 hdr->data_length = clib_net_to_host_u16 (ip60->payload_length);
209 hdr->data_length -= sizeof (udp_header_t);
210 s = session_lookup_safe6 (fib_index, &ip60->dst_address,
211 &ip60->src_address, udp->dst_port,
212 udp->src_port, TRANSPORT_PROTO_UDP);
213 }
214
215 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
216 b->current_length = hdr->data_length;
217 else
218 b->total_length_not_including_first_buffer = hdr->data_length
219 - b->current_length;
220
221 return s;
222}
223
Florin Coras3cbc04b2017-10-02 00:18:51 -0700224always_inline uword
225udp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
226 vlib_frame_t * frame, u8 is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -0500227{
Florin Corase759bb52020-04-08 01:55:39 +0000228 u32 n_left_from, *from, errors, *first_buffer;
229 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
230 u16 err_counters[UDP_N_ERROR] = { 0 };
231 u32 thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500232
Aloys Augustin8fadb652019-09-24 18:57:50 +0200233 from = first_buffer = vlib_frame_vector_args (frame);
Dave Barach68b0fb02017-02-28 15:15:56 -0500234 n_left_from = frame->n_vectors;
Florin Corase759bb52020-04-08 01:55:39 +0000235 vlib_get_buffers (vm, from, bufs, n_left_from);
236
237 b = bufs;
Dave Barach68b0fb02017-02-28 15:15:56 -0500238
239 while (n_left_from > 0)
240 {
Aloys Augustin8fadb652019-09-24 18:57:50 +0200241 u32 error0 = UDP_ERROR_ENQUEUED;
Aloys Augustin8fadb652019-09-24 18:57:50 +0200242 session_dgram_hdr_t hdr0;
Florin Corase759bb52020-04-08 01:55:39 +0000243 udp_connection_t *uc0;
244 session_t *s0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500245
Florin Corase759bb52020-04-08 01:55:39 +0000246 s0 = udp_parse_and_lookup_buffer (b[0], &hdr0, is_ip4);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200247 if (PREDICT_FALSE (!s0))
248 {
249 error0 = UDP_ERROR_NO_LISTENER;
Florin Corase759bb52020-04-08 01:55:39 +0000250 goto done;
Aloys Augustin8fadb652019-09-24 18:57:50 +0200251 }
252
Florin Corase759bb52020-04-08 01:55:39 +0000253 /*
254 * If session exists pool peeker lock is taken at this point unless
255 * the session is already on the right thread or is a listener
256 */
257
Aloys Augustin8fadb652019-09-24 18:57:50 +0200258 if (s0->session_state == SESSION_STATE_OPENED)
259 {
Florin Corase759bb52020-04-08 01:55:39 +0000260 u8 queue_event = 1;
261 uc0 = udp_connection_from_transport (session_get_transport (s0));
Aloys Augustin8fadb652019-09-24 18:57:50 +0200262 if (uc0->flags & UDP_CONN_F_CONNECTED)
263 {
Florin Corase759bb52020-04-08 01:55:39 +0000264 if (s0->thread_index != thread_index)
Aloys Augustin8fadb652019-09-24 18:57:50 +0200265 {
266 /*
267 * Clone the transport. It will be cleaned up with the
268 * session once we notify the session layer.
269 */
Florin Corase759bb52020-04-08 01:55:39 +0000270 uc0 = udp_connection_clone_safe (s0->connection_index,
271 s0->thread_index);
272 ASSERT (s0->session_index == uc0->c_s_index);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200273
274 /*
Florin Corase759bb52020-04-08 01:55:39 +0000275 * Drop the peeker lock on pool resize and ask session
276 * layer for a new session.
Aloys Augustin8fadb652019-09-24 18:57:50 +0200277 */
278 session_pool_remove_peeker (s0->thread_index);
Florin Corase759bb52020-04-08 01:55:39 +0000279 session_dgram_connect_notify (&uc0->connection,
Aloys Augustin8fadb652019-09-24 18:57:50 +0200280 s0->thread_index, &s0);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200281 queue_event = 0;
282 }
283 else
284 s0->session_state = SESSION_STATE_READY;
285 }
Florin Corase759bb52020-04-08 01:55:39 +0000286 udp_connection_enqueue (uc0, s0, &hdr0, thread_index, b[0],
287 queue_event, &error0);
288 session_pool_remove_peeker (s0->thread_index);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200289 }
290 else if (s0->session_state == SESSION_STATE_READY)
291 {
Florin Corase759bb52020-04-08 01:55:39 +0000292 uc0 = udp_connection_from_transport (session_get_transport (s0));
293 udp_connection_enqueue (uc0, s0, &hdr0, thread_index, b[0], 1,
294 &error0);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200295 }
296 else if (s0->session_state == SESSION_STATE_LISTENING)
297 {
Florin Corase759bb52020-04-08 01:55:39 +0000298 uc0 = udp_connection_from_transport (session_get_transport (s0));
Aloys Augustin8fadb652019-09-24 18:57:50 +0200299 if (uc0->flags & UDP_CONN_F_CONNECTED)
300 {
Florin Corase759bb52020-04-08 01:55:39 +0000301 uc0 = udp_connection_accept (uc0, &hdr0, thread_index);
302 if (!uc0)
Aloys Augustin8fadb652019-09-24 18:57:50 +0200303 {
304 error0 = UDP_ERROR_CREATE_SESSION;
Florin Corase759bb52020-04-08 01:55:39 +0000305 goto done;
Aloys Augustin8fadb652019-09-24 18:57:50 +0200306 }
Florin Corase759bb52020-04-08 01:55:39 +0000307 s0 = session_get (uc0->c_s_index, uc0->c_thread_index);
308 error0 = UDP_ERROR_ACCEPT;
Aloys Augustin8fadb652019-09-24 18:57:50 +0200309 }
Florin Corase759bb52020-04-08 01:55:39 +0000310 udp_connection_enqueue (uc0, s0, &hdr0, thread_index, b[0], 1,
311 &error0);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200312 }
313 else
314 {
315 error0 = UDP_ERROR_NOT_READY;
Florin Corase759bb52020-04-08 01:55:39 +0000316 session_pool_remove_peeker (s0->thread_index);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200317 }
318
Florin Corase759bb52020-04-08 01:55:39 +0000319 done:
Andreas Schultz063f2b82020-04-16 16:18:57 +0200320 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
321 udp_trace_buffer (vm, node, b[0], s0, error0);
Aloys Augustin8fadb652019-09-24 18:57:50 +0200322
Florin Corase759bb52020-04-08 01:55:39 +0000323 b += 1;
324 n_left_from -= 1;
Florin Corasba78e232020-04-06 21:28:59 +0000325
Florin Corase759bb52020-04-08 01:55:39 +0000326 udp_inc_err_counter (err_counters, error0, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500327 }
328
Aloys Augustin8fadb652019-09-24 18:57:50 +0200329 vlib_buffer_free (vm, first_buffer, frame->n_vectors);
Florin Coras9cbf6812019-08-06 18:28:49 -0700330 errors = session_main_flush_enqueue_events (TRANSPORT_PROTO_UDP,
Florin Corase759bb52020-04-08 01:55:39 +0000331 thread_index);
332 err_counters[UDP_ERROR_MQ_FULL] = errors;
333 udp_store_err_counters (vm, is_ip4, err_counters);
Dave Barach68b0fb02017-02-28 15:15:56 -0500334 return frame->n_vectors;
335}
336
Florin Coras3cbc04b2017-10-02 00:18:51 -0700337static uword
338udp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
339 vlib_frame_t * frame)
Dave Barach68b0fb02017-02-28 15:15:56 -0500340{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700341 return udp46_input_inline (vm, node, frame, 1);
342}
343
344/* *INDENT-OFF* */
345VLIB_REGISTER_NODE (udp4_input_node) =
346{
347 .function = udp4_input,
348 .name = "udp4-input",
349 .vector_size = sizeof (u32),
350 .format_trace = format_udp_input_trace,
351 .type = VLIB_NODE_TYPE_INTERNAL,
352 .n_errors = ARRAY_LEN (udp_error_strings),
353 .error_strings = udp_error_strings,
354 .n_next_nodes = UDP_INPUT_N_NEXT,
355 .next_nodes = {
356#define _(s, n) [UDP_INPUT_NEXT_##s] = n,
357 foreach_udp_input_next
358#undef _
359 },
360};
361/* *INDENT-ON* */
362
363static uword
364udp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
365 vlib_frame_t * frame)
366{
367 return udp46_input_inline (vm, node, frame, 0);
368}
369
370/* *INDENT-OFF* */
371VLIB_REGISTER_NODE (udp6_input_node) =
372{
373 .function = udp6_input,
374 .name = "udp6-input",
375 .vector_size = sizeof (u32),
376 .format_trace = format_udp_input_trace,
377 .type = VLIB_NODE_TYPE_INTERNAL,
378 .n_errors = ARRAY_LEN (udp_error_strings),
379 .error_strings = udp_error_strings,
380 .n_next_nodes = UDP_INPUT_N_NEXT,
381 .next_nodes = {
382#define _(s, n) [UDP_INPUT_NEXT_##s] = n,
383 foreach_udp_input_next
384#undef _
385 },
386};
387/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500388
389/*
390 * fd.io coding-style-patch-verification: ON
391 *
392 * Local Variables:
393 * eval: (c-set-style "gnu")
394 * End:
395 */