xen-netfront.c 49 KB

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