2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_param.h"
38 #include <sys/param.h>
39 #include <sys/aio.h> /* for aio_swake proto */
40 #include <sys/domain.h>
41 #include <sys/event.h>
42 #include <sys/eventhandler.h>
43 #include <sys/file.h> /* for maxfiles */
44 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/mutex.h>
51 #include <sys/protosw.h>
52 #include <sys/resourcevar.h>
53 #include <sys/signalvar.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
62 void (*aio_swake)(struct socket *, struct sockbuf *);
65 * Primitive routines for operating on sockets and socket buffers
68 u_long sb_max = SB_MAX;
69 static u_long sb_max_adj =
70 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
72 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
75 static int regression_sonewconn_earlytest = 1;
76 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
77 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
81 * Procedures to manipulate state flags of socket
82 * and do appropriate wakeups. Normal sequence from the
83 * active (originating) side is that soisconnecting() is
84 * called during processing of connect() call,
85 * resulting in an eventual call to soisconnected() if/when the
86 * connection is established. When the connection is torn down
87 * soisdisconnecting() is called during processing of disconnect() call,
88 * and soisdisconnected() is called when the connection to the peer
89 * is totally severed. The semantics of these routines are such that
90 * connectionless protocols can call soisconnected() and soisdisconnected()
91 * only, bypassing the in-progress calls when setting up a ``connection''
94 * From the passive side, a socket is created with
95 * two queues of sockets: so_incomp for connections in progress
96 * and so_comp for connections already made and awaiting user acceptance.
97 * As a protocol is preparing incoming connections, it creates a socket
98 * structure queued on so_incomp by calling sonewconn(). When the connection
99 * is established, soisconnected() is called, and transfers the
100 * socket structure to so_comp, making it available to accept().
102 * If a socket is closed with sockets on either
103 * so_incomp or so_comp, these sockets are dropped.
105 * If higher level protocols are implemented in
106 * the kernel, the wakeups done here will sometimes
107 * cause software-interrupt process scheduling.
112 register struct socket *so;
116 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
117 so->so_state |= SS_ISCONNECTING;
129 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
130 so->so_state |= SS_ISCONNECTED;
132 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
133 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
135 TAILQ_REMOVE(&head->so_incomp, so, so_list);
137 so->so_qstate &= ~SQ_INCOMP;
138 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
140 so->so_qstate |= SQ_COMP;
143 wakeup_one(&head->so_timeo);
147 head->so_accf->so_accept_filter->accf_callback;
148 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
149 so->so_rcv.sb_flags |= SB_UPCALL;
150 so->so_options &= ~SO_ACCEPTFILTER;
152 so->so_upcall(so, so->so_upcallarg, M_DONTWAIT);
158 wakeup(&so->so_timeo);
164 soisdisconnecting(so)
165 register struct socket *so;
169 * XXXRW: This code assumes that SOCK_LOCK(so) and
170 * SOCKBUF_LOCK(&so->so_rcv) are the same.
172 SOCKBUF_LOCK(&so->so_rcv);
173 so->so_state &= ~SS_ISCONNECTING;
174 so->so_state |= SS_ISDISCONNECTING;
175 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
176 sorwakeup_locked(so);
177 SOCKBUF_LOCK(&so->so_snd);
178 so->so_snd.sb_state |= SBS_CANTSENDMORE;
179 sowwakeup_locked(so);
180 wakeup(&so->so_timeo);
185 register struct socket *so;
189 * XXXRW: This code assumes that SOCK_LOCK(so) and
190 * SOCKBUF_LOCK(&so->so_rcv) are the same.
192 SOCKBUF_LOCK(&so->so_rcv);
193 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
194 so->so_state |= SS_ISDISCONNECTED;
195 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
196 sorwakeup_locked(so);
197 SOCKBUF_LOCK(&so->so_snd);
198 so->so_snd.sb_state |= SBS_CANTSENDMORE;
199 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
200 sowwakeup_locked(so);
201 wakeup(&so->so_timeo);
205 * When an attempt at a new connection is noted on a socket
206 * which accepts connections, sonewconn is called. If the
207 * connection is possible (subject to space constraints, etc.)
208 * then we allocate a new structure, propoerly linked into the
209 * data structure of the original socket, and return this.
210 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
212 * note: the ref count on the socket is 0 on return
215 sonewconn(head, connstatus)
216 register struct socket *head;
219 register struct socket *so;
223 over = (head->so_qlen > 3 * head->so_qlimit / 2);
226 if (regression_sonewconn_earlytest && over)
231 so = soalloc(M_NOWAIT);
234 if ((head->so_options & SO_ACCEPTFILTER) != 0)
237 so->so_type = head->so_type;
238 so->so_options = head->so_options &~ SO_ACCEPTCONN;
239 so->so_linger = head->so_linger;
240 so->so_state = head->so_state | SS_NOFDREF;
241 so->so_proto = head->so_proto;
242 so->so_timeo = head->so_timeo;
243 so->so_cred = crhold(head->so_cred);
246 mac_create_socket_from_socket(head, so);
249 knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv),
251 knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd),
253 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
254 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
258 so->so_state |= connstatus;
261 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
262 so->so_qstate |= SQ_COMP;
266 * Keep removing sockets from the head until there's room for
267 * us to insert on the tail. In pre-locking revisions, this
268 * was a simple if(), but as we could be racing with other
269 * threads and soabort() requires dropping locks, we must
270 * loop waiting for the condition to be true.
272 while (head->so_incqlen > head->so_qlimit) {
274 sp = TAILQ_FIRST(&head->so_incomp);
275 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
277 sp->so_qstate &= ~SQ_INCOMP;
283 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
284 so->so_qstate |= SQ_INCOMP;
290 wakeup_one(&head->so_timeo);
296 * Socantsendmore indicates that no more data will be sent on the
297 * socket; it would normally be applied to a socket when the user
298 * informs the system that no more data is to be sent, by the protocol
299 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
300 * will be received, and will normally be applied to the socket by a
301 * protocol when it detects that the peer will send no more data.
302 * Data queued for reading in the socket may yet be read.
305 socantsendmore_locked(so)
309 SOCKBUF_LOCK_ASSERT(&so->so_snd);
311 so->so_snd.sb_state |= SBS_CANTSENDMORE;
312 sowwakeup_locked(so);
313 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
321 SOCKBUF_LOCK(&so->so_snd);
322 socantsendmore_locked(so);
323 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
327 socantrcvmore_locked(so)
331 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
333 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
334 sorwakeup_locked(so);
335 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
343 SOCKBUF_LOCK(&so->so_rcv);
344 socantrcvmore_locked(so);
345 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
349 * Wait for data to arrive at/drain from a socket buffer.
356 SOCKBUF_LOCK_ASSERT(sb);
358 sb->sb_flags |= SB_WAIT;
359 return (msleep(&sb->sb_cc, &sb->sb_mtx,
360 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
365 * Lock a sockbuf already known to be locked;
366 * return any error returned from sleep (EINTR).
370 register struct sockbuf *sb;
374 SOCKBUF_LOCK_ASSERT(sb);
376 while (sb->sb_flags & SB_LOCK) {
377 sb->sb_flags |= SB_WANT;
378 error = msleep(&sb->sb_flags, &sb->sb_mtx,
379 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
384 sb->sb_flags |= SB_LOCK;
389 * Wakeup processes waiting on a socket buffer. Do asynchronous
390 * notification via SIGIO if the socket has the SS_ASYNC flag set.
392 * Called with the socket buffer lock held; will release the lock by the end
393 * of the function. This allows the caller to acquire the socket buffer lock
394 * while testing for the need for various sorts of wakeup and hold it through
395 * to the point where it's no longer required. We currently hold the lock
396 * through calls out to other subsystems (with the exception of kqueue), and
397 * then release it to avoid lock order issues. It's not clear that's
402 register struct socket *so;
403 register struct sockbuf *sb;
406 SOCKBUF_LOCK_ASSERT(sb);
408 selwakeuppri(&sb->sb_sel, PSOCK);
409 sb->sb_flags &= ~SB_SEL;
410 if (sb->sb_flags & SB_WAIT) {
411 sb->sb_flags &= ~SB_WAIT;
414 KNOTE_LOCKED(&sb->sb_sel.si_note, 0);
416 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
417 pgsigio(&so->so_sigio, SIGIO, 0);
418 if (sb->sb_flags & SB_UPCALL)
419 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
420 if (sb->sb_flags & SB_AIO)
422 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
426 * Socket buffer (struct sockbuf) utility routines.
428 * Each socket contains two socket buffers: one for sending data and
429 * one for receiving data. Each buffer contains a queue of mbufs,
430 * information about the number of mbufs and amount of data in the
431 * queue, and other fields allowing select() statements and notification
432 * on data availability to be implemented.
434 * Data stored in a socket buffer is maintained as a list of records.
435 * Each record is a list of mbufs chained together with the m_next
436 * field. Records are chained together with the m_nextpkt field. The upper
437 * level routine soreceive() expects the following conventions to be
438 * observed when placing information in the receive buffer:
440 * 1. If the protocol requires each message be preceded by the sender's
441 * name, then a record containing that name must be present before
442 * any associated data (mbuf's must be of type MT_SONAME).
443 * 2. If the protocol supports the exchange of ``access rights'' (really
444 * just additional data associated with the message), and there are
445 * ``rights'' to be received, then a record containing this data
446 * should be present (mbuf's must be of type MT_RIGHTS).
447 * 3. If a name or rights record exists, then it must be followed by
448 * a data record, perhaps of zero length.
450 * Before using a new socket structure it is first necessary to reserve
451 * buffer space to the socket, by calling sbreserve(). This should commit
452 * some of the available buffer space in the system buffer pool for the
453 * socket (currently, it does nothing but enforce limits). The space
454 * should be released by calling sbrelease() when the socket is destroyed.
458 soreserve(so, sndcc, rcvcc)
459 register struct socket *so;
462 struct thread *td = curthread;
464 SOCKBUF_LOCK(&so->so_snd);
465 SOCKBUF_LOCK(&so->so_rcv);
466 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
468 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
470 if (so->so_rcv.sb_lowat == 0)
471 so->so_rcv.sb_lowat = 1;
472 if (so->so_snd.sb_lowat == 0)
473 so->so_snd.sb_lowat = MCLBYTES;
474 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
475 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
476 SOCKBUF_UNLOCK(&so->so_rcv);
477 SOCKBUF_UNLOCK(&so->so_snd);
480 sbrelease_locked(&so->so_snd, so);
482 SOCKBUF_UNLOCK(&so->so_rcv);
483 SOCKBUF_UNLOCK(&so->so_snd);
488 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
491 u_long old_sb_max = sb_max;
493 error = SYSCTL_OUT(req, arg1, sizeof(u_long));
494 if (error || !req->newptr)
496 error = SYSCTL_IN(req, arg1, sizeof(u_long));
499 if (sb_max < MSIZE + MCLBYTES) {
503 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
508 * Allot mbufs to a sockbuf.
509 * Attempt to scale mbmax so that mbcnt doesn't become limiting
510 * if buffering efficiency is near the normal case.
513 sbreserve_locked(sb, cc, so, td)
521 SOCKBUF_LOCK_ASSERT(sb);
524 * td will only be NULL when we're in an interrupt
525 * (e.g. in tcp_input())
530 PROC_LOCK(td->td_proc);
531 sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE);
532 PROC_UNLOCK(td->td_proc);
534 sbsize_limit = RLIM_INFINITY;
535 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
538 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
539 if (sb->sb_lowat > sb->sb_hiwat)
540 sb->sb_lowat = sb->sb_hiwat;
545 sbreserve(sb, cc, so, td)
554 error = sbreserve_locked(sb, cc, so, td);
560 * Free mbufs held by a socket, and reserved mbuf space.
563 sbrelease_locked(sb, so)
568 SOCKBUF_LOCK_ASSERT(sb);
571 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
583 sbrelease_locked(sb, so);
587 * Routines to add and remove
588 * data from an mbuf queue.
590 * The routines sbappend() or sbappendrecord() are normally called to
591 * append new mbufs to a socket buffer, after checking that adequate
592 * space is available, comparing the function sbspace() with the amount
593 * of data to be added. sbappendrecord() differs from sbappend() in
594 * that data supplied is treated as the beginning of a new record.
595 * To place a sender's address, optional access rights, and data in a
596 * socket receive buffer, sbappendaddr() should be used. To place
597 * access rights and data in a socket receive buffer, sbappendrights()
598 * should be used. In either case, the new data begins a new record.
599 * Note that unlike sbappend() and sbappendrecord(), these routines check
600 * for the caller that there will be enough space to store the data.
601 * Each fails if there is not enough space, or if it cannot find mbufs
602 * to store additional information in.
604 * Reliable protocols may use the socket send buffer to hold data
605 * awaiting acknowledgement. Data is normally copied from a socket
606 * send buffer in a protocol with m_copy for output to a peer,
607 * and then removing the data from the socket buffer with sbdrop()
608 * or sbdroprecord() when the data is acknowledged by the peer.
613 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
615 struct mbuf *m = sb->sb_mb;
617 SOCKBUF_LOCK_ASSERT(sb);
619 while (m && m->m_nextpkt)
622 if (m != sb->sb_lastrecord) {
623 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
624 __func__, sb->sb_mb, sb->sb_lastrecord, m);
625 printf("packet chain:\n");
626 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
628 panic("%s from %s:%u", __func__, file, line);
633 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
635 struct mbuf *m = sb->sb_mb;
638 SOCKBUF_LOCK_ASSERT(sb);
640 while (m && m->m_nextpkt)
643 while (m && m->m_next)
646 if (m != sb->sb_mbtail) {
647 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
648 __func__, sb->sb_mb, sb->sb_mbtail, m);
649 printf("packet tree:\n");
650 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
652 for (n = m; n != NULL; n = n->m_next)
656 panic("%s from %s:%u", __func__, file, line);
659 #endif /* SOCKBUF_DEBUG */
661 #define SBLINKRECORD(sb, m0) do { \
662 SOCKBUF_LOCK_ASSERT(sb); \
663 if ((sb)->sb_lastrecord != NULL) \
664 (sb)->sb_lastrecord->m_nextpkt = (m0); \
666 (sb)->sb_mb = (m0); \
667 (sb)->sb_lastrecord = (m0); \
668 } while (/*CONSTCOND*/0)
671 * Append mbuf chain m to the last record in the
672 * socket buffer sb. The additional space associated
673 * the mbuf chain is recorded in sb. Empty mbufs are
674 * discarded and mbufs are compacted where possible.
677 sbappend_locked(sb, m)
681 register struct mbuf *n;
683 SOCKBUF_LOCK_ASSERT(sb);
694 if (n->m_flags & M_EOR) {
695 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
698 } while (n->m_next && (n = n->m_next));
701 * XXX Would like to simply use sb_mbtail here, but
702 * XXX I need to verify that I won't miss an EOR that
705 if ((n = sb->sb_lastrecord) != NULL) {
707 if (n->m_flags & M_EOR) {
708 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
711 } while (n->m_next && (n = n->m_next));
714 * If this is the first record in the socket buffer,
715 * it's also the last record.
717 sb->sb_lastrecord = m;
720 sbcompress(sb, m, n);
725 * Append mbuf chain m to the last record in the
726 * socket buffer sb. The additional space associated
727 * the mbuf chain is recorded in sb. Empty mbufs are
728 * discarded and mbufs are compacted where possible.
737 sbappend_locked(sb, m);
742 * This version of sbappend() should only be used when the caller
743 * absolutely knows that there will never be more than one record
744 * in the socket buffer, that is, a stream protocol (such as TCP).
747 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m)
749 SOCKBUF_LOCK_ASSERT(sb);
751 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
752 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
756 sbcompress(sb, m, sb->sb_mbtail);
758 sb->sb_lastrecord = sb->sb_mb;
763 * This version of sbappend() should only be used when the caller
764 * absolutely knows that there will never be more than one record
765 * in the socket buffer, that is, a stream protocol (such as TCP).
768 sbappendstream(struct sockbuf *sb, struct mbuf *m)
772 sbappendstream_locked(sb, m);
783 u_long len = 0, mbcnt = 0;
785 SOCKBUF_LOCK_ASSERT(sb);
787 for (m = sb->sb_mb; m; m = n) {
789 for (; m; m = m->m_next) {
792 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
793 mbcnt += m->m_ext.ext_size;
796 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
797 printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc,
798 mbcnt, sb->sb_mbcnt);
805 * As above, except the mbuf chain
806 * begins a new record.
809 sbappendrecord_locked(sb, m0)
810 register struct sockbuf *sb;
811 register struct mbuf *m0;
813 register struct mbuf *m;
815 SOCKBUF_LOCK_ASSERT(sb);
824 * Put the first mbuf on the queue.
825 * Note this permits zero length records.
829 SBLINKRECORD(sb, m0);
836 if (m && (m0->m_flags & M_EOR)) {
837 m0->m_flags &= ~M_EOR;
840 sbcompress(sb, m, m0);
844 * As above, except the mbuf chain
845 * begins a new record.
848 sbappendrecord(sb, m0)
849 register struct sockbuf *sb;
850 register struct mbuf *m0;
854 sbappendrecord_locked(sb, m0);
859 * As above except that OOB data
860 * is inserted at the beginning of the sockbuf,
861 * but after any other OOB data.
864 sbinsertoob_locked(sb, m0)
865 register struct sockbuf *sb;
866 register struct mbuf *m0;
868 register struct mbuf *m;
869 register struct mbuf **mp;
871 SOCKBUF_LOCK_ASSERT(sb);
875 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
881 continue; /* WANT next train */
886 goto again; /* inspect THIS train further */
891 * Put the first mbuf on the queue.
892 * Note this permits zero length records.
899 if (m && (m0->m_flags & M_EOR)) {
900 m0->m_flags &= ~M_EOR;
903 sbcompress(sb, m, m0);
907 * As above except that OOB data
908 * is inserted at the beginning of the sockbuf,
909 * but after any other OOB data.
913 register struct sockbuf *sb;
914 register struct mbuf *m0;
918 sbinsertoob_locked(sb, m0);
923 * Append address and data, and optionally, control (ancillary) data
924 * to the receive queue of a socket. If present,
925 * m0 must include a packet header with total length.
926 * Returns 0 if no space in sockbuf or insufficient mbufs.
929 sbappendaddr_locked(sb, asa, m0, control)
931 const struct sockaddr *asa;
932 struct mbuf *m0, *control;
934 struct mbuf *m, *n, *nlast;
935 int space = asa->sa_len;
937 SOCKBUF_LOCK_ASSERT(sb);
939 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
940 panic("sbappendaddr_locked");
942 space += m0->m_pkthdr.len;
943 space += m_length(control, &n);
945 if (space > sbspace(sb))
948 if (asa->sa_len > MLEN)
951 MGET(m, M_DONTWAIT, MT_SONAME);
954 m->m_len = asa->sa_len;
955 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
957 n->m_next = m0; /* concatenate data to control */
961 for (n = m; n->m_next != NULL; n = n->m_next)
967 sb->sb_mbtail = nlast;
975 * Append address and data, and optionally, control (ancillary) data
976 * to the receive queue of a socket. If present,
977 * m0 must include a packet header with total length.
978 * Returns 0 if no space in sockbuf or insufficient mbufs.
981 sbappendaddr(sb, asa, m0, control)
983 const struct sockaddr *asa;
984 struct mbuf *m0, *control;
989 retval = sbappendaddr_locked(sb, asa, m0, control);
995 sbappendcontrol_locked(sb, m0, control)
997 struct mbuf *control, *m0;
999 struct mbuf *m, *n, *mlast;
1002 SOCKBUF_LOCK_ASSERT(sb);
1005 panic("sbappendcontrol_locked");
1006 space = m_length(control, &n) + m_length(m0, NULL);
1008 if (space > sbspace(sb))
1010 n->m_next = m0; /* concatenate data to control */
1012 SBLASTRECORDCHK(sb);
1014 for (m = control; m->m_next; m = m->m_next)
1018 SBLINKRECORD(sb, control);
1020 sb->sb_mbtail = mlast;
1023 SBLASTRECORDCHK(sb);
1028 sbappendcontrol(sb, m0, control)
1030 struct mbuf *control, *m0;
1035 retval = sbappendcontrol_locked(sb, m0, control);
1041 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
1042 * (n). If (n) is NULL, the buffer is presumed empty.
1044 * When the data is compressed, mbufs in the chain may be handled in one of
1047 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
1048 * record boundary, and no change in data type).
1050 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
1051 * an mbuf already in the socket buffer. This can occur if an
1052 * appropriate mbuf exists, there is room, and no merging of data types
1055 * (3) The mbuf may be appended to the end of the existing mbuf chain.
1057 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
1061 sbcompress(sb, m, n)
1062 register struct sockbuf *sb;
1063 register struct mbuf *m, *n;
1065 register int eor = 0;
1066 register struct mbuf *o;
1068 SOCKBUF_LOCK_ASSERT(sb);
1071 eor |= m->m_flags & M_EOR;
1072 if (m->m_len == 0 &&
1074 (((o = m->m_next) || (o = n)) &&
1075 o->m_type == m->m_type))) {
1076 if (sb->sb_lastrecord == m)
1077 sb->sb_lastrecord = m->m_next;
1081 if (n && (n->m_flags & M_EOR) == 0 &&
1083 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1084 m->m_len <= M_TRAILINGSPACE(n) &&
1085 n->m_type == m->m_type) {
1086 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
1087 (unsigned)m->m_len);
1088 n->m_len += m->m_len;
1089 sb->sb_cc += m->m_len;
1090 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1091 /* XXX: Probably don't need.*/
1092 sb->sb_ctl += m->m_len;
1103 m->m_flags &= ~M_EOR;
1108 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
1115 * Free all mbufs in a sockbuf.
1116 * Check that all resources are reclaimed.
1120 register struct sockbuf *sb;
1123 SOCKBUF_LOCK_ASSERT(sb);
1125 if (sb->sb_flags & SB_LOCK)
1126 panic("sbflush_locked: locked");
1127 while (sb->sb_mbcnt) {
1129 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
1130 * we would loop forever. Panic instead.
1132 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1134 sbdrop_locked(sb, (int)sb->sb_cc);
1136 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
1137 panic("sbflush_locked: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
1142 register struct sockbuf *sb;
1151 * Drop data from (the front of) a sockbuf.
1154 sbdrop_locked(sb, len)
1155 register struct sockbuf *sb;
1158 register struct mbuf *m;
1161 SOCKBUF_LOCK_ASSERT(sb);
1163 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1169 next = m->m_nextpkt;
1172 if (m->m_len > len) {
1176 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1184 while (m && m->m_len == 0) {
1190 m->m_nextpkt = next;
1194 * First part is an inline SB_EMPTY_FIXUP(). Second part
1195 * makes sure sb_lastrecord is up-to-date if we dropped
1196 * part of the last record.
1200 sb->sb_mbtail = NULL;
1201 sb->sb_lastrecord = NULL;
1202 } else if (m->m_nextpkt == NULL) {
1203 sb->sb_lastrecord = m;
1208 * Drop data from (the front of) a sockbuf.
1212 register struct sockbuf *sb;
1217 sbdrop_locked(sb, len);
1222 * Drop a record off the front of a sockbuf
1223 * and move the next record to the front.
1226 sbdroprecord_locked(sb)
1227 register struct sockbuf *sb;
1229 register struct mbuf *m;
1231 SOCKBUF_LOCK_ASSERT(sb);
1235 sb->sb_mb = m->m_nextpkt;
1245 * Drop a record off the front of a sockbuf
1246 * and move the next record to the front.
1250 register struct sockbuf *sb;
1254 sbdroprecord_locked(sb);
1259 * Create a "control" mbuf containing the specified data
1260 * with the specified type for presentation on a socket buffer.
1263 sbcreatecontrol(p, size, type, level)
1268 register struct cmsghdr *cp;
1271 if (CMSG_SPACE((u_int)size) > MCLBYTES)
1272 return ((struct mbuf *) NULL);
1273 if (CMSG_SPACE((u_int)size) > MLEN)
1274 m = m_getcl(M_DONTWAIT, MT_CONTROL, 0);
1276 m = m_get(M_DONTWAIT, MT_CONTROL);
1278 return ((struct mbuf *) NULL);
1279 cp = mtod(m, struct cmsghdr *);
1281 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
1282 ("sbcreatecontrol: short mbuf"));
1284 (void)memcpy(CMSG_DATA(cp), p, size);
1285 m->m_len = CMSG_SPACE(size);
1286 cp->cmsg_len = CMSG_LEN(size);
1287 cp->cmsg_level = level;
1288 cp->cmsg_type = type;
1293 * Some routines that return EOPNOTSUPP for entry points that are not
1294 * supported by a protocol. Fill in as needed.
1297 pru_abort_notsupp(struct socket *so)
1303 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
1309 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
1315 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
1321 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
1327 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
1333 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
1334 struct ifnet *ifp, struct thread *td)
1340 pru_detach_notsupp(struct socket *so)
1346 pru_disconnect_notsupp(struct socket *so)
1352 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
1358 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
1364 pru_rcvd_notsupp(struct socket *so, int flags)
1370 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
1376 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
1377 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1383 * This isn't really a ``null'' operation, but it's the default one
1384 * and doesn't do anything destructive.
1387 pru_sense_null(struct socket *so, struct stat *sb)
1389 sb->st_blksize = so->so_snd.sb_hiwat;
1394 pru_shutdown_notsupp(struct socket *so)
1400 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
1406 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
1407 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1413 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
1414 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
1421 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
1428 * For protocol types that don't keep cached copies of labels in their
1429 * pcbs, provide a null sosetlabel that does a NOOP.
1432 pru_sosetlabel_null(struct socket *so)
1438 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1441 sodupsockaddr(const struct sockaddr *sa, int mflags)
1443 struct sockaddr *sa2;
1445 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
1447 bcopy(sa, sa2, sa->sa_len);
1452 * Create an external-format (``xsocket'') structure using the information
1453 * in the kernel-format socket structure pointed to by so. This is done
1454 * to reduce the spew of irrelevant information over this interface,
1455 * to isolate user code from changes in the kernel structure, and
1456 * potentially to provide information-hiding if we decide that
1457 * some of this information should be hidden from users.
1460 sotoxsocket(struct socket *so, struct xsocket *xso)
1462 xso->xso_len = sizeof *xso;
1464 xso->so_type = so->so_type;
1465 xso->so_options = so->so_options;
1466 xso->so_linger = so->so_linger;
1467 xso->so_state = so->so_state;
1468 xso->so_pcb = so->so_pcb;
1469 xso->xso_protocol = so->so_proto->pr_protocol;
1470 xso->xso_family = so->so_proto->pr_domain->dom_family;
1471 xso->so_qlen = so->so_qlen;
1472 xso->so_incqlen = so->so_incqlen;
1473 xso->so_qlimit = so->so_qlimit;
1474 xso->so_timeo = so->so_timeo;
1475 xso->so_error = so->so_error;
1476 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
1477 xso->so_oobmark = so->so_oobmark;
1478 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
1479 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
1480 xso->so_uid = so->so_cred->cr_uid;
1484 * This does the same for sockbufs. Note that the xsockbuf structure,
1485 * since it is always embedded in a socket, does not include a self
1486 * pointer nor a length. We make this entry point public in case
1487 * some other mechanism needs it.
1490 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1492 xsb->sb_cc = sb->sb_cc;
1493 xsb->sb_hiwat = sb->sb_hiwat;
1494 xsb->sb_mbcnt = sb->sb_mbcnt;
1495 xsb->sb_mbmax = sb->sb_mbmax;
1496 xsb->sb_lowat = sb->sb_lowat;
1497 xsb->sb_flags = sb->sb_flags;
1498 xsb->sb_timeo = sb->sb_timeo;
1502 * Here is the definition of some of the basic objects in the kern.ipc
1503 * branch of the MIB.
1505 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
1507 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1509 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1510 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1511 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1513 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
1515 int error, newmaxsockets;
1517 newmaxsockets = maxsockets;
1518 error = sysctl_handle_int(oidp, &newmaxsockets, sizeof(int), req);
1519 if (error == 0 && req->newptr) {
1520 if (newmaxsockets > maxsockets) {
1521 maxsockets = newmaxsockets;
1522 if (maxsockets > ((maxfiles / 4) * 3)) {
1523 maxfiles = (maxsockets * 5) / 4;
1524 maxfilesperproc = (maxfiles * 9) / 10;
1526 EVENTHANDLER_INVOKE(maxsockets_change);
1532 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
1533 &maxsockets, 0, sysctl_maxsockets, "IU",
1534 "Maximum number of sockets avaliable");
1535 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1536 &sb_efficiency, 0, "");
1539 * Initialise maxsockets
1541 static void init_maxsockets(void *ignored)
1543 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
1544 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
1546 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);