aboutsummaryrefslogtreecommitdiffstats
path: root/linux-2.6-xen-sparse/drivers/xen/netback/interface.c
blob: df0f43b3b203d16f07358f80baaa2b6c520af79c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/******************************************************************************
 * arch/xen/drivers/netif/backend/interface.c
 * 
 * Network-device interface management.
 * 
 * Copyright (c) 2004-2005, Keir Fraser
 */

#include "common.h"
#include <linux/rtnetlink.h>

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
#define VMALLOC_VMADDR(x) ((unsigned long)(x))
#endif

#define NETIF_HASHSZ 1024
#define NETIF_HASH(_d,_h) (((int)(_d)^(int)(_h))&(NETIF_HASHSZ-1))

static netif_t *netif_hash[NETIF_HASHSZ];

netif_t *netif_find_by_handle(domid_t domid, unsigned int handle)
{
    netif_t *netif = netif_hash[NETIF_HASH(domid, handle)];
    while ( (netif != NULL) && 
            ((netif->domid != domid) || (netif->handle != handle)) )
        netif = netif->hash_next;
    return netif;
}

static void __netif_up(netif_t *netif)
{
    struct net_device *dev = netif->dev;
    spin_lock_bh(&dev->xmit_lock);
    netif->active = 1;
    spin_unlock_bh(&dev->xmit_lock);
    (void)bind_evtchn_to_irqhandler(
        netif->evtchn, netif_be_int, 0, dev->name, netif);
    netif_schedule_work(netif);
}

static void __netif_down(netif_t *netif)
{
    struct net_device *dev = netif->dev;
    spin_lock_bh(&dev->xmit_lock);
    netif->active = 0;
    spin_unlock_bh(&dev->xmit_lock);
    unbind_evtchn_from_irqhandler(netif->evtchn, netif);
    netif_deschedule_work(netif);
}

static int net_open(struct net_device *dev)
{
    netif_t *netif = netdev_priv(dev);
    if ( netif->status == CONNECTED )
        __netif_up(netif);
    netif_start_queue(dev);
    return 0;
}

static int net_close(struct net_device *dev)
{
    netif_t *netif = netdev_priv(dev);
    netif_stop_queue(dev);
    if ( netif->status == CONNECTED )
        __netif_down(netif);
    return 0;
}

static void __netif_disconnect_complete(void *arg)
{
    netif_t              *netif = (netif_t *)arg;
    ctrl_msg_t            cmsg;
    netif_be_disconnect_t disc;

    /*
     * These can't be done in netif_disconnect() because at that point there
     * may be outstanding requests in the network stack whose asynchronous
     * responses must still be notified to the remote driver.
     */
    vfree(netif->tx); /* Frees netif->rx as well. */

    /* Construct the deferred response message. */
    cmsg.type         = CMSG_NETIF_BE;
    cmsg.subtype      = CMSG_NETIF_BE_DISCONNECT;
    cmsg.id           = netif->disconnect_rspid;
    cmsg.length       = sizeof(netif_be_disconnect_t);
    disc.domid        = netif->domid;
    disc.netif_handle = netif->handle;
    disc.status       = NETIF_BE_STATUS_OKAY;
    memcpy(cmsg.msg, &disc, sizeof(disc));

    /*
     * Make sure message is constructed /before/ status change, because
     * after the status change the 'netif' structure could be deallocated at
     * any time. Also make sure we send the response /after/ status change,
     * as otherwise a subsequent CONNECT request could spuriously fail if
     * another CPU doesn't see the status change yet.
     */
    mb();
    if ( netif->status != DISCONNECTING )
        BUG();
    netif->status = DISCONNECTED;
    mb();

    /* Send the successful response. */
    ctrl_if_send_response(&cmsg);
}

void netif_disconnect_complete(netif_t *netif)
{
    INIT_WORK(&netif->work, __netif_disconnect_complete, (void *)netif);
    schedule_work(&netif->work);
}

void netif_create(netif_be_create_t *create)
{
    int                err = 0;
    domid_t            domid  = create->domid;
    unsigned int       handle = create->netif_handle;
    struct net_device *dev;
    netif_t          **pnetif, *netif;
    char               name[IFNAMSIZ] = {};

    snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
    dev = alloc_netdev(sizeof(netif_t), name, ether_setup);
    if ( dev == NULL )
    {
        DPRINTK("Could not create netif: out of memory\n");
        create->status = NETIF_BE_STATUS_OUT_OF_MEMORY;
        return;
    }

    netif = netdev_priv(dev);
    memset(netif, 0, sizeof(*netif));
    netif->domid  = domid;
    netif->handle = handle;
    netif->status = DISCONNECTED;
    atomic_set(&netif->refcnt, 0);
    netif->dev = dev;

    netif->credit_bytes = netif->remaining_credit = ~0UL;
    netif->credit_usec  = 0UL;
    init_timer(&netif->credit_timeout);

    pnetif = &netif_hash[NETIF_HASH(domid, handle)];
    while ( *pnetif != NULL )
    {
        if ( ((*pnetif)->domid == domid) && ((*pnetif)->handle == handle) )
        {
            DPRINTK("Could not create netif: already exists\n");
            create->status = NETIF_BE_STATUS_INTERFACE_EXISTS;
            free_netdev(dev);
            return;
        }
        pnetif = &(*pnetif)->hash_next;
    }

    dev->hard_start_xmit = netif_be_start_xmit;
    dev->get_stats       = netif_be_get_stats;
    dev->open            = net_open;
    dev->stop            = net_close;
    dev->features        = NETIF_F_NO_CSUM;

    /* Disable queuing. */
    dev->tx_queue_len = 0;

    if ( (create->be_mac[0] == 0) && (create->be_mac[1] == 0) &&
         (create->be_mac[2] == 0) && (create->be_mac[3] == 0) &&
         (create->be_mac[4] == 0) && (create->be_mac[5] == 0) )
    {
        /*
         * Initialise a dummy MAC address. We choose the numerically largest
         * non-broadcast address to prevent the address getting stolen by an
         * Ethernet bridge for STP purposes. (FE:FF:FF:FF:FF:FF)
         */ 
        memset(dev->dev_addr, 0xFF, ETH_ALEN);
        dev->dev_addr[0] &= ~0x01;
    }
    else
    {
        memcpy(dev->dev_addr, create->be_mac, ETH_ALEN);
    }

    memcpy(netif->fe_dev_addr, create->mac, ETH_ALEN);

    rtnl_lock();
    err = register_netdevice(dev);
    rtnl_unlock();

    if ( err != 0 )
    {
        DPRINTK("Could not register new net device %s: err=%d\n",
                dev->name, err);
        create->status = NETIF_BE_STATUS_OUT_OF_MEMORY;
        free_netdev(dev);
        return;
    }

    netif->hash_next = *pnetif;
    *pnetif = netif;

    DPRINTK("Successfully created netif\n");
    create->status = NETIF_BE_STATUS_OKAY;
}

void netif_destroy(netif_be_destroy_t *destroy)
{
    domid_t       domid  = destroy->domid;
    unsigned int  handle = destroy->netif_handle;
    netif_t     **pnetif, *netif;

    pnetif = &netif_hash[NETIF_HASH(domid, handle)];
    while ( (netif = *pnetif) != NULL )
    {
        if ( (netif->domid == domid) && (netif->handle == handle) )
        {
            if ( netif->status != DISCONNECTED )
                goto still_connected;
            goto destroy;
        }
        pnetif = &netif->hash_next;
    }

    destroy->status = NETIF_BE_STATUS_INTERFACE_NOT_FOUND;
    return;

 still_connected:
    destroy->status = NETIF_BE_STATUS_INTERFACE_CONNECTED;
    return;

 destroy:
    *pnetif = netif->hash_next;
    unregister_netdev(netif->dev);
    free_netdev(netif->dev);
    destroy->status = NETIF_BE_STATUS_OKAY;
}

void netif_creditlimit(netif_be_creditlimit_t *creditlimit)
{
    domid_t       domid  = creditlimit->domid;
    unsigned int  handle = creditlimit->netif_handle;
    netif_t      *netif;

    netif = netif_find_by_handle(domid, handle);
    if ( unlikely(netif == NULL) )
    {
        DPRINTK("netif_creditlimit attempted for non-existent netif"
                " (%u,%u)\n", creditlimit->domid, creditlimit->netif_handle); 
        creditlimit->status = NETIF_BE_STATUS_INTERFACE_NOT_FOUND;
        return; 
    }

    /* Set the credit limit (reset remaining credit to new limit). */
    netif->credit_bytes = netif->remaining_credit = creditlimit->credit_bytes;
    netif->credit_usec = creditlimit->period_usec;

    if ( netif->status == CONNECTED )
    {
        /*
         * Schedule work so that any packets waiting under previous credit 
         * limit are dealt with (acts like a replenishment point).
         */
        netif->credit_timeout.expires = jiffies;
        netif_schedule_work(netif);
    }
    
    creditlimit->status = NETIF_BE_STATUS_OKAY;
}

void netif_connect(netif_be_connect_t *connect)
{
    domid_t       domid  = connect->domid;
    unsigned int  handle = connect->netif_handle;
    unsigned int  evtchn = connect->evtchn;
    unsigned long tx_shmem_frame = connect->tx_shmem_frame;
    unsigned long rx_shmem_frame = connect->rx_shmem_frame;
    struct vm_struct *vma;
    pgprot_t      prot;
    int           error;
    netif_t      *netif;

    netif = netif_find_by_handle(domid, handle);
    if ( unlikely(netif == NULL) )
    {
        DPRINTK("netif_connect attempted for non-existent netif (%u,%u)\n", 
                connect->domid, connect->netif_handle); 
        connect->status = NETIF_BE_STATUS_INTERFACE_NOT_FOUND;
        return;
    }

    if ( netif->status != DISCONNECTED )
    {
        connect->status = NETIF_BE_STATUS_INTERFACE_CONNECTED;
        return;
    }

    if ( (vma = get_vm_area(2*PAGE_SIZE, VM_IOREMAP)) == NULL )
    {
        connect->status = NETIF_BE_STATUS_OUT_OF_MEMORY;
        return;
    }

    prot = __pgprot(_KERNPG_TABLE);
    error  = direct_remap_area_pages(&init_mm, 
                                     VMALLOC_VMADDR(vma->addr),
                                     tx_shmem_frame<<PAGE_SHIFT, PAGE_SIZE,
                                     prot, domid);
    error |= direct_remap_area_pages(&init_mm, 
                                     VMALLOC_VMADDR(vma->addr) + PAGE_SIZE,
                                     rx_shmem_frame<<PAGE_SHIFT, PAGE_SIZE,
                                     prot, domid);
    if ( error != 0 )
    {
        if ( error == -ENOMEM )
            connect->status = NETIF_BE_STATUS_OUT_OF_MEMORY;
        else if ( error == -EFAULT )
            connect->status = NETIF_BE_STATUS_MAPPING_ERROR;
        else
            connect->status = NETIF_BE_STATUS_ERROR;
        vfree(vma->addr);
        return;
    }

    netif->evtchn         = evtchn;
    netif->tx_shmem_frame = tx_shmem_frame;
    netif->rx_shmem_frame = rx_shmem_frame;
    netif->tx             = 
        (netif_tx_interface_t *)vma->addr;
    netif->rx             = 
        (netif_rx_interface_t *)((char *)vma->addr + PAGE_SIZE);
    netif->tx->resp_prod = netif->rx->resp_prod = 0;
    netif_get(netif);
    wmb(); /* Other CPUs see new state before interface is started. */

    rtnl_lock();
    netif->status = CONNECTED;
    wmb();
    if ( netif_running(netif->dev) )
        __netif_up(netif);
    rtnl_unlock();

    connect->status = NETIF_BE_STATUS_OKAY;
}

int netif_disconnect(netif_be_disconnect_t *disconnect, u8 rsp_id)
{
    domid_t       domid  = disconnect->domid;
    unsigned int  handle = disconnect->netif_handle;
    netif_t      *netif;

    netif = netif_find_by_handle(domid, handle);
    if ( unlikely(netif == NULL) )
    {
        DPRINTK("netif_disconnect attempted for non-existent netif"
                " (%u,%u)\n", disconnect->domid, disconnect->netif_handle); 
        disconnect->status = NETIF_BE_STATUS_INTERFACE_NOT_FOUND;
        return 1; /* Caller will send response error message. */
    }

    if ( netif->status == CONNECTED )
    {
        rtnl_lock();
        netif->status = DISCONNECTING;
        netif->disconnect_rspid = rsp_id;
        wmb();
        if ( netif_running(netif->dev) )
            __netif_down(netif);
        rtnl_unlock();
        netif_put(netif);
        return 0; /* Caller should not send response message. */
    }

    disconnect->status = NETIF_BE_STATUS_OKAY;
    return 1;
}

void netif_interface_init(void)
{
    memset(netif_hash, 0, sizeof(netif_hash));
}