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