xen-netfront.c 54 KB

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