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/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>
30 static char *udp_error_strings[] = {
31 #define udp_error(n,s) s,
32 #include "udp_error.def"
43 /* packet trace format function */
45 format_udp_input_trace (u8 * s, va_list * args)
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 *);
51 s = format (s, "UDP_INPUT: connection %d, disposition %d, thread %d",
52 t->connection, t->disposition, t->thread_index);
56 #define foreach_udp_input_next \
57 _ (DROP, "error-drop")
61 #define _(s, n) UDP_INPUT_NEXT_##s,
62 foreach_udp_input_next
68 udp_input_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
70 if (PREDICT_TRUE (!val))
74 vlib_node_increment_counter (vm, udp4_input_node.index, evt, val);
76 vlib_node_increment_counter (vm, udp6_input_node.index, evt, val);
80 udp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
81 vlib_frame_t * frame, u8 is_ip4)
83 u32 n_left_from, *from, *to_next;
84 u32 next_index, errors;
85 u32 my_thread_index = vm->thread_index;
87 from = vlib_frame_vector_args (frame);
88 n_left_from = frame->n_vectors;
89 next_index = node->cached_next_index;
91 while (n_left_from > 0)
95 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
97 while (n_left_from > 0 && n_left_to_next > 0)
101 u32 next0 = UDP_INPUT_NEXT_DROP;
102 u32 error0 = UDP_ERROR_ENQUEUED;
108 udp_connection_t *uc0, *child0, *new_uc0;
109 transport_connection_t *tc0;
111 void *rmt_addr, *lcl_addr;
112 session_dgram_hdr_t hdr0;
114 /* speculatively enqueue b0 to the current next frame */
122 b0 = vlib_get_buffer (vm, bi0);
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;
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,
135 udp0->src_port, TRANSPORT_PROTO_UDP);
136 lcl_addr = &ip40->dst_address;
137 rmt_addr = &ip40->src_address;
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,
145 udp0->src_port, TRANSPORT_PROTO_UDP);
146 lcl_addr = &ip60->dst_address;
147 rmt_addr = &ip60->src_address;
150 if (PREDICT_FALSE (!s0))
152 error0 = UDP_ERROR_NO_LISTENER;
156 if (s0->session_state == SESSION_STATE_OPENED)
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.
163 tc0 = session_get_transport (s0);
164 uc0 = udp_get_connection_from_transport (tc0);
165 if (uc0->is_connected)
168 * Clone the transport. It will be cleaned up with the
169 * session once we notify the session layer.
171 new_uc0 = udp_connection_clone_safe (s0->connection_index,
173 ASSERT (s0->session_index == new_uc0->c_s_index);
176 * Drop the 'lock' on pool resize
178 session_pool_remove_peeker (s0->thread_index);
179 session_dgram_connect_notify (&new_uc0->connection,
180 s0->thread_index, &s0);
181 tc0 = &new_uc0->connection;
184 else if (s0->session_state == SESSION_STATE_READY)
186 tc0 = session_get_transport (s0);
187 uc0 = udp_get_connection_from_transport (tc0);
189 else if (s0->session_state == SESSION_STATE_LISTENING)
191 tc0 = listen_session_get_transport (s0);
192 uc0 = udp_get_connection_from_transport (tc0);
193 if (uc0->is_connected)
195 child0 = udp_connection_alloc (my_thread_index);
198 ip_set (&child0->c_lcl_ip, &ip40->dst_address, 1);
199 ip_set (&child0->c_rmt_ip, &ip40->src_address, 1);
203 ip_set (&child0->c_lcl_ip, &ip60->dst_address, 0);
204 ip_set (&child0->c_rmt_ip, &ip60->src_address, 0);
206 child0->c_lcl_port = udp0->dst_port;
207 child0->c_rmt_port = udp0->src_port;
208 child0->c_is_ip4 = is_ip4;
209 child0->c_fib_index = tc0->fib_index;
210 child0->is_connected = 1;
212 if (session_stream_accept (&child0->connection,
213 tc0->s_index, tc0->thread_index,
216 error0 = UDP_ERROR_CREATE_SESSION;
220 session_get (child0->c_s_index, child0->c_thread_index);
221 s0->session_state = SESSION_STATE_READY;
222 tc0 = &child0->connection;
223 uc0 = udp_get_connection_from_transport (tc0);
224 error0 = UDP_ERROR_LISTENER;
229 error0 = UDP_ERROR_NOT_READY;
234 if (svm_fifo_max_enqueue_prod (s0->rx_fifo)
235 < b0->current_length + sizeof (session_dgram_hdr_t))
237 error0 = UDP_ERROR_FIFO_FULL;
240 hdr0.data_length = b0->current_length;
241 hdr0.data_offset = 0;
242 ip_set (&hdr0.lcl_ip, lcl_addr, is_ip4);
243 ip_set (&hdr0.rmt_ip, rmt_addr, is_ip4);
244 hdr0.lcl_port = udp0->dst_port;
245 hdr0.rmt_port = udp0->src_port;
246 hdr0.is_ip4 = is_ip4;
248 clib_spinlock_lock (&uc0->rx_lock);
249 wrote0 = session_enqueue_dgram_connection (s0, &hdr0, b0,
252 clib_spinlock_unlock (&uc0->rx_lock);
255 if (s0->session_state != SESSION_STATE_LISTENING)
256 session_pool_remove_peeker (s0->thread_index);
260 b0->error = node->errors[error0];
262 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
263 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
265 udp_input_trace_t *t = vlib_add_trace (vm, node, b0,
268 t->connection = s0 ? s0->connection_index : ~0;
269 t->disposition = error0;
270 t->thread_index = my_thread_index;
273 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
274 to_next, n_left_to_next,
278 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
281 errors = session_main_flush_all_enqueue_events (TRANSPORT_PROTO_UDP);
282 udp_input_inc_counter (vm, is_ip4, UDP_ERROR_EVENT_FIFO_FULL, errors);
283 return frame->n_vectors;
288 udp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
289 vlib_frame_t * frame)
291 return udp46_input_inline (vm, node, frame, 1);
295 VLIB_REGISTER_NODE (udp4_input_node) =
297 .function = udp4_input,
298 .name = "udp4-input",
299 .vector_size = sizeof (u32),
300 .format_trace = format_udp_input_trace,
301 .type = VLIB_NODE_TYPE_INTERNAL,
302 .n_errors = ARRAY_LEN (udp_error_strings),
303 .error_strings = udp_error_strings,
304 .n_next_nodes = UDP_INPUT_N_NEXT,
306 #define _(s, n) [UDP_INPUT_NEXT_##s] = n,
307 foreach_udp_input_next
314 udp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
315 vlib_frame_t * frame)
317 return udp46_input_inline (vm, node, frame, 0);
321 VLIB_REGISTER_NODE (udp6_input_node) =
323 .function = udp6_input,
324 .name = "udp6-input",
325 .vector_size = sizeof (u32),
326 .format_trace = format_udp_input_trace,
327 .type = VLIB_NODE_TYPE_INTERNAL,
328 .n_errors = ARRAY_LEN (udp_error_strings),
329 .error_strings = udp_error_strings,
330 .n_next_nodes = UDP_INPUT_N_NEXT,
332 #define _(s, n) [UDP_INPUT_NEXT_##s] = n,
333 foreach_udp_input_next
340 * fd.io coding-style-patch-verification: ON
343 * eval: (c-set-style "gnu")