virtio_net.c 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* A network driver using virtio.
  3. *
  4. * Copyright 2007 Rusty Russell <[email protected]> IBM Corporation
  5. */
  6. //#define DEBUG
  7. #include <linux/netdevice.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/ethtool.h>
  10. #include <linux/module.h>
  11. #include <linux/virtio.h>
  12. #include <linux/virtio_net.h>
  13. #include <linux/bpf.h>
  14. #include <linux/bpf_trace.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/slab.h>
  18. #include <linux/cpu.h>
  19. #include <linux/average.h>
  20. #include <linux/filter.h>
  21. #include <linux/kernel.h>
  22. #include <net/route.h>
  23. #include <net/xdp.h>
  24. #include <net/net_failover.h>
  25. static int napi_weight = NAPI_POLL_WEIGHT;
  26. module_param(napi_weight, int, 0444);
  27. static bool csum = true, gso = true, napi_tx = true;
  28. module_param(csum, bool, 0444);
  29. module_param(gso, bool, 0444);
  30. module_param(napi_tx, bool, 0644);
  31. /* FIXME: MTU in config. */
  32. #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
  33. #define GOOD_COPY_LEN 128
  34. #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
  35. /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
  36. #define VIRTIO_XDP_HEADROOM 256
  37. /* Separating two types of XDP xmit */
  38. #define VIRTIO_XDP_TX BIT(0)
  39. #define VIRTIO_XDP_REDIR BIT(1)
  40. #define VIRTIO_XDP_FLAG BIT(0)
  41. /* Timestamp length */
  42. #define HW_TIMESTAMP_LEN 8
  43. /* Guest can handle H/W timestamp in. */
  44. #define VIRTIO_NET_F_GUEST_TS 50
  45. /* H/W timestamp */
  46. #define VIRTIO_NET_HDR_F_TIMESTAMP 128
  47. /* RX packet size EWMA. The average packet size is used to determine the packet
  48. * buffer size when refilling RX rings. As the entire RX ring may be refilled
  49. * at once, the weight is chosen so that the EWMA will be insensitive to short-
  50. * term, transient changes in packet size.
  51. */
  52. DECLARE_EWMA(pkt_len, 0, 64)
  53. #define VIRTNET_DRIVER_VERSION "1.0.0"
  54. static const unsigned long guest_offloads[] = {
  55. VIRTIO_NET_F_GUEST_TSO4,
  56. VIRTIO_NET_F_GUEST_TSO6,
  57. VIRTIO_NET_F_GUEST_ECN,
  58. VIRTIO_NET_F_GUEST_UFO,
  59. VIRTIO_NET_F_GUEST_CSUM
  60. };
  61. #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
  62. (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
  63. (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
  64. (1ULL << VIRTIO_NET_F_GUEST_UFO))
  65. struct virtnet_stat_desc {
  66. char desc[ETH_GSTRING_LEN];
  67. size_t offset;
  68. };
  69. struct virtnet_sq_stats {
  70. struct u64_stats_sync syncp;
  71. u64 packets;
  72. u64 bytes;
  73. u64 xdp_tx;
  74. u64 xdp_tx_drops;
  75. u64 kicks;
  76. u64 tx_timeouts;
  77. };
  78. struct virtnet_rq_stats {
  79. struct u64_stats_sync syncp;
  80. u64 packets;
  81. u64 bytes;
  82. u64 drops;
  83. u64 xdp_packets;
  84. u64 xdp_tx;
  85. u64 xdp_redirects;
  86. u64 xdp_drops;
  87. u64 kicks;
  88. };
  89. #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
  90. #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
  91. static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
  92. { "packets", VIRTNET_SQ_STAT(packets) },
  93. { "bytes", VIRTNET_SQ_STAT(bytes) },
  94. { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
  95. { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
  96. { "kicks", VIRTNET_SQ_STAT(kicks) },
  97. { "tx_timeouts", VIRTNET_SQ_STAT(tx_timeouts) },
  98. };
  99. static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
  100. { "packets", VIRTNET_RQ_STAT(packets) },
  101. { "bytes", VIRTNET_RQ_STAT(bytes) },
  102. { "drops", VIRTNET_RQ_STAT(drops) },
  103. { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
  104. { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
  105. { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
  106. { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
  107. { "kicks", VIRTNET_RQ_STAT(kicks) },
  108. };
  109. #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
  110. #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
  111. /* Internal representation of a send virtqueue */
  112. struct send_queue {
  113. /* Virtqueue associated with this send _queue */
  114. struct virtqueue *vq;
  115. /* TX: fragments + linear part + virtio header */
  116. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  117. /* Name of the send queue: output.$index */
  118. char name[40];
  119. struct virtnet_sq_stats stats;
  120. struct napi_struct napi;
  121. /* Record whether sq is in reset state. */
  122. bool reset;
  123. };
  124. /* Internal representation of a receive virtqueue */
  125. struct receive_queue {
  126. /* Virtqueue associated with this receive_queue */
  127. struct virtqueue *vq;
  128. struct napi_struct napi;
  129. struct bpf_prog __rcu *xdp_prog;
  130. struct virtnet_rq_stats stats;
  131. /* Chain pages by the private ptr. */
  132. struct page *pages;
  133. /* Average packet length for mergeable receive buffers. */
  134. struct ewma_pkt_len mrg_avg_pkt_len;
  135. /* Page frag for packet buffer allocation. */
  136. struct page_frag alloc_frag;
  137. /* RX: fragments + linear part + virtio header */
  138. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  139. /* Min single buffer size for mergeable buffers case. */
  140. unsigned int min_buf_len;
  141. /* Name of this receive queue: input.$index */
  142. char name[40];
  143. struct xdp_rxq_info xdp_rxq;
  144. };
  145. /* This structure can contain rss message with maximum settings for indirection table and keysize
  146. * Note, that default structure that describes RSS configuration virtio_net_rss_config
  147. * contains same info but can't handle table values.
  148. * In any case, structure would be passed to virtio hw through sg_buf split by parts
  149. * because table sizes may be differ according to the device configuration.
  150. */
  151. #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
  152. #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
  153. struct virtio_net_ctrl_rss {
  154. u32 hash_types;
  155. u16 indirection_table_mask;
  156. u16 unclassified_queue;
  157. u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
  158. u16 max_tx_vq;
  159. u8 hash_key_length;
  160. u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
  161. };
  162. /* Control VQ buffers: protected by the rtnl lock */
  163. struct control_buf {
  164. struct virtio_net_ctrl_hdr hdr;
  165. virtio_net_ctrl_ack status;
  166. struct virtio_net_ctrl_mq mq;
  167. u8 promisc;
  168. u8 allmulti;
  169. __virtio16 vid;
  170. __virtio64 offloads;
  171. struct virtio_net_ctrl_rss rss;
  172. struct virtio_net_ctrl_coal_tx coal_tx;
  173. struct virtio_net_ctrl_coal_rx coal_rx;
  174. };
  175. struct virtnet_info {
  176. struct virtio_device *vdev;
  177. struct virtqueue *cvq;
  178. struct net_device *dev;
  179. struct send_queue *sq;
  180. struct receive_queue *rq;
  181. unsigned int status;
  182. /* Max # of queue pairs supported by the device */
  183. u16 max_queue_pairs;
  184. /* # of queue pairs currently used by the driver */
  185. u16 curr_queue_pairs;
  186. /* # of XDP queue pairs currently used by the driver */
  187. u16 xdp_queue_pairs;
  188. /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
  189. bool xdp_enabled;
  190. /* I like... big packets and I cannot lie! */
  191. bool big_packets;
  192. /* number of sg entries allocated for big packets */
  193. unsigned int big_packets_num_skbfrags;
  194. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  195. bool mergeable_rx_bufs;
  196. /* Host supports rss and/or hash report */
  197. bool has_rss;
  198. bool has_rss_hash_report;
  199. u8 rss_key_size;
  200. u16 rss_indir_table_size;
  201. u32 rss_hash_types_supported;
  202. u32 rss_hash_types_saved;
  203. /* Has control virtqueue */
  204. bool has_cvq;
  205. /* Host can handle any s/g split between our header and packet data */
  206. bool any_header_sg;
  207. /* Packet virtio header size */
  208. u8 hdr_len;
  209. /* Work struct for delayed refilling if we run low on memory. */
  210. struct delayed_work refill;
  211. /* Is delayed refill enabled? */
  212. bool refill_enabled;
  213. /* The lock to synchronize the access to refill_enabled */
  214. spinlock_t refill_lock;
  215. /* Work struct for config space updates */
  216. struct work_struct config_work;
  217. /* Does the affinity hint is set for virtqueues? */
  218. bool affinity_hint_set;
  219. /* CPU hotplug instances for online & dead */
  220. struct hlist_node node;
  221. struct hlist_node node_dead;
  222. struct control_buf *ctrl;
  223. /* Ethtool settings */
  224. u8 duplex;
  225. u32 speed;
  226. /* Interrupt coalescing settings */
  227. u32 tx_usecs;
  228. u32 rx_usecs;
  229. u32 tx_max_packets;
  230. u32 rx_max_packets;
  231. unsigned long guest_offloads;
  232. unsigned long guest_offloads_capable;
  233. /* failover when STANDBY feature enabled */
  234. struct failover *failover;
  235. /* RX H/W timestamp control */
  236. bool rx_hwts_enabled;
  237. };
  238. struct padded_vnet_hdr {
  239. struct virtio_net_hdr_v1_hash hdr;
  240. /*
  241. * hdr is in a separate sg buffer, and data sg buffer shares same page
  242. * with this header sg. This padding makes next sg 16 byte aligned
  243. * after the header.
  244. */
  245. char padding[12];
  246. };
  247. static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf);
  248. static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
  249. static bool is_xdp_frame(void *ptr)
  250. {
  251. return (unsigned long)ptr & VIRTIO_XDP_FLAG;
  252. }
  253. static void *xdp_to_ptr(struct xdp_frame *ptr)
  254. {
  255. return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
  256. }
  257. static struct xdp_frame *ptr_to_xdp(void *ptr)
  258. {
  259. return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
  260. }
  261. /* Converting between virtqueue no. and kernel tx/rx queue no.
  262. * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
  263. */
  264. static int vq2txq(struct virtqueue *vq)
  265. {
  266. return (vq->index - 1) / 2;
  267. }
  268. static int txq2vq(int txq)
  269. {
  270. return txq * 2 + 1;
  271. }
  272. static int vq2rxq(struct virtqueue *vq)
  273. {
  274. return vq->index / 2;
  275. }
  276. static int rxq2vq(int rxq)
  277. {
  278. return rxq * 2;
  279. }
  280. static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
  281. {
  282. return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
  283. }
  284. /*
  285. * private is used to chain pages for big packets, put the whole
  286. * most recent used list in the beginning for reuse
  287. */
  288. static void give_pages(struct receive_queue *rq, struct page *page)
  289. {
  290. struct page *end;
  291. /* Find end of list, sew whole thing into vi->rq.pages. */
  292. for (end = page; end->private; end = (struct page *)end->private);
  293. end->private = (unsigned long)rq->pages;
  294. rq->pages = page;
  295. }
  296. static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  297. {
  298. struct page *p = rq->pages;
  299. if (p) {
  300. rq->pages = (struct page *)p->private;
  301. /* clear private here, it is used to chain pages */
  302. p->private = 0;
  303. } else
  304. p = alloc_page(gfp_mask);
  305. return p;
  306. }
  307. static void enable_delayed_refill(struct virtnet_info *vi)
  308. {
  309. spin_lock_bh(&vi->refill_lock);
  310. vi->refill_enabled = true;
  311. spin_unlock_bh(&vi->refill_lock);
  312. }
  313. static void disable_delayed_refill(struct virtnet_info *vi)
  314. {
  315. spin_lock_bh(&vi->refill_lock);
  316. vi->refill_enabled = false;
  317. spin_unlock_bh(&vi->refill_lock);
  318. }
  319. static void virtqueue_napi_schedule(struct napi_struct *napi,
  320. struct virtqueue *vq)
  321. {
  322. if (napi_schedule_prep(napi)) {
  323. virtqueue_disable_cb(vq);
  324. __napi_schedule(napi);
  325. }
  326. }
  327. static void virtqueue_napi_complete(struct napi_struct *napi,
  328. struct virtqueue *vq, int processed)
  329. {
  330. int opaque;
  331. opaque = virtqueue_enable_cb_prepare(vq);
  332. if (napi_complete_done(napi, processed)) {
  333. if (unlikely(virtqueue_poll(vq, opaque)))
  334. virtqueue_napi_schedule(napi, vq);
  335. } else {
  336. virtqueue_disable_cb(vq);
  337. }
  338. }
  339. static void skb_xmit_done(struct virtqueue *vq)
  340. {
  341. struct virtnet_info *vi = vq->vdev->priv;
  342. struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
  343. /* Suppress further interrupts. */
  344. virtqueue_disable_cb(vq);
  345. if (napi->weight)
  346. virtqueue_napi_schedule(napi, vq);
  347. else
  348. /* We were probably waiting for more output buffers. */
  349. netif_wake_subqueue(vi->dev, vq2txq(vq));
  350. }
  351. #define MRG_CTX_HEADER_SHIFT 22
  352. static void *mergeable_len_to_ctx(unsigned int truesize,
  353. unsigned int headroom)
  354. {
  355. return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
  356. }
  357. static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
  358. {
  359. return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
  360. }
  361. static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
  362. {
  363. return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
  364. }
  365. /* Called from bottom half context */
  366. static struct sk_buff *page_to_skb(struct virtnet_info *vi,
  367. struct receive_queue *rq,
  368. struct page *page, unsigned int offset,
  369. unsigned int len, unsigned int truesize,
  370. bool hdr_valid, unsigned int metasize,
  371. unsigned int headroom)
  372. {
  373. struct sk_buff *skb;
  374. struct virtio_net_hdr_mrg_rxbuf *hdr;
  375. unsigned int copy, hdr_len, hdr_padded_len;
  376. struct page *page_to_free = NULL;
  377. int tailroom, shinfo_size;
  378. char *p, *hdr_p, *buf;
  379. p = page_address(page) + offset;
  380. hdr_p = p;
  381. hdr_len = vi->hdr_len;
  382. if (vi->mergeable_rx_bufs)
  383. hdr_padded_len = hdr_len;
  384. else
  385. hdr_padded_len = sizeof(struct padded_vnet_hdr);
  386. /* If headroom is not 0, there is an offset between the beginning of the
  387. * data and the allocated space, otherwise the data and the allocated
  388. * space are aligned.
  389. *
  390. * Buffers with headroom use PAGE_SIZE as alloc size, see
  391. * add_recvbuf_mergeable() + get_mergeable_buf_len()
  392. */
  393. truesize = headroom ? PAGE_SIZE : truesize;
  394. tailroom = truesize - headroom;
  395. buf = p - headroom;
  396. len -= hdr_len;
  397. offset += hdr_padded_len;
  398. p += hdr_padded_len;
  399. tailroom -= hdr_padded_len + len;
  400. shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  401. /* copy small packet so we can reuse these pages */
  402. if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
  403. skb = build_skb(buf, truesize);
  404. if (unlikely(!skb))
  405. return NULL;
  406. skb_reserve(skb, p - buf);
  407. skb_put(skb, len);
  408. page = (struct page *)page->private;
  409. if (page)
  410. give_pages(rq, page);
  411. goto ok;
  412. }
  413. /* copy small packet so we can reuse these pages for small data */
  414. skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
  415. if (unlikely(!skb))
  416. return NULL;
  417. /* Copy all frame if it fits skb->head, otherwise
  418. * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
  419. */
  420. if (len <= skb_tailroom(skb))
  421. copy = len;
  422. else
  423. copy = ETH_HLEN + metasize;
  424. skb_put_data(skb, p, copy);
  425. len -= copy;
  426. offset += copy;
  427. if (vi->mergeable_rx_bufs) {
  428. if (len)
  429. skb_add_rx_frag(skb, 0, page, offset, len, truesize);
  430. else
  431. page_to_free = page;
  432. goto ok;
  433. }
  434. /*
  435. * Verify that we can indeed put this data into a skb.
  436. * This is here to handle cases when the device erroneously
  437. * tries to receive more than is possible. This is usually
  438. * the case of a broken device.
  439. */
  440. if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
  441. net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
  442. dev_kfree_skb(skb);
  443. return NULL;
  444. }
  445. BUG_ON(offset >= PAGE_SIZE);
  446. while (len) {
  447. unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
  448. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
  449. frag_size, truesize);
  450. len -= frag_size;
  451. page = (struct page *)page->private;
  452. offset = 0;
  453. }
  454. if (page)
  455. give_pages(rq, page);
  456. ok:
  457. /* hdr_valid means no XDP, so we can copy the vnet header */
  458. if (hdr_valid) {
  459. hdr = skb_vnet_hdr(skb);
  460. memcpy(hdr, hdr_p, hdr_len);
  461. }
  462. if (page_to_free)
  463. put_page(page_to_free);
  464. if (metasize) {
  465. __skb_pull(skb, metasize);
  466. skb_metadata_set(skb, metasize);
  467. }
  468. return skb;
  469. }
  470. static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
  471. struct send_queue *sq,
  472. struct xdp_frame *xdpf)
  473. {
  474. struct virtio_net_hdr_mrg_rxbuf *hdr;
  475. int err;
  476. if (unlikely(xdpf->headroom < vi->hdr_len))
  477. return -EOVERFLOW;
  478. /* Make room for virtqueue hdr (also change xdpf->headroom?) */
  479. xdpf->data -= vi->hdr_len;
  480. /* Zero header and leave csum up to XDP layers */
  481. hdr = xdpf->data;
  482. memset(hdr, 0, vi->hdr_len);
  483. xdpf->len += vi->hdr_len;
  484. sg_init_one(sq->sg, xdpf->data, xdpf->len);
  485. err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
  486. GFP_ATOMIC);
  487. if (unlikely(err))
  488. return -ENOSPC; /* Caller handle free/refcnt */
  489. return 0;
  490. }
  491. /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
  492. * the current cpu, so it does not need to be locked.
  493. *
  494. * Here we use marco instead of inline functions because we have to deal with
  495. * three issues at the same time: 1. the choice of sq. 2. judge and execute the
  496. * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
  497. * functions to perfectly solve these three problems at the same time.
  498. */
  499. #define virtnet_xdp_get_sq(vi) ({ \
  500. int cpu = smp_processor_id(); \
  501. struct netdev_queue *txq; \
  502. typeof(vi) v = (vi); \
  503. unsigned int qp; \
  504. \
  505. if (v->curr_queue_pairs > nr_cpu_ids) { \
  506. qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
  507. qp += cpu; \
  508. txq = netdev_get_tx_queue(v->dev, qp); \
  509. __netif_tx_acquire(txq); \
  510. } else { \
  511. qp = cpu % v->curr_queue_pairs; \
  512. txq = netdev_get_tx_queue(v->dev, qp); \
  513. __netif_tx_lock(txq, cpu); \
  514. } \
  515. v->sq + qp; \
  516. })
  517. #define virtnet_xdp_put_sq(vi, q) { \
  518. struct netdev_queue *txq; \
  519. typeof(vi) v = (vi); \
  520. \
  521. txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
  522. if (v->curr_queue_pairs > nr_cpu_ids) \
  523. __netif_tx_release(txq); \
  524. else \
  525. __netif_tx_unlock(txq); \
  526. }
  527. static int virtnet_xdp_xmit(struct net_device *dev,
  528. int n, struct xdp_frame **frames, u32 flags)
  529. {
  530. struct virtnet_info *vi = netdev_priv(dev);
  531. struct receive_queue *rq = vi->rq;
  532. struct bpf_prog *xdp_prog;
  533. struct send_queue *sq;
  534. unsigned int len;
  535. int packets = 0;
  536. int bytes = 0;
  537. int nxmit = 0;
  538. int kicks = 0;
  539. void *ptr;
  540. int ret;
  541. int i;
  542. /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
  543. * indicate XDP resources have been successfully allocated.
  544. */
  545. xdp_prog = rcu_access_pointer(rq->xdp_prog);
  546. if (!xdp_prog)
  547. return -ENXIO;
  548. sq = virtnet_xdp_get_sq(vi);
  549. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
  550. ret = -EINVAL;
  551. goto out;
  552. }
  553. /* Free up any pending old buffers before queueing new ones. */
  554. while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  555. if (likely(is_xdp_frame(ptr))) {
  556. struct xdp_frame *frame = ptr_to_xdp(ptr);
  557. bytes += frame->len;
  558. xdp_return_frame(frame);
  559. } else {
  560. struct sk_buff *skb = ptr;
  561. bytes += skb->len;
  562. napi_consume_skb(skb, false);
  563. }
  564. packets++;
  565. }
  566. for (i = 0; i < n; i++) {
  567. struct xdp_frame *xdpf = frames[i];
  568. if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
  569. break;
  570. nxmit++;
  571. }
  572. ret = nxmit;
  573. if (flags & XDP_XMIT_FLUSH) {
  574. if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
  575. kicks = 1;
  576. }
  577. out:
  578. u64_stats_update_begin(&sq->stats.syncp);
  579. sq->stats.bytes += bytes;
  580. sq->stats.packets += packets;
  581. sq->stats.xdp_tx += n;
  582. sq->stats.xdp_tx_drops += n - nxmit;
  583. sq->stats.kicks += kicks;
  584. u64_stats_update_end(&sq->stats.syncp);
  585. virtnet_xdp_put_sq(vi, sq);
  586. return ret;
  587. }
  588. static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
  589. {
  590. return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
  591. }
  592. /* We copy the packet for XDP in the following cases:
  593. *
  594. * 1) Packet is scattered across multiple rx buffers.
  595. * 2) Headroom space is insufficient.
  596. *
  597. * This is inefficient but it's a temporary condition that
  598. * we hit right after XDP is enabled and until queue is refilled
  599. * with large buffers with sufficient headroom - so it should affect
  600. * at most queue size packets.
  601. * Afterwards, the conditions to enable
  602. * XDP should preclude the underlying device from sending packets
  603. * across multiple buffers (num_buf > 1), and we make sure buffers
  604. * have enough headroom.
  605. */
  606. static struct page *xdp_linearize_page(struct receive_queue *rq,
  607. u16 *num_buf,
  608. struct page *p,
  609. int offset,
  610. int page_off,
  611. unsigned int *len)
  612. {
  613. int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  614. struct page *page;
  615. if (page_off + *len + tailroom > PAGE_SIZE)
  616. return NULL;
  617. page = alloc_page(GFP_ATOMIC);
  618. if (!page)
  619. return NULL;
  620. memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
  621. page_off += *len;
  622. while (--*num_buf) {
  623. unsigned int buflen;
  624. void *buf;
  625. int off;
  626. buf = virtqueue_get_buf(rq->vq, &buflen);
  627. if (unlikely(!buf))
  628. goto err_buf;
  629. p = virt_to_head_page(buf);
  630. off = buf - page_address(p);
  631. /* guard against a misconfigured or uncooperative backend that
  632. * is sending packet larger than the MTU.
  633. */
  634. if ((page_off + buflen + tailroom) > PAGE_SIZE) {
  635. put_page(p);
  636. goto err_buf;
  637. }
  638. memcpy(page_address(page) + page_off,
  639. page_address(p) + off, buflen);
  640. page_off += buflen;
  641. put_page(p);
  642. }
  643. /* Headroom does not contribute to packet length */
  644. *len = page_off - VIRTIO_XDP_HEADROOM;
  645. return page;
  646. err_buf:
  647. __free_pages(page, 0);
  648. return NULL;
  649. }
  650. static struct sk_buff *receive_small(struct net_device *dev,
  651. struct virtnet_info *vi,
  652. struct receive_queue *rq,
  653. void *buf, void *ctx,
  654. unsigned int len,
  655. unsigned int *xdp_xmit,
  656. struct virtnet_rq_stats *stats)
  657. {
  658. struct sk_buff *skb;
  659. struct bpf_prog *xdp_prog;
  660. unsigned int xdp_headroom = (unsigned long)ctx;
  661. unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
  662. unsigned int headroom = vi->hdr_len + header_offset;
  663. unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  664. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  665. struct page *page = virt_to_head_page(buf);
  666. unsigned int delta = 0;
  667. struct page *xdp_page;
  668. int err;
  669. unsigned int metasize = 0;
  670. len -= vi->hdr_len;
  671. stats->bytes += len;
  672. if (unlikely(len > GOOD_PACKET_LEN)) {
  673. pr_debug("%s: rx error: len %u exceeds max size %d\n",
  674. dev->name, len, GOOD_PACKET_LEN);
  675. dev->stats.rx_length_errors++;
  676. goto err;
  677. }
  678. if (likely(!vi->xdp_enabled)) {
  679. xdp_prog = NULL;
  680. goto skip_xdp;
  681. }
  682. rcu_read_lock();
  683. xdp_prog = rcu_dereference(rq->xdp_prog);
  684. if (xdp_prog) {
  685. struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
  686. struct xdp_frame *xdpf;
  687. struct xdp_buff xdp;
  688. void *orig_data;
  689. u32 act;
  690. if (unlikely(hdr->hdr.gso_type))
  691. goto err_xdp;
  692. if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
  693. int offset = buf - page_address(page) + header_offset;
  694. unsigned int tlen = len + vi->hdr_len;
  695. u16 num_buf = 1;
  696. xdp_headroom = virtnet_get_headroom(vi);
  697. header_offset = VIRTNET_RX_PAD + xdp_headroom;
  698. headroom = vi->hdr_len + header_offset;
  699. buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  700. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  701. xdp_page = xdp_linearize_page(rq, &num_buf, page,
  702. offset, header_offset,
  703. &tlen);
  704. if (!xdp_page)
  705. goto err_xdp;
  706. buf = page_address(xdp_page);
  707. put_page(page);
  708. page = xdp_page;
  709. }
  710. xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
  711. xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
  712. xdp_headroom, len, true);
  713. orig_data = xdp.data;
  714. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  715. stats->xdp_packets++;
  716. switch (act) {
  717. case XDP_PASS:
  718. /* Recalculate length in case bpf program changed it */
  719. delta = orig_data - xdp.data;
  720. len = xdp.data_end - xdp.data;
  721. metasize = xdp.data - xdp.data_meta;
  722. break;
  723. case XDP_TX:
  724. stats->xdp_tx++;
  725. xdpf = xdp_convert_buff_to_frame(&xdp);
  726. if (unlikely(!xdpf))
  727. goto err_xdp;
  728. err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
  729. if (unlikely(!err)) {
  730. xdp_return_frame_rx_napi(xdpf);
  731. } else if (unlikely(err < 0)) {
  732. trace_xdp_exception(vi->dev, xdp_prog, act);
  733. goto err_xdp;
  734. }
  735. *xdp_xmit |= VIRTIO_XDP_TX;
  736. rcu_read_unlock();
  737. goto xdp_xmit;
  738. case XDP_REDIRECT:
  739. stats->xdp_redirects++;
  740. err = xdp_do_redirect(dev, &xdp, xdp_prog);
  741. if (err)
  742. goto err_xdp;
  743. *xdp_xmit |= VIRTIO_XDP_REDIR;
  744. rcu_read_unlock();
  745. goto xdp_xmit;
  746. default:
  747. bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
  748. fallthrough;
  749. case XDP_ABORTED:
  750. trace_xdp_exception(vi->dev, xdp_prog, act);
  751. goto err_xdp;
  752. case XDP_DROP:
  753. goto err_xdp;
  754. }
  755. }
  756. rcu_read_unlock();
  757. skip_xdp:
  758. skb = build_skb(buf, buflen);
  759. if (!skb)
  760. goto err;
  761. skb_reserve(skb, headroom - delta);
  762. skb_put(skb, len);
  763. if (!xdp_prog) {
  764. buf += header_offset;
  765. memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
  766. } /* keep zeroed vnet hdr since XDP is loaded */
  767. if (metasize)
  768. skb_metadata_set(skb, metasize);
  769. return skb;
  770. err_xdp:
  771. rcu_read_unlock();
  772. stats->xdp_drops++;
  773. err:
  774. stats->drops++;
  775. put_page(page);
  776. xdp_xmit:
  777. return NULL;
  778. }
  779. static struct sk_buff *receive_big(struct net_device *dev,
  780. struct virtnet_info *vi,
  781. struct receive_queue *rq,
  782. void *buf,
  783. unsigned int len,
  784. struct virtnet_rq_stats *stats)
  785. {
  786. struct page *page = buf;
  787. struct sk_buff *skb =
  788. page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
  789. stats->bytes += len - vi->hdr_len;
  790. if (unlikely(!skb))
  791. goto err;
  792. return skb;
  793. err:
  794. stats->drops++;
  795. give_pages(rq, page);
  796. return NULL;
  797. }
  798. static struct sk_buff *receive_mergeable(struct net_device *dev,
  799. struct virtnet_info *vi,
  800. struct receive_queue *rq,
  801. void *buf,
  802. void *ctx,
  803. unsigned int len,
  804. unsigned int *xdp_xmit,
  805. struct virtnet_rq_stats *stats)
  806. {
  807. struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
  808. u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
  809. struct page *page = virt_to_head_page(buf);
  810. int offset = buf - page_address(page);
  811. struct sk_buff *head_skb, *curr_skb;
  812. struct bpf_prog *xdp_prog;
  813. unsigned int truesize = mergeable_ctx_to_truesize(ctx);
  814. unsigned int headroom = mergeable_ctx_to_headroom(ctx);
  815. unsigned int metasize = 0;
  816. unsigned int frame_sz;
  817. int err;
  818. head_skb = NULL;
  819. stats->bytes += len - vi->hdr_len;
  820. if (unlikely(len > truesize)) {
  821. pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
  822. dev->name, len, (unsigned long)ctx);
  823. dev->stats.rx_length_errors++;
  824. goto err_skb;
  825. }
  826. if (likely(!vi->xdp_enabled)) {
  827. xdp_prog = NULL;
  828. goto skip_xdp;
  829. }
  830. rcu_read_lock();
  831. xdp_prog = rcu_dereference(rq->xdp_prog);
  832. if (xdp_prog) {
  833. struct xdp_frame *xdpf;
  834. struct page *xdp_page;
  835. struct xdp_buff xdp;
  836. void *data;
  837. u32 act;
  838. /* Transient failure which in theory could occur if
  839. * in-flight packets from before XDP was enabled reach
  840. * the receive path after XDP is loaded.
  841. */
  842. if (unlikely(hdr->hdr.gso_type))
  843. goto err_xdp;
  844. /* Buffers with headroom use PAGE_SIZE as alloc size,
  845. * see add_recvbuf_mergeable() + get_mergeable_buf_len()
  846. */
  847. frame_sz = headroom ? PAGE_SIZE : truesize;
  848. /* This happens when rx buffer size is underestimated
  849. * or headroom is not enough because of the buffer
  850. * was refilled before XDP is set. This should only
  851. * happen for the first several packets, so we don't
  852. * care much about its performance.
  853. */
  854. if (unlikely(num_buf > 1 ||
  855. headroom < virtnet_get_headroom(vi))) {
  856. /* linearize data for XDP */
  857. xdp_page = xdp_linearize_page(rq, &num_buf,
  858. page, offset,
  859. VIRTIO_XDP_HEADROOM,
  860. &len);
  861. frame_sz = PAGE_SIZE;
  862. if (!xdp_page)
  863. goto err_xdp;
  864. offset = VIRTIO_XDP_HEADROOM;
  865. } else {
  866. xdp_page = page;
  867. }
  868. /* Allow consuming headroom but reserve enough space to push
  869. * the descriptor on if we get an XDP_TX return code.
  870. */
  871. data = page_address(xdp_page) + offset;
  872. xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq);
  873. xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len,
  874. VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true);
  875. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  876. stats->xdp_packets++;
  877. switch (act) {
  878. case XDP_PASS:
  879. metasize = xdp.data - xdp.data_meta;
  880. /* recalculate offset to account for any header
  881. * adjustments and minus the metasize to copy the
  882. * metadata in page_to_skb(). Note other cases do not
  883. * build an skb and avoid using offset
  884. */
  885. offset = xdp.data - page_address(xdp_page) -
  886. vi->hdr_len - metasize;
  887. /* recalculate len if xdp.data, xdp.data_end or
  888. * xdp.data_meta were adjusted
  889. */
  890. len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
  891. /* recalculate headroom if xdp.data or xdp_data_meta
  892. * were adjusted, note that offset should always point
  893. * to the start of the reserved bytes for virtio_net
  894. * header which are followed by xdp.data, that means
  895. * that offset is equal to the headroom (when buf is
  896. * starting at the beginning of the page, otherwise
  897. * there is a base offset inside the page) but it's used
  898. * with a different starting point (buf start) than
  899. * xdp.data (buf start + vnet hdr size). If xdp.data or
  900. * data_meta were adjusted by the xdp prog then the
  901. * headroom size has changed and so has the offset, we
  902. * can use data_hard_start, which points at buf start +
  903. * vnet hdr size, to calculate the new headroom and use
  904. * it later to compute buf start in page_to_skb()
  905. */
  906. headroom = xdp.data - xdp.data_hard_start - metasize;
  907. /* We can only create skb based on xdp_page. */
  908. if (unlikely(xdp_page != page)) {
  909. rcu_read_unlock();
  910. put_page(page);
  911. head_skb = page_to_skb(vi, rq, xdp_page, offset,
  912. len, PAGE_SIZE, false,
  913. metasize,
  914. headroom);
  915. return head_skb;
  916. }
  917. break;
  918. case XDP_TX:
  919. stats->xdp_tx++;
  920. xdpf = xdp_convert_buff_to_frame(&xdp);
  921. if (unlikely(!xdpf)) {
  922. if (unlikely(xdp_page != page))
  923. put_page(xdp_page);
  924. goto err_xdp;
  925. }
  926. err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
  927. if (unlikely(!err)) {
  928. xdp_return_frame_rx_napi(xdpf);
  929. } else if (unlikely(err < 0)) {
  930. trace_xdp_exception(vi->dev, xdp_prog, act);
  931. if (unlikely(xdp_page != page))
  932. put_page(xdp_page);
  933. goto err_xdp;
  934. }
  935. *xdp_xmit |= VIRTIO_XDP_TX;
  936. if (unlikely(xdp_page != page))
  937. put_page(page);
  938. rcu_read_unlock();
  939. goto xdp_xmit;
  940. case XDP_REDIRECT:
  941. stats->xdp_redirects++;
  942. err = xdp_do_redirect(dev, &xdp, xdp_prog);
  943. if (err) {
  944. if (unlikely(xdp_page != page))
  945. put_page(xdp_page);
  946. goto err_xdp;
  947. }
  948. *xdp_xmit |= VIRTIO_XDP_REDIR;
  949. if (unlikely(xdp_page != page))
  950. put_page(page);
  951. rcu_read_unlock();
  952. goto xdp_xmit;
  953. default:
  954. bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
  955. fallthrough;
  956. case XDP_ABORTED:
  957. trace_xdp_exception(vi->dev, xdp_prog, act);
  958. fallthrough;
  959. case XDP_DROP:
  960. if (unlikely(xdp_page != page))
  961. __free_pages(xdp_page, 0);
  962. goto err_xdp;
  963. }
  964. }
  965. rcu_read_unlock();
  966. skip_xdp:
  967. head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
  968. metasize, headroom);
  969. curr_skb = head_skb;
  970. if (unlikely(!curr_skb))
  971. goto err_skb;
  972. while (--num_buf) {
  973. int num_skb_frags;
  974. buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
  975. if (unlikely(!buf)) {
  976. pr_debug("%s: rx error: %d buffers out of %d missing\n",
  977. dev->name, num_buf,
  978. virtio16_to_cpu(vi->vdev,
  979. hdr->num_buffers));
  980. dev->stats.rx_length_errors++;
  981. goto err_buf;
  982. }
  983. stats->bytes += len;
  984. page = virt_to_head_page(buf);
  985. truesize = mergeable_ctx_to_truesize(ctx);
  986. if (unlikely(len > truesize)) {
  987. pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
  988. dev->name, len, (unsigned long)ctx);
  989. dev->stats.rx_length_errors++;
  990. goto err_skb;
  991. }
  992. num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
  993. if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
  994. struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
  995. if (unlikely(!nskb))
  996. goto err_skb;
  997. if (curr_skb == head_skb)
  998. skb_shinfo(curr_skb)->frag_list = nskb;
  999. else
  1000. curr_skb->next = nskb;
  1001. curr_skb = nskb;
  1002. head_skb->truesize += nskb->truesize;
  1003. num_skb_frags = 0;
  1004. }
  1005. if (curr_skb != head_skb) {
  1006. head_skb->data_len += len;
  1007. head_skb->len += len;
  1008. head_skb->truesize += truesize;
  1009. }
  1010. offset = buf - page_address(page);
  1011. if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
  1012. put_page(page);
  1013. skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
  1014. len, truesize);
  1015. } else {
  1016. skb_add_rx_frag(curr_skb, num_skb_frags, page,
  1017. offset, len, truesize);
  1018. }
  1019. }
  1020. ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
  1021. return head_skb;
  1022. err_xdp:
  1023. rcu_read_unlock();
  1024. stats->xdp_drops++;
  1025. err_skb:
  1026. put_page(page);
  1027. while (num_buf-- > 1) {
  1028. buf = virtqueue_get_buf(rq->vq, &len);
  1029. if (unlikely(!buf)) {
  1030. pr_debug("%s: rx error: %d buffers missing\n",
  1031. dev->name, num_buf);
  1032. dev->stats.rx_length_errors++;
  1033. break;
  1034. }
  1035. stats->bytes += len;
  1036. page = virt_to_head_page(buf);
  1037. put_page(page);
  1038. }
  1039. err_buf:
  1040. stats->drops++;
  1041. dev_kfree_skb(head_skb);
  1042. xdp_xmit:
  1043. return NULL;
  1044. }
  1045. static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
  1046. struct sk_buff *skb)
  1047. {
  1048. enum pkt_hash_types rss_hash_type;
  1049. if (!hdr_hash || !skb)
  1050. return;
  1051. switch (__le16_to_cpu(hdr_hash->hash_report)) {
  1052. case VIRTIO_NET_HASH_REPORT_TCPv4:
  1053. case VIRTIO_NET_HASH_REPORT_UDPv4:
  1054. case VIRTIO_NET_HASH_REPORT_TCPv6:
  1055. case VIRTIO_NET_HASH_REPORT_UDPv6:
  1056. case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
  1057. case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
  1058. rss_hash_type = PKT_HASH_TYPE_L4;
  1059. break;
  1060. case VIRTIO_NET_HASH_REPORT_IPv4:
  1061. case VIRTIO_NET_HASH_REPORT_IPv6:
  1062. case VIRTIO_NET_HASH_REPORT_IPv6_EX:
  1063. rss_hash_type = PKT_HASH_TYPE_L3;
  1064. break;
  1065. case VIRTIO_NET_HASH_REPORT_NONE:
  1066. default:
  1067. rss_hash_type = PKT_HASH_TYPE_NONE;
  1068. }
  1069. skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
  1070. }
  1071. static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
  1072. void *buf, unsigned int len, void **ctx,
  1073. unsigned int *xdp_xmit,
  1074. struct virtnet_rq_stats *stats)
  1075. {
  1076. struct net_device *dev = vi->dev;
  1077. struct sk_buff *skb;
  1078. struct virtio_net_hdr_mrg_rxbuf *hdr;
  1079. struct skb_shared_hwtstamps *ts;
  1080. s64 hwts;
  1081. if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
  1082. pr_debug("%s: short packet %i\n", dev->name, len);
  1083. dev->stats.rx_length_errors++;
  1084. if (vi->mergeable_rx_bufs) {
  1085. put_page(virt_to_head_page(buf));
  1086. } else if (vi->big_packets) {
  1087. give_pages(rq, buf);
  1088. } else {
  1089. put_page(virt_to_head_page(buf));
  1090. }
  1091. return;
  1092. }
  1093. if (vi->mergeable_rx_bufs)
  1094. skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
  1095. stats);
  1096. else if (vi->big_packets)
  1097. skb = receive_big(dev, vi, rq, buf, len, stats);
  1098. else
  1099. skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
  1100. if (unlikely(!skb))
  1101. return;
  1102. hdr = skb_vnet_hdr(skb);
  1103. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_TIMESTAMP) {
  1104. stats->bytes -= HW_TIMESTAMP_LEN;
  1105. skb_copy_bits(skb, skb->len - HW_TIMESTAMP_LEN, &hwts, HW_TIMESTAMP_LEN);
  1106. pskb_trim(skb, skb->len - HW_TIMESTAMP_LEN);
  1107. if (vi->rx_hwts_enabled) {
  1108. ts = skb_hwtstamps(skb);
  1109. if (ts)
  1110. ts->hwtstamp = ns_to_ktime(virtio64_to_cpu(vi->vdev, hwts));
  1111. }
  1112. }
  1113. if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
  1114. virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
  1115. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
  1116. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1117. if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
  1118. virtio_is_little_endian(vi->vdev))) {
  1119. net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
  1120. dev->name, hdr->hdr.gso_type,
  1121. hdr->hdr.gso_size);
  1122. goto frame_err;
  1123. }
  1124. skb_record_rx_queue(skb, vq2rxq(rq->vq));
  1125. skb->protocol = eth_type_trans(skb, dev);
  1126. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  1127. ntohs(skb->protocol), skb->len, skb->pkt_type);
  1128. napi_gro_receive(&rq->napi, skb);
  1129. return;
  1130. frame_err:
  1131. dev->stats.rx_frame_errors++;
  1132. dev_kfree_skb(skb);
  1133. }
  1134. /* Unlike mergeable buffers, all buffers are allocated to the
  1135. * same size, except for the headroom. For this reason we do
  1136. * not need to use mergeable_len_to_ctx here - it is enough
  1137. * to store the headroom as the context ignoring the truesize.
  1138. */
  1139. static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
  1140. gfp_t gfp)
  1141. {
  1142. struct page_frag *alloc_frag = &rq->alloc_frag;
  1143. char *buf;
  1144. unsigned int xdp_headroom = virtnet_get_headroom(vi);
  1145. void *ctx = (void *)(unsigned long)xdp_headroom;
  1146. int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
  1147. int err;
  1148. len = SKB_DATA_ALIGN(len) +
  1149. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1150. if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
  1151. return -ENOMEM;
  1152. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  1153. get_page(alloc_frag->page);
  1154. alloc_frag->offset += len;
  1155. sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
  1156. vi->hdr_len + GOOD_PACKET_LEN);
  1157. err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
  1158. if (err < 0)
  1159. put_page(virt_to_head_page(buf));
  1160. return err;
  1161. }
  1162. static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
  1163. gfp_t gfp)
  1164. {
  1165. struct page *first, *list = NULL;
  1166. char *p;
  1167. int i, err, offset;
  1168. sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
  1169. /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
  1170. for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
  1171. first = get_a_page(rq, gfp);
  1172. if (!first) {
  1173. if (list)
  1174. give_pages(rq, list);
  1175. return -ENOMEM;
  1176. }
  1177. sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
  1178. /* chain new page in list head to match sg */
  1179. first->private = (unsigned long)list;
  1180. list = first;
  1181. }
  1182. first = get_a_page(rq, gfp);
  1183. if (!first) {
  1184. give_pages(rq, list);
  1185. return -ENOMEM;
  1186. }
  1187. p = page_address(first);
  1188. /* rq->sg[0], rq->sg[1] share the same page */
  1189. /* a separated rq->sg[0] for header - required in case !any_header_sg */
  1190. sg_set_buf(&rq->sg[0], p, vi->hdr_len);
  1191. /* rq->sg[1] for data packet, from offset */
  1192. offset = sizeof(struct padded_vnet_hdr);
  1193. sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
  1194. /* chain first in list head */
  1195. first->private = (unsigned long)list;
  1196. err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
  1197. first, gfp);
  1198. if (err < 0)
  1199. give_pages(rq, first);
  1200. return err;
  1201. }
  1202. static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
  1203. struct ewma_pkt_len *avg_pkt_len,
  1204. unsigned int room)
  1205. {
  1206. struct virtnet_info *vi = rq->vq->vdev->priv;
  1207. const size_t hdr_len = vi->hdr_len;
  1208. unsigned int len;
  1209. if (room)
  1210. return PAGE_SIZE - room;
  1211. len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
  1212. rq->min_buf_len, PAGE_SIZE - hdr_len);
  1213. return ALIGN(len, L1_CACHE_BYTES);
  1214. }
  1215. static int add_recvbuf_mergeable(struct virtnet_info *vi,
  1216. struct receive_queue *rq, gfp_t gfp)
  1217. {
  1218. struct page_frag *alloc_frag = &rq->alloc_frag;
  1219. unsigned int headroom = virtnet_get_headroom(vi);
  1220. unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
  1221. unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
  1222. char *buf;
  1223. void *ctx;
  1224. int err;
  1225. unsigned int len, hole;
  1226. /* Extra tailroom is needed to satisfy XDP's assumption. This
  1227. * means rx frags coalescing won't work, but consider we've
  1228. * disabled GSO for XDP, it won't be a big issue.
  1229. */
  1230. len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
  1231. if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
  1232. return -ENOMEM;
  1233. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  1234. buf += headroom; /* advance address leaving hole at front of pkt */
  1235. get_page(alloc_frag->page);
  1236. alloc_frag->offset += len + room;
  1237. hole = alloc_frag->size - alloc_frag->offset;
  1238. if (hole < len + room) {
  1239. /* To avoid internal fragmentation, if there is very likely not
  1240. * enough space for another buffer, add the remaining space to
  1241. * the current buffer.
  1242. */
  1243. len += hole;
  1244. alloc_frag->offset += hole;
  1245. }
  1246. sg_init_one(rq->sg, buf, len);
  1247. ctx = mergeable_len_to_ctx(len, headroom);
  1248. err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
  1249. if (err < 0)
  1250. put_page(virt_to_head_page(buf));
  1251. return err;
  1252. }
  1253. /*
  1254. * Returns false if we couldn't fill entirely (OOM).
  1255. *
  1256. * Normally run in the receive path, but can also be run from ndo_open
  1257. * before we're receiving packets, or from refill_work which is
  1258. * careful to disable receiving (using napi_disable).
  1259. */
  1260. static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
  1261. gfp_t gfp)
  1262. {
  1263. int err;
  1264. bool oom;
  1265. do {
  1266. if (vi->mergeable_rx_bufs)
  1267. err = add_recvbuf_mergeable(vi, rq, gfp);
  1268. else if (vi->big_packets)
  1269. err = add_recvbuf_big(vi, rq, gfp);
  1270. else
  1271. err = add_recvbuf_small(vi, rq, gfp);
  1272. oom = err == -ENOMEM;
  1273. if (err)
  1274. break;
  1275. } while (rq->vq->num_free);
  1276. if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
  1277. unsigned long flags;
  1278. flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
  1279. rq->stats.kicks++;
  1280. u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
  1281. }
  1282. return !oom;
  1283. }
  1284. static void skb_recv_done(struct virtqueue *rvq)
  1285. {
  1286. struct virtnet_info *vi = rvq->vdev->priv;
  1287. struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
  1288. virtqueue_napi_schedule(&rq->napi, rvq);
  1289. }
  1290. static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
  1291. {
  1292. napi_enable(napi);
  1293. /* If all buffers were filled by other side before we napi_enabled, we
  1294. * won't get another interrupt, so process any outstanding packets now.
  1295. * Call local_bh_enable after to trigger softIRQ processing.
  1296. */
  1297. local_bh_disable();
  1298. virtqueue_napi_schedule(napi, vq);
  1299. local_bh_enable();
  1300. }
  1301. static void virtnet_napi_tx_enable(struct virtnet_info *vi,
  1302. struct virtqueue *vq,
  1303. struct napi_struct *napi)
  1304. {
  1305. if (!napi->weight)
  1306. return;
  1307. /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
  1308. * enable the feature if this is likely affine with the transmit path.
  1309. */
  1310. if (!vi->affinity_hint_set) {
  1311. napi->weight = 0;
  1312. return;
  1313. }
  1314. return virtnet_napi_enable(vq, napi);
  1315. }
  1316. static void virtnet_napi_tx_disable(struct napi_struct *napi)
  1317. {
  1318. if (napi->weight)
  1319. napi_disable(napi);
  1320. }
  1321. static void refill_work(struct work_struct *work)
  1322. {
  1323. struct virtnet_info *vi =
  1324. container_of(work, struct virtnet_info, refill.work);
  1325. bool still_empty;
  1326. int i;
  1327. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1328. struct receive_queue *rq = &vi->rq[i];
  1329. napi_disable(&rq->napi);
  1330. still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
  1331. virtnet_napi_enable(rq->vq, &rq->napi);
  1332. /* In theory, this can happen: if we don't get any buffers in
  1333. * we will *never* try to fill again.
  1334. */
  1335. if (still_empty)
  1336. schedule_delayed_work(&vi->refill, HZ/2);
  1337. }
  1338. }
  1339. static int virtnet_receive(struct receive_queue *rq, int budget,
  1340. unsigned int *xdp_xmit)
  1341. {
  1342. struct virtnet_info *vi = rq->vq->vdev->priv;
  1343. struct virtnet_rq_stats stats = {};
  1344. unsigned int len;
  1345. void *buf;
  1346. int i;
  1347. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  1348. void *ctx;
  1349. while (stats.packets < budget &&
  1350. (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
  1351. receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
  1352. stats.packets++;
  1353. }
  1354. } else {
  1355. while (stats.packets < budget &&
  1356. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  1357. receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
  1358. stats.packets++;
  1359. }
  1360. }
  1361. if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
  1362. if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
  1363. spin_lock(&vi->refill_lock);
  1364. if (vi->refill_enabled)
  1365. schedule_delayed_work(&vi->refill, 0);
  1366. spin_unlock(&vi->refill_lock);
  1367. }
  1368. }
  1369. u64_stats_update_begin(&rq->stats.syncp);
  1370. for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
  1371. size_t offset = virtnet_rq_stats_desc[i].offset;
  1372. u64 *item;
  1373. item = (u64 *)((u8 *)&rq->stats + offset);
  1374. *item += *(u64 *)((u8 *)&stats + offset);
  1375. }
  1376. u64_stats_update_end(&rq->stats.syncp);
  1377. return stats.packets;
  1378. }
  1379. static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
  1380. {
  1381. unsigned int len;
  1382. unsigned int packets = 0;
  1383. unsigned int bytes = 0;
  1384. void *ptr;
  1385. while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  1386. if (likely(!is_xdp_frame(ptr))) {
  1387. struct sk_buff *skb = ptr;
  1388. pr_debug("Sent skb %p\n", skb);
  1389. bytes += skb->len;
  1390. napi_consume_skb(skb, in_napi);
  1391. } else {
  1392. struct xdp_frame *frame = ptr_to_xdp(ptr);
  1393. bytes += frame->len;
  1394. xdp_return_frame(frame);
  1395. }
  1396. packets++;
  1397. }
  1398. /* Avoid overhead when no packets have been processed
  1399. * happens when called speculatively from start_xmit.
  1400. */
  1401. if (!packets)
  1402. return;
  1403. u64_stats_update_begin(&sq->stats.syncp);
  1404. sq->stats.bytes += bytes;
  1405. sq->stats.packets += packets;
  1406. u64_stats_update_end(&sq->stats.syncp);
  1407. }
  1408. static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
  1409. {
  1410. if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
  1411. return false;
  1412. else if (q < vi->curr_queue_pairs)
  1413. return true;
  1414. else
  1415. return false;
  1416. }
  1417. static void virtnet_poll_cleantx(struct receive_queue *rq)
  1418. {
  1419. struct virtnet_info *vi = rq->vq->vdev->priv;
  1420. unsigned int index = vq2rxq(rq->vq);
  1421. struct send_queue *sq = &vi->sq[index];
  1422. struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
  1423. if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
  1424. return;
  1425. if (__netif_tx_trylock(txq)) {
  1426. if (sq->reset) {
  1427. __netif_tx_unlock(txq);
  1428. return;
  1429. }
  1430. do {
  1431. virtqueue_disable_cb(sq->vq);
  1432. free_old_xmit_skbs(sq, true);
  1433. } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
  1434. if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  1435. netif_tx_wake_queue(txq);
  1436. __netif_tx_unlock(txq);
  1437. }
  1438. }
  1439. static int virtnet_poll(struct napi_struct *napi, int budget)
  1440. {
  1441. struct receive_queue *rq =
  1442. container_of(napi, struct receive_queue, napi);
  1443. struct virtnet_info *vi = rq->vq->vdev->priv;
  1444. struct send_queue *sq;
  1445. unsigned int received;
  1446. unsigned int xdp_xmit = 0;
  1447. virtnet_poll_cleantx(rq);
  1448. received = virtnet_receive(rq, budget, &xdp_xmit);
  1449. if (xdp_xmit & VIRTIO_XDP_REDIR)
  1450. xdp_do_flush();
  1451. /* Out of packets? */
  1452. if (received < budget)
  1453. virtqueue_napi_complete(napi, rq->vq, received);
  1454. if (xdp_xmit & VIRTIO_XDP_TX) {
  1455. sq = virtnet_xdp_get_sq(vi);
  1456. if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
  1457. u64_stats_update_begin(&sq->stats.syncp);
  1458. sq->stats.kicks++;
  1459. u64_stats_update_end(&sq->stats.syncp);
  1460. }
  1461. virtnet_xdp_put_sq(vi, sq);
  1462. }
  1463. return received;
  1464. }
  1465. static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
  1466. {
  1467. virtnet_napi_tx_disable(&vi->sq[qp_index].napi);
  1468. napi_disable(&vi->rq[qp_index].napi);
  1469. xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
  1470. }
  1471. static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
  1472. {
  1473. struct net_device *dev = vi->dev;
  1474. int err;
  1475. err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
  1476. vi->rq[qp_index].napi.napi_id);
  1477. if (err < 0)
  1478. return err;
  1479. err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
  1480. MEM_TYPE_PAGE_SHARED, NULL);
  1481. if (err < 0)
  1482. goto err_xdp_reg_mem_model;
  1483. virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi);
  1484. virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi);
  1485. return 0;
  1486. err_xdp_reg_mem_model:
  1487. xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
  1488. return err;
  1489. }
  1490. static int virtnet_open(struct net_device *dev)
  1491. {
  1492. struct virtnet_info *vi = netdev_priv(dev);
  1493. int i, err;
  1494. enable_delayed_refill(vi);
  1495. for (i = 0; i < vi->max_queue_pairs; i++) {
  1496. if (i < vi->curr_queue_pairs)
  1497. /* Make sure we have some buffers: if oom use wq. */
  1498. if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
  1499. schedule_delayed_work(&vi->refill, 0);
  1500. err = virtnet_enable_queue_pair(vi, i);
  1501. if (err < 0)
  1502. goto err_enable_qp;
  1503. }
  1504. return 0;
  1505. err_enable_qp:
  1506. disable_delayed_refill(vi);
  1507. cancel_delayed_work_sync(&vi->refill);
  1508. for (i--; i >= 0; i--)
  1509. virtnet_disable_queue_pair(vi, i);
  1510. return err;
  1511. }
  1512. static int virtnet_poll_tx(struct napi_struct *napi, int budget)
  1513. {
  1514. struct send_queue *sq = container_of(napi, struct send_queue, napi);
  1515. struct virtnet_info *vi = sq->vq->vdev->priv;
  1516. unsigned int index = vq2txq(sq->vq);
  1517. struct netdev_queue *txq;
  1518. int opaque;
  1519. bool done;
  1520. if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
  1521. /* We don't need to enable cb for XDP */
  1522. napi_complete_done(napi, 0);
  1523. return 0;
  1524. }
  1525. txq = netdev_get_tx_queue(vi->dev, index);
  1526. __netif_tx_lock(txq, raw_smp_processor_id());
  1527. virtqueue_disable_cb(sq->vq);
  1528. free_old_xmit_skbs(sq, true);
  1529. if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  1530. netif_tx_wake_queue(txq);
  1531. opaque = virtqueue_enable_cb_prepare(sq->vq);
  1532. done = napi_complete_done(napi, 0);
  1533. if (!done)
  1534. virtqueue_disable_cb(sq->vq);
  1535. __netif_tx_unlock(txq);
  1536. if (done) {
  1537. if (unlikely(virtqueue_poll(sq->vq, opaque))) {
  1538. if (napi_schedule_prep(napi)) {
  1539. __netif_tx_lock(txq, raw_smp_processor_id());
  1540. virtqueue_disable_cb(sq->vq);
  1541. __netif_tx_unlock(txq);
  1542. __napi_schedule(napi);
  1543. }
  1544. }
  1545. }
  1546. return 0;
  1547. }
  1548. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
  1549. {
  1550. struct virtio_net_hdr_mrg_rxbuf *hdr;
  1551. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  1552. struct virtnet_info *vi = sq->vq->vdev->priv;
  1553. int num_sg;
  1554. unsigned hdr_len = vi->hdr_len;
  1555. bool can_push;
  1556. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  1557. can_push = vi->any_header_sg &&
  1558. !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
  1559. !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
  1560. /* Even if we can, don't push here yet as this would skew
  1561. * csum_start offset below. */
  1562. if (can_push)
  1563. hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
  1564. else
  1565. hdr = skb_vnet_hdr(skb);
  1566. if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
  1567. virtio_is_little_endian(vi->vdev), false,
  1568. 0))
  1569. return -EPROTO;
  1570. if (vi->mergeable_rx_bufs)
  1571. hdr->num_buffers = 0;
  1572. sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
  1573. if (can_push) {
  1574. __skb_push(skb, hdr_len);
  1575. num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
  1576. if (unlikely(num_sg < 0))
  1577. return num_sg;
  1578. /* Pull header back to avoid skew in tx bytes calculations. */
  1579. __skb_pull(skb, hdr_len);
  1580. } else {
  1581. sg_set_buf(sq->sg, hdr, hdr_len);
  1582. num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
  1583. if (unlikely(num_sg < 0))
  1584. return num_sg;
  1585. num_sg++;
  1586. }
  1587. return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
  1588. }
  1589. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  1590. {
  1591. struct virtnet_info *vi = netdev_priv(dev);
  1592. int qnum = skb_get_queue_mapping(skb);
  1593. struct send_queue *sq = &vi->sq[qnum];
  1594. int err;
  1595. struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
  1596. bool kick = !netdev_xmit_more();
  1597. bool use_napi = sq->napi.weight;
  1598. /* Free up any pending old buffers before queueing new ones. */
  1599. do {
  1600. if (use_napi)
  1601. virtqueue_disable_cb(sq->vq);
  1602. free_old_xmit_skbs(sq, false);
  1603. } while (use_napi && kick &&
  1604. unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
  1605. /* timestamp packet in software */
  1606. skb_tx_timestamp(skb);
  1607. /* Try to transmit */
  1608. err = xmit_skb(sq, skb);
  1609. /* This should not happen! */
  1610. if (unlikely(err)) {
  1611. dev->stats.tx_fifo_errors++;
  1612. if (net_ratelimit())
  1613. dev_warn(&dev->dev,
  1614. "Unexpected TXQ (%d) queue failure: %d\n",
  1615. qnum, err);
  1616. dev->stats.tx_dropped++;
  1617. dev_kfree_skb_any(skb);
  1618. return NETDEV_TX_OK;
  1619. }
  1620. /* Don't wait up for transmitted skbs to be freed. */
  1621. if (!use_napi) {
  1622. skb_orphan(skb);
  1623. nf_reset_ct(skb);
  1624. }
  1625. /* If running out of space, stop queue to avoid getting packets that we
  1626. * are then unable to transmit.
  1627. * An alternative would be to force queuing layer to requeue the skb by
  1628. * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
  1629. * returned in a normal path of operation: it means that driver is not
  1630. * maintaining the TX queue stop/start state properly, and causes
  1631. * the stack to do a non-trivial amount of useless work.
  1632. * Since most packets only take 1 or 2 ring slots, stopping the queue
  1633. * early means 16 slots are typically wasted.
  1634. */
  1635. if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
  1636. netif_stop_subqueue(dev, qnum);
  1637. if (use_napi) {
  1638. if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
  1639. virtqueue_napi_schedule(&sq->napi, sq->vq);
  1640. } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
  1641. /* More just got used, free them then recheck. */
  1642. free_old_xmit_skbs(sq, false);
  1643. if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
  1644. netif_start_subqueue(dev, qnum);
  1645. virtqueue_disable_cb(sq->vq);
  1646. }
  1647. }
  1648. }
  1649. if (kick || netif_xmit_stopped(txq)) {
  1650. if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
  1651. u64_stats_update_begin(&sq->stats.syncp);
  1652. sq->stats.kicks++;
  1653. u64_stats_update_end(&sq->stats.syncp);
  1654. }
  1655. }
  1656. return NETDEV_TX_OK;
  1657. }
  1658. static int virtnet_rx_resize(struct virtnet_info *vi,
  1659. struct receive_queue *rq, u32 ring_num)
  1660. {
  1661. bool running = netif_running(vi->dev);
  1662. int err, qindex;
  1663. qindex = rq - vi->rq;
  1664. if (running)
  1665. napi_disable(&rq->napi);
  1666. err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_free_unused_buf);
  1667. if (err)
  1668. netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
  1669. if (!try_fill_recv(vi, rq, GFP_KERNEL))
  1670. schedule_delayed_work(&vi->refill, 0);
  1671. if (running)
  1672. virtnet_napi_enable(rq->vq, &rq->napi);
  1673. return err;
  1674. }
  1675. static int virtnet_tx_resize(struct virtnet_info *vi,
  1676. struct send_queue *sq, u32 ring_num)
  1677. {
  1678. bool running = netif_running(vi->dev);
  1679. struct netdev_queue *txq;
  1680. int err, qindex;
  1681. qindex = sq - vi->sq;
  1682. if (running)
  1683. virtnet_napi_tx_disable(&sq->napi);
  1684. txq = netdev_get_tx_queue(vi->dev, qindex);
  1685. /* 1. wait all ximt complete
  1686. * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
  1687. */
  1688. __netif_tx_lock_bh(txq);
  1689. /* Prevent rx poll from accessing sq. */
  1690. sq->reset = true;
  1691. /* Prevent the upper layer from trying to send packets. */
  1692. netif_stop_subqueue(vi->dev, qindex);
  1693. __netif_tx_unlock_bh(txq);
  1694. err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
  1695. if (err)
  1696. netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
  1697. __netif_tx_lock_bh(txq);
  1698. sq->reset = false;
  1699. netif_tx_wake_queue(txq);
  1700. __netif_tx_unlock_bh(txq);
  1701. if (running)
  1702. virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
  1703. return err;
  1704. }
  1705. /*
  1706. * Send command via the control virtqueue and check status. Commands
  1707. * supported by the hypervisor, as indicated by feature bits, should
  1708. * never fail unless improperly formatted.
  1709. */
  1710. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  1711. struct scatterlist *out)
  1712. {
  1713. struct scatterlist *sgs[4], hdr, stat;
  1714. unsigned out_num = 0, tmp;
  1715. int ret;
  1716. /* Caller should know better */
  1717. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
  1718. vi->ctrl->status = ~0;
  1719. vi->ctrl->hdr.class = class;
  1720. vi->ctrl->hdr.cmd = cmd;
  1721. /* Add header */
  1722. sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
  1723. sgs[out_num++] = &hdr;
  1724. if (out)
  1725. sgs[out_num++] = out;
  1726. /* Add return status. */
  1727. sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
  1728. sgs[out_num] = &stat;
  1729. BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
  1730. ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
  1731. if (ret < 0) {
  1732. dev_warn(&vi->vdev->dev,
  1733. "Failed to add sgs for command vq: %d\n.", ret);
  1734. return false;
  1735. }
  1736. if (unlikely(!virtqueue_kick(vi->cvq)))
  1737. return vi->ctrl->status == VIRTIO_NET_OK;
  1738. /* Spin for a response, the kick causes an ioport write, trapping
  1739. * into the hypervisor, so the request should be handled immediately.
  1740. */
  1741. while (!virtqueue_get_buf(vi->cvq, &tmp) &&
  1742. !virtqueue_is_broken(vi->cvq))
  1743. cpu_relax();
  1744. return vi->ctrl->status == VIRTIO_NET_OK;
  1745. }
  1746. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  1747. {
  1748. struct virtnet_info *vi = netdev_priv(dev);
  1749. struct virtio_device *vdev = vi->vdev;
  1750. int ret;
  1751. struct sockaddr *addr;
  1752. struct scatterlist sg;
  1753. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
  1754. return -EOPNOTSUPP;
  1755. addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
  1756. if (!addr)
  1757. return -ENOMEM;
  1758. ret = eth_prepare_mac_addr_change(dev, addr);
  1759. if (ret)
  1760. goto out;
  1761. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  1762. sg_init_one(&sg, addr->sa_data, dev->addr_len);
  1763. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  1764. VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
  1765. dev_warn(&vdev->dev,
  1766. "Failed to set mac address by vq command.\n");
  1767. ret = -EINVAL;
  1768. goto out;
  1769. }
  1770. } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
  1771. !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  1772. unsigned int i;
  1773. /* Naturally, this has an atomicity problem. */
  1774. for (i = 0; i < dev->addr_len; i++)
  1775. virtio_cwrite8(vdev,
  1776. offsetof(struct virtio_net_config, mac) +
  1777. i, addr->sa_data[i]);
  1778. }
  1779. eth_commit_mac_addr_change(dev, p);
  1780. ret = 0;
  1781. out:
  1782. kfree(addr);
  1783. return ret;
  1784. }
  1785. static void virtnet_stats(struct net_device *dev,
  1786. struct rtnl_link_stats64 *tot)
  1787. {
  1788. struct virtnet_info *vi = netdev_priv(dev);
  1789. unsigned int start;
  1790. int i;
  1791. for (i = 0; i < vi->max_queue_pairs; i++) {
  1792. u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
  1793. struct receive_queue *rq = &vi->rq[i];
  1794. struct send_queue *sq = &vi->sq[i];
  1795. do {
  1796. start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
  1797. tpackets = sq->stats.packets;
  1798. tbytes = sq->stats.bytes;
  1799. terrors = sq->stats.tx_timeouts;
  1800. } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
  1801. do {
  1802. start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
  1803. rpackets = rq->stats.packets;
  1804. rbytes = rq->stats.bytes;
  1805. rdrops = rq->stats.drops;
  1806. } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
  1807. tot->rx_packets += rpackets;
  1808. tot->tx_packets += tpackets;
  1809. tot->rx_bytes += rbytes;
  1810. tot->tx_bytes += tbytes;
  1811. tot->rx_dropped += rdrops;
  1812. tot->tx_errors += terrors;
  1813. }
  1814. tot->tx_dropped = dev->stats.tx_dropped;
  1815. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  1816. tot->rx_length_errors = dev->stats.rx_length_errors;
  1817. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  1818. }
  1819. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  1820. {
  1821. rtnl_lock();
  1822. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  1823. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
  1824. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  1825. rtnl_unlock();
  1826. }
  1827. static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  1828. {
  1829. struct scatterlist sg;
  1830. struct net_device *dev = vi->dev;
  1831. if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
  1832. return 0;
  1833. vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
  1834. sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
  1835. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  1836. VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
  1837. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
  1838. queue_pairs);
  1839. return -EINVAL;
  1840. } else {
  1841. vi->curr_queue_pairs = queue_pairs;
  1842. /* virtnet_open() will refill when device is going to up. */
  1843. if (dev->flags & IFF_UP)
  1844. schedule_delayed_work(&vi->refill, 0);
  1845. }
  1846. return 0;
  1847. }
  1848. static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  1849. {
  1850. int err;
  1851. rtnl_lock();
  1852. err = _virtnet_set_queues(vi, queue_pairs);
  1853. rtnl_unlock();
  1854. return err;
  1855. }
  1856. static int virtnet_close(struct net_device *dev)
  1857. {
  1858. struct virtnet_info *vi = netdev_priv(dev);
  1859. int i;
  1860. /* Make sure NAPI doesn't schedule refill work */
  1861. disable_delayed_refill(vi);
  1862. /* Make sure refill_work doesn't re-enable napi! */
  1863. cancel_delayed_work_sync(&vi->refill);
  1864. for (i = 0; i < vi->max_queue_pairs; i++)
  1865. virtnet_disable_queue_pair(vi, i);
  1866. return 0;
  1867. }
  1868. static void virtnet_set_rx_mode(struct net_device *dev)
  1869. {
  1870. struct virtnet_info *vi = netdev_priv(dev);
  1871. struct scatterlist sg[2];
  1872. struct virtio_net_ctrl_mac *mac_data;
  1873. struct netdev_hw_addr *ha;
  1874. int uc_count;
  1875. int mc_count;
  1876. void *buf;
  1877. int i;
  1878. /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
  1879. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  1880. return;
  1881. vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
  1882. vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  1883. sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
  1884. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  1885. VIRTIO_NET_CTRL_RX_PROMISC, sg))
  1886. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  1887. vi->ctrl->promisc ? "en" : "dis");
  1888. sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
  1889. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  1890. VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
  1891. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  1892. vi->ctrl->allmulti ? "en" : "dis");
  1893. uc_count = netdev_uc_count(dev);
  1894. mc_count = netdev_mc_count(dev);
  1895. /* MAC filter - use one buffer for both lists */
  1896. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  1897. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  1898. mac_data = buf;
  1899. if (!buf)
  1900. return;
  1901. sg_init_table(sg, 2);
  1902. /* Store the unicast list and count in the front of the buffer */
  1903. mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
  1904. i = 0;
  1905. netdev_for_each_uc_addr(ha, dev)
  1906. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  1907. sg_set_buf(&sg[0], mac_data,
  1908. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  1909. /* multicast list and count fill the end */
  1910. mac_data = (void *)&mac_data->macs[uc_count][0];
  1911. mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
  1912. i = 0;
  1913. netdev_for_each_mc_addr(ha, dev)
  1914. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  1915. sg_set_buf(&sg[1], mac_data,
  1916. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  1917. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  1918. VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
  1919. dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
  1920. kfree(buf);
  1921. }
  1922. static int virtnet_vlan_rx_add_vid(struct net_device *dev,
  1923. __be16 proto, u16 vid)
  1924. {
  1925. struct virtnet_info *vi = netdev_priv(dev);
  1926. struct scatterlist sg;
  1927. vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
  1928. sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
  1929. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1930. VIRTIO_NET_CTRL_VLAN_ADD, &sg))
  1931. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  1932. return 0;
  1933. }
  1934. static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
  1935. __be16 proto, u16 vid)
  1936. {
  1937. struct virtnet_info *vi = netdev_priv(dev);
  1938. struct scatterlist sg;
  1939. vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
  1940. sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
  1941. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1942. VIRTIO_NET_CTRL_VLAN_DEL, &sg))
  1943. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  1944. return 0;
  1945. }
  1946. static void virtnet_clean_affinity(struct virtnet_info *vi)
  1947. {
  1948. int i;
  1949. if (vi->affinity_hint_set) {
  1950. for (i = 0; i < vi->max_queue_pairs; i++) {
  1951. virtqueue_set_affinity(vi->rq[i].vq, NULL);
  1952. virtqueue_set_affinity(vi->sq[i].vq, NULL);
  1953. }
  1954. vi->affinity_hint_set = false;
  1955. }
  1956. }
  1957. static void virtnet_set_affinity(struct virtnet_info *vi)
  1958. {
  1959. cpumask_var_t mask;
  1960. int stragglers;
  1961. int group_size;
  1962. int i, j, cpu;
  1963. int num_cpu;
  1964. int stride;
  1965. if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
  1966. virtnet_clean_affinity(vi);
  1967. return;
  1968. }
  1969. num_cpu = num_online_cpus();
  1970. stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
  1971. stragglers = num_cpu >= vi->curr_queue_pairs ?
  1972. num_cpu % vi->curr_queue_pairs :
  1973. 0;
  1974. cpu = cpumask_first(cpu_online_mask);
  1975. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1976. group_size = stride + (i < stragglers ? 1 : 0);
  1977. for (j = 0; j < group_size; j++) {
  1978. cpumask_set_cpu(cpu, mask);
  1979. cpu = cpumask_next_wrap(cpu, cpu_online_mask,
  1980. nr_cpu_ids, false);
  1981. }
  1982. virtqueue_set_affinity(vi->rq[i].vq, mask);
  1983. virtqueue_set_affinity(vi->sq[i].vq, mask);
  1984. __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
  1985. cpumask_clear(mask);
  1986. }
  1987. vi->affinity_hint_set = true;
  1988. free_cpumask_var(mask);
  1989. }
  1990. static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
  1991. {
  1992. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1993. node);
  1994. virtnet_set_affinity(vi);
  1995. return 0;
  1996. }
  1997. static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
  1998. {
  1999. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  2000. node_dead);
  2001. virtnet_set_affinity(vi);
  2002. return 0;
  2003. }
  2004. static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
  2005. {
  2006. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  2007. node);
  2008. virtnet_clean_affinity(vi);
  2009. return 0;
  2010. }
  2011. static enum cpuhp_state virtionet_online;
  2012. static int virtnet_cpu_notif_add(struct virtnet_info *vi)
  2013. {
  2014. int ret;
  2015. ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
  2016. if (ret)
  2017. return ret;
  2018. ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  2019. &vi->node_dead);
  2020. if (!ret)
  2021. return ret;
  2022. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  2023. return ret;
  2024. }
  2025. static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
  2026. {
  2027. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  2028. cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  2029. &vi->node_dead);
  2030. }
  2031. static void virtnet_get_ringparam(struct net_device *dev,
  2032. struct ethtool_ringparam *ring,
  2033. struct kernel_ethtool_ringparam *kernel_ring,
  2034. struct netlink_ext_ack *extack)
  2035. {
  2036. struct virtnet_info *vi = netdev_priv(dev);
  2037. ring->rx_max_pending = vi->rq[0].vq->num_max;
  2038. ring->tx_max_pending = vi->sq[0].vq->num_max;
  2039. ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  2040. ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  2041. }
  2042. static int virtnet_set_ringparam(struct net_device *dev,
  2043. struct ethtool_ringparam *ring,
  2044. struct kernel_ethtool_ringparam *kernel_ring,
  2045. struct netlink_ext_ack *extack)
  2046. {
  2047. struct virtnet_info *vi = netdev_priv(dev);
  2048. u32 rx_pending, tx_pending;
  2049. struct receive_queue *rq;
  2050. struct send_queue *sq;
  2051. int i, err;
  2052. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  2053. return -EINVAL;
  2054. rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  2055. tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  2056. if (ring->rx_pending == rx_pending &&
  2057. ring->tx_pending == tx_pending)
  2058. return 0;
  2059. if (ring->rx_pending > vi->rq[0].vq->num_max)
  2060. return -EINVAL;
  2061. if (ring->tx_pending > vi->sq[0].vq->num_max)
  2062. return -EINVAL;
  2063. for (i = 0; i < vi->max_queue_pairs; i++) {
  2064. rq = vi->rq + i;
  2065. sq = vi->sq + i;
  2066. if (ring->tx_pending != tx_pending) {
  2067. err = virtnet_tx_resize(vi, sq, ring->tx_pending);
  2068. if (err)
  2069. return err;
  2070. }
  2071. if (ring->rx_pending != rx_pending) {
  2072. err = virtnet_rx_resize(vi, rq, ring->rx_pending);
  2073. if (err)
  2074. return err;
  2075. }
  2076. }
  2077. return 0;
  2078. }
  2079. static bool virtnet_commit_rss_command(struct virtnet_info *vi)
  2080. {
  2081. struct net_device *dev = vi->dev;
  2082. struct scatterlist sgs[4];
  2083. unsigned int sg_buf_size;
  2084. /* prepare sgs */
  2085. sg_init_table(sgs, 4);
  2086. sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
  2087. sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
  2088. sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
  2089. sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
  2090. sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
  2091. - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
  2092. sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
  2093. sg_buf_size = vi->rss_key_size;
  2094. sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
  2095. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  2096. vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
  2097. : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
  2098. dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
  2099. return false;
  2100. }
  2101. return true;
  2102. }
  2103. static void virtnet_init_default_rss(struct virtnet_info *vi)
  2104. {
  2105. u32 indir_val = 0;
  2106. int i = 0;
  2107. vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
  2108. vi->rss_hash_types_saved = vi->rss_hash_types_supported;
  2109. vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
  2110. ? vi->rss_indir_table_size - 1 : 0;
  2111. vi->ctrl->rss.unclassified_queue = 0;
  2112. for (; i < vi->rss_indir_table_size; ++i) {
  2113. indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
  2114. vi->ctrl->rss.indirection_table[i] = indir_val;
  2115. }
  2116. vi->ctrl->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0;
  2117. vi->ctrl->rss.hash_key_length = vi->rss_key_size;
  2118. netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
  2119. }
  2120. static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
  2121. {
  2122. info->data = 0;
  2123. switch (info->flow_type) {
  2124. case TCP_V4_FLOW:
  2125. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
  2126. info->data = RXH_IP_SRC | RXH_IP_DST |
  2127. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  2128. } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
  2129. info->data = RXH_IP_SRC | RXH_IP_DST;
  2130. }
  2131. break;
  2132. case TCP_V6_FLOW:
  2133. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
  2134. info->data = RXH_IP_SRC | RXH_IP_DST |
  2135. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  2136. } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
  2137. info->data = RXH_IP_SRC | RXH_IP_DST;
  2138. }
  2139. break;
  2140. case UDP_V4_FLOW:
  2141. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
  2142. info->data = RXH_IP_SRC | RXH_IP_DST |
  2143. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  2144. } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
  2145. info->data = RXH_IP_SRC | RXH_IP_DST;
  2146. }
  2147. break;
  2148. case UDP_V6_FLOW:
  2149. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
  2150. info->data = RXH_IP_SRC | RXH_IP_DST |
  2151. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  2152. } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
  2153. info->data = RXH_IP_SRC | RXH_IP_DST;
  2154. }
  2155. break;
  2156. case IPV4_FLOW:
  2157. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
  2158. info->data = RXH_IP_SRC | RXH_IP_DST;
  2159. break;
  2160. case IPV6_FLOW:
  2161. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
  2162. info->data = RXH_IP_SRC | RXH_IP_DST;
  2163. break;
  2164. default:
  2165. info->data = 0;
  2166. break;
  2167. }
  2168. }
  2169. static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
  2170. {
  2171. u32 new_hashtypes = vi->rss_hash_types_saved;
  2172. bool is_disable = info->data & RXH_DISCARD;
  2173. bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
  2174. /* supports only 'sd', 'sdfn' and 'r' */
  2175. if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
  2176. return false;
  2177. switch (info->flow_type) {
  2178. case TCP_V4_FLOW:
  2179. new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
  2180. if (!is_disable)
  2181. new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
  2182. | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
  2183. break;
  2184. case UDP_V4_FLOW:
  2185. new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
  2186. if (!is_disable)
  2187. new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
  2188. | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
  2189. break;
  2190. case IPV4_FLOW:
  2191. new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
  2192. if (!is_disable)
  2193. new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
  2194. break;
  2195. case TCP_V6_FLOW:
  2196. new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
  2197. if (!is_disable)
  2198. new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
  2199. | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
  2200. break;
  2201. case UDP_V6_FLOW:
  2202. new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
  2203. if (!is_disable)
  2204. new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
  2205. | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
  2206. break;
  2207. case IPV6_FLOW:
  2208. new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
  2209. if (!is_disable)
  2210. new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
  2211. break;
  2212. default:
  2213. /* unsupported flow */
  2214. return false;
  2215. }
  2216. /* if unsupported hashtype was set */
  2217. if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
  2218. return false;
  2219. if (new_hashtypes != vi->rss_hash_types_saved) {
  2220. vi->rss_hash_types_saved = new_hashtypes;
  2221. vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
  2222. if (vi->dev->features & NETIF_F_RXHASH)
  2223. return virtnet_commit_rss_command(vi);
  2224. }
  2225. return true;
  2226. }
  2227. static void virtnet_get_drvinfo(struct net_device *dev,
  2228. struct ethtool_drvinfo *info)
  2229. {
  2230. struct virtnet_info *vi = netdev_priv(dev);
  2231. struct virtio_device *vdev = vi->vdev;
  2232. strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  2233. strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  2234. strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  2235. }
  2236. /* TODO: Eliminate OOO packets during switching */
  2237. static int virtnet_set_channels(struct net_device *dev,
  2238. struct ethtool_channels *channels)
  2239. {
  2240. struct virtnet_info *vi = netdev_priv(dev);
  2241. u16 queue_pairs = channels->combined_count;
  2242. int err;
  2243. /* We don't support separate rx/tx channels.
  2244. * We don't allow setting 'other' channels.
  2245. */
  2246. if (channels->rx_count || channels->tx_count || channels->other_count)
  2247. return -EINVAL;
  2248. if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
  2249. return -EINVAL;
  2250. /* For now we don't support modifying channels while XDP is loaded
  2251. * also when XDP is loaded all RX queues have XDP programs so we only
  2252. * need to check a single RX queue.
  2253. */
  2254. if (vi->rq[0].xdp_prog)
  2255. return -EINVAL;
  2256. cpus_read_lock();
  2257. err = _virtnet_set_queues(vi, queue_pairs);
  2258. if (err) {
  2259. cpus_read_unlock();
  2260. goto err;
  2261. }
  2262. virtnet_set_affinity(vi);
  2263. cpus_read_unlock();
  2264. netif_set_real_num_tx_queues(dev, queue_pairs);
  2265. netif_set_real_num_rx_queues(dev, queue_pairs);
  2266. err:
  2267. return err;
  2268. }
  2269. static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  2270. {
  2271. struct virtnet_info *vi = netdev_priv(dev);
  2272. unsigned int i, j;
  2273. u8 *p = data;
  2274. switch (stringset) {
  2275. case ETH_SS_STATS:
  2276. for (i = 0; i < vi->curr_queue_pairs; i++) {
  2277. for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
  2278. ethtool_sprintf(&p, "rx_queue_%u_%s", i,
  2279. virtnet_rq_stats_desc[j].desc);
  2280. }
  2281. for (i = 0; i < vi->curr_queue_pairs; i++) {
  2282. for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
  2283. ethtool_sprintf(&p, "tx_queue_%u_%s", i,
  2284. virtnet_sq_stats_desc[j].desc);
  2285. }
  2286. break;
  2287. }
  2288. }
  2289. static int virtnet_get_sset_count(struct net_device *dev, int sset)
  2290. {
  2291. struct virtnet_info *vi = netdev_priv(dev);
  2292. switch (sset) {
  2293. case ETH_SS_STATS:
  2294. return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
  2295. VIRTNET_SQ_STATS_LEN);
  2296. default:
  2297. return -EOPNOTSUPP;
  2298. }
  2299. }
  2300. static void virtnet_get_ethtool_stats(struct net_device *dev,
  2301. struct ethtool_stats *stats, u64 *data)
  2302. {
  2303. struct virtnet_info *vi = netdev_priv(dev);
  2304. unsigned int idx = 0, start, i, j;
  2305. const u8 *stats_base;
  2306. size_t offset;
  2307. for (i = 0; i < vi->curr_queue_pairs; i++) {
  2308. struct receive_queue *rq = &vi->rq[i];
  2309. stats_base = (u8 *)&rq->stats;
  2310. do {
  2311. start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
  2312. for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
  2313. offset = virtnet_rq_stats_desc[j].offset;
  2314. data[idx + j] = *(u64 *)(stats_base + offset);
  2315. }
  2316. } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
  2317. idx += VIRTNET_RQ_STATS_LEN;
  2318. }
  2319. for (i = 0; i < vi->curr_queue_pairs; i++) {
  2320. struct send_queue *sq = &vi->sq[i];
  2321. stats_base = (u8 *)&sq->stats;
  2322. do {
  2323. start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
  2324. for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
  2325. offset = virtnet_sq_stats_desc[j].offset;
  2326. data[idx + j] = *(u64 *)(stats_base + offset);
  2327. }
  2328. } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
  2329. idx += VIRTNET_SQ_STATS_LEN;
  2330. }
  2331. }
  2332. static void virtnet_get_channels(struct net_device *dev,
  2333. struct ethtool_channels *channels)
  2334. {
  2335. struct virtnet_info *vi = netdev_priv(dev);
  2336. channels->combined_count = vi->curr_queue_pairs;
  2337. channels->max_combined = vi->max_queue_pairs;
  2338. channels->max_other = 0;
  2339. channels->rx_count = 0;
  2340. channels->tx_count = 0;
  2341. channels->other_count = 0;
  2342. }
  2343. static int virtnet_set_link_ksettings(struct net_device *dev,
  2344. const struct ethtool_link_ksettings *cmd)
  2345. {
  2346. struct virtnet_info *vi = netdev_priv(dev);
  2347. return ethtool_virtdev_set_link_ksettings(dev, cmd,
  2348. &vi->speed, &vi->duplex);
  2349. }
  2350. static int virtnet_get_link_ksettings(struct net_device *dev,
  2351. struct ethtool_link_ksettings *cmd)
  2352. {
  2353. struct virtnet_info *vi = netdev_priv(dev);
  2354. cmd->base.speed = vi->speed;
  2355. cmd->base.duplex = vi->duplex;
  2356. cmd->base.port = PORT_OTHER;
  2357. return 0;
  2358. }
  2359. static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
  2360. struct ethtool_coalesce *ec)
  2361. {
  2362. struct scatterlist sgs_tx, sgs_rx;
  2363. vi->ctrl->coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
  2364. vi->ctrl->coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
  2365. sg_init_one(&sgs_tx, &vi->ctrl->coal_tx, sizeof(vi->ctrl->coal_tx));
  2366. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
  2367. VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
  2368. &sgs_tx))
  2369. return -EINVAL;
  2370. /* Save parameters */
  2371. vi->tx_usecs = ec->tx_coalesce_usecs;
  2372. vi->tx_max_packets = ec->tx_max_coalesced_frames;
  2373. vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
  2374. vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
  2375. sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx));
  2376. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
  2377. VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
  2378. &sgs_rx))
  2379. return -EINVAL;
  2380. /* Save parameters */
  2381. vi->rx_usecs = ec->rx_coalesce_usecs;
  2382. vi->rx_max_packets = ec->rx_max_coalesced_frames;
  2383. return 0;
  2384. }
  2385. static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
  2386. {
  2387. /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
  2388. * feature is negotiated.
  2389. */
  2390. if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
  2391. return -EOPNOTSUPP;
  2392. if (ec->tx_max_coalesced_frames > 1 ||
  2393. ec->rx_max_coalesced_frames != 1)
  2394. return -EINVAL;
  2395. return 0;
  2396. }
  2397. static int virtnet_set_coalesce(struct net_device *dev,
  2398. struct ethtool_coalesce *ec,
  2399. struct kernel_ethtool_coalesce *kernel_coal,
  2400. struct netlink_ext_ack *extack)
  2401. {
  2402. struct virtnet_info *vi = netdev_priv(dev);
  2403. int ret, i, napi_weight;
  2404. bool update_napi = false;
  2405. /* Can't change NAPI weight if the link is up */
  2406. napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
  2407. if (napi_weight ^ vi->sq[0].napi.weight) {
  2408. if (dev->flags & IFF_UP)
  2409. return -EBUSY;
  2410. else
  2411. update_napi = true;
  2412. }
  2413. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
  2414. ret = virtnet_send_notf_coal_cmds(vi, ec);
  2415. else
  2416. ret = virtnet_coal_params_supported(ec);
  2417. if (ret)
  2418. return ret;
  2419. if (update_napi) {
  2420. for (i = 0; i < vi->max_queue_pairs; i++)
  2421. vi->sq[i].napi.weight = napi_weight;
  2422. }
  2423. return ret;
  2424. }
  2425. static int virtnet_get_coalesce(struct net_device *dev,
  2426. struct ethtool_coalesce *ec,
  2427. struct kernel_ethtool_coalesce *kernel_coal,
  2428. struct netlink_ext_ack *extack)
  2429. {
  2430. struct virtnet_info *vi = netdev_priv(dev);
  2431. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
  2432. ec->rx_coalesce_usecs = vi->rx_usecs;
  2433. ec->tx_coalesce_usecs = vi->tx_usecs;
  2434. ec->tx_max_coalesced_frames = vi->tx_max_packets;
  2435. ec->rx_max_coalesced_frames = vi->rx_max_packets;
  2436. } else {
  2437. ec->rx_max_coalesced_frames = 1;
  2438. if (vi->sq[0].napi.weight)
  2439. ec->tx_max_coalesced_frames = 1;
  2440. }
  2441. return 0;
  2442. }
  2443. static void virtnet_init_settings(struct net_device *dev)
  2444. {
  2445. struct virtnet_info *vi = netdev_priv(dev);
  2446. vi->speed = SPEED_UNKNOWN;
  2447. vi->duplex = DUPLEX_UNKNOWN;
  2448. }
  2449. static void virtnet_update_settings(struct virtnet_info *vi)
  2450. {
  2451. u32 speed;
  2452. u8 duplex;
  2453. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
  2454. return;
  2455. virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
  2456. if (ethtool_validate_speed(speed))
  2457. vi->speed = speed;
  2458. virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
  2459. if (ethtool_validate_duplex(duplex))
  2460. vi->duplex = duplex;
  2461. }
  2462. static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
  2463. {
  2464. return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
  2465. }
  2466. static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
  2467. {
  2468. return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
  2469. }
  2470. static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
  2471. {
  2472. struct virtnet_info *vi = netdev_priv(dev);
  2473. int i;
  2474. if (indir) {
  2475. for (i = 0; i < vi->rss_indir_table_size; ++i)
  2476. indir[i] = vi->ctrl->rss.indirection_table[i];
  2477. }
  2478. if (key)
  2479. memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
  2480. if (hfunc)
  2481. *hfunc = ETH_RSS_HASH_TOP;
  2482. return 0;
  2483. }
  2484. static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
  2485. {
  2486. struct virtnet_info *vi = netdev_priv(dev);
  2487. int i;
  2488. if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
  2489. return -EOPNOTSUPP;
  2490. if (indir) {
  2491. for (i = 0; i < vi->rss_indir_table_size; ++i)
  2492. vi->ctrl->rss.indirection_table[i] = indir[i];
  2493. }
  2494. if (key)
  2495. memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
  2496. virtnet_commit_rss_command(vi);
  2497. return 0;
  2498. }
  2499. static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
  2500. {
  2501. struct virtnet_info *vi = netdev_priv(dev);
  2502. int rc = 0;
  2503. switch (info->cmd) {
  2504. case ETHTOOL_GRXRINGS:
  2505. info->data = vi->curr_queue_pairs;
  2506. break;
  2507. case ETHTOOL_GRXFH:
  2508. virtnet_get_hashflow(vi, info);
  2509. break;
  2510. default:
  2511. rc = -EOPNOTSUPP;
  2512. }
  2513. return rc;
  2514. }
  2515. static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
  2516. {
  2517. struct virtnet_info *vi = netdev_priv(dev);
  2518. int rc = 0;
  2519. switch (info->cmd) {
  2520. case ETHTOOL_SRXFH:
  2521. if (!virtnet_set_hashflow(vi, info))
  2522. rc = -EINVAL;
  2523. break;
  2524. default:
  2525. rc = -EOPNOTSUPP;
  2526. }
  2527. return rc;
  2528. }
  2529. static int ethtool_op_get_ts_info_plus(struct net_device *dev, struct ethtool_ts_info *eti)
  2530. {
  2531. struct virtnet_info *vi = netdev_priv(dev);
  2532. int ret = ethtool_op_get_ts_info(dev, eti);
  2533. if (ret) {
  2534. /* calling the default failed */
  2535. return ret;
  2536. }
  2537. /* set TX */
  2538. eti->tx_types = HWTSTAMP_TX_OFF;
  2539. /* set RX */
  2540. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TS)) {
  2541. eti->rx_filters = HWTSTAMP_FILTER_ALL;
  2542. eti->so_timestamping |= (SOF_TIMESTAMPING_RX_HARDWARE
  2543. | SOF_TIMESTAMPING_RAW_HARDWARE);
  2544. } else {
  2545. eti->rx_filters = HWTSTAMP_FILTER_NONE;
  2546. }
  2547. return 0;
  2548. }
  2549. static const struct ethtool_ops virtnet_ethtool_ops = {
  2550. .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
  2551. ETHTOOL_COALESCE_USECS,
  2552. .get_drvinfo = virtnet_get_drvinfo,
  2553. .get_link = ethtool_op_get_link,
  2554. .get_ringparam = virtnet_get_ringparam,
  2555. .set_ringparam = virtnet_set_ringparam,
  2556. .get_strings = virtnet_get_strings,
  2557. .get_sset_count = virtnet_get_sset_count,
  2558. .get_ethtool_stats = virtnet_get_ethtool_stats,
  2559. .set_channels = virtnet_set_channels,
  2560. .get_channels = virtnet_get_channels,
  2561. .get_ts_info = ethtool_op_get_ts_info_plus,
  2562. .get_link_ksettings = virtnet_get_link_ksettings,
  2563. .set_link_ksettings = virtnet_set_link_ksettings,
  2564. .set_coalesce = virtnet_set_coalesce,
  2565. .get_coalesce = virtnet_get_coalesce,
  2566. .get_rxfh_key_size = virtnet_get_rxfh_key_size,
  2567. .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
  2568. .get_rxfh = virtnet_get_rxfh,
  2569. .set_rxfh = virtnet_set_rxfh,
  2570. .get_rxnfc = virtnet_get_rxnfc,
  2571. .set_rxnfc = virtnet_set_rxnfc,
  2572. };
  2573. static void virtnet_freeze_down(struct virtio_device *vdev)
  2574. {
  2575. struct virtnet_info *vi = vdev->priv;
  2576. /* Make sure no work handler is accessing the device */
  2577. flush_work(&vi->config_work);
  2578. netif_tx_lock_bh(vi->dev);
  2579. netif_device_detach(vi->dev);
  2580. netif_tx_unlock_bh(vi->dev);
  2581. if (netif_running(vi->dev))
  2582. virtnet_close(vi->dev);
  2583. }
  2584. static int init_vqs(struct virtnet_info *vi);
  2585. static int virtnet_restore_up(struct virtio_device *vdev)
  2586. {
  2587. struct virtnet_info *vi = vdev->priv;
  2588. int err;
  2589. err = init_vqs(vi);
  2590. if (err)
  2591. return err;
  2592. virtio_device_ready(vdev);
  2593. enable_delayed_refill(vi);
  2594. if (netif_running(vi->dev)) {
  2595. err = virtnet_open(vi->dev);
  2596. if (err)
  2597. return err;
  2598. }
  2599. netif_tx_lock_bh(vi->dev);
  2600. netif_device_attach(vi->dev);
  2601. netif_tx_unlock_bh(vi->dev);
  2602. return err;
  2603. }
  2604. static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
  2605. {
  2606. struct scatterlist sg;
  2607. vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
  2608. sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
  2609. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
  2610. VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
  2611. dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
  2612. return -EINVAL;
  2613. }
  2614. return 0;
  2615. }
  2616. static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
  2617. {
  2618. u64 offloads = 0;
  2619. if (!vi->guest_offloads)
  2620. return 0;
  2621. return virtnet_set_guest_offloads(vi, offloads);
  2622. }
  2623. static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
  2624. {
  2625. u64 offloads = vi->guest_offloads;
  2626. if (!vi->guest_offloads)
  2627. return 0;
  2628. return virtnet_set_guest_offloads(vi, offloads);
  2629. }
  2630. static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  2631. struct netlink_ext_ack *extack)
  2632. {
  2633. unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
  2634. struct virtnet_info *vi = netdev_priv(dev);
  2635. struct bpf_prog *old_prog;
  2636. u16 xdp_qp = 0, curr_qp;
  2637. int i, err;
  2638. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
  2639. && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  2640. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  2641. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
  2642. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
  2643. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
  2644. NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
  2645. return -EOPNOTSUPP;
  2646. }
  2647. if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
  2648. NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
  2649. return -EINVAL;
  2650. }
  2651. if (dev->mtu > max_sz) {
  2652. NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
  2653. netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
  2654. return -EINVAL;
  2655. }
  2656. curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
  2657. if (prog)
  2658. xdp_qp = nr_cpu_ids;
  2659. /* XDP requires extra queues for XDP_TX */
  2660. if (curr_qp + xdp_qp > vi->max_queue_pairs) {
  2661. netdev_warn_once(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
  2662. curr_qp + xdp_qp, vi->max_queue_pairs);
  2663. xdp_qp = 0;
  2664. }
  2665. old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
  2666. if (!prog && !old_prog)
  2667. return 0;
  2668. if (prog)
  2669. bpf_prog_add(prog, vi->max_queue_pairs - 1);
  2670. /* Make sure NAPI is not using any XDP TX queues for RX. */
  2671. if (netif_running(dev)) {
  2672. for (i = 0; i < vi->max_queue_pairs; i++) {
  2673. napi_disable(&vi->rq[i].napi);
  2674. virtnet_napi_tx_disable(&vi->sq[i].napi);
  2675. }
  2676. }
  2677. if (!prog) {
  2678. for (i = 0; i < vi->max_queue_pairs; i++) {
  2679. rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
  2680. if (i == 0)
  2681. virtnet_restore_guest_offloads(vi);
  2682. }
  2683. synchronize_net();
  2684. }
  2685. err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
  2686. if (err)
  2687. goto err;
  2688. netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
  2689. vi->xdp_queue_pairs = xdp_qp;
  2690. if (prog) {
  2691. vi->xdp_enabled = true;
  2692. for (i = 0; i < vi->max_queue_pairs; i++) {
  2693. rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
  2694. if (i == 0 && !old_prog)
  2695. virtnet_clear_guest_offloads(vi);
  2696. }
  2697. } else {
  2698. vi->xdp_enabled = false;
  2699. }
  2700. for (i = 0; i < vi->max_queue_pairs; i++) {
  2701. if (old_prog)
  2702. bpf_prog_put(old_prog);
  2703. if (netif_running(dev)) {
  2704. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  2705. virtnet_napi_tx_enable(vi, vi->sq[i].vq,
  2706. &vi->sq[i].napi);
  2707. }
  2708. }
  2709. return 0;
  2710. err:
  2711. if (!prog) {
  2712. virtnet_clear_guest_offloads(vi);
  2713. for (i = 0; i < vi->max_queue_pairs; i++)
  2714. rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
  2715. }
  2716. if (netif_running(dev)) {
  2717. for (i = 0; i < vi->max_queue_pairs; i++) {
  2718. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  2719. virtnet_napi_tx_enable(vi, vi->sq[i].vq,
  2720. &vi->sq[i].napi);
  2721. }
  2722. }
  2723. if (prog)
  2724. bpf_prog_sub(prog, vi->max_queue_pairs - 1);
  2725. return err;
  2726. }
  2727. static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  2728. {
  2729. switch (xdp->command) {
  2730. case XDP_SETUP_PROG:
  2731. return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
  2732. default:
  2733. return -EINVAL;
  2734. }
  2735. }
  2736. static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
  2737. size_t len)
  2738. {
  2739. struct virtnet_info *vi = netdev_priv(dev);
  2740. int ret;
  2741. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
  2742. return -EOPNOTSUPP;
  2743. ret = snprintf(buf, len, "sby");
  2744. if (ret >= len)
  2745. return -EOPNOTSUPP;
  2746. return 0;
  2747. }
  2748. static int virtnet_set_features(struct net_device *dev,
  2749. netdev_features_t features)
  2750. {
  2751. struct virtnet_info *vi = netdev_priv(dev);
  2752. u64 offloads;
  2753. int err;
  2754. if ((dev->features ^ features) & NETIF_F_GRO_HW) {
  2755. if (vi->xdp_enabled)
  2756. return -EBUSY;
  2757. if (features & NETIF_F_GRO_HW)
  2758. offloads = vi->guest_offloads_capable;
  2759. else
  2760. offloads = vi->guest_offloads_capable &
  2761. ~GUEST_OFFLOAD_GRO_HW_MASK;
  2762. err = virtnet_set_guest_offloads(vi, offloads);
  2763. if (err)
  2764. return err;
  2765. vi->guest_offloads = offloads;
  2766. }
  2767. if ((dev->features ^ features) & NETIF_F_RXHASH) {
  2768. if (features & NETIF_F_RXHASH)
  2769. vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
  2770. else
  2771. vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
  2772. if (!virtnet_commit_rss_command(vi))
  2773. return -EINVAL;
  2774. }
  2775. return 0;
  2776. }
  2777. static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
  2778. {
  2779. struct virtnet_info *priv = netdev_priv(dev);
  2780. struct send_queue *sq = &priv->sq[txqueue];
  2781. struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
  2782. u64_stats_update_begin(&sq->stats.syncp);
  2783. sq->stats.tx_timeouts++;
  2784. u64_stats_update_end(&sq->stats.syncp);
  2785. netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
  2786. txqueue, sq->name, sq->vq->index, sq->vq->name,
  2787. jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
  2788. }
  2789. static int virtnet_ioctl(struct net_device *dev, struct ifreq *if_r, int io_cmd)
  2790. {
  2791. struct virtnet_info *vi = netdev_priv(dev);
  2792. struct hwtstamp_config ts_cfg;
  2793. int flag = 0;
  2794. switch (io_cmd) {
  2795. case SIOCGHWTSTAMP:
  2796. memset(&ts_cfg, 0, sizeof(ts_cfg));
  2797. ts_cfg.tx_type = HWTSTAMP_TX_OFF;
  2798. ts_cfg.rx_filter = vi->rx_hwts_enabled ? HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
  2799. if (copy_to_user(if_r->ifr_data, &ts_cfg, sizeof(ts_cfg)))
  2800. flag = -EFAULT;
  2801. break;
  2802. case SIOCSHWTSTAMP:
  2803. if (copy_from_user(&ts_cfg, if_r->ifr_data, sizeof(ts_cfg))) {
  2804. flag = -EFAULT;
  2805. } else {
  2806. if (ts_cfg.flags || ts_cfg.tx_type != HWTSTAMP_TX_OFF) {
  2807. flag = -EINVAL;
  2808. } else {
  2809. if (ts_cfg.rx_filter != HWTSTAMP_FILTER_NONE)
  2810. ts_cfg.rx_filter = HWTSTAMP_FILTER_ALL;
  2811. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TS)) {
  2812. vi->rx_hwts_enabled = (ts_cfg.rx_filter ==
  2813. HWTSTAMP_FILTER_ALL);
  2814. } else {
  2815. vi->rx_hwts_enabled = false;
  2816. ts_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
  2817. }
  2818. if (copy_to_user(if_r->ifr_data, &ts_cfg, sizeof(ts_cfg)))
  2819. flag = -EFAULT;
  2820. }
  2821. }
  2822. break;
  2823. default:
  2824. flag = -EOPNOTSUPP;
  2825. break;
  2826. }
  2827. return flag;
  2828. }
  2829. static const struct net_device_ops virtnet_netdev = {
  2830. .ndo_open = virtnet_open,
  2831. .ndo_stop = virtnet_close,
  2832. .ndo_start_xmit = start_xmit,
  2833. .ndo_validate_addr = eth_validate_addr,
  2834. .ndo_set_mac_address = virtnet_set_mac_address,
  2835. .ndo_set_rx_mode = virtnet_set_rx_mode,
  2836. .ndo_get_stats64 = virtnet_stats,
  2837. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  2838. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  2839. .ndo_bpf = virtnet_xdp,
  2840. .ndo_xdp_xmit = virtnet_xdp_xmit,
  2841. .ndo_features_check = passthru_features_check,
  2842. .ndo_get_phys_port_name = virtnet_get_phys_port_name,
  2843. .ndo_set_features = virtnet_set_features,
  2844. .ndo_tx_timeout = virtnet_tx_timeout,
  2845. .ndo_eth_ioctl = virtnet_ioctl,
  2846. };
  2847. static void virtnet_config_changed_work(struct work_struct *work)
  2848. {
  2849. struct virtnet_info *vi =
  2850. container_of(work, struct virtnet_info, config_work);
  2851. u16 v;
  2852. if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
  2853. struct virtio_net_config, status, &v) < 0)
  2854. return;
  2855. if (v & VIRTIO_NET_S_ANNOUNCE) {
  2856. netdev_notify_peers(vi->dev);
  2857. virtnet_ack_link_announce(vi);
  2858. }
  2859. /* Ignore unknown (future) status bits */
  2860. v &= VIRTIO_NET_S_LINK_UP;
  2861. if (vi->status == v)
  2862. return;
  2863. vi->status = v;
  2864. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  2865. virtnet_update_settings(vi);
  2866. netif_carrier_on(vi->dev);
  2867. netif_tx_wake_all_queues(vi->dev);
  2868. } else {
  2869. netif_carrier_off(vi->dev);
  2870. netif_tx_stop_all_queues(vi->dev);
  2871. }
  2872. }
  2873. static void virtnet_config_changed(struct virtio_device *vdev)
  2874. {
  2875. struct virtnet_info *vi = vdev->priv;
  2876. schedule_work(&vi->config_work);
  2877. }
  2878. static void virtnet_free_queues(struct virtnet_info *vi)
  2879. {
  2880. int i;
  2881. for (i = 0; i < vi->max_queue_pairs; i++) {
  2882. __netif_napi_del(&vi->rq[i].napi);
  2883. __netif_napi_del(&vi->sq[i].napi);
  2884. }
  2885. /* We called __netif_napi_del(),
  2886. * we need to respect an RCU grace period before freeing vi->rq
  2887. */
  2888. synchronize_net();
  2889. kfree(vi->rq);
  2890. kfree(vi->sq);
  2891. kfree(vi->ctrl);
  2892. }
  2893. static void _free_receive_bufs(struct virtnet_info *vi)
  2894. {
  2895. struct bpf_prog *old_prog;
  2896. int i;
  2897. for (i = 0; i < vi->max_queue_pairs; i++) {
  2898. while (vi->rq[i].pages)
  2899. __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
  2900. old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  2901. RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
  2902. if (old_prog)
  2903. bpf_prog_put(old_prog);
  2904. }
  2905. }
  2906. static void free_receive_bufs(struct virtnet_info *vi)
  2907. {
  2908. rtnl_lock();
  2909. _free_receive_bufs(vi);
  2910. rtnl_unlock();
  2911. }
  2912. static void free_receive_page_frags(struct virtnet_info *vi)
  2913. {
  2914. int i;
  2915. for (i = 0; i < vi->max_queue_pairs; i++)
  2916. if (vi->rq[i].alloc_frag.page)
  2917. put_page(vi->rq[i].alloc_frag.page);
  2918. }
  2919. static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
  2920. {
  2921. if (!is_xdp_frame(buf))
  2922. dev_kfree_skb(buf);
  2923. else
  2924. xdp_return_frame(ptr_to_xdp(buf));
  2925. }
  2926. static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf)
  2927. {
  2928. struct virtnet_info *vi = vq->vdev->priv;
  2929. int i = vq2rxq(vq);
  2930. if (vi->mergeable_rx_bufs)
  2931. put_page(virt_to_head_page(buf));
  2932. else if (vi->big_packets)
  2933. give_pages(&vi->rq[i], buf);
  2934. else
  2935. put_page(virt_to_head_page(buf));
  2936. }
  2937. static void free_unused_bufs(struct virtnet_info *vi)
  2938. {
  2939. void *buf;
  2940. int i;
  2941. for (i = 0; i < vi->max_queue_pairs; i++) {
  2942. struct virtqueue *vq = vi->sq[i].vq;
  2943. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
  2944. virtnet_sq_free_unused_buf(vq, buf);
  2945. cond_resched();
  2946. }
  2947. for (i = 0; i < vi->max_queue_pairs; i++) {
  2948. struct virtqueue *vq = vi->rq[i].vq;
  2949. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
  2950. virtnet_rq_free_unused_buf(vq, buf);
  2951. cond_resched();
  2952. }
  2953. }
  2954. static void virtnet_del_vqs(struct virtnet_info *vi)
  2955. {
  2956. struct virtio_device *vdev = vi->vdev;
  2957. virtnet_clean_affinity(vi);
  2958. vdev->config->del_vqs(vdev);
  2959. virtnet_free_queues(vi);
  2960. }
  2961. /* How large should a single buffer be so a queue full of these can fit at
  2962. * least one full packet?
  2963. * Logic below assumes the mergeable buffer header is used.
  2964. */
  2965. static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
  2966. {
  2967. const unsigned int hdr_len = vi->hdr_len;
  2968. unsigned int rq_size = virtqueue_get_vring_size(vq);
  2969. unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
  2970. unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
  2971. unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
  2972. return max(max(min_buf_len, hdr_len) - hdr_len,
  2973. (unsigned int)GOOD_PACKET_LEN);
  2974. }
  2975. static int virtnet_find_vqs(struct virtnet_info *vi)
  2976. {
  2977. vq_callback_t **callbacks;
  2978. struct virtqueue **vqs;
  2979. int ret = -ENOMEM;
  2980. int i, total_vqs;
  2981. const char **names;
  2982. bool *ctx;
  2983. /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
  2984. * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
  2985. * possible control vq.
  2986. */
  2987. total_vqs = vi->max_queue_pairs * 2 +
  2988. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
  2989. /* Allocate space for find_vqs parameters */
  2990. vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
  2991. if (!vqs)
  2992. goto err_vq;
  2993. callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
  2994. if (!callbacks)
  2995. goto err_callback;
  2996. names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
  2997. if (!names)
  2998. goto err_names;
  2999. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  3000. ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
  3001. if (!ctx)
  3002. goto err_ctx;
  3003. } else {
  3004. ctx = NULL;
  3005. }
  3006. /* Parameters for control virtqueue, if any */
  3007. if (vi->has_cvq) {
  3008. callbacks[total_vqs - 1] = NULL;
  3009. names[total_vqs - 1] = "control";
  3010. }
  3011. /* Allocate/initialize parameters for send/receive virtqueues */
  3012. for (i = 0; i < vi->max_queue_pairs; i++) {
  3013. callbacks[rxq2vq(i)] = skb_recv_done;
  3014. callbacks[txq2vq(i)] = skb_xmit_done;
  3015. sprintf(vi->rq[i].name, "input.%d", i);
  3016. sprintf(vi->sq[i].name, "output.%d", i);
  3017. names[rxq2vq(i)] = vi->rq[i].name;
  3018. names[txq2vq(i)] = vi->sq[i].name;
  3019. if (ctx)
  3020. ctx[rxq2vq(i)] = true;
  3021. }
  3022. ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
  3023. names, ctx, NULL);
  3024. if (ret)
  3025. goto err_find;
  3026. if (vi->has_cvq) {
  3027. vi->cvq = vqs[total_vqs - 1];
  3028. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  3029. vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  3030. }
  3031. for (i = 0; i < vi->max_queue_pairs; i++) {
  3032. vi->rq[i].vq = vqs[rxq2vq(i)];
  3033. vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
  3034. vi->sq[i].vq = vqs[txq2vq(i)];
  3035. }
  3036. /* run here: ret == 0. */
  3037. err_find:
  3038. kfree(ctx);
  3039. err_ctx:
  3040. kfree(names);
  3041. err_names:
  3042. kfree(callbacks);
  3043. err_callback:
  3044. kfree(vqs);
  3045. err_vq:
  3046. return ret;
  3047. }
  3048. static int virtnet_alloc_queues(struct virtnet_info *vi)
  3049. {
  3050. int i;
  3051. if (vi->has_cvq) {
  3052. vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
  3053. if (!vi->ctrl)
  3054. goto err_ctrl;
  3055. } else {
  3056. vi->ctrl = NULL;
  3057. }
  3058. vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
  3059. if (!vi->sq)
  3060. goto err_sq;
  3061. vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
  3062. if (!vi->rq)
  3063. goto err_rq;
  3064. INIT_DELAYED_WORK(&vi->refill, refill_work);
  3065. for (i = 0; i < vi->max_queue_pairs; i++) {
  3066. vi->rq[i].pages = NULL;
  3067. netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
  3068. napi_weight);
  3069. netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
  3070. virtnet_poll_tx,
  3071. napi_tx ? napi_weight : 0);
  3072. sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
  3073. ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
  3074. sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
  3075. u64_stats_init(&vi->rq[i].stats.syncp);
  3076. u64_stats_init(&vi->sq[i].stats.syncp);
  3077. }
  3078. return 0;
  3079. err_rq:
  3080. kfree(vi->sq);
  3081. err_sq:
  3082. kfree(vi->ctrl);
  3083. err_ctrl:
  3084. return -ENOMEM;
  3085. }
  3086. static int init_vqs(struct virtnet_info *vi)
  3087. {
  3088. int ret;
  3089. /* Allocate send & receive queues */
  3090. ret = virtnet_alloc_queues(vi);
  3091. if (ret)
  3092. goto err;
  3093. ret = virtnet_find_vqs(vi);
  3094. if (ret)
  3095. goto err_free;
  3096. cpus_read_lock();
  3097. virtnet_set_affinity(vi);
  3098. cpus_read_unlock();
  3099. return 0;
  3100. err_free:
  3101. virtnet_free_queues(vi);
  3102. err:
  3103. return ret;
  3104. }
  3105. #ifdef CONFIG_SYSFS
  3106. static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
  3107. char *buf)
  3108. {
  3109. struct virtnet_info *vi = netdev_priv(queue->dev);
  3110. unsigned int queue_index = get_netdev_rx_queue_index(queue);
  3111. unsigned int headroom = virtnet_get_headroom(vi);
  3112. unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
  3113. struct ewma_pkt_len *avg;
  3114. BUG_ON(queue_index >= vi->max_queue_pairs);
  3115. avg = &vi->rq[queue_index].mrg_avg_pkt_len;
  3116. return sprintf(buf, "%u\n",
  3117. get_mergeable_buf_len(&vi->rq[queue_index], avg,
  3118. SKB_DATA_ALIGN(headroom + tailroom)));
  3119. }
  3120. static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
  3121. __ATTR_RO(mergeable_rx_buffer_size);
  3122. static struct attribute *virtio_net_mrg_rx_attrs[] = {
  3123. &mergeable_rx_buffer_size_attribute.attr,
  3124. NULL
  3125. };
  3126. static const struct attribute_group virtio_net_mrg_rx_group = {
  3127. .name = "virtio_net",
  3128. .attrs = virtio_net_mrg_rx_attrs
  3129. };
  3130. #endif
  3131. static bool virtnet_fail_on_feature(struct virtio_device *vdev,
  3132. unsigned int fbit,
  3133. const char *fname, const char *dname)
  3134. {
  3135. if (!virtio_has_feature(vdev, fbit))
  3136. return false;
  3137. dev_err(&vdev->dev, "device advertises feature %s but not %s",
  3138. fname, dname);
  3139. return true;
  3140. }
  3141. #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
  3142. virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
  3143. static bool virtnet_validate_features(struct virtio_device *vdev)
  3144. {
  3145. if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
  3146. (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
  3147. "VIRTIO_NET_F_CTRL_VQ") ||
  3148. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
  3149. "VIRTIO_NET_F_CTRL_VQ") ||
  3150. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
  3151. "VIRTIO_NET_F_CTRL_VQ") ||
  3152. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
  3153. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
  3154. "VIRTIO_NET_F_CTRL_VQ") ||
  3155. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
  3156. "VIRTIO_NET_F_CTRL_VQ") ||
  3157. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
  3158. "VIRTIO_NET_F_CTRL_VQ") ||
  3159. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
  3160. "VIRTIO_NET_F_CTRL_VQ"))) {
  3161. return false;
  3162. }
  3163. return true;
  3164. }
  3165. #define MIN_MTU ETH_MIN_MTU
  3166. #define MAX_MTU ETH_MAX_MTU
  3167. static int virtnet_validate(struct virtio_device *vdev)
  3168. {
  3169. if (!vdev->config->get) {
  3170. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  3171. __func__);
  3172. return -EINVAL;
  3173. }
  3174. if (!virtnet_validate_features(vdev))
  3175. return -EINVAL;
  3176. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  3177. int mtu = virtio_cread16(vdev,
  3178. offsetof(struct virtio_net_config,
  3179. mtu));
  3180. if (mtu < MIN_MTU)
  3181. __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
  3182. }
  3183. return 0;
  3184. }
  3185. static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
  3186. {
  3187. return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  3188. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  3189. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
  3190. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO);
  3191. }
  3192. static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
  3193. {
  3194. bool guest_gso = virtnet_check_guest_gso(vi);
  3195. /* If device can receive ANY guest GSO packets, regardless of mtu,
  3196. * allocate packets of maximum size, otherwise limit it to only
  3197. * mtu size worth only.
  3198. */
  3199. if (mtu > ETH_DATA_LEN || guest_gso) {
  3200. vi->big_packets = true;
  3201. vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
  3202. }
  3203. }
  3204. static int virtnet_probe(struct virtio_device *vdev)
  3205. {
  3206. int i, err = -ENOMEM;
  3207. struct net_device *dev;
  3208. struct virtnet_info *vi;
  3209. u16 max_queue_pairs;
  3210. int mtu = 0;
  3211. /* Find if host supports multiqueue/rss virtio_net device */
  3212. max_queue_pairs = 1;
  3213. if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
  3214. max_queue_pairs =
  3215. virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
  3216. /* We need at least 2 queue's */
  3217. if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  3218. max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  3219. !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  3220. max_queue_pairs = 1;
  3221. /* Allocate ourselves a network device with room for our info */
  3222. dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
  3223. if (!dev)
  3224. return -ENOMEM;
  3225. /* Set up network device as normal. */
  3226. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
  3227. IFF_TX_SKB_NO_LINEAR;
  3228. dev->netdev_ops = &virtnet_netdev;
  3229. dev->features = NETIF_F_HIGHDMA;
  3230. dev->ethtool_ops = &virtnet_ethtool_ops;
  3231. SET_NETDEV_DEV(dev, &vdev->dev);
  3232. /* Do we support "hardware" checksums? */
  3233. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  3234. /* This opens up the world of extra features. */
  3235. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  3236. if (csum)
  3237. dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  3238. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  3239. dev->hw_features |= NETIF_F_TSO
  3240. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  3241. }
  3242. /* Individual feature bits: what can host handle? */
  3243. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  3244. dev->hw_features |= NETIF_F_TSO;
  3245. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  3246. dev->hw_features |= NETIF_F_TSO6;
  3247. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  3248. dev->hw_features |= NETIF_F_TSO_ECN;
  3249. dev->features |= NETIF_F_GSO_ROBUST;
  3250. if (gso)
  3251. dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
  3252. /* (!csum && gso) case will be fixed by register_netdev() */
  3253. }
  3254. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
  3255. dev->features |= NETIF_F_RXCSUM;
  3256. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  3257. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
  3258. dev->features |= NETIF_F_GRO_HW;
  3259. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
  3260. dev->hw_features |= NETIF_F_GRO_HW;
  3261. dev->vlan_features = dev->features;
  3262. /* MTU range: 68 - 65535 */
  3263. dev->min_mtu = MIN_MTU;
  3264. dev->max_mtu = MAX_MTU;
  3265. /* Configuration may specify what MAC to use. Otherwise random. */
  3266. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  3267. u8 addr[ETH_ALEN];
  3268. virtio_cread_bytes(vdev,
  3269. offsetof(struct virtio_net_config, mac),
  3270. addr, ETH_ALEN);
  3271. eth_hw_addr_set(dev, addr);
  3272. } else {
  3273. eth_hw_addr_random(dev);
  3274. dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
  3275. dev->dev_addr);
  3276. }
  3277. /* Set up our device-specific information */
  3278. vi = netdev_priv(dev);
  3279. vi->dev = dev;
  3280. vi->vdev = vdev;
  3281. vdev->priv = vi;
  3282. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  3283. spin_lock_init(&vi->refill_lock);
  3284. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  3285. vi->mergeable_rx_bufs = true;
  3286. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
  3287. vi->rx_usecs = 0;
  3288. vi->tx_usecs = 0;
  3289. vi->tx_max_packets = 0;
  3290. vi->rx_max_packets = 0;
  3291. }
  3292. if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
  3293. vi->has_rss_hash_report = true;
  3294. if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
  3295. vi->has_rss = true;
  3296. if (vi->has_rss || vi->has_rss_hash_report) {
  3297. vi->rss_indir_table_size =
  3298. virtio_cread16(vdev, offsetof(struct virtio_net_config,
  3299. rss_max_indirection_table_length));
  3300. vi->rss_key_size =
  3301. virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
  3302. vi->rss_hash_types_supported =
  3303. virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
  3304. vi->rss_hash_types_supported &=
  3305. ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
  3306. VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
  3307. VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
  3308. dev->hw_features |= NETIF_F_RXHASH;
  3309. }
  3310. if (vi->has_rss_hash_report)
  3311. vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
  3312. else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
  3313. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  3314. vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  3315. else
  3316. vi->hdr_len = sizeof(struct virtio_net_hdr);
  3317. if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
  3318. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  3319. vi->any_header_sg = true;
  3320. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  3321. vi->has_cvq = true;
  3322. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  3323. mtu = virtio_cread16(vdev,
  3324. offsetof(struct virtio_net_config,
  3325. mtu));
  3326. if (mtu < dev->min_mtu) {
  3327. /* Should never trigger: MTU was previously validated
  3328. * in virtnet_validate.
  3329. */
  3330. dev_err(&vdev->dev,
  3331. "device MTU appears to have changed it is now %d < %d",
  3332. mtu, dev->min_mtu);
  3333. err = -EINVAL;
  3334. goto free;
  3335. }
  3336. dev->mtu = mtu;
  3337. dev->max_mtu = mtu;
  3338. }
  3339. virtnet_set_big_packets(vi, mtu);
  3340. if (vi->any_header_sg)
  3341. dev->needed_headroom = vi->hdr_len;
  3342. /* Enable multiqueue by default */
  3343. if (num_online_cpus() >= max_queue_pairs)
  3344. vi->curr_queue_pairs = max_queue_pairs;
  3345. else
  3346. vi->curr_queue_pairs = num_online_cpus();
  3347. vi->max_queue_pairs = max_queue_pairs;
  3348. /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
  3349. err = init_vqs(vi);
  3350. if (err)
  3351. goto free;
  3352. #ifdef CONFIG_SYSFS
  3353. if (vi->mergeable_rx_bufs)
  3354. dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
  3355. #endif
  3356. netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
  3357. netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
  3358. virtnet_init_settings(dev);
  3359. if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
  3360. vi->failover = net_failover_create(vi->dev);
  3361. if (IS_ERR(vi->failover)) {
  3362. err = PTR_ERR(vi->failover);
  3363. goto free_vqs;
  3364. }
  3365. }
  3366. if (vi->has_rss || vi->has_rss_hash_report)
  3367. virtnet_init_default_rss(vi);
  3368. /* serialize netdev register + virtio_device_ready() with ndo_open() */
  3369. rtnl_lock();
  3370. err = register_netdevice(dev);
  3371. if (err) {
  3372. pr_debug("virtio_net: registering device failed\n");
  3373. rtnl_unlock();
  3374. goto free_failover;
  3375. }
  3376. virtio_device_ready(vdev);
  3377. _virtnet_set_queues(vi, vi->curr_queue_pairs);
  3378. /* a random MAC address has been assigned, notify the device.
  3379. * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
  3380. * because many devices work fine without getting MAC explicitly
  3381. */
  3382. if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
  3383. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  3384. struct scatterlist sg;
  3385. sg_init_one(&sg, dev->dev_addr, dev->addr_len);
  3386. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  3387. VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
  3388. pr_debug("virtio_net: setting MAC address failed\n");
  3389. rtnl_unlock();
  3390. err = -EINVAL;
  3391. goto free_unregister_netdev;
  3392. }
  3393. }
  3394. rtnl_unlock();
  3395. err = virtnet_cpu_notif_add(vi);
  3396. if (err) {
  3397. pr_debug("virtio_net: registering cpu notifier failed\n");
  3398. goto free_unregister_netdev;
  3399. }
  3400. /* Assume link up if device can't report link status,
  3401. otherwise get link status from config. */
  3402. netif_carrier_off(dev);
  3403. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  3404. schedule_work(&vi->config_work);
  3405. } else {
  3406. vi->status = VIRTIO_NET_S_LINK_UP;
  3407. virtnet_update_settings(vi);
  3408. netif_carrier_on(dev);
  3409. }
  3410. for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
  3411. if (virtio_has_feature(vi->vdev, guest_offloads[i]))
  3412. set_bit(guest_offloads[i], &vi->guest_offloads);
  3413. vi->guest_offloads_capable = vi->guest_offloads;
  3414. pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
  3415. dev->name, max_queue_pairs);
  3416. return 0;
  3417. free_unregister_netdev:
  3418. unregister_netdev(dev);
  3419. free_failover:
  3420. net_failover_destroy(vi->failover);
  3421. free_vqs:
  3422. virtio_reset_device(vdev);
  3423. cancel_delayed_work_sync(&vi->refill);
  3424. free_receive_page_frags(vi);
  3425. virtnet_del_vqs(vi);
  3426. free:
  3427. free_netdev(dev);
  3428. return err;
  3429. }
  3430. static void remove_vq_common(struct virtnet_info *vi)
  3431. {
  3432. virtio_reset_device(vi->vdev);
  3433. /* Free unused buffers in both send and recv, if any. */
  3434. free_unused_bufs(vi);
  3435. free_receive_bufs(vi);
  3436. free_receive_page_frags(vi);
  3437. virtnet_del_vqs(vi);
  3438. }
  3439. static void virtnet_remove(struct virtio_device *vdev)
  3440. {
  3441. struct virtnet_info *vi = vdev->priv;
  3442. virtnet_cpu_notif_remove(vi);
  3443. /* Make sure no work handler is accessing the device. */
  3444. flush_work(&vi->config_work);
  3445. unregister_netdev(vi->dev);
  3446. net_failover_destroy(vi->failover);
  3447. remove_vq_common(vi);
  3448. free_netdev(vi->dev);
  3449. }
  3450. static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
  3451. {
  3452. struct virtnet_info *vi = vdev->priv;
  3453. virtnet_cpu_notif_remove(vi);
  3454. virtnet_freeze_down(vdev);
  3455. remove_vq_common(vi);
  3456. return 0;
  3457. }
  3458. static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
  3459. {
  3460. struct virtnet_info *vi = vdev->priv;
  3461. int err;
  3462. err = virtnet_restore_up(vdev);
  3463. if (err)
  3464. return err;
  3465. virtnet_set_queues(vi, vi->curr_queue_pairs);
  3466. err = virtnet_cpu_notif_add(vi);
  3467. if (err) {
  3468. virtnet_freeze_down(vdev);
  3469. remove_vq_common(vi);
  3470. return err;
  3471. }
  3472. return 0;
  3473. }
  3474. static struct virtio_device_id id_table[] = {
  3475. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  3476. { 0 },
  3477. };
  3478. #define VIRTNET_FEATURES \
  3479. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
  3480. VIRTIO_NET_F_MAC, \
  3481. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
  3482. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
  3483. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
  3484. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
  3485. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
  3486. VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
  3487. VIRTIO_NET_F_CTRL_MAC_ADDR, \
  3488. VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
  3489. VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
  3490. VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
  3491. VIRTIO_NET_F_GUEST_TS
  3492. static unsigned int features[] = {
  3493. VIRTNET_FEATURES,
  3494. };
  3495. static unsigned int features_legacy[] = {
  3496. VIRTNET_FEATURES,
  3497. VIRTIO_NET_F_GSO,
  3498. VIRTIO_F_ANY_LAYOUT,
  3499. };
  3500. static struct virtio_driver virtio_net_driver = {
  3501. .feature_table = features,
  3502. .feature_table_size = ARRAY_SIZE(features),
  3503. .feature_table_legacy = features_legacy,
  3504. .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
  3505. .driver.name = KBUILD_MODNAME,
  3506. .driver.owner = THIS_MODULE,
  3507. .id_table = id_table,
  3508. .validate = virtnet_validate,
  3509. .probe = virtnet_probe,
  3510. .remove = virtnet_remove,
  3511. .config_changed = virtnet_config_changed,
  3512. #ifdef CONFIG_PM_SLEEP
  3513. .freeze = virtnet_freeze,
  3514. .restore = virtnet_restore,
  3515. #endif
  3516. };
  3517. static __init int virtio_net_driver_init(void)
  3518. {
  3519. int ret;
  3520. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
  3521. virtnet_cpu_online,
  3522. virtnet_cpu_down_prep);
  3523. if (ret < 0)
  3524. goto out;
  3525. virtionet_online = ret;
  3526. ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
  3527. NULL, virtnet_cpu_dead);
  3528. if (ret)
  3529. goto err_dead;
  3530. ret = register_virtio_driver(&virtio_net_driver);
  3531. if (ret)
  3532. goto err_virtio;
  3533. return 0;
  3534. err_virtio:
  3535. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  3536. err_dead:
  3537. cpuhp_remove_multi_state(virtionet_online);
  3538. out:
  3539. return ret;
  3540. }
  3541. module_init(virtio_net_driver_init);
  3542. static __exit void virtio_net_driver_exit(void)
  3543. {
  3544. unregister_virtio_driver(&virtio_net_driver);
  3545. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  3546. cpuhp_remove_multi_state(virtionet_online);
  3547. }
  3548. module_exit(virtio_net_driver_exit);
  3549. MODULE_DEVICE_TABLE(virtio, id_table);
  3550. MODULE_DESCRIPTION("Virtio network driver");
  3551. MODULE_LICENSE("GPL");