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