xen-netfront.c 55 KB

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