virtio_net.c 83 KB

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