xen-netfront.c 54 KB

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