xref: /illumos-gate/usr/src/uts/common/sys/ksocket.h (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 2011 Nexenta Systems, Inc.  All rights reserved.
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2015, Joyent, Inc.
25  */
26 
27 #ifndef _SYS_KSOCKET_H_
28 #define	_SYS_KSOCKET_H_
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 /* Opaque kernel socket type */
35 typedef struct __ksocket *ksocket_t;
36 struct nmsghdr;
37 struct msgb;	/* avoiding sys/stream.h here */
38 
39 /* flag bit for each Callback Event */
40 #define	KSOCKET_CB_CONNECTED		0x00000001
41 #define	KSOCKET_CB_CONNECTFAILED	0x00000002
42 #define	KSOCKET_CB_DISCONNECTED		0x00000004
43 #define	KSOCKET_CB_NEWDATA		0x00000008
44 #define	KSOCKET_CB_NEWCONN		0x00000010
45 #define	KSOCKET_CB_CANSEND		0x00000020
46 #define	KSOCKET_CB_OOBDATA		0x00000040
47 #define	KSOCKET_CB_CANTSENDMORE		0x00000080
48 #define	KSOCKET_CB_CANTRECVMORE		0x00000100
49 #define	KSOCKET_CB_ERROR		0x00000200
50 
51 /*
52  * Kernel Socket Callback Events
53  */
54 typedef enum ksocket_event {
55 	KSOCKET_EV_CONNECTED,
56 	KSOCKET_EV_CONNECTFAILED,
57 	KSOCKET_EV_DISCONNECTED,
58 	KSOCKET_EV_OOBDATA,
59 	KSOCKET_EV_NEWDATA,
60 	KSOCKET_EV_NEWCONN,
61 	KSOCKET_EV_CANSEND,
62 	KSOCKET_EV_CANTSENDMORE,
63 	KSOCKET_EV_CANTRECVMORE,
64 	KSOCKET_EV_ERROR
65 } ksocket_callback_event_t;
66 
67 typedef	void (*ksocket_callback_t)(ksocket_t, ksocket_callback_event_t,
68     void *, uintptr_t);
69 
70 typedef struct ksocket_callbacks {
71 	uint32_t		ksock_cb_flags;
72 	ksocket_callback_t	ksock_cb_connected;
73 	ksocket_callback_t	ksock_cb_connectfailed;
74 	ksocket_callback_t	ksock_cb_disconnected;
75 	ksocket_callback_t	ksock_cb_newdata;
76 	ksocket_callback_t	ksock_cb_newconn;
77 	ksocket_callback_t	ksock_cb_cansend;
78 	ksocket_callback_t	ksock_cb_oobdata;
79 	ksocket_callback_t	ksock_cb_cantsendmore;
80 	ksocket_callback_t	ksock_cb_cantrecvmore;
81 	ksocket_callback_t	ksock_cb_error;
82 } ksocket_callbacks_t;
83 
84 #define	KSOCKET_SLEEP	SOCKET_SLEEP
85 #define	KSOCKET_NOSLEEP	SOCKET_NOSLEEP
86 
87 extern int	ksocket_socket(ksocket_t *, int, int, int, int, struct cred *);
88 extern int	ksocket_bind(ksocket_t, struct sockaddr *, socklen_t,
89 		    struct cred *);
90 extern int	ksocket_listen(ksocket_t, int, struct cred *);
91 extern int	ksocket_accept(ksocket_t, struct sockaddr *, socklen_t *,
92 		    ksocket_t *, struct cred *);
93 extern int	ksocket_connect(ksocket_t, struct sockaddr *, socklen_t,
94 		    struct cred *);
95 extern int	ksocket_send(ksocket_t, void *, size_t, int, size_t *,
96 		    struct cred *);
97 extern int	ksocket_sendto(ksocket_t, void *, size_t, int,
98 		    struct sockaddr *, socklen_t, size_t *, struct cred *);
99 extern int	ksocket_sendmsg(ksocket_t, struct nmsghdr *, int, size_t *,
100 		    struct cred *);
101 extern int	ksocket_sendmblk(ksocket_t, struct nmsghdr *, int,
102 		    struct msgb **, struct cred *);
103 extern int	ksocket_recv(ksocket_t, void *, size_t, int, size_t *,
104 		    struct cred *);
105 extern int	ksocket_recvfrom(ksocket_t, void *, size_t, int,
106 		    struct sockaddr *, socklen_t *, size_t *, struct cred *);
107 extern int	ksocket_recvmsg(ksocket_t, struct nmsghdr *, int, size_t *,
108 		    struct cred *);
109 extern int	ksocket_shutdown(ksocket_t, int, struct cred *);
110 extern int	ksocket_setsockopt(ksocket_t, int, int, const void *, int,
111 		    struct cred *);
112 extern int	ksocket_getsockopt(ksocket_t, int, int, void *, int *,
113 		    struct cred *);
114 extern int	ksocket_getpeername(ksocket_t, struct sockaddr *, socklen_t *,
115 		    struct cred *);
116 extern int	ksocket_getsockname(ksocket_t, struct sockaddr *, socklen_t *,
117 		    struct cred *);
118 extern int	ksocket_ioctl(ksocket_t, int, intptr_t, int *, struct cred *);
119 extern int	ksocket_spoll(ksocket_t, int, short, short *, struct cred *);
120 extern int	ksocket_setcallbacks(ksocket_t, ksocket_callbacks_t *, void *,
121 		    struct cred *);
122 extern int	ksocket_close(ksocket_t, struct cred *);
123 extern void	ksocket_hold(ksocket_t);
124 extern void	ksocket_rele(ksocket_t);
125 
126 /*
127  * These functions allow an alternative way for a ksocket to directly
128  * receive data when it arrives in sockfs rather than having it queued
129  * in a socket buffer that it must separately poll. The use of this
130  * results in no data being queued in sockfs.
131  *
132  * When the receive function receives data, it is responsible for always
133  * consuming all of the data. The return value of the callback function
134  * is used to indicate flow control and backpressure (similar to
135  * mac_tx(9E)). If, after processing the data, additional data can be
136  * received and processed, then the callback function should return
137  * B_TRUE. Otherwise it should return B_FALSE. This will result in the
138  * lower level socket interfaces (e.g. TCP) understanding that
139  * backpressure has been asserted (as though the sockfs buffer is full).
140  *
141  * Once whatever conditions that caused the callback function to assert
142  * that it needed to assert flow control are done, then it must call
143  * ksocket_krecv_unblock() to allow the flow to continue. If the receive
144  * callback ever returns B_FALSE there will generally be no additional
145  * data received until this is called.
146  */
147 typedef boolean_t (*ksocket_krecv_f)(ksocket_t, struct msgb *, size_t, int,
148     void *);
149 extern int	ksocket_krecv_set(ksocket_t, ksocket_krecv_f, void *);
150 extern void	ksocket_krecv_unblock(ksocket_t);
151 
152 #ifdef	__cplusplus
153 }
154 #endif
155 
156 #endif /* _SYS_KSOCKET_H_ */
157