38 #include <linux/if_vlan.h>
39 #include <linux/udp.h>
44 #include <xen/events.h>
47 #include <asm/xen/hypercall.h>
48 #include <asm/xen/page.h>
62 #define MAX_PENDING_REQS 256
65 #define INVALID_PENDING_IDX 0xFFFF
67 #define MAX_BUFFER_OFFSET PAGE_SIZE
72 #if BITS_PER_LONG < 64
74 #define GROUP_WIDTH (BITS_PER_LONG - IDX_WIDTH)
119 static int xen_netbk_group_nr;
124 int min_netfront_count;
126 struct xen_netbk *netbk;
129 for (i = 0; i < xen_netbk_group_nr; i++) {
130 int netfront_count =
atomic_read(&xen_netbk[i].netfront_count);
131 if (netfront_count < min_netfront_count) {
137 netbk = &xen_netbk[min_group];
145 struct xen_netbk *netbk = vif->
netbk;
150 static void xen_netbk_idx_release(
struct xen_netbk *netbk,
u16 pending_idx);
151 static void make_tx_response(
struct xenvif *vif,
161 static inline unsigned long idx_to_pfn(
struct xen_netbk *netbk,
167 static inline unsigned long idx_to_kaddr(
struct xen_netbk *netbk,
170 return (
unsigned long)
pfn_to_kaddr(idx_to_pfn(netbk, idx));
174 static inline void set_page_ext(
struct page *
pg,
struct xen_netbk *netbk,
177 unsigned int group = netbk - xen_netbk;
178 union page_ext ext = { .e = { .group = group + 1, .idx = idx } };
184 static int get_page_ext(
struct page *pg,
185 unsigned int *
pgroup,
unsigned int *pidx)
188 struct xen_netbk *netbk;
191 group = ext.
e.group - 1;
193 if (group < 0 || group >= xen_netbk_group_nr)
196 netbk = &xen_netbk[
group];
217 #define PKT_PROT_LEN (ETH_HLEN + \
219 sizeof(struct iphdr) + MAX_IPOPTLEN + \
220 sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE)
243 static void xen_netbk_kick_thread(
struct xen_netbk *netbk)
248 static int max_required_rx_slots(
struct xenvif *vif)
261 RING_IDX needed = max_required_rx_slots(vif);
263 return ((vif->
rx.sring->req_prod - peek) < needed) ||
273 max_required_rx_slots(vif);
284 static bool start_new_rx_buffer(
int offset,
unsigned long size,
int head)
333 if (skb_shinfo(skb)->gso_size)
336 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
337 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
338 unsigned long offset = skb_shinfo(skb)->frags[
i].page_offset;
352 if (start_new_rx_buffer(copy_off, bytes, 0)) {
406 struct page *
page,
unsigned long size,
407 unsigned long offset,
int *head)
416 int foreign = get_page_ext(page, &group, &idx);
437 if (start_new_rx_buffer(npo->
copy_off, bytes, *head)) {
444 meta = get_next_rx_buffer(vif, npo);
453 struct xen_netbk *netbk = &xen_netbk[
group];
458 copy_gop->
source.domid = src_pend->
vif->domid;
459 copy_gop->
source.u.ref = src_pend->
req.gref;
481 BUG_ON(!PageCompound(page));
487 if (*head && skb_shinfo(skb)->gso_size && !vif->
gso_prefix)
507 static int netbk_gop_skb(
struct sk_buff *skb,
510 struct xenvif *vif = netdev_priv(skb->
dev);
511 int nr_frags = skb_shinfo(skb)->nr_frags;
525 meta->
gso_size = skb_shinfo(skb)->gso_size;
534 meta->
gso_size = skb_shinfo(skb)->gso_size;
544 while (data < skb_tail_pointer(skb)) {
548 if (data + len > skb_tail_pointer(skb))
549 len = skb_tail_pointer(skb) -
data;
551 netbk_gop_frag_copy(vif, skb, npo,
556 for (i = 0; i < nr_frags; i++) {
557 netbk_gop_frag_copy(vif, skb, npo,
558 skb_frag_page(&skb_shinfo(skb)->frags[i]),
559 skb_frag_size(&skb_shinfo(skb)->frags[i]),
573 static int netbk_check_gop(
struct xenvif *vif,
int nr_meta_slots,
580 for (i = 0; i < nr_meta_slots; i++) {
584 "Bad status %d from copy to DOM%d.\n",
593 static void netbk_add_frag_responses(
struct xenvif *vif,
int status,
601 if (nr_meta_slots <= 1)
606 for (i = 0; i < nr_meta_slots; i++) {
608 if (i == nr_meta_slots - 1)
614 make_rx_response(vif, meta[i].
id, status, offset,
615 meta[i].size, flags);
623 static void xen_netbk_rx_action(
struct xen_netbk *netbk)
643 skb_queue_head_init(&rxq);
648 vif = netdev_priv(skb->
dev);
649 nr_frags = skb_shinfo(skb)->nr_frags;
654 count += nr_frags + 1;
656 __skb_queue_tail(&rxq, skb);
671 while ((skb = __skb_dequeue(&rxq)) !=
NULL) {
674 vif = netdev_priv(skb->
dev);
678 vif->
rx.rsp_prod_pvt++);
691 vif->
dev->stats.tx_bytes += skb->
len;
692 vif->
dev->stats.tx_packets++;
717 vif->
rx.rsp_prod_pvt++);
724 gso->
u.
gso.features = 0;
730 netbk_add_frag_responses(vif, status,
752 if (!skb_queue_empty(&netbk->
rx_queue) &&
754 xen_netbk_kick_thread(netbk);
759 struct xen_netbk *netbk = vif->
netbk;
763 xen_netbk_kick_thread(netbk);
766 static void xen_netbk_alarm(
unsigned long data)
768 struct xen_netbk *netbk = (
struct xen_netbk *)data;
769 xen_netbk_kick_thread(netbk);
772 static int __on_net_schedule_list(
struct xenvif *vif)
778 static void remove_from_net_schedule_list(
struct xenvif *vif)
780 if (
likely(__on_net_schedule_list(vif))) {
786 static struct xenvif *poll_net_schedule_list(
struct xen_netbk *netbk)
801 remove_from_net_schedule_list(vif);
810 struct xen_netbk *netbk = vif->
netbk;
812 if (__on_net_schedule_list(vif))
816 if (!__on_net_schedule_list(vif) &&
827 xen_netbk_kick_thread(netbk);
832 struct xen_netbk *netbk = vif->
netbk;
834 remove_from_net_schedule_list(vif);
848 static void tx_add_credit(
struct xenvif *vif)
850 unsigned long max_burst, max_credit;
857 max_burst =
min(max_burst, 131072
UL);
862 if (max_credit < vif->remaining_credit)
868 static void tx_credit_callback(
unsigned long data)
875 static void netbk_tx_err(
struct xenvif *vif,
891 static int netbk_count_requests(
struct xenvif *vif,
903 if (frags >= work_to_do) {
932 static struct page *xen_netbk_alloc_page(
struct xen_netbk *netbk,
940 set_page_ext(page, netbk, pending_idx);
945 static struct gnttab_copy *xen_netbk_get_requests(
struct xen_netbk *netbk,
957 start = (frag_get_pending_idx(&shinfo->
frags[0]) == pending_idx);
959 for (i = start; i < shinfo->
nr_frags; i++, txp++) {
967 page = xen_netbk_alloc_page(netbk, skb, pending_idx);
984 memcpy(&pending_tx_info[pending_idx].req, txp,
sizeof(*txp));
986 pending_tx_info[pending_idx].
vif =
vif;
987 frag_set_pending_idx(&frags[i], pending_idx);
993 static int xen_netbk_tx_check_gop(
struct xen_netbk *netbk,
1000 struct xenvif *vif = pending_tx_info[pending_idx].
vif;
1011 txp = &pending_tx_info[pending_idx].
req;
1018 start = (frag_get_pending_idx(&shinfo->
frags[0]) == pending_idx);
1020 for (i = start; i <
nr_frags; i++) {
1024 pending_idx = frag_get_pending_idx(&shinfo->
frags[i]);
1027 newerr = (++gop)->status;
1031 xen_netbk_idx_release(netbk, pending_idx);
1047 pending_idx = *((
u16 *)skb->
data);
1048 xen_netbk_idx_release(netbk, pending_idx);
1049 for (j = start; j <
i; j++) {
1050 pending_idx = frag_get_pending_idx(&shinfo->
frags[j]);
1051 xen_netbk_idx_release(netbk, pending_idx);
1062 static void xen_netbk_fill_frags(
struct xen_netbk *netbk,
struct sk_buff *skb)
1074 pending_idx = frag_get_pending_idx(frag);
1078 __skb_fill_page_desc(skb, i, page, txp->
offset, txp->
size);
1085 xen_netbk_idx_release(netbk, pending_idx);
1089 static int xen_netbk_get_extras(
struct xenvif *vif,
1106 vif->
tx.req_cons = ++
cons;
1108 "Invalid extra type: %d\n",
extra.type);
1113 vif->
tx.req_cons = ++
cons;
1119 static int netbk_set_skb_gso(
struct xenvif *vif,
1123 if (!gso->
u.
gso.size) {
1134 skb_shinfo(skb)->gso_size = gso->
u.
gso.size;
1139 skb_shinfo(skb)->gso_segs = 0;
1144 static int checksum_setup(
struct xenvif *vif,
struct sk_buff *skb)
1149 int recalculate_partial_csum = 0;
1160 recalculate_partial_csum = 1;
1170 iph = (
void *)skb->
data;
1171 th = skb->
data + 4 * iph->ihl;
1172 if (th >= skb_tail_pointer(skb))
1180 if (recalculate_partial_csum) {
1183 skb->
len - iph->ihl*4,
1190 if (recalculate_partial_csum) {
1193 skb->
len - iph->ihl*4,
1199 netdev_err(vif->
dev,
1200 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1205 if ((th + skb->
csum_offset + 2) > skb_tail_pointer(skb))
1214 static bool tx_credit_exceeded(
struct xenvif *vif,
unsigned size)
1217 unsigned long next_credit =
1246 static unsigned xen_netbk_tx_build_gops(
struct xen_netbk *netbk)
1266 vif = poll_net_schedule_list(netbk);
1276 idx = vif->
tx.req_cons;
1282 tx_credit_exceeded(vif, txreq.size)) {
1290 vif->
tx.req_cons = ++
idx;
1292 memset(extras, 0,
sizeof(extras));
1294 work_to_do = xen_netbk_get_extras(vif, extras,
1296 idx = vif->
tx.req_cons;
1298 netbk_tx_err(vif, &txreq, idx);
1303 ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
1305 netbk_tx_err(vif, &txreq, idx - ret);
1312 "Bad packet size: %d\n", txreq.size);
1313 netbk_tx_err(vif, &txreq, idx);
1320 "txreq.offset: %x, size: %u, end: %lu\n",
1321 txreq.offset, txreq.size,
1322 (txreq.offset&~
PAGE_MASK) + txreq.size);
1323 netbk_tx_err(vif, &txreq, idx);
1338 "Can't allocate a skb in start_xmit.\n");
1339 netbk_tx_err(vif, &txreq, idx);
1350 if (netbk_set_skb_gso(vif, skb, gso)) {
1352 netbk_tx_err(vif, &txreq, idx);
1358 page = xen_netbk_alloc_page(netbk, skb, pending_idx);
1361 netbk_tx_err(vif, &txreq, idx);
1365 gop->
source.u.ref = txreq.gref;
1367 gop->
source.offset = txreq.offset;
1371 gop->
dest.offset = txreq.offset;
1373 gop->
len = txreq.size;
1379 &txreq,
sizeof(txreq));
1381 *((
u16 *)skb->
data) = pending_idx;
1383 __skb_put(skb, data_len);
1385 skb_shinfo(skb)->nr_frags =
ret;
1386 if (data_len < txreq.size) {
1387 skb_shinfo(skb)->nr_frags++;
1388 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1391 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1397 request_gop = xen_netbk_get_requests(netbk, vif,
1399 if (request_gop ==
NULL) {
1401 netbk_tx_err(vif, &txreq, idx);
1406 __skb_queue_tail(&netbk->
tx_queue, skb);
1408 vif->
tx.req_cons =
idx;
1418 static void xen_netbk_tx_submit(
struct xen_netbk *netbk)
1423 while ((skb = __skb_dequeue(&netbk->
tx_queue)) !=
NULL) {
1429 pending_idx = *((
u16 *)skb->
data);
1434 if (
unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
1436 skb_shinfo(skb)->nr_frags = 0;
1441 data_len = skb->
len;
1443 (
void *)(idx_to_kaddr(netbk, pending_idx)|txp->
offset),
1445 if (data_len < txp->size) {
1451 xen_netbk_idx_release(netbk, pending_idx);
1459 xen_netbk_fill_frags(netbk, skb);
1466 if (skb_headlen(skb) <
PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1474 if (checksum_setup(vif, skb)) {
1476 "Can't setup checksum in net_tx_action\n");
1481 vif->
dev->stats.rx_bytes += skb->
len;
1482 vif->
dev->stats.rx_packets++;
1489 static void xen_netbk_tx_action(
struct xen_netbk *netbk)
1493 nr_gops = xen_netbk_tx_build_gops(netbk);
1500 xen_netbk_tx_submit(netbk);
1503 static void xen_netbk_idx_release(
struct xen_netbk *netbk,
u16 pending_idx)
1506 struct pending_tx_info *pending_tx_info;
1515 vif = pending_tx_info->
vif;
1529 static void make_tx_response(
struct xenvif *vif,
1544 vif->
tx.rsp_prod_pvt = ++
i;
1568 vif->
rx.rsp_prod_pvt = ++
i;
1573 static inline int rx_work_todo(
struct xen_netbk *netbk)
1575 return !skb_queue_empty(&netbk->
rx_queue);
1578 static inline int tx_work_todo(
struct xen_netbk *netbk)
1588 static int xen_netbk_kthread(
void *data)
1590 struct xen_netbk *netbk =
data;
1593 rx_work_todo(netbk) ||
1594 tx_work_todo(netbk) ||
1601 if (rx_work_todo(netbk))
1602 xen_netbk_rx_action(netbk);
1604 if (tx_work_todo(netbk))
1605 xen_netbk_tx_action(netbk);
1626 struct xen_netif_tx_sring *txs;
1627 struct xen_netif_rx_sring *rxs;
1632 tx_ring_ref, &addr);
1636 txs = (
struct xen_netif_tx_sring *)addr;
1640 rx_ring_ref, &addr);
1644 rxs = (
struct xen_netif_rx_sring *)addr;
1656 static int __init netback_init(
void)
1666 xen_netbk =
vzalloc(
sizeof(
struct xen_netbk) * xen_netbk_group_nr);
1670 for (group = 0; group < xen_netbk_group_nr; group++) {
1671 struct xen_netbk *netbk = &xen_netbk[
group];
1672 skb_queue_head_init(&netbk->
rx_queue);
1673 skb_queue_head_init(&netbk->
tx_queue);
1677 netbk->
net_timer.function = xen_netbk_alarm;
1687 "netback/%u", group);
1689 if (IS_ERR(netbk->
task)) {
1692 rc = PTR_ERR(netbk->
task);
1714 while (--group >= 0) {
1715 struct xen_netbk *netbk = &xen_netbk[
group];