Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 Cisco and/or its affiliates. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 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: |
| 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 Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 16 | #include <vlibmemory/api.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 17 | #include <vlib/vlib.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 18 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 19 | #include <vppinfra/hash.h> |
| 20 | #include <vppinfra/error.h> |
| 21 | #include <vppinfra/elog.h> |
| 22 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 23 | #include <vnet/vnet.h> |
| 24 | #include <vnet/pg/pg.h> |
| 25 | #include <vnet/ip/ip.h> |
| 26 | #include <vnet/udp/udp.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | #include <vnet/udp/udp_packet.h> |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 28 | #include <vnet/session/session.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 29 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 30 | static char *udp_error_strings[] = { |
| 31 | #define udp_error(n,s) s, |
| 32 | #include "udp_error.def" |
| 33 | #undef udp_error |
| 34 | }; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 35 | |
| 36 | typedef struct |
| 37 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 38 | u32 connection; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 39 | u32 disposition; |
| 40 | u32 thread_index; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 41 | } udp_input_trace_t; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 42 | |
| 43 | /* packet trace format function */ |
| 44 | static u8 * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 45 | format_udp_input_trace (u8 * s, va_list * args) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 46 | { |
| 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 Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 49 | udp_input_trace_t *t = va_arg (*args, udp_input_trace_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 50 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 51 | s = format (s, "UDP_INPUT: connection %d, disposition %d, thread %d", |
| 52 | t->connection, t->disposition, t->thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 53 | return s; |
| 54 | } |
| 55 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 56 | #define foreach_udp_input_next \ |
| 57 | _ (DROP, "error-drop") |
| 58 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 59 | typedef enum |
| 60 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 61 | #define _(s, n) UDP_INPUT_NEXT_##s, |
| 62 | foreach_udp_input_next |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 63 | #undef _ |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 64 | UDP_INPUT_N_NEXT, |
| 65 | } udp_input_next_t; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 66 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 67 | always_inline void |
| 68 | udp_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 | |
| 79 | always_inline uword |
| 80 | udp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 81 | vlib_frame_t * frame, u8 is_ip4) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 82 | { |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 83 | u32 n_left_from, *from; |
| 84 | u32 errors, *first_buffer; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 85 | u32 my_thread_index = vm->thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 86 | |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 87 | from = first_buffer = vlib_frame_vector_args (frame); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 88 | n_left_from = frame->n_vectors; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 89 | |
| 90 | while (n_left_from > 0) |
| 91 | { |
Florin Coras | 9361974 | 2020-03-31 01:49:40 +0000 | [diff] [blame] | 92 | u32 bi0, fib_index0, data_len; |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 93 | vlib_buffer_t *b0; |
| 94 | u32 error0 = UDP_ERROR_ENQUEUED; |
| 95 | udp_header_t *udp0; |
| 96 | ip4_header_t *ip40; |
| 97 | ip6_header_t *ip60; |
| 98 | u8 *data0; |
| 99 | session_t *s0; |
| 100 | udp_connection_t *uc0, *child0, *new_uc0; |
| 101 | transport_connection_t *tc0; |
| 102 | int wrote0; |
| 103 | void *rmt_addr, *lcl_addr; |
| 104 | session_dgram_hdr_t hdr0; |
| 105 | u8 queue_event = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 106 | |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 107 | /* speculatively enqueue b0 to the current next frame */ |
| 108 | bi0 = from[0]; |
| 109 | from += 1; |
| 110 | n_left_from -= 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 111 | |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 112 | b0 = vlib_get_buffer (vm, bi0); |
| 113 | |
| 114 | /* udp_local hands us a pointer to the udp data */ |
| 115 | data0 = vlib_buffer_get_current (b0); |
| 116 | udp0 = (udp_header_t *) (data0 - sizeof (*udp0)); |
| 117 | fib_index0 = vnet_buffer (b0)->ip.fib_index; |
| 118 | |
| 119 | if (is_ip4) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 120 | { |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 121 | /* TODO: must fix once udp_local does ip options correctly */ |
| 122 | ip40 = (ip4_header_t *) (((u8 *) udp0) - sizeof (*ip40)); |
| 123 | s0 = session_lookup_safe4 (fib_index0, &ip40->dst_address, |
| 124 | &ip40->src_address, udp0->dst_port, |
| 125 | udp0->src_port, TRANSPORT_PROTO_UDP); |
| 126 | lcl_addr = &ip40->dst_address; |
| 127 | rmt_addr = &ip40->src_address; |
Florin Coras | 9361974 | 2020-03-31 01:49:40 +0000 | [diff] [blame] | 128 | data_len = clib_net_to_host_u16 (ip40->length); |
| 129 | data_len -= sizeof (ip4_header_t) + sizeof (udp_header_t); |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 130 | } |
| 131 | else |
| 132 | { |
| 133 | ip60 = (ip6_header_t *) (((u8 *) udp0) - sizeof (*ip60)); |
| 134 | s0 = session_lookup_safe6 (fib_index0, &ip60->dst_address, |
| 135 | &ip60->src_address, udp0->dst_port, |
| 136 | udp0->src_port, TRANSPORT_PROTO_UDP); |
| 137 | lcl_addr = &ip60->dst_address; |
| 138 | rmt_addr = &ip60->src_address; |
Florin Coras | 9361974 | 2020-03-31 01:49:40 +0000 | [diff] [blame] | 139 | data_len = clib_net_to_host_u16 (ip60->payload_length); |
| 140 | data_len -= sizeof (udp_header_t); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 143 | if (PREDICT_FALSE (!s0)) |
| 144 | { |
| 145 | error0 = UDP_ERROR_NO_LISTENER; |
| 146 | goto trace0; |
| 147 | } |
| 148 | |
| 149 | if (s0->session_state == SESSION_STATE_OPENED) |
| 150 | { |
| 151 | /* TODO optimization: move cl session to right thread |
| 152 | * However, since such a move would affect the session handle, |
| 153 | * which we pass 'raw' to the app, we'd also have notify the |
| 154 | * app of the change or change the way we pass handles to apps. |
| 155 | */ |
| 156 | tc0 = session_get_transport (s0); |
| 157 | uc0 = udp_get_connection_from_transport (tc0); |
| 158 | if (uc0->flags & UDP_CONN_F_CONNECTED) |
| 159 | { |
| 160 | if (s0->thread_index != vlib_get_thread_index ()) |
| 161 | { |
| 162 | /* |
| 163 | * Clone the transport. It will be cleaned up with the |
| 164 | * session once we notify the session layer. |
| 165 | */ |
| 166 | new_uc0 = |
| 167 | udp_connection_clone_safe (s0->connection_index, |
| 168 | s0->thread_index); |
| 169 | ASSERT (s0->session_index == new_uc0->c_s_index); |
| 170 | |
| 171 | /* |
| 172 | * Drop the 'lock' on pool resize |
| 173 | */ |
| 174 | session_pool_remove_peeker (s0->thread_index); |
| 175 | session_dgram_connect_notify (&new_uc0->connection, |
| 176 | s0->thread_index, &s0); |
| 177 | tc0 = &new_uc0->connection; |
| 178 | uc0 = new_uc0; |
| 179 | queue_event = 0; |
| 180 | } |
| 181 | else |
| 182 | s0->session_state = SESSION_STATE_READY; |
| 183 | } |
| 184 | } |
| 185 | else if (s0->session_state == SESSION_STATE_READY) |
| 186 | { |
| 187 | tc0 = session_get_transport (s0); |
| 188 | uc0 = udp_get_connection_from_transport (tc0); |
| 189 | } |
| 190 | else if (s0->session_state == SESSION_STATE_LISTENING) |
| 191 | { |
| 192 | tc0 = listen_session_get_transport (s0); |
| 193 | uc0 = udp_get_connection_from_transport (tc0); |
| 194 | if (uc0->flags & UDP_CONN_F_CONNECTED) |
| 195 | { |
| 196 | child0 = udp_connection_alloc (my_thread_index); |
| 197 | if (is_ip4) |
| 198 | { |
| 199 | ip_set (&child0->c_lcl_ip, &ip40->dst_address, 1); |
| 200 | ip_set (&child0->c_rmt_ip, &ip40->src_address, 1); |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | ip_set (&child0->c_lcl_ip, &ip60->dst_address, 0); |
| 205 | ip_set (&child0->c_rmt_ip, &ip60->src_address, 0); |
| 206 | } |
| 207 | child0->c_lcl_port = udp0->dst_port; |
| 208 | child0->c_rmt_port = udp0->src_port; |
| 209 | child0->c_is_ip4 = is_ip4; |
| 210 | child0->c_fib_index = tc0->fib_index; |
| 211 | child0->flags |= UDP_CONN_F_CONNECTED; |
| 212 | |
| 213 | if (session_stream_accept (&child0->connection, |
| 214 | tc0->s_index, tc0->thread_index, 1)) |
| 215 | { |
| 216 | error0 = UDP_ERROR_CREATE_SESSION; |
| 217 | goto trace0; |
| 218 | } |
| 219 | s0 = session_get (child0->c_s_index, child0->c_thread_index); |
| 220 | s0->session_state = SESSION_STATE_READY; |
| 221 | tc0 = &child0->connection; |
| 222 | uc0 = udp_get_connection_from_transport (tc0); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 223 | udp_connection_share_port (clib_net_to_host_u16 |
| 224 | (uc0->c_lcl_port), uc0->c_is_ip4); |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 225 | error0 = UDP_ERROR_LISTENER; |
| 226 | } |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | error0 = UDP_ERROR_NOT_READY; |
| 231 | goto trace0; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | if (svm_fifo_max_enqueue_prod (s0->rx_fifo) |
Florin Coras | 9361974 | 2020-03-31 01:49:40 +0000 | [diff] [blame] | 236 | < data_len + sizeof (session_dgram_hdr_t)) |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 237 | { |
| 238 | error0 = UDP_ERROR_FIFO_FULL; |
| 239 | goto trace0; |
| 240 | } |
Florin Coras | 9361974 | 2020-03-31 01:49:40 +0000 | [diff] [blame] | 241 | hdr0.data_length = b0->current_length = data_len; |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 242 | hdr0.data_offset = 0; |
| 243 | ip_set (&hdr0.lcl_ip, lcl_addr, is_ip4); |
| 244 | ip_set (&hdr0.rmt_ip, rmt_addr, is_ip4); |
| 245 | hdr0.lcl_port = udp0->dst_port; |
| 246 | hdr0.rmt_port = udp0->src_port; |
| 247 | hdr0.is_ip4 = is_ip4; |
| 248 | |
| 249 | clib_spinlock_lock (&uc0->rx_lock); |
| 250 | /* If session is owned by another thread and rx event needed, |
| 251 | * enqueue event now while we still have the peeker lock */ |
| 252 | if (s0->thread_index != my_thread_index) |
| 253 | { |
| 254 | wrote0 = session_enqueue_dgram_connection (s0, &hdr0, b0, |
| 255 | TRANSPORT_PROTO_UDP, |
| 256 | /* queue event */ 0); |
| 257 | if (queue_event && !svm_fifo_has_event (s0->rx_fifo)) |
| 258 | session_enqueue_notify (s0); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | wrote0 = session_enqueue_dgram_connection (s0, &hdr0, b0, |
| 263 | TRANSPORT_PROTO_UDP, |
| 264 | queue_event); |
| 265 | } |
| 266 | clib_spinlock_unlock (&uc0->rx_lock); |
| 267 | ASSERT (wrote0 > 0); |
| 268 | |
| 269 | if (s0->session_state != SESSION_STATE_LISTENING) |
| 270 | session_pool_remove_peeker (s0->thread_index); |
| 271 | |
| 272 | trace0: |
| 273 | |
| 274 | b0->error = node->errors[error0]; |
| 275 | |
| 276 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) |
| 277 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) |
| 278 | { |
| 279 | udp_input_trace_t *t = vlib_add_trace (vm, node, b0, |
| 280 | sizeof (*t)); |
| 281 | |
| 282 | t->connection = s0 ? s0->connection_index : ~0; |
| 283 | t->disposition = error0; |
| 284 | t->thread_index = my_thread_index; |
| 285 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 286 | } |
| 287 | |
Aloys Augustin | 8fadb65 | 2019-09-24 18:57:50 +0200 | [diff] [blame] | 288 | vlib_buffer_free (vm, first_buffer, frame->n_vectors); |
Florin Coras | 9cbf681 | 2019-08-06 18:28:49 -0700 | [diff] [blame] | 289 | errors = session_main_flush_enqueue_events (TRANSPORT_PROTO_UDP, |
| 290 | my_thread_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 291 | udp_input_inc_counter (vm, is_ip4, UDP_ERROR_EVENT_FIFO_FULL, errors); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 292 | return frame->n_vectors; |
| 293 | } |
| 294 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 295 | |
| 296 | static uword |
| 297 | udp4_input (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 298 | vlib_frame_t * frame) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 299 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 300 | return udp46_input_inline (vm, node, frame, 1); |
| 301 | } |
| 302 | |
| 303 | /* *INDENT-OFF* */ |
| 304 | VLIB_REGISTER_NODE (udp4_input_node) = |
| 305 | { |
| 306 | .function = udp4_input, |
| 307 | .name = "udp4-input", |
| 308 | .vector_size = sizeof (u32), |
| 309 | .format_trace = format_udp_input_trace, |
| 310 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 311 | .n_errors = ARRAY_LEN (udp_error_strings), |
| 312 | .error_strings = udp_error_strings, |
| 313 | .n_next_nodes = UDP_INPUT_N_NEXT, |
| 314 | .next_nodes = { |
| 315 | #define _(s, n) [UDP_INPUT_NEXT_##s] = n, |
| 316 | foreach_udp_input_next |
| 317 | #undef _ |
| 318 | }, |
| 319 | }; |
| 320 | /* *INDENT-ON* */ |
| 321 | |
| 322 | static uword |
| 323 | udp6_input (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 324 | vlib_frame_t * frame) |
| 325 | { |
| 326 | return udp46_input_inline (vm, node, frame, 0); |
| 327 | } |
| 328 | |
| 329 | /* *INDENT-OFF* */ |
| 330 | VLIB_REGISTER_NODE (udp6_input_node) = |
| 331 | { |
| 332 | .function = udp6_input, |
| 333 | .name = "udp6-input", |
| 334 | .vector_size = sizeof (u32), |
| 335 | .format_trace = format_udp_input_trace, |
| 336 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 337 | .n_errors = ARRAY_LEN (udp_error_strings), |
| 338 | .error_strings = udp_error_strings, |
| 339 | .n_next_nodes = UDP_INPUT_N_NEXT, |
| 340 | .next_nodes = { |
| 341 | #define _(s, n) [UDP_INPUT_NEXT_##s] = n, |
| 342 | foreach_udp_input_next |
| 343 | #undef _ |
| 344 | }, |
| 345 | }; |
| 346 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * fd.io coding-style-patch-verification: ON |
| 350 | * |
| 351 | * Local Variables: |
| 352 | * eval: (c-set-style "gnu") |
| 353 | * End: |
| 354 | */ |