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