xen-netfront.c 55 KB

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