xen-netfront.c 54 KB

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