~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

« back to all changes in this revision

Viewing changes to src/VBox/Devices/Network/slirp/mbuf.c

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2009-12-18 16:44:29 UTC
  • mfrom: (0.3.3 upstream) (0.4.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091218164429-jd34ccexpv5na11a
Tags: 3.1.2-dfsg-1ubuntu1
* Merge from Debian unstable (LP: #498219), remaining changes:
  - Disable update action
    - debian/patches/u01-disable-update-action.dpatch
  - VirtualBox should go in Accessories, not in System tools (LP: #288590)
    - debian/virtualbox-ose-qt.files/virtualbox-ose.desktop
  - Add Apport hook
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Add Launchpad integration
    - debian/control
    - debian/lpi-bug.xpm
    - debian/patches/u02-lp-integration.dpatch
* Fixes the following bugs:
  - Kernel module fails to build with Linux >= 2.6.32 (LP: #474625)
  - X.Org drivers need to be rebuilt against X-Server 1.7 (LP: #495935)
  - The *-source packages try to build the kernel modules even though the
    kernel headers aren't available (LP: #473334)
* Replace *-source packages with transitional packages for *-dkms.
* Adapt u01-disable-update-action.dpatch and u02-lp-integration.dpatch for
  new upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 * by m_ext (and the data pointers) and M_EXT is set in
15
15
 * the flags
16
16
 */
17
 
 
18
17
#include <slirp.h>
19
18
 
 
19
#define MBUF_ZONE_SIZE 100
 
20
static int mbuf_zone_init(PNATState pData)
 
21
{
 
22
    int limit;
 
23
    struct mbuf_zone *mzone;
 
24
    int i;
 
25
    struct mbuf *m;
 
26
    uint8_t *zone = RTMemAlloc(msize * MBUF_ZONE_SIZE);  
 
27
    if (zone == NULL)
 
28
    {
 
29
        LogRel(("NAT: can't allocate new zone\n"));
 
30
        return -1;
 
31
    }
 
32
    mzone = RTMemAllocZ(sizeof (struct mbuf_zone));
 
33
    if (mzone == NULL)
 
34
    {
 
35
        RTMemFree(zone);
 
36
        LogRel(("NAT: can't allocate zone descriptor\n"));
 
37
        return -1;
 
38
    }
 
39
 
 
40
    for(i = 0; i < MBUF_ZONE_SIZE; ++i)
 
41
    {
 
42
        m = (struct mbuf *)((char *)zone + i*msize);
 
43
        memset(m, 0, sizeof(struct mbuf));
 
44
#ifdef M_BUF_DEBUG
 
45
        m->m_hdr.mh_id = pData->mbuf_zone_count * MBUF_ZONE_SIZE + i;
 
46
#endif
 
47
        insque(pData, m, &m_freelist);
 
48
    }
 
49
    mzone->mbuf_zone_base_addr = zone;
 
50
    LIST_INSERT_HEAD(&pData->mbuf_zone_head, mzone, list);
 
51
    pData->mbuf_zone_count++;
 
52
    pData->mbuf_water_line_limit = pData->mbuf_zone_count * MBUF_ZONE_SIZE;
 
53
    return 0;
 
54
 
55
 
 
56
void m_fini(PNATState pData)
 
57
{
 
58
    struct mbuf_zone *mz;
 
59
    struct mbuf *m;
 
60
    int i;
 
61
    void *zone;
 
62
    while(!LIST_EMPTY(&pData->mbuf_zone_head))
 
63
    {
 
64
        mz = LIST_FIRST(&pData->mbuf_zone_head);
 
65
        zone = mz->mbuf_zone_base_addr;
 
66
        for(i = 0; i < MBUF_ZONE_SIZE; ++i)
 
67
        {
 
68
            m = (struct mbuf *)((char *)zone + i*msize);
 
69
            if (   (m->m_flags & M_EXT) 
 
70
                && m->m_ext != NULL)
 
71
                RTMemFree(m->m_ext);
 
72
        }
 
73
        RTMemFree(zone);
 
74
        LIST_REMOVE(mz, list);
 
75
        RTMemFree(mz);
 
76
    }
 
77
}
20
78
 
21
79
void
22
80
m_init(PNATState pData)
23
81
{
 
82
    int i;
 
83
    struct mbuf *m;
 
84
    int rc = 0;
24
85
    m_freelist.m_next = m_freelist.m_prev = &m_freelist;
25
86
    m_usedlist.m_next = m_usedlist.m_prev = &m_usedlist;
26
87
    mbuf_alloced = 0;
27
88
    msize_init(pData);
 
89
#if 1
 
90
    rc = RTCritSectInit(&pData->cs_mbuf_zone);
 
91
    AssertReleaseRC(rc);
 
92
    rc = mbuf_zone_init(pData);
 
93
    Assert((rc == 0));
 
94
#endif
28
95
}
29
96
 
30
97
void
32
99
{
33
100
    /*
34
101
     * Find a nice value for msize
35
 
     * XXX if_maxlinkhdr already in mtu
36
102
     */
37
103
    msize = (if_mtu>if_mru ? if_mtu : if_mru)
38
104
          + sizeof(struct m_hdr) + sizeof(void *)   /*pointer to the backstore*/
39
105
          + if_maxlinkhdr ;
40
106
}
 
107
#ifdef m_get
 
108
# undef m_get
 
109
#endif
41
110
 
 
111
#ifdef m_free
 
112
# undef m_free
 
113
#endif
42
114
/*
43
115
 * Get an mbuf from the free list, if there are none
44
116
 * malloc one
52
124
{
53
125
    register struct mbuf *m;
54
126
    int flags = 0;
 
127
    int rc = 0;
55
128
 
56
129
    DEBUG_CALL("m_get");
 
130
    
 
131
    rc = RTCritSectEnter(&pData->cs_mbuf_zone);
 
132
    AssertReleaseRC(rc);
57
133
 
 
134
recheck_zone:
58
135
    if (m_freelist.m_next == &m_freelist)
59
136
    {
 
137
#if 1
 
138
        int rc = mbuf_zone_init(pData);
 
139
        if (rc == 0)
 
140
            goto recheck_zone;
 
141
        AssertMsgFailed(("No mbufs on free list\n"));
 
142
        return NULL;
 
143
#else
60
144
        m = (struct mbuf *)RTMemAlloc(msize);
61
145
        if (m == NULL)
62
146
            goto end_error;
65
149
            flags = M_DOFREE;
66
150
        if (mbuf_alloced > mbuf_max)
67
151
            mbuf_max = mbuf_alloced;
 
152
#endif
68
153
    }
69
154
    else
70
155
    {
72
157
        remque(pData, m);
73
158
    }
74
159
 
 
160
    STAM_COUNTER_INC(&pData->StatMBufAllocation);
75
161
    /* Insert it in the used list */
 
162
    mbuf_alloced++;
 
163
#if 0
 
164
    if (mbuf_alloced >= MBUF_ZONE_SIZE/2)
 
165
    {
 
166
        pData->fmbuf_water_line = 1;
 
167
    }
 
168
#endif
76
169
    insque(pData, m, &m_usedlist);
77
170
    m->m_flags = (flags | M_USEDLIST);
78
171
 
82
175
    m->m_len = 0;
83
176
    m->m_nextpkt = 0;
84
177
    m->m_prevpkt = 0;
85
 
#ifdef VBOX_WITH_SLIRP_ALIAS
86
178
    m->m_la = NULL;
87
 
#endif
88
179
    memset(m->m_data, 0, if_maxlinkhdr); /*initialization of ether area */
89
180
 
90
181
end_error:
91
182
    DEBUG_ARG("m = %lx", (long )m);
 
183
    rc = RTCritSectLeave(&pData->cs_mbuf_zone);
 
184
    AssertReleaseRC(rc);
92
185
    return m;
93
186
}
94
187
 
95
188
void
96
189
m_free(PNATState pData, struct mbuf *m)
97
190
{
98
 
 
 
191
    int rc;
99
192
    DEBUG_CALL("m_free");
100
193
    DEBUG_ARG("m = %lx", (long )m);
101
194
 
102
 
    if (m)
 
195
    rc = RTCritSectEnter(&pData->cs_mbuf_zone);
 
196
    AssertReleaseRC(rc);
 
197
    mbuf_alloced--;
 
198
    if(m)
103
199
    {
104
200
        /* Remove from m_usedlist */
105
201
        if (m->m_flags & M_USEDLIST)
114
210
         */
115
211
        if (m->m_flags & M_DOFREE)
116
212
        {
 
213
#if 1
 
214
            if ((m->m_flags & M_EXT) == 0)
 
215
                memset(m->m_dat, 0, if_mtu);
 
216
            insque(pData, m, &m_freelist);
 
217
            m->m_flags = M_FREELIST; /* Clobber other flags */
 
218
#else
117
219
            RTMemFree(m);
 
220
#endif
118
221
            mbuf_alloced--;
119
222
        }
120
223
        else if ((m->m_flags & M_FREELIST) == 0)
122
225
            insque(pData, m,&m_freelist);
123
226
            m->m_flags = M_FREELIST; /* Clobber other flags */
124
227
        }
125
 
    } /* if (m) */
 
228
        STAM_COUNTER_INC(&pData->StatMBufAllocation);
 
229
    } /* if(m) */
 
230
    rc = RTCritSectLeave(&pData->cs_mbuf_zone);
 
231
    AssertReleaseRC(rc);
126
232
}
127
233
 
 
234
/* update macros for m_get/m_free*/
 
235
#undef m_get
 
236
#undef m_free
 
237
#include "mbuf.h"
 
238
 
128
239
/*
129
240
 * Copy data from one mbuf to the end of
130
241
 * the other.. if result is too big for one mbuf, malloc()
256
367
 
257
368
    return (struct mbuf *)0;
258
369
}
 
370
#ifndef VBOX_WITH_SLIRP_BSD_MBUF
 
371
void *slirp_ext_m_get(PNATState pData)
 
372
{
 
373
    return (void *)m_get(pData);
 
374
}
 
375
 
 
376
void slirp_ext_m_free(PNATState pData, void *arg)
 
377
{
 
378
    struct mbuf *m = (struct mbuf *)arg;
 
379
    m_free(pData, m);
 
380
}
 
381
void slirp_ext_m_append(PNATState pData, void *arg, uint8_t *pu8Buf, size_t cbBuf)
 
382
{
 
383
    char *c;
 
384
    struct mbuf *m = (struct mbuf *)arg;
 
385
    if (cbBuf > M_FREEROOM(m))
 
386
    {
 
387
        m_inc(m, cbBuf);
 
388
    } 
 
389
    c = mtod(m, char *);
 
390
    memcpy(c, pu8Buf, cbBuf);
 
391
    m->m_len = cbBuf;
 
392
}
 
393
#endif