2 * Copyright (c) 2005 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 1982, 1986, 1988, 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
35 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.17 2002/08/31 19:04:55 dwmalone Exp $
36 * $DragonFly: src/sys/kern/uipc_socket2.c,v 1.33 2008/09/02 16:17:52 dillon Exp $
39 #include "opt_param.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/domain.h>
43 #include <sys/file.h> /* for maxfiles */
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/resourcevar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/signalvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/aio.h> /* for aio_swake proto */
56 #include <sys/event.h>
58 #include <sys/thread2.h>
59 #include <sys/msgport2.h>
64 * Primitive routines for operating on sockets and socket buffers
67 u_long sb_max = SB_MAX;
69 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
71 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
73 /************************************************************************
74 * signalsockbuf procedures *
75 ************************************************************************/
78 * Wait for data to arrive at/drain from a socket buffer.
81 ssb_wait(struct signalsockbuf *ssb)
84 ssb->ssb_flags |= SSB_WAIT;
85 return (tsleep((caddr_t)&ssb->ssb_cc,
86 ((ssb->ssb_flags & SSB_NOINTR) ? 0 : PCATCH),
92 * Lock a sockbuf already known to be locked;
93 * return any error returned from sleep (EINTR).
96 _ssb_lock(struct signalsockbuf *ssb)
100 while (ssb->ssb_flags & SSB_LOCK) {
101 ssb->ssb_flags |= SSB_WANT;
102 error = tsleep((caddr_t)&ssb->ssb_flags,
103 ((ssb->ssb_flags & SSB_NOINTR) ? 0 : PCATCH),
108 ssb->ssb_flags |= SSB_LOCK;
113 * This does the same for sockbufs. Note that the xsockbuf structure,
114 * since it is always embedded in a socket, does not include a self
115 * pointer nor a length. We make this entry point public in case
116 * some other mechanism needs it.
119 ssbtoxsockbuf(struct signalsockbuf *ssb, struct xsockbuf *xsb)
121 xsb->sb_cc = ssb->ssb_cc;
122 xsb->sb_hiwat = ssb->ssb_hiwat;
123 xsb->sb_mbcnt = ssb->ssb_mbcnt;
124 xsb->sb_mbmax = ssb->ssb_mbmax;
125 xsb->sb_lowat = ssb->ssb_lowat;
126 xsb->sb_flags = ssb->ssb_flags;
127 xsb->sb_timeo = ssb->ssb_timeo;
131 /************************************************************************
132 * Procedures which manipulate socket state flags, wakeups, etc. *
133 ************************************************************************
135 * Normal sequence from the active (originating) side is that
136 * soisconnecting() is called during processing of connect() call, resulting
137 * in an eventual call to soisconnected() if/when the connection is
138 * established. When the connection is torn down soisdisconnecting() is
139 * called during processing of disconnect() call, and soisdisconnected() is
140 * called when the connection to the peer is totally severed.
142 * The semantics of these routines are such that connectionless protocols
143 * can call soisconnected() and soisdisconnected() only, bypassing the
144 * in-progress calls when setting up a ``connection'' takes no time.
146 * From the passive side, a socket is created with two queues of sockets:
147 * so_incomp for connections in progress and so_comp for connections
148 * already made and awaiting user acceptance. As a protocol is preparing
149 * incoming connections, it creates a socket structure queued on so_incomp
150 * by calling sonewconn(). When the connection is established,
151 * soisconnected() is called, and transfers the socket structure to so_comp,
152 * making it available to accept().
154 * If a socket is closed with sockets on either so_incomp or so_comp, these
155 * sockets are dropped.
157 * If higher level protocols are implemented in the kernel, the wakeups
158 * done here will sometimes cause software-interrupt process scheduling.
162 soisconnecting(struct socket *so)
164 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
165 so->so_state |= SS_ISCONNECTING;
169 soisconnected(struct socket *so)
171 struct socket *head = so->so_head;
173 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
174 so->so_state |= SS_ISCONNECTED;
175 if (head && (so->so_state & SS_INCOMP)) {
176 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
177 so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
178 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
179 so->so_rcv.ssb_flags |= SSB_UPCALL;
180 so->so_options &= ~SO_ACCEPTFILTER;
181 so->so_upcall(so, so->so_upcallarg, 0);
184 TAILQ_REMOVE(&head->so_incomp, so, so_list);
186 so->so_state &= ~SS_INCOMP;
187 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
189 so->so_state |= SS_COMP;
191 wakeup_one(&head->so_timeo);
193 wakeup(&so->so_timeo);
200 soisdisconnecting(struct socket *so)
202 so->so_state &= ~SS_ISCONNECTING;
203 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
204 wakeup((caddr_t)&so->so_timeo);
210 soisdisconnected(struct socket *so)
212 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
213 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
214 wakeup((caddr_t)&so->so_timeo);
215 sbdrop(&so->so_snd.sb, so->so_snd.ssb_cc);
221 * Set or change the message port a socket receives commands on.
226 sosetport(struct socket *so, lwkt_port_t port)
232 * When an attempt at a new connection is noted on a socket
233 * which accepts connections, sonewconn is called. If the
234 * connection is possible (subject to space constraints, etc.)
235 * then we allocate a new structure, propoerly linked into the
236 * data structure of the original socket, and return this.
237 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
240 sonewconn(struct socket *head, int connstatus)
244 struct pru_attach_info ai;
246 if (head->so_qlen > 3 * head->so_qlimit / 2)
251 if ((head->so_options & SO_ACCEPTFILTER) != 0)
254 so->so_type = head->so_type;
255 so->so_options = head->so_options &~ SO_ACCEPTCONN;
256 so->so_linger = head->so_linger;
257 so->so_state = head->so_state | SS_NOFDREF;
258 so->so_proto = head->so_proto;
259 so->so_cred = crhold(head->so_cred);
262 ai.fd_rdir = NULL; /* jail code cruft XXX JH */
263 if (soreserve(so, head->so_snd.ssb_hiwat, head->so_rcv.ssb_hiwat, NULL) ||
264 /* Directly call function since we're already at protocol level. */
265 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, &ai)) {
269 KKASSERT(so->so_port != NULL);
270 so->so_rcv.ssb_lowat = head->so_rcv.ssb_lowat;
271 so->so_snd.ssb_lowat = head->so_snd.ssb_lowat;
272 so->so_rcv.ssb_timeo = head->so_rcv.ssb_timeo;
273 so->so_snd.ssb_timeo = head->so_snd.ssb_timeo;
274 so->so_rcv.ssb_flags |= head->so_rcv.ssb_flags &
275 (SSB_AUTOSIZE | SSB_AUTOLOWAT);
276 so->so_snd.ssb_flags |= head->so_snd.ssb_flags &
277 (SSB_AUTOSIZE | SSB_AUTOLOWAT);
279 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
280 so->so_state |= SS_COMP;
283 if (head->so_incqlen > head->so_qlimit) {
284 sp = TAILQ_FIRST(&head->so_incomp);
285 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
287 sp->so_state &= ~SS_INCOMP;
291 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
292 so->so_state |= SS_INCOMP;
297 wakeup((caddr_t)&head->so_timeo);
298 so->so_state |= connstatus;
304 * Socantsendmore indicates that no more data will be sent on the
305 * socket; it would normally be applied to a socket when the user
306 * informs the system that no more data is to be sent, by the protocol
307 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
308 * will be received, and will normally be applied to the socket by a
309 * protocol when it detects that the peer will send no more data.
310 * Data queued for reading in the socket may yet be read.
313 socantsendmore(struct socket *so)
315 so->so_state |= SS_CANTSENDMORE;
320 socantrcvmore(struct socket *so)
322 so->so_state |= SS_CANTRCVMORE;
327 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
328 * via SIGIO if the socket has the SS_ASYNC flag set.
330 * For users waiting on send/recv try to avoid unnecessary context switch
331 * thrashing. Particularly for senders of large buffers (needs to be
332 * extended to sel and aio? XXX)
335 sowakeup(struct socket *so, struct signalsockbuf *ssb)
337 struct selinfo *selinfo = &ssb->ssb_sel;
339 if (ssb->ssb_flags & SSB_WAIT) {
340 if ((ssb == &so->so_snd && ssb_space(ssb) >= ssb->ssb_lowat) ||
341 (ssb == &so->so_rcv && ssb->ssb_cc >= ssb->ssb_lowat) ||
342 (ssb == &so->so_snd && (so->so_state & SS_CANTSENDMORE)) ||
343 (ssb == &so->so_rcv && (so->so_state & SS_CANTRCVMORE))
345 ssb->ssb_flags &= ~SSB_WAIT;
346 wakeup((caddr_t)&ssb->ssb_cc);
349 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
350 pgsigio(so->so_sigio, SIGIO, 0);
351 if (ssb->ssb_flags & SSB_UPCALL)
352 (*so->so_upcall)(so, so->so_upcallarg, MB_DONTWAIT);
353 if (ssb->ssb_flags & SSB_AIO)
355 KNOTE(&selinfo->si_note, 0);
356 if (ssb->ssb_flags & SSB_MEVENT) {
357 struct netmsg_so_notify *msg, *nmsg;
359 TAILQ_FOREACH_MUTABLE(msg, &selinfo->si_mlist, nm_list, nmsg) {
360 if (msg->nm_predicate(&msg->nm_netmsg)) {
361 TAILQ_REMOVE(&selinfo->si_mlist, msg, nm_list);
362 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg,
363 msg->nm_netmsg.nm_lmsg.ms_error);
366 if (TAILQ_EMPTY(&ssb->ssb_sel.si_mlist))
367 ssb->ssb_flags &= ~SSB_MEVENT;
372 * Socket buffer (struct signalsockbuf) utility routines.
374 * Each socket contains two socket buffers: one for sending data and
375 * one for receiving data. Each buffer contains a queue of mbufs,
376 * information about the number of mbufs and amount of data in the
377 * queue, and other fields allowing select() statements and notification
378 * on data availability to be implemented.
380 * Data stored in a socket buffer is maintained as a list of records.
381 * Each record is a list of mbufs chained together with the m_next
382 * field. Records are chained together with the m_nextpkt field. The upper
383 * level routine soreceive() expects the following conventions to be
384 * observed when placing information in the receive buffer:
386 * 1. If the protocol requires each message be preceded by the sender's
387 * name, then a record containing that name must be present before
388 * any associated data (mbuf's must be of type MT_SONAME).
389 * 2. If the protocol supports the exchange of ``access rights'' (really
390 * just additional data associated with the message), and there are
391 * ``rights'' to be received, then a record containing this data
392 * should be present (mbuf's must be of type MT_RIGHTS).
393 * 3. If a name or rights record exists, then it must be followed by
394 * a data record, perhaps of zero length.
396 * Before using a new socket structure it is first necessary to reserve
397 * buffer space to the socket, by calling sbreserve(). This should commit
398 * some of the available buffer space in the system buffer pool for the
399 * socket (currently, it does nothing but enforce limits). The space
400 * should be released by calling ssb_release() when the socket is destroyed.
403 soreserve(struct socket *so, u_long sndcc, u_long rcvcc, struct rlimit *rl)
405 if (so->so_snd.ssb_lowat == 0)
406 so->so_snd.ssb_flags |= SSB_AUTOLOWAT;
407 if (ssb_reserve(&so->so_snd, sndcc, so, rl) == 0)
409 if (ssb_reserve(&so->so_rcv, rcvcc, so, rl) == 0)
411 if (so->so_rcv.ssb_lowat == 0)
412 so->so_rcv.ssb_lowat = 1;
413 if (so->so_snd.ssb_lowat == 0)
414 so->so_snd.ssb_lowat = MCLBYTES;
415 if (so->so_snd.ssb_lowat > so->so_snd.ssb_hiwat)
416 so->so_snd.ssb_lowat = so->so_snd.ssb_hiwat;
419 ssb_release(&so->so_snd, so);
425 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
428 u_long old_sb_max = sb_max;
430 error = SYSCTL_OUT(req, arg1, sizeof(int));
431 if (error || !req->newptr)
433 error = SYSCTL_IN(req, arg1, sizeof(int));
436 if (sb_max < MSIZE + MCLBYTES) {
440 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
445 * Allot mbufs to a signalsockbuf.
447 * Attempt to scale mbmax so that mbcnt doesn't become limiting
448 * if buffering efficiency is near the normal case.
450 * sb_max only applies to user-sockets (where rl != NULL). It does
451 * not apply to kernel sockets or kernel-controlled sockets. Note
452 * that NFS overrides the sockbuf limits created when nfsd creates
456 ssb_reserve(struct signalsockbuf *ssb, u_long cc, struct socket *so,
460 * rl will only be NULL when we're in an interrupt (eg, in tcp_input)
461 * or when called from netgraph (ie, ngd_attach)
463 if (rl && cc > sb_max_adj)
465 if (!chgsbsize(so->so_cred->cr_uidinfo, &ssb->ssb_hiwat, cc,
466 rl ? rl->rlim_cur : RLIM_INFINITY)) {
470 ssb->ssb_mbmax = min(cc * sb_efficiency, sb_max);
472 ssb->ssb_mbmax = cc * sb_efficiency;
475 * AUTOLOWAT is set on send buffers and prevents large writes
476 * from generating a huge number of context switches.
478 if (ssb->ssb_flags & SSB_AUTOLOWAT) {
479 ssb->ssb_lowat = ssb->ssb_hiwat / 2;
480 if (ssb->ssb_lowat < MCLBYTES)
481 ssb->ssb_lowat = MCLBYTES;
483 if (ssb->ssb_lowat > ssb->ssb_hiwat)
484 ssb->ssb_lowat = ssb->ssb_hiwat;
489 * Free mbufs held by a socket, and reserved mbuf space.
492 ssb_release(struct signalsockbuf *ssb, struct socket *so)
495 (void)chgsbsize(so->so_cred->cr_uidinfo, &ssb->ssb_hiwat, 0,
501 * Some routines that return EOPNOTSUPP for entry points that are not
502 * supported by a protocol. Fill in as needed.
505 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
511 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
517 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
523 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
529 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
530 struct ifnet *ifp, struct thread *td)
536 pru_disconnect_notsupp(struct socket *so)
542 pru_listen_notsupp(struct socket *so, struct thread *td)
548 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
554 pru_rcvd_notsupp(struct socket *so, int flags)
560 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
566 pru_shutdown_notsupp(struct socket *so)
572 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
578 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
579 struct mbuf *top, struct mbuf *control, int flags,
590 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
591 struct uio *uio, struct sockbuf *sio,
592 struct mbuf **controlp, int *flagsp)
598 pru_ctloutput_notsupp(struct socket *so, struct sockopt *sopt)
604 * This isn't really a ``null'' operation, but it's the default one
605 * and doesn't do anything destructive.
608 pru_sense_null(struct socket *so, struct stat *sb)
610 sb->st_blksize = so->so_snd.ssb_hiwat;
615 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. Callers
616 * of this routine assume that it always succeeds, so we have to use a
617 * blockable allocation even though we might be called from a critical thread.
620 dup_sockaddr(const struct sockaddr *sa)
622 struct sockaddr *sa2;
624 sa2 = kmalloc(sa->sa_len, M_SONAME, M_INTWAIT);
625 bcopy(sa, sa2, sa->sa_len);
630 * Create an external-format (``xsocket'') structure using the information
631 * in the kernel-format socket structure pointed to by so. This is done
632 * to reduce the spew of irrelevant information over this interface,
633 * to isolate user code from changes in the kernel structure, and
634 * potentially to provide information-hiding if we decide that
635 * some of this information should be hidden from users.
638 sotoxsocket(struct socket *so, struct xsocket *xso)
640 xso->xso_len = sizeof *xso;
642 xso->so_type = so->so_type;
643 xso->so_options = so->so_options;
644 xso->so_linger = so->so_linger;
645 xso->so_state = so->so_state;
646 xso->so_pcb = so->so_pcb;
647 xso->xso_protocol = so->so_proto->pr_protocol;
648 xso->xso_family = so->so_proto->pr_domain->dom_family;
649 xso->so_qlen = so->so_qlen;
650 xso->so_incqlen = so->so_incqlen;
651 xso->so_qlimit = so->so_qlimit;
652 xso->so_timeo = so->so_timeo;
653 xso->so_error = so->so_error;
654 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
655 xso->so_oobmark = so->so_oobmark;
656 ssbtoxsockbuf(&so->so_snd, &xso->so_snd);
657 ssbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
658 xso->so_uid = so->so_cred->cr_uid;
662 * Here is the definition of some of the basic objects in the kern.ipc
665 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
668 * This takes the place of kern.maxsockbuf, which moved to kern.ipc.
670 * NOTE! sb_max only applies to user-created socket buffers.
673 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
674 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW,
675 &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size");
676 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
677 &maxsockets, 0, "Maximum number of sockets available");
678 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
679 &sb_efficiency, 0, "");
682 * Initialize maxsockets
685 init_maxsockets(void *ignored)
687 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
688 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
690 SYSINIT(param, SI_BOOT1_TUNABLES, SI_ORDER_ANY,
691 init_maxsockets, NULL);