xen-netfront.c 55 KB

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