kernel -- Convert sb_timeo for socket timeout from short -> int.
[dragonfly.git] / sys / sys / socketvar.h
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1982, 1986, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
f1f552f6 34 * $FreeBSD: src/sys/sys/socketvar.h,v 1.46.2.10 2003/08/24 08:24:39 hsu Exp $
4402d8a2 35 * $DragonFly: src/sys/sys/socketvar.h,v 1.35 2008/08/28 23:15:45 dillon Exp $
984263bc
MD
36 */
37
38#ifndef _SYS_SOCKETVAR_H_
39#define _SYS_SOCKETVAR_H_
40
03d6a592
MD
41#ifndef _SYS_TYPES_H_
42#include <sys/types.h>
43#endif
44#ifndef _SYS_QUEUE_H_
984263bc 45#include <sys/queue.h> /* for TAILQ macros */
03d6a592 46#endif
5b22f1a7
SG
47#ifndef _SYS_EVENT_H_
48#include <sys/event.h> /* for struct kqinfo */
03d6a592 49#endif
6cef7136
MD
50#ifndef _SYS_THREAD_H_
51#include <sys/thread.h> /* for struct lwkt_token */
52#endif
6d49aa6f
MD
53#ifndef _SYS_SOCKBUF_H_
54#include <sys/sockbuf.h>
55#endif
984263bc 56
03d6a592 57#if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
6d49aa6f
MD
58
59struct accept_filter;
60
984263bc 61/*
6d49aa6f
MD
62 * Signaling socket buffers contain additional elements for locking
63 * and signaling conditions. These are used primarily by sockets.
14343ad3
MD
64 *
65 * WARNING: See partial clearing of fields in kern/uipc_socket.c
66 * sorflush() and sowflush().
984263bc 67 */
6d49aa6f
MD
68struct signalsockbuf {
69 struct sockbuf sb;
5b22f1a7 70 struct kqinfo ssb_kq; /* process selecting read/write */
14343ad3 71 uint32_t ssb_flags; /* flags, see below (use atomic ops) */
45546849 72 u_int ssb_timeo; /* timeout for read/write */
6d49aa6f
MD
73 long ssb_lowat; /* low water mark */
74 u_long ssb_hiwat; /* high water mark / max actual char count */
75 u_long ssb_mbmax; /* max chars of mbufs to use */
6cef7136 76 struct lwkt_token ssb_token; /* frontend/backend serializer */
6d49aa6f 77};
984263bc 78
6d49aa6f
MD
79#define ssb_cc sb.sb_cc /* commonly used fields */
80#define ssb_mb sb.sb_mb /* commonly used fields */
81#define ssb_mbcnt sb.sb_mbcnt /* commonly used fields */
82
14343ad3
MD
83#define SSB_LOCK 0x0001 /* lock on data queue */
84#define SSB_WANT 0x0002 /* someone is waiting to lock */
85#define SSB_WAIT 0x0004 /* someone is waiting for data/space */
86#define SSB_ASYNC 0x0010 /* ASYNC I/O, need signals */
87#define SSB_UPCALL 0x0020 /* someone wants an upcall */
88#define SSB_NOINTR 0x0040 /* operations not interruptible */
91bcbcd6 89/*#define SSB_AIO 0x0080*/ /* AIO operations queued */
14343ad3
MD
90#define SSB_KNOTE 0x0100 /* kernel note attached */
91#define SSB_MEVENT 0x0200 /* need message event notification */
92#define SSB_STOP 0x0400 /* backpressure indicator */
93#define SSB_AUTOSIZE 0x0800 /* automatically size socket buffer */
dbcbe5d3 94#define SSB_AUTOLOWAT 0x1000 /* automatically scale lowat */
14343ad3
MD
95#define SSB_WAKEUP 0x2000 /* wakeup event race */
96
97#define SSB_CLEAR_MASK (SSB_ASYNC | SSB_UPCALL | SSB_STOP | \
98 SSB_AUTOSIZE | SSB_AUTOLOWAT)
99
100#define SSB_NOTIFY_MASK (SSB_WAIT | SSB_ASYNC | SSB_UPCALL | \
91bcbcd6 101 SSB_KNOTE | SSB_MEVENT)
984263bc 102
6d49aa6f
MD
103/*
104 * Per-socket kernel structure. Contains universal send and receive queues,
105 * protocol control handle, and error information.
106 */
984263bc
MD
107struct socket {
108 short so_type; /* generic type, see socket.h */
109 short so_options; /* from socket call, see socket.h */
110 short so_linger; /* time to linger while closing */
111 short so_state; /* internal state flags SS_*, below */
ef87f48d 112 void *so_pcb; /* protocol control block */
984263bc 113 struct protosw *so_proto; /* protocol handle */
984263bc 114 struct socket *so_head; /* back pointer to accept socket */
48e7b118 115 lwkt_port_t so_port; /* message port */
6d49aa6f
MD
116
117 /*
118 * These fields are used to manage sockets capable of accepting
119 * new connections.
120 */
121 TAILQ_HEAD(, socket) so_incomp; /* in-progress, incomplete */
122 TAILQ_HEAD(, socket) so_comp; /* completed but not yet accepted */
984263bc 123 TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */
6d49aa6f
MD
124 short so_qlen; /* so_comp count */
125 short so_incqlen; /* so_incomp count */
984263bc 126 short so_qlimit; /* max number queued connections */
6d49aa6f
MD
127
128 /*
129 * Misc socket support
130 */
984263bc
MD
131 short so_timeo; /* connection timeout */
132 u_short so_error; /* error affecting connection */
133 struct sigio *so_sigio; /* information for async I/O or
134 out of band data (SIGURG) */
135 u_long so_oobmark; /* chars to oob mark */
136 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
6d49aa6f
MD
137 struct signalsockbuf so_rcv;
138 struct signalsockbuf so_snd;
984263bc 139
b153f746 140 void (*so_upcall) (struct socket *, void *, int);
984263bc
MD
141 void *so_upcallarg;
142 struct ucred *so_cred; /* user credentials */
143 /* NB: generation count must not be first; easiest to make it last. */
984263bc 144 void *so_emuldata; /* private data for emulators */
6cef7136 145 int so_refs; /* shutdown refs */
984263bc
MD
146 struct so_accf {
147 struct accept_filter *so_accept_filter;
148 void *so_accept_filter_arg; /* saved filter args */
149 char *so_accept_filter_str; /* saved user args */
150 } *so_accf;
151};
152
03d6a592
MD
153#endif
154
984263bc
MD
155/*
156 * Socket state bits.
6cef7136
MD
157 *
158 * NOTE: The following states are interlocked with so_refs:
159 *
160 * SS_NOFDREF so_refs while not set
161 * (so_pcb) so_refs while set
984263bc
MD
162 */
163#define SS_NOFDREF 0x0001 /* no file table ref any more */
164#define SS_ISCONNECTED 0x0002 /* socket connected to a peer */
165#define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */
166#define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */
167#define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */
168#define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */
169#define SS_RCVATMARK 0x0040 /* at mark on input */
170
e28d8186 171#define SS_ASSERTINPROG 0x0100 /* sonewconn race debugging */
984263bc
MD
172#define SS_ASYNC 0x0200 /* async i/o notify */
173#define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */
174
175#define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */
176#define SS_COMP 0x1000 /* unaccepted, complete connection */
177#define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */
178
179/*
180 * Externalized form of struct socket used by the sysctl(3) interface.
181 */
182struct xsocket {
183 size_t xso_len; /* length of this structure */
184 struct socket *xso_so; /* makes a convenient handle sometimes */
185 short so_type;
186 short so_options;
187 short so_linger;
188 short so_state;
ef87f48d 189 void *so_pcb; /* another convenient handle */
984263bc
MD
190 int xso_protocol;
191 int xso_family;
192 short so_qlen;
193 short so_incqlen;
194 short so_qlimit;
195 short so_timeo;
196 u_short so_error;
197 pid_t so_pgid;
198 u_long so_oobmark;
199 struct xsockbuf {
200 u_long sb_cc;
201 u_long sb_hiwat;
202 u_long sb_mbcnt;
203 u_long sb_mbmax;
204 long sb_lowat;
45546849 205 u_int sb_timeo;
984263bc 206 short sb_flags;
984263bc
MD
207 } so_rcv, so_snd;
208 uid_t so_uid; /* XXX */
209};
210
211/*
212 * Macros for sockets and socket buffering.
213 */
214
984263bc
MD
215#define sosendallatonce(so) \
216 ((so)->so_proto->pr_flags & PR_ATOMIC)
217
218/* can we read something from so? */
219#define soreadable(so) \
6d49aa6f 220 ((so)->so_rcv.ssb_cc >= (so)->so_rcv.ssb_lowat || \
984263bc 221 ((so)->so_state & SS_CANTRCVMORE) || \
ecd80f47 222 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
984263bc
MD
223
224/* can we write something to so? */
225#define sowriteable(so) \
6d49aa6f 226 ((ssb_space(&(so)->so_snd) >= (so)->so_snd.ssb_lowat && \
984263bc
MD
227 (((so)->so_state&SS_ISCONNECTED) || \
228 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
229 ((so)->so_state & SS_CANTSENDMORE) || \
230 (so)->so_error)
231
6d49aa6f
MD
232/*
233 * Do we need to notify the other side when I/O is possible?
14343ad3
MD
234 *
235 * NOTE: Interlock for ssb_wait/wakeup. The protocol side will set
236 * SSB_WAKEUP asynchronously and this can race, so if it isn't
237 * set we have to go through the full-on notification check.
238 * If it is set but no waiting ever takes place it simply
239 * remains set.
6d49aa6f 240 */
14343ad3
MD
241#define ssb_notify(ssb) \
242 (((ssb)->ssb_flags & SSB_NOTIFY_MASK) || \
243 ((ssb)->ssb_flags & SSB_WAKEUP) == 0)
984263bc 244
6d49aa6f 245/* do we have to send all at once on a socket? */
984263bc 246
0efd86dd
MD
247#ifdef _KERNEL
248
6d49aa6f
MD
249/*
250 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
251 * This is problematical if the fields are unsigned, as the space might
252 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
3a6117bb
MD
253 * overflow and return 0.
254 *
255 * SSB_STOP ignores cc/hiwat and returns 0. This is used by unix domain
256 * stream sockets to signal backpressure.
6d49aa6f 257 */
3a6117bb
MD
258static __inline
259long
260ssb_space(struct signalsockbuf *ssb)
261{
262 long bleft;
263 long mleft;
264
265 if (ssb->ssb_flags & SSB_STOP)
266 return(0);
267 bleft = ssb->ssb_hiwat - ssb->ssb_cc;
268 mleft = ssb->ssb_mbmax - ssb->ssb_mbcnt;
269 return((bleft < mleft) ? bleft : mleft);
270}
6d49aa6f 271
0efd86dd
MD
272#endif
273
6d49aa6f
MD
274#define ssb_append(ssb, m) \
275 sbappend(&(ssb)->sb, m)
276
277#define ssb_appendstream(ssb, m) \
278 sbappendstream(&(ssb)->sb, m)
279
280#define ssb_appendrecord(ssb, m) \
281 sbappendrecord(&(ssb)->sb, m)
282
283#define ssb_appendaddr(ssb, src, m, control) \
ee38db90 284 ((ssb_space(ssb) <= 0) ? 0 : sbappendaddr(&(ssb)->sb, src, m, control))
6d49aa6f
MD
285
286#define ssb_appendcontrol(ssb, m, control) \
ee38db90 287 ((ssb_space(ssb) <= 0) ? 0 : sbappendcontrol(&(ssb)->sb, m, control))
6d49aa6f
MD
288
289#define ssb_insert_knote(ssb, kn) { \
14343ad3
MD
290 knote_insert(&(ssb)->ssb_kq.ki_note, kn); \
291 atomic_set_int(&(ssb)->ssb_flags, SSB_KNOTE); \
6d49aa6f
MD
292}
293
294#define ssb_remove_knote(ssb, kn) { \
14343ad3 295 knote_remove(&(ssb)->ssb_kq.ki_note, kn); \
5b22f1a7 296 if (SLIST_EMPTY(&(ssb)->ssb_kq.ki_note)) \
14343ad3 297 atomic_clear_int(&(ssb)->ssb_flags, SSB_KNOTE); \
6d49aa6f
MD
298}
299
14343ad3
MD
300#define sorwakeup(so) \
301 do { \
302 if (ssb_notify(&(so)->so_rcv)) \
303 sowakeup((so), &(so)->so_rcv); \
304 } while (0)
305
306#define sowwakeup(so) \
307 do { \
308 if (ssb_notify(&(so)->so_snd)) \
309 sowakeup((so), &(so)->so_snd); \
310 } while (0)
984263bc
MD
311
312#ifdef _KERNEL
313
314/*
315 * Argument structure for sosetopt et seq. This is in the KERNEL
316 * section because it will never be visible to user code.
317 */
318enum sopt_dir { SOPT_GET, SOPT_SET };
319struct sockopt {
320 enum sopt_dir sopt_dir; /* is this a get or a set? */
321 int sopt_level; /* second arg of [gs]etsockopt */
322 int sopt_name; /* third arg of [gs]etsockopt */
323 void *sopt_val; /* fourth arg of [gs]etsockopt */
324 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */
dadab5e9 325 struct thread *sopt_td; /* calling thread or null if kernel */
984263bc
MD
326};
327
984263bc
MD
328struct accept_filter {
329 char accf_name[16];
330 void (*accf_callback)
b153f746 331 (struct socket *so, void *arg, int waitflag);
984263bc 332 void * (*accf_create)
b153f746 333 (struct socket *so, char *arg);
984263bc 334 void (*accf_destroy)
b153f746 335 (struct socket *so);
984263bc
MD
336 SLIST_ENTRY(accept_filter) accf_next; /* next on the list */
337};
338
339#ifdef MALLOC_DECLARE
340MALLOC_DECLARE(M_PCB);
341MALLOC_DECLARE(M_SONAME);
342MALLOC_DECLARE(M_ACCF);
343#endif
344
345extern int maxsockets;
e9855d6b
MD
346extern u_long sb_max; /* nominal limit */
347extern u_long sb_max_adj; /* actual limit used by sbreserve() */
984263bc
MD
348
349struct file;
350struct filedesc;
351struct mbuf;
e4700d00 352struct rlimit;
984263bc
MD
353struct sockaddr;
354struct stat;
355struct ucred;
356struct uio;
357struct knote;
87baaf0c 358struct sysmsg;
984263bc
MD
359
360/*
361 * File operations on sockets.
362 */
b153f746 363int soo_read (struct file *fp, struct uio *uio, struct ucred *cred,
87baaf0c 364 int flags);
b153f746 365int soo_write (struct file *fp, struct uio *uio, struct ucred *cred,
87baaf0c 366 int flags);
87de5057
MD
367int soo_close (struct file *fp);
368int soo_shutdown (struct file *fp, int how);
b153f746 369int soo_ioctl (struct file *fp, u_long cmd, caddr_t data,
87baaf0c 370 struct ucred *cred, struct sysmsg *msg);
87de5057 371int soo_stat (struct file *fp, struct stat *ub, struct ucred *cred);
b153f746 372int sokqfilter (struct file *fp, struct knote *kn);
984263bc
MD
373
374/*
375 * From uipc_socket and friends
376 */
590b8cd4 377struct sockaddr *dup_sockaddr (const struct sockaddr *sa);
b153f746 378int getsockaddr (struct sockaddr **namp, caddr_t uaddr, size_t len);
6d49aa6f
MD
379
380void ssb_release (struct signalsockbuf *ssb, struct socket *so);
381int ssb_reserve (struct signalsockbuf *ssb, u_long cc, struct socket *so,
e4700d00 382 struct rlimit *rl);
6d49aa6f
MD
383void ssbtoxsockbuf (struct signalsockbuf *sb, struct xsockbuf *xsb);
384int ssb_wait (struct signalsockbuf *sb);
385int _ssb_lock (struct signalsockbuf *sb);
386
4402d8a2
MD
387void soabort (struct socket *so);
388void soaborta (struct socket *so);
fd86a41c 389void soabort_oncpu (struct socket *so);
b153f746
RG
390int soaccept (struct socket *so, struct sockaddr **nam);
391struct socket *soalloc (int waitok);
392int sobind (struct socket *so, struct sockaddr *nam, struct thread *td);
393void socantrcvmore (struct socket *so);
394void socantsendmore (struct socket *so);
8b5c39bb 395int socket_wait (struct socket *so, struct timespec *ts, int *res);
9ba76b73 396int soclose (struct socket *so, int fflags);
b153f746
RG
397int soconnect (struct socket *so, struct sockaddr *nam, struct thread *td);
398int soconnect2 (struct socket *so1, struct socket *so2);
399int socreate (int dom, struct socket **aso, int type, int proto,
400 struct thread *td);
b153f746
RG
401int sodisconnect (struct socket *so);
402void sofree (struct socket *so);
403int sogetopt (struct socket *so, struct sockopt *sopt);
404void sohasoutofband (struct socket *so);
405void soisconnected (struct socket *so);
406void soisconnecting (struct socket *so);
407void soisdisconnected (struct socket *so);
408void soisdisconnecting (struct socket *so);
0f2e13ef
SG
409void soisreconnected (struct socket *so);
410void soisreconnecting (struct socket *so);
48e7b118 411void sosetport (struct socket *so, struct lwkt_port *port);
b153f746
RG
412int solisten (struct socket *so, int backlog, struct thread *td);
413struct socket *sonewconn (struct socket *head, int connstatus);
414int sooptcopyin (struct sockopt *sopt, void *buf, size_t len,
415 size_t minlen);
e71a125f
AE
416int soopt_to_kbuf (struct sockopt *sopt, void *buf, size_t len,
417 size_t minlen);
f1f552f6 418int sooptcopyout (struct sockopt *sopt, const void *buf, size_t len);
e71a125f 419void soopt_from_kbuf (struct sockopt *sopt, const void *buf, size_t len);
984263bc
MD
420
421/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
b153f746
RG
422int soopt_getm (struct sockopt *sopt, struct mbuf **mp);
423int soopt_mcopyin (struct sockopt *sopt, struct mbuf *m);
e71a125f 424void soopt_to_mbuf (struct sockopt *sopt, struct mbuf *m);
b153f746 425int soopt_mcopyout (struct sockopt *sopt, struct mbuf *m);
e71a125f 426int soopt_from_mbuf (struct sockopt *sopt, struct mbuf *m);
984263bc 427
b153f746 428int soreceive (struct socket *so, struct sockaddr **paddr,
6d49aa6f 429 struct uio *uio, struct sockbuf *sio,
b153f746 430 struct mbuf **controlp, int *flagsp);
e4700d00
JH
431int soreserve (struct socket *so, u_long sndcc, u_long rcvcc,
432 struct rlimit *rl);
b153f746
RG
433void sorflush (struct socket *so);
434int sosend (struct socket *so, struct sockaddr *addr, struct uio *uio,
984263bc 435 struct mbuf *top, struct mbuf *control, int flags,
b153f746 436 struct thread *td);
6ea1e9b9
JH
437int sosendudp (struct socket *so, struct sockaddr *addr, struct uio *uio,
438 struct mbuf *top, struct mbuf *control, int flags,
439 struct thread *td);
b153f746
RG
440int sosetopt (struct socket *so, struct sockopt *sopt);
441int soshutdown (struct socket *so, int how);
442void sotoxsocket (struct socket *so, struct xsocket *xso);
6d49aa6f 443void sowakeup (struct socket *so, struct signalsockbuf *sb);
984263bc
MD
444
445/* accept filter functions */
b153f746
RG
446int accept_filt_add (struct accept_filter *filt);
447int accept_filt_del (char *name);
448struct accept_filter * accept_filt_get (char *name);
984263bc 449#ifdef ACCEPT_FILTER_MOD
b153f746 450int accept_filt_generic_mod_event (module_t mod, int event, void *data);
984263bc
MD
451SYSCTL_DECL(_net_inet_accf);
452#endif /* ACCEPT_FILTER_MOD */
453
454#endif /* _KERNEL */
455
456#endif /* !_SYS_SOCKETVAR_H_ */