xen-netfront.c 54 KB

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