2 * Copyright (c) 1982, 1986, 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 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/domain.h>
40 #include <sys/filedesc.h>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/malloc.h>
45 #include <sys/protosw.h>
46 #include <sys/sigio.h>
47 #include <sys/signal.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/filio.h> /* XXX */
52 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/sysproto.h>
56 #include <sys/taskqueue.h>
58 #include <sys/ucred.h>
60 #include <sys/unpcb.h>
64 #include <net/if_var.h>
65 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_pcb.h>
71 #include <security/mac/mac_framework.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_map.h>
78 static SYSCTL_NODE(_kern_ipc, OID_AUTO, aio, CTLFLAG_RD, NULL,
81 static int empty_results;
82 SYSCTL_INT(_kern_ipc_aio, OID_AUTO, empty_results, CTLFLAG_RD, &empty_results,
83 0, "socket operation returned EAGAIN");
85 static int empty_retries;
86 SYSCTL_INT(_kern_ipc_aio, OID_AUTO, empty_retries, CTLFLAG_RD, &empty_retries,
87 0, "socket operation retries");
89 static fo_rdwr_t soo_read;
90 static fo_rdwr_t soo_write;
91 static fo_ioctl_t soo_ioctl;
92 static fo_poll_t soo_poll;
93 extern fo_kqfilter_t soo_kqfilter;
94 static fo_stat_t soo_stat;
95 static fo_close_t soo_close;
96 static fo_fill_kinfo_t soo_fill_kinfo;
97 static fo_aio_queue_t soo_aio_queue;
99 static void soo_aio_cancel(struct kaiocb *job);
101 struct fileops socketops = {
103 .fo_write = soo_write,
104 .fo_truncate = invfo_truncate,
105 .fo_ioctl = soo_ioctl,
107 .fo_kqfilter = soo_kqfilter,
109 .fo_close = soo_close,
110 .fo_chmod = invfo_chmod,
111 .fo_chown = invfo_chown,
112 .fo_sendfile = invfo_sendfile,
113 .fo_fill_kinfo = soo_fill_kinfo,
114 .fo_aio_queue = soo_aio_queue,
115 .fo_flags = DFLAG_PASSABLE
119 soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
120 int flags, struct thread *td)
122 struct socket *so = fp->f_data;
126 error = mac_socket_check_receive(active_cred, so);
130 error = soreceive(so, 0, uio, 0, 0, 0);
135 soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
136 int flags, struct thread *td)
138 struct socket *so = fp->f_data;
142 error = mac_socket_check_send(active_cred, so);
146 error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
147 if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
148 PROC_LOCK(uio->uio_td->td_proc);
149 tdsignal(uio->uio_td, SIGPIPE);
150 PROC_UNLOCK(uio->uio_td->td_proc);
156 soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
159 struct socket *so = fp->f_data;
166 so->so_state |= SS_NBIO;
168 so->so_state &= ~SS_NBIO;
174 * XXXRW: This code separately acquires SOCK_LOCK(so) and
175 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
176 * mutex to avoid introducing the assumption that they are
181 so->so_state |= SS_ASYNC;
183 SOCKBUF_LOCK(&so->so_rcv);
184 so->so_rcv.sb_flags |= SB_ASYNC;
185 SOCKBUF_UNLOCK(&so->so_rcv);
186 SOCKBUF_LOCK(&so->so_snd);
187 so->so_snd.sb_flags |= SB_ASYNC;
188 SOCKBUF_UNLOCK(&so->so_snd);
191 so->so_state &= ~SS_ASYNC;
193 SOCKBUF_LOCK(&so->so_rcv);
194 so->so_rcv.sb_flags &= ~SB_ASYNC;
195 SOCKBUF_UNLOCK(&so->so_rcv);
196 SOCKBUF_LOCK(&so->so_snd);
197 so->so_snd.sb_flags &= ~SB_ASYNC;
198 SOCKBUF_UNLOCK(&so->so_snd);
204 *(int *)data = sbavail(&so->so_rcv);
209 *(int *)data = sbavail(&so->so_snd);
214 if ((so->so_snd.sb_hiwat < sbused(&so->so_snd)) ||
215 (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
218 *(int *)data = sbspace(&so->so_snd);
222 error = fsetown(*(int *)data, &so->so_sigio);
226 *(int *)data = fgetown(&so->so_sigio);
230 error = fsetown(-(*(int *)data), &so->so_sigio);
234 *(int *)data = -fgetown(&so->so_sigio);
239 *(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
243 * Interface/routing/protocol specific ioctls: interface and
244 * routing ioctls should have a different entry since a
245 * socket is unnecessary.
247 if (IOCGROUP(cmd) == 'i')
248 error = ifioctl(so, cmd, data, td);
249 else if (IOCGROUP(cmd) == 'r') {
250 CURVNET_SET(so->so_vnet);
251 error = rtioctl_fib(cmd, data, so->so_fibnum);
254 CURVNET_SET(so->so_vnet);
255 error = ((*so->so_proto->pr_usrreqs->pru_control)
256 (so, cmd, data, 0, td));
265 soo_poll(struct file *fp, int events, struct ucred *active_cred,
268 struct socket *so = fp->f_data;
272 error = mac_socket_check_poll(active_cred, so);
276 return (sopoll(so, events, fp->f_cred, td));
280 soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
283 struct socket *so = fp->f_data;
289 bzero((caddr_t)ub, sizeof (*ub));
290 ub->st_mode = S_IFSOCK;
292 error = mac_socket_check_stat(active_cred, so);
297 * If SBS_CANTRCVMORE is set, but there's still data left in the
298 * receive buffer, the socket is still readable.
302 if ((sb->sb_state & SBS_CANTRCVMORE) == 0 || sbavail(sb))
303 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
304 ub->st_size = sbavail(sb) - sb->sb_ctl;
309 if ((sb->sb_state & SBS_CANTSENDMORE) == 0)
310 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
312 ub->st_uid = so->so_cred->cr_uid;
313 ub->st_gid = so->so_cred->cr_gid;
314 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
318 * API socket close on file pointer. We call soclose() to close the socket
319 * (including initiating closing protocols). soclose() will sorele() the
320 * file reference but the actual socket will not go away until the socket's
324 soo_close(struct file *fp, struct thread *td)
330 fp->f_ops = &badfileops;
339 soo_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
347 kif->kf_type = KF_TYPE_SOCKET;
349 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
350 kif->kf_sock_type = so->so_type;
351 kif->kf_sock_protocol = so->so_proto->pr_protocol;
352 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
353 switch (kif->kf_sock_domain) {
356 if (kif->kf_sock_protocol == IPPROTO_TCP) {
357 if (so->so_pcb != NULL) {
358 inpcb = (struct inpcb *)(so->so_pcb);
359 kif->kf_un.kf_sock.kf_sock_inpcb =
360 (uintptr_t)inpcb->inp_ppcb;
365 if (so->so_pcb != NULL) {
366 unpcb = (struct unpcb *)(so->so_pcb);
367 if (unpcb->unp_conn) {
368 kif->kf_un.kf_sock.kf_sock_unpconn =
369 (uintptr_t)unpcb->unp_conn;
370 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
372 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
378 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
379 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
380 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
383 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
384 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
385 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
388 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
389 sizeof(kif->kf_path));
393 static STAILQ_HEAD(, task) soaio_jobs;
394 static struct mtx soaio_jobs_lock;
395 static struct task soaio_kproc_task;
396 static int soaio_starting, soaio_idle, soaio_queued;
397 static struct unrhdr *soaio_kproc_unr;
399 static int soaio_max_procs = MAX_AIO_PROCS;
400 SYSCTL_INT(_kern_ipc_aio, OID_AUTO, max_procs, CTLFLAG_RW, &soaio_max_procs, 0,
401 "Maximum number of kernel processes to use for async socket IO");
403 static int soaio_num_procs;
404 SYSCTL_INT(_kern_ipc_aio, OID_AUTO, num_procs, CTLFLAG_RD, &soaio_num_procs, 0,
405 "Number of active kernel processes for async socket IO");
407 static int soaio_target_procs = TARGET_AIO_PROCS;
408 SYSCTL_INT(_kern_ipc_aio, OID_AUTO, target_procs, CTLFLAG_RD,
409 &soaio_target_procs, 0,
410 "Preferred number of ready kernel processes for async socket IO");
412 static int soaio_lifetime;
413 SYSCTL_INT(_kern_ipc_aio, OID_AUTO, lifetime, CTLFLAG_RW, &soaio_lifetime, 0,
414 "Maximum lifetime for idle aiod");
417 soaio_kproc_loop(void *arg)
420 struct vmspace *myvm;
422 int error, id, pending;
427 * Grab an extra reference on the daemon's vmspace so that it
428 * doesn't get freed by jobs that switch to a different
432 myvm = vmspace_acquire_ref(p);
434 mtx_lock(&soaio_jobs_lock);
435 MPASS(soaio_starting > 0);
438 while (!STAILQ_EMPTY(&soaio_jobs)) {
439 task = STAILQ_FIRST(&soaio_jobs);
440 STAILQ_REMOVE_HEAD(&soaio_jobs, ta_link);
442 pending = task->ta_pending;
443 task->ta_pending = 0;
444 mtx_unlock(&soaio_jobs_lock);
446 task->ta_func(task->ta_context, pending);
448 mtx_lock(&soaio_jobs_lock);
450 MPASS(soaio_queued == 0);
452 if (p->p_vmspace != myvm) {
453 mtx_unlock(&soaio_jobs_lock);
454 vmspace_switch_aio(myvm);
455 mtx_lock(&soaio_jobs_lock);
460 error = mtx_sleep(&soaio_idle, &soaio_jobs_lock, 0, "-",
463 if (error == EWOULDBLOCK && STAILQ_EMPTY(&soaio_jobs) &&
464 soaio_num_procs > soaio_target_procs)
468 mtx_unlock(&soaio_jobs_lock);
469 free_unr(soaio_kproc_unr, id);
474 soaio_kproc_create(void *context, int pending)
479 mtx_lock(&soaio_jobs_lock);
481 if (soaio_num_procs < soaio_target_procs) {
483 } else if (soaio_num_procs >= soaio_max_procs) {
485 * Hit the limit on kernel processes, don't
486 * create another one.
489 } else if (soaio_queued <= soaio_idle + soaio_starting) {
491 * No more AIO jobs waiting for a process to be
497 mtx_unlock(&soaio_jobs_lock);
499 id = alloc_unr(soaio_kproc_unr);
500 error = kproc_create(soaio_kproc_loop, (void *)(intptr_t)id,
501 &p, 0, 0, "soaiod%d", id);
503 free_unr(soaio_kproc_unr, id);
504 mtx_lock(&soaio_jobs_lock);
509 mtx_lock(&soaio_jobs_lock);
512 mtx_unlock(&soaio_jobs_lock);
516 soaio_enqueue(struct task *task)
519 mtx_lock(&soaio_jobs_lock);
520 MPASS(task->ta_pending == 0);
522 STAILQ_INSERT_TAIL(&soaio_jobs, task, ta_link);
524 if (soaio_queued <= soaio_idle)
525 wakeup_one(&soaio_idle);
526 else if (soaio_num_procs < soaio_max_procs)
527 taskqueue_enqueue(taskqueue_thread, &soaio_kproc_task);
528 mtx_unlock(&soaio_jobs_lock);
535 soaio_lifetime = AIOD_LIFETIME_DEFAULT;
536 STAILQ_INIT(&soaio_jobs);
537 mtx_init(&soaio_jobs_lock, "soaio jobs", NULL, MTX_DEF);
538 soaio_kproc_unr = new_unrhdr(1, INT_MAX, NULL);
539 TASK_INIT(&soaio_kproc_task, 0, soaio_kproc_create, NULL);
540 if (soaio_target_procs > 0)
541 taskqueue_enqueue(taskqueue_thread, &soaio_kproc_task);
543 SYSINIT(soaio, SI_SUB_VFS, SI_ORDER_ANY, soaio_init, NULL);
546 soaio_ready(struct socket *so, struct sockbuf *sb)
548 return (sb == &so->so_rcv ? soreadable(so) : sowriteable(so));
552 soaio_process_job(struct socket *so, struct sockbuf *sb, struct kaiocb *job)
554 struct ucred *td_savedcred;
563 aio_switch_vmspace(job);
567 td_savedcred = td->td_ucred;
568 td->td_ucred = job->cred;
570 cnt = job->uaiocb.aio_nbytes;
571 iov.iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
577 uio.uio_segflg = UIO_USERSPACE;
581 /* TODO: Charge ru_msg* to job. */
583 if (sb == &so->so_rcv) {
584 uio.uio_rw = UIO_READ;
586 error = mac_socket_check_receive(fp->f_cred, so);
590 error = soreceive(so, NULL, &uio, NULL, NULL, &flags);
592 uio.uio_rw = UIO_WRITE;
594 error = mac_socket_check_send(fp->f_cred, so);
597 error = sosend(so, NULL, &uio, NULL, NULL, flags, td);
598 if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
599 PROC_LOCK(job->userproc);
600 kern_psignal(job->userproc, SIGPIPE);
601 PROC_UNLOCK(job->userproc);
605 cnt -= uio.uio_resid;
606 td->td_ucred = td_savedcred;
608 /* XXX: Not sure if this is needed? */
609 if (cnt != 0 && (error == ERESTART || error == EINTR ||
610 error == EWOULDBLOCK))
612 if (error == EWOULDBLOCK) {
614 * A read() or write() on the socket raced with this
615 * request. If the socket is now ready, try again.
616 * If it is not, place this request at the head of the
617 * queue to try again when the socket is ready.
621 if (soaio_ready(so, sb)) {
627 if (!aio_set_cancel_function(job, soo_aio_cancel)) {
633 TAILQ_INSERT_HEAD(&sb->sb_aiojobq, job, list);
636 aio_complete(job, cnt, error);
642 soaio_process_sb(struct socket *so, struct sockbuf *sb)
647 while (!TAILQ_EMPTY(&sb->sb_aiojobq) && soaio_ready(so, sb)) {
648 job = TAILQ_FIRST(&sb->sb_aiojobq);
649 TAILQ_REMOVE(&sb->sb_aiojobq, job, list);
650 if (!aio_clear_cancel_function(job))
653 soaio_process_job(so, sb, job);
657 * If there are still pending requests, the socket must not be
658 * ready so set SB_AIO to request a wakeup when the socket
661 if (!TAILQ_EMPTY(&sb->sb_aiojobq))
662 sb->sb_flags |= SB_AIO;
663 sb->sb_flags &= ~SB_AIO_RUNNING;
672 soaio_rcv(void *context, int pending)
677 soaio_process_sb(so, &so->so_rcv);
681 soaio_snd(void *context, int pending)
686 soaio_process_sb(so, &so->so_snd);
690 sowakeup_aio(struct socket *so, struct sockbuf *sb)
693 SOCKBUF_LOCK_ASSERT(sb);
694 sb->sb_flags &= ~SB_AIO;
695 if (sb->sb_flags & SB_AIO_RUNNING)
697 sb->sb_flags |= SB_AIO_RUNNING;
698 if (sb == &so->so_snd)
701 if (sb == &so->so_snd)
703 soaio_enqueue(&sb->sb_aiotask);
707 soo_aio_cancel(struct kaiocb *job)
713 so = job->fd_file->f_data;
714 opcode = job->uaiocb.aio_lio_opcode;
715 if (opcode == LIO_READ)
718 MPASS(opcode == LIO_WRITE);
723 if (!aio_cancel_cleared(job))
724 TAILQ_REMOVE(&sb->sb_aiojobq, job, list);
725 if (TAILQ_EMPTY(&sb->sb_aiojobq))
726 sb->sb_flags &= ~SB_AIO;
733 soo_aio_queue(struct file *fp, struct kaiocb *job)
739 switch (job->uaiocb.aio_lio_opcode) {
751 if (!aio_set_cancel_function(job, soo_aio_cancel))
752 panic("new job was cancelled");
753 TAILQ_INSERT_TAIL(&sb->sb_aiojobq, job, list);
754 if (!(sb->sb_flags & SB_AIO_RUNNING)) {
755 if (soaio_ready(so, sb))
756 sowakeup_aio(so, sb);
758 sb->sb_flags |= SB_AIO;