blob: c6f867500e0c244586a88d0efe909faf990c7f5a [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Florin Corasc98ef752020-04-07 17:30:13 +00002 * Copyright (c) 2017-2020 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * 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 Barach68b0fb02017-02-28 15:15:56 -050015#ifndef __included_udp_h__
16#define __included_udp_h__
Ed Warnickecb9cada2015-12-08 15:45:58 -070017
18#include <vnet/vnet.h>
Florin Corasb040f982020-10-20 14:59:43 -070019#include <vnet/udp/udp_inlines.h>
20#include <vnet/udp/udp_local.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050021#include <vnet/udp/udp_packet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022#include <vnet/ip/ip4_packet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023#include <vnet/ip/format.h>
24
Dave Barach68b0fb02017-02-28 15:15:56 -050025#include <vnet/ip/ip.h>
26#include <vnet/session/transport.h>
27
Florin Corase1f20582022-11-11 11:37:36 -080028#define UDP_NO_NODE_SET ((u16) ~0)
29
Dave Barachd7cb1b52016-12-09 09:52:16 -050030typedef enum
31{
Filip Tehlar0c562172021-10-06 12:48:34 +000032#define udp_error(f, n, s, d) UDP_ERROR_##f,
Dave Barach68b0fb02017-02-28 15:15:56 -050033#include <vnet/udp/udp_error.def>
Ed Warnickecb9cada2015-12-08 15:45:58 -070034#undef udp_error
35 UDP_N_ERROR,
36} udp_error_t;
37
Florin Coras9c4ec6f2020-04-01 02:50:13 +000038#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 Corasd85666f2020-04-03 00:58:48 +000043 _(MIGRATED, "MIGRATED") /**< cloned to another thread */ \
Florin Coras9c4ec6f2020-04-01 02:50:13 +000044
45enum udp_conn_flags_bits
Florin Corasc7542392019-07-22 08:08:43 -070046{
Florin Coras9c4ec6f2020-04-01 02:50:13 +000047#define _(sym, str) UDP_CONN_F_BIT_##sym,
48 foreach_udp_connection_flag
49#undef _
50 UDP_CONN_N_FLAGS
51};
52
53typedef 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 Corasc7542392019-07-22 08:08:43 -070058} udp_conn_flags_t;
59
Florin Corasf8ee39f2022-10-18 18:37:56 -070060#define foreach_udp_cfg_flag _ (NO_CSUM_OFFLOAD, "no-csum-offload")
61
62typedef 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
70typedef 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 Coras3cbc04b2017-10-02 00:18:51 -070078typedef struct
79{
Dave Baracheb987d32018-05-03 08:26:39 -040080 /** Required for pool_get_aligned */
81 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Florin Coras7fb0fe12018-04-09 09:24:52 -070082 transport_connection_t connection; /**< must be first */
83 clib_spinlock_t rx_lock; /**< rx fifo lock */
Florin Corasc7542392019-07-22 08:08:43 -070084 u8 flags; /**< connection flags */
Florin Corasf8ee39f2022-10-18 18:37:56 -070085 udp_cfg_flags_t cfg_flags; /**< configuration flags */
Florin Corasba78e232020-04-06 21:28:59 +000086 u16 mss; /**< connection mss */
Steven Luongbf12efc2022-07-25 09:29:23 -070087 u32 sw_if_index; /**< connection sw_if_index */
Florin Coras8c1be052022-10-17 11:52:34 -070088 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 Coras3cbc04b2017-10-02 00:18:51 -070090} udp_connection_t;
91
Florin Corasf8ee39f2022-10-18 18:37:56 -070092#define udp_csum_offload(uc) (!((uc)->cfg_flags & UDP_CFG_F_NO_CSUM_OFFLOAD))
93
Dave Barachd7cb1b52016-12-09 09:52:16 -050094typedef struct
95{
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 /* Name (a c string). */
Dave Barachd7cb1b52016-12-09 09:52:16 -050097 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Florin Corasa0396202020-04-01 00:11:16 +000099 /* Port number in host byte order. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 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 Xie4af47092018-10-26 23:42:15 -0400107
108 /* Parser for packet generator edits for this protocol */
109 unformat_function_t *unformat_pg_edit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110} udp_dst_port_info_t;
111
Dave Barachd7cb1b52016-12-09 09:52:16 -0500112typedef enum
113{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 UDP_IP6 = 0,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500115 UDP_IP4, /* the code is full of is_ip4... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 N_UDP_AF,
117} udp_af_t;
118
Florin Coras1c29dfb2022-10-24 18:46:20 -0700119typedef struct udp_worker_
120{
Florin Coras4c89b182022-10-24 18:59:06 -0700121 udp_connection_t *connections;
Florin Coras1c29dfb2022-10-24 18:46:20 -0700122 u32 *pending_cleanups;
123} udp_worker_t;
124
Dave Barachd7cb1b52016-12-09 09:52:16 -0500125typedef struct
126{
127 udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128
129 /* Hash tables mapping name/protocol to protocol info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500130 uword *dst_port_info_by_name[N_UDP_AF];
131 uword *dst_port_info_by_dst_port[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
Damjan Marion615fc612017-04-03 14:56:08 +0200133 /* 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 Corasa0396202020-04-01 00:11:16 +0000140 /* Udp local to input arc index */
141 u32 local_to_input_edge[N_UDP_AF];
142
Florin Coras3cbc04b2017-10-02 00:18:51 -0700143 /*
Florin Coras4c89b182022-10-24 18:59:06 -0700144 * UDP transport layer per-thread context
Florin Coras3cbc04b2017-10-02 00:18:51 -0700145 */
Florin Coras1c29dfb2022-10-24 18:46:20 -0700146
Florin Coras1c29dfb2022-10-24 18:46:20 -0700147 udp_worker_t *wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700148 udp_connection_t *listener_pool;
149
Florin Coras9bc72ac2023-01-09 12:46:07 -0800150 /* Refcounts for ports consumed by udp transports to handle
151 * both passive and active opens using the same port */
152 u16 *transport_ports_refcnt[N_UDP_AF];
153
Florin Corasba78e232020-04-06 21:28:59 +0000154 u16 default_mtu;
Filip Tehlar30062892021-06-21 13:01:24 +0000155 u16 msg_id_base;
Florin Corasf8ee39f2022-10-18 18:37:56 -0700156 u8 csum_offload;
Florin Coras75e8e1e2024-07-02 04:34:54 -0700157 u8 is_init;
Florin Coras7743d6b2021-07-22 14:03:11 -0700158
159 u8 icmp_send_unreachable_disabled;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160} udp_main_t;
161
Florin Coras3cbc04b2017-10-02 00:18:51 -0700162extern udp_main_t udp_main;
163extern vlib_node_registration_t udp4_input_node;
164extern vlib_node_registration_t udp6_input_node;
Filip Tehlar2c49ffe2019-03-06 07:16:08 -0800165extern vlib_node_registration_t udp4_local_node;
166extern vlib_node_registration_t udp6_local_node;
Florin Coras8c1be052022-10-17 11:52:34 -0700167extern vlib_node_registration_t udp4_output_node;
168extern vlib_node_registration_t udp6_output_node;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700169
Florin Corasb040f982020-10-20 14:59:43 -0700170void udp_add_dst_port (udp_main_t * um, udp_dst_port_t dst_port,
171 char *dst_port_name, u8 is_ip4);
172
Florin Coras4c89b182022-10-24 18:59:06 -0700173always_inline udp_worker_t *
174udp_worker_get (u32 thread_index)
175{
176 return vec_elt_at_index (udp_main.wrk, thread_index);
177}
178
Florin Coras3cbc04b2017-10-02 00:18:51 -0700179always_inline udp_connection_t *
180udp_connection_get (u32 conn_index, u32 thread_index)
181{
Florin Coras4c89b182022-10-24 18:59:06 -0700182 udp_worker_t *wrk = udp_worker_get (thread_index);
183
184 if (pool_is_free_index (wrk->connections, conn_index))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700185 return 0;
Florin Coras4c89b182022-10-24 18:59:06 -0700186 return pool_elt_at_index (wrk->connections, conn_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700187}
188
189always_inline udp_connection_t *
190udp_listener_get (u32 conn_index)
191{
192 return pool_elt_at_index (udp_main.listener_pool, conn_index);
193}
194
195always_inline udp_main_t *
196vnet_get_udp_main ()
197{
198 return &udp_main;
199}
200
201always_inline udp_connection_t *
Florin Corase759bb52020-04-08 01:55:39 +0000202udp_connection_from_transport (transport_connection_t * tc)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700203{
204 return ((udp_connection_t *) tc);
205}
206
Florin Corase759bb52020-04-08 01:55:39 +0000207void udp_connection_free (udp_connection_t * uc);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700208udp_connection_t *udp_connection_alloc (u32 thread_index);
Florin Coras37127f72024-02-17 11:45:25 -0800209void udp_connection_share_port (u16 lcl_port, u8 is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700210
Florin Coras3cbc04b2017-10-02 00:18:51 -0700211always_inline udp_connection_t *
Florin Coras7fb0fe12018-04-09 09:24:52 -0700212udp_connection_clone_safe (u32 connection_index, u32 thread_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700213{
Florin Corasa2b358b2022-03-18 12:50:03 -0700214 u32 current_thread_index = vlib_get_thread_index (), new_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700215 udp_connection_t *old_c, *new_c;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700216
Florin Corasa2b358b2022-03-18 12:50:03 -0700217 new_c = udp_connection_alloc (current_thread_index);
218 new_index = new_c->c_c_index;
219 /* Connection pool always realloced with barrier */
Florin Coras4c89b182022-10-24 18:59:06 -0700220 old_c = udp_main.wrk[thread_index].connections + connection_index;
Dave Barach178cf492018-11-13 16:34:13 -0500221 clib_memcpy_fast (new_c, old_c, sizeof (*new_c));
Florin Corasd85666f2020-04-03 00:58:48 +0000222 old_c->flags |= UDP_CONN_F_MIGRATED;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700223 new_c->c_thread_index = current_thread_index;
Florin Corasa2b358b2022-03-18 12:50:03 -0700224 new_c->c_c_index = new_index;
Nathan Skrzypczak161638f2019-05-13 16:25:50 +0200225 new_c->c_fib_index = old_c->c_fib_index;
Florin Coras30fdf392020-12-02 21:14:56 -0800226 /* Assume cloned sessions don't need lock */
227 new_c->rx_lock = 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700228 return new_c;
229}
230
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231always_inline udp_dst_port_info_t *
232udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
233{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500234 uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
236}
237
238format_function_t format_udp_header;
239format_function_t format_udp_rx_trace;
Florin Corasc98ef752020-04-07 17:30:13 +0000240format_function_t format_udp_connection;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241unformat_function_t unformat_udp_header;
Elias Rudberg2dca1802020-05-27 01:03:46 +0200242unformat_function_t unformat_udp_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
Dave Barachd7cb1b52016-12-09 09:52:16 -0500244void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800245
Dave Barachd7cb1b52016-12-09 09:52:16 -0500246/*
247 * fd.io coding-style-patch-verification: ON
248 *
249 * Local Variables:
250 * eval: (c-set-style "gnu")
251 * End:
252 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500253
254#endif /* __included_udp_h__ */