xref: /illumos-gate/usr/src/uts/common/inet/udp_impl.h (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2015, Joyent, Inc.
24  */
25 
26 #ifndef	_UDP_IMPL_H
27 #define	_UDP_IMPL_H
28 
29 /*
30  * UDP implementation private declarations.  These interfaces are
31  * used to build the IP module and are not meant to be accessed
32  * by any modules except IP itself.  They are undocumented and are
33  * subject to change without notice.
34  */
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #ifdef _KERNEL
41 
42 #include <sys/int_types.h>
43 #include <sys/netstack.h>
44 
45 #include <netinet/in.h>
46 #include <netinet/ip6.h>
47 
48 #include <inet/common.h>
49 #include <inet/ip.h>
50 #include <inet/optcom.h>
51 #include <inet/tunables.h>
52 
53 #define	UDP_MOD_ID		5607
54 
55 /*
56  * Bind hash list size and hash function.  It has to be a power of 2 for
57  * hashing.
58  */
59 #define	UDP_BIND_FANOUT_SIZE	512
60 #define	UDP_BIND_HASH(lport, size) \
61 	((ntohs((uint16_t)lport)) & (size - 1))
62 
63 /* UDP bind fanout hash structure. */
64 typedef struct udp_fanout_s {
65 	struct udp_s *uf_udp;
66 	kmutex_t uf_lock;
67 #if defined(_LP64) || defined(_I32LPx)
68 	char	uf_pad[48];
69 #else
70 	char	uf_pad[56];
71 #endif
72 } udp_fanout_t;
73 
74 /* Kstats */
75 typedef struct udp_stat {			/* Class "net" kstats */
76 	kstat_named_t	udp_sock_fallback;
77 	kstat_named_t	udp_out_opt;
78 	kstat_named_t	udp_out_err_notconn;
79 	kstat_named_t	udp_out_err_output;
80 	kstat_named_t	udp_out_err_tudr;
81 #ifdef DEBUG
82 	kstat_named_t	udp_data_conn;
83 	kstat_named_t	udp_data_notconn;
84 	kstat_named_t	udp_out_lastdst;
85 	kstat_named_t	udp_out_diffdst;
86 	kstat_named_t	udp_out_ipv6;
87 	kstat_named_t	udp_out_mapped;
88 	kstat_named_t	udp_out_ipv4;
89 #endif
90 } udp_stat_t;
91 
92 /*
93  * This struct contains only the counter part of udp_stat_t.  It is used
94  * in udp_stats_cpu_t instead of udp_stat_t to save memory space.
95  */
96 typedef struct {
97 	uint64_t	udp_sock_fallback;
98 	uint64_t	udp_out_opt;
99 	uint64_t	udp_out_err_notconn;
100 	uint64_t	udp_out_err_output;
101 	uint64_t	udp_out_err_tudr;
102 #ifdef DEBUG
103 	uint64_t	udp_data_conn;
104 	uint64_t	udp_data_notconn;
105 	uint64_t	udp_out_lastdst;
106 	uint64_t	udp_out_diffdst;
107 	uint64_t	udp_out_ipv6;
108 	uint64_t	udp_out_mapped;
109 	uint64_t	udp_out_ipv4;
110 #endif
111 } udp_stat_counter_t;
112 
113 /* Per CPU stats: UDP MIB2 and UDP kstat. */
114 typedef struct {
115 	mib2_udp_t		udp_sc_mib;
116 	udp_stat_counter_t	udp_sc_stats;
117 } udp_stats_cpu_t;
118 
119 #define	UDP_NUM_EPRIV_PORTS	64
120 
121 /* Default buffer size and flow control wake up threshold. */
122 #define	UDP_RECV_HIWATER	(56 * 1024)
123 #define	UDP_RECV_LOWATER	128
124 #define	UDP_XMIT_HIWATER	(56 * 1024)
125 #define	UDP_XMIT_LOWATER	1024
126 
127 /*
128  * UDP stack instances
129  */
130 struct udp_stack {
131 	netstack_t	*us_netstack;	/* Common netstack */
132 
133 	uint_t		us_bind_fanout_size;
134 	udp_fanout_t	*us_bind_fanout;
135 
136 	int		us_num_epriv_ports;
137 	in_port_t	us_epriv_ports[UDP_NUM_EPRIV_PORTS];
138 	kmutex_t	us_epriv_port_lock;
139 
140 	/* Hint not protected by any lock */
141 	in_port_t	us_next_port_to_try;
142 
143 	/* UDP tunables table */
144 	struct mod_prop_info_s	*us_propinfo_tbl;
145 
146 	kstat_t		*us_mibkp;	/* kstats exporting mib data */
147 	kstat_t		*us_kstat;
148 
149 /*
150  * The smallest anonymous port in the priviledged port range which UDP
151  * looks for free port.  Use in the option UDP_ANONPRIVBIND.
152  */
153 	in_port_t	us_min_anonpriv_port;
154 
155 	ldi_ident_t	us_ldi_ident;
156 
157 	udp_stats_cpu_t	**us_sc;
158 	int		us_sc_cnt;
159 };
160 
161 typedef struct udp_stack udp_stack_t;
162 
163 /* Internal udp control structure, one per open stream */
164 typedef	struct udp_s {
165 	/*
166 	 * The addresses and ports in the conn_t and udp_state are protected by
167 	 * conn_lock and the fanout lock i.e. uf_lock. Need both locks to change
168 	 * the fields, either lock is sufficient for reading the field.
169 	 * conn_lock also protects the content of udp_t.
170 	 */
171 	uint32_t	udp_state;	/* TPI state */
172 
173 	ip_pkt_t	udp_recv_ipp;	/* Used for IPv4 options received */
174 
175 	/* Written to only once at the time of opening the endpoint */
176 	conn_t		*udp_connp;
177 
178 	uint32_t
179 		udp_issocket : 1,	/* socket mode; sockfs is on top */
180 		udp_nat_t_endpoint : 1,	/* UDP_NAT_T_ENDPOINT option */
181 		udp_rcvhdr : 1,		/* UDP_RCVHDR option */
182 
183 		udp_vxlanhash: 1,	/* UDP_SRCPORT_HASH option */
184 					/* Because there's only VXLAN, cheat */
185 					/* and only use a single bit */
186 
187 		udp_pad_to_bit_31 : 28;
188 
189 	/* Following 2 fields protected by the uf_lock */
190 	struct udp_s	*udp_bind_hash; /* Bind hash chain */
191 	struct udp_s	**udp_ptpbhn; /* Pointer to previous bind hash next. */
192 
193 	kmutex_t	udp_recv_lock;		/* recv lock */
194 	size_t		udp_rcv_disply_hiwat;	/* user's view of rcvbuf */
195 	size_t		udp_rcv_hiwat;		/* receive high watermark */
196 
197 	/* Set at open time and never changed */
198 	udp_stack_t	*udp_us;		/* Stack instance for zone */
199 
200 	int		udp_delayed_error;
201 	mblk_t		*udp_fallback_queue_head;
202 	mblk_t		*udp_fallback_queue_tail;
203 	struct sockaddr_storage	udp_delayed_addr;
204 } udp_t;
205 
206 /* UDP Protocol header aligned */
207 typedef	struct udpahdr_s {
208 	in_port_t	uha_src_port;		/* Source port */
209 	in_port_t	uha_dst_port;		/* Destination port */
210 	uint16_t	uha_length;		/* UDP length */
211 	uint16_t	uha_checksum;		/* UDP checksum */
212 } udpha_t;
213 
214 #define	us_wroff_extra			us_propinfo_tbl[0].prop_cur_uval
215 #define	us_ipv4_ttl			us_propinfo_tbl[1].prop_cur_uval
216 #define	us_ipv6_hoplimit		us_propinfo_tbl[2].prop_cur_uval
217 #define	us_smallest_nonpriv_port	us_propinfo_tbl[3].prop_cur_uval
218 #define	us_do_checksum			us_propinfo_tbl[4].prop_cur_bval
219 #define	us_smallest_anon_port		us_propinfo_tbl[5].prop_cur_uval
220 #define	us_largest_anon_port		us_propinfo_tbl[6].prop_cur_uval
221 #define	us_xmit_hiwat			us_propinfo_tbl[7].prop_cur_uval
222 #define	us_xmit_lowat			us_propinfo_tbl[8].prop_cur_uval
223 #define	us_recv_hiwat			us_propinfo_tbl[9].prop_cur_uval
224 #define	us_max_buf			us_propinfo_tbl[10].prop_cur_uval
225 #define	us_pmtu_discovery		us_propinfo_tbl[11].prop_cur_bval
226 #define	us_sendto_ignerr		us_propinfo_tbl[12].prop_cur_bval
227 
228 #define	UDPS_BUMP_MIB(us, x)	\
229 	BUMP_MIB(&(us)->us_sc[CPU->cpu_seqid]->udp_sc_mib, x)
230 
231 #define	UDP_STAT(us, x)		((us)->us_sc[CPU->cpu_seqid]->udp_sc_stats.x++)
232 #define	UDP_STAT_UPDATE(us, x, n)	\
233 	((us)->us->sc[CPU->cpu_seqid]->udp_sc_stats.x.value.ui64 += (n))
234 #ifdef DEBUG
235 #define	UDP_DBGSTAT(us, x)	UDP_STAT(us, x)
236 #else
237 #define	UDP_DBGSTAT(us, x)
238 #endif /* DEBUG */
239 
240 extern int	udp_opt_default(queue_t *, t_scalar_t, t_scalar_t, uchar_t *);
241 extern int	udp_tpi_opt_get(queue_t *, t_scalar_t, t_scalar_t, uchar_t *);
242 extern int	udp_tpi_opt_set(queue_t *, uint_t, int, int, uint_t, uchar_t *,
243 		    uint_t *, uchar_t *, void *, cred_t *);
244 extern mblk_t	*udp_snmp_get(queue_t *, mblk_t *, boolean_t);
245 extern int	udp_snmp_set(queue_t *, t_scalar_t, t_scalar_t, uchar_t *, int);
246 extern void	udp_ddi_g_init(void);
247 extern void	udp_ddi_g_destroy(void);
248 extern void	udp_output(conn_t *connp, mblk_t *mp, struct sockaddr *addr,
249 		    socklen_t addrlen);
250 extern int	udp_wput(queue_t *, mblk_t *);
251 
252 extern void	*udp_kstat_init(netstackid_t stackid);
253 extern void	udp_kstat_fini(netstackid_t stackid, kstat_t *ksp);
254 extern void	*udp_kstat2_init(netstackid_t stackid);
255 extern void	udp_kstat2_fini(netstackid_t, kstat_t *);
256 
257 extern void	udp_stack_cpu_add(udp_stack_t *, processorid_t);
258 
259 /*
260  * Object to represent database of options to search passed to
261  * {sock,tpi}optcom_req() interface routine to take care of option
262  * management and associated methods.
263  */
264 extern optdb_obj_t	udp_opt_obj;
265 extern uint_t		udp_max_optsize;
266 
267 extern sock_lower_handle_t udp_create(int, int, int, sock_downcalls_t **,
268     uint_t *, int *, int, cred_t *);
269 extern int udp_fallback(sock_lower_handle_t, queue_t *, boolean_t,
270     so_proto_quiesced_cb_t, sock_quiesce_arg_t *);
271 
272 extern sock_downcalls_t sock_udp_downcalls;
273 
274 #endif	/*  _KERNEL */
275 
276 #ifdef	__cplusplus
277 }
278 #endif
279 
280 #endif	/* _UDP_IMPL_H */
281