]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/sockbuf.h
Merge bmake-20150505 improve detection of malformed conditionals.
[FreeBSD/FreeBSD.git] / sys / sys / sockbuf.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  * 4. 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  *
31  * $FreeBSD$
32  */
33 #ifndef _SYS_SOCKBUF_H_
34 #define _SYS_SOCKBUF_H_
35 #include <sys/selinfo.h>                /* for struct selinfo */
36 #include <sys/_lock.h>
37 #include <sys/_mutex.h>
38 #include <sys/_sx.h>
39
40 #define SB_MAX          (2*1024*1024)   /* default for max chars in sockbuf */
41
42 /*
43  * Constants for sb_flags field of struct sockbuf.
44  */
45 #define SB_WAIT         0x04            /* someone is waiting for data/space */
46 #define SB_SEL          0x08            /* someone is selecting */
47 #define SB_ASYNC        0x10            /* ASYNC I/O, need signals */
48 #define SB_UPCALL       0x20            /* someone wants an upcall */
49 #define SB_NOINTR       0x40            /* operations not interruptible */
50 #define SB_AIO          0x80            /* AIO operations queued */
51 #define SB_KNOTE        0x100           /* kernel note attached */
52 #define SB_NOCOALESCE   0x200           /* don't coalesce new data into existing mbufs */
53 #define SB_IN_TOE       0x400           /* socket buffer is in the middle of an operation */
54 #define SB_AUTOSIZE     0x800           /* automatically size socket buffer */
55 #define SB_STOP         0x1000          /* backpressure indicator */
56
57 #define SBS_CANTSENDMORE        0x0010  /* can't send more data to peer */
58 #define SBS_CANTRCVMORE         0x0020  /* can't receive more data from peer */
59 #define SBS_RCVATMARK           0x0040  /* at mark on input */
60
61 struct mbuf;
62 struct sockaddr;
63 struct socket;
64 struct thread;
65
66 struct  xsockbuf {
67         u_int   sb_cc;
68         u_int   sb_hiwat;
69         u_int   sb_mbcnt;
70         u_int   sb_mcnt;
71         u_int   sb_ccnt;
72         u_int   sb_mbmax;
73         int     sb_lowat;
74         int     sb_timeo;
75         short   sb_flags;
76 };
77
78 /*
79  * Variables for socket buffering.
80  */
81 struct  sockbuf {
82         struct  selinfo sb_sel; /* process selecting read/write */
83         struct  mtx sb_mtx;     /* sockbuf lock */
84         struct  sx sb_sx;       /* prevent I/O interlacing */
85         short   sb_state;       /* (c/d) socket state on sockbuf */
86 #define sb_startzero    sb_mb
87         struct  mbuf *sb_mb;    /* (c/d) the mbuf chain */
88         struct  mbuf *sb_mbtail; /* (c/d) the last mbuf in the chain */
89         struct  mbuf *sb_lastrecord;    /* (c/d) first mbuf of last
90                                          * record in socket buffer */
91         struct  mbuf *sb_sndptr; /* (c/d) pointer into mbuf chain */
92         struct  mbuf *sb_fnrdy; /* (c/d) pointer to first not ready buffer */
93         u_int   sb_sndptroff;   /* (c/d) byte offset of ptr into chain */
94         u_int   sb_acc;         /* (c/d) available chars in buffer */
95         u_int   sb_ccc;         /* (c/d) claimed chars in buffer */
96         u_int   sb_hiwat;       /* (c/d) max actual char count */
97         u_int   sb_mbcnt;       /* (c/d) chars of mbufs used */
98         u_int   sb_mcnt;        /* (c/d) number of mbufs in buffer */
99         u_int   sb_ccnt;        /* (c/d) number of clusters in buffer */
100         u_int   sb_mbmax;       /* (c/d) max chars of mbufs to use */
101         u_int   sb_ctl;         /* (c/d) non-data chars in buffer */
102         int     sb_lowat;       /* (c/d) low water mark */
103         sbintime_t      sb_timeo;       /* (c/d) timeout for read/write */
104         short   sb_flags;       /* (c/d) flags, see below */
105         int     (*sb_upcall)(struct socket *, void *, int); /* (c/d) */
106         void    *sb_upcallarg;  /* (c/d) */
107 };
108
109 #ifdef _KERNEL
110
111 /*
112  * Per-socket buffer mutex used to protect most fields in the socket
113  * buffer.
114  */
115 #define SOCKBUF_MTX(_sb)                (&(_sb)->sb_mtx)
116 #define SOCKBUF_LOCK_INIT(_sb, _name) \
117         mtx_init(SOCKBUF_MTX(_sb), _name, NULL, MTX_DEF)
118 #define SOCKBUF_LOCK_DESTROY(_sb)       mtx_destroy(SOCKBUF_MTX(_sb))
119 #define SOCKBUF_LOCK(_sb)               mtx_lock(SOCKBUF_MTX(_sb))
120 #define SOCKBUF_OWNED(_sb)              mtx_owned(SOCKBUF_MTX(_sb))
121 #define SOCKBUF_UNLOCK(_sb)             mtx_unlock(SOCKBUF_MTX(_sb))
122 #define SOCKBUF_LOCK_ASSERT(_sb)        mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED)
123 #define SOCKBUF_UNLOCK_ASSERT(_sb)      mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED)
124
125 /*
126  * Socket buffer private mbuf(9) flags.
127  */
128 #define M_NOTREADY      M_PROTO1        /* m_data not populated yet */
129 #define M_BLOCKED       M_PROTO2        /* M_NOTREADY in front of m */
130 #define M_NOTAVAIL      (M_NOTREADY | M_BLOCKED)
131
132 void    sbappend(struct sockbuf *sb, struct mbuf *m);
133 void    sbappend_locked(struct sockbuf *sb, struct mbuf *m);
134 void    sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags);
135 void    sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags);
136 int     sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
137             struct mbuf *m0, struct mbuf *control);
138 int     sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
139             struct mbuf *m0, struct mbuf *control);
140 int     sbappendaddr_nospacecheck_locked(struct sockbuf *sb,
141             const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control);
142 int     sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
143             struct mbuf *control);
144 int     sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
145             struct mbuf *control);
146 void    sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
147 void    sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0);
148 void    sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
149 struct mbuf *
150         sbcreatecontrol(caddr_t p, int size, int type, int level);
151 void    sbdestroy(struct sockbuf *sb, struct socket *so);
152 void    sbdrop(struct sockbuf *sb, int len);
153 void    sbdrop_locked(struct sockbuf *sb, int len);
154 struct mbuf *
155         sbcut_locked(struct sockbuf *sb, int len);
156 void    sbdroprecord(struct sockbuf *sb);
157 void    sbdroprecord_locked(struct sockbuf *sb);
158 void    sbflush(struct sockbuf *sb);
159 void    sbflush_locked(struct sockbuf *sb);
160 void    sbrelease(struct sockbuf *sb, struct socket *so);
161 void    sbrelease_internal(struct sockbuf *sb, struct socket *so);
162 void    sbrelease_locked(struct sockbuf *sb, struct socket *so);
163 int     sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
164             struct thread *td);
165 int     sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
166             struct thread *td);
167 struct mbuf *
168         sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff);
169 struct mbuf *
170         sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff);
171 void    sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
172 int     sbwait(struct sockbuf *sb);
173 int     sblock(struct sockbuf *sb, int flags);
174 void    sbunlock(struct sockbuf *sb);
175 void    sballoc(struct sockbuf *, struct mbuf *);
176 void    sbfree(struct sockbuf *, struct mbuf *);
177 int     sbready(struct sockbuf *, struct mbuf *, int);
178
179 /*
180  * Return how much data is available to be taken out of socket
181  * buffer right now.
182  */
183 static inline u_int
184 sbavail(struct sockbuf *sb)
185 {
186
187 #if 0
188         SOCKBUF_LOCK_ASSERT(sb);
189 #endif
190         return (sb->sb_acc);
191 }
192
193 /*
194  * Return how much data sits there in the socket buffer
195  * It might be that some data is not yet ready to be read.
196  */
197 static inline u_int
198 sbused(struct sockbuf *sb)
199 {
200
201 #if 0
202         SOCKBUF_LOCK_ASSERT(sb);
203 #endif
204         return (sb->sb_ccc);
205 }
206
207 /*
208  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
209  * This is problematical if the fields are unsigned, as the space might
210  * still be negative (ccc > hiwat or mbcnt > mbmax).
211  */
212 static inline long
213 sbspace(struct sockbuf *sb)
214 {
215         int bleft, mleft;               /* size should match sockbuf fields */
216
217 #if 0
218         SOCKBUF_LOCK_ASSERT(sb);
219 #endif
220
221         if (sb->sb_flags & SB_STOP)
222                 return(0);
223
224         bleft = sb->sb_hiwat - sb->sb_ccc;
225         mleft = sb->sb_mbmax - sb->sb_mbcnt;
226
227         return ((bleft < mleft) ? bleft : mleft);
228 }
229
230 #define SB_EMPTY_FIXUP(sb) do {                                         \
231         if ((sb)->sb_mb == NULL) {                                      \
232                 (sb)->sb_mbtail = NULL;                                 \
233                 (sb)->sb_lastrecord = NULL;                             \
234         }                                                               \
235 } while (/*CONSTCOND*/0)
236
237 #ifdef SOCKBUF_DEBUG
238 void    sblastrecordchk(struct sockbuf *, const char *, int);
239 void    sblastmbufchk(struct sockbuf *, const char *, int);
240 void    sbcheck(struct sockbuf *, const char *, int);
241 #define SBLASTRECORDCHK(sb)     sblastrecordchk((sb), __FILE__, __LINE__)
242 #define SBLASTMBUFCHK(sb)       sblastmbufchk((sb), __FILE__, __LINE__)
243 #define SBCHECK(sb)             sbcheck((sb), __FILE__, __LINE__)
244 #else
245 #define SBLASTRECORDCHK(sb)     do {} while (0)
246 #define SBLASTMBUFCHK(sb)       do {} while (0)
247 #define SBCHECK(sb)             do {} while (0)
248 #endif /* SOCKBUF_DEBUG */
249
250 #endif /* _KERNEL */
251
252 #endif /* _SYS_SOCKBUF_H_ */