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