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 gfn;
  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. gfn = xen_page_to_gfn(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. gfn,
  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,
  350. queue->info->xbdev->otherend_id,
  351. xen_page_to_gfn(page),
  352. GNTMAP_readonly);
  353. queue->tx_skbs[id].skb = skb;
  354. queue->grant_tx_page[id] = page;
  355. queue->grant_tx_ref[id] = ref;
  356. tx->id = id;
  357. tx->gref = ref;
  358. tx->offset = offset;
  359. tx->size = len;
  360. tx->flags = 0;
  361. return tx;
  362. }
  363. static struct xen_netif_tx_request *xennet_make_txreqs(
  364. struct netfront_queue *queue, struct xen_netif_tx_request *tx,
  365. struct sk_buff *skb, struct page *page,
  366. unsigned int offset, unsigned int len)
  367. {
  368. /* Skip unused frames from start of page */
  369. page += offset >> PAGE_SHIFT;
  370. offset &= ~PAGE_MASK;
  371. while (len) {
  372. tx->flags |= XEN_NETTXF_more_data;
  373. tx = xennet_make_one_txreq(queue, skb_get(skb),
  374. page, offset, len);
  375. page++;
  376. offset = 0;
  377. len -= tx->size;
  378. }
  379. return tx;
  380. }
  381. /*
  382. * Count how many ring slots are required to send this skb. Each frag
  383. * might be a compound page.
  384. */
  385. static int xennet_count_skb_slots(struct sk_buff *skb)
  386. {
  387. int i, frags = skb_shinfo(skb)->nr_frags;
  388. int pages;
  389. pages = PFN_UP(offset_in_page(skb->data) + skb_headlen(skb));
  390. for (i = 0; i < frags; i++) {
  391. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  392. unsigned long size = skb_frag_size(frag);
  393. unsigned long offset = frag->page_offset;
  394. /* Skip unused frames from start of page */
  395. offset &= ~PAGE_MASK;
  396. pages += PFN_UP(offset + size);
  397. }
  398. return pages;
  399. }
  400. static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
  401. void *accel_priv, select_queue_fallback_t fallback)
  402. {
  403. unsigned int num_queues = dev->real_num_tx_queues;
  404. u32 hash;
  405. u16 queue_idx;
  406. /* First, check if there is only one queue */
  407. if (num_queues == 1) {
  408. queue_idx = 0;
  409. } else {
  410. hash = skb_get_hash(skb);
  411. queue_idx = hash % num_queues;
  412. }
  413. return queue_idx;
  414. }
  415. static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  416. {
  417. struct netfront_info *np = netdev_priv(dev);
  418. struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
  419. struct xen_netif_tx_request *tx, *first_tx;
  420. unsigned int i;
  421. int notify;
  422. int slots;
  423. struct page *page;
  424. unsigned int offset;
  425. unsigned int len;
  426. unsigned long flags;
  427. struct netfront_queue *queue = NULL;
  428. unsigned int num_queues = dev->real_num_tx_queues;
  429. u16 queue_index;
  430. /* Drop the packet if no queues are set up */
  431. if (num_queues < 1)
  432. goto drop;
  433. /* Determine which queue to transmit this SKB on */
  434. queue_index = skb_get_queue_mapping(skb);
  435. queue = &np->queues[queue_index];
  436. /* If skb->len is too big for wire format, drop skb and alert
  437. * user about misconfiguration.
  438. */
  439. if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
  440. net_alert_ratelimited(
  441. "xennet: skb->len = %u, too big for wire format\n",
  442. skb->len);
  443. goto drop;
  444. }
  445. slots = xennet_count_skb_slots(skb);
  446. if (unlikely(slots > MAX_SKB_FRAGS + 1)) {
  447. net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
  448. slots, skb->len);
  449. if (skb_linearize(skb))
  450. goto drop;
  451. }
  452. page = virt_to_page(skb->data);
  453. offset = offset_in_page(skb->data);
  454. len = skb_headlen(skb);
  455. spin_lock_irqsave(&queue->tx_lock, flags);
  456. if (unlikely(!netif_carrier_ok(dev) ||
  457. (slots > 1 && !xennet_can_sg(dev)) ||
  458. netif_needs_gso(skb, netif_skb_features(skb)))) {
  459. spin_unlock_irqrestore(&queue->tx_lock, flags);
  460. goto drop;
  461. }
  462. /* First request for the linear area. */
  463. first_tx = tx = xennet_make_one_txreq(queue, skb,
  464. page, offset, len);
  465. page++;
  466. offset = 0;
  467. len -= tx->size;
  468. if (skb->ip_summed == CHECKSUM_PARTIAL)
  469. /* local packet? */
  470. tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
  471. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  472. /* remote but checksummed. */
  473. tx->flags |= XEN_NETTXF_data_validated;
  474. /* Optional extra info after the first request. */
  475. if (skb_shinfo(skb)->gso_size) {
  476. struct xen_netif_extra_info *gso;
  477. gso = (struct xen_netif_extra_info *)
  478. RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
  479. tx->flags |= XEN_NETTXF_extra_info;
  480. gso->u.gso.size = skb_shinfo(skb)->gso_size;
  481. gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
  482. XEN_NETIF_GSO_TYPE_TCPV6 :
  483. XEN_NETIF_GSO_TYPE_TCPV4;
  484. gso->u.gso.pad = 0;
  485. gso->u.gso.features = 0;
  486. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  487. gso->flags = 0;
  488. }
  489. /* Requests for the rest of the linear area. */
  490. tx = xennet_make_txreqs(queue, tx, skb, page, offset, len);
  491. /* Requests for all the frags. */
  492. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  493. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  494. tx = xennet_make_txreqs(queue, tx, skb,
  495. skb_frag_page(frag), frag->page_offset,
  496. skb_frag_size(frag));
  497. }
  498. /* First request has the packet length. */
  499. first_tx->size = skb->len;
  500. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
  501. if (notify)
  502. notify_remote_via_irq(queue->tx_irq);
  503. u64_stats_update_begin(&tx_stats->syncp);
  504. tx_stats->bytes += skb->len;
  505. tx_stats->packets++;
  506. u64_stats_update_end(&tx_stats->syncp);
  507. /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
  508. xennet_tx_buf_gc(queue);
  509. if (!netfront_tx_slot_available(queue))
  510. netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
  511. spin_unlock_irqrestore(&queue->tx_lock, flags);
  512. return NETDEV_TX_OK;
  513. drop:
  514. dev->stats.tx_dropped++;
  515. dev_kfree_skb_any(skb);
  516. return NETDEV_TX_OK;
  517. }
  518. static int xennet_close(struct net_device *dev)
  519. {
  520. struct netfront_info *np = netdev_priv(dev);
  521. unsigned int num_queues = dev->real_num_tx_queues;
  522. unsigned int i;
  523. struct netfront_queue *queue;
  524. netif_tx_stop_all_queues(np->netdev);
  525. for (i = 0; i < num_queues; ++i) {
  526. queue = &np->queues[i];
  527. napi_disable(&queue->napi);
  528. }
  529. return 0;
  530. }
  531. static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
  532. grant_ref_t ref)
  533. {
  534. int new = xennet_rxidx(queue->rx.req_prod_pvt);
  535. BUG_ON(queue->rx_skbs[new]);
  536. queue->rx_skbs[new] = skb;
  537. queue->grant_rx_ref[new] = ref;
  538. RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
  539. RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
  540. queue->rx.req_prod_pvt++;
  541. }
  542. static int xennet_get_extras(struct netfront_queue *queue,
  543. struct xen_netif_extra_info *extras,
  544. RING_IDX rp)
  545. {
  546. struct xen_netif_extra_info *extra;
  547. struct device *dev = &queue->info->netdev->dev;
  548. RING_IDX cons = queue->rx.rsp_cons;
  549. int err = 0;
  550. do {
  551. struct sk_buff *skb;
  552. grant_ref_t ref;
  553. if (unlikely(cons + 1 == rp)) {
  554. if (net_ratelimit())
  555. dev_warn(dev, "Missing extra info\n");
  556. err = -EBADR;
  557. break;
  558. }
  559. extra = (struct xen_netif_extra_info *)
  560. RING_GET_RESPONSE(&queue->rx, ++cons);
  561. if (unlikely(!extra->type ||
  562. extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  563. if (net_ratelimit())
  564. dev_warn(dev, "Invalid extra type: %d\n",
  565. extra->type);
  566. err = -EINVAL;
  567. } else {
  568. memcpy(&extras[extra->type - 1], extra,
  569. sizeof(*extra));
  570. }
  571. skb = xennet_get_rx_skb(queue, cons);
  572. ref = xennet_get_rx_ref(queue, cons);
  573. xennet_move_rx_slot(queue, skb, ref);
  574. } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
  575. queue->rx.rsp_cons = cons;
  576. return err;
  577. }
  578. static int xennet_get_responses(struct netfront_queue *queue,
  579. struct netfront_rx_info *rinfo, RING_IDX rp,
  580. struct sk_buff_head *list)
  581. {
  582. struct xen_netif_rx_response *rx = &rinfo->rx;
  583. struct xen_netif_extra_info *extras = rinfo->extras;
  584. struct device *dev = &queue->info->netdev->dev;
  585. RING_IDX cons = queue->rx.rsp_cons;
  586. struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
  587. grant_ref_t ref = xennet_get_rx_ref(queue, cons);
  588. int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
  589. int slots = 1;
  590. int err = 0;
  591. unsigned long ret;
  592. if (rx->flags & XEN_NETRXF_extra_info) {
  593. err = xennet_get_extras(queue, extras, rp);
  594. cons = queue->rx.rsp_cons;
  595. }
  596. for (;;) {
  597. if (unlikely(rx->status < 0 ||
  598. rx->offset + rx->status > PAGE_SIZE)) {
  599. if (net_ratelimit())
  600. dev_warn(dev, "rx->offset: %u, size: %d\n",
  601. rx->offset, rx->status);
  602. xennet_move_rx_slot(queue, skb, ref);
  603. err = -EINVAL;
  604. goto next;
  605. }
  606. /*
  607. * This definitely indicates a bug, either in this driver or in
  608. * the backend driver. In future this should flag the bad
  609. * situation to the system controller to reboot the backend.
  610. */
  611. if (ref == GRANT_INVALID_REF) {
  612. if (net_ratelimit())
  613. dev_warn(dev, "Bad rx response id %d.\n",
  614. rx->id);
  615. err = -EINVAL;
  616. goto next;
  617. }
  618. ret = gnttab_end_foreign_access_ref(ref, 0);
  619. BUG_ON(!ret);
  620. gnttab_release_grant_reference(&queue->gref_rx_head, ref);
  621. __skb_queue_tail(list, skb);
  622. next:
  623. if (!(rx->flags & XEN_NETRXF_more_data))
  624. break;
  625. if (cons + slots == rp) {
  626. if (net_ratelimit())
  627. dev_warn(dev, "Need more slots\n");
  628. err = -ENOENT;
  629. break;
  630. }
  631. rx = RING_GET_RESPONSE(&queue->rx, cons + slots);
  632. skb = xennet_get_rx_skb(queue, cons + slots);
  633. ref = xennet_get_rx_ref(queue, cons + slots);
  634. slots++;
  635. }
  636. if (unlikely(slots > max)) {
  637. if (net_ratelimit())
  638. dev_warn(dev, "Too many slots\n");
  639. err = -E2BIG;
  640. }
  641. if (unlikely(err))
  642. queue->rx.rsp_cons = cons + slots;
  643. return err;
  644. }
  645. static int xennet_set_skb_gso(struct sk_buff *skb,
  646. struct xen_netif_extra_info *gso)
  647. {
  648. if (!gso->u.gso.size) {
  649. if (net_ratelimit())
  650. pr_warn("GSO size must not be zero\n");
  651. return -EINVAL;
  652. }
  653. if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
  654. gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
  655. if (net_ratelimit())
  656. pr_warn("Bad GSO type %d\n", gso->u.gso.type);
  657. return -EINVAL;
  658. }
  659. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  660. skb_shinfo(skb)->gso_type =
  661. (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
  662. SKB_GSO_TCPV4 :
  663. SKB_GSO_TCPV6;
  664. /* Header must be checked, and gso_segs computed. */
  665. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  666. skb_shinfo(skb)->gso_segs = 0;
  667. return 0;
  668. }
  669. static RING_IDX xennet_fill_frags(struct netfront_queue *queue,
  670. struct sk_buff *skb,
  671. struct sk_buff_head *list)
  672. {
  673. struct skb_shared_info *shinfo = skb_shinfo(skb);
  674. RING_IDX cons = queue->rx.rsp_cons;
  675. struct sk_buff *nskb;
  676. while ((nskb = __skb_dequeue(list))) {
  677. struct xen_netif_rx_response *rx =
  678. RING_GET_RESPONSE(&queue->rx, ++cons);
  679. skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
  680. if (shinfo->nr_frags == MAX_SKB_FRAGS) {
  681. unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  682. BUG_ON(pull_to <= skb_headlen(skb));
  683. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  684. }
  685. BUG_ON(shinfo->nr_frags >= MAX_SKB_FRAGS);
  686. skb_add_rx_frag(skb, shinfo->nr_frags, skb_frag_page(nfrag),
  687. rx->offset, rx->status, PAGE_SIZE);
  688. skb_shinfo(nskb)->nr_frags = 0;
  689. kfree_skb(nskb);
  690. }
  691. return cons;
  692. }
  693. static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
  694. {
  695. bool recalculate_partial_csum = false;
  696. /*
  697. * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  698. * peers can fail to set NETRXF_csum_blank when sending a GSO
  699. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  700. * recalculate the partial checksum.
  701. */
  702. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  703. struct netfront_info *np = netdev_priv(dev);
  704. atomic_inc(&np->rx_gso_checksum_fixup);
  705. skb->ip_summed = CHECKSUM_PARTIAL;
  706. recalculate_partial_csum = true;
  707. }
  708. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  709. if (skb->ip_summed != CHECKSUM_PARTIAL)
  710. return 0;
  711. return skb_checksum_setup(skb, recalculate_partial_csum);
  712. }
  713. static int handle_incoming_queue(struct netfront_queue *queue,
  714. struct sk_buff_head *rxq)
  715. {
  716. struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
  717. int packets_dropped = 0;
  718. struct sk_buff *skb;
  719. while ((skb = __skb_dequeue(rxq)) != NULL) {
  720. int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  721. if (pull_to > skb_headlen(skb))
  722. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  723. /* Ethernet work: Delayed to here as it peeks the header. */
  724. skb->protocol = eth_type_trans(skb, queue->info->netdev);
  725. skb_reset_network_header(skb);
  726. if (checksum_setup(queue->info->netdev, skb)) {
  727. kfree_skb(skb);
  728. packets_dropped++;
  729. queue->info->netdev->stats.rx_errors++;
  730. continue;
  731. }
  732. u64_stats_update_begin(&rx_stats->syncp);
  733. rx_stats->packets++;
  734. rx_stats->bytes += skb->len;
  735. u64_stats_update_end(&rx_stats->syncp);
  736. /* Pass it up. */
  737. napi_gro_receive(&queue->napi, skb);
  738. }
  739. return packets_dropped;
  740. }
  741. static int xennet_poll(struct napi_struct *napi, int budget)
  742. {
  743. struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
  744. struct net_device *dev = queue->info->netdev;
  745. struct sk_buff *skb;
  746. struct netfront_rx_info rinfo;
  747. struct xen_netif_rx_response *rx = &rinfo.rx;
  748. struct xen_netif_extra_info *extras = rinfo.extras;
  749. RING_IDX i, rp;
  750. int work_done;
  751. struct sk_buff_head rxq;
  752. struct sk_buff_head errq;
  753. struct sk_buff_head tmpq;
  754. int err;
  755. spin_lock(&queue->rx_lock);
  756. skb_queue_head_init(&rxq);
  757. skb_queue_head_init(&errq);
  758. skb_queue_head_init(&tmpq);
  759. rp = queue->rx.sring->rsp_prod;
  760. rmb(); /* Ensure we see queued responses up to 'rp'. */
  761. i = queue->rx.rsp_cons;
  762. work_done = 0;
  763. while ((i != rp) && (work_done < budget)) {
  764. memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
  765. memset(extras, 0, sizeof(rinfo.extras));
  766. err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
  767. if (unlikely(err)) {
  768. err:
  769. while ((skb = __skb_dequeue(&tmpq)))
  770. __skb_queue_tail(&errq, skb);
  771. dev->stats.rx_errors++;
  772. i = queue->rx.rsp_cons;
  773. continue;
  774. }
  775. skb = __skb_dequeue(&tmpq);
  776. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  777. struct xen_netif_extra_info *gso;
  778. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  779. if (unlikely(xennet_set_skb_gso(skb, gso))) {
  780. __skb_queue_head(&tmpq, skb);
  781. queue->rx.rsp_cons += skb_queue_len(&tmpq);
  782. goto err;
  783. }
  784. }
  785. NETFRONT_SKB_CB(skb)->pull_to = rx->status;
  786. if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
  787. NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
  788. skb_shinfo(skb)->frags[0].page_offset = rx->offset;
  789. skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
  790. skb->data_len = rx->status;
  791. skb->len += rx->status;
  792. i = xennet_fill_frags(queue, skb, &tmpq);
  793. if (rx->flags & XEN_NETRXF_csum_blank)
  794. skb->ip_summed = CHECKSUM_PARTIAL;
  795. else if (rx->flags & XEN_NETRXF_data_validated)
  796. skb->ip_summed = CHECKSUM_UNNECESSARY;
  797. __skb_queue_tail(&rxq, skb);
  798. queue->rx.rsp_cons = ++i;
  799. work_done++;
  800. }
  801. __skb_queue_purge(&errq);
  802. work_done -= handle_incoming_queue(queue, &rxq);
  803. xennet_alloc_rx_buffers(queue);
  804. if (work_done < budget) {
  805. int more_to_do = 0;
  806. napi_complete(napi);
  807. RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
  808. if (more_to_do)
  809. napi_schedule(napi);
  810. }
  811. spin_unlock(&queue->rx_lock);
  812. return work_done;
  813. }
  814. static int xennet_change_mtu(struct net_device *dev, int mtu)
  815. {
  816. int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : 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. np->queues = NULL;
  1007. err = -ENOMEM;
  1008. np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
  1009. if (np->rx_stats == NULL)
  1010. goto exit;
  1011. np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
  1012. if (np->tx_stats == NULL)
  1013. goto exit;
  1014. netdev->netdev_ops = &xennet_netdev_ops;
  1015. netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  1016. NETIF_F_GSO_ROBUST;
  1017. netdev->hw_features = NETIF_F_SG |
  1018. NETIF_F_IPV6_CSUM |
  1019. NETIF_F_TSO | NETIF_F_TSO6;
  1020. /*
  1021. * Assume that all hw features are available for now. This set
  1022. * will be adjusted by the call to netdev_update_features() in
  1023. * xennet_connect() which is the earliest point where we can
  1024. * negotiate with the backend regarding supported features.
  1025. */
  1026. netdev->features |= netdev->hw_features;
  1027. netdev->ethtool_ops = &xennet_ethtool_ops;
  1028. SET_NETDEV_DEV(netdev, &dev->dev);
  1029. np->netdev = netdev;
  1030. netif_carrier_off(netdev);
  1031. return netdev;
  1032. exit:
  1033. xennet_free_netdev(netdev);
  1034. return ERR_PTR(err);
  1035. }
  1036. /**
  1037. * Entry point to this code when a new device is created. Allocate the basic
  1038. * structures and the ring buffers for communication with the backend, and
  1039. * inform the backend of the appropriate details for those.
  1040. */
  1041. static int netfront_probe(struct xenbus_device *dev,
  1042. const struct xenbus_device_id *id)
  1043. {
  1044. int err;
  1045. struct net_device *netdev;
  1046. struct netfront_info *info;
  1047. netdev = xennet_create_dev(dev);
  1048. if (IS_ERR(netdev)) {
  1049. err = PTR_ERR(netdev);
  1050. xenbus_dev_fatal(dev, err, "creating netdev");
  1051. return err;
  1052. }
  1053. info = netdev_priv(netdev);
  1054. dev_set_drvdata(&dev->dev, info);
  1055. #ifdef CONFIG_SYSFS
  1056. info->netdev->sysfs_groups[0] = &xennet_dev_group;
  1057. #endif
  1058. err = register_netdev(info->netdev);
  1059. if (err) {
  1060. pr_warn("%s: register_netdev err=%d\n", __func__, err);
  1061. goto fail;
  1062. }
  1063. return 0;
  1064. fail:
  1065. xennet_free_netdev(netdev);
  1066. dev_set_drvdata(&dev->dev, NULL);
  1067. return err;
  1068. }
  1069. static void xennet_end_access(int ref, void *page)
  1070. {
  1071. /* This frees the page as a side-effect */
  1072. if (ref != GRANT_INVALID_REF)
  1073. gnttab_end_foreign_access(ref, 0, (unsigned long)page);
  1074. }
  1075. static void xennet_disconnect_backend(struct netfront_info *info)
  1076. {
  1077. unsigned int i = 0;
  1078. unsigned int num_queues = info->netdev->real_num_tx_queues;
  1079. netif_carrier_off(info->netdev);
  1080. for (i = 0; i < num_queues && info->queues; ++i) {
  1081. struct netfront_queue *queue = &info->queues[i];
  1082. if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
  1083. unbind_from_irqhandler(queue->tx_irq, queue);
  1084. if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
  1085. unbind_from_irqhandler(queue->tx_irq, queue);
  1086. unbind_from_irqhandler(queue->rx_irq, queue);
  1087. }
  1088. queue->tx_evtchn = queue->rx_evtchn = 0;
  1089. queue->tx_irq = queue->rx_irq = 0;
  1090. if (netif_running(info->netdev))
  1091. napi_synchronize(&queue->napi);
  1092. xennet_release_tx_bufs(queue);
  1093. xennet_release_rx_bufs(queue);
  1094. gnttab_free_grant_references(queue->gref_tx_head);
  1095. gnttab_free_grant_references(queue->gref_rx_head);
  1096. /* End access and free the pages */
  1097. xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
  1098. xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
  1099. queue->tx_ring_ref = GRANT_INVALID_REF;
  1100. queue->rx_ring_ref = GRANT_INVALID_REF;
  1101. queue->tx.sring = NULL;
  1102. queue->rx.sring = NULL;
  1103. }
  1104. }
  1105. /**
  1106. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1107. * driver restart. We tear down our netif structure and recreate it, but
  1108. * leave the device-layer structures intact so that this is transparent to the
  1109. * rest of the kernel.
  1110. */
  1111. static int netfront_resume(struct xenbus_device *dev)
  1112. {
  1113. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1114. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1115. xennet_disconnect_backend(info);
  1116. return 0;
  1117. }
  1118. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  1119. {
  1120. char *s, *e, *macstr;
  1121. int i;
  1122. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  1123. if (IS_ERR(macstr))
  1124. return PTR_ERR(macstr);
  1125. for (i = 0; i < ETH_ALEN; i++) {
  1126. mac[i] = simple_strtoul(s, &e, 16);
  1127. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  1128. kfree(macstr);
  1129. return -ENOENT;
  1130. }
  1131. s = e+1;
  1132. }
  1133. kfree(macstr);
  1134. return 0;
  1135. }
  1136. static int setup_netfront_single(struct netfront_queue *queue)
  1137. {
  1138. int err;
  1139. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1140. if (err < 0)
  1141. goto fail;
  1142. err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
  1143. xennet_interrupt,
  1144. 0, queue->info->netdev->name, queue);
  1145. if (err < 0)
  1146. goto bind_fail;
  1147. queue->rx_evtchn = queue->tx_evtchn;
  1148. queue->rx_irq = queue->tx_irq = err;
  1149. return 0;
  1150. bind_fail:
  1151. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1152. queue->tx_evtchn = 0;
  1153. fail:
  1154. return err;
  1155. }
  1156. static int setup_netfront_split(struct netfront_queue *queue)
  1157. {
  1158. int err;
  1159. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1160. if (err < 0)
  1161. goto fail;
  1162. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
  1163. if (err < 0)
  1164. goto alloc_rx_evtchn_fail;
  1165. snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
  1166. "%s-tx", queue->name);
  1167. err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
  1168. xennet_tx_interrupt,
  1169. 0, queue->tx_irq_name, queue);
  1170. if (err < 0)
  1171. goto bind_tx_fail;
  1172. queue->tx_irq = err;
  1173. snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
  1174. "%s-rx", queue->name);
  1175. err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
  1176. xennet_rx_interrupt,
  1177. 0, queue->rx_irq_name, queue);
  1178. if (err < 0)
  1179. goto bind_rx_fail;
  1180. queue->rx_irq = err;
  1181. return 0;
  1182. bind_rx_fail:
  1183. unbind_from_irqhandler(queue->tx_irq, queue);
  1184. queue->tx_irq = 0;
  1185. bind_tx_fail:
  1186. xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
  1187. queue->rx_evtchn = 0;
  1188. alloc_rx_evtchn_fail:
  1189. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1190. queue->tx_evtchn = 0;
  1191. fail:
  1192. return err;
  1193. }
  1194. static int setup_netfront(struct xenbus_device *dev,
  1195. struct netfront_queue *queue, unsigned int feature_split_evtchn)
  1196. {
  1197. struct xen_netif_tx_sring *txs;
  1198. struct xen_netif_rx_sring *rxs;
  1199. grant_ref_t gref;
  1200. int err;
  1201. queue->tx_ring_ref = GRANT_INVALID_REF;
  1202. queue->rx_ring_ref = GRANT_INVALID_REF;
  1203. queue->rx.sring = NULL;
  1204. queue->tx.sring = NULL;
  1205. txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1206. if (!txs) {
  1207. err = -ENOMEM;
  1208. xenbus_dev_fatal(dev, err, "allocating tx ring page");
  1209. goto fail;
  1210. }
  1211. SHARED_RING_INIT(txs);
  1212. FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE);
  1213. err = xenbus_grant_ring(dev, txs, 1, &gref);
  1214. if (err < 0)
  1215. goto grant_tx_ring_fail;
  1216. queue->tx_ring_ref = gref;
  1217. rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1218. if (!rxs) {
  1219. err = -ENOMEM;
  1220. xenbus_dev_fatal(dev, err, "allocating rx ring page");
  1221. goto alloc_rx_ring_fail;
  1222. }
  1223. SHARED_RING_INIT(rxs);
  1224. FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
  1225. err = xenbus_grant_ring(dev, rxs, 1, &gref);
  1226. if (err < 0)
  1227. goto grant_rx_ring_fail;
  1228. queue->rx_ring_ref = gref;
  1229. if (feature_split_evtchn)
  1230. err = setup_netfront_split(queue);
  1231. /* setup single event channel if
  1232. * a) feature-split-event-channels == 0
  1233. * b) feature-split-event-channels == 1 but failed to setup
  1234. */
  1235. if (!feature_split_evtchn || (feature_split_evtchn && err))
  1236. err = setup_netfront_single(queue);
  1237. if (err)
  1238. goto alloc_evtchn_fail;
  1239. return 0;
  1240. /* If we fail to setup netfront, it is safe to just revoke access to
  1241. * granted pages because backend is not accessing it at this point.
  1242. */
  1243. alloc_evtchn_fail:
  1244. gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
  1245. grant_rx_ring_fail:
  1246. free_page((unsigned long)rxs);
  1247. alloc_rx_ring_fail:
  1248. gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
  1249. grant_tx_ring_fail:
  1250. free_page((unsigned long)txs);
  1251. fail:
  1252. return err;
  1253. }
  1254. /* Queue-specific initialisation
  1255. * This used to be done in xennet_create_dev() but must now
  1256. * be run per-queue.
  1257. */
  1258. static int xennet_init_queue(struct netfront_queue *queue)
  1259. {
  1260. unsigned short i;
  1261. int err = 0;
  1262. spin_lock_init(&queue->tx_lock);
  1263. spin_lock_init(&queue->rx_lock);
  1264. setup_timer(&queue->rx_refill_timer, rx_refill_timeout,
  1265. (unsigned long)queue);
  1266. snprintf(queue->name, sizeof(queue->name), "%s-q%u",
  1267. queue->info->netdev->name, queue->id);
  1268. /* Initialise tx_skbs as a free chain containing every entry. */
  1269. queue->tx_skb_freelist = 0;
  1270. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  1271. skb_entry_set_link(&queue->tx_skbs[i], i+1);
  1272. queue->grant_tx_ref[i] = GRANT_INVALID_REF;
  1273. queue->grant_tx_page[i] = NULL;
  1274. }
  1275. /* Clear out rx_skbs */
  1276. for (i = 0; i < NET_RX_RING_SIZE; i++) {
  1277. queue->rx_skbs[i] = NULL;
  1278. queue->grant_rx_ref[i] = GRANT_INVALID_REF;
  1279. }
  1280. /* A grant for every tx ring slot */
  1281. if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
  1282. &queue->gref_tx_head) < 0) {
  1283. pr_alert("can't alloc tx grant refs\n");
  1284. err = -ENOMEM;
  1285. goto exit;
  1286. }
  1287. /* A grant for every rx ring slot */
  1288. if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
  1289. &queue->gref_rx_head) < 0) {
  1290. pr_alert("can't alloc rx grant refs\n");
  1291. err = -ENOMEM;
  1292. goto exit_free_tx;
  1293. }
  1294. return 0;
  1295. exit_free_tx:
  1296. gnttab_free_grant_references(queue->gref_tx_head);
  1297. exit:
  1298. return err;
  1299. }
  1300. static int write_queue_xenstore_keys(struct netfront_queue *queue,
  1301. struct xenbus_transaction *xbt, int write_hierarchical)
  1302. {
  1303. /* Write the queue-specific keys into XenStore in the traditional
  1304. * way for a single queue, or in a queue subkeys for multiple
  1305. * queues.
  1306. */
  1307. struct xenbus_device *dev = queue->info->xbdev;
  1308. int err;
  1309. const char *message;
  1310. char *path;
  1311. size_t pathsize;
  1312. /* Choose the correct place to write the keys */
  1313. if (write_hierarchical) {
  1314. pathsize = strlen(dev->nodename) + 10;
  1315. path = kzalloc(pathsize, GFP_KERNEL);
  1316. if (!path) {
  1317. err = -ENOMEM;
  1318. message = "out of memory while writing ring references";
  1319. goto error;
  1320. }
  1321. snprintf(path, pathsize, "%s/queue-%u",
  1322. dev->nodename, queue->id);
  1323. } else {
  1324. path = (char *)dev->nodename;
  1325. }
  1326. /* Write ring references */
  1327. err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
  1328. queue->tx_ring_ref);
  1329. if (err) {
  1330. message = "writing tx-ring-ref";
  1331. goto error;
  1332. }
  1333. err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
  1334. queue->rx_ring_ref);
  1335. if (err) {
  1336. message = "writing rx-ring-ref";
  1337. goto error;
  1338. }
  1339. /* Write event channels; taking into account both shared
  1340. * and split event channel scenarios.
  1341. */
  1342. if (queue->tx_evtchn == queue->rx_evtchn) {
  1343. /* Shared event channel */
  1344. err = xenbus_printf(*xbt, path,
  1345. "event-channel", "%u", queue->tx_evtchn);
  1346. if (err) {
  1347. message = "writing event-channel";
  1348. goto error;
  1349. }
  1350. } else {
  1351. /* Split event channels */
  1352. err = xenbus_printf(*xbt, path,
  1353. "event-channel-tx", "%u", queue->tx_evtchn);
  1354. if (err) {
  1355. message = "writing event-channel-tx";
  1356. goto error;
  1357. }
  1358. err = xenbus_printf(*xbt, path,
  1359. "event-channel-rx", "%u", queue->rx_evtchn);
  1360. if (err) {
  1361. message = "writing event-channel-rx";
  1362. goto error;
  1363. }
  1364. }
  1365. if (write_hierarchical)
  1366. kfree(path);
  1367. return 0;
  1368. error:
  1369. if (write_hierarchical)
  1370. kfree(path);
  1371. xenbus_dev_fatal(dev, err, "%s", message);
  1372. return err;
  1373. }
  1374. static void xennet_destroy_queues(struct netfront_info *info)
  1375. {
  1376. unsigned int i;
  1377. rtnl_lock();
  1378. for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
  1379. struct netfront_queue *queue = &info->queues[i];
  1380. if (netif_running(info->netdev))
  1381. napi_disable(&queue->napi);
  1382. del_timer_sync(&queue->rx_refill_timer);
  1383. netif_napi_del(&queue->napi);
  1384. }
  1385. rtnl_unlock();
  1386. kfree(info->queues);
  1387. info->queues = NULL;
  1388. }
  1389. static int xennet_create_queues(struct netfront_info *info,
  1390. unsigned int num_queues)
  1391. {
  1392. unsigned int i;
  1393. int ret;
  1394. info->queues = kcalloc(num_queues, sizeof(struct netfront_queue),
  1395. GFP_KERNEL);
  1396. if (!info->queues)
  1397. return -ENOMEM;
  1398. rtnl_lock();
  1399. for (i = 0; i < num_queues; i++) {
  1400. struct netfront_queue *queue = &info->queues[i];
  1401. queue->id = i;
  1402. queue->info = info;
  1403. ret = xennet_init_queue(queue);
  1404. if (ret < 0) {
  1405. dev_warn(&info->netdev->dev,
  1406. "only created %d queues\n", i);
  1407. num_queues = i;
  1408. break;
  1409. }
  1410. netif_napi_add(queue->info->netdev, &queue->napi,
  1411. xennet_poll, 64);
  1412. if (netif_running(info->netdev))
  1413. napi_enable(&queue->napi);
  1414. }
  1415. netif_set_real_num_tx_queues(info->netdev, num_queues);
  1416. rtnl_unlock();
  1417. if (num_queues == 0) {
  1418. dev_err(&info->netdev->dev, "no queues\n");
  1419. return -EINVAL;
  1420. }
  1421. return 0;
  1422. }
  1423. /* Common code used when first setting up, and when resuming. */
  1424. static int talk_to_netback(struct xenbus_device *dev,
  1425. struct netfront_info *info)
  1426. {
  1427. const char *message;
  1428. struct xenbus_transaction xbt;
  1429. int err;
  1430. unsigned int feature_split_evtchn;
  1431. unsigned int i = 0;
  1432. unsigned int max_queues = 0;
  1433. struct netfront_queue *queue = NULL;
  1434. unsigned int num_queues = 1;
  1435. info->netdev->irq = 0;
  1436. /* Check if backend supports multiple queues */
  1437. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1438. "multi-queue-max-queues", "%u", &max_queues);
  1439. if (err < 0)
  1440. max_queues = 1;
  1441. num_queues = min(max_queues, xennet_max_queues);
  1442. /* Check feature-split-event-channels */
  1443. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1444. "feature-split-event-channels", "%u",
  1445. &feature_split_evtchn);
  1446. if (err < 0)
  1447. feature_split_evtchn = 0;
  1448. /* Read mac addr. */
  1449. err = xen_net_read_mac(dev, info->netdev->dev_addr);
  1450. if (err) {
  1451. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  1452. goto out;
  1453. }
  1454. if (info->queues)
  1455. xennet_destroy_queues(info);
  1456. err = xennet_create_queues(info, num_queues);
  1457. if (err < 0)
  1458. goto destroy_ring;
  1459. /* Create shared ring, alloc event channel -- for each queue */
  1460. for (i = 0; i < num_queues; ++i) {
  1461. queue = &info->queues[i];
  1462. err = setup_netfront(dev, queue, feature_split_evtchn);
  1463. if (err) {
  1464. /* setup_netfront() will tidy up the current
  1465. * queue on error, but we need to clean up
  1466. * those already allocated.
  1467. */
  1468. if (i > 0) {
  1469. rtnl_lock();
  1470. netif_set_real_num_tx_queues(info->netdev, i);
  1471. rtnl_unlock();
  1472. goto destroy_ring;
  1473. } else {
  1474. goto out;
  1475. }
  1476. }
  1477. }
  1478. again:
  1479. err = xenbus_transaction_start(&xbt);
  1480. if (err) {
  1481. xenbus_dev_fatal(dev, err, "starting transaction");
  1482. goto destroy_ring;
  1483. }
  1484. if (num_queues == 1) {
  1485. err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
  1486. if (err)
  1487. goto abort_transaction_no_dev_fatal;
  1488. } else {
  1489. /* Write the number of queues */
  1490. err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues",
  1491. "%u", num_queues);
  1492. if (err) {
  1493. message = "writing multi-queue-num-queues";
  1494. goto abort_transaction_no_dev_fatal;
  1495. }
  1496. /* Write the keys for each queue */
  1497. for (i = 0; i < num_queues; ++i) {
  1498. queue = &info->queues[i];
  1499. err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
  1500. if (err)
  1501. goto abort_transaction_no_dev_fatal;
  1502. }
  1503. }
  1504. /* The remaining keys are not queue-specific */
  1505. err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
  1506. 1);
  1507. if (err) {
  1508. message = "writing request-rx-copy";
  1509. goto abort_transaction;
  1510. }
  1511. err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
  1512. if (err) {
  1513. message = "writing feature-rx-notify";
  1514. goto abort_transaction;
  1515. }
  1516. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
  1517. if (err) {
  1518. message = "writing feature-sg";
  1519. goto abort_transaction;
  1520. }
  1521. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
  1522. if (err) {
  1523. message = "writing feature-gso-tcpv4";
  1524. goto abort_transaction;
  1525. }
  1526. err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
  1527. if (err) {
  1528. message = "writing feature-gso-tcpv6";
  1529. goto abort_transaction;
  1530. }
  1531. err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
  1532. "1");
  1533. if (err) {
  1534. message = "writing feature-ipv6-csum-offload";
  1535. goto abort_transaction;
  1536. }
  1537. err = xenbus_transaction_end(xbt, 0);
  1538. if (err) {
  1539. if (err == -EAGAIN)
  1540. goto again;
  1541. xenbus_dev_fatal(dev, err, "completing transaction");
  1542. goto destroy_ring;
  1543. }
  1544. return 0;
  1545. abort_transaction:
  1546. xenbus_dev_fatal(dev, err, "%s", message);
  1547. abort_transaction_no_dev_fatal:
  1548. xenbus_transaction_end(xbt, 1);
  1549. destroy_ring:
  1550. xennet_disconnect_backend(info);
  1551. kfree(info->queues);
  1552. info->queues = NULL;
  1553. out:
  1554. return err;
  1555. }
  1556. static int xennet_connect(struct net_device *dev)
  1557. {
  1558. struct netfront_info *np = netdev_priv(dev);
  1559. unsigned int num_queues = 0;
  1560. int err;
  1561. unsigned int feature_rx_copy;
  1562. unsigned int j = 0;
  1563. struct netfront_queue *queue = NULL;
  1564. err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  1565. "feature-rx-copy", "%u", &feature_rx_copy);
  1566. if (err != 1)
  1567. feature_rx_copy = 0;
  1568. if (!feature_rx_copy) {
  1569. dev_info(&dev->dev,
  1570. "backend does not support copying receive path\n");
  1571. return -ENODEV;
  1572. }
  1573. err = talk_to_netback(np->xbdev, np);
  1574. if (err)
  1575. return err;
  1576. /* talk_to_netback() sets the correct number of queues */
  1577. num_queues = dev->real_num_tx_queues;
  1578. rtnl_lock();
  1579. netdev_update_features(dev);
  1580. rtnl_unlock();
  1581. /*
  1582. * All public and private state should now be sane. Get
  1583. * ready to start sending and receiving packets and give the driver
  1584. * domain a kick because we've probably just requeued some
  1585. * packets.
  1586. */
  1587. netif_carrier_on(np->netdev);
  1588. for (j = 0; j < num_queues; ++j) {
  1589. queue = &np->queues[j];
  1590. notify_remote_via_irq(queue->tx_irq);
  1591. if (queue->tx_irq != queue->rx_irq)
  1592. notify_remote_via_irq(queue->rx_irq);
  1593. spin_lock_irq(&queue->tx_lock);
  1594. xennet_tx_buf_gc(queue);
  1595. spin_unlock_irq(&queue->tx_lock);
  1596. spin_lock_bh(&queue->rx_lock);
  1597. xennet_alloc_rx_buffers(queue);
  1598. spin_unlock_bh(&queue->rx_lock);
  1599. }
  1600. return 0;
  1601. }
  1602. /**
  1603. * Callback received when the backend's state changes.
  1604. */
  1605. static void netback_changed(struct xenbus_device *dev,
  1606. enum xenbus_state backend_state)
  1607. {
  1608. struct netfront_info *np = dev_get_drvdata(&dev->dev);
  1609. struct net_device *netdev = np->netdev;
  1610. dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
  1611. switch (backend_state) {
  1612. case XenbusStateInitialising:
  1613. case XenbusStateInitialised:
  1614. case XenbusStateReconfiguring:
  1615. case XenbusStateReconfigured:
  1616. case XenbusStateUnknown:
  1617. break;
  1618. case XenbusStateInitWait:
  1619. if (dev->state != XenbusStateInitialising)
  1620. break;
  1621. if (xennet_connect(netdev) != 0)
  1622. break;
  1623. xenbus_switch_state(dev, XenbusStateConnected);
  1624. break;
  1625. case XenbusStateConnected:
  1626. netdev_notify_peers(netdev);
  1627. break;
  1628. case XenbusStateClosed:
  1629. if (dev->state == XenbusStateClosed)
  1630. break;
  1631. /* Missed the backend's CLOSING state -- fallthrough */
  1632. case XenbusStateClosing:
  1633. xenbus_frontend_closed(dev);
  1634. break;
  1635. }
  1636. }
  1637. static const struct xennet_stat {
  1638. char name[ETH_GSTRING_LEN];
  1639. u16 offset;
  1640. } xennet_stats[] = {
  1641. {
  1642. "rx_gso_checksum_fixup",
  1643. offsetof(struct netfront_info, rx_gso_checksum_fixup)
  1644. },
  1645. };
  1646. static int xennet_get_sset_count(struct net_device *dev, int string_set)
  1647. {
  1648. switch (string_set) {
  1649. case ETH_SS_STATS:
  1650. return ARRAY_SIZE(xennet_stats);
  1651. default:
  1652. return -EINVAL;
  1653. }
  1654. }
  1655. static void xennet_get_ethtool_stats(struct net_device *dev,
  1656. struct ethtool_stats *stats, u64 * data)
  1657. {
  1658. void *np = netdev_priv(dev);
  1659. int i;
  1660. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1661. data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
  1662. }
  1663. static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  1664. {
  1665. int i;
  1666. switch (stringset) {
  1667. case ETH_SS_STATS:
  1668. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1669. memcpy(data + i * ETH_GSTRING_LEN,
  1670. xennet_stats[i].name, ETH_GSTRING_LEN);
  1671. break;
  1672. }
  1673. }
  1674. static const struct ethtool_ops xennet_ethtool_ops =
  1675. {
  1676. .get_link = ethtool_op_get_link,
  1677. .get_sset_count = xennet_get_sset_count,
  1678. .get_ethtool_stats = xennet_get_ethtool_stats,
  1679. .get_strings = xennet_get_strings,
  1680. };
  1681. #ifdef CONFIG_SYSFS
  1682. static ssize_t show_rxbuf(struct device *dev,
  1683. struct device_attribute *attr, char *buf)
  1684. {
  1685. return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
  1686. }
  1687. static ssize_t store_rxbuf(struct device *dev,
  1688. struct device_attribute *attr,
  1689. const char *buf, size_t len)
  1690. {
  1691. char *endp;
  1692. unsigned long target;
  1693. if (!capable(CAP_NET_ADMIN))
  1694. return -EPERM;
  1695. target = simple_strtoul(buf, &endp, 0);
  1696. if (endp == buf)
  1697. return -EBADMSG;
  1698. /* rxbuf_min and rxbuf_max are no longer configurable. */
  1699. return len;
  1700. }
  1701. static DEVICE_ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
  1702. static DEVICE_ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
  1703. static DEVICE_ATTR(rxbuf_cur, S_IRUGO, show_rxbuf, NULL);
  1704. static struct attribute *xennet_dev_attrs[] = {
  1705. &dev_attr_rxbuf_min.attr,
  1706. &dev_attr_rxbuf_max.attr,
  1707. &dev_attr_rxbuf_cur.attr,
  1708. NULL
  1709. };
  1710. static const struct attribute_group xennet_dev_group = {
  1711. .attrs = xennet_dev_attrs
  1712. };
  1713. #endif /* CONFIG_SYSFS */
  1714. static int xennet_remove(struct xenbus_device *dev)
  1715. {
  1716. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1717. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1718. xennet_disconnect_backend(info);
  1719. unregister_netdev(info->netdev);
  1720. if (info->queues)
  1721. xennet_destroy_queues(info);
  1722. xennet_free_netdev(info->netdev);
  1723. return 0;
  1724. }
  1725. static const struct xenbus_device_id netfront_ids[] = {
  1726. { "vif" },
  1727. { "" }
  1728. };
  1729. static struct xenbus_driver netfront_driver = {
  1730. .ids = netfront_ids,
  1731. .probe = netfront_probe,
  1732. .remove = xennet_remove,
  1733. .resume = netfront_resume,
  1734. .otherend_changed = netback_changed,
  1735. };
  1736. static int __init netif_init(void)
  1737. {
  1738. if (!xen_domain())
  1739. return -ENODEV;
  1740. if (!xen_has_pv_nic_devices())
  1741. return -ENODEV;
  1742. pr_info("Initialising Xen virtual ethernet driver\n");
  1743. /* Allow as many queues as there are CPUs if user has not
  1744. * specified a value.
  1745. */
  1746. if (xennet_max_queues == 0)
  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");