Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 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 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 16 | #include <vnet/udp/udp.h> |
| 17 | #include <vnet/session/session.h> |
| 18 | #include <vnet/dpo/load_balance.h> |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 19 | #include <vnet/ip/ip4_inlines.h> |
| 20 | #include <vnet/ip/ip6_inlines.h> |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 21 | #include <vppinfra/sparse_vec.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 22 | |
Filip Tehlar | 2c49ffe | 2019-03-06 07:16:08 -0800 | [diff] [blame] | 23 | udp_main_t udp_main; |
| 24 | |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 25 | static void |
Florin Coras | c8e4110 | 2022-03-18 10:27:29 -0700 | [diff] [blame] | 26 | udp_connection_register_port (u16 lcl_port, u8 is_ip4) |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 27 | { |
| 28 | udp_main_t *um = &udp_main; |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 29 | u16 *n; |
| 30 | |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 31 | /* Setup udp protocol -> next index sparse vector mapping. Do not setup |
| 32 | * udp_dst_port_info_t as that is used to distinguish between external |
| 33 | * and transport consumed ports */ |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 34 | |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 35 | if (is_ip4) |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 36 | n = sparse_vec_validate (um->next_by_dst_port4, lcl_port); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 37 | else |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 38 | n = sparse_vec_validate (um->next_by_dst_port6, lcl_port); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 39 | |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 40 | n[0] = um->local_to_input_edge[is_ip4]; |
Florin Coras | 9bc72ac | 2023-01-09 12:46:07 -0800 | [diff] [blame] | 41 | |
| 42 | __atomic_add_fetch (&um->transport_ports_refcnt[is_ip4][lcl_port], 1, |
| 43 | __ATOMIC_RELAXED); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Florin Coras | 37127f7 | 2024-02-17 11:45:25 -0800 | [diff] [blame] | 46 | void |
| 47 | udp_connection_share_port (u16 lcl_port, u8 is_ip4) |
| 48 | { |
| 49 | udp_main_t *um = &udp_main; |
| 50 | __atomic_add_fetch (&um->transport_ports_refcnt[is_ip4][lcl_port], 1, |
| 51 | __ATOMIC_RELAXED); |
| 52 | } |
| 53 | |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 54 | static void |
| 55 | udp_connection_unregister_port (u16 lcl_port, u8 is_ip4) |
| 56 | { |
| 57 | udp_main_t *um = &udp_main; |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 58 | u16 *n; |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 59 | |
Florin Coras | 9bc72ac | 2023-01-09 12:46:07 -0800 | [diff] [blame] | 60 | /* Needed because listeners are not tracked as local endpoints */ |
| 61 | if (__atomic_sub_fetch (&um->transport_ports_refcnt[is_ip4][lcl_port], 1, |
| 62 | __ATOMIC_RELAXED)) |
| 63 | return; |
| 64 | |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 65 | if (is_ip4) |
| 66 | n = sparse_vec_validate (um->next_by_dst_port4, lcl_port); |
| 67 | else |
| 68 | n = sparse_vec_validate (um->next_by_dst_port6, lcl_port); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 69 | |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 70 | n[0] = UDP_NO_NODE_SET; |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 73 | udp_connection_t * |
| 74 | udp_connection_alloc (u32 thread_index) |
| 75 | { |
Florin Coras | 4c89b18 | 2022-10-24 18:59:06 -0700 | [diff] [blame] | 76 | udp_worker_t *wrk = udp_worker_get (thread_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 77 | udp_connection_t *uc; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 78 | |
Florin Coras | 4c89b18 | 2022-10-24 18:59:06 -0700 | [diff] [blame] | 79 | pool_get_aligned_safe (wrk->connections, uc, CLIB_CACHE_LINE_BYTES); |
Florin Coras | a2b358b | 2022-03-18 12:50:03 -0700 | [diff] [blame] | 80 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 81 | clib_memset (uc, 0, sizeof (*uc)); |
Florin Coras | 4c89b18 | 2022-10-24 18:59:06 -0700 | [diff] [blame] | 82 | uc->c_c_index = uc - wrk->connections; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 83 | uc->c_thread_index = thread_index; |
| 84 | uc->c_proto = TRANSPORT_PROTO_UDP; |
| 85 | return uc; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | udp_connection_free (udp_connection_t * uc) |
| 90 | { |
Florin Coras | 4c89b18 | 2022-10-24 18:59:06 -0700 | [diff] [blame] | 91 | udp_worker_t *wrk = udp_worker_get (uc->c_thread_index); |
| 92 | |
Florin Coras | 30fdf39 | 2020-12-02 21:14:56 -0800 | [diff] [blame] | 93 | clib_spinlock_free (&uc->rx_lock); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 94 | if (CLIB_DEBUG) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 95 | clib_memset (uc, 0xFA, sizeof (*uc)); |
Florin Coras | 4c89b18 | 2022-10-24 18:59:06 -0700 | [diff] [blame] | 96 | pool_put (wrk->connections, uc); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 97 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 98 | |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 99 | static void |
| 100 | udp_connection_cleanup (udp_connection_t * uc) |
| 101 | { |
Florin Coras | 81a6ffc | 2024-02-07 11:42:02 -0800 | [diff] [blame] | 102 | transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip, |
| 103 | uc->c_lcl_port); |
| 104 | udp_connection_unregister_port (uc->c_lcl_port, uc->c_is_ip4); |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 105 | udp_connection_free (uc); |
| 106 | } |
| 107 | |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 108 | void |
| 109 | udp_connection_delete (udp_connection_t * uc) |
| 110 | { |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 111 | session_transport_delete_notify (&uc->connection); |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 112 | udp_connection_cleanup (uc); |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Florin Coras | 1c29dfb | 2022-10-24 18:46:20 -0700 | [diff] [blame] | 115 | static void |
| 116 | udp_handle_cleanups (void *args) |
| 117 | { |
| 118 | u32 thread_index = (u32) pointer_to_uword (args); |
| 119 | udp_connection_t *uc; |
| 120 | udp_worker_t *wrk; |
| 121 | u32 *uc_index; |
| 122 | |
| 123 | wrk = udp_worker_get (thread_index); |
| 124 | vec_foreach (uc_index, wrk->pending_cleanups) |
| 125 | { |
| 126 | uc = udp_connection_get (*uc_index, thread_index); |
| 127 | udp_connection_delete (uc); |
| 128 | } |
| 129 | vec_reset_length (wrk->pending_cleanups); |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | udp_connection_program_cleanup (udp_connection_t *uc) |
| 134 | { |
| 135 | uword thread_index = uc->c_thread_index; |
| 136 | udp_worker_t *wrk; |
| 137 | |
| 138 | wrk = udp_worker_get (uc->c_thread_index); |
| 139 | vec_add1 (wrk->pending_cleanups, uc->c_c_index); |
| 140 | |
| 141 | if (vec_len (wrk->pending_cleanups) == 1) |
| 142 | session_send_rpc_evt_to_thread_force ( |
| 143 | thread_index, udp_handle_cleanups, |
| 144 | uword_to_pointer (thread_index, void *)); |
| 145 | } |
| 146 | |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 147 | static u8 |
| 148 | udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4) |
| 149 | { |
| 150 | udp_main_t *um = vnet_get_udp_main (); |
| 151 | udp_dst_port_info_t *pi; |
| 152 | |
| 153 | pi = udp_get_dst_port_info (um, lcl_port, is_ip4); |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 154 | return (pi && udp_is_valid_dst_port (lcl_port, is_ip4)); |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 157 | static u16 |
| 158 | udp_default_mtu (udp_main_t * um, u8 is_ip4) |
| 159 | { |
| 160 | u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t); |
| 161 | return (um->default_mtu - sizeof (udp_header_t) - ip_hlen); |
| 162 | } |
| 163 | |
| 164 | static u32 |
Florin Coras | 0bce71e | 2022-02-10 11:57:06 -0800 | [diff] [blame] | 165 | udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 166 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 167 | udp_main_t *um = vnet_get_udp_main (); |
Florin Coras | 87b7e3d | 2020-03-27 15:06:07 +0000 | [diff] [blame] | 168 | transport_endpoint_cfg_t *lcl_ext; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 169 | udp_connection_t *listener; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 170 | void *iface_ip; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 171 | |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 172 | if (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl->port), |
| 173 | lcl->is_ip4)) |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 174 | { |
| 175 | clib_warning ("port already used"); |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 176 | return SESSION_E_PORTINUSE; |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 177 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 178 | |
| 179 | pool_get (um->listener_pool, listener); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 180 | clib_memset (listener, 0, sizeof (udp_connection_t)); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 181 | |
Florin Coras | 0e49568 | 2017-09-19 22:27:18 -0700 | [diff] [blame] | 182 | listener->c_lcl_port = lcl->port; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 183 | listener->c_c_index = listener - um->listener_pool; |
| 184 | |
| 185 | /* If we are provided a sw_if_index, bind using one of its ips */ |
| 186 | if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX) |
| 187 | { |
| 188 | if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index, |
| 189 | lcl->is_ip4))) |
| 190 | ip_set (&lcl->ip, iface_ip, lcl->is_ip4); |
| 191 | } |
| 192 | ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4); |
| 193 | listener->c_is_ip4 = lcl->is_ip4; |
| 194 | listener->c_proto = TRANSPORT_PROTO_UDP; |
| 195 | listener->c_s_index = session_index; |
| 196 | listener->c_fib_index = lcl->fib_index; |
Steven Luong | bf12efc | 2022-07-25 09:29:23 -0700 | [diff] [blame] | 197 | listener->mss = |
| 198 | lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4); |
Florin Coras | 9c4ec6f | 2020-04-01 02:50:13 +0000 | [diff] [blame] | 199 | listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN; |
Florin Coras | 87b7e3d | 2020-03-27 15:06:07 +0000 | [diff] [blame] | 200 | lcl_ext = (transport_endpoint_cfg_t *) lcl; |
| 201 | if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED) |
| 202 | listener->flags |= UDP_CONN_F_CONNECTED; |
| 203 | else |
| 204 | listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 205 | clib_spinlock_init (&listener->rx_lock); |
Florin Coras | f8ee39f | 2022-10-18 18:37:56 -0700 | [diff] [blame] | 206 | if (!um->csum_offload) |
| 207 | listener->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 208 | |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 209 | udp_connection_register_port (listener->c_lcl_port, lcl->is_ip4); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 210 | return listener->c_c_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 213 | static u32 |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 214 | udp_session_unbind (u32 listener_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 215 | { |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 216 | udp_main_t *um = &udp_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 217 | udp_connection_t *listener; |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 218 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 219 | listener = udp_listener_get (listener_index); |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 220 | udp_connection_unregister_port (listener->c_lcl_port, listener->c_is_ip4); |
Florin Coras | 30fdf39 | 2020-12-02 21:14:56 -0800 | [diff] [blame] | 221 | clib_spinlock_free (&listener->rx_lock); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 222 | pool_put (um->listener_pool, listener); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 226 | static transport_connection_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 227 | udp_session_get_listener (u32 listener_index) |
| 228 | { |
| 229 | udp_connection_t *us; |
| 230 | |
| 231 | us = udp_listener_get (listener_index); |
| 232 | return &us->connection; |
| 233 | } |
| 234 | |
Florin Coras | fc42280 | 2024-05-23 18:22:48 -0700 | [diff] [blame] | 235 | always_inline u16 |
| 236 | udp_compute_checksum (vlib_main_t *vm, vlib_buffer_t *b, u8 csum_offload, |
| 237 | u8 is_ip4) |
| 238 | { |
| 239 | u16 csum = 0; |
| 240 | |
| 241 | if (csum_offload) |
| 242 | vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM); |
| 243 | else |
| 244 | { |
| 245 | if (is_ip4) |
| 246 | csum = |
| 247 | ip4_tcp_udp_compute_checksum (vm, b, vlib_buffer_get_current (b)); |
| 248 | else |
| 249 | { |
| 250 | int bogus = 0; |
| 251 | csum = ip6_tcp_udp_icmp_compute_checksum ( |
| 252 | vm, b, vlib_buffer_get_current (b), &bogus); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return csum; |
| 257 | } |
| 258 | |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 259 | always_inline u32 |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 260 | udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b, |
| 261 | u8 is_cless) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 262 | { |
Florin Coras | fc42280 | 2024-05-23 18:22:48 -0700 | [diff] [blame] | 263 | udp_header_t *uh; |
| 264 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 265 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 266 | /* reuse tcp medatada for now */ |
| 267 | vnet_buffer (b)->tcp.connection_index = uc->c_c_index; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 268 | |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 269 | if (!is_cless) |
| 270 | { |
Florin Coras | fc42280 | 2024-05-23 18:22:48 -0700 | [diff] [blame] | 271 | uh = vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port); |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 272 | |
| 273 | if (uc->c_is_ip4) |
| 274 | vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4, |
| 275 | IP_PROTOCOL_UDP, udp_csum_offload (uc), |
| 276 | 0 /* is_df */, uc->c_dscp); |
| 277 | else |
| 278 | vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6, |
| 279 | IP_PROTOCOL_UDP); |
| 280 | |
| 281 | vnet_buffer (b)->tcp.flags = 0; |
| 282 | } |
Florin Coras | 15952b2 | 2022-12-19 10:55:18 -0800 | [diff] [blame] | 283 | else |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 284 | { |
| 285 | u8 *data = vlib_buffer_get_current (b); |
| 286 | session_dgram_hdr_t hdr; |
| 287 | |
| 288 | hdr = *(session_dgram_hdr_t *) (data - sizeof (hdr)); |
| 289 | |
| 290 | /* Local port assumed to be bound, not overwriting it */ |
Florin Coras | fc42280 | 2024-05-23 18:22:48 -0700 | [diff] [blame] | 291 | uh = vlib_buffer_push_udp (b, uc->c_lcl_port, hdr.rmt_port); |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 292 | |
| 293 | if (uc->c_is_ip4) |
| 294 | vlib_buffer_push_ip4_custom (vm, b, &hdr.lcl_ip.ip4, &hdr.rmt_ip.ip4, |
| 295 | IP_PROTOCOL_UDP, udp_csum_offload (uc), |
| 296 | 0 /* is_df */, uc->c_dscp); |
| 297 | else |
| 298 | vlib_buffer_push_ip6 (vm, b, &hdr.lcl_ip.ip6, &hdr.rmt_ip.ip6, |
| 299 | IP_PROTOCOL_UDP); |
| 300 | |
| 301 | /* Not connected udp session. Mark buffer for custom handling in |
| 302 | * udp_output */ |
| 303 | vnet_buffer (b)->tcp.flags |= UDP_CONN_F_LISTEN; |
| 304 | } |
Florin Coras | 15952b2 | 2022-12-19 10:55:18 -0800 | [diff] [blame] | 305 | |
Florin Coras | fc42280 | 2024-05-23 18:22:48 -0700 | [diff] [blame] | 306 | uh->checksum = |
| 307 | udp_compute_checksum (vm, b, udp_csum_offload (uc), uc->c_is_ip4); |
| 308 | |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 312 | always_inline void |
| 313 | udp_push_header_batch (udp_connection_t *uc, vlib_buffer_t **bs, u32 n_bufs, |
| 314 | u8 is_cless) |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 315 | { |
| 316 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 317 | |
| 318 | while (n_bufs >= 4) |
| 319 | { |
| 320 | vlib_prefetch_buffer_header (bs[2], STORE); |
| 321 | vlib_prefetch_buffer_header (bs[3], STORE); |
| 322 | |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 323 | udp_push_one_header (vm, uc, bs[0], is_cless); |
| 324 | udp_push_one_header (vm, uc, bs[1], is_cless); |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 325 | |
| 326 | n_bufs -= 2; |
| 327 | bs += 2; |
| 328 | } |
| 329 | while (n_bufs) |
| 330 | { |
| 331 | if (n_bufs > 1) |
| 332 | vlib_prefetch_buffer_header (bs[1], STORE); |
| 333 | |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 334 | udp_push_one_header (vm, uc, bs[0], is_cless); |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 335 | |
| 336 | n_bufs -= 1; |
| 337 | bs += 1; |
| 338 | } |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static u32 |
| 342 | udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs) |
| 343 | { |
| 344 | udp_connection_t *uc; |
| 345 | |
| 346 | uc = udp_connection_from_transport (tc); |
| 347 | if (uc->flags & UDP_CONN_F_CONNECTED) |
| 348 | udp_push_header_batch (uc, bs, n_bufs, 0 /* is_cless */); |
| 349 | else |
| 350 | udp_push_header_batch (uc, bs, n_bufs, 1 /* is_cless */); |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 351 | |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 352 | if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING)) |
| 353 | { |
Florin Coras | c8767c4 | 2023-06-27 19:45:59 -0700 | [diff] [blame] | 354 | if (!transport_tx_fifo_has_dgram (&uc->connection)) |
Florin Coras | 1c29dfb | 2022-10-24 18:46:20 -0700 | [diff] [blame] | 355 | udp_connection_program_cleanup (uc); |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 358 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 361 | static transport_connection_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 362 | udp_session_get (u32 connection_index, u32 thread_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 363 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 364 | udp_connection_t *uc; |
| 365 | uc = udp_connection_get (connection_index, thread_index); |
| 366 | if (uc) |
| 367 | return &uc->connection; |
| 368 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 371 | static void |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 372 | udp_session_close (u32 connection_index, u32 thread_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 373 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 374 | udp_connection_t *uc; |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 375 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 376 | uc = udp_connection_get (connection_index, thread_index); |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 377 | if (!uc || (uc->flags & UDP_CONN_F_MIGRATED)) |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 378 | return; |
| 379 | |
Florin Coras | c8767c4 | 2023-06-27 19:45:59 -0700 | [diff] [blame] | 380 | if (!transport_tx_fifo_has_dgram (&uc->connection)) |
Florin Coras | 1c29dfb | 2022-10-24 18:46:20 -0700 | [diff] [blame] | 381 | udp_connection_program_cleanup (uc); |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 382 | else |
| 383 | uc->flags |= UDP_CONN_F_CLOSING; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 386 | static void |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 387 | udp_session_cleanup (u32 connection_index, u32 thread_index) |
| 388 | { |
| 389 | udp_connection_t *uc; |
| 390 | uc = udp_connection_get (connection_index, thread_index); |
Florin Coras | d85666f | 2020-04-03 00:58:48 +0000 | [diff] [blame] | 391 | if (!uc) |
| 392 | return; |
| 393 | if (uc->flags & UDP_CONN_F_MIGRATED) |
| 394 | udp_connection_free (uc); |
| 395 | else |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 396 | udp_connection_cleanup (uc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 397 | } |
| 398 | |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 399 | static int |
| 400 | udp_session_send_params (transport_connection_t * tconn, |
| 401 | transport_send_params_t * sp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 402 | { |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 403 | udp_connection_t *uc; |
| 404 | |
Florin Coras | e759bb5 | 2020-04-08 01:55:39 +0000 | [diff] [blame] | 405 | uc = udp_connection_from_transport (tconn); |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 406 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 407 | /* No constraint on TX window */ |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 408 | sp->snd_space = ~0; |
| 409 | /* TODO figure out MTU of output interface */ |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 410 | sp->snd_mss = uc->mss; |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 411 | sp->tx_offset = 0; |
| 412 | sp->flags = 0; |
| 413 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 414 | } |
| 415 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 416 | static int |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 417 | udp_open_connection (transport_endpoint_cfg_t * rmt) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 418 | { |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 419 | udp_main_t *um = &udp_main; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 420 | ip46_address_t lcl_addr; |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 421 | udp_connection_t *uc; |
Florin Coras | c8e4110 | 2022-03-18 10:27:29 -0700 | [diff] [blame] | 422 | u32 thread_index; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 423 | u16 lcl_port; |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 424 | int rv; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 425 | |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 426 | rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr, |
| 427 | &lcl_port); |
| 428 | if (rv) |
Florin Coras | 3d6156f | 2023-01-30 11:18:36 -0800 | [diff] [blame] | 429 | return rv; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 430 | |
Florin Coras | d921b89 | 2023-05-25 12:04:53 -0700 | [diff] [blame] | 431 | if (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl_port), |
| 432 | rmt->is_ip4)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 433 | { |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 434 | /* If specific source port was requested abort */ |
| 435 | if (rmt->peer.port) |
Florin Coras | 3d6156f | 2023-01-30 11:18:36 -0800 | [diff] [blame] | 436 | { |
| 437 | transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr, |
| 438 | lcl_port); |
| 439 | return SESSION_E_PORTINUSE; |
| 440 | } |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 441 | |
| 442 | /* Try to find a port that's not used */ |
Florin Coras | d921b89 | 2023-05-25 12:04:53 -0700 | [diff] [blame] | 443 | while (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl_port), |
| 444 | rmt->is_ip4)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 445 | { |
Florin Coras | 3d6156f | 2023-01-30 11:18:36 -0800 | [diff] [blame] | 446 | transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr, |
| 447 | lcl_port); |
| 448 | lcl_port = |
| 449 | transport_alloc_local_port (TRANSPORT_PROTO_UDP, &lcl_addr, rmt); |
Florin Coras | 2365341 | 2024-03-09 15:51:50 -0800 | [diff] [blame] | 450 | if ((int) lcl_port < 1) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 451 | return SESSION_E_PORTINUSE; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 455 | /* We don't poll main thread if we have workers */ |
Florin Coras | fb50bc3 | 2021-05-18 00:28:59 -0700 | [diff] [blame] | 456 | thread_index = transport_cl_thread (); |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 457 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 458 | uc = udp_connection_alloc (thread_index); |
| 459 | ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4); |
| 460 | ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4); |
| 461 | uc->c_rmt_port = rmt->port; |
Florin Coras | 2365341 | 2024-03-09 15:51:50 -0800 | [diff] [blame] | 462 | uc->c_lcl_port = lcl_port; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 463 | uc->c_is_ip4 = rmt->is_ip4; |
| 464 | uc->c_proto = TRANSPORT_PROTO_UDP; |
| 465 | uc->c_fib_index = rmt->fib_index; |
Filip Tehlar | 3ef8bf3 | 2021-10-21 14:07:31 +0000 | [diff] [blame] | 466 | uc->c_dscp = rmt->dscp; |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 467 | uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4); |
Steven Luong | bf12efc | 2022-07-25 09:29:23 -0700 | [diff] [blame] | 468 | if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX) |
| 469 | uc->sw_if_index = rmt->peer.sw_if_index; |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 470 | uc->flags |= UDP_CONN_F_OWNS_PORT; |
Florin Coras | 87b7e3d | 2020-03-27 15:06:07 +0000 | [diff] [blame] | 471 | if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED) |
Florin Coras | 0ac5782 | 2021-03-03 08:06:12 -0800 | [diff] [blame] | 472 | { |
| 473 | uc->flags |= UDP_CONN_F_CONNECTED; |
| 474 | } |
Florin Coras | 87b7e3d | 2020-03-27 15:06:07 +0000 | [diff] [blame] | 475 | else |
Florin Coras | 0ac5782 | 2021-03-03 08:06:12 -0800 | [diff] [blame] | 476 | { |
| 477 | clib_spinlock_init (&uc->rx_lock); |
| 478 | uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS; |
| 479 | } |
Florin Coras | f8ee39f | 2022-10-18 18:37:56 -0700 | [diff] [blame] | 480 | if (!um->csum_offload) |
| 481 | uc->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD; |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 482 | uc->next_node_index = rmt->next_node_index; |
| 483 | uc->next_node_opaque = rmt->next_node_opaque; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 484 | |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 485 | udp_connection_register_port (uc->c_lcl_port, rmt->is_ip4); |
| 486 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 487 | return uc->c_c_index; |
| 488 | } |
| 489 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 490 | static transport_connection_t * |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 491 | udp_session_get_half_open (u32 conn_index) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 492 | { |
| 493 | udp_connection_t *uc; |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 494 | u32 thread_index; |
| 495 | |
| 496 | /* We don't poll main thread if we have workers */ |
Florin Coras | fb50bc3 | 2021-05-18 00:28:59 -0700 | [diff] [blame] | 497 | thread_index = transport_cl_thread (); |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 498 | uc = udp_connection_get (conn_index, thread_index); |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 499 | if (!uc) |
| 500 | return 0; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 501 | return &uc->connection; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 502 | } |
| 503 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 504 | static u8 * |
| 505 | format_udp_session (u8 * s, va_list * args) |
| 506 | { |
| 507 | u32 uci = va_arg (*args, u32); |
| 508 | u32 thread_index = va_arg (*args, u32); |
| 509 | u32 verbose = va_arg (*args, u32); |
| 510 | udp_connection_t *uc; |
| 511 | |
| 512 | uc = udp_connection_get (uci, thread_index); |
| 513 | return format (s, "%U", format_udp_connection, uc, verbose); |
| 514 | } |
| 515 | |
| 516 | static u8 * |
| 517 | format_udp_half_open_session (u8 * s, va_list * args) |
| 518 | { |
| 519 | u32 __clib_unused tci = va_arg (*args, u32); |
| 520 | u32 __clib_unused thread_index = va_arg (*args, u32); |
| 521 | clib_warning ("BUG"); |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static u8 * |
| 526 | format_udp_listener_session (u8 * s, va_list * args) |
| 527 | { |
| 528 | u32 tci = va_arg (*args, u32); |
| 529 | u32 __clib_unused thread_index = va_arg (*args, u32); |
| 530 | u32 verbose = va_arg (*args, u32); |
| 531 | udp_connection_t *uc = udp_listener_get (tci); |
| 532 | return format (s, "%U", format_udp_connection, uc, verbose); |
| 533 | } |
| 534 | |
Florin Coras | 797562c | 2022-11-18 18:29:23 -0800 | [diff] [blame] | 535 | static void |
| 536 | udp_realloc_ports_sv (u16 **ports_nh_svp) |
| 537 | { |
| 538 | u16 port, port_no, *ports_nh_sv, *mc; |
| 539 | u32 *ports = 0, *nh = 0, msum, i; |
| 540 | sparse_vec_header_t *h; |
| 541 | uword sv_index, *mb; |
| 542 | |
| 543 | ports_nh_sv = *ports_nh_svp; |
| 544 | |
| 545 | for (port = 1; port < 65535; port++) |
| 546 | { |
| 547 | port_no = clib_host_to_net_u16 (port); |
| 548 | |
| 549 | sv_index = sparse_vec_index (ports_nh_sv, port_no); |
| 550 | if (sv_index != SPARSE_VEC_INVALID_INDEX) |
| 551 | { |
| 552 | vec_add1 (ports, port_no); |
| 553 | vec_add1 (nh, ports_nh_sv[sv_index]); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | sparse_vec_free (ports_nh_sv); |
| 558 | |
| 559 | ports_nh_sv = |
| 560 | sparse_vec_new (/* elt bytes */ sizeof (ports_nh_sv[0]), |
| 561 | /* bits in index */ BITS (((udp_header_t *) 0)->dst_port)); |
| 562 | |
| 563 | vec_resize (ports_nh_sv, 65535); |
| 564 | |
| 565 | for (port = 1; port < 65535; port++) |
Florin Coras | e1f2058 | 2022-11-11 11:37:36 -0800 | [diff] [blame] | 566 | ports_nh_sv[port] = UDP_NO_NODE_SET; |
Florin Coras | 797562c | 2022-11-18 18:29:23 -0800 | [diff] [blame] | 567 | |
| 568 | for (i = 0; i < vec_len (ports); i++) |
| 569 | ports_nh_sv[ports[i]] = nh[i]; |
| 570 | |
| 571 | h = sparse_vec_header (ports_nh_sv); |
| 572 | vec_foreach (mb, h->is_member_bitmap) |
| 573 | *mb = (uword) ~0; |
| 574 | |
| 575 | msum = 0; |
| 576 | vec_foreach (mc, h->member_counts) |
| 577 | { |
| 578 | *mc = msum; |
| 579 | msum += msum == 0 ? 63 : 64; |
| 580 | } |
| 581 | |
| 582 | vec_free (ports); |
| 583 | vec_free (nh); |
| 584 | |
| 585 | *ports_nh_svp = ports_nh_sv; |
| 586 | } |
| 587 | |
| 588 | static clib_error_t * |
| 589 | udp_enable_disable (vlib_main_t *vm, u8 is_en) |
| 590 | { |
| 591 | udp_main_t *um = &udp_main; |
| 592 | |
Florin Coras | 75e8e1e | 2024-07-02 04:34:54 -0700 | [diff] [blame^] | 593 | if (!is_en || um->is_init) |
| 594 | return 0; |
| 595 | |
Florin Coras | 797562c | 2022-11-18 18:29:23 -0800 | [diff] [blame] | 596 | /* Not ideal. The sparse vector used to map ports to next nodes assumes |
| 597 | * only a few ports are ever used. When udp transport is enabled this does |
| 598 | * not hold and, to make matters worse, ports are consumed in a random |
| 599 | * order. |
| 600 | * |
| 601 | * This can lead to a lot of slow updates to internal data structures |
| 602 | * which in turn can slow udp connection allocations until all ports are |
| 603 | * eventually consumed. |
| 604 | * |
| 605 | * Consequently, reallocate sparse vector, preallocate all ports and have |
| 606 | * them point to UDP_NO_NODE_SET. We could consider switching the sparse |
| 607 | * vector to a preallocated vector but that would increase memory |
| 608 | * consumption for vpp deployments that do not rely on host stack. |
| 609 | */ |
| 610 | |
| 611 | udp_realloc_ports_sv (&um->next_by_dst_port4); |
| 612 | udp_realloc_ports_sv (&um->next_by_dst_port6); |
| 613 | |
Florin Coras | 9bc72ac | 2023-01-09 12:46:07 -0800 | [diff] [blame] | 614 | vec_validate (um->transport_ports_refcnt[0], 65535); |
| 615 | vec_validate (um->transport_ports_refcnt[1], 65535); |
Florin Coras | 75e8e1e | 2024-07-02 04:34:54 -0700 | [diff] [blame^] | 616 | um->is_init = 1; |
Florin Coras | 9bc72ac | 2023-01-09 12:46:07 -0800 | [diff] [blame] | 617 | |
Florin Coras | 797562c | 2022-11-18 18:29:23 -0800 | [diff] [blame] | 618 | return 0; |
| 619 | } |
| 620 | |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 621 | static const transport_proto_vft_t udp_proto = { |
Florin Coras | 797562c | 2022-11-18 18:29:23 -0800 | [diff] [blame] | 622 | .enable = udp_enable_disable, |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 623 | .start_listen = udp_session_bind, |
| 624 | .connect = udp_open_connection, |
| 625 | .stop_listen = udp_session_unbind, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 626 | .push_header = udp_push_header, |
| 627 | .get_connection = udp_session_get, |
| 628 | .get_listener = udp_session_get_listener, |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 629 | .get_half_open = udp_session_get_half_open, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 630 | .close = udp_session_close, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 631 | .cleanup = udp_session_cleanup, |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 632 | .send_params = udp_session_send_params, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 633 | .format_connection = format_udp_session, |
| 634 | .format_half_open = format_udp_half_open_session, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 635 | .format_listener = format_udp_listener_session, |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 636 | .transport_options = { |
Florin Coras | 07063b8 | 2020-03-13 04:44:51 +0000 | [diff] [blame] | 637 | .name = "udp", |
| 638 | .short_name = "U", |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 639 | .tx_type = TRANSPORT_TX_DGRAM, |
| 640 | .service_type = TRANSPORT_SERVICE_CL, |
| 641 | }, |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 642 | }; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 643 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 644 | static clib_error_t * |
| 645 | udp_init (vlib_main_t * vm) |
| 646 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 647 | udp_main_t *um = vnet_get_udp_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 648 | ip_main_t *im = &ip_main; |
| 649 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 650 | u32 num_threads; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 651 | ip_protocol_info_t *pi; |
| 652 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 653 | /* |
| 654 | * Registrations |
| 655 | */ |
| 656 | |
| 657 | /* IP registration */ |
| 658 | pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP); |
| 659 | if (pi == 0) |
| 660 | return clib_error_return (0, "UDP protocol info AWOL"); |
| 661 | pi->format_header = format_udp_header; |
| 662 | pi->unformat_pg_edit = unformat_pg_udp_header; |
| 663 | |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 664 | /* Register as transport with session layer */ |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 665 | transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto, |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 666 | FIB_PROTOCOL_IP4, udp4_output_node.index); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 667 | transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto, |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame] | 668 | FIB_PROTOCOL_IP6, udp6_output_node.index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 669 | |
| 670 | /* |
| 671 | * Initialize data structures |
| 672 | */ |
| 673 | |
| 674 | num_threads = 1 /* main thread */ + tm->n_threads; |
Florin Coras | 1c29dfb | 2022-10-24 18:46:20 -0700 | [diff] [blame] | 675 | vec_validate (um->wrk, num_threads - 1); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 676 | |
| 677 | um->local_to_input_edge[UDP_IP4] = |
| 678 | vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index); |
| 679 | um->local_to_input_edge[UDP_IP6] = |
| 680 | vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index); |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 681 | |
| 682 | um->default_mtu = 1500; |
Florin Coras | f8ee39f | 2022-10-18 18:37:56 -0700 | [diff] [blame] | 683 | um->csum_offload = 1; |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 684 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 685 | } |
| 686 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 687 | VLIB_INIT_FUNCTION (udp_init) = |
| 688 | { |
| 689 | .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init", |
| 690 | "ip6_lookup_init"), |
| 691 | }; |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 692 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 693 | /* |
| 694 | * fd.io coding-style-patch-verification: ON |
| 695 | * |
| 696 | * Local Variables: |
| 697 | * eval: (c-set-style "gnu") |
| 698 | * End: |
| 699 | */ |