xen-netfront.c 53 KB

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