xen-netfront.c 55 KB

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