xen-netfront.c 59 KB

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