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/file.h> /* for maxfiles */
43 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
50 #include <sys/protosw.h>
51 #include <sys/resourcevar.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
61 void (*aio_swake)(struct socket *, struct sockbuf *);
64 * Primitive routines for operating on sockets and socket buffers
67 u_long sb_max = SB_MAX;
68 static u_long sb_max_adj =
69 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
71 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
74 * Procedures to manipulate state flags of socket
75 * and do appropriate wakeups. Normal sequence from the
76 * active (originating) side is that soisconnecting() is
77 * called during processing of connect() call,
78 * resulting in an eventual call to soisconnected() if/when the
79 * connection is established. When the connection is torn down
80 * soisdisconnecting() is called during processing of disconnect() call,
81 * and soisdisconnected() is called when the connection to the peer
82 * is totally severed. The semantics of these routines are such that
83 * connectionless protocols can call soisconnected() and soisdisconnected()
84 * only, bypassing the in-progress calls when setting up a ``connection''
87 * From the passive side, a socket is created with
88 * two queues of sockets: so_incomp for connections in progress
89 * and so_comp for connections already made and awaiting user acceptance.
90 * As a protocol is preparing incoming connections, it creates a socket
91 * structure queued on so_incomp by calling sonewconn(). When the connection
92 * is established, soisconnected() is called, and transfers the
93 * socket structure to so_comp, making it available to accept().
95 * If a socket is closed with sockets on either
96 * so_incomp or so_comp, these sockets are dropped.
98 * If higher level protocols are implemented in
99 * the kernel, the wakeups done here will sometimes
100 * cause software-interrupt process scheduling.
105 register struct socket *so;
109 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
110 so->so_state |= SS_ISCONNECTING;
121 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
122 so->so_state |= SS_ISCONNECTED;
126 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
127 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
128 TAILQ_REMOVE(&head->so_incomp, so, so_list);
130 so->so_qstate &= ~SQ_INCOMP;
131 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
133 so->so_qstate |= SQ_COMP;
136 wakeup_one(&head->so_timeo);
141 head->so_accf->so_accept_filter->accf_callback;
142 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
143 so->so_rcv.sb_flags |= SB_UPCALL;
144 so->so_options &= ~SO_ACCEPTFILTER;
146 so->so_upcall(so, so->so_upcallarg, M_TRYWAIT);
151 wakeup(&so->so_timeo);
157 soisdisconnecting(so)
158 register struct socket *so;
162 * XXXRW: This code separately acquires SOCK_LOCK(so) and
163 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same mutex to
164 * avoid introducing the assumption that they are the same.
167 so->so_state &= ~SS_ISCONNECTING;
168 so->so_state |= SS_ISDISCONNECTING;
170 SOCKBUF_LOCK(&so->so_rcv);
171 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
172 sorwakeup_locked(so);
173 SOCKBUF_LOCK(&so->so_snd);
174 so->so_snd.sb_state |= SBS_CANTSENDMORE;
175 sowwakeup_locked(so);
176 wakeup(&so->so_timeo);
181 register struct socket *so;
185 * XXXRW: This code separately acquires SOCK_LOCK(so) and
186 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same mutex to
187 * avoid introducing the assumption that they are the same.
189 /* XXXRW: so_state locking? */
191 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
192 so->so_state |= SS_ISDISCONNECTED;
194 SOCKBUF_LOCK(&so->so_rcv);
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);
227 so = soalloc(M_NOWAIT);
230 if ((head->so_options & SO_ACCEPTFILTER) != 0)
233 so->so_type = head->so_type;
234 so->so_options = head->so_options &~ SO_ACCEPTCONN;
235 so->so_linger = head->so_linger;
236 so->so_state = head->so_state | SS_NOFDREF;
237 so->so_proto = head->so_proto;
238 so->so_timeo = head->so_timeo;
239 so->so_cred = crhold(head->so_cred);
242 mac_create_socket_from_socket(head, so);
245 knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
246 knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
247 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
248 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
252 so->so_state |= connstatus;
255 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
256 so->so_qstate |= SQ_COMP;
260 * Keep removing sockets from the head until there's room for
261 * us to insert on the tail. In pre-locking revisions, this
262 * was a simple if(), but as we could be racing with other
263 * threads and soabort() requires dropping locks, we must
264 * loop waiting for the condition to be true.
266 while (head->so_incqlen > head->so_qlimit) {
268 sp = TAILQ_FIRST(&head->so_incomp);
269 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
271 sp->so_qstate &= ~SQ_INCOMP;
277 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
278 so->so_qstate |= SQ_INCOMP;
284 wakeup_one(&head->so_timeo);
290 * Socantsendmore indicates that no more data will be sent on the
291 * socket; it would normally be applied to a socket when the user
292 * informs the system that no more data is to be sent, by the protocol
293 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
294 * will be received, and will normally be applied to the socket by a
295 * protocol when it detects that the peer will send no more data.
296 * Data queued for reading in the socket may yet be read.
299 socantsendmore_locked(so)
303 SOCKBUF_LOCK_ASSERT(&so->so_snd);
305 so->so_snd.sb_state |= SBS_CANTSENDMORE;
306 sowwakeup_locked(so);
307 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
315 SOCKBUF_LOCK(&so->so_snd);
316 socantsendmore_locked(so);
317 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
321 socantrcvmore_locked(so)
325 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
327 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
328 sorwakeup_locked(so);
329 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
337 SOCKBUF_LOCK(&so->so_rcv);
338 socantrcvmore_locked(so);
339 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
343 * Wait for data to arrive at/drain from a socket buffer.
350 SOCKBUF_LOCK_ASSERT(sb);
352 sb->sb_flags |= SB_WAIT;
353 return (msleep(&sb->sb_cc, &sb->sb_mtx,
354 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
359 * Lock a sockbuf already known to be locked;
360 * return any error returned from sleep (EINTR).
364 register struct sockbuf *sb;
368 SOCKBUF_LOCK_ASSERT(sb);
370 while (sb->sb_flags & SB_LOCK) {
371 sb->sb_flags |= SB_WANT;
372 error = msleep(&sb->sb_flags, &sb->sb_mtx,
373 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
378 sb->sb_flags |= SB_LOCK;
383 * Wakeup processes waiting on a socket buffer. Do asynchronous
384 * notification via SIGIO if the socket has the SS_ASYNC flag set.
386 * Called with the socket buffer lock held; will release the lock by the end
387 * of the function. This allows the caller to acquire the socket buffer lock
388 * while testing for the need for various sorts of wakeup and hold it through
389 * to the point where it's no longer required. We currently hold the lock
390 * through calls out to other subsystems (with the exception of kqueue), and
391 * then release it to avoid lock order issues. It's not clear that's
396 register struct socket *so;
397 register struct sockbuf *sb;
400 SOCKBUF_LOCK_ASSERT(sb);
402 selwakeuppri(&sb->sb_sel, PSOCK);
403 sb->sb_flags &= ~SB_SEL;
404 if (sb->sb_flags & SB_WAIT) {
405 sb->sb_flags &= ~SB_WAIT;
408 KNOTE_LOCKED(&sb->sb_sel.si_note, 0);
410 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
411 pgsigio(&so->so_sigio, SIGIO, 0);
412 if (sb->sb_flags & SB_UPCALL)
413 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
414 if (sb->sb_flags & SB_AIO)
416 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
420 * Socket buffer (struct sockbuf) utility routines.
422 * Each socket contains two socket buffers: one for sending data and
423 * one for receiving data. Each buffer contains a queue of mbufs,
424 * information about the number of mbufs and amount of data in the
425 * queue, and other fields allowing select() statements and notification
426 * on data availability to be implemented.
428 * Data stored in a socket buffer is maintained as a list of records.
429 * Each record is a list of mbufs chained together with the m_next
430 * field. Records are chained together with the m_nextpkt field. The upper
431 * level routine soreceive() expects the following conventions to be
432 * observed when placing information in the receive buffer:
434 * 1. If the protocol requires each message be preceded by the sender's
435 * name, then a record containing that name must be present before
436 * any associated data (mbuf's must be of type MT_SONAME).
437 * 2. If the protocol supports the exchange of ``access rights'' (really
438 * just additional data associated with the message), and there are
439 * ``rights'' to be received, then a record containing this data
440 * should be present (mbuf's must be of type MT_RIGHTS).
441 * 3. If a name or rights record exists, then it must be followed by
442 * a data record, perhaps of zero length.
444 * Before using a new socket structure it is first necessary to reserve
445 * buffer space to the socket, by calling sbreserve(). This should commit
446 * some of the available buffer space in the system buffer pool for the
447 * socket (currently, it does nothing but enforce limits). The space
448 * should be released by calling sbrelease() when the socket is destroyed.
452 soreserve(so, sndcc, rcvcc)
453 register struct socket *so;
456 struct thread *td = curthread;
458 SOCKBUF_LOCK(&so->so_snd);
459 SOCKBUF_LOCK(&so->so_rcv);
460 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
462 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
464 if (so->so_rcv.sb_lowat == 0)
465 so->so_rcv.sb_lowat = 1;
466 if (so->so_snd.sb_lowat == 0)
467 so->so_snd.sb_lowat = MCLBYTES;
468 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
469 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
470 SOCKBUF_UNLOCK(&so->so_rcv);
471 SOCKBUF_UNLOCK(&so->so_snd);
474 sbrelease_locked(&so->so_snd, so);
476 SOCKBUF_UNLOCK(&so->so_rcv);
477 SOCKBUF_UNLOCK(&so->so_snd);
482 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
485 u_long old_sb_max = sb_max;
487 error = SYSCTL_OUT(req, arg1, sizeof(u_long));
488 if (error || !req->newptr)
490 error = SYSCTL_IN(req, arg1, sizeof(u_long));
493 if (sb_max < MSIZE + MCLBYTES) {
497 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
502 * Allot mbufs to a sockbuf.
503 * Attempt to scale mbmax so that mbcnt doesn't become limiting
504 * if buffering efficiency is near the normal case.
507 sbreserve_locked(sb, cc, so, td)
515 SOCKBUF_LOCK_ASSERT(sb);
518 * td will only be NULL when we're in an interrupt
519 * (e.g. in tcp_input())
524 PROC_LOCK(td->td_proc);
525 sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE);
526 PROC_UNLOCK(td->td_proc);
528 sbsize_limit = RLIM_INFINITY;
529 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
532 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
533 if (sb->sb_lowat > sb->sb_hiwat)
534 sb->sb_lowat = sb->sb_hiwat;
539 sbreserve(sb, cc, so, td)
548 error = sbreserve_locked(sb, cc, so, td);
554 * Free mbufs held by a socket, and reserved mbuf space.
557 sbrelease_locked(sb, so)
562 SOCKBUF_LOCK_ASSERT(sb);
565 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
577 sbrelease_locked(sb, so);
581 * Routines to add and remove
582 * data from an mbuf queue.
584 * The routines sbappend() or sbappendrecord() are normally called to
585 * append new mbufs to a socket buffer, after checking that adequate
586 * space is available, comparing the function sbspace() with the amount
587 * of data to be added. sbappendrecord() differs from sbappend() in
588 * that data supplied is treated as the beginning of a new record.
589 * To place a sender's address, optional access rights, and data in a
590 * socket receive buffer, sbappendaddr() should be used. To place
591 * access rights and data in a socket receive buffer, sbappendrights()
592 * should be used. In either case, the new data begins a new record.
593 * Note that unlike sbappend() and sbappendrecord(), these routines check
594 * for the caller that there will be enough space to store the data.
595 * Each fails if there is not enough space, or if it cannot find mbufs
596 * to store additional information in.
598 * Reliable protocols may use the socket send buffer to hold data
599 * awaiting acknowledgement. Data is normally copied from a socket
600 * send buffer in a protocol with m_copy for output to a peer,
601 * and then removing the data from the socket buffer with sbdrop()
602 * or sbdroprecord() when the data is acknowledged by the peer.
607 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
609 struct mbuf *m = sb->sb_mb;
611 SOCKBUF_LOCK_ASSERT(sb);
613 while (m && m->m_nextpkt)
616 if (m != sb->sb_lastrecord) {
617 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
618 __func__, sb->sb_mb, sb->sb_lastrecord, m);
619 printf("packet chain:\n");
620 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
622 panic("%s from %s:%u", __func__, file, line);
627 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
629 struct mbuf *m = sb->sb_mb;
632 SOCKBUF_LOCK_ASSERT(sb);
634 while (m && m->m_nextpkt)
637 while (m && m->m_next)
640 if (m != sb->sb_mbtail) {
641 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
642 __func__, sb->sb_mb, sb->sb_mbtail, m);
643 printf("packet tree:\n");
644 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
646 for (n = m; n != NULL; n = n->m_next)
650 panic("%s from %s:%u", __func__, file, line);
653 #endif /* SOCKBUF_DEBUG */
655 #define SBLINKRECORD(sb, m0) do { \
656 SOCKBUF_LOCK_ASSERT(sb); \
657 if ((sb)->sb_lastrecord != NULL) \
658 (sb)->sb_lastrecord->m_nextpkt = (m0); \
660 (sb)->sb_mb = (m0); \
661 (sb)->sb_lastrecord = (m0); \
662 } while (/*CONSTCOND*/0)
665 * Append mbuf chain m to the last record in the
666 * socket buffer sb. The additional space associated
667 * the mbuf chain is recorded in sb. Empty mbufs are
668 * discarded and mbufs are compacted where possible.
671 sbappend_locked(sb, m)
675 register struct mbuf *n;
677 SOCKBUF_LOCK_ASSERT(sb);
688 if (n->m_flags & M_EOR) {
689 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
692 } while (n->m_next && (n = n->m_next));
695 * XXX Would like to simply use sb_mbtail here, but
696 * XXX I need to verify that I won't miss an EOR that
699 if ((n = sb->sb_lastrecord) != NULL) {
701 if (n->m_flags & M_EOR) {
702 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
705 } while (n->m_next && (n = n->m_next));
708 * If this is the first record in the socket buffer,
709 * it's also the last record.
711 sb->sb_lastrecord = m;
714 sbcompress(sb, m, n);
719 * Append mbuf chain m to the last record in the
720 * socket buffer sb. The additional space associated
721 * the mbuf chain is recorded in sb. Empty mbufs are
722 * discarded and mbufs are compacted where possible.
731 sbappend_locked(sb, m);
736 * This version of sbappend() should only be used when the caller
737 * absolutely knows that there will never be more than one record
738 * in the socket buffer, that is, a stream protocol (such as TCP).
741 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m)
743 SOCKBUF_LOCK_ASSERT(sb);
745 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
746 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
750 sbcompress(sb, m, sb->sb_mbtail);
752 sb->sb_lastrecord = sb->sb_mb;
757 * This version of sbappend() should only be used when the caller
758 * absolutely knows that there will never be more than one record
759 * in the socket buffer, that is, a stream protocol (such as TCP).
762 sbappendstream(struct sockbuf *sb, struct mbuf *m)
766 sbappendstream_locked(sb, m);
777 u_long len = 0, mbcnt = 0;
779 SOCKBUF_LOCK_ASSERT(sb);
781 for (m = sb->sb_mb; m; m = n) {
783 for (; m; m = m->m_next) {
786 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
787 mbcnt += m->m_ext.ext_size;
790 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
791 printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc,
792 mbcnt, sb->sb_mbcnt);
799 * As above, except the mbuf chain
800 * begins a new record.
803 sbappendrecord_locked(sb, m0)
804 register struct sockbuf *sb;
805 register struct mbuf *m0;
807 register struct mbuf *m;
809 SOCKBUF_LOCK_ASSERT(sb);
818 * Put the first mbuf on the queue.
819 * Note this permits zero length records.
823 SBLINKRECORD(sb, m0);
830 if (m && (m0->m_flags & M_EOR)) {
831 m0->m_flags &= ~M_EOR;
834 sbcompress(sb, m, m0);
838 * As above, except the mbuf chain
839 * begins a new record.
842 sbappendrecord(sb, m0)
843 register struct sockbuf *sb;
844 register struct mbuf *m0;
848 sbappendrecord_locked(sb, m0);
853 * As above except that OOB data
854 * is inserted at the beginning of the sockbuf,
855 * but after any other OOB data.
858 sbinsertoob_locked(sb, m0)
859 register struct sockbuf *sb;
860 register struct mbuf *m0;
862 register struct mbuf *m;
863 register struct mbuf **mp;
865 SOCKBUF_LOCK_ASSERT(sb);
869 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
875 continue; /* WANT next train */
880 goto again; /* inspect THIS train further */
885 * Put the first mbuf on the queue.
886 * Note this permits zero length records.
893 if (m && (m0->m_flags & M_EOR)) {
894 m0->m_flags &= ~M_EOR;
897 sbcompress(sb, m, m0);
901 * As above except that OOB data
902 * is inserted at the beginning of the sockbuf,
903 * but after any other OOB data.
907 register struct sockbuf *sb;
908 register struct mbuf *m0;
912 sbinsertoob_locked(sb, m0);
917 * Append address and data, and optionally, control (ancillary) data
918 * to the receive queue of a socket. If present,
919 * m0 must include a packet header with total length.
920 * Returns 0 if no space in sockbuf or insufficient mbufs.
923 sbappendaddr_locked(sb, asa, m0, control)
925 const struct sockaddr *asa;
926 struct mbuf *m0, *control;
928 struct mbuf *m, *n, *nlast;
929 int space = asa->sa_len;
931 SOCKBUF_LOCK_ASSERT(sb);
933 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
934 panic("sbappendaddr_locked");
936 space += m0->m_pkthdr.len;
937 space += m_length(control, &n);
939 if (space > sbspace(sb))
942 if (asa->sa_len > MLEN)
945 MGET(m, M_DONTWAIT, MT_SONAME);
948 m->m_len = asa->sa_len;
949 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
951 n->m_next = m0; /* concatenate data to control */
955 for (n = m; n->m_next != NULL; n = n->m_next)
961 sb->sb_mbtail = nlast;
969 * Append address and data, and optionally, control (ancillary) data
970 * to the receive queue of a socket. If present,
971 * m0 must include a packet header with total length.
972 * Returns 0 if no space in sockbuf or insufficient mbufs.
975 sbappendaddr(sb, asa, m0, control)
977 const struct sockaddr *asa;
978 struct mbuf *m0, *control;
983 retval = sbappendaddr_locked(sb, asa, m0, control);
989 sbappendcontrol_locked(sb, m0, control)
991 struct mbuf *control, *m0;
993 struct mbuf *m, *n, *mlast;
996 SOCKBUF_LOCK_ASSERT(sb);
999 panic("sbappendcontrol_locked");
1000 space = m_length(control, &n) + m_length(m0, NULL);
1002 if (space > sbspace(sb))
1004 n->m_next = m0; /* concatenate data to control */
1006 SBLASTRECORDCHK(sb);
1008 for (m = control; m->m_next; m = m->m_next)
1012 SBLINKRECORD(sb, control);
1014 sb->sb_mbtail = mlast;
1017 SBLASTRECORDCHK(sb);
1022 sbappendcontrol(sb, m0, control)
1024 struct mbuf *control, *m0;
1029 retval = sbappendcontrol_locked(sb, m0, control);
1035 * Compress mbuf chain m into the socket
1036 * buffer sb following mbuf n. If n
1037 * is null, the buffer is presumed empty.
1040 sbcompress(sb, m, n)
1041 register struct sockbuf *sb;
1042 register struct mbuf *m, *n;
1044 register int eor = 0;
1045 register struct mbuf *o;
1047 SOCKBUF_LOCK_ASSERT(sb);
1050 eor |= m->m_flags & M_EOR;
1051 if (m->m_len == 0 &&
1053 (((o = m->m_next) || (o = n)) &&
1054 o->m_type == m->m_type))) {
1055 if (sb->sb_lastrecord == m)
1056 sb->sb_lastrecord = m->m_next;
1060 if (n && (n->m_flags & M_EOR) == 0 &&
1062 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1063 m->m_len <= M_TRAILINGSPACE(n) &&
1064 n->m_type == m->m_type) {
1065 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
1066 (unsigned)m->m_len);
1067 n->m_len += m->m_len;
1068 sb->sb_cc += m->m_len;
1069 if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
1070 m->m_type != MT_OOBDATA)
1071 /* XXX: Probably don't need.*/
1072 sb->sb_ctl += m->m_len;
1083 m->m_flags &= ~M_EOR;
1091 printf("semi-panic: sbcompress\n");
1097 * Free all mbufs in a sockbuf.
1098 * Check that all resources are reclaimed.
1102 register struct sockbuf *sb;
1105 SOCKBUF_LOCK_ASSERT(sb);
1107 if (sb->sb_flags & SB_LOCK)
1108 panic("sbflush_locked: locked");
1109 while (sb->sb_mbcnt) {
1111 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
1112 * we would loop forever. Panic instead.
1114 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1116 sbdrop_locked(sb, (int)sb->sb_cc);
1118 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
1119 panic("sbflush_locked: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
1124 register struct sockbuf *sb;
1133 * Drop data from (the front of) a sockbuf.
1136 sbdrop_locked(sb, len)
1137 register struct sockbuf *sb;
1140 register struct mbuf *m;
1143 SOCKBUF_LOCK_ASSERT(sb);
1145 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1151 next = m->m_nextpkt;
1154 if (m->m_len > len) {
1158 if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
1159 m->m_type != MT_OOBDATA)
1167 while (m && m->m_len == 0) {
1173 m->m_nextpkt = next;
1177 * First part is an inline SB_EMPTY_FIXUP(). Second part
1178 * makes sure sb_lastrecord is up-to-date if we dropped
1179 * part of the last record.
1183 sb->sb_mbtail = NULL;
1184 sb->sb_lastrecord = NULL;
1185 } else if (m->m_nextpkt == NULL) {
1186 sb->sb_lastrecord = m;
1191 * Drop data from (the front of) a sockbuf.
1195 register struct sockbuf *sb;
1200 sbdrop_locked(sb, len);
1205 * Drop a record off the front of a sockbuf
1206 * and move the next record to the front.
1209 sbdroprecord_locked(sb)
1210 register struct sockbuf *sb;
1212 register struct mbuf *m;
1214 SOCKBUF_LOCK_ASSERT(sb);
1218 sb->sb_mb = m->m_nextpkt;
1228 * Drop a record off the front of a sockbuf
1229 * and move the next record to the front.
1233 register struct sockbuf *sb;
1237 sbdroprecord_locked(sb);
1242 * Create a "control" mbuf containing the specified data
1243 * with the specified type for presentation on a socket buffer.
1246 sbcreatecontrol(p, size, type, level)
1251 register struct cmsghdr *cp;
1254 if (CMSG_SPACE((u_int)size) > MCLBYTES)
1255 return ((struct mbuf *) NULL);
1256 if (CMSG_SPACE((u_int)size) > MLEN)
1257 m = m_getcl(M_DONTWAIT, MT_CONTROL, 0);
1259 m = m_get(M_DONTWAIT, MT_CONTROL);
1261 return ((struct mbuf *) NULL);
1262 cp = mtod(m, struct cmsghdr *);
1264 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
1265 ("sbcreatecontrol: short mbuf"));
1267 (void)memcpy(CMSG_DATA(cp), p, size);
1268 m->m_len = CMSG_SPACE(size);
1269 cp->cmsg_len = CMSG_LEN(size);
1270 cp->cmsg_level = level;
1271 cp->cmsg_type = type;
1276 * Some routines that return EOPNOTSUPP for entry points that are not
1277 * supported by a protocol. Fill in as needed.
1280 pru_abort_notsupp(struct socket *so)
1286 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
1292 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
1298 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
1304 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
1310 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
1316 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
1317 struct ifnet *ifp, struct thread *td)
1323 pru_detach_notsupp(struct socket *so)
1329 pru_disconnect_notsupp(struct socket *so)
1335 pru_listen_notsupp(struct socket *so, struct thread *td)
1341 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
1347 pru_rcvd_notsupp(struct socket *so, int flags)
1353 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
1359 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
1360 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1366 * This isn't really a ``null'' operation, but it's the default one
1367 * and doesn't do anything destructive.
1370 pru_sense_null(struct socket *so, struct stat *sb)
1372 sb->st_blksize = so->so_snd.sb_hiwat;
1377 pru_shutdown_notsupp(struct socket *so)
1383 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
1389 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
1390 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1396 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
1397 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
1404 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
1411 * For protocol types that don't keep cached copies of labels in their
1412 * pcbs, provide a null sosetlabel that does a NOOP.
1415 pru_sosetlabel_null(struct socket *so)
1421 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1424 sodupsockaddr(const struct sockaddr *sa, int mflags)
1426 struct sockaddr *sa2;
1428 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
1430 bcopy(sa, sa2, sa->sa_len);
1435 * Create an external-format (``xsocket'') structure using the information
1436 * in the kernel-format socket structure pointed to by so. This is done
1437 * to reduce the spew of irrelevant information over this interface,
1438 * to isolate user code from changes in the kernel structure, and
1439 * potentially to provide information-hiding if we decide that
1440 * some of this information should be hidden from users.
1443 sotoxsocket(struct socket *so, struct xsocket *xso)
1445 xso->xso_len = sizeof *xso;
1447 xso->so_type = so->so_type;
1448 xso->so_options = so->so_options;
1449 xso->so_linger = so->so_linger;
1450 xso->so_state = so->so_state;
1451 xso->so_pcb = so->so_pcb;
1452 xso->xso_protocol = so->so_proto->pr_protocol;
1453 xso->xso_family = so->so_proto->pr_domain->dom_family;
1454 xso->so_qlen = so->so_qlen;
1455 xso->so_incqlen = so->so_incqlen;
1456 xso->so_qlimit = so->so_qlimit;
1457 xso->so_timeo = so->so_timeo;
1458 xso->so_error = so->so_error;
1459 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
1460 xso->so_oobmark = so->so_oobmark;
1461 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
1462 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
1463 xso->so_uid = so->so_cred->cr_uid;
1467 * This does the same for sockbufs. Note that the xsockbuf structure,
1468 * since it is always embedded in a socket, does not include a self
1469 * pointer nor a length. We make this entry point public in case
1470 * some other mechanism needs it.
1473 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1475 xsb->sb_cc = sb->sb_cc;
1476 xsb->sb_hiwat = sb->sb_hiwat;
1477 xsb->sb_mbcnt = sb->sb_mbcnt;
1478 xsb->sb_mbmax = sb->sb_mbmax;
1479 xsb->sb_lowat = sb->sb_lowat;
1480 xsb->sb_flags = sb->sb_flags;
1481 xsb->sb_timeo = sb->sb_timeo;
1485 * Here is the definition of some of the basic objects in the kern.ipc
1486 * branch of the MIB.
1488 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
1490 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1492 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1493 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1494 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1495 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RDTUN,
1496 &maxsockets, 0, "Maximum number of sockets avaliable");
1497 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1498 &sb_efficiency, 0, "");
1501 * Initialise maxsockets
1503 static void init_maxsockets(void *ignored)
1505 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
1506 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
1508 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);