xen-netfront.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  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/grant_table.h>
  49. #include <xen/interface/io/netif.h>
  50. #include <xen/interface/memory.h>
  51. #include <xen/interface/grant_table.h>
  52. static const struct ethtool_ops xennet_ethtool_ops;
  53. struct netfront_cb {
  54. struct page *page;
  55. unsigned offset;
  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_RX_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. frags += DIV_ROUND_UP(offset + len, PAGE_SIZE);
  411. if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
  412. printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
  413. frags);
  414. dump_stack();
  415. goto drop;
  416. }
  417. spin_lock_irq(&np->tx_lock);
  418. if (unlikely(!netif_carrier_ok(dev) ||
  419. (frags > 1 && !xennet_can_sg(dev)) ||
  420. netif_needs_gso(skb, netif_skb_features(skb)))) {
  421. spin_unlock_irq(&np->tx_lock);
  422. goto drop;
  423. }
  424. i = np->tx.req_prod_pvt;
  425. id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
  426. np->tx_skbs[id].skb = skb;
  427. tx = RING_GET_REQUEST(&np->tx, i);
  428. tx->id = id;
  429. ref = gnttab_claim_grant_reference(&np->gref_tx_head);
  430. BUG_ON((signed short)ref < 0);
  431. mfn = virt_to_mfn(data);
  432. gnttab_grant_foreign_access_ref(
  433. ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
  434. tx->gref = np->grant_tx_ref[id] = ref;
  435. tx->offset = offset;
  436. tx->size = len;
  437. extra = NULL;
  438. tx->flags = 0;
  439. if (skb->ip_summed == CHECKSUM_PARTIAL)
  440. /* local packet? */
  441. tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
  442. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  443. /* remote but checksummed. */
  444. tx->flags |= XEN_NETTXF_data_validated;
  445. if (skb_shinfo(skb)->gso_size) {
  446. struct xen_netif_extra_info *gso;
  447. gso = (struct xen_netif_extra_info *)
  448. RING_GET_REQUEST(&np->tx, ++i);
  449. if (extra)
  450. extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
  451. else
  452. tx->flags |= XEN_NETTXF_extra_info;
  453. gso->u.gso.size = skb_shinfo(skb)->gso_size;
  454. gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
  455. gso->u.gso.pad = 0;
  456. gso->u.gso.features = 0;
  457. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  458. gso->flags = 0;
  459. extra = gso;
  460. }
  461. np->tx.req_prod_pvt = i + 1;
  462. xennet_make_frags(skb, dev, tx);
  463. tx->size = skb->len;
  464. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
  465. if (notify)
  466. notify_remote_via_irq(np->netdev->irq);
  467. u64_stats_update_begin(&stats->syncp);
  468. stats->tx_bytes += skb->len;
  469. stats->tx_packets++;
  470. u64_stats_update_end(&stats->syncp);
  471. /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
  472. xennet_tx_buf_gc(dev);
  473. if (!netfront_tx_slot_available(np))
  474. netif_stop_queue(dev);
  475. spin_unlock_irq(&np->tx_lock);
  476. return NETDEV_TX_OK;
  477. drop:
  478. dev->stats.tx_dropped++;
  479. dev_kfree_skb(skb);
  480. return NETDEV_TX_OK;
  481. }
  482. static int xennet_close(struct net_device *dev)
  483. {
  484. struct netfront_info *np = netdev_priv(dev);
  485. netif_stop_queue(np->netdev);
  486. napi_disable(&np->napi);
  487. return 0;
  488. }
  489. static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
  490. grant_ref_t ref)
  491. {
  492. int new = xennet_rxidx(np->rx.req_prod_pvt);
  493. BUG_ON(np->rx_skbs[new]);
  494. np->rx_skbs[new] = skb;
  495. np->grant_rx_ref[new] = ref;
  496. RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
  497. RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
  498. np->rx.req_prod_pvt++;
  499. }
  500. static int xennet_get_extras(struct netfront_info *np,
  501. struct xen_netif_extra_info *extras,
  502. RING_IDX rp)
  503. {
  504. struct xen_netif_extra_info *extra;
  505. struct device *dev = &np->netdev->dev;
  506. RING_IDX cons = np->rx.rsp_cons;
  507. int err = 0;
  508. do {
  509. struct sk_buff *skb;
  510. grant_ref_t ref;
  511. if (unlikely(cons + 1 == rp)) {
  512. if (net_ratelimit())
  513. dev_warn(dev, "Missing extra info\n");
  514. err = -EBADR;
  515. break;
  516. }
  517. extra = (struct xen_netif_extra_info *)
  518. RING_GET_RESPONSE(&np->rx, ++cons);
  519. if (unlikely(!extra->type ||
  520. extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  521. if (net_ratelimit())
  522. dev_warn(dev, "Invalid extra type: %d\n",
  523. extra->type);
  524. err = -EINVAL;
  525. } else {
  526. memcpy(&extras[extra->type - 1], extra,
  527. sizeof(*extra));
  528. }
  529. skb = xennet_get_rx_skb(np, cons);
  530. ref = xennet_get_rx_ref(np, cons);
  531. xennet_move_rx_slot(np, skb, ref);
  532. } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
  533. np->rx.rsp_cons = cons;
  534. return err;
  535. }
  536. static int xennet_get_responses(struct netfront_info *np,
  537. struct netfront_rx_info *rinfo, RING_IDX rp,
  538. struct sk_buff_head *list)
  539. {
  540. struct xen_netif_rx_response *rx = &rinfo->rx;
  541. struct xen_netif_extra_info *extras = rinfo->extras;
  542. struct device *dev = &np->netdev->dev;
  543. RING_IDX cons = np->rx.rsp_cons;
  544. struct sk_buff *skb = xennet_get_rx_skb(np, cons);
  545. grant_ref_t ref = xennet_get_rx_ref(np, cons);
  546. int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
  547. int frags = 1;
  548. int err = 0;
  549. unsigned long ret;
  550. if (rx->flags & XEN_NETRXF_extra_info) {
  551. err = xennet_get_extras(np, extras, rp);
  552. cons = np->rx.rsp_cons;
  553. }
  554. for (;;) {
  555. if (unlikely(rx->status < 0 ||
  556. rx->offset + rx->status > PAGE_SIZE)) {
  557. if (net_ratelimit())
  558. dev_warn(dev, "rx->offset: %x, size: %u\n",
  559. rx->offset, rx->status);
  560. xennet_move_rx_slot(np, skb, ref);
  561. err = -EINVAL;
  562. goto next;
  563. }
  564. /*
  565. * This definitely indicates a bug, either in this driver or in
  566. * the backend driver. In future this should flag the bad
  567. * situation to the system controller to reboot the backed.
  568. */
  569. if (ref == GRANT_INVALID_REF) {
  570. if (net_ratelimit())
  571. dev_warn(dev, "Bad rx response id %d.\n",
  572. rx->id);
  573. err = -EINVAL;
  574. goto next;
  575. }
  576. ret = gnttab_end_foreign_access_ref(ref, 0);
  577. BUG_ON(!ret);
  578. gnttab_release_grant_reference(&np->gref_rx_head, ref);
  579. __skb_queue_tail(list, skb);
  580. next:
  581. if (!(rx->flags & XEN_NETRXF_more_data))
  582. break;
  583. if (cons + frags == rp) {
  584. if (net_ratelimit())
  585. dev_warn(dev, "Need more frags\n");
  586. err = -ENOENT;
  587. break;
  588. }
  589. rx = RING_GET_RESPONSE(&np->rx, cons + frags);
  590. skb = xennet_get_rx_skb(np, cons + frags);
  591. ref = xennet_get_rx_ref(np, cons + frags);
  592. frags++;
  593. }
  594. if (unlikely(frags > max)) {
  595. if (net_ratelimit())
  596. dev_warn(dev, "Too many frags\n");
  597. err = -E2BIG;
  598. }
  599. if (unlikely(err))
  600. np->rx.rsp_cons = cons + frags;
  601. return err;
  602. }
  603. static int xennet_set_skb_gso(struct sk_buff *skb,
  604. struct xen_netif_extra_info *gso)
  605. {
  606. if (!gso->u.gso.size) {
  607. if (net_ratelimit())
  608. printk(KERN_WARNING "GSO size must not be zero.\n");
  609. return -EINVAL;
  610. }
  611. /* Currently only TCPv4 S.O. is supported. */
  612. if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
  613. if (net_ratelimit())
  614. printk(KERN_WARNING "Bad GSO type %d.\n", gso->u.gso.type);
  615. return -EINVAL;
  616. }
  617. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  618. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  619. /* Header must be checked, and gso_segs computed. */
  620. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  621. skb_shinfo(skb)->gso_segs = 0;
  622. return 0;
  623. }
  624. static RING_IDX xennet_fill_frags(struct netfront_info *np,
  625. struct sk_buff *skb,
  626. struct sk_buff_head *list)
  627. {
  628. struct skb_shared_info *shinfo = skb_shinfo(skb);
  629. int nr_frags = shinfo->nr_frags;
  630. RING_IDX cons = np->rx.rsp_cons;
  631. struct sk_buff *nskb;
  632. while ((nskb = __skb_dequeue(list))) {
  633. struct xen_netif_rx_response *rx =
  634. RING_GET_RESPONSE(&np->rx, ++cons);
  635. skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
  636. __skb_fill_page_desc(skb, nr_frags,
  637. skb_frag_page(nfrag),
  638. rx->offset, rx->status);
  639. skb->data_len += rx->status;
  640. skb_shinfo(nskb)->nr_frags = 0;
  641. kfree_skb(nskb);
  642. nr_frags++;
  643. }
  644. shinfo->nr_frags = nr_frags;
  645. return cons;
  646. }
  647. static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
  648. {
  649. struct iphdr *iph;
  650. unsigned char *th;
  651. int err = -EPROTO;
  652. int recalculate_partial_csum = 0;
  653. /*
  654. * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  655. * peers can fail to set NETRXF_csum_blank when sending a GSO
  656. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  657. * recalculate the partial checksum.
  658. */
  659. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  660. struct netfront_info *np = netdev_priv(dev);
  661. np->rx_gso_checksum_fixup++;
  662. skb->ip_summed = CHECKSUM_PARTIAL;
  663. recalculate_partial_csum = 1;
  664. }
  665. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  666. if (skb->ip_summed != CHECKSUM_PARTIAL)
  667. return 0;
  668. if (skb->protocol != htons(ETH_P_IP))
  669. goto out;
  670. iph = (void *)skb->data;
  671. th = skb->data + 4 * iph->ihl;
  672. if (th >= skb_tail_pointer(skb))
  673. goto out;
  674. skb->csum_start = th - skb->head;
  675. switch (iph->protocol) {
  676. case IPPROTO_TCP:
  677. skb->csum_offset = offsetof(struct tcphdr, check);
  678. if (recalculate_partial_csum) {
  679. struct tcphdr *tcph = (struct tcphdr *)th;
  680. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  681. skb->len - iph->ihl*4,
  682. IPPROTO_TCP, 0);
  683. }
  684. break;
  685. case IPPROTO_UDP:
  686. skb->csum_offset = offsetof(struct udphdr, check);
  687. if (recalculate_partial_csum) {
  688. struct udphdr *udph = (struct udphdr *)th;
  689. udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  690. skb->len - iph->ihl*4,
  691. IPPROTO_UDP, 0);
  692. }
  693. break;
  694. default:
  695. if (net_ratelimit())
  696. printk(KERN_ERR "Attempting to checksum a non-"
  697. "TCP/UDP packet, dropping a protocol"
  698. " %d packet", iph->protocol);
  699. goto out;
  700. }
  701. if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
  702. goto out;
  703. err = 0;
  704. out:
  705. return err;
  706. }
  707. static int handle_incoming_queue(struct net_device *dev,
  708. struct sk_buff_head *rxq)
  709. {
  710. struct netfront_info *np = netdev_priv(dev);
  711. struct netfront_stats *stats = this_cpu_ptr(np->stats);
  712. int packets_dropped = 0;
  713. struct sk_buff *skb;
  714. while ((skb = __skb_dequeue(rxq)) != NULL) {
  715. struct page *page = NETFRONT_SKB_CB(skb)->page;
  716. void *vaddr = page_address(page);
  717. unsigned offset = NETFRONT_SKB_CB(skb)->offset;
  718. memcpy(skb->data, vaddr + offset,
  719. skb_headlen(skb));
  720. if (page != skb_frag_page(&skb_shinfo(skb)->frags[0]))
  721. __free_page(page);
  722. /* Ethernet work: Delayed to here as it peeks the header. */
  723. skb->protocol = eth_type_trans(skb, dev);
  724. if (checksum_setup(dev, skb)) {
  725. kfree_skb(skb);
  726. packets_dropped++;
  727. dev->stats.rx_errors++;
  728. continue;
  729. }
  730. u64_stats_update_begin(&stats->syncp);
  731. stats->rx_packets++;
  732. stats->rx_bytes += skb->len;
  733. u64_stats_update_end(&stats->syncp);
  734. /* Pass it up. */
  735. netif_receive_skb(skb);
  736. }
  737. return packets_dropped;
  738. }
  739. static int xennet_poll(struct napi_struct *napi, int budget)
  740. {
  741. struct netfront_info *np = container_of(napi, struct netfront_info, napi);
  742. struct net_device *dev = np->netdev;
  743. struct sk_buff *skb;
  744. struct netfront_rx_info rinfo;
  745. struct xen_netif_rx_response *rx = &rinfo.rx;
  746. struct xen_netif_extra_info *extras = rinfo.extras;
  747. RING_IDX i, rp;
  748. int work_done;
  749. struct sk_buff_head rxq;
  750. struct sk_buff_head errq;
  751. struct sk_buff_head tmpq;
  752. unsigned long flags;
  753. unsigned int len;
  754. int err;
  755. spin_lock(&np->rx_lock);
  756. skb_queue_head_init(&rxq);
  757. skb_queue_head_init(&errq);
  758. skb_queue_head_init(&tmpq);
  759. rp = np->rx.sring->rsp_prod;
  760. rmb(); /* Ensure we see queued responses up to 'rp'. */
  761. i = np->rx.rsp_cons;
  762. work_done = 0;
  763. while ((i != rp) && (work_done < budget)) {
  764. memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
  765. memset(extras, 0, sizeof(rinfo.extras));
  766. err = xennet_get_responses(np, &rinfo, rp, &tmpq);
  767. if (unlikely(err)) {
  768. err:
  769. while ((skb = __skb_dequeue(&tmpq)))
  770. __skb_queue_tail(&errq, skb);
  771. dev->stats.rx_errors++;
  772. i = np->rx.rsp_cons;
  773. continue;
  774. }
  775. skb = __skb_dequeue(&tmpq);
  776. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  777. struct xen_netif_extra_info *gso;
  778. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  779. if (unlikely(xennet_set_skb_gso(skb, gso))) {
  780. __skb_queue_head(&tmpq, skb);
  781. np->rx.rsp_cons += skb_queue_len(&tmpq);
  782. goto err;
  783. }
  784. }
  785. NETFRONT_SKB_CB(skb)->page =
  786. skb_frag_page(&skb_shinfo(skb)->frags[0]);
  787. NETFRONT_SKB_CB(skb)->offset = rx->offset;
  788. len = rx->status;
  789. if (len > RX_COPY_THRESHOLD)
  790. len = RX_COPY_THRESHOLD;
  791. skb_put(skb, len);
  792. if (rx->status > len) {
  793. skb_shinfo(skb)->frags[0].page_offset =
  794. rx->offset + len;
  795. skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status - len);
  796. skb->data_len = rx->status - len;
  797. } else {
  798. __skb_fill_page_desc(skb, 0, NULL, 0, 0);
  799. skb_shinfo(skb)->nr_frags = 0;
  800. }
  801. i = xennet_fill_frags(np, skb, &tmpq);
  802. /*
  803. * Truesize approximates the size of true data plus
  804. * any supervisor overheads. Adding hypervisor
  805. * overheads has been shown to significantly reduce
  806. * achievable bandwidth with the default receive
  807. * buffer size. It is therefore not wise to account
  808. * for it here.
  809. *
  810. * After alloc_skb(RX_COPY_THRESHOLD), truesize is set
  811. * to RX_COPY_THRESHOLD + the supervisor
  812. * overheads. Here, we add the size of the data pulled
  813. * in xennet_fill_frags().
  814. *
  815. * We also adjust for any unused space in the main
  816. * data area by subtracting (RX_COPY_THRESHOLD -
  817. * len). This is especially important with drivers
  818. * which split incoming packets into header and data,
  819. * using only 66 bytes of the main data area (see the
  820. * e1000 driver for example.) On such systems,
  821. * without this last adjustement, our achievable
  822. * receive throughout using the standard receive
  823. * buffer size was cut by 25%(!!!).
  824. */
  825. skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
  826. skb->len += skb->data_len;
  827. if (rx->flags & XEN_NETRXF_csum_blank)
  828. skb->ip_summed = CHECKSUM_PARTIAL;
  829. else if (rx->flags & XEN_NETRXF_data_validated)
  830. skb->ip_summed = CHECKSUM_UNNECESSARY;
  831. __skb_queue_tail(&rxq, skb);
  832. np->rx.rsp_cons = ++i;
  833. work_done++;
  834. }
  835. __skb_queue_purge(&errq);
  836. work_done -= handle_incoming_queue(dev, &rxq);
  837. /* If we get a callback with very few responses, reduce fill target. */
  838. /* NB. Note exponential increase, linear decrease. */
  839. if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
  840. ((3*np->rx_target) / 4)) &&
  841. (--np->rx_target < np->rx_min_target))
  842. np->rx_target = np->rx_min_target;
  843. xennet_alloc_rx_buffers(dev);
  844. if (work_done < budget) {
  845. int more_to_do = 0;
  846. local_irq_save(flags);
  847. RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
  848. if (!more_to_do)
  849. __napi_complete(napi);
  850. local_irq_restore(flags);
  851. }
  852. spin_unlock(&np->rx_lock);
  853. return work_done;
  854. }
  855. static int xennet_change_mtu(struct net_device *dev, int mtu)
  856. {
  857. int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
  858. if (mtu > max)
  859. return -EINVAL;
  860. dev->mtu = mtu;
  861. return 0;
  862. }
  863. static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev,
  864. struct rtnl_link_stats64 *tot)
  865. {
  866. struct netfront_info *np = netdev_priv(dev);
  867. int cpu;
  868. for_each_possible_cpu(cpu) {
  869. struct netfront_stats *stats = per_cpu_ptr(np->stats, cpu);
  870. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  871. unsigned int start;
  872. do {
  873. start = u64_stats_fetch_begin_bh(&stats->syncp);
  874. rx_packets = stats->rx_packets;
  875. tx_packets = stats->tx_packets;
  876. rx_bytes = stats->rx_bytes;
  877. tx_bytes = stats->tx_bytes;
  878. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  879. tot->rx_packets += rx_packets;
  880. tot->tx_packets += tx_packets;
  881. tot->rx_bytes += rx_bytes;
  882. tot->tx_bytes += tx_bytes;
  883. }
  884. tot->rx_errors = dev->stats.rx_errors;
  885. tot->tx_dropped = dev->stats.tx_dropped;
  886. return tot;
  887. }
  888. static void xennet_release_tx_bufs(struct netfront_info *np)
  889. {
  890. struct sk_buff *skb;
  891. int i;
  892. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  893. /* Skip over entries which are actually freelist references */
  894. if (skb_entry_is_link(&np->tx_skbs[i]))
  895. continue;
  896. skb = np->tx_skbs[i].skb;
  897. gnttab_end_foreign_access_ref(np->grant_tx_ref[i],
  898. GNTMAP_readonly);
  899. gnttab_release_grant_reference(&np->gref_tx_head,
  900. np->grant_tx_ref[i]);
  901. np->grant_tx_ref[i] = GRANT_INVALID_REF;
  902. add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, i);
  903. dev_kfree_skb_irq(skb);
  904. }
  905. }
  906. static void xennet_release_rx_bufs(struct netfront_info *np)
  907. {
  908. struct mmu_update *mmu = np->rx_mmu;
  909. struct multicall_entry *mcl = np->rx_mcl;
  910. struct sk_buff_head free_list;
  911. struct sk_buff *skb;
  912. unsigned long mfn;
  913. int xfer = 0, noxfer = 0, unused = 0;
  914. int id, ref;
  915. dev_warn(&np->netdev->dev, "%s: fix me for copying receiver.\n",
  916. __func__);
  917. return;
  918. skb_queue_head_init(&free_list);
  919. spin_lock_bh(&np->rx_lock);
  920. for (id = 0; id < NET_RX_RING_SIZE; id++) {
  921. ref = np->grant_rx_ref[id];
  922. if (ref == GRANT_INVALID_REF) {
  923. unused++;
  924. continue;
  925. }
  926. skb = np->rx_skbs[id];
  927. mfn = gnttab_end_foreign_transfer_ref(ref);
  928. gnttab_release_grant_reference(&np->gref_rx_head, ref);
  929. np->grant_rx_ref[id] = GRANT_INVALID_REF;
  930. if (0 == mfn) {
  931. skb_shinfo(skb)->nr_frags = 0;
  932. dev_kfree_skb(skb);
  933. noxfer++;
  934. continue;
  935. }
  936. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  937. /* Remap the page. */
  938. const struct page *page =
  939. skb_frag_page(&skb_shinfo(skb)->frags[0]);
  940. unsigned long pfn = page_to_pfn(page);
  941. void *vaddr = page_address(page);
  942. MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
  943. mfn_pte(mfn, PAGE_KERNEL),
  944. 0);
  945. mcl++;
  946. mmu->ptr = ((u64)mfn << PAGE_SHIFT)
  947. | MMU_MACHPHYS_UPDATE;
  948. mmu->val = pfn;
  949. mmu++;
  950. set_phys_to_machine(pfn, mfn);
  951. }
  952. __skb_queue_tail(&free_list, skb);
  953. xfer++;
  954. }
  955. dev_info(&np->netdev->dev, "%s: %d xfer, %d noxfer, %d unused\n",
  956. __func__, xfer, noxfer, unused);
  957. if (xfer) {
  958. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  959. /* Do all the remapping work and M2P updates. */
  960. MULTI_mmu_update(mcl, np->rx_mmu, mmu - np->rx_mmu,
  961. NULL, DOMID_SELF);
  962. mcl++;
  963. HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
  964. }
  965. }
  966. __skb_queue_purge(&free_list);
  967. spin_unlock_bh(&np->rx_lock);
  968. }
  969. static void xennet_uninit(struct net_device *dev)
  970. {
  971. struct netfront_info *np = netdev_priv(dev);
  972. xennet_release_tx_bufs(np);
  973. xennet_release_rx_bufs(np);
  974. gnttab_free_grant_references(np->gref_tx_head);
  975. gnttab_free_grant_references(np->gref_rx_head);
  976. }
  977. static netdev_features_t xennet_fix_features(struct net_device *dev,
  978. netdev_features_t features)
  979. {
  980. struct netfront_info *np = netdev_priv(dev);
  981. int val;
  982. if (features & NETIF_F_SG) {
  983. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
  984. "%d", &val) < 0)
  985. val = 0;
  986. if (!val)
  987. features &= ~NETIF_F_SG;
  988. }
  989. if (features & NETIF_F_TSO) {
  990. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  991. "feature-gso-tcpv4", "%d", &val) < 0)
  992. val = 0;
  993. if (!val)
  994. features &= ~NETIF_F_TSO;
  995. }
  996. return features;
  997. }
  998. static int xennet_set_features(struct net_device *dev,
  999. netdev_features_t features)
  1000. {
  1001. if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
  1002. netdev_info(dev, "Reducing MTU because no SG offload");
  1003. dev->mtu = ETH_DATA_LEN;
  1004. }
  1005. return 0;
  1006. }
  1007. static const struct net_device_ops xennet_netdev_ops = {
  1008. .ndo_open = xennet_open,
  1009. .ndo_uninit = xennet_uninit,
  1010. .ndo_stop = xennet_close,
  1011. .ndo_start_xmit = xennet_start_xmit,
  1012. .ndo_change_mtu = xennet_change_mtu,
  1013. .ndo_get_stats64 = xennet_get_stats64,
  1014. .ndo_set_mac_address = eth_mac_addr,
  1015. .ndo_validate_addr = eth_validate_addr,
  1016. .ndo_fix_features = xennet_fix_features,
  1017. .ndo_set_features = xennet_set_features,
  1018. };
  1019. static struct net_device * __devinit xennet_create_dev(struct xenbus_device *dev)
  1020. {
  1021. int i, err;
  1022. struct net_device *netdev;
  1023. struct netfront_info *np;
  1024. netdev = alloc_etherdev(sizeof(struct netfront_info));
  1025. if (!netdev) {
  1026. printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
  1027. __func__);
  1028. return ERR_PTR(-ENOMEM);
  1029. }
  1030. np = netdev_priv(netdev);
  1031. np->xbdev = dev;
  1032. spin_lock_init(&np->tx_lock);
  1033. spin_lock_init(&np->rx_lock);
  1034. skb_queue_head_init(&np->rx_batch);
  1035. np->rx_target = RX_DFL_MIN_TARGET;
  1036. np->rx_min_target = RX_DFL_MIN_TARGET;
  1037. np->rx_max_target = RX_MAX_TARGET;
  1038. init_timer(&np->rx_refill_timer);
  1039. np->rx_refill_timer.data = (unsigned long)netdev;
  1040. np->rx_refill_timer.function = rx_refill_timeout;
  1041. err = -ENOMEM;
  1042. np->stats = alloc_percpu(struct netfront_stats);
  1043. if (np->stats == NULL)
  1044. goto exit;
  1045. /* Initialise tx_skbs as a free chain containing every entry. */
  1046. np->tx_skb_freelist = 0;
  1047. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  1048. skb_entry_set_link(&np->tx_skbs[i], i+1);
  1049. np->grant_tx_ref[i] = GRANT_INVALID_REF;
  1050. }
  1051. /* Clear out rx_skbs */
  1052. for (i = 0; i < NET_RX_RING_SIZE; i++) {
  1053. np->rx_skbs[i] = NULL;
  1054. np->grant_rx_ref[i] = GRANT_INVALID_REF;
  1055. }
  1056. /* A grant for every tx ring slot */
  1057. if (gnttab_alloc_grant_references(TX_MAX_TARGET,
  1058. &np->gref_tx_head) < 0) {
  1059. printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
  1060. err = -ENOMEM;
  1061. goto exit_free_stats;
  1062. }
  1063. /* A grant for every rx ring slot */
  1064. if (gnttab_alloc_grant_references(RX_MAX_TARGET,
  1065. &np->gref_rx_head) < 0) {
  1066. printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
  1067. err = -ENOMEM;
  1068. goto exit_free_tx;
  1069. }
  1070. netdev->netdev_ops = &xennet_netdev_ops;
  1071. netif_napi_add(netdev, &np->napi, xennet_poll, 64);
  1072. netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  1073. NETIF_F_GSO_ROBUST;
  1074. netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO;
  1075. /*
  1076. * Assume that all hw features are available for now. This set
  1077. * will be adjusted by the call to netdev_update_features() in
  1078. * xennet_connect() which is the earliest point where we can
  1079. * negotiate with the backend regarding supported features.
  1080. */
  1081. netdev->features |= netdev->hw_features;
  1082. SET_ETHTOOL_OPS(netdev, &xennet_ethtool_ops);
  1083. SET_NETDEV_DEV(netdev, &dev->dev);
  1084. np->netdev = netdev;
  1085. netif_carrier_off(netdev);
  1086. return netdev;
  1087. exit_free_tx:
  1088. gnttab_free_grant_references(np->gref_tx_head);
  1089. exit_free_stats:
  1090. free_percpu(np->stats);
  1091. exit:
  1092. free_netdev(netdev);
  1093. return ERR_PTR(err);
  1094. }
  1095. /**
  1096. * Entry point to this code when a new device is created. Allocate the basic
  1097. * structures and the ring buffers for communication with the backend, and
  1098. * inform the backend of the appropriate details for those.
  1099. */
  1100. static int __devinit netfront_probe(struct xenbus_device *dev,
  1101. const struct xenbus_device_id *id)
  1102. {
  1103. int err;
  1104. struct net_device *netdev;
  1105. struct netfront_info *info;
  1106. netdev = xennet_create_dev(dev);
  1107. if (IS_ERR(netdev)) {
  1108. err = PTR_ERR(netdev);
  1109. xenbus_dev_fatal(dev, err, "creating netdev");
  1110. return err;
  1111. }
  1112. info = netdev_priv(netdev);
  1113. dev_set_drvdata(&dev->dev, info);
  1114. err = register_netdev(info->netdev);
  1115. if (err) {
  1116. printk(KERN_WARNING "%s: register_netdev err=%d\n",
  1117. __func__, err);
  1118. goto fail;
  1119. }
  1120. err = xennet_sysfs_addif(info->netdev);
  1121. if (err) {
  1122. unregister_netdev(info->netdev);
  1123. printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
  1124. __func__, err);
  1125. goto fail;
  1126. }
  1127. return 0;
  1128. fail:
  1129. free_netdev(netdev);
  1130. dev_set_drvdata(&dev->dev, NULL);
  1131. return err;
  1132. }
  1133. static void xennet_end_access(int ref, void *page)
  1134. {
  1135. /* This frees the page as a side-effect */
  1136. if (ref != GRANT_INVALID_REF)
  1137. gnttab_end_foreign_access(ref, 0, (unsigned long)page);
  1138. }
  1139. static void xennet_disconnect_backend(struct netfront_info *info)
  1140. {
  1141. /* Stop old i/f to prevent errors whilst we rebuild the state. */
  1142. spin_lock_bh(&info->rx_lock);
  1143. spin_lock_irq(&info->tx_lock);
  1144. netif_carrier_off(info->netdev);
  1145. spin_unlock_irq(&info->tx_lock);
  1146. spin_unlock_bh(&info->rx_lock);
  1147. if (info->netdev->irq)
  1148. unbind_from_irqhandler(info->netdev->irq, info->netdev);
  1149. info->evtchn = info->netdev->irq = 0;
  1150. /* End access and free the pages */
  1151. xennet_end_access(info->tx_ring_ref, info->tx.sring);
  1152. xennet_end_access(info->rx_ring_ref, info->rx.sring);
  1153. info->tx_ring_ref = GRANT_INVALID_REF;
  1154. info->rx_ring_ref = GRANT_INVALID_REF;
  1155. info->tx.sring = NULL;
  1156. info->rx.sring = NULL;
  1157. }
  1158. /**
  1159. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1160. * driver restart. We tear down our netif structure and recreate it, but
  1161. * leave the device-layer structures intact so that this is transparent to the
  1162. * rest of the kernel.
  1163. */
  1164. static int netfront_resume(struct xenbus_device *dev)
  1165. {
  1166. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1167. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1168. xennet_disconnect_backend(info);
  1169. return 0;
  1170. }
  1171. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  1172. {
  1173. char *s, *e, *macstr;
  1174. int i;
  1175. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  1176. if (IS_ERR(macstr))
  1177. return PTR_ERR(macstr);
  1178. for (i = 0; i < ETH_ALEN; i++) {
  1179. mac[i] = simple_strtoul(s, &e, 16);
  1180. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  1181. kfree(macstr);
  1182. return -ENOENT;
  1183. }
  1184. s = e+1;
  1185. }
  1186. kfree(macstr);
  1187. return 0;
  1188. }
  1189. static irqreturn_t xennet_interrupt(int irq, void *dev_id)
  1190. {
  1191. struct net_device *dev = dev_id;
  1192. struct netfront_info *np = netdev_priv(dev);
  1193. unsigned long flags;
  1194. spin_lock_irqsave(&np->tx_lock, flags);
  1195. if (likely(netif_carrier_ok(dev))) {
  1196. xennet_tx_buf_gc(dev);
  1197. /* Under tx_lock: protects access to rx shared-ring indexes. */
  1198. if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
  1199. napi_schedule(&np->napi);
  1200. }
  1201. spin_unlock_irqrestore(&np->tx_lock, flags);
  1202. return IRQ_HANDLED;
  1203. }
  1204. static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)
  1205. {
  1206. struct xen_netif_tx_sring *txs;
  1207. struct xen_netif_rx_sring *rxs;
  1208. int err;
  1209. struct net_device *netdev = info->netdev;
  1210. info->tx_ring_ref = GRANT_INVALID_REF;
  1211. info->rx_ring_ref = GRANT_INVALID_REF;
  1212. info->rx.sring = NULL;
  1213. info->tx.sring = NULL;
  1214. netdev->irq = 0;
  1215. err = xen_net_read_mac(dev, netdev->dev_addr);
  1216. if (err) {
  1217. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  1218. goto fail;
  1219. }
  1220. txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1221. if (!txs) {
  1222. err = -ENOMEM;
  1223. xenbus_dev_fatal(dev, err, "allocating tx ring page");
  1224. goto fail;
  1225. }
  1226. SHARED_RING_INIT(txs);
  1227. FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
  1228. err = xenbus_grant_ring(dev, virt_to_mfn(txs));
  1229. if (err < 0) {
  1230. free_page((unsigned long)txs);
  1231. goto fail;
  1232. }
  1233. info->tx_ring_ref = err;
  1234. rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1235. if (!rxs) {
  1236. err = -ENOMEM;
  1237. xenbus_dev_fatal(dev, err, "allocating rx ring page");
  1238. goto fail;
  1239. }
  1240. SHARED_RING_INIT(rxs);
  1241. FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
  1242. err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
  1243. if (err < 0) {
  1244. free_page((unsigned long)rxs);
  1245. goto fail;
  1246. }
  1247. info->rx_ring_ref = err;
  1248. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  1249. if (err)
  1250. goto fail;
  1251. err = bind_evtchn_to_irqhandler(info->evtchn, xennet_interrupt,
  1252. 0, netdev->name, netdev);
  1253. if (err < 0)
  1254. goto fail;
  1255. netdev->irq = err;
  1256. return 0;
  1257. fail:
  1258. return err;
  1259. }
  1260. /* Common code used when first setting up, and when resuming. */
  1261. static int talk_to_netback(struct xenbus_device *dev,
  1262. struct netfront_info *info)
  1263. {
  1264. const char *message;
  1265. struct xenbus_transaction xbt;
  1266. int err;
  1267. /* Create shared ring, alloc event channel. */
  1268. err = setup_netfront(dev, info);
  1269. if (err)
  1270. goto out;
  1271. again:
  1272. err = xenbus_transaction_start(&xbt);
  1273. if (err) {
  1274. xenbus_dev_fatal(dev, err, "starting transaction");
  1275. goto destroy_ring;
  1276. }
  1277. err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
  1278. info->tx_ring_ref);
  1279. if (err) {
  1280. message = "writing tx ring-ref";
  1281. goto abort_transaction;
  1282. }
  1283. err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
  1284. info->rx_ring_ref);
  1285. if (err) {
  1286. message = "writing rx ring-ref";
  1287. goto abort_transaction;
  1288. }
  1289. err = xenbus_printf(xbt, dev->nodename,
  1290. "event-channel", "%u", info->evtchn);
  1291. if (err) {
  1292. message = "writing event-channel";
  1293. goto abort_transaction;
  1294. }
  1295. err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
  1296. 1);
  1297. if (err) {
  1298. message = "writing request-rx-copy";
  1299. goto abort_transaction;
  1300. }
  1301. err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
  1302. if (err) {
  1303. message = "writing feature-rx-notify";
  1304. goto abort_transaction;
  1305. }
  1306. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
  1307. if (err) {
  1308. message = "writing feature-sg";
  1309. goto abort_transaction;
  1310. }
  1311. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
  1312. if (err) {
  1313. message = "writing feature-gso-tcpv4";
  1314. goto abort_transaction;
  1315. }
  1316. err = xenbus_transaction_end(xbt, 0);
  1317. if (err) {
  1318. if (err == -EAGAIN)
  1319. goto again;
  1320. xenbus_dev_fatal(dev, err, "completing transaction");
  1321. goto destroy_ring;
  1322. }
  1323. return 0;
  1324. abort_transaction:
  1325. xenbus_transaction_end(xbt, 1);
  1326. xenbus_dev_fatal(dev, err, "%s", message);
  1327. destroy_ring:
  1328. xennet_disconnect_backend(info);
  1329. out:
  1330. return err;
  1331. }
  1332. static int xennet_connect(struct net_device *dev)
  1333. {
  1334. struct netfront_info *np = netdev_priv(dev);
  1335. int i, requeue_idx, err;
  1336. struct sk_buff *skb;
  1337. grant_ref_t ref;
  1338. struct xen_netif_rx_request *req;
  1339. unsigned int feature_rx_copy;
  1340. err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  1341. "feature-rx-copy", "%u", &feature_rx_copy);
  1342. if (err != 1)
  1343. feature_rx_copy = 0;
  1344. if (!feature_rx_copy) {
  1345. dev_info(&dev->dev,
  1346. "backend does not support copying receive path\n");
  1347. return -ENODEV;
  1348. }
  1349. err = talk_to_netback(np->xbdev, np);
  1350. if (err)
  1351. return err;
  1352. rtnl_lock();
  1353. netdev_update_features(dev);
  1354. rtnl_unlock();
  1355. spin_lock_bh(&np->rx_lock);
  1356. spin_lock_irq(&np->tx_lock);
  1357. /* Step 1: Discard all pending TX packet fragments. */
  1358. xennet_release_tx_bufs(np);
  1359. /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
  1360. for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
  1361. skb_frag_t *frag;
  1362. const struct page *page;
  1363. if (!np->rx_skbs[i])
  1364. continue;
  1365. skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
  1366. ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
  1367. req = RING_GET_REQUEST(&np->rx, requeue_idx);
  1368. frag = &skb_shinfo(skb)->frags[0];
  1369. page = skb_frag_page(frag);
  1370. gnttab_grant_foreign_access_ref(
  1371. ref, np->xbdev->otherend_id,
  1372. pfn_to_mfn(page_to_pfn(page)),
  1373. 0);
  1374. req->gref = ref;
  1375. req->id = requeue_idx;
  1376. requeue_idx++;
  1377. }
  1378. np->rx.req_prod_pvt = requeue_idx;
  1379. /*
  1380. * Step 3: All public and private state should now be sane. Get
  1381. * ready to start sending and receiving packets and give the driver
  1382. * domain a kick because we've probably just requeued some
  1383. * packets.
  1384. */
  1385. netif_carrier_on(np->netdev);
  1386. notify_remote_via_irq(np->netdev->irq);
  1387. xennet_tx_buf_gc(dev);
  1388. xennet_alloc_rx_buffers(dev);
  1389. spin_unlock_irq(&np->tx_lock);
  1390. spin_unlock_bh(&np->rx_lock);
  1391. return 0;
  1392. }
  1393. /**
  1394. * Callback received when the backend's state changes.
  1395. */
  1396. static void netback_changed(struct xenbus_device *dev,
  1397. enum xenbus_state backend_state)
  1398. {
  1399. struct netfront_info *np = dev_get_drvdata(&dev->dev);
  1400. struct net_device *netdev = np->netdev;
  1401. dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
  1402. switch (backend_state) {
  1403. case XenbusStateInitialising:
  1404. case XenbusStateInitialised:
  1405. case XenbusStateReconfiguring:
  1406. case XenbusStateReconfigured:
  1407. case XenbusStateUnknown:
  1408. case XenbusStateClosed:
  1409. break;
  1410. case XenbusStateInitWait:
  1411. if (dev->state != XenbusStateInitialising)
  1412. break;
  1413. if (xennet_connect(netdev) != 0)
  1414. break;
  1415. xenbus_switch_state(dev, XenbusStateConnected);
  1416. break;
  1417. case XenbusStateConnected:
  1418. netif_notify_peers(netdev);
  1419. break;
  1420. case XenbusStateClosing:
  1421. xenbus_frontend_closed(dev);
  1422. break;
  1423. }
  1424. }
  1425. static const struct xennet_stat {
  1426. char name[ETH_GSTRING_LEN];
  1427. u16 offset;
  1428. } xennet_stats[] = {
  1429. {
  1430. "rx_gso_checksum_fixup",
  1431. offsetof(struct netfront_info, rx_gso_checksum_fixup)
  1432. },
  1433. };
  1434. static int xennet_get_sset_count(struct net_device *dev, int string_set)
  1435. {
  1436. switch (string_set) {
  1437. case ETH_SS_STATS:
  1438. return ARRAY_SIZE(xennet_stats);
  1439. default:
  1440. return -EINVAL;
  1441. }
  1442. }
  1443. static void xennet_get_ethtool_stats(struct net_device *dev,
  1444. struct ethtool_stats *stats, u64 * data)
  1445. {
  1446. void *np = netdev_priv(dev);
  1447. int i;
  1448. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1449. data[i] = *(unsigned long *)(np + xennet_stats[i].offset);
  1450. }
  1451. static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  1452. {
  1453. int i;
  1454. switch (stringset) {
  1455. case ETH_SS_STATS:
  1456. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1457. memcpy(data + i * ETH_GSTRING_LEN,
  1458. xennet_stats[i].name, ETH_GSTRING_LEN);
  1459. break;
  1460. }
  1461. }
  1462. static const struct ethtool_ops xennet_ethtool_ops =
  1463. {
  1464. .get_link = ethtool_op_get_link,
  1465. .get_sset_count = xennet_get_sset_count,
  1466. .get_ethtool_stats = xennet_get_ethtool_stats,
  1467. .get_strings = xennet_get_strings,
  1468. };
  1469. #ifdef CONFIG_SYSFS
  1470. static ssize_t show_rxbuf_min(struct device *dev,
  1471. struct device_attribute *attr, char *buf)
  1472. {
  1473. struct net_device *netdev = to_net_dev(dev);
  1474. struct netfront_info *info = netdev_priv(netdev);
  1475. return sprintf(buf, "%u\n", info->rx_min_target);
  1476. }
  1477. static ssize_t store_rxbuf_min(struct device *dev,
  1478. struct device_attribute *attr,
  1479. const char *buf, size_t len)
  1480. {
  1481. struct net_device *netdev = to_net_dev(dev);
  1482. struct netfront_info *np = netdev_priv(netdev);
  1483. char *endp;
  1484. unsigned long target;
  1485. if (!capable(CAP_NET_ADMIN))
  1486. return -EPERM;
  1487. target = simple_strtoul(buf, &endp, 0);
  1488. if (endp == buf)
  1489. return -EBADMSG;
  1490. if (target < RX_MIN_TARGET)
  1491. target = RX_MIN_TARGET;
  1492. if (target > RX_MAX_TARGET)
  1493. target = RX_MAX_TARGET;
  1494. spin_lock_bh(&np->rx_lock);
  1495. if (target > np->rx_max_target)
  1496. np->rx_max_target = target;
  1497. np->rx_min_target = target;
  1498. if (target > np->rx_target)
  1499. np->rx_target = target;
  1500. xennet_alloc_rx_buffers(netdev);
  1501. spin_unlock_bh(&np->rx_lock);
  1502. return len;
  1503. }
  1504. static ssize_t show_rxbuf_max(struct device *dev,
  1505. struct device_attribute *attr, char *buf)
  1506. {
  1507. struct net_device *netdev = to_net_dev(dev);
  1508. struct netfront_info *info = netdev_priv(netdev);
  1509. return sprintf(buf, "%u\n", info->rx_max_target);
  1510. }
  1511. static ssize_t store_rxbuf_max(struct device *dev,
  1512. struct device_attribute *attr,
  1513. const char *buf, size_t len)
  1514. {
  1515. struct net_device *netdev = to_net_dev(dev);
  1516. struct netfront_info *np = netdev_priv(netdev);
  1517. char *endp;
  1518. unsigned long target;
  1519. if (!capable(CAP_NET_ADMIN))
  1520. return -EPERM;
  1521. target = simple_strtoul(buf, &endp, 0);
  1522. if (endp == buf)
  1523. return -EBADMSG;
  1524. if (target < RX_MIN_TARGET)
  1525. target = RX_MIN_TARGET;
  1526. if (target > RX_MAX_TARGET)
  1527. target = RX_MAX_TARGET;
  1528. spin_lock_bh(&np->rx_lock);
  1529. if (target < np->rx_min_target)
  1530. np->rx_min_target = target;
  1531. np->rx_max_target = target;
  1532. if (target < np->rx_target)
  1533. np->rx_target = target;
  1534. xennet_alloc_rx_buffers(netdev);
  1535. spin_unlock_bh(&np->rx_lock);
  1536. return len;
  1537. }
  1538. static ssize_t show_rxbuf_cur(struct device *dev,
  1539. struct device_attribute *attr, char *buf)
  1540. {
  1541. struct net_device *netdev = to_net_dev(dev);
  1542. struct netfront_info *info = netdev_priv(netdev);
  1543. return sprintf(buf, "%u\n", info->rx_target);
  1544. }
  1545. static struct device_attribute xennet_attrs[] = {
  1546. __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
  1547. __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
  1548. __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
  1549. };
  1550. static int xennet_sysfs_addif(struct net_device *netdev)
  1551. {
  1552. int i;
  1553. int err;
  1554. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
  1555. err = device_create_file(&netdev->dev,
  1556. &xennet_attrs[i]);
  1557. if (err)
  1558. goto fail;
  1559. }
  1560. return 0;
  1561. fail:
  1562. while (--i >= 0)
  1563. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1564. return err;
  1565. }
  1566. static void xennet_sysfs_delif(struct net_device *netdev)
  1567. {
  1568. int i;
  1569. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
  1570. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1571. }
  1572. #endif /* CONFIG_SYSFS */
  1573. static const struct xenbus_device_id netfront_ids[] = {
  1574. { "vif" },
  1575. { "" }
  1576. };
  1577. static int __devexit xennet_remove(struct xenbus_device *dev)
  1578. {
  1579. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1580. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1581. unregister_netdev(info->netdev);
  1582. xennet_disconnect_backend(info);
  1583. del_timer_sync(&info->rx_refill_timer);
  1584. xennet_sysfs_delif(info->netdev);
  1585. free_percpu(info->stats);
  1586. free_netdev(info->netdev);
  1587. return 0;
  1588. }
  1589. static DEFINE_XENBUS_DRIVER(netfront, ,
  1590. .probe = netfront_probe,
  1591. .remove = __devexit_p(xennet_remove),
  1592. .resume = netfront_resume,
  1593. .otherend_changed = netback_changed,
  1594. );
  1595. static int __init netif_init(void)
  1596. {
  1597. if (!xen_domain())
  1598. return -ENODEV;
  1599. if (xen_initial_domain())
  1600. return 0;
  1601. printk(KERN_INFO "Initialising Xen virtual ethernet driver.\n");
  1602. return xenbus_register_frontend(&netfront_driver);
  1603. }
  1604. module_init(netif_init);
  1605. static void __exit netif_exit(void)
  1606. {
  1607. if (xen_initial_domain())
  1608. return;
  1609. xenbus_unregister_driver(&netfront_driver);
  1610. }
  1611. module_exit(netif_exit);
  1612. MODULE_DESCRIPTION("Xen virtual network device frontend");
  1613. MODULE_LICENSE("GPL");
  1614. MODULE_ALIAS("xen:vif");
  1615. MODULE_ALIAS("xennet");