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