xen-netfront.c 55 KB

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