xen-netfront.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  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 netdev_features_t xennet_fix_features(struct net_device *dev,
  972. netdev_features_t features)
  973. {
  974. struct netfront_info *np = netdev_priv(dev);
  975. int val;
  976. if (features & NETIF_F_SG) {
  977. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
  978. "%d", &val) < 0)
  979. val = 0;
  980. if (!val)
  981. features &= ~NETIF_F_SG;
  982. }
  983. if (features & NETIF_F_IPV6_CSUM) {
  984. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  985. "feature-ipv6-csum-offload", "%d", &val) < 0)
  986. val = 0;
  987. if (!val)
  988. features &= ~NETIF_F_IPV6_CSUM;
  989. }
  990. if (features & NETIF_F_TSO) {
  991. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  992. "feature-gso-tcpv4", "%d", &val) < 0)
  993. val = 0;
  994. if (!val)
  995. features &= ~NETIF_F_TSO;
  996. }
  997. if (features & NETIF_F_TSO6) {
  998. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  999. "feature-gso-tcpv6", "%d", &val) < 0)
  1000. val = 0;
  1001. if (!val)
  1002. features &= ~NETIF_F_TSO6;
  1003. }
  1004. return features;
  1005. }
  1006. static int xennet_set_features(struct net_device *dev,
  1007. netdev_features_t features)
  1008. {
  1009. if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
  1010. netdev_info(dev, "Reducing MTU because no SG offload");
  1011. dev->mtu = ETH_DATA_LEN;
  1012. }
  1013. return 0;
  1014. }
  1015. static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
  1016. {
  1017. struct netfront_queue *queue = dev_id;
  1018. unsigned long flags;
  1019. spin_lock_irqsave(&queue->tx_lock, flags);
  1020. xennet_tx_buf_gc(queue);
  1021. spin_unlock_irqrestore(&queue->tx_lock, flags);
  1022. return IRQ_HANDLED;
  1023. }
  1024. static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
  1025. {
  1026. struct netfront_queue *queue = dev_id;
  1027. struct net_device *dev = queue->info->netdev;
  1028. if (likely(netif_carrier_ok(dev) &&
  1029. RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
  1030. napi_schedule(&queue->napi);
  1031. return IRQ_HANDLED;
  1032. }
  1033. static irqreturn_t xennet_interrupt(int irq, void *dev_id)
  1034. {
  1035. xennet_tx_interrupt(irq, dev_id);
  1036. xennet_rx_interrupt(irq, dev_id);
  1037. return IRQ_HANDLED;
  1038. }
  1039. #ifdef CONFIG_NET_POLL_CONTROLLER
  1040. static void xennet_poll_controller(struct net_device *dev)
  1041. {
  1042. /* Poll each queue */
  1043. struct netfront_info *info = netdev_priv(dev);
  1044. unsigned int num_queues = dev->real_num_tx_queues;
  1045. unsigned int i;
  1046. for (i = 0; i < num_queues; ++i)
  1047. xennet_interrupt(0, &info->queues[i]);
  1048. }
  1049. #endif
  1050. static const struct net_device_ops xennet_netdev_ops = {
  1051. .ndo_open = xennet_open,
  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), xennet_max_queues);
  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. unsigned int num_queues = info->netdev->real_num_tx_queues;
  1153. netif_carrier_off(info->netdev);
  1154. for (i = 0; i < num_queues; ++i) {
  1155. struct netfront_queue *queue = &info->queues[i];
  1156. if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
  1157. unbind_from_irqhandler(queue->tx_irq, queue);
  1158. if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
  1159. unbind_from_irqhandler(queue->tx_irq, queue);
  1160. unbind_from_irqhandler(queue->rx_irq, queue);
  1161. }
  1162. queue->tx_evtchn = queue->rx_evtchn = 0;
  1163. queue->tx_irq = queue->rx_irq = 0;
  1164. napi_synchronize(&queue->napi);
  1165. xennet_release_tx_bufs(queue);
  1166. xennet_release_rx_bufs(queue);
  1167. gnttab_free_grant_references(queue->gref_tx_head);
  1168. gnttab_free_grant_references(queue->gref_rx_head);
  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. snprintf(queue->name, sizeof(queue->name), "%s-q%u",
  1344. queue->info->netdev->name, queue->id);
  1345. /* Initialise tx_skbs as a free chain containing every entry. */
  1346. queue->tx_skb_freelist = 0;
  1347. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  1348. skb_entry_set_link(&queue->tx_skbs[i], i+1);
  1349. queue->grant_tx_ref[i] = GRANT_INVALID_REF;
  1350. queue->grant_tx_page[i] = NULL;
  1351. }
  1352. /* Clear out rx_skbs */
  1353. for (i = 0; i < NET_RX_RING_SIZE; i++) {
  1354. queue->rx_skbs[i] = NULL;
  1355. queue->grant_rx_ref[i] = GRANT_INVALID_REF;
  1356. }
  1357. /* A grant for every tx ring slot */
  1358. if (gnttab_alloc_grant_references(TX_MAX_TARGET,
  1359. &queue->gref_tx_head) < 0) {
  1360. pr_alert("can't alloc tx grant refs\n");
  1361. err = -ENOMEM;
  1362. goto exit;
  1363. }
  1364. /* A grant for every rx ring slot */
  1365. if (gnttab_alloc_grant_references(RX_MAX_TARGET,
  1366. &queue->gref_rx_head) < 0) {
  1367. pr_alert("can't alloc rx grant refs\n");
  1368. err = -ENOMEM;
  1369. goto exit_free_tx;
  1370. }
  1371. return 0;
  1372. exit_free_tx:
  1373. gnttab_free_grant_references(queue->gref_tx_head);
  1374. exit:
  1375. return err;
  1376. }
  1377. static int write_queue_xenstore_keys(struct netfront_queue *queue,
  1378. struct xenbus_transaction *xbt, int write_hierarchical)
  1379. {
  1380. /* Write the queue-specific keys into XenStore in the traditional
  1381. * way for a single queue, or in a queue subkeys for multiple
  1382. * queues.
  1383. */
  1384. struct xenbus_device *dev = queue->info->xbdev;
  1385. int err;
  1386. const char *message;
  1387. char *path;
  1388. size_t pathsize;
  1389. /* Choose the correct place to write the keys */
  1390. if (write_hierarchical) {
  1391. pathsize = strlen(dev->nodename) + 10;
  1392. path = kzalloc(pathsize, GFP_KERNEL);
  1393. if (!path) {
  1394. err = -ENOMEM;
  1395. message = "out of memory while writing ring references";
  1396. goto error;
  1397. }
  1398. snprintf(path, pathsize, "%s/queue-%u",
  1399. dev->nodename, queue->id);
  1400. } else {
  1401. path = (char *)dev->nodename;
  1402. }
  1403. /* Write ring references */
  1404. err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
  1405. queue->tx_ring_ref);
  1406. if (err) {
  1407. message = "writing tx-ring-ref";
  1408. goto error;
  1409. }
  1410. err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
  1411. queue->rx_ring_ref);
  1412. if (err) {
  1413. message = "writing rx-ring-ref";
  1414. goto error;
  1415. }
  1416. /* Write event channels; taking into account both shared
  1417. * and split event channel scenarios.
  1418. */
  1419. if (queue->tx_evtchn == queue->rx_evtchn) {
  1420. /* Shared event channel */
  1421. err = xenbus_printf(*xbt, path,
  1422. "event-channel", "%u", queue->tx_evtchn);
  1423. if (err) {
  1424. message = "writing event-channel";
  1425. goto error;
  1426. }
  1427. } else {
  1428. /* Split event channels */
  1429. err = xenbus_printf(*xbt, path,
  1430. "event-channel-tx", "%u", queue->tx_evtchn);
  1431. if (err) {
  1432. message = "writing event-channel-tx";
  1433. goto error;
  1434. }
  1435. err = xenbus_printf(*xbt, path,
  1436. "event-channel-rx", "%u", queue->rx_evtchn);
  1437. if (err) {
  1438. message = "writing event-channel-rx";
  1439. goto error;
  1440. }
  1441. }
  1442. if (write_hierarchical)
  1443. kfree(path);
  1444. return 0;
  1445. error:
  1446. if (write_hierarchical)
  1447. kfree(path);
  1448. xenbus_dev_fatal(dev, err, "%s", message);
  1449. return err;
  1450. }
  1451. static void xennet_destroy_queues(struct netfront_info *info)
  1452. {
  1453. unsigned int i;
  1454. rtnl_lock();
  1455. for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
  1456. struct netfront_queue *queue = &info->queues[i];
  1457. if (netif_running(info->netdev))
  1458. napi_disable(&queue->napi);
  1459. netif_napi_del(&queue->napi);
  1460. }
  1461. rtnl_unlock();
  1462. kfree(info->queues);
  1463. info->queues = NULL;
  1464. }
  1465. static int xennet_create_queues(struct netfront_info *info,
  1466. unsigned int num_queues)
  1467. {
  1468. unsigned int i;
  1469. int ret;
  1470. info->queues = kcalloc(num_queues, sizeof(struct netfront_queue),
  1471. GFP_KERNEL);
  1472. if (!info->queues)
  1473. return -ENOMEM;
  1474. rtnl_lock();
  1475. for (i = 0; i < num_queues; i++) {
  1476. struct netfront_queue *queue = &info->queues[i];
  1477. queue->id = i;
  1478. queue->info = info;
  1479. ret = xennet_init_queue(queue);
  1480. if (ret < 0) {
  1481. dev_warn(&info->netdev->dev,
  1482. "only created %d queues\n", i);
  1483. num_queues = i;
  1484. break;
  1485. }
  1486. netif_napi_add(queue->info->netdev, &queue->napi,
  1487. xennet_poll, 64);
  1488. if (netif_running(info->netdev))
  1489. napi_enable(&queue->napi);
  1490. }
  1491. netif_set_real_num_tx_queues(info->netdev, num_queues);
  1492. rtnl_unlock();
  1493. if (num_queues == 0) {
  1494. dev_err(&info->netdev->dev, "no queues\n");
  1495. return -EINVAL;
  1496. }
  1497. return 0;
  1498. }
  1499. /* Common code used when first setting up, and when resuming. */
  1500. static int talk_to_netback(struct xenbus_device *dev,
  1501. struct netfront_info *info)
  1502. {
  1503. const char *message;
  1504. struct xenbus_transaction xbt;
  1505. int err;
  1506. unsigned int feature_split_evtchn;
  1507. unsigned int i = 0;
  1508. unsigned int max_queues = 0;
  1509. struct netfront_queue *queue = NULL;
  1510. unsigned int num_queues = 1;
  1511. info->netdev->irq = 0;
  1512. /* Check if backend supports multiple queues */
  1513. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1514. "multi-queue-max-queues", "%u", &max_queues);
  1515. if (err < 0)
  1516. max_queues = 1;
  1517. num_queues = min(max_queues, xennet_max_queues);
  1518. /* Check feature-split-event-channels */
  1519. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1520. "feature-split-event-channels", "%u",
  1521. &feature_split_evtchn);
  1522. if (err < 0)
  1523. feature_split_evtchn = 0;
  1524. /* Read mac addr. */
  1525. err = xen_net_read_mac(dev, info->netdev->dev_addr);
  1526. if (err) {
  1527. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  1528. goto out;
  1529. }
  1530. if (info->queues)
  1531. xennet_destroy_queues(info);
  1532. err = xennet_create_queues(info, num_queues);
  1533. if (err < 0)
  1534. goto destroy_ring;
  1535. /* Create shared ring, alloc event channel -- for each queue */
  1536. for (i = 0; i < num_queues; ++i) {
  1537. queue = &info->queues[i];
  1538. err = setup_netfront(dev, queue, feature_split_evtchn);
  1539. if (err) {
  1540. /* setup_netfront() will tidy up the current
  1541. * queue on error, but we need to clean up
  1542. * those already allocated.
  1543. */
  1544. if (i > 0) {
  1545. rtnl_lock();
  1546. netif_set_real_num_tx_queues(info->netdev, i);
  1547. rtnl_unlock();
  1548. goto destroy_ring;
  1549. } else {
  1550. goto out;
  1551. }
  1552. }
  1553. }
  1554. again:
  1555. err = xenbus_transaction_start(&xbt);
  1556. if (err) {
  1557. xenbus_dev_fatal(dev, err, "starting transaction");
  1558. goto destroy_ring;
  1559. }
  1560. if (num_queues == 1) {
  1561. err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
  1562. if (err)
  1563. goto abort_transaction_no_dev_fatal;
  1564. } else {
  1565. /* Write the number of queues */
  1566. err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues",
  1567. "%u", num_queues);
  1568. if (err) {
  1569. message = "writing multi-queue-num-queues";
  1570. goto abort_transaction_no_dev_fatal;
  1571. }
  1572. /* Write the keys for each queue */
  1573. for (i = 0; i < num_queues; ++i) {
  1574. queue = &info->queues[i];
  1575. err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
  1576. if (err)
  1577. goto abort_transaction_no_dev_fatal;
  1578. }
  1579. }
  1580. /* The remaining keys are not queue-specific */
  1581. err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
  1582. 1);
  1583. if (err) {
  1584. message = "writing request-rx-copy";
  1585. goto abort_transaction;
  1586. }
  1587. err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
  1588. if (err) {
  1589. message = "writing feature-rx-notify";
  1590. goto abort_transaction;
  1591. }
  1592. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
  1593. if (err) {
  1594. message = "writing feature-sg";
  1595. goto abort_transaction;
  1596. }
  1597. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
  1598. if (err) {
  1599. message = "writing feature-gso-tcpv4";
  1600. goto abort_transaction;
  1601. }
  1602. err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
  1603. if (err) {
  1604. message = "writing feature-gso-tcpv6";
  1605. goto abort_transaction;
  1606. }
  1607. err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
  1608. "1");
  1609. if (err) {
  1610. message = "writing feature-ipv6-csum-offload";
  1611. goto abort_transaction;
  1612. }
  1613. err = xenbus_transaction_end(xbt, 0);
  1614. if (err) {
  1615. if (err == -EAGAIN)
  1616. goto again;
  1617. xenbus_dev_fatal(dev, err, "completing transaction");
  1618. goto destroy_ring;
  1619. }
  1620. return 0;
  1621. abort_transaction:
  1622. xenbus_dev_fatal(dev, err, "%s", message);
  1623. abort_transaction_no_dev_fatal:
  1624. xenbus_transaction_end(xbt, 1);
  1625. destroy_ring:
  1626. xennet_disconnect_backend(info);
  1627. kfree(info->queues);
  1628. info->queues = NULL;
  1629. rtnl_lock();
  1630. netif_set_real_num_tx_queues(info->netdev, 0);
  1631. rtnl_unlock();
  1632. out:
  1633. return err;
  1634. }
  1635. static int xennet_connect(struct net_device *dev)
  1636. {
  1637. struct netfront_info *np = netdev_priv(dev);
  1638. unsigned int num_queues = 0;
  1639. int err;
  1640. unsigned int feature_rx_copy;
  1641. unsigned int j = 0;
  1642. struct netfront_queue *queue = NULL;
  1643. err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  1644. "feature-rx-copy", "%u", &feature_rx_copy);
  1645. if (err != 1)
  1646. feature_rx_copy = 0;
  1647. if (!feature_rx_copy) {
  1648. dev_info(&dev->dev,
  1649. "backend does not support copying receive path\n");
  1650. return -ENODEV;
  1651. }
  1652. err = talk_to_netback(np->xbdev, np);
  1653. if (err)
  1654. return err;
  1655. /* talk_to_netback() sets the correct number of queues */
  1656. num_queues = dev->real_num_tx_queues;
  1657. rtnl_lock();
  1658. netdev_update_features(dev);
  1659. rtnl_unlock();
  1660. /*
  1661. * All public and private state should now be sane. Get
  1662. * ready to start sending and receiving packets and give the driver
  1663. * domain a kick because we've probably just requeued some
  1664. * packets.
  1665. */
  1666. netif_carrier_on(np->netdev);
  1667. for (j = 0; j < num_queues; ++j) {
  1668. queue = &np->queues[j];
  1669. notify_remote_via_irq(queue->tx_irq);
  1670. if (queue->tx_irq != queue->rx_irq)
  1671. notify_remote_via_irq(queue->rx_irq);
  1672. spin_lock_irq(&queue->tx_lock);
  1673. xennet_tx_buf_gc(queue);
  1674. spin_unlock_irq(&queue->tx_lock);
  1675. spin_lock_bh(&queue->rx_lock);
  1676. xennet_alloc_rx_buffers(queue);
  1677. spin_unlock_bh(&queue->rx_lock);
  1678. }
  1679. return 0;
  1680. }
  1681. /**
  1682. * Callback received when the backend's state changes.
  1683. */
  1684. static void netback_changed(struct xenbus_device *dev,
  1685. enum xenbus_state backend_state)
  1686. {
  1687. struct netfront_info *np = dev_get_drvdata(&dev->dev);
  1688. struct net_device *netdev = np->netdev;
  1689. dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
  1690. switch (backend_state) {
  1691. case XenbusStateInitialising:
  1692. case XenbusStateInitialised:
  1693. case XenbusStateReconfiguring:
  1694. case XenbusStateReconfigured:
  1695. case XenbusStateUnknown:
  1696. break;
  1697. case XenbusStateInitWait:
  1698. if (dev->state != XenbusStateInitialising)
  1699. break;
  1700. if (xennet_connect(netdev) != 0)
  1701. break;
  1702. xenbus_switch_state(dev, XenbusStateConnected);
  1703. break;
  1704. case XenbusStateConnected:
  1705. netdev_notify_peers(netdev);
  1706. break;
  1707. case XenbusStateClosed:
  1708. if (dev->state == XenbusStateClosed)
  1709. break;
  1710. /* Missed the backend's CLOSING state -- fallthrough */
  1711. case XenbusStateClosing:
  1712. xenbus_frontend_closed(dev);
  1713. break;
  1714. }
  1715. }
  1716. static const struct xennet_stat {
  1717. char name[ETH_GSTRING_LEN];
  1718. u16 offset;
  1719. } xennet_stats[] = {
  1720. {
  1721. "rx_gso_checksum_fixup",
  1722. offsetof(struct netfront_info, rx_gso_checksum_fixup)
  1723. },
  1724. };
  1725. static int xennet_get_sset_count(struct net_device *dev, int string_set)
  1726. {
  1727. switch (string_set) {
  1728. case ETH_SS_STATS:
  1729. return ARRAY_SIZE(xennet_stats);
  1730. default:
  1731. return -EINVAL;
  1732. }
  1733. }
  1734. static void xennet_get_ethtool_stats(struct net_device *dev,
  1735. struct ethtool_stats *stats, u64 * data)
  1736. {
  1737. void *np = netdev_priv(dev);
  1738. int i;
  1739. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1740. data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
  1741. }
  1742. static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  1743. {
  1744. int i;
  1745. switch (stringset) {
  1746. case ETH_SS_STATS:
  1747. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1748. memcpy(data + i * ETH_GSTRING_LEN,
  1749. xennet_stats[i].name, ETH_GSTRING_LEN);
  1750. break;
  1751. }
  1752. }
  1753. static const struct ethtool_ops xennet_ethtool_ops =
  1754. {
  1755. .get_link = ethtool_op_get_link,
  1756. .get_sset_count = xennet_get_sset_count,
  1757. .get_ethtool_stats = xennet_get_ethtool_stats,
  1758. .get_strings = xennet_get_strings,
  1759. };
  1760. #ifdef CONFIG_SYSFS
  1761. static ssize_t show_rxbuf_min(struct device *dev,
  1762. struct device_attribute *attr, char *buf)
  1763. {
  1764. struct net_device *netdev = to_net_dev(dev);
  1765. struct netfront_info *info = netdev_priv(netdev);
  1766. unsigned int num_queues = netdev->real_num_tx_queues;
  1767. if (num_queues)
  1768. return sprintf(buf, "%u\n", info->queues[0].rx_min_target);
  1769. else
  1770. return sprintf(buf, "%u\n", RX_MIN_TARGET);
  1771. }
  1772. static ssize_t store_rxbuf_min(struct device *dev,
  1773. struct device_attribute *attr,
  1774. const char *buf, size_t len)
  1775. {
  1776. struct net_device *netdev = to_net_dev(dev);
  1777. struct netfront_info *np = netdev_priv(netdev);
  1778. unsigned int num_queues = netdev->real_num_tx_queues;
  1779. char *endp;
  1780. unsigned long target;
  1781. unsigned int i;
  1782. struct netfront_queue *queue;
  1783. if (!capable(CAP_NET_ADMIN))
  1784. return -EPERM;
  1785. target = simple_strtoul(buf, &endp, 0);
  1786. if (endp == buf)
  1787. return -EBADMSG;
  1788. if (target < RX_MIN_TARGET)
  1789. target = RX_MIN_TARGET;
  1790. if (target > RX_MAX_TARGET)
  1791. target = RX_MAX_TARGET;
  1792. for (i = 0; i < num_queues; ++i) {
  1793. queue = &np->queues[i];
  1794. spin_lock_bh(&queue->rx_lock);
  1795. if (target > queue->rx_max_target)
  1796. queue->rx_max_target = target;
  1797. queue->rx_min_target = target;
  1798. if (target > queue->rx_target)
  1799. queue->rx_target = target;
  1800. xennet_alloc_rx_buffers(queue);
  1801. spin_unlock_bh(&queue->rx_lock);
  1802. }
  1803. return len;
  1804. }
  1805. static ssize_t show_rxbuf_max(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_max_target);
  1813. else
  1814. return sprintf(buf, "%u\n", RX_MAX_TARGET);
  1815. }
  1816. static ssize_t store_rxbuf_max(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 = 0;
  1826. struct netfront_queue *queue = NULL;
  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_min_target)
  1840. queue->rx_min_target = target;
  1841. queue->rx_max_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_cur(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_target);
  1857. else
  1858. return sprintf(buf, "0\n");
  1859. }
  1860. static struct device_attribute xennet_attrs[] = {
  1861. __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
  1862. __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
  1863. __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
  1864. };
  1865. static int xennet_sysfs_addif(struct net_device *netdev)
  1866. {
  1867. int i;
  1868. int err;
  1869. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
  1870. err = device_create_file(&netdev->dev,
  1871. &xennet_attrs[i]);
  1872. if (err)
  1873. goto fail;
  1874. }
  1875. return 0;
  1876. fail:
  1877. while (--i >= 0)
  1878. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1879. return err;
  1880. }
  1881. static void xennet_sysfs_delif(struct net_device *netdev)
  1882. {
  1883. int i;
  1884. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
  1885. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1886. }
  1887. #endif /* CONFIG_SYSFS */
  1888. static const struct xenbus_device_id netfront_ids[] = {
  1889. { "vif" },
  1890. { "" }
  1891. };
  1892. static int xennet_remove(struct xenbus_device *dev)
  1893. {
  1894. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1895. unsigned int num_queues = info->netdev->real_num_tx_queues;
  1896. struct netfront_queue *queue = NULL;
  1897. unsigned int i = 0;
  1898. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1899. xennet_disconnect_backend(info);
  1900. xennet_sysfs_delif(info->netdev);
  1901. unregister_netdev(info->netdev);
  1902. for (i = 0; i < num_queues; ++i) {
  1903. queue = &info->queues[i];
  1904. del_timer_sync(&queue->rx_refill_timer);
  1905. }
  1906. if (num_queues) {
  1907. kfree(info->queues);
  1908. info->queues = NULL;
  1909. }
  1910. free_percpu(info->stats);
  1911. free_netdev(info->netdev);
  1912. return 0;
  1913. }
  1914. static DEFINE_XENBUS_DRIVER(netfront, ,
  1915. .probe = netfront_probe,
  1916. .remove = xennet_remove,
  1917. .resume = netfront_resume,
  1918. .otherend_changed = netback_changed,
  1919. );
  1920. static int __init netif_init(void)
  1921. {
  1922. if (!xen_domain())
  1923. return -ENODEV;
  1924. if (!xen_has_pv_nic_devices())
  1925. return -ENODEV;
  1926. pr_info("Initialising Xen virtual ethernet driver\n");
  1927. /* Allow as many queues as there are CPUs, by default */
  1928. xennet_max_queues = num_online_cpus();
  1929. return xenbus_register_frontend(&netfront_driver);
  1930. }
  1931. module_init(netif_init);
  1932. static void __exit netif_exit(void)
  1933. {
  1934. xenbus_unregister_driver(&netfront_driver);
  1935. }
  1936. module_exit(netif_exit);
  1937. MODULE_DESCRIPTION("Xen virtual network device frontend");
  1938. MODULE_LICENSE("GPL");
  1939. MODULE_ALIAS("xen:vif");
  1940. MODULE_ALIAS("xennet");