xen-netfront.c 49 KB

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