1/*- 2 * Copyright (c) 1982, 1986, 1988, 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 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 30 */ 31 32#include <sys/cdefs.h>
| 1/*- 2 * Copyright (c) 1982, 1986, 1988, 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 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 30 */ 31 32#include <sys/cdefs.h>
|
33__FBSDID("$FreeBSD: head/sys/kern/uipc_sockbuf.c 160875 2006-08-01 10:30:26Z rwatson $");
| 33__FBSDID("$FreeBSD: head/sys/kern/uipc_sockbuf.c 160915 2006-08-02 13:01:58Z rwatson $");
|
34 35#include "opt_param.h" 36 37#include <sys/param.h> 38#include <sys/aio.h> /* for aio_swake proto */ 39#include <sys/kernel.h> 40#include <sys/lock.h> 41#include <sys/mbuf.h> 42#include <sys/mutex.h> 43#include <sys/proc.h> 44#include <sys/protosw.h> 45#include <sys/resourcevar.h> 46#include <sys/signalvar.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/sysctl.h> 50 51/* 52 * Function pointer set by the AIO routines so that the socket buffer code 53 * can call back into the AIO module if it is loaded. 54 */ 55void (*aio_swake)(struct socket *, struct sockbuf *); 56 57/* 58 * Primitive routines for operating on socket buffers 59 */ 60 61u_long sb_max = SB_MAX; 62static u_long sb_max_adj = 63 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ 64 65static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 66
| 34 35#include "opt_param.h" 36 37#include <sys/param.h> 38#include <sys/aio.h> /* for aio_swake proto */ 39#include <sys/kernel.h> 40#include <sys/lock.h> 41#include <sys/mbuf.h> 42#include <sys/mutex.h> 43#include <sys/proc.h> 44#include <sys/protosw.h> 45#include <sys/resourcevar.h> 46#include <sys/signalvar.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/sysctl.h> 50 51/* 52 * Function pointer set by the AIO routines so that the socket buffer code 53 * can call back into the AIO module if it is loaded. 54 */ 55void (*aio_swake)(struct socket *, struct sockbuf *); 56 57/* 58 * Primitive routines for operating on socket buffers 59 */ 60 61u_long sb_max = SB_MAX; 62static u_long sb_max_adj = 63 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ 64 65static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 66
|
67static void sbdrop_internal(register struct sockbuf *sb, register int len); 68static void sbflush_internal(register struct sockbuf *sb);
| 67static void sbdrop_internal(struct sockbuf *sb, int len); 68static void sbflush_internal(struct sockbuf *sb);
|
69static void sbrelease_internal(struct sockbuf *sb, struct socket *so); 70 71/*
| 69static void sbrelease_internal(struct sockbuf *sb, struct socket *so); 70 71/*
|
72 * Socantsendmore indicates that no more data will be sent on the 73 * socket; it would normally be applied to a socket when the user 74 * informs the system that no more data is to be sent, by the protocol 75 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 76 * will be received, and will normally be applied to the socket by a 77 * protocol when it detects that the peer will send no more data. 78 * Data queued for reading in the socket may yet be read.
| 72 * Socantsendmore indicates that no more data will be sent on the socket; it 73 * would normally be applied to a socket when the user informs the system 74 * that no more data is to be sent, by the protocol code (in case 75 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be 76 * received, and will normally be applied to the socket by a protocol when it 77 * detects that the peer will send no more data. Data queued for reading in 78 * the socket may yet be read.
|
79 */ 80void
| 79 */ 80void
|
81socantsendmore_locked(so) 82 struct socket *so;
| 81socantsendmore_locked(struct socket *so)
|
83{ 84 85 SOCKBUF_LOCK_ASSERT(&so->so_snd); 86 87 so->so_snd.sb_state |= SBS_CANTSENDMORE; 88 sowwakeup_locked(so); 89 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 90} 91 92void
| 82{ 83 84 SOCKBUF_LOCK_ASSERT(&so->so_snd); 85 86 so->so_snd.sb_state |= SBS_CANTSENDMORE; 87 sowwakeup_locked(so); 88 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 89} 90 91void
|
93socantsendmore(so) 94 struct socket *so;
| 92socantsendmore(struct socket *so)
|
95{ 96 97 SOCKBUF_LOCK(&so->so_snd); 98 socantsendmore_locked(so); 99 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 100} 101 102void
| 93{ 94 95 SOCKBUF_LOCK(&so->so_snd); 96 socantsendmore_locked(so); 97 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 98} 99 100void
|
103socantrcvmore_locked(so) 104 struct socket *so;
| 101socantrcvmore_locked(struct socket *so)
|
105{ 106 107 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 108 109 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 110 sorwakeup_locked(so); 111 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 112} 113 114void
| 102{ 103 104 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 105 106 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 107 sorwakeup_locked(so); 108 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 109} 110 111void
|
115socantrcvmore(so) 116 struct socket *so;
| 112socantrcvmore(struct socket *so)
|
117{ 118 119 SOCKBUF_LOCK(&so->so_rcv); 120 socantrcvmore_locked(so); 121 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 122} 123 124/* 125 * Wait for data to arrive at/drain from a socket buffer. 126 */ 127int
| 113{ 114 115 SOCKBUF_LOCK(&so->so_rcv); 116 socantrcvmore_locked(so); 117 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 118} 119 120/* 121 * Wait for data to arrive at/drain from a socket buffer. 122 */ 123int
|
128sbwait(sb) 129 struct sockbuf *sb;
| 124sbwait(struct sockbuf *sb)
|
130{ 131 132 SOCKBUF_LOCK_ASSERT(sb); 133 134 sb->sb_flags |= SB_WAIT; 135 return (msleep(&sb->sb_cc, &sb->sb_mtx, 136 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 137 sb->sb_timeo)); 138} 139 140/*
| 125{ 126 127 SOCKBUF_LOCK_ASSERT(sb); 128 129 sb->sb_flags |= SB_WAIT; 130 return (msleep(&sb->sb_cc, &sb->sb_mtx, 131 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 132 sb->sb_timeo)); 133} 134 135/*
|
141 * Lock a sockbuf already known to be locked; 142 * return any error returned from sleep (EINTR).
| 136 * Lock a sockbuf already known to be locked; return any error returned from 137 * sleep (EINTR).
|
143 */ 144int
| 138 */ 139int
|
145sb_lock(sb) 146 register struct sockbuf *sb;
| 140sb_lock(struct sockbuf *sb)
|
147{ 148 int error; 149 150 SOCKBUF_LOCK_ASSERT(sb); 151 152 while (sb->sb_flags & SB_LOCK) { 153 sb->sb_flags |= SB_WANT; 154 error = msleep(&sb->sb_flags, &sb->sb_mtx, 155 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 156 "sblock", 0); 157 if (error) 158 return (error); 159 } 160 sb->sb_flags |= SB_LOCK; 161 return (0); 162} 163 164/*
| 141{ 142 int error; 143 144 SOCKBUF_LOCK_ASSERT(sb); 145 146 while (sb->sb_flags & SB_LOCK) { 147 sb->sb_flags |= SB_WANT; 148 error = msleep(&sb->sb_flags, &sb->sb_mtx, 149 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 150 "sblock", 0); 151 if (error) 152 return (error); 153 } 154 sb->sb_flags |= SB_LOCK; 155 return (0); 156} 157 158/*
|
165 * Wakeup processes waiting on a socket buffer. Do asynchronous 166 * notification via SIGIO if the socket has the SS_ASYNC flag set.
| 159 * Wakeup processes waiting on a socket buffer. Do asynchronous notification 160 * via SIGIO if the socket has the SS_ASYNC flag set.
|
167 * 168 * Called with the socket buffer lock held; will release the lock by the end 169 * of the function. This allows the caller to acquire the socket buffer lock 170 * while testing for the need for various sorts of wakeup and hold it through 171 * to the point where it's no longer required. We currently hold the lock 172 * through calls out to other subsystems (with the exception of kqueue), and 173 * then release it to avoid lock order issues. It's not clear that's 174 * correct. 175 */ 176void
| 161 * 162 * Called with the socket buffer lock held; will release the lock by the end 163 * of the function. This allows the caller to acquire the socket buffer lock 164 * while testing for the need for various sorts of wakeup and hold it through 165 * to the point where it's no longer required. We currently hold the lock 166 * through calls out to other subsystems (with the exception of kqueue), and 167 * then release it to avoid lock order issues. It's not clear that's 168 * correct. 169 */ 170void
|
177sowakeup(so, sb) 178 register struct socket *so; 179 register struct sockbuf *sb;
| 171sowakeup(struct socket *so, struct sockbuf *sb)
|
180{ 181 182 SOCKBUF_LOCK_ASSERT(sb); 183 184 selwakeuppri(&sb->sb_sel, PSOCK); 185 sb->sb_flags &= ~SB_SEL; 186 if (sb->sb_flags & SB_WAIT) { 187 sb->sb_flags &= ~SB_WAIT; 188 wakeup(&sb->sb_cc); 189 } 190 KNOTE_LOCKED(&sb->sb_sel.si_note, 0); 191 SOCKBUF_UNLOCK(sb); 192 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 193 pgsigio(&so->so_sigio, SIGIO, 0); 194 if (sb->sb_flags & SB_UPCALL) 195 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); 196 if (sb->sb_flags & SB_AIO) 197 aio_swake(so, sb); 198 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED); 199} 200 201/* 202 * Socket buffer (struct sockbuf) utility routines. 203 *
| 172{ 173 174 SOCKBUF_LOCK_ASSERT(sb); 175 176 selwakeuppri(&sb->sb_sel, PSOCK); 177 sb->sb_flags &= ~SB_SEL; 178 if (sb->sb_flags & SB_WAIT) { 179 sb->sb_flags &= ~SB_WAIT; 180 wakeup(&sb->sb_cc); 181 } 182 KNOTE_LOCKED(&sb->sb_sel.si_note, 0); 183 SOCKBUF_UNLOCK(sb); 184 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 185 pgsigio(&so->so_sigio, SIGIO, 0); 186 if (sb->sb_flags & SB_UPCALL) 187 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); 188 if (sb->sb_flags & SB_AIO) 189 aio_swake(so, sb); 190 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED); 191} 192 193/* 194 * Socket buffer (struct sockbuf) utility routines. 195 *
|
204 * Each socket contains two socket buffers: one for sending data and 205 * one for receiving data. Each buffer contains a queue of mbufs, 206 * information about the number of mbufs and amount of data in the 207 * queue, and other fields allowing select() statements and notification 208 * on data availability to be implemented.
| 196 * Each socket contains two socket buffers: one for sending data and one for 197 * receiving data. Each buffer contains a queue of mbufs, information about 198 * the number of mbufs and amount of data in the queue, and other fields 199 * allowing select() statements and notification on data availability to be 200 * implemented.
|
209 *
| 201 *
|
210 * Data stored in a socket buffer is maintained as a list of records. 211 * Each record is a list of mbufs chained together with the m_next 212 * field. Records are chained together with the m_nextpkt field. The upper 213 * level routine soreceive() expects the following conventions to be 214 * observed when placing information in the receive buffer:
| 202 * Data stored in a socket buffer is maintained as a list of records. Each 203 * record is a list of mbufs chained together with the m_next field. Records 204 * are chained together with the m_nextpkt field. The upper level routine 205 * soreceive() expects the following conventions to be observed when placing 206 * information in the receive buffer:
|
215 *
| 207 *
|
216 * 1. If the protocol requires each message be preceded by the sender's 217 * name, then a record containing that name must be present before 218 * any associated data (mbuf's must be of type MT_SONAME). 219 * 2. If the protocol supports the exchange of ``access rights'' (really 220 * just additional data associated with the message), and there are 221 * ``rights'' to be received, then a record containing this data 222 * should be present (mbuf's must be of type MT_RIGHTS). 223 * 3. If a name or rights record exists, then it must be followed by 224 * a data record, perhaps of zero length.
| 208 * 1. If the protocol requires each message be preceded by the sender's name, 209 * then a record containing that name must be present before any 210 * associated data (mbuf's must be of type MT_SONAME). 211 * 2. If the protocol supports the exchange of ``access rights'' (really just 212 * additional data associated with the message), and there are ``rights'' 213 * to be received, then a record containing this data should be present 214 * (mbuf's must be of type MT_RIGHTS). 215 * 3. If a name or rights record exists, then it must be followed by a data 216 * record, perhaps of zero length.
|
225 * 226 * Before using a new socket structure it is first necessary to reserve 227 * buffer space to the socket, by calling sbreserve(). This should commit 228 * some of the available buffer space in the system buffer pool for the
| 217 * 218 * Before using a new socket structure it is first necessary to reserve 219 * buffer space to the socket, by calling sbreserve(). This should commit 220 * some of the available buffer space in the system buffer pool for the
|
229 * socket (currently, it does nothing but enforce limits). The space 230 * should be released by calling sbrelease() when the socket is destroyed.
| 221 * socket (currently, it does nothing but enforce limits). The space should 222 * be released by calling sbrelease() when the socket is destroyed.
|
231 */
| 223 */
|
232
| |
233int
| 224int
|
234soreserve(so, sndcc, rcvcc) 235 register struct socket *so; 236 u_long sndcc, rcvcc;
| 225soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
|
237{ 238 struct thread *td = curthread; 239 240 SOCKBUF_LOCK(&so->so_snd); 241 SOCKBUF_LOCK(&so->so_rcv); 242 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0) 243 goto bad; 244 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0) 245 goto bad2; 246 if (so->so_rcv.sb_lowat == 0) 247 so->so_rcv.sb_lowat = 1; 248 if (so->so_snd.sb_lowat == 0) 249 so->so_snd.sb_lowat = MCLBYTES; 250 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 251 so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 252 SOCKBUF_UNLOCK(&so->so_rcv); 253 SOCKBUF_UNLOCK(&so->so_snd); 254 return (0); 255bad2: 256 sbrelease_locked(&so->so_snd, so); 257bad: 258 SOCKBUF_UNLOCK(&so->so_rcv); 259 SOCKBUF_UNLOCK(&so->so_snd); 260 return (ENOBUFS); 261} 262 263static int 264sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS) 265{ 266 int error = 0; 267 u_long old_sb_max = sb_max; 268 269 error = SYSCTL_OUT(req, arg1, sizeof(u_long)); 270 if (error || !req->newptr) 271 return (error); 272 error = SYSCTL_IN(req, arg1, sizeof(u_long)); 273 if (error) 274 return (error); 275 if (sb_max < MSIZE + MCLBYTES) { 276 sb_max = old_sb_max; 277 return (EINVAL); 278 } 279 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES); 280 return (0); 281} 282 283/*
| 226{ 227 struct thread *td = curthread; 228 229 SOCKBUF_LOCK(&so->so_snd); 230 SOCKBUF_LOCK(&so->so_rcv); 231 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0) 232 goto bad; 233 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0) 234 goto bad2; 235 if (so->so_rcv.sb_lowat == 0) 236 so->so_rcv.sb_lowat = 1; 237 if (so->so_snd.sb_lowat == 0) 238 so->so_snd.sb_lowat = MCLBYTES; 239 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 240 so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 241 SOCKBUF_UNLOCK(&so->so_rcv); 242 SOCKBUF_UNLOCK(&so->so_snd); 243 return (0); 244bad2: 245 sbrelease_locked(&so->so_snd, so); 246bad: 247 SOCKBUF_UNLOCK(&so->so_rcv); 248 SOCKBUF_UNLOCK(&so->so_snd); 249 return (ENOBUFS); 250} 251 252static int 253sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS) 254{ 255 int error = 0; 256 u_long old_sb_max = sb_max; 257 258 error = SYSCTL_OUT(req, arg1, sizeof(u_long)); 259 if (error || !req->newptr) 260 return (error); 261 error = SYSCTL_IN(req, arg1, sizeof(u_long)); 262 if (error) 263 return (error); 264 if (sb_max < MSIZE + MCLBYTES) { 265 sb_max = old_sb_max; 266 return (EINVAL); 267 } 268 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES); 269 return (0); 270} 271 272/*
|
284 * Allot mbufs to a sockbuf. 285 * Attempt to scale mbmax so that mbcnt doesn't become limiting 286 * if buffering efficiency is near the normal case.
| 273 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't 274 * become limiting if buffering efficiency is near the normal case.
|
287 */ 288int
| 275 */ 276int
|
289sbreserve_locked(sb, cc, so, td) 290 struct sockbuf *sb; 291 u_long cc; 292 struct socket *so; 293 struct thread *td;
| 277sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so, 278 struct thread *td)
|
294{ 295 rlim_t sbsize_limit; 296 297 SOCKBUF_LOCK_ASSERT(sb); 298 299 /*
| 279{ 280 rlim_t sbsize_limit; 281 282 SOCKBUF_LOCK_ASSERT(sb); 283 284 /*
|
300 * td will only be NULL when we're in an interrupt 301 * (e.g. in tcp_input())
| 285 * td will only be NULL when we're in an interrupt (e.g. in 286 * tcp_input()). 287 * 288 * XXXRW: This comment needs updating, as might the code.
|
302 */ 303 if (cc > sb_max_adj) 304 return (0); 305 if (td != NULL) { 306 PROC_LOCK(td->td_proc); 307 sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE); 308 PROC_UNLOCK(td->td_proc); 309 } else 310 sbsize_limit = RLIM_INFINITY; 311 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 312 sbsize_limit)) 313 return (0); 314 sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 315 if (sb->sb_lowat > sb->sb_hiwat) 316 sb->sb_lowat = sb->sb_hiwat; 317 return (1); 318} 319 320int
| 289 */ 290 if (cc > sb_max_adj) 291 return (0); 292 if (td != NULL) { 293 PROC_LOCK(td->td_proc); 294 sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE); 295 PROC_UNLOCK(td->td_proc); 296 } else 297 sbsize_limit = RLIM_INFINITY; 298 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 299 sbsize_limit)) 300 return (0); 301 sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 302 if (sb->sb_lowat > sb->sb_hiwat) 303 sb->sb_lowat = sb->sb_hiwat; 304 return (1); 305} 306 307int
|
321sbreserve(sb, cc, so, td) 322 struct sockbuf *sb; 323 u_long cc; 324 struct socket *so; 325 struct thread *td;
| 308sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, 309 struct thread *td)
|
326{ 327 int error; 328 329 SOCKBUF_LOCK(sb); 330 error = sbreserve_locked(sb, cc, so, td); 331 SOCKBUF_UNLOCK(sb); 332 return (error); 333} 334 335/* 336 * Free mbufs held by a socket, and reserved mbuf space. 337 */ 338static void
| 310{ 311 int error; 312 313 SOCKBUF_LOCK(sb); 314 error = sbreserve_locked(sb, cc, so, td); 315 SOCKBUF_UNLOCK(sb); 316 return (error); 317} 318 319/* 320 * Free mbufs held by a socket, and reserved mbuf space. 321 */ 322static void
|
339sbrelease_internal(sb, so) 340 struct sockbuf *sb; 341 struct socket *so;
| 323sbrelease_internal(struct sockbuf *sb, struct socket *so)
|
342{ 343 344 sbflush_internal(sb); 345 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 346 RLIM_INFINITY); 347 sb->sb_mbmax = 0; 348} 349 350void
| 324{ 325 326 sbflush_internal(sb); 327 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 328 RLIM_INFINITY); 329 sb->sb_mbmax = 0; 330} 331 332void
|
351sbrelease_locked(sb, so) 352 struct sockbuf *sb; 353 struct socket *so;
| 333sbrelease_locked(struct sockbuf *sb, struct socket *so)
|
354{ 355 356 SOCKBUF_LOCK_ASSERT(sb); 357 358 sbrelease_internal(sb, so); 359} 360 361void
| 334{ 335 336 SOCKBUF_LOCK_ASSERT(sb); 337 338 sbrelease_internal(sb, so); 339} 340 341void
|
362sbrelease(sb, so) 363 struct sockbuf *sb; 364 struct socket *so;
| 342sbrelease(struct sockbuf *sb, struct socket *so)
|
365{ 366 367 SOCKBUF_LOCK(sb); 368 sbrelease_locked(sb, so); 369 SOCKBUF_UNLOCK(sb); 370} 371 372void
| 343{ 344 345 SOCKBUF_LOCK(sb); 346 sbrelease_locked(sb, so); 347 SOCKBUF_UNLOCK(sb); 348} 349 350void
|
373sbdestroy(sb, so) 374 struct sockbuf *sb; 375 struct socket *so;
| 351sbdestroy(struct sockbuf *sb, struct socket *so)
|
376{ 377 378 sbrelease_internal(sb, so); 379} 380 381 382/*
| 352{ 353 354 sbrelease_internal(sb, so); 355} 356 357 358/*
|
383 * Routines to add and remove 384 * data from an mbuf queue.
| 359 * Routines to add and remove data from an mbuf queue.
|
385 *
| 360 *
|
386 * The routines sbappend() or sbappendrecord() are normally called to 387 * append new mbufs to a socket buffer, after checking that adequate 388 * space is available, comparing the function sbspace() with the amount 389 * of data to be added. sbappendrecord() differs from sbappend() in 390 * that data supplied is treated as the beginning of a new record. 391 * To place a sender's address, optional access rights, and data in a 392 * socket receive buffer, sbappendaddr() should be used. To place 393 * access rights and data in a socket receive buffer, sbappendrights() 394 * should be used. In either case, the new data begins a new record. 395 * Note that unlike sbappend() and sbappendrecord(), these routines check 396 * for the caller that there will be enough space to store the data. 397 * Each fails if there is not enough space, or if it cannot find mbufs 398 * to store additional information in.
| 361 * The routines sbappend() or sbappendrecord() are normally called to append 362 * new mbufs to a socket buffer, after checking that adequate space is 363 * available, comparing the function sbspace() with the amount of data to be 364 * added. sbappendrecord() differs from sbappend() in that data supplied is 365 * treated as the beginning of a new record. To place a sender's address, 366 * optional access rights, and data in a socket receive buffer, 367 * sbappendaddr() should be used. To place access rights and data in a 368 * socket receive buffer, sbappendrights() should be used. In either case, 369 * the new data begins a new record. Note that unlike sbappend() and 370 * sbappendrecord(), these routines check for the caller that there will be 371 * enough space to store the data. Each fails if there is not enough space, 372 * or if it cannot find mbufs to store additional information in.
|
399 *
| 373 *
|
400 * Reliable protocols may use the socket send buffer to hold data 401 * awaiting acknowledgement. Data is normally copied from a socket 402 * send buffer in a protocol with m_copy for output to a peer, 403 * and then removing the data from the socket buffer with sbdrop() 404 * or sbdroprecord() when the data is acknowledged by the peer.
| 374 * Reliable protocols may use the socket send buffer to hold data awaiting 375 * acknowledgement. Data is normally copied from a socket send buffer in a 376 * protocol with m_copy for output to a peer, and then removing the data from 377 * the socket buffer with sbdrop() or sbdroprecord() when the data is 378 * acknowledged by the peer.
|
405 */
| 379 */
|
406
| |
407#ifdef SOCKBUF_DEBUG 408void 409sblastrecordchk(struct sockbuf *sb, const char *file, int line) 410{ 411 struct mbuf *m = sb->sb_mb; 412 413 SOCKBUF_LOCK_ASSERT(sb); 414 415 while (m && m->m_nextpkt) 416 m = m->m_nextpkt; 417 418 if (m != sb->sb_lastrecord) { 419 printf("%s: sb_mb %p sb_lastrecord %p last %p\n", 420 __func__, sb->sb_mb, sb->sb_lastrecord, m); 421 printf("packet chain:\n"); 422 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 423 printf("\t%p\n", m); 424 panic("%s from %s:%u", __func__, file, line); 425 } 426} 427 428void 429sblastmbufchk(struct sockbuf *sb, const char *file, int line) 430{ 431 struct mbuf *m = sb->sb_mb; 432 struct mbuf *n; 433 434 SOCKBUF_LOCK_ASSERT(sb); 435 436 while (m && m->m_nextpkt) 437 m = m->m_nextpkt; 438 439 while (m && m->m_next) 440 m = m->m_next; 441 442 if (m != sb->sb_mbtail) { 443 printf("%s: sb_mb %p sb_mbtail %p last %p\n", 444 __func__, sb->sb_mb, sb->sb_mbtail, m); 445 printf("packet tree:\n"); 446 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 447 printf("\t"); 448 for (n = m; n != NULL; n = n->m_next) 449 printf("%p ", n); 450 printf("\n"); 451 } 452 panic("%s from %s:%u", __func__, file, line); 453 } 454} 455#endif /* SOCKBUF_DEBUG */ 456 457#define SBLINKRECORD(sb, m0) do { \ 458 SOCKBUF_LOCK_ASSERT(sb); \ 459 if ((sb)->sb_lastrecord != NULL) \ 460 (sb)->sb_lastrecord->m_nextpkt = (m0); \ 461 else \ 462 (sb)->sb_mb = (m0); \ 463 (sb)->sb_lastrecord = (m0); \ 464} while (/*CONSTCOND*/0) 465 466/*
| 380#ifdef SOCKBUF_DEBUG 381void 382sblastrecordchk(struct sockbuf *sb, const char *file, int line) 383{ 384 struct mbuf *m = sb->sb_mb; 385 386 SOCKBUF_LOCK_ASSERT(sb); 387 388 while (m && m->m_nextpkt) 389 m = m->m_nextpkt; 390 391 if (m != sb->sb_lastrecord) { 392 printf("%s: sb_mb %p sb_lastrecord %p last %p\n", 393 __func__, sb->sb_mb, sb->sb_lastrecord, m); 394 printf("packet chain:\n"); 395 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 396 printf("\t%p\n", m); 397 panic("%s from %s:%u", __func__, file, line); 398 } 399} 400 401void 402sblastmbufchk(struct sockbuf *sb, const char *file, int line) 403{ 404 struct mbuf *m = sb->sb_mb; 405 struct mbuf *n; 406 407 SOCKBUF_LOCK_ASSERT(sb); 408 409 while (m && m->m_nextpkt) 410 m = m->m_nextpkt; 411 412 while (m && m->m_next) 413 m = m->m_next; 414 415 if (m != sb->sb_mbtail) { 416 printf("%s: sb_mb %p sb_mbtail %p last %p\n", 417 __func__, sb->sb_mb, sb->sb_mbtail, m); 418 printf("packet tree:\n"); 419 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 420 printf("\t"); 421 for (n = m; n != NULL; n = n->m_next) 422 printf("%p ", n); 423 printf("\n"); 424 } 425 panic("%s from %s:%u", __func__, file, line); 426 } 427} 428#endif /* SOCKBUF_DEBUG */ 429 430#define SBLINKRECORD(sb, m0) do { \ 431 SOCKBUF_LOCK_ASSERT(sb); \ 432 if ((sb)->sb_lastrecord != NULL) \ 433 (sb)->sb_lastrecord->m_nextpkt = (m0); \ 434 else \ 435 (sb)->sb_mb = (m0); \ 436 (sb)->sb_lastrecord = (m0); \ 437} while (/*CONSTCOND*/0) 438 439/*
|
467 * Append mbuf chain m to the last record in the 468 * socket buffer sb. The additional space associated 469 * the mbuf chain is recorded in sb. Empty mbufs are 470 * discarded and mbufs are compacted where possible.
| 440 * Append mbuf chain m to the last record in the socket buffer sb. The 441 * additional space associated the mbuf chain is recorded in sb. Empty mbufs 442 * are discarded and mbufs are compacted where possible.
|
471 */ 472void
| 443 */ 444void
|
473sbappend_locked(sb, m) 474 struct sockbuf *sb; 475 struct mbuf *m;
| 445sbappend_locked(struct sockbuf *sb, struct mbuf *m)
|
476{
| 446{
|
477 register struct mbuf *n;
| 447 struct mbuf *n;
|
478 479 SOCKBUF_LOCK_ASSERT(sb); 480 481 if (m == 0) 482 return; 483 484 SBLASTRECORDCHK(sb); 485 n = sb->sb_mb; 486 if (n) { 487 while (n->m_nextpkt) 488 n = n->m_nextpkt; 489 do { 490 if (n->m_flags & M_EOR) { 491 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 492 return; 493 } 494 } while (n->m_next && (n = n->m_next)); 495 } else { 496 /* 497 * XXX Would like to simply use sb_mbtail here, but 498 * XXX I need to verify that I won't miss an EOR that 499 * XXX way. 500 */ 501 if ((n = sb->sb_lastrecord) != NULL) { 502 do { 503 if (n->m_flags & M_EOR) { 504 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 505 return; 506 } 507 } while (n->m_next && (n = n->m_next)); 508 } else { 509 /* 510 * If this is the first record in the socket buffer, 511 * it's also the last record. 512 */ 513 sb->sb_lastrecord = m; 514 } 515 } 516 sbcompress(sb, m, n); 517 SBLASTRECORDCHK(sb); 518} 519 520/*
| 448 449 SOCKBUF_LOCK_ASSERT(sb); 450 451 if (m == 0) 452 return; 453 454 SBLASTRECORDCHK(sb); 455 n = sb->sb_mb; 456 if (n) { 457 while (n->m_nextpkt) 458 n = n->m_nextpkt; 459 do { 460 if (n->m_flags & M_EOR) { 461 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 462 return; 463 } 464 } while (n->m_next && (n = n->m_next)); 465 } else { 466 /* 467 * XXX Would like to simply use sb_mbtail here, but 468 * XXX I need to verify that I won't miss an EOR that 469 * XXX way. 470 */ 471 if ((n = sb->sb_lastrecord) != NULL) { 472 do { 473 if (n->m_flags & M_EOR) { 474 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 475 return; 476 } 477 } while (n->m_next && (n = n->m_next)); 478 } else { 479 /* 480 * If this is the first record in the socket buffer, 481 * it's also the last record. 482 */ 483 sb->sb_lastrecord = m; 484 } 485 } 486 sbcompress(sb, m, n); 487 SBLASTRECORDCHK(sb); 488} 489 490/*
|
521 * Append mbuf chain m to the last record in the 522 * socket buffer sb. The additional space associated 523 * the mbuf chain is recorded in sb. Empty mbufs are 524 * discarded and mbufs are compacted where possible.
| 491 * Append mbuf chain m to the last record in the socket buffer sb. The 492 * additional space associated the mbuf chain is recorded in sb. Empty mbufs 493 * are discarded and mbufs are compacted where possible.
|
525 */ 526void
| 494 */ 495void
|
527sbappend(sb, m) 528 struct sockbuf *sb; 529 struct mbuf *m;
| 496sbappend(struct sockbuf *sb, struct mbuf *m)
|
530{ 531 532 SOCKBUF_LOCK(sb); 533 sbappend_locked(sb, m); 534 SOCKBUF_UNLOCK(sb); 535} 536 537/*
| 497{ 498 499 SOCKBUF_LOCK(sb); 500 sbappend_locked(sb, m); 501 SOCKBUF_UNLOCK(sb); 502} 503 504/*
|
538 * This version of sbappend() should only be used when the caller 539 * absolutely knows that there will never be more than one record 540 * in the socket buffer, that is, a stream protocol (such as TCP).
| 505 * This version of sbappend() should only be used when the caller absolutely 506 * knows that there will never be more than one record in the socket buffer, 507 * that is, a stream protocol (such as TCP).
|
541 */ 542void 543sbappendstream_locked(struct sockbuf *sb, struct mbuf *m) 544{ 545 SOCKBUF_LOCK_ASSERT(sb); 546 547 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0")); 548 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1")); 549 550 SBLASTMBUFCHK(sb); 551 552 sbcompress(sb, m, sb->sb_mbtail); 553 554 sb->sb_lastrecord = sb->sb_mb; 555 SBLASTRECORDCHK(sb); 556} 557 558/*
| 508 */ 509void 510sbappendstream_locked(struct sockbuf *sb, struct mbuf *m) 511{ 512 SOCKBUF_LOCK_ASSERT(sb); 513 514 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0")); 515 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1")); 516 517 SBLASTMBUFCHK(sb); 518 519 sbcompress(sb, m, sb->sb_mbtail); 520 521 sb->sb_lastrecord = sb->sb_mb; 522 SBLASTRECORDCHK(sb); 523} 524 525/*
|
559 * This version of sbappend() should only be used when the caller 560 * absolutely knows that there will never be more than one record 561 * in the socket buffer, that is, a stream protocol (such as TCP).
| 526 * This version of sbappend() should only be used when the caller absolutely 527 * knows that there will never be more than one record in the socket buffer, 528 * that is, a stream protocol (such as TCP).
|
562 */ 563void 564sbappendstream(struct sockbuf *sb, struct mbuf *m) 565{ 566 567 SOCKBUF_LOCK(sb); 568 sbappendstream_locked(sb, m); 569 SOCKBUF_UNLOCK(sb); 570} 571 572#ifdef SOCKBUF_DEBUG 573void
| 529 */ 530void 531sbappendstream(struct sockbuf *sb, struct mbuf *m) 532{ 533 534 SOCKBUF_LOCK(sb); 535 sbappendstream_locked(sb, m); 536 SOCKBUF_UNLOCK(sb); 537} 538 539#ifdef SOCKBUF_DEBUG 540void
|
574sbcheck(sb) 575 struct sockbuf *sb;
| 541sbcheck(struct sockbuf *sb)
|
576{ 577 struct mbuf *m; 578 struct mbuf *n = 0; 579 u_long len = 0, mbcnt = 0; 580 581 SOCKBUF_LOCK_ASSERT(sb); 582 583 for (m = sb->sb_mb; m; m = n) { 584 n = m->m_nextpkt; 585 for (; m; m = m->m_next) { 586 len += m->m_len; 587 mbcnt += MSIZE; 588 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 589 mbcnt += m->m_ext.ext_size; 590 } 591 } 592 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 593 printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc, 594 mbcnt, sb->sb_mbcnt); 595 panic("sbcheck"); 596 } 597} 598#endif 599 600/*
| 542{ 543 struct mbuf *m; 544 struct mbuf *n = 0; 545 u_long len = 0, mbcnt = 0; 546 547 SOCKBUF_LOCK_ASSERT(sb); 548 549 for (m = sb->sb_mb; m; m = n) { 550 n = m->m_nextpkt; 551 for (; m; m = m->m_next) { 552 len += m->m_len; 553 mbcnt += MSIZE; 554 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 555 mbcnt += m->m_ext.ext_size; 556 } 557 } 558 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 559 printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc, 560 mbcnt, sb->sb_mbcnt); 561 panic("sbcheck"); 562 } 563} 564#endif 565 566/*
|
601 * As above, except the mbuf chain 602 * begins a new record.
| 567 * As above, except the mbuf chain begins a new record.
|
603 */ 604void
| 568 */ 569void
|
605sbappendrecord_locked(sb, m0) 606 register struct sockbuf *sb; 607 register struct mbuf *m0;
| 570sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
|
608{
| 571{
|
609 register struct mbuf *m;
| 572 struct mbuf *m;
|
610 611 SOCKBUF_LOCK_ASSERT(sb); 612 613 if (m0 == 0) 614 return; 615 m = sb->sb_mb; 616 if (m) 617 while (m->m_nextpkt) 618 m = m->m_nextpkt; 619 /*
| 573 574 SOCKBUF_LOCK_ASSERT(sb); 575 576 if (m0 == 0) 577 return; 578 m = sb->sb_mb; 579 if (m) 580 while (m->m_nextpkt) 581 m = m->m_nextpkt; 582 /*
|
620 * Put the first mbuf on the queue. 621 * Note this permits zero length records.
| 583 * Put the first mbuf on the queue. Note this permits zero length 584 * records.
|
622 */ 623 sballoc(sb, m0); 624 SBLASTRECORDCHK(sb); 625 SBLINKRECORD(sb, m0); 626 if (m) 627 m->m_nextpkt = m0; 628 else 629 sb->sb_mb = m0; 630 m = m0->m_next; 631 m0->m_next = 0; 632 if (m && (m0->m_flags & M_EOR)) { 633 m0->m_flags &= ~M_EOR; 634 m->m_flags |= M_EOR; 635 } 636 sbcompress(sb, m, m0); 637} 638 639/*
| 585 */ 586 sballoc(sb, m0); 587 SBLASTRECORDCHK(sb); 588 SBLINKRECORD(sb, m0); 589 if (m) 590 m->m_nextpkt = m0; 591 else 592 sb->sb_mb = m0; 593 m = m0->m_next; 594 m0->m_next = 0; 595 if (m && (m0->m_flags & M_EOR)) { 596 m0->m_flags &= ~M_EOR; 597 m->m_flags |= M_EOR; 598 } 599 sbcompress(sb, m, m0); 600} 601 602/*
|
640 * As above, except the mbuf chain 641 * begins a new record.
| 603 * As above, except the mbuf chain begins a new record.
|
642 */ 643void
| 604 */ 605void
|
644sbappendrecord(sb, m0) 645 register struct sockbuf *sb; 646 register struct mbuf *m0;
| 606sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
|
647{ 648 649 SOCKBUF_LOCK(sb); 650 sbappendrecord_locked(sb, m0); 651 SOCKBUF_UNLOCK(sb); 652} 653 654/*
| 607{ 608 609 SOCKBUF_LOCK(sb); 610 sbappendrecord_locked(sb, m0); 611 SOCKBUF_UNLOCK(sb); 612} 613 614/*
|
655 * Append address and data, and optionally, control (ancillary) data 656 * to the receive queue of a socket. If present, 657 * m0 must include a packet header with total length. 658 * Returns 0 if no space in sockbuf or insufficient mbufs.
| 615 * Append address and data, and optionally, control (ancillary) data to the 616 * receive queue of a socket. If present, m0 must include a packet header 617 * with total length. Returns 0 if no space in sockbuf or insufficient 618 * mbufs.
|
659 */ 660int
| 619 */ 620int
|
661sbappendaddr_locked(sb, asa, m0, control) 662 struct sockbuf *sb; 663 const struct sockaddr *asa; 664 struct mbuf *m0, *control;
| 621sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa, 622 struct mbuf *m0, struct mbuf *control)
|
665{ 666 struct mbuf *m, *n, *nlast; 667 int space = asa->sa_len; 668 669 SOCKBUF_LOCK_ASSERT(sb); 670 671 if (m0 && (m0->m_flags & M_PKTHDR) == 0) 672 panic("sbappendaddr_locked"); 673 if (m0) 674 space += m0->m_pkthdr.len; 675 space += m_length(control, &n); 676 677 if (space > sbspace(sb)) 678 return (0); 679#if MSIZE <= 256 680 if (asa->sa_len > MLEN) 681 return (0); 682#endif 683 MGET(m, M_DONTWAIT, MT_SONAME); 684 if (m == 0) 685 return (0); 686 m->m_len = asa->sa_len; 687 bcopy(asa, mtod(m, caddr_t), asa->sa_len); 688 if (n) 689 n->m_next = m0; /* concatenate data to control */ 690 else 691 control = m0; 692 m->m_next = control; 693 for (n = m; n->m_next != NULL; n = n->m_next) 694 sballoc(sb, n); 695 sballoc(sb, n); 696 nlast = n; 697 SBLINKRECORD(sb, m); 698 699 sb->sb_mbtail = nlast; 700 SBLASTMBUFCHK(sb); 701 702 SBLASTRECORDCHK(sb); 703 return (1); 704} 705 706/*
| 623{ 624 struct mbuf *m, *n, *nlast; 625 int space = asa->sa_len; 626 627 SOCKBUF_LOCK_ASSERT(sb); 628 629 if (m0 && (m0->m_flags & M_PKTHDR) == 0) 630 panic("sbappendaddr_locked"); 631 if (m0) 632 space += m0->m_pkthdr.len; 633 space += m_length(control, &n); 634 635 if (space > sbspace(sb)) 636 return (0); 637#if MSIZE <= 256 638 if (asa->sa_len > MLEN) 639 return (0); 640#endif 641 MGET(m, M_DONTWAIT, MT_SONAME); 642 if (m == 0) 643 return (0); 644 m->m_len = asa->sa_len; 645 bcopy(asa, mtod(m, caddr_t), asa->sa_len); 646 if (n) 647 n->m_next = m0; /* concatenate data to control */ 648 else 649 control = m0; 650 m->m_next = control; 651 for (n = m; n->m_next != NULL; n = n->m_next) 652 sballoc(sb, n); 653 sballoc(sb, n); 654 nlast = n; 655 SBLINKRECORD(sb, m); 656 657 sb->sb_mbtail = nlast; 658 SBLASTMBUFCHK(sb); 659 660 SBLASTRECORDCHK(sb); 661 return (1); 662} 663 664/*
|
707 * Append address and data, and optionally, control (ancillary) data 708 * to the receive queue of a socket. If present, 709 * m0 must include a packet header with total length. 710 * Returns 0 if no space in sockbuf or insufficient mbufs.
| 665 * Append address and data, and optionally, control (ancillary) data to the 666 * receive queue of a socket. If present, m0 must include a packet header 667 * with total length. Returns 0 if no space in sockbuf or insufficient 668 * mbufs.
|
711 */ 712int
| 669 */ 670int
|
713sbappendaddr(sb, asa, m0, control) 714 struct sockbuf *sb; 715 const struct sockaddr *asa; 716 struct mbuf *m0, *control;
| 671sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, 672 struct mbuf *m0, struct mbuf *control)
|
717{ 718 int retval; 719 720 SOCKBUF_LOCK(sb); 721 retval = sbappendaddr_locked(sb, asa, m0, control); 722 SOCKBUF_UNLOCK(sb); 723 return (retval); 724} 725 726int
| 673{ 674 int retval; 675 676 SOCKBUF_LOCK(sb); 677 retval = sbappendaddr_locked(sb, asa, m0, control); 678 SOCKBUF_UNLOCK(sb); 679 return (retval); 680} 681 682int
|
727sbappendcontrol_locked(sb, m0, control) 728 struct sockbuf *sb; 729 struct mbuf *control, *m0;
| 683sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0, 684 struct mbuf *control)
|
730{ 731 struct mbuf *m, *n, *mlast; 732 int space; 733 734 SOCKBUF_LOCK_ASSERT(sb); 735 736 if (control == 0) 737 panic("sbappendcontrol_locked"); 738 space = m_length(control, &n) + m_length(m0, NULL); 739 740 if (space > sbspace(sb)) 741 return (0); 742 n->m_next = m0; /* concatenate data to control */ 743 744 SBLASTRECORDCHK(sb); 745 746 for (m = control; m->m_next; m = m->m_next) 747 sballoc(sb, m); 748 sballoc(sb, m); 749 mlast = m; 750 SBLINKRECORD(sb, control); 751 752 sb->sb_mbtail = mlast; 753 SBLASTMBUFCHK(sb); 754 755 SBLASTRECORDCHK(sb); 756 return (1); 757} 758 759int
| 685{ 686 struct mbuf *m, *n, *mlast; 687 int space; 688 689 SOCKBUF_LOCK_ASSERT(sb); 690 691 if (control == 0) 692 panic("sbappendcontrol_locked"); 693 space = m_length(control, &n) + m_length(m0, NULL); 694 695 if (space > sbspace(sb)) 696 return (0); 697 n->m_next = m0; /* concatenate data to control */ 698 699 SBLASTRECORDCHK(sb); 700 701 for (m = control; m->m_next; m = m->m_next) 702 sballoc(sb, m); 703 sballoc(sb, m); 704 mlast = m; 705 SBLINKRECORD(sb, control); 706 707 sb->sb_mbtail = mlast; 708 SBLASTMBUFCHK(sb); 709 710 SBLASTRECORDCHK(sb); 711 return (1); 712} 713 714int
|
760sbappendcontrol(sb, m0, control) 761 struct sockbuf *sb; 762 struct mbuf *control, *m0;
| 715sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
|
763{ 764 int retval; 765 766 SOCKBUF_LOCK(sb); 767 retval = sbappendcontrol_locked(sb, m0, control); 768 SOCKBUF_UNLOCK(sb); 769 return (retval); 770} 771 772/* 773 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf 774 * (n). If (n) is NULL, the buffer is presumed empty. 775 * 776 * When the data is compressed, mbufs in the chain may be handled in one of 777 * three ways: 778 * 779 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no 780 * record boundary, and no change in data type). 781 * 782 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into 783 * an mbuf already in the socket buffer. This can occur if an 784 * appropriate mbuf exists, there is room, and no merging of data types 785 * will occur. 786 * 787 * (3) The mbuf may be appended to the end of the existing mbuf chain. 788 * 789 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as 790 * end-of-record. 791 */ 792void
| 716{ 717 int retval; 718 719 SOCKBUF_LOCK(sb); 720 retval = sbappendcontrol_locked(sb, m0, control); 721 SOCKBUF_UNLOCK(sb); 722 return (retval); 723} 724 725/* 726 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf 727 * (n). If (n) is NULL, the buffer is presumed empty. 728 * 729 * When the data is compressed, mbufs in the chain may be handled in one of 730 * three ways: 731 * 732 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no 733 * record boundary, and no change in data type). 734 * 735 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into 736 * an mbuf already in the socket buffer. This can occur if an 737 * appropriate mbuf exists, there is room, and no merging of data types 738 * will occur. 739 * 740 * (3) The mbuf may be appended to the end of the existing mbuf chain. 741 * 742 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as 743 * end-of-record. 744 */ 745void
|
793sbcompress(sb, m, n) 794 register struct sockbuf *sb; 795 register struct mbuf *m, *n;
| 746sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
|
796{
| 747{
|
797 register int eor = 0; 798 register struct mbuf *o;
| 748 int eor = 0; 749 struct mbuf *o;
|
799 800 SOCKBUF_LOCK_ASSERT(sb); 801 802 while (m) { 803 eor |= m->m_flags & M_EOR; 804 if (m->m_len == 0 && 805 (eor == 0 || 806 (((o = m->m_next) || (o = n)) && 807 o->m_type == m->m_type))) { 808 if (sb->sb_lastrecord == m) 809 sb->sb_lastrecord = m->m_next; 810 m = m_free(m); 811 continue; 812 } 813 if (n && (n->m_flags & M_EOR) == 0 && 814 M_WRITABLE(n) && 815 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 816 m->m_len <= M_TRAILINGSPACE(n) && 817 n->m_type == m->m_type) { 818 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 819 (unsigned)m->m_len); 820 n->m_len += m->m_len; 821 sb->sb_cc += m->m_len; 822 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 823 /* XXX: Probably don't need.*/ 824 sb->sb_ctl += m->m_len; 825 m = m_free(m); 826 continue; 827 } 828 if (n) 829 n->m_next = m; 830 else 831 sb->sb_mb = m; 832 sb->sb_mbtail = m; 833 sballoc(sb, m); 834 n = m; 835 m->m_flags &= ~M_EOR; 836 m = m->m_next; 837 n->m_next = 0; 838 } 839 if (eor) { 840 KASSERT(n != NULL, ("sbcompress: eor && n == NULL")); 841 n->m_flags |= eor; 842 } 843 SBLASTMBUFCHK(sb); 844} 845 846/*
| 750 751 SOCKBUF_LOCK_ASSERT(sb); 752 753 while (m) { 754 eor |= m->m_flags & M_EOR; 755 if (m->m_len == 0 && 756 (eor == 0 || 757 (((o = m->m_next) || (o = n)) && 758 o->m_type == m->m_type))) { 759 if (sb->sb_lastrecord == m) 760 sb->sb_lastrecord = m->m_next; 761 m = m_free(m); 762 continue; 763 } 764 if (n && (n->m_flags & M_EOR) == 0 && 765 M_WRITABLE(n) && 766 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 767 m->m_len <= M_TRAILINGSPACE(n) && 768 n->m_type == m->m_type) { 769 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 770 (unsigned)m->m_len); 771 n->m_len += m->m_len; 772 sb->sb_cc += m->m_len; 773 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 774 /* XXX: Probably don't need.*/ 775 sb->sb_ctl += m->m_len; 776 m = m_free(m); 777 continue; 778 } 779 if (n) 780 n->m_next = m; 781 else 782 sb->sb_mb = m; 783 sb->sb_mbtail = m; 784 sballoc(sb, m); 785 n = m; 786 m->m_flags &= ~M_EOR; 787 m = m->m_next; 788 n->m_next = 0; 789 } 790 if (eor) { 791 KASSERT(n != NULL, ("sbcompress: eor && n == NULL")); 792 n->m_flags |= eor; 793 } 794 SBLASTMBUFCHK(sb); 795} 796 797/*
|
847 * Free all mbufs in a sockbuf. 848 * Check that all resources are reclaimed.
| 798 * Free all mbufs in a sockbuf. Check that all resources are reclaimed.
|
849 */ 850static void
| 799 */ 800static void
|
851sbflush_internal(sb) 852 register struct sockbuf *sb;
| 801sbflush_internal(struct sockbuf *sb)
|
853{ 854 855 if (sb->sb_flags & SB_LOCK)
| 802{ 803 804 if (sb->sb_flags & SB_LOCK)
|
856 panic("sbflush_locked: locked");
| 805 panic("sbflush_internal: locked");
|
857 while (sb->sb_mbcnt) { 858 /* 859 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty: 860 * we would loop forever. Panic instead. 861 */ 862 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 863 break; 864 sbdrop_internal(sb, (int)sb->sb_cc); 865 } 866 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
| 806 while (sb->sb_mbcnt) { 807 /* 808 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty: 809 * we would loop forever. Panic instead. 810 */ 811 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 812 break; 813 sbdrop_internal(sb, (int)sb->sb_cc); 814 } 815 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
|
867 panic("sbflush_locked: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
| 816 panic("sbflush_internal: cc %u || mb %p || mbcnt %u", 817 sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
|
868} 869 870void
| 818} 819 820void
|
871sbflush_locked(sb) 872 register struct sockbuf *sb;
| 821sbflush_locked(struct sockbuf *sb)
|
873{ 874 875 SOCKBUF_LOCK_ASSERT(sb); 876 sbflush_internal(sb); 877} 878 879void
| 822{ 823 824 SOCKBUF_LOCK_ASSERT(sb); 825 sbflush_internal(sb); 826} 827 828void
|
880sbflush(sb) 881 register struct sockbuf *sb;
| 829sbflush(struct sockbuf *sb)
|
882{ 883 884 SOCKBUF_LOCK(sb); 885 sbflush_locked(sb); 886 SOCKBUF_UNLOCK(sb); 887} 888 889/* 890 * Drop data from (the front of) a sockbuf. 891 */ 892static void
| 830{ 831 832 SOCKBUF_LOCK(sb); 833 sbflush_locked(sb); 834 SOCKBUF_UNLOCK(sb); 835} 836 837/* 838 * Drop data from (the front of) a sockbuf. 839 */ 840static void
|
893sbdrop_internal(sb, len) 894 register struct sockbuf *sb; 895 register int len;
| 841sbdrop_internal(struct sockbuf *sb, int len)
|
896{
| 842{
|
897 register struct mbuf *m;
| 843 struct mbuf *m;
|
898 struct mbuf *next; 899 900 next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 901 while (len > 0) { 902 if (m == 0) { 903 if (next == 0) 904 panic("sbdrop"); 905 m = next; 906 next = m->m_nextpkt; 907 continue; 908 } 909 if (m->m_len > len) { 910 m->m_len -= len; 911 m->m_data += len; 912 sb->sb_cc -= len; 913 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 914 sb->sb_ctl -= len; 915 break; 916 } 917 len -= m->m_len; 918 sbfree(sb, m); 919 m = m_free(m); 920 } 921 while (m && m->m_len == 0) { 922 sbfree(sb, m); 923 m = m_free(m); 924 } 925 if (m) { 926 sb->sb_mb = m; 927 m->m_nextpkt = next; 928 } else 929 sb->sb_mb = next; 930 /*
| 844 struct mbuf *next; 845 846 next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 847 while (len > 0) { 848 if (m == 0) { 849 if (next == 0) 850 panic("sbdrop"); 851 m = next; 852 next = m->m_nextpkt; 853 continue; 854 } 855 if (m->m_len > len) { 856 m->m_len -= len; 857 m->m_data += len; 858 sb->sb_cc -= len; 859 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 860 sb->sb_ctl -= len; 861 break; 862 } 863 len -= m->m_len; 864 sbfree(sb, m); 865 m = m_free(m); 866 } 867 while (m && m->m_len == 0) { 868 sbfree(sb, m); 869 m = m_free(m); 870 } 871 if (m) { 872 sb->sb_mb = m; 873 m->m_nextpkt = next; 874 } else 875 sb->sb_mb = next; 876 /*
|
931 * First part is an inline SB_EMPTY_FIXUP(). Second part 932 * makes sure sb_lastrecord is up-to-date if we dropped 933 * part of the last record.
| 877 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure 878 * sb_lastrecord is up-to-date if we dropped part of the last record.
|
934 */ 935 m = sb->sb_mb; 936 if (m == NULL) { 937 sb->sb_mbtail = NULL; 938 sb->sb_lastrecord = NULL; 939 } else if (m->m_nextpkt == NULL) { 940 sb->sb_lastrecord = m; 941 } 942} 943 944/* 945 * Drop data from (the front of) a sockbuf. 946 */ 947void
| 879 */ 880 m = sb->sb_mb; 881 if (m == NULL) { 882 sb->sb_mbtail = NULL; 883 sb->sb_lastrecord = NULL; 884 } else if (m->m_nextpkt == NULL) { 885 sb->sb_lastrecord = m; 886 } 887} 888 889/* 890 * Drop data from (the front of) a sockbuf. 891 */ 892void
|
948sbdrop_locked(sb, len) 949 register struct sockbuf *sb; 950 register int len;
| 893sbdrop_locked(struct sockbuf *sb, int len)
|
951{ 952 953 SOCKBUF_LOCK_ASSERT(sb); 954 955 sbdrop_internal(sb, len); 956} 957 958void
| 894{ 895 896 SOCKBUF_LOCK_ASSERT(sb); 897 898 sbdrop_internal(sb, len); 899} 900 901void
|
959sbdrop(sb, len) 960 register struct sockbuf *sb; 961 register int len;
| 902sbdrop(struct sockbuf *sb, int len)
|
962{ 963 964 SOCKBUF_LOCK(sb); 965 sbdrop_locked(sb, len); 966 SOCKBUF_UNLOCK(sb); 967} 968 969/*
| 903{ 904 905 SOCKBUF_LOCK(sb); 906 sbdrop_locked(sb, len); 907 SOCKBUF_UNLOCK(sb); 908} 909 910/*
|
970 * Drop a record off the front of a sockbuf 971 * and move the next record to the front.
| 911 * Drop a record off the front of a sockbuf and move the next record to the 912 * front.
|
972 */ 973void
| 913 */ 914void
|
974sbdroprecord_locked(sb) 975 register struct sockbuf *sb;
| 915sbdroprecord_locked(struct sockbuf *sb)
|
976{
| 916{
|
977 register struct mbuf *m;
| 917 struct mbuf *m;
|
978 979 SOCKBUF_LOCK_ASSERT(sb); 980 981 m = sb->sb_mb; 982 if (m) { 983 sb->sb_mb = m->m_nextpkt; 984 do { 985 sbfree(sb, m); 986 m = m_free(m); 987 } while (m); 988 } 989 SB_EMPTY_FIXUP(sb); 990} 991 992/*
| 918 919 SOCKBUF_LOCK_ASSERT(sb); 920 921 m = sb->sb_mb; 922 if (m) { 923 sb->sb_mb = m->m_nextpkt; 924 do { 925 sbfree(sb, m); 926 m = m_free(m); 927 } while (m); 928 } 929 SB_EMPTY_FIXUP(sb); 930} 931 932/*
|
993 * Drop a record off the front of a sockbuf 994 * and move the next record to the front.
| 933 * Drop a record off the front of a sockbuf and move the next record to the 934 * front.
|
995 */ 996void
| 935 */ 936void
|
997sbdroprecord(sb) 998 register struct sockbuf *sb;
| 937sbdroprecord(struct sockbuf *sb)
|
999{ 1000 1001 SOCKBUF_LOCK(sb); 1002 sbdroprecord_locked(sb); 1003 SOCKBUF_UNLOCK(sb); 1004} 1005 1006/* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 1007static int dummy; 1008SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 1009SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW, 1010 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size"); 1011SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 1012 &sb_efficiency, 0, "");
| 938{ 939 940 SOCKBUF_LOCK(sb); 941 sbdroprecord_locked(sb); 942 SOCKBUF_UNLOCK(sb); 943} 944 945/* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 946static int dummy; 947SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 948SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW, 949 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size"); 950SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 951 &sb_efficiency, 0, "");
|