2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1995 Apple Computer, Inc.
26 * Created, March 17, 1997 by Tuyen Nguyen for MacOSX.
29 #include <sys/errno.h>
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <machine/spl.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/filedesc.h>
37 #include <sys/fcntl.h>
39 #include <sys/ioctl.h>
40 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/ioccom.h>
45 #include <sys/sysctl.h>
49 #include <netat/sysglue.h>
50 #include <netat/appletalk.h>
51 #include <netat/ddp.h>
52 #include <netat/at_pcb.h>
53 #include <netat/at_var.h>
54 #include <netat/routing_tables.h>
55 #include <netat/debug.h>
57 extern struct atpcb ddp_head
;
60 ddp_putmsg(gref_t
*gref
, gbuf_t
*m
),
61 elap_wput(gref_t
*gref
, gbuf_t
*m
),
62 atp_wput(gref_t
*gref
, gbuf_t
*m
),
63 asp_wput(gref_t
*gref
, gbuf_t
*m
),
65 aurp_wput(gref_t
*gref
, gbuf_t
*m
),
67 adsp_wput(gref_t
*gref
, gbuf_t
*m
);
69 int atp_free_cluster_timeout_set
= 0;
72 void atalk_putnext(gref_t
*gref
, gbuf_t
*m
);
73 /* bms: make gref_close non static so its callable from kernel */
74 int gref_close(gref_t
*gref
);
76 SYSCTL_DECL(_net_appletalk
);
78 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, debug
, CTLFLAG_WR
,
79 &dbgBits
, dbgBits
, "AppleTalk Debug Flags");
80 volatile int RouterMix
= RT_MIX_DEFAULT
; /* default for nbr of ppsec */
81 SYSCTL_INT(_net_appletalk
, OID_AUTO
, routermix
, CTLFLAG_WR
,
82 &RouterMix
, 0, "Appletalk RouterMix");
83 at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
84 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, ddpstats
, CTLFLAG_RD
,
85 &at_ddp_stats
, at_ddp_stats
, "AppleTalk DDP Stats");
89 caddr_t atp_free_cluster_list
= 0;
91 void gref_wput(gref
, m
)
95 switch (gref
->proto
) {
97 ddp_putmsg(gref
, m
); break;
99 elap_wput(gref
, m
); break;
101 atp_wput(gref
, m
); break;
103 asp_wput(gref
, m
); break;
106 aurp_wput(gref
, m
); break;
109 adsp_wput(gref
, m
); break;
111 if (gbuf_type(m
) == MSG_IOCTL
) {
112 gbuf_freem(gbuf_cont(m
));
114 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
115 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= EPROTO
;
116 gbuf_set_type(m
, MSG_IOCNAK
);
117 atalk_putnext(gref
, m
);
127 int _ATsocket(proto
, err
, proc
)
135 /* make sure the specified protocol id is valid */
138 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
139 BSD-style socket interface. */
148 #ifdef APPLETALK_DEBUG
149 kprintf("_ATsocket: error EPROTOTYPE =%d\n", *err
);
154 /* allocate a protocol channel */
155 if ((*err
= gref_alloc(&gref
)) != 0) {
156 #ifdef APPLETALK_DEBUG
157 kprintf("_ATsocket: error gref_open =%d\n", *err
);
162 gref
->pid
= ((struct proc
*)proc
)->p_pid
;
164 /* open the specified protocol */
165 switch (gref
->proto
) {
167 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
168 BSD-style socket interface. */
171 *err
= atp_open(gref
, 1); break;
173 *err
= asp_open(gref
); break;
176 *err
= aurp_open(gref
); break;
179 *err
= adsp_open(gref
); break;
182 /* create the descriptor for the channel */
184 #ifdef APPLETALK_DEBUG
185 kprintf("_ATsocket: open failed for %d proto; err = %d\n",
188 gref
->proto
= ATPROTO_NONE
;
190 if (*err
|| (*err
= atalk_openref(gref
, &fd
, proc
))) {
191 #ifdef APPLETALK_DEBUG
192 kprintf("_ATsocket: error atalk_openref =%d\n", *err
);
194 (void)gref_close(gref
);
198 kprintf("_ATsocket: proto=%d return=%d fd=%d\n", proto, *err, fd);
203 int _ATgetmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
214 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) == 0) {
215 switch (gref
->proto
) {
217 rc
= ASPgetmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
);
221 rc
= AURPgetmsg(err
);
225 *err
= EPROTONOSUPPORT
;
230 /* kprintf("_ATgetmsg: return=%d\n", *err);*/
234 int _ATputmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
245 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) == 0) {
246 switch (gref
->proto
) {
248 rc
= ASPputmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
); break;
250 *err
= EPROTONOSUPPORT
; break;
254 /* kprintf("_ATputmsg: return=%d\n", *err); */
258 int _ATclose(fp
, proc
)
265 if ((err
= atalk_closeref(fp
, &gref
)) == 0) {
266 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
267 (void)gref_close(gref
);
268 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
274 int _ATrw(fp
, rw
, uio
, ext
)
280 int s
, err
, len
, clen
= 0, res
;
282 gbuf_t
*m
, *mhead
, *mprev
;
284 if ((err
= atalk_getref(fp
, 0, &gref
, 0)) != 0)
287 if ((len
= uio
->uio_resid
) == 0)
290 ATDISABLE(s
, gref
->lock
);
292 if (rw
== UIO_READ
) {
293 KERNEL_DEBUG(DBG_ADSP_ATRW
, 0, gref
, len
, gref
->rdhead
, 0);
294 while ((gref
->errno
== 0) && ((mhead
= gref
->rdhead
) == 0)) {
295 gref
->sevents
|= POLLMSG
;
296 err
= tsleep(&gref
->event
, PSOCK
| PCATCH
, "AT read", 0);
297 gref
->sevents
&= ~POLLMSG
;
299 ATENABLE(s
, gref
->lock
);
302 KERNEL_DEBUG(DBG_ADSP_ATRW
, 1, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
306 ATENABLE(s
, gref
->lock
);
309 if ((gref
->rdhead
= gbuf_next(mhead
)) == 0)
312 KERNEL_DEBUG(DBG_ADSP_ATRW
, 2, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
314 ATENABLE(s
, gref
->lock
);
316 //##### LD TEST 08/05
317 // simple_lock(&gref->lock);
319 gbuf_next(mhead
) = 0;
321 for (mprev
=0, m
=mhead
; m
&& len
; len
-=clen
) {
322 if ((clen
= gbuf_len(m
)) > 0) {
325 uio
->uio_rw
= UIO_READ
;
326 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
),
328 KERNEL_DEBUG(DBG_ADSP_ATRW
, 3, m
, clen
,
332 if (gbuf_len(m
) > len
) {
341 KERNEL_DEBUG(DBG_ADSP_ATRW
, 4, m
, gbuf_len(m
), mprev
, gref
->rdhead
);
343 gbuf_cont(mprev
) = 0;
346 ATDISABLE(s
, gref
->lock
);
347 if (gref
->rdhead
== 0)
349 gbuf_next(m
) = gref
->rdhead
;
351 ATENABLE(s
, gref
->lock
);
356 // simple_unlock(&gref->lock);
358 if (gref
->writeable
) {
359 while (!(*gref
->writeable
)(gref
)) {
360 /* flow control on, wait to be enabled to write */
361 gref
->sevents
|= POLLSYNC
;
362 err
= tsleep(&gref
->event
, PSOCK
| PCATCH
, "AT write", 0);
363 gref
->sevents
&= ~POLLSYNC
;
365 ATENABLE(s
, gref
->lock
);
371 ATENABLE(s
, gref
->lock
);
373 /* allocate a buffer to copy in the write data */
374 if ((m
= gbuf_alloc(AT_WR_OFFSET
+len
, PRI_MED
)) == 0)
376 gbuf_rinc(m
,AT_WR_OFFSET
);
379 /* copy in the write data */
380 uio
->uio_rw
= UIO_WRITE
;
381 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
), len
, uio
))) {
382 #ifdef APPLETALK_DEBUG
383 kprintf("_ATrw: UIO_WRITE: res=%d\n", res
);
389 /* forward the write data to the appropriate protocol module */
396 int _ATread(fp
, uio
, cred
)
403 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
404 stat
= _ATrw(fp
, UIO_READ
, uio
, 0);
405 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
409 int _ATwrite(fp
, uio
, cred
)
417 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
418 stat
= _ATrw(fp
, UIO_WRITE
, uio
, 0);
419 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
424 /* Most of the processing from _ATioctl, so that it can be called
425 from the new ioctl code */
426 /* bms: update to be callable from kernel */
427 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
434 /* error if not for us */
435 if ((cmd
& 0xffff) != 0xff99)
438 /* copy in ioc command info */
440 kprintf("at_ioctl: arg ioccmd.ic_cmd=%x ic_len=%x gref->lock=%x, gref->event=%x\n",
441 ((ioccmd_t *)arg)->ic_cmd, ((ioccmd_t *)arg)->ic_len,
442 gref->lock, gref->event);
445 bcopy (arg
, &ioccmd
, sizeof (ioccmd_t
));
447 if ((err
= copyin((caddr_t
)arg
, (caddr_t
)&ioccmd
, sizeof(ioccmd_t
))) != 0) {
448 #ifdef APPLETALK_DEBUG
449 kprintf("at_ioctl: err = %d, copyin(%x, %x, %d)\n", err
,
450 (caddr_t
)arg
, (caddr_t
)&ioccmd
, sizeof(ioccmd_t
));
456 /* allocate a buffer to create an ioc command
457 first mbuf contains ioc command */
458 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
460 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
461 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
463 /* create the ioc command
464 second mbuf contains the actual ASP command */
466 if ((gbuf_cont(m
) = gbuf_alloc(ioccmd
.ic_len
, PRI_HI
)) == 0) {
468 #ifdef APPLETALK_DEBUG
469 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
473 gbuf_wset(gbuf_cont(m
), ioccmd
.ic_len
); /* mbuf->m_len */
475 bcopy (ioccmd
.ic_dp
, gbuf_rptr(gbuf_cont(m
)), ioccmd
.ic_len
);
477 if ((err
= copyin((caddr_t
)ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), ioccmd
.ic_len
)) != 0) {
483 ioc
= (ioc_t
*) gbuf_rptr(m
);
484 ioc
->ioc_cmd
= ioccmd
.ic_cmd
;
485 ioc
->ioc_count
= ioccmd
.ic_len
;
489 /* send the ioc command to the appropriate recipient */
492 /* wait for the ioc ack */
493 ATDISABLE(s
, gref
->lock
);
494 while ((m
= gref
->ichead
) == 0) {
495 gref
->sevents
|= POLLPRI
;
496 #ifdef APPLETALK_DEBUG
497 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
499 err
= tsleep(&gref
->iocevent
, PSOCK
| PCATCH
, "AT ioctl", 0);
500 gref
->sevents
&= ~POLLPRI
;
502 ATENABLE(s
, gref
->lock
);
503 #ifdef APPLETALK_DEBUG
504 kprintf("at_ioctl: EINTR\n");
511 if (gbuf_next(m
) == m
) /* error case */
514 gref
->ichead
= gbuf_next(m
);
516 ATENABLE(s
, gref
->lock
);
518 #ifdef APPLETALK_DEBUG
519 kprintf("at_ioctl: woke up from ioc sleep gref = 0x%x\n",
523 /* process the ioc response */
524 ioc
= (ioc_t
*) gbuf_rptr(m
);
525 if ((err
= ioc
->ioc_error
) == 0) {
526 ioccmd
.ic_timout
= ioc
->ioc_rval
;
528 mdata
= gbuf_cont(m
);
529 if (mdata
&& ioccmd
.ic_dp
) {
530 ioccmd
.ic_len
= gbuf_msgsize(mdata
);
531 for (len
= 0; mdata
; mdata
= gbuf_cont(mdata
)) {
533 bcopy (gbuf_rptr(mdata
), &ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
535 if ((err
= copyout((caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&ioccmd
.ic_dp
[len
], gbuf_len(mdata
))) < 0) {
536 #ifdef APPLETALK_DEBUG
537 kprintf("at_ioctl: len=%d error copyout=%d from=%x to=%x gbuf_len=%x\n",
538 len
, err
, (caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
543 len
+= gbuf_len(mdata
);
548 bcopy (&ioccmd
, arg
, sizeof(ioccmd_t
));
550 if ((err
= copyout((caddr_t
)&ioccmd
, (caddr_t
)arg
, sizeof(ioccmd_t
))) != 0) {
551 #ifdef APPLETALK_DEBUG
552 kprintf("at_ioctl: error copyout2=%d from=%x to=%x len=%d\n",
553 err
, &ioccmd
, arg
, sizeof(ioccmd_t
));
562 /*kprintf("at_ioctl: I_done=%d\n", err);*/
566 int _ATioctl(fp
, cmd
, arg
, proc
)
569 register caddr_t arg
;
575 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
576 if ((err
= atalk_getref(fp
, 0, &gref
, 0)) != 0) {
577 #ifdef APPLETALK_DEBUG
578 kprintf("_ATioctl: atalk_getref err = %d\n", err
);
582 err
= at_ioctl(gref
, cmd
, arg
, 0);
584 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
589 int _ATselect(fp
, which
, wql
, proc
)
598 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
599 err
= atalk_getref(fp
, 0, &gref
, 0);
600 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
605 ATDISABLE(s
, gref
->lock
);
606 if (which
== FREAD
) {
607 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
610 gref
->sevents
|= POLLIN
;
611 selrecord(proc
, &gref
->si
, wql
);
614 else if (which
== POLLOUT
) {
615 if (gref
->writeable
) {
616 if ((*gref
->writeable
)(gref
))
619 gref
->sevents
|= POLLOUT
;
620 selrecord(proc
, &gref
->si
, wql
);
625 ATENABLE(s
, gref
->lock
);
631 void atalk_putnext(gref
, m
)
637 ATDISABLE(s
, gref
->lock
);
639 /* *** potential leak? *** */
642 switch (gbuf_type(m
)) {
646 gbuf_next(gref
->ichead
) = m
;
649 if (gref
->sevents
& POLLPRI
) {
650 #ifdef APPLETALK_DEBUG
651 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
653 thread_wakeup(&gref
->iocevent
);
658 /* *** this processing was moved to atalk_notify *** */
659 panic("atalk_putnext receved MSG_ERROR");
666 gbuf_next(gref
->rdtail
) = m
;
670 if (gref
->sevents
& POLLMSG
) {
671 gref
->sevents
&= ~POLLMSG
;
672 thread_wakeup(&gref
->event
);
674 if (gref
->sevents
& POLLIN
) {
675 gref
->sevents
&= ~POLLIN
;
676 selwakeup(&gref
->si
);
680 } /* switch gbuf_type(m) */
682 ATENABLE(s
, gref
->lock
);
683 } /* atalk_putnext */
685 void atalk_enablew(gref
)
688 if (gref
->sevents
& POLLSYNC
)
689 thread_wakeup(&gref
->event
);
692 void atalk_flush(gref
)
697 ATDISABLE(s
, gref
->lock
);
699 gbuf_freel(gref
->rdhead
);
703 gbuf_freel(gref
->ichead
);
706 ATENABLE(s
, gref
->lock
);
710 * Notify an appletalk user of an asynchronous error;
711 * just wake up so that he can collect error status.
713 void atalk_notify(gref
, errno
)
714 register gref_t
*gref
;
718 ATDISABLE(s
, gref
->lock
);
720 if (gref
->atpcb_socket
) {
722 This section is patterned after udp_notify() in
725 gref
->atpcb_socket
->so_error
= errno
;
726 sorwakeup(gref
->atpcb_socket
);
727 sowwakeup(gref
->atpcb_socket
);
729 /* for ATP, ASP, and ADSP */
730 if (gref
->errno
== 0) {
732 /* clear out data waiting to be read */
734 gbuf_freel(gref
->rdhead
);
738 if (gref
->sevents
& POLLMSG
) {
739 gref
->sevents
&= ~POLLMSG
;
740 thread_wakeup(&gref
->event
);
743 if (gref
->sevents
& POLLIN
) {
744 gref
->sevents
&= ~POLLIN
;
745 selwakeup(&gref
->si
);
749 ATENABLE(s
, gref
->lock
);
752 void atalk_notify_sel(gref
)
757 ATDISABLE(s
, gref
->lock
);
758 if (gref
->sevents
& POLLIN
) {
759 gref
->sevents
&= ~POLLIN
;
760 selwakeup(&gref
->si
);
762 ATENABLE(s
, gref
->lock
);
765 int atalk_peek(gref
, event
)
767 unsigned char *event
;
771 ATDISABLE(s
, gref
->lock
);
773 *event
= *gbuf_rptr(gref
->rdhead
);
777 ATENABLE(s
, gref
->lock
);
782 static gbuf_t
*trace_msg
;
784 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
791 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
792 len
= strlen(trace_buf
);
793 #ifdef APPLETALK_DEBUG
794 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
796 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
799 strcpy(gbuf_rptr(m
), trace_buf
);
801 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
802 gbuf_cont(nextm
) = m
;
807 void atalk_gettrace(m
)
811 gbuf_cont(m
) = trace_msg
;
816 #define GREF_PER_BLK 32
817 static gref_t
*gref_free_list
= 0;
819 int gref_alloc(grefp
)
822 extern gbuf_t
*atp_resource_m
;
825 gref_t
*gref
, *gref_array
;
827 *grefp
= (gref_t
*)NULL
;
829 ATDISABLE(s
, refall_lock
);
830 if (gref_free_list
== 0) {
831 ATENABLE(s
, refall_lock
);
832 #ifdef APPLETALK_DEBUG
833 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
835 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
837 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
838 gref_array
= (gref_t
*)gbuf_rptr(m
);
839 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
840 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
841 ATDISABLE(s
, refall_lock
);
842 gbuf_cont(m
) = atp_resource_m
;
844 gref_array
[i
].atpcb_next
= gref_free_list
;
845 gref_free_list
= (gref_t
*)&gref_array
[0];
848 gref
= gref_free_list
;
849 gref_free_list
= gref
->atpcb_next
;
850 ATENABLE(s
, refall_lock
);
851 ATLOCKINIT(gref
->lock
);
852 //### LD Test 08/05/98
853 // simple_lock_init(&gref->lock);
854 ATEVENTINIT(gref
->event
);
855 ATEVENTINIT(gref
->iocevent
);
857 /* *** just for now *** */
858 gref
->atpcb_socket
= (struct socket
*)NULL
;
864 /* bms: make gref_close callable from kernel */
865 int gref_close(gref_t
*gref
)
869 switch (gref
->proto
) {
871 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
872 BSD-style socket interface. */
875 rc
= atp_close(gref
, 1); break;
877 rc
= asp_close(gref
); break;
880 rc
= aurp_close(gref
); break;
884 rc
= adsp_close(gref
); break;
892 selthreadclear(&gref
->si
);
894 /* from original gref_free() */
895 ATDISABLE(s
, refall_lock
);
896 bzero((char *)gref
, sizeof(gref_t
));
897 gref
->atpcb_next
= gref_free_list
;
898 gref_free_list
= gref
;
899 ATENABLE(s
, refall_lock
);
908 *** Some to be replaced with mbuf routines, some to be re-written
909 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
915 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
916 * "Allocates an mbuf structure and attaches an external cluster."
919 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
928 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
931 m
->m_ext
.ext_buf
= extbuf
;
932 m
->m_ext
.ext_free
= extfree
;
933 m
->m_ext
.ext_size
= extsize
;
934 m
->m_ext
.ext_arg
= extarg
;
935 m
->m_ext
.ext_refs
.forward
=
936 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
946 temp fix for bug 2731148 - until this code is re-written to use standard clusters
947 Deletes any free clusters on the free list.
949 void atp_delete_free_clusters()
952 caddr_t cluster_list
;
955 /* check for free clusters on the free_cluster_list to be deleted */
956 MBUF_LOCK(); /* lock used by mbuf routines */
958 untimeout(&atp_delete_free_clusters
, NULL
);
959 atp_free_cluster_timeout_set
= 0;
961 cluster_list
= atp_free_cluster_list
;
962 atp_free_cluster_list
= 0;
966 while (cluster
= cluster_list
)
968 cluster_list
= *((caddr_t
*)cluster
);
969 FREE(cluster
, M_MCLUST
);
976 Used as the "free" routine for over-size clusters allocated using
977 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
980 void m_lgbuf_free(buf
, size
, arg
)
982 int size
, arg
; /* not needed, but they're in m_free() */
984 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
986 /* move to free_cluster_list to be deleted later */
987 caddr_t cluster
= (caddr_t
)buf
;
989 /* don't need a lock because this is only called called from m_free which */
990 /* is under MBUF_LOCK */
991 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
992 atp_free_cluster_list
= cluster
;
994 if (atp_free_cluster_timeout_set
== 0)
996 atp_free_cluster_timeout_set
= 1;
997 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1002 Used to allocate an mbuf when there is the possibility that it may
1003 need to be larger than the size of a standard cluster.
1006 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1011 if (atp_free_cluster_list
)
1012 atp_delete_free_clusters(); /* delete any free clusters on the free list */
1014 /* If size is too large, allocate a cluster, otherwise, use the
1015 standard mbuf allocation routines.*/
1016 if (size
> MCLBYTES
) {
1019 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1020 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1024 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1025 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1030 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1031 if (m
&& (size
> MHLEN
)) {
1032 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1033 if (!(m
->m_flags
& M_EXT
)) {
1041 } /* m_lgbuf_alloc */
1044 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1045 allocate an mbuf when there is the possibility that it may need
1046 to be larger than the size of a standard cluster.
1048 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1051 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1054 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1056 /* Standard mbuf allocation routines assume that the caller
1057 will set the size. */
1059 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1060 (struct mbuf
*)m
->m_len
= size
;
1071 for (size
=0; m
; m
=gbuf_cont(m
))
1072 size
+= gbuf_len(m
);
1076 int append_copy(m1
, m2
, wait
)
1077 struct mbuf
*m1
, *m2
;
1080 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1081 (m_trailingspace(m1
) >= m2
->m_len
)) {
1082 /* splat the data from one into the other */
1083 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1085 m1
->m_len
+= m2
->m_len
;
1086 if (m1
->m_flags
& M_PKTHDR
)
1087 m1
->m_pkthdr
.len
+= m2
->m_len
;
1090 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1091 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1097 Copy an mbuf chain, referencing existing external storage, if any.
1098 Leave space for a header in the new chain, if the space has been
1099 left in the origin chain.
1101 struct mbuf
*copy_pkt(mlist
, pad
)
1102 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1103 int pad
; /* hint as to how long the header might be
1104 If pad is < 0, leave the same amount of space
1105 as there was in the original. */
1111 len
= m_leadingspace(mlist
);
1113 len
= min(pad
, m_leadingspace(mlist
));
1115 /* preserve space for the header at the beginning of the mbuf */
1117 mlist
->m_data
-= (len
);
1118 mlist
->m_len
+= (len
);
1119 if (mlist
->m_flags
& M_PKTHDR
)
1120 mlist
->m_pkthdr
.len
+= (len
);
1121 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1125 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1130 void gbuf_linkb(m1
, m2
)
1134 while (gbuf_cont(m1
) != 0)
1139 void gbuf_linkpkt(m1
, m2
)
1143 while (gbuf_next(m1
) != 0)
1153 while ((tmp_m
= m
) != 0) {
1155 gbuf_next(tmp_m
) = 0;
1161 /* free empty mbufs at the front of the chain */
1162 gbuf_t
*gbuf_strip(m
)
1167 while (m
&& gbuf_len(m
) == 0) {
1175 /**************************************/
1177 int ddp_adjmsg(m
, len
)
1182 gbuf_t
*curr_m
, *prev_m
;
1184 if (m
== (gbuf_t
*)0)
1188 for (curr_m
=m
; curr_m
;) {
1189 buf_len
= gbuf_len(curr_m
);
1190 if (len
< buf_len
) {
1191 gbuf_rinc(curr_m
,len
);
1195 gbuf_rinc(curr_m
,buf_len
);
1196 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1202 } else if (len
< 0) {
1205 for (curr_m
=m
; gbuf_cont(curr_m
);
1206 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1207 buf_len
= gbuf_len(curr_m
);
1208 if (len
< buf_len
) {
1209 gbuf_wdec(curr_m
,len
);
1214 gbuf_cont(prev_m
) = 0;
1224 * The message chain, m is grown in size by len contiguous bytes.
1225 * If len is non-negative, len bytes are added to the
1226 * end of the gbuf_t chain. If len is negative, the
1227 * bytes are added to the front. ddp_growmsg only adds bytes to
1228 * message blocks of the same type.
1229 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1232 gbuf_t
*ddp_growmsg(mp
, len
)
1238 if ((m
= mp
) == (gbuf_t
*) 0)
1239 return ((gbuf_t
*) 0);
1243 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1244 return ((gbuf_t
*) 0);
1245 gbuf_set_type(d
, gbuf_type(m
));
1247 /* link in new gbuf_t */
1256 if ((count
= gbuf_msgsize(m
)) < 0)
1257 return ((gbuf_t
*) 0);
1258 /* find end of chain */
1259 for ( ; m
; m
= gbuf_cont(m
)) {
1260 if (gbuf_len(m
) >= count
)
1262 count
-= gbuf_len(m
);
1264 /* m now points to gbuf_t to add to */
1265 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1266 return ((gbuf_t
*) 0);
1267 gbuf_set_type(d
, gbuf_type(m
));
1268 /* link in new gbuf_t */
1269 gbuf_cont(d
) = gbuf_cont(m
);
1277 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1278 * block is used as the vehicle, and that if there is an error return,
1279 * then linked blocks are lopped off. BEWARE of multiple references.
1280 * Used by other appletalk modules, so it is not static!
1283 void ioc_ack(errno
, m
, gref
)
1286 register gref_t
*gref
;
1288 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1290 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1291 if ((iocbp
->ioc_error
= errno
) != 0)
1292 { /* errno != 0, then there is an error, get rid of linked blocks! */
1295 gbuf_freem(gbuf_cont(m
));
1298 gbuf_set_type(m
, MSG_IOCNAK
);
1299 iocbp
->ioc_count
= 0; /* only make zero length if error */
1300 iocbp
->ioc_rval
= -1;
1302 gbuf_set_type(m
, MSG_IOCACK
);
1304 atalk_putnext(gref
, m
);