Annotation of XNU/bsd/net/netbuf.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
                      3:  *
                      4:  * @APPLE_LICENSE_HEADER_START@
                      5:  * 
                      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.
                     11:  * 
                     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
                     18:  * under the License.
                     19:  * 
                     20:  * @APPLE_LICENSE_HEADER_END@
                     21:  */
                     22: /* 
                     23:  * Copyright (C) 1990 by NeXT, Inc., All Rights Reserved
                     24:  *
                     25:  */
                     26: 
                     27: /* 
                     28:  * Network Buffer handling
                     29:  * These should be used instead of mbufs. Currently, they are just
                     30:  * wrappers around mbufs, but we hope to flush mbufs one day. Third parties
                     31:  * must use this API, or risk breakage after an OS upgrade.
                     32:  *
                     33:  * HISTORY
                     34:  * 09-Apr-90  Bradley Taylor (btaylor) at NeXT, Inc.
                     35:  *     Created.
                     36:  */
                     37: 
                     38: #import <sys/types.h>
                     39: #import <sys/param.h>
                     40: #import <sys/mbuf.h>
                     41: #import <machine/spl.h>
                     42: #import <net/netbuf.h>
                     43: 
                     44: const char
                     45:        IFCONTROL_SETFLAGS[] = "setflags",      
                     46:        IFCONTROL_SETADDR[] = "setaddr",
                     47:        IFCONTROL_GETADDR[] = "getaddr",
                     48:        IFCONTROL_AUTOADDR[] = "autoaddr",
                     49:        IFCONTROL_UNIXIOCTL[] = "unix-ioctl",
                     50:        IFCONTROL_RCVPROMISCUOUS[] = "promiscuous-on",
                     51:        IFCONTROL_RCVPROMISCOFF[] = "promiscuous-off",
                     52:        IFCONTROL_ADDMULTICAST[] = "add-multicast",
                     53:        IFCONTROL_RMVMULTICAST[] = "rmv-multicast";
                     54: 
                     55: static void
                     56: nb_alloc_free(
                     57:              void *orig_data
                     58:              )
                     59: {
                     60:        int orig_size = *(unsigned *)orig_data;
                     61: 
                     62:         if ( orig_size <= MCLBYTES ) {
                     63:                MCLFREE( orig_data );
                     64:        } else {
                     65:                kfree(orig_data, orig_size);
                     66:        }
                     67: }
                     68: 
                     69: void
                     70: nb_delayed_free(void *data, unsigned size, void *orig_data)
                     71: {
                     72:        int orig_size = *(unsigned *)orig_data;
                     73: 
                     74:         if ( orig_size <= MCLBYTES ) {
                     75:                MCLFREE( orig_data );
                     76:        } else {
                     77:                kfree(orig_data, orig_size);
                     78:         }
                     79: }
                     80: 
                     81: 
                     82: 
                     83: netbuf_t
                     84: nb_alloc(
                     85:         unsigned data_size
                     86:         )
                     87: {
                     88:        void *data;
                     89:        netbuf_t nb;
                     90:        unsigned *orig_data;
                     91:        unsigned orig_size;
                     92: 
                     93:        orig_size = data_size + 0x20;           /* Add cache-line size to request */
                     94: 
                     95:         if ( orig_size <= MCLBYTES ) {
                     96:                MCLALLOC( orig_data, M_DONTWAIT );
                     97:         } else { 
                     98:                orig_data = (unsigned *)kalloc(orig_size);
                     99:         }
                    100:        if (orig_data == NULL) {
                    101:                return (NULL);
                    102:        }
                    103:        orig_data[0] = orig_size;
                    104:        data = (void *)&orig_data[1];
                    105:         data = (void *)(((unsigned int)data + 0x1F) & ~0x1F);  /* Align to cache-line boundary */
                    106:        nb = nb_alloc_wrapper(data, data_size, 
                    107:                              nb_delayed_free, 
                    108:                              (void *)orig_data);
                    109:        if (nb == NULL) {
                    110:                nb_alloc_free((void *)orig_data);
                    111:                return (NULL);
                    112:        }
                    113: 
                    114:        return (nb);
                    115: }
                    116: struct mbuf *
                    117: mclgetx(fun, arg, addr, len, wait)
                    118:        int (*fun)(), arg, len, wait;
                    119:        caddr_t addr;
                    120: {
                    121:        register struct mbuf *m;
                    122: 
                    123:        MGETHDR(m, wait, MT_DATA);
                    124:        if (m == 0)
                    125:                return (0);
                    126: //     m->m_off = (int)addr - (int)m;
                    127: //     m->m_len = len;
                    128: //     m->m_cltype = MCL_LOANED;
                    129: //     m->m_clfun = fun;
                    130: //     m->m_clarg = arg;
                    131: //     m->m_clswp = NULL;
                    132:        m->m_ext.ext_buf = addr;
                    133:        m->m_ext.ext_free = fun;
                    134:        m->m_ext.ext_size = len;
                    135:        m->m_ext.ext_arg = arg;
                    136:        m->m_ext.ext_refs.forward = m->m_ext.ext_refs.backward =
                    137:                        &m->m_ext.ext_refs;
                    138:        m->m_data = addr;
                    139:        m->m_flags |= M_EXT;
                    140:        m->m_pkthdr.len = len;
                    141:        m->m_len = len;
                    142: 
                    143:        return (m);
                    144: }
                    145: 
                    146: netbuf_t
                    147: nb_alloc_wrapper(
                    148:                 void *data,
                    149:                 unsigned data_size,
                    150:                 void data_free(void *arg),
                    151:                 void *data_free_arg
                    152:                 )
                    153: {
                    154:        struct mbuf *m;
                    155:        m = mclgetx(data_free, data_free_arg, data, data_size, M_DONTWAIT);
                    156:        if (m == NULL) {
                    157:                return (NULL);
                    158:        }
                    159:        return ((netbuf_t)m);
                    160: }
                    161: 
                    162: char *
                    163: nb_map(netbuf_t nb)
                    164: {
                    165:        return (mtod(((struct mbuf *)nb), char *));
                    166: }
                    167: 
                    168: void
                    169: nb_free(netbuf_t nb)
                    170: {
                    171:        m_freem((struct mbuf *)nb);
                    172: }
                    173: 
                    174: unsigned
                    175: nb_size(netbuf_t nb)
                    176: {
                    177:        return (((struct mbuf *)nb)->m_len);
                    178: }
                    179: 
                    180: int
                    181: nb_read(netbuf_t nb, unsigned offset, unsigned size, void *target)
                    182: {
                    183:        struct mbuf *m = (struct mbuf *)nb;
                    184:        void *data;
                    185:        
                    186:        if (offset + size > m->m_len) {
                    187:                return (-1);
                    188:        }
                    189:        data = mtod(m, void *);
                    190:        bcopy(data + offset, target, size);
                    191:        return (0);
                    192: }
                    193: 
                    194: int
                    195: nb_write(netbuf_t nb, unsigned offset, unsigned size, void *source)
                    196: {
                    197:        struct mbuf *m = (struct mbuf *)nb;
                    198:        void *data;
                    199:        
                    200:        if (offset + size > m->m_len) {
                    201:                return (-1);
                    202:        }
                    203:        data = mtod(m, void *);
                    204:        bcopy(source, data + offset, size);
                    205:        return (0);
                    206: }
                    207: /* Remember to look at the PPP and slip interfaces, which bypassses the
                    208:  * m_data field XXXX : (AR, CCG 6/30/95)
                    209:  */
                    210: int
                    211: nb_shrink_top(netbuf_t nb, unsigned size)
                    212: {
                    213:        struct mbuf *m = (struct mbuf *)nb;
                    214:        
                    215:        m->m_len -= size;
                    216:        m->m_data += size;
                    217:        return (0); /* XXX should error check */
                    218: }
                    219: 
                    220: int
                    221: nb_grow_top(netbuf_t nb, unsigned size)
                    222: {
                    223:        struct mbuf *m = (struct mbuf *)nb;
                    224:        
                    225:        m->m_len += size;
                    226:        m->m_data -= size;
                    227:        return (0); /* XXX should error check */
                    228: }
                    229: 
                    230: int
                    231: nb_shrink_bot(netbuf_t nb, unsigned size)
                    232: {
                    233:        struct mbuf *m = (struct mbuf *)nb;
                    234:        
                    235:        m->m_len -= size;
                    236:        return (0); /* XXX should error check */
                    237: }
                    238: 
                    239: 
                    240: int
                    241: nb_grow_bot(netbuf_t nb, unsigned size)
                    242: {
                    243:        struct mbuf *m = (struct mbuf *)nb;
                    244:        
                    245:        m->m_len += size;
                    246:        return (0); /* XXX should error check */
                    247: }
                    248: 
                    249: int
                    250: nb_aligned_copy(netbuf_t nb, unsigned aligned)
                    251: {
                    252:        struct mbuf *m = (struct mbuf *)nb;
                    253:        unsigned char *buf_p, *buf_m;
                    254:        
                    255:        buf_p = buf_m = mtod(m, unsigned char *);
                    256:        buf_m -= aligned;
                    257:        if (buf_p != buf_m)
                    258:          memmove(buf_m, buf_p,  m->m_len);
                    259:        m->m_data = buf_m;
                    260:        return(0);
                    261: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.