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

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

unix.superglobalmegacorp.com

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