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