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