xen-netfront.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  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. /* Skip unused frames from start of page */
  413. page += offset >> PAGE_SHIFT;
  414. offset &= ~PAGE_MASK;
  415. while (len > 0) {
  416. unsigned long bytes;
  417. bytes = PAGE_SIZE - offset;
  418. if (bytes > len)
  419. bytes = len;
  420. tx->flags |= XEN_NETTXF_more_data;
  421. id = get_id_from_freelist(&queue->tx_skb_freelist,
  422. queue->tx_skbs);
  423. queue->tx_skbs[id].skb = skb_get(skb);
  424. tx = RING_GET_REQUEST(&queue->tx, prod++);
  425. tx->id = id;
  426. ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
  427. BUG_ON((signed short)ref < 0);
  428. mfn = pfn_to_mfn(page_to_pfn(page));
  429. gnttab_grant_foreign_access_ref(ref,
  430. queue->info->xbdev->otherend_id,
  431. mfn, GNTMAP_readonly);
  432. queue->grant_tx_page[id] = page;
  433. tx->gref = queue->grant_tx_ref[id] = ref;
  434. tx->offset = offset;
  435. tx->size = bytes;
  436. tx->flags = 0;
  437. offset += bytes;
  438. len -= bytes;
  439. /* Next frame */
  440. if (offset == PAGE_SIZE && len) {
  441. BUG_ON(!PageCompound(page));
  442. page++;
  443. offset = 0;
  444. }
  445. }
  446. }
  447. queue->tx.req_prod_pvt = prod;
  448. }
  449. /*
  450. * Count how many ring slots are required to send the frags of this
  451. * skb. Each frag might be a compound page.
  452. */
  453. static int xennet_count_skb_frag_slots(struct sk_buff *skb)
  454. {
  455. int i, frags = skb_shinfo(skb)->nr_frags;
  456. int pages = 0;
  457. for (i = 0; i < frags; i++) {
  458. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  459. unsigned long size = skb_frag_size(frag);
  460. unsigned long offset = frag->page_offset;
  461. /* Skip unused frames from start of page */
  462. offset &= ~PAGE_MASK;
  463. pages += PFN_UP(offset + size);
  464. }
  465. return pages;
  466. }
  467. static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
  468. void *accel_priv, select_queue_fallback_t fallback)
  469. {
  470. unsigned int num_queues = dev->real_num_tx_queues;
  471. u32 hash;
  472. u16 queue_idx;
  473. /* First, check if there is only one queue */
  474. if (num_queues == 1) {
  475. queue_idx = 0;
  476. } else {
  477. hash = skb_get_hash(skb);
  478. queue_idx = hash % num_queues;
  479. }
  480. return queue_idx;
  481. }
  482. static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  483. {
  484. unsigned short id;
  485. struct netfront_info *np = netdev_priv(dev);
  486. struct netfront_stats *stats = this_cpu_ptr(np->stats);
  487. struct xen_netif_tx_request *tx;
  488. char *data = skb->data;
  489. RING_IDX i;
  490. grant_ref_t ref;
  491. unsigned long mfn;
  492. int notify;
  493. int slots;
  494. unsigned int offset = offset_in_page(data);
  495. unsigned int len = skb_headlen(skb);
  496. unsigned long flags;
  497. struct netfront_queue *queue = NULL;
  498. unsigned int num_queues = dev->real_num_tx_queues;
  499. u16 queue_index;
  500. /* Drop the packet if no queues are set up */
  501. if (num_queues < 1)
  502. goto drop;
  503. /* Determine which queue to transmit this SKB on */
  504. queue_index = skb_get_queue_mapping(skb);
  505. queue = &np->queues[queue_index];
  506. /* If skb->len is too big for wire format, drop skb and alert
  507. * user about misconfiguration.
  508. */
  509. if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
  510. net_alert_ratelimited(
  511. "xennet: skb->len = %u, too big for wire format\n",
  512. skb->len);
  513. goto drop;
  514. }
  515. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE) +
  516. xennet_count_skb_frag_slots(skb);
  517. if (unlikely(slots > MAX_SKB_FRAGS + 1)) {
  518. net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
  519. slots, skb->len);
  520. if (skb_linearize(skb))
  521. goto drop;
  522. data = skb->data;
  523. offset = offset_in_page(data);
  524. len = skb_headlen(skb);
  525. }
  526. spin_lock_irqsave(&queue->tx_lock, flags);
  527. if (unlikely(!netif_carrier_ok(dev) ||
  528. (slots > 1 && !xennet_can_sg(dev)) ||
  529. netif_needs_gso(dev, skb, netif_skb_features(skb)))) {
  530. spin_unlock_irqrestore(&queue->tx_lock, flags);
  531. goto drop;
  532. }
  533. i = queue->tx.req_prod_pvt;
  534. id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs);
  535. queue->tx_skbs[id].skb = skb;
  536. tx = RING_GET_REQUEST(&queue->tx, i);
  537. tx->id = id;
  538. ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
  539. BUG_ON((signed short)ref < 0);
  540. mfn = virt_to_mfn(data);
  541. gnttab_grant_foreign_access_ref(
  542. ref, queue->info->xbdev->otherend_id, mfn, GNTMAP_readonly);
  543. queue->grant_tx_page[id] = virt_to_page(data);
  544. tx->gref = queue->grant_tx_ref[id] = ref;
  545. tx->offset = offset;
  546. tx->size = len;
  547. tx->flags = 0;
  548. if (skb->ip_summed == CHECKSUM_PARTIAL)
  549. /* local packet? */
  550. tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
  551. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  552. /* remote but checksummed. */
  553. tx->flags |= XEN_NETTXF_data_validated;
  554. if (skb_shinfo(skb)->gso_size) {
  555. struct xen_netif_extra_info *gso;
  556. gso = (struct xen_netif_extra_info *)
  557. RING_GET_REQUEST(&queue->tx, ++i);
  558. tx->flags |= XEN_NETTXF_extra_info;
  559. gso->u.gso.size = skb_shinfo(skb)->gso_size;
  560. gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
  561. XEN_NETIF_GSO_TYPE_TCPV6 :
  562. XEN_NETIF_GSO_TYPE_TCPV4;
  563. gso->u.gso.pad = 0;
  564. gso->u.gso.features = 0;
  565. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  566. gso->flags = 0;
  567. }
  568. queue->tx.req_prod_pvt = i + 1;
  569. xennet_make_frags(skb, queue, tx);
  570. tx->size = skb->len;
  571. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
  572. if (notify)
  573. notify_remote_via_irq(queue->tx_irq);
  574. u64_stats_update_begin(&stats->syncp);
  575. stats->tx_bytes += skb->len;
  576. stats->tx_packets++;
  577. u64_stats_update_end(&stats->syncp);
  578. /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
  579. xennet_tx_buf_gc(queue);
  580. if (!netfront_tx_slot_available(queue))
  581. netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
  582. spin_unlock_irqrestore(&queue->tx_lock, flags);
  583. return NETDEV_TX_OK;
  584. drop:
  585. dev->stats.tx_dropped++;
  586. dev_kfree_skb_any(skb);
  587. return NETDEV_TX_OK;
  588. }
  589. static int xennet_close(struct net_device *dev)
  590. {
  591. struct netfront_info *np = netdev_priv(dev);
  592. unsigned int num_queues = dev->real_num_tx_queues;
  593. unsigned int i;
  594. struct netfront_queue *queue;
  595. netif_tx_stop_all_queues(np->netdev);
  596. for (i = 0; i < num_queues; ++i) {
  597. queue = &np->queues[i];
  598. napi_disable(&queue->napi);
  599. }
  600. return 0;
  601. }
  602. static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
  603. grant_ref_t ref)
  604. {
  605. int new = xennet_rxidx(queue->rx.req_prod_pvt);
  606. BUG_ON(queue->rx_skbs[new]);
  607. queue->rx_skbs[new] = skb;
  608. queue->grant_rx_ref[new] = ref;
  609. RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
  610. RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
  611. queue->rx.req_prod_pvt++;
  612. }
  613. static int xennet_get_extras(struct netfront_queue *queue,
  614. struct xen_netif_extra_info *extras,
  615. RING_IDX rp)
  616. {
  617. struct xen_netif_extra_info *extra;
  618. struct device *dev = &queue->info->netdev->dev;
  619. RING_IDX cons = queue->rx.rsp_cons;
  620. int err = 0;
  621. do {
  622. struct sk_buff *skb;
  623. grant_ref_t ref;
  624. if (unlikely(cons + 1 == rp)) {
  625. if (net_ratelimit())
  626. dev_warn(dev, "Missing extra info\n");
  627. err = -EBADR;
  628. break;
  629. }
  630. extra = (struct xen_netif_extra_info *)
  631. RING_GET_RESPONSE(&queue->rx, ++cons);
  632. if (unlikely(!extra->type ||
  633. extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  634. if (net_ratelimit())
  635. dev_warn(dev, "Invalid extra type: %d\n",
  636. extra->type);
  637. err = -EINVAL;
  638. } else {
  639. memcpy(&extras[extra->type - 1], extra,
  640. sizeof(*extra));
  641. }
  642. skb = xennet_get_rx_skb(queue, cons);
  643. ref = xennet_get_rx_ref(queue, cons);
  644. xennet_move_rx_slot(queue, skb, ref);
  645. } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
  646. queue->rx.rsp_cons = cons;
  647. return err;
  648. }
  649. static int xennet_get_responses(struct netfront_queue *queue,
  650. struct netfront_rx_info *rinfo, RING_IDX rp,
  651. struct sk_buff_head *list)
  652. {
  653. struct xen_netif_rx_response *rx = &rinfo->rx;
  654. struct xen_netif_extra_info *extras = rinfo->extras;
  655. struct device *dev = &queue->info->netdev->dev;
  656. RING_IDX cons = queue->rx.rsp_cons;
  657. struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
  658. grant_ref_t ref = xennet_get_rx_ref(queue, cons);
  659. int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
  660. int slots = 1;
  661. int err = 0;
  662. unsigned long ret;
  663. if (rx->flags & XEN_NETRXF_extra_info) {
  664. err = xennet_get_extras(queue, extras, rp);
  665. cons = queue->rx.rsp_cons;
  666. }
  667. for (;;) {
  668. if (unlikely(rx->status < 0 ||
  669. rx->offset + rx->status > PAGE_SIZE)) {
  670. if (net_ratelimit())
  671. dev_warn(dev, "rx->offset: %x, size: %u\n",
  672. rx->offset, rx->status);
  673. xennet_move_rx_slot(queue, skb, ref);
  674. err = -EINVAL;
  675. goto next;
  676. }
  677. /*
  678. * This definitely indicates a bug, either in this driver or in
  679. * the backend driver. In future this should flag the bad
  680. * situation to the system controller to reboot the backend.
  681. */
  682. if (ref == GRANT_INVALID_REF) {
  683. if (net_ratelimit())
  684. dev_warn(dev, "Bad rx response id %d.\n",
  685. rx->id);
  686. err = -EINVAL;
  687. goto next;
  688. }
  689. ret = gnttab_end_foreign_access_ref(ref, 0);
  690. BUG_ON(!ret);
  691. gnttab_release_grant_reference(&queue->gref_rx_head, ref);
  692. __skb_queue_tail(list, skb);
  693. next:
  694. if (!(rx->flags & XEN_NETRXF_more_data))
  695. break;
  696. if (cons + slots == rp) {
  697. if (net_ratelimit())
  698. dev_warn(dev, "Need more slots\n");
  699. err = -ENOENT;
  700. break;
  701. }
  702. rx = RING_GET_RESPONSE(&queue->rx, cons + slots);
  703. skb = xennet_get_rx_skb(queue, cons + slots);
  704. ref = xennet_get_rx_ref(queue, cons + slots);
  705. slots++;
  706. }
  707. if (unlikely(slots > max)) {
  708. if (net_ratelimit())
  709. dev_warn(dev, "Too many slots\n");
  710. err = -E2BIG;
  711. }
  712. if (unlikely(err))
  713. queue->rx.rsp_cons = cons + slots;
  714. return err;
  715. }
  716. static int xennet_set_skb_gso(struct sk_buff *skb,
  717. struct xen_netif_extra_info *gso)
  718. {
  719. if (!gso->u.gso.size) {
  720. if (net_ratelimit())
  721. pr_warn("GSO size must not be zero\n");
  722. return -EINVAL;
  723. }
  724. if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
  725. gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
  726. if (net_ratelimit())
  727. pr_warn("Bad GSO type %d\n", gso->u.gso.type);
  728. return -EINVAL;
  729. }
  730. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  731. skb_shinfo(skb)->gso_type =
  732. (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
  733. SKB_GSO_TCPV4 :
  734. SKB_GSO_TCPV6;
  735. /* Header must be checked, and gso_segs computed. */
  736. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  737. skb_shinfo(skb)->gso_segs = 0;
  738. return 0;
  739. }
  740. static RING_IDX xennet_fill_frags(struct netfront_queue *queue,
  741. struct sk_buff *skb,
  742. struct sk_buff_head *list)
  743. {
  744. struct skb_shared_info *shinfo = skb_shinfo(skb);
  745. RING_IDX cons = queue->rx.rsp_cons;
  746. struct sk_buff *nskb;
  747. while ((nskb = __skb_dequeue(list))) {
  748. struct xen_netif_rx_response *rx =
  749. RING_GET_RESPONSE(&queue->rx, ++cons);
  750. skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
  751. if (shinfo->nr_frags == MAX_SKB_FRAGS) {
  752. unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  753. BUG_ON(pull_to <= skb_headlen(skb));
  754. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  755. }
  756. BUG_ON(shinfo->nr_frags >= MAX_SKB_FRAGS);
  757. skb_add_rx_frag(skb, shinfo->nr_frags, skb_frag_page(nfrag),
  758. rx->offset, rx->status, PAGE_SIZE);
  759. skb_shinfo(nskb)->nr_frags = 0;
  760. kfree_skb(nskb);
  761. }
  762. return cons;
  763. }
  764. static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
  765. {
  766. bool recalculate_partial_csum = false;
  767. /*
  768. * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  769. * peers can fail to set NETRXF_csum_blank when sending a GSO
  770. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  771. * recalculate the partial checksum.
  772. */
  773. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  774. struct netfront_info *np = netdev_priv(dev);
  775. atomic_inc(&np->rx_gso_checksum_fixup);
  776. skb->ip_summed = CHECKSUM_PARTIAL;
  777. recalculate_partial_csum = true;
  778. }
  779. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  780. if (skb->ip_summed != CHECKSUM_PARTIAL)
  781. return 0;
  782. return skb_checksum_setup(skb, recalculate_partial_csum);
  783. }
  784. static int handle_incoming_queue(struct netfront_queue *queue,
  785. struct sk_buff_head *rxq)
  786. {
  787. struct netfront_stats *stats = this_cpu_ptr(queue->info->stats);
  788. int packets_dropped = 0;
  789. struct sk_buff *skb;
  790. while ((skb = __skb_dequeue(rxq)) != NULL) {
  791. int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  792. if (pull_to > skb_headlen(skb))
  793. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  794. /* Ethernet work: Delayed to here as it peeks the header. */
  795. skb->protocol = eth_type_trans(skb, queue->info->netdev);
  796. skb_reset_network_header(skb);
  797. if (checksum_setup(queue->info->netdev, skb)) {
  798. kfree_skb(skb);
  799. packets_dropped++;
  800. queue->info->netdev->stats.rx_errors++;
  801. continue;
  802. }
  803. u64_stats_update_begin(&stats->syncp);
  804. stats->rx_packets++;
  805. stats->rx_bytes += skb->len;
  806. u64_stats_update_end(&stats->syncp);
  807. /* Pass it up. */
  808. napi_gro_receive(&queue->napi, skb);
  809. }
  810. return packets_dropped;
  811. }
  812. static int xennet_poll(struct napi_struct *napi, int budget)
  813. {
  814. struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
  815. struct net_device *dev = queue->info->netdev;
  816. struct sk_buff *skb;
  817. struct netfront_rx_info rinfo;
  818. struct xen_netif_rx_response *rx = &rinfo.rx;
  819. struct xen_netif_extra_info *extras = rinfo.extras;
  820. RING_IDX i, rp;
  821. int work_done;
  822. struct sk_buff_head rxq;
  823. struct sk_buff_head errq;
  824. struct sk_buff_head tmpq;
  825. unsigned long flags;
  826. int err;
  827. spin_lock(&queue->rx_lock);
  828. skb_queue_head_init(&rxq);
  829. skb_queue_head_init(&errq);
  830. skb_queue_head_init(&tmpq);
  831. rp = queue->rx.sring->rsp_prod;
  832. rmb(); /* Ensure we see queued responses up to 'rp'. */
  833. i = queue->rx.rsp_cons;
  834. work_done = 0;
  835. while ((i != rp) && (work_done < budget)) {
  836. memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
  837. memset(extras, 0, sizeof(rinfo.extras));
  838. err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
  839. if (unlikely(err)) {
  840. err:
  841. while ((skb = __skb_dequeue(&tmpq)))
  842. __skb_queue_tail(&errq, skb);
  843. dev->stats.rx_errors++;
  844. i = queue->rx.rsp_cons;
  845. continue;
  846. }
  847. skb = __skb_dequeue(&tmpq);
  848. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  849. struct xen_netif_extra_info *gso;
  850. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  851. if (unlikely(xennet_set_skb_gso(skb, gso))) {
  852. __skb_queue_head(&tmpq, skb);
  853. queue->rx.rsp_cons += skb_queue_len(&tmpq);
  854. goto err;
  855. }
  856. }
  857. NETFRONT_SKB_CB(skb)->pull_to = rx->status;
  858. if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
  859. NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
  860. skb_shinfo(skb)->frags[0].page_offset = rx->offset;
  861. skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
  862. skb->data_len = rx->status;
  863. skb->len += rx->status;
  864. i = xennet_fill_frags(queue, skb, &tmpq);
  865. if (rx->flags & XEN_NETRXF_csum_blank)
  866. skb->ip_summed = CHECKSUM_PARTIAL;
  867. else if (rx->flags & XEN_NETRXF_data_validated)
  868. skb->ip_summed = CHECKSUM_UNNECESSARY;
  869. __skb_queue_tail(&rxq, skb);
  870. queue->rx.rsp_cons = ++i;
  871. work_done++;
  872. }
  873. __skb_queue_purge(&errq);
  874. work_done -= handle_incoming_queue(queue, &rxq);
  875. /* If we get a callback with very few responses, reduce fill target. */
  876. /* NB. Note exponential increase, linear decrease. */
  877. if (((queue->rx.req_prod_pvt - queue->rx.sring->rsp_prod) >
  878. ((3*queue->rx_target) / 4)) &&
  879. (--queue->rx_target < queue->rx_min_target))
  880. queue->rx_target = queue->rx_min_target;
  881. xennet_alloc_rx_buffers(queue);
  882. if (work_done < budget) {
  883. int more_to_do = 0;
  884. napi_gro_flush(napi, false);
  885. local_irq_save(flags);
  886. RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
  887. if (!more_to_do)
  888. __napi_complete(napi);
  889. local_irq_restore(flags);
  890. }
  891. spin_unlock(&queue->rx_lock);
  892. return work_done;
  893. }
  894. static int xennet_change_mtu(struct net_device *dev, int mtu)
  895. {
  896. int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : 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 netdev_features_t xennet_fix_features(struct net_device *dev,
  972. netdev_features_t features)
  973. {
  974. struct netfront_info *np = netdev_priv(dev);
  975. int val;
  976. if (features & NETIF_F_SG) {
  977. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
  978. "%d", &val) < 0)
  979. val = 0;
  980. if (!val)
  981. features &= ~NETIF_F_SG;
  982. }
  983. if (features & NETIF_F_IPV6_CSUM) {
  984. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  985. "feature-ipv6-csum-offload", "%d", &val) < 0)
  986. val = 0;
  987. if (!val)
  988. features &= ~NETIF_F_IPV6_CSUM;
  989. }
  990. if (features & NETIF_F_TSO) {
  991. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  992. "feature-gso-tcpv4", "%d", &val) < 0)
  993. val = 0;
  994. if (!val)
  995. features &= ~NETIF_F_TSO;
  996. }
  997. if (features & NETIF_F_TSO6) {
  998. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  999. "feature-gso-tcpv6", "%d", &val) < 0)
  1000. val = 0;
  1001. if (!val)
  1002. features &= ~NETIF_F_TSO6;
  1003. }
  1004. return features;
  1005. }
  1006. static int xennet_set_features(struct net_device *dev,
  1007. netdev_features_t features)
  1008. {
  1009. if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
  1010. netdev_info(dev, "Reducing MTU because no SG offload");
  1011. dev->mtu = ETH_DATA_LEN;
  1012. }
  1013. return 0;
  1014. }
  1015. static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
  1016. {
  1017. struct netfront_queue *queue = dev_id;
  1018. unsigned long flags;
  1019. spin_lock_irqsave(&queue->tx_lock, flags);
  1020. xennet_tx_buf_gc(queue);
  1021. spin_unlock_irqrestore(&queue->tx_lock, flags);
  1022. return IRQ_HANDLED;
  1023. }
  1024. static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
  1025. {
  1026. struct netfront_queue *queue = dev_id;
  1027. struct net_device *dev = queue->info->netdev;
  1028. if (likely(netif_carrier_ok(dev) &&
  1029. RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
  1030. napi_schedule(&queue->napi);
  1031. return IRQ_HANDLED;
  1032. }
  1033. static irqreturn_t xennet_interrupt(int irq, void *dev_id)
  1034. {
  1035. xennet_tx_interrupt(irq, dev_id);
  1036. xennet_rx_interrupt(irq, dev_id);
  1037. return IRQ_HANDLED;
  1038. }
  1039. #ifdef CONFIG_NET_POLL_CONTROLLER
  1040. static void xennet_poll_controller(struct net_device *dev)
  1041. {
  1042. /* Poll each queue */
  1043. struct netfront_info *info = netdev_priv(dev);
  1044. unsigned int num_queues = dev->real_num_tx_queues;
  1045. unsigned int i;
  1046. for (i = 0; i < num_queues; ++i)
  1047. xennet_interrupt(0, &info->queues[i]);
  1048. }
  1049. #endif
  1050. static const struct net_device_ops xennet_netdev_ops = {
  1051. .ndo_open = xennet_open,
  1052. .ndo_stop = xennet_close,
  1053. .ndo_start_xmit = xennet_start_xmit,
  1054. .ndo_change_mtu = xennet_change_mtu,
  1055. .ndo_get_stats64 = xennet_get_stats64,
  1056. .ndo_set_mac_address = eth_mac_addr,
  1057. .ndo_validate_addr = eth_validate_addr,
  1058. .ndo_fix_features = xennet_fix_features,
  1059. .ndo_set_features = xennet_set_features,
  1060. .ndo_select_queue = xennet_select_queue,
  1061. #ifdef CONFIG_NET_POLL_CONTROLLER
  1062. .ndo_poll_controller = xennet_poll_controller,
  1063. #endif
  1064. };
  1065. static struct net_device *xennet_create_dev(struct xenbus_device *dev)
  1066. {
  1067. int err;
  1068. struct net_device *netdev;
  1069. struct netfront_info *np;
  1070. netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
  1071. if (!netdev)
  1072. return ERR_PTR(-ENOMEM);
  1073. np = netdev_priv(netdev);
  1074. np->xbdev = dev;
  1075. /* No need to use rtnl_lock() before the call below as it
  1076. * happens before register_netdev().
  1077. */
  1078. netif_set_real_num_tx_queues(netdev, 0);
  1079. np->queues = NULL;
  1080. err = -ENOMEM;
  1081. np->stats = netdev_alloc_pcpu_stats(struct netfront_stats);
  1082. if (np->stats == NULL)
  1083. goto exit;
  1084. netdev->netdev_ops = &xennet_netdev_ops;
  1085. netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  1086. NETIF_F_GSO_ROBUST;
  1087. netdev->hw_features = NETIF_F_SG |
  1088. NETIF_F_IPV6_CSUM |
  1089. NETIF_F_TSO | NETIF_F_TSO6;
  1090. /*
  1091. * Assume that all hw features are available for now. This set
  1092. * will be adjusted by the call to netdev_update_features() in
  1093. * xennet_connect() which is the earliest point where we can
  1094. * negotiate with the backend regarding supported features.
  1095. */
  1096. netdev->features |= netdev->hw_features;
  1097. netdev->ethtool_ops = &xennet_ethtool_ops;
  1098. SET_NETDEV_DEV(netdev, &dev->dev);
  1099. np->netdev = netdev;
  1100. netif_carrier_off(netdev);
  1101. return netdev;
  1102. exit:
  1103. free_netdev(netdev);
  1104. return ERR_PTR(err);
  1105. }
  1106. /**
  1107. * Entry point to this code when a new device is created. Allocate the basic
  1108. * structures and the ring buffers for communication with the backend, and
  1109. * inform the backend of the appropriate details for those.
  1110. */
  1111. static int netfront_probe(struct xenbus_device *dev,
  1112. const struct xenbus_device_id *id)
  1113. {
  1114. int err;
  1115. struct net_device *netdev;
  1116. struct netfront_info *info;
  1117. netdev = xennet_create_dev(dev);
  1118. if (IS_ERR(netdev)) {
  1119. err = PTR_ERR(netdev);
  1120. xenbus_dev_fatal(dev, err, "creating netdev");
  1121. return err;
  1122. }
  1123. info = netdev_priv(netdev);
  1124. dev_set_drvdata(&dev->dev, info);
  1125. err = register_netdev(info->netdev);
  1126. if (err) {
  1127. pr_warn("%s: register_netdev err=%d\n", __func__, err);
  1128. goto fail;
  1129. }
  1130. err = xennet_sysfs_addif(info->netdev);
  1131. if (err) {
  1132. unregister_netdev(info->netdev);
  1133. pr_warn("%s: add sysfs failed err=%d\n", __func__, err);
  1134. goto fail;
  1135. }
  1136. return 0;
  1137. fail:
  1138. free_netdev(netdev);
  1139. dev_set_drvdata(&dev->dev, NULL);
  1140. return err;
  1141. }
  1142. static void xennet_end_access(int ref, void *page)
  1143. {
  1144. /* This frees the page as a side-effect */
  1145. if (ref != GRANT_INVALID_REF)
  1146. gnttab_end_foreign_access(ref, 0, (unsigned long)page);
  1147. }
  1148. static void xennet_disconnect_backend(struct netfront_info *info)
  1149. {
  1150. unsigned int i = 0;
  1151. unsigned int num_queues = info->netdev->real_num_tx_queues;
  1152. netif_carrier_off(info->netdev);
  1153. for (i = 0; i < num_queues; ++i) {
  1154. struct netfront_queue *queue = &info->queues[i];
  1155. if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
  1156. unbind_from_irqhandler(queue->tx_irq, queue);
  1157. if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
  1158. unbind_from_irqhandler(queue->tx_irq, queue);
  1159. unbind_from_irqhandler(queue->rx_irq, queue);
  1160. }
  1161. queue->tx_evtchn = queue->rx_evtchn = 0;
  1162. queue->tx_irq = queue->rx_irq = 0;
  1163. napi_synchronize(&queue->napi);
  1164. xennet_release_tx_bufs(queue);
  1165. xennet_release_rx_bufs(queue);
  1166. gnttab_free_grant_references(queue->gref_tx_head);
  1167. gnttab_free_grant_references(queue->gref_rx_head);
  1168. /* End access and free the pages */
  1169. xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
  1170. xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
  1171. queue->tx_ring_ref = GRANT_INVALID_REF;
  1172. queue->rx_ring_ref = GRANT_INVALID_REF;
  1173. queue->tx.sring = NULL;
  1174. queue->rx.sring = NULL;
  1175. }
  1176. }
  1177. /**
  1178. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1179. * driver restart. We tear down our netif structure and recreate it, but
  1180. * leave the device-layer structures intact so that this is transparent to the
  1181. * rest of the kernel.
  1182. */
  1183. static int netfront_resume(struct xenbus_device *dev)
  1184. {
  1185. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1186. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1187. xennet_disconnect_backend(info);
  1188. return 0;
  1189. }
  1190. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  1191. {
  1192. char *s, *e, *macstr;
  1193. int i;
  1194. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  1195. if (IS_ERR(macstr))
  1196. return PTR_ERR(macstr);
  1197. for (i = 0; i < ETH_ALEN; i++) {
  1198. mac[i] = simple_strtoul(s, &e, 16);
  1199. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  1200. kfree(macstr);
  1201. return -ENOENT;
  1202. }
  1203. s = e+1;
  1204. }
  1205. kfree(macstr);
  1206. return 0;
  1207. }
  1208. static int setup_netfront_single(struct netfront_queue *queue)
  1209. {
  1210. int err;
  1211. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1212. if (err < 0)
  1213. goto fail;
  1214. err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
  1215. xennet_interrupt,
  1216. 0, queue->info->netdev->name, queue);
  1217. if (err < 0)
  1218. goto bind_fail;
  1219. queue->rx_evtchn = queue->tx_evtchn;
  1220. queue->rx_irq = queue->tx_irq = err;
  1221. return 0;
  1222. bind_fail:
  1223. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1224. queue->tx_evtchn = 0;
  1225. fail:
  1226. return err;
  1227. }
  1228. static int setup_netfront_split(struct netfront_queue *queue)
  1229. {
  1230. int err;
  1231. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1232. if (err < 0)
  1233. goto fail;
  1234. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
  1235. if (err < 0)
  1236. goto alloc_rx_evtchn_fail;
  1237. snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
  1238. "%s-tx", queue->name);
  1239. err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
  1240. xennet_tx_interrupt,
  1241. 0, queue->tx_irq_name, queue);
  1242. if (err < 0)
  1243. goto bind_tx_fail;
  1244. queue->tx_irq = err;
  1245. snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
  1246. "%s-rx", queue->name);
  1247. err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
  1248. xennet_rx_interrupt,
  1249. 0, queue->rx_irq_name, queue);
  1250. if (err < 0)
  1251. goto bind_rx_fail;
  1252. queue->rx_irq = err;
  1253. return 0;
  1254. bind_rx_fail:
  1255. unbind_from_irqhandler(queue->tx_irq, queue);
  1256. queue->tx_irq = 0;
  1257. bind_tx_fail:
  1258. xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
  1259. queue->rx_evtchn = 0;
  1260. alloc_rx_evtchn_fail:
  1261. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1262. queue->tx_evtchn = 0;
  1263. fail:
  1264. return err;
  1265. }
  1266. static int setup_netfront(struct xenbus_device *dev,
  1267. struct netfront_queue *queue, unsigned int feature_split_evtchn)
  1268. {
  1269. struct xen_netif_tx_sring *txs;
  1270. struct xen_netif_rx_sring *rxs;
  1271. int err;
  1272. queue->tx_ring_ref = GRANT_INVALID_REF;
  1273. queue->rx_ring_ref = GRANT_INVALID_REF;
  1274. queue->rx.sring = NULL;
  1275. queue->tx.sring = NULL;
  1276. txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1277. if (!txs) {
  1278. err = -ENOMEM;
  1279. xenbus_dev_fatal(dev, err, "allocating tx ring page");
  1280. goto fail;
  1281. }
  1282. SHARED_RING_INIT(txs);
  1283. FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE);
  1284. err = xenbus_grant_ring(dev, virt_to_mfn(txs));
  1285. if (err < 0)
  1286. goto grant_tx_ring_fail;
  1287. queue->tx_ring_ref = err;
  1288. rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1289. if (!rxs) {
  1290. err = -ENOMEM;
  1291. xenbus_dev_fatal(dev, err, "allocating rx ring page");
  1292. goto alloc_rx_ring_fail;
  1293. }
  1294. SHARED_RING_INIT(rxs);
  1295. FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
  1296. err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
  1297. if (err < 0)
  1298. goto grant_rx_ring_fail;
  1299. queue->rx_ring_ref = err;
  1300. if (feature_split_evtchn)
  1301. err = setup_netfront_split(queue);
  1302. /* setup single event channel if
  1303. * a) feature-split-event-channels == 0
  1304. * b) feature-split-event-channels == 1 but failed to setup
  1305. */
  1306. if (!feature_split_evtchn || (feature_split_evtchn && err))
  1307. err = setup_netfront_single(queue);
  1308. if (err)
  1309. goto alloc_evtchn_fail;
  1310. return 0;
  1311. /* If we fail to setup netfront, it is safe to just revoke access to
  1312. * granted pages because backend is not accessing it at this point.
  1313. */
  1314. alloc_evtchn_fail:
  1315. gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
  1316. grant_rx_ring_fail:
  1317. free_page((unsigned long)rxs);
  1318. alloc_rx_ring_fail:
  1319. gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
  1320. grant_tx_ring_fail:
  1321. free_page((unsigned long)txs);
  1322. fail:
  1323. return err;
  1324. }
  1325. /* Queue-specific initialisation
  1326. * This used to be done in xennet_create_dev() but must now
  1327. * be run per-queue.
  1328. */
  1329. static int xennet_init_queue(struct netfront_queue *queue)
  1330. {
  1331. unsigned short i;
  1332. int err = 0;
  1333. spin_lock_init(&queue->tx_lock);
  1334. spin_lock_init(&queue->rx_lock);
  1335. skb_queue_head_init(&queue->rx_batch);
  1336. queue->rx_target = RX_DFL_MIN_TARGET;
  1337. queue->rx_min_target = RX_DFL_MIN_TARGET;
  1338. queue->rx_max_target = RX_MAX_TARGET;
  1339. init_timer(&queue->rx_refill_timer);
  1340. queue->rx_refill_timer.data = (unsigned long)queue;
  1341. queue->rx_refill_timer.function = rx_refill_timeout;
  1342. snprintf(queue->name, sizeof(queue->name), "%s-q%u",
  1343. queue->info->netdev->name, queue->id);
  1344. /* Initialise tx_skbs as a free chain containing every entry. */
  1345. queue->tx_skb_freelist = 0;
  1346. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  1347. skb_entry_set_link(&queue->tx_skbs[i], i+1);
  1348. queue->grant_tx_ref[i] = GRANT_INVALID_REF;
  1349. queue->grant_tx_page[i] = NULL;
  1350. }
  1351. /* Clear out rx_skbs */
  1352. for (i = 0; i < NET_RX_RING_SIZE; i++) {
  1353. queue->rx_skbs[i] = NULL;
  1354. queue->grant_rx_ref[i] = GRANT_INVALID_REF;
  1355. }
  1356. /* A grant for every tx ring slot */
  1357. if (gnttab_alloc_grant_references(TX_MAX_TARGET,
  1358. &queue->gref_tx_head) < 0) {
  1359. pr_alert("can't alloc tx grant refs\n");
  1360. err = -ENOMEM;
  1361. goto exit;
  1362. }
  1363. /* A grant for every rx ring slot */
  1364. if (gnttab_alloc_grant_references(RX_MAX_TARGET,
  1365. &queue->gref_rx_head) < 0) {
  1366. pr_alert("can't alloc rx grant refs\n");
  1367. err = -ENOMEM;
  1368. goto exit_free_tx;
  1369. }
  1370. return 0;
  1371. exit_free_tx:
  1372. gnttab_free_grant_references(queue->gref_tx_head);
  1373. exit:
  1374. return err;
  1375. }
  1376. static int write_queue_xenstore_keys(struct netfront_queue *queue,
  1377. struct xenbus_transaction *xbt, int write_hierarchical)
  1378. {
  1379. /* Write the queue-specific keys into XenStore in the traditional
  1380. * way for a single queue, or in a queue subkeys for multiple
  1381. * queues.
  1382. */
  1383. struct xenbus_device *dev = queue->info->xbdev;
  1384. int err;
  1385. const char *message;
  1386. char *path;
  1387. size_t pathsize;
  1388. /* Choose the correct place to write the keys */
  1389. if (write_hierarchical) {
  1390. pathsize = strlen(dev->nodename) + 10;
  1391. path = kzalloc(pathsize, GFP_KERNEL);
  1392. if (!path) {
  1393. err = -ENOMEM;
  1394. message = "out of memory while writing ring references";
  1395. goto error;
  1396. }
  1397. snprintf(path, pathsize, "%s/queue-%u",
  1398. dev->nodename, queue->id);
  1399. } else {
  1400. path = (char *)dev->nodename;
  1401. }
  1402. /* Write ring references */
  1403. err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
  1404. queue->tx_ring_ref);
  1405. if (err) {
  1406. message = "writing tx-ring-ref";
  1407. goto error;
  1408. }
  1409. err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
  1410. queue->rx_ring_ref);
  1411. if (err) {
  1412. message = "writing rx-ring-ref";
  1413. goto error;
  1414. }
  1415. /* Write event channels; taking into account both shared
  1416. * and split event channel scenarios.
  1417. */
  1418. if (queue->tx_evtchn == queue->rx_evtchn) {
  1419. /* Shared event channel */
  1420. err = xenbus_printf(*xbt, path,
  1421. "event-channel", "%u", queue->tx_evtchn);
  1422. if (err) {
  1423. message = "writing event-channel";
  1424. goto error;
  1425. }
  1426. } else {
  1427. /* Split event channels */
  1428. err = xenbus_printf(*xbt, path,
  1429. "event-channel-tx", "%u", queue->tx_evtchn);
  1430. if (err) {
  1431. message = "writing event-channel-tx";
  1432. goto error;
  1433. }
  1434. err = xenbus_printf(*xbt, path,
  1435. "event-channel-rx", "%u", queue->rx_evtchn);
  1436. if (err) {
  1437. message = "writing event-channel-rx";
  1438. goto error;
  1439. }
  1440. }
  1441. if (write_hierarchical)
  1442. kfree(path);
  1443. return 0;
  1444. error:
  1445. if (write_hierarchical)
  1446. kfree(path);
  1447. xenbus_dev_fatal(dev, err, "%s", message);
  1448. return err;
  1449. }
  1450. static void xennet_destroy_queues(struct netfront_info *info)
  1451. {
  1452. unsigned int i;
  1453. rtnl_lock();
  1454. for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
  1455. struct netfront_queue *queue = &info->queues[i];
  1456. if (netif_running(info->netdev))
  1457. napi_disable(&queue->napi);
  1458. netif_napi_del(&queue->napi);
  1459. }
  1460. rtnl_unlock();
  1461. kfree(info->queues);
  1462. info->queues = NULL;
  1463. }
  1464. static int xennet_create_queues(struct netfront_info *info,
  1465. unsigned int num_queues)
  1466. {
  1467. unsigned int i;
  1468. int ret;
  1469. info->queues = kcalloc(num_queues, sizeof(struct netfront_queue),
  1470. GFP_KERNEL);
  1471. if (!info->queues)
  1472. return -ENOMEM;
  1473. rtnl_lock();
  1474. for (i = 0; i < num_queues; i++) {
  1475. struct netfront_queue *queue = &info->queues[i];
  1476. queue->id = i;
  1477. queue->info = info;
  1478. ret = xennet_init_queue(queue);
  1479. if (ret < 0) {
  1480. dev_warn(&info->netdev->dev,
  1481. "only created %d queues\n", i);
  1482. num_queues = i;
  1483. break;
  1484. }
  1485. netif_napi_add(queue->info->netdev, &queue->napi,
  1486. xennet_poll, 64);
  1487. if (netif_running(info->netdev))
  1488. napi_enable(&queue->napi);
  1489. }
  1490. netif_set_real_num_tx_queues(info->netdev, num_queues);
  1491. rtnl_unlock();
  1492. if (num_queues == 0) {
  1493. dev_err(&info->netdev->dev, "no queues\n");
  1494. return -EINVAL;
  1495. }
  1496. return 0;
  1497. }
  1498. /* Common code used when first setting up, and when resuming. */
  1499. static int talk_to_netback(struct xenbus_device *dev,
  1500. struct netfront_info *info)
  1501. {
  1502. const char *message;
  1503. struct xenbus_transaction xbt;
  1504. int err;
  1505. unsigned int feature_split_evtchn;
  1506. unsigned int i = 0;
  1507. unsigned int max_queues = 0;
  1508. struct netfront_queue *queue = NULL;
  1509. unsigned int num_queues = 1;
  1510. info->netdev->irq = 0;
  1511. /* Check if backend supports multiple queues */
  1512. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1513. "multi-queue-max-queues", "%u", &max_queues);
  1514. if (err < 0)
  1515. max_queues = 1;
  1516. num_queues = min(max_queues, xennet_max_queues);
  1517. /* Check feature-split-event-channels */
  1518. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1519. "feature-split-event-channels", "%u",
  1520. &feature_split_evtchn);
  1521. if (err < 0)
  1522. feature_split_evtchn = 0;
  1523. /* Read mac addr. */
  1524. err = xen_net_read_mac(dev, info->netdev->dev_addr);
  1525. if (err) {
  1526. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  1527. goto out;
  1528. }
  1529. if (info->queues)
  1530. xennet_destroy_queues(info);
  1531. err = xennet_create_queues(info, num_queues);
  1532. if (err < 0)
  1533. goto destroy_ring;
  1534. /* Create shared ring, alloc event channel -- for each queue */
  1535. for (i = 0; i < num_queues; ++i) {
  1536. queue = &info->queues[i];
  1537. err = setup_netfront(dev, queue, feature_split_evtchn);
  1538. if (err) {
  1539. /* setup_netfront() will tidy up the current
  1540. * queue on error, but we need to clean up
  1541. * those already allocated.
  1542. */
  1543. if (i > 0) {
  1544. rtnl_lock();
  1545. netif_set_real_num_tx_queues(info->netdev, i);
  1546. rtnl_unlock();
  1547. goto destroy_ring;
  1548. } else {
  1549. goto out;
  1550. }
  1551. }
  1552. }
  1553. again:
  1554. err = xenbus_transaction_start(&xbt);
  1555. if (err) {
  1556. xenbus_dev_fatal(dev, err, "starting transaction");
  1557. goto destroy_ring;
  1558. }
  1559. if (num_queues == 1) {
  1560. err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
  1561. if (err)
  1562. goto abort_transaction_no_dev_fatal;
  1563. } else {
  1564. /* Write the number of queues */
  1565. err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues",
  1566. "%u", num_queues);
  1567. if (err) {
  1568. message = "writing multi-queue-num-queues";
  1569. goto abort_transaction_no_dev_fatal;
  1570. }
  1571. /* Write the keys for each queue */
  1572. for (i = 0; i < num_queues; ++i) {
  1573. queue = &info->queues[i];
  1574. err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
  1575. if (err)
  1576. goto abort_transaction_no_dev_fatal;
  1577. }
  1578. }
  1579. /* The remaining keys are not queue-specific */
  1580. err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
  1581. 1);
  1582. if (err) {
  1583. message = "writing request-rx-copy";
  1584. goto abort_transaction;
  1585. }
  1586. err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
  1587. if (err) {
  1588. message = "writing feature-rx-notify";
  1589. goto abort_transaction;
  1590. }
  1591. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
  1592. if (err) {
  1593. message = "writing feature-sg";
  1594. goto abort_transaction;
  1595. }
  1596. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
  1597. if (err) {
  1598. message = "writing feature-gso-tcpv4";
  1599. goto abort_transaction;
  1600. }
  1601. err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
  1602. if (err) {
  1603. message = "writing feature-gso-tcpv6";
  1604. goto abort_transaction;
  1605. }
  1606. err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
  1607. "1");
  1608. if (err) {
  1609. message = "writing feature-ipv6-csum-offload";
  1610. goto abort_transaction;
  1611. }
  1612. err = xenbus_transaction_end(xbt, 0);
  1613. if (err) {
  1614. if (err == -EAGAIN)
  1615. goto again;
  1616. xenbus_dev_fatal(dev, err, "completing transaction");
  1617. goto destroy_ring;
  1618. }
  1619. return 0;
  1620. abort_transaction:
  1621. xenbus_dev_fatal(dev, err, "%s", message);
  1622. abort_transaction_no_dev_fatal:
  1623. xenbus_transaction_end(xbt, 1);
  1624. destroy_ring:
  1625. xennet_disconnect_backend(info);
  1626. kfree(info->queues);
  1627. info->queues = NULL;
  1628. rtnl_lock();
  1629. netif_set_real_num_tx_queues(info->netdev, 0);
  1630. rtnl_unlock();
  1631. out:
  1632. return err;
  1633. }
  1634. static int xennet_connect(struct net_device *dev)
  1635. {
  1636. struct netfront_info *np = netdev_priv(dev);
  1637. unsigned int num_queues = 0;
  1638. int err;
  1639. unsigned int feature_rx_copy;
  1640. unsigned int j = 0;
  1641. struct netfront_queue *queue = NULL;
  1642. err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  1643. "feature-rx-copy", "%u", &feature_rx_copy);
  1644. if (err != 1)
  1645. feature_rx_copy = 0;
  1646. if (!feature_rx_copy) {
  1647. dev_info(&dev->dev,
  1648. "backend does not support copying receive path\n");
  1649. return -ENODEV;
  1650. }
  1651. err = talk_to_netback(np->xbdev, np);
  1652. if (err)
  1653. return err;
  1654. /* talk_to_netback() sets the correct number of queues */
  1655. num_queues = dev->real_num_tx_queues;
  1656. rtnl_lock();
  1657. netdev_update_features(dev);
  1658. rtnl_unlock();
  1659. /*
  1660. * All public and private state should now be sane. Get
  1661. * ready to start sending and receiving packets and give the driver
  1662. * domain a kick because we've probably just requeued some
  1663. * packets.
  1664. */
  1665. netif_carrier_on(np->netdev);
  1666. for (j = 0; j < num_queues; ++j) {
  1667. queue = &np->queues[j];
  1668. notify_remote_via_irq(queue->tx_irq);
  1669. if (queue->tx_irq != queue->rx_irq)
  1670. notify_remote_via_irq(queue->rx_irq);
  1671. spin_lock_irq(&queue->tx_lock);
  1672. xennet_tx_buf_gc(queue);
  1673. spin_unlock_irq(&queue->tx_lock);
  1674. spin_lock_bh(&queue->rx_lock);
  1675. xennet_alloc_rx_buffers(queue);
  1676. spin_unlock_bh(&queue->rx_lock);
  1677. }
  1678. return 0;
  1679. }
  1680. /**
  1681. * Callback received when the backend's state changes.
  1682. */
  1683. static void netback_changed(struct xenbus_device *dev,
  1684. enum xenbus_state backend_state)
  1685. {
  1686. struct netfront_info *np = dev_get_drvdata(&dev->dev);
  1687. struct net_device *netdev = np->netdev;
  1688. dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
  1689. switch (backend_state) {
  1690. case XenbusStateInitialising:
  1691. case XenbusStateInitialised:
  1692. case XenbusStateReconfiguring:
  1693. case XenbusStateReconfigured:
  1694. case XenbusStateUnknown:
  1695. break;
  1696. case XenbusStateInitWait:
  1697. if (dev->state != XenbusStateInitialising)
  1698. break;
  1699. if (xennet_connect(netdev) != 0)
  1700. break;
  1701. xenbus_switch_state(dev, XenbusStateConnected);
  1702. break;
  1703. case XenbusStateConnected:
  1704. netdev_notify_peers(netdev);
  1705. break;
  1706. case XenbusStateClosed:
  1707. if (dev->state == XenbusStateClosed)
  1708. break;
  1709. /* Missed the backend's CLOSING state -- fallthrough */
  1710. case XenbusStateClosing:
  1711. xenbus_frontend_closed(dev);
  1712. break;
  1713. }
  1714. }
  1715. static const struct xennet_stat {
  1716. char name[ETH_GSTRING_LEN];
  1717. u16 offset;
  1718. } xennet_stats[] = {
  1719. {
  1720. "rx_gso_checksum_fixup",
  1721. offsetof(struct netfront_info, rx_gso_checksum_fixup)
  1722. },
  1723. };
  1724. static int xennet_get_sset_count(struct net_device *dev, int string_set)
  1725. {
  1726. switch (string_set) {
  1727. case ETH_SS_STATS:
  1728. return ARRAY_SIZE(xennet_stats);
  1729. default:
  1730. return -EINVAL;
  1731. }
  1732. }
  1733. static void xennet_get_ethtool_stats(struct net_device *dev,
  1734. struct ethtool_stats *stats, u64 * data)
  1735. {
  1736. void *np = netdev_priv(dev);
  1737. int i;
  1738. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1739. data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
  1740. }
  1741. static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  1742. {
  1743. int i;
  1744. switch (stringset) {
  1745. case ETH_SS_STATS:
  1746. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1747. memcpy(data + i * ETH_GSTRING_LEN,
  1748. xennet_stats[i].name, ETH_GSTRING_LEN);
  1749. break;
  1750. }
  1751. }
  1752. static const struct ethtool_ops xennet_ethtool_ops =
  1753. {
  1754. .get_link = ethtool_op_get_link,
  1755. .get_sset_count = xennet_get_sset_count,
  1756. .get_ethtool_stats = xennet_get_ethtool_stats,
  1757. .get_strings = xennet_get_strings,
  1758. };
  1759. #ifdef CONFIG_SYSFS
  1760. static ssize_t show_rxbuf_min(struct device *dev,
  1761. struct device_attribute *attr, char *buf)
  1762. {
  1763. struct net_device *netdev = to_net_dev(dev);
  1764. struct netfront_info *info = netdev_priv(netdev);
  1765. unsigned int num_queues = netdev->real_num_tx_queues;
  1766. if (num_queues)
  1767. return sprintf(buf, "%u\n", info->queues[0].rx_min_target);
  1768. else
  1769. return sprintf(buf, "%u\n", RX_MIN_TARGET);
  1770. }
  1771. static ssize_t store_rxbuf_min(struct device *dev,
  1772. struct device_attribute *attr,
  1773. const char *buf, size_t len)
  1774. {
  1775. struct net_device *netdev = to_net_dev(dev);
  1776. struct netfront_info *np = netdev_priv(netdev);
  1777. unsigned int num_queues = netdev->real_num_tx_queues;
  1778. char *endp;
  1779. unsigned long target;
  1780. unsigned int i;
  1781. struct netfront_queue *queue;
  1782. if (!capable(CAP_NET_ADMIN))
  1783. return -EPERM;
  1784. target = simple_strtoul(buf, &endp, 0);
  1785. if (endp == buf)
  1786. return -EBADMSG;
  1787. if (target < RX_MIN_TARGET)
  1788. target = RX_MIN_TARGET;
  1789. if (target > RX_MAX_TARGET)
  1790. target = RX_MAX_TARGET;
  1791. for (i = 0; i < num_queues; ++i) {
  1792. queue = &np->queues[i];
  1793. spin_lock_bh(&queue->rx_lock);
  1794. if (target > queue->rx_max_target)
  1795. queue->rx_max_target = target;
  1796. queue->rx_min_target = target;
  1797. if (target > queue->rx_target)
  1798. queue->rx_target = target;
  1799. xennet_alloc_rx_buffers(queue);
  1800. spin_unlock_bh(&queue->rx_lock);
  1801. }
  1802. return len;
  1803. }
  1804. static ssize_t show_rxbuf_max(struct device *dev,
  1805. struct device_attribute *attr, char *buf)
  1806. {
  1807. struct net_device *netdev = to_net_dev(dev);
  1808. struct netfront_info *info = netdev_priv(netdev);
  1809. unsigned int num_queues = netdev->real_num_tx_queues;
  1810. if (num_queues)
  1811. return sprintf(buf, "%u\n", info->queues[0].rx_max_target);
  1812. else
  1813. return sprintf(buf, "%u\n", RX_MAX_TARGET);
  1814. }
  1815. static ssize_t store_rxbuf_max(struct device *dev,
  1816. struct device_attribute *attr,
  1817. const char *buf, size_t len)
  1818. {
  1819. struct net_device *netdev = to_net_dev(dev);
  1820. struct netfront_info *np = netdev_priv(netdev);
  1821. unsigned int num_queues = netdev->real_num_tx_queues;
  1822. char *endp;
  1823. unsigned long target;
  1824. unsigned int i = 0;
  1825. struct netfront_queue *queue = NULL;
  1826. if (!capable(CAP_NET_ADMIN))
  1827. return -EPERM;
  1828. target = simple_strtoul(buf, &endp, 0);
  1829. if (endp == buf)
  1830. return -EBADMSG;
  1831. if (target < RX_MIN_TARGET)
  1832. target = RX_MIN_TARGET;
  1833. if (target > RX_MAX_TARGET)
  1834. target = RX_MAX_TARGET;
  1835. for (i = 0; i < num_queues; ++i) {
  1836. queue = &np->queues[i];
  1837. spin_lock_bh(&queue->rx_lock);
  1838. if (target < queue->rx_min_target)
  1839. queue->rx_min_target = target;
  1840. queue->rx_max_target = target;
  1841. if (target < queue->rx_target)
  1842. queue->rx_target = target;
  1843. xennet_alloc_rx_buffers(queue);
  1844. spin_unlock_bh(&queue->rx_lock);
  1845. }
  1846. return len;
  1847. }
  1848. static ssize_t show_rxbuf_cur(struct device *dev,
  1849. struct device_attribute *attr, char *buf)
  1850. {
  1851. struct net_device *netdev = to_net_dev(dev);
  1852. struct netfront_info *info = netdev_priv(netdev);
  1853. unsigned int num_queues = netdev->real_num_tx_queues;
  1854. if (num_queues)
  1855. return sprintf(buf, "%u\n", info->queues[0].rx_target);
  1856. else
  1857. return sprintf(buf, "0\n");
  1858. }
  1859. static struct device_attribute xennet_attrs[] = {
  1860. __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
  1861. __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
  1862. __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
  1863. };
  1864. static int xennet_sysfs_addif(struct net_device *netdev)
  1865. {
  1866. int i;
  1867. int err;
  1868. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
  1869. err = device_create_file(&netdev->dev,
  1870. &xennet_attrs[i]);
  1871. if (err)
  1872. goto fail;
  1873. }
  1874. return 0;
  1875. fail:
  1876. while (--i >= 0)
  1877. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1878. return err;
  1879. }
  1880. static void xennet_sysfs_delif(struct net_device *netdev)
  1881. {
  1882. int i;
  1883. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
  1884. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1885. }
  1886. #endif /* CONFIG_SYSFS */
  1887. static int xennet_remove(struct xenbus_device *dev)
  1888. {
  1889. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1890. unsigned int num_queues = info->netdev->real_num_tx_queues;
  1891. struct netfront_queue *queue = NULL;
  1892. unsigned int i = 0;
  1893. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1894. xennet_disconnect_backend(info);
  1895. xennet_sysfs_delif(info->netdev);
  1896. unregister_netdev(info->netdev);
  1897. for (i = 0; i < num_queues; ++i) {
  1898. queue = &info->queues[i];
  1899. del_timer_sync(&queue->rx_refill_timer);
  1900. }
  1901. if (num_queues) {
  1902. kfree(info->queues);
  1903. info->queues = NULL;
  1904. }
  1905. free_percpu(info->stats);
  1906. free_netdev(info->netdev);
  1907. return 0;
  1908. }
  1909. static const struct xenbus_device_id netfront_ids[] = {
  1910. { "vif" },
  1911. { "" }
  1912. };
  1913. static struct xenbus_driver netfront_driver = {
  1914. .ids = netfront_ids,
  1915. .probe = netfront_probe,
  1916. .remove = xennet_remove,
  1917. .resume = netfront_resume,
  1918. .otherend_changed = netback_changed,
  1919. };
  1920. static int __init netif_init(void)
  1921. {
  1922. if (!xen_domain())
  1923. return -ENODEV;
  1924. if (!xen_has_pv_nic_devices())
  1925. return -ENODEV;
  1926. pr_info("Initialising Xen virtual ethernet driver\n");
  1927. /* Allow as many queues as there are CPUs, by default */
  1928. xennet_max_queues = num_online_cpus();
  1929. return xenbus_register_frontend(&netfront_driver);
  1930. }
  1931. module_init(netif_init);
  1932. static void __exit netif_exit(void)
  1933. {
  1934. xenbus_unregister_driver(&netfront_driver);
  1935. }
  1936. module_exit(netif_exit);
  1937. MODULE_DESCRIPTION("Xen virtual network device frontend");
  1938. MODULE_LICENSE("GPL");
  1939. MODULE_ALIAS("xen:vif");
  1940. MODULE_ALIAS("xennet");