xen-netfront.c 52 KB

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