TCP - Reduce context switching when handling large send*() or write()'s
[dragonfly.git] / sys / sys / socketvar.h
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
34  * $FreeBSD: src/sys/sys/socketvar.h,v 1.46.2.10 2003/08/24 08:24:39 hsu Exp $
35  * $DragonFly: src/sys/sys/socketvar.h,v 1.35 2008/08/28 23:15:45 dillon Exp $
36  */
37
38 #ifndef _SYS_SOCKETVAR_H_
39 #define _SYS_SOCKETVAR_H_
40
41 #ifndef _SYS_TYPES_H_
42 #include <sys/types.h>
43 #endif
44 #ifndef _SYS_QUEUE_H_
45 #include <sys/queue.h>                  /* for TAILQ macros */
46 #endif
47 #ifndef _SYS_SELINFO_H_
48 #include <sys/selinfo.h>                /* for struct selinfo */
49 #endif
50 #ifndef _SYS_SOCKBUF_H_
51 #include <sys/sockbuf.h>
52 #endif
53
54 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
55
56 struct accept_filter;
57
58 /*
59  * Signaling socket buffers contain additional elements for locking
60  * and signaling conditions.  These are used primarily by sockets.
61  */
62 struct signalsockbuf {
63         struct sockbuf sb;
64         struct selinfo ssb_sel; /* process selecting read/write */
65         short   ssb_flags;      /* flags, see below */
66         short   ssb_timeo;      /* timeout for read/write */
67         long    ssb_lowat;      /* low water mark */
68         u_long  ssb_hiwat;      /* high water mark / max actual char count */
69         u_long  ssb_mbmax;      /* max chars of mbufs to use */
70 };
71
72 #define ssb_cc          sb.sb_cc        /* commonly used fields */
73 #define ssb_mb          sb.sb_mb        /* commonly used fields */
74 #define ssb_mbcnt       sb.sb_mbcnt     /* commonly used fields */
75
76 #define SSB_LOCK        0x01            /* lock on data queue */
77 #define SSB_WANT        0x02            /* someone is waiting to lock */
78 #define SSB_WAIT        0x04            /* someone is waiting for data/space */
79 #define SSB_SEL         0x08            /* someone is selecting */
80 #define SSB_ASYNC       0x10            /* ASYNC I/O, need signals */
81 #define SSB_UPCALL      0x20            /* someone wants an upcall */
82 #define SSB_NOINTR      0x40            /* operations not interruptible */
83 #define SSB_AIO         0x80            /* AIO operations queued */
84 #define SSB_KNOTE       0x100           /* kernel note attached */
85 #define SSB_MEVENT      0x200           /* need message event notification */
86 #define SSB_STOP        0x400           /* backpressure indicator */
87 #define SSB_AUTOSIZE    0x800           /* automatically size socket buffer */
88 #define SSB_AUTOLOWAT   0x1000          /* automatically scale lowat */
89
90 /*
91  * Per-socket kernel structure.  Contains universal send and receive queues,
92  * protocol control handle, and error information.
93  */
94 struct socket {
95         short   so_type;                /* generic type, see socket.h */
96         short   so_options;             /* from socket call, see socket.h */
97         short   so_linger;              /* time to linger while closing */
98         short   so_state;               /* internal state flags SS_*, below */
99         void    *so_pcb;                /* protocol control block */
100         struct  protosw *so_proto;      /* protocol handle */
101         struct  socket *so_head;        /* back pointer to accept socket */
102
103         /*
104          * These fields are used to manage sockets capable of accepting
105          * new connections.
106          */
107         TAILQ_HEAD(, socket) so_incomp; /* in-progress, incomplete */
108         TAILQ_HEAD(, socket) so_comp;   /* completed but not yet accepted */
109         TAILQ_ENTRY(socket) so_list;    /* list of unaccepted connections */
110         short   so_qlen;                /* so_comp count */
111         short   so_incqlen;             /* so_incomp count */
112         short   so_qlimit;              /* max number queued connections */
113
114         /*
115          * Misc socket support
116          */
117         short   so_timeo;               /* connection timeout */
118         u_short so_error;               /* error affecting connection */
119         struct  sigio *so_sigio;        /* information for async I/O or
120                                            out of band data (SIGURG) */
121         u_long  so_oobmark;             /* chars to oob mark */
122         TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
123         struct signalsockbuf so_rcv;
124         struct signalsockbuf so_snd;
125
126         void    (*so_upcall) (struct socket *, void *, int);
127         void    *so_upcallarg;
128         struct  ucred *so_cred;         /* user credentials */
129         /* NB: generation count must not be first; easiest to make it last. */
130         void    *so_emuldata;           /* private data for emulators */
131         struct  so_accf { 
132                 struct  accept_filter *so_accept_filter;
133                 void    *so_accept_filter_arg;  /* saved filter args */
134                 char    *so_accept_filter_str;  /* saved user args */
135         } *so_accf;
136 };
137
138 #endif
139
140 /*
141  * Socket state bits.
142  */
143 #define SS_NOFDREF              0x0001  /* no file table ref any more */
144 #define SS_ISCONNECTED          0x0002  /* socket connected to a peer */
145 #define SS_ISCONNECTING         0x0004  /* in process of connecting to peer */
146 #define SS_ISDISCONNECTING      0x0008  /* in process of disconnecting */
147 #define SS_CANTSENDMORE         0x0010  /* can't send more data to peer */
148 #define SS_CANTRCVMORE          0x0020  /* can't receive more data from peer */
149 #define SS_RCVATMARK            0x0040  /* at mark on input */
150
151 #define SS_ABORTING             0x0100  /* so_abort() in progress */
152 #define SS_ASYNC                0x0200  /* async i/o notify */
153 #define SS_ISCONFIRMING         0x0400  /* deciding to accept connection req */
154
155 #define SS_INCOMP               0x0800  /* unaccepted, incomplete connection */
156 #define SS_COMP                 0x1000  /* unaccepted, complete connection */
157 #define SS_ISDISCONNECTED       0x2000  /* socket disconnected from peer */
158
159 /*
160  * Externalized form of struct socket used by the sysctl(3) interface.
161  */
162 struct  xsocket {
163         size_t  xso_len;        /* length of this structure */
164         struct  socket *xso_so; /* makes a convenient handle sometimes */
165         short   so_type;
166         short   so_options;
167         short   so_linger;
168         short   so_state;
169         void    *so_pcb;                /* another convenient handle */
170         int     xso_protocol;
171         int     xso_family;
172         short   so_qlen;
173         short   so_incqlen;
174         short   so_qlimit;
175         short   so_timeo;
176         u_short so_error;
177         pid_t   so_pgid;
178         u_long  so_oobmark;
179         struct  xsockbuf {
180                 u_long  sb_cc;
181                 u_long  sb_hiwat;
182                 u_long  sb_mbcnt;
183                 u_long  sb_mbmax;
184                 long    sb_lowat;
185                 short   sb_flags;
186                 short   sb_timeo;
187         } so_rcv, so_snd;
188         uid_t   so_uid;         /* XXX */
189 };
190
191 /*
192  * Macros for sockets and socket buffering.
193  */
194
195 #define sosendallatonce(so) \
196     ((so)->so_proto->pr_flags & PR_ATOMIC)
197
198 /* can we read something from so? */
199 #define soreadable(so) \
200     ((so)->so_rcv.ssb_cc >= (so)->so_rcv.ssb_lowat || \
201         ((so)->so_state & SS_CANTRCVMORE) || \
202         !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
203
204 /* can we write something to so? */
205 #define sowriteable(so) \
206     ((ssb_space(&(so)->so_snd) >= (so)->so_snd.ssb_lowat && \
207         (((so)->so_state&SS_ISCONNECTED) || \
208           ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
209      ((so)->so_state & SS_CANTSENDMORE) || \
210      (so)->so_error)
211
212 /*
213  * Do we need to notify the other side when I/O is possible?
214  */
215 #define ssb_notify(ssb)                                 \
216         (((ssb)->ssb_flags &                            \
217         (SSB_WAIT | SSB_SEL | SSB_ASYNC | SSB_UPCALL |  \
218         SSB_AIO | SSB_KNOTE | SSB_MEVENT)))
219
220 /* do we have to send all at once on a socket? */
221
222 #ifdef _KERNEL
223
224 /*
225  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
226  * This is problematical if the fields are unsigned, as the space might
227  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
228  * overflow and return 0.
229  *
230  * SSB_STOP ignores cc/hiwat and returns 0.  This is used by unix domain
231  * stream sockets to signal backpressure.
232  */
233 static __inline
234 long
235 ssb_space(struct signalsockbuf *ssb)
236 {
237         long bleft;
238         long mleft;
239
240         if (ssb->ssb_flags & SSB_STOP)
241                 return(0);
242         bleft = ssb->ssb_hiwat - ssb->ssb_cc;
243         mleft = ssb->ssb_mbmax - ssb->ssb_mbcnt;
244         return((bleft < mleft) ? bleft : mleft);
245 }
246
247 #endif
248
249 #define ssb_append(ssb, m)                                              \
250         sbappend(&(ssb)->sb, m)
251
252 #define ssb_appendstream(ssb, m)                                        \
253         sbappendstream(&(ssb)->sb, m)
254
255 #define ssb_appendrecord(ssb, m)                                        \
256         sbappendrecord(&(ssb)->sb, m)
257
258 #define ssb_appendaddr(ssb, src, m, control)                            \
259         ((ssb_space(ssb) <= 0) ? 0 : sbappendaddr(&(ssb)->sb, src, m, control))
260
261 #define ssb_appendcontrol(ssb, m, control)                              \
262         ((ssb_space(ssb) <= 0) ? 0 : sbappendcontrol(&(ssb)->sb, m, control))
263
264 #define ssb_insert_knote(ssb, kn) {                                     \
265         SLIST_INSERT_HEAD(&(ssb)->ssb_sel.si_note, kn, kn_selnext);     \
266         (ssb)->ssb_flags |= SSB_KNOTE;                                  \
267 }
268
269 #define ssb_remove_knote(ssb, kn) {                                     \
270         SLIST_REMOVE(&(ssb)->ssb_sel.si_note, kn, knote, kn_selnext);   \
271         if (SLIST_EMPTY(&(ssb)->ssb_sel.si_note))                       \
272                 (ssb)->ssb_flags &= ~SSB_KNOTE;                         \
273 }
274
275 #define sorwakeup(so)   do { \
276                           if (ssb_notify(&(so)->so_rcv)) \
277                             sowakeup((so), &(so)->so_rcv); \
278                         } while (0)
279
280 #define sowwakeup(so)   do { \
281                           if (ssb_notify(&(so)->so_snd)) \
282                             sowakeup((so), &(so)->so_snd); \
283                         } while (0)
284
285 #ifdef _KERNEL
286
287 /*
288  * Argument structure for sosetopt et seq.  This is in the KERNEL
289  * section because it will never be visible to user code.
290  */
291 enum sopt_dir { SOPT_GET, SOPT_SET };
292 struct sockopt {
293         enum    sopt_dir sopt_dir; /* is this a get or a set? */
294         int     sopt_level;     /* second arg of [gs]etsockopt */
295         int     sopt_name;      /* third arg of [gs]etsockopt */
296         void   *sopt_val;       /* fourth arg of [gs]etsockopt */
297         size_t  sopt_valsize;   /* (almost) fifth arg of [gs]etsockopt */
298         struct  thread *sopt_td; /* calling thread or null if kernel */
299 };
300
301 struct accept_filter {
302         char    accf_name[16];
303         void    (*accf_callback)
304                 (struct socket *so, void *arg, int waitflag);
305         void *  (*accf_create)
306                 (struct socket *so, char *arg);
307         void    (*accf_destroy)
308                 (struct socket *so);
309         SLIST_ENTRY(accept_filter) accf_next;   /* next on the list */
310 };
311
312 #ifdef MALLOC_DECLARE
313 MALLOC_DECLARE(M_PCB);
314 MALLOC_DECLARE(M_SONAME);
315 MALLOC_DECLARE(M_ACCF);
316 #endif
317
318 extern int      maxsockets;
319 extern u_long   sb_max;         /* nominal limit */
320 extern u_long   sb_max_adj;     /* actual limit used by sbreserve() */
321
322 struct file;
323 struct filedesc;
324 struct mbuf;
325 struct rlimit;
326 struct sockaddr;
327 struct stat;
328 struct ucred;
329 struct uio;
330 struct knote;
331 struct sysmsg;
332
333 /*
334  * File operations on sockets.
335  */
336 int     soo_read (struct file *fp, struct uio *uio, struct ucred *cred,
337                         int flags);
338 int     soo_write (struct file *fp, struct uio *uio, struct ucred *cred,
339                         int flags);
340 int     soo_close (struct file *fp);
341 int     soo_shutdown (struct file *fp, int how);
342 int     soo_ioctl (struct file *fp, u_long cmd, caddr_t data,
343                         struct ucred *cred, struct sysmsg *msg);
344 int     soo_poll (struct file *fp, int events, struct ucred *cred);
345 int     soo_stat (struct file *fp, struct stat *ub, struct ucred *cred);
346 int     sokqfilter (struct file *fp, struct knote *kn);
347
348 /*
349  * From uipc_socket and friends
350  */
351 struct  sockaddr *dup_sockaddr (const struct sockaddr *sa);
352 int     getsockaddr (struct sockaddr **namp, caddr_t uaddr, size_t len);
353
354 void    ssb_release (struct signalsockbuf *ssb, struct socket *so);
355 int     ssb_reserve (struct signalsockbuf *ssb, u_long cc, struct socket *so,
356                    struct rlimit *rl);
357 void    ssbtoxsockbuf (struct signalsockbuf *sb, struct xsockbuf *xsb);
358 int     ssb_wait (struct signalsockbuf *sb);
359 int     _ssb_lock (struct signalsockbuf *sb);
360
361 void    soabort (struct socket *so);
362 void    soaborta (struct socket *so);
363 void    soabort_oncpu (struct socket *so);
364 int     soaccept (struct socket *so, struct sockaddr **nam);
365 struct  socket *soalloc (int waitok);
366 int     sobind (struct socket *so, struct sockaddr *nam, struct thread *td);
367 void    socantrcvmore (struct socket *so);
368 void    socantsendmore (struct socket *so);
369 int     soclose (struct socket *so, int fflags);
370 int     soconnect (struct socket *so, struct sockaddr *nam, struct thread *td);
371 int     soconnect2 (struct socket *so1, struct socket *so2);
372 int     socreate (int dom, struct socket **aso, int type, int proto,
373             struct thread *td);
374 void    sodealloc (struct socket *so);
375 int     sodisconnect (struct socket *so);
376 void    sofree (struct socket *so);
377 int     sogetopt (struct socket *so, struct sockopt *sopt);
378 void    sohasoutofband (struct socket *so);
379 void    soisconnected (struct socket *so);
380 void    soisconnecting (struct socket *so);
381 void    soisdisconnected (struct socket *so);
382 void    soisdisconnecting (struct socket *so);
383 int     solisten (struct socket *so, int backlog, struct thread *td);
384 struct socket *sonewconn (struct socket *head, int connstatus);
385 int     sooptcopyin (struct sockopt *sopt, void *buf, size_t len,
386                          size_t minlen);
387 int     soopt_to_kbuf (struct sockopt *sopt, void *buf, size_t len,
388                          size_t minlen);
389 int     sooptcopyout (struct sockopt *sopt, const void *buf, size_t len);
390 void    soopt_from_kbuf (struct sockopt *sopt, const void *buf, size_t len);
391
392 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
393 int     soopt_getm (struct sockopt *sopt, struct mbuf **mp);
394 int     soopt_mcopyin (struct sockopt *sopt, struct mbuf *m);
395 void    soopt_to_mbuf (struct sockopt *sopt, struct mbuf *m);
396 int     soopt_mcopyout (struct sockopt *sopt, struct mbuf *m);
397 int     soopt_from_mbuf (struct sockopt *sopt, struct mbuf *m);
398
399 int     sopoll (struct socket *so, int events, struct ucred *cred,
400                     struct thread *td);
401 int     soreceive (struct socket *so, struct sockaddr **paddr,
402                        struct uio *uio, struct sockbuf *sio,
403                        struct mbuf **controlp, int *flagsp);
404 int     soreserve (struct socket *so, u_long sndcc, u_long rcvcc,
405                    struct rlimit *rl);
406 void    sorflush (struct socket *so);
407 int     sosend (struct socket *so, struct sockaddr *addr, struct uio *uio,
408                     struct mbuf *top, struct mbuf *control, int flags,
409                     struct thread *td);
410 int     sosendudp (struct socket *so, struct sockaddr *addr, struct uio *uio,
411                     struct mbuf *top, struct mbuf *control, int flags,
412                     struct thread *td);
413 int     sosetopt (struct socket *so, struct sockopt *sopt);
414 int     soshutdown (struct socket *so, int how);
415 void    sotoxsocket (struct socket *so, struct xsocket *xso);
416 void    sowakeup (struct socket *so, struct signalsockbuf *sb);
417
418 /* accept filter functions */
419 int     accept_filt_add (struct accept_filter *filt);
420 int     accept_filt_del (char *name);
421 struct accept_filter *  accept_filt_get (char *name);
422 #ifdef ACCEPT_FILTER_MOD
423 int accept_filt_generic_mod_event (module_t mod, int event, void *data);
424 SYSCTL_DECL(_net_inet_accf);
425 #endif /* ACCEPT_FILTER_MOD */
426
427 #endif /* _KERNEL */
428
429 #endif /* !_SYS_SOCKETVAR_H_ */