2 * Copyright (c) 2000-2002 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
, flags
, p
)
405 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
406 stat
= _ATrw(fp
, UIO_READ
, uio
, 0);
407 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
411 int _ATwrite(fp
, uio
, cred
, flags
, p
)
421 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
422 stat
= _ATrw(fp
, UIO_WRITE
, uio
, 0);
423 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
428 /* Most of the processing from _ATioctl, so that it can be called
429 from the new ioctl code */
430 /* bms: update to be callable from kernel */
431 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
438 /* error if not for us */
439 if ((cmd
& 0xffff) != 0xff99)
442 /* copy in ioc command info */
444 kprintf("at_ioctl: arg ioccmd.ic_cmd=%x ic_len=%x gref->lock=%x, gref->event=%x\n",
445 ((ioccmd_t *)arg)->ic_cmd, ((ioccmd_t *)arg)->ic_len,
446 gref->lock, gref->event);
449 bcopy (arg
, &ioccmd
, sizeof (ioccmd_t
));
451 if ((err
= copyin((caddr_t
)arg
, (caddr_t
)&ioccmd
, sizeof(ioccmd_t
))) != 0) {
452 #ifdef APPLETALK_DEBUG
453 kprintf("at_ioctl: err = %d, copyin(%x, %x, %d)\n", err
,
454 (caddr_t
)arg
, (caddr_t
)&ioccmd
, sizeof(ioccmd_t
));
460 /* allocate a buffer to create an ioc command
461 first mbuf contains ioc command */
462 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
464 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
465 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
467 /* create the ioc command
468 second mbuf contains the actual ASP command */
470 if ((gbuf_cont(m
) = gbuf_alloc(ioccmd
.ic_len
, PRI_HI
)) == 0) {
472 #ifdef APPLETALK_DEBUG
473 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
477 gbuf_wset(gbuf_cont(m
), ioccmd
.ic_len
); /* mbuf->m_len */
479 bcopy (ioccmd
.ic_dp
, gbuf_rptr(gbuf_cont(m
)), ioccmd
.ic_len
);
481 if ((err
= copyin((caddr_t
)ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), ioccmd
.ic_len
)) != 0) {
487 ioc
= (ioc_t
*) gbuf_rptr(m
);
488 ioc
->ioc_cmd
= ioccmd
.ic_cmd
;
489 ioc
->ioc_count
= ioccmd
.ic_len
;
493 /* send the ioc command to the appropriate recipient */
496 /* wait for the ioc ack */
497 ATDISABLE(s
, gref
->lock
);
498 while ((m
= gref
->ichead
) == 0) {
499 gref
->sevents
|= POLLPRI
;
500 #ifdef APPLETALK_DEBUG
501 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
503 err
= tsleep(&gref
->iocevent
, PSOCK
| PCATCH
, "AT ioctl", 0);
504 gref
->sevents
&= ~POLLPRI
;
506 ATENABLE(s
, gref
->lock
);
507 #ifdef APPLETALK_DEBUG
508 kprintf("at_ioctl: EINTR\n");
515 if (gbuf_next(m
) == m
) /* error case */
518 gref
->ichead
= gbuf_next(m
);
520 ATENABLE(s
, gref
->lock
);
522 #ifdef APPLETALK_DEBUG
523 kprintf("at_ioctl: woke up from ioc sleep gref = 0x%x\n",
527 /* process the ioc response */
528 ioc
= (ioc_t
*) gbuf_rptr(m
);
529 if ((err
= ioc
->ioc_error
) == 0) {
530 ioccmd
.ic_timout
= ioc
->ioc_rval
;
532 mdata
= gbuf_cont(m
);
533 if (mdata
&& ioccmd
.ic_dp
) {
534 ioccmd
.ic_len
= gbuf_msgsize(mdata
);
535 for (len
= 0; mdata
; mdata
= gbuf_cont(mdata
)) {
537 bcopy (gbuf_rptr(mdata
), &ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
539 if ((err
= copyout((caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&ioccmd
.ic_dp
[len
], gbuf_len(mdata
))) < 0) {
540 #ifdef APPLETALK_DEBUG
541 kprintf("at_ioctl: len=%d error copyout=%d from=%x to=%x gbuf_len=%x\n",
542 len
, err
, (caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
547 len
+= gbuf_len(mdata
);
552 bcopy (&ioccmd
, arg
, sizeof(ioccmd_t
));
554 if ((err
= copyout((caddr_t
)&ioccmd
, (caddr_t
)arg
, sizeof(ioccmd_t
))) != 0) {
555 #ifdef APPLETALK_DEBUG
556 kprintf("at_ioctl: error copyout2=%d from=%x to=%x len=%d\n",
557 err
, &ioccmd
, arg
, sizeof(ioccmd_t
));
566 /*kprintf("at_ioctl: I_done=%d\n", err);*/
570 int _ATioctl(fp
, cmd
, arg
, proc
)
573 register caddr_t arg
;
579 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
580 if ((err
= atalk_getref(fp
, 0, &gref
, 0)) != 0) {
581 #ifdef APPLETALK_DEBUG
582 kprintf("_ATioctl: atalk_getref err = %d\n", err
);
586 err
= at_ioctl(gref
, cmd
, arg
, 0);
588 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
593 int _ATselect(fp
, which
, wql
, proc
)
602 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
603 err
= atalk_getref(fp
, 0, &gref
, 0);
604 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
609 ATDISABLE(s
, gref
->lock
);
610 if (which
== FREAD
) {
611 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
614 gref
->sevents
|= POLLIN
;
615 selrecord(proc
, &gref
->si
, wql
);
618 else if (which
== POLLOUT
) {
619 if (gref
->writeable
) {
620 if ((*gref
->writeable
)(gref
))
623 gref
->sevents
|= POLLOUT
;
624 selrecord(proc
, &gref
->si
, wql
);
629 ATENABLE(s
, gref
->lock
);
635 void atalk_putnext(gref
, m
)
641 ATDISABLE(s
, gref
->lock
);
643 /* *** potential leak? *** */
646 switch (gbuf_type(m
)) {
650 gbuf_next(gref
->ichead
) = m
;
653 if (gref
->sevents
& POLLPRI
) {
654 #ifdef APPLETALK_DEBUG
655 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
657 wakeup(&gref
->iocevent
);
662 /* *** this processing was moved to atalk_notify *** */
663 panic("atalk_putnext receved MSG_ERROR");
670 gbuf_next(gref
->rdtail
) = m
;
674 if (gref
->sevents
& POLLMSG
) {
675 gref
->sevents
&= ~POLLMSG
;
676 wakeup(&gref
->event
);
678 if (gref
->sevents
& POLLIN
) {
679 gref
->sevents
&= ~POLLIN
;
680 selwakeup(&gref
->si
);
684 } /* switch gbuf_type(m) */
686 ATENABLE(s
, gref
->lock
);
687 } /* atalk_putnext */
689 void atalk_enablew(gref
)
692 if (gref
->sevents
& POLLSYNC
)
693 wakeup(&gref
->event
);
696 void atalk_flush(gref
)
701 ATDISABLE(s
, gref
->lock
);
703 gbuf_freel(gref
->rdhead
);
707 gbuf_freel(gref
->ichead
);
710 ATENABLE(s
, gref
->lock
);
714 * Notify an appletalk user of an asynchronous error;
715 * just wake up so that he can collect error status.
717 void atalk_notify(gref
, errno
)
718 register gref_t
*gref
;
722 ATDISABLE(s
, gref
->lock
);
724 if (gref
->atpcb_socket
) {
726 This section is patterned after udp_notify() in
729 gref
->atpcb_socket
->so_error
= errno
;
730 sorwakeup(gref
->atpcb_socket
);
731 sowwakeup(gref
->atpcb_socket
);
733 /* for ATP, ASP, and ADSP */
734 if (gref
->errno
== 0) {
736 /* clear out data waiting to be read */
738 gbuf_freel(gref
->rdhead
);
742 if (gref
->sevents
& POLLMSG
) {
743 gref
->sevents
&= ~POLLMSG
;
744 wakeup(&gref
->event
);
747 if (gref
->sevents
& POLLIN
) {
748 gref
->sevents
&= ~POLLIN
;
749 selwakeup(&gref
->si
);
753 ATENABLE(s
, gref
->lock
);
756 void atalk_notify_sel(gref
)
761 ATDISABLE(s
, gref
->lock
);
762 if (gref
->sevents
& POLLIN
) {
763 gref
->sevents
&= ~POLLIN
;
764 selwakeup(&gref
->si
);
766 ATENABLE(s
, gref
->lock
);
769 int atalk_peek(gref
, event
)
771 unsigned char *event
;
775 ATDISABLE(s
, gref
->lock
);
777 *event
= *gbuf_rptr(gref
->rdhead
);
781 ATENABLE(s
, gref
->lock
);
786 static gbuf_t
*trace_msg
;
788 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
795 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
796 len
= strlen(trace_buf
);
797 #ifdef APPLETALK_DEBUG
798 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
800 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
803 strcpy(gbuf_rptr(m
), trace_buf
);
805 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
806 gbuf_cont(nextm
) = m
;
811 void atalk_gettrace(m
)
815 gbuf_cont(m
) = trace_msg
;
820 #define GREF_PER_BLK 32
821 static gref_t
*gref_free_list
= 0;
823 int gref_alloc(grefp
)
826 extern gbuf_t
*atp_resource_m
;
829 gref_t
*gref
, *gref_array
;
831 *grefp
= (gref_t
*)NULL
;
833 ATDISABLE(s
, refall_lock
);
834 if (gref_free_list
== 0) {
835 ATENABLE(s
, refall_lock
);
836 #ifdef APPLETALK_DEBUG
837 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
839 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
841 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
842 gref_array
= (gref_t
*)gbuf_rptr(m
);
843 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
844 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
845 ATDISABLE(s
, refall_lock
);
846 gbuf_cont(m
) = atp_resource_m
;
848 gref_array
[i
].atpcb_next
= gref_free_list
;
849 gref_free_list
= (gref_t
*)&gref_array
[0];
852 gref
= gref_free_list
;
853 gref_free_list
= gref
->atpcb_next
;
854 ATENABLE(s
, refall_lock
);
855 ATLOCKINIT(gref
->lock
);
856 //### LD Test 08/05/98
857 // simple_lock_init(&gref->lock);
858 ATEVENTINIT(gref
->event
);
859 ATEVENTINIT(gref
->iocevent
);
861 /* *** just for now *** */
862 gref
->atpcb_socket
= (struct socket
*)NULL
;
868 /* bms: make gref_close callable from kernel */
869 int gref_close(gref_t
*gref
)
873 switch (gref
->proto
) {
875 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
876 BSD-style socket interface. */
879 rc
= atp_close(gref
, 1); break;
881 rc
= asp_close(gref
); break;
884 rc
= aurp_close(gref
); break;
888 rc
= adsp_close(gref
); break;
896 selthreadclear(&gref
->si
);
898 /* from original gref_free() */
899 ATDISABLE(s
, refall_lock
);
900 bzero((char *)gref
, sizeof(gref_t
));
901 gref
->atpcb_next
= gref_free_list
;
902 gref_free_list
= gref
;
903 ATENABLE(s
, refall_lock
);
912 *** Some to be replaced with mbuf routines, some to be re-written
913 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
919 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
920 * "Allocates an mbuf structure and attaches an external cluster."
923 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
932 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
935 m
->m_ext
.ext_buf
= extbuf
;
936 m
->m_ext
.ext_free
= extfree
;
937 m
->m_ext
.ext_size
= extsize
;
938 m
->m_ext
.ext_arg
= extarg
;
939 m
->m_ext
.ext_refs
.forward
=
940 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
950 temp fix for bug 2731148 - until this code is re-written to use standard clusters
951 Deletes any free clusters on the free list.
953 void atp_delete_free_clusters()
956 caddr_t cluster_list
;
959 /* check for free clusters on the free_cluster_list to be deleted */
960 MBUF_LOCK(); /* lock used by mbuf routines */
962 untimeout(&atp_delete_free_clusters
, NULL
);
963 atp_free_cluster_timeout_set
= 0;
965 cluster_list
= atp_free_cluster_list
;
966 atp_free_cluster_list
= 0;
970 while (cluster
= cluster_list
)
972 cluster_list
= *((caddr_t
*)cluster
);
973 FREE(cluster
, M_MCLUST
);
980 Used as the "free" routine for over-size clusters allocated using
981 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
984 void m_lgbuf_free(buf
, size
, arg
)
986 int size
, arg
; /* not needed, but they're in m_free() */
988 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
990 /* move to free_cluster_list to be deleted later */
991 caddr_t cluster
= (caddr_t
)buf
;
993 /* don't need a lock because this is only called called from m_free which */
994 /* is under MBUF_LOCK */
995 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
996 atp_free_cluster_list
= cluster
;
998 if (atp_free_cluster_timeout_set
== 0)
1000 atp_free_cluster_timeout_set
= 1;
1001 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1006 Used to allocate an mbuf when there is the possibility that it may
1007 need to be larger than the size of a standard cluster.
1010 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1015 if (atp_free_cluster_list
)
1016 atp_delete_free_clusters(); /* delete any free clusters on the free list */
1018 /* If size is too large, allocate a cluster, otherwise, use the
1019 standard mbuf allocation routines.*/
1020 if (size
> MCLBYTES
) {
1023 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1024 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1028 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1029 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1034 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1035 if (m
&& (size
> MHLEN
)) {
1036 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1037 if (!(m
->m_flags
& M_EXT
)) {
1045 } /* m_lgbuf_alloc */
1048 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1049 allocate an mbuf when there is the possibility that it may need
1050 to be larger than the size of a standard cluster.
1052 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1055 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1058 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1060 /* Standard mbuf allocation routines assume that the caller
1061 will set the size. */
1063 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1064 (struct mbuf
*)m
->m_len
= size
;
1075 for (size
=0; m
; m
=gbuf_cont(m
))
1076 size
+= gbuf_len(m
);
1080 int append_copy(m1
, m2
, wait
)
1081 struct mbuf
*m1
, *m2
;
1084 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1085 (m_trailingspace(m1
) >= m2
->m_len
)) {
1086 /* splat the data from one into the other */
1087 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1089 m1
->m_len
+= m2
->m_len
;
1090 if (m1
->m_flags
& M_PKTHDR
)
1091 m1
->m_pkthdr
.len
+= m2
->m_len
;
1094 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1095 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1101 Copy an mbuf chain, referencing existing external storage, if any.
1102 Leave space for a header in the new chain, if the space has been
1103 left in the origin chain.
1105 struct mbuf
*copy_pkt(mlist
, pad
)
1106 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1107 int pad
; /* hint as to how long the header might be
1108 If pad is < 0, leave the same amount of space
1109 as there was in the original. */
1115 len
= m_leadingspace(mlist
);
1117 len
= min(pad
, m_leadingspace(mlist
));
1119 /* preserve space for the header at the beginning of the mbuf */
1121 mlist
->m_data
-= (len
);
1122 mlist
->m_len
+= (len
);
1123 if (mlist
->m_flags
& M_PKTHDR
)
1124 mlist
->m_pkthdr
.len
+= (len
);
1125 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1129 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1134 void gbuf_linkb(m1
, m2
)
1138 while (gbuf_cont(m1
) != 0)
1143 void gbuf_linkpkt(m1
, m2
)
1147 while (gbuf_next(m1
) != 0)
1157 while ((tmp_m
= m
) != 0) {
1159 gbuf_next(tmp_m
) = 0;
1165 /* free empty mbufs at the front of the chain */
1166 gbuf_t
*gbuf_strip(m
)
1171 while (m
&& gbuf_len(m
) == 0) {
1179 /**************************************/
1181 int ddp_adjmsg(m
, len
)
1186 gbuf_t
*curr_m
, *prev_m
;
1188 if (m
== (gbuf_t
*)0)
1192 for (curr_m
=m
; curr_m
;) {
1193 buf_len
= gbuf_len(curr_m
);
1194 if (len
< buf_len
) {
1195 gbuf_rinc(curr_m
,len
);
1199 gbuf_rinc(curr_m
,buf_len
);
1200 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1206 } else if (len
< 0) {
1209 for (curr_m
=m
; gbuf_cont(curr_m
);
1210 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1211 buf_len
= gbuf_len(curr_m
);
1212 if (len
< buf_len
) {
1213 gbuf_wdec(curr_m
,len
);
1218 gbuf_cont(prev_m
) = 0;
1228 * The message chain, m is grown in size by len contiguous bytes.
1229 * If len is non-negative, len bytes are added to the
1230 * end of the gbuf_t chain. If len is negative, the
1231 * bytes are added to the front. ddp_growmsg only adds bytes to
1232 * message blocks of the same type.
1233 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1236 gbuf_t
*ddp_growmsg(mp
, len
)
1242 if ((m
= mp
) == (gbuf_t
*) 0)
1243 return ((gbuf_t
*) 0);
1247 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1248 return ((gbuf_t
*) 0);
1249 gbuf_set_type(d
, gbuf_type(m
));
1251 /* link in new gbuf_t */
1260 if ((count
= gbuf_msgsize(m
)) < 0)
1261 return ((gbuf_t
*) 0);
1262 /* find end of chain */
1263 for ( ; m
; m
= gbuf_cont(m
)) {
1264 if (gbuf_len(m
) >= count
)
1266 count
-= gbuf_len(m
);
1268 /* m now points to gbuf_t to add to */
1269 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1270 return ((gbuf_t
*) 0);
1271 gbuf_set_type(d
, gbuf_type(m
));
1272 /* link in new gbuf_t */
1273 gbuf_cont(d
) = gbuf_cont(m
);
1281 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1282 * block is used as the vehicle, and that if there is an error return,
1283 * then linked blocks are lopped off. BEWARE of multiple references.
1284 * Used by other appletalk modules, so it is not static!
1287 void ioc_ack(errno
, m
, gref
)
1290 register gref_t
*gref
;
1292 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1294 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1295 if ((iocbp
->ioc_error
= errno
) != 0)
1296 { /* errno != 0, then there is an error, get rid of linked blocks! */
1299 gbuf_freem(gbuf_cont(m
));
1302 gbuf_set_type(m
, MSG_IOCNAK
);
1303 iocbp
->ioc_count
= 0; /* only make zero length if error */
1304 iocbp
->ioc_rval
= -1;
1306 gbuf_set_type(m
, MSG_IOCACK
);
1308 atalk_putnext(gref
, m
);