xen-netfront.c 53 KB

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