xen-netfront.c 55 KB

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