2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1995 Apple Computer, Inc.
29 * Created, March 17, 1997 by Tuyen Nguyen for MacOSX.
32 #include <sys/errno.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <machine/spl.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/filedesc.h>
40 #include <sys/fcntl.h>
42 #include <sys/ioctl.h>
43 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/ioccom.h>
48 #include <sys/sysctl.h>
52 #include <netat/sysglue.h>
53 #include <netat/appletalk.h>
54 #include <netat/ddp.h>
55 #include <netat/at_pcb.h>
56 #include <netat/at_var.h>
57 #include <netat/routing_tables.h>
58 #include <netat/debug.h>
60 extern struct atpcb ddp_head
;
63 ddp_putmsg(gref_t
*gref
, gbuf_t
*m
),
64 elap_wput(gref_t
*gref
, gbuf_t
*m
),
65 atp_wput(gref_t
*gref
, gbuf_t
*m
),
66 asp_wput(gref_t
*gref
, gbuf_t
*m
),
68 aurp_wput(gref_t
*gref
, gbuf_t
*m
),
70 adsp_wput(gref_t
*gref
, gbuf_t
*m
);
72 int atp_free_cluster_timeout_set
= 0;
75 void atalk_putnext(gref_t
*gref
, gbuf_t
*m
);
76 /* bms: make gref_close non static so its callable from kernel */
77 int gref_close(gref_t
*gref
);
79 SYSCTL_DECL(_net_appletalk
);
81 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, debug
, CTLFLAG_WR
,
82 &dbgBits
, dbgBits
, "AppleTalk Debug Flags");
83 volatile int RouterMix
= RT_MIX_DEFAULT
; /* default for nbr of ppsec */
84 SYSCTL_INT(_net_appletalk
, OID_AUTO
, routermix
, CTLFLAG_WR
,
85 &RouterMix
, 0, "Appletalk RouterMix");
86 at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
87 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, ddpstats
, CTLFLAG_RD
,
88 &at_ddp_stats
, at_ddp_stats
, "AppleTalk DDP Stats");
92 caddr_t atp_free_cluster_list
= 0;
94 void gref_wput(gref
, m
)
98 switch (gref
->proto
) {
100 ddp_putmsg(gref
, m
); break;
102 elap_wput(gref
, m
); break;
104 atp_wput(gref
, m
); break;
106 asp_wput(gref
, m
); break;
109 aurp_wput(gref
, m
); break;
112 adsp_wput(gref
, m
); break;
114 if (gbuf_type(m
) == MSG_IOCTL
) {
115 gbuf_freem(gbuf_cont(m
));
117 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
118 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= EPROTO
;
119 gbuf_set_type(m
, MSG_IOCNAK
);
120 atalk_putnext(gref
, m
);
130 int _ATsocket(proto
, err
, proc
)
138 /* make sure the specified protocol id is valid */
141 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
142 BSD-style socket interface. */
151 #ifdef APPLETALK_DEBUG
152 kprintf("_ATsocket: error EPROTOTYPE =%d\n", *err
);
157 /* allocate a protocol channel */
158 if ((*err
= gref_alloc(&gref
)) != 0) {
159 #ifdef APPLETALK_DEBUG
160 kprintf("_ATsocket: error gref_open =%d\n", *err
);
165 gref
->pid
= ((struct proc
*)proc
)->p_pid
;
167 /* open the specified protocol */
168 switch (gref
->proto
) {
170 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
171 BSD-style socket interface. */
174 *err
= atp_open(gref
, 1); break;
176 *err
= asp_open(gref
); break;
179 *err
= aurp_open(gref
); break;
182 *err
= adsp_open(gref
); break;
185 /* create the descriptor for the channel */
187 #ifdef APPLETALK_DEBUG
188 kprintf("_ATsocket: open failed for %d proto; err = %d\n",
191 gref
->proto
= ATPROTO_NONE
;
193 if (*err
|| (*err
= atalk_openref(gref
, &fd
, proc
))) {
194 #ifdef APPLETALK_DEBUG
195 kprintf("_ATsocket: error atalk_openref =%d\n", *err
);
197 (void)gref_close(gref
);
201 kprintf("_ATsocket: proto=%d return=%d fd=%d\n", proto, *err, fd);
206 int _ATgetmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
217 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) == 0) {
218 switch (gref
->proto
) {
220 rc
= ASPgetmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
);
224 rc
= AURPgetmsg(err
);
228 *err
= EPROTONOSUPPORT
;
233 /* kprintf("_ATgetmsg: return=%d\n", *err);*/
237 int _ATputmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
248 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) == 0) {
249 switch (gref
->proto
) {
251 rc
= ASPputmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
); break;
253 *err
= EPROTONOSUPPORT
; break;
257 /* kprintf("_ATputmsg: return=%d\n", *err); */
261 int _ATclose(fp
, proc
)
268 if ((err
= atalk_closeref(fp
, &gref
)) == 0) {
269 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
270 (void)gref_close(gref
);
271 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
277 int _ATrw(fp
, rw
, uio
, ext
)
283 int s
, err
, len
, clen
= 0, res
;
285 gbuf_t
*m
, *mhead
, *mprev
;
287 if ((err
= atalk_getref(fp
, 0, &gref
, 0)) != 0)
290 if ((len
= uio
->uio_resid
) == 0)
293 ATDISABLE(s
, gref
->lock
);
295 if (rw
== UIO_READ
) {
296 KERNEL_DEBUG(DBG_ADSP_ATRW
, 0, gref
, len
, gref
->rdhead
, 0);
297 while ((gref
->errno
== 0) && ((mhead
= gref
->rdhead
) == 0)) {
298 gref
->sevents
|= POLLMSG
;
299 err
= tsleep(&gref
->event
, PSOCK
| PCATCH
, "AT read", 0);
300 gref
->sevents
&= ~POLLMSG
;
302 ATENABLE(s
, gref
->lock
);
305 KERNEL_DEBUG(DBG_ADSP_ATRW
, 1, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
309 ATENABLE(s
, gref
->lock
);
312 if ((gref
->rdhead
= gbuf_next(mhead
)) == 0)
315 KERNEL_DEBUG(DBG_ADSP_ATRW
, 2, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
317 ATENABLE(s
, gref
->lock
);
319 //##### LD TEST 08/05
320 // simple_lock(&gref->lock);
322 gbuf_next(mhead
) = 0;
324 for (mprev
=0, m
=mhead
; m
&& len
; len
-=clen
) {
325 if ((clen
= gbuf_len(m
)) > 0) {
328 uio
->uio_rw
= UIO_READ
;
329 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
),
331 KERNEL_DEBUG(DBG_ADSP_ATRW
, 3, m
, clen
,
335 if (gbuf_len(m
) > len
) {
344 KERNEL_DEBUG(DBG_ADSP_ATRW
, 4, m
, gbuf_len(m
), mprev
, gref
->rdhead
);
346 gbuf_cont(mprev
) = 0;
349 ATDISABLE(s
, gref
->lock
);
350 if (gref
->rdhead
== 0)
352 gbuf_next(m
) = gref
->rdhead
;
354 ATENABLE(s
, gref
->lock
);
359 // simple_unlock(&gref->lock);
361 if (gref
->writeable
) {
362 while (!(*gref
->writeable
)(gref
)) {
363 /* flow control on, wait to be enabled to write */
364 gref
->sevents
|= POLLSYNC
;
365 err
= tsleep(&gref
->event
, PSOCK
| PCATCH
, "AT write", 0);
366 gref
->sevents
&= ~POLLSYNC
;
368 ATENABLE(s
, gref
->lock
);
374 ATENABLE(s
, gref
->lock
);
376 /* allocate a buffer to copy in the write data */
377 if ((m
= gbuf_alloc(AT_WR_OFFSET
+len
, PRI_MED
)) == 0)
379 gbuf_rinc(m
,AT_WR_OFFSET
);
382 /* copy in the write data */
383 uio
->uio_rw
= UIO_WRITE
;
384 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
), len
, uio
))) {
385 #ifdef APPLETALK_DEBUG
386 kprintf("_ATrw: UIO_WRITE: res=%d\n", res
);
392 /* forward the write data to the appropriate protocol module */
399 int _ATread(fp
, uio
, cred
, flags
, p
)
408 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
409 stat
= _ATrw(fp
, UIO_READ
, uio
, 0);
410 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
414 int _ATwrite(fp
, uio
, cred
, flags
, p
)
424 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
425 stat
= _ATrw(fp
, UIO_WRITE
, uio
, 0);
426 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
431 /* Most of the processing from _ATioctl, so that it can be called
432 from the new ioctl code */
433 /* bms: update to be callable from kernel */
434 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
441 /* error if not for us */
442 if ((cmd
& 0xffff) != 0xff99)
445 /* copy in ioc command info */
447 kprintf("at_ioctl: arg ioccmd.ic_cmd=%x ic_len=%x gref->lock=%x, gref->event=%x\n",
448 ((ioccmd_t *)arg)->ic_cmd, ((ioccmd_t *)arg)->ic_len,
449 gref->lock, gref->event);
452 bcopy (arg
, &ioccmd
, sizeof (ioccmd_t
));
454 if ((err
= copyin((caddr_t
)arg
, (caddr_t
)&ioccmd
, sizeof(ioccmd_t
))) != 0) {
455 #ifdef APPLETALK_DEBUG
456 kprintf("at_ioctl: err = %d, copyin(%x, %x, %d)\n", err
,
457 (caddr_t
)arg
, (caddr_t
)&ioccmd
, sizeof(ioccmd_t
));
463 /* allocate a buffer to create an ioc command
464 first mbuf contains ioc command */
465 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
467 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
468 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
470 /* create the ioc command
471 second mbuf contains the actual ASP command */
473 if ((gbuf_cont(m
) = gbuf_alloc(ioccmd
.ic_len
, PRI_HI
)) == 0) {
475 #ifdef APPLETALK_DEBUG
476 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
480 gbuf_wset(gbuf_cont(m
), ioccmd
.ic_len
); /* mbuf->m_len */
482 bcopy (ioccmd
.ic_dp
, gbuf_rptr(gbuf_cont(m
)), ioccmd
.ic_len
);
484 if ((err
= copyin((caddr_t
)ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), ioccmd
.ic_len
)) != 0) {
490 ioc
= (ioc_t
*) gbuf_rptr(m
);
491 ioc
->ioc_cmd
= ioccmd
.ic_cmd
;
492 ioc
->ioc_count
= ioccmd
.ic_len
;
496 /* send the ioc command to the appropriate recipient */
499 /* wait for the ioc ack */
500 ATDISABLE(s
, gref
->lock
);
501 while ((m
= gref
->ichead
) == 0) {
502 gref
->sevents
|= POLLPRI
;
503 #ifdef APPLETALK_DEBUG
504 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
506 err
= tsleep(&gref
->iocevent
, PSOCK
| PCATCH
, "AT ioctl", 0);
507 gref
->sevents
&= ~POLLPRI
;
509 ATENABLE(s
, gref
->lock
);
510 #ifdef APPLETALK_DEBUG
511 kprintf("at_ioctl: EINTR\n");
518 if (gbuf_next(m
) == m
) /* error case */
521 gref
->ichead
= gbuf_next(m
);
523 ATENABLE(s
, gref
->lock
);
525 #ifdef APPLETALK_DEBUG
526 kprintf("at_ioctl: woke up from ioc sleep gref = 0x%x\n",
530 /* process the ioc response */
531 ioc
= (ioc_t
*) gbuf_rptr(m
);
532 if ((err
= ioc
->ioc_error
) == 0) {
533 ioccmd
.ic_timout
= ioc
->ioc_rval
;
535 mdata
= gbuf_cont(m
);
536 if (mdata
&& ioccmd
.ic_dp
) {
537 ioccmd
.ic_len
= gbuf_msgsize(mdata
);
538 for (len
= 0; mdata
; mdata
= gbuf_cont(mdata
)) {
540 bcopy (gbuf_rptr(mdata
), &ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
542 if ((err
= copyout((caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&ioccmd
.ic_dp
[len
], gbuf_len(mdata
))) < 0) {
543 #ifdef APPLETALK_DEBUG
544 kprintf("at_ioctl: len=%d error copyout=%d from=%x to=%x gbuf_len=%x\n",
545 len
, err
, (caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
550 len
+= gbuf_len(mdata
);
555 bcopy (&ioccmd
, arg
, sizeof(ioccmd_t
));
557 if ((err
= copyout((caddr_t
)&ioccmd
, (caddr_t
)arg
, sizeof(ioccmd_t
))) != 0) {
558 #ifdef APPLETALK_DEBUG
559 kprintf("at_ioctl: error copyout2=%d from=%x to=%x len=%d\n",
560 err
, &ioccmd
, arg
, sizeof(ioccmd_t
));
569 /*kprintf("at_ioctl: I_done=%d\n", err);*/
573 int _ATioctl(fp
, cmd
, arg
, proc
)
576 register caddr_t arg
;
582 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
583 if ((err
= atalk_getref(fp
, 0, &gref
, 0)) != 0) {
584 #ifdef APPLETALK_DEBUG
585 kprintf("_ATioctl: atalk_getref err = %d\n", err
);
589 err
= at_ioctl(gref
, cmd
, arg
, 0);
591 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
596 int _ATselect(fp
, which
, wql
, proc
)
605 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
606 err
= atalk_getref(fp
, 0, &gref
, 0);
607 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
612 ATDISABLE(s
, gref
->lock
);
613 if (which
== FREAD
) {
614 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
617 gref
->sevents
|= POLLIN
;
618 selrecord(proc
, &gref
->si
, wql
);
621 else if (which
== POLLOUT
) {
622 if (gref
->writeable
) {
623 if ((*gref
->writeable
)(gref
))
626 gref
->sevents
|= POLLOUT
;
627 selrecord(proc
, &gref
->si
, wql
);
632 ATENABLE(s
, gref
->lock
);
638 void atalk_putnext(gref
, m
)
644 ATDISABLE(s
, gref
->lock
);
646 /* *** potential leak? *** */
649 switch (gbuf_type(m
)) {
653 gbuf_next(gref
->ichead
) = m
;
656 if (gref
->sevents
& POLLPRI
) {
657 #ifdef APPLETALK_DEBUG
658 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
660 wakeup(&gref
->iocevent
);
665 /* *** this processing was moved to atalk_notify *** */
666 panic("atalk_putnext receved MSG_ERROR");
673 gbuf_next(gref
->rdtail
) = m
;
677 if (gref
->sevents
& POLLMSG
) {
678 gref
->sevents
&= ~POLLMSG
;
679 wakeup(&gref
->event
);
681 if (gref
->sevents
& POLLIN
) {
682 gref
->sevents
&= ~POLLIN
;
683 selwakeup(&gref
->si
);
687 } /* switch gbuf_type(m) */
689 ATENABLE(s
, gref
->lock
);
690 } /* atalk_putnext */
692 void atalk_enablew(gref
)
695 if (gref
->sevents
& POLLSYNC
)
696 wakeup(&gref
->event
);
699 void atalk_flush(gref
)
704 ATDISABLE(s
, gref
->lock
);
706 gbuf_freel(gref
->rdhead
);
710 gbuf_freel(gref
->ichead
);
713 ATENABLE(s
, gref
->lock
);
717 * Notify an appletalk user of an asynchronous error;
718 * just wake up so that he can collect error status.
720 void atalk_notify(gref
, errno
)
721 register gref_t
*gref
;
725 ATDISABLE(s
, gref
->lock
);
727 if (gref
->atpcb_socket
) {
729 This section is patterned after udp_notify() in
732 gref
->atpcb_socket
->so_error
= errno
;
733 sorwakeup(gref
->atpcb_socket
);
734 sowwakeup(gref
->atpcb_socket
);
736 /* for ATP, ASP, and ADSP */
737 if (gref
->errno
== 0) {
739 /* clear out data waiting to be read */
741 gbuf_freel(gref
->rdhead
);
745 if (gref
->sevents
& POLLMSG
) {
746 gref
->sevents
&= ~POLLMSG
;
747 wakeup(&gref
->event
);
750 if (gref
->sevents
& POLLIN
) {
751 gref
->sevents
&= ~POLLIN
;
752 selwakeup(&gref
->si
);
756 ATENABLE(s
, gref
->lock
);
759 void atalk_notify_sel(gref
)
764 ATDISABLE(s
, gref
->lock
);
765 if (gref
->sevents
& POLLIN
) {
766 gref
->sevents
&= ~POLLIN
;
767 selwakeup(&gref
->si
);
769 ATENABLE(s
, gref
->lock
);
772 int atalk_peek(gref
, event
)
774 unsigned char *event
;
778 ATDISABLE(s
, gref
->lock
);
780 *event
= *gbuf_rptr(gref
->rdhead
);
784 ATENABLE(s
, gref
->lock
);
789 static gbuf_t
*trace_msg
;
791 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
798 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
799 len
= strlen(trace_buf
);
800 #ifdef APPLETALK_DEBUG
801 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
803 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
806 strcpy(gbuf_rptr(m
), trace_buf
);
808 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
809 gbuf_cont(nextm
) = m
;
814 void atalk_gettrace(m
)
818 gbuf_cont(m
) = trace_msg
;
823 #define GREF_PER_BLK 32
824 static gref_t
*gref_free_list
= 0;
826 int gref_alloc(grefp
)
829 extern gbuf_t
*atp_resource_m
;
832 gref_t
*gref
, *gref_array
;
834 *grefp
= (gref_t
*)NULL
;
836 ATDISABLE(s
, refall_lock
);
837 if (gref_free_list
== 0) {
838 ATENABLE(s
, refall_lock
);
839 #ifdef APPLETALK_DEBUG
840 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
842 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
844 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
845 gref_array
= (gref_t
*)gbuf_rptr(m
);
846 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
847 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
848 ATDISABLE(s
, refall_lock
);
849 gbuf_cont(m
) = atp_resource_m
;
851 gref_array
[i
].atpcb_next
= gref_free_list
;
852 gref_free_list
= (gref_t
*)&gref_array
[0];
855 gref
= gref_free_list
;
856 gref_free_list
= gref
->atpcb_next
;
857 ATENABLE(s
, refall_lock
);
858 ATLOCKINIT(gref
->lock
);
859 //### LD Test 08/05/98
860 // simple_lock_init(&gref->lock);
861 ATEVENTINIT(gref
->event
);
862 ATEVENTINIT(gref
->iocevent
);
864 /* *** just for now *** */
865 gref
->atpcb_socket
= (struct socket
*)NULL
;
871 /* bms: make gref_close callable from kernel */
872 int gref_close(gref_t
*gref
)
876 switch (gref
->proto
) {
878 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
879 BSD-style socket interface. */
882 rc
= atp_close(gref
, 1); break;
884 rc
= asp_close(gref
); break;
887 rc
= aurp_close(gref
); break;
891 rc
= adsp_close(gref
); break;
899 selthreadclear(&gref
->si
);
901 /* from original gref_free() */
902 ATDISABLE(s
, refall_lock
);
903 bzero((char *)gref
, sizeof(gref_t
));
904 gref
->atpcb_next
= gref_free_list
;
905 gref_free_list
= gref
;
906 ATENABLE(s
, refall_lock
);
915 *** Some to be replaced with mbuf routines, some to be re-written
916 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
922 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
923 * "Allocates an mbuf structure and attaches an external cluster."
926 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
935 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
938 m
->m_ext
.ext_buf
= extbuf
;
939 m
->m_ext
.ext_free
= extfree
;
940 m
->m_ext
.ext_size
= extsize
;
941 m
->m_ext
.ext_arg
= extarg
;
942 m
->m_ext
.ext_refs
.forward
=
943 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
953 temp fix for bug 2731148 - until this code is re-written to use standard clusters
954 Deletes any free clusters on the free list.
956 void atp_delete_free_clusters()
959 caddr_t cluster_list
;
962 /* check for free clusters on the free_cluster_list to be deleted */
963 MBUF_LOCK(); /* lock used by mbuf routines */
965 untimeout(&atp_delete_free_clusters
, NULL
);
966 atp_free_cluster_timeout_set
= 0;
968 cluster_list
= atp_free_cluster_list
;
969 atp_free_cluster_list
= 0;
973 while (cluster
= cluster_list
)
975 cluster_list
= *((caddr_t
*)cluster
);
976 FREE(cluster
, M_MCLUST
);
983 Used as the "free" routine for over-size clusters allocated using
984 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
987 void m_lgbuf_free(buf
, size
, arg
)
989 int size
, arg
; /* not needed, but they're in m_free() */
991 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
993 /* move to free_cluster_list to be deleted later */
994 caddr_t cluster
= (caddr_t
)buf
;
996 /* don't need a lock because this is only called called from m_free which */
997 /* is under MBUF_LOCK */
998 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
999 atp_free_cluster_list
= cluster
;
1001 if (atp_free_cluster_timeout_set
== 0)
1003 atp_free_cluster_timeout_set
= 1;
1004 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1009 Used to allocate an mbuf when there is the possibility that it may
1010 need to be larger than the size of a standard cluster.
1013 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1018 if (atp_free_cluster_list
)
1019 atp_delete_free_clusters(); /* delete any free clusters on the free list */
1021 /* If size is too large, allocate a cluster, otherwise, use the
1022 standard mbuf allocation routines.*/
1023 if (size
> MCLBYTES
) {
1026 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1027 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1031 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1032 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1037 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1038 if (m
&& (size
> MHLEN
)) {
1039 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1040 if (!(m
->m_flags
& M_EXT
)) {
1048 } /* m_lgbuf_alloc */
1051 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1052 allocate an mbuf when there is the possibility that it may need
1053 to be larger than the size of a standard cluster.
1055 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1058 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1061 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1063 /* Standard mbuf allocation routines assume that the caller
1064 will set the size. */
1066 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1067 (struct mbuf
*)m
->m_len
= size
;
1078 for (size
=0; m
; m
=gbuf_cont(m
))
1079 size
+= gbuf_len(m
);
1083 int append_copy(m1
, m2
, wait
)
1084 struct mbuf
*m1
, *m2
;
1087 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1088 (m_trailingspace(m1
) >= m2
->m_len
)) {
1089 /* splat the data from one into the other */
1090 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1092 m1
->m_len
+= m2
->m_len
;
1093 if (m1
->m_flags
& M_PKTHDR
)
1094 m1
->m_pkthdr
.len
+= m2
->m_len
;
1097 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1098 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1104 Copy an mbuf chain, referencing existing external storage, if any.
1105 Leave space for a header in the new chain, if the space has been
1106 left in the origin chain.
1108 struct mbuf
*copy_pkt(mlist
, pad
)
1109 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1110 int pad
; /* hint as to how long the header might be
1111 If pad is < 0, leave the same amount of space
1112 as there was in the original. */
1118 len
= m_leadingspace(mlist
);
1120 len
= min(pad
, m_leadingspace(mlist
));
1122 /* preserve space for the header at the beginning of the mbuf */
1124 mlist
->m_data
-= (len
);
1125 mlist
->m_len
+= (len
);
1126 if (mlist
->m_flags
& M_PKTHDR
)
1127 mlist
->m_pkthdr
.len
+= (len
);
1128 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1132 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1137 void gbuf_linkb(m1
, m2
)
1141 while (gbuf_cont(m1
) != 0)
1146 void gbuf_linkpkt(m1
, m2
)
1150 while (gbuf_next(m1
) != 0)
1160 while ((tmp_m
= m
) != 0) {
1162 gbuf_next(tmp_m
) = 0;
1168 /* free empty mbufs at the front of the chain */
1169 gbuf_t
*gbuf_strip(m
)
1174 while (m
&& gbuf_len(m
) == 0) {
1182 /**************************************/
1184 int ddp_adjmsg(m
, len
)
1189 gbuf_t
*curr_m
, *prev_m
;
1191 if (m
== (gbuf_t
*)0)
1195 for (curr_m
=m
; curr_m
;) {
1196 buf_len
= gbuf_len(curr_m
);
1197 if (len
< buf_len
) {
1198 gbuf_rinc(curr_m
,len
);
1202 gbuf_rinc(curr_m
,buf_len
);
1203 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1209 } else if (len
< 0) {
1212 for (curr_m
=m
; gbuf_cont(curr_m
);
1213 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1214 buf_len
= gbuf_len(curr_m
);
1215 if (len
< buf_len
) {
1216 gbuf_wdec(curr_m
,len
);
1221 gbuf_cont(prev_m
) = 0;
1231 * The message chain, m is grown in size by len contiguous bytes.
1232 * If len is non-negative, len bytes are added to the
1233 * end of the gbuf_t chain. If len is negative, the
1234 * bytes are added to the front. ddp_growmsg only adds bytes to
1235 * message blocks of the same type.
1236 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1239 gbuf_t
*ddp_growmsg(mp
, len
)
1245 if ((m
= mp
) == (gbuf_t
*) 0)
1246 return ((gbuf_t
*) 0);
1250 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1251 return ((gbuf_t
*) 0);
1252 gbuf_set_type(d
, gbuf_type(m
));
1254 /* link in new gbuf_t */
1263 if ((count
= gbuf_msgsize(m
)) < 0)
1264 return ((gbuf_t
*) 0);
1265 /* find end of chain */
1266 for ( ; m
; m
= gbuf_cont(m
)) {
1267 if (gbuf_len(m
) >= count
)
1269 count
-= gbuf_len(m
);
1271 /* m now points to gbuf_t to add to */
1272 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1273 return ((gbuf_t
*) 0);
1274 gbuf_set_type(d
, gbuf_type(m
));
1275 /* link in new gbuf_t */
1276 gbuf_cont(d
) = gbuf_cont(m
);
1284 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1285 * block is used as the vehicle, and that if there is an error return,
1286 * then linked blocks are lopped off. BEWARE of multiple references.
1287 * Used by other appletalk modules, so it is not static!
1290 void ioc_ack(errno
, m
, gref
)
1293 register gref_t
*gref
;
1295 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1297 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1298 if ((iocbp
->ioc_error
= errno
) != 0)
1299 { /* errno != 0, then there is an error, get rid of linked blocks! */
1302 gbuf_freem(gbuf_cont(m
));
1305 gbuf_set_type(m
, MSG_IOCNAK
);
1306 iocbp
->ioc_count
= 0; /* only make zero length if error */
1307 iocbp
->ioc_rval
= -1;
1309 gbuf_set_type(m
, MSG_IOCACK
);
1311 atalk_putnext(gref
, m
);