xen-netfront.c 55 KB

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