xen-netfront.c 55 KB

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