xen-netfront.c 55 KB

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