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;
122 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
123 so->so_state |= SS_ISCONNECTED;
125 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
126 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);
140 head->so_accf->so_accept_filter->accf_callback;
141 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
142 so->so_rcv.sb_flags |= SB_UPCALL;
143 so->so_options &= ~SO_ACCEPTFILTER;
145 so->so_upcall(so, so->so_upcallarg, M_DONTWAIT);
151 wakeup(&so->so_timeo);
157 soisdisconnecting(so)
158 register struct socket *so;
162 * XXXRW: This code assumes that SOCK_LOCK(so) and
163 * SOCKBUF_LOCK(&so->so_rcv) are the same.
165 SOCKBUF_LOCK(&so->so_rcv);
166 so->so_state &= ~SS_ISCONNECTING;
167 so->so_state |= SS_ISDISCONNECTING;
168 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
169 sorwakeup_locked(so);
170 SOCKBUF_LOCK(&so->so_snd);
171 so->so_snd.sb_state |= SBS_CANTSENDMORE;
172 sowwakeup_locked(so);
173 wakeup(&so->so_timeo);
178 register struct socket *so;
182 * XXXRW: This code assumes that SOCK_LOCK(so) and
183 * SOCKBUF_LOCK(&so->so_rcv) are the same.
185 SOCKBUF_LOCK(&so->so_rcv);
186 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
187 so->so_state |= SS_ISDISCONNECTED;
188 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
189 sorwakeup_locked(so);
190 SOCKBUF_LOCK(&so->so_snd);
191 so->so_snd.sb_state |= SBS_CANTSENDMORE;
192 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
193 sowwakeup_locked(so);
194 wakeup(&so->so_timeo);
198 * When an attempt at a new connection is noted on a socket
199 * which accepts connections, sonewconn is called. If the
200 * connection is possible (subject to space constraints, etc.)
201 * then we allocate a new structure, propoerly linked into the
202 * data structure of the original socket, and return this.
203 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
205 * note: the ref count on the socket is 0 on return
208 sonewconn(head, connstatus)
209 register struct socket *head;
212 register struct socket *so;
216 over = (head->so_qlen > 3 * head->so_qlimit / 2);
220 so = soalloc(M_NOWAIT);
223 if ((head->so_options & SO_ACCEPTFILTER) != 0)
226 so->so_type = head->so_type;
227 so->so_options = head->so_options &~ SO_ACCEPTCONN;
228 so->so_linger = head->so_linger;
229 so->so_state = head->so_state | SS_NOFDREF;
230 so->so_proto = head->so_proto;
231 so->so_timeo = head->so_timeo;
232 so->so_cred = crhold(head->so_cred);
235 mac_create_socket_from_socket(head, so);
238 knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv),
240 knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd),
242 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
243 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
247 so->so_state |= connstatus;
250 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
251 so->so_qstate |= SQ_COMP;
255 * Keep removing sockets from the head until there's room for
256 * us to insert on the tail. In pre-locking revisions, this
257 * was a simple if(), but as we could be racing with other
258 * threads and soabort() requires dropping locks, we must
259 * loop waiting for the condition to be true.
261 while (head->so_incqlen > head->so_qlimit) {
263 sp = TAILQ_FIRST(&head->so_incomp);
264 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
266 sp->so_qstate &= ~SQ_INCOMP;
272 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
273 so->so_qstate |= SQ_INCOMP;
279 wakeup_one(&head->so_timeo);
285 * Socantsendmore indicates that no more data will be sent on the
286 * socket; it would normally be applied to a socket when the user
287 * informs the system that no more data is to be sent, by the protocol
288 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
289 * will be received, and will normally be applied to the socket by a
290 * protocol when it detects that the peer will send no more data.
291 * Data queued for reading in the socket may yet be read.
294 socantsendmore_locked(so)
298 SOCKBUF_LOCK_ASSERT(&so->so_snd);
300 so->so_snd.sb_state |= SBS_CANTSENDMORE;
301 sowwakeup_locked(so);
302 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
310 SOCKBUF_LOCK(&so->so_snd);
311 socantsendmore_locked(so);
312 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
316 socantrcvmore_locked(so)
320 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
322 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
323 sorwakeup_locked(so);
324 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
332 SOCKBUF_LOCK(&so->so_rcv);
333 socantrcvmore_locked(so);
334 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
338 * Wait for data to arrive at/drain from a socket buffer.
345 SOCKBUF_LOCK_ASSERT(sb);
347 sb->sb_flags |= SB_WAIT;
348 return (msleep(&sb->sb_cc, &sb->sb_mtx,
349 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
354 * Lock a sockbuf already known to be locked;
355 * return any error returned from sleep (EINTR).
359 register struct sockbuf *sb;
363 SOCKBUF_LOCK_ASSERT(sb);
365 while (sb->sb_flags & SB_LOCK) {
366 sb->sb_flags |= SB_WANT;
367 error = msleep(&sb->sb_flags, &sb->sb_mtx,
368 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
373 sb->sb_flags |= SB_LOCK;
378 * Wakeup processes waiting on a socket buffer. Do asynchronous
379 * notification via SIGIO if the socket has the SS_ASYNC flag set.
381 * Called with the socket buffer lock held; will release the lock by the end
382 * of the function. This allows the caller to acquire the socket buffer lock
383 * while testing for the need for various sorts of wakeup and hold it through
384 * to the point where it's no longer required. We currently hold the lock
385 * through calls out to other subsystems (with the exception of kqueue), and
386 * then release it to avoid lock order issues. It's not clear that's
391 register struct socket *so;
392 register struct sockbuf *sb;
395 SOCKBUF_LOCK_ASSERT(sb);
397 selwakeuppri(&sb->sb_sel, PSOCK);
398 sb->sb_flags &= ~SB_SEL;
399 if (sb->sb_flags & SB_WAIT) {
400 sb->sb_flags &= ~SB_WAIT;
403 KNOTE_LOCKED(&sb->sb_sel.si_note, 0);
405 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
406 pgsigio(&so->so_sigio, SIGIO, 0);
407 if (sb->sb_flags & SB_UPCALL)
408 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
409 if (sb->sb_flags & SB_AIO)
411 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
415 * Socket buffer (struct sockbuf) utility routines.
417 * Each socket contains two socket buffers: one for sending data and
418 * one for receiving data. Each buffer contains a queue of mbufs,
419 * information about the number of mbufs and amount of data in the
420 * queue, and other fields allowing select() statements and notification
421 * on data availability to be implemented.
423 * Data stored in a socket buffer is maintained as a list of records.
424 * Each record is a list of mbufs chained together with the m_next
425 * field. Records are chained together with the m_nextpkt field. The upper
426 * level routine soreceive() expects the following conventions to be
427 * observed when placing information in the receive buffer:
429 * 1. If the protocol requires each message be preceded by the sender's
430 * name, then a record containing that name must be present before
431 * any associated data (mbuf's must be of type MT_SONAME).
432 * 2. If the protocol supports the exchange of ``access rights'' (really
433 * just additional data associated with the message), and there are
434 * ``rights'' to be received, then a record containing this data
435 * should be present (mbuf's must be of type MT_RIGHTS).
436 * 3. If a name or rights record exists, then it must be followed by
437 * a data record, perhaps of zero length.
439 * Before using a new socket structure it is first necessary to reserve
440 * buffer space to the socket, by calling sbreserve(). This should commit
441 * some of the available buffer space in the system buffer pool for the
442 * socket (currently, it does nothing but enforce limits). The space
443 * should be released by calling sbrelease() when the socket is destroyed.
447 soreserve(so, sndcc, rcvcc)
448 register struct socket *so;
451 struct thread *td = curthread;
453 SOCKBUF_LOCK(&so->so_snd);
454 SOCKBUF_LOCK(&so->so_rcv);
455 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
457 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
459 if (so->so_rcv.sb_lowat == 0)
460 so->so_rcv.sb_lowat = 1;
461 if (so->so_snd.sb_lowat == 0)
462 so->so_snd.sb_lowat = MCLBYTES;
463 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
464 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
465 SOCKBUF_UNLOCK(&so->so_rcv);
466 SOCKBUF_UNLOCK(&so->so_snd);
469 sbrelease_locked(&so->so_snd, so);
471 SOCKBUF_UNLOCK(&so->so_rcv);
472 SOCKBUF_UNLOCK(&so->so_snd);
477 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
480 u_long old_sb_max = sb_max;
482 error = SYSCTL_OUT(req, arg1, sizeof(u_long));
483 if (error || !req->newptr)
485 error = SYSCTL_IN(req, arg1, sizeof(u_long));
488 if (sb_max < MSIZE + MCLBYTES) {
492 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
497 * Allot mbufs to a sockbuf.
498 * Attempt to scale mbmax so that mbcnt doesn't become limiting
499 * if buffering efficiency is near the normal case.
502 sbreserve_locked(sb, cc, so, td)
510 SOCKBUF_LOCK_ASSERT(sb);
513 * td will only be NULL when we're in an interrupt
514 * (e.g. in tcp_input())
519 PROC_LOCK(td->td_proc);
520 sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE);
521 PROC_UNLOCK(td->td_proc);
523 sbsize_limit = RLIM_INFINITY;
524 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
527 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
528 if (sb->sb_lowat > sb->sb_hiwat)
529 sb->sb_lowat = sb->sb_hiwat;
534 sbreserve(sb, cc, so, td)
543 error = sbreserve_locked(sb, cc, so, td);
549 * Free mbufs held by a socket, and reserved mbuf space.
552 sbrelease_locked(sb, so)
557 SOCKBUF_LOCK_ASSERT(sb);
560 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
572 sbrelease_locked(sb, so);
576 * Routines to add and remove
577 * data from an mbuf queue.
579 * The routines sbappend() or sbappendrecord() are normally called to
580 * append new mbufs to a socket buffer, after checking that adequate
581 * space is available, comparing the function sbspace() with the amount
582 * of data to be added. sbappendrecord() differs from sbappend() in
583 * that data supplied is treated as the beginning of a new record.
584 * To place a sender's address, optional access rights, and data in a
585 * socket receive buffer, sbappendaddr() should be used. To place
586 * access rights and data in a socket receive buffer, sbappendrights()
587 * should be used. In either case, the new data begins a new record.
588 * Note that unlike sbappend() and sbappendrecord(), these routines check
589 * for the caller that there will be enough space to store the data.
590 * Each fails if there is not enough space, or if it cannot find mbufs
591 * to store additional information in.
593 * Reliable protocols may use the socket send buffer to hold data
594 * awaiting acknowledgement. Data is normally copied from a socket
595 * send buffer in a protocol with m_copy for output to a peer,
596 * and then removing the data from the socket buffer with sbdrop()
597 * or sbdroprecord() when the data is acknowledged by the peer.
602 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
604 struct mbuf *m = sb->sb_mb;
606 SOCKBUF_LOCK_ASSERT(sb);
608 while (m && m->m_nextpkt)
611 if (m != sb->sb_lastrecord) {
612 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
613 __func__, sb->sb_mb, sb->sb_lastrecord, m);
614 printf("packet chain:\n");
615 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
617 panic("%s from %s:%u", __func__, file, line);
622 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
624 struct mbuf *m = sb->sb_mb;
627 SOCKBUF_LOCK_ASSERT(sb);
629 while (m && m->m_nextpkt)
632 while (m && m->m_next)
635 if (m != sb->sb_mbtail) {
636 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
637 __func__, sb->sb_mb, sb->sb_mbtail, m);
638 printf("packet tree:\n");
639 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
641 for (n = m; n != NULL; n = n->m_next)
645 panic("%s from %s:%u", __func__, file, line);
648 #endif /* SOCKBUF_DEBUG */
650 #define SBLINKRECORD(sb, m0) do { \
651 SOCKBUF_LOCK_ASSERT(sb); \
652 if ((sb)->sb_lastrecord != NULL) \
653 (sb)->sb_lastrecord->m_nextpkt = (m0); \
655 (sb)->sb_mb = (m0); \
656 (sb)->sb_lastrecord = (m0); \
657 } while (/*CONSTCOND*/0)
660 * Append mbuf chain m to the last record in the
661 * socket buffer sb. The additional space associated
662 * the mbuf chain is recorded in sb. Empty mbufs are
663 * discarded and mbufs are compacted where possible.
666 sbappend_locked(sb, m)
670 register struct mbuf *n;
672 SOCKBUF_LOCK_ASSERT(sb);
683 if (n->m_flags & M_EOR) {
684 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
687 } while (n->m_next && (n = n->m_next));
690 * XXX Would like to simply use sb_mbtail here, but
691 * XXX I need to verify that I won't miss an EOR that
694 if ((n = sb->sb_lastrecord) != NULL) {
696 if (n->m_flags & M_EOR) {
697 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
700 } while (n->m_next && (n = n->m_next));
703 * If this is the first record in the socket buffer,
704 * it's also the last record.
706 sb->sb_lastrecord = m;
709 sbcompress(sb, m, n);
714 * Append mbuf chain m to the last record in the
715 * socket buffer sb. The additional space associated
716 * the mbuf chain is recorded in sb. Empty mbufs are
717 * discarded and mbufs are compacted where possible.
726 sbappend_locked(sb, m);
731 * This version of sbappend() should only be used when the caller
732 * absolutely knows that there will never be more than one record
733 * in the socket buffer, that is, a stream protocol (such as TCP).
736 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m)
738 SOCKBUF_LOCK_ASSERT(sb);
740 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
741 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
745 sbcompress(sb, m, sb->sb_mbtail);
747 sb->sb_lastrecord = sb->sb_mb;
752 * This version of sbappend() should only be used when the caller
753 * absolutely knows that there will never be more than one record
754 * in the socket buffer, that is, a stream protocol (such as TCP).
757 sbappendstream(struct sockbuf *sb, struct mbuf *m)
761 sbappendstream_locked(sb, m);
772 u_long len = 0, mbcnt = 0;
774 SOCKBUF_LOCK_ASSERT(sb);
776 for (m = sb->sb_mb; m; m = n) {
778 for (; m; m = m->m_next) {
781 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
782 mbcnt += m->m_ext.ext_size;
785 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
786 printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc,
787 mbcnt, sb->sb_mbcnt);
794 * As above, except the mbuf chain
795 * begins a new record.
798 sbappendrecord_locked(sb, m0)
799 register struct sockbuf *sb;
800 register struct mbuf *m0;
802 register struct mbuf *m;
804 SOCKBUF_LOCK_ASSERT(sb);
813 * Put the first mbuf on the queue.
814 * Note this permits zero length records.
818 SBLINKRECORD(sb, m0);
825 if (m && (m0->m_flags & M_EOR)) {
826 m0->m_flags &= ~M_EOR;
829 sbcompress(sb, m, m0);
833 * As above, except the mbuf chain
834 * begins a new record.
837 sbappendrecord(sb, m0)
838 register struct sockbuf *sb;
839 register struct mbuf *m0;
843 sbappendrecord_locked(sb, m0);
848 * As above except that OOB data
849 * is inserted at the beginning of the sockbuf,
850 * but after any other OOB data.
853 sbinsertoob_locked(sb, m0)
854 register struct sockbuf *sb;
855 register struct mbuf *m0;
857 register struct mbuf *m;
858 register struct mbuf **mp;
860 SOCKBUF_LOCK_ASSERT(sb);
864 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
870 continue; /* WANT next train */
875 goto again; /* inspect THIS train further */
880 * Put the first mbuf on the queue.
881 * Note this permits zero length records.
888 if (m && (m0->m_flags & M_EOR)) {
889 m0->m_flags &= ~M_EOR;
892 sbcompress(sb, m, m0);
896 * As above except that OOB data
897 * is inserted at the beginning of the sockbuf,
898 * but after any other OOB data.
902 register struct sockbuf *sb;
903 register struct mbuf *m0;
907 sbinsertoob_locked(sb, m0);
912 * Append address and data, and optionally, control (ancillary) data
913 * to the receive queue of a socket. If present,
914 * m0 must include a packet header with total length.
915 * Returns 0 if no space in sockbuf or insufficient mbufs.
918 sbappendaddr_locked(sb, asa, m0, control)
920 const struct sockaddr *asa;
921 struct mbuf *m0, *control;
923 struct mbuf *m, *n, *nlast;
924 int space = asa->sa_len;
926 SOCKBUF_LOCK_ASSERT(sb);
928 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
929 panic("sbappendaddr_locked");
931 space += m0->m_pkthdr.len;
932 space += m_length(control, &n);
934 if (space > sbspace(sb))
937 if (asa->sa_len > MLEN)
940 MGET(m, M_DONTWAIT, MT_SONAME);
943 m->m_len = asa->sa_len;
944 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
946 n->m_next = m0; /* concatenate data to control */
950 for (n = m; n->m_next != NULL; n = n->m_next)
956 sb->sb_mbtail = nlast;
964 * Append address and data, and optionally, control (ancillary) data
965 * to the receive queue of a socket. If present,
966 * m0 must include a packet header with total length.
967 * Returns 0 if no space in sockbuf or insufficient mbufs.
970 sbappendaddr(sb, asa, m0, control)
972 const struct sockaddr *asa;
973 struct mbuf *m0, *control;
978 retval = sbappendaddr_locked(sb, asa, m0, control);
984 sbappendcontrol_locked(sb, m0, control)
986 struct mbuf *control, *m0;
988 struct mbuf *m, *n, *mlast;
991 SOCKBUF_LOCK_ASSERT(sb);
994 panic("sbappendcontrol_locked");
995 space = m_length(control, &n) + m_length(m0, NULL);
997 if (space > sbspace(sb))
999 n->m_next = m0; /* concatenate data to control */
1001 SBLASTRECORDCHK(sb);
1003 for (m = control; m->m_next; m = m->m_next)
1007 SBLINKRECORD(sb, control);
1009 sb->sb_mbtail = mlast;
1012 SBLASTRECORDCHK(sb);
1017 sbappendcontrol(sb, m0, control)
1019 struct mbuf *control, *m0;
1024 retval = sbappendcontrol_locked(sb, m0, control);
1030 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
1031 * (n). If (n) is NULL, the buffer is presumed empty.
1033 * When the data is compressed, mbufs in the chain may be handled in one of
1036 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
1037 * record boundary, and no change in data type).
1039 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
1040 * an mbuf already in the socket buffer. This can occur if an
1041 * appropriate mbuf exists, there is room, and no merging of data types
1044 * (3) The mbuf may be appended to the end of the existing mbuf chain.
1046 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
1050 sbcompress(sb, m, n)
1051 register struct sockbuf *sb;
1052 register struct mbuf *m, *n;
1054 register int eor = 0;
1055 register struct mbuf *o;
1057 SOCKBUF_LOCK_ASSERT(sb);
1060 eor |= m->m_flags & M_EOR;
1061 if (m->m_len == 0 &&
1063 (((o = m->m_next) || (o = n)) &&
1064 o->m_type == m->m_type))) {
1065 if (sb->sb_lastrecord == m)
1066 sb->sb_lastrecord = m->m_next;
1070 if (n && (n->m_flags & M_EOR) == 0 &&
1072 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1073 m->m_len <= M_TRAILINGSPACE(n) &&
1074 n->m_type == m->m_type) {
1075 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
1076 (unsigned)m->m_len);
1077 n->m_len += m->m_len;
1078 sb->sb_cc += m->m_len;
1079 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1080 /* XXX: Probably don't need.*/
1081 sb->sb_ctl += m->m_len;
1092 m->m_flags &= ~M_EOR;
1097 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
1104 * Free all mbufs in a sockbuf.
1105 * Check that all resources are reclaimed.
1109 register struct sockbuf *sb;
1112 SOCKBUF_LOCK_ASSERT(sb);
1114 if (sb->sb_flags & SB_LOCK)
1115 panic("sbflush_locked: locked");
1116 while (sb->sb_mbcnt) {
1118 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
1119 * we would loop forever. Panic instead.
1121 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1123 sbdrop_locked(sb, (int)sb->sb_cc);
1125 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
1126 panic("sbflush_locked: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
1131 register struct sockbuf *sb;
1140 * Drop data from (the front of) a sockbuf.
1143 sbdrop_locked(sb, len)
1144 register struct sockbuf *sb;
1147 register struct mbuf *m;
1150 SOCKBUF_LOCK_ASSERT(sb);
1152 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1158 next = m->m_nextpkt;
1161 if (m->m_len > len) {
1165 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1173 while (m && m->m_len == 0) {
1179 m->m_nextpkt = next;
1183 * First part is an inline SB_EMPTY_FIXUP(). Second part
1184 * makes sure sb_lastrecord is up-to-date if we dropped
1185 * part of the last record.
1189 sb->sb_mbtail = NULL;
1190 sb->sb_lastrecord = NULL;
1191 } else if (m->m_nextpkt == NULL) {
1192 sb->sb_lastrecord = m;
1197 * Drop data from (the front of) a sockbuf.
1201 register struct sockbuf *sb;
1206 sbdrop_locked(sb, len);
1211 * Drop a record off the front of a sockbuf
1212 * and move the next record to the front.
1215 sbdroprecord_locked(sb)
1216 register struct sockbuf *sb;
1218 register struct mbuf *m;
1220 SOCKBUF_LOCK_ASSERT(sb);
1224 sb->sb_mb = m->m_nextpkt;
1234 * Drop a record off the front of a sockbuf
1235 * and move the next record to the front.
1239 register struct sockbuf *sb;
1243 sbdroprecord_locked(sb);
1248 * Create a "control" mbuf containing the specified data
1249 * with the specified type for presentation on a socket buffer.
1252 sbcreatecontrol(p, size, type, level)
1257 register struct cmsghdr *cp;
1260 if (CMSG_SPACE((u_int)size) > MCLBYTES)
1261 return ((struct mbuf *) NULL);
1262 if (CMSG_SPACE((u_int)size) > MLEN)
1263 m = m_getcl(M_DONTWAIT, MT_CONTROL, 0);
1265 m = m_get(M_DONTWAIT, MT_CONTROL);
1267 return ((struct mbuf *) NULL);
1268 cp = mtod(m, struct cmsghdr *);
1270 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
1271 ("sbcreatecontrol: short mbuf"));
1273 (void)memcpy(CMSG_DATA(cp), p, size);
1274 m->m_len = CMSG_SPACE(size);
1275 cp->cmsg_len = CMSG_LEN(size);
1276 cp->cmsg_level = level;
1277 cp->cmsg_type = type;
1282 * Some routines that return EOPNOTSUPP for entry points that are not
1283 * supported by a protocol. Fill in as needed.
1286 pru_abort_notsupp(struct socket *so)
1292 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
1298 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
1304 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
1310 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
1316 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
1322 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
1323 struct ifnet *ifp, struct thread *td)
1329 pru_detach_notsupp(struct socket *so)
1335 pru_disconnect_notsupp(struct socket *so)
1341 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
1347 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
1353 pru_rcvd_notsupp(struct socket *so, int flags)
1359 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
1365 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
1366 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1372 * This isn't really a ``null'' operation, but it's the default one
1373 * and doesn't do anything destructive.
1376 pru_sense_null(struct socket *so, struct stat *sb)
1378 sb->st_blksize = so->so_snd.sb_hiwat;
1383 pru_shutdown_notsupp(struct socket *so)
1389 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
1395 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
1396 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1402 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
1403 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
1410 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
1417 * For protocol types that don't keep cached copies of labels in their
1418 * pcbs, provide a null sosetlabel that does a NOOP.
1421 pru_sosetlabel_null(struct socket *so)
1427 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1430 sodupsockaddr(const struct sockaddr *sa, int mflags)
1432 struct sockaddr *sa2;
1434 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
1436 bcopy(sa, sa2, sa->sa_len);
1441 * Create an external-format (``xsocket'') structure using the information
1442 * in the kernel-format socket structure pointed to by so. This is done
1443 * to reduce the spew of irrelevant information over this interface,
1444 * to isolate user code from changes in the kernel structure, and
1445 * potentially to provide information-hiding if we decide that
1446 * some of this information should be hidden from users.
1449 sotoxsocket(struct socket *so, struct xsocket *xso)
1451 xso->xso_len = sizeof *xso;
1453 xso->so_type = so->so_type;
1454 xso->so_options = so->so_options;
1455 xso->so_linger = so->so_linger;
1456 xso->so_state = so->so_state;
1457 xso->so_pcb = so->so_pcb;
1458 xso->xso_protocol = so->so_proto->pr_protocol;
1459 xso->xso_family = so->so_proto->pr_domain->dom_family;
1460 xso->so_qlen = so->so_qlen;
1461 xso->so_incqlen = so->so_incqlen;
1462 xso->so_qlimit = so->so_qlimit;
1463 xso->so_timeo = so->so_timeo;
1464 xso->so_error = so->so_error;
1465 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
1466 xso->so_oobmark = so->so_oobmark;
1467 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
1468 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
1469 xso->so_uid = so->so_cred->cr_uid;
1473 * This does the same for sockbufs. Note that the xsockbuf structure,
1474 * since it is always embedded in a socket, does not include a self
1475 * pointer nor a length. We make this entry point public in case
1476 * some other mechanism needs it.
1479 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1481 xsb->sb_cc = sb->sb_cc;
1482 xsb->sb_hiwat = sb->sb_hiwat;
1483 xsb->sb_mbcnt = sb->sb_mbcnt;
1484 xsb->sb_mbmax = sb->sb_mbmax;
1485 xsb->sb_lowat = sb->sb_lowat;
1486 xsb->sb_flags = sb->sb_flags;
1487 xsb->sb_timeo = sb->sb_timeo;
1491 * Here is the definition of some of the basic objects in the kern.ipc
1492 * branch of the MIB.
1494 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
1496 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1498 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1499 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1500 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1501 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RDTUN,
1502 &maxsockets, 0, "Maximum number of sockets avaliable");
1503 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1504 &sb_efficiency, 0, "");
1507 * Initialise maxsockets
1509 static void init_maxsockets(void *ignored)
1511 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
1512 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
1514 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);