virtio_net.c 80 KB

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