xen-netfront.c 48 KB

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