xen-netfront.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719
  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 <linux/bpf.h>
  46. #include <net/page_pool/types.h>
  47. #include <linux/bpf_trace.h>
  48. #include <xen/xen.h>
  49. #include <xen/xenbus.h>
  50. #include <xen/events.h>
  51. #include <xen/page.h>
  52. #include <xen/platform_pci.h>
  53. #include <xen/grant_table.h>
  54. #include <xen/interface/io/netif.h>
  55. #include <xen/interface/memory.h>
  56. #include <xen/interface/grant_table.h>
  57. /* Module parameters */
  58. #define MAX_QUEUES_DEFAULT 8
  59. static unsigned int xennet_max_queues;
  60. module_param_named(max_queues, xennet_max_queues, uint, 0644);
  61. MODULE_PARM_DESC(max_queues,
  62. "Maximum number of queues per virtual interface");
  63. static bool __read_mostly xennet_trusted = true;
  64. module_param_named(trusted, xennet_trusted, bool, 0644);
  65. MODULE_PARM_DESC(trusted, "Is the backend trusted");
  66. #define XENNET_TIMEOUT (5 * HZ)
  67. static const struct ethtool_ops xennet_ethtool_ops;
  68. struct netfront_cb {
  69. int pull_to;
  70. };
  71. #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
  72. #define RX_COPY_THRESHOLD 256
  73. #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
  74. #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
  75. /* Minimum number of Rx slots (includes slot for GSO metadata). */
  76. #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
  77. /* Queue name is interface name with "-qNNN" appended */
  78. #define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
  79. /* IRQ name is queue name with "-tx" or "-rx" appended */
  80. #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
  81. static DECLARE_WAIT_QUEUE_HEAD(module_wq);
  82. struct netfront_stats {
  83. u64 packets;
  84. u64 bytes;
  85. struct u64_stats_sync syncp;
  86. };
  87. struct netfront_info;
  88. struct netfront_queue {
  89. unsigned int id; /* Queue ID, 0-based */
  90. char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
  91. struct netfront_info *info;
  92. struct bpf_prog __rcu *xdp_prog;
  93. struct napi_struct napi;
  94. /* Split event channels support, tx_* == rx_* when using
  95. * single event channel.
  96. */
  97. unsigned int tx_evtchn, rx_evtchn;
  98. unsigned int tx_irq, rx_irq;
  99. /* Only used when split event channels support is enabled */
  100. char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
  101. char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
  102. spinlock_t tx_lock;
  103. struct xen_netif_tx_front_ring tx;
  104. int tx_ring_ref;
  105. /*
  106. * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
  107. * are linked from tx_skb_freelist through tx_link.
  108. */
  109. struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
  110. unsigned short tx_link[NET_TX_RING_SIZE];
  111. #define TX_LINK_NONE 0xffff
  112. #define TX_PENDING 0xfffe
  113. grant_ref_t gref_tx_head;
  114. grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
  115. struct page *grant_tx_page[NET_TX_RING_SIZE];
  116. unsigned tx_skb_freelist;
  117. unsigned int tx_pend_queue;
  118. spinlock_t rx_lock ____cacheline_aligned_in_smp;
  119. struct xen_netif_rx_front_ring rx;
  120. int rx_ring_ref;
  121. struct timer_list rx_refill_timer;
  122. struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
  123. grant_ref_t gref_rx_head;
  124. grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
  125. unsigned int rx_rsp_unconsumed;
  126. spinlock_t rx_cons_lock;
  127. struct page_pool *page_pool;
  128. struct xdp_rxq_info xdp_rxq;
  129. };
  130. struct netfront_info {
  131. struct list_head list;
  132. struct net_device *netdev;
  133. struct xenbus_device *xbdev;
  134. /* Multi-queue support */
  135. struct netfront_queue *queues;
  136. /* Statistics */
  137. struct netfront_stats __percpu *rx_stats;
  138. struct netfront_stats __percpu *tx_stats;
  139. /* XDP state */
  140. bool netback_has_xdp_headroom;
  141. bool netfront_xdp_enabled;
  142. /* Is device behaving sane? */
  143. bool broken;
  144. /* Should skbs be bounced into a zeroed buffer? */
  145. bool bounce;
  146. atomic_t rx_gso_checksum_fixup;
  147. };
  148. struct netfront_rx_info {
  149. struct xen_netif_rx_response rx;
  150. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
  151. };
  152. /*
  153. * Access macros for acquiring freeing slots in tx_skbs[].
  154. */
  155. static void add_id_to_list(unsigned *head, unsigned short *list,
  156. unsigned short id)
  157. {
  158. list[id] = *head;
  159. *head = id;
  160. }
  161. static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
  162. {
  163. unsigned int id = *head;
  164. if (id != TX_LINK_NONE) {
  165. *head = list[id];
  166. list[id] = TX_LINK_NONE;
  167. }
  168. return id;
  169. }
  170. static int xennet_rxidx(RING_IDX idx)
  171. {
  172. return idx & (NET_RX_RING_SIZE - 1);
  173. }
  174. static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
  175. RING_IDX ri)
  176. {
  177. int i = xennet_rxidx(ri);
  178. struct sk_buff *skb = queue->rx_skbs[i];
  179. queue->rx_skbs[i] = NULL;
  180. return skb;
  181. }
  182. static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
  183. RING_IDX ri)
  184. {
  185. int i = xennet_rxidx(ri);
  186. grant_ref_t ref = queue->grant_rx_ref[i];
  187. queue->grant_rx_ref[i] = INVALID_GRANT_REF;
  188. return ref;
  189. }
  190. #ifdef CONFIG_SYSFS
  191. static const struct attribute_group xennet_dev_group;
  192. #endif
  193. static bool xennet_can_sg(struct net_device *dev)
  194. {
  195. return dev->features & NETIF_F_SG;
  196. }
  197. static void rx_refill_timeout(struct timer_list *t)
  198. {
  199. struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
  200. napi_schedule(&queue->napi);
  201. }
  202. static int netfront_tx_slot_available(struct netfront_queue *queue)
  203. {
  204. return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
  205. (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
  206. }
  207. static void xennet_maybe_wake_tx(struct netfront_queue *queue)
  208. {
  209. struct net_device *dev = queue->info->netdev;
  210. struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
  211. if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
  212. netfront_tx_slot_available(queue) &&
  213. likely(netif_running(dev)))
  214. netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
  215. }
  216. static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
  217. {
  218. struct sk_buff *skb;
  219. struct page *page;
  220. skb = __netdev_alloc_skb(queue->info->netdev,
  221. RX_COPY_THRESHOLD + NET_IP_ALIGN,
  222. GFP_ATOMIC | __GFP_NOWARN);
  223. if (unlikely(!skb))
  224. return NULL;
  225. page = page_pool_alloc_pages(queue->page_pool,
  226. GFP_ATOMIC | __GFP_NOWARN | __GFP_ZERO);
  227. if (unlikely(!page)) {
  228. kfree_skb(skb);
  229. return NULL;
  230. }
  231. skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
  232. skb_mark_for_recycle(skb);
  233. /* Align ip header to a 16 bytes boundary */
  234. skb_reserve(skb, NET_IP_ALIGN);
  235. skb->dev = queue->info->netdev;
  236. return skb;
  237. }
  238. static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
  239. {
  240. RING_IDX req_prod = queue->rx.req_prod_pvt;
  241. int notify;
  242. int err = 0;
  243. if (unlikely(!netif_carrier_ok(queue->info->netdev)))
  244. return;
  245. for (req_prod = queue->rx.req_prod_pvt;
  246. req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
  247. req_prod++) {
  248. struct sk_buff *skb;
  249. unsigned short id;
  250. grant_ref_t ref;
  251. struct page *page;
  252. struct xen_netif_rx_request *req;
  253. skb = xennet_alloc_one_rx_buffer(queue);
  254. if (!skb) {
  255. err = -ENOMEM;
  256. break;
  257. }
  258. id = xennet_rxidx(req_prod);
  259. BUG_ON(queue->rx_skbs[id]);
  260. queue->rx_skbs[id] = skb;
  261. ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
  262. WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
  263. queue->grant_rx_ref[id] = ref;
  264. page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
  265. req = RING_GET_REQUEST(&queue->rx, req_prod);
  266. gnttab_page_grant_foreign_access_ref_one(ref,
  267. queue->info->xbdev->otherend_id,
  268. page,
  269. 0);
  270. req->id = id;
  271. req->gref = ref;
  272. }
  273. queue->rx.req_prod_pvt = req_prod;
  274. /* Try again later if there are not enough requests or skb allocation
  275. * failed.
  276. * Enough requests is quantified as the sum of newly created slots and
  277. * the unconsumed slots at the backend.
  278. */
  279. if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
  280. unlikely(err)) {
  281. mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
  282. return;
  283. }
  284. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
  285. if (notify)
  286. notify_remote_via_irq(queue->rx_irq);
  287. }
  288. static int xennet_open(struct net_device *dev)
  289. {
  290. struct netfront_info *np = netdev_priv(dev);
  291. unsigned int num_queues = dev->real_num_tx_queues;
  292. unsigned int i = 0;
  293. struct netfront_queue *queue = NULL;
  294. if (!np->queues || np->broken)
  295. return -ENODEV;
  296. for (i = 0; i < num_queues; ++i) {
  297. queue = &np->queues[i];
  298. napi_enable(&queue->napi);
  299. spin_lock_bh(&queue->rx_lock);
  300. if (netif_carrier_ok(dev)) {
  301. xennet_alloc_rx_buffers(queue);
  302. queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
  303. if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
  304. napi_schedule(&queue->napi);
  305. }
  306. spin_unlock_bh(&queue->rx_lock);
  307. }
  308. netif_tx_start_all_queues(dev);
  309. return 0;
  310. }
  311. static bool xennet_tx_buf_gc(struct netfront_queue *queue)
  312. {
  313. RING_IDX cons, prod;
  314. unsigned short id;
  315. struct sk_buff *skb;
  316. bool more_to_do;
  317. bool work_done = false;
  318. const struct device *dev = &queue->info->netdev->dev;
  319. BUG_ON(!netif_carrier_ok(queue->info->netdev));
  320. do {
  321. prod = queue->tx.sring->rsp_prod;
  322. if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) {
  323. dev_alert(dev, "Illegal number of responses %u\n",
  324. prod - queue->tx.rsp_cons);
  325. goto err;
  326. }
  327. rmb(); /* Ensure we see responses up to 'rp'. */
  328. for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
  329. struct xen_netif_tx_response txrsp;
  330. work_done = true;
  331. RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
  332. if (txrsp.status == XEN_NETIF_RSP_NULL)
  333. continue;
  334. id = txrsp.id;
  335. if (id >= RING_SIZE(&queue->tx)) {
  336. dev_alert(dev,
  337. "Response has incorrect id (%u)\n",
  338. id);
  339. goto err;
  340. }
  341. if (queue->tx_link[id] != TX_PENDING) {
  342. dev_alert(dev,
  343. "Response for inactive request\n");
  344. goto err;
  345. }
  346. queue->tx_link[id] = TX_LINK_NONE;
  347. skb = queue->tx_skbs[id];
  348. queue->tx_skbs[id] = NULL;
  349. if (unlikely(!gnttab_end_foreign_access_ref(
  350. queue->grant_tx_ref[id]))) {
  351. dev_alert(dev,
  352. "Grant still in use by backend domain\n");
  353. goto err;
  354. }
  355. gnttab_release_grant_reference(
  356. &queue->gref_tx_head, queue->grant_tx_ref[id]);
  357. queue->grant_tx_ref[id] = INVALID_GRANT_REF;
  358. queue->grant_tx_page[id] = NULL;
  359. add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
  360. dev_kfree_skb_irq(skb);
  361. }
  362. queue->tx.rsp_cons = prod;
  363. RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
  364. } while (more_to_do);
  365. xennet_maybe_wake_tx(queue);
  366. return work_done;
  367. err:
  368. queue->info->broken = true;
  369. dev_alert(dev, "Disabled for further use\n");
  370. return work_done;
  371. }
  372. struct xennet_gnttab_make_txreq {
  373. struct netfront_queue *queue;
  374. struct sk_buff *skb;
  375. struct page *page;
  376. struct xen_netif_tx_request *tx; /* Last request on ring page */
  377. struct xen_netif_tx_request tx_local; /* Last request local copy*/
  378. unsigned int size;
  379. };
  380. static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
  381. unsigned int len, void *data)
  382. {
  383. struct xennet_gnttab_make_txreq *info = data;
  384. unsigned int id;
  385. struct xen_netif_tx_request *tx;
  386. grant_ref_t ref;
  387. /* convenient aliases */
  388. struct page *page = info->page;
  389. struct netfront_queue *queue = info->queue;
  390. struct sk_buff *skb = info->skb;
  391. id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
  392. tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
  393. ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
  394. WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
  395. gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
  396. gfn, GNTMAP_readonly);
  397. queue->tx_skbs[id] = skb;
  398. queue->grant_tx_page[id] = page;
  399. queue->grant_tx_ref[id] = ref;
  400. info->tx_local.id = id;
  401. info->tx_local.gref = ref;
  402. info->tx_local.offset = offset;
  403. info->tx_local.size = len;
  404. info->tx_local.flags = 0;
  405. *tx = info->tx_local;
  406. /*
  407. * Put the request in the pending queue, it will be set to be pending
  408. * when the producer index is about to be raised.
  409. */
  410. add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id);
  411. info->tx = tx;
  412. info->size += info->tx_local.size;
  413. }
  414. static struct xen_netif_tx_request *xennet_make_first_txreq(
  415. struct xennet_gnttab_make_txreq *info,
  416. unsigned int offset, unsigned int len)
  417. {
  418. info->size = 0;
  419. gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
  420. return info->tx;
  421. }
  422. static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
  423. unsigned int len, void *data)
  424. {
  425. struct xennet_gnttab_make_txreq *info = data;
  426. info->tx->flags |= XEN_NETTXF_more_data;
  427. skb_get(info->skb);
  428. xennet_tx_setup_grant(gfn, offset, len, data);
  429. }
  430. static void xennet_make_txreqs(
  431. struct xennet_gnttab_make_txreq *info,
  432. struct page *page,
  433. unsigned int offset, unsigned int len)
  434. {
  435. /* Skip unused frames from start of page */
  436. page += offset >> PAGE_SHIFT;
  437. offset &= ~PAGE_MASK;
  438. while (len) {
  439. info->page = page;
  440. info->size = 0;
  441. gnttab_foreach_grant_in_range(page, offset, len,
  442. xennet_make_one_txreq,
  443. info);
  444. page++;
  445. offset = 0;
  446. len -= info->size;
  447. }
  448. }
  449. /*
  450. * Count how many ring slots are required to send this skb. Each frag
  451. * might be a compound page.
  452. */
  453. static int xennet_count_skb_slots(struct sk_buff *skb)
  454. {
  455. int i, frags = skb_shinfo(skb)->nr_frags;
  456. int slots;
  457. slots = gnttab_count_grant(offset_in_page(skb->data),
  458. skb_headlen(skb));
  459. for (i = 0; i < frags; i++) {
  460. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  461. unsigned long size = skb_frag_size(frag);
  462. unsigned long offset = skb_frag_off(frag);
  463. /* Skip unused frames from start of page */
  464. offset &= ~PAGE_MASK;
  465. slots += gnttab_count_grant(offset, size);
  466. }
  467. return slots;
  468. }
  469. static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
  470. struct net_device *sb_dev)
  471. {
  472. unsigned int num_queues = dev->real_num_tx_queues;
  473. u32 hash;
  474. u16 queue_idx;
  475. /* First, check if there is only one queue */
  476. if (num_queues == 1) {
  477. queue_idx = 0;
  478. } else {
  479. hash = skb_get_hash(skb);
  480. queue_idx = hash % num_queues;
  481. }
  482. return queue_idx;
  483. }
  484. static void xennet_mark_tx_pending(struct netfront_queue *queue)
  485. {
  486. unsigned int i;
  487. while ((i = get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) !=
  488. TX_LINK_NONE)
  489. queue->tx_link[i] = TX_PENDING;
  490. }
  491. static int xennet_xdp_xmit_one(struct net_device *dev,
  492. struct netfront_queue *queue,
  493. struct xdp_frame *xdpf)
  494. {
  495. struct netfront_info *np = netdev_priv(dev);
  496. struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
  497. struct xennet_gnttab_make_txreq info = {
  498. .queue = queue,
  499. .skb = NULL,
  500. .page = virt_to_page(xdpf->data),
  501. };
  502. int notify;
  503. xennet_make_first_txreq(&info,
  504. offset_in_page(xdpf->data),
  505. xdpf->len);
  506. xennet_mark_tx_pending(queue);
  507. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
  508. if (notify)
  509. notify_remote_via_irq(queue->tx_irq);
  510. u64_stats_update_begin(&tx_stats->syncp);
  511. tx_stats->bytes += xdpf->len;
  512. tx_stats->packets++;
  513. u64_stats_update_end(&tx_stats->syncp);
  514. return 0;
  515. }
  516. static int xennet_xdp_xmit(struct net_device *dev, int n,
  517. struct xdp_frame **frames, u32 flags)
  518. {
  519. unsigned int num_queues = dev->real_num_tx_queues;
  520. struct netfront_info *np = netdev_priv(dev);
  521. struct netfront_queue *queue = NULL;
  522. unsigned long irq_flags;
  523. int nxmit = 0;
  524. int i;
  525. if (unlikely(np->broken))
  526. return -ENODEV;
  527. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  528. return -EINVAL;
  529. queue = &np->queues[smp_processor_id() % num_queues];
  530. spin_lock_irqsave(&queue->tx_lock, irq_flags);
  531. for (i = 0; i < n; i++) {
  532. struct xdp_frame *xdpf = frames[i];
  533. if (!xdpf)
  534. continue;
  535. if (xennet_xdp_xmit_one(dev, queue, xdpf))
  536. break;
  537. nxmit++;
  538. }
  539. spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
  540. return nxmit;
  541. }
  542. static struct sk_buff *bounce_skb(const struct sk_buff *skb)
  543. {
  544. unsigned int headerlen = skb_headroom(skb);
  545. /* Align size to allocate full pages and avoid contiguous data leaks */
  546. unsigned int size = ALIGN(skb_end_offset(skb) + skb->data_len,
  547. XEN_PAGE_SIZE);
  548. struct sk_buff *n = alloc_skb(size, GFP_ATOMIC | __GFP_ZERO);
  549. if (!n)
  550. return NULL;
  551. if (!IS_ALIGNED((uintptr_t)n->head, XEN_PAGE_SIZE)) {
  552. WARN_ONCE(1, "misaligned skb allocated\n");
  553. kfree_skb(n);
  554. return NULL;
  555. }
  556. /* Set the data pointer */
  557. skb_reserve(n, headerlen);
  558. /* Set the tail pointer and length */
  559. skb_put(n, skb->len);
  560. BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
  561. skb_copy_header(n, skb);
  562. return n;
  563. }
  564. #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
  565. static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  566. {
  567. struct netfront_info *np = netdev_priv(dev);
  568. struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
  569. struct xen_netif_tx_request *first_tx;
  570. unsigned int i;
  571. int notify;
  572. int slots;
  573. struct page *page;
  574. unsigned int offset;
  575. unsigned int len;
  576. unsigned long flags;
  577. struct netfront_queue *queue = NULL;
  578. struct xennet_gnttab_make_txreq info = { };
  579. unsigned int num_queues = dev->real_num_tx_queues;
  580. u16 queue_index;
  581. struct sk_buff *nskb;
  582. /* Drop the packet if no queues are set up */
  583. if (num_queues < 1)
  584. goto drop;
  585. if (unlikely(np->broken))
  586. goto drop;
  587. /* Determine which queue to transmit this SKB on */
  588. queue_index = skb_get_queue_mapping(skb);
  589. queue = &np->queues[queue_index];
  590. /* If skb->len is too big for wire format, drop skb and alert
  591. * user about misconfiguration.
  592. */
  593. if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
  594. net_alert_ratelimited(
  595. "xennet: skb->len = %u, too big for wire format\n",
  596. skb->len);
  597. goto drop;
  598. }
  599. slots = xennet_count_skb_slots(skb);
  600. if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
  601. net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
  602. slots, skb->len);
  603. if (skb_linearize(skb))
  604. goto drop;
  605. }
  606. page = virt_to_page(skb->data);
  607. offset = offset_in_page(skb->data);
  608. /* The first req should be at least ETH_HLEN size or the packet will be
  609. * dropped by netback.
  610. *
  611. * If the backend is not trusted bounce all data to zeroed pages to
  612. * avoid exposing contiguous data on the granted page not belonging to
  613. * the skb.
  614. */
  615. if (np->bounce || unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
  616. nskb = bounce_skb(skb);
  617. if (!nskb)
  618. goto drop;
  619. dev_consume_skb_any(skb);
  620. skb = nskb;
  621. page = virt_to_page(skb->data);
  622. offset = offset_in_page(skb->data);
  623. }
  624. len = skb_headlen(skb);
  625. spin_lock_irqsave(&queue->tx_lock, flags);
  626. if (unlikely(!netif_carrier_ok(dev) ||
  627. (slots > 1 && !xennet_can_sg(dev)) ||
  628. netif_needs_gso(skb, netif_skb_features(skb)))) {
  629. spin_unlock_irqrestore(&queue->tx_lock, flags);
  630. goto drop;
  631. }
  632. /* First request for the linear area. */
  633. info.queue = queue;
  634. info.skb = skb;
  635. info.page = page;
  636. first_tx = xennet_make_first_txreq(&info, offset, len);
  637. offset += info.tx_local.size;
  638. if (offset == PAGE_SIZE) {
  639. page++;
  640. offset = 0;
  641. }
  642. len -= info.tx_local.size;
  643. if (skb->ip_summed == CHECKSUM_PARTIAL)
  644. /* local packet? */
  645. first_tx->flags |= XEN_NETTXF_csum_blank |
  646. XEN_NETTXF_data_validated;
  647. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  648. /* remote but checksummed. */
  649. first_tx->flags |= XEN_NETTXF_data_validated;
  650. /* Optional extra info after the first request. */
  651. if (skb_shinfo(skb)->gso_size) {
  652. struct xen_netif_extra_info *gso;
  653. gso = (struct xen_netif_extra_info *)
  654. RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
  655. first_tx->flags |= XEN_NETTXF_extra_info;
  656. gso->u.gso.size = skb_shinfo(skb)->gso_size;
  657. gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
  658. XEN_NETIF_GSO_TYPE_TCPV6 :
  659. XEN_NETIF_GSO_TYPE_TCPV4;
  660. gso->u.gso.pad = 0;
  661. gso->u.gso.features = 0;
  662. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  663. gso->flags = 0;
  664. }
  665. /* Requests for the rest of the linear area. */
  666. xennet_make_txreqs(&info, page, offset, len);
  667. /* Requests for all the frags. */
  668. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  669. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  670. xennet_make_txreqs(&info, skb_frag_page(frag),
  671. skb_frag_off(frag),
  672. skb_frag_size(frag));
  673. }
  674. /* First request has the packet length. */
  675. first_tx->size = skb->len;
  676. /* timestamp packet in software */
  677. skb_tx_timestamp(skb);
  678. xennet_mark_tx_pending(queue);
  679. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
  680. if (notify)
  681. notify_remote_via_irq(queue->tx_irq);
  682. u64_stats_update_begin(&tx_stats->syncp);
  683. tx_stats->bytes += skb->len;
  684. tx_stats->packets++;
  685. u64_stats_update_end(&tx_stats->syncp);
  686. if (!netfront_tx_slot_available(queue))
  687. netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
  688. spin_unlock_irqrestore(&queue->tx_lock, flags);
  689. return NETDEV_TX_OK;
  690. drop:
  691. dev->stats.tx_dropped++;
  692. dev_kfree_skb_any(skb);
  693. return NETDEV_TX_OK;
  694. }
  695. static int xennet_close(struct net_device *dev)
  696. {
  697. struct netfront_info *np = netdev_priv(dev);
  698. unsigned int num_queues = np->queues ? dev->real_num_tx_queues : 0;
  699. unsigned int i;
  700. struct netfront_queue *queue;
  701. netif_tx_stop_all_queues(np->netdev);
  702. for (i = 0; i < num_queues; ++i) {
  703. queue = &np->queues[i];
  704. napi_disable(&queue->napi);
  705. }
  706. return 0;
  707. }
  708. static void xennet_destroy_queues(struct netfront_info *info)
  709. {
  710. unsigned int i;
  711. if (!info->queues)
  712. return;
  713. for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
  714. struct netfront_queue *queue = &info->queues[i];
  715. if (netif_running(info->netdev))
  716. napi_disable(&queue->napi);
  717. netif_napi_del(&queue->napi);
  718. }
  719. kfree(info->queues);
  720. info->queues = NULL;
  721. }
  722. static void xennet_uninit(struct net_device *dev)
  723. {
  724. struct netfront_info *np = netdev_priv(dev);
  725. xennet_destroy_queues(np);
  726. }
  727. static void xennet_set_rx_rsp_cons(struct netfront_queue *queue, RING_IDX val)
  728. {
  729. unsigned long flags;
  730. spin_lock_irqsave(&queue->rx_cons_lock, flags);
  731. queue->rx.rsp_cons = val;
  732. queue->rx_rsp_unconsumed = XEN_RING_NR_UNCONSUMED_RESPONSES(&queue->rx);
  733. spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
  734. }
  735. static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
  736. grant_ref_t ref)
  737. {
  738. int new = xennet_rxidx(queue->rx.req_prod_pvt);
  739. BUG_ON(queue->rx_skbs[new]);
  740. queue->rx_skbs[new] = skb;
  741. queue->grant_rx_ref[new] = ref;
  742. RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
  743. RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
  744. queue->rx.req_prod_pvt++;
  745. }
  746. static int xennet_get_extras(struct netfront_queue *queue,
  747. struct xen_netif_extra_info *extras,
  748. RING_IDX rp)
  749. {
  750. struct xen_netif_extra_info extra;
  751. struct device *dev = &queue->info->netdev->dev;
  752. RING_IDX cons = queue->rx.rsp_cons;
  753. int err = 0;
  754. do {
  755. struct sk_buff *skb;
  756. grant_ref_t ref;
  757. if (unlikely(cons + 1 == rp)) {
  758. if (net_ratelimit())
  759. dev_warn(dev, "Missing extra info\n");
  760. err = -EBADR;
  761. break;
  762. }
  763. RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
  764. if (unlikely(!extra.type ||
  765. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  766. if (net_ratelimit())
  767. dev_warn(dev, "Invalid extra type: %d\n",
  768. extra.type);
  769. err = -EINVAL;
  770. } else {
  771. extras[extra.type - 1] = extra;
  772. }
  773. skb = xennet_get_rx_skb(queue, cons);
  774. ref = xennet_get_rx_ref(queue, cons);
  775. xennet_move_rx_slot(queue, skb, ref);
  776. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  777. xennet_set_rx_rsp_cons(queue, cons);
  778. return err;
  779. }
  780. static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
  781. struct xen_netif_rx_response *rx, struct bpf_prog *prog,
  782. struct xdp_buff *xdp, bool *need_xdp_flush)
  783. {
  784. struct xdp_frame *xdpf;
  785. u32 len = rx->status;
  786. u32 act;
  787. int err;
  788. xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
  789. &queue->xdp_rxq);
  790. xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
  791. len, false);
  792. act = bpf_prog_run_xdp(prog, xdp);
  793. switch (act) {
  794. case XDP_TX:
  795. xdpf = xdp_convert_buff_to_frame(xdp);
  796. if (unlikely(!xdpf)) {
  797. trace_xdp_exception(queue->info->netdev, prog, act);
  798. break;
  799. }
  800. get_page(pdata);
  801. err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
  802. if (unlikely(err <= 0)) {
  803. if (err < 0)
  804. trace_xdp_exception(queue->info->netdev, prog, act);
  805. xdp_return_frame_rx_napi(xdpf);
  806. }
  807. break;
  808. case XDP_REDIRECT:
  809. get_page(pdata);
  810. err = xdp_do_redirect(queue->info->netdev, xdp, prog);
  811. *need_xdp_flush = true;
  812. if (unlikely(err)) {
  813. trace_xdp_exception(queue->info->netdev, prog, act);
  814. xdp_return_buff(xdp);
  815. }
  816. break;
  817. case XDP_PASS:
  818. case XDP_DROP:
  819. break;
  820. case XDP_ABORTED:
  821. trace_xdp_exception(queue->info->netdev, prog, act);
  822. break;
  823. default:
  824. bpf_warn_invalid_xdp_action(queue->info->netdev, prog, act);
  825. }
  826. return act;
  827. }
  828. static int xennet_get_responses(struct netfront_queue *queue,
  829. struct netfront_rx_info *rinfo, RING_IDX rp,
  830. struct sk_buff_head *list,
  831. bool *need_xdp_flush)
  832. {
  833. struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
  834. int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
  835. RING_IDX cons = queue->rx.rsp_cons;
  836. struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
  837. struct xen_netif_extra_info *extras = rinfo->extras;
  838. grant_ref_t ref = xennet_get_rx_ref(queue, cons);
  839. struct device *dev = &queue->info->netdev->dev;
  840. struct bpf_prog *xdp_prog;
  841. struct xdp_buff xdp;
  842. int slots = 1;
  843. int err = 0;
  844. u32 verdict;
  845. if (rx->flags & XEN_NETRXF_extra_info) {
  846. err = xennet_get_extras(queue, extras, rp);
  847. if (!err) {
  848. if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
  849. struct xen_netif_extra_info *xdp;
  850. xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
  851. rx->offset = xdp->u.xdp.headroom;
  852. }
  853. }
  854. cons = queue->rx.rsp_cons;
  855. }
  856. for (;;) {
  857. /*
  858. * This definitely indicates a bug, either in this driver or in
  859. * the backend driver. In future this should flag the bad
  860. * situation to the system controller to reboot the backend.
  861. */
  862. if (ref == INVALID_GRANT_REF) {
  863. if (net_ratelimit())
  864. dev_warn(dev, "Bad rx response id %d.\n",
  865. rx->id);
  866. err = -EINVAL;
  867. goto next;
  868. }
  869. if (unlikely(rx->status < 0 ||
  870. rx->offset + rx->status > XEN_PAGE_SIZE)) {
  871. if (net_ratelimit())
  872. dev_warn(dev, "rx->offset: %u, size: %d\n",
  873. rx->offset, rx->status);
  874. xennet_move_rx_slot(queue, skb, ref);
  875. err = -EINVAL;
  876. goto next;
  877. }
  878. if (!gnttab_end_foreign_access_ref(ref)) {
  879. dev_alert(dev,
  880. "Grant still in use by backend domain\n");
  881. queue->info->broken = true;
  882. dev_alert(dev, "Disabled for further use\n");
  883. return -EINVAL;
  884. }
  885. gnttab_release_grant_reference(&queue->gref_rx_head, ref);
  886. rcu_read_lock();
  887. xdp_prog = rcu_dereference(queue->xdp_prog);
  888. if (xdp_prog) {
  889. if (!(rx->flags & XEN_NETRXF_more_data)) {
  890. /* currently only a single page contains data */
  891. verdict = xennet_run_xdp(queue,
  892. skb_frag_page(&skb_shinfo(skb)->frags[0]),
  893. rx, xdp_prog, &xdp, need_xdp_flush);
  894. if (verdict != XDP_PASS)
  895. err = -EINVAL;
  896. } else {
  897. /* drop the frame */
  898. err = -EINVAL;
  899. }
  900. }
  901. rcu_read_unlock();
  902. __skb_queue_tail(list, skb);
  903. next:
  904. if (!(rx->flags & XEN_NETRXF_more_data))
  905. break;
  906. if (cons + slots == rp) {
  907. if (net_ratelimit())
  908. dev_warn(dev, "Need more slots\n");
  909. err = -ENOENT;
  910. break;
  911. }
  912. RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
  913. rx = &rx_local;
  914. skb = xennet_get_rx_skb(queue, cons + slots);
  915. ref = xennet_get_rx_ref(queue, cons + slots);
  916. slots++;
  917. }
  918. if (unlikely(slots > max)) {
  919. if (net_ratelimit())
  920. dev_warn(dev, "Too many slots\n");
  921. err = -E2BIG;
  922. }
  923. if (unlikely(err))
  924. xennet_set_rx_rsp_cons(queue, cons + slots);
  925. return err;
  926. }
  927. static int xennet_set_skb_gso(struct sk_buff *skb,
  928. struct xen_netif_extra_info *gso)
  929. {
  930. if (!gso->u.gso.size) {
  931. if (net_ratelimit())
  932. pr_warn("GSO size must not be zero\n");
  933. return -EINVAL;
  934. }
  935. if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
  936. gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
  937. if (net_ratelimit())
  938. pr_warn("Bad GSO type %d\n", gso->u.gso.type);
  939. return -EINVAL;
  940. }
  941. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  942. skb_shinfo(skb)->gso_type =
  943. (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
  944. SKB_GSO_TCPV4 :
  945. SKB_GSO_TCPV6;
  946. /* Header must be checked, and gso_segs computed. */
  947. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  948. skb_shinfo(skb)->gso_segs = 0;
  949. return 0;
  950. }
  951. static int xennet_fill_frags(struct netfront_queue *queue,
  952. struct sk_buff *skb,
  953. struct sk_buff_head *list)
  954. {
  955. RING_IDX cons = queue->rx.rsp_cons;
  956. struct sk_buff *nskb;
  957. while ((nskb = __skb_dequeue(list))) {
  958. struct xen_netif_rx_response rx;
  959. skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
  960. RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
  961. if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
  962. unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  963. BUG_ON(pull_to < skb_headlen(skb));
  964. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  965. }
  966. if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
  967. xennet_set_rx_rsp_cons(queue,
  968. ++cons + skb_queue_len(list));
  969. kfree_skb(nskb);
  970. return -ENOENT;
  971. }
  972. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  973. skb_frag_page(nfrag),
  974. rx.offset, rx.status, PAGE_SIZE);
  975. skb_shinfo(nskb)->nr_frags = 0;
  976. kfree_skb(nskb);
  977. }
  978. xennet_set_rx_rsp_cons(queue, cons);
  979. return 0;
  980. }
  981. static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
  982. {
  983. bool recalculate_partial_csum = false;
  984. /*
  985. * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  986. * peers can fail to set NETRXF_csum_blank when sending a GSO
  987. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  988. * recalculate the partial checksum.
  989. */
  990. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  991. struct netfront_info *np = netdev_priv(dev);
  992. atomic_inc(&np->rx_gso_checksum_fixup);
  993. skb->ip_summed = CHECKSUM_PARTIAL;
  994. recalculate_partial_csum = true;
  995. }
  996. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  997. if (skb->ip_summed != CHECKSUM_PARTIAL)
  998. return 0;
  999. return skb_checksum_setup(skb, recalculate_partial_csum);
  1000. }
  1001. static int handle_incoming_queue(struct netfront_queue *queue,
  1002. struct sk_buff_head *rxq)
  1003. {
  1004. struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
  1005. int packets_dropped = 0;
  1006. struct sk_buff *skb;
  1007. while ((skb = __skb_dequeue(rxq)) != NULL) {
  1008. int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  1009. if (pull_to > skb_headlen(skb))
  1010. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  1011. /* Ethernet work: Delayed to here as it peeks the header. */
  1012. skb->protocol = eth_type_trans(skb, queue->info->netdev);
  1013. skb_reset_network_header(skb);
  1014. if (checksum_setup(queue->info->netdev, skb)) {
  1015. kfree_skb(skb);
  1016. packets_dropped++;
  1017. queue->info->netdev->stats.rx_errors++;
  1018. continue;
  1019. }
  1020. u64_stats_update_begin(&rx_stats->syncp);
  1021. rx_stats->packets++;
  1022. rx_stats->bytes += skb->len;
  1023. u64_stats_update_end(&rx_stats->syncp);
  1024. /* Pass it up. */
  1025. napi_gro_receive(&queue->napi, skb);
  1026. }
  1027. return packets_dropped;
  1028. }
  1029. static int xennet_poll(struct napi_struct *napi, int budget)
  1030. {
  1031. struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
  1032. struct net_device *dev = queue->info->netdev;
  1033. struct sk_buff *skb;
  1034. struct netfront_rx_info rinfo;
  1035. struct xen_netif_rx_response *rx = &rinfo.rx;
  1036. struct xen_netif_extra_info *extras = rinfo.extras;
  1037. RING_IDX i, rp;
  1038. int work_done;
  1039. struct sk_buff_head rxq;
  1040. struct sk_buff_head errq;
  1041. struct sk_buff_head tmpq;
  1042. int err;
  1043. bool need_xdp_flush = false;
  1044. spin_lock(&queue->rx_lock);
  1045. skb_queue_head_init(&rxq);
  1046. skb_queue_head_init(&errq);
  1047. skb_queue_head_init(&tmpq);
  1048. rp = queue->rx.sring->rsp_prod;
  1049. if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) {
  1050. dev_alert(&dev->dev, "Illegal number of responses %u\n",
  1051. rp - queue->rx.rsp_cons);
  1052. queue->info->broken = true;
  1053. spin_unlock(&queue->rx_lock);
  1054. return 0;
  1055. }
  1056. rmb(); /* Ensure we see queued responses up to 'rp'. */
  1057. i = queue->rx.rsp_cons;
  1058. work_done = 0;
  1059. while ((i != rp) && (work_done < budget)) {
  1060. RING_COPY_RESPONSE(&queue->rx, i, rx);
  1061. memset(extras, 0, sizeof(rinfo.extras));
  1062. err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
  1063. &need_xdp_flush);
  1064. if (unlikely(err)) {
  1065. if (queue->info->broken) {
  1066. spin_unlock(&queue->rx_lock);
  1067. return 0;
  1068. }
  1069. err:
  1070. while ((skb = __skb_dequeue(&tmpq)))
  1071. __skb_queue_tail(&errq, skb);
  1072. dev->stats.rx_errors++;
  1073. i = queue->rx.rsp_cons;
  1074. continue;
  1075. }
  1076. skb = __skb_dequeue(&tmpq);
  1077. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  1078. struct xen_netif_extra_info *gso;
  1079. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  1080. if (unlikely(xennet_set_skb_gso(skb, gso))) {
  1081. __skb_queue_head(&tmpq, skb);
  1082. xennet_set_rx_rsp_cons(queue,
  1083. queue->rx.rsp_cons +
  1084. skb_queue_len(&tmpq));
  1085. goto err;
  1086. }
  1087. }
  1088. NETFRONT_SKB_CB(skb)->pull_to = rx->status;
  1089. if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
  1090. NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
  1091. skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
  1092. skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
  1093. skb->data_len = rx->status;
  1094. skb->len += rx->status;
  1095. if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
  1096. goto err;
  1097. if (rx->flags & XEN_NETRXF_csum_blank)
  1098. skb->ip_summed = CHECKSUM_PARTIAL;
  1099. else if (rx->flags & XEN_NETRXF_data_validated)
  1100. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1101. __skb_queue_tail(&rxq, skb);
  1102. i = queue->rx.rsp_cons + 1;
  1103. xennet_set_rx_rsp_cons(queue, i);
  1104. work_done++;
  1105. }
  1106. if (need_xdp_flush)
  1107. xdp_do_flush();
  1108. __skb_queue_purge(&errq);
  1109. work_done -= handle_incoming_queue(queue, &rxq);
  1110. xennet_alloc_rx_buffers(queue);
  1111. if (work_done < budget) {
  1112. int more_to_do = 0;
  1113. napi_complete_done(napi, work_done);
  1114. RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
  1115. if (more_to_do)
  1116. napi_schedule(napi);
  1117. }
  1118. spin_unlock(&queue->rx_lock);
  1119. return work_done;
  1120. }
  1121. static int xennet_change_mtu(struct net_device *dev, int mtu)
  1122. {
  1123. int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
  1124. if (mtu > max)
  1125. return -EINVAL;
  1126. WRITE_ONCE(dev->mtu, mtu);
  1127. return 0;
  1128. }
  1129. static void xennet_get_stats64(struct net_device *dev,
  1130. struct rtnl_link_stats64 *tot)
  1131. {
  1132. struct netfront_info *np = netdev_priv(dev);
  1133. int cpu;
  1134. for_each_possible_cpu(cpu) {
  1135. struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
  1136. struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
  1137. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  1138. unsigned int start;
  1139. do {
  1140. start = u64_stats_fetch_begin(&tx_stats->syncp);
  1141. tx_packets = tx_stats->packets;
  1142. tx_bytes = tx_stats->bytes;
  1143. } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
  1144. do {
  1145. start = u64_stats_fetch_begin(&rx_stats->syncp);
  1146. rx_packets = rx_stats->packets;
  1147. rx_bytes = rx_stats->bytes;
  1148. } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
  1149. tot->rx_packets += rx_packets;
  1150. tot->tx_packets += tx_packets;
  1151. tot->rx_bytes += rx_bytes;
  1152. tot->tx_bytes += tx_bytes;
  1153. }
  1154. tot->rx_errors = dev->stats.rx_errors;
  1155. tot->tx_dropped = dev->stats.tx_dropped;
  1156. }
  1157. static void xennet_release_tx_bufs(struct netfront_queue *queue)
  1158. {
  1159. struct sk_buff *skb;
  1160. int i;
  1161. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  1162. /* Skip over entries which are actually freelist references */
  1163. if (!queue->tx_skbs[i])
  1164. continue;
  1165. skb = queue->tx_skbs[i];
  1166. queue->tx_skbs[i] = NULL;
  1167. get_page(queue->grant_tx_page[i]);
  1168. gnttab_end_foreign_access(queue->grant_tx_ref[i],
  1169. queue->grant_tx_page[i]);
  1170. queue->grant_tx_page[i] = NULL;
  1171. queue->grant_tx_ref[i] = INVALID_GRANT_REF;
  1172. add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
  1173. dev_kfree_skb_irq(skb);
  1174. }
  1175. }
  1176. static void xennet_release_rx_bufs(struct netfront_queue *queue)
  1177. {
  1178. int id, ref;
  1179. spin_lock_bh(&queue->rx_lock);
  1180. for (id = 0; id < NET_RX_RING_SIZE; id++) {
  1181. struct sk_buff *skb;
  1182. struct page *page;
  1183. skb = queue->rx_skbs[id];
  1184. if (!skb)
  1185. continue;
  1186. ref = queue->grant_rx_ref[id];
  1187. if (ref == INVALID_GRANT_REF)
  1188. continue;
  1189. page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
  1190. /* gnttab_end_foreign_access() needs a page ref until
  1191. * foreign access is ended (which may be deferred).
  1192. */
  1193. get_page(page);
  1194. gnttab_end_foreign_access(ref, page);
  1195. queue->grant_rx_ref[id] = INVALID_GRANT_REF;
  1196. kfree_skb(skb);
  1197. }
  1198. spin_unlock_bh(&queue->rx_lock);
  1199. }
  1200. static netdev_features_t xennet_fix_features(struct net_device *dev,
  1201. netdev_features_t features)
  1202. {
  1203. struct netfront_info *np = netdev_priv(dev);
  1204. if (features & NETIF_F_SG &&
  1205. !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
  1206. features &= ~NETIF_F_SG;
  1207. if (features & NETIF_F_IPV6_CSUM &&
  1208. !xenbus_read_unsigned(np->xbdev->otherend,
  1209. "feature-ipv6-csum-offload", 0))
  1210. features &= ~NETIF_F_IPV6_CSUM;
  1211. if (features & NETIF_F_TSO &&
  1212. !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
  1213. features &= ~NETIF_F_TSO;
  1214. if (features & NETIF_F_TSO6 &&
  1215. !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
  1216. features &= ~NETIF_F_TSO6;
  1217. return features;
  1218. }
  1219. static int xennet_set_features(struct net_device *dev,
  1220. netdev_features_t features)
  1221. {
  1222. if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
  1223. netdev_info(dev, "Reducing MTU because no SG offload");
  1224. dev->mtu = ETH_DATA_LEN;
  1225. }
  1226. return 0;
  1227. }
  1228. static bool xennet_handle_tx(struct netfront_queue *queue, unsigned int *eoi)
  1229. {
  1230. unsigned long flags;
  1231. if (unlikely(queue->info->broken))
  1232. return false;
  1233. spin_lock_irqsave(&queue->tx_lock, flags);
  1234. if (xennet_tx_buf_gc(queue))
  1235. *eoi = 0;
  1236. spin_unlock_irqrestore(&queue->tx_lock, flags);
  1237. return true;
  1238. }
  1239. static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
  1240. {
  1241. unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
  1242. if (likely(xennet_handle_tx(dev_id, &eoiflag)))
  1243. xen_irq_lateeoi(irq, eoiflag);
  1244. return IRQ_HANDLED;
  1245. }
  1246. static bool xennet_handle_rx(struct netfront_queue *queue, unsigned int *eoi)
  1247. {
  1248. unsigned int work_queued;
  1249. unsigned long flags;
  1250. if (unlikely(queue->info->broken))
  1251. return false;
  1252. spin_lock_irqsave(&queue->rx_cons_lock, flags);
  1253. work_queued = XEN_RING_NR_UNCONSUMED_RESPONSES(&queue->rx);
  1254. if (work_queued > queue->rx_rsp_unconsumed) {
  1255. queue->rx_rsp_unconsumed = work_queued;
  1256. *eoi = 0;
  1257. } else if (unlikely(work_queued < queue->rx_rsp_unconsumed)) {
  1258. const struct device *dev = &queue->info->netdev->dev;
  1259. spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
  1260. dev_alert(dev, "RX producer index going backwards\n");
  1261. dev_alert(dev, "Disabled for further use\n");
  1262. queue->info->broken = true;
  1263. return false;
  1264. }
  1265. spin_unlock_irqrestore(&queue->rx_cons_lock, flags);
  1266. if (likely(netif_carrier_ok(queue->info->netdev) && work_queued))
  1267. napi_schedule(&queue->napi);
  1268. return true;
  1269. }
  1270. static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
  1271. {
  1272. unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
  1273. if (likely(xennet_handle_rx(dev_id, &eoiflag)))
  1274. xen_irq_lateeoi(irq, eoiflag);
  1275. return IRQ_HANDLED;
  1276. }
  1277. static irqreturn_t xennet_interrupt(int irq, void *dev_id)
  1278. {
  1279. unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
  1280. if (xennet_handle_tx(dev_id, &eoiflag) &&
  1281. xennet_handle_rx(dev_id, &eoiflag))
  1282. xen_irq_lateeoi(irq, eoiflag);
  1283. return IRQ_HANDLED;
  1284. }
  1285. #ifdef CONFIG_NET_POLL_CONTROLLER
  1286. static void xennet_poll_controller(struct net_device *dev)
  1287. {
  1288. /* Poll each queue */
  1289. struct netfront_info *info = netdev_priv(dev);
  1290. unsigned int num_queues = dev->real_num_tx_queues;
  1291. unsigned int i;
  1292. if (info->broken)
  1293. return;
  1294. for (i = 0; i < num_queues; ++i)
  1295. xennet_interrupt(0, &info->queues[i]);
  1296. }
  1297. #endif
  1298. #define NETBACK_XDP_HEADROOM_DISABLE 0
  1299. #define NETBACK_XDP_HEADROOM_ENABLE 1
  1300. static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
  1301. {
  1302. int err;
  1303. unsigned short headroom;
  1304. headroom = xdp ? XDP_PACKET_HEADROOM : 0;
  1305. err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
  1306. "xdp-headroom", "%hu",
  1307. headroom);
  1308. if (err)
  1309. pr_warn("Error writing xdp-headroom\n");
  1310. return err;
  1311. }
  1312. static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  1313. struct netlink_ext_ack *extack)
  1314. {
  1315. unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
  1316. struct netfront_info *np = netdev_priv(dev);
  1317. struct bpf_prog *old_prog;
  1318. unsigned int i, err;
  1319. if (dev->mtu > max_mtu) {
  1320. netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
  1321. return -EINVAL;
  1322. }
  1323. if (!np->netback_has_xdp_headroom)
  1324. return 0;
  1325. xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
  1326. err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
  1327. NETBACK_XDP_HEADROOM_DISABLE);
  1328. if (err)
  1329. return err;
  1330. /* avoid the race with XDP headroom adjustment */
  1331. wait_event(module_wq,
  1332. xenbus_read_driver_state(np->xbdev->otherend) ==
  1333. XenbusStateReconfigured);
  1334. np->netfront_xdp_enabled = true;
  1335. old_prog = rtnl_dereference(np->queues[0].xdp_prog);
  1336. if (prog)
  1337. bpf_prog_add(prog, dev->real_num_tx_queues);
  1338. for (i = 0; i < dev->real_num_tx_queues; ++i)
  1339. rcu_assign_pointer(np->queues[i].xdp_prog, prog);
  1340. if (old_prog)
  1341. for (i = 0; i < dev->real_num_tx_queues; ++i)
  1342. bpf_prog_put(old_prog);
  1343. xenbus_switch_state(np->xbdev, XenbusStateConnected);
  1344. return 0;
  1345. }
  1346. static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  1347. {
  1348. struct netfront_info *np = netdev_priv(dev);
  1349. if (np->broken)
  1350. return -ENODEV;
  1351. switch (xdp->command) {
  1352. case XDP_SETUP_PROG:
  1353. return xennet_xdp_set(dev, xdp->prog, xdp->extack);
  1354. default:
  1355. return -EINVAL;
  1356. }
  1357. }
  1358. static const struct net_device_ops xennet_netdev_ops = {
  1359. .ndo_uninit = xennet_uninit,
  1360. .ndo_open = xennet_open,
  1361. .ndo_stop = xennet_close,
  1362. .ndo_start_xmit = xennet_start_xmit,
  1363. .ndo_change_mtu = xennet_change_mtu,
  1364. .ndo_get_stats64 = xennet_get_stats64,
  1365. .ndo_set_mac_address = eth_mac_addr,
  1366. .ndo_validate_addr = eth_validate_addr,
  1367. .ndo_fix_features = xennet_fix_features,
  1368. .ndo_set_features = xennet_set_features,
  1369. .ndo_select_queue = xennet_select_queue,
  1370. .ndo_bpf = xennet_xdp,
  1371. .ndo_xdp_xmit = xennet_xdp_xmit,
  1372. #ifdef CONFIG_NET_POLL_CONTROLLER
  1373. .ndo_poll_controller = xennet_poll_controller,
  1374. #endif
  1375. };
  1376. static void xennet_free_netdev(struct net_device *netdev)
  1377. {
  1378. struct netfront_info *np = netdev_priv(netdev);
  1379. free_percpu(np->rx_stats);
  1380. free_percpu(np->tx_stats);
  1381. free_netdev(netdev);
  1382. }
  1383. static struct net_device *xennet_create_dev(struct xenbus_device *dev)
  1384. {
  1385. int err;
  1386. struct net_device *netdev;
  1387. struct netfront_info *np;
  1388. netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
  1389. if (!netdev)
  1390. return ERR_PTR(-ENOMEM);
  1391. np = netdev_priv(netdev);
  1392. np->xbdev = dev;
  1393. np->queues = NULL;
  1394. err = -ENOMEM;
  1395. np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
  1396. if (np->rx_stats == NULL)
  1397. goto exit;
  1398. np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
  1399. if (np->tx_stats == NULL)
  1400. goto exit;
  1401. netdev->netdev_ops = &xennet_netdev_ops;
  1402. netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  1403. NETIF_F_GSO_ROBUST;
  1404. netdev->hw_features = NETIF_F_SG |
  1405. NETIF_F_IPV6_CSUM |
  1406. NETIF_F_TSO | NETIF_F_TSO6;
  1407. /*
  1408. * Assume that all hw features are available for now. This set
  1409. * will be adjusted by the call to netdev_update_features() in
  1410. * xennet_connect() which is the earliest point where we can
  1411. * negotiate with the backend regarding supported features.
  1412. */
  1413. netdev->features |= netdev->hw_features;
  1414. netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
  1415. NETDEV_XDP_ACT_NDO_XMIT;
  1416. netdev->ethtool_ops = &xennet_ethtool_ops;
  1417. netdev->min_mtu = ETH_MIN_MTU;
  1418. netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
  1419. SET_NETDEV_DEV(netdev, &dev->dev);
  1420. np->netdev = netdev;
  1421. np->netfront_xdp_enabled = false;
  1422. netif_carrier_off(netdev);
  1423. do {
  1424. xenbus_switch_state(dev, XenbusStateInitialising);
  1425. err = wait_event_timeout(module_wq,
  1426. xenbus_read_driver_state(dev->otherend) !=
  1427. XenbusStateClosed &&
  1428. xenbus_read_driver_state(dev->otherend) !=
  1429. XenbusStateUnknown, XENNET_TIMEOUT);
  1430. } while (!err);
  1431. return netdev;
  1432. exit:
  1433. xennet_free_netdev(netdev);
  1434. return ERR_PTR(err);
  1435. }
  1436. /*
  1437. * Entry point to this code when a new device is created. Allocate the basic
  1438. * structures and the ring buffers for communication with the backend, and
  1439. * inform the backend of the appropriate details for those.
  1440. */
  1441. static int netfront_probe(struct xenbus_device *dev,
  1442. const struct xenbus_device_id *id)
  1443. {
  1444. int err;
  1445. struct net_device *netdev;
  1446. struct netfront_info *info;
  1447. netdev = xennet_create_dev(dev);
  1448. if (IS_ERR(netdev)) {
  1449. err = PTR_ERR(netdev);
  1450. xenbus_dev_fatal(dev, err, "creating netdev");
  1451. return err;
  1452. }
  1453. info = netdev_priv(netdev);
  1454. dev_set_drvdata(&dev->dev, info);
  1455. #ifdef CONFIG_SYSFS
  1456. info->netdev->sysfs_groups[0] = &xennet_dev_group;
  1457. #endif
  1458. return 0;
  1459. }
  1460. static void xennet_end_access(int ref, void *page)
  1461. {
  1462. /* This frees the page as a side-effect */
  1463. if (ref != INVALID_GRANT_REF)
  1464. gnttab_end_foreign_access(ref, virt_to_page(page));
  1465. }
  1466. static void xennet_disconnect_backend(struct netfront_info *info)
  1467. {
  1468. unsigned int i = 0;
  1469. unsigned int num_queues = info->netdev->real_num_tx_queues;
  1470. netif_carrier_off(info->netdev);
  1471. for (i = 0; i < num_queues && info->queues; ++i) {
  1472. struct netfront_queue *queue = &info->queues[i];
  1473. del_timer_sync(&queue->rx_refill_timer);
  1474. if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
  1475. unbind_from_irqhandler(queue->tx_irq, queue);
  1476. if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
  1477. unbind_from_irqhandler(queue->tx_irq, queue);
  1478. unbind_from_irqhandler(queue->rx_irq, queue);
  1479. }
  1480. queue->tx_evtchn = queue->rx_evtchn = 0;
  1481. queue->tx_irq = queue->rx_irq = 0;
  1482. if (netif_running(info->netdev))
  1483. napi_synchronize(&queue->napi);
  1484. xennet_release_tx_bufs(queue);
  1485. xennet_release_rx_bufs(queue);
  1486. gnttab_free_grant_references(queue->gref_tx_head);
  1487. gnttab_free_grant_references(queue->gref_rx_head);
  1488. /* End access and free the pages */
  1489. xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
  1490. xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
  1491. queue->tx_ring_ref = INVALID_GRANT_REF;
  1492. queue->rx_ring_ref = INVALID_GRANT_REF;
  1493. queue->tx.sring = NULL;
  1494. queue->rx.sring = NULL;
  1495. page_pool_destroy(queue->page_pool);
  1496. }
  1497. }
  1498. /*
  1499. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1500. * driver restart. We tear down our netif structure and recreate it, but
  1501. * leave the device-layer structures intact so that this is transparent to the
  1502. * rest of the kernel.
  1503. */
  1504. static int netfront_resume(struct xenbus_device *dev)
  1505. {
  1506. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1507. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1508. netif_tx_lock_bh(info->netdev);
  1509. netif_device_detach(info->netdev);
  1510. netif_tx_unlock_bh(info->netdev);
  1511. xennet_disconnect_backend(info);
  1512. rtnl_lock();
  1513. if (info->queues)
  1514. xennet_destroy_queues(info);
  1515. rtnl_unlock();
  1516. return 0;
  1517. }
  1518. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  1519. {
  1520. char *s, *e, *macstr;
  1521. int i;
  1522. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  1523. if (IS_ERR(macstr))
  1524. return PTR_ERR(macstr);
  1525. for (i = 0; i < ETH_ALEN; i++) {
  1526. mac[i] = simple_strtoul(s, &e, 16);
  1527. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  1528. kfree(macstr);
  1529. return -ENOENT;
  1530. }
  1531. s = e+1;
  1532. }
  1533. kfree(macstr);
  1534. return 0;
  1535. }
  1536. static int setup_netfront_single(struct netfront_queue *queue)
  1537. {
  1538. int err;
  1539. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1540. if (err < 0)
  1541. goto fail;
  1542. err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
  1543. xennet_interrupt, 0,
  1544. queue->info->netdev->name,
  1545. queue);
  1546. if (err < 0)
  1547. goto bind_fail;
  1548. queue->rx_evtchn = queue->tx_evtchn;
  1549. queue->rx_irq = queue->tx_irq = err;
  1550. return 0;
  1551. bind_fail:
  1552. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1553. queue->tx_evtchn = 0;
  1554. fail:
  1555. return err;
  1556. }
  1557. static int setup_netfront_split(struct netfront_queue *queue)
  1558. {
  1559. int err;
  1560. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
  1561. if (err < 0)
  1562. goto fail;
  1563. err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
  1564. if (err < 0)
  1565. goto alloc_rx_evtchn_fail;
  1566. snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
  1567. "%s-tx", queue->name);
  1568. err = bind_evtchn_to_irqhandler_lateeoi(queue->tx_evtchn,
  1569. xennet_tx_interrupt, 0,
  1570. queue->tx_irq_name, queue);
  1571. if (err < 0)
  1572. goto bind_tx_fail;
  1573. queue->tx_irq = err;
  1574. snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
  1575. "%s-rx", queue->name);
  1576. err = bind_evtchn_to_irqhandler_lateeoi(queue->rx_evtchn,
  1577. xennet_rx_interrupt, 0,
  1578. queue->rx_irq_name, queue);
  1579. if (err < 0)
  1580. goto bind_rx_fail;
  1581. queue->rx_irq = err;
  1582. return 0;
  1583. bind_rx_fail:
  1584. unbind_from_irqhandler(queue->tx_irq, queue);
  1585. queue->tx_irq = 0;
  1586. bind_tx_fail:
  1587. xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
  1588. queue->rx_evtchn = 0;
  1589. alloc_rx_evtchn_fail:
  1590. xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
  1591. queue->tx_evtchn = 0;
  1592. fail:
  1593. return err;
  1594. }
  1595. static int setup_netfront(struct xenbus_device *dev,
  1596. struct netfront_queue *queue, unsigned int feature_split_evtchn)
  1597. {
  1598. struct xen_netif_tx_sring *txs;
  1599. struct xen_netif_rx_sring *rxs;
  1600. int err;
  1601. queue->tx_ring_ref = INVALID_GRANT_REF;
  1602. queue->rx_ring_ref = INVALID_GRANT_REF;
  1603. queue->rx.sring = NULL;
  1604. queue->tx.sring = NULL;
  1605. err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH, (void **)&txs,
  1606. 1, &queue->tx_ring_ref);
  1607. if (err)
  1608. goto fail;
  1609. XEN_FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
  1610. err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH, (void **)&rxs,
  1611. 1, &queue->rx_ring_ref);
  1612. if (err)
  1613. goto fail;
  1614. XEN_FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
  1615. if (feature_split_evtchn)
  1616. err = setup_netfront_split(queue);
  1617. /* setup single event channel if
  1618. * a) feature-split-event-channels == 0
  1619. * b) feature-split-event-channels == 1 but failed to setup
  1620. */
  1621. if (!feature_split_evtchn || err)
  1622. err = setup_netfront_single(queue);
  1623. if (err)
  1624. goto fail;
  1625. return 0;
  1626. fail:
  1627. xenbus_teardown_ring((void **)&queue->rx.sring, 1, &queue->rx_ring_ref);
  1628. xenbus_teardown_ring((void **)&queue->tx.sring, 1, &queue->tx_ring_ref);
  1629. return err;
  1630. }
  1631. /* Queue-specific initialisation
  1632. * This used to be done in xennet_create_dev() but must now
  1633. * be run per-queue.
  1634. */
  1635. static int xennet_init_queue(struct netfront_queue *queue)
  1636. {
  1637. unsigned short i;
  1638. int err = 0;
  1639. char *devid;
  1640. spin_lock_init(&queue->tx_lock);
  1641. spin_lock_init(&queue->rx_lock);
  1642. spin_lock_init(&queue->rx_cons_lock);
  1643. timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
  1644. devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
  1645. snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
  1646. devid, queue->id);
  1647. /* Initialise tx_skb_freelist as a free chain containing every entry. */
  1648. queue->tx_skb_freelist = 0;
  1649. queue->tx_pend_queue = TX_LINK_NONE;
  1650. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  1651. queue->tx_link[i] = i + 1;
  1652. queue->grant_tx_ref[i] = INVALID_GRANT_REF;
  1653. queue->grant_tx_page[i] = NULL;
  1654. }
  1655. queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
  1656. /* Clear out rx_skbs */
  1657. for (i = 0; i < NET_RX_RING_SIZE; i++) {
  1658. queue->rx_skbs[i] = NULL;
  1659. queue->grant_rx_ref[i] = INVALID_GRANT_REF;
  1660. }
  1661. /* A grant for every tx ring slot */
  1662. if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
  1663. &queue->gref_tx_head) < 0) {
  1664. pr_alert("can't alloc tx grant refs\n");
  1665. err = -ENOMEM;
  1666. goto exit;
  1667. }
  1668. /* A grant for every rx ring slot */
  1669. if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
  1670. &queue->gref_rx_head) < 0) {
  1671. pr_alert("can't alloc rx grant refs\n");
  1672. err = -ENOMEM;
  1673. goto exit_free_tx;
  1674. }
  1675. return 0;
  1676. exit_free_tx:
  1677. gnttab_free_grant_references(queue->gref_tx_head);
  1678. exit:
  1679. return err;
  1680. }
  1681. static int write_queue_xenstore_keys(struct netfront_queue *queue,
  1682. struct xenbus_transaction *xbt, int write_hierarchical)
  1683. {
  1684. /* Write the queue-specific keys into XenStore in the traditional
  1685. * way for a single queue, or in a queue subkeys for multiple
  1686. * queues.
  1687. */
  1688. struct xenbus_device *dev = queue->info->xbdev;
  1689. int err;
  1690. const char *message;
  1691. char *path;
  1692. size_t pathsize;
  1693. /* Choose the correct place to write the keys */
  1694. if (write_hierarchical) {
  1695. pathsize = strlen(dev->nodename) + 10;
  1696. path = kzalloc(pathsize, GFP_KERNEL);
  1697. if (!path) {
  1698. err = -ENOMEM;
  1699. message = "out of memory while writing ring references";
  1700. goto error;
  1701. }
  1702. snprintf(path, pathsize, "%s/queue-%u",
  1703. dev->nodename, queue->id);
  1704. } else {
  1705. path = (char *)dev->nodename;
  1706. }
  1707. /* Write ring references */
  1708. err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
  1709. queue->tx_ring_ref);
  1710. if (err) {
  1711. message = "writing tx-ring-ref";
  1712. goto error;
  1713. }
  1714. err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
  1715. queue->rx_ring_ref);
  1716. if (err) {
  1717. message = "writing rx-ring-ref";
  1718. goto error;
  1719. }
  1720. /* Write event channels; taking into account both shared
  1721. * and split event channel scenarios.
  1722. */
  1723. if (queue->tx_evtchn == queue->rx_evtchn) {
  1724. /* Shared event channel */
  1725. err = xenbus_printf(*xbt, path,
  1726. "event-channel", "%u", queue->tx_evtchn);
  1727. if (err) {
  1728. message = "writing event-channel";
  1729. goto error;
  1730. }
  1731. } else {
  1732. /* Split event channels */
  1733. err = xenbus_printf(*xbt, path,
  1734. "event-channel-tx", "%u", queue->tx_evtchn);
  1735. if (err) {
  1736. message = "writing event-channel-tx";
  1737. goto error;
  1738. }
  1739. err = xenbus_printf(*xbt, path,
  1740. "event-channel-rx", "%u", queue->rx_evtchn);
  1741. if (err) {
  1742. message = "writing event-channel-rx";
  1743. goto error;
  1744. }
  1745. }
  1746. if (write_hierarchical)
  1747. kfree(path);
  1748. return 0;
  1749. error:
  1750. if (write_hierarchical)
  1751. kfree(path);
  1752. xenbus_dev_fatal(dev, err, "%s", message);
  1753. return err;
  1754. }
  1755. static int xennet_create_page_pool(struct netfront_queue *queue)
  1756. {
  1757. int err;
  1758. struct page_pool_params pp_params = {
  1759. .order = 0,
  1760. .flags = 0,
  1761. .pool_size = NET_RX_RING_SIZE,
  1762. .nid = NUMA_NO_NODE,
  1763. .dev = &queue->info->netdev->dev,
  1764. .offset = XDP_PACKET_HEADROOM,
  1765. .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
  1766. };
  1767. queue->page_pool = page_pool_create(&pp_params);
  1768. if (IS_ERR(queue->page_pool)) {
  1769. err = PTR_ERR(queue->page_pool);
  1770. queue->page_pool = NULL;
  1771. return err;
  1772. }
  1773. err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
  1774. queue->id, 0);
  1775. if (err) {
  1776. netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
  1777. goto err_free_pp;
  1778. }
  1779. err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
  1780. MEM_TYPE_PAGE_POOL, queue->page_pool);
  1781. if (err) {
  1782. netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
  1783. goto err_unregister_rxq;
  1784. }
  1785. return 0;
  1786. err_unregister_rxq:
  1787. xdp_rxq_info_unreg(&queue->xdp_rxq);
  1788. err_free_pp:
  1789. page_pool_destroy(queue->page_pool);
  1790. queue->page_pool = NULL;
  1791. return err;
  1792. }
  1793. static int xennet_create_queues(struct netfront_info *info,
  1794. unsigned int *num_queues)
  1795. {
  1796. unsigned int i;
  1797. int ret;
  1798. info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
  1799. GFP_KERNEL);
  1800. if (!info->queues)
  1801. return -ENOMEM;
  1802. for (i = 0; i < *num_queues; i++) {
  1803. struct netfront_queue *queue = &info->queues[i];
  1804. queue->id = i;
  1805. queue->info = info;
  1806. ret = xennet_init_queue(queue);
  1807. if (ret < 0) {
  1808. dev_warn(&info->xbdev->dev,
  1809. "only created %d queues\n", i);
  1810. *num_queues = i;
  1811. break;
  1812. }
  1813. /* use page pool recycling instead of buddy allocator */
  1814. ret = xennet_create_page_pool(queue);
  1815. if (ret < 0) {
  1816. dev_err(&info->xbdev->dev, "can't allocate page pool\n");
  1817. *num_queues = i;
  1818. return ret;
  1819. }
  1820. netif_napi_add(queue->info->netdev, &queue->napi, xennet_poll);
  1821. if (netif_running(info->netdev))
  1822. napi_enable(&queue->napi);
  1823. }
  1824. netif_set_real_num_tx_queues(info->netdev, *num_queues);
  1825. if (*num_queues == 0) {
  1826. dev_err(&info->xbdev->dev, "no queues\n");
  1827. return -EINVAL;
  1828. }
  1829. return 0;
  1830. }
  1831. /* Common code used when first setting up, and when resuming. */
  1832. static int talk_to_netback(struct xenbus_device *dev,
  1833. struct netfront_info *info)
  1834. {
  1835. const char *message;
  1836. struct xenbus_transaction xbt;
  1837. int err;
  1838. unsigned int feature_split_evtchn;
  1839. unsigned int i = 0;
  1840. unsigned int max_queues = 0;
  1841. struct netfront_queue *queue = NULL;
  1842. unsigned int num_queues = 1;
  1843. u8 addr[ETH_ALEN];
  1844. info->netdev->irq = 0;
  1845. /* Check if backend is trusted. */
  1846. info->bounce = !xennet_trusted ||
  1847. !xenbus_read_unsigned(dev->nodename, "trusted", 1);
  1848. /* Check if backend supports multiple queues */
  1849. max_queues = xenbus_read_unsigned(info->xbdev->otherend,
  1850. "multi-queue-max-queues", 1);
  1851. num_queues = min(max_queues, xennet_max_queues);
  1852. /* Check feature-split-event-channels */
  1853. feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
  1854. "feature-split-event-channels", 0);
  1855. /* Read mac addr. */
  1856. err = xen_net_read_mac(dev, addr);
  1857. if (err) {
  1858. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  1859. goto out_unlocked;
  1860. }
  1861. eth_hw_addr_set(info->netdev, addr);
  1862. info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
  1863. "feature-xdp-headroom", 0);
  1864. if (info->netback_has_xdp_headroom) {
  1865. /* set the current xen-netfront xdp state */
  1866. err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
  1867. NETBACK_XDP_HEADROOM_ENABLE :
  1868. NETBACK_XDP_HEADROOM_DISABLE);
  1869. if (err)
  1870. goto out_unlocked;
  1871. }
  1872. rtnl_lock();
  1873. if (info->queues)
  1874. xennet_destroy_queues(info);
  1875. /* For the case of a reconnect reset the "broken" indicator. */
  1876. info->broken = false;
  1877. err = xennet_create_queues(info, &num_queues);
  1878. if (err < 0) {
  1879. xenbus_dev_fatal(dev, err, "creating queues");
  1880. kfree(info->queues);
  1881. info->queues = NULL;
  1882. goto out;
  1883. }
  1884. rtnl_unlock();
  1885. /* Create shared ring, alloc event channel -- for each queue */
  1886. for (i = 0; i < num_queues; ++i) {
  1887. queue = &info->queues[i];
  1888. err = setup_netfront(dev, queue, feature_split_evtchn);
  1889. if (err)
  1890. goto destroy_ring;
  1891. }
  1892. again:
  1893. err = xenbus_transaction_start(&xbt);
  1894. if (err) {
  1895. xenbus_dev_fatal(dev, err, "starting transaction");
  1896. goto destroy_ring;
  1897. }
  1898. if (xenbus_exists(XBT_NIL,
  1899. info->xbdev->otherend, "multi-queue-max-queues")) {
  1900. /* Write the number of queues */
  1901. err = xenbus_printf(xbt, dev->nodename,
  1902. "multi-queue-num-queues", "%u", num_queues);
  1903. if (err) {
  1904. message = "writing multi-queue-num-queues";
  1905. goto abort_transaction_no_dev_fatal;
  1906. }
  1907. }
  1908. if (num_queues == 1) {
  1909. err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
  1910. if (err)
  1911. goto abort_transaction_no_dev_fatal;
  1912. } else {
  1913. /* Write the keys for each queue */
  1914. for (i = 0; i < num_queues; ++i) {
  1915. queue = &info->queues[i];
  1916. err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
  1917. if (err)
  1918. goto abort_transaction_no_dev_fatal;
  1919. }
  1920. }
  1921. /* The remaining keys are not queue-specific */
  1922. err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
  1923. 1);
  1924. if (err) {
  1925. message = "writing request-rx-copy";
  1926. goto abort_transaction;
  1927. }
  1928. err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
  1929. if (err) {
  1930. message = "writing feature-rx-notify";
  1931. goto abort_transaction;
  1932. }
  1933. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
  1934. if (err) {
  1935. message = "writing feature-sg";
  1936. goto abort_transaction;
  1937. }
  1938. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
  1939. if (err) {
  1940. message = "writing feature-gso-tcpv4";
  1941. goto abort_transaction;
  1942. }
  1943. err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
  1944. if (err) {
  1945. message = "writing feature-gso-tcpv6";
  1946. goto abort_transaction;
  1947. }
  1948. err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
  1949. "1");
  1950. if (err) {
  1951. message = "writing feature-ipv6-csum-offload";
  1952. goto abort_transaction;
  1953. }
  1954. err = xenbus_transaction_end(xbt, 0);
  1955. if (err) {
  1956. if (err == -EAGAIN)
  1957. goto again;
  1958. xenbus_dev_fatal(dev, err, "completing transaction");
  1959. goto destroy_ring;
  1960. }
  1961. return 0;
  1962. abort_transaction:
  1963. xenbus_dev_fatal(dev, err, "%s", message);
  1964. abort_transaction_no_dev_fatal:
  1965. xenbus_transaction_end(xbt, 1);
  1966. destroy_ring:
  1967. xennet_disconnect_backend(info);
  1968. rtnl_lock();
  1969. xennet_destroy_queues(info);
  1970. out:
  1971. rtnl_unlock();
  1972. out_unlocked:
  1973. device_unregister(&dev->dev);
  1974. return err;
  1975. }
  1976. static int xennet_connect(struct net_device *dev)
  1977. {
  1978. struct netfront_info *np = netdev_priv(dev);
  1979. unsigned int num_queues = 0;
  1980. int err;
  1981. unsigned int j = 0;
  1982. struct netfront_queue *queue = NULL;
  1983. if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
  1984. dev_info(&dev->dev,
  1985. "backend does not support copying receive path\n");
  1986. return -ENODEV;
  1987. }
  1988. err = talk_to_netback(np->xbdev, np);
  1989. if (err)
  1990. return err;
  1991. if (np->netback_has_xdp_headroom)
  1992. pr_info("backend supports XDP headroom\n");
  1993. if (np->bounce)
  1994. dev_info(&np->xbdev->dev,
  1995. "bouncing transmitted data to zeroed pages\n");
  1996. /* talk_to_netback() sets the correct number of queues */
  1997. num_queues = dev->real_num_tx_queues;
  1998. if (dev->reg_state == NETREG_UNINITIALIZED) {
  1999. err = register_netdev(dev);
  2000. if (err) {
  2001. pr_warn("%s: register_netdev err=%d\n", __func__, err);
  2002. device_unregister(&np->xbdev->dev);
  2003. return err;
  2004. }
  2005. }
  2006. rtnl_lock();
  2007. netdev_update_features(dev);
  2008. rtnl_unlock();
  2009. /*
  2010. * All public and private state should now be sane. Get
  2011. * ready to start sending and receiving packets and give the driver
  2012. * domain a kick because we've probably just requeued some
  2013. * packets.
  2014. */
  2015. netif_tx_lock_bh(np->netdev);
  2016. netif_device_attach(np->netdev);
  2017. netif_tx_unlock_bh(np->netdev);
  2018. netif_carrier_on(np->netdev);
  2019. for (j = 0; j < num_queues; ++j) {
  2020. queue = &np->queues[j];
  2021. notify_remote_via_irq(queue->tx_irq);
  2022. if (queue->tx_irq != queue->rx_irq)
  2023. notify_remote_via_irq(queue->rx_irq);
  2024. spin_lock_bh(&queue->rx_lock);
  2025. xennet_alloc_rx_buffers(queue);
  2026. spin_unlock_bh(&queue->rx_lock);
  2027. }
  2028. return 0;
  2029. }
  2030. /*
  2031. * Callback received when the backend's state changes.
  2032. */
  2033. static void netback_changed(struct xenbus_device *dev,
  2034. enum xenbus_state backend_state)
  2035. {
  2036. struct netfront_info *np = dev_get_drvdata(&dev->dev);
  2037. struct net_device *netdev = np->netdev;
  2038. dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
  2039. wake_up_all(&module_wq);
  2040. switch (backend_state) {
  2041. case XenbusStateInitialising:
  2042. case XenbusStateInitialised:
  2043. case XenbusStateReconfiguring:
  2044. case XenbusStateReconfigured:
  2045. case XenbusStateUnknown:
  2046. break;
  2047. case XenbusStateInitWait:
  2048. if (dev->state != XenbusStateInitialising)
  2049. break;
  2050. if (xennet_connect(netdev) != 0)
  2051. break;
  2052. xenbus_switch_state(dev, XenbusStateConnected);
  2053. break;
  2054. case XenbusStateConnected:
  2055. netdev_notify_peers(netdev);
  2056. break;
  2057. case XenbusStateClosed:
  2058. if (dev->state == XenbusStateClosed)
  2059. break;
  2060. fallthrough; /* Missed the backend's CLOSING state */
  2061. case XenbusStateClosing:
  2062. xenbus_frontend_closed(dev);
  2063. break;
  2064. }
  2065. }
  2066. static const struct xennet_stat {
  2067. char name[ETH_GSTRING_LEN];
  2068. u16 offset;
  2069. } xennet_stats[] = {
  2070. {
  2071. "rx_gso_checksum_fixup",
  2072. offsetof(struct netfront_info, rx_gso_checksum_fixup)
  2073. },
  2074. };
  2075. static int xennet_get_sset_count(struct net_device *dev, int string_set)
  2076. {
  2077. switch (string_set) {
  2078. case ETH_SS_STATS:
  2079. return ARRAY_SIZE(xennet_stats);
  2080. default:
  2081. return -EINVAL;
  2082. }
  2083. }
  2084. static void xennet_get_ethtool_stats(struct net_device *dev,
  2085. struct ethtool_stats *stats, u64 * data)
  2086. {
  2087. void *np = netdev_priv(dev);
  2088. int i;
  2089. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  2090. data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
  2091. }
  2092. static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  2093. {
  2094. int i;
  2095. switch (stringset) {
  2096. case ETH_SS_STATS:
  2097. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  2098. memcpy(data + i * ETH_GSTRING_LEN,
  2099. xennet_stats[i].name, ETH_GSTRING_LEN);
  2100. break;
  2101. }
  2102. }
  2103. static const struct ethtool_ops xennet_ethtool_ops =
  2104. {
  2105. .get_link = ethtool_op_get_link,
  2106. .get_sset_count = xennet_get_sset_count,
  2107. .get_ethtool_stats = xennet_get_ethtool_stats,
  2108. .get_strings = xennet_get_strings,
  2109. .get_ts_info = ethtool_op_get_ts_info,
  2110. };
  2111. #ifdef CONFIG_SYSFS
  2112. static ssize_t show_rxbuf(struct device *dev,
  2113. struct device_attribute *attr, char *buf)
  2114. {
  2115. return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
  2116. }
  2117. static ssize_t store_rxbuf(struct device *dev,
  2118. struct device_attribute *attr,
  2119. const char *buf, size_t len)
  2120. {
  2121. char *endp;
  2122. if (!capable(CAP_NET_ADMIN))
  2123. return -EPERM;
  2124. simple_strtoul(buf, &endp, 0);
  2125. if (endp == buf)
  2126. return -EBADMSG;
  2127. /* rxbuf_min and rxbuf_max are no longer configurable. */
  2128. return len;
  2129. }
  2130. static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
  2131. static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
  2132. static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
  2133. static struct attribute *xennet_dev_attrs[] = {
  2134. &dev_attr_rxbuf_min.attr,
  2135. &dev_attr_rxbuf_max.attr,
  2136. &dev_attr_rxbuf_cur.attr,
  2137. NULL
  2138. };
  2139. static const struct attribute_group xennet_dev_group = {
  2140. .attrs = xennet_dev_attrs
  2141. };
  2142. #endif /* CONFIG_SYSFS */
  2143. static void xennet_bus_close(struct xenbus_device *dev)
  2144. {
  2145. int ret;
  2146. if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
  2147. return;
  2148. do {
  2149. xenbus_switch_state(dev, XenbusStateClosing);
  2150. ret = wait_event_timeout(module_wq,
  2151. xenbus_read_driver_state(dev->otherend) ==
  2152. XenbusStateClosing ||
  2153. xenbus_read_driver_state(dev->otherend) ==
  2154. XenbusStateClosed ||
  2155. xenbus_read_driver_state(dev->otherend) ==
  2156. XenbusStateUnknown,
  2157. XENNET_TIMEOUT);
  2158. } while (!ret);
  2159. if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
  2160. return;
  2161. do {
  2162. xenbus_switch_state(dev, XenbusStateClosed);
  2163. ret = wait_event_timeout(module_wq,
  2164. xenbus_read_driver_state(dev->otherend) ==
  2165. XenbusStateClosed ||
  2166. xenbus_read_driver_state(dev->otherend) ==
  2167. XenbusStateUnknown,
  2168. XENNET_TIMEOUT);
  2169. } while (!ret);
  2170. }
  2171. static void xennet_remove(struct xenbus_device *dev)
  2172. {
  2173. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  2174. xennet_bus_close(dev);
  2175. xennet_disconnect_backend(info);
  2176. if (info->netdev->reg_state == NETREG_REGISTERED)
  2177. unregister_netdev(info->netdev);
  2178. if (info->queues) {
  2179. rtnl_lock();
  2180. xennet_destroy_queues(info);
  2181. rtnl_unlock();
  2182. }
  2183. xennet_free_netdev(info->netdev);
  2184. }
  2185. static const struct xenbus_device_id netfront_ids[] = {
  2186. { "vif" },
  2187. { "" }
  2188. };
  2189. static struct xenbus_driver netfront_driver = {
  2190. .ids = netfront_ids,
  2191. .probe = netfront_probe,
  2192. .remove = xennet_remove,
  2193. .resume = netfront_resume,
  2194. .otherend_changed = netback_changed,
  2195. };
  2196. static int __init netif_init(void)
  2197. {
  2198. if (!xen_domain())
  2199. return -ENODEV;
  2200. if (!xen_has_pv_nic_devices())
  2201. return -ENODEV;
  2202. pr_info("Initialising Xen virtual ethernet driver\n");
  2203. /* Allow as many queues as there are CPUs inut max. 8 if user has not
  2204. * specified a value.
  2205. */
  2206. if (xennet_max_queues == 0)
  2207. xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
  2208. num_online_cpus());
  2209. return xenbus_register_frontend(&netfront_driver);
  2210. }
  2211. module_init(netif_init);
  2212. static void __exit netif_exit(void)
  2213. {
  2214. xenbus_unregister_driver(&netfront_driver);
  2215. }
  2216. module_exit(netif_exit);
  2217. MODULE_DESCRIPTION("Xen virtual network device frontend");
  2218. MODULE_LICENSE("GPL");
  2219. MODULE_ALIAS("xen:vif");
  2220. MODULE_ALIAS("xennet");