xen-netfront.c 52 KB

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