xen-netfront.c 53 KB

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