32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/netdevice.h>
37 #include <linux/ethtool.h>
38 #include <linux/if_ether.h>
39 #include <linux/tcp.h>
40 #include <linux/udp.h>
43 #include <linux/slab.h>
46 #include <asm/xen/page.h>
49 #include <xen/events.h>
64 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
66 #define RX_COPY_THRESHOLD 256
68 #define GRANT_INVALID_REF 0
70 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
71 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
72 #define TX_MAX_TARGET min_t(int, NET_TX_RING_SIZE, 256)
92 struct xen_netif_tx_front_ring
tx;
113 struct xen_netif_rx_front_ring
rx;
117 #define RX_MIN_TARGET 8
118 #define RX_DFL_MIN_TARGET 64
119 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
144 static void skb_entry_set_link(
union skb_entry *
list,
unsigned short id)
149 static int skb_entry_is_link(
const union skb_entry *
list)
159 static void add_id_to_freelist(
unsigned *
head,
union skb_entry *list,
162 skb_entry_set_link(&list[
id], *head);
166 static unsigned short get_id_from_freelist(
unsigned *
head,
167 union skb_entry *list)
169 unsigned int id = *
head;
170 *head = list[
id].link;
182 int i = xennet_rxidx(ri);
191 int i = xennet_rxidx(ri);
201 #define xennet_sysfs_addif(dev) (0)
202 #define xennet_sysfs_delif(dev) do { } while (0)
211 static void rx_refill_timeout(
unsigned long data)
215 napi_schedule(&np->
napi);
218 static int netfront_tx_slot_available(
struct netfront_info *np)
220 return (np->
tx.req_prod_pvt - np->
tx.rsp_cons) <
228 if (
unlikely(netif_queue_stopped(dev)) &&
229 netfront_tx_slot_available(np) &&
230 likely(netif_running(dev)))
231 netif_wake_queue(dev);
234 static void xennet_alloc_rx_buffers(
struct net_device *dev)
247 if (
unlikely(!netif_carrier_ok(dev)))
256 batch_target = np->
rx_target - (req_prod - np->
rx.rsp_cons);
257 for (i = skb_queue_len(&np->
rx_batch); i < batch_target; i++) {
279 __skb_fill_page_desc(skb, 0, page, 0, 0);
280 skb_shinfo(skb)->nr_frags = 1;
281 __skb_queue_tail(&np->
rx_batch, skb);
286 if (req_prod > np->
rx.sring->req_prod)
292 if (((req_prod - np->
rx.sring->rsp_prod) < (np->
rx_target / 4)) &&
304 id = xennet_rxidx(req_prod + i);
310 BUG_ON((
signed short)ref < 0);
313 pfn =
page_to_pfn(skb_frag_page(&skb_shinfo(skb)->frags[0]));
314 vaddr =
page_address(skb_frag_page(&skb_shinfo(skb)->frags[0]));
318 np->
xbdev->otherend_id,
329 np->
rx.req_prod_pvt = req_prod +
i;
336 static int xennet_open(
struct net_device *dev)
340 napi_enable(&np->
napi);
342 spin_lock_bh(&np->rx_lock);
343 if (netif_carrier_ok(dev)) {
344 xennet_alloc_rx_buffers(dev);
345 np->
rx.sring->rsp_event = np->
rx.rsp_cons + 1;
347 napi_schedule(&np->
napi);
349 spin_unlock_bh(&np->rx_lock);
351 netif_start_queue(dev);
356 static void xennet_tx_buf_gc(
struct net_device *dev)
363 BUG_ON(!netif_carrier_ok(dev));
366 prod = np->
tx.sring->rsp_prod;
369 for (cons = np->
tx.rsp_cons; cons != prod; cons++) {
381 "-- grant still in use by backend "
394 np->
tx.rsp_cons = prod;
404 np->
tx.sring->rsp_event =
405 prod + ((np->
tx.sring->req_prod - prod) >> 1) + 1;
407 }
while ((cons == prod) && (prod != np->
tx.sring->rsp_prod));
409 xennet_maybe_wake_tx(dev);
419 int frags = skb_shinfo(skb)->nr_frags;
421 unsigned int len = skb_headlen(skb);
440 BUG_ON((
signed short)ref < 0);
453 for (i = 0; i < frags; i++) {
455 struct page *page = skb_frag_page(frag);
457 len = skb_frag_size(frag);
484 BUG_ON((
signed short)ref < 0);
488 np->
xbdev->otherend_id,
501 BUG_ON(!PageCompound(page));
508 np->
tx.req_prod_pvt = prod;
515 static int xennet_count_skb_frag_slots(
struct sk_buff *skb)
517 int i, frags = skb_shinfo(skb)->nr_frags;
520 for (i = 0; i < frags; i++) {
522 unsigned long size = skb_frag_size(frag);
528 pages +=
PFN_UP(offset + size);
541 char *data = skb->
data;
548 unsigned int len = skb_headlen(skb);
552 xennet_count_skb_frag_slots(skb);
555 "xennet: skb rides the rocket: %d slots\n", slots);
561 if (
unlikely(!netif_carrier_ok(dev) ||
562 (slots > 1 && !xennet_can_sg(dev)) ||
564 spin_unlock_irqrestore(&np->
tx_lock, flags);
568 i = np->
tx.req_prod_pvt;
577 BUG_ON((
signed short)ref < 0);
594 if (skb_shinfo(skb)->gso_size) {
605 gso->
u.
gso.size = skb_shinfo(skb)->gso_size;
608 gso->
u.
gso.features = 0;
615 np->
tx.req_prod_pvt = i + 1;
617 xennet_make_frags(skb, dev, tx);
624 u64_stats_update_begin(&stats->
syncp);
627 u64_stats_update_end(&stats->
syncp);
630 xennet_tx_buf_gc(dev);
632 if (!netfront_tx_slot_available(np))
633 netif_stop_queue(dev);
635 spin_unlock_irqrestore(&np->
tx_lock, flags);
640 dev->
stats.tx_dropped++;
645 static int xennet_close(
struct net_device *dev)
648 netif_stop_queue(np->
netdev);
649 napi_disable(&np->
napi);
656 int new = xennet_rxidx(np->
rx.req_prod_pvt);
663 np->
rx.req_prod_pvt++;
682 dev_warn(dev,
"Missing extra info\n");
693 dev_warn(dev,
"Invalid extra type: %d\n",
701 skb = xennet_get_rx_skb(np, cons);
702 ref = xennet_get_rx_ref(np, cons);
703 xennet_move_rx_slot(np, skb, ref);
718 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
726 err = xennet_get_extras(np, extras, rp);
727 cons = np->
rx.rsp_cons;
734 dev_warn(dev,
"rx->offset: %x, size: %u\n",
736 xennet_move_rx_slot(np, skb, ref);
748 dev_warn(dev,
"Bad rx response id %d.\n",
759 __skb_queue_tail(list, skb);
765 if (cons + frags == rp) {
773 skb = xennet_get_rx_skb(np, cons + frags);
774 ref = xennet_get_rx_ref(np, cons + frags);
785 np->
rx.rsp_cons = cons + frags;
790 static int xennet_set_skb_gso(
struct sk_buff *skb,
793 if (!gso->
u.
gso.size) {
806 skb_shinfo(skb)->gso_size = gso->
u.
gso.size;
811 skb_shinfo(skb)->gso_segs = 0;
825 while ((nskb = __skb_dequeue(list))) {
828 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
830 __skb_fill_page_desc(skb, nr_frags,
831 skb_frag_page(nfrag),
836 skb_shinfo(nskb)->nr_frags = 0;
851 int recalculate_partial_csum = 0;
863 recalculate_partial_csum = 1;
873 iph = (
void *)skb->
data;
874 th = skb->
data + 4 * iph->ihl;
875 if (th >= skb_tail_pointer(skb))
883 if (recalculate_partial_csum) {
886 skb->
len - iph->ihl*4,
893 if (recalculate_partial_csum) {
896 skb->
len - iph->ihl*4,
903 "TCP/UDP packet, dropping a protocol"
908 if ((th + skb->
csum_offset + 2) > skb_tail_pointer(skb))
917 static int handle_incoming_queue(
struct net_device *dev,
922 int packets_dropped = 0;
925 while ((skb = __skb_dequeue(rxq)) !=
NULL) {
933 if (checksum_setup(dev, skb)) {
936 dev->
stats.rx_errors++;
940 u64_stats_update_begin(&stats->
syncp);
943 u64_stats_update_end(&stats->
syncp);
949 return packets_dropped;
968 spin_lock(&np->rx_lock);
970 skb_queue_head_init(&rxq);
971 skb_queue_head_init(&errq);
972 skb_queue_head_init(&tmpq);
974 rp = np->
rx.sring->rsp_prod;
979 while ((i != rp) && (work_done < budget)) {
983 err = xennet_get_responses(np, &rinfo, rp, &tmpq);
987 while ((skb = __skb_dequeue(&tmpq)))
988 __skb_queue_tail(&errq, skb);
989 dev->
stats.rx_errors++;
994 skb = __skb_dequeue(&tmpq);
1000 if (
unlikely(xennet_set_skb_gso(skb, gso))) {
1001 __skb_queue_head(&tmpq, skb);
1002 np->
rx.rsp_cons += skb_queue_len(&tmpq);
1011 skb_shinfo(skb)->frags[0].page_offset = rx->
offset;
1012 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->
status);
1015 i = xennet_fill_frags(np, skb, &tmpq);
1048 __skb_queue_tail(&rxq, skb);
1050 np->
rx.rsp_cons = ++
i;
1054 __skb_queue_purge(&errq);
1056 work_done -= handle_incoming_queue(dev, &rxq);
1060 if (((np->
rx.req_prod_pvt - np->
rx.sring->rsp_prod) >
1065 xennet_alloc_rx_buffers(dev);
1067 if (work_done < budget) {
1079 spin_unlock(&np->rx_lock);
1084 static int xennet_change_mtu(
struct net_device *dev,
int mtu)
1106 start = u64_stats_fetch_begin_bh(&stats->
syncp);
1112 }
while (u64_stats_fetch_retry_bh(&stats->
syncp, start));
1126 static void xennet_release_tx_bufs(
struct netfront_info *np)
1133 if (skb_entry_is_link(&np->
tx_skbs[i]))
1147 static void xennet_release_rx_bufs(
struct netfront_info *np)
1154 int xfer = 0, noxfer = 0,
unused = 0;
1161 skb_queue_head_init(&free_list);
1163 spin_lock_bh(&np->rx_lock);
1178 skb_shinfo(skb)->nr_frags = 0;
1186 const struct page *page =
1187 skb_frag_page(&skb_shinfo(skb)->frags[0]);
1191 MULTI_update_va_mapping(mcl, (
unsigned long)vaddr,
1195 mmu->
ptr = ((
u64)mfn << PAGE_SHIFT)
1202 __skb_queue_tail(&free_list, skb);
1206 dev_info(&np->
netdev->dev,
"%s: %d xfer, %d noxfer, %d unused\n",
1207 __func__, xfer, noxfer,
unused);
1219 __skb_queue_purge(&free_list);
1221 spin_unlock_bh(&np->rx_lock);
1224 static void xennet_uninit(
struct net_device *dev)
1227 xennet_release_tx_bufs(np);
1228 xennet_release_rx_bufs(np);
1245 features &= ~NETIF_F_SG;
1250 "feature-gso-tcpv4",
"%d", &val) < 0)
1254 features &= ~NETIF_F_TSO;
1260 static int xennet_set_features(
struct net_device *dev,
1264 netdev_info(dev,
"Reducing MTU because no SG offload");
1275 unsigned long flags;
1279 if (
likely(netif_carrier_ok(dev))) {
1280 xennet_tx_buf_gc(dev);
1283 napi_schedule(&np->
napi);
1286 spin_unlock_irqrestore(&np->
tx_lock, flags);
1291 #ifdef CONFIG_NET_POLL_CONTROLLER
1292 static void xennet_poll_controller(
struct net_device *dev)
1294 xennet_interrupt(0, dev);
1299 .ndo_open = xennet_open,
1300 .ndo_uninit = xennet_uninit,
1301 .ndo_stop = xennet_close,
1302 .ndo_start_xmit = xennet_start_xmit,
1303 .ndo_change_mtu = xennet_change_mtu,
1304 .ndo_get_stats64 = xennet_get_stats64,
1307 .ndo_fix_features = xennet_fix_features,
1308 .ndo_set_features = xennet_set_features,
1309 #ifdef CONFIG_NET_POLL_CONTROLLER
1310 .ndo_poll_controller = xennet_poll_controller,
1324 np = netdev_priv(netdev);
1330 skb_queue_head_init(&np->
rx_batch);
1347 skb_entry_set_link(&np->
tx_skbs[i], i+1);
1362 goto exit_free_stats;
1402 return ERR_PTR(err);
1417 netdev = xennet_create_dev(dev);
1418 if (IS_ERR(netdev)) {
1419 err = PTR_ERR(netdev);
1424 info = netdev_priv(netdev);
1450 static void xennet_end_access(
int ref,
void *page)
1457 static void xennet_disconnect_backend(
struct netfront_info *info)
1460 spin_lock_bh(&info->rx_lock);
1461 spin_lock_irq(&info->
tx_lock);
1463 spin_unlock_irq(&info->
tx_lock);
1464 spin_unlock_bh(&info->rx_lock);
1492 xennet_disconnect_backend(info);
1498 char *
s, *
e, *macstr;
1503 return PTR_ERR(macstr);
1507 if ((s == e) || (*e != ((i == ETH_ALEN-1) ?
'\0' :
':'))) {
1520 struct xen_netif_tx_sring *txs;
1521 struct xen_netif_rx_sring *rxs;
1531 err = xen_net_read_mac(dev, netdev->
dev_addr);
1574 0, netdev->
name, netdev);
1593 err = setup_netfront(dev, info);
1607 message =
"writing tx ring-ref";
1608 goto abort_transaction;
1613 message =
"writing rx ring-ref";
1614 goto abort_transaction;
1617 "event-channel",
"%u", info->
evtchn);
1619 message =
"writing event-channel";
1620 goto abort_transaction;
1626 message =
"writing request-rx-copy";
1627 goto abort_transaction;
1632 message =
"writing feature-rx-notify";
1633 goto abort_transaction;
1638 message =
"writing feature-sg";
1639 goto abort_transaction;
1644 message =
"writing feature-gso-tcpv4";
1645 goto abort_transaction;
1662 xennet_disconnect_backend(info);
1667 static int xennet_connect(
struct net_device *dev)
1670 int i, requeue_idx,
err;
1674 unsigned int feature_rx_copy;
1677 "feature-rx-copy",
"%u", &feature_rx_copy);
1679 feature_rx_copy = 0;
1681 if (!feature_rx_copy) {
1683 "backend does not support copying receive path\n");
1687 err = talk_to_netback(np->
xbdev, np);
1695 spin_lock_bh(&np->rx_lock);
1699 xennet_release_tx_bufs(np);
1704 const struct page *
page;
1708 skb = np->
rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1709 ref = np->
grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1712 frag = &skb_shinfo(skb)->frags[0];
1713 page = skb_frag_page(frag);
1715 ref, np->
xbdev->otherend_id,
1719 req->
id = requeue_idx;
1724 np->
rx.req_prod_pvt = requeue_idx;
1734 xennet_tx_buf_gc(dev);
1735 xennet_alloc_rx_buffers(dev);
1737 spin_unlock_irq(&np->
tx_lock);
1738 spin_unlock_bh(&np->rx_lock);
1754 switch (backend_state) {
1766 if (xennet_connect(netdev) != 0)
1781 static const struct xennet_stat {
1784 } xennet_stats[] = {
1786 "rx_gso_checksum_fixup",
1791 static int xennet_get_sset_count(
struct net_device *dev,
int string_set)
1793 switch (string_set) {
1801 static void xennet_get_ethtool_stats(
struct net_device *dev,
1804 void *np = netdev_priv(dev);
1807 for (i = 0; i <
ARRAY_SIZE(xennet_stats); i++)
1808 data[i] = *(
unsigned long *)(np + xennet_stats[
i].offset);
1811 static void xennet_get_strings(
struct net_device *dev,
u32 stringset,
u8 * data)
1815 switch (stringset) {
1817 for (i = 0; i <
ARRAY_SIZE(xennet_stats); i++)
1819 xennet_stats[i].
name, ETH_GSTRING_LEN);
1824 static const struct ethtool_ops xennet_ethtool_ops =
1828 .get_sset_count = xennet_get_sset_count,
1829 .get_ethtool_stats = xennet_get_ethtool_stats,
1830 .get_strings = xennet_get_strings,
1845 const char *
buf,
size_t len)
1864 spin_lock_bh(&np->rx_lock);
1871 xennet_alloc_rx_buffers(netdev);
1873 spin_unlock_bh(&np->rx_lock);
1888 const char *buf,
size_t len)
1907 spin_lock_bh(&np->rx_lock);
1914 xennet_alloc_rx_buffers(netdev);
1916 spin_unlock_bh(&np->rx_lock);
1940 for (i = 0; i <
ARRAY_SIZE(xennet_attrs); i++) {
1958 for (i = 0; i <
ARRAY_SIZE(xennet_attrs); i++)
1976 xennet_disconnect_backend(info);
1992 .probe = netfront_probe,
1994 .
resume = netfront_resume,
1995 .otherend_changed = netback_changed,
1998 static int __init netif_init(
void)
2013 static void __exit netif_exit(
void)