xen-netfront.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281
  1. /*
  2. * Virtual network driver for conversing with remote driver backends.
  3. *
  4. * Copyright (c) 2002-2005, K A Fraser
  5. * Copyright (c) 2005, XenSource Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation; or, when distributed
  10. * separately from the Linux kernel or incorporated into other
  11. * software packages, subject to the following license:
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this source file (the "Software"), to deal in the Software without
  15. * restriction, including without limitation the rights to use, copy, modify,
  16. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  17. * and to permit persons to whom the Software is furnished to do so, subject to
  18. * the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  29. * IN THE SOFTWARE.
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #include <linux/module.h>
  33. #include <linux/kernel.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/if_ether.h>
  39. #include <net/tcp.h>
  40. #include <linux/udp.h>
  41. #include <linux/moduleparam.h>
  42. #include <linux/mm.h>
  43. #include <linux/slab.h>
  44. #include <net/ip.h>
  45. #include <asm/xen/page.h>
  46. #include <xen/xen.h>
  47. #include <xen/xenbus.h>
  48. #include <xen/events.h>
  49. #include <xen/page.h>
  50. #include <xen/platform_pci.h>
  51. #include <xen/grant_table.h>
  52. #include <xen/interface/io/netif.h>
  53. #include <xen/interface/memory.h>
  54. #include <xen/interface/grant_table.h>
  55. static const struct ethtool_ops xennet_ethtool_ops;
  56. struct netfront_cb {
  57. int pull_to;
  58. };
  59. #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
  60. #define RX_COPY_THRESHOLD 256
  61. #define GRANT_INVALID_REF 0
  62. #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
  63. #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
  64. #define TX_MAX_TARGET min_t(int, NET_TX_RING_SIZE, 256)
  65. /* Queue name is interface name with "-qNNN" appended */
  66. #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
  67. /* IRQ name is queue name with "-tx" or "-rx" appended */
  68. #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
  69. struct netfront_stats {
  70. u64 rx_packets;
  71. u64 tx_packets;
  72. u64 rx_bytes;
  73. u64 tx_bytes;
  74. struct u64_stats_sync syncp;
  75. };
  76. struct netfront_info;
  77. struct netfront_queue {
  78. unsigned int id; /* Queue ID, 0-based */
  79. char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
  80. struct netfront_info *info;
  81. struct napi_struct napi;
  82. /* Split event channels support, tx_* == rx_* when using
  83. * single event channel.
  84. */
  85. unsigned int tx_evtchn, rx_evtchn;
  86. unsigned int tx_irq, rx_irq;
  87. /* Only used when split event channels support is enabled */
  88. char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
  89. char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
  90. spinlock_t tx_lock;
  91. struct xen_netif_tx_front_ring tx;
  92. int tx_ring_ref;
  93. /*
  94. * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
  95. * are linked from tx_skb_freelist through skb_entry.link.
  96. *
  97. * NB. Freelist index entries are always going to be less than
  98. * PAGE_OFFSET, whereas pointers to skbs will always be equal or
  99. * greater than PAGE_OFFSET: we use this property to distinguish
  100. * them.
  101. */
  102. union skb_entry {
  103. struct sk_buff *skb;
  104. unsigned long link;
  105. } tx_skbs[NET_TX_RING_SIZE];
  106. grant_ref_t gref_tx_head;
  107. grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
  108. struct page *grant_tx_page[NET_TX_RING_SIZE];
  109. unsigned tx_skb_freelist;
  110. spinlock_t rx_lock ____cacheline_aligned_in_smp;
  111. struct xen_netif_rx_front_ring rx;
  112. int rx_ring_ref;
  113. /* Receive-ring batched refills. */
  114. #define RX_MIN_TARGET 8
  115. #define RX_DFL_MIN_TARGET 64
  116. #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
  117. unsigned rx_min_target, rx_max_target, rx_target;
  118. struct sk_buff_head rx_batch;
  119. struct timer_list rx_refill_timer;
  120. struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
  121. grant_ref_t gref_rx_head;
  122. grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
  123. unsigned long rx_pfn_array[NET_RX_RING_SIZE];
  124. struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
  125. struct mmu_update rx_mmu[NET_RX_RING_SIZE];
  126. };
  127. struct netfront_info {
  128. struct list_head list;
  129. struct net_device *netdev;
  130. struct xenbus_device *xbdev;
  131. /* Multi-queue support */
  132. struct netfront_queue *queues;
  133. /* Statistics */
  134. struct netfront_stats __percpu *stats;
  135. atomic_t rx_gso_checksum_fixup;
  136. };
  137. struct netfront_rx_info {
  138. struct xen_netif_rx_response rx;
  139. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
  140. };
  141. static void skb_entry_set_link(union skb_entry *list, unsigned short id)
  142. {
  143. list->link = id;
  144. }
  145. static int skb_entry_is_link(const union skb_entry *list)
  146. {
  147. BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
  148. return (unsigned long)list->skb < PAGE_OFFSET;
  149. }
  150. /*
  151. * Access macros for acquiring freeing slots in tx_skbs[].
  152. */
  153. static void add_id_to_freelist(unsigned *head, union skb_entry *list,
  154. unsigned short id)
  155. {
  156. skb_entry_set_link(&list[id], *head);
  157. *head = id;
  158. }
  159. static unsigned short get_id_from_freelist(unsigned *head,
  160. union skb_entry *list)
  161. {
  162. unsigned int id = *head;
  163. *head = list[id].link;
  164. return id;
  165. }
  166. static int xennet_rxidx(RING_IDX idx)
  167. {
  168. return idx & (NET_RX_RING_SIZE - 1);
  169. }
  170. static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
  171. RING_IDX ri)
  172. {
  173. int i = xennet_rxidx(ri);
  174. struct sk_buff *skb = queue->rx_skbs[i];
  175. queue->rx_skbs[i] = NULL;
  176. return skb;
  177. }
  178. static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
  179. RING_IDX ri)
  180. {
  181. int i = xennet_rxidx(ri);
  182. grant_ref_t ref = queue->grant_rx_ref[i];
  183. queue->grant_rx_ref[i] = GRANT_INVALID_REF;
  184. return ref;
  185. }
  186. #ifdef CONFIG_SYSFS
  187. static int xennet_sysfs_addif(struct net_device *netdev);
  188. static void xennet_sysfs_delif(struct net_device *netdev);
  189. #else /* !CONFIG_SYSFS */
  190. #define xennet_sysfs_addif(dev) (0)
  191. #define xennet_sysfs_delif(dev) do { } while (0)
  192. #endif
  193. static bool xennet_can_sg(struct net_device *dev)
  194. {
  195. return dev->features & NETIF_F_SG;
  196. }
  197. static void rx_refill_timeout(unsigned long data)
  198. {
  199. struct netfront_queue *queue = (struct netfront_queue *)data;
  200. napi_schedule(&queue->napi);
  201. }
  202. static int netfront_tx_slot_available(struct netfront_queue *queue)
  203. {
  204. return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
  205. (TX_MAX_TARGET - MAX_SKB_FRAGS - 2);
  206. }
  207. static void xennet_maybe_wake_tx(struct netfront_queue *queue)
  208. {
  209. struct net_device *dev = queue->info->netdev;
  210. struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
  211. if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
  212. netfront_tx_slot_available(queue) &&
  213. likely(netif_running(dev)))
  214. netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
  215. }
  216. static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
  217. {
  218. unsigned short id;
  219. struct sk_buff *skb;
  220. struct page *page;
  221. int i, batch_target, notify;
  222. RING_IDX req_prod = queue->rx.req_prod_pvt;
  223. grant_ref_t ref;
  224. unsigned long pfn;
  225. void *vaddr;
  226. struct xen_netif_rx_request *req;
  227. if (unlikely(!netif_carrier_ok(queue->info->netdev)))
  228. return;
  229. /*
  230. * Allocate skbuffs greedily, even though we batch updates to the
  231. * receive ring. This creates a less bursty demand on the memory
  232. * allocator, so should reduce the chance of failed allocation requests
  233. * both for ourself and for other kernel subsystems.
  234. */
  235. batch_target = queue->rx_target - (req_prod - queue->rx.rsp_cons);
  236. for (i = skb_queue_len(&queue->rx_batch); i < batch_target; i++) {
  237. skb = __netdev_alloc_skb(queue->info->netdev,
  238. RX_COPY_THRESHOLD + NET_IP_ALIGN,
  239. GFP_ATOMIC | __GFP_NOWARN);
  240. if (unlikely(!skb))
  241. goto no_skb;
  242. /* Align ip header to a 16 bytes boundary */
  243. skb_reserve(skb, NET_IP_ALIGN);
  244. page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
  245. if (!page) {
  246. kfree_skb(skb);
  247. no_skb:
  248. /* Could not allocate any skbuffs. Try again later. */
  249. mod_timer(&queue->rx_refill_timer,
  250. jiffies + (HZ/10));
  251. /* Any skbuffs queued for refill? Force them out. */
  252. if (i != 0)
  253. goto refill;
  254. break;
  255. }
  256. skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
  257. __skb_queue_tail(&queue->rx_batch, skb);
  258. }
  259. /* Is the batch large enough to be worthwhile? */
  260. if (i < (queue->rx_target/2)) {
  261. if (req_prod > queue->rx.sring->req_prod)
  262. goto push;
  263. return;
  264. }
  265. /* Adjust our fill target if we risked running out of buffers. */
  266. if (((req_prod - queue->rx.sring->rsp_prod) < (queue->rx_target / 4)) &&
  267. ((queue->rx_target *= 2) > queue->rx_max_target))
  268. queue->rx_target = queue->rx_max_target;
  269. refill:
  270. for (i = 0; ; i++) {
  271. skb = __skb_dequeue(&queue->rx_batch);
  272. if (skb == NULL)
  273. break;
  274. skb->dev = queue->info->netdev;
  275. id = xennet_rxidx(req_prod + i);
  276. BUG_ON(queue->rx_skbs[id]);
  277. queue->rx_skbs[id] = skb;
  278. ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
  279. BUG_ON((signed short)ref < 0);
  280. queue->grant_rx_ref[id] = ref;
  281. pfn = page_to_pfn(skb_frag_page(&skb_shinfo(skb)->frags[0]));
  282. vaddr = page_address(skb_frag_page(&skb_shinfo(skb)->frags[0]));
  283. req = RING_GET_REQUEST(&queue->rx, req_prod + i);
  284. gnttab_grant_foreign_access_ref(ref,
  285. queue->info->xbdev->otherend_id,
  286. pfn_to_mfn(pfn),
  287. 0);
  288. req->id = id;
  289. req->gref = ref;
  290. }
  291. wmb(); /* barrier so backend seens requests */
  292. /* Above is a suitable barrier to ensure backend will see requests. */
  293. queue->rx.req_prod_pvt = req_prod + i;
  294. push:
  295. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
  296. if (notify)
  297. notify_remote_via_irq(queue->rx_irq);
  298. }
  299. static int xennet_open(struct net_device *dev)
  300. {
  301. struct netfront_info *np = netdev_priv(dev);
  302. unsigned int num_queues = dev->real_num_tx_queues;
  303. unsigned int i = 0;
  304. struct netfront_queue *queue = NULL;
  305. for (i = 0; i < num_queues; ++i) {
  306. queue = &np->queues[i];
  307. napi_enable(&queue->napi);
  308. spin_lock_bh(&queue->rx_lock);
  309. if (netif_carrier_ok(dev)) {
  310. xennet_alloc_rx_buffers(queue);
  311. queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
  312. if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
  313. napi_schedule(&queue->napi);
  314. }
  315. spin_unlock_bh(&queue->rx_lock);
  316. }
  317. netif_tx_start_all_queues(dev);
  318. return 0;
  319. }
  320. static void xennet_tx_buf_gc(struct netfront_queue *queue)
  321. {
  322. RING_IDX cons, prod;
  323. unsigned short id;
  324. struct sk_buff *skb;
  325. BUG_ON(!netif_carrier_ok(queue->info->netdev));
  326. do {
  327. prod = queue->tx.sring->rsp_prod;
  328. rmb(); /* Ensure we see responses up to 'rp'. */
  329. for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
  330. struct xen_netif_tx_response *txrsp;
  331. txrsp = RING_GET_RESPONSE(&queue->tx, cons);
  332. if (txrsp->status == XEN_NETIF_RSP_NULL)
  333. continue;
  334. id = txrsp->id;
  335. skb = queue->tx_skbs[id].skb;
  336. if (unlikely(gnttab_query_foreign_access(
  337. queue->grant_tx_ref[id]) != 0)) {
  338. pr_alert("%s: warning -- grant still in use by backend domain\n",
  339. __func__);
  340. BUG();
  341. }
  342. gnttab_end_foreign_access_ref(
  343. queue->grant_tx_ref[id], GNTMAP_readonly);
  344. gnttab_release_grant_reference(
  345. &queue->gref_tx_head, queue->grant_tx_ref[id]);
  346. queue->grant_tx_ref[id] = GRANT_INVALID_REF;
  347. queue->grant_tx_page[id] = NULL;
  348. add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, id);
  349. dev_kfree_skb_irq(skb);
  350. }
  351. queue->tx.rsp_cons = prod;
  352. /*
  353. * Set a new event, then check for race with update of tx_cons.
  354. * Note that it is essential to schedule a callback, no matter
  355. * how few buffers are pending. Even if there is space in the
  356. * transmit ring, higher layers may be blocked because too much
  357. * data is outstanding: in such cases notification from Xen is
  358. * likely to be the only kick that we'll get.
  359. */
  360. queue->tx.sring->rsp_event =
  361. prod + ((queue->tx.sring->req_prod - prod) >> 1) + 1;
  362. mb(); /* update shared area */
  363. } while ((cons == prod) && (prod != queue->tx.sring->rsp_prod));
  364. xennet_maybe_wake_tx(queue);
  365. }
  366. static void xennet_make_frags(struct sk_buff *skb, struct netfront_queue *queue,
  367. struct xen_netif_tx_request *tx)
  368. {
  369. char *data = skb->data;
  370. unsigned long mfn;
  371. RING_IDX prod = queue->tx.req_prod_pvt;
  372. int frags = skb_shinfo(skb)->nr_frags;
  373. unsigned int offset = offset_in_page(data);
  374. unsigned int len = skb_headlen(skb);
  375. unsigned int id;
  376. grant_ref_t ref;
  377. int i;
  378. /* While the header overlaps a page boundary (including being
  379. larger than a page), split it it into page-sized chunks. */
  380. while (len > PAGE_SIZE - offset) {
  381. tx->size = PAGE_SIZE - offset;
  382. tx->flags |= XEN_NETTXF_more_data;
  383. len -= tx->size;
  384. data += tx->size;
  385. offset = 0;
  386. id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs);
  387. queue->tx_skbs[id].skb = skb_get(skb);
  388. tx = RING_GET_REQUEST(&queue->tx, prod++);
  389. tx->id = id;
  390. ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
  391. BUG_ON((signed short)ref < 0);
  392. mfn = virt_to_mfn(data);
  393. gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
  394. mfn, GNTMAP_readonly);
  395. queue->grant_tx_page[id] = virt_to_page(data);
  396. tx->gref = queue->grant_tx_ref[id] = ref;
  397. tx->offset = offset;
  398. tx->size = len;
  399. tx->flags = 0;
  400. }
  401. /* Grant backend access to each skb fragment page. */
  402. for (i = 0; i < frags; i++) {
  403. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  404. struct page *page = skb_frag_page(frag);
  405. len = skb_frag_size(frag);
  406. offset = frag->page_offset;
  407. /* Data must not cross a page boundary. */
  408. BUG_ON(len + offset > PAGE_SIZE<<compound_order(page));
  409. /* Skip unused frames from start of page */
  410. page += offset >> PAGE_SHIFT;
  411. offset &= ~PAGE_MASK;
  412. while (len > 0) {
  413. unsigned long bytes;
  414. BUG_ON(offset >= PAGE_SIZE);
  415. bytes = PAGE_SIZE - offset;
  416. if (bytes > len)
  417. bytes = len;
  418. tx->flags |= XEN_NETTXF_more_data;
  419. id = get_id_from_freelist(&queue->tx_skb_freelist,
  420. queue->tx_skbs);
  421. queue->tx_skbs[id].skb = skb_get(skb);
  422. tx = RING_GET_REQUEST(&queue->tx, prod++);
  423. tx->id = id;
  424. ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
  425. BUG_ON((signed short)ref < 0);
  426. mfn = pfn_to_mfn(page_to_pfn(page));
  427. gnttab_grant_foreign_access_ref(ref,
  428. queue->info->xbdev->otherend_id,
  429. mfn, GNTMAP_readonly);
  430. queue->grant_tx_page[id] = page;
  431. tx->gref = queue->grant_tx_ref[id] = ref;
  432. tx->offset = offset;
  433. tx->size = bytes;
  434. tx->flags = 0;
  435. offset += bytes;
  436. len -= bytes;
  437. /* Next frame */
  438. if (offset == PAGE_SIZE && len) {
  439. BUG_ON(!PageCompound(page));
  440. page++;
  441. offset = 0;
  442. }
  443. }
  444. }
  445. queue->tx.req_prod_pvt = prod;
  446. }
  447. /*
  448. * Count how many ring slots are required to send the frags of this
  449. * skb. Each frag might be a compound page.
  450. */
  451. static int xennet_count_skb_frag_slots(struct sk_buff *skb)
  452. {
  453. int i, frags = skb_shinfo(skb)->nr_frags;
  454. int pages = 0;
  455. for (i = 0; i < frags; i++) {
  456. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  457. unsigned long size = skb_frag_size(frag);
  458. unsigned long offset = frag->page_offset;
  459. /* Skip unused frames from start of page */
  460. offset &= ~PAGE_MASK;
  461. pages += PFN_UP(offset + size);
  462. }
  463. return pages;
  464. }
  465. static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb)
  466. {
  467. /* Stub for later implementation of queue selection */
  468. return 0;
  469. }
  470. static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  471. {
  472. unsigned short id;
  473. struct netfront_info *np = netdev_priv(dev);
  474. struct netfront_stats *stats = this_cpu_ptr(np->stats);
  475. struct xen_netif_tx_request *tx;
  476. char *data = skb->data;
  477. RING_IDX i;
  478. grant_ref_t ref;
  479. unsigned long mfn;
  480. int notify;
  481. int slots;
  482. unsigned int offset = offset_in_page(data);
  483. unsigned int len = skb_headlen(skb);
  484. unsigned long flags;
  485. struct netfront_queue *queue = NULL;
  486. unsigned int num_queues = dev->real_num_tx_queues;
  487. u16 queue_index;
  488. /* Drop the packet if no queues are set up */
  489. if (num_queues < 1)
  490. goto drop;
  491. /* Determine which queue to transmit this SKB on */
  492. queue_index = skb_get_queue_mapping(skb);
  493. queue = &np->queues[queue_index];
  494. /* If skb->len is too big for wire format, drop skb and alert
  495. * user about misconfiguration.
  496. */
  497. if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
  498. net_alert_ratelimited(
  499. "xennet: skb->len = %u, too big for wire format\n",
  500. skb->len);
  501. goto drop;
  502. }
  503. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE) +
  504. xennet_count_skb_frag_slots(skb);
  505. if (unlikely(slots > MAX_SKB_FRAGS + 1)) {
  506. net_alert_ratelimited(
  507. "xennet: skb rides the rocket: %d slots\n", slots);
  508. goto drop;
  509. }
  510. spin_lock_irqsave(&queue->tx_lock, flags);
  511. if (unlikely(!netif_carrier_ok(dev) ||
  512. (slots > 1 && !xennet_can_sg(dev)) ||
  513. netif_needs_gso(skb, netif_skb_features(skb)))) {
  514. spin_unlock_irqrestore(&queue->tx_lock, flags);
  515. goto drop;
  516. }
  517. i = queue->tx.req_prod_pvt;
  518. id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs);
  519. queue->tx_skbs[id].skb = skb;
  520. tx = RING_GET_REQUEST(&queue->tx, i);
  521. tx->id = id;
  522. ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
  523. BUG_ON((signed short)ref < 0);
  524. mfn = virt_to_mfn(data);
  525. gnttab_grant_foreign_access_ref(
  526. ref, queue->info->xbdev->otherend_id, mfn, GNTMAP_readonly);
  527. queue->grant_tx_page[id] = virt_to_page(data);
  528. tx->gref = queue->grant_tx_ref[id] = ref;
  529. tx->offset = offset;
  530. tx->size = len;
  531. tx->flags = 0;
  532. if (skb->ip_summed == CHECKSUM_PARTIAL)
  533. /* local packet? */
  534. tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
  535. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  536. /* remote but checksummed. */
  537. tx->flags |= XEN_NETTXF_data_validated;
  538. if (skb_shinfo(skb)->gso_size) {
  539. struct xen_netif_extra_info *gso;
  540. gso = (struct xen_netif_extra_info *)
  541. RING_GET_REQUEST(&queue->tx, ++i);
  542. tx->flags |= XEN_NETTXF_extra_info;
  543. gso->u.gso.size = skb_shinfo(skb)->gso_size;
  544. gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
  545. XEN_NETIF_GSO_TYPE_TCPV6 :
  546. XEN_NETIF_GSO_TYPE_TCPV4;
  547. gso->u.gso.pad = 0;
  548. gso->u.gso.features = 0;
  549. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  550. gso->flags = 0;
  551. }
  552. queue->tx.req_prod_pvt = i + 1;
  553. xennet_make_frags(skb, queue, tx);
  554. tx->size = skb->len;
  555. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
  556. if (notify)
  557. notify_remote_via_irq(queue->tx_irq);
  558. u64_stats_update_begin(&stats->syncp);
  559. stats->tx_bytes += skb->len;
  560. stats->tx_packets++;
  561. u64_stats_update_end(&stats->syncp);
  562. /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
  563. xennet_tx_buf_gc(queue);
  564. if (!netfront_tx_slot_available(queue))
  565. netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
  566. spin_unlock_irqrestore(&queue->tx_lock, flags);
  567. return NETDEV_TX_OK;
  568. drop:
  569. dev->stats.tx_dropped++;
  570. dev_kfree_skb_any(skb);
  571. return NETDEV_TX_OK;
  572. }
  573. static int xennet_close(struct net_device *dev)
  574. {
  575. struct netfront_info *np = netdev_priv(dev);
  576. unsigned int num_queues = dev->real_num_tx_queues;
  577. unsigned int i;
  578. struct netfront_queue *queue;
  579. netif_tx_stop_all_queues(np->netdev);
  580. for (i = 0; i < num_queues; ++i) {
  581. queue = &np->queues[i];
  582. napi_disable(&queue->napi);
  583. }
  584. return 0;
  585. }
  586. static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
  587. grant_ref_t ref)
  588. {
  589. int new = xennet_rxidx(queue->rx.req_prod_pvt);
  590. BUG_ON(queue->rx_skbs[new]);
  591. queue->rx_skbs[new] = skb;
  592. queue->grant_rx_ref[new] = ref;
  593. RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
  594. RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
  595. queue->rx.req_prod_pvt++;
  596. }
  597. static int xennet_get_extras(struct netfront_queue *queue,
  598. struct xen_netif_extra_info *extras,
  599. RING_IDX rp)
  600. {
  601. struct xen_netif_extra_info *extra;
  602. struct device *dev = &queue->info->netdev->dev;
  603. RING_IDX cons = queue->rx.rsp_cons;
  604. int err = 0;
  605. do {
  606. struct sk_buff *skb;
  607. grant_ref_t ref;
  608. if (unlikely(cons + 1 == rp)) {
  609. if (net_ratelimit())
  610. dev_warn(dev, "Missing extra info\n");
  611. err = -EBADR;
  612. break;
  613. }
  614. extra = (struct xen_netif_extra_info *)
  615. RING_GET_RESPONSE(&queue->rx, ++cons);
  616. if (unlikely(!extra->type ||
  617. extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  618. if (net_ratelimit())
  619. dev_warn(dev, "Invalid extra type: %d\n",
  620. extra->type);
  621. err = -EINVAL;
  622. } else {
  623. memcpy(&extras[extra->type - 1], extra,
  624. sizeof(*extra));
  625. }
  626. skb = xennet_get_rx_skb(queue, cons);
  627. ref = xennet_get_rx_ref(queue, cons);
  628. xennet_move_rx_slot(queue, skb, ref);
  629. } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
  630. queue->rx.rsp_cons = cons;
  631. return err;
  632. }
  633. static int xennet_get_responses(struct netfront_queue *queue,
  634. struct netfront_rx_info *rinfo, RING_IDX rp,
  635. struct sk_buff_head *list)
  636. {
  637. struct xen_netif_rx_response *rx = &rinfo->rx;
  638. struct xen_netif_extra_info *extras = rinfo->extras;
  639. struct device *dev = &queue->info->netdev->dev;
  640. RING_IDX cons = queue->rx.rsp_cons;
  641. struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
  642. grant_ref_t ref = xennet_get_rx_ref(queue, cons);
  643. int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
  644. int slots = 1;
  645. int err = 0;
  646. unsigned long ret;
  647. if (rx->flags & XEN_NETRXF_extra_info) {
  648. err = xennet_get_extras(queue, extras, rp);
  649. cons = queue->rx.rsp_cons;
  650. }
  651. for (;;) {
  652. if (unlikely(rx->status < 0 ||
  653. rx->offset + rx->status > PAGE_SIZE)) {
  654. if (net_ratelimit())
  655. dev_warn(dev, "rx->offset: %x, size: %u\n",
  656. rx->offset, rx->status);
  657. xennet_move_rx_slot(queue, skb, ref);
  658. err = -EINVAL;
  659. goto next;
  660. }
  661. /*
  662. * This definitely indicates a bug, either in this driver or in
  663. * the backend driver. In future this should flag the bad
  664. * situation to the system controller to reboot the backend.
  665. */
  666. if (ref == GRANT_INVALID_REF) {
  667. if (net_ratelimit())
  668. dev_warn(dev, "Bad rx response id %d.\n",
  669. rx->id);
  670. err = -EINVAL;
  671. goto next;
  672. }
  673. ret = gnttab_end_foreign_access_ref(ref, 0);
  674. BUG_ON(!ret);
  675. gnttab_release_grant_reference(&queue->gref_rx_head, ref);
  676. __skb_queue_tail(list, skb);
  677. next:
  678. if (!(rx->flags & XEN_NETRXF_more_data))
  679. break;
  680. if (cons + slots == rp) {
  681. if (net_ratelimit())
  682. dev_warn(dev, "Need more slots\n");
  683. err = -ENOENT;
  684. break;
  685. }
  686. rx = RING_GET_RESPONSE(&queue->rx, cons + slots);
  687. skb = xennet_get_rx_skb(queue, cons + slots);
  688. ref = xennet_get_rx_ref(queue, cons + slots);
  689. slots++;
  690. }
  691. if (unlikely(slots > max)) {
  692. if (net_ratelimit())
  693. dev_warn(dev, "Too many slots\n");
  694. err = -E2BIG;
  695. }
  696. if (unlikely(err))
  697. queue->rx.rsp_cons = cons + slots;
  698. return err;
  699. }
  700. static int xennet_set_skb_gso(struct sk_buff *skb,
  701. struct xen_netif_extra_info *gso)
  702. {
  703. if (!gso->u.gso.size) {
  704. if (net_ratelimit())
  705. pr_warn("GSO size must not be zero\n");
  706. return -EINVAL;
  707. }
  708. if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
  709. gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
  710. if (net_ratelimit())
  711. pr_warn("Bad GSO type %d\n", gso->u.gso.type);
  712. return -EINVAL;
  713. }
  714. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  715. skb_shinfo(skb)->gso_type =
  716. (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
  717. SKB_GSO_TCPV4 :
  718. SKB_GSO_TCPV6;
  719. /* Header must be checked, and gso_segs computed. */
  720. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  721. skb_shinfo(skb)->gso_segs = 0;
  722. return 0;
  723. }
  724. static RING_IDX xennet_fill_frags(struct netfront_queue *queue,
  725. struct sk_buff *skb,
  726. struct sk_buff_head *list)
  727. {
  728. struct skb_shared_info *shinfo = skb_shinfo(skb);
  729. RING_IDX cons = queue->rx.rsp_cons;
  730. struct sk_buff *nskb;
  731. while ((nskb = __skb_dequeue(list))) {
  732. struct xen_netif_rx_response *rx =
  733. RING_GET_RESPONSE(&queue->rx, ++cons);
  734. skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
  735. if (shinfo->nr_frags == MAX_SKB_FRAGS) {
  736. unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  737. BUG_ON(pull_to <= skb_headlen(skb));
  738. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  739. }
  740. BUG_ON(shinfo->nr_frags >= MAX_SKB_FRAGS);
  741. skb_add_rx_frag(skb, shinfo->nr_frags, skb_frag_page(nfrag),
  742. rx->offset, rx->status, PAGE_SIZE);
  743. skb_shinfo(nskb)->nr_frags = 0;
  744. kfree_skb(nskb);
  745. }
  746. return cons;
  747. }
  748. static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
  749. {
  750. bool recalculate_partial_csum = false;
  751. /*
  752. * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  753. * peers can fail to set NETRXF_csum_blank when sending a GSO
  754. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  755. * recalculate the partial checksum.
  756. */
  757. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  758. struct netfront_info *np = netdev_priv(dev);
  759. atomic_inc(&np->rx_gso_checksum_fixup);
  760. skb->ip_summed = CHECKSUM_PARTIAL;
  761. recalculate_partial_csum = true;
  762. }
  763. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  764. if (skb->ip_summed != CHECKSUM_PARTIAL)
  765. return 0;
  766. return skb_checksum_setup(skb, recalculate_partial_csum);
  767. }
  768. static int handle_incoming_queue(struct netfront_queue *queue,
  769. struct sk_buff_head *rxq)
  770. {
  771. struct netfront_stats *stats = this_cpu_ptr(queue->info->stats);
  772. int packets_dropped = 0;
  773. struct sk_buff *skb;
  774. while ((skb = __skb_dequeue(rxq)) != NULL) {
  775. int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  776. if (pull_to > skb_headlen(skb))
  777. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  778. /* Ethernet work: Delayed to here as it peeks the header. */
  779. skb->protocol = eth_type_trans(skb, queue->info->netdev);
  780. skb_reset_network_header(skb);
  781. if (checksum_setup(queue->info->netdev, skb)) {
  782. kfree_skb(skb);
  783. packets_dropped++;
  784. queue->info->netdev->stats.rx_errors++;
  785. continue;
  786. }
  787. u64_stats_update_begin(&stats->syncp);
  788. stats->rx_packets++;
  789. stats->rx_bytes += skb->len;
  790. u64_stats_update_end(&stats->syncp);
  791. /* Pass it up. */
  792. napi_gro_receive(&queue->napi, skb);
  793. }
  794. return packets_dropped;
  795. }
  796. static int xennet_poll(struct napi_struct *napi, int budget)
  797. {
  798. struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
  799. struct net_device *dev = queue->info->netdev;
  800. struct sk_buff *skb;
  801. struct netfront_rx_info rinfo;
  802. struct xen_netif_rx_response *rx = &rinfo.rx;
  803. struct xen_netif_extra_info *extras = rinfo.extras;
  804. RING_IDX i, rp;
  805. int work_done;
  806. struct sk_buff_head rxq;
  807. struct sk_buff_head errq;
  808. struct sk_buff_head tmpq;
  809. unsigned long flags;
  810. int err;
  811. spin_lock(&queue->rx_lock);
  812. skb_queue_head_init(&rxq);
  813. skb_queue_head_init(&errq);
  814. skb_queue_head_init(&tmpq);
  815. rp = queue->rx.sring->rsp_prod;
  816. rmb(); /* Ensure we see queued responses up to 'rp'. */
  817. i = queue->rx.rsp_cons;
  818. work_done = 0;
  819. while ((i != rp) && (work_done < budget)) {
  820. memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
  821. memset(extras, 0, sizeof(rinfo.extras));
  822. err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
  823. if (unlikely(err)) {
  824. err:
  825. while ((skb = __skb_dequeue(&tmpq)))
  826. __skb_queue_tail(&errq, skb);
  827. dev->stats.rx_errors++;
  828. i = queue->rx.rsp_cons;
  829. continue;
  830. }
  831. skb = __skb_dequeue(&tmpq);
  832. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  833. struct xen_netif_extra_info *gso;
  834. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  835. if (unlikely(xennet_set_skb_gso(skb, gso))) {
  836. __skb_queue_head(&tmpq, skb);
  837. queue->rx.rsp_cons += skb_queue_len(&tmpq);
  838. goto err;
  839. }
  840. }
  841. NETFRONT_SKB_CB(skb)->pull_to = rx->status;
  842. if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
  843. NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
  844. skb_shinfo(skb)->frags[0].page_offset = rx->offset;
  845. skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
  846. skb->data_len = rx->status;
  847. skb->len += rx->status;
  848. i = xennet_fill_frags(queue, skb, &tmpq);
  849. if (rx->flags & XEN_NETRXF_csum_blank)
  850. skb->ip_summed = CHECKSUM_PARTIAL;
  851. else if (rx->flags & XEN_NETRXF_data_validated)
  852. skb->ip_summed = CHECKSUM_UNNECESSARY;
  853. __skb_queue_tail(&rxq, skb);
  854. queue->rx.rsp_cons = ++i;
  855. work_done++;
  856. }
  857. __skb_queue_purge(&errq);
  858. work_done -= handle_incoming_queue(queue, &rxq);
  859. /* If we get a callback with very few responses, reduce fill target. */
  860. /* NB. Note exponential increase, linear decrease. */
  861. if (((queue->rx.req_prod_pvt - queue->rx.sring->rsp_prod) >
  862. ((3*queue->rx_target) / 4)) &&
  863. (--queue->rx_target < queue->rx_min_target))
  864. queue->rx_target = queue->rx_min_target;
  865. xennet_alloc_rx_buffers(queue);
  866. if (work_done < budget) {
  867. int more_to_do = 0;
  868. napi_gro_flush(napi, false);
  869. local_irq_save(flags);
  870. RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
  871. if (!more_to_do)
  872. __napi_complete(napi);
  873. local_irq_restore(flags);
  874. }
  875. spin_unlock(&queue->rx_lock);
  876. return work_done;
  877. }
  878. static int xennet_change_mtu(struct net_device *dev, int mtu)
  879. {
  880. int max = xennet_can_sg(dev) ?
  881. XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER : ETH_DATA_LEN;
  882. if (mtu > max)
  883. return -EINVAL;
  884. dev->mtu = mtu;
  885. return 0;
  886. }
  887. static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev,
  888. struct rtnl_link_stats64 *tot)
  889. {
  890. struct netfront_info *np = netdev_priv(dev);
  891. int cpu;
  892. for_each_possible_cpu(cpu) {
  893. struct netfront_stats *stats = per_cpu_ptr(np->stats, cpu);
  894. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  895. unsigned int start;
  896. do {
  897. start = u64_stats_fetch_begin_irq(&stats->syncp);
  898. rx_packets = stats->rx_packets;
  899. tx_packets = stats->tx_packets;
  900. rx_bytes = stats->rx_bytes;
  901. tx_bytes = stats->tx_bytes;
  902. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  903. tot->rx_packets += rx_packets;
  904. tot->tx_packets += tx_packets;
  905. tot->rx_bytes += rx_bytes;
  906. tot->tx_bytes += tx_bytes;
  907. }
  908. tot->rx_errors = dev->stats.rx_errors;
  909. tot->tx_dropped = dev->stats.tx_dropped;
  910. return tot;
  911. }
  912. static void xennet_release_tx_bufs(struct netfront_queue *queue)
  913. {
  914. struct sk_buff *skb;
  915. int i;
  916. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  917. /* Skip over entries which are actually freelist references */
  918. if (skb_entry_is_link(&queue->tx_skbs[i]))
  919. continue;
  920. skb = queue->tx_skbs[i].skb;
  921. get_page(queue->grant_tx_page[i]);
  922. gnttab_end_foreign_access(queue->grant_tx_ref[i],
  923. GNTMAP_readonly,
  924. (unsigned long)page_address(queue->grant_tx_page[i]));
  925. queue->grant_tx_page[i] = NULL;
  926. queue->grant_tx_ref[i] = GRANT_INVALID_REF;
  927. add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, i);
  928. dev_kfree_skb_irq(skb);
  929. }
  930. }
  931. static void xennet_release_rx_bufs(struct netfront_queue *queue)
  932. {
  933. int id, ref;
  934. spin_lock_bh(&queue->rx_lock);
  935. for (id = 0; id < NET_RX_RING_SIZE; id++) {
  936. struct sk_buff *skb;
  937. struct page *page;
  938. skb = queue->rx_skbs[id];
  939. if (!skb)
  940. continue;
  941. ref = queue->grant_rx_ref[id];
  942. if (ref == GRANT_INVALID_REF)
  943. continue;
  944. page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
  945. /* gnttab_end_foreign_access() needs a page ref until
  946. * foreign access is ended (which may be deferred).
  947. */
  948. get_page(page);
  949. gnttab_end_foreign_access(ref, 0,
  950. (unsigned long)page_address(page));
  951. queue->grant_rx_ref[id] = GRANT_INVALID_REF;
  952. kfree_skb(skb);
  953. }
  954. spin_unlock_bh(&queue->rx_lock);
  955. }
  956. static void xennet_uninit(struct net_device *dev)
  957. {
  958. struct netfront_info *np = netdev_priv(dev);
  959. unsigned int num_queues = dev->real_num_tx_queues;
  960. struct netfront_queue *queue;
  961. unsigned int i;
  962. for (i = 0; i < num_queues; ++i) {
  963. queue = &np->queues[i];
  964. xennet_release_tx_bufs(queue);
  965. xennet_release_rx_bufs(queue);
  966. gnttab_free_grant_references(queue->gref_tx_head);
  967. gnttab_free_grant_references(queue->gref_rx_head);
  968. }
  969. }
  970. static netdev_features_t xennet_fix_features(struct net_device *dev,
  971. netdev_features_t features)
  972. {
  973. struct netfront_info *np = netdev_priv(dev);
  974. int val;
  975. if (features & NETIF_F_SG) {
  976. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
  977. "%d", &val) < 0)
  978. val = 0;
  979. if (!val)
  980. features &= ~NETIF_F_SG;
  981. }
  982. if (features & NETIF_F_IPV6_CSUM) {
  983. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  984. "feature-ipv6-csum-offload", "%d", &val) < 0)
  985. val = 0;
  986. if (!val)
  987. features &= ~NETIF_F_IPV6_CSUM;
  988. }
  989. if (features & NETIF_F_TSO) {
  990. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  991. "feature-gso-tcpv4", "%d", &val) < 0)
  992. val = 0;
  993. if (!val)
  994. features &= ~NETIF_F_TSO;
  995. }
  996. if (features & NETIF_F_TSO6) {
  997. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  998. "feature-gso-tcpv6", "%d", &val) < 0)
  999. val = 0;
  1000. if (!val)
  1001. features &= ~NETIF_F_TSO6;
  1002. }
  1003. return features;
  1004. }
  1005. static int xennet_set_features(struct net_device *dev,
  1006. netdev_features_t features)
  1007. {
  1008. if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
  1009. netdev_info(dev, "Reducing MTU because no SG offload");
  1010. dev->mtu = ETH_DATA_LEN;
  1011. }
  1012. return 0;
  1013. }
  1014. static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
  1015. {
  1016. struct netfront_queue *queue = dev_id;
  1017. unsigned long flags;
  1018. spin_lock_irqsave(&queue->tx_lock, flags);
  1019. xennet_tx_buf_gc(queue);
  1020. spin_unlock_irqrestore(&queue->tx_lock, flags);
  1021. return IRQ_HANDLED;
  1022. }
  1023. static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
  1024. {
  1025. struct netfront_queue *queue = dev_id;
  1026. struct net_device *dev = queue->info->netdev;
  1027. if (likely(netif_carrier_ok(dev) &&
  1028. RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
  1029. napi_schedule(&queue->napi);
  1030. return IRQ_HANDLED;
  1031. }
  1032. static irqreturn_t xennet_interrupt(int irq, void *dev_id)
  1033. {
  1034. xennet_tx_interrupt(irq, dev_id);
  1035. xennet_rx_interrupt(irq, dev_id);
  1036. return IRQ_HANDLED;
  1037. }
  1038. #ifdef CONFIG_NET_POLL_CONTROLLER
  1039. static void xennet_poll_controller(struct net_device *dev)
  1040. {
  1041. /* Poll each queue */
  1042. struct netfront_info *info = netdev_priv(dev);
  1043. unsigned int num_queues = dev->real_num_tx_queues;
  1044. unsigned int i;
  1045. for (i = 0; i < num_queues; ++i)
  1046. xennet_interrupt(0, &info->queues[i]);
  1047. }
  1048. #endif
  1049. static const struct net_device_ops xennet_netdev_ops = {
  1050. .ndo_open = xennet_open,
  1051. .ndo_uninit = xennet_uninit,
  1052. .ndo_stop = xennet_close,
  1053. .ndo_start_xmit = xennet_start_xmit,
  1054. .ndo_change_mtu = xennet_change_mtu,
  1055. .ndo_get_stats64 = xennet_get_stats64,
  1056. .ndo_set_mac_address = eth_mac_addr,
  1057. .ndo_validate_addr = eth_validate_addr,
  1058. .ndo_fix_features = xennet_fix_features,
  1059. .ndo_set_features = xennet_set_features,
  1060. .ndo_select_queue = xennet_select_queue,
  1061. #ifdef CONFIG_NET_POLL_CONTROLLER
  1062. .ndo_poll_controller = xennet_poll_controller,
  1063. #endif
  1064. };
  1065. static struct net_device *xennet_create_dev(struct xenbus_device *dev)
  1066. {
  1067. int err;
  1068. struct net_device *netdev;
  1069. struct netfront_info *np;
  1070. netdev = alloc_etherdev_mq(sizeof(struct netfront_info), 1);
  1071. if (!netdev)
  1072. return ERR_PTR(-ENOMEM);
  1073. np = netdev_priv(netdev);
  1074. np->xbdev = dev;
  1075. /* No need to use rtnl_lock() before the call below as it
  1076. * happens before register_netdev().
  1077. */
  1078. netif_set_real_num_tx_queues(netdev, 0);
  1079. np->queues = NULL;
  1080. err = -ENOMEM;
  1081. np->stats = netdev_alloc_pcpu_stats(struct netfront_stats);
  1082. if (np->stats == NULL)
  1083. goto exit;
  1084. netdev->netdev_ops = &xennet_netdev_ops;
  1085. netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  1086. NETIF_F_GSO_ROBUST;
  1087. netdev->hw_features = NETIF_F_SG |
  1088. NETIF_F_IPV6_CSUM |
  1089. NETIF_F_TSO | NETIF_F_TSO6;
  1090. /*
  1091. * Assume that all hw features are available for now. This set
  1092. * will be adjusted by the call to netdev_update_features() in
  1093. * xennet_connect() which is the earliest point where we can
  1094. * negotiate with the backend regarding supported features.
  1095. */
  1096. netdev->features |= netdev->hw_features;
  1097. netdev->ethtool_ops = &xennet_ethtool_ops;
  1098. SET_NETDEV_DEV(netdev, &dev->dev);
  1099. netif_set_gso_max_size(netdev, XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER);
  1100. np->netdev = netdev;
  1101. netif_carrier_off(netdev);
  1102. return netdev;
  1103. exit:
  1104. free_netdev(netdev);
  1105. return ERR_PTR(err);
  1106. }
  1107. /**
  1108. * Entry point to this code when a new device is created. Allocate the basic
  1109. * structures and the ring buffers for communication with the backend, and
  1110. * inform the backend of the appropriate details for those.
  1111. */
  1112. static int netfront_probe(struct xenbus_device *dev,
  1113. const struct xenbus_device_id *id)
  1114. {
  1115. int err;
  1116. struct net_device *netdev;
  1117. struct netfront_info *info;
  1118. netdev = xennet_create_dev(dev);
  1119. if (IS_ERR(netdev)) {
  1120. err = PTR_ERR(netdev);
  1121. xenbus_dev_fatal(dev, err, "creating netdev");
  1122. return err;
  1123. }
  1124. info = netdev_priv(netdev);
  1125. dev_set_drvdata(&dev->dev, info);
  1126. err = register_netdev(info->netdev);
  1127. if (err) {
  1128. pr_warn("%s: register_netdev err=%d\n", __func__, err);
  1129. goto fail;
  1130. }
  1131. err = xennet_sysfs_addif(info->netdev);
  1132. if (err) {
  1133. unregister_netdev(info->netdev);
  1134. pr_warn("%s: add sysfs failed err=%d\n", __func__, err);
  1135. goto fail;
  1136. }
  1137. return 0;
  1138. fail:
  1139. free_netdev(netdev);
  1140. dev_set_drvdata(&dev->dev, NULL);
  1141. return err;
  1142. }
  1143. static void xennet_end_access(int ref, void *page)
  1144. {
  1145. /* This frees the page as a side-effect */
  1146. if (ref != GRANT_INVALID_REF)
  1147. gnttab_end_foreign_access(ref, 0, (unsigned long)page);
  1148. }
  1149. static void xennet_disconnect_backend(struct netfront_info *info)
  1150. {
  1151. unsigned int i = 0;
  1152. struct netfront_queue *queue = NULL;
  1153. unsigned int num_queues = info->netdev->real_num_tx_queues;
  1154. for (i = 0; i < num_queues; ++i) {
  1155. /* Stop old i/f to prevent errors whilst we rebuild the state. */
  1156. spin_lock_bh(&queue->rx_lock);
  1157. spin_lock_irq(&queue->tx_lock);
  1158. netif_carrier_off(queue->info->netdev);
  1159. spin_unlock_irq(&queue->tx_lock);
  1160. spin_unlock_bh(&queue->rx_lock);
  1161. if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
  1162. unbind_from_irqhandler(queue->tx_irq, queue);
  1163. if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
  1164. unbind_from_irqhandler(queue->tx_irq, queue);
  1165. unbind_from_irqhandler(queue->rx_irq, queue);
  1166. }
  1167. queue->tx_evtchn = queue->rx_evtchn = 0;
  1168. queue->tx_irq = queue->rx_irq = 0;
  1169. /* End access and free the pages */
  1170. xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
  1171. xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
  1172. queue->tx_ring_ref = GRANT_INVALID_REF;
  1173. queue->rx_ring_ref = GRANT_INVALID_REF;
  1174. queue->tx.sring = NULL;
  1175. queue->rx.sring = NULL;
  1176. }
  1177. }
  1178. /**
  1179. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1180. * driver restart. We tear down our netif structure and recreate it, but
  1181. * leave the device-layer structures intact so that this is transparent to the
  1182. * rest of the kernel.
  1183. */
  1184. static int netfront_resume(struct xenbus_device *dev)
  1185. {
  1186. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1187. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1188. xennet_disconnect_backend(info);
  1189. return 0;
  1190. }
  1191. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  1192. {
  1193. char *s, *e, *macstr;
  1194. int i;
  1195. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  1196. if (IS_ERR(macstr))
  1197. return PTR_ERR(macstr);
  1198. for (i = 0; i < ETH_ALEN; i++) {
  1199. mac[i] = simple_strtoul(s, &e, 16);
  1200. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  1201. kfree(macstr);
  1202. return -ENOENT;
  1203. }
  1204. s = e+1;
  1205. }
  1206. kfree(macstr);
  1207. return 0;
  1208. }
  1209. static int setup_netfront_single(struct netfront_queue *queue)
  1210. {
  1211. int err;
  1212. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1213. if (err < 0)
  1214. goto fail;
  1215. err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
  1216. xennet_interrupt,
  1217. 0, queue->info->netdev->name, queue);
  1218. if (err < 0)
  1219. goto bind_fail;
  1220. queue->rx_evtchn = queue->tx_evtchn;
  1221. queue->rx_irq = queue->tx_irq = err;
  1222. return 0;
  1223. bind_fail:
  1224. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1225. queue->tx_evtchn = 0;
  1226. fail:
  1227. return err;
  1228. }
  1229. static int setup_netfront_split(struct netfront_queue *queue)
  1230. {
  1231. int err;
  1232. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1233. if (err < 0)
  1234. goto fail;
  1235. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
  1236. if (err < 0)
  1237. goto alloc_rx_evtchn_fail;
  1238. snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
  1239. "%s-tx", queue->name);
  1240. err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
  1241. xennet_tx_interrupt,
  1242. 0, queue->tx_irq_name, queue);
  1243. if (err < 0)
  1244. goto bind_tx_fail;
  1245. queue->tx_irq = err;
  1246. snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
  1247. "%s-rx", queue->name);
  1248. err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
  1249. xennet_rx_interrupt,
  1250. 0, queue->rx_irq_name, queue);
  1251. if (err < 0)
  1252. goto bind_rx_fail;
  1253. queue->rx_irq = err;
  1254. return 0;
  1255. bind_rx_fail:
  1256. unbind_from_irqhandler(queue->tx_irq, queue);
  1257. queue->tx_irq = 0;
  1258. bind_tx_fail:
  1259. xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
  1260. queue->rx_evtchn = 0;
  1261. alloc_rx_evtchn_fail:
  1262. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1263. queue->tx_evtchn = 0;
  1264. fail:
  1265. return err;
  1266. }
  1267. static int setup_netfront(struct xenbus_device *dev,
  1268. struct netfront_queue *queue, unsigned int feature_split_evtchn)
  1269. {
  1270. struct xen_netif_tx_sring *txs;
  1271. struct xen_netif_rx_sring *rxs;
  1272. int err;
  1273. queue->tx_ring_ref = GRANT_INVALID_REF;
  1274. queue->rx_ring_ref = GRANT_INVALID_REF;
  1275. queue->rx.sring = NULL;
  1276. queue->tx.sring = NULL;
  1277. txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1278. if (!txs) {
  1279. err = -ENOMEM;
  1280. xenbus_dev_fatal(dev, err, "allocating tx ring page");
  1281. goto fail;
  1282. }
  1283. SHARED_RING_INIT(txs);
  1284. FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE);
  1285. err = xenbus_grant_ring(dev, virt_to_mfn(txs));
  1286. if (err < 0)
  1287. goto grant_tx_ring_fail;
  1288. queue->tx_ring_ref = err;
  1289. rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1290. if (!rxs) {
  1291. err = -ENOMEM;
  1292. xenbus_dev_fatal(dev, err, "allocating rx ring page");
  1293. goto alloc_rx_ring_fail;
  1294. }
  1295. SHARED_RING_INIT(rxs);
  1296. FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
  1297. err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
  1298. if (err < 0)
  1299. goto grant_rx_ring_fail;
  1300. queue->rx_ring_ref = err;
  1301. if (feature_split_evtchn)
  1302. err = setup_netfront_split(queue);
  1303. /* setup single event channel if
  1304. * a) feature-split-event-channels == 0
  1305. * b) feature-split-event-channels == 1 but failed to setup
  1306. */
  1307. if (!feature_split_evtchn || (feature_split_evtchn && err))
  1308. err = setup_netfront_single(queue);
  1309. if (err)
  1310. goto alloc_evtchn_fail;
  1311. return 0;
  1312. /* If we fail to setup netfront, it is safe to just revoke access to
  1313. * granted pages because backend is not accessing it at this point.
  1314. */
  1315. alloc_evtchn_fail:
  1316. gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
  1317. grant_rx_ring_fail:
  1318. free_page((unsigned long)rxs);
  1319. alloc_rx_ring_fail:
  1320. gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
  1321. grant_tx_ring_fail:
  1322. free_page((unsigned long)txs);
  1323. fail:
  1324. return err;
  1325. }
  1326. /* Queue-specific initialisation
  1327. * This used to be done in xennet_create_dev() but must now
  1328. * be run per-queue.
  1329. */
  1330. static int xennet_init_queue(struct netfront_queue *queue)
  1331. {
  1332. unsigned short i;
  1333. int err = 0;
  1334. spin_lock_init(&queue->tx_lock);
  1335. spin_lock_init(&queue->rx_lock);
  1336. skb_queue_head_init(&queue->rx_batch);
  1337. queue->rx_target = RX_DFL_MIN_TARGET;
  1338. queue->rx_min_target = RX_DFL_MIN_TARGET;
  1339. queue->rx_max_target = RX_MAX_TARGET;
  1340. init_timer(&queue->rx_refill_timer);
  1341. queue->rx_refill_timer.data = (unsigned long)queue;
  1342. queue->rx_refill_timer.function = rx_refill_timeout;
  1343. /* Initialise tx_skbs as a free chain containing every entry. */
  1344. queue->tx_skb_freelist = 0;
  1345. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  1346. skb_entry_set_link(&queue->tx_skbs[i], i+1);
  1347. queue->grant_tx_ref[i] = GRANT_INVALID_REF;
  1348. queue->grant_tx_page[i] = NULL;
  1349. }
  1350. /* Clear out rx_skbs */
  1351. for (i = 0; i < NET_RX_RING_SIZE; i++) {
  1352. queue->rx_skbs[i] = NULL;
  1353. queue->grant_rx_ref[i] = GRANT_INVALID_REF;
  1354. }
  1355. /* A grant for every tx ring slot */
  1356. if (gnttab_alloc_grant_references(TX_MAX_TARGET,
  1357. &queue->gref_tx_head) < 0) {
  1358. pr_alert("can't alloc tx grant refs\n");
  1359. err = -ENOMEM;
  1360. goto exit;
  1361. }
  1362. /* A grant for every rx ring slot */
  1363. if (gnttab_alloc_grant_references(RX_MAX_TARGET,
  1364. &queue->gref_rx_head) < 0) {
  1365. pr_alert("can't alloc rx grant refs\n");
  1366. err = -ENOMEM;
  1367. goto exit_free_tx;
  1368. }
  1369. netif_napi_add(queue->info->netdev, &queue->napi, xennet_poll, 64);
  1370. return 0;
  1371. exit_free_tx:
  1372. gnttab_free_grant_references(queue->gref_tx_head);
  1373. exit:
  1374. return err;
  1375. }
  1376. /* Common code used when first setting up, and when resuming. */
  1377. static int talk_to_netback(struct xenbus_device *dev,
  1378. struct netfront_info *info)
  1379. {
  1380. const char *message;
  1381. struct xenbus_transaction xbt;
  1382. int err;
  1383. unsigned int feature_split_evtchn;
  1384. unsigned int i = 0;
  1385. struct netfront_queue *queue = NULL;
  1386. unsigned int num_queues = 1;
  1387. info->netdev->irq = 0;
  1388. /* Check feature-split-event-channels */
  1389. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1390. "feature-split-event-channels", "%u",
  1391. &feature_split_evtchn);
  1392. if (err < 0)
  1393. feature_split_evtchn = 0;
  1394. /* Read mac addr. */
  1395. err = xen_net_read_mac(dev, info->netdev->dev_addr);
  1396. if (err) {
  1397. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  1398. goto out;
  1399. }
  1400. /* Allocate array of queues */
  1401. info->queues = kcalloc(num_queues, sizeof(struct netfront_queue), GFP_KERNEL);
  1402. if (!info->queues) {
  1403. err = -ENOMEM;
  1404. goto out;
  1405. }
  1406. rtnl_lock();
  1407. netif_set_real_num_tx_queues(info->netdev, num_queues);
  1408. rtnl_unlock();
  1409. /* Create shared ring, alloc event channel -- for each queue */
  1410. for (i = 0; i < num_queues; ++i) {
  1411. queue = &info->queues[i];
  1412. queue->id = i;
  1413. queue->info = info;
  1414. err = xennet_init_queue(queue);
  1415. if (err) {
  1416. /* xennet_init_queue() cleans up after itself on failure,
  1417. * but we still have to clean up any previously initialised
  1418. * queues. If i > 0, set num_queues to i, then goto
  1419. * destroy_ring, which calls xennet_disconnect_backend()
  1420. * to tidy up.
  1421. */
  1422. if (i > 0) {
  1423. rtnl_lock();
  1424. netif_set_real_num_tx_queues(info->netdev, i);
  1425. rtnl_unlock();
  1426. goto destroy_ring;
  1427. } else {
  1428. goto out;
  1429. }
  1430. }
  1431. err = setup_netfront(dev, queue, feature_split_evtchn);
  1432. if (err) {
  1433. /* As for xennet_init_queue(), setup_netfront() will tidy
  1434. * up the current queue on error, but we need to clean up
  1435. * those already allocated.
  1436. */
  1437. if (i > 0) {
  1438. rtnl_lock();
  1439. netif_set_real_num_tx_queues(info->netdev, i);
  1440. rtnl_unlock();
  1441. goto destroy_ring;
  1442. } else {
  1443. goto out;
  1444. }
  1445. }
  1446. }
  1447. again:
  1448. queue = &info->queues[0]; /* Use first queue only */
  1449. err = xenbus_transaction_start(&xbt);
  1450. if (err) {
  1451. xenbus_dev_fatal(dev, err, "starting transaction");
  1452. goto destroy_ring;
  1453. }
  1454. err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
  1455. queue->tx_ring_ref);
  1456. if (err) {
  1457. message = "writing tx ring-ref";
  1458. goto abort_transaction;
  1459. }
  1460. err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
  1461. queue->rx_ring_ref);
  1462. if (err) {
  1463. message = "writing rx ring-ref";
  1464. goto abort_transaction;
  1465. }
  1466. if (queue->tx_evtchn == queue->rx_evtchn) {
  1467. err = xenbus_printf(xbt, dev->nodename,
  1468. "event-channel", "%u", queue->tx_evtchn);
  1469. if (err) {
  1470. message = "writing event-channel";
  1471. goto abort_transaction;
  1472. }
  1473. } else {
  1474. err = xenbus_printf(xbt, dev->nodename,
  1475. "event-channel-tx", "%u", queue->tx_evtchn);
  1476. if (err) {
  1477. message = "writing event-channel-tx";
  1478. goto abort_transaction;
  1479. }
  1480. err = xenbus_printf(xbt, dev->nodename,
  1481. "event-channel-rx", "%u", queue->rx_evtchn);
  1482. if (err) {
  1483. message = "writing event-channel-rx";
  1484. goto abort_transaction;
  1485. }
  1486. }
  1487. err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
  1488. 1);
  1489. if (err) {
  1490. message = "writing request-rx-copy";
  1491. goto abort_transaction;
  1492. }
  1493. err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
  1494. if (err) {
  1495. message = "writing feature-rx-notify";
  1496. goto abort_transaction;
  1497. }
  1498. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
  1499. if (err) {
  1500. message = "writing feature-sg";
  1501. goto abort_transaction;
  1502. }
  1503. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
  1504. if (err) {
  1505. message = "writing feature-gso-tcpv4";
  1506. goto abort_transaction;
  1507. }
  1508. err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
  1509. if (err) {
  1510. message = "writing feature-gso-tcpv6";
  1511. goto abort_transaction;
  1512. }
  1513. err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
  1514. "1");
  1515. if (err) {
  1516. message = "writing feature-ipv6-csum-offload";
  1517. goto abort_transaction;
  1518. }
  1519. err = xenbus_transaction_end(xbt, 0);
  1520. if (err) {
  1521. if (err == -EAGAIN)
  1522. goto again;
  1523. xenbus_dev_fatal(dev, err, "completing transaction");
  1524. goto destroy_ring;
  1525. }
  1526. return 0;
  1527. abort_transaction:
  1528. xenbus_transaction_end(xbt, 1);
  1529. xenbus_dev_fatal(dev, err, "%s", message);
  1530. destroy_ring:
  1531. xennet_disconnect_backend(info);
  1532. kfree(info->queues);
  1533. info->queues = NULL;
  1534. rtnl_lock();
  1535. netif_set_real_num_tx_queues(info->netdev, 0);
  1536. rtnl_lock();
  1537. out:
  1538. return err;
  1539. }
  1540. static int xennet_connect(struct net_device *dev)
  1541. {
  1542. struct netfront_info *np = netdev_priv(dev);
  1543. unsigned int num_queues = 0;
  1544. int i, requeue_idx, err;
  1545. struct sk_buff *skb;
  1546. grant_ref_t ref;
  1547. struct xen_netif_rx_request *req;
  1548. unsigned int feature_rx_copy;
  1549. unsigned int j = 0;
  1550. struct netfront_queue *queue = NULL;
  1551. err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  1552. "feature-rx-copy", "%u", &feature_rx_copy);
  1553. if (err != 1)
  1554. feature_rx_copy = 0;
  1555. if (!feature_rx_copy) {
  1556. dev_info(&dev->dev,
  1557. "backend does not support copying receive path\n");
  1558. return -ENODEV;
  1559. }
  1560. err = talk_to_netback(np->xbdev, np);
  1561. if (err)
  1562. return err;
  1563. /* talk_to_netback() sets the correct number of queues */
  1564. num_queues = dev->real_num_tx_queues;
  1565. rtnl_lock();
  1566. netdev_update_features(dev);
  1567. rtnl_unlock();
  1568. /* By now, the queue structures have been set up */
  1569. for (j = 0; j < num_queues; ++j) {
  1570. queue = &np->queues[j];
  1571. spin_lock_bh(&queue->rx_lock);
  1572. spin_lock_irq(&queue->tx_lock);
  1573. /* Step 1: Discard all pending TX packet fragments. */
  1574. xennet_release_tx_bufs(queue);
  1575. /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
  1576. for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
  1577. skb_frag_t *frag;
  1578. const struct page *page;
  1579. if (!queue->rx_skbs[i])
  1580. continue;
  1581. skb = queue->rx_skbs[requeue_idx] = xennet_get_rx_skb(queue, i);
  1582. ref = queue->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(queue, i);
  1583. req = RING_GET_REQUEST(&queue->rx, requeue_idx);
  1584. frag = &skb_shinfo(skb)->frags[0];
  1585. page = skb_frag_page(frag);
  1586. gnttab_grant_foreign_access_ref(
  1587. ref, queue->info->xbdev->otherend_id,
  1588. pfn_to_mfn(page_to_pfn(page)),
  1589. 0);
  1590. req->gref = ref;
  1591. req->id = requeue_idx;
  1592. requeue_idx++;
  1593. }
  1594. queue->rx.req_prod_pvt = requeue_idx;
  1595. }
  1596. /*
  1597. * Step 3: All public and private state should now be sane. Get
  1598. * ready to start sending and receiving packets and give the driver
  1599. * domain a kick because we've probably just requeued some
  1600. * packets.
  1601. */
  1602. netif_carrier_on(np->netdev);
  1603. for (j = 0; j < num_queues; ++j) {
  1604. queue = &np->queues[j];
  1605. notify_remote_via_irq(queue->tx_irq);
  1606. if (queue->tx_irq != queue->rx_irq)
  1607. notify_remote_via_irq(queue->rx_irq);
  1608. xennet_tx_buf_gc(queue);
  1609. xennet_alloc_rx_buffers(queue);
  1610. spin_unlock_irq(&queue->tx_lock);
  1611. spin_unlock_bh(&queue->rx_lock);
  1612. }
  1613. return 0;
  1614. }
  1615. /**
  1616. * Callback received when the backend's state changes.
  1617. */
  1618. static void netback_changed(struct xenbus_device *dev,
  1619. enum xenbus_state backend_state)
  1620. {
  1621. struct netfront_info *np = dev_get_drvdata(&dev->dev);
  1622. struct net_device *netdev = np->netdev;
  1623. dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
  1624. switch (backend_state) {
  1625. case XenbusStateInitialising:
  1626. case XenbusStateInitialised:
  1627. case XenbusStateReconfiguring:
  1628. case XenbusStateReconfigured:
  1629. case XenbusStateUnknown:
  1630. break;
  1631. case XenbusStateInitWait:
  1632. if (dev->state != XenbusStateInitialising)
  1633. break;
  1634. if (xennet_connect(netdev) != 0)
  1635. break;
  1636. xenbus_switch_state(dev, XenbusStateConnected);
  1637. break;
  1638. case XenbusStateConnected:
  1639. netdev_notify_peers(netdev);
  1640. break;
  1641. case XenbusStateClosed:
  1642. if (dev->state == XenbusStateClosed)
  1643. break;
  1644. /* Missed the backend's CLOSING state -- fallthrough */
  1645. case XenbusStateClosing:
  1646. xenbus_frontend_closed(dev);
  1647. break;
  1648. }
  1649. }
  1650. static const struct xennet_stat {
  1651. char name[ETH_GSTRING_LEN];
  1652. u16 offset;
  1653. } xennet_stats[] = {
  1654. {
  1655. "rx_gso_checksum_fixup",
  1656. offsetof(struct netfront_info, rx_gso_checksum_fixup)
  1657. },
  1658. };
  1659. static int xennet_get_sset_count(struct net_device *dev, int string_set)
  1660. {
  1661. switch (string_set) {
  1662. case ETH_SS_STATS:
  1663. return ARRAY_SIZE(xennet_stats);
  1664. default:
  1665. return -EINVAL;
  1666. }
  1667. }
  1668. static void xennet_get_ethtool_stats(struct net_device *dev,
  1669. struct ethtool_stats *stats, u64 * data)
  1670. {
  1671. void *np = netdev_priv(dev);
  1672. int i;
  1673. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1674. data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
  1675. }
  1676. static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  1677. {
  1678. int i;
  1679. switch (stringset) {
  1680. case ETH_SS_STATS:
  1681. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1682. memcpy(data + i * ETH_GSTRING_LEN,
  1683. xennet_stats[i].name, ETH_GSTRING_LEN);
  1684. break;
  1685. }
  1686. }
  1687. static const struct ethtool_ops xennet_ethtool_ops =
  1688. {
  1689. .get_link = ethtool_op_get_link,
  1690. .get_sset_count = xennet_get_sset_count,
  1691. .get_ethtool_stats = xennet_get_ethtool_stats,
  1692. .get_strings = xennet_get_strings,
  1693. };
  1694. #ifdef CONFIG_SYSFS
  1695. static ssize_t show_rxbuf_min(struct device *dev,
  1696. struct device_attribute *attr, char *buf)
  1697. {
  1698. struct net_device *netdev = to_net_dev(dev);
  1699. struct netfront_info *info = netdev_priv(netdev);
  1700. unsigned int num_queues = netdev->real_num_tx_queues;
  1701. if (num_queues)
  1702. return sprintf(buf, "%u\n", info->queues[0].rx_min_target);
  1703. else
  1704. return sprintf(buf, "%u\n", RX_MIN_TARGET);
  1705. }
  1706. static ssize_t store_rxbuf_min(struct device *dev,
  1707. struct device_attribute *attr,
  1708. const char *buf, size_t len)
  1709. {
  1710. struct net_device *netdev = to_net_dev(dev);
  1711. struct netfront_info *np = netdev_priv(netdev);
  1712. unsigned int num_queues = netdev->real_num_tx_queues;
  1713. char *endp;
  1714. unsigned long target;
  1715. unsigned int i;
  1716. struct netfront_queue *queue;
  1717. if (!capable(CAP_NET_ADMIN))
  1718. return -EPERM;
  1719. target = simple_strtoul(buf, &endp, 0);
  1720. if (endp == buf)
  1721. return -EBADMSG;
  1722. if (target < RX_MIN_TARGET)
  1723. target = RX_MIN_TARGET;
  1724. if (target > RX_MAX_TARGET)
  1725. target = RX_MAX_TARGET;
  1726. for (i = 0; i < num_queues; ++i) {
  1727. queue = &np->queues[i];
  1728. spin_lock_bh(&queue->rx_lock);
  1729. if (target > queue->rx_max_target)
  1730. queue->rx_max_target = target;
  1731. queue->rx_min_target = target;
  1732. if (target > queue->rx_target)
  1733. queue->rx_target = target;
  1734. xennet_alloc_rx_buffers(queue);
  1735. spin_unlock_bh(&queue->rx_lock);
  1736. }
  1737. return len;
  1738. }
  1739. static ssize_t show_rxbuf_max(struct device *dev,
  1740. struct device_attribute *attr, char *buf)
  1741. {
  1742. struct net_device *netdev = to_net_dev(dev);
  1743. struct netfront_info *info = netdev_priv(netdev);
  1744. unsigned int num_queues = netdev->real_num_tx_queues;
  1745. if (num_queues)
  1746. return sprintf(buf, "%u\n", info->queues[0].rx_max_target);
  1747. else
  1748. return sprintf(buf, "%u\n", RX_MAX_TARGET);
  1749. }
  1750. static ssize_t store_rxbuf_max(struct device *dev,
  1751. struct device_attribute *attr,
  1752. const char *buf, size_t len)
  1753. {
  1754. struct net_device *netdev = to_net_dev(dev);
  1755. struct netfront_info *np = netdev_priv(netdev);
  1756. unsigned int num_queues = netdev->real_num_tx_queues;
  1757. char *endp;
  1758. unsigned long target;
  1759. unsigned int i = 0;
  1760. struct netfront_queue *queue = NULL;
  1761. if (!capable(CAP_NET_ADMIN))
  1762. return -EPERM;
  1763. target = simple_strtoul(buf, &endp, 0);
  1764. if (endp == buf)
  1765. return -EBADMSG;
  1766. if (target < RX_MIN_TARGET)
  1767. target = RX_MIN_TARGET;
  1768. if (target > RX_MAX_TARGET)
  1769. target = RX_MAX_TARGET;
  1770. for (i = 0; i < num_queues; ++i) {
  1771. queue = &np->queues[i];
  1772. spin_lock_bh(&queue->rx_lock);
  1773. if (target < queue->rx_min_target)
  1774. queue->rx_min_target = target;
  1775. queue->rx_max_target = target;
  1776. if (target < queue->rx_target)
  1777. queue->rx_target = target;
  1778. xennet_alloc_rx_buffers(queue);
  1779. spin_unlock_bh(&queue->rx_lock);
  1780. }
  1781. return len;
  1782. }
  1783. static ssize_t show_rxbuf_cur(struct device *dev,
  1784. struct device_attribute *attr, char *buf)
  1785. {
  1786. struct net_device *netdev = to_net_dev(dev);
  1787. struct netfront_info *info = netdev_priv(netdev);
  1788. unsigned int num_queues = netdev->real_num_tx_queues;
  1789. if (num_queues)
  1790. return sprintf(buf, "%u\n", info->queues[0].rx_target);
  1791. else
  1792. return sprintf(buf, "0\n");
  1793. }
  1794. static struct device_attribute xennet_attrs[] = {
  1795. __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
  1796. __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
  1797. __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
  1798. };
  1799. static int xennet_sysfs_addif(struct net_device *netdev)
  1800. {
  1801. int i;
  1802. int err;
  1803. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
  1804. err = device_create_file(&netdev->dev,
  1805. &xennet_attrs[i]);
  1806. if (err)
  1807. goto fail;
  1808. }
  1809. return 0;
  1810. fail:
  1811. while (--i >= 0)
  1812. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1813. return err;
  1814. }
  1815. static void xennet_sysfs_delif(struct net_device *netdev)
  1816. {
  1817. int i;
  1818. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
  1819. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1820. }
  1821. #endif /* CONFIG_SYSFS */
  1822. static const struct xenbus_device_id netfront_ids[] = {
  1823. { "vif" },
  1824. { "" }
  1825. };
  1826. static int xennet_remove(struct xenbus_device *dev)
  1827. {
  1828. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1829. unsigned int num_queues = info->netdev->real_num_tx_queues;
  1830. struct netfront_queue *queue = NULL;
  1831. unsigned int i = 0;
  1832. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1833. xennet_disconnect_backend(info);
  1834. xennet_sysfs_delif(info->netdev);
  1835. unregister_netdev(info->netdev);
  1836. for (i = 0; i < num_queues; ++i) {
  1837. queue = &info->queues[i];
  1838. del_timer_sync(&queue->rx_refill_timer);
  1839. }
  1840. if (num_queues) {
  1841. kfree(info->queues);
  1842. info->queues = NULL;
  1843. }
  1844. free_percpu(info->stats);
  1845. free_netdev(info->netdev);
  1846. return 0;
  1847. }
  1848. static DEFINE_XENBUS_DRIVER(netfront, ,
  1849. .probe = netfront_probe,
  1850. .remove = xennet_remove,
  1851. .resume = netfront_resume,
  1852. .otherend_changed = netback_changed,
  1853. );
  1854. static int __init netif_init(void)
  1855. {
  1856. if (!xen_domain())
  1857. return -ENODEV;
  1858. if (!xen_has_pv_nic_devices())
  1859. return -ENODEV;
  1860. pr_info("Initialising Xen virtual ethernet driver\n");
  1861. return xenbus_register_frontend(&netfront_driver);
  1862. }
  1863. module_init(netif_init);
  1864. static void __exit netif_exit(void)
  1865. {
  1866. xenbus_unregister_driver(&netfront_driver);
  1867. }
  1868. module_exit(netif_exit);
  1869. MODULE_DESCRIPTION("Xen virtual network device frontend");
  1870. MODULE_LICENSE("GPL");
  1871. MODULE_ALIAS("xen:vif");
  1872. MODULE_ALIAS("xennet");