xen-netfront.c 55 KB

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