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$");
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/sx.h>
50 #include <sys/sysctl.h>
51
52 /*
53 * Function pointer set by the AIO routines so that the socket buffer code
54 * can call back into the AIO module if it is loaded.
55 */
56 void (*aio_swake)(struct socket *, struct sockbuf *);
57
58 /*
59 * Primitive routines for operating on socket buffers
60 */
61
62 u_long sb_max = SB_MAX;
63 u_long sb_max_adj =
64 (quad_t)SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
65
66 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
67
68 static void sbdrop_internal(struct sockbuf *sb, int len);
69 static void sbflush_internal(struct sockbuf *sb);
70
71 /*
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 */
80 void
81 socantsendmore_locked(struct socket *so)
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
91 void
92 socantsendmore(struct socket *so)
93 {
94
95 SOCKBUF_LOCK(&so->so_snd);
96 socantsendmore_locked(so);
97 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
98 }
99
100 void
101 socantrcvmore_locked(struct socket *so)
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
111 void
112 socantrcvmore(struct socket *so)
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 */
123 int
124 sbwait(struct sockbuf *sb)
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 int
136 sblock(struct sockbuf *sb, int flags)
137 {
138
139 KASSERT((flags & SBL_VALID) == flags,
140 ("sblock: flags invalid (0x%x)", flags));
141
142 if (flags & SBL_WAIT) {
143 if ((sb->sb_flags & SB_NOINTR) ||
144 (flags & SBL_NOINTR)) {
145 sx_xlock(&sb->sb_sx);
146 return (0);
147 }
148 return (sx_xlock_sig(&sb->sb_sx));
149 } else {
150 if (sx_try_xlock(&sb->sb_sx) == 0)
151 return (EWOULDBLOCK);
152 return (0);
153 }
154 }
155
156 void
157 sbunlock(struct sockbuf *sb)
158 {
159
160 sx_xunlock(&sb->sb_sx);
161 }
162
163 /*
164 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
165 * via SIGIO if the socket has the SS_ASYNC flag set.
166 *
167 * Called with the socket buffer lock held; will release the lock by the end
168 * of the function. This allows the caller to acquire the socket buffer lock
169 * while testing for the need for various sorts of wakeup and hold it through
170 * to the point where it's no longer required. We currently hold the lock
171 * through calls out to other subsystems (with the exception of kqueue), and
172 * then release it to avoid lock order issues. It's not clear that's
173 * correct.
174 */
175 void
176 sowakeup(struct socket *so, struct sockbuf *sb)
177 {
178 int ret;
179
180 SOCKBUF_LOCK_ASSERT(sb);
181
182 selwakeuppri(&sb->sb_sel, PSOCK);
183 if (!SEL_WAITING(&sb->sb_sel))
184 sb->sb_flags &= ~SB_SEL;
185 if (sb->sb_flags & SB_WAIT) {
186 sb->sb_flags &= ~SB_WAIT;
187 wakeup(&sb->sb_cc);
188 }
189 KNOTE_LOCKED(&sb->sb_sel.si_note, 0);
190 if (sb->sb_upcall != NULL) {
191 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_DONTWAIT);
192 if (ret == SU_ISCONNECTED) {
193 KASSERT(sb == &so->so_rcv,
194 ("SO_SND upcall returned SU_ISCONNECTED"));
195 soupcall_clear(so, SO_RCV);
196 }
197 } else
198 ret = SU_OK;
199 if (sb->sb_flags & SB_AIO)
200 aio_swake(so, sb);
201 SOCKBUF_UNLOCK(sb);
202 if (ret == SU_ISCONNECTED)
203 soisconnected(so);
204 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
205 pgsigio(&so->so_sigio, SIGIO, 0);
206 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
207 }
208
209 /*
210 * Socket buffer (struct sockbuf) utility routines.
211 *
212 * Each socket contains two socket buffers: one for sending data and one for
213 * receiving data. Each buffer contains a queue of mbufs, information about
214 * the number of mbufs and amount of data in the queue, and other fields
215 * allowing select() statements and notification on data availability to be
216 * implemented.
217 *
218 * Data stored in a socket buffer is maintained as a list of records. Each
219 * record is a list of mbufs chained together with the m_next field. Records
220 * are chained together with the m_nextpkt field. The upper level routine
221 * soreceive() expects the following conventions to be observed when placing
222 * information in the receive buffer:
223 *
224 * 1. If the protocol requires each message be preceded by the sender's name,
225 * then a record containing that name must be present before any
226 * associated data (mbuf's must be of type MT_SONAME).
227 * 2. If the protocol supports the exchange of ``access rights'' (really just
228 * additional data associated with the message), and there are ``rights''
229 * to be received, then a record containing this data should be present
230 * (mbuf's must be of type MT_RIGHTS).
231 * 3. If a name or rights record exists, then it must be followed by a data
232 * record, perhaps of zero length.
233 *
234 * Before using a new socket structure it is first necessary to reserve
235 * buffer space to the socket, by calling sbreserve(). This should commit
236 * some of the available buffer space in the system buffer pool for the
237 * socket (currently, it does nothing but enforce limits). The space should
238 * be released by calling sbrelease() when the socket is destroyed.
239 */
240 int
241 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
242 {
243 struct thread *td = curthread;
244
245 SOCKBUF_LOCK(&so->so_snd);
246 SOCKBUF_LOCK(&so->so_rcv);
247 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
248 goto bad;
249 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
250 goto bad2;
251 if (so->so_rcv.sb_lowat == 0)
252 so->so_rcv.sb_lowat = 1;
253 if (so->so_snd.sb_lowat == 0)
254 so->so_snd.sb_lowat = MCLBYTES;
255 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
256 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
257 SOCKBUF_UNLOCK(&so->so_rcv);
258 SOCKBUF_UNLOCK(&so->so_snd);
259 return (0);
260 bad2:
261 sbrelease_locked(&so->so_snd, so);
262 bad:
263 SOCKBUF_UNLOCK(&so->so_rcv);
264 SOCKBUF_UNLOCK(&so->so_snd);
265 return (ENOBUFS);
266 }
267
268 static int
269 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
270 {
271 int error = 0;
272 u_long tmp_sb_max = sb_max;
273
274 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
275 if (error || !req->newptr)
276 return (error);
277 if (tmp_sb_max < MSIZE + MCLBYTES)
278 return (EINVAL);
279 sb_max = tmp_sb_max;
280 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
281 return (0);
282 }
283
284 /*
285 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
286 * become limiting if buffering efficiency is near the normal case.
287 */
288 int
289 sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
290 struct thread *td)
291 {
292 rlim_t sbsize_limit;
293
294 SOCKBUF_LOCK_ASSERT(sb);
295
296 /*
297 * When a thread is passed, we take into account the thread's socket
298 * buffer size limit. The caller will generally pass curthread, but
299 * in the TCP input path, NULL will be passed to indicate that no
300 * appropriate thread resource limits are available. In that case,
301 * we don't apply a process limit.
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
320 int
321 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
322 struct thread *td)
323 {
324 int error;
325
326 SOCKBUF_LOCK(sb);
327 error = sbreserve_locked(sb, cc, so, td);
328 SOCKBUF_UNLOCK(sb);
329 return (error);
330 }
331
332 /*
333 * Free mbufs held by a socket, and reserved mbuf space.
334 */
335 void
336 sbrelease_internal(struct sockbuf *sb, struct socket *so)
337 {
338
339 sbflush_internal(sb);
340 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
341 RLIM_INFINITY);
342 sb->sb_mbmax = 0;
343 }
344
345 void
346 sbrelease_locked(struct sockbuf *sb, struct socket *so)
347 {
348
349 SOCKBUF_LOCK_ASSERT(sb);
350
351 sbrelease_internal(sb, so);
352 }
353
354 void
355 sbrelease(struct sockbuf *sb, struct socket *so)
356 {
357
358 SOCKBUF_LOCK(sb);
359 sbrelease_locked(sb, so);
360 SOCKBUF_UNLOCK(sb);
361 }
362
363 void
364 sbdestroy(struct sockbuf *sb, struct socket *so)
365 {
366
367 sbrelease_internal(sb, so);
368 }
369
370 /*
371 * Routines to add and remove data from an mbuf queue.
372 *
373 * The routines sbappend() or sbappendrecord() are normally called to append
374 * new mbufs to a socket buffer, after checking that adequate space is
375 * available, comparing the function sbspace() with the amount of data to be
376 * added. sbappendrecord() differs from sbappend() in that data supplied is
377 * treated as the beginning of a new record. To place a sender's address,
378 * optional access rights, and data in a socket receive buffer,
379 * sbappendaddr() should be used. To place access rights and data in a
380 * socket receive buffer, sbappendrights() should be used. In either case,
381 * the new data begins a new record. Note that unlike sbappend() and
382 * sbappendrecord(), these routines check for the caller that there will be
383 * enough space to store the data. Each fails if there is not enough space,
384 * or if it cannot find mbufs to store additional information in.
385 *
386 * Reliable protocols may use the socket send buffer to hold data awaiting
387 * acknowledgement. Data is normally copied from a socket send buffer in a
388 * protocol with m_copy for output to a peer, and then removing the data from
389 * the socket buffer with sbdrop() or sbdroprecord() when the data is
390 * acknowledged by the peer.
391 */
392 #ifdef SOCKBUF_DEBUG
393 void
394 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
395 {
396 struct mbuf *m = sb->sb_mb;
397
398 SOCKBUF_LOCK_ASSERT(sb);
399
400 while (m && m->m_nextpkt)
401 m = m->m_nextpkt;
402
403 if (m != sb->sb_lastrecord) {
404 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
405 __func__, sb->sb_mb, sb->sb_lastrecord, m);
406 printf("packet chain:\n");
407 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
408 printf("\t%p\n", m);
409 panic("%s from %s:%u", __func__, file, line);
410 }
411 }
412
413 void
414 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
415 {
416 struct mbuf *m = sb->sb_mb;
417 struct mbuf *n;
418
419 SOCKBUF_LOCK_ASSERT(sb);
420
421 while (m && m->m_nextpkt)
422 m = m->m_nextpkt;
423
424 while (m && m->m_next)
425 m = m->m_next;
426
427 if (m != sb->sb_mbtail) {
428 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
429 __func__, sb->sb_mb, sb->sb_mbtail, m);
430 printf("packet tree:\n");
431 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
432 printf("\t");
433 for (n = m; n != NULL; n = n->m_next)
434 printf("%p ", n);
435 printf("\n");
436 }
437 panic("%s from %s:%u", __func__, file, line);
438 }
439 }
440 #endif /* SOCKBUF_DEBUG */
441
442 #define SBLINKRECORD(sb, m0) do { \
443 SOCKBUF_LOCK_ASSERT(sb); \
444 if ((sb)->sb_lastrecord != NULL) \
445 (sb)->sb_lastrecord->m_nextpkt = (m0); \
446 else \
447 (sb)->sb_mb = (m0); \
448 (sb)->sb_lastrecord = (m0); \
449 } while (/*CONSTCOND*/0)
450
451 /*
452 * Append mbuf chain m to the last record in the socket buffer sb. The
453 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
454 * are discarded and mbufs are compacted where possible.
455 */
456 void
457 sbappend_locked(struct sockbuf *sb, struct mbuf *m)
458 {
459 struct mbuf *n;
460
461 SOCKBUF_LOCK_ASSERT(sb);
462
463 if (m == 0)
464 return;
465
466 SBLASTRECORDCHK(sb);
467 n = sb->sb_mb;
468 if (n) {
469 while (n->m_nextpkt)
470 n = n->m_nextpkt;
471 do {
472 if (n->m_flags & M_EOR) {
473 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
474 return;
475 }
476 } while (n->m_next && (n = n->m_next));
477 } else {
478 /*
479 * XXX Would like to simply use sb_mbtail here, but
480 * XXX I need to verify that I won't miss an EOR that
481 * XXX way.
482 */
483 if ((n = sb->sb_lastrecord) != NULL) {
484 do {
485 if (n->m_flags & M_EOR) {
486 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
487 return;
488 }
489 } while (n->m_next && (n = n->m_next));
490 } else {
491 /*
492 * If this is the first record in the socket buffer,
493 * it's also the last record.
494 */
495 sb->sb_lastrecord = m;
496 }
497 }
498 sbcompress(sb, m, n);
499 SBLASTRECORDCHK(sb);
500 }
501
502 /*
503 * Append mbuf chain m to the last record in the socket buffer sb. The
504 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
505 * are discarded and mbufs are compacted where possible.
506 */
507 void
508 sbappend(struct sockbuf *sb, struct mbuf *m)
509 {
510
511 SOCKBUF_LOCK(sb);
512 sbappend_locked(sb, m);
513 SOCKBUF_UNLOCK(sb);
514 }
515
516 /*
517 * This version of sbappend() should only be used when the caller absolutely
518 * knows that there will never be more than one record in the socket buffer,
519 * that is, a stream protocol (such as TCP).
520 */
521 void
522 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m)
523 {
524 SOCKBUF_LOCK_ASSERT(sb);
525
526 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
527 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
528
529 SBLASTMBUFCHK(sb);
530
531 sbcompress(sb, m, sb->sb_mbtail);
532
533 sb->sb_lastrecord = sb->sb_mb;
534 SBLASTRECORDCHK(sb);
535 }
536
537 /*
538 * This version of sbappend() should only be used when the caller absolutely
539 * knows that there will never be more than one record in the socket buffer,
540 * that is, a stream protocol (such as TCP).
541 */
542 void
543 sbappendstream(struct sockbuf *sb, struct mbuf *m)
544 {
545
546 SOCKBUF_LOCK(sb);
547 sbappendstream_locked(sb, m);
548 SOCKBUF_UNLOCK(sb);
549 }
550
551 #ifdef SOCKBUF_DEBUG
552 void
553 sbcheck(struct sockbuf *sb)
554 {
555 struct mbuf *m;
556 struct mbuf *n = 0;
557 u_long len = 0, mbcnt = 0;
558
559 SOCKBUF_LOCK_ASSERT(sb);
560
561 for (m = sb->sb_mb; m; m = n) {
562 n = m->m_nextpkt;
563 for (; m; m = m->m_next) {
564 len += m->m_len;
565 mbcnt += MSIZE;
566 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
567 mbcnt += m->m_ext.ext_size;
568 }
569 }
570 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
571 printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc,
572 mbcnt, sb->sb_mbcnt);
573 panic("sbcheck");
574 }
575 }
576 #endif
577
578 /*
579 * As above, except the mbuf chain begins a new record.
580 */
581 void
582 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
583 {
584 struct mbuf *m;
585
586 SOCKBUF_LOCK_ASSERT(sb);
587
588 if (m0 == 0)
589 return;
590 /*
591 * Put the first mbuf on the queue. Note this permits zero length
592 * records.
593 */
594 sballoc(sb, m0);
595 SBLASTRECORDCHK(sb);
596 SBLINKRECORD(sb, m0);
597 sb->sb_mbtail = m0;
598 m = m0->m_next;
599 m0->m_next = 0;
600 if (m && (m0->m_flags & M_EOR)) {
601 m0->m_flags &= ~M_EOR;
602 m->m_flags |= M_EOR;
603 }
604 /* always call sbcompress() so it can do SBLASTMBUFCHK() */
605 sbcompress(sb, m, m0);
606 }
607
608 /*
609 * As above, except the mbuf chain begins a new record.
610 */
611 void
612 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
613 {
614
615 SOCKBUF_LOCK(sb);
616 sbappendrecord_locked(sb, m0);
617 SOCKBUF_UNLOCK(sb);
618 }
619
620 /* Helper routine that appends data, control, and address to a sockbuf. */
621 static int
622 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
623 struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
624 {
625 struct mbuf *m, *n, *nlast;
626 #if MSIZE <= 256
627 if (asa->sa_len > MLEN)
628 return (0);
629 #endif
630 MGET(m, M_DONTWAIT, MT_SONAME);
631 if (m == 0)
632 return (0);
633 m->m_len = asa->sa_len;
634 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
635 if (ctrl_last)
636 ctrl_last->m_next = m0; /* concatenate data to control */
637 else
638 control = m0;
639 m->m_next = control;
640 for (n = m; n->m_next != NULL; n = n->m_next)
641 sballoc(sb, n);
642 sballoc(sb, n);
643 nlast = n;
644 SBLINKRECORD(sb, m);
645
646 sb->sb_mbtail = nlast;
647 SBLASTMBUFCHK(sb);
648
649 SBLASTRECORDCHK(sb);
650 return (1);
651 }
652
653 /*
654 * Append address and data, and optionally, control (ancillary) data to the
655 * receive queue of a socket. If present, m0 must include a packet header
656 * with total length. Returns 0 if no space in sockbuf or insufficient
657 * mbufs.
658 */
659 int
660 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
661 struct mbuf *m0, struct mbuf *control)
662 {
663 struct mbuf *ctrl_last;
664 int space = asa->sa_len;
665
666 SOCKBUF_LOCK_ASSERT(sb);
667
668 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
669 panic("sbappendaddr_locked");
670 if (m0)
671 space += m0->m_pkthdr.len;
672 space += m_length(control, &ctrl_last);
673
674 if (space > sbspace(sb))
675 return (0);
676 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
677 }
678
679 /*
680 * Append address and data, and optionally, control (ancillary) data to the
681 * receive queue of a socket. If present, m0 must include a packet header
682 * with total length. Returns 0 if insufficient mbufs. Does not validate space
683 * on the receiving sockbuf.
684 */
685 int
686 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa,
687 struct mbuf *m0, struct mbuf *control)
688 {
689 struct mbuf *ctrl_last;
690
691 SOCKBUF_LOCK_ASSERT(sb);
692
693 ctrl_last = (control == NULL) ? NULL : m_last(control);
694 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
695 }
696
697 /*
698 * Append address and data, and optionally, control (ancillary) data to the
699 * receive queue of a socket. If present, m0 must include a packet header
700 * with total length. Returns 0 if no space in sockbuf or insufficient
701 * mbufs.
702 */
703 int
704 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
705 struct mbuf *m0, struct mbuf *control)
706 {
707 int retval;
708
709 SOCKBUF_LOCK(sb);
710 retval = sbappendaddr_locked(sb, asa, m0, control);
711 SOCKBUF_UNLOCK(sb);
712 return (retval);
713 }
714
715 int
716 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
717 struct mbuf *control)
718 {
719 struct mbuf *m, *n, *mlast;
720 int space;
721
722 SOCKBUF_LOCK_ASSERT(sb);
723
724 if (control == 0)
725 panic("sbappendcontrol_locked");
726 space = m_length(control, &n) + m_length(m0, NULL);
727
728 if (space > sbspace(sb))
729 return (0);
730 n->m_next = m0; /* concatenate data to control */
731
732 SBLASTRECORDCHK(sb);
733
734 for (m = control; m->m_next; m = m->m_next)
735 sballoc(sb, m);
736 sballoc(sb, m);
737 mlast = m;
738 SBLINKRECORD(sb, control);
739
740 sb->sb_mbtail = mlast;
741 SBLASTMBUFCHK(sb);
742
743 SBLASTRECORDCHK(sb);
744 return (1);
745 }
746
747 int
748 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
749 {
750 int retval;
751
752 SOCKBUF_LOCK(sb);
753 retval = sbappendcontrol_locked(sb, m0, control);
754 SOCKBUF_UNLOCK(sb);
755 return (retval);
756 }
757
758 /*
759 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
760 * (n). If (n) is NULL, the buffer is presumed empty.
761 *
762 * When the data is compressed, mbufs in the chain may be handled in one of
763 * three ways:
764 *
765 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
766 * record boundary, and no change in data type).
767 *
768 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
769 * an mbuf already in the socket buffer. This can occur if an
770 * appropriate mbuf exists, there is room, and no merging of data types
771 * will occur.
772 *
773 * (3) The mbuf may be appended to the end of the existing mbuf chain.
774 *
775 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
776 * end-of-record.
777 */
778 void
779 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
780 {
781 int eor = 0;
782 struct mbuf *o;
783
784 SOCKBUF_LOCK_ASSERT(sb);
785
786 while (m) {
787 eor |= m->m_flags & M_EOR;
788 if (m->m_len == 0 &&
789 (eor == 0 ||
790 (((o = m->m_next) || (o = n)) &&
791 o->m_type == m->m_type))) {
792 if (sb->sb_lastrecord == m)
793 sb->sb_lastrecord = m->m_next;
794 m = m_free(m);
795 continue;
796 }
797 if (n && (n->m_flags & M_EOR) == 0 &&
798 M_WRITABLE(n) &&
799 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
800 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
801 m->m_len <= M_TRAILINGSPACE(n) &&
802 n->m_type == m->m_type) {
803 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
804 (unsigned)m->m_len);
805 n->m_len += m->m_len;
806 sb->sb_cc += m->m_len;
807 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
808 /* XXX: Probably don't need.*/
809 sb->sb_ctl += m->m_len;
810 m = m_free(m);
811 continue;
812 }
813 if (n)
814 n->m_next = m;
815 else
816 sb->sb_mb = m;
817 sb->sb_mbtail = m;
818 sballoc(sb, m);
819 n = m;
820 m->m_flags &= ~M_EOR;
821 m = m->m_next;
822 n->m_next = 0;
823 }
824 if (eor) {
825 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
826 n->m_flags |= eor;
827 }
828 SBLASTMBUFCHK(sb);
829 }
830
831 /*
832 * Free all mbufs in a sockbuf. Check that all resources are reclaimed.
833 */
834 static void
835 sbflush_internal(struct sockbuf *sb)
836 {
837
838 while (sb->sb_mbcnt) {
839 /*
840 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
841 * we would loop forever. Panic instead.
842 */
843 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
844 break;
845 sbdrop_internal(sb, (int)sb->sb_cc);
846 }
847 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
848 panic("sbflush_internal: cc %u || mb %p || mbcnt %u",
849 sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
850 }
851
852 void
853 sbflush_locked(struct sockbuf *sb)
854 {
855
856 SOCKBUF_LOCK_ASSERT(sb);
857 sbflush_internal(sb);
858 }
859
860 void
861 sbflush(struct sockbuf *sb)
862 {
863
864 SOCKBUF_LOCK(sb);
865 sbflush_locked(sb);
866 SOCKBUF_UNLOCK(sb);
867 }
868
869 /*
870 * Drop data from (the front of) a sockbuf.
871 */
872 static void
873 sbdrop_internal(struct sockbuf *sb, int len)
874 {
875 struct mbuf *m;
876 struct mbuf *next;
877
878 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
879 while (len > 0) {
880 if (m == 0) {
881 if (next == 0)
882 panic("sbdrop");
883 m = next;
884 next = m->m_nextpkt;
885 continue;
886 }
887 if (m->m_len > len) {
888 m->m_len -= len;
889 m->m_data += len;
890 sb->sb_cc -= len;
891 if (sb->sb_sndptroff != 0)
892 sb->sb_sndptroff -= len;
893 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
894 sb->sb_ctl -= len;
895 break;
896 }
897 len -= m->m_len;
898 sbfree(sb, m);
899 m = m_free(m);
900 }
901 while (m && m->m_len == 0) {
902 sbfree(sb, m);
903 m = m_free(m);
904 }
905 if (m) {
906 sb->sb_mb = m;
907 m->m_nextpkt = next;
908 } else
909 sb->sb_mb = next;
910 /*
911 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure
912 * sb_lastrecord is up-to-date if we dropped part of the last record.
913 */
914 m = sb->sb_mb;
915 if (m == NULL) {
916 sb->sb_mbtail = NULL;
917 sb->sb_lastrecord = NULL;
918 } else if (m->m_nextpkt == NULL) {
919 sb->sb_lastrecord = m;
920 }
921 }
922
923 /*
924 * Drop data from (the front of) a sockbuf.
925 */
926 void
927 sbdrop_locked(struct sockbuf *sb, int len)
928 {
929
930 SOCKBUF_LOCK_ASSERT(sb);
931
932 sbdrop_internal(sb, len);
933 }
934
935 void
936 sbdrop(struct sockbuf *sb, int len)
937 {
938
939 SOCKBUF_LOCK(sb);
940 sbdrop_locked(sb, len);
941 SOCKBUF_UNLOCK(sb);
942 }
943
944 /*
945 * Maintain a pointer and offset pair into the socket buffer mbuf chain to
946 * avoid traversal of the entire socket buffer for larger offsets.
947 */
948 struct mbuf *
949 sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff)
950 {
951 struct mbuf *m, *ret;
952
953 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
954 KASSERT(off + len <= sb->sb_cc, ("%s: beyond sb", __func__));
955 KASSERT(sb->sb_sndptroff <= sb->sb_cc, ("%s: sndptroff broken", __func__));
956
957 /*
958 * Is off below stored offset? Happens on retransmits.
959 * Just return, we can't help here.
960 */
961 if (sb->sb_sndptroff > off) {
962 *moff = off;
963 return (sb->sb_mb);
964 }
965
966 /* Return closest mbuf in chain for current offset. */
967 *moff = off - sb->sb_sndptroff;
968 m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb;
969 if (*moff == m->m_len) {
970 *moff = 0;
971 sb->sb_sndptroff += m->m_len;
972 m = ret = m->m_next;
973 KASSERT(ret->m_len > 0,
974 ("mbuf %p in sockbuf %p chain has no valid data", ret, sb));
975 }
976
977 /* Advance by len to be as close as possible for the next transmit. */
978 for (off = off - sb->sb_sndptroff + len - 1;
979 off > 0 && m != NULL && off >= m->m_len;
980 m = m->m_next) {
981 sb->sb_sndptroff += m->m_len;
982 off -= m->m_len;
983 }
984 if (off > 0 && m == NULL)
985 panic("%s: sockbuf %p and mbuf %p clashing", __func__, sb, ret);
986 sb->sb_sndptr = m;
987
988 return (ret);
989 }
990
991 /*
992 * Return the first mbuf and the mbuf data offset for the provided
993 * send offset without changing the "sb_sndptroff" field.
994 */
995 struct mbuf *
996 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff)
997 {
998 struct mbuf *m;
999
1000 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1001
1002 /*
1003 * If the "off" is below the stored offset, which happens on
1004 * retransmits, just use "sb_mb":
1005 */
1006 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1007 m = sb->sb_mb;
1008 } else {
1009 m = sb->sb_sndptr;
1010 off -= sb->sb_sndptroff;
1011 }
1012 while (off > 0 && m != NULL) {
1013 if (off < m->m_len)
1014 break;
1015 off -= m->m_len;
1016 m = m->m_next;
1017 }
1018 *moff = off;
1019 return (m);
1020 }
1021
1022 /*
1023 * Drop a record off the front of a sockbuf and move the next record to the
1024 * front.
1025 */
1026 void
1027 sbdroprecord_locked(struct sockbuf *sb)
1028 {
1029 struct mbuf *m;
1030
1031 SOCKBUF_LOCK_ASSERT(sb);
1032
1033 m = sb->sb_mb;
1034 if (m) {
1035 sb->sb_mb = m->m_nextpkt;
1036 do {
1037 sbfree(sb, m);
1038 m = m_free(m);
1039 } while (m);
1040 }
1041 SB_EMPTY_FIXUP(sb);
1042 }
1043
1044 /*
1045 * Drop a record off the front of a sockbuf and move the next record to the
1046 * front.
1047 */
1048 void
1049 sbdroprecord(struct sockbuf *sb)
1050 {
1051
1052 SOCKBUF_LOCK(sb);
1053 sbdroprecord_locked(sb);
1054 SOCKBUF_UNLOCK(sb);
1055 }
1056
1057 /*
1058 * Create a "control" mbuf containing the specified data with the specified
1059 * type for presentation on a socket buffer.
1060 */
1061 struct mbuf *
1062 sbcreatecontrol(caddr_t p, int size, int type, int level)
1063 {
1064 struct cmsghdr *cp;
1065 struct mbuf *m;
1066
1067 if (CMSG_SPACE((u_int)size) > MCLBYTES)
1068 return ((struct mbuf *) NULL);
1069 if (CMSG_SPACE((u_int)size) > MLEN)
1070 m = m_getcl(M_DONTWAIT, MT_CONTROL, 0);
1071 else
1072 m = m_get(M_DONTWAIT, MT_CONTROL);
1073 if (m == NULL)
1074 return ((struct mbuf *) NULL);
1075 cp = mtod(m, struct cmsghdr *);
1076 m->m_len = 0;
1077 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
1078 ("sbcreatecontrol: short mbuf"));
1079 /*
1080 * Don't leave the padding between the msg header and the
1081 * cmsg data and the padding after the cmsg data un-initialized.
1082 */
1083 bzero(cp, CMSG_SPACE((u_int)size));
1084 if (p != NULL)
1085 (void)memcpy(CMSG_DATA(cp), p, size);
1086 m->m_len = CMSG_SPACE(size);
1087 cp->cmsg_len = CMSG_LEN(size);
1088 cp->cmsg_level = level;
1089 cp->cmsg_type = type;
1090 return (m);
1091 }
1092
1093 /*
1094 * This does the same for socket buffers that sotoxsocket does for sockets:
1095 * generate an user-format data structure describing the socket buffer. Note
1096 * that the xsockbuf structure, since it is always embedded in a socket, does
1097 * not include a self pointer nor a length. We make this entry point public
1098 * in case some other mechanism needs it.
1099 */
1100 void
1101 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1102 {
1103
1104 xsb->sb_cc = sb->sb_cc;
1105 xsb->sb_hiwat = sb->sb_hiwat;
1106 xsb->sb_mbcnt = sb->sb_mbcnt;
1107 xsb->sb_mcnt = sb->sb_mcnt;
1108 xsb->sb_ccnt = sb->sb_ccnt;
1109 xsb->sb_mbmax = sb->sb_mbmax;
1110 xsb->sb_lowat = sb->sb_lowat;
1111 xsb->sb_flags = sb->sb_flags;
1112 xsb->sb_timeo = sb->sb_timeo;
1113 }
1114
1115 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1116 static int dummy;
1117 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1118 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1119 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1120 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1121 &sb_efficiency, 0, "");
Cache object: a30021736b89a078b7209016c0818a92
|