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