xen-netfront.c 60 KB

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