ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netback/netback.c @ 10040:91c77df11b43

When we copy packet sin netback/netfront make sure the new skb has
all the necessary fields initialised. In particular, before we were
not copying ip_summed and that screws up checksum offload.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed May 10 17:30:42 2006 +0100 (2006-05-10)
parents 60f7b567bb2b
children 48c0f5489d44
line source
1 /******************************************************************************
2 * drivers/xen/netback/netback.c
3 *
4 * Back-end of the driver for virtual network devices. This portion of the
5 * driver exports a 'unified' network-device interface that can be accessed
6 * by any operating system that implements a compatible front end. A
7 * reference front-end implementation can be found in:
8 * drivers/xen/netfront/netfront.c
9 *
10 * Copyright (c) 2002-2005, K A Fraser
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation; or, when distributed
15 * separately from the Linux kernel or incorporated into other
16 * software packages, subject to the following license:
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this source file (the "Software"), to deal in the Software without
20 * restriction, including without limitation the rights to use, copy, modify,
21 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
22 * and to permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 * IN THE SOFTWARE.
35 */
37 #include "common.h"
38 #include <xen/balloon.h>
39 #include <xen/interface/memory.h>
41 /*#define NETBE_DEBUG_INTERRUPT*/
43 static void netif_idx_release(u16 pending_idx);
44 static void netif_page_release(struct page *page);
45 static void make_tx_response(netif_t *netif,
46 u16 id,
47 s8 st);
48 static int make_rx_response(netif_t *netif,
49 u16 id,
50 s8 st,
51 u16 offset,
52 u16 size,
53 u16 flags);
55 static void net_tx_action(unsigned long unused);
56 static DECLARE_TASKLET(net_tx_tasklet, net_tx_action, 0);
58 static void net_rx_action(unsigned long unused);
59 static DECLARE_TASKLET(net_rx_tasklet, net_rx_action, 0);
61 static struct timer_list net_timer;
63 #define MAX_PENDING_REQS 256
65 static struct sk_buff_head rx_queue;
66 static multicall_entry_t rx_mcl[NET_RX_RING_SIZE+1];
67 static mmu_update_t rx_mmu[NET_RX_RING_SIZE];
68 static gnttab_transfer_t grant_rx_op[NET_RX_RING_SIZE];
69 static unsigned char rx_notify[NR_IRQS];
71 static unsigned long mmap_vstart;
72 #define MMAP_VADDR(_req) (mmap_vstart + ((_req) * PAGE_SIZE))
74 #define PKT_PROT_LEN 64
76 static struct {
77 netif_tx_request_t req;
78 netif_t *netif;
79 } pending_tx_info[MAX_PENDING_REQS];
80 static u16 pending_ring[MAX_PENDING_REQS];
81 typedef unsigned int PEND_RING_IDX;
82 #define MASK_PEND_IDX(_i) ((_i)&(MAX_PENDING_REQS-1))
83 static PEND_RING_IDX pending_prod, pending_cons;
84 #define NR_PENDING_REQS (MAX_PENDING_REQS - pending_prod + pending_cons)
86 /* Freed TX SKBs get batched on this ring before return to pending_ring. */
87 static u16 dealloc_ring[MAX_PENDING_REQS];
88 static PEND_RING_IDX dealloc_prod, dealloc_cons;
90 static struct sk_buff_head tx_queue;
92 static grant_handle_t grant_tx_handle[MAX_PENDING_REQS];
93 static gnttab_unmap_grant_ref_t tx_unmap_ops[MAX_PENDING_REQS];
94 static gnttab_map_grant_ref_t tx_map_ops[MAX_PENDING_REQS];
96 static struct list_head net_schedule_list;
97 static spinlock_t net_schedule_list_lock;
99 #define MAX_MFN_ALLOC 64
100 static unsigned long mfn_list[MAX_MFN_ALLOC];
101 static unsigned int alloc_index = 0;
102 static spinlock_t mfn_lock = SPIN_LOCK_UNLOCKED;
104 static unsigned long alloc_mfn(void)
105 {
106 unsigned long mfn = 0, flags;
107 struct xen_memory_reservation reservation = {
108 .nr_extents = MAX_MFN_ALLOC,
109 .extent_order = 0,
110 .domid = DOMID_SELF
111 };
112 set_xen_guest_handle(reservation.extent_start, mfn_list);
113 spin_lock_irqsave(&mfn_lock, flags);
114 if ( unlikely(alloc_index == 0) )
115 alloc_index = HYPERVISOR_memory_op(
116 XENMEM_increase_reservation, &reservation);
117 if ( alloc_index != 0 )
118 mfn = mfn_list[--alloc_index];
119 spin_unlock_irqrestore(&mfn_lock, flags);
120 return mfn;
121 }
123 static inline void maybe_schedule_tx_action(void)
124 {
125 smp_mb();
126 if ((NR_PENDING_REQS < (MAX_PENDING_REQS/2)) &&
127 !list_empty(&net_schedule_list))
128 tasklet_schedule(&net_tx_tasklet);
129 }
131 /*
132 * A gross way of confirming the origin of an skb data page. The slab
133 * allocator abuses a field in the page struct to cache the kmem_cache_t ptr.
134 */
135 static inline int is_xen_skb(struct sk_buff *skb)
136 {
137 extern kmem_cache_t *skbuff_cachep;
138 kmem_cache_t *cp = (kmem_cache_t *)virt_to_page(skb->head)->lru.next;
139 return (cp == skbuff_cachep);
140 }
142 int netif_be_start_xmit(struct sk_buff *skb, struct net_device *dev)
143 {
144 netif_t *netif = netdev_priv(dev);
146 BUG_ON(skb->dev != dev);
148 /* Drop the packet if the target domain has no receive buffers. */
149 if (!netif->active ||
150 (netif->rx_req_cons_peek == netif->rx.sring->req_prod) ||
151 ((netif->rx_req_cons_peek - netif->rx.rsp_prod_pvt) ==
152 NET_RX_RING_SIZE))
153 goto drop;
155 /*
156 * We do not copy the packet unless:
157 * 1. The data is shared; or
158 * 2. The data is not allocated from our special cache.
159 * NB. We also couldn't cope with fragmented packets, but we won't get
160 * any because we not advertise the NETIF_F_SG feature.
161 */
162 if (skb_shared(skb) || skb_cloned(skb) || !is_xen_skb(skb)) {
163 int hlen = skb->data - skb->head;
164 int ret;
165 struct sk_buff *nskb = dev_alloc_skb(hlen + skb->len);
166 if ( unlikely(nskb == NULL) )
167 goto drop;
168 skb_reserve(nskb, hlen);
169 __skb_put(nskb, skb->len);
170 ret = skb_copy_bits(skb, -hlen, nskb->data - hlen,
171 skb->len + hlen);
172 BUG_ON(ret);
173 /* Copy only the header fields we use in this driver. */
174 nskb->dev = skb->dev;
175 nskb->ip_summed = skb->ip_summed;
176 nskb->proto_data_valid = skb->proto_data_valid;
177 dev_kfree_skb(skb);
178 skb = nskb;
179 }
181 netif->rx_req_cons_peek++;
182 netif_get(netif);
184 skb_queue_tail(&rx_queue, skb);
185 tasklet_schedule(&net_rx_tasklet);
187 return 0;
189 drop:
190 netif->stats.tx_dropped++;
191 dev_kfree_skb(skb);
192 return 0;
193 }
195 #if 0
196 static void xen_network_done_notify(void)
197 {
198 static struct net_device *eth0_dev = NULL;
199 if (unlikely(eth0_dev == NULL))
200 eth0_dev = __dev_get_by_name("eth0");
201 netif_rx_schedule(eth0_dev);
202 }
203 /*
204 * Add following to poll() function in NAPI driver (Tigon3 is example):
205 * if ( xen_network_done() )
206 * tg3_enable_ints(tp);
207 */
208 int xen_network_done(void)
209 {
210 return skb_queue_empty(&rx_queue);
211 }
212 #endif
214 static void net_rx_action(unsigned long unused)
215 {
216 netif_t *netif = NULL;
217 s8 status;
218 u16 size, id, irq, flags;
219 multicall_entry_t *mcl;
220 mmu_update_t *mmu;
221 gnttab_transfer_t *gop;
222 unsigned long vdata, old_mfn, new_mfn;
223 struct sk_buff_head rxq;
224 struct sk_buff *skb;
225 u16 notify_list[NET_RX_RING_SIZE];
226 int notify_nr = 0;
227 int ret;
229 skb_queue_head_init(&rxq);
231 mcl = rx_mcl;
232 mmu = rx_mmu;
233 gop = grant_rx_op;
235 while ((skb = skb_dequeue(&rx_queue)) != NULL) {
236 netif = netdev_priv(skb->dev);
237 vdata = (unsigned long)skb->data;
238 old_mfn = virt_to_mfn(vdata);
240 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
241 /* Memory squeeze? Back off for an arbitrary while. */
242 if ((new_mfn = alloc_mfn()) == 0) {
243 if ( net_ratelimit() )
244 WPRINTK("Memory squeeze in netback "
245 "driver.\n");
246 mod_timer(&net_timer, jiffies + HZ);
247 skb_queue_head(&rx_queue, skb);
248 break;
249 }
250 /*
251 * Set the new P2M table entry before reassigning
252 * the old data page. Heed the comment in
253 * pgtable-2level.h:pte_page(). :-)
254 */
255 set_phys_to_machine(
256 __pa(skb->data) >> PAGE_SHIFT,
257 new_mfn);
259 MULTI_update_va_mapping(mcl, vdata,
260 pfn_pte_ma(new_mfn,
261 PAGE_KERNEL), 0);
262 mcl++;
264 mmu->ptr = ((maddr_t)new_mfn << PAGE_SHIFT) |
265 MMU_MACHPHYS_UPDATE;
266 mmu->val = __pa(vdata) >> PAGE_SHIFT;
267 mmu++;
268 }
270 gop->mfn = old_mfn;
271 gop->domid = netif->domid;
272 gop->ref = RING_GET_REQUEST(
273 &netif->rx, netif->rx.req_cons)->gref;
274 netif->rx.req_cons++;
275 gop++;
277 __skb_queue_tail(&rxq, skb);
279 /* Filled the batch queue? */
280 if ((gop - grant_rx_op) == ARRAY_SIZE(grant_rx_op))
281 break;
282 }
284 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
285 if (mcl == rx_mcl)
286 return;
288 mcl[-1].args[MULTI_UVMFLAGS_INDEX] = UVMF_TLB_FLUSH|UVMF_ALL;
290 if (mmu - rx_mmu) {
291 mcl->op = __HYPERVISOR_mmu_update;
292 mcl->args[0] = (unsigned long)rx_mmu;
293 mcl->args[1] = mmu - rx_mmu;
294 mcl->args[2] = 0;
295 mcl->args[3] = DOMID_SELF;
296 mcl++;
297 }
299 ret = HYPERVISOR_multicall(rx_mcl, mcl - rx_mcl);
300 BUG_ON(ret != 0);
301 }
303 ret = HYPERVISOR_grant_table_op(GNTTABOP_transfer, grant_rx_op,
304 gop - grant_rx_op);
305 BUG_ON(ret != 0);
307 mcl = rx_mcl;
308 gop = grant_rx_op;
309 while ((skb = __skb_dequeue(&rxq)) != NULL) {
310 netif = netdev_priv(skb->dev);
311 size = skb->tail - skb->data;
313 atomic_set(&(skb_shinfo(skb)->dataref), 1);
314 skb_shinfo(skb)->nr_frags = 0;
315 skb_shinfo(skb)->frag_list = NULL;
317 netif->stats.tx_bytes += size;
318 netif->stats.tx_packets++;
320 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
321 /* The update_va_mapping() must not fail. */
322 BUG_ON(mcl->result != 0);
323 mcl++;
324 }
326 /* Check the reassignment error code. */
327 status = NETIF_RSP_OKAY;
328 if (gop->status != 0) {
329 DPRINTK("Bad status %d from grant transfer to DOM%u\n",
330 gop->status, netif->domid);
331 /*
332 * Page no longer belongs to us unless GNTST_bad_page,
333 * but that should be a fatal error anyway.
334 */
335 BUG_ON(gop->status == GNTST_bad_page);
336 status = NETIF_RSP_ERROR;
337 }
338 irq = netif->irq;
339 id = RING_GET_REQUEST(&netif->rx, netif->rx.rsp_prod_pvt)->id;
340 flags = 0;
341 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
342 flags |= NETRXF_csum_blank | NETRXF_data_validated;
343 else if (skb->proto_data_valid) /* remote but checksummed? */
344 flags |= NETRXF_data_validated;
345 if (make_rx_response(netif, id, status,
346 (unsigned long)skb->data & ~PAGE_MASK,
347 size, flags) &&
348 (rx_notify[irq] == 0)) {
349 rx_notify[irq] = 1;
350 notify_list[notify_nr++] = irq;
351 }
353 netif_put(netif);
354 dev_kfree_skb(skb);
355 gop++;
356 }
358 while (notify_nr != 0) {
359 irq = notify_list[--notify_nr];
360 rx_notify[irq] = 0;
361 notify_remote_via_irq(irq);
362 }
364 /* More work to do? */
365 if (!skb_queue_empty(&rx_queue) && !timer_pending(&net_timer))
366 tasklet_schedule(&net_rx_tasklet);
367 #if 0
368 else
369 xen_network_done_notify();
370 #endif
371 }
373 static void net_alarm(unsigned long unused)
374 {
375 tasklet_schedule(&net_rx_tasklet);
376 }
378 struct net_device_stats *netif_be_get_stats(struct net_device *dev)
379 {
380 netif_t *netif = netdev_priv(dev);
381 return &netif->stats;
382 }
384 static int __on_net_schedule_list(netif_t *netif)
385 {
386 return netif->list.next != NULL;
387 }
389 static void remove_from_net_schedule_list(netif_t *netif)
390 {
391 spin_lock_irq(&net_schedule_list_lock);
392 if (likely(__on_net_schedule_list(netif))) {
393 list_del(&netif->list);
394 netif->list.next = NULL;
395 netif_put(netif);
396 }
397 spin_unlock_irq(&net_schedule_list_lock);
398 }
400 static void add_to_net_schedule_list_tail(netif_t *netif)
401 {
402 if (__on_net_schedule_list(netif))
403 return;
405 spin_lock_irq(&net_schedule_list_lock);
406 if (!__on_net_schedule_list(netif) && netif->active) {
407 list_add_tail(&netif->list, &net_schedule_list);
408 netif_get(netif);
409 }
410 spin_unlock_irq(&net_schedule_list_lock);
411 }
413 /*
414 * Note on CONFIG_XEN_NETDEV_PIPELINED_TRANSMITTER:
415 * If this driver is pipelining transmit requests then we can be very
416 * aggressive in avoiding new-packet notifications -- frontend only needs to
417 * send a notification if there are no outstanding unreceived responses.
418 * If we may be buffer transmit buffers for any reason then we must be rather
419 * more conservative and treat this as the final check for pending work.
420 */
421 void netif_schedule_work(netif_t *netif)
422 {
423 int more_to_do;
425 #ifdef CONFIG_XEN_NETDEV_PIPELINED_TRANSMITTER
426 more_to_do = RING_HAS_UNCONSUMED_REQUESTS(&netif->tx);
427 #else
428 RING_FINAL_CHECK_FOR_REQUESTS(&netif->tx, more_to_do);
429 #endif
431 if (more_to_do) {
432 add_to_net_schedule_list_tail(netif);
433 maybe_schedule_tx_action();
434 }
435 }
437 void netif_deschedule_work(netif_t *netif)
438 {
439 remove_from_net_schedule_list(netif);
440 }
443 static void tx_credit_callback(unsigned long data)
444 {
445 netif_t *netif = (netif_t *)data;
446 netif->remaining_credit = netif->credit_bytes;
447 netif_schedule_work(netif);
448 }
450 inline static void net_tx_action_dealloc(void)
451 {
452 gnttab_unmap_grant_ref_t *gop;
453 u16 pending_idx;
454 PEND_RING_IDX dc, dp;
455 netif_t *netif;
456 int ret;
458 dc = dealloc_cons;
459 dp = dealloc_prod;
461 /*
462 * Free up any grants we have finished using
463 */
464 gop = tx_unmap_ops;
465 while (dc != dp) {
466 pending_idx = dealloc_ring[MASK_PEND_IDX(dc++)];
467 gnttab_set_unmap_op(gop, MMAP_VADDR(pending_idx),
468 GNTMAP_host_map,
469 grant_tx_handle[pending_idx]);
470 gop++;
471 }
472 ret = HYPERVISOR_grant_table_op(
473 GNTTABOP_unmap_grant_ref, tx_unmap_ops, gop - tx_unmap_ops);
474 BUG_ON(ret);
476 while (dealloc_cons != dp) {
477 pending_idx = dealloc_ring[MASK_PEND_IDX(dealloc_cons++)];
479 netif = pending_tx_info[pending_idx].netif;
481 make_tx_response(netif, pending_tx_info[pending_idx].req.id,
482 NETIF_RSP_OKAY);
484 pending_ring[MASK_PEND_IDX(pending_prod++)] = pending_idx;
486 netif_put(netif);
487 }
488 }
490 /* Called after netfront has transmitted */
491 static void net_tx_action(unsigned long unused)
492 {
493 struct list_head *ent;
494 struct sk_buff *skb;
495 netif_t *netif;
496 netif_tx_request_t txreq;
497 u16 pending_idx;
498 RING_IDX i;
499 gnttab_map_grant_ref_t *mop;
500 unsigned int data_len;
501 int ret, work_to_do;
503 if (dealloc_cons != dealloc_prod)
504 net_tx_action_dealloc();
506 mop = tx_map_ops;
507 while ((NR_PENDING_REQS < MAX_PENDING_REQS) &&
508 !list_empty(&net_schedule_list)) {
509 /* Get a netif from the list with work to do. */
510 ent = net_schedule_list.next;
511 netif = list_entry(ent, netif_t, list);
512 netif_get(netif);
513 remove_from_net_schedule_list(netif);
515 RING_FINAL_CHECK_FOR_REQUESTS(&netif->tx, work_to_do);
516 if (!work_to_do) {
517 netif_put(netif);
518 continue;
519 }
521 i = netif->tx.req_cons;
522 rmb(); /* Ensure that we see the request before we copy it. */
523 memcpy(&txreq, RING_GET_REQUEST(&netif->tx, i), sizeof(txreq));
524 /* Credit-based scheduling. */
525 if (txreq.size > netif->remaining_credit) {
526 unsigned long now = jiffies;
527 unsigned long next_credit =
528 netif->credit_timeout.expires +
529 msecs_to_jiffies(netif->credit_usec / 1000);
531 /* Timer could already be pending in rare cases. */
532 if (timer_pending(&netif->credit_timeout))
533 break;
535 /* Passed the point where we can replenish credit? */
536 if (time_after_eq(now, next_credit)) {
537 netif->credit_timeout.expires = now;
538 netif->remaining_credit = netif->credit_bytes;
539 }
541 /* Still too big to send right now? Set a callback. */
542 if (txreq.size > netif->remaining_credit) {
543 netif->remaining_credit = 0;
544 netif->credit_timeout.data =
545 (unsigned long)netif;
546 netif->credit_timeout.function =
547 tx_credit_callback;
548 __mod_timer(&netif->credit_timeout,
549 next_credit);
550 break;
551 }
552 }
553 netif->remaining_credit -= txreq.size;
555 netif->tx.req_cons++;
557 netif_schedule_work(netif);
559 if (unlikely(txreq.size < ETH_HLEN) ||
560 unlikely(txreq.size > ETH_FRAME_LEN)) {
561 DPRINTK("Bad packet size: %d\n", txreq.size);
562 make_tx_response(netif, txreq.id, NETIF_RSP_ERROR);
563 netif_put(netif);
564 continue;
565 }
567 /* No crossing a page as the payload mustn't fragment. */
568 if (unlikely((txreq.offset + txreq.size) >= PAGE_SIZE)) {
569 DPRINTK("txreq.offset: %x, size: %u, end: %lu\n",
570 txreq.offset, txreq.size,
571 (txreq.offset &~PAGE_MASK) + txreq.size);
572 make_tx_response(netif, txreq.id, NETIF_RSP_ERROR);
573 netif_put(netif);
574 continue;
575 }
577 pending_idx = pending_ring[MASK_PEND_IDX(pending_cons)];
579 data_len = (txreq.size > PKT_PROT_LEN) ?
580 PKT_PROT_LEN : txreq.size;
582 skb = alloc_skb(data_len+16, GFP_ATOMIC);
583 if (unlikely(skb == NULL)) {
584 DPRINTK("Can't allocate a skb in start_xmit.\n");
585 make_tx_response(netif, txreq.id, NETIF_RSP_ERROR);
586 netif_put(netif);
587 break;
588 }
590 /* Packets passed to netif_rx() must have some headroom. */
591 skb_reserve(skb, 16);
593 gnttab_set_map_op(mop, MMAP_VADDR(pending_idx),
594 GNTMAP_host_map | GNTMAP_readonly,
595 txreq.gref, netif->domid);
596 mop++;
598 memcpy(&pending_tx_info[pending_idx].req,
599 &txreq, sizeof(txreq));
600 pending_tx_info[pending_idx].netif = netif;
601 *((u16 *)skb->data) = pending_idx;
603 __skb_queue_tail(&tx_queue, skb);
605 pending_cons++;
607 if ((mop - tx_map_ops) >= ARRAY_SIZE(tx_map_ops))
608 break;
609 }
611 if (mop == tx_map_ops)
612 return;
614 ret = HYPERVISOR_grant_table_op(
615 GNTTABOP_map_grant_ref, tx_map_ops, mop - tx_map_ops);
616 BUG_ON(ret);
618 mop = tx_map_ops;
619 while ((skb = __skb_dequeue(&tx_queue)) != NULL) {
620 pending_idx = *((u16 *)skb->data);
621 netif = pending_tx_info[pending_idx].netif;
622 memcpy(&txreq, &pending_tx_info[pending_idx].req,
623 sizeof(txreq));
625 /* Check the remap error code. */
626 if (unlikely(mop->status)) {
627 printk(KERN_ALERT "#### netback grant fails\n");
628 make_tx_response(netif, txreq.id, NETIF_RSP_ERROR);
629 netif_put(netif);
630 kfree_skb(skb);
631 mop++;
632 pending_ring[MASK_PEND_IDX(pending_prod++)] =
633 pending_idx;
634 continue;
635 }
636 set_phys_to_machine(
637 __pa(MMAP_VADDR(pending_idx)) >> PAGE_SHIFT,
638 FOREIGN_FRAME(mop->dev_bus_addr >> PAGE_SHIFT));
639 grant_tx_handle[pending_idx] = mop->handle;
641 data_len = (txreq.size > PKT_PROT_LEN) ?
642 PKT_PROT_LEN : txreq.size;
644 __skb_put(skb, data_len);
645 memcpy(skb->data,
646 (void *)(MMAP_VADDR(pending_idx)|txreq.offset),
647 data_len);
648 if (data_len < txreq.size) {
649 /* Append the packet payload as a fragment. */
650 skb_shinfo(skb)->frags[0].page =
651 virt_to_page(MMAP_VADDR(pending_idx));
652 skb_shinfo(skb)->frags[0].size =
653 txreq.size - data_len;
654 skb_shinfo(skb)->frags[0].page_offset =
655 txreq.offset + data_len;
656 skb_shinfo(skb)->nr_frags = 1;
657 } else {
658 /* Schedule a response immediately. */
659 netif_idx_release(pending_idx);
660 }
662 skb->data_len = txreq.size - data_len;
663 skb->len += skb->data_len;
664 skb->truesize += skb->data_len;
666 skb->dev = netif->dev;
667 skb->protocol = eth_type_trans(skb, skb->dev);
669 /*
670 * Old frontends do not assert data_validated but we
671 * can infer it from csum_blank so test both flags.
672 */
673 if (txreq.flags & (NETTXF_data_validated|NETTXF_csum_blank)) {
674 skb->ip_summed = CHECKSUM_UNNECESSARY;
675 skb->proto_data_valid = 1;
676 } else {
677 skb->ip_summed = CHECKSUM_NONE;
678 skb->proto_data_valid = 0;
679 }
680 skb->proto_csum_blank = !!(txreq.flags & NETTXF_csum_blank);
682 netif->stats.rx_bytes += txreq.size;
683 netif->stats.rx_packets++;
685 netif_rx(skb);
686 netif->dev->last_rx = jiffies;
688 mop++;
689 }
690 }
692 static void netif_idx_release(u16 pending_idx)
693 {
694 static spinlock_t _lock = SPIN_LOCK_UNLOCKED;
695 unsigned long flags;
697 spin_lock_irqsave(&_lock, flags);
698 dealloc_ring[MASK_PEND_IDX(dealloc_prod++)] = pending_idx;
699 spin_unlock_irqrestore(&_lock, flags);
701 tasklet_schedule(&net_tx_tasklet);
702 }
704 static void netif_page_release(struct page *page)
705 {
706 u16 pending_idx = page - virt_to_page(mmap_vstart);
708 /* Ready for next use. */
709 set_page_count(page, 1);
711 netif_idx_release(pending_idx);
712 }
714 irqreturn_t netif_be_int(int irq, void *dev_id, struct pt_regs *regs)
715 {
716 netif_t *netif = dev_id;
717 add_to_net_schedule_list_tail(netif);
718 maybe_schedule_tx_action();
719 return IRQ_HANDLED;
720 }
722 static void make_tx_response(netif_t *netif,
723 u16 id,
724 s8 st)
725 {
726 RING_IDX i = netif->tx.rsp_prod_pvt;
727 netif_tx_response_t *resp;
728 int notify;
730 resp = RING_GET_RESPONSE(&netif->tx, i);
731 resp->id = id;
732 resp->status = st;
734 netif->tx.rsp_prod_pvt = ++i;
735 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netif->tx, notify);
736 if (notify)
737 notify_remote_via_irq(netif->irq);
739 #ifdef CONFIG_XEN_NETDEV_PIPELINED_TRANSMITTER
740 if (i == netif->tx.req_cons) {
741 int more_to_do;
742 RING_FINAL_CHECK_FOR_REQUESTS(&netif->tx, more_to_do);
743 if (more_to_do)
744 add_to_net_schedule_list_tail(netif);
745 }
746 #endif
747 }
749 static int make_rx_response(netif_t *netif,
750 u16 id,
751 s8 st,
752 u16 offset,
753 u16 size,
754 u16 flags)
755 {
756 RING_IDX i = netif->rx.rsp_prod_pvt;
757 netif_rx_response_t *resp;
758 int notify;
760 resp = RING_GET_RESPONSE(&netif->rx, i);
761 resp->offset = offset;
762 resp->flags = flags;
763 resp->id = id;
764 resp->status = (s16)size;
765 if (st < 0)
766 resp->status = (s16)st;
768 netif->rx.rsp_prod_pvt = ++i;
769 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netif->rx, notify);
771 return notify;
772 }
774 #ifdef NETBE_DEBUG_INTERRUPT
775 static irqreturn_t netif_be_dbg(int irq, void *dev_id, struct pt_regs *regs)
776 {
777 struct list_head *ent;
778 netif_t *netif;
779 int i = 0;
781 printk(KERN_ALERT "netif_schedule_list:\n");
782 spin_lock_irq(&net_schedule_list_lock);
784 list_for_each (ent, &net_schedule_list) {
785 netif = list_entry(ent, netif_t, list);
786 printk(KERN_ALERT " %d: private(rx_req_cons=%08x "
787 "rx_resp_prod=%08x\n",
788 i, netif->rx.req_cons, netif->rx.rsp_prod_pvt);
789 printk(KERN_ALERT " tx_req_cons=%08x tx_resp_prod=%08x)\n",
790 netif->tx.req_cons, netif->tx.rsp_prod_pvt);
791 printk(KERN_ALERT " shared(rx_req_prod=%08x "
792 "rx_resp_prod=%08x\n",
793 netif->rx.sring->req_prod, netif->rx.sring->rsp_prod);
794 printk(KERN_ALERT " rx_event=%08x tx_req_prod=%08x\n",
795 netif->rx.sring->rsp_event, netif->tx.sring->req_prod);
796 printk(KERN_ALERT " tx_resp_prod=%08x, tx_event=%08x)\n",
797 netif->tx.sring->rsp_prod, netif->tx.sring->rsp_event);
798 i++;
799 }
801 spin_unlock_irq(&net_schedule_list_lock);
802 printk(KERN_ALERT " ** End of netif_schedule_list **\n");
804 return IRQ_HANDLED;
805 }
806 #endif
808 static int __init netback_init(void)
809 {
810 int i;
811 struct page *page;
813 /* We can increase reservation by this much in net_rx_action(). */
814 balloon_update_driver_allowance(NET_RX_RING_SIZE);
816 skb_queue_head_init(&rx_queue);
817 skb_queue_head_init(&tx_queue);
819 init_timer(&net_timer);
820 net_timer.data = 0;
821 net_timer.function = net_alarm;
823 page = balloon_alloc_empty_page_range(MAX_PENDING_REQS);
824 BUG_ON(page == NULL);
825 mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
827 for (i = 0; i < MAX_PENDING_REQS; i++) {
828 page = virt_to_page(MMAP_VADDR(i));
829 set_page_count(page, 1);
830 SetPageForeign(page, netif_page_release);
831 }
833 pending_cons = 0;
834 pending_prod = MAX_PENDING_REQS;
835 for (i = 0; i < MAX_PENDING_REQS; i++)
836 pending_ring[i] = i;
838 spin_lock_init(&net_schedule_list_lock);
839 INIT_LIST_HEAD(&net_schedule_list);
841 netif_xenbus_init();
843 #ifdef NETBE_DEBUG_INTERRUPT
844 (void)bind_virq_to_irqhandler(
845 VIRQ_DEBUG,
846 0,
847 netif_be_dbg,
848 SA_SHIRQ,
849 "net-be-dbg",
850 &netif_be_dbg);
851 #endif
853 return 0;
854 }
856 module_init(netback_init);
858 MODULE_LICENSE("Dual BSD/GPL");
860 /*
861 * Local variables:
862 * c-file-style: "linux"
863 * indent-tabs-mode: t
864 * c-indent-level: 8
865 * c-basic-offset: 8
866 * tab-width: 8
867 * End:
868 */