xen-netfront.c 53 KB

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