xen-netfront.c 52 KB

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