xen-netfront.c 55 KB

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