xref: /titanic_51/usr/src/uts/common/sys/neti.h (revision 8ad7418892268f9f0ba29518ab332f6a26f69fc5)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_NETI_H
27 #define	_SYS_NETI_H
28 
29 #include <netinet/in.h>
30 #include <sys/int_types.h>
31 #include <sys/queue.h>
32 #include <sys/hook_impl.h>
33 #include <sys/netstack.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #define	NETINFO_VERSION 1
40 
41 /*
42  * Network hooks framework stack protocol name
43  */
44 #define	NHF_INET	"NHF_INET"
45 #define	NHF_INET6	"NHF_INET6"
46 #define	NHF_ARP		"NHF_ARP"
47 
48 /*
49  * Event identification
50  */
51 #define	NH_PHYSICAL_IN	"PHYSICAL_IN"
52 #define	NH_PHYSICAL_OUT	"PHYSICAL_OUT"
53 #define	NH_FORWARDING	"FORWARDING"
54 #define	NH_LOOPBACK_IN	"LOOPBACK_IN"
55 #define	NH_LOOPBACK_OUT	"LOOPBACK_OUT"
56 #define	NH_NIC_EVENTS	"NIC_EVENTS"
57 
58 /*
59  * Network NIC hardware checksum capability
60  */
61 #define	NET_HCK_NONE   	0x00
62 #define	NET_HCK_L3_FULL	0x01
63 #define	NET_HCK_L3_PART	0x02
64 #define	NET_HCK_L4_FULL	0x10
65 #define	NET_HCK_L4_PART	0x20
66 
67 #define	NET_IS_HCK_L3_FULL(n, x)                                             \
68 	((net_ispartialchecksum(n, x) & NET_HCK_L3_FULL) == NET_HCK_L3_FULL)
69 #define	NET_IS_HCK_L3_PART(n, x)                                             \
70 	((net_ispartialchecksum(n, x) & NET_HCK_L3_PART) == NET_HCK_L3_PART)
71 #define	NET_IS_HCK_L4_FULL(n, x)                                             \
72 	((net_ispartialchecksum(n, x) & NET_HCK_L4_FULL) == NET_HCK_L4_FULL)
73 #define	NET_IS_HCK_L4_PART(n, x)                                             \
74 	((net_ispartialchecksum(n, x) & NET_HCK_L4_PART) == NET_HCK_L4_PART)
75 #define	NET_IS_HCK_L34_FULL(n, x)                                            \
76 	((net_ispartialchecksum(n, x) & (NET_HCK_L3_FULL|NET_HCK_L4_FULL))   \
77 	    == (NET_HCK_L3_FULL | NET_HCK_L4_FULL))
78 
79 typedef uintptr_t	phy_if_t;
80 typedef intptr_t	lif_if_t;
81 typedef uintptr_t	net_ifdata_t;
82 typedef id_t		netid_t;
83 
84 /*
85  * Netinfo interface specification
86  *
87  * Netinfo provides an extensible and easy to use interface for
88  * accessing data and functionality already embedded within network
89  * code that exists within the kernel.
90  */
91 typedef enum net_ifaddr {
92 	NA_ADDRESS = 1,
93 	NA_PEER,
94 	NA_BROADCAST,
95 	NA_NETMASK
96 } net_ifaddr_t;
97 
98 
99 typedef enum inject {
100 	NI_QUEUE_IN = 1,
101 	NI_QUEUE_OUT,
102 	NI_DIRECT_OUT
103 } inject_t;
104 
105 /*
106  * net_inject - public interface
107  */
108 typedef struct net_inject {
109 	int			ni_version;
110 	netid_t			ni_netid;
111 	mblk_t			*ni_packet;
112 	struct sockaddr_storage	ni_addr;
113 	phy_if_t		ni_physical;
114 } net_inject_t;
115 
116 typedef struct net_data *net_handle_t;
117 
118 /*
119  * net_protocol_t private interface
120  */
121 struct net_protocol_s {
122 	int		netp_version;
123 	char		*netp_name;
124 	int		(*netp_getifname)(net_handle_t, phy_if_t, char *,
125 			    const size_t);
126 	int		(*netp_getmtu)(net_handle_t, phy_if_t, lif_if_t);
127 	int		(*netp_getpmtuenabled)(net_handle_t);
128 	int		(*netp_getlifaddr)(net_handle_t, phy_if_t, lif_if_t,
129 			    size_t, net_ifaddr_t [], void *);
130 	phy_if_t	(*netp_phygetnext)(net_handle_t, phy_if_t);
131 	phy_if_t	(*netp_phylookup)(net_handle_t, const char *);
132 	lif_if_t	(*netp_lifgetnext)(net_handle_t, phy_if_t, lif_if_t);
133 	int		(*netp_inject)(net_handle_t, inject_t, net_inject_t *);
134 	phy_if_t	(*netp_routeto)(net_handle_t, struct sockaddr *,
135 			    struct sockaddr *);
136 	int		(*netp_ispartialchecksum)(net_handle_t, mblk_t *);
137 	int		(*netp_isvalidchecksum)(net_handle_t, mblk_t *);
138 };
139 typedef struct net_protocol_s net_protocol_t;
140 
141 
142 /*
143  * Private data structures
144  */
145 struct net_data {
146 	LIST_ENTRY(net_data)		netd_list;
147 	net_protocol_t			netd_info;
148 	int				netd_refcnt;
149 	hook_family_int_t		*netd_hooks;
150 	struct neti_stack_s		*netd_stack;
151 	int				netd_condemned;
152 };
153 
154 
155 typedef struct injection_s {
156 	net_inject_t	inj_data;
157 	boolean_t	inj_isv6;
158 	void *		inj_ptr;
159 } injection_t;
160 
161 /*
162  * The ipif_id space is [0,MAX) but this interface wants to return [1,MAX] as
163  * a valid range of logical interface numbers so that it can return 0 to mean
164  * "end of list" with net_lifgetnext.  Changing ipif_id's to use the [1,MAX]
165  * space is something to be considered for the future, if it is worthwhile.
166  */
167 #define	MAP_IPIF_ID(x)		((x) + 1)
168 #define	UNMAP_IPIF_ID(x)	(((x) > 0) ? (x) - 1 : (x))
169 
170 struct net_instance_s {
171 	int				nin_version;
172 	char				*nin_name;
173 	void				*(*nin_create)(const netid_t);
174 	void				(*nin_destroy)(const netid_t, void *);
175 	void				(*nin_shutdown)(const netid_t, void *);
176 };
177 typedef struct net_instance_s net_instance_t;
178 
179 struct net_instance_int_s {
180 	LIST_ENTRY(net_instance_int_s)	nini_next;
181 	uint_t				nini_ref;
182 	void				*nini_created;
183 	struct net_instance_int_s	*nini_parent;
184 	net_instance_t			*nini_instance;
185 	hook_notify_t			nini_notify;
186 	uint32_t			nini_flags;
187 	kcondvar_t			nini_cv;
188 	boolean_t			nini_condemned;
189 };
190 typedef struct net_instance_int_s net_instance_int_t;
191 LIST_HEAD(nini_head_s, net_instance_int_s);
192 typedef struct nini_head_s nini_head_t;
193 
194 #define	nini_version	nini_instance->nin_version
195 #define	nini_name	nini_instance->nin_name
196 #define	nini_create	nini_instance->nin_create
197 #define	nini_destroy	nini_instance->nin_destroy
198 #define	nini_shutdown	nini_instance->nin_shutdown
199 
200 /*
201  * netinfo stack instances
202  */
203 struct neti_stack_s {
204 	kmutex_t			nts_lock;
205 	LIST_ENTRY(neti_stack_s)	nts_next;
206 	netid_t				nts_id;
207 	zoneid_t			nts_zoneid;
208 	netstackid_t			nts_stackid;
209 	netstack_t			*nts_netstack;
210 	nini_head_t			nts_instances;
211 	uint32_t			nts_flags;
212 	kcondvar_t			nts_cv;
213 	/* list of net_handle_t */
214 	LIST_HEAD(netd_listhead, net_data) nts_netd_head;
215 };
216 typedef struct neti_stack_s neti_stack_t;
217 LIST_HEAD(neti_stack_head_s, neti_stack_s);
218 typedef struct neti_stack_head_s neti_stack_head_t;
219 
220 /*
221  * Internal functions that need to be exported within the module.
222  */
223 extern void neti_init(void);
224 extern void neti_fini(void);
225 extern neti_stack_t *net_getnetistackbyid(netid_t);
226 extern netstackid_t net_getnetstackidbynetid(netid_t);
227 extern netid_t net_getnetidbynetstackid(netstackid_t);
228 extern netid_t net_zoneidtonetid(zoneid_t);
229 extern zoneid_t net_getzoneidbynetid(netid_t);
230 
231 /*
232  * Functions available for public use.
233  */
234 extern hook_event_token_t net_event_register(net_handle_t, hook_event_t *);
235 extern int net_event_shutdown(net_handle_t, hook_event_t *);
236 extern int net_event_unregister(net_handle_t, hook_event_t *);
237 extern int net_event_notify_register(net_handle_t, char *,
238     hook_notify_fn_t, void *);
239 extern int net_event_notify_unregister(net_handle_t, char *, hook_notify_fn_t);
240 
241 extern int net_family_register(net_handle_t, hook_family_t *);
242 extern int net_family_shutdown(net_handle_t, hook_family_t *);
243 extern int net_family_unregister(net_handle_t, hook_family_t *);
244 
245 extern int net_hook_register(net_handle_t, char *, hook_t *);
246 extern int net_hook_unregister(net_handle_t, char *, hook_t *);
247 
248 extern int net_inject(net_handle_t, inject_t, net_inject_t *);
249 extern net_inject_t *net_inject_alloc(const int);
250 extern void net_inject_free(net_inject_t *);
251 
252 extern net_instance_t *net_instance_alloc(const int version);
253 extern void net_instance_free(net_instance_t *);
254 extern int net_instance_register(net_instance_t *);
255 extern int net_instance_unregister(net_instance_t *);
256 extern int net_instance_notify_register(netid_t, hook_notify_fn_t, void *);
257 extern int net_instance_notify_unregister(netid_t netid, hook_notify_fn_t);
258 
259 extern kstat_t *net_kstat_create(netid_t, char *, int, char *, char *,
260     uchar_t, ulong_t, uchar_t);
261 extern void net_kstat_delete(netid_t, kstat_t *);
262 
263 extern net_handle_t net_protocol_lookup(netid_t, const char *);
264 extern net_handle_t net_protocol_register(netid_t, const net_protocol_t *);
265 extern int net_protocol_release(net_handle_t);
266 extern int net_protocol_unregister(net_handle_t);
267 extern net_handle_t net_protocol_walk(netid_t, net_handle_t);
268 extern int net_protocol_notify_register(net_handle_t, hook_notify_fn_t, void *);
269 extern int net_protocol_notify_unregister(net_handle_t, hook_notify_fn_t);
270 
271 
272 extern int net_getifname(net_handle_t, phy_if_t, char *, const size_t);
273 extern int net_getmtu(net_handle_t, phy_if_t, lif_if_t);
274 extern int net_getpmtuenabled(net_handle_t);
275 extern int net_getlifaddr(net_handle_t, phy_if_t, lif_if_t,
276     int, net_ifaddr_t [], void *);
277 extern phy_if_t net_phygetnext(net_handle_t, phy_if_t);
278 extern phy_if_t net_phylookup(net_handle_t, const char *);
279 extern lif_if_t net_lifgetnext(net_handle_t, phy_if_t, lif_if_t);
280 extern phy_if_t net_routeto(net_handle_t, struct sockaddr *,
281     struct sockaddr *);
282 extern int net_ispartialchecksum(net_handle_t, mblk_t *);
283 extern int net_isvalidchecksum(net_handle_t, mblk_t *);
284 
285 #ifdef	__cplusplus
286 }
287 #endif
288 
289 #endif /* _SYS_NETI_H */
290