proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / kern / uipc_socket2.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.17 2002/08/31 19:04:55 dwmalone Exp $
dadab5e9 35 * $DragonFly: src/sys/kern/uipc_socket2.c,v 1.3 2003/06/25 03:55:57 dillon Exp $
984263bc
MD
36 */
37
38#include "opt_param.h"
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/domain.h>
42#include <sys/file.h> /* for maxfiles */
43#include <sys/kernel.h>
44#include <sys/proc.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/protosw.h>
48#include <sys/resourcevar.h>
49#include <sys/stat.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/signalvar.h>
53#include <sys/sysctl.h>
54#include <sys/aio.h> /* for aio_swake proto */
55#include <sys/event.h>
56
57int maxsockets;
58
59/*
60 * Primitive routines for operating on sockets and socket buffers
61 */
62
63u_long sb_max = SB_MAX;
64u_long sb_max_adj =
65 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
66
67static u_long sb_efficiency = 8; /* parameter for sbreserve() */
68
69/*
70 * Procedures to manipulate state flags of socket
71 * and do appropriate wakeups. Normal sequence from the
72 * active (originating) side is that soisconnecting() is
73 * called during processing of connect() call,
74 * resulting in an eventual call to soisconnected() if/when the
75 * connection is established. When the connection is torn down
76 * soisdisconnecting() is called during processing of disconnect() call,
77 * and soisdisconnected() is called when the connection to the peer
78 * is totally severed. The semantics of these routines are such that
79 * connectionless protocols can call soisconnected() and soisdisconnected()
80 * only, bypassing the in-progress calls when setting up a ``connection''
81 * takes no time.
82 *
83 * From the passive side, a socket is created with
84 * two queues of sockets: so_incomp for connections in progress
85 * and so_comp for connections already made and awaiting user acceptance.
86 * As a protocol is preparing incoming connections, it creates a socket
87 * structure queued on so_incomp by calling sonewconn(). When the connection
88 * is established, soisconnected() is called, and transfers the
89 * socket structure to so_comp, making it available to accept().
90 *
91 * If a socket is closed with sockets on either
92 * so_incomp or so_comp, these sockets are dropped.
93 *
94 * If higher level protocols are implemented in
95 * the kernel, the wakeups done here will sometimes
96 * cause software-interrupt process scheduling.
97 */
98
99void
100soisconnecting(so)
101 register struct socket *so;
102{
103
104 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
105 so->so_state |= SS_ISCONNECTING;
106}
107
108void
109soisconnected(so)
110 struct socket *so;
111{
112 struct socket *head = so->so_head;
113
114 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
115 so->so_state |= SS_ISCONNECTED;
116 if (head && (so->so_state & SS_INCOMP)) {
117 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
118 so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
119 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
120 so->so_rcv.sb_flags |= SB_UPCALL;
121 so->so_options &= ~SO_ACCEPTFILTER;
122 so->so_upcall(so, so->so_upcallarg, 0);
123 return;
124 }
125 TAILQ_REMOVE(&head->so_incomp, so, so_list);
126 head->so_incqlen--;
127 so->so_state &= ~SS_INCOMP;
128 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
129 head->so_qlen++;
130 so->so_state |= SS_COMP;
131 sorwakeup(head);
132 wakeup_one(&head->so_timeo);
133 } else {
134 wakeup(&so->so_timeo);
135 sorwakeup(so);
136 sowwakeup(so);
137 }
138}
139
140void
141soisdisconnecting(so)
142 register struct socket *so;
143{
144
145 so->so_state &= ~SS_ISCONNECTING;
146 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
147 wakeup((caddr_t)&so->so_timeo);
148 sowwakeup(so);
149 sorwakeup(so);
150}
151
152void
153soisdisconnected(so)
154 register struct socket *so;
155{
156
157 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
158 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
159 wakeup((caddr_t)&so->so_timeo);
160 sbdrop(&so->so_snd, so->so_snd.sb_cc);
161 sowwakeup(so);
162 sorwakeup(so);
163}
164
165/*
166 * When an attempt at a new connection is noted on a socket
167 * which accepts connections, sonewconn is called. If the
168 * connection is possible (subject to space constraints, etc.)
169 * then we allocate a new structure, propoerly linked into the
170 * data structure of the original socket, and return this.
171 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
172 */
173struct socket *
dadab5e9 174sonewconn(struct socket *head, int connstatus)
984263bc
MD
175{
176 register struct socket *so;
177
178 if (head->so_qlen > 3 * head->so_qlimit / 2)
179 return ((struct socket *)0);
180 so = soalloc(0);
181 if (so == NULL)
182 return ((struct socket *)0);
183 if ((head->so_options & SO_ACCEPTFILTER) != 0)
184 connstatus = 0;
185 so->so_head = head;
186 so->so_type = head->so_type;
187 so->so_options = head->so_options &~ SO_ACCEPTCONN;
188 so->so_linger = head->so_linger;
189 so->so_state = head->so_state | SS_NOFDREF;
190 so->so_proto = head->so_proto;
191 so->so_timeo = head->so_timeo;
dadab5e9 192 so->so_cred = crhold(head->so_cred);
984263bc
MD
193 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
194 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
195 sodealloc(so);
196 return ((struct socket *)0);
197 }
198
199 if (connstatus) {
200 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
201 so->so_state |= SS_COMP;
202 head->so_qlen++;
203 } else {
204 if (head->so_incqlen > head->so_qlimit) {
205 struct socket *sp;
206 sp = TAILQ_FIRST(&head->so_incomp);
207 (void) soabort(sp);
208 }
209 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
210 so->so_state |= SS_INCOMP;
211 head->so_incqlen++;
212 }
213 if (connstatus) {
214 sorwakeup(head);
215 wakeup((caddr_t)&head->so_timeo);
216 so->so_state |= connstatus;
217 }
218 return (so);
219}
220
221/*
222 * Socantsendmore indicates that no more data will be sent on the
223 * socket; it would normally be applied to a socket when the user
224 * informs the system that no more data is to be sent, by the protocol
225 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
226 * will be received, and will normally be applied to the socket by a
227 * protocol when it detects that the peer will send no more data.
228 * Data queued for reading in the socket may yet be read.
229 */
230
231void
232socantsendmore(so)
233 struct socket *so;
234{
235
236 so->so_state |= SS_CANTSENDMORE;
237 sowwakeup(so);
238}
239
240void
241socantrcvmore(so)
242 struct socket *so;
243{
244
245 so->so_state |= SS_CANTRCVMORE;
246 sorwakeup(so);
247}
248
249/*
250 * Wait for data to arrive at/drain from a socket buffer.
251 */
252int
253sbwait(sb)
254 struct sockbuf *sb;
255{
256
257 sb->sb_flags |= SB_WAIT;
258 return (tsleep((caddr_t)&sb->sb_cc,
259 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
260 sb->sb_timeo));
261}
262
263/*
264 * Lock a sockbuf already known to be locked;
265 * return any error returned from sleep (EINTR).
266 */
267int
268sb_lock(sb)
269 register struct sockbuf *sb;
270{
271 int error;
272
273 while (sb->sb_flags & SB_LOCK) {
274 sb->sb_flags |= SB_WANT;
275 error = tsleep((caddr_t)&sb->sb_flags,
276 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
277 "sblock", 0);
278 if (error)
279 return (error);
280 }
281 sb->sb_flags |= SB_LOCK;
282 return (0);
283}
284
285/*
286 * Wakeup processes waiting on a socket buffer.
287 * Do asynchronous notification via SIGIO
288 * if the socket has the SS_ASYNC flag set.
289 */
290void
291sowakeup(so, sb)
292 register struct socket *so;
293 register struct sockbuf *sb;
294{
295 selwakeup(&sb->sb_sel);
296 sb->sb_flags &= ~SB_SEL;
297 if (sb->sb_flags & SB_WAIT) {
298 sb->sb_flags &= ~SB_WAIT;
299 wakeup((caddr_t)&sb->sb_cc);
300 }
301 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
302 pgsigio(so->so_sigio, SIGIO, 0);
303 if (sb->sb_flags & SB_UPCALL)
304 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
305 if (sb->sb_flags & SB_AIO)
306 aio_swake(so, sb);
307 KNOTE(&sb->sb_sel.si_note, 0);
308}
309
310/*
311 * Socket buffer (struct sockbuf) utility routines.
312 *
313 * Each socket contains two socket buffers: one for sending data and
314 * one for receiving data. Each buffer contains a queue of mbufs,
315 * information about the number of mbufs and amount of data in the
316 * queue, and other fields allowing select() statements and notification
317 * on data availability to be implemented.
318 *
319 * Data stored in a socket buffer is maintained as a list of records.
320 * Each record is a list of mbufs chained together with the m_next
321 * field. Records are chained together with the m_nextpkt field. The upper
322 * level routine soreceive() expects the following conventions to be
323 * observed when placing information in the receive buffer:
324 *
325 * 1. If the protocol requires each message be preceded by the sender's
326 * name, then a record containing that name must be present before
327 * any associated data (mbuf's must be of type MT_SONAME).
328 * 2. If the protocol supports the exchange of ``access rights'' (really
329 * just additional data associated with the message), and there are
330 * ``rights'' to be received, then a record containing this data
331 * should be present (mbuf's must be of type MT_RIGHTS).
332 * 3. If a name or rights record exists, then it must be followed by
333 * a data record, perhaps of zero length.
334 *
335 * Before using a new socket structure it is first necessary to reserve
336 * buffer space to the socket, by calling sbreserve(). This should commit
337 * some of the available buffer space in the system buffer pool for the
338 * socket (currently, it does nothing but enforce limits). The space
339 * should be released by calling sbrelease() when the socket is destroyed.
340 */
341
342int
343soreserve(so, sndcc, rcvcc)
344 register struct socket *so;
345 u_long sndcc, rcvcc;
346{
347 struct proc *p = curproc;
348
349 if (sbreserve(&so->so_snd, sndcc, so, p) == 0)
350 goto bad;
351 if (sbreserve(&so->so_rcv, rcvcc, so, p) == 0)
352 goto bad2;
353 if (so->so_rcv.sb_lowat == 0)
354 so->so_rcv.sb_lowat = 1;
355 if (so->so_snd.sb_lowat == 0)
356 so->so_snd.sb_lowat = MCLBYTES;
357 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
358 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
359 return (0);
360bad2:
361 sbrelease(&so->so_snd, so);
362bad:
363 return (ENOBUFS);
364}
365
366static int
367sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
368{
369 int error = 0;
370 u_long old_sb_max = sb_max;
371
372 error = SYSCTL_OUT(req, arg1, sizeof(int));
373 if (error || !req->newptr)
374 return (error);
375 error = SYSCTL_IN(req, arg1, sizeof(int));
376 if (error)
377 return (error);
378 if (sb_max < MSIZE + MCLBYTES) {
379 sb_max = old_sb_max;
380 return (EINVAL);
381 }
382 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
383 return (0);
384}
385
386/*
387 * Allot mbufs to a sockbuf.
388 * Attempt to scale mbmax so that mbcnt doesn't become limiting
389 * if buffering efficiency is near the normal case.
390 */
391int
392sbreserve(sb, cc, so, p)
393 struct sockbuf *sb;
394 u_long cc;
395 struct socket *so;
396 struct proc *p;
397{
398
399 /*
400 * p will only be NULL when we're in an interrupt
401 * (e.g. in tcp_input())
402 */
403 if (cc > sb_max_adj)
404 return (0);
405 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
406 p ? p->p_rlimit[RLIMIT_SBSIZE].rlim_cur : RLIM_INFINITY)) {
407 return (0);
408 }
409 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
410 if (sb->sb_lowat > sb->sb_hiwat)
411 sb->sb_lowat = sb->sb_hiwat;
412 return (1);
413}
414
415/*
416 * Free mbufs held by a socket, and reserved mbuf space.
417 */
418void
419sbrelease(sb, so)
420 struct sockbuf *sb;
421 struct socket *so;
422{
423
424 sbflush(sb);
425 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
426 RLIM_INFINITY);
427 sb->sb_mbmax = 0;
428}
429
430/*
431 * Routines to add and remove
432 * data from an mbuf queue.
433 *
434 * The routines sbappend() or sbappendrecord() are normally called to
435 * append new mbufs to a socket buffer, after checking that adequate
436 * space is available, comparing the function sbspace() with the amount
437 * of data to be added. sbappendrecord() differs from sbappend() in
438 * that data supplied is treated as the beginning of a new record.
439 * To place a sender's address, optional access rights, and data in a
440 * socket receive buffer, sbappendaddr() should be used. To place
441 * access rights and data in a socket receive buffer, sbappendrights()
442 * should be used. In either case, the new data begins a new record.
443 * Note that unlike sbappend() and sbappendrecord(), these routines check
444 * for the caller that there will be enough space to store the data.
445 * Each fails if there is not enough space, or if it cannot find mbufs
446 * to store additional information in.
447 *
448 * Reliable protocols may use the socket send buffer to hold data
449 * awaiting acknowledgement. Data is normally copied from a socket
450 * send buffer in a protocol with m_copy for output to a peer,
451 * and then removing the data from the socket buffer with sbdrop()
452 * or sbdroprecord() when the data is acknowledged by the peer.
453 */
454
455/*
456 * Append mbuf chain m to the last record in the
457 * socket buffer sb. The additional space associated
458 * the mbuf chain is recorded in sb. Empty mbufs are
459 * discarded and mbufs are compacted where possible.
460 */
461void
462sbappend(sb, m)
463 struct sockbuf *sb;
464 struct mbuf *m;
465{
466 register struct mbuf *n;
467
468 if (m == 0)
469 return;
470 n = sb->sb_mb;
471 if (n) {
472 while (n->m_nextpkt)
473 n = n->m_nextpkt;
474 do {
475 if (n->m_flags & M_EOR) {
476 sbappendrecord(sb, m); /* XXXXXX!!!! */
477 return;
478 }
479 } while (n->m_next && (n = n->m_next));
480 }
481 sbcompress(sb, m, n);
482}
483
484#ifdef SOCKBUF_DEBUG
485void
486sbcheck(sb)
487 register struct sockbuf *sb;
488{
489 register struct mbuf *m;
490 register struct mbuf *n = 0;
491 register u_long len = 0, mbcnt = 0;
492
493 for (m = sb->sb_mb; m; m = n) {
494 n = m->m_nextpkt;
495 for (; m; m = m->m_next) {
496 len += m->m_len;
497 mbcnt += MSIZE;
498 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
499 mbcnt += m->m_ext.ext_size;
500 }
501 }
502 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
503 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
504 mbcnt, sb->sb_mbcnt);
505 panic("sbcheck");
506 }
507}
508#endif
509
510/*
511 * As above, except the mbuf chain
512 * begins a new record.
513 */
514void
515sbappendrecord(sb, m0)
516 register struct sockbuf *sb;
517 register struct mbuf *m0;
518{
519 register struct mbuf *m;
520
521 if (m0 == 0)
522 return;
523 m = sb->sb_mb;
524 if (m)
525 while (m->m_nextpkt)
526 m = m->m_nextpkt;
527 /*
528 * Put the first mbuf on the queue.
529 * Note this permits zero length records.
530 */
531 sballoc(sb, m0);
532 if (m)
533 m->m_nextpkt = m0;
534 else
535 sb->sb_mb = m0;
536 m = m0->m_next;
537 m0->m_next = 0;
538 if (m && (m0->m_flags & M_EOR)) {
539 m0->m_flags &= ~M_EOR;
540 m->m_flags |= M_EOR;
541 }
542 sbcompress(sb, m, m0);
543}
544
545/*
546 * As above except that OOB data
547 * is inserted at the beginning of the sockbuf,
548 * but after any other OOB data.
549 */
550void
551sbinsertoob(sb, m0)
552 register struct sockbuf *sb;
553 register struct mbuf *m0;
554{
555 register struct mbuf *m;
556 register struct mbuf **mp;
557
558 if (m0 == 0)
559 return;
560 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
561 m = *mp;
562 again:
563 switch (m->m_type) {
564
565 case MT_OOBDATA:
566 continue; /* WANT next train */
567
568 case MT_CONTROL:
569 m = m->m_next;
570 if (m)
571 goto again; /* inspect THIS train further */
572 }
573 break;
574 }
575 /*
576 * Put the first mbuf on the queue.
577 * Note this permits zero length records.
578 */
579 sballoc(sb, m0);
580 m0->m_nextpkt = *mp;
581 *mp = m0;
582 m = m0->m_next;
583 m0->m_next = 0;
584 if (m && (m0->m_flags & M_EOR)) {
585 m0->m_flags &= ~M_EOR;
586 m->m_flags |= M_EOR;
587 }
588 sbcompress(sb, m, m0);
589}
590
591/*
592 * Append address and data, and optionally, control (ancillary) data
593 * to the receive queue of a socket. If present,
594 * m0 must include a packet header with total length.
595 * Returns 0 if no space in sockbuf or insufficient mbufs.
596 */
597int
598sbappendaddr(sb, asa, m0, control)
599 register struct sockbuf *sb;
600 struct sockaddr *asa;
601 struct mbuf *m0, *control;
602{
603 register struct mbuf *m, *n;
604 int space = asa->sa_len;
605
606if (m0 && (m0->m_flags & M_PKTHDR) == 0)
607panic("sbappendaddr");
608 if (m0)
609 space += m0->m_pkthdr.len;
610 for (n = control; n; n = n->m_next) {
611 space += n->m_len;
612 if (n->m_next == 0) /* keep pointer to last control buf */
613 break;
614 }
615 if (space > sbspace(sb))
616 return (0);
617 if (asa->sa_len > MLEN)
618 return (0);
619 MGET(m, M_DONTWAIT, MT_SONAME);
620 if (m == 0)
621 return (0);
622 m->m_len = asa->sa_len;
623 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
624 if (n)
625 n->m_next = m0; /* concatenate data to control */
626 else
627 control = m0;
628 m->m_next = control;
629 for (n = m; n; n = n->m_next)
630 sballoc(sb, n);
631 n = sb->sb_mb;
632 if (n) {
633 while (n->m_nextpkt)
634 n = n->m_nextpkt;
635 n->m_nextpkt = m;
636 } else
637 sb->sb_mb = m;
638 return (1);
639}
640
641int
642sbappendcontrol(sb, m0, control)
643 struct sockbuf *sb;
644 struct mbuf *control, *m0;
645{
646 register struct mbuf *m, *n;
647 int space = 0;
648
649 if (control == 0)
650 panic("sbappendcontrol");
651 for (m = control; ; m = m->m_next) {
652 space += m->m_len;
653 if (m->m_next == 0)
654 break;
655 }
656 n = m; /* save pointer to last control buffer */
657 for (m = m0; m; m = m->m_next)
658 space += m->m_len;
659 if (space > sbspace(sb))
660 return (0);
661 n->m_next = m0; /* concatenate data to control */
662 for (m = control; m; m = m->m_next)
663 sballoc(sb, m);
664 n = sb->sb_mb;
665 if (n) {
666 while (n->m_nextpkt)
667 n = n->m_nextpkt;
668 n->m_nextpkt = control;
669 } else
670 sb->sb_mb = control;
671 return (1);
672}
673
674/*
675 * Compress mbuf chain m into the socket
676 * buffer sb following mbuf n. If n
677 * is null, the buffer is presumed empty.
678 */
679void
680sbcompress(sb, m, n)
681 register struct sockbuf *sb;
682 register struct mbuf *m, *n;
683{
684 register int eor = 0;
685 register struct mbuf *o;
686
687 while (m) {
688 eor |= m->m_flags & M_EOR;
689 if (m->m_len == 0 &&
690 (eor == 0 ||
691 (((o = m->m_next) || (o = n)) &&
692 o->m_type == m->m_type))) {
693 m = m_free(m);
694 continue;
695 }
696 if (n && (n->m_flags & M_EOR) == 0 &&
697 M_WRITABLE(n) &&
698 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
699 m->m_len <= M_TRAILINGSPACE(n) &&
700 n->m_type == m->m_type) {
701 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
702 (unsigned)m->m_len);
703 n->m_len += m->m_len;
704 sb->sb_cc += m->m_len;
705 m = m_free(m);
706 continue;
707 }
708 if (n)
709 n->m_next = m;
710 else
711 sb->sb_mb = m;
712 sballoc(sb, m);
713 n = m;
714 m->m_flags &= ~M_EOR;
715 m = m->m_next;
716 n->m_next = 0;
717 }
718 if (eor) {
719 if (n)
720 n->m_flags |= eor;
721 else
722 printf("semi-panic: sbcompress\n");
723 }
724}
725
726/*
727 * Free all mbufs in a sockbuf.
728 * Check that all resources are reclaimed.
729 */
730void
731sbflush(sb)
732 register struct sockbuf *sb;
733{
734
735 if (sb->sb_flags & SB_LOCK)
736 panic("sbflush: locked");
737 while (sb->sb_mbcnt) {
738 /*
739 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
740 * we would loop forever. Panic instead.
741 */
742 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
743 break;
744 sbdrop(sb, (int)sb->sb_cc);
745 }
746 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
747 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
748}
749
750/*
751 * Drop data from (the front of) a sockbuf.
752 */
753void
754sbdrop(sb, len)
755 register struct sockbuf *sb;
756 register int len;
757{
758 register struct mbuf *m;
759 struct mbuf *next;
760
761 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
762 while (len > 0) {
763 if (m == 0) {
764 if (next == 0)
765 panic("sbdrop");
766 m = next;
767 next = m->m_nextpkt;
768 continue;
769 }
770 if (m->m_len > len) {
771 m->m_len -= len;
772 m->m_data += len;
773 sb->sb_cc -= len;
774 break;
775 }
776 len -= m->m_len;
777 sbfree(sb, m);
778 m = m_free(m);
779 }
780 while (m && m->m_len == 0) {
781 sbfree(sb, m);
782 m = m_free(m);
783 }
784 if (m) {
785 sb->sb_mb = m;
786 m->m_nextpkt = next;
787 } else
788 sb->sb_mb = next;
789}
790
791/*
792 * Drop a record off the front of a sockbuf
793 * and move the next record to the front.
794 */
795void
796sbdroprecord(sb)
797 register struct sockbuf *sb;
798{
799 register struct mbuf *m;
800
801 m = sb->sb_mb;
802 if (m) {
803 sb->sb_mb = m->m_nextpkt;
804 do {
805 sbfree(sb, m);
806 m = m_free(m);
807 } while (m);
808 }
809}
810
811/*
812 * Create a "control" mbuf containing the specified data
813 * with the specified type for presentation on a socket buffer.
814 */
815struct mbuf *
816sbcreatecontrol(p, size, type, level)
817 caddr_t p;
818 register int size;
819 int type, level;
820{
821 register struct cmsghdr *cp;
822 struct mbuf *m;
823
824 if (CMSG_SPACE((u_int)size) > MCLBYTES)
825 return ((struct mbuf *) NULL);
826 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
827 return ((struct mbuf *) NULL);
828 if (CMSG_SPACE((u_int)size) > MLEN) {
829 MCLGET(m, M_DONTWAIT);
830 if ((m->m_flags & M_EXT) == 0) {
831 m_free(m);
832 return ((struct mbuf *) NULL);
833 }
834 }
835 cp = mtod(m, struct cmsghdr *);
836 m->m_len = 0;
837 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
838 ("sbcreatecontrol: short mbuf"));
839 if (p != NULL)
840 (void)memcpy(CMSG_DATA(cp), p, size);
841 m->m_len = CMSG_SPACE(size);
842 cp->cmsg_len = CMSG_LEN(size);
843 cp->cmsg_level = level;
844 cp->cmsg_type = type;
845 return (m);
846}
847
848/*
849 * Some routines that return EOPNOTSUPP for entry points that are not
850 * supported by a protocol. Fill in as needed.
851 */
852int
853pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
854{
855 return EOPNOTSUPP;
856}
857
858int
dadab5e9 859pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
984263bc
MD
860{
861 return EOPNOTSUPP;
862}
863
864int
865pru_connect2_notsupp(struct socket *so1, struct socket *so2)
866{
867 return EOPNOTSUPP;
868}
869
870int
871pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
dadab5e9 872 struct ifnet *ifp, struct thread *td)
984263bc
MD
873{
874 return EOPNOTSUPP;
875}
876
877int
dadab5e9 878pru_listen_notsupp(struct socket *so, struct thread *td)
984263bc
MD
879{
880 return EOPNOTSUPP;
881}
882
883int
884pru_rcvd_notsupp(struct socket *so, int flags)
885{
886 return EOPNOTSUPP;
887}
888
889int
890pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
891{
892 return EOPNOTSUPP;
893}
894
895/*
896 * This isn't really a ``null'' operation, but it's the default one
897 * and doesn't do anything destructive.
898 */
899int
900pru_sense_null(struct socket *so, struct stat *sb)
901{
902 sb->st_blksize = so->so_snd.sb_hiwat;
903 return 0;
904}
905
906/*
907 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
908 */
909struct sockaddr *
910dup_sockaddr(sa, canwait)
911 struct sockaddr *sa;
912 int canwait;
913{
914 struct sockaddr *sa2;
915
916 MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME,
917 canwait ? M_WAITOK : M_NOWAIT);
918 if (sa2)
919 bcopy(sa, sa2, sa->sa_len);
920 return sa2;
921}
922
923/*
924 * Create an external-format (``xsocket'') structure using the information
925 * in the kernel-format socket structure pointed to by so. This is done
926 * to reduce the spew of irrelevant information over this interface,
927 * to isolate user code from changes in the kernel structure, and
928 * potentially to provide information-hiding if we decide that
929 * some of this information should be hidden from users.
930 */
931void
932sotoxsocket(struct socket *so, struct xsocket *xso)
933{
934 xso->xso_len = sizeof *xso;
935 xso->xso_so = so;
936 xso->so_type = so->so_type;
937 xso->so_options = so->so_options;
938 xso->so_linger = so->so_linger;
939 xso->so_state = so->so_state;
940 xso->so_pcb = so->so_pcb;
941 xso->xso_protocol = so->so_proto->pr_protocol;
942 xso->xso_family = so->so_proto->pr_domain->dom_family;
943 xso->so_qlen = so->so_qlen;
944 xso->so_incqlen = so->so_incqlen;
945 xso->so_qlimit = so->so_qlimit;
946 xso->so_timeo = so->so_timeo;
947 xso->so_error = so->so_error;
948 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
949 xso->so_oobmark = so->so_oobmark;
950 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
951 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
952 xso->so_uid = so->so_cred->cr_uid;
953}
954
955/*
956 * This does the same for sockbufs. Note that the xsockbuf structure,
957 * since it is always embedded in a socket, does not include a self
958 * pointer nor a length. We make this entry point public in case
959 * some other mechanism needs it.
960 */
961void
962sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
963{
964 xsb->sb_cc = sb->sb_cc;
965 xsb->sb_hiwat = sb->sb_hiwat;
966 xsb->sb_mbcnt = sb->sb_mbcnt;
967 xsb->sb_mbmax = sb->sb_mbmax;
968 xsb->sb_lowat = sb->sb_lowat;
969 xsb->sb_flags = sb->sb_flags;
970 xsb->sb_timeo = sb->sb_timeo;
971}
972
973/*
974 * Here is the definition of some of the basic objects in the kern.ipc
975 * branch of the MIB.
976 */
977SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
978
979/* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
980static int dummy;
981SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
982SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW,
983 &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size");
984SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
985 &maxsockets, 0, "Maximum number of sockets avaliable");
986SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
987 &sb_efficiency, 0, "");
988
989/*
990 * Initialise maxsockets
991 */
992static void init_maxsockets(void *ignored)
993{
994 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
995 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
996}
997SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);