2 * Copyright (c) 2016-2019 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlibmemory/api.h>
17 #include <vlib/vlib.h>
19 #include <vppinfra/hash.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/elog.h>
23 #include <vnet/vnet.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/udp/udp.h>
26 #include <vnet/udp/udp_packet.h>
27 #include <vnet/session/session.h>
29 static vlib_error_desc_t udp_error_counters[] = {
30 #define udp_error(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s },
31 #include "udp_error.def"
42 /* packet trace format function */
44 format_udp_input_trace (u8 * s, va_list * args)
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 *);
48 udp_input_trace_t *t = va_arg (*args, udp_input_trace_t *);
50 s = format (s, "UDP_INPUT: connection %d, disposition %d, thread %d",
51 t->connection, t->disposition, t->thread_index);
55 #define foreach_udp_input_next \
56 _ (DROP, "error-drop")
60 #define _(s, n) UDP_INPUT_NEXT_##s,
61 foreach_udp_input_next
67 udp_input_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
70 vlib_node_increment_counter (vm, udp4_input_node.index, evt, val);
72 vlib_node_increment_counter (vm, udp6_input_node.index, evt, val);
75 #define udp_store_err_counters(vm, is_ip4, cnts) \
78 for (i = 0; i < UDP_N_ERROR; i++) \
80 udp_input_inc_counter(vm, is_ip4, i, cnts[i]); \
83 #define udp_inc_err_counter(cnts, err, val) \
89 udp_trace_buffer (vlib_main_t * vm, vlib_node_runtime_t * node,
90 vlib_buffer_t * b, session_t * s, u16 error0)
94 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_IS_TRACED)))
97 t = vlib_add_trace (vm, node, b, sizeof (*t));
98 t->connection = s ? s->connection_index : ~0;
99 t->disposition = error0;
100 t->thread_index = s ? s->thread_index : vm->thread_index;
103 static udp_connection_t *
104 udp_connection_accept (udp_connection_t * listener, session_dgram_hdr_t * hdr,
107 udp_connection_t *uc;
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;
119 if (session_dgram_accept (&uc->connection, listener->c_s_index,
120 listener->c_thread_index))
122 udp_connection_free (uc);
125 udp_connection_share_port (clib_net_to_host_u16
126 (uc->c_lcl_port), uc->c_is_ip4);
131 udp_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)
137 if (!(uc0->flags & UDP_CONN_F_CONNECTED))
138 clib_spinlock_lock (&uc0->rx_lock);
140 if (svm_fifo_max_enqueue_prod (s0->rx_fifo)
141 < hdr0->data_length + sizeof (session_dgram_hdr_t))
143 *error0 = UDP_ERROR_FIFO_FULL;
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)
151 wrote0 = session_enqueue_dgram_connection (s0, hdr0, b,
153 /* queue event */ 0);
154 if (queue_event && !svm_fifo_has_event (s0->rx_fifo))
155 session_enqueue_notify (s0);
159 wrote0 = session_enqueue_dgram_connection (s0, hdr0, b,
167 if (!(uc0->flags & UDP_CONN_F_CONNECTED))
168 clib_spinlock_unlock (&uc0->rx_lock);
171 always_inline session_t *
172 udp_parse_and_lookup_buffer (vlib_buffer_t * b, session_dgram_hdr_t * hdr,
179 /* udp_local hands us a pointer to the udp data */
180 udp = (udp_header_t *) (vlib_buffer_get_current (b) - sizeof (*udp));
181 fib_index = vnet_buffer (b)->ip.fib_index;
183 hdr->data_offset = 0;
184 hdr->lcl_port = udp->dst_port;
185 hdr->rmt_port = udp->src_port;
186 hdr->is_ip4 = is_ip4;
192 /* TODO: must fix once udp_local does ip options correctly */
193 ip4 = (ip4_header_t *) (((u8 *) udp) - sizeof (*ip4));
194 ip_set (&hdr->lcl_ip, &ip4->dst_address, 1);
195 ip_set (&hdr->rmt_ip, &ip4->src_address, 1);
196 hdr->data_length = clib_net_to_host_u16 (ip4->length);
197 hdr->data_length -= sizeof (ip4_header_t) + sizeof (udp_header_t);
198 s = session_lookup_safe4 (fib_index, &ip4->dst_address,
199 &ip4->src_address, udp->dst_port,
200 udp->src_port, TRANSPORT_PROTO_UDP);
206 ip60 = (ip6_header_t *) (((u8 *) udp) - sizeof (*ip60));
207 ip_set (&hdr->lcl_ip, &ip60->dst_address, 0);
208 ip_set (&hdr->rmt_ip, &ip60->src_address, 0);
209 hdr->data_length = clib_net_to_host_u16 (ip60->payload_length);
210 hdr->data_length -= sizeof (udp_header_t);
211 s = session_lookup_safe6 (fib_index, &ip60->dst_address,
212 &ip60->src_address, udp->dst_port,
213 udp->src_port, TRANSPORT_PROTO_UDP);
216 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
217 b->current_length = hdr->data_length;
219 b->total_length_not_including_first_buffer = hdr->data_length
226 udp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
227 vlib_frame_t * frame, u8 is_ip4)
229 u32 n_left_from, *from, errors, *first_buffer;
230 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
231 u16 err_counters[UDP_N_ERROR] = { 0 };
232 u32 thread_index = vm->thread_index;
234 from = first_buffer = vlib_frame_vector_args (frame);
235 n_left_from = frame->n_vectors;
236 vlib_get_buffers (vm, from, bufs, n_left_from);
240 while (n_left_from > 0)
242 u32 error0 = UDP_ERROR_ENQUEUED;
243 session_dgram_hdr_t hdr0;
244 udp_connection_t *uc0;
247 s0 = udp_parse_and_lookup_buffer (b[0], &hdr0, is_ip4);
248 if (PREDICT_FALSE (!s0))
250 error0 = UDP_ERROR_NO_LISTENER;
254 if (s0->session_state == SESSION_STATE_OPENED)
257 uc0 = udp_connection_from_transport (session_get_transport (s0));
258 if (uc0->flags & UDP_CONN_F_CONNECTED)
260 if (s0->thread_index != thread_index)
263 * Clone the transport. It will be cleaned up with the
264 * session once we notify the session layer.
266 uc0 = udp_connection_clone_safe (s0->connection_index,
268 ASSERT (s0->session_index == uc0->c_s_index);
271 * Ask session layer for a new session.
273 session_dgram_connect_notify (&uc0->connection,
274 s0->thread_index, &s0);
278 s0->session_state = SESSION_STATE_READY;
280 udp_connection_enqueue (uc0, s0, &hdr0, thread_index, b[0],
281 queue_event, &error0);
283 else if (s0->session_state == SESSION_STATE_READY)
285 uc0 = udp_connection_from_transport (session_get_transport (s0));
286 udp_connection_enqueue (uc0, s0, &hdr0, thread_index, b[0], 1,
289 else if (s0->session_state == SESSION_STATE_LISTENING)
291 uc0 = udp_connection_from_transport (session_get_transport (s0));
292 if (uc0->flags & UDP_CONN_F_CONNECTED)
294 uc0 = udp_connection_accept (uc0, &hdr0, thread_index);
297 error0 = UDP_ERROR_CREATE_SESSION;
300 s0 = session_get (uc0->c_s_index, uc0->c_thread_index);
301 error0 = UDP_ERROR_ACCEPT;
303 udp_connection_enqueue (uc0, s0, &hdr0, thread_index, b[0], 1,
308 error0 = UDP_ERROR_NOT_READY;
312 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
313 udp_trace_buffer (vm, node, b[0], s0, error0);
318 udp_inc_err_counter (err_counters, error0, 1);
321 vlib_buffer_free (vm, first_buffer, frame->n_vectors);
322 errors = session_main_flush_enqueue_events (TRANSPORT_PROTO_UDP,
324 err_counters[UDP_ERROR_MQ_FULL] = errors;
325 udp_store_err_counters (vm, is_ip4, err_counters);
326 return frame->n_vectors;
330 udp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
331 vlib_frame_t * frame)
333 return udp46_input_inline (vm, node, frame, 1);
337 VLIB_REGISTER_NODE (udp4_input_node) =
339 .function = udp4_input,
340 .name = "udp4-input",
341 .vector_size = sizeof (u32),
342 .format_trace = format_udp_input_trace,
343 .type = VLIB_NODE_TYPE_INTERNAL,
344 .n_errors = UDP_N_ERROR,
345 .error_counters = udp_error_counters,
346 .n_next_nodes = UDP_INPUT_N_NEXT,
348 #define _(s, n) [UDP_INPUT_NEXT_##s] = n,
349 foreach_udp_input_next
356 udp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
357 vlib_frame_t * frame)
359 return udp46_input_inline (vm, node, frame, 0);
363 VLIB_REGISTER_NODE (udp6_input_node) =
365 .function = udp6_input,
366 .name = "udp6-input",
367 .vector_size = sizeof (u32),
368 .format_trace = format_udp_input_trace,
369 .type = VLIB_NODE_TYPE_INTERNAL,
370 .n_errors = UDP_N_ERROR,
371 .error_counters = udp_error_counters,
372 .n_next_nodes = UDP_INPUT_N_NEXT,
374 #define _(s, n) [UDP_INPUT_NEXT_##s] = n,
375 foreach_udp_input_next
382 * fd.io coding-style-patch-verification: ON
385 * eval: (c-set-style "gnu")