Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Cisco and/or its affiliates. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [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 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 15 | #ifndef __included_udp_h__ |
| 16 | #define __included_udp_h__ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | |
| 18 | #include <vnet/vnet.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 19 | #include <vnet/udp/udp_inlines.h> |
| 20 | #include <vnet/udp/udp_local.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 21 | #include <vnet/udp/udp_packet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | #include <vnet/ip/ip4_packet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | #include <vnet/ip/format.h> |
| 24 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 25 | #include <vnet/ip/ip.h> |
| 26 | #include <vnet/session/transport.h> |
| 27 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 28 | typedef enum |
| 29 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | #define udp_error(n,s) UDP_ERROR_##n, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 31 | #include <vnet/udp/udp_error.def> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | #undef udp_error |
| 33 | UDP_N_ERROR, |
| 34 | } udp_error_t; |
| 35 | |
Florin Coras | 9c4ec6f | 2020-04-01 02:50:13 +0000 | [diff] [blame] | 36 | #define foreach_udp_connection_flag \ |
| 37 | _(CONNECTED, "CONNECTED") /**< connected mode */ \ |
| 38 | _(OWNS_PORT, "OWNS_PORT") /**< port belong to conn (UDPC) */ \ |
| 39 | _(CLOSING, "CLOSING") /**< conn closed with data */ \ |
| 40 | _(LISTEN, "LISTEN") /**< conn is listening */ \ |
Florin Coras | d85666f | 2020-04-03 00:58:48 +0000 | [diff] [blame] | 41 | _(MIGRATED, "MIGRATED") /**< cloned to another thread */ \ |
Florin Coras | 9c4ec6f | 2020-04-01 02:50:13 +0000 | [diff] [blame] | 42 | |
| 43 | enum udp_conn_flags_bits |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 44 | { |
Florin Coras | 9c4ec6f | 2020-04-01 02:50:13 +0000 | [diff] [blame] | 45 | #define _(sym, str) UDP_CONN_F_BIT_##sym, |
| 46 | foreach_udp_connection_flag |
| 47 | #undef _ |
| 48 | UDP_CONN_N_FLAGS |
| 49 | }; |
| 50 | |
| 51 | typedef enum udp_conn_flags_ |
| 52 | { |
| 53 | #define _(sym, str) UDP_CONN_F_##sym = 1 << UDP_CONN_F_BIT_##sym, |
| 54 | foreach_udp_connection_flag |
| 55 | #undef _ |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 56 | } udp_conn_flags_t; |
| 57 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 58 | typedef struct |
| 59 | { |
Dave Barach | eb987d3 | 2018-05-03 08:26:39 -0400 | [diff] [blame] | 60 | /** Required for pool_get_aligned */ |
| 61 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 62 | transport_connection_t connection; /**< must be first */ |
| 63 | clib_spinlock_t rx_lock; /**< rx fifo lock */ |
Florin Coras | c754239 | 2019-07-22 08:08:43 -0700 | [diff] [blame] | 64 | u8 flags; /**< connection flags */ |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 65 | u16 mss; /**< connection mss */ |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 66 | } udp_connection_t; |
| 67 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 68 | typedef struct |
| 69 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | /* Name (a c string). */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 71 | char *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 73 | /* Port number in host byte order. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | udp_dst_port_t dst_port; |
| 75 | |
| 76 | /* Node which handles this type. */ |
| 77 | u32 node_index; |
| 78 | |
| 79 | /* Next index for this type. */ |
| 80 | u32 next_index; |
Kingwel Xie | 4af4709 | 2018-10-26 23:42:15 -0400 | [diff] [blame] | 81 | |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 82 | /* UDP sessions refcount (not tunnels) */ |
| 83 | u32 n_connections; |
| 84 | |
Kingwel Xie | 4af4709 | 2018-10-26 23:42:15 -0400 | [diff] [blame] | 85 | /* Parser for packet generator edits for this protocol */ |
| 86 | unformat_function_t *unformat_pg_edit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | } udp_dst_port_info_t; |
| 88 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 89 | typedef enum |
| 90 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | UDP_IP6 = 0, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 92 | UDP_IP4, /* the code is full of is_ip4... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | N_UDP_AF, |
| 94 | } udp_af_t; |
| 95 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 96 | typedef struct |
| 97 | { |
| 98 | udp_dst_port_info_t *dst_port_infos[N_UDP_AF]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | |
| 100 | /* Hash tables mapping name/protocol to protocol info index. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 101 | uword *dst_port_info_by_name[N_UDP_AF]; |
| 102 | uword *dst_port_info_by_dst_port[N_UDP_AF]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | |
Damjan Marion | 615fc61 | 2017-04-03 14:56:08 +0200 | [diff] [blame] | 104 | /* Sparse vector mapping udp dst_port in network byte order |
| 105 | to next index. */ |
| 106 | u16 *next_by_dst_port4; |
| 107 | u16 *next_by_dst_port6; |
| 108 | u8 punt_unknown4; |
| 109 | u8 punt_unknown6; |
| 110 | |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 111 | /* Udp local to input arc index */ |
| 112 | u32 local_to_input_edge[N_UDP_AF]; |
| 113 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Per-worker thread udp connection pools used with session layer |
| 116 | */ |
| 117 | udp_connection_t **connections; |
| 118 | u32 *connection_peekers; |
| 119 | clib_spinlock_t *peekers_readers_locks; |
| 120 | clib_spinlock_t *peekers_write_locks; |
| 121 | udp_connection_t *listener_pool; |
| 122 | |
Florin Coras | ba78e23 | 2020-04-06 21:28:59 +0000 | [diff] [blame] | 123 | u16 default_mtu; |
Filip Tehlar | 3006289 | 2021-06-21 13:01:24 +0000 | [diff] [blame] | 124 | u16 msg_id_base; |
Florin Coras | 7743d6b | 2021-07-22 14:03:11 -0700 | [diff] [blame^] | 125 | |
| 126 | u8 icmp_send_unreachable_disabled; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | } udp_main_t; |
| 128 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 129 | extern udp_main_t udp_main; |
| 130 | extern vlib_node_registration_t udp4_input_node; |
| 131 | extern vlib_node_registration_t udp6_input_node; |
Filip Tehlar | 2c49ffe | 2019-03-06 07:16:08 -0800 | [diff] [blame] | 132 | extern vlib_node_registration_t udp4_local_node; |
| 133 | extern vlib_node_registration_t udp6_local_node; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 134 | |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 135 | void udp_add_dst_port (udp_main_t * um, udp_dst_port_t dst_port, |
| 136 | char *dst_port_name, u8 is_ip4); |
| 137 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 138 | always_inline udp_connection_t * |
| 139 | udp_connection_get (u32 conn_index, u32 thread_index) |
| 140 | { |
| 141 | if (pool_is_free_index (udp_main.connections[thread_index], conn_index)) |
| 142 | return 0; |
| 143 | return pool_elt_at_index (udp_main.connections[thread_index], conn_index); |
| 144 | } |
| 145 | |
| 146 | always_inline udp_connection_t * |
| 147 | udp_listener_get (u32 conn_index) |
| 148 | { |
| 149 | return pool_elt_at_index (udp_main.listener_pool, conn_index); |
| 150 | } |
| 151 | |
| 152 | always_inline udp_main_t * |
| 153 | vnet_get_udp_main () |
| 154 | { |
| 155 | return &udp_main; |
| 156 | } |
| 157 | |
| 158 | always_inline udp_connection_t * |
Florin Coras | e759bb5 | 2020-04-08 01:55:39 +0000 | [diff] [blame] | 159 | udp_connection_from_transport (transport_connection_t * tc) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 160 | { |
| 161 | return ((udp_connection_t *) tc); |
| 162 | } |
| 163 | |
| 164 | always_inline u32 |
| 165 | udp_connection_index (udp_connection_t * uc) |
| 166 | { |
| 167 | return (uc - udp_main.connections[uc->c_thread_index]); |
| 168 | } |
| 169 | |
Florin Coras | e759bb5 | 2020-04-08 01:55:39 +0000 | [diff] [blame] | 170 | void udp_connection_free (udp_connection_t * uc); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 171 | udp_connection_t *udp_connection_alloc (u32 thread_index); |
| 172 | |
| 173 | /** |
| 174 | * Acquires a lock that blocks a connection pool from expanding. |
| 175 | */ |
| 176 | always_inline void |
| 177 | udp_pool_add_peeker (u32 thread_index) |
| 178 | { |
| 179 | if (thread_index != vlib_get_thread_index ()) |
| 180 | return; |
| 181 | clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]); |
| 182 | udp_main.connection_peekers[thread_index] += 1; |
| 183 | if (udp_main.connection_peekers[thread_index] == 1) |
| 184 | clib_spinlock_lock_if_init (&udp_main.peekers_write_locks[thread_index]); |
| 185 | clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks |
| 186 | [thread_index]); |
| 187 | } |
| 188 | |
| 189 | always_inline void |
| 190 | udp_pool_remove_peeker (u32 thread_index) |
| 191 | { |
| 192 | if (thread_index != vlib_get_thread_index ()) |
| 193 | return; |
| 194 | ASSERT (udp_main.connection_peekers[thread_index] > 0); |
| 195 | clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]); |
| 196 | udp_main.connection_peekers[thread_index] -= 1; |
| 197 | if (udp_main.connection_peekers[thread_index] == 0) |
| 198 | clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks |
| 199 | [thread_index]); |
| 200 | clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks |
| 201 | [thread_index]); |
| 202 | } |
| 203 | |
| 204 | always_inline udp_connection_t * |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 205 | udp_connection_clone_safe (u32 connection_index, u32 thread_index) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 206 | { |
| 207 | udp_connection_t *old_c, *new_c; |
| 208 | u32 current_thread_index = vlib_get_thread_index (); |
| 209 | new_c = udp_connection_alloc (current_thread_index); |
| 210 | |
| 211 | /* If during the memcpy pool is reallocated AND the memory allocator |
| 212 | * decides to give the old chunk of memory to somebody in a hurry to |
| 213 | * scribble something on it, we have a problem. So add this thread as |
| 214 | * a session pool peeker. |
| 215 | */ |
| 216 | udp_pool_add_peeker (thread_index); |
| 217 | old_c = udp_main.connections[thread_index] + connection_index; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 218 | clib_memcpy_fast (new_c, old_c, sizeof (*new_c)); |
Florin Coras | d85666f | 2020-04-03 00:58:48 +0000 | [diff] [blame] | 219 | old_c->flags |= UDP_CONN_F_MIGRATED; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 220 | udp_pool_remove_peeker (thread_index); |
| 221 | new_c->c_thread_index = current_thread_index; |
| 222 | new_c->c_c_index = udp_connection_index (new_c); |
Nathan Skrzypczak | 161638f | 2019-05-13 16:25:50 +0200 | [diff] [blame] | 223 | new_c->c_fib_index = old_c->c_fib_index; |
Florin Coras | 30fdf39 | 2020-12-02 21:14:56 -0800 | [diff] [blame] | 224 | /* Assume cloned sessions don't need lock */ |
| 225 | new_c->rx_lock = 0; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 226 | return new_c; |
| 227 | } |
| 228 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | always_inline udp_dst_port_info_t * |
| 230 | udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4) |
| 231 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 232 | uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0; |
| 234 | } |
| 235 | |
| 236 | format_function_t format_udp_header; |
| 237 | format_function_t format_udp_rx_trace; |
Florin Coras | c98ef75 | 2020-04-07 17:30:13 +0000 | [diff] [blame] | 238 | format_function_t format_udp_connection; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | unformat_function_t unformat_udp_header; |
Elias Rudberg | 2dca180 | 2020-05-27 01:03:46 +0200 | [diff] [blame] | 240 | unformat_function_t unformat_udp_port; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | |
Florin Coras | a039620 | 2020-04-01 00:11:16 +0000 | [diff] [blame] | 242 | void udp_connection_share_port (u16 lcl_port, u8 is_ip4); |
| 243 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 244 | void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 245 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 246 | /* |
| 247 | * fd.io coding-style-patch-verification: ON |
| 248 | * |
| 249 | * Local Variables: |
| 250 | * eval: (c-set-style "gnu") |
| 251 | * End: |
| 252 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 253 | |
| 254 | #endif /* __included_udp_h__ */ |