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; |
| 29 | udp_dst_port_info_t *pi; |
| 30 | u16 *n; |
| 31 | |
| 32 | pi = udp_get_dst_port_info (um, lcl_port, is_ip4); |
| 33 | if (!pi) |
| 34 | { |
| 35 | udp_add_dst_port (um, lcl_port, 0, is_ip4); |
| 36 | pi = udp_get_dst_port_info (um, lcl_port, is_ip4); |
| 37 | pi->n_connections = 1; |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | pi->n_connections += 1; |
| 42 | /* Do not return. The fact that the pi is valid does not mean |
| 43 | * it's up to date */ |
| 44 | } |
| 45 | |
| 46 | pi->node_index = is_ip4 ? udp4_input_node.index : udp6_input_node.index; |
| 47 | pi->next_index = um->local_to_input_edge[is_ip4]; |
| 48 | |
| 49 | /* Setup udp protocol -> next index sparse vector mapping. */ |
| 50 | if (is_ip4) |
| 51 | n = sparse_vec_validate (um->next_by_dst_port4, |
| 52 | clib_host_to_net_u16 (lcl_port)); |
| 53 | else |
| 54 | n = sparse_vec_validate (um->next_by_dst_port6, |
| 55 | clib_host_to_net_u16 (lcl_port)); |
| 56 | |
| 57 | n[0] = pi->next_index; |
| 58 | } |
| 59 | |
| 60 | static void |
| 61 | udp_connection_unregister_port (u16 lcl_port, u8 is_ip4) |
| 62 | { |
| 63 | udp_main_t *um = &udp_main; |
| 64 | udp_dst_port_info_t *pi; |
| 65 | |
| 66 | pi = udp_get_dst_port_info (um, lcl_port, is_ip4); |
| 67 | if (!pi) |
| 68 | return; |
| 69 | |
| 70 | if (!pi->n_connections) |
| 71 | { |
| 72 | clib_warning ("no connections using port %u", lcl_port); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | if (!clib_atomic_sub_fetch (&pi->n_connections, 1)) |
| 77 | udp_unregister_dst_port (0, lcl_port, is_ip4); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | udp_connection_share_port (u16 lcl_port, u8 is_ip4) |
| 82 | { |
| 83 | udp_main_t *um = &udp_main; |
| 84 | udp_dst_port_info_t *pi; |
| 85 | |
| 86 | /* Done without a lock but the operation is atomic. Writers to pi hash |
| 87 | * table and vector should be guarded by a barrier sync */ |
| 88 | pi = udp_get_dst_port_info (um, lcl_port, is_ip4); |
| 89 | clib_atomic_fetch_add_rel (&pi->n_connections, 1); |
| 90 | } |
| 91 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 92 | udp_connection_t * |
| 93 | udp_connection_alloc (u32 thread_index) |
| 94 | { |
| 95 | udp_main_t *um = &udp_main; |
| 96 | udp_connection_t *uc; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 97 | |
Florin Coras | a2b358b | 2022-03-18 12:50:03 -0700 | [diff] [blame] | 98 | pool_get_aligned_safe (um->connections[thread_index], uc, |
| 99 | CLIB_CACHE_LINE_BYTES); |
| 100 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 101 | clib_memset (uc, 0, sizeof (*uc)); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 102 | uc->c_c_index = uc - um->connections[thread_index]; |
| 103 | uc->c_thread_index = thread_index; |
| 104 | uc->c_proto = TRANSPORT_PROTO_UDP; |
| 105 | return uc; |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | udp_connection_free (udp_connection_t * uc) |
| 110 | { |
Benoît Ganne | d4aeb84 | 2019-07-18 18:38:42 +0200 | [diff] [blame] | 111 | u32 thread_index = uc->c_thread_index; |
Florin Coras | 30fdf39 | 2020-12-02 21:14:56 -0800 | [diff] [blame] | 112 | clib_spinlock_free (&uc->rx_lock); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 113 | if (CLIB_DEBUG) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 114 | clib_memset (uc, 0xFA, sizeof (*uc)); |
Benoît Ganne | d4aeb84 | 2019-07-18 18:38:42 +0200 | [diff] [blame] | 115 | pool_put (udp_main.connections[thread_index], uc); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 116 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 117 | |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 118 | static void |
| 119 | udp_connection_cleanup (udp_connection_t * uc) |
| 120 | { |
| 121 | transport_endpoint_cleanup (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip, |
| 122 | uc->c_lcl_port); |
| 123 | udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port), |
| 124 | uc->c_is_ip4); |
| 125 | udp_connection_free (uc); |
| 126 | } |
| 127 | |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 128 | void |
| 129 | udp_connection_delete (udp_connection_t * uc) |
| 130 | { |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 131 | session_transport_delete_notify (&uc->connection); |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 132 | udp_connection_cleanup (uc); |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 135 | static u8 |
| 136 | udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4) |
| 137 | { |
| 138 | udp_main_t *um = vnet_get_udp_main (); |
| 139 | udp_dst_port_info_t *pi; |
| 140 | |
| 141 | pi = udp_get_dst_port_info (um, lcl_port, is_ip4); |
| 142 | return (pi && !pi->n_connections |
| 143 | && udp_is_valid_dst_port (lcl_port, is_ip4)); |
| 144 | } |
| 145 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 146 | static u16 |
| 147 | udp_default_mtu (udp_main_t * um, u8 is_ip4) |
| 148 | { |
| 149 | u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t); |
| 150 | return (um->default_mtu - sizeof (udp_header_t) - ip_hlen); |
| 151 | } |
| 152 | |
| 153 | static u32 |
Florin Coras | 0bce71e | 2022-02-10 11:57:06 -0800 | [diff] [blame] | 154 | udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 155 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 156 | udp_main_t *um = vnet_get_udp_main (); |
Florin Coras | 87b7e3d | 2020-03-27 15:06:07 +0000 | [diff] [blame] | 157 | transport_endpoint_cfg_t *lcl_ext; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 158 | udp_connection_t *listener; |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 159 | u16 lcl_port_ho; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 160 | void *iface_ip; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 161 | |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 162 | lcl_port_ho = clib_net_to_host_u16 (lcl->port); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 163 | |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 164 | if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4)) |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 165 | { |
| 166 | clib_warning ("port already used"); |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 167 | return SESSION_E_PORTINUSE; |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 168 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 169 | |
| 170 | pool_get (um->listener_pool, listener); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 171 | clib_memset (listener, 0, sizeof (udp_connection_t)); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 172 | |
Florin Coras | 0e49568 | 2017-09-19 22:27:18 -0700 | [diff] [blame] | 173 | listener->c_lcl_port = lcl->port; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 174 | listener->c_c_index = listener - um->listener_pool; |
| 175 | |
| 176 | /* If we are provided a sw_if_index, bind using one of its ips */ |
| 177 | if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX) |
| 178 | { |
| 179 | if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index, |
| 180 | lcl->is_ip4))) |
| 181 | ip_set (&lcl->ip, iface_ip, lcl->is_ip4); |
| 182 | } |
| 183 | ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4); |
| 184 | listener->c_is_ip4 = lcl->is_ip4; |
| 185 | listener->c_proto = TRANSPORT_PROTO_UDP; |
| 186 | listener->c_s_index = session_index; |
| 187 | listener->c_fib_index = lcl->fib_index; |
Steven Luong | bf12efc | 2022-07-25 09:29:23 -0700 | [diff] [blame] | 188 | listener->mss = |
| 189 | lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4); |
Florin Coras | 9c4ec6f | 2020-04-01 02:50:13 +0000 | [diff] [blame] | 190 | listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN; |
Florin Coras | 87b7e3d | 2020-03-27 15:06:07 +0000 | [diff] [blame] | 191 | lcl_ext = (transport_endpoint_cfg_t *) lcl; |
| 192 | if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED) |
| 193 | listener->flags |= UDP_CONN_F_CONNECTED; |
| 194 | else |
| 195 | listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 196 | clib_spinlock_init (&listener->rx_lock); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 197 | |
Florin Coras | c8e4110 | 2022-03-18 10:27:29 -0700 | [diff] [blame] | 198 | udp_connection_register_port (lcl_port_ho, lcl->is_ip4); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 199 | return listener->c_c_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 202 | static u32 |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 203 | udp_session_unbind (u32 listener_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 204 | { |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 205 | udp_main_t *um = &udp_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 206 | udp_connection_t *listener; |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 207 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 208 | listener = udp_listener_get (listener_index); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 209 | udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port), |
| 210 | listener->c_is_ip4); |
Florin Coras | 30fdf39 | 2020-12-02 21:14:56 -0800 | [diff] [blame] | 211 | clib_spinlock_free (&listener->rx_lock); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 212 | pool_put (um->listener_pool, listener); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 216 | static transport_connection_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 217 | udp_session_get_listener (u32 listener_index) |
| 218 | { |
| 219 | udp_connection_t *us; |
| 220 | |
| 221 | us = udp_listener_get (listener_index); |
| 222 | return &us->connection; |
| 223 | } |
| 224 | |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 225 | always_inline u32 |
| 226 | udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 227 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 228 | vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 229 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame^] | 230 | /* reuse tcp medatada for now */ |
| 231 | vnet_buffer (b)->tcp.connection_index = uc->c_c_index; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 232 | |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static u32 |
| 237 | udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs) |
| 238 | { |
| 239 | vlib_main_t *vm = vlib_get_main (); |
| 240 | udp_connection_t *uc; |
| 241 | |
| 242 | uc = udp_connection_from_transport (tc); |
| 243 | |
| 244 | while (n_bufs >= 4) |
| 245 | { |
| 246 | vlib_prefetch_buffer_header (bs[2], STORE); |
| 247 | vlib_prefetch_buffer_header (bs[3], STORE); |
| 248 | |
| 249 | udp_push_one_header (vm, uc, bs[0]); |
| 250 | udp_push_one_header (vm, uc, bs[1]); |
| 251 | |
| 252 | n_bufs -= 2; |
| 253 | bs += 2; |
| 254 | } |
| 255 | while (n_bufs) |
| 256 | { |
| 257 | if (n_bufs > 1) |
| 258 | vlib_prefetch_buffer_header (bs[1], STORE); |
| 259 | |
| 260 | udp_push_one_header (vm, uc, bs[0]); |
| 261 | |
| 262 | n_bufs -= 1; |
| 263 | bs += 1; |
| 264 | } |
| 265 | |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 266 | if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING)) |
| 267 | { |
| 268 | if (!transport_max_tx_dequeue (&uc->connection)) |
| 269 | udp_connection_delete (uc); |
| 270 | } |
| 271 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 272 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 273 | } |
| 274 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 275 | static transport_connection_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 276 | udp_session_get (u32 connection_index, u32 thread_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 277 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 278 | udp_connection_t *uc; |
| 279 | uc = udp_connection_get (connection_index, thread_index); |
| 280 | if (uc) |
| 281 | return &uc->connection; |
| 282 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 285 | static void |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 286 | udp_session_close (u32 connection_index, u32 thread_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 287 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 288 | udp_connection_t *uc; |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 289 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 290 | uc = udp_connection_get (connection_index, thread_index); |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 291 | if (!uc || (uc->flags & UDP_CONN_F_MIGRATED)) |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 292 | return; |
| 293 | |
| 294 | if (!transport_max_tx_dequeue (&uc->connection)) |
| 295 | udp_connection_delete (uc); |
| 296 | else |
| 297 | uc->flags |= UDP_CONN_F_CLOSING; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 300 | static void |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 301 | udp_session_cleanup (u32 connection_index, u32 thread_index) |
| 302 | { |
| 303 | udp_connection_t *uc; |
| 304 | uc = udp_connection_get (connection_index, thread_index); |
Florin Coras | d85666f | 2020-04-03 00:58:48 +0000 | [diff] [blame] | 305 | if (!uc) |
| 306 | return; |
| 307 | if (uc->flags & UDP_CONN_F_MIGRATED) |
| 308 | udp_connection_free (uc); |
| 309 | else |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 310 | udp_connection_cleanup (uc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 313 | static int |
| 314 | udp_session_send_params (transport_connection_t * tconn, |
| 315 | transport_send_params_t * sp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 316 | { |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 317 | udp_connection_t *uc; |
| 318 | |
Florin Coras | e759bb5 | 2020-04-08 01:55:39 +0000 | [diff] [blame] | 319 | uc = udp_connection_from_transport (tconn); |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 320 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 321 | /* No constraint on TX window */ |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 322 | sp->snd_space = ~0; |
| 323 | /* TODO figure out MTU of output interface */ |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 324 | sp->snd_mss = uc->mss; |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 325 | sp->tx_offset = 0; |
| 326 | sp->flags = 0; |
| 327 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 328 | } |
| 329 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 330 | static int |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 331 | udp_open_connection (transport_endpoint_cfg_t * rmt) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 332 | { |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 333 | udp_main_t *um = &udp_main; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 334 | ip46_address_t lcl_addr; |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 335 | udp_connection_t *uc; |
Florin Coras | c8e4110 | 2022-03-18 10:27:29 -0700 | [diff] [blame] | 336 | u32 thread_index; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 337 | u16 lcl_port; |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 338 | int rv; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 339 | |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 340 | rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr, |
| 341 | &lcl_port); |
| 342 | if (rv) |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 343 | { |
| 344 | if (rv != SESSION_E_PORTINUSE) |
| 345 | return rv; |
| 346 | |
| 347 | if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4)) |
| 348 | return SESSION_E_PORTINUSE; |
| 349 | |
| 350 | /* If port in use, check if 5-tuple is also in use */ |
| 351 | if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip, |
| 352 | lcl_port, rmt->port, TRANSPORT_PROTO_UDP, |
| 353 | rmt->is_ip4)) |
| 354 | return SESSION_E_PORTINUSE; |
| 355 | |
| 356 | /* 5-tuple is available so increase lcl endpoint refcount and proceed |
| 357 | * with connection allocation */ |
| 358 | transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr, |
| 359 | lcl_port); |
| 360 | goto conn_alloc; |
| 361 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 362 | |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 363 | if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 364 | { |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 365 | /* If specific source port was requested abort */ |
| 366 | if (rmt->peer.port) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 367 | return SESSION_E_PORTINUSE; |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 368 | |
| 369 | /* Try to find a port that's not used */ |
| 370 | while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 371 | { |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 372 | lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP, |
| 373 | &lcl_addr); |
| 374 | if (lcl_port < 1) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 375 | return SESSION_E_PORTINUSE; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 379 | conn_alloc: |
| 380 | |
Florin Coras | c8e4110 | 2022-03-18 10:27:29 -0700 | [diff] [blame] | 381 | udp_connection_register_port (lcl_port, rmt->is_ip4); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 382 | |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 383 | /* We don't poll main thread if we have workers */ |
Florin Coras | fb50bc3 | 2021-05-18 00:28:59 -0700 | [diff] [blame] | 384 | thread_index = transport_cl_thread (); |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 385 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 386 | uc = udp_connection_alloc (thread_index); |
| 387 | ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4); |
| 388 | ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4); |
| 389 | uc->c_rmt_port = rmt->port; |
| 390 | uc->c_lcl_port = clib_host_to_net_u16 (lcl_port); |
| 391 | uc->c_is_ip4 = rmt->is_ip4; |
| 392 | uc->c_proto = TRANSPORT_PROTO_UDP; |
| 393 | uc->c_fib_index = rmt->fib_index; |
Filip Tehlar | 3ef8bf3 | 2021-10-21 14:07:31 +0000 | [diff] [blame] | 394 | uc->c_dscp = rmt->dscp; |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 395 | 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] | 396 | if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX) |
| 397 | uc->sw_if_index = rmt->peer.sw_if_index; |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 398 | uc->flags |= UDP_CONN_F_OWNS_PORT; |
Florin Coras | 87b7e3d | 2020-03-27 15:06:07 +0000 | [diff] [blame] | 399 | if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED) |
Florin Coras | 0ac5782 | 2021-03-03 08:06:12 -0800 | [diff] [blame] | 400 | { |
| 401 | uc->flags |= UDP_CONN_F_CONNECTED; |
| 402 | } |
Florin Coras | 87b7e3d | 2020-03-27 15:06:07 +0000 | [diff] [blame] | 403 | else |
Florin Coras | 0ac5782 | 2021-03-03 08:06:12 -0800 | [diff] [blame] | 404 | { |
| 405 | clib_spinlock_init (&uc->rx_lock); |
| 406 | uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS; |
| 407 | } |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame^] | 408 | uc->next_node_index = rmt->next_node_index; |
| 409 | uc->next_node_opaque = rmt->next_node_opaque; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 410 | |
| 411 | return uc->c_c_index; |
| 412 | } |
| 413 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 414 | static transport_connection_t * |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 415 | udp_session_get_half_open (u32 conn_index) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 416 | { |
| 417 | udp_connection_t *uc; |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 418 | u32 thread_index; |
| 419 | |
| 420 | /* We don't poll main thread if we have workers */ |
Florin Coras | fb50bc3 | 2021-05-18 00:28:59 -0700 | [diff] [blame] | 421 | thread_index = transport_cl_thread (); |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 422 | uc = udp_connection_get (conn_index, thread_index); |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 423 | if (!uc) |
| 424 | return 0; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 425 | return &uc->connection; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 426 | } |
| 427 | |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 428 | static u8 * |
| 429 | format_udp_session (u8 * s, va_list * args) |
| 430 | { |
| 431 | u32 uci = va_arg (*args, u32); |
| 432 | u32 thread_index = va_arg (*args, u32); |
| 433 | u32 verbose = va_arg (*args, u32); |
| 434 | udp_connection_t *uc; |
| 435 | |
| 436 | uc = udp_connection_get (uci, thread_index); |
| 437 | return format (s, "%U", format_udp_connection, uc, verbose); |
| 438 | } |
| 439 | |
| 440 | static u8 * |
| 441 | format_udp_half_open_session (u8 * s, va_list * args) |
| 442 | { |
| 443 | u32 __clib_unused tci = va_arg (*args, u32); |
| 444 | u32 __clib_unused thread_index = va_arg (*args, u32); |
| 445 | clib_warning ("BUG"); |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static u8 * |
| 450 | format_udp_listener_session (u8 * s, va_list * args) |
| 451 | { |
| 452 | u32 tci = va_arg (*args, u32); |
| 453 | u32 __clib_unused thread_index = va_arg (*args, u32); |
| 454 | u32 verbose = va_arg (*args, u32); |
| 455 | udp_connection_t *uc = udp_listener_get (tci); |
| 456 | return format (s, "%U", format_udp_connection, uc, verbose); |
| 457 | } |
| 458 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 459 | /* *INDENT-OFF* */ |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 460 | static const transport_proto_vft_t udp_proto = { |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 461 | .start_listen = udp_session_bind, |
| 462 | .connect = udp_open_connection, |
| 463 | .stop_listen = udp_session_unbind, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 464 | .push_header = udp_push_header, |
| 465 | .get_connection = udp_session_get, |
| 466 | .get_listener = udp_session_get_listener, |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 467 | .get_half_open = udp_session_get_half_open, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 468 | .close = udp_session_close, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 469 | .cleanup = udp_session_cleanup, |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 470 | .send_params = udp_session_send_params, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 471 | .format_connection = format_udp_session, |
| 472 | .format_half_open = format_udp_half_open_session, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 473 | .format_listener = format_udp_listener_session, |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 474 | .transport_options = { |
Florin Coras | 07063b8 | 2020-03-13 04:44:51 +0000 | [diff] [blame] | 475 | .name = "udp", |
| 476 | .short_name = "U", |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 477 | .tx_type = TRANSPORT_TX_DGRAM, |
| 478 | .service_type = TRANSPORT_SERVICE_CL, |
| 479 | }, |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 480 | }; |
| 481 | /* *INDENT-ON* */ |
| 482 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 483 | static clib_error_t * |
| 484 | udp_init (vlib_main_t * vm) |
| 485 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 486 | udp_main_t *um = vnet_get_udp_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 487 | ip_main_t *im = &ip_main; |
| 488 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 489 | u32 num_threads; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 490 | ip_protocol_info_t *pi; |
| 491 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 492 | /* |
| 493 | * Registrations |
| 494 | */ |
| 495 | |
| 496 | /* IP registration */ |
| 497 | pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP); |
| 498 | if (pi == 0) |
| 499 | return clib_error_return (0, "UDP protocol info AWOL"); |
| 500 | pi->format_header = format_udp_header; |
| 501 | pi->unformat_pg_edit = unformat_pg_udp_header; |
| 502 | |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame^] | 503 | /* Register as transport with session layer */ |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 504 | transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto, |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame^] | 505 | FIB_PROTOCOL_IP4, udp4_output_node.index); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 506 | transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto, |
Florin Coras | 8c1be05 | 2022-10-17 11:52:34 -0700 | [diff] [blame^] | 507 | FIB_PROTOCOL_IP6, udp6_output_node.index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 508 | |
| 509 | /* |
| 510 | * Initialize data structures |
| 511 | */ |
| 512 | |
| 513 | num_threads = 1 /* main thread */ + tm->n_threads; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 514 | vec_validate (um->connections, num_threads - 1); |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 515 | |
| 516 | um->local_to_input_edge[UDP_IP4] = |
| 517 | vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index); |
| 518 | um->local_to_input_edge[UDP_IP6] = |
| 519 | 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] | 520 | |
| 521 | um->default_mtu = 1500; |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 522 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 525 | /* *INDENT-OFF* */ |
| 526 | VLIB_INIT_FUNCTION (udp_init) = |
| 527 | { |
| 528 | .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init", |
| 529 | "ip6_lookup_init"), |
| 530 | }; |
| 531 | /* *INDENT-ON* */ |
| 532 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 533 | /* |
| 534 | * fd.io coding-style-patch-verification: ON |
| 535 | * |
| 536 | * Local Variables: |
| 537 | * eval: (c-set-style "gnu") |
| 538 | * End: |
| 539 | */ |