virtio_net.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863
  1. /* A network driver using virtio.
  2. *
  3. * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. //#define DEBUG
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/module.h>
  23. #include <linux/virtio.h>
  24. #include <linux/virtio_net.h>
  25. #include <linux/bpf.h>
  26. #include <linux/bpf_trace.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/slab.h>
  30. #include <linux/cpu.h>
  31. #include <linux/average.h>
  32. #include <linux/filter.h>
  33. #include <net/route.h>
  34. static int napi_weight = NAPI_POLL_WEIGHT;
  35. module_param(napi_weight, int, 0444);
  36. static bool csum = true, gso = true, napi_tx;
  37. module_param(csum, bool, 0444);
  38. module_param(gso, bool, 0444);
  39. module_param(napi_tx, bool, 0644);
  40. /* FIXME: MTU in config. */
  41. #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
  42. #define GOOD_COPY_LEN 128
  43. #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
  44. /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
  45. #define VIRTIO_XDP_HEADROOM 256
  46. /* RX packet size EWMA. The average packet size is used to determine the packet
  47. * buffer size when refilling RX rings. As the entire RX ring may be refilled
  48. * at once, the weight is chosen so that the EWMA will be insensitive to short-
  49. * term, transient changes in packet size.
  50. */
  51. DECLARE_EWMA(pkt_len, 0, 64)
  52. #define VIRTNET_DRIVER_VERSION "1.0.0"
  53. static const unsigned long guest_offloads[] = {
  54. VIRTIO_NET_F_GUEST_TSO4,
  55. VIRTIO_NET_F_GUEST_TSO6,
  56. VIRTIO_NET_F_GUEST_ECN,
  57. VIRTIO_NET_F_GUEST_UFO
  58. };
  59. struct virtnet_stats {
  60. struct u64_stats_sync tx_syncp;
  61. struct u64_stats_sync rx_syncp;
  62. u64 tx_bytes;
  63. u64 tx_packets;
  64. u64 rx_bytes;
  65. u64 rx_packets;
  66. };
  67. /* Internal representation of a send virtqueue */
  68. struct send_queue {
  69. /* Virtqueue associated with this send _queue */
  70. struct virtqueue *vq;
  71. /* TX: fragments + linear part + virtio header */
  72. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  73. /* Name of the send queue: output.$index */
  74. char name[40];
  75. struct napi_struct napi;
  76. };
  77. /* Internal representation of a receive virtqueue */
  78. struct receive_queue {
  79. /* Virtqueue associated with this receive_queue */
  80. struct virtqueue *vq;
  81. struct napi_struct napi;
  82. struct bpf_prog __rcu *xdp_prog;
  83. /* Chain pages by the private ptr. */
  84. struct page *pages;
  85. /* Average packet length for mergeable receive buffers. */
  86. struct ewma_pkt_len mrg_avg_pkt_len;
  87. /* Page frag for packet buffer allocation. */
  88. struct page_frag alloc_frag;
  89. /* RX: fragments + linear part + virtio header */
  90. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  91. /* Min single buffer size for mergeable buffers case. */
  92. unsigned int min_buf_len;
  93. /* Name of this receive queue: input.$index */
  94. char name[40];
  95. };
  96. struct virtnet_info {
  97. struct virtio_device *vdev;
  98. struct virtqueue *cvq;
  99. struct net_device *dev;
  100. struct send_queue *sq;
  101. struct receive_queue *rq;
  102. unsigned int status;
  103. /* Max # of queue pairs supported by the device */
  104. u16 max_queue_pairs;
  105. /* # of queue pairs currently used by the driver */
  106. u16 curr_queue_pairs;
  107. /* # of XDP queue pairs currently used by the driver */
  108. u16 xdp_queue_pairs;
  109. /* I like... big packets and I cannot lie! */
  110. bool big_packets;
  111. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  112. bool mergeable_rx_bufs;
  113. /* Has control virtqueue */
  114. bool has_cvq;
  115. /* Host can handle any s/g split between our header and packet data */
  116. bool any_header_sg;
  117. /* Packet virtio header size */
  118. u8 hdr_len;
  119. /* Active statistics */
  120. struct virtnet_stats __percpu *stats;
  121. /* Work struct for refilling if we run low on memory. */
  122. struct delayed_work refill;
  123. /* Work struct for config space updates */
  124. struct work_struct config_work;
  125. /* Does the affinity hint is set for virtqueues? */
  126. bool affinity_hint_set;
  127. /* CPU hotplug instances for online & dead */
  128. struct hlist_node node;
  129. struct hlist_node node_dead;
  130. /* Control VQ buffers: protected by the rtnl lock */
  131. struct virtio_net_ctrl_hdr ctrl_hdr;
  132. virtio_net_ctrl_ack ctrl_status;
  133. struct virtio_net_ctrl_mq ctrl_mq;
  134. u8 ctrl_promisc;
  135. u8 ctrl_allmulti;
  136. u16 ctrl_vid;
  137. u64 ctrl_offloads;
  138. /* Ethtool settings */
  139. u8 duplex;
  140. u32 speed;
  141. unsigned long guest_offloads;
  142. };
  143. struct padded_vnet_hdr {
  144. struct virtio_net_hdr_mrg_rxbuf hdr;
  145. /*
  146. * hdr is in a separate sg buffer, and data sg buffer shares same page
  147. * with this header sg. This padding makes next sg 16 byte aligned
  148. * after the header.
  149. */
  150. char padding[4];
  151. };
  152. /* Converting between virtqueue no. and kernel tx/rx queue no.
  153. * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
  154. */
  155. static int vq2txq(struct virtqueue *vq)
  156. {
  157. return (vq->index - 1) / 2;
  158. }
  159. static int txq2vq(int txq)
  160. {
  161. return txq * 2 + 1;
  162. }
  163. static int vq2rxq(struct virtqueue *vq)
  164. {
  165. return vq->index / 2;
  166. }
  167. static int rxq2vq(int rxq)
  168. {
  169. return rxq * 2;
  170. }
  171. static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
  172. {
  173. return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
  174. }
  175. /*
  176. * private is used to chain pages for big packets, put the whole
  177. * most recent used list in the beginning for reuse
  178. */
  179. static void give_pages(struct receive_queue *rq, struct page *page)
  180. {
  181. struct page *end;
  182. /* Find end of list, sew whole thing into vi->rq.pages. */
  183. for (end = page; end->private; end = (struct page *)end->private);
  184. end->private = (unsigned long)rq->pages;
  185. rq->pages = page;
  186. }
  187. static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  188. {
  189. struct page *p = rq->pages;
  190. if (p) {
  191. rq->pages = (struct page *)p->private;
  192. /* clear private here, it is used to chain pages */
  193. p->private = 0;
  194. } else
  195. p = alloc_page(gfp_mask);
  196. return p;
  197. }
  198. static void virtqueue_napi_schedule(struct napi_struct *napi,
  199. struct virtqueue *vq)
  200. {
  201. if (napi_schedule_prep(napi)) {
  202. virtqueue_disable_cb(vq);
  203. __napi_schedule(napi);
  204. }
  205. }
  206. static void virtqueue_napi_complete(struct napi_struct *napi,
  207. struct virtqueue *vq, int processed)
  208. {
  209. int opaque;
  210. opaque = virtqueue_enable_cb_prepare(vq);
  211. if (napi_complete_done(napi, processed) &&
  212. unlikely(virtqueue_poll(vq, opaque)))
  213. virtqueue_napi_schedule(napi, vq);
  214. }
  215. static void skb_xmit_done(struct virtqueue *vq)
  216. {
  217. struct virtnet_info *vi = vq->vdev->priv;
  218. struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
  219. /* Suppress further interrupts. */
  220. virtqueue_disable_cb(vq);
  221. if (napi->weight)
  222. virtqueue_napi_schedule(napi, vq);
  223. else
  224. /* We were probably waiting for more output buffers. */
  225. netif_wake_subqueue(vi->dev, vq2txq(vq));
  226. }
  227. #define MRG_CTX_HEADER_SHIFT 22
  228. static void *mergeable_len_to_ctx(unsigned int truesize,
  229. unsigned int headroom)
  230. {
  231. return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
  232. }
  233. static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
  234. {
  235. return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
  236. }
  237. static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
  238. {
  239. return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
  240. }
  241. /* Called from bottom half context */
  242. static struct sk_buff *page_to_skb(struct virtnet_info *vi,
  243. struct receive_queue *rq,
  244. struct page *page, unsigned int offset,
  245. unsigned int len, unsigned int truesize)
  246. {
  247. struct sk_buff *skb;
  248. struct virtio_net_hdr_mrg_rxbuf *hdr;
  249. unsigned int copy, hdr_len, hdr_padded_len;
  250. char *p;
  251. p = page_address(page) + offset;
  252. /* copy small packet so we can reuse these pages for small data */
  253. skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
  254. if (unlikely(!skb))
  255. return NULL;
  256. hdr = skb_vnet_hdr(skb);
  257. hdr_len = vi->hdr_len;
  258. if (vi->mergeable_rx_bufs)
  259. hdr_padded_len = sizeof(*hdr);
  260. else
  261. hdr_padded_len = sizeof(struct padded_vnet_hdr);
  262. memcpy(hdr, p, hdr_len);
  263. len -= hdr_len;
  264. offset += hdr_padded_len;
  265. p += hdr_padded_len;
  266. copy = len;
  267. if (copy > skb_tailroom(skb))
  268. copy = skb_tailroom(skb);
  269. skb_put_data(skb, p, copy);
  270. len -= copy;
  271. offset += copy;
  272. if (vi->mergeable_rx_bufs) {
  273. if (len)
  274. skb_add_rx_frag(skb, 0, page, offset, len, truesize);
  275. else
  276. put_page(page);
  277. return skb;
  278. }
  279. /*
  280. * Verify that we can indeed put this data into a skb.
  281. * This is here to handle cases when the device erroneously
  282. * tries to receive more than is possible. This is usually
  283. * the case of a broken device.
  284. */
  285. if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
  286. net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
  287. dev_kfree_skb(skb);
  288. return NULL;
  289. }
  290. BUG_ON(offset >= PAGE_SIZE);
  291. while (len) {
  292. unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
  293. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
  294. frag_size, truesize);
  295. len -= frag_size;
  296. page = (struct page *)page->private;
  297. offset = 0;
  298. }
  299. if (page)
  300. give_pages(rq, page);
  301. return skb;
  302. }
  303. static void virtnet_xdp_flush(struct net_device *dev)
  304. {
  305. struct virtnet_info *vi = netdev_priv(dev);
  306. struct send_queue *sq;
  307. unsigned int qp;
  308. qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
  309. sq = &vi->sq[qp];
  310. virtqueue_kick(sq->vq);
  311. }
  312. static bool __virtnet_xdp_xmit(struct virtnet_info *vi,
  313. struct xdp_buff *xdp)
  314. {
  315. struct virtio_net_hdr_mrg_rxbuf *hdr;
  316. unsigned int len;
  317. struct send_queue *sq;
  318. unsigned int qp;
  319. void *xdp_sent;
  320. int err;
  321. qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
  322. sq = &vi->sq[qp];
  323. /* Free up any pending old buffers before queueing new ones. */
  324. while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  325. struct page *sent_page = virt_to_head_page(xdp_sent);
  326. put_page(sent_page);
  327. }
  328. xdp->data -= vi->hdr_len;
  329. /* Zero header and leave csum up to XDP layers */
  330. hdr = xdp->data;
  331. memset(hdr, 0, vi->hdr_len);
  332. sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
  333. err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC);
  334. if (unlikely(err)) {
  335. struct page *page = virt_to_head_page(xdp->data);
  336. put_page(page);
  337. return false;
  338. }
  339. return true;
  340. }
  341. static int virtnet_xdp_xmit(struct net_device *dev, struct xdp_buff *xdp)
  342. {
  343. struct virtnet_info *vi = netdev_priv(dev);
  344. bool sent = __virtnet_xdp_xmit(vi, xdp);
  345. if (!sent)
  346. return -ENOSPC;
  347. return 0;
  348. }
  349. static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
  350. {
  351. return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
  352. }
  353. /* We copy the packet for XDP in the following cases:
  354. *
  355. * 1) Packet is scattered across multiple rx buffers.
  356. * 2) Headroom space is insufficient.
  357. *
  358. * This is inefficient but it's a temporary condition that
  359. * we hit right after XDP is enabled and until queue is refilled
  360. * with large buffers with sufficient headroom - so it should affect
  361. * at most queue size packets.
  362. * Afterwards, the conditions to enable
  363. * XDP should preclude the underlying device from sending packets
  364. * across multiple buffers (num_buf > 1), and we make sure buffers
  365. * have enough headroom.
  366. */
  367. static struct page *xdp_linearize_page(struct receive_queue *rq,
  368. u16 *num_buf,
  369. struct page *p,
  370. int offset,
  371. int page_off,
  372. unsigned int *len)
  373. {
  374. struct page *page = alloc_page(GFP_ATOMIC);
  375. if (!page)
  376. return NULL;
  377. memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
  378. page_off += *len;
  379. while (--*num_buf) {
  380. unsigned int buflen;
  381. void *buf;
  382. int off;
  383. buf = virtqueue_get_buf(rq->vq, &buflen);
  384. if (unlikely(!buf))
  385. goto err_buf;
  386. p = virt_to_head_page(buf);
  387. off = buf - page_address(p);
  388. /* guard against a misconfigured or uncooperative backend that
  389. * is sending packet larger than the MTU.
  390. */
  391. if ((page_off + buflen) > PAGE_SIZE) {
  392. put_page(p);
  393. goto err_buf;
  394. }
  395. memcpy(page_address(page) + page_off,
  396. page_address(p) + off, buflen);
  397. page_off += buflen;
  398. put_page(p);
  399. }
  400. /* Headroom does not contribute to packet length */
  401. *len = page_off - VIRTIO_XDP_HEADROOM;
  402. return page;
  403. err_buf:
  404. __free_pages(page, 0);
  405. return NULL;
  406. }
  407. static struct sk_buff *receive_small(struct net_device *dev,
  408. struct virtnet_info *vi,
  409. struct receive_queue *rq,
  410. void *buf, void *ctx,
  411. unsigned int len,
  412. bool *xdp_xmit)
  413. {
  414. struct sk_buff *skb;
  415. struct bpf_prog *xdp_prog;
  416. unsigned int xdp_headroom = (unsigned long)ctx;
  417. unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
  418. unsigned int headroom = vi->hdr_len + header_offset;
  419. unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  420. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  421. struct page *page = virt_to_head_page(buf);
  422. unsigned int delta = 0, err;
  423. struct page *xdp_page;
  424. len -= vi->hdr_len;
  425. rcu_read_lock();
  426. xdp_prog = rcu_dereference(rq->xdp_prog);
  427. if (xdp_prog) {
  428. struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
  429. struct xdp_buff xdp;
  430. void *orig_data;
  431. u32 act;
  432. if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
  433. goto err_xdp;
  434. if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
  435. int offset = buf - page_address(page) + header_offset;
  436. unsigned int tlen = len + vi->hdr_len;
  437. u16 num_buf = 1;
  438. xdp_headroom = virtnet_get_headroom(vi);
  439. header_offset = VIRTNET_RX_PAD + xdp_headroom;
  440. headroom = vi->hdr_len + header_offset;
  441. buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  442. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  443. xdp_page = xdp_linearize_page(rq, &num_buf, page,
  444. offset, header_offset,
  445. &tlen);
  446. if (!xdp_page)
  447. goto err_xdp;
  448. buf = page_address(xdp_page);
  449. put_page(page);
  450. page = xdp_page;
  451. }
  452. xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
  453. xdp.data = xdp.data_hard_start + xdp_headroom;
  454. xdp.data_end = xdp.data + len;
  455. orig_data = xdp.data;
  456. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  457. switch (act) {
  458. case XDP_PASS:
  459. /* Recalculate length in case bpf program changed it */
  460. delta = orig_data - xdp.data;
  461. break;
  462. case XDP_TX:
  463. if (unlikely(!__virtnet_xdp_xmit(vi, &xdp)))
  464. trace_xdp_exception(vi->dev, xdp_prog, act);
  465. else
  466. *xdp_xmit = true;
  467. rcu_read_unlock();
  468. goto xdp_xmit;
  469. case XDP_REDIRECT:
  470. err = xdp_do_redirect(dev, &xdp, xdp_prog);
  471. if (!err)
  472. *xdp_xmit = true;
  473. rcu_read_unlock();
  474. goto xdp_xmit;
  475. default:
  476. bpf_warn_invalid_xdp_action(act);
  477. case XDP_ABORTED:
  478. trace_xdp_exception(vi->dev, xdp_prog, act);
  479. case XDP_DROP:
  480. goto err_xdp;
  481. }
  482. }
  483. rcu_read_unlock();
  484. skb = build_skb(buf, buflen);
  485. if (!skb) {
  486. put_page(page);
  487. goto err;
  488. }
  489. skb_reserve(skb, headroom - delta);
  490. skb_put(skb, len + delta);
  491. if (!delta) {
  492. buf += header_offset;
  493. memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
  494. } /* keep zeroed vnet hdr since packet was changed by bpf */
  495. err:
  496. return skb;
  497. err_xdp:
  498. rcu_read_unlock();
  499. dev->stats.rx_dropped++;
  500. put_page(page);
  501. xdp_xmit:
  502. return NULL;
  503. }
  504. static struct sk_buff *receive_big(struct net_device *dev,
  505. struct virtnet_info *vi,
  506. struct receive_queue *rq,
  507. void *buf,
  508. unsigned int len)
  509. {
  510. struct page *page = buf;
  511. struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
  512. if (unlikely(!skb))
  513. goto err;
  514. return skb;
  515. err:
  516. dev->stats.rx_dropped++;
  517. give_pages(rq, page);
  518. return NULL;
  519. }
  520. static struct sk_buff *receive_mergeable(struct net_device *dev,
  521. struct virtnet_info *vi,
  522. struct receive_queue *rq,
  523. void *buf,
  524. void *ctx,
  525. unsigned int len,
  526. bool *xdp_xmit)
  527. {
  528. struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
  529. u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
  530. struct page *page = virt_to_head_page(buf);
  531. int offset = buf - page_address(page);
  532. struct sk_buff *head_skb, *curr_skb;
  533. struct bpf_prog *xdp_prog;
  534. unsigned int truesize;
  535. unsigned int headroom = mergeable_ctx_to_headroom(ctx);
  536. int err;
  537. head_skb = NULL;
  538. rcu_read_lock();
  539. xdp_prog = rcu_dereference(rq->xdp_prog);
  540. if (xdp_prog) {
  541. struct page *xdp_page;
  542. struct xdp_buff xdp;
  543. void *data;
  544. u32 act;
  545. /* This happens when rx buffer size is underestimated */
  546. if (unlikely(num_buf > 1 ||
  547. headroom < virtnet_get_headroom(vi))) {
  548. /* linearize data for XDP */
  549. xdp_page = xdp_linearize_page(rq, &num_buf,
  550. page, offset,
  551. VIRTIO_XDP_HEADROOM,
  552. &len);
  553. if (!xdp_page)
  554. goto err_xdp;
  555. offset = VIRTIO_XDP_HEADROOM;
  556. } else {
  557. xdp_page = page;
  558. }
  559. /* Transient failure which in theory could occur if
  560. * in-flight packets from before XDP was enabled reach
  561. * the receive path after XDP is loaded. In practice I
  562. * was not able to create this condition.
  563. */
  564. if (unlikely(hdr->hdr.gso_type))
  565. goto err_xdp;
  566. /* Allow consuming headroom but reserve enough space to push
  567. * the descriptor on if we get an XDP_TX return code.
  568. */
  569. data = page_address(xdp_page) + offset;
  570. xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
  571. xdp.data = data + vi->hdr_len;
  572. xdp.data_end = xdp.data + (len - vi->hdr_len);
  573. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  574. if (act != XDP_PASS)
  575. ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
  576. switch (act) {
  577. case XDP_PASS:
  578. /* recalculate offset to account for any header
  579. * adjustments. Note other cases do not build an
  580. * skb and avoid using offset
  581. */
  582. offset = xdp.data -
  583. page_address(xdp_page) - vi->hdr_len;
  584. /* We can only create skb based on xdp_page. */
  585. if (unlikely(xdp_page != page)) {
  586. rcu_read_unlock();
  587. put_page(page);
  588. head_skb = page_to_skb(vi, rq, xdp_page,
  589. offset, len, PAGE_SIZE);
  590. return head_skb;
  591. }
  592. break;
  593. case XDP_TX:
  594. if (unlikely(!__virtnet_xdp_xmit(vi, &xdp)))
  595. trace_xdp_exception(vi->dev, xdp_prog, act);
  596. else
  597. *xdp_xmit = true;
  598. if (unlikely(xdp_page != page))
  599. goto err_xdp;
  600. rcu_read_unlock();
  601. goto xdp_xmit;
  602. case XDP_REDIRECT:
  603. err = xdp_do_redirect(dev, &xdp, xdp_prog);
  604. if (err)
  605. *xdp_xmit = true;
  606. rcu_read_unlock();
  607. goto xdp_xmit;
  608. default:
  609. bpf_warn_invalid_xdp_action(act);
  610. case XDP_ABORTED:
  611. trace_xdp_exception(vi->dev, xdp_prog, act);
  612. case XDP_DROP:
  613. if (unlikely(xdp_page != page))
  614. __free_pages(xdp_page, 0);
  615. goto err_xdp;
  616. }
  617. }
  618. rcu_read_unlock();
  619. truesize = mergeable_ctx_to_truesize(ctx);
  620. if (unlikely(len > truesize)) {
  621. pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
  622. dev->name, len, (unsigned long)ctx);
  623. dev->stats.rx_length_errors++;
  624. goto err_skb;
  625. }
  626. head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
  627. curr_skb = head_skb;
  628. if (unlikely(!curr_skb))
  629. goto err_skb;
  630. while (--num_buf) {
  631. int num_skb_frags;
  632. buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
  633. if (unlikely(!ctx)) {
  634. pr_debug("%s: rx error: %d buffers out of %d missing\n",
  635. dev->name, num_buf,
  636. virtio16_to_cpu(vi->vdev,
  637. hdr->num_buffers));
  638. dev->stats.rx_length_errors++;
  639. goto err_buf;
  640. }
  641. page = virt_to_head_page(buf);
  642. truesize = mergeable_ctx_to_truesize(ctx);
  643. if (unlikely(len > truesize)) {
  644. pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
  645. dev->name, len, (unsigned long)ctx);
  646. dev->stats.rx_length_errors++;
  647. goto err_skb;
  648. }
  649. num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
  650. if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
  651. struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
  652. if (unlikely(!nskb))
  653. goto err_skb;
  654. if (curr_skb == head_skb)
  655. skb_shinfo(curr_skb)->frag_list = nskb;
  656. else
  657. curr_skb->next = nskb;
  658. curr_skb = nskb;
  659. head_skb->truesize += nskb->truesize;
  660. num_skb_frags = 0;
  661. }
  662. if (curr_skb != head_skb) {
  663. head_skb->data_len += len;
  664. head_skb->len += len;
  665. head_skb->truesize += truesize;
  666. }
  667. offset = buf - page_address(page);
  668. if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
  669. put_page(page);
  670. skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
  671. len, truesize);
  672. } else {
  673. skb_add_rx_frag(curr_skb, num_skb_frags, page,
  674. offset, len, truesize);
  675. }
  676. }
  677. ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
  678. return head_skb;
  679. err_xdp:
  680. rcu_read_unlock();
  681. err_skb:
  682. put_page(page);
  683. while (--num_buf) {
  684. buf = virtqueue_get_buf(rq->vq, &len);
  685. if (unlikely(!buf)) {
  686. pr_debug("%s: rx error: %d buffers missing\n",
  687. dev->name, num_buf);
  688. dev->stats.rx_length_errors++;
  689. break;
  690. }
  691. page = virt_to_head_page(buf);
  692. put_page(page);
  693. }
  694. err_buf:
  695. dev->stats.rx_dropped++;
  696. dev_kfree_skb(head_skb);
  697. xdp_xmit:
  698. return NULL;
  699. }
  700. static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
  701. void *buf, unsigned int len, void **ctx, bool *xdp_xmit)
  702. {
  703. struct net_device *dev = vi->dev;
  704. struct sk_buff *skb;
  705. struct virtio_net_hdr_mrg_rxbuf *hdr;
  706. int ret;
  707. if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
  708. pr_debug("%s: short packet %i\n", dev->name, len);
  709. dev->stats.rx_length_errors++;
  710. if (vi->mergeable_rx_bufs) {
  711. put_page(virt_to_head_page(buf));
  712. } else if (vi->big_packets) {
  713. give_pages(rq, buf);
  714. } else {
  715. put_page(virt_to_head_page(buf));
  716. }
  717. return 0;
  718. }
  719. if (vi->mergeable_rx_bufs)
  720. skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit);
  721. else if (vi->big_packets)
  722. skb = receive_big(dev, vi, rq, buf, len);
  723. else
  724. skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit);
  725. if (unlikely(!skb))
  726. return 0;
  727. hdr = skb_vnet_hdr(skb);
  728. ret = skb->len;
  729. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
  730. skb->ip_summed = CHECKSUM_UNNECESSARY;
  731. if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
  732. virtio_is_little_endian(vi->vdev))) {
  733. net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
  734. dev->name, hdr->hdr.gso_type,
  735. hdr->hdr.gso_size);
  736. goto frame_err;
  737. }
  738. skb->protocol = eth_type_trans(skb, dev);
  739. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  740. ntohs(skb->protocol), skb->len, skb->pkt_type);
  741. napi_gro_receive(&rq->napi, skb);
  742. return ret;
  743. frame_err:
  744. dev->stats.rx_frame_errors++;
  745. dev_kfree_skb(skb);
  746. return 0;
  747. }
  748. /* Unlike mergeable buffers, all buffers are allocated to the
  749. * same size, except for the headroom. For this reason we do
  750. * not need to use mergeable_len_to_ctx here - it is enough
  751. * to store the headroom as the context ignoring the truesize.
  752. */
  753. static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
  754. gfp_t gfp)
  755. {
  756. struct page_frag *alloc_frag = &rq->alloc_frag;
  757. char *buf;
  758. unsigned int xdp_headroom = virtnet_get_headroom(vi);
  759. void *ctx = (void *)(unsigned long)xdp_headroom;
  760. int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
  761. int err;
  762. len = SKB_DATA_ALIGN(len) +
  763. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  764. if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
  765. return -ENOMEM;
  766. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  767. get_page(alloc_frag->page);
  768. alloc_frag->offset += len;
  769. sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
  770. vi->hdr_len + GOOD_PACKET_LEN);
  771. err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
  772. if (err < 0)
  773. put_page(virt_to_head_page(buf));
  774. return err;
  775. }
  776. static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
  777. gfp_t gfp)
  778. {
  779. struct page *first, *list = NULL;
  780. char *p;
  781. int i, err, offset;
  782. sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
  783. /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
  784. for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
  785. first = get_a_page(rq, gfp);
  786. if (!first) {
  787. if (list)
  788. give_pages(rq, list);
  789. return -ENOMEM;
  790. }
  791. sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
  792. /* chain new page in list head to match sg */
  793. first->private = (unsigned long)list;
  794. list = first;
  795. }
  796. first = get_a_page(rq, gfp);
  797. if (!first) {
  798. give_pages(rq, list);
  799. return -ENOMEM;
  800. }
  801. p = page_address(first);
  802. /* rq->sg[0], rq->sg[1] share the same page */
  803. /* a separated rq->sg[0] for header - required in case !any_header_sg */
  804. sg_set_buf(&rq->sg[0], p, vi->hdr_len);
  805. /* rq->sg[1] for data packet, from offset */
  806. offset = sizeof(struct padded_vnet_hdr);
  807. sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
  808. /* chain first in list head */
  809. first->private = (unsigned long)list;
  810. err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
  811. first, gfp);
  812. if (err < 0)
  813. give_pages(rq, first);
  814. return err;
  815. }
  816. static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
  817. struct ewma_pkt_len *avg_pkt_len)
  818. {
  819. const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  820. unsigned int len;
  821. len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
  822. rq->min_buf_len, PAGE_SIZE - hdr_len);
  823. return ALIGN(len, L1_CACHE_BYTES);
  824. }
  825. static int add_recvbuf_mergeable(struct virtnet_info *vi,
  826. struct receive_queue *rq, gfp_t gfp)
  827. {
  828. struct page_frag *alloc_frag = &rq->alloc_frag;
  829. unsigned int headroom = virtnet_get_headroom(vi);
  830. char *buf;
  831. void *ctx;
  832. int err;
  833. unsigned int len, hole;
  834. len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len);
  835. if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp)))
  836. return -ENOMEM;
  837. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  838. buf += headroom; /* advance address leaving hole at front of pkt */
  839. get_page(alloc_frag->page);
  840. alloc_frag->offset += len + headroom;
  841. hole = alloc_frag->size - alloc_frag->offset;
  842. if (hole < len + headroom) {
  843. /* To avoid internal fragmentation, if there is very likely not
  844. * enough space for another buffer, add the remaining space to
  845. * the current buffer.
  846. */
  847. len += hole;
  848. alloc_frag->offset += hole;
  849. }
  850. sg_init_one(rq->sg, buf, len);
  851. ctx = mergeable_len_to_ctx(len, headroom);
  852. err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
  853. if (err < 0)
  854. put_page(virt_to_head_page(buf));
  855. return err;
  856. }
  857. /*
  858. * Returns false if we couldn't fill entirely (OOM).
  859. *
  860. * Normally run in the receive path, but can also be run from ndo_open
  861. * before we're receiving packets, or from refill_work which is
  862. * careful to disable receiving (using napi_disable).
  863. */
  864. static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
  865. gfp_t gfp)
  866. {
  867. int err;
  868. bool oom;
  869. gfp |= __GFP_COLD;
  870. do {
  871. if (vi->mergeable_rx_bufs)
  872. err = add_recvbuf_mergeable(vi, rq, gfp);
  873. else if (vi->big_packets)
  874. err = add_recvbuf_big(vi, rq, gfp);
  875. else
  876. err = add_recvbuf_small(vi, rq, gfp);
  877. oom = err == -ENOMEM;
  878. if (err)
  879. break;
  880. } while (rq->vq->num_free);
  881. virtqueue_kick(rq->vq);
  882. return !oom;
  883. }
  884. static void skb_recv_done(struct virtqueue *rvq)
  885. {
  886. struct virtnet_info *vi = rvq->vdev->priv;
  887. struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
  888. virtqueue_napi_schedule(&rq->napi, rvq);
  889. }
  890. static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
  891. {
  892. napi_enable(napi);
  893. /* If all buffers were filled by other side before we napi_enabled, we
  894. * won't get another interrupt, so process any outstanding packets now.
  895. * Call local_bh_enable after to trigger softIRQ processing.
  896. */
  897. local_bh_disable();
  898. virtqueue_napi_schedule(napi, vq);
  899. local_bh_enable();
  900. }
  901. static void virtnet_napi_tx_enable(struct virtnet_info *vi,
  902. struct virtqueue *vq,
  903. struct napi_struct *napi)
  904. {
  905. if (!napi->weight)
  906. return;
  907. /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
  908. * enable the feature if this is likely affine with the transmit path.
  909. */
  910. if (!vi->affinity_hint_set) {
  911. napi->weight = 0;
  912. return;
  913. }
  914. return virtnet_napi_enable(vq, napi);
  915. }
  916. static void virtnet_napi_tx_disable(struct napi_struct *napi)
  917. {
  918. if (napi->weight)
  919. napi_disable(napi);
  920. }
  921. static void refill_work(struct work_struct *work)
  922. {
  923. struct virtnet_info *vi =
  924. container_of(work, struct virtnet_info, refill.work);
  925. bool still_empty;
  926. int i;
  927. for (i = 0; i < vi->curr_queue_pairs; i++) {
  928. struct receive_queue *rq = &vi->rq[i];
  929. napi_disable(&rq->napi);
  930. still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
  931. virtnet_napi_enable(rq->vq, &rq->napi);
  932. /* In theory, this can happen: if we don't get any buffers in
  933. * we will *never* try to fill again.
  934. */
  935. if (still_empty)
  936. schedule_delayed_work(&vi->refill, HZ/2);
  937. }
  938. }
  939. static int virtnet_receive(struct receive_queue *rq, int budget, bool *xdp_xmit)
  940. {
  941. struct virtnet_info *vi = rq->vq->vdev->priv;
  942. unsigned int len, received = 0, bytes = 0;
  943. void *buf;
  944. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  945. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  946. void *ctx;
  947. while (received < budget &&
  948. (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
  949. bytes += receive_buf(vi, rq, buf, len, ctx, xdp_xmit);
  950. received++;
  951. }
  952. } else {
  953. while (received < budget &&
  954. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  955. bytes += receive_buf(vi, rq, buf, len, NULL, xdp_xmit);
  956. received++;
  957. }
  958. }
  959. if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
  960. if (!try_fill_recv(vi, rq, GFP_ATOMIC))
  961. schedule_delayed_work(&vi->refill, 0);
  962. }
  963. u64_stats_update_begin(&stats->rx_syncp);
  964. stats->rx_bytes += bytes;
  965. stats->rx_packets += received;
  966. u64_stats_update_end(&stats->rx_syncp);
  967. return received;
  968. }
  969. static void free_old_xmit_skbs(struct send_queue *sq)
  970. {
  971. struct sk_buff *skb;
  972. unsigned int len;
  973. struct virtnet_info *vi = sq->vq->vdev->priv;
  974. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  975. unsigned int packets = 0;
  976. unsigned int bytes = 0;
  977. while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  978. pr_debug("Sent skb %p\n", skb);
  979. bytes += skb->len;
  980. packets++;
  981. dev_consume_skb_any(skb);
  982. }
  983. /* Avoid overhead when no packets have been processed
  984. * happens when called speculatively from start_xmit.
  985. */
  986. if (!packets)
  987. return;
  988. u64_stats_update_begin(&stats->tx_syncp);
  989. stats->tx_bytes += bytes;
  990. stats->tx_packets += packets;
  991. u64_stats_update_end(&stats->tx_syncp);
  992. }
  993. static void virtnet_poll_cleantx(struct receive_queue *rq)
  994. {
  995. struct virtnet_info *vi = rq->vq->vdev->priv;
  996. unsigned int index = vq2rxq(rq->vq);
  997. struct send_queue *sq = &vi->sq[index];
  998. struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
  999. if (!sq->napi.weight)
  1000. return;
  1001. if (__netif_tx_trylock(txq)) {
  1002. free_old_xmit_skbs(sq);
  1003. __netif_tx_unlock(txq);
  1004. }
  1005. if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  1006. netif_tx_wake_queue(txq);
  1007. }
  1008. static int virtnet_poll(struct napi_struct *napi, int budget)
  1009. {
  1010. struct receive_queue *rq =
  1011. container_of(napi, struct receive_queue, napi);
  1012. unsigned int received;
  1013. bool xdp_xmit = false;
  1014. virtnet_poll_cleantx(rq);
  1015. received = virtnet_receive(rq, budget, &xdp_xmit);
  1016. /* Out of packets? */
  1017. if (received < budget)
  1018. virtqueue_napi_complete(napi, rq->vq, received);
  1019. if (xdp_xmit)
  1020. xdp_do_flush_map();
  1021. return received;
  1022. }
  1023. static int virtnet_open(struct net_device *dev)
  1024. {
  1025. struct virtnet_info *vi = netdev_priv(dev);
  1026. int i;
  1027. for (i = 0; i < vi->max_queue_pairs; i++) {
  1028. if (i < vi->curr_queue_pairs)
  1029. /* Make sure we have some buffers: if oom use wq. */
  1030. if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
  1031. schedule_delayed_work(&vi->refill, 0);
  1032. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  1033. virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
  1034. }
  1035. return 0;
  1036. }
  1037. static int virtnet_poll_tx(struct napi_struct *napi, int budget)
  1038. {
  1039. struct send_queue *sq = container_of(napi, struct send_queue, napi);
  1040. struct virtnet_info *vi = sq->vq->vdev->priv;
  1041. struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
  1042. __netif_tx_lock(txq, raw_smp_processor_id());
  1043. free_old_xmit_skbs(sq);
  1044. __netif_tx_unlock(txq);
  1045. virtqueue_napi_complete(napi, sq->vq, 0);
  1046. if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  1047. netif_tx_wake_queue(txq);
  1048. return 0;
  1049. }
  1050. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
  1051. {
  1052. struct virtio_net_hdr_mrg_rxbuf *hdr;
  1053. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  1054. struct virtnet_info *vi = sq->vq->vdev->priv;
  1055. int num_sg;
  1056. unsigned hdr_len = vi->hdr_len;
  1057. bool can_push;
  1058. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  1059. can_push = vi->any_header_sg &&
  1060. !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
  1061. !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
  1062. /* Even if we can, don't push here yet as this would skew
  1063. * csum_start offset below. */
  1064. if (can_push)
  1065. hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
  1066. else
  1067. hdr = skb_vnet_hdr(skb);
  1068. if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
  1069. virtio_is_little_endian(vi->vdev), false))
  1070. BUG();
  1071. if (vi->mergeable_rx_bufs)
  1072. hdr->num_buffers = 0;
  1073. sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
  1074. if (can_push) {
  1075. __skb_push(skb, hdr_len);
  1076. num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
  1077. if (unlikely(num_sg < 0))
  1078. return num_sg;
  1079. /* Pull header back to avoid skew in tx bytes calculations. */
  1080. __skb_pull(skb, hdr_len);
  1081. } else {
  1082. sg_set_buf(sq->sg, hdr, hdr_len);
  1083. num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
  1084. if (unlikely(num_sg < 0))
  1085. return num_sg;
  1086. num_sg++;
  1087. }
  1088. return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
  1089. }
  1090. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  1091. {
  1092. struct virtnet_info *vi = netdev_priv(dev);
  1093. int qnum = skb_get_queue_mapping(skb);
  1094. struct send_queue *sq = &vi->sq[qnum];
  1095. int err;
  1096. struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
  1097. bool kick = !skb->xmit_more;
  1098. bool use_napi = sq->napi.weight;
  1099. /* Free up any pending old buffers before queueing new ones. */
  1100. free_old_xmit_skbs(sq);
  1101. if (use_napi && kick)
  1102. virtqueue_enable_cb_delayed(sq->vq);
  1103. /* timestamp packet in software */
  1104. skb_tx_timestamp(skb);
  1105. /* Try to transmit */
  1106. err = xmit_skb(sq, skb);
  1107. /* This should not happen! */
  1108. if (unlikely(err)) {
  1109. dev->stats.tx_fifo_errors++;
  1110. if (net_ratelimit())
  1111. dev_warn(&dev->dev,
  1112. "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
  1113. dev->stats.tx_dropped++;
  1114. dev_kfree_skb_any(skb);
  1115. return NETDEV_TX_OK;
  1116. }
  1117. /* Don't wait up for transmitted skbs to be freed. */
  1118. if (!use_napi) {
  1119. skb_orphan(skb);
  1120. nf_reset(skb);
  1121. }
  1122. /* If running out of space, stop queue to avoid getting packets that we
  1123. * are then unable to transmit.
  1124. * An alternative would be to force queuing layer to requeue the skb by
  1125. * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
  1126. * returned in a normal path of operation: it means that driver is not
  1127. * maintaining the TX queue stop/start state properly, and causes
  1128. * the stack to do a non-trivial amount of useless work.
  1129. * Since most packets only take 1 or 2 ring slots, stopping the queue
  1130. * early means 16 slots are typically wasted.
  1131. */
  1132. if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
  1133. netif_stop_subqueue(dev, qnum);
  1134. if (!use_napi &&
  1135. unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
  1136. /* More just got used, free them then recheck. */
  1137. free_old_xmit_skbs(sq);
  1138. if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
  1139. netif_start_subqueue(dev, qnum);
  1140. virtqueue_disable_cb(sq->vq);
  1141. }
  1142. }
  1143. }
  1144. if (kick || netif_xmit_stopped(txq))
  1145. virtqueue_kick(sq->vq);
  1146. return NETDEV_TX_OK;
  1147. }
  1148. /*
  1149. * Send command via the control virtqueue and check status. Commands
  1150. * supported by the hypervisor, as indicated by feature bits, should
  1151. * never fail unless improperly formatted.
  1152. */
  1153. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  1154. struct scatterlist *out)
  1155. {
  1156. struct scatterlist *sgs[4], hdr, stat;
  1157. unsigned out_num = 0, tmp;
  1158. /* Caller should know better */
  1159. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
  1160. vi->ctrl_status = ~0;
  1161. vi->ctrl_hdr.class = class;
  1162. vi->ctrl_hdr.cmd = cmd;
  1163. /* Add header */
  1164. sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
  1165. sgs[out_num++] = &hdr;
  1166. if (out)
  1167. sgs[out_num++] = out;
  1168. /* Add return status. */
  1169. sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
  1170. sgs[out_num] = &stat;
  1171. BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
  1172. virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
  1173. if (unlikely(!virtqueue_kick(vi->cvq)))
  1174. return vi->ctrl_status == VIRTIO_NET_OK;
  1175. /* Spin for a response, the kick causes an ioport write, trapping
  1176. * into the hypervisor, so the request should be handled immediately.
  1177. */
  1178. while (!virtqueue_get_buf(vi->cvq, &tmp) &&
  1179. !virtqueue_is_broken(vi->cvq))
  1180. cpu_relax();
  1181. return vi->ctrl_status == VIRTIO_NET_OK;
  1182. }
  1183. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  1184. {
  1185. struct virtnet_info *vi = netdev_priv(dev);
  1186. struct virtio_device *vdev = vi->vdev;
  1187. int ret;
  1188. struct sockaddr *addr;
  1189. struct scatterlist sg;
  1190. addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
  1191. if (!addr)
  1192. return -ENOMEM;
  1193. ret = eth_prepare_mac_addr_change(dev, addr);
  1194. if (ret)
  1195. goto out;
  1196. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  1197. sg_init_one(&sg, addr->sa_data, dev->addr_len);
  1198. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  1199. VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
  1200. dev_warn(&vdev->dev,
  1201. "Failed to set mac address by vq command.\n");
  1202. ret = -EINVAL;
  1203. goto out;
  1204. }
  1205. } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
  1206. !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  1207. unsigned int i;
  1208. /* Naturally, this has an atomicity problem. */
  1209. for (i = 0; i < dev->addr_len; i++)
  1210. virtio_cwrite8(vdev,
  1211. offsetof(struct virtio_net_config, mac) +
  1212. i, addr->sa_data[i]);
  1213. }
  1214. eth_commit_mac_addr_change(dev, p);
  1215. ret = 0;
  1216. out:
  1217. kfree(addr);
  1218. return ret;
  1219. }
  1220. static void virtnet_stats(struct net_device *dev,
  1221. struct rtnl_link_stats64 *tot)
  1222. {
  1223. struct virtnet_info *vi = netdev_priv(dev);
  1224. int cpu;
  1225. unsigned int start;
  1226. for_each_possible_cpu(cpu) {
  1227. struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
  1228. u64 tpackets, tbytes, rpackets, rbytes;
  1229. do {
  1230. start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
  1231. tpackets = stats->tx_packets;
  1232. tbytes = stats->tx_bytes;
  1233. } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
  1234. do {
  1235. start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
  1236. rpackets = stats->rx_packets;
  1237. rbytes = stats->rx_bytes;
  1238. } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
  1239. tot->rx_packets += rpackets;
  1240. tot->tx_packets += tpackets;
  1241. tot->rx_bytes += rbytes;
  1242. tot->tx_bytes += tbytes;
  1243. }
  1244. tot->tx_dropped = dev->stats.tx_dropped;
  1245. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  1246. tot->rx_dropped = dev->stats.rx_dropped;
  1247. tot->rx_length_errors = dev->stats.rx_length_errors;
  1248. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  1249. }
  1250. #ifdef CONFIG_NET_POLL_CONTROLLER
  1251. static void virtnet_netpoll(struct net_device *dev)
  1252. {
  1253. struct virtnet_info *vi = netdev_priv(dev);
  1254. int i;
  1255. for (i = 0; i < vi->curr_queue_pairs; i++)
  1256. napi_schedule(&vi->rq[i].napi);
  1257. }
  1258. #endif
  1259. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  1260. {
  1261. rtnl_lock();
  1262. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  1263. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
  1264. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  1265. rtnl_unlock();
  1266. }
  1267. static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  1268. {
  1269. struct scatterlist sg;
  1270. struct net_device *dev = vi->dev;
  1271. if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
  1272. return 0;
  1273. vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
  1274. sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
  1275. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  1276. VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
  1277. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
  1278. queue_pairs);
  1279. return -EINVAL;
  1280. } else {
  1281. vi->curr_queue_pairs = queue_pairs;
  1282. /* virtnet_open() will refill when device is going to up. */
  1283. if (dev->flags & IFF_UP)
  1284. schedule_delayed_work(&vi->refill, 0);
  1285. }
  1286. return 0;
  1287. }
  1288. static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  1289. {
  1290. int err;
  1291. rtnl_lock();
  1292. err = _virtnet_set_queues(vi, queue_pairs);
  1293. rtnl_unlock();
  1294. return err;
  1295. }
  1296. static int virtnet_close(struct net_device *dev)
  1297. {
  1298. struct virtnet_info *vi = netdev_priv(dev);
  1299. int i;
  1300. /* Make sure refill_work doesn't re-enable napi! */
  1301. cancel_delayed_work_sync(&vi->refill);
  1302. for (i = 0; i < vi->max_queue_pairs; i++) {
  1303. napi_disable(&vi->rq[i].napi);
  1304. virtnet_napi_tx_disable(&vi->sq[i].napi);
  1305. }
  1306. return 0;
  1307. }
  1308. static void virtnet_set_rx_mode(struct net_device *dev)
  1309. {
  1310. struct virtnet_info *vi = netdev_priv(dev);
  1311. struct scatterlist sg[2];
  1312. struct virtio_net_ctrl_mac *mac_data;
  1313. struct netdev_hw_addr *ha;
  1314. int uc_count;
  1315. int mc_count;
  1316. void *buf;
  1317. int i;
  1318. /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
  1319. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  1320. return;
  1321. vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
  1322. vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  1323. sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
  1324. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  1325. VIRTIO_NET_CTRL_RX_PROMISC, sg))
  1326. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  1327. vi->ctrl_promisc ? "en" : "dis");
  1328. sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
  1329. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  1330. VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
  1331. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  1332. vi->ctrl_allmulti ? "en" : "dis");
  1333. uc_count = netdev_uc_count(dev);
  1334. mc_count = netdev_mc_count(dev);
  1335. /* MAC filter - use one buffer for both lists */
  1336. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  1337. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  1338. mac_data = buf;
  1339. if (!buf)
  1340. return;
  1341. sg_init_table(sg, 2);
  1342. /* Store the unicast list and count in the front of the buffer */
  1343. mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
  1344. i = 0;
  1345. netdev_for_each_uc_addr(ha, dev)
  1346. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  1347. sg_set_buf(&sg[0], mac_data,
  1348. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  1349. /* multicast list and count fill the end */
  1350. mac_data = (void *)&mac_data->macs[uc_count][0];
  1351. mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
  1352. i = 0;
  1353. netdev_for_each_mc_addr(ha, dev)
  1354. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  1355. sg_set_buf(&sg[1], mac_data,
  1356. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  1357. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  1358. VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
  1359. dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
  1360. kfree(buf);
  1361. }
  1362. static int virtnet_vlan_rx_add_vid(struct net_device *dev,
  1363. __be16 proto, u16 vid)
  1364. {
  1365. struct virtnet_info *vi = netdev_priv(dev);
  1366. struct scatterlist sg;
  1367. vi->ctrl_vid = vid;
  1368. sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
  1369. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1370. VIRTIO_NET_CTRL_VLAN_ADD, &sg))
  1371. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  1372. return 0;
  1373. }
  1374. static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
  1375. __be16 proto, u16 vid)
  1376. {
  1377. struct virtnet_info *vi = netdev_priv(dev);
  1378. struct scatterlist sg;
  1379. vi->ctrl_vid = vid;
  1380. sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
  1381. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1382. VIRTIO_NET_CTRL_VLAN_DEL, &sg))
  1383. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  1384. return 0;
  1385. }
  1386. static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
  1387. {
  1388. int i;
  1389. if (vi->affinity_hint_set) {
  1390. for (i = 0; i < vi->max_queue_pairs; i++) {
  1391. virtqueue_set_affinity(vi->rq[i].vq, -1);
  1392. virtqueue_set_affinity(vi->sq[i].vq, -1);
  1393. }
  1394. vi->affinity_hint_set = false;
  1395. }
  1396. }
  1397. static void virtnet_set_affinity(struct virtnet_info *vi)
  1398. {
  1399. int i;
  1400. int cpu;
  1401. /* In multiqueue mode, when the number of cpu is equal to the number of
  1402. * queue pairs, we let the queue pairs to be private to one cpu by
  1403. * setting the affinity hint to eliminate the contention.
  1404. */
  1405. if (vi->curr_queue_pairs == 1 ||
  1406. vi->max_queue_pairs != num_online_cpus()) {
  1407. virtnet_clean_affinity(vi, -1);
  1408. return;
  1409. }
  1410. i = 0;
  1411. for_each_online_cpu(cpu) {
  1412. virtqueue_set_affinity(vi->rq[i].vq, cpu);
  1413. virtqueue_set_affinity(vi->sq[i].vq, cpu);
  1414. netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
  1415. i++;
  1416. }
  1417. vi->affinity_hint_set = true;
  1418. }
  1419. static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
  1420. {
  1421. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1422. node);
  1423. virtnet_set_affinity(vi);
  1424. return 0;
  1425. }
  1426. static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
  1427. {
  1428. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1429. node_dead);
  1430. virtnet_set_affinity(vi);
  1431. return 0;
  1432. }
  1433. static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
  1434. {
  1435. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1436. node);
  1437. virtnet_clean_affinity(vi, cpu);
  1438. return 0;
  1439. }
  1440. static enum cpuhp_state virtionet_online;
  1441. static int virtnet_cpu_notif_add(struct virtnet_info *vi)
  1442. {
  1443. int ret;
  1444. ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
  1445. if (ret)
  1446. return ret;
  1447. ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  1448. &vi->node_dead);
  1449. if (!ret)
  1450. return ret;
  1451. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  1452. return ret;
  1453. }
  1454. static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
  1455. {
  1456. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  1457. cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  1458. &vi->node_dead);
  1459. }
  1460. static void virtnet_get_ringparam(struct net_device *dev,
  1461. struct ethtool_ringparam *ring)
  1462. {
  1463. struct virtnet_info *vi = netdev_priv(dev);
  1464. ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  1465. ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  1466. ring->rx_pending = ring->rx_max_pending;
  1467. ring->tx_pending = ring->tx_max_pending;
  1468. }
  1469. static void virtnet_get_drvinfo(struct net_device *dev,
  1470. struct ethtool_drvinfo *info)
  1471. {
  1472. struct virtnet_info *vi = netdev_priv(dev);
  1473. struct virtio_device *vdev = vi->vdev;
  1474. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  1475. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  1476. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  1477. }
  1478. /* TODO: Eliminate OOO packets during switching */
  1479. static int virtnet_set_channels(struct net_device *dev,
  1480. struct ethtool_channels *channels)
  1481. {
  1482. struct virtnet_info *vi = netdev_priv(dev);
  1483. u16 queue_pairs = channels->combined_count;
  1484. int err;
  1485. /* We don't support separate rx/tx channels.
  1486. * We don't allow setting 'other' channels.
  1487. */
  1488. if (channels->rx_count || channels->tx_count || channels->other_count)
  1489. return -EINVAL;
  1490. if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
  1491. return -EINVAL;
  1492. /* For now we don't support modifying channels while XDP is loaded
  1493. * also when XDP is loaded all RX queues have XDP programs so we only
  1494. * need to check a single RX queue.
  1495. */
  1496. if (vi->rq[0].xdp_prog)
  1497. return -EINVAL;
  1498. get_online_cpus();
  1499. err = _virtnet_set_queues(vi, queue_pairs);
  1500. if (!err) {
  1501. netif_set_real_num_tx_queues(dev, queue_pairs);
  1502. netif_set_real_num_rx_queues(dev, queue_pairs);
  1503. virtnet_set_affinity(vi);
  1504. }
  1505. put_online_cpus();
  1506. return err;
  1507. }
  1508. static void virtnet_get_channels(struct net_device *dev,
  1509. struct ethtool_channels *channels)
  1510. {
  1511. struct virtnet_info *vi = netdev_priv(dev);
  1512. channels->combined_count = vi->curr_queue_pairs;
  1513. channels->max_combined = vi->max_queue_pairs;
  1514. channels->max_other = 0;
  1515. channels->rx_count = 0;
  1516. channels->tx_count = 0;
  1517. channels->other_count = 0;
  1518. }
  1519. /* Check if the user is trying to change anything besides speed/duplex */
  1520. static bool
  1521. virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
  1522. {
  1523. struct ethtool_link_ksettings diff1 = *cmd;
  1524. struct ethtool_link_ksettings diff2 = {};
  1525. /* cmd is always set so we need to clear it, validate the port type
  1526. * and also without autonegotiation we can ignore advertising
  1527. */
  1528. diff1.base.speed = 0;
  1529. diff2.base.port = PORT_OTHER;
  1530. ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
  1531. diff1.base.duplex = 0;
  1532. diff1.base.cmd = 0;
  1533. diff1.base.link_mode_masks_nwords = 0;
  1534. return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
  1535. bitmap_empty(diff1.link_modes.supported,
  1536. __ETHTOOL_LINK_MODE_MASK_NBITS) &&
  1537. bitmap_empty(diff1.link_modes.advertising,
  1538. __ETHTOOL_LINK_MODE_MASK_NBITS) &&
  1539. bitmap_empty(diff1.link_modes.lp_advertising,
  1540. __ETHTOOL_LINK_MODE_MASK_NBITS);
  1541. }
  1542. static int virtnet_set_link_ksettings(struct net_device *dev,
  1543. const struct ethtool_link_ksettings *cmd)
  1544. {
  1545. struct virtnet_info *vi = netdev_priv(dev);
  1546. u32 speed;
  1547. speed = cmd->base.speed;
  1548. /* don't allow custom speed and duplex */
  1549. if (!ethtool_validate_speed(speed) ||
  1550. !ethtool_validate_duplex(cmd->base.duplex) ||
  1551. !virtnet_validate_ethtool_cmd(cmd))
  1552. return -EINVAL;
  1553. vi->speed = speed;
  1554. vi->duplex = cmd->base.duplex;
  1555. return 0;
  1556. }
  1557. static int virtnet_get_link_ksettings(struct net_device *dev,
  1558. struct ethtool_link_ksettings *cmd)
  1559. {
  1560. struct virtnet_info *vi = netdev_priv(dev);
  1561. cmd->base.speed = vi->speed;
  1562. cmd->base.duplex = vi->duplex;
  1563. cmd->base.port = PORT_OTHER;
  1564. return 0;
  1565. }
  1566. static void virtnet_init_settings(struct net_device *dev)
  1567. {
  1568. struct virtnet_info *vi = netdev_priv(dev);
  1569. vi->speed = SPEED_UNKNOWN;
  1570. vi->duplex = DUPLEX_UNKNOWN;
  1571. }
  1572. static const struct ethtool_ops virtnet_ethtool_ops = {
  1573. .get_drvinfo = virtnet_get_drvinfo,
  1574. .get_link = ethtool_op_get_link,
  1575. .get_ringparam = virtnet_get_ringparam,
  1576. .set_channels = virtnet_set_channels,
  1577. .get_channels = virtnet_get_channels,
  1578. .get_ts_info = ethtool_op_get_ts_info,
  1579. .get_link_ksettings = virtnet_get_link_ksettings,
  1580. .set_link_ksettings = virtnet_set_link_ksettings,
  1581. };
  1582. static void virtnet_freeze_down(struct virtio_device *vdev)
  1583. {
  1584. struct virtnet_info *vi = vdev->priv;
  1585. int i;
  1586. /* Make sure no work handler is accessing the device */
  1587. flush_work(&vi->config_work);
  1588. netif_device_detach(vi->dev);
  1589. netif_tx_disable(vi->dev);
  1590. cancel_delayed_work_sync(&vi->refill);
  1591. if (netif_running(vi->dev)) {
  1592. for (i = 0; i < vi->max_queue_pairs; i++) {
  1593. napi_disable(&vi->rq[i].napi);
  1594. virtnet_napi_tx_disable(&vi->sq[i].napi);
  1595. }
  1596. }
  1597. }
  1598. static int init_vqs(struct virtnet_info *vi);
  1599. static int virtnet_restore_up(struct virtio_device *vdev)
  1600. {
  1601. struct virtnet_info *vi = vdev->priv;
  1602. int err, i;
  1603. err = init_vqs(vi);
  1604. if (err)
  1605. return err;
  1606. virtio_device_ready(vdev);
  1607. if (netif_running(vi->dev)) {
  1608. for (i = 0; i < vi->curr_queue_pairs; i++)
  1609. if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
  1610. schedule_delayed_work(&vi->refill, 0);
  1611. for (i = 0; i < vi->max_queue_pairs; i++) {
  1612. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  1613. virtnet_napi_tx_enable(vi, vi->sq[i].vq,
  1614. &vi->sq[i].napi);
  1615. }
  1616. }
  1617. netif_device_attach(vi->dev);
  1618. return err;
  1619. }
  1620. static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
  1621. {
  1622. struct scatterlist sg;
  1623. vi->ctrl_offloads = cpu_to_virtio64(vi->vdev, offloads);
  1624. sg_init_one(&sg, &vi->ctrl_offloads, sizeof(vi->ctrl_offloads));
  1625. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
  1626. VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
  1627. dev_warn(&vi->dev->dev, "Fail to set guest offload. \n");
  1628. return -EINVAL;
  1629. }
  1630. return 0;
  1631. }
  1632. static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
  1633. {
  1634. u64 offloads = 0;
  1635. if (!vi->guest_offloads)
  1636. return 0;
  1637. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
  1638. offloads = 1ULL << VIRTIO_NET_F_GUEST_CSUM;
  1639. return virtnet_set_guest_offloads(vi, offloads);
  1640. }
  1641. static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
  1642. {
  1643. u64 offloads = vi->guest_offloads;
  1644. if (!vi->guest_offloads)
  1645. return 0;
  1646. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
  1647. offloads |= 1ULL << VIRTIO_NET_F_GUEST_CSUM;
  1648. return virtnet_set_guest_offloads(vi, offloads);
  1649. }
  1650. static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  1651. struct netlink_ext_ack *extack)
  1652. {
  1653. unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
  1654. struct virtnet_info *vi = netdev_priv(dev);
  1655. struct bpf_prog *old_prog;
  1656. u16 xdp_qp = 0, curr_qp;
  1657. int i, err;
  1658. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
  1659. && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  1660. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  1661. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
  1662. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO))) {
  1663. NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO, disable LRO first");
  1664. return -EOPNOTSUPP;
  1665. }
  1666. if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
  1667. NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
  1668. return -EINVAL;
  1669. }
  1670. if (dev->mtu > max_sz) {
  1671. NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
  1672. netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
  1673. return -EINVAL;
  1674. }
  1675. curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
  1676. if (prog)
  1677. xdp_qp = nr_cpu_ids;
  1678. /* XDP requires extra queues for XDP_TX */
  1679. if (curr_qp + xdp_qp > vi->max_queue_pairs) {
  1680. NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available");
  1681. netdev_warn(dev, "request %i queues but max is %i\n",
  1682. curr_qp + xdp_qp, vi->max_queue_pairs);
  1683. return -ENOMEM;
  1684. }
  1685. if (prog) {
  1686. prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
  1687. if (IS_ERR(prog))
  1688. return PTR_ERR(prog);
  1689. }
  1690. /* Make sure NAPI is not using any XDP TX queues for RX. */
  1691. for (i = 0; i < vi->max_queue_pairs; i++)
  1692. napi_disable(&vi->rq[i].napi);
  1693. netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
  1694. err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
  1695. if (err)
  1696. goto err;
  1697. vi->xdp_queue_pairs = xdp_qp;
  1698. for (i = 0; i < vi->max_queue_pairs; i++) {
  1699. old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  1700. rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
  1701. if (i == 0) {
  1702. if (!old_prog)
  1703. virtnet_clear_guest_offloads(vi);
  1704. if (!prog)
  1705. virtnet_restore_guest_offloads(vi);
  1706. }
  1707. if (old_prog)
  1708. bpf_prog_put(old_prog);
  1709. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  1710. }
  1711. return 0;
  1712. err:
  1713. for (i = 0; i < vi->max_queue_pairs; i++)
  1714. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  1715. if (prog)
  1716. bpf_prog_sub(prog, vi->max_queue_pairs - 1);
  1717. return err;
  1718. }
  1719. static u32 virtnet_xdp_query(struct net_device *dev)
  1720. {
  1721. struct virtnet_info *vi = netdev_priv(dev);
  1722. const struct bpf_prog *xdp_prog;
  1723. int i;
  1724. for (i = 0; i < vi->max_queue_pairs; i++) {
  1725. xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  1726. if (xdp_prog)
  1727. return xdp_prog->aux->id;
  1728. }
  1729. return 0;
  1730. }
  1731. static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
  1732. {
  1733. switch (xdp->command) {
  1734. case XDP_SETUP_PROG:
  1735. return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
  1736. case XDP_QUERY_PROG:
  1737. xdp->prog_id = virtnet_xdp_query(dev);
  1738. xdp->prog_attached = !!xdp->prog_id;
  1739. return 0;
  1740. default:
  1741. return -EINVAL;
  1742. }
  1743. }
  1744. static const struct net_device_ops virtnet_netdev = {
  1745. .ndo_open = virtnet_open,
  1746. .ndo_stop = virtnet_close,
  1747. .ndo_start_xmit = start_xmit,
  1748. .ndo_validate_addr = eth_validate_addr,
  1749. .ndo_set_mac_address = virtnet_set_mac_address,
  1750. .ndo_set_rx_mode = virtnet_set_rx_mode,
  1751. .ndo_get_stats64 = virtnet_stats,
  1752. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  1753. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  1754. #ifdef CONFIG_NET_POLL_CONTROLLER
  1755. .ndo_poll_controller = virtnet_netpoll,
  1756. #endif
  1757. .ndo_xdp = virtnet_xdp,
  1758. .ndo_xdp_xmit = virtnet_xdp_xmit,
  1759. .ndo_xdp_flush = virtnet_xdp_flush,
  1760. .ndo_features_check = passthru_features_check,
  1761. };
  1762. static void virtnet_config_changed_work(struct work_struct *work)
  1763. {
  1764. struct virtnet_info *vi =
  1765. container_of(work, struct virtnet_info, config_work);
  1766. u16 v;
  1767. if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
  1768. struct virtio_net_config, status, &v) < 0)
  1769. return;
  1770. if (v & VIRTIO_NET_S_ANNOUNCE) {
  1771. netdev_notify_peers(vi->dev);
  1772. virtnet_ack_link_announce(vi);
  1773. }
  1774. /* Ignore unknown (future) status bits */
  1775. v &= VIRTIO_NET_S_LINK_UP;
  1776. if (vi->status == v)
  1777. return;
  1778. vi->status = v;
  1779. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  1780. netif_carrier_on(vi->dev);
  1781. netif_tx_wake_all_queues(vi->dev);
  1782. } else {
  1783. netif_carrier_off(vi->dev);
  1784. netif_tx_stop_all_queues(vi->dev);
  1785. }
  1786. }
  1787. static void virtnet_config_changed(struct virtio_device *vdev)
  1788. {
  1789. struct virtnet_info *vi = vdev->priv;
  1790. schedule_work(&vi->config_work);
  1791. }
  1792. static void virtnet_free_queues(struct virtnet_info *vi)
  1793. {
  1794. int i;
  1795. for (i = 0; i < vi->max_queue_pairs; i++) {
  1796. napi_hash_del(&vi->rq[i].napi);
  1797. netif_napi_del(&vi->rq[i].napi);
  1798. netif_napi_del(&vi->sq[i].napi);
  1799. }
  1800. /* We called napi_hash_del() before netif_napi_del(),
  1801. * we need to respect an RCU grace period before freeing vi->rq
  1802. */
  1803. synchronize_net();
  1804. kfree(vi->rq);
  1805. kfree(vi->sq);
  1806. }
  1807. static void _free_receive_bufs(struct virtnet_info *vi)
  1808. {
  1809. struct bpf_prog *old_prog;
  1810. int i;
  1811. for (i = 0; i < vi->max_queue_pairs; i++) {
  1812. while (vi->rq[i].pages)
  1813. __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
  1814. old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  1815. RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
  1816. if (old_prog)
  1817. bpf_prog_put(old_prog);
  1818. }
  1819. }
  1820. static void free_receive_bufs(struct virtnet_info *vi)
  1821. {
  1822. rtnl_lock();
  1823. _free_receive_bufs(vi);
  1824. rtnl_unlock();
  1825. }
  1826. static void free_receive_page_frags(struct virtnet_info *vi)
  1827. {
  1828. int i;
  1829. for (i = 0; i < vi->max_queue_pairs; i++)
  1830. if (vi->rq[i].alloc_frag.page)
  1831. put_page(vi->rq[i].alloc_frag.page);
  1832. }
  1833. static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
  1834. {
  1835. if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
  1836. return false;
  1837. else if (q < vi->curr_queue_pairs)
  1838. return true;
  1839. else
  1840. return false;
  1841. }
  1842. static void free_unused_bufs(struct virtnet_info *vi)
  1843. {
  1844. void *buf;
  1845. int i;
  1846. for (i = 0; i < vi->max_queue_pairs; i++) {
  1847. struct virtqueue *vq = vi->sq[i].vq;
  1848. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  1849. if (!is_xdp_raw_buffer_queue(vi, i))
  1850. dev_kfree_skb(buf);
  1851. else
  1852. put_page(virt_to_head_page(buf));
  1853. }
  1854. }
  1855. for (i = 0; i < vi->max_queue_pairs; i++) {
  1856. struct virtqueue *vq = vi->rq[i].vq;
  1857. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  1858. if (vi->mergeable_rx_bufs) {
  1859. put_page(virt_to_head_page(buf));
  1860. } else if (vi->big_packets) {
  1861. give_pages(&vi->rq[i], buf);
  1862. } else {
  1863. put_page(virt_to_head_page(buf));
  1864. }
  1865. }
  1866. }
  1867. }
  1868. static void virtnet_del_vqs(struct virtnet_info *vi)
  1869. {
  1870. struct virtio_device *vdev = vi->vdev;
  1871. virtnet_clean_affinity(vi, -1);
  1872. vdev->config->del_vqs(vdev);
  1873. virtnet_free_queues(vi);
  1874. }
  1875. /* How large should a single buffer be so a queue full of these can fit at
  1876. * least one full packet?
  1877. * Logic below assumes the mergeable buffer header is used.
  1878. */
  1879. static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
  1880. {
  1881. const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  1882. unsigned int rq_size = virtqueue_get_vring_size(vq);
  1883. unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
  1884. unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
  1885. unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
  1886. return max(max(min_buf_len, hdr_len) - hdr_len,
  1887. (unsigned int)GOOD_PACKET_LEN);
  1888. }
  1889. static int virtnet_find_vqs(struct virtnet_info *vi)
  1890. {
  1891. vq_callback_t **callbacks;
  1892. struct virtqueue **vqs;
  1893. int ret = -ENOMEM;
  1894. int i, total_vqs;
  1895. const char **names;
  1896. bool *ctx;
  1897. /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
  1898. * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
  1899. * possible control vq.
  1900. */
  1901. total_vqs = vi->max_queue_pairs * 2 +
  1902. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
  1903. /* Allocate space for find_vqs parameters */
  1904. vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
  1905. if (!vqs)
  1906. goto err_vq;
  1907. callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
  1908. if (!callbacks)
  1909. goto err_callback;
  1910. names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
  1911. if (!names)
  1912. goto err_names;
  1913. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  1914. ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL);
  1915. if (!ctx)
  1916. goto err_ctx;
  1917. } else {
  1918. ctx = NULL;
  1919. }
  1920. /* Parameters for control virtqueue, if any */
  1921. if (vi->has_cvq) {
  1922. callbacks[total_vqs - 1] = NULL;
  1923. names[total_vqs - 1] = "control";
  1924. }
  1925. /* Allocate/initialize parameters for send/receive virtqueues */
  1926. for (i = 0; i < vi->max_queue_pairs; i++) {
  1927. callbacks[rxq2vq(i)] = skb_recv_done;
  1928. callbacks[txq2vq(i)] = skb_xmit_done;
  1929. sprintf(vi->rq[i].name, "input.%d", i);
  1930. sprintf(vi->sq[i].name, "output.%d", i);
  1931. names[rxq2vq(i)] = vi->rq[i].name;
  1932. names[txq2vq(i)] = vi->sq[i].name;
  1933. if (ctx)
  1934. ctx[rxq2vq(i)] = true;
  1935. }
  1936. ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
  1937. names, ctx, NULL);
  1938. if (ret)
  1939. goto err_find;
  1940. if (vi->has_cvq) {
  1941. vi->cvq = vqs[total_vqs - 1];
  1942. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  1943. vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1944. }
  1945. for (i = 0; i < vi->max_queue_pairs; i++) {
  1946. vi->rq[i].vq = vqs[rxq2vq(i)];
  1947. vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
  1948. vi->sq[i].vq = vqs[txq2vq(i)];
  1949. }
  1950. kfree(names);
  1951. kfree(callbacks);
  1952. kfree(vqs);
  1953. kfree(ctx);
  1954. return 0;
  1955. err_find:
  1956. kfree(ctx);
  1957. err_ctx:
  1958. kfree(names);
  1959. err_names:
  1960. kfree(callbacks);
  1961. err_callback:
  1962. kfree(vqs);
  1963. err_vq:
  1964. return ret;
  1965. }
  1966. static int virtnet_alloc_queues(struct virtnet_info *vi)
  1967. {
  1968. int i;
  1969. vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
  1970. if (!vi->sq)
  1971. goto err_sq;
  1972. vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
  1973. if (!vi->rq)
  1974. goto err_rq;
  1975. INIT_DELAYED_WORK(&vi->refill, refill_work);
  1976. for (i = 0; i < vi->max_queue_pairs; i++) {
  1977. vi->rq[i].pages = NULL;
  1978. netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
  1979. napi_weight);
  1980. netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
  1981. napi_tx ? napi_weight : 0);
  1982. sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
  1983. ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
  1984. sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
  1985. }
  1986. return 0;
  1987. err_rq:
  1988. kfree(vi->sq);
  1989. err_sq:
  1990. return -ENOMEM;
  1991. }
  1992. static int init_vqs(struct virtnet_info *vi)
  1993. {
  1994. int ret;
  1995. /* Allocate send & receive queues */
  1996. ret = virtnet_alloc_queues(vi);
  1997. if (ret)
  1998. goto err;
  1999. ret = virtnet_find_vqs(vi);
  2000. if (ret)
  2001. goto err_free;
  2002. get_online_cpus();
  2003. virtnet_set_affinity(vi);
  2004. put_online_cpus();
  2005. return 0;
  2006. err_free:
  2007. virtnet_free_queues(vi);
  2008. err:
  2009. return ret;
  2010. }
  2011. #ifdef CONFIG_SYSFS
  2012. static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
  2013. char *buf)
  2014. {
  2015. struct virtnet_info *vi = netdev_priv(queue->dev);
  2016. unsigned int queue_index = get_netdev_rx_queue_index(queue);
  2017. struct ewma_pkt_len *avg;
  2018. BUG_ON(queue_index >= vi->max_queue_pairs);
  2019. avg = &vi->rq[queue_index].mrg_avg_pkt_len;
  2020. return sprintf(buf, "%u\n",
  2021. get_mergeable_buf_len(&vi->rq[queue_index], avg));
  2022. }
  2023. static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
  2024. __ATTR_RO(mergeable_rx_buffer_size);
  2025. static struct attribute *virtio_net_mrg_rx_attrs[] = {
  2026. &mergeable_rx_buffer_size_attribute.attr,
  2027. NULL
  2028. };
  2029. static const struct attribute_group virtio_net_mrg_rx_group = {
  2030. .name = "virtio_net",
  2031. .attrs = virtio_net_mrg_rx_attrs
  2032. };
  2033. #endif
  2034. static bool virtnet_fail_on_feature(struct virtio_device *vdev,
  2035. unsigned int fbit,
  2036. const char *fname, const char *dname)
  2037. {
  2038. if (!virtio_has_feature(vdev, fbit))
  2039. return false;
  2040. dev_err(&vdev->dev, "device advertises feature %s but not %s",
  2041. fname, dname);
  2042. return true;
  2043. }
  2044. #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
  2045. virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
  2046. static bool virtnet_validate_features(struct virtio_device *vdev)
  2047. {
  2048. if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
  2049. (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
  2050. "VIRTIO_NET_F_CTRL_VQ") ||
  2051. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
  2052. "VIRTIO_NET_F_CTRL_VQ") ||
  2053. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
  2054. "VIRTIO_NET_F_CTRL_VQ") ||
  2055. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
  2056. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
  2057. "VIRTIO_NET_F_CTRL_VQ"))) {
  2058. return false;
  2059. }
  2060. return true;
  2061. }
  2062. #define MIN_MTU ETH_MIN_MTU
  2063. #define MAX_MTU ETH_MAX_MTU
  2064. static int virtnet_validate(struct virtio_device *vdev)
  2065. {
  2066. if (!vdev->config->get) {
  2067. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  2068. __func__);
  2069. return -EINVAL;
  2070. }
  2071. if (!virtnet_validate_features(vdev))
  2072. return -EINVAL;
  2073. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  2074. int mtu = virtio_cread16(vdev,
  2075. offsetof(struct virtio_net_config,
  2076. mtu));
  2077. if (mtu < MIN_MTU)
  2078. __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
  2079. }
  2080. return 0;
  2081. }
  2082. static int virtnet_probe(struct virtio_device *vdev)
  2083. {
  2084. int i, err;
  2085. struct net_device *dev;
  2086. struct virtnet_info *vi;
  2087. u16 max_queue_pairs;
  2088. int mtu;
  2089. /* Find if host supports multiqueue virtio_net device */
  2090. err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
  2091. struct virtio_net_config,
  2092. max_virtqueue_pairs, &max_queue_pairs);
  2093. /* We need at least 2 queue's */
  2094. if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  2095. max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  2096. !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  2097. max_queue_pairs = 1;
  2098. /* Allocate ourselves a network device with room for our info */
  2099. dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
  2100. if (!dev)
  2101. return -ENOMEM;
  2102. /* Set up network device as normal. */
  2103. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  2104. dev->netdev_ops = &virtnet_netdev;
  2105. dev->features = NETIF_F_HIGHDMA;
  2106. dev->ethtool_ops = &virtnet_ethtool_ops;
  2107. SET_NETDEV_DEV(dev, &vdev->dev);
  2108. /* Do we support "hardware" checksums? */
  2109. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  2110. /* This opens up the world of extra features. */
  2111. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  2112. if (csum)
  2113. dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  2114. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  2115. dev->hw_features |= NETIF_F_TSO
  2116. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  2117. }
  2118. /* Individual feature bits: what can host handle? */
  2119. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  2120. dev->hw_features |= NETIF_F_TSO;
  2121. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  2122. dev->hw_features |= NETIF_F_TSO6;
  2123. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  2124. dev->hw_features |= NETIF_F_TSO_ECN;
  2125. dev->features |= NETIF_F_GSO_ROBUST;
  2126. if (gso)
  2127. dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
  2128. /* (!csum && gso) case will be fixed by register_netdev() */
  2129. }
  2130. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
  2131. dev->features |= NETIF_F_RXCSUM;
  2132. dev->vlan_features = dev->features;
  2133. /* MTU range: 68 - 65535 */
  2134. dev->min_mtu = MIN_MTU;
  2135. dev->max_mtu = MAX_MTU;
  2136. /* Configuration may specify what MAC to use. Otherwise random. */
  2137. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  2138. virtio_cread_bytes(vdev,
  2139. offsetof(struct virtio_net_config, mac),
  2140. dev->dev_addr, dev->addr_len);
  2141. else
  2142. eth_hw_addr_random(dev);
  2143. /* Set up our device-specific information */
  2144. vi = netdev_priv(dev);
  2145. vi->dev = dev;
  2146. vi->vdev = vdev;
  2147. vdev->priv = vi;
  2148. vi->stats = alloc_percpu(struct virtnet_stats);
  2149. err = -ENOMEM;
  2150. if (vi->stats == NULL)
  2151. goto free;
  2152. for_each_possible_cpu(i) {
  2153. struct virtnet_stats *virtnet_stats;
  2154. virtnet_stats = per_cpu_ptr(vi->stats, i);
  2155. u64_stats_init(&virtnet_stats->tx_syncp);
  2156. u64_stats_init(&virtnet_stats->rx_syncp);
  2157. }
  2158. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  2159. /* If we can receive ANY GSO packets, we must allocate large ones. */
  2160. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  2161. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  2162. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
  2163. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
  2164. vi->big_packets = true;
  2165. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  2166. vi->mergeable_rx_bufs = true;
  2167. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
  2168. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  2169. vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  2170. else
  2171. vi->hdr_len = sizeof(struct virtio_net_hdr);
  2172. if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
  2173. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  2174. vi->any_header_sg = true;
  2175. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  2176. vi->has_cvq = true;
  2177. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  2178. mtu = virtio_cread16(vdev,
  2179. offsetof(struct virtio_net_config,
  2180. mtu));
  2181. if (mtu < dev->min_mtu) {
  2182. /* Should never trigger: MTU was previously validated
  2183. * in virtnet_validate.
  2184. */
  2185. dev_err(&vdev->dev, "device MTU appears to have changed "
  2186. "it is now %d < %d", mtu, dev->min_mtu);
  2187. goto free_stats;
  2188. }
  2189. dev->mtu = mtu;
  2190. dev->max_mtu = mtu;
  2191. /* TODO: size buffers correctly in this case. */
  2192. if (dev->mtu > ETH_DATA_LEN)
  2193. vi->big_packets = true;
  2194. }
  2195. if (vi->any_header_sg)
  2196. dev->needed_headroom = vi->hdr_len;
  2197. /* Enable multiqueue by default */
  2198. if (num_online_cpus() >= max_queue_pairs)
  2199. vi->curr_queue_pairs = max_queue_pairs;
  2200. else
  2201. vi->curr_queue_pairs = num_online_cpus();
  2202. vi->max_queue_pairs = max_queue_pairs;
  2203. /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
  2204. err = init_vqs(vi);
  2205. if (err)
  2206. goto free_stats;
  2207. #ifdef CONFIG_SYSFS
  2208. if (vi->mergeable_rx_bufs)
  2209. dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
  2210. #endif
  2211. netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
  2212. netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
  2213. virtnet_init_settings(dev);
  2214. err = register_netdev(dev);
  2215. if (err) {
  2216. pr_debug("virtio_net: registering device failed\n");
  2217. goto free_vqs;
  2218. }
  2219. virtio_device_ready(vdev);
  2220. err = virtnet_cpu_notif_add(vi);
  2221. if (err) {
  2222. pr_debug("virtio_net: registering cpu notifier failed\n");
  2223. goto free_unregister_netdev;
  2224. }
  2225. virtnet_set_queues(vi, vi->curr_queue_pairs);
  2226. /* Assume link up if device can't report link status,
  2227. otherwise get link status from config. */
  2228. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  2229. netif_carrier_off(dev);
  2230. schedule_work(&vi->config_work);
  2231. } else {
  2232. vi->status = VIRTIO_NET_S_LINK_UP;
  2233. netif_carrier_on(dev);
  2234. }
  2235. for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
  2236. if (virtio_has_feature(vi->vdev, guest_offloads[i]))
  2237. set_bit(guest_offloads[i], &vi->guest_offloads);
  2238. pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
  2239. dev->name, max_queue_pairs);
  2240. return 0;
  2241. free_unregister_netdev:
  2242. vi->vdev->config->reset(vdev);
  2243. unregister_netdev(dev);
  2244. free_vqs:
  2245. cancel_delayed_work_sync(&vi->refill);
  2246. free_receive_page_frags(vi);
  2247. virtnet_del_vqs(vi);
  2248. free_stats:
  2249. free_percpu(vi->stats);
  2250. free:
  2251. free_netdev(dev);
  2252. return err;
  2253. }
  2254. static void remove_vq_common(struct virtnet_info *vi)
  2255. {
  2256. vi->vdev->config->reset(vi->vdev);
  2257. /* Free unused buffers in both send and recv, if any. */
  2258. free_unused_bufs(vi);
  2259. free_receive_bufs(vi);
  2260. free_receive_page_frags(vi);
  2261. virtnet_del_vqs(vi);
  2262. }
  2263. static void virtnet_remove(struct virtio_device *vdev)
  2264. {
  2265. struct virtnet_info *vi = vdev->priv;
  2266. virtnet_cpu_notif_remove(vi);
  2267. /* Make sure no work handler is accessing the device. */
  2268. flush_work(&vi->config_work);
  2269. unregister_netdev(vi->dev);
  2270. remove_vq_common(vi);
  2271. free_percpu(vi->stats);
  2272. free_netdev(vi->dev);
  2273. }
  2274. static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
  2275. {
  2276. struct virtnet_info *vi = vdev->priv;
  2277. virtnet_cpu_notif_remove(vi);
  2278. virtnet_freeze_down(vdev);
  2279. remove_vq_common(vi);
  2280. return 0;
  2281. }
  2282. static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
  2283. {
  2284. struct virtnet_info *vi = vdev->priv;
  2285. int err;
  2286. err = virtnet_restore_up(vdev);
  2287. if (err)
  2288. return err;
  2289. virtnet_set_queues(vi, vi->curr_queue_pairs);
  2290. err = virtnet_cpu_notif_add(vi);
  2291. if (err)
  2292. return err;
  2293. return 0;
  2294. }
  2295. static struct virtio_device_id id_table[] = {
  2296. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  2297. { 0 },
  2298. };
  2299. #define VIRTNET_FEATURES \
  2300. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
  2301. VIRTIO_NET_F_MAC, \
  2302. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
  2303. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
  2304. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
  2305. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
  2306. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
  2307. VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
  2308. VIRTIO_NET_F_CTRL_MAC_ADDR, \
  2309. VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
  2310. static unsigned int features[] = {
  2311. VIRTNET_FEATURES,
  2312. };
  2313. static unsigned int features_legacy[] = {
  2314. VIRTNET_FEATURES,
  2315. VIRTIO_NET_F_GSO,
  2316. VIRTIO_F_ANY_LAYOUT,
  2317. };
  2318. static struct virtio_driver virtio_net_driver = {
  2319. .feature_table = features,
  2320. .feature_table_size = ARRAY_SIZE(features),
  2321. .feature_table_legacy = features_legacy,
  2322. .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
  2323. .driver.name = KBUILD_MODNAME,
  2324. .driver.owner = THIS_MODULE,
  2325. .id_table = id_table,
  2326. .validate = virtnet_validate,
  2327. .probe = virtnet_probe,
  2328. .remove = virtnet_remove,
  2329. .config_changed = virtnet_config_changed,
  2330. #ifdef CONFIG_PM_SLEEP
  2331. .freeze = virtnet_freeze,
  2332. .restore = virtnet_restore,
  2333. #endif
  2334. };
  2335. static __init int virtio_net_driver_init(void)
  2336. {
  2337. int ret;
  2338. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
  2339. virtnet_cpu_online,
  2340. virtnet_cpu_down_prep);
  2341. if (ret < 0)
  2342. goto out;
  2343. virtionet_online = ret;
  2344. ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
  2345. NULL, virtnet_cpu_dead);
  2346. if (ret)
  2347. goto err_dead;
  2348. ret = register_virtio_driver(&virtio_net_driver);
  2349. if (ret)
  2350. goto err_virtio;
  2351. return 0;
  2352. err_virtio:
  2353. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  2354. err_dead:
  2355. cpuhp_remove_multi_state(virtionet_online);
  2356. out:
  2357. return ret;
  2358. }
  2359. module_init(virtio_net_driver_init);
  2360. static __exit void virtio_net_driver_exit(void)
  2361. {
  2362. unregister_virtio_driver(&virtio_net_driver);
  2363. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  2364. cpuhp_remove_multi_state(virtionet_online);
  2365. }
  2366. module_exit(virtio_net_driver_exit);
  2367. MODULE_DEVICE_TABLE(virtio, id_table);
  2368. MODULE_DESCRIPTION("Virtio network driver");
  2369. MODULE_LICENSE("GPL");