uipc_socket2.c revision 1.93 1 /* $NetBSD: uipc_socket2.c,v 1.93 2008/05/24 16:35:28 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1982, 1986, 1988, 1990, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)uipc_socket2.c 8.2 (Berkeley) 2/14/95
58 */
59
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.93 2008/05/24 16:35:28 christos Exp $");
62
63 #include "opt_mbuftrace.h"
64 #include "opt_sb_max.h"
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/proc.h>
69 #include <sys/file.h>
70 #include <sys/buf.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/protosw.h>
74 #include <sys/domain.h>
75 #include <sys/poll.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/signalvar.h>
79 #include <sys/kauth.h>
80 #include <sys/pool.h>
81
82 /*
83 * Primitive routines for operating on sockets and socket buffers.
84 *
85 * Locking rules and assumptions:
86 *
87 * o socket::so_lock can change on the fly. The low level routines used
88 * to lock sockets are aware of this. When so_lock is acquired, the
89 * routine locking must check to see if so_lock still points to the
90 * lock that was acquired. If so_lock has changed in the meantime, the
91 * now irellevant lock that was acquired must be dropped and the lock
92 * operation retried. Although not proven here, this is completely safe
93 * on a multiprocessor system, even with relaxed memory ordering, given
94 * the next two rules:
95 *
96 * o In order to mutate so_lock, the lock pointed to by the current value
97 * of so_lock must be held: i.e., the socket must be held locked by the
98 * changing thread. The thread must issue membar_exit() to prevent
99 * memory accesses being reordered, and can set so_lock to the desired
100 * value. If the lock pointed to by the new value of so_lock is not
101 * held by the changing thread, the socket must then be considered
102 * unlocked.
103 *
104 * o If so_lock is mutated, and the previous lock referred to by so_lock
105 * could still be visible to other threads in the system (e.g. via file
106 * descriptor or protocol-internal reference), then the old lock must
107 * remain valid until the socket and/or protocol control block has been
108 * torn down.
109 *
110 * o If a socket has a non-NULL so_head value (i.e. is in the process of
111 * connecting), then locking the socket must also lock the socket pointed
112 * to by so_head: their lock pointers must match.
113 *
114 * o If a socket has connections in progress (so_q, so_q0 not empty) then
115 * locking the socket must also lock the sockets attached to both queues.
116 * Again, their lock pointers must match.
117 *
118 * o Beyond the initial lock assigment in socreate(), assigning locks to
119 * sockets is the responsibility of the individual protocols / protocol
120 * domains.
121 */
122
123 static POOL_INIT(socket_pool, sizeof(struct socket), 0, 0, 0, "sockpl", NULL,
124 IPL_SOFTNET);
125
126 u_long sb_max = SB_MAX; /* maximum socket buffer size */
127 static u_long sb_max_adj; /* adjusted sb_max */
128
129 /*
130 * Procedures to manipulate state flags of socket
131 * and do appropriate wakeups. Normal sequence from the
132 * active (originating) side is that soisconnecting() is
133 * called during processing of connect() call,
134 * resulting in an eventual call to soisconnected() if/when the
135 * connection is established. When the connection is torn down
136 * soisdisconnecting() is called during processing of disconnect() call,
137 * and soisdisconnected() is called when the connection to the peer
138 * is totally severed. The semantics of these routines are such that
139 * connectionless protocols can call soisconnected() and soisdisconnected()
140 * only, bypassing the in-progress calls when setting up a ``connection''
141 * takes no time.
142 *
143 * From the passive side, a socket is created with
144 * two queues of sockets: so_q0 for connections in progress
145 * and so_q for connections already made and awaiting user acceptance.
146 * As a protocol is preparing incoming connections, it creates a socket
147 * structure queued on so_q0 by calling sonewconn(). When the connection
148 * is established, soisconnected() is called, and transfers the
149 * socket structure to so_q, making it available to accept().
150 *
151 * If a socket is closed with sockets on either
152 * so_q0 or so_q, these sockets are dropped.
153 *
154 * If higher level protocols are implemented in
155 * the kernel, the wakeups done here will sometimes
156 * cause software-interrupt process scheduling.
157 */
158
159 void
160 soisconnecting(struct socket *so)
161 {
162
163 KASSERT(solocked(so));
164
165 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
166 so->so_state |= SS_ISCONNECTING;
167 }
168
169 void
170 soisconnected(struct socket *so)
171 {
172 struct socket *head;
173
174 head = so->so_head;
175
176 KASSERT(solocked(so));
177 KASSERT(head == NULL || solocked2(so, head));
178
179 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
180 so->so_state |= SS_ISCONNECTED;
181 if (head && soqremque(so, 0)) {
182 soqinsque(head, so, 1);
183 sorwakeup(head);
184 cv_broadcast(&head->so_cv);
185 } else {
186 cv_broadcast(&so->so_cv);
187 sorwakeup(so);
188 sowwakeup(so);
189 }
190 }
191
192 void
193 soisdisconnecting(struct socket *so)
194 {
195
196 KASSERT(solocked(so));
197
198 so->so_state &= ~SS_ISCONNECTING;
199 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
200 cv_broadcast(&so->so_cv);
201 sowwakeup(so);
202 sorwakeup(so);
203 }
204
205 void
206 soisdisconnected(struct socket *so)
207 {
208
209 KASSERT(solocked(so));
210
211 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
212 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
213 cv_broadcast(&so->so_cv);
214 sowwakeup(so);
215 sorwakeup(so);
216 }
217
218 /*
219 * When an attempt at a new connection is noted on a socket
220 * which accepts connections, sonewconn is called. If the
221 * connection is possible (subject to space constraints, etc.)
222 * then we allocate a new structure, propoerly linked into the
223 * data structure of the original socket, and return this.
224 * Connstatus may be 0, SS_ISCONFIRMING, or SS_ISCONNECTED.
225 */
226 struct socket *
227 sonewconn(struct socket *head, int connstatus)
228 {
229 struct socket *so;
230 int soqueue, error;
231
232 KASSERT(solocked(head));
233
234 soqueue = connstatus ? 1 : 0;
235 if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
236 return ((struct socket *)0);
237 so = soget(false);
238 if (so == NULL)
239 return (NULL);
240 mutex_obj_hold(head->so_lock);
241 so->so_lock = head->so_lock;
242 so->so_type = head->so_type;
243 so->so_options = head->so_options &~ SO_ACCEPTCONN;
244 so->so_linger = head->so_linger;
245 so->so_state = head->so_state | SS_NOFDREF;
246 so->so_nbio = head->so_nbio;
247 so->so_proto = head->so_proto;
248 so->so_timeo = head->so_timeo;
249 so->so_pgid = head->so_pgid;
250 so->so_send = head->so_send;
251 so->so_receive = head->so_receive;
252 so->so_uidinfo = head->so_uidinfo;
253 #ifdef MBUFTRACE
254 so->so_mowner = head->so_mowner;
255 so->so_rcv.sb_mowner = head->so_rcv.sb_mowner;
256 so->so_snd.sb_mowner = head->so_snd.sb_mowner;
257 #endif
258 (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
259 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
260 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
261 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
262 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
263 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
264 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
265 soqinsque(head, so, soqueue);
266 error = (*so->so_proto->pr_usrreq)(so, PRU_ATTACH, NULL, NULL,
267 NULL, NULL);
268 KASSERT(solocked(so));
269 if (error != 0) {
270 (void) soqremque(so, soqueue);
271 soput(so);
272 return (NULL);
273 }
274 if (connstatus) {
275 sorwakeup(head);
276 cv_broadcast(&head->so_cv);
277 so->so_state |= connstatus;
278 }
279 return (so);
280 }
281
282 struct socket *
283 soget(bool waitok)
284 {
285 struct socket *so;
286
287 so = pool_get(&socket_pool, (waitok ? PR_WAITOK : PR_NOWAIT));
288 if (__predict_false(so == NULL))
289 return (NULL);
290 memset(so, 0, sizeof(*so));
291 TAILQ_INIT(&so->so_q0);
292 TAILQ_INIT(&so->so_q);
293 cv_init(&so->so_cv, "socket");
294 cv_init(&so->so_rcv.sb_cv, "netio");
295 cv_init(&so->so_snd.sb_cv, "netio");
296 selinit(&so->so_rcv.sb_sel);
297 selinit(&so->so_snd.sb_sel);
298 so->so_rcv.sb_so = so;
299 so->so_snd.sb_so = so;
300 return so;
301 }
302
303 void
304 soput(struct socket *so)
305 {
306
307 KASSERT(!cv_has_waiters(&so->so_cv));
308 KASSERT(!cv_has_waiters(&so->so_rcv.sb_cv));
309 KASSERT(!cv_has_waiters(&so->so_snd.sb_cv));
310 seldestroy(&so->so_rcv.sb_sel);
311 seldestroy(&so->so_snd.sb_sel);
312 mutex_obj_free(so->so_lock);
313 cv_destroy(&so->so_cv);
314 cv_destroy(&so->so_rcv.sb_cv);
315 cv_destroy(&so->so_snd.sb_cv);
316 pool_put(&socket_pool, so);
317 }
318
319 void
320 soqinsque(struct socket *head, struct socket *so, int q)
321 {
322
323 KASSERT(solocked2(head, so));
324
325 #ifdef DIAGNOSTIC
326 if (so->so_onq != NULL)
327 panic("soqinsque");
328 #endif
329
330 so->so_head = head;
331 if (q == 0) {
332 head->so_q0len++;
333 so->so_onq = &head->so_q0;
334 } else {
335 head->so_qlen++;
336 so->so_onq = &head->so_q;
337 }
338 TAILQ_INSERT_TAIL(so->so_onq, so, so_qe);
339 }
340
341 int
342 soqremque(struct socket *so, int q)
343 {
344 struct socket *head;
345
346 head = so->so_head;
347
348 KASSERT(solocked(so));
349 if (q == 0) {
350 if (so->so_onq != &head->so_q0)
351 return (0);
352 head->so_q0len--;
353 } else {
354 if (so->so_onq != &head->so_q)
355 return (0);
356 head->so_qlen--;
357 }
358 KASSERT(solocked2(so, head));
359 TAILQ_REMOVE(so->so_onq, so, so_qe);
360 so->so_onq = NULL;
361 so->so_head = NULL;
362 return (1);
363 }
364
365 /*
366 * Socantsendmore indicates that no more data will be sent on the
367 * socket; it would normally be applied to a socket when the user
368 * informs the system that no more data is to be sent, by the protocol
369 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
370 * will be received, and will normally be applied to the socket by a
371 * protocol when it detects that the peer will send no more data.
372 * Data queued for reading in the socket may yet be read.
373 */
374
375 void
376 socantsendmore(struct socket *so)
377 {
378
379 KASSERT(solocked(so));
380
381 so->so_state |= SS_CANTSENDMORE;
382 sowwakeup(so);
383 }
384
385 void
386 socantrcvmore(struct socket *so)
387 {
388
389 KASSERT(solocked(so));
390
391 so->so_state |= SS_CANTRCVMORE;
392 sorwakeup(so);
393 }
394
395 /*
396 * Wait for data to arrive at/drain from a socket buffer.
397 */
398 int
399 sbwait(struct sockbuf *sb)
400 {
401 struct socket *so;
402 kmutex_t *lock;
403 int error;
404
405 so = sb->sb_so;
406
407 KASSERT(solocked(so));
408
409 sb->sb_flags |= SB_NOTIFY;
410 lock = so->so_lock;
411 if ((sb->sb_flags & SB_NOINTR) != 0)
412 error = cv_timedwait(&sb->sb_cv, lock, sb->sb_timeo);
413 else
414 error = cv_timedwait_sig(&sb->sb_cv, lock, sb->sb_timeo);
415 if (__predict_false(lock != so->so_lock))
416 solockretry(so, lock);
417 return error;
418 }
419
420 /*
421 * Wakeup processes waiting on a socket buffer.
422 * Do asynchronous notification via SIGIO
423 * if the socket buffer has the SB_ASYNC flag set.
424 */
425 void
426 sowakeup(struct socket *so, struct sockbuf *sb, int code)
427 {
428 int band;
429
430 KASSERT(solocked(so));
431 KASSERT(sb->sb_so == so);
432
433 if (code == POLL_IN)
434 band = POLLIN|POLLRDNORM;
435 else
436 band = POLLOUT|POLLWRNORM;
437 sb->sb_flags &= ~SB_NOTIFY;
438 selnotify(&sb->sb_sel, band, NOTE_SUBMIT);
439 cv_broadcast(&sb->sb_cv);
440 if (sb->sb_flags & SB_ASYNC)
441 fownsignal(so->so_pgid, SIGIO, code, band, so);
442 if (sb->sb_flags & SB_UPCALL)
443 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
444 }
445
446 /*
447 * Socket buffer (struct sockbuf) utility routines.
448 *
449 * Each socket contains two socket buffers: one for sending data and
450 * one for receiving data. Each buffer contains a queue of mbufs,
451 * information about the number of mbufs and amount of data in the
452 * queue, and other fields allowing poll() statements and notification
453 * on data availability to be implemented.
454 *
455 * Data stored in a socket buffer is maintained as a list of records.
456 * Each record is a list of mbufs chained together with the m_next
457 * field. Records are chained together with the m_nextpkt field. The upper
458 * level routine soreceive() expects the following conventions to be
459 * observed when placing information in the receive buffer:
460 *
461 * 1. If the protocol requires each message be preceded by the sender's
462 * name, then a record containing that name must be present before
463 * any associated data (mbuf's must be of type MT_SONAME).
464 * 2. If the protocol supports the exchange of ``access rights'' (really
465 * just additional data associated with the message), and there are
466 * ``rights'' to be received, then a record containing this data
467 * should be present (mbuf's must be of type MT_CONTROL).
468 * 3. If a name or rights record exists, then it must be followed by
469 * a data record, perhaps of zero length.
470 *
471 * Before using a new socket structure it is first necessary to reserve
472 * buffer space to the socket, by calling sbreserve(). This should commit
473 * some of the available buffer space in the system buffer pool for the
474 * socket (currently, it does nothing but enforce limits). The space
475 * should be released by calling sbrelease() when the socket is destroyed.
476 */
477
478 int
479 sb_max_set(u_long new_sbmax)
480 {
481 int s;
482
483 if (new_sbmax < (16 * 1024))
484 return (EINVAL);
485
486 s = splsoftnet();
487 sb_max = new_sbmax;
488 sb_max_adj = (u_quad_t)new_sbmax * MCLBYTES / (MSIZE + MCLBYTES);
489 splx(s);
490
491 return (0);
492 }
493
494 int
495 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
496 {
497
498 KASSERT(so->so_lock == NULL || solocked(so));
499
500 /*
501 * there's at least one application (a configure script of screen)
502 * which expects a fifo is writable even if it has "some" bytes
503 * in its buffer.
504 * so we want to make sure (hiwat - lowat) >= (some bytes).
505 *
506 * PIPE_BUF here is an arbitrary value chosen as (some bytes) above.
507 * we expect it's large enough for such applications.
508 */
509 u_long lowat = MAX(sock_loan_thresh, MCLBYTES);
510 u_long hiwat = lowat + PIPE_BUF;
511
512 if (sndcc < hiwat)
513 sndcc = hiwat;
514 if (sbreserve(&so->so_snd, sndcc, so) == 0)
515 goto bad;
516 if (sbreserve(&so->so_rcv, rcvcc, so) == 0)
517 goto bad2;
518 if (so->so_rcv.sb_lowat == 0)
519 so->so_rcv.sb_lowat = 1;
520 if (so->so_snd.sb_lowat == 0)
521 so->so_snd.sb_lowat = lowat;
522 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
523 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
524 return (0);
525 bad2:
526 sbrelease(&so->so_snd, so);
527 bad:
528 return (ENOBUFS);
529 }
530
531 /*
532 * Allot mbufs to a sockbuf.
533 * Attempt to scale mbmax so that mbcnt doesn't become limiting
534 * if buffering efficiency is near the normal case.
535 */
536 int
537 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so)
538 {
539 struct lwp *l = curlwp; /* XXX */
540 rlim_t maxcc;
541 struct uidinfo *uidinfo;
542
543 KASSERT(so->so_lock == NULL || solocked(so));
544 KASSERT(sb->sb_so == so);
545 KASSERT(sb_max_adj != 0);
546
547 if (cc == 0 || cc > sb_max_adj)
548 return (0);
549
550 if (kauth_cred_geteuid(l->l_cred) == so->so_uidinfo->ui_uid)
551 maxcc = l->l_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur;
552 else
553 maxcc = RLIM_INFINITY;
554
555 uidinfo = so->so_uidinfo;
556 if (!chgsbsize(uidinfo, &sb->sb_hiwat, cc, maxcc))
557 return 0;
558 sb->sb_mbmax = min(cc * 2, sb_max);
559 if (sb->sb_lowat > sb->sb_hiwat)
560 sb->sb_lowat = sb->sb_hiwat;
561 return (1);
562 }
563
564 /*
565 * Free mbufs held by a socket, and reserved mbuf space. We do not assert
566 * that the socket is held locked here: see sorflush().
567 */
568 void
569 sbrelease(struct sockbuf *sb, struct socket *so)
570 {
571
572 KASSERT(sb->sb_so == so);
573
574 sbflush(sb);
575 (void)chgsbsize(so->so_uidinfo, &sb->sb_hiwat, 0, RLIM_INFINITY);
576 sb->sb_mbmax = 0;
577 }
578
579 /*
580 * Routines to add and remove
581 * data from an mbuf queue.
582 *
583 * The routines sbappend() or sbappendrecord() are normally called to
584 * append new mbufs to a socket buffer, after checking that adequate
585 * space is available, comparing the function sbspace() with the amount
586 * of data to be added. sbappendrecord() differs from sbappend() in
587 * that data supplied is treated as the beginning of a new record.
588 * To place a sender's address, optional access rights, and data in a
589 * socket receive buffer, sbappendaddr() should be used. To place
590 * access rights and data in a socket receive buffer, sbappendrights()
591 * should be used. In either case, the new data begins a new record.
592 * Note that unlike sbappend() and sbappendrecord(), these routines check
593 * for the caller that there will be enough space to store the data.
594 * Each fails if there is not enough space, or if it cannot find mbufs
595 * to store additional information in.
596 *
597 * Reliable protocols may use the socket send buffer to hold data
598 * awaiting acknowledgement. Data is normally copied from a socket
599 * send buffer in a protocol with m_copy for output to a peer,
600 * and then removing the data from the socket buffer with sbdrop()
601 * or sbdroprecord() when the data is acknowledged by the peer.
602 */
603
604 #ifdef SOCKBUF_DEBUG
605 void
606 sblastrecordchk(struct sockbuf *sb, const char *where)
607 {
608 struct mbuf *m = sb->sb_mb;
609
610 KASSERT(solocked(sb->sb_so));
611
612 while (m && m->m_nextpkt)
613 m = m->m_nextpkt;
614
615 if (m != sb->sb_lastrecord) {
616 printf("sblastrecordchk: sb_mb %p sb_lastrecord %p last %p\n",
617 sb->sb_mb, sb->sb_lastrecord, m);
618 printf("packet chain:\n");
619 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
620 printf("\t%p\n", m);
621 panic("sblastrecordchk from %s", where);
622 }
623 }
624
625 void
626 sblastmbufchk(struct sockbuf *sb, const char *where)
627 {
628 struct mbuf *m = sb->sb_mb;
629 struct mbuf *n;
630
631 KASSERT(solocked(sb->sb_so));
632
633 while (m && m->m_nextpkt)
634 m = m->m_nextpkt;
635
636 while (m && m->m_next)
637 m = m->m_next;
638
639 if (m != sb->sb_mbtail) {
640 printf("sblastmbufchk: sb_mb %p sb_mbtail %p last %p\n",
641 sb->sb_mb, sb->sb_mbtail, m);
642 printf("packet tree:\n");
643 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
644 printf("\t");
645 for (n = m; n != NULL; n = n->m_next)
646 printf("%p ", n);
647 printf("\n");
648 }
649 panic("sblastmbufchk from %s", where);
650 }
651 }
652 #endif /* SOCKBUF_DEBUG */
653
654 /*
655 * Link a chain of records onto a socket buffer
656 */
657 #define SBLINKRECORDCHAIN(sb, m0, mlast) \
658 do { \
659 if ((sb)->sb_lastrecord != NULL) \
660 (sb)->sb_lastrecord->m_nextpkt = (m0); \
661 else \
662 (sb)->sb_mb = (m0); \
663 (sb)->sb_lastrecord = (mlast); \
664 } while (/*CONSTCOND*/0)
665
666
667 #define SBLINKRECORD(sb, m0) \
668 SBLINKRECORDCHAIN(sb, m0, m0)
669
670 /*
671 * Append mbuf chain m to the last record in the
672 * socket buffer sb. The additional space associated
673 * the mbuf chain is recorded in sb. Empty mbufs are
674 * discarded and mbufs are compacted where possible.
675 */
676 void
677 sbappend(struct sockbuf *sb, struct mbuf *m)
678 {
679 struct mbuf *n;
680
681 KASSERT(solocked(sb->sb_so));
682
683 if (m == 0)
684 return;
685
686 #ifdef MBUFTRACE
687 m_claimm(m, sb->sb_mowner);
688 #endif
689
690 SBLASTRECORDCHK(sb, "sbappend 1");
691
692 if ((n = sb->sb_lastrecord) != NULL) {
693 /*
694 * XXX Would like to simply use sb_mbtail here, but
695 * XXX I need to verify that I won't miss an EOR that
696 * XXX way.
697 */
698 do {
699 if (n->m_flags & M_EOR) {
700 sbappendrecord(sb, m); /* XXXXXX!!!! */
701 return;
702 }
703 } while (n->m_next && (n = n->m_next));
704 } else {
705 /*
706 * If this is the first record in the socket buffer, it's
707 * also the last record.
708 */
709 sb->sb_lastrecord = m;
710 }
711 sbcompress(sb, m, n);
712 SBLASTRECORDCHK(sb, "sbappend 2");
713 }
714
715 /*
716 * This version of sbappend() should only be used when the caller
717 * absolutely knows that there will never be more than one record
718 * in the socket buffer, that is, a stream protocol (such as TCP).
719 */
720 void
721 sbappendstream(struct sockbuf *sb, struct mbuf *m)
722 {
723
724 KASSERT(solocked(sb->sb_so));
725 KDASSERT(m->m_nextpkt == NULL);
726 KASSERT(sb->sb_mb == sb->sb_lastrecord);
727
728 SBLASTMBUFCHK(sb, __func__);
729
730 #ifdef MBUFTRACE
731 m_claimm(m, sb->sb_mowner);
732 #endif
733
734 sbcompress(sb, m, sb->sb_mbtail);
735
736 sb->sb_lastrecord = sb->sb_mb;
737 SBLASTRECORDCHK(sb, __func__);
738 }
739
740 #ifdef SOCKBUF_DEBUG
741 void
742 sbcheck(struct sockbuf *sb)
743 {
744 struct mbuf *m, *m2;
745 u_long len, mbcnt;
746
747 KASSERT(solocked(sb->sb_so));
748
749 len = 0;
750 mbcnt = 0;
751 for (m = sb->sb_mb; m; m = m->m_nextpkt) {
752 for (m2 = m; m2 != NULL; m2 = m2->m_next) {
753 len += m2->m_len;
754 mbcnt += MSIZE;
755 if (m2->m_flags & M_EXT)
756 mbcnt += m2->m_ext.ext_size;
757 if (m2->m_nextpkt != NULL)
758 panic("sbcheck nextpkt");
759 }
760 }
761 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
762 printf("cc %lu != %lu || mbcnt %lu != %lu\n", len, sb->sb_cc,
763 mbcnt, sb->sb_mbcnt);
764 panic("sbcheck");
765 }
766 }
767 #endif
768
769 /*
770 * As above, except the mbuf chain
771 * begins a new record.
772 */
773 void
774 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
775 {
776 struct mbuf *m;
777
778 KASSERT(solocked(sb->sb_so));
779
780 if (m0 == 0)
781 return;
782
783 #ifdef MBUFTRACE
784 m_claimm(m0, sb->sb_mowner);
785 #endif
786 /*
787 * Put the first mbuf on the queue.
788 * Note this permits zero length records.
789 */
790 sballoc(sb, m0);
791 SBLASTRECORDCHK(sb, "sbappendrecord 1");
792 SBLINKRECORD(sb, m0);
793 m = m0->m_next;
794 m0->m_next = 0;
795 if (m && (m0->m_flags & M_EOR)) {
796 m0->m_flags &= ~M_EOR;
797 m->m_flags |= M_EOR;
798 }
799 sbcompress(sb, m, m0);
800 SBLASTRECORDCHK(sb, "sbappendrecord 2");
801 }
802
803 /*
804 * As above except that OOB data
805 * is inserted at the beginning of the sockbuf,
806 * but after any other OOB data.
807 */
808 void
809 sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
810 {
811 struct mbuf *m, **mp;
812
813 KASSERT(solocked(sb->sb_so));
814
815 if (m0 == 0)
816 return;
817
818 SBLASTRECORDCHK(sb, "sbinsertoob 1");
819
820 for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) {
821 again:
822 switch (m->m_type) {
823
824 case MT_OOBDATA:
825 continue; /* WANT next train */
826
827 case MT_CONTROL:
828 if ((m = m->m_next) != NULL)
829 goto again; /* inspect THIS train further */
830 }
831 break;
832 }
833 /*
834 * Put the first mbuf on the queue.
835 * Note this permits zero length records.
836 */
837 sballoc(sb, m0);
838 m0->m_nextpkt = *mp;
839 if (*mp == NULL) {
840 /* m0 is actually the new tail */
841 sb->sb_lastrecord = m0;
842 }
843 *mp = m0;
844 m = m0->m_next;
845 m0->m_next = 0;
846 if (m && (m0->m_flags & M_EOR)) {
847 m0->m_flags &= ~M_EOR;
848 m->m_flags |= M_EOR;
849 }
850 sbcompress(sb, m, m0);
851 SBLASTRECORDCHK(sb, "sbinsertoob 2");
852 }
853
854 /*
855 * Append address and data, and optionally, control (ancillary) data
856 * to the receive queue of a socket. If present,
857 * m0 must include a packet header with total length.
858 * Returns 0 if no space in sockbuf or insufficient mbufs.
859 */
860 int
861 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, struct mbuf *m0,
862 struct mbuf *control)
863 {
864 struct mbuf *m, *n, *nlast;
865 int space, len;
866
867 KASSERT(solocked(sb->sb_so));
868
869 space = asa->sa_len;
870
871 if (m0 != NULL) {
872 if ((m0->m_flags & M_PKTHDR) == 0)
873 panic("sbappendaddr");
874 space += m0->m_pkthdr.len;
875 #ifdef MBUFTRACE
876 m_claimm(m0, sb->sb_mowner);
877 #endif
878 }
879 for (n = control; n; n = n->m_next) {
880 space += n->m_len;
881 MCLAIM(n, sb->sb_mowner);
882 if (n->m_next == 0) /* keep pointer to last control buf */
883 break;
884 }
885 if (space > sbspace(sb))
886 return (0);
887 MGET(m, M_DONTWAIT, MT_SONAME);
888 if (m == 0)
889 return (0);
890 MCLAIM(m, sb->sb_mowner);
891 /*
892 * XXX avoid 'comparison always true' warning which isn't easily
893 * avoided.
894 */
895 len = asa->sa_len;
896 if (len > MLEN) {
897 MEXTMALLOC(m, asa->sa_len, M_NOWAIT);
898 if ((m->m_flags & M_EXT) == 0) {
899 m_free(m);
900 return (0);
901 }
902 }
903 m->m_len = asa->sa_len;
904 memcpy(mtod(m, void *), asa, asa->sa_len);
905 if (n)
906 n->m_next = m0; /* concatenate data to control */
907 else
908 control = m0;
909 m->m_next = control;
910
911 SBLASTRECORDCHK(sb, "sbappendaddr 1");
912
913 for (n = m; n->m_next != NULL; n = n->m_next)
914 sballoc(sb, n);
915 sballoc(sb, n);
916 nlast = n;
917 SBLINKRECORD(sb, m);
918
919 sb->sb_mbtail = nlast;
920 SBLASTMBUFCHK(sb, "sbappendaddr");
921 SBLASTRECORDCHK(sb, "sbappendaddr 2");
922
923 return (1);
924 }
925
926 /*
927 * Helper for sbappendchainaddr: prepend a struct sockaddr* to
928 * an mbuf chain.
929 */
930 static inline struct mbuf *
931 m_prepend_sockaddr(struct sockbuf *sb, struct mbuf *m0,
932 const struct sockaddr *asa)
933 {
934 struct mbuf *m;
935 const int salen = asa->sa_len;
936
937 KASSERT(solocked(sb->sb_so));
938
939 /* only the first in each chain need be a pkthdr */
940 MGETHDR(m, M_DONTWAIT, MT_SONAME);
941 if (m == 0)
942 return (0);
943 MCLAIM(m, sb->sb_mowner);
944 #ifdef notyet
945 if (salen > MHLEN) {
946 MEXTMALLOC(m, salen, M_NOWAIT);
947 if ((m->m_flags & M_EXT) == 0) {
948 m_free(m);
949 return (0);
950 }
951 }
952 #else
953 KASSERT(salen <= MHLEN);
954 #endif
955 m->m_len = salen;
956 memcpy(mtod(m, void *), asa, salen);
957 m->m_next = m0;
958 m->m_pkthdr.len = salen + m0->m_pkthdr.len;
959
960 return m;
961 }
962
963 int
964 sbappendaddrchain(struct sockbuf *sb, const struct sockaddr *asa,
965 struct mbuf *m0, int sbprio)
966 {
967 int space;
968 struct mbuf *m, *n, *n0, *nlast;
969 int error;
970
971 KASSERT(solocked(sb->sb_so));
972
973 /*
974 * XXX sbprio reserved for encoding priority of this* request:
975 * SB_PRIO_NONE --> honour normal sb limits
976 * SB_PRIO_ONESHOT_OVERFLOW --> if socket has any space,
977 * take whole chain. Intended for large requests
978 * that should be delivered atomically (all, or none).
979 * SB_PRIO_OVERDRAFT -- allow a small (2*MLEN) overflow
980 * over normal socket limits, for messages indicating
981 * buffer overflow in earlier normal/lower-priority messages
982 * SB_PRIO_BESTEFFORT --> ignore limits entirely.
983 * Intended for kernel-generated messages only.
984 * Up to generator to avoid total mbuf resource exhaustion.
985 */
986 (void)sbprio;
987
988 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
989 panic("sbappendaddrchain");
990
991 space = sbspace(sb);
992
993 #ifdef notyet
994 /*
995 * Enforce SB_PRIO_* limits as described above.
996 */
997 #endif
998
999 n0 = NULL;
1000 nlast = NULL;
1001 for (m = m0; m; m = m->m_nextpkt) {
1002 struct mbuf *np;
1003
1004 #ifdef MBUFTRACE
1005 m_claimm(m, sb->sb_mowner);
1006 #endif
1007
1008 /* Prepend sockaddr to this record (m) of input chain m0 */
1009 n = m_prepend_sockaddr(sb, m, asa);
1010 if (n == NULL) {
1011 error = ENOBUFS;
1012 goto bad;
1013 }
1014
1015 /* Append record (asa+m) to end of new chain n0 */
1016 if (n0 == NULL) {
1017 n0 = n;
1018 } else {
1019 nlast->m_nextpkt = n;
1020 }
1021 /* Keep track of last record on new chain */
1022 nlast = n;
1023
1024 for (np = n; np; np = np->m_next)
1025 sballoc(sb, np);
1026 }
1027
1028 SBLASTRECORDCHK(sb, "sbappendaddrchain 1");
1029
1030 /* Drop the entire chain of (asa+m) records onto the socket */
1031 SBLINKRECORDCHAIN(sb, n0, nlast);
1032
1033 SBLASTRECORDCHK(sb, "sbappendaddrchain 2");
1034
1035 for (m = nlast; m->m_next; m = m->m_next)
1036 ;
1037 sb->sb_mbtail = m;
1038 SBLASTMBUFCHK(sb, "sbappendaddrchain");
1039
1040 return (1);
1041
1042 bad:
1043 /*
1044 * On error, free the prepended addreseses. For consistency
1045 * with sbappendaddr(), leave it to our caller to free
1046 * the input record chain passed to us as m0.
1047 */
1048 while ((n = n0) != NULL) {
1049 struct mbuf *np;
1050
1051 /* Undo the sballoc() of this record */
1052 for (np = n; np; np = np->m_next)
1053 sbfree(sb, np);
1054
1055 n0 = n->m_nextpkt; /* iterate at next prepended address */
1056 MFREE(n, np); /* free prepended address (not data) */
1057 }
1058 return 0;
1059 }
1060
1061
1062 int
1063 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
1064 {
1065 struct mbuf *m, *mlast, *n;
1066 int space;
1067
1068 KASSERT(solocked(sb->sb_so));
1069
1070 space = 0;
1071 if (control == 0)
1072 panic("sbappendcontrol");
1073 for (m = control; ; m = m->m_next) {
1074 space += m->m_len;
1075 MCLAIM(m, sb->sb_mowner);
1076 if (m->m_next == 0)
1077 break;
1078 }
1079 n = m; /* save pointer to last control buffer */
1080 for (m = m0; m; m = m->m_next) {
1081 MCLAIM(m, sb->sb_mowner);
1082 space += m->m_len;
1083 }
1084 if (space > sbspace(sb))
1085 return (0);
1086 n->m_next = m0; /* concatenate data to control */
1087
1088 SBLASTRECORDCHK(sb, "sbappendcontrol 1");
1089
1090 for (m = control; m->m_next != NULL; m = m->m_next)
1091 sballoc(sb, m);
1092 sballoc(sb, m);
1093 mlast = m;
1094 SBLINKRECORD(sb, control);
1095
1096 sb->sb_mbtail = mlast;
1097 SBLASTMBUFCHK(sb, "sbappendcontrol");
1098 SBLASTRECORDCHK(sb, "sbappendcontrol 2");
1099
1100 return (1);
1101 }
1102
1103 /*
1104 * Compress mbuf chain m into the socket
1105 * buffer sb following mbuf n. If n
1106 * is null, the buffer is presumed empty.
1107 */
1108 void
1109 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1110 {
1111 int eor;
1112 struct mbuf *o;
1113
1114 KASSERT(solocked(sb->sb_so));
1115
1116 eor = 0;
1117 while (m) {
1118 eor |= m->m_flags & M_EOR;
1119 if (m->m_len == 0 &&
1120 (eor == 0 ||
1121 (((o = m->m_next) || (o = n)) &&
1122 o->m_type == m->m_type))) {
1123 if (sb->sb_lastrecord == m)
1124 sb->sb_lastrecord = m->m_next;
1125 m = m_free(m);
1126 continue;
1127 }
1128 if (n && (n->m_flags & M_EOR) == 0 &&
1129 /* M_TRAILINGSPACE() checks buffer writeability */
1130 m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */
1131 m->m_len <= M_TRAILINGSPACE(n) &&
1132 n->m_type == m->m_type) {
1133 memcpy(mtod(n, char *) + n->m_len, mtod(m, void *),
1134 (unsigned)m->m_len);
1135 n->m_len += m->m_len;
1136 sb->sb_cc += m->m_len;
1137 m = m_free(m);
1138 continue;
1139 }
1140 if (n)
1141 n->m_next = m;
1142 else
1143 sb->sb_mb = m;
1144 sb->sb_mbtail = m;
1145 sballoc(sb, m);
1146 n = m;
1147 m->m_flags &= ~M_EOR;
1148 m = m->m_next;
1149 n->m_next = 0;
1150 }
1151 if (eor) {
1152 if (n)
1153 n->m_flags |= eor;
1154 else
1155 printf("semi-panic: sbcompress\n");
1156 }
1157 SBLASTMBUFCHK(sb, __func__);
1158 }
1159
1160 /*
1161 * Free all mbufs in a sockbuf.
1162 * Check that all resources are reclaimed.
1163 */
1164 void
1165 sbflush(struct sockbuf *sb)
1166 {
1167
1168 KASSERT(solocked(sb->sb_so));
1169 KASSERT((sb->sb_flags & SB_LOCK) == 0);
1170
1171 while (sb->sb_mbcnt)
1172 sbdrop(sb, (int)sb->sb_cc);
1173
1174 KASSERT(sb->sb_cc == 0);
1175 KASSERT(sb->sb_mb == NULL);
1176 KASSERT(sb->sb_mbtail == NULL);
1177 KASSERT(sb->sb_lastrecord == NULL);
1178 }
1179
1180 /*
1181 * Drop data from (the front of) a sockbuf.
1182 */
1183 void
1184 sbdrop(struct sockbuf *sb, int len)
1185 {
1186 struct mbuf *m, *mn, *next;
1187
1188 KASSERT(solocked(sb->sb_so));
1189
1190 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1191 while (len > 0) {
1192 if (m == 0) {
1193 if (next == 0)
1194 panic("sbdrop");
1195 m = next;
1196 next = m->m_nextpkt;
1197 continue;
1198 }
1199 if (m->m_len > len) {
1200 m->m_len -= len;
1201 m->m_data += len;
1202 sb->sb_cc -= len;
1203 break;
1204 }
1205 len -= m->m_len;
1206 sbfree(sb, m);
1207 MFREE(m, mn);
1208 m = mn;
1209 }
1210 while (m && m->m_len == 0) {
1211 sbfree(sb, m);
1212 MFREE(m, mn);
1213 m = mn;
1214 }
1215 if (m) {
1216 sb->sb_mb = m;
1217 m->m_nextpkt = next;
1218 } else
1219 sb->sb_mb = next;
1220 /*
1221 * First part is an inline SB_EMPTY_FIXUP(). Second part
1222 * makes sure sb_lastrecord is up-to-date if we dropped
1223 * part of the last record.
1224 */
1225 m = sb->sb_mb;
1226 if (m == NULL) {
1227 sb->sb_mbtail = NULL;
1228 sb->sb_lastrecord = NULL;
1229 } else if (m->m_nextpkt == NULL)
1230 sb->sb_lastrecord = m;
1231 }
1232
1233 /*
1234 * Drop a record off the front of a sockbuf
1235 * and move the next record to the front.
1236 */
1237 void
1238 sbdroprecord(struct sockbuf *sb)
1239 {
1240 struct mbuf *m, *mn;
1241
1242 KASSERT(solocked(sb->sb_so));
1243
1244 m = sb->sb_mb;
1245 if (m) {
1246 sb->sb_mb = m->m_nextpkt;
1247 do {
1248 sbfree(sb, m);
1249 MFREE(m, mn);
1250 } while ((m = mn) != NULL);
1251 }
1252 SB_EMPTY_FIXUP(sb);
1253 }
1254
1255 /*
1256 * Create a "control" mbuf containing the specified data
1257 * with the specified type for presentation on a socket buffer.
1258 */
1259 struct mbuf *
1260 sbcreatecontrol(void *p, int size, int type, int level)
1261 {
1262 struct cmsghdr *cp;
1263 struct mbuf *m;
1264
1265 if (CMSG_SPACE(size) > MCLBYTES) {
1266 printf("sbcreatecontrol: message too large %d\n", size);
1267 return NULL;
1268 }
1269
1270 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
1271 return ((struct mbuf *) NULL);
1272 if (CMSG_SPACE(size) > MLEN) {
1273 MCLGET(m, M_DONTWAIT);
1274 if ((m->m_flags & M_EXT) == 0) {
1275 m_free(m);
1276 return NULL;
1277 }
1278 }
1279 cp = mtod(m, struct cmsghdr *);
1280 memcpy(CMSG_DATA(cp), p, size);
1281 m->m_len = CMSG_SPACE(size);
1282 cp->cmsg_len = CMSG_LEN(size);
1283 cp->cmsg_level = level;
1284 cp->cmsg_type = type;
1285 return (m);
1286 }
1287
1288 void
1289 solockretry(struct socket *so, kmutex_t *lock)
1290 {
1291
1292 while (lock != so->so_lock) {
1293 mutex_exit(lock);
1294 lock = so->so_lock;
1295 mutex_enter(lock);
1296 }
1297 }
1298
1299 bool
1300 solocked(struct socket *so)
1301 {
1302
1303 return mutex_owned(so->so_lock);
1304 }
1305
1306 bool
1307 solocked2(struct socket *so1, struct socket *so2)
1308 {
1309 kmutex_t *lock;
1310
1311 lock = so1->so_lock;
1312 if (lock != so2->so_lock)
1313 return false;
1314 return mutex_owned(lock);
1315 }
1316
1317 /*
1318 * Assign a default lock to a new socket. For PRU_ATTACH, and done by
1319 * protocols that do not have special locking requirements.
1320 */
1321 void
1322 sosetlock(struct socket *so)
1323 {
1324 kmutex_t *lock;
1325
1326 if (so->so_lock == NULL) {
1327 lock = softnet_lock;
1328 so->so_lock = lock;
1329 mutex_obj_hold(lock);
1330 mutex_enter(lock);
1331 }
1332
1333 /* In all cases, lock must be held on return from PRU_ATTACH. */
1334 KASSERT(solocked(so));
1335 }
1336
1337 /*
1338 * Set lock on sockbuf sb; sleep if lock is already held.
1339 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1340 * Returns error without lock if sleep is interrupted.
1341 */
1342 int
1343 sblock(struct sockbuf *sb, int wf)
1344 {
1345 struct socket *so;
1346 kmutex_t *lock;
1347 int error;
1348
1349 KASSERT(solocked(sb->sb_so));
1350
1351 for (;;) {
1352 if (__predict_true((sb->sb_flags & SB_LOCK) == 0)) {
1353 sb->sb_flags |= SB_LOCK;
1354 return 0;
1355 }
1356 if (wf != M_WAITOK)
1357 return EWOULDBLOCK;
1358 so = sb->sb_so;
1359 lock = so->so_lock;
1360 if ((sb->sb_flags & SB_NOINTR) != 0) {
1361 cv_wait(&so->so_cv, lock);
1362 error = 0;
1363 } else
1364 error = cv_wait_sig(&so->so_cv, lock);
1365 if (__predict_false(lock != so->so_lock))
1366 solockretry(so, lock);
1367 if (error != 0)
1368 return error;
1369 }
1370 }
1371
1372 void
1373 sbunlock(struct sockbuf *sb)
1374 {
1375 struct socket *so;
1376
1377 so = sb->sb_so;
1378
1379 KASSERT(solocked(so));
1380 KASSERT((sb->sb_flags & SB_LOCK) != 0);
1381
1382 sb->sb_flags &= ~SB_LOCK;
1383 cv_broadcast(&so->so_cv);
1384 }
1385
1386 int
1387 sowait(struct socket *so, int timo)
1388 {
1389 kmutex_t *lock;
1390 int error;
1391
1392 KASSERT(solocked(so));
1393
1394 lock = so->so_lock;
1395 error = cv_timedwait_sig(&so->so_cv, lock, timo);
1396 if (__predict_false(lock != so->so_lock))
1397 solockretry(so, lock);
1398 return error;
1399 }
1400