xen-netfront.c 60 KB

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