xen-netfront.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  1. /*
  2. * Virtual network driver for conversing with remote driver backends.
  3. *
  4. * Copyright (c) 2002-2005, K A Fraser
  5. * Copyright (c) 2005, XenSource Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation; or, when distributed
  10. * separately from the Linux kernel or incorporated into other
  11. * software packages, subject to the following license:
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this source file (the "Software"), to deal in the Software without
  15. * restriction, including without limitation the rights to use, copy, modify,
  16. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  17. * and to permit persons to whom the Software is furnished to do so, subject to
  18. * the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  29. * IN THE SOFTWARE.
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #include <linux/module.h>
  33. #include <linux/kernel.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/if_ether.h>
  39. #include <net/tcp.h>
  40. #include <linux/udp.h>
  41. #include <linux/moduleparam.h>
  42. #include <linux/mm.h>
  43. #include <linux/slab.h>
  44. #include <net/ip.h>
  45. #include <xen/xen.h>
  46. #include <xen/xenbus.h>
  47. #include <xen/events.h>
  48. #include <xen/page.h>
  49. #include <xen/platform_pci.h>
  50. #include <xen/grant_table.h>
  51. #include <xen/interface/io/netif.h>
  52. #include <xen/interface/memory.h>
  53. #include <xen/interface/grant_table.h>
  54. /* Module parameters */
  55. #define MAX_QUEUES_DEFAULT 8
  56. static unsigned int xennet_max_queues;
  57. module_param_named(max_queues, xennet_max_queues, uint, 0644);
  58. MODULE_PARM_DESC(max_queues,
  59. "Maximum number of queues per virtual interface");
  60. static const struct ethtool_ops xennet_ethtool_ops;
  61. struct netfront_cb {
  62. int pull_to;
  63. };
  64. #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
  65. #define RX_COPY_THRESHOLD 256
  66. #define GRANT_INVALID_REF 0
  67. #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
  68. #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
  69. /* Minimum number of Rx slots (includes slot for GSO metadata). */
  70. #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
  71. /* Queue name is interface name with "-qNNN" appended */
  72. #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
  73. /* IRQ name is queue name with "-tx" or "-rx" appended */
  74. #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
  75. static DECLARE_WAIT_QUEUE_HEAD(module_unload_q);
  76. struct netfront_stats {
  77. u64 packets;
  78. u64 bytes;
  79. struct u64_stats_sync syncp;
  80. };
  81. struct netfront_info;
  82. struct netfront_queue {
  83. unsigned int id; /* Queue ID, 0-based */
  84. char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
  85. struct netfront_info *info;
  86. struct napi_struct napi;
  87. /* Split event channels support, tx_* == rx_* when using
  88. * single event channel.
  89. */
  90. unsigned int tx_evtchn, rx_evtchn;
  91. unsigned int tx_irq, rx_irq;
  92. /* Only used when split event channels support is enabled */
  93. char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
  94. char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
  95. spinlock_t tx_lock;
  96. struct xen_netif_tx_front_ring tx;
  97. int tx_ring_ref;
  98. /*
  99. * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
  100. * are linked from tx_skb_freelist through skb_entry.link.
  101. *
  102. * NB. Freelist index entries are always going to be less than
  103. * PAGE_OFFSET, whereas pointers to skbs will always be equal or
  104. * greater than PAGE_OFFSET: we use this property to distinguish
  105. * them.
  106. */
  107. union skb_entry {
  108. struct sk_buff *skb;
  109. unsigned long link;
  110. } tx_skbs[NET_TX_RING_SIZE];
  111. grant_ref_t gref_tx_head;
  112. grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
  113. struct page *grant_tx_page[NET_TX_RING_SIZE];
  114. unsigned tx_skb_freelist;
  115. spinlock_t rx_lock ____cacheline_aligned_in_smp;
  116. struct xen_netif_rx_front_ring rx;
  117. int rx_ring_ref;
  118. struct timer_list rx_refill_timer;
  119. struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
  120. grant_ref_t gref_rx_head;
  121. grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
  122. };
  123. struct netfront_info {
  124. struct list_head list;
  125. struct net_device *netdev;
  126. struct xenbus_device *xbdev;
  127. /* Multi-queue support */
  128. struct netfront_queue *queues;
  129. /* Statistics */
  130. struct netfront_stats __percpu *rx_stats;
  131. struct netfront_stats __percpu *tx_stats;
  132. atomic_t rx_gso_checksum_fixup;
  133. };
  134. struct netfront_rx_info {
  135. struct xen_netif_rx_response rx;
  136. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
  137. };
  138. static void skb_entry_set_link(union skb_entry *list, unsigned short id)
  139. {
  140. list->link = id;
  141. }
  142. static int skb_entry_is_link(const union skb_entry *list)
  143. {
  144. BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
  145. return (unsigned long)list->skb < PAGE_OFFSET;
  146. }
  147. /*
  148. * Access macros for acquiring freeing slots in tx_skbs[].
  149. */
  150. static void add_id_to_freelist(unsigned *head, union skb_entry *list,
  151. unsigned short id)
  152. {
  153. skb_entry_set_link(&list[id], *head);
  154. *head = id;
  155. }
  156. static unsigned short get_id_from_freelist(unsigned *head,
  157. union skb_entry *list)
  158. {
  159. unsigned int id = *head;
  160. *head = list[id].link;
  161. return id;
  162. }
  163. static int xennet_rxidx(RING_IDX idx)
  164. {
  165. return idx & (NET_RX_RING_SIZE - 1);
  166. }
  167. static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
  168. RING_IDX ri)
  169. {
  170. int i = xennet_rxidx(ri);
  171. struct sk_buff *skb = queue->rx_skbs[i];
  172. queue->rx_skbs[i] = NULL;
  173. return skb;
  174. }
  175. static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
  176. RING_IDX ri)
  177. {
  178. int i = xennet_rxidx(ri);
  179. grant_ref_t ref = queue->grant_rx_ref[i];
  180. queue->grant_rx_ref[i] = GRANT_INVALID_REF;
  181. return ref;
  182. }
  183. #ifdef CONFIG_SYSFS
  184. static const struct attribute_group xennet_dev_group;
  185. #endif
  186. static bool xennet_can_sg(struct net_device *dev)
  187. {
  188. return dev->features & NETIF_F_SG;
  189. }
  190. static void rx_refill_timeout(struct timer_list *t)
  191. {
  192. struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
  193. napi_schedule(&queue->napi);
  194. }
  195. static int netfront_tx_slot_available(struct netfront_queue *queue)
  196. {
  197. return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
  198. (NET_TX_RING_SIZE - MAX_SKB_FRAGS - 2);
  199. }
  200. static void xennet_maybe_wake_tx(struct netfront_queue *queue)
  201. {
  202. struct net_device *dev = queue->info->netdev;
  203. struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
  204. if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
  205. netfront_tx_slot_available(queue) &&
  206. likely(netif_running(dev)))
  207. netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
  208. }
  209. static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
  210. {
  211. struct sk_buff *skb;
  212. struct page *page;
  213. skb = __netdev_alloc_skb(queue->info->netdev,
  214. RX_COPY_THRESHOLD + NET_IP_ALIGN,
  215. GFP_ATOMIC | __GFP_NOWARN);
  216. if (unlikely(!skb))
  217. return NULL;
  218. page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
  219. if (!page) {
  220. kfree_skb(skb);
  221. return NULL;
  222. }
  223. skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
  224. /* Align ip header to a 16 bytes boundary */
  225. skb_reserve(skb, NET_IP_ALIGN);
  226. skb->dev = queue->info->netdev;
  227. return skb;
  228. }
  229. static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
  230. {
  231. RING_IDX req_prod = queue->rx.req_prod_pvt;
  232. int notify;
  233. int err = 0;
  234. if (unlikely(!netif_carrier_ok(queue->info->netdev)))
  235. return;
  236. for (req_prod = queue->rx.req_prod_pvt;
  237. req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
  238. req_prod++) {
  239. struct sk_buff *skb;
  240. unsigned short id;
  241. grant_ref_t ref;
  242. struct page *page;
  243. struct xen_netif_rx_request *req;
  244. skb = xennet_alloc_one_rx_buffer(queue);
  245. if (!skb) {
  246. err = -ENOMEM;
  247. break;
  248. }
  249. id = xennet_rxidx(req_prod);
  250. BUG_ON(queue->rx_skbs[id]);
  251. queue->rx_skbs[id] = skb;
  252. ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
  253. WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
  254. queue->grant_rx_ref[id] = ref;
  255. page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
  256. req = RING_GET_REQUEST(&queue->rx, req_prod);
  257. gnttab_page_grant_foreign_access_ref_one(ref,
  258. queue->info->xbdev->otherend_id,
  259. page,
  260. 0);
  261. req->id = id;
  262. req->gref = ref;
  263. }
  264. queue->rx.req_prod_pvt = req_prod;
  265. /* Try again later if there are not enough requests or skb allocation
  266. * failed.
  267. * Enough requests is quantified as the sum of newly created slots and
  268. * the unconsumed slots at the backend.
  269. */
  270. if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
  271. unlikely(err)) {
  272. mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
  273. return;
  274. }
  275. wmb(); /* barrier so backend seens requests */
  276. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
  277. if (notify)
  278. notify_remote_via_irq(queue->rx_irq);
  279. }
  280. static int xennet_open(struct net_device *dev)
  281. {
  282. struct netfront_info *np = netdev_priv(dev);
  283. unsigned int num_queues = dev->real_num_tx_queues;
  284. unsigned int i = 0;
  285. struct netfront_queue *queue = NULL;
  286. 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 = frag->page_offset;
  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. void *accel_priv, select_queue_fallback_t fallback)
  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 int 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,
  553. skb_frag_page(frag), frag->page_offset,
  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 = MAX_SKB_FRAGS + (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 RING_IDX xennet_fill_frags(struct netfront_queue *queue,
  728. struct sk_buff *skb,
  729. struct sk_buff_head *list)
  730. {
  731. struct skb_shared_info *shinfo = skb_shinfo(skb);
  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 (shinfo->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(shinfo->nr_frags >= MAX_SKB_FRAGS);
  744. skb_add_rx_frag(skb, shinfo->nr_frags, skb_frag_page(nfrag),
  745. rx->offset, rx->status, PAGE_SIZE);
  746. skb_shinfo(nskb)->nr_frags = 0;
  747. kfree_skb(nskb);
  748. }
  749. return cons;
  750. }
  751. static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
  752. {
  753. bool recalculate_partial_csum = false;
  754. /*
  755. * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  756. * peers can fail to set NETRXF_csum_blank when sending a GSO
  757. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  758. * recalculate the partial checksum.
  759. */
  760. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  761. struct netfront_info *np = netdev_priv(dev);
  762. atomic_inc(&np->rx_gso_checksum_fixup);
  763. skb->ip_summed = CHECKSUM_PARTIAL;
  764. recalculate_partial_csum = true;
  765. }
  766. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  767. if (skb->ip_summed != CHECKSUM_PARTIAL)
  768. return 0;
  769. return skb_checksum_setup(skb, recalculate_partial_csum);
  770. }
  771. static int handle_incoming_queue(struct netfront_queue *queue,
  772. struct sk_buff_head *rxq)
  773. {
  774. struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
  775. int packets_dropped = 0;
  776. struct sk_buff *skb;
  777. while ((skb = __skb_dequeue(rxq)) != NULL) {
  778. int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  779. if (pull_to > skb_headlen(skb))
  780. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  781. /* Ethernet work: Delayed to here as it peeks the header. */
  782. skb->protocol = eth_type_trans(skb, queue->info->netdev);
  783. skb_reset_network_header(skb);
  784. if (checksum_setup(queue->info->netdev, skb)) {
  785. kfree_skb(skb);
  786. packets_dropped++;
  787. queue->info->netdev->stats.rx_errors++;
  788. continue;
  789. }
  790. u64_stats_update_begin(&rx_stats->syncp);
  791. rx_stats->packets++;
  792. rx_stats->bytes += skb->len;
  793. u64_stats_update_end(&rx_stats->syncp);
  794. /* Pass it up. */
  795. napi_gro_receive(&queue->napi, skb);
  796. }
  797. return packets_dropped;
  798. }
  799. static int xennet_poll(struct napi_struct *napi, int budget)
  800. {
  801. struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
  802. struct net_device *dev = queue->info->netdev;
  803. struct sk_buff *skb;
  804. struct netfront_rx_info rinfo;
  805. struct xen_netif_rx_response *rx = &rinfo.rx;
  806. struct xen_netif_extra_info *extras = rinfo.extras;
  807. RING_IDX i, rp;
  808. int work_done;
  809. struct sk_buff_head rxq;
  810. struct sk_buff_head errq;
  811. struct sk_buff_head tmpq;
  812. int err;
  813. spin_lock(&queue->rx_lock);
  814. skb_queue_head_init(&rxq);
  815. skb_queue_head_init(&errq);
  816. skb_queue_head_init(&tmpq);
  817. rp = queue->rx.sring->rsp_prod;
  818. rmb(); /* Ensure we see queued responses up to 'rp'. */
  819. i = queue->rx.rsp_cons;
  820. work_done = 0;
  821. while ((i != rp) && (work_done < budget)) {
  822. memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
  823. memset(extras, 0, sizeof(rinfo.extras));
  824. err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
  825. if (unlikely(err)) {
  826. err:
  827. while ((skb = __skb_dequeue(&tmpq)))
  828. __skb_queue_tail(&errq, skb);
  829. dev->stats.rx_errors++;
  830. i = queue->rx.rsp_cons;
  831. continue;
  832. }
  833. skb = __skb_dequeue(&tmpq);
  834. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  835. struct xen_netif_extra_info *gso;
  836. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  837. if (unlikely(xennet_set_skb_gso(skb, gso))) {
  838. __skb_queue_head(&tmpq, skb);
  839. queue->rx.rsp_cons += skb_queue_len(&tmpq);
  840. goto err;
  841. }
  842. }
  843. NETFRONT_SKB_CB(skb)->pull_to = rx->status;
  844. if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
  845. NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
  846. skb_shinfo(skb)->frags[0].page_offset = rx->offset;
  847. skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
  848. skb->data_len = rx->status;
  849. skb->len += rx->status;
  850. i = xennet_fill_frags(queue, skb, &tmpq);
  851. if (rx->flags & XEN_NETRXF_csum_blank)
  852. skb->ip_summed = CHECKSUM_PARTIAL;
  853. else if (rx->flags & XEN_NETRXF_data_validated)
  854. skb->ip_summed = CHECKSUM_UNNECESSARY;
  855. __skb_queue_tail(&rxq, skb);
  856. queue->rx.rsp_cons = ++i;
  857. work_done++;
  858. }
  859. __skb_queue_purge(&errq);
  860. work_done -= handle_incoming_queue(queue, &rxq);
  861. xennet_alloc_rx_buffers(queue);
  862. if (work_done < budget) {
  863. int more_to_do = 0;
  864. napi_complete_done(napi, work_done);
  865. RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
  866. if (more_to_do)
  867. napi_schedule(napi);
  868. }
  869. spin_unlock(&queue->rx_lock);
  870. return work_done;
  871. }
  872. static int xennet_change_mtu(struct net_device *dev, int mtu)
  873. {
  874. int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
  875. if (mtu > max)
  876. return -EINVAL;
  877. dev->mtu = mtu;
  878. return 0;
  879. }
  880. static void xennet_get_stats64(struct net_device *dev,
  881. struct rtnl_link_stats64 *tot)
  882. {
  883. struct netfront_info *np = netdev_priv(dev);
  884. int cpu;
  885. for_each_possible_cpu(cpu) {
  886. struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
  887. struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
  888. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  889. unsigned int start;
  890. do {
  891. start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
  892. tx_packets = tx_stats->packets;
  893. tx_bytes = tx_stats->bytes;
  894. } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
  895. do {
  896. start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
  897. rx_packets = rx_stats->packets;
  898. rx_bytes = rx_stats->bytes;
  899. } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
  900. tot->rx_packets += rx_packets;
  901. tot->tx_packets += tx_packets;
  902. tot->rx_bytes += rx_bytes;
  903. tot->tx_bytes += tx_bytes;
  904. }
  905. tot->rx_errors = dev->stats.rx_errors;
  906. tot->tx_dropped = dev->stats.tx_dropped;
  907. }
  908. static void xennet_release_tx_bufs(struct netfront_queue *queue)
  909. {
  910. struct sk_buff *skb;
  911. int i;
  912. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  913. /* Skip over entries which are actually freelist references */
  914. if (skb_entry_is_link(&queue->tx_skbs[i]))
  915. continue;
  916. skb = queue->tx_skbs[i].skb;
  917. get_page(queue->grant_tx_page[i]);
  918. gnttab_end_foreign_access(queue->grant_tx_ref[i],
  919. GNTMAP_readonly,
  920. (unsigned long)page_address(queue->grant_tx_page[i]));
  921. queue->grant_tx_page[i] = NULL;
  922. queue->grant_tx_ref[i] = GRANT_INVALID_REF;
  923. add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, i);
  924. dev_kfree_skb_irq(skb);
  925. }
  926. }
  927. static void xennet_release_rx_bufs(struct netfront_queue *queue)
  928. {
  929. int id, ref;
  930. spin_lock_bh(&queue->rx_lock);
  931. for (id = 0; id < NET_RX_RING_SIZE; id++) {
  932. struct sk_buff *skb;
  933. struct page *page;
  934. skb = queue->rx_skbs[id];
  935. if (!skb)
  936. continue;
  937. ref = queue->grant_rx_ref[id];
  938. if (ref == GRANT_INVALID_REF)
  939. continue;
  940. page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
  941. /* gnttab_end_foreign_access() needs a page ref until
  942. * foreign access is ended (which may be deferred).
  943. */
  944. get_page(page);
  945. gnttab_end_foreign_access(ref, 0,
  946. (unsigned long)page_address(page));
  947. queue->grant_rx_ref[id] = GRANT_INVALID_REF;
  948. kfree_skb(skb);
  949. }
  950. spin_unlock_bh(&queue->rx_lock);
  951. }
  952. static netdev_features_t xennet_fix_features(struct net_device *dev,
  953. netdev_features_t features)
  954. {
  955. struct netfront_info *np = netdev_priv(dev);
  956. if (features & NETIF_F_SG &&
  957. !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
  958. features &= ~NETIF_F_SG;
  959. if (features & NETIF_F_IPV6_CSUM &&
  960. !xenbus_read_unsigned(np->xbdev->otherend,
  961. "feature-ipv6-csum-offload", 0))
  962. features &= ~NETIF_F_IPV6_CSUM;
  963. if (features & NETIF_F_TSO &&
  964. !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
  965. features &= ~NETIF_F_TSO;
  966. if (features & NETIF_F_TSO6 &&
  967. !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
  968. features &= ~NETIF_F_TSO6;
  969. return features;
  970. }
  971. static int xennet_set_features(struct net_device *dev,
  972. netdev_features_t features)
  973. {
  974. if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
  975. netdev_info(dev, "Reducing MTU because no SG offload");
  976. dev->mtu = ETH_DATA_LEN;
  977. }
  978. return 0;
  979. }
  980. static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
  981. {
  982. struct netfront_queue *queue = dev_id;
  983. unsigned long flags;
  984. spin_lock_irqsave(&queue->tx_lock, flags);
  985. xennet_tx_buf_gc(queue);
  986. spin_unlock_irqrestore(&queue->tx_lock, flags);
  987. return IRQ_HANDLED;
  988. }
  989. static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
  990. {
  991. struct netfront_queue *queue = dev_id;
  992. struct net_device *dev = queue->info->netdev;
  993. if (likely(netif_carrier_ok(dev) &&
  994. RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
  995. napi_schedule(&queue->napi);
  996. return IRQ_HANDLED;
  997. }
  998. static irqreturn_t xennet_interrupt(int irq, void *dev_id)
  999. {
  1000. xennet_tx_interrupt(irq, dev_id);
  1001. xennet_rx_interrupt(irq, dev_id);
  1002. return IRQ_HANDLED;
  1003. }
  1004. #ifdef CONFIG_NET_POLL_CONTROLLER
  1005. static void xennet_poll_controller(struct net_device *dev)
  1006. {
  1007. /* Poll each queue */
  1008. struct netfront_info *info = netdev_priv(dev);
  1009. unsigned int num_queues = dev->real_num_tx_queues;
  1010. unsigned int i;
  1011. for (i = 0; i < num_queues; ++i)
  1012. xennet_interrupt(0, &info->queues[i]);
  1013. }
  1014. #endif
  1015. static const struct net_device_ops xennet_netdev_ops = {
  1016. .ndo_open = xennet_open,
  1017. .ndo_stop = xennet_close,
  1018. .ndo_start_xmit = xennet_start_xmit,
  1019. .ndo_change_mtu = xennet_change_mtu,
  1020. .ndo_get_stats64 = xennet_get_stats64,
  1021. .ndo_set_mac_address = eth_mac_addr,
  1022. .ndo_validate_addr = eth_validate_addr,
  1023. .ndo_fix_features = xennet_fix_features,
  1024. .ndo_set_features = xennet_set_features,
  1025. .ndo_select_queue = xennet_select_queue,
  1026. #ifdef CONFIG_NET_POLL_CONTROLLER
  1027. .ndo_poll_controller = xennet_poll_controller,
  1028. #endif
  1029. };
  1030. static void xennet_free_netdev(struct net_device *netdev)
  1031. {
  1032. struct netfront_info *np = netdev_priv(netdev);
  1033. free_percpu(np->rx_stats);
  1034. free_percpu(np->tx_stats);
  1035. free_netdev(netdev);
  1036. }
  1037. static struct net_device *xennet_create_dev(struct xenbus_device *dev)
  1038. {
  1039. int err;
  1040. struct net_device *netdev;
  1041. struct netfront_info *np;
  1042. netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
  1043. if (!netdev)
  1044. return ERR_PTR(-ENOMEM);
  1045. np = netdev_priv(netdev);
  1046. np->xbdev = dev;
  1047. np->queues = NULL;
  1048. err = -ENOMEM;
  1049. np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
  1050. if (np->rx_stats == NULL)
  1051. goto exit;
  1052. np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
  1053. if (np->tx_stats == NULL)
  1054. goto exit;
  1055. netdev->netdev_ops = &xennet_netdev_ops;
  1056. netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  1057. NETIF_F_GSO_ROBUST;
  1058. netdev->hw_features = NETIF_F_SG |
  1059. NETIF_F_IPV6_CSUM |
  1060. NETIF_F_TSO | NETIF_F_TSO6;
  1061. /*
  1062. * Assume that all hw features are available for now. This set
  1063. * will be adjusted by the call to netdev_update_features() in
  1064. * xennet_connect() which is the earliest point where we can
  1065. * negotiate with the backend regarding supported features.
  1066. */
  1067. netdev->features |= netdev->hw_features;
  1068. netdev->ethtool_ops = &xennet_ethtool_ops;
  1069. netdev->min_mtu = ETH_MIN_MTU;
  1070. netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
  1071. SET_NETDEV_DEV(netdev, &dev->dev);
  1072. np->netdev = netdev;
  1073. netif_carrier_off(netdev);
  1074. xenbus_switch_state(dev, XenbusStateInitialising);
  1075. return netdev;
  1076. exit:
  1077. xennet_free_netdev(netdev);
  1078. return ERR_PTR(err);
  1079. }
  1080. /**
  1081. * Entry point to this code when a new device is created. Allocate the basic
  1082. * structures and the ring buffers for communication with the backend, and
  1083. * inform the backend of the appropriate details for those.
  1084. */
  1085. static int netfront_probe(struct xenbus_device *dev,
  1086. const struct xenbus_device_id *id)
  1087. {
  1088. int err;
  1089. struct net_device *netdev;
  1090. struct netfront_info *info;
  1091. netdev = xennet_create_dev(dev);
  1092. if (IS_ERR(netdev)) {
  1093. err = PTR_ERR(netdev);
  1094. xenbus_dev_fatal(dev, err, "creating netdev");
  1095. return err;
  1096. }
  1097. info = netdev_priv(netdev);
  1098. dev_set_drvdata(&dev->dev, info);
  1099. #ifdef CONFIG_SYSFS
  1100. info->netdev->sysfs_groups[0] = &xennet_dev_group;
  1101. #endif
  1102. return 0;
  1103. }
  1104. static void xennet_end_access(int ref, void *page)
  1105. {
  1106. /* This frees the page as a side-effect */
  1107. if (ref != GRANT_INVALID_REF)
  1108. gnttab_end_foreign_access(ref, 0, (unsigned long)page);
  1109. }
  1110. static void xennet_disconnect_backend(struct netfront_info *info)
  1111. {
  1112. unsigned int i = 0;
  1113. unsigned int num_queues = info->netdev->real_num_tx_queues;
  1114. netif_carrier_off(info->netdev);
  1115. for (i = 0; i < num_queues && info->queues; ++i) {
  1116. struct netfront_queue *queue = &info->queues[i];
  1117. del_timer_sync(&queue->rx_refill_timer);
  1118. if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
  1119. unbind_from_irqhandler(queue->tx_irq, queue);
  1120. if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
  1121. unbind_from_irqhandler(queue->tx_irq, queue);
  1122. unbind_from_irqhandler(queue->rx_irq, queue);
  1123. }
  1124. queue->tx_evtchn = queue->rx_evtchn = 0;
  1125. queue->tx_irq = queue->rx_irq = 0;
  1126. if (netif_running(info->netdev))
  1127. napi_synchronize(&queue->napi);
  1128. xennet_release_tx_bufs(queue);
  1129. xennet_release_rx_bufs(queue);
  1130. gnttab_free_grant_references(queue->gref_tx_head);
  1131. gnttab_free_grant_references(queue->gref_rx_head);
  1132. /* End access and free the pages */
  1133. xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
  1134. xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
  1135. queue->tx_ring_ref = GRANT_INVALID_REF;
  1136. queue->rx_ring_ref = GRANT_INVALID_REF;
  1137. queue->tx.sring = NULL;
  1138. queue->rx.sring = NULL;
  1139. }
  1140. }
  1141. /**
  1142. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1143. * driver restart. We tear down our netif structure and recreate it, but
  1144. * leave the device-layer structures intact so that this is transparent to the
  1145. * rest of the kernel.
  1146. */
  1147. static int netfront_resume(struct xenbus_device *dev)
  1148. {
  1149. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1150. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1151. xennet_disconnect_backend(info);
  1152. return 0;
  1153. }
  1154. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  1155. {
  1156. char *s, *e, *macstr;
  1157. int i;
  1158. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  1159. if (IS_ERR(macstr))
  1160. return PTR_ERR(macstr);
  1161. for (i = 0; i < ETH_ALEN; i++) {
  1162. mac[i] = simple_strtoul(s, &e, 16);
  1163. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  1164. kfree(macstr);
  1165. return -ENOENT;
  1166. }
  1167. s = e+1;
  1168. }
  1169. kfree(macstr);
  1170. return 0;
  1171. }
  1172. static int setup_netfront_single(struct netfront_queue *queue)
  1173. {
  1174. int err;
  1175. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1176. if (err < 0)
  1177. goto fail;
  1178. err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
  1179. xennet_interrupt,
  1180. 0, queue->info->netdev->name, queue);
  1181. if (err < 0)
  1182. goto bind_fail;
  1183. queue->rx_evtchn = queue->tx_evtchn;
  1184. queue->rx_irq = queue->tx_irq = err;
  1185. return 0;
  1186. bind_fail:
  1187. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1188. queue->tx_evtchn = 0;
  1189. fail:
  1190. return err;
  1191. }
  1192. static int setup_netfront_split(struct netfront_queue *queue)
  1193. {
  1194. int err;
  1195. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1196. if (err < 0)
  1197. goto fail;
  1198. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
  1199. if (err < 0)
  1200. goto alloc_rx_evtchn_fail;
  1201. snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
  1202. "%s-tx", queue->name);
  1203. err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
  1204. xennet_tx_interrupt,
  1205. 0, queue->tx_irq_name, queue);
  1206. if (err < 0)
  1207. goto bind_tx_fail;
  1208. queue->tx_irq = err;
  1209. snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
  1210. "%s-rx", queue->name);
  1211. err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
  1212. xennet_rx_interrupt,
  1213. 0, queue->rx_irq_name, queue);
  1214. if (err < 0)
  1215. goto bind_rx_fail;
  1216. queue->rx_irq = err;
  1217. return 0;
  1218. bind_rx_fail:
  1219. unbind_from_irqhandler(queue->tx_irq, queue);
  1220. queue->tx_irq = 0;
  1221. bind_tx_fail:
  1222. xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
  1223. queue->rx_evtchn = 0;
  1224. alloc_rx_evtchn_fail:
  1225. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1226. queue->tx_evtchn = 0;
  1227. fail:
  1228. return err;
  1229. }
  1230. static int setup_netfront(struct xenbus_device *dev,
  1231. struct netfront_queue *queue, unsigned int feature_split_evtchn)
  1232. {
  1233. struct xen_netif_tx_sring *txs;
  1234. struct xen_netif_rx_sring *rxs;
  1235. grant_ref_t gref;
  1236. int err;
  1237. queue->tx_ring_ref = GRANT_INVALID_REF;
  1238. queue->rx_ring_ref = GRANT_INVALID_REF;
  1239. queue->rx.sring = NULL;
  1240. queue->tx.sring = NULL;
  1241. txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1242. if (!txs) {
  1243. err = -ENOMEM;
  1244. xenbus_dev_fatal(dev, err, "allocating tx ring page");
  1245. goto fail;
  1246. }
  1247. SHARED_RING_INIT(txs);
  1248. FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
  1249. err = xenbus_grant_ring(dev, txs, 1, &gref);
  1250. if (err < 0)
  1251. goto grant_tx_ring_fail;
  1252. queue->tx_ring_ref = gref;
  1253. rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1254. if (!rxs) {
  1255. err = -ENOMEM;
  1256. xenbus_dev_fatal(dev, err, "allocating rx ring page");
  1257. goto alloc_rx_ring_fail;
  1258. }
  1259. SHARED_RING_INIT(rxs);
  1260. FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
  1261. err = xenbus_grant_ring(dev, rxs, 1, &gref);
  1262. if (err < 0)
  1263. goto grant_rx_ring_fail;
  1264. queue->rx_ring_ref = gref;
  1265. if (feature_split_evtchn)
  1266. err = setup_netfront_split(queue);
  1267. /* setup single event channel if
  1268. * a) feature-split-event-channels == 0
  1269. * b) feature-split-event-channels == 1 but failed to setup
  1270. */
  1271. if (!feature_split_evtchn || (feature_split_evtchn && err))
  1272. err = setup_netfront_single(queue);
  1273. if (err)
  1274. goto alloc_evtchn_fail;
  1275. return 0;
  1276. /* If we fail to setup netfront, it is safe to just revoke access to
  1277. * granted pages because backend is not accessing it at this point.
  1278. */
  1279. alloc_evtchn_fail:
  1280. gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
  1281. grant_rx_ring_fail:
  1282. free_page((unsigned long)rxs);
  1283. alloc_rx_ring_fail:
  1284. gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
  1285. grant_tx_ring_fail:
  1286. free_page((unsigned long)txs);
  1287. fail:
  1288. return err;
  1289. }
  1290. /* Queue-specific initialisation
  1291. * This used to be done in xennet_create_dev() but must now
  1292. * be run per-queue.
  1293. */
  1294. static int xennet_init_queue(struct netfront_queue *queue)
  1295. {
  1296. unsigned short i;
  1297. int err = 0;
  1298. spin_lock_init(&queue->tx_lock);
  1299. spin_lock_init(&queue->rx_lock);
  1300. timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
  1301. snprintf(queue->name, sizeof(queue->name), "%s-q%u",
  1302. queue->info->netdev->name, queue->id);
  1303. /* Initialise tx_skbs as a free chain containing every entry. */
  1304. queue->tx_skb_freelist = 0;
  1305. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  1306. skb_entry_set_link(&queue->tx_skbs[i], i+1);
  1307. queue->grant_tx_ref[i] = GRANT_INVALID_REF;
  1308. queue->grant_tx_page[i] = NULL;
  1309. }
  1310. /* Clear out rx_skbs */
  1311. for (i = 0; i < NET_RX_RING_SIZE; i++) {
  1312. queue->rx_skbs[i] = NULL;
  1313. queue->grant_rx_ref[i] = GRANT_INVALID_REF;
  1314. }
  1315. /* A grant for every tx ring slot */
  1316. if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
  1317. &queue->gref_tx_head) < 0) {
  1318. pr_alert("can't alloc tx grant refs\n");
  1319. err = -ENOMEM;
  1320. goto exit;
  1321. }
  1322. /* A grant for every rx ring slot */
  1323. if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
  1324. &queue->gref_rx_head) < 0) {
  1325. pr_alert("can't alloc rx grant refs\n");
  1326. err = -ENOMEM;
  1327. goto exit_free_tx;
  1328. }
  1329. return 0;
  1330. exit_free_tx:
  1331. gnttab_free_grant_references(queue->gref_tx_head);
  1332. exit:
  1333. return err;
  1334. }
  1335. static int write_queue_xenstore_keys(struct netfront_queue *queue,
  1336. struct xenbus_transaction *xbt, int write_hierarchical)
  1337. {
  1338. /* Write the queue-specific keys into XenStore in the traditional
  1339. * way for a single queue, or in a queue subkeys for multiple
  1340. * queues.
  1341. */
  1342. struct xenbus_device *dev = queue->info->xbdev;
  1343. int err;
  1344. const char *message;
  1345. char *path;
  1346. size_t pathsize;
  1347. /* Choose the correct place to write the keys */
  1348. if (write_hierarchical) {
  1349. pathsize = strlen(dev->nodename) + 10;
  1350. path = kzalloc(pathsize, GFP_KERNEL);
  1351. if (!path) {
  1352. err = -ENOMEM;
  1353. message = "out of memory while writing ring references";
  1354. goto error;
  1355. }
  1356. snprintf(path, pathsize, "%s/queue-%u",
  1357. dev->nodename, queue->id);
  1358. } else {
  1359. path = (char *)dev->nodename;
  1360. }
  1361. /* Write ring references */
  1362. err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
  1363. queue->tx_ring_ref);
  1364. if (err) {
  1365. message = "writing tx-ring-ref";
  1366. goto error;
  1367. }
  1368. err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
  1369. queue->rx_ring_ref);
  1370. if (err) {
  1371. message = "writing rx-ring-ref";
  1372. goto error;
  1373. }
  1374. /* Write event channels; taking into account both shared
  1375. * and split event channel scenarios.
  1376. */
  1377. if (queue->tx_evtchn == queue->rx_evtchn) {
  1378. /* Shared event channel */
  1379. err = xenbus_printf(*xbt, path,
  1380. "event-channel", "%u", queue->tx_evtchn);
  1381. if (err) {
  1382. message = "writing event-channel";
  1383. goto error;
  1384. }
  1385. } else {
  1386. /* Split event channels */
  1387. err = xenbus_printf(*xbt, path,
  1388. "event-channel-tx", "%u", queue->tx_evtchn);
  1389. if (err) {
  1390. message = "writing event-channel-tx";
  1391. goto error;
  1392. }
  1393. err = xenbus_printf(*xbt, path,
  1394. "event-channel-rx", "%u", queue->rx_evtchn);
  1395. if (err) {
  1396. message = "writing event-channel-rx";
  1397. goto error;
  1398. }
  1399. }
  1400. if (write_hierarchical)
  1401. kfree(path);
  1402. return 0;
  1403. error:
  1404. if (write_hierarchical)
  1405. kfree(path);
  1406. xenbus_dev_fatal(dev, err, "%s", message);
  1407. return err;
  1408. }
  1409. static void xennet_destroy_queues(struct netfront_info *info)
  1410. {
  1411. unsigned int i;
  1412. for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
  1413. struct netfront_queue *queue = &info->queues[i];
  1414. if (netif_running(info->netdev))
  1415. napi_disable(&queue->napi);
  1416. netif_napi_del(&queue->napi);
  1417. }
  1418. kfree(info->queues);
  1419. info->queues = NULL;
  1420. }
  1421. static int xennet_create_queues(struct netfront_info *info,
  1422. unsigned int *num_queues)
  1423. {
  1424. unsigned int i;
  1425. int ret;
  1426. info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
  1427. GFP_KERNEL);
  1428. if (!info->queues)
  1429. return -ENOMEM;
  1430. for (i = 0; i < *num_queues; i++) {
  1431. struct netfront_queue *queue = &info->queues[i];
  1432. queue->id = i;
  1433. queue->info = info;
  1434. ret = xennet_init_queue(queue);
  1435. if (ret < 0) {
  1436. dev_warn(&info->xbdev->dev,
  1437. "only created %d queues\n", i);
  1438. *num_queues = i;
  1439. break;
  1440. }
  1441. netif_napi_add(queue->info->netdev, &queue->napi,
  1442. xennet_poll, 64);
  1443. if (netif_running(info->netdev))
  1444. napi_enable(&queue->napi);
  1445. }
  1446. netif_set_real_num_tx_queues(info->netdev, *num_queues);
  1447. if (*num_queues == 0) {
  1448. dev_err(&info->xbdev->dev, "no queues\n");
  1449. return -EINVAL;
  1450. }
  1451. return 0;
  1452. }
  1453. /* Common code used when first setting up, and when resuming. */
  1454. static int talk_to_netback(struct xenbus_device *dev,
  1455. struct netfront_info *info)
  1456. {
  1457. const char *message;
  1458. struct xenbus_transaction xbt;
  1459. int err;
  1460. unsigned int feature_split_evtchn;
  1461. unsigned int i = 0;
  1462. unsigned int max_queues = 0;
  1463. struct netfront_queue *queue = NULL;
  1464. unsigned int num_queues = 1;
  1465. info->netdev->irq = 0;
  1466. /* Check if backend supports multiple queues */
  1467. max_queues = xenbus_read_unsigned(info->xbdev->otherend,
  1468. "multi-queue-max-queues", 1);
  1469. num_queues = min(max_queues, xennet_max_queues);
  1470. /* Check feature-split-event-channels */
  1471. feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
  1472. "feature-split-event-channels", 0);
  1473. /* Read mac addr. */
  1474. err = xen_net_read_mac(dev, info->netdev->dev_addr);
  1475. if (err) {
  1476. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  1477. goto out;
  1478. }
  1479. rtnl_lock();
  1480. if (info->queues)
  1481. xennet_destroy_queues(info);
  1482. err = xennet_create_queues(info, &num_queues);
  1483. if (err < 0) {
  1484. xenbus_dev_fatal(dev, err, "creating queues");
  1485. kfree(info->queues);
  1486. info->queues = NULL;
  1487. goto out;
  1488. }
  1489. rtnl_unlock();
  1490. /* Create shared ring, alloc event channel -- for each queue */
  1491. for (i = 0; i < num_queues; ++i) {
  1492. queue = &info->queues[i];
  1493. err = setup_netfront(dev, queue, feature_split_evtchn);
  1494. if (err)
  1495. goto destroy_ring;
  1496. }
  1497. again:
  1498. err = xenbus_transaction_start(&xbt);
  1499. if (err) {
  1500. xenbus_dev_fatal(dev, err, "starting transaction");
  1501. goto destroy_ring;
  1502. }
  1503. if (xenbus_exists(XBT_NIL,
  1504. info->xbdev->otherend, "multi-queue-max-queues")) {
  1505. /* Write the number of queues */
  1506. err = xenbus_printf(xbt, dev->nodename,
  1507. "multi-queue-num-queues", "%u", num_queues);
  1508. if (err) {
  1509. message = "writing multi-queue-num-queues";
  1510. goto abort_transaction_no_dev_fatal;
  1511. }
  1512. }
  1513. if (num_queues == 1) {
  1514. err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
  1515. if (err)
  1516. goto abort_transaction_no_dev_fatal;
  1517. } else {
  1518. /* Write the keys for each queue */
  1519. for (i = 0; i < num_queues; ++i) {
  1520. queue = &info->queues[i];
  1521. err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
  1522. if (err)
  1523. goto abort_transaction_no_dev_fatal;
  1524. }
  1525. }
  1526. /* The remaining keys are not queue-specific */
  1527. err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
  1528. 1);
  1529. if (err) {
  1530. message = "writing request-rx-copy";
  1531. goto abort_transaction;
  1532. }
  1533. err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
  1534. if (err) {
  1535. message = "writing feature-rx-notify";
  1536. goto abort_transaction;
  1537. }
  1538. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
  1539. if (err) {
  1540. message = "writing feature-sg";
  1541. goto abort_transaction;
  1542. }
  1543. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
  1544. if (err) {
  1545. message = "writing feature-gso-tcpv4";
  1546. goto abort_transaction;
  1547. }
  1548. err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
  1549. if (err) {
  1550. message = "writing feature-gso-tcpv6";
  1551. goto abort_transaction;
  1552. }
  1553. err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
  1554. "1");
  1555. if (err) {
  1556. message = "writing feature-ipv6-csum-offload";
  1557. goto abort_transaction;
  1558. }
  1559. err = xenbus_transaction_end(xbt, 0);
  1560. if (err) {
  1561. if (err == -EAGAIN)
  1562. goto again;
  1563. xenbus_dev_fatal(dev, err, "completing transaction");
  1564. goto destroy_ring;
  1565. }
  1566. return 0;
  1567. abort_transaction:
  1568. xenbus_dev_fatal(dev, err, "%s", message);
  1569. abort_transaction_no_dev_fatal:
  1570. xenbus_transaction_end(xbt, 1);
  1571. destroy_ring:
  1572. xennet_disconnect_backend(info);
  1573. rtnl_lock();
  1574. xennet_destroy_queues(info);
  1575. out:
  1576. rtnl_unlock();
  1577. device_unregister(&dev->dev);
  1578. return err;
  1579. }
  1580. static int xennet_connect(struct net_device *dev)
  1581. {
  1582. struct netfront_info *np = netdev_priv(dev);
  1583. unsigned int num_queues = 0;
  1584. int err;
  1585. unsigned int j = 0;
  1586. struct netfront_queue *queue = NULL;
  1587. if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
  1588. dev_info(&dev->dev,
  1589. "backend does not support copying receive path\n");
  1590. return -ENODEV;
  1591. }
  1592. err = talk_to_netback(np->xbdev, np);
  1593. if (err)
  1594. return err;
  1595. /* talk_to_netback() sets the correct number of queues */
  1596. num_queues = dev->real_num_tx_queues;
  1597. rtnl_lock();
  1598. netdev_update_features(dev);
  1599. rtnl_unlock();
  1600. if (dev->reg_state == NETREG_UNINITIALIZED) {
  1601. err = register_netdev(dev);
  1602. if (err) {
  1603. pr_warn("%s: register_netdev err=%d\n", __func__, err);
  1604. device_unregister(&np->xbdev->dev);
  1605. return err;
  1606. }
  1607. }
  1608. /*
  1609. * All public and private state should now be sane. Get
  1610. * ready to start sending and receiving packets and give the driver
  1611. * domain a kick because we've probably just requeued some
  1612. * packets.
  1613. */
  1614. netif_carrier_on(np->netdev);
  1615. for (j = 0; j < num_queues; ++j) {
  1616. queue = &np->queues[j];
  1617. notify_remote_via_irq(queue->tx_irq);
  1618. if (queue->tx_irq != queue->rx_irq)
  1619. notify_remote_via_irq(queue->rx_irq);
  1620. spin_lock_irq(&queue->tx_lock);
  1621. xennet_tx_buf_gc(queue);
  1622. spin_unlock_irq(&queue->tx_lock);
  1623. spin_lock_bh(&queue->rx_lock);
  1624. xennet_alloc_rx_buffers(queue);
  1625. spin_unlock_bh(&queue->rx_lock);
  1626. }
  1627. return 0;
  1628. }
  1629. /**
  1630. * Callback received when the backend's state changes.
  1631. */
  1632. static void netback_changed(struct xenbus_device *dev,
  1633. enum xenbus_state backend_state)
  1634. {
  1635. struct netfront_info *np = dev_get_drvdata(&dev->dev);
  1636. struct net_device *netdev = np->netdev;
  1637. dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
  1638. switch (backend_state) {
  1639. case XenbusStateInitialising:
  1640. case XenbusStateInitialised:
  1641. case XenbusStateReconfiguring:
  1642. case XenbusStateReconfigured:
  1643. case XenbusStateUnknown:
  1644. break;
  1645. case XenbusStateInitWait:
  1646. if (dev->state != XenbusStateInitialising)
  1647. break;
  1648. if (xennet_connect(netdev) != 0)
  1649. break;
  1650. xenbus_switch_state(dev, XenbusStateConnected);
  1651. break;
  1652. case XenbusStateConnected:
  1653. netdev_notify_peers(netdev);
  1654. break;
  1655. case XenbusStateClosed:
  1656. wake_up_all(&module_unload_q);
  1657. if (dev->state == XenbusStateClosed)
  1658. break;
  1659. /* Missed the backend's CLOSING state -- fallthrough */
  1660. case XenbusStateClosing:
  1661. wake_up_all(&module_unload_q);
  1662. xenbus_frontend_closed(dev);
  1663. break;
  1664. }
  1665. }
  1666. static const struct xennet_stat {
  1667. char name[ETH_GSTRING_LEN];
  1668. u16 offset;
  1669. } xennet_stats[] = {
  1670. {
  1671. "rx_gso_checksum_fixup",
  1672. offsetof(struct netfront_info, rx_gso_checksum_fixup)
  1673. },
  1674. };
  1675. static int xennet_get_sset_count(struct net_device *dev, int string_set)
  1676. {
  1677. switch (string_set) {
  1678. case ETH_SS_STATS:
  1679. return ARRAY_SIZE(xennet_stats);
  1680. default:
  1681. return -EINVAL;
  1682. }
  1683. }
  1684. static void xennet_get_ethtool_stats(struct net_device *dev,
  1685. struct ethtool_stats *stats, u64 * data)
  1686. {
  1687. void *np = netdev_priv(dev);
  1688. int i;
  1689. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1690. data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
  1691. }
  1692. static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  1693. {
  1694. int i;
  1695. switch (stringset) {
  1696. case ETH_SS_STATS:
  1697. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1698. memcpy(data + i * ETH_GSTRING_LEN,
  1699. xennet_stats[i].name, ETH_GSTRING_LEN);
  1700. break;
  1701. }
  1702. }
  1703. static const struct ethtool_ops xennet_ethtool_ops =
  1704. {
  1705. .get_link = ethtool_op_get_link,
  1706. .get_sset_count = xennet_get_sset_count,
  1707. .get_ethtool_stats = xennet_get_ethtool_stats,
  1708. .get_strings = xennet_get_strings,
  1709. };
  1710. #ifdef CONFIG_SYSFS
  1711. static ssize_t show_rxbuf(struct device *dev,
  1712. struct device_attribute *attr, char *buf)
  1713. {
  1714. return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
  1715. }
  1716. static ssize_t store_rxbuf(struct device *dev,
  1717. struct device_attribute *attr,
  1718. const char *buf, size_t len)
  1719. {
  1720. char *endp;
  1721. unsigned long target;
  1722. if (!capable(CAP_NET_ADMIN))
  1723. return -EPERM;
  1724. target = simple_strtoul(buf, &endp, 0);
  1725. if (endp == buf)
  1726. return -EBADMSG;
  1727. /* rxbuf_min and rxbuf_max are no longer configurable. */
  1728. return len;
  1729. }
  1730. static DEVICE_ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
  1731. static DEVICE_ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
  1732. static DEVICE_ATTR(rxbuf_cur, S_IRUGO, show_rxbuf, NULL);
  1733. static struct attribute *xennet_dev_attrs[] = {
  1734. &dev_attr_rxbuf_min.attr,
  1735. &dev_attr_rxbuf_max.attr,
  1736. &dev_attr_rxbuf_cur.attr,
  1737. NULL
  1738. };
  1739. static const struct attribute_group xennet_dev_group = {
  1740. .attrs = xennet_dev_attrs
  1741. };
  1742. #endif /* CONFIG_SYSFS */
  1743. static int xennet_remove(struct xenbus_device *dev)
  1744. {
  1745. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1746. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1747. if (xenbus_read_driver_state(dev->otherend) != XenbusStateClosed) {
  1748. xenbus_switch_state(dev, XenbusStateClosing);
  1749. wait_event(module_unload_q,
  1750. xenbus_read_driver_state(dev->otherend) ==
  1751. XenbusStateClosing);
  1752. xenbus_switch_state(dev, XenbusStateClosed);
  1753. wait_event(module_unload_q,
  1754. xenbus_read_driver_state(dev->otherend) ==
  1755. XenbusStateClosed ||
  1756. xenbus_read_driver_state(dev->otherend) ==
  1757. XenbusStateUnknown);
  1758. }
  1759. xennet_disconnect_backend(info);
  1760. if (info->netdev->reg_state == NETREG_REGISTERED)
  1761. unregister_netdev(info->netdev);
  1762. if (info->queues) {
  1763. rtnl_lock();
  1764. xennet_destroy_queues(info);
  1765. rtnl_unlock();
  1766. }
  1767. xennet_free_netdev(info->netdev);
  1768. return 0;
  1769. }
  1770. static const struct xenbus_device_id netfront_ids[] = {
  1771. { "vif" },
  1772. { "" }
  1773. };
  1774. static struct xenbus_driver netfront_driver = {
  1775. .ids = netfront_ids,
  1776. .probe = netfront_probe,
  1777. .remove = xennet_remove,
  1778. .resume = netfront_resume,
  1779. .otherend_changed = netback_changed,
  1780. };
  1781. static int __init netif_init(void)
  1782. {
  1783. if (!xen_domain())
  1784. return -ENODEV;
  1785. if (!xen_has_pv_nic_devices())
  1786. return -ENODEV;
  1787. pr_info("Initialising Xen virtual ethernet driver\n");
  1788. /* Allow as many queues as there are CPUs inut max. 8 if user has not
  1789. * specified a value.
  1790. */
  1791. if (xennet_max_queues == 0)
  1792. xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
  1793. num_online_cpus());
  1794. return xenbus_register_frontend(&netfront_driver);
  1795. }
  1796. module_init(netif_init);
  1797. static void __exit netif_exit(void)
  1798. {
  1799. xenbus_unregister_driver(&netfront_driver);
  1800. }
  1801. module_exit(netif_exit);
  1802. MODULE_DESCRIPTION("Xen virtual network device frontend");
  1803. MODULE_LICENSE("GPL");
  1804. MODULE_ALIAS("xen:vif");
  1805. MODULE_ALIAS("xennet");