xen-netfront.c 51 KB

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