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