virtio_net.c 188 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* A network driver using virtio.
  3. *
  4. * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> 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 <linux/dim.h>
  23. #include <net/route.h>
  24. #include <net/xdp.h>
  25. #include <net/net_failover.h>
  26. #include <net/netdev_rx_queue.h>
  27. #include <net/netdev_queues.h>
  28. #include <net/xdp_sock_drv.h>
  29. static int napi_weight = NAPI_POLL_WEIGHT;
  30. module_param(napi_weight, int, 0444);
  31. static bool csum = true, gso = true, napi_tx = true;
  32. module_param(csum, bool, 0444);
  33. module_param(gso, bool, 0444);
  34. module_param(napi_tx, bool, 0644);
  35. #define VIRTIO_OFFLOAD_MAP_MIN 46
  36. #define VIRTIO_OFFLOAD_MAP_MAX 47
  37. #define VIRTIO_FEATURES_MAP_MIN 65
  38. #define VIRTIO_O2F_DELTA (VIRTIO_FEATURES_MAP_MIN - \
  39. VIRTIO_OFFLOAD_MAP_MIN)
  40. static bool virtio_is_mapped_offload(unsigned int obit)
  41. {
  42. return obit >= VIRTIO_OFFLOAD_MAP_MIN &&
  43. obit <= VIRTIO_OFFLOAD_MAP_MAX;
  44. }
  45. static unsigned int virtio_offload_to_feature(unsigned int obit)
  46. {
  47. return virtio_is_mapped_offload(obit) ? obit + VIRTIO_O2F_DELTA : obit;
  48. }
  49. /* FIXME: MTU in config. */
  50. #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
  51. #define GOOD_COPY_LEN 128
  52. #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
  53. /* Separating two types of XDP xmit */
  54. #define VIRTIO_XDP_TX BIT(0)
  55. #define VIRTIO_XDP_REDIR BIT(1)
  56. /* RX packet size EWMA. The average packet size is used to determine the packet
  57. * buffer size when refilling RX rings. As the entire RX ring may be refilled
  58. * at once, the weight is chosen so that the EWMA will be insensitive to short-
  59. * term, transient changes in packet size.
  60. */
  61. DECLARE_EWMA(pkt_len, 0, 64)
  62. #define VIRTNET_DRIVER_VERSION "1.0.0"
  63. static const unsigned long guest_offloads[] = {
  64. VIRTIO_NET_F_GUEST_TSO4,
  65. VIRTIO_NET_F_GUEST_TSO6,
  66. VIRTIO_NET_F_GUEST_ECN,
  67. VIRTIO_NET_F_GUEST_UFO,
  68. VIRTIO_NET_F_GUEST_CSUM,
  69. VIRTIO_NET_F_GUEST_USO4,
  70. VIRTIO_NET_F_GUEST_USO6,
  71. VIRTIO_NET_F_GUEST_HDRLEN,
  72. VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_MAPPED,
  73. VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM_MAPPED,
  74. };
  75. #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
  76. (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
  77. (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
  78. (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
  79. (1ULL << VIRTIO_NET_F_GUEST_USO4) | \
  80. (1ULL << VIRTIO_NET_F_GUEST_USO6) | \
  81. (1ULL << VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_MAPPED) | \
  82. (1ULL << VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM_MAPPED))
  83. struct virtnet_stat_desc {
  84. char desc[ETH_GSTRING_LEN];
  85. size_t offset;
  86. size_t qstat_offset;
  87. };
  88. struct virtnet_sq_free_stats {
  89. u64 packets;
  90. u64 bytes;
  91. u64 napi_packets;
  92. u64 napi_bytes;
  93. u64 xsk;
  94. };
  95. struct virtnet_sq_stats {
  96. struct u64_stats_sync syncp;
  97. u64_stats_t packets;
  98. u64_stats_t bytes;
  99. u64_stats_t xdp_tx;
  100. u64_stats_t xdp_tx_drops;
  101. u64_stats_t kicks;
  102. u64_stats_t tx_timeouts;
  103. u64_stats_t stop;
  104. u64_stats_t wake;
  105. };
  106. struct virtnet_rq_stats {
  107. struct u64_stats_sync syncp;
  108. u64_stats_t packets;
  109. u64_stats_t bytes;
  110. u64_stats_t drops;
  111. u64_stats_t xdp_packets;
  112. u64_stats_t xdp_tx;
  113. u64_stats_t xdp_redirects;
  114. u64_stats_t xdp_drops;
  115. u64_stats_t kicks;
  116. };
  117. #define VIRTNET_SQ_STAT(name, m) {name, offsetof(struct virtnet_sq_stats, m), -1}
  118. #define VIRTNET_RQ_STAT(name, m) {name, offsetof(struct virtnet_rq_stats, m), -1}
  119. #define VIRTNET_SQ_STAT_QSTAT(name, m) \
  120. { \
  121. name, \
  122. offsetof(struct virtnet_sq_stats, m), \
  123. offsetof(struct netdev_queue_stats_tx, m), \
  124. }
  125. #define VIRTNET_RQ_STAT_QSTAT(name, m) \
  126. { \
  127. name, \
  128. offsetof(struct virtnet_rq_stats, m), \
  129. offsetof(struct netdev_queue_stats_rx, m), \
  130. }
  131. static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
  132. VIRTNET_SQ_STAT("xdp_tx", xdp_tx),
  133. VIRTNET_SQ_STAT("xdp_tx_drops", xdp_tx_drops),
  134. VIRTNET_SQ_STAT("kicks", kicks),
  135. VIRTNET_SQ_STAT("tx_timeouts", tx_timeouts),
  136. };
  137. static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
  138. VIRTNET_RQ_STAT("drops", drops),
  139. VIRTNET_RQ_STAT("xdp_packets", xdp_packets),
  140. VIRTNET_RQ_STAT("xdp_tx", xdp_tx),
  141. VIRTNET_RQ_STAT("xdp_redirects", xdp_redirects),
  142. VIRTNET_RQ_STAT("xdp_drops", xdp_drops),
  143. VIRTNET_RQ_STAT("kicks", kicks),
  144. };
  145. static const struct virtnet_stat_desc virtnet_sq_stats_desc_qstat[] = {
  146. VIRTNET_SQ_STAT_QSTAT("packets", packets),
  147. VIRTNET_SQ_STAT_QSTAT("bytes", bytes),
  148. VIRTNET_SQ_STAT_QSTAT("stop", stop),
  149. VIRTNET_SQ_STAT_QSTAT("wake", wake),
  150. };
  151. static const struct virtnet_stat_desc virtnet_rq_stats_desc_qstat[] = {
  152. VIRTNET_RQ_STAT_QSTAT("packets", packets),
  153. VIRTNET_RQ_STAT_QSTAT("bytes", bytes),
  154. };
  155. #define VIRTNET_STATS_DESC_CQ(name) \
  156. {#name, offsetof(struct virtio_net_stats_cvq, name), -1}
  157. #define VIRTNET_STATS_DESC_RX(class, name) \
  158. {#name, offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), -1}
  159. #define VIRTNET_STATS_DESC_TX(class, name) \
  160. {#name, offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), -1}
  161. static const struct virtnet_stat_desc virtnet_stats_cvq_desc[] = {
  162. VIRTNET_STATS_DESC_CQ(command_num),
  163. VIRTNET_STATS_DESC_CQ(ok_num),
  164. };
  165. static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc[] = {
  166. VIRTNET_STATS_DESC_RX(basic, packets),
  167. VIRTNET_STATS_DESC_RX(basic, bytes),
  168. VIRTNET_STATS_DESC_RX(basic, notifications),
  169. VIRTNET_STATS_DESC_RX(basic, interrupts),
  170. };
  171. static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc[] = {
  172. VIRTNET_STATS_DESC_TX(basic, packets),
  173. VIRTNET_STATS_DESC_TX(basic, bytes),
  174. VIRTNET_STATS_DESC_TX(basic, notifications),
  175. VIRTNET_STATS_DESC_TX(basic, interrupts),
  176. };
  177. static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc[] = {
  178. VIRTNET_STATS_DESC_RX(csum, needs_csum),
  179. };
  180. static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc[] = {
  181. VIRTNET_STATS_DESC_TX(gso, gso_packets_noseg),
  182. VIRTNET_STATS_DESC_TX(gso, gso_bytes_noseg),
  183. };
  184. static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc[] = {
  185. VIRTNET_STATS_DESC_RX(speed, ratelimit_bytes),
  186. };
  187. static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc[] = {
  188. VIRTNET_STATS_DESC_TX(speed, ratelimit_bytes),
  189. };
  190. #define VIRTNET_STATS_DESC_RX_QSTAT(class, name, qstat_field) \
  191. { \
  192. #name, \
  193. offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), \
  194. offsetof(struct netdev_queue_stats_rx, qstat_field), \
  195. }
  196. #define VIRTNET_STATS_DESC_TX_QSTAT(class, name, qstat_field) \
  197. { \
  198. #name, \
  199. offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), \
  200. offsetof(struct netdev_queue_stats_tx, qstat_field), \
  201. }
  202. static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc_qstat[] = {
  203. VIRTNET_STATS_DESC_RX_QSTAT(basic, drops, hw_drops),
  204. VIRTNET_STATS_DESC_RX_QSTAT(basic, drop_overruns, hw_drop_overruns),
  205. };
  206. static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc_qstat[] = {
  207. VIRTNET_STATS_DESC_TX_QSTAT(basic, drops, hw_drops),
  208. VIRTNET_STATS_DESC_TX_QSTAT(basic, drop_malformed, hw_drop_errors),
  209. };
  210. static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc_qstat[] = {
  211. VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_valid, csum_unnecessary),
  212. VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_none, csum_none),
  213. VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_bad, csum_bad),
  214. };
  215. static const struct virtnet_stat_desc virtnet_stats_tx_csum_desc_qstat[] = {
  216. VIRTNET_STATS_DESC_TX_QSTAT(csum, csum_none, csum_none),
  217. VIRTNET_STATS_DESC_TX_QSTAT(csum, needs_csum, needs_csum),
  218. };
  219. static const struct virtnet_stat_desc virtnet_stats_rx_gso_desc_qstat[] = {
  220. VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets, hw_gro_packets),
  221. VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes, hw_gro_bytes),
  222. VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets_coalesced, hw_gro_wire_packets),
  223. VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes_coalesced, hw_gro_wire_bytes),
  224. };
  225. static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc_qstat[] = {
  226. VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_packets, hw_gso_packets),
  227. VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_bytes, hw_gso_bytes),
  228. VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments, hw_gso_wire_packets),
  229. VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments_bytes, hw_gso_wire_bytes),
  230. };
  231. static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc_qstat[] = {
  232. VIRTNET_STATS_DESC_RX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
  233. };
  234. static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
  235. VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
  236. };
  237. #define VIRTNET_Q_TYPE_RX 0
  238. #define VIRTNET_Q_TYPE_TX 1
  239. #define VIRTNET_Q_TYPE_CQ 2
  240. struct virtnet_interrupt_coalesce {
  241. u32 max_packets;
  242. u32 max_usecs;
  243. };
  244. /* The dma information of pages allocated at a time. */
  245. struct virtnet_rq_dma {
  246. dma_addr_t addr;
  247. u32 ref;
  248. u16 len;
  249. u16 need_sync;
  250. };
  251. /* Internal representation of a send virtqueue */
  252. struct send_queue {
  253. /* Virtqueue associated with this send _queue */
  254. struct virtqueue *vq;
  255. /* TX: fragments + linear part + virtio header */
  256. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  257. /* Name of the send queue: output.$index */
  258. char name[16];
  259. struct virtnet_sq_stats stats;
  260. struct virtnet_interrupt_coalesce intr_coal;
  261. struct napi_struct napi;
  262. /* Record whether sq is in reset state. */
  263. bool reset;
  264. struct xsk_buff_pool *xsk_pool;
  265. dma_addr_t xsk_hdr_dma_addr;
  266. };
  267. /* Internal representation of a receive virtqueue */
  268. struct receive_queue {
  269. /* Virtqueue associated with this receive_queue */
  270. struct virtqueue *vq;
  271. struct napi_struct napi;
  272. struct bpf_prog __rcu *xdp_prog;
  273. struct virtnet_rq_stats stats;
  274. /* The number of rx notifications */
  275. u16 calls;
  276. /* Is dynamic interrupt moderation enabled? */
  277. bool dim_enabled;
  278. /* Used to protect dim_enabled and inter_coal */
  279. struct mutex dim_lock;
  280. /* Dynamic Interrupt Moderation */
  281. struct dim dim;
  282. u32 packets_in_napi;
  283. struct virtnet_interrupt_coalesce intr_coal;
  284. /* Chain pages by the private ptr. */
  285. struct page *pages;
  286. /* Average packet length for mergeable receive buffers. */
  287. struct ewma_pkt_len mrg_avg_pkt_len;
  288. /* Page frag for packet buffer allocation. */
  289. struct page_frag alloc_frag;
  290. /* RX: fragments + linear part + virtio header */
  291. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  292. /* Min single buffer size for mergeable buffers case. */
  293. unsigned int min_buf_len;
  294. /* Name of this receive queue: input.$index */
  295. char name[16];
  296. struct xdp_rxq_info xdp_rxq;
  297. /* Record the last dma info to free after new pages is allocated. */
  298. struct virtnet_rq_dma *last_dma;
  299. struct xsk_buff_pool *xsk_pool;
  300. /* xdp rxq used by xsk */
  301. struct xdp_rxq_info xsk_rxq_info;
  302. struct xdp_buff **xsk_buffs;
  303. };
  304. /* Control VQ buffers: protected by the rtnl lock */
  305. struct control_buf {
  306. struct virtio_net_ctrl_hdr hdr;
  307. virtio_net_ctrl_ack status;
  308. };
  309. struct virtnet_info {
  310. struct virtio_device *vdev;
  311. struct virtqueue *cvq;
  312. struct net_device *dev;
  313. struct send_queue *sq;
  314. struct receive_queue *rq;
  315. unsigned int status;
  316. /* Max # of queue pairs supported by the device */
  317. u16 max_queue_pairs;
  318. /* # of queue pairs currently used by the driver */
  319. u16 curr_queue_pairs;
  320. /* # of XDP queue pairs currently used by the driver */
  321. u16 xdp_queue_pairs;
  322. /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
  323. bool xdp_enabled;
  324. /* I like... big packets and I cannot lie! */
  325. bool big_packets;
  326. /* number of sg entries allocated for big packets */
  327. unsigned int big_packets_num_skbfrags;
  328. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  329. bool mergeable_rx_bufs;
  330. /* Host supports rss and/or hash report */
  331. bool has_rss;
  332. bool has_rss_hash_report;
  333. u8 rss_key_size;
  334. u16 rss_indir_table_size;
  335. u32 rss_hash_types_supported;
  336. u32 rss_hash_types_saved;
  337. /* Has control virtqueue */
  338. bool has_cvq;
  339. /* Lock to protect the control VQ */
  340. struct mutex cvq_lock;
  341. /* Host can handle any s/g split between our header and packet data */
  342. bool any_header_sg;
  343. /* Packet virtio header size */
  344. u8 hdr_len;
  345. /* UDP tunnel support */
  346. bool tx_tnl;
  347. bool rx_tnl;
  348. bool rx_tnl_csum;
  349. /* Work struct for config space updates */
  350. struct work_struct config_work;
  351. /* Work struct for setting rx mode */
  352. struct work_struct rx_mode_work;
  353. /* OK to queue work setting RX mode? */
  354. bool rx_mode_work_enabled;
  355. /* Does the affinity hint is set for virtqueues? */
  356. bool affinity_hint_set;
  357. /* CPU hotplug instances for online & dead */
  358. struct hlist_node node;
  359. struct hlist_node node_dead;
  360. struct control_buf *ctrl;
  361. /* Ethtool settings */
  362. u8 duplex;
  363. u32 speed;
  364. /* Is rx dynamic interrupt moderation enabled? */
  365. bool rx_dim_enabled;
  366. /* Interrupt coalescing settings */
  367. struct virtnet_interrupt_coalesce intr_coal_tx;
  368. struct virtnet_interrupt_coalesce intr_coal_rx;
  369. unsigned long guest_offloads;
  370. unsigned long guest_offloads_capable;
  371. /* failover when STANDBY feature enabled */
  372. struct failover *failover;
  373. u64 device_stats_cap;
  374. struct virtio_net_rss_config_hdr *rss_hdr;
  375. /* Must be last as it ends in a flexible-array member. */
  376. TRAILING_OVERLAP(struct virtio_net_rss_config_trailer, rss_trailer, hash_key_data,
  377. u8 rss_hash_key_data[NETDEV_RSS_KEY_LEN];
  378. );
  379. };
  380. static_assert(offsetof(struct virtnet_info, rss_trailer.hash_key_data) ==
  381. offsetof(struct virtnet_info, rss_hash_key_data));
  382. struct padded_vnet_hdr {
  383. struct virtio_net_hdr_v1_hash hdr;
  384. /*
  385. * hdr is in a separate sg buffer, and data sg buffer shares same page
  386. * with this header sg. This padding makes next sg 16 byte aligned
  387. * after the header.
  388. */
  389. char padding[12];
  390. };
  391. struct virtio_net_common_hdr {
  392. union {
  393. struct virtio_net_hdr hdr;
  394. struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
  395. struct virtio_net_hdr_v1_hash hash_v1_hdr;
  396. struct virtio_net_hdr_v1_hash_tunnel tnl_hdr;
  397. };
  398. };
  399. static struct virtio_net_common_hdr xsk_hdr;
  400. static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
  401. static void virtnet_sq_free_unused_buf_done(struct virtqueue *vq);
  402. static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
  403. struct net_device *dev,
  404. unsigned int *xdp_xmit,
  405. struct virtnet_rq_stats *stats);
  406. static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
  407. struct sk_buff *skb, u8 flags);
  408. static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
  409. struct sk_buff *curr_skb,
  410. struct page *page, void *buf,
  411. int len, int truesize);
  412. static void virtnet_xsk_completed(struct send_queue *sq, int num);
  413. enum virtnet_xmit_type {
  414. VIRTNET_XMIT_TYPE_SKB,
  415. VIRTNET_XMIT_TYPE_SKB_ORPHAN,
  416. VIRTNET_XMIT_TYPE_XDP,
  417. VIRTNET_XMIT_TYPE_XSK,
  418. };
  419. static size_t virtnet_rss_hdr_size(const struct virtnet_info *vi)
  420. {
  421. u16 indir_table_size = vi->has_rss ? vi->rss_indir_table_size : 1;
  422. return struct_size(vi->rss_hdr, indirection_table, indir_table_size);
  423. }
  424. static size_t virtnet_rss_trailer_size(const struct virtnet_info *vi)
  425. {
  426. return struct_size(&vi->rss_trailer, hash_key_data, vi->rss_key_size);
  427. }
  428. /* We use the last two bits of the pointer to distinguish the xmit type. */
  429. #define VIRTNET_XMIT_TYPE_MASK (BIT(0) | BIT(1))
  430. #define VIRTIO_XSK_FLAG_OFFSET 2
  431. static enum virtnet_xmit_type virtnet_xmit_ptr_unpack(void **ptr)
  432. {
  433. unsigned long p = (unsigned long)*ptr;
  434. *ptr = (void *)(p & ~VIRTNET_XMIT_TYPE_MASK);
  435. return p & VIRTNET_XMIT_TYPE_MASK;
  436. }
  437. static void *virtnet_xmit_ptr_pack(void *ptr, enum virtnet_xmit_type type)
  438. {
  439. return (void *)((unsigned long)ptr | type);
  440. }
  441. static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
  442. enum virtnet_xmit_type type)
  443. {
  444. return virtqueue_add_outbuf(sq->vq, sq->sg, num,
  445. virtnet_xmit_ptr_pack(data, type),
  446. GFP_ATOMIC);
  447. }
  448. static u32 virtnet_ptr_to_xsk_buff_len(void *ptr)
  449. {
  450. return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
  451. }
  452. static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
  453. {
  454. sg_dma_address(sg) = addr;
  455. sg_dma_len(sg) = len;
  456. }
  457. static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
  458. bool in_napi, struct virtnet_sq_free_stats *stats)
  459. {
  460. struct xdp_frame *frame;
  461. struct sk_buff *skb;
  462. unsigned int len;
  463. void *ptr;
  464. while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  465. switch (virtnet_xmit_ptr_unpack(&ptr)) {
  466. case VIRTNET_XMIT_TYPE_SKB:
  467. skb = ptr;
  468. pr_debug("Sent skb %p\n", skb);
  469. stats->napi_packets++;
  470. stats->napi_bytes += skb->len;
  471. napi_consume_skb(skb, in_napi);
  472. break;
  473. case VIRTNET_XMIT_TYPE_SKB_ORPHAN:
  474. skb = ptr;
  475. stats->packets++;
  476. stats->bytes += skb->len;
  477. napi_consume_skb(skb, in_napi);
  478. break;
  479. case VIRTNET_XMIT_TYPE_XDP:
  480. frame = ptr;
  481. stats->packets++;
  482. stats->bytes += xdp_get_frame_len(frame);
  483. xdp_return_frame(frame);
  484. break;
  485. case VIRTNET_XMIT_TYPE_XSK:
  486. stats->bytes += virtnet_ptr_to_xsk_buff_len(ptr);
  487. stats->xsk++;
  488. break;
  489. }
  490. }
  491. netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
  492. }
  493. static void virtnet_free_old_xmit(struct send_queue *sq,
  494. struct netdev_queue *txq,
  495. bool in_napi,
  496. struct virtnet_sq_free_stats *stats)
  497. {
  498. __free_old_xmit(sq, txq, in_napi, stats);
  499. if (stats->xsk)
  500. virtnet_xsk_completed(sq, stats->xsk);
  501. }
  502. /* Converting between virtqueue no. and kernel tx/rx queue no.
  503. * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
  504. */
  505. static int vq2txq(struct virtqueue *vq)
  506. {
  507. return (vq->index - 1) / 2;
  508. }
  509. static int txq2vq(int txq)
  510. {
  511. return txq * 2 + 1;
  512. }
  513. static int vq2rxq(struct virtqueue *vq)
  514. {
  515. return vq->index / 2;
  516. }
  517. static int rxq2vq(int rxq)
  518. {
  519. return rxq * 2;
  520. }
  521. static int vq_type(struct virtnet_info *vi, int qid)
  522. {
  523. if (qid == vi->max_queue_pairs * 2)
  524. return VIRTNET_Q_TYPE_CQ;
  525. if (qid % 2)
  526. return VIRTNET_Q_TYPE_TX;
  527. return VIRTNET_Q_TYPE_RX;
  528. }
  529. static inline struct virtio_net_common_hdr *
  530. skb_vnet_common_hdr(struct sk_buff *skb)
  531. {
  532. return (struct virtio_net_common_hdr *)skb->cb;
  533. }
  534. /*
  535. * private is used to chain pages for big packets, put the whole
  536. * most recent used list in the beginning for reuse
  537. */
  538. static void give_pages(struct receive_queue *rq, struct page *page)
  539. {
  540. struct page *end;
  541. /* Find end of list, sew whole thing into vi->rq.pages. */
  542. for (end = page; end->private; end = (struct page *)end->private);
  543. end->private = (unsigned long)rq->pages;
  544. rq->pages = page;
  545. }
  546. static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  547. {
  548. struct page *p = rq->pages;
  549. if (p) {
  550. rq->pages = (struct page *)p->private;
  551. /* clear private here, it is used to chain pages */
  552. p->private = 0;
  553. } else
  554. p = alloc_page(gfp_mask);
  555. return p;
  556. }
  557. static void virtnet_rq_free_buf(struct virtnet_info *vi,
  558. struct receive_queue *rq, void *buf)
  559. {
  560. if (vi->mergeable_rx_bufs)
  561. put_page(virt_to_head_page(buf));
  562. else if (vi->big_packets)
  563. give_pages(rq, buf);
  564. else
  565. put_page(virt_to_head_page(buf));
  566. }
  567. static void enable_rx_mode_work(struct virtnet_info *vi)
  568. {
  569. rtnl_lock();
  570. vi->rx_mode_work_enabled = true;
  571. rtnl_unlock();
  572. }
  573. static void disable_rx_mode_work(struct virtnet_info *vi)
  574. {
  575. rtnl_lock();
  576. vi->rx_mode_work_enabled = false;
  577. rtnl_unlock();
  578. }
  579. static void virtqueue_napi_schedule(struct napi_struct *napi,
  580. struct virtqueue *vq)
  581. {
  582. if (napi_schedule_prep(napi)) {
  583. virtqueue_disable_cb(vq);
  584. __napi_schedule(napi);
  585. }
  586. }
  587. static bool virtqueue_napi_complete(struct napi_struct *napi,
  588. struct virtqueue *vq, int processed)
  589. {
  590. int opaque;
  591. opaque = virtqueue_enable_cb_prepare(vq);
  592. if (napi_complete_done(napi, processed)) {
  593. if (unlikely(virtqueue_poll(vq, opaque)))
  594. virtqueue_napi_schedule(napi, vq);
  595. else
  596. return true;
  597. } else {
  598. virtqueue_disable_cb(vq);
  599. }
  600. return false;
  601. }
  602. static void virtnet_tx_wake_queue(struct virtnet_info *vi,
  603. struct send_queue *sq)
  604. {
  605. unsigned int index = vq2txq(sq->vq);
  606. struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
  607. if (netif_tx_queue_stopped(txq)) {
  608. u64_stats_update_begin(&sq->stats.syncp);
  609. u64_stats_inc(&sq->stats.wake);
  610. u64_stats_update_end(&sq->stats.syncp);
  611. netif_tx_wake_queue(txq);
  612. }
  613. }
  614. static void skb_xmit_done(struct virtqueue *vq)
  615. {
  616. struct virtnet_info *vi = vq->vdev->priv;
  617. unsigned int index = vq2txq(vq);
  618. struct send_queue *sq = &vi->sq[index];
  619. struct napi_struct *napi = &sq->napi;
  620. /* Suppress further interrupts. */
  621. virtqueue_disable_cb(vq);
  622. if (napi->weight)
  623. virtqueue_napi_schedule(napi, vq);
  624. else
  625. virtnet_tx_wake_queue(vi, sq);
  626. }
  627. #define MRG_CTX_HEADER_SHIFT 22
  628. static void *mergeable_len_to_ctx(unsigned int truesize,
  629. unsigned int headroom)
  630. {
  631. return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
  632. }
  633. static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
  634. {
  635. return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
  636. }
  637. static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
  638. {
  639. return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
  640. }
  641. static int check_mergeable_len(struct net_device *dev, void *mrg_ctx,
  642. unsigned int len)
  643. {
  644. unsigned int headroom, tailroom, room, truesize;
  645. truesize = mergeable_ctx_to_truesize(mrg_ctx);
  646. headroom = mergeable_ctx_to_headroom(mrg_ctx);
  647. tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
  648. room = SKB_DATA_ALIGN(headroom + tailroom);
  649. if (len > truesize - room) {
  650. pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
  651. dev->name, len, (unsigned long)(truesize - room));
  652. DEV_STATS_INC(dev, rx_length_errors);
  653. return -1;
  654. }
  655. return 0;
  656. }
  657. static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
  658. unsigned int headroom,
  659. unsigned int len)
  660. {
  661. struct sk_buff *skb;
  662. skb = build_skb(buf, buflen);
  663. if (unlikely(!skb))
  664. return NULL;
  665. skb_reserve(skb, headroom);
  666. skb_put(skb, len);
  667. return skb;
  668. }
  669. /* Called from bottom half context */
  670. static struct sk_buff *page_to_skb(struct virtnet_info *vi,
  671. struct receive_queue *rq,
  672. struct page *page, unsigned int offset,
  673. unsigned int len, unsigned int truesize,
  674. unsigned int headroom)
  675. {
  676. struct sk_buff *skb;
  677. struct virtio_net_common_hdr *hdr;
  678. unsigned int copy, hdr_len, hdr_padded_len;
  679. struct page *page_to_free = NULL;
  680. int tailroom, shinfo_size;
  681. char *p, *hdr_p, *buf;
  682. p = page_address(page) + offset;
  683. hdr_p = p;
  684. hdr_len = vi->hdr_len;
  685. if (vi->mergeable_rx_bufs)
  686. hdr_padded_len = hdr_len;
  687. else
  688. hdr_padded_len = sizeof(struct padded_vnet_hdr);
  689. buf = p - headroom;
  690. len -= hdr_len;
  691. offset += hdr_padded_len;
  692. p += hdr_padded_len;
  693. tailroom = truesize - headroom - hdr_padded_len - len;
  694. shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  695. if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
  696. skb = virtnet_build_skb(buf, truesize, p - buf, len);
  697. if (unlikely(!skb))
  698. return NULL;
  699. page = (struct page *)page->private;
  700. if (page)
  701. give_pages(rq, page);
  702. goto ok;
  703. }
  704. /* copy small packet so we can reuse these pages for small data */
  705. skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
  706. if (unlikely(!skb))
  707. return NULL;
  708. /* Copy all frame if it fits skb->head, otherwise
  709. * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
  710. */
  711. if (len <= skb_tailroom(skb))
  712. copy = len;
  713. else
  714. copy = ETH_HLEN;
  715. skb_put_data(skb, p, copy);
  716. len -= copy;
  717. offset += copy;
  718. if (vi->mergeable_rx_bufs) {
  719. if (len)
  720. skb_add_rx_frag(skb, 0, page, offset, len, truesize);
  721. else
  722. page_to_free = page;
  723. goto ok;
  724. }
  725. BUG_ON(offset >= PAGE_SIZE);
  726. while (len) {
  727. unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
  728. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
  729. frag_size, truesize);
  730. len -= frag_size;
  731. page = (struct page *)page->private;
  732. offset = 0;
  733. }
  734. if (page)
  735. give_pages(rq, page);
  736. ok:
  737. hdr = skb_vnet_common_hdr(skb);
  738. memcpy(hdr, hdr_p, hdr_len);
  739. if (page_to_free)
  740. put_page(page_to_free);
  741. return skb;
  742. }
  743. static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
  744. {
  745. struct virtnet_info *vi = rq->vq->vdev->priv;
  746. struct page *page = virt_to_head_page(buf);
  747. struct virtnet_rq_dma *dma;
  748. void *head;
  749. int offset;
  750. BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
  751. head = page_address(page);
  752. dma = head;
  753. --dma->ref;
  754. if (dma->need_sync && len) {
  755. offset = buf - (head + sizeof(*dma));
  756. virtqueue_map_sync_single_range_for_cpu(rq->vq, dma->addr,
  757. offset, len,
  758. DMA_FROM_DEVICE);
  759. }
  760. if (dma->ref)
  761. return;
  762. virtqueue_unmap_single_attrs(rq->vq, dma->addr, dma->len,
  763. DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
  764. put_page(page);
  765. }
  766. static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
  767. {
  768. struct virtnet_info *vi = rq->vq->vdev->priv;
  769. void *buf;
  770. BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
  771. buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
  772. if (buf)
  773. virtnet_rq_unmap(rq, buf, *len);
  774. return buf;
  775. }
  776. static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
  777. {
  778. struct virtnet_info *vi = rq->vq->vdev->priv;
  779. struct virtnet_rq_dma *dma;
  780. dma_addr_t addr;
  781. u32 offset;
  782. void *head;
  783. BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
  784. head = page_address(rq->alloc_frag.page);
  785. offset = buf - head;
  786. dma = head;
  787. addr = dma->addr - sizeof(*dma) + offset;
  788. sg_init_table(rq->sg, 1);
  789. sg_fill_dma(rq->sg, addr, len);
  790. }
  791. static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
  792. {
  793. struct page_frag *alloc_frag = &rq->alloc_frag;
  794. struct virtnet_info *vi = rq->vq->vdev->priv;
  795. struct virtnet_rq_dma *dma;
  796. void *buf, *head;
  797. dma_addr_t addr;
  798. BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
  799. head = page_address(alloc_frag->page);
  800. dma = head;
  801. /* new pages */
  802. if (!alloc_frag->offset) {
  803. if (rq->last_dma) {
  804. /* Now, the new page is allocated, the last dma
  805. * will not be used. So the dma can be unmapped
  806. * if the ref is 0.
  807. */
  808. virtnet_rq_unmap(rq, rq->last_dma, 0);
  809. rq->last_dma = NULL;
  810. }
  811. dma->len = alloc_frag->size - sizeof(*dma);
  812. addr = virtqueue_map_single_attrs(rq->vq, dma + 1,
  813. dma->len, DMA_FROM_DEVICE, 0);
  814. if (virtqueue_map_mapping_error(rq->vq, addr))
  815. return NULL;
  816. dma->addr = addr;
  817. dma->need_sync = virtqueue_map_need_sync(rq->vq, addr);
  818. /* Add a reference to dma to prevent the entire dma from
  819. * being released during error handling. This reference
  820. * will be freed after the pages are no longer used.
  821. */
  822. get_page(alloc_frag->page);
  823. dma->ref = 1;
  824. alloc_frag->offset = sizeof(*dma);
  825. rq->last_dma = dma;
  826. }
  827. ++dma->ref;
  828. buf = head + alloc_frag->offset;
  829. get_page(alloc_frag->page);
  830. alloc_frag->offset += size;
  831. return buf;
  832. }
  833. static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
  834. {
  835. struct virtnet_info *vi = vq->vdev->priv;
  836. struct receive_queue *rq;
  837. int i = vq2rxq(vq);
  838. rq = &vi->rq[i];
  839. if (rq->xsk_pool) {
  840. xsk_buff_free((struct xdp_buff *)buf);
  841. return;
  842. }
  843. if (!vi->big_packets || vi->mergeable_rx_bufs)
  844. virtnet_rq_unmap(rq, buf, 0);
  845. virtnet_rq_free_buf(vi, rq, buf);
  846. }
  847. static void free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
  848. bool in_napi)
  849. {
  850. struct virtnet_sq_free_stats stats = {0};
  851. virtnet_free_old_xmit(sq, txq, in_napi, &stats);
  852. /* Avoid overhead when no packets have been processed
  853. * happens when called speculatively from start_xmit.
  854. */
  855. if (!stats.packets && !stats.napi_packets)
  856. return;
  857. u64_stats_update_begin(&sq->stats.syncp);
  858. u64_stats_add(&sq->stats.bytes, stats.bytes + stats.napi_bytes);
  859. u64_stats_add(&sq->stats.packets, stats.packets + stats.napi_packets);
  860. u64_stats_update_end(&sq->stats.syncp);
  861. }
  862. static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
  863. {
  864. if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
  865. return false;
  866. else if (q < vi->curr_queue_pairs)
  867. return true;
  868. else
  869. return false;
  870. }
  871. static bool tx_may_stop(struct virtnet_info *vi,
  872. struct net_device *dev,
  873. struct send_queue *sq)
  874. {
  875. int qnum;
  876. qnum = sq - vi->sq;
  877. /* If running out of space, stop queue to avoid getting packets that we
  878. * are then unable to transmit.
  879. * An alternative would be to force queuing layer to requeue the skb by
  880. * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
  881. * returned in a normal path of operation: it means that driver is not
  882. * maintaining the TX queue stop/start state properly, and causes
  883. * the stack to do a non-trivial amount of useless work.
  884. * Since most packets only take 1 or 2 ring slots, stopping the queue
  885. * early means 16 slots are typically wasted.
  886. */
  887. if (sq->vq->num_free < MAX_SKB_FRAGS + 2) {
  888. struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
  889. netif_tx_stop_queue(txq);
  890. u64_stats_update_begin(&sq->stats.syncp);
  891. u64_stats_inc(&sq->stats.stop);
  892. u64_stats_update_end(&sq->stats.syncp);
  893. return true;
  894. }
  895. return false;
  896. }
  897. static void check_sq_full_and_disable(struct virtnet_info *vi,
  898. struct net_device *dev,
  899. struct send_queue *sq)
  900. {
  901. bool use_napi = sq->napi.weight;
  902. int qnum;
  903. qnum = sq - vi->sq;
  904. if (tx_may_stop(vi, dev, sq)) {
  905. struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
  906. if (use_napi) {
  907. if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
  908. virtqueue_napi_schedule(&sq->napi, sq->vq);
  909. } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
  910. /* More just got used, free them then recheck. */
  911. free_old_xmit(sq, txq, false);
  912. if (sq->vq->num_free >= MAX_SKB_FRAGS + 2) {
  913. netif_start_subqueue(dev, qnum);
  914. u64_stats_update_begin(&sq->stats.syncp);
  915. u64_stats_inc(&sq->stats.wake);
  916. u64_stats_update_end(&sq->stats.syncp);
  917. virtqueue_disable_cb(sq->vq);
  918. }
  919. }
  920. }
  921. }
  922. /* Note that @len is the length of received data without virtio header */
  923. static struct xdp_buff *buf_to_xdp(struct virtnet_info *vi,
  924. struct receive_queue *rq, void *buf,
  925. u32 len, bool first_buf)
  926. {
  927. struct xdp_buff *xdp;
  928. u32 bufsize;
  929. xdp = (struct xdp_buff *)buf;
  930. /* In virtnet_add_recvbuf_xsk, we use part of XDP_PACKET_HEADROOM for
  931. * virtio header and ask the vhost to fill data from
  932. * hard_start + XDP_PACKET_HEADROOM - vi->hdr_len
  933. * The first buffer has virtio header so the remaining region for frame
  934. * data is
  935. * xsk_pool_get_rx_frame_size()
  936. * While other buffers than the first one do not have virtio header, so
  937. * the maximum frame data's length can be
  938. * xsk_pool_get_rx_frame_size() + vi->hdr_len
  939. */
  940. bufsize = xsk_pool_get_rx_frame_size(rq->xsk_pool);
  941. if (!first_buf)
  942. bufsize += vi->hdr_len;
  943. if (unlikely(len > bufsize)) {
  944. pr_debug("%s: rx error: len %u exceeds truesize %u\n",
  945. vi->dev->name, len, bufsize);
  946. DEV_STATS_INC(vi->dev, rx_length_errors);
  947. xsk_buff_free(xdp);
  948. return NULL;
  949. }
  950. if (first_buf) {
  951. xsk_buff_set_size(xdp, len);
  952. } else {
  953. xdp_prepare_buff(xdp, xdp->data_hard_start,
  954. XDP_PACKET_HEADROOM - vi->hdr_len, len, 1);
  955. xdp->flags = 0;
  956. }
  957. xsk_buff_dma_sync_for_cpu(xdp);
  958. return xdp;
  959. }
  960. static struct sk_buff *xsk_construct_skb(struct receive_queue *rq,
  961. struct xdp_buff *xdp)
  962. {
  963. unsigned int metasize = xdp->data - xdp->data_meta;
  964. struct sk_buff *skb;
  965. unsigned int size;
  966. size = xdp->data_end - xdp->data_hard_start;
  967. skb = napi_alloc_skb(&rq->napi, size);
  968. if (unlikely(!skb)) {
  969. xsk_buff_free(xdp);
  970. return NULL;
  971. }
  972. skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);
  973. size = xdp->data_end - xdp->data_meta;
  974. memcpy(__skb_put(skb, size), xdp->data_meta, size);
  975. if (metasize) {
  976. __skb_pull(skb, metasize);
  977. skb_metadata_set(skb, metasize);
  978. }
  979. xsk_buff_free(xdp);
  980. return skb;
  981. }
  982. static struct sk_buff *virtnet_receive_xsk_small(struct net_device *dev, struct virtnet_info *vi,
  983. struct receive_queue *rq, struct xdp_buff *xdp,
  984. unsigned int *xdp_xmit,
  985. struct virtnet_rq_stats *stats)
  986. {
  987. struct bpf_prog *prog;
  988. u32 ret;
  989. ret = XDP_PASS;
  990. rcu_read_lock();
  991. prog = rcu_dereference(rq->xdp_prog);
  992. if (prog)
  993. ret = virtnet_xdp_handler(prog, xdp, dev, xdp_xmit, stats);
  994. rcu_read_unlock();
  995. switch (ret) {
  996. case XDP_PASS:
  997. return xsk_construct_skb(rq, xdp);
  998. case XDP_TX:
  999. case XDP_REDIRECT:
  1000. return NULL;
  1001. default:
  1002. /* drop packet */
  1003. xsk_buff_free(xdp);
  1004. u64_stats_inc(&stats->drops);
  1005. return NULL;
  1006. }
  1007. }
  1008. static void xsk_drop_follow_bufs(struct net_device *dev,
  1009. struct receive_queue *rq,
  1010. u32 num_buf,
  1011. struct virtnet_rq_stats *stats)
  1012. {
  1013. struct xdp_buff *xdp;
  1014. u32 len;
  1015. while (num_buf-- > 1) {
  1016. xdp = virtqueue_get_buf(rq->vq, &len);
  1017. if (unlikely(!xdp)) {
  1018. pr_debug("%s: rx error: %d buffers missing\n",
  1019. dev->name, num_buf);
  1020. DEV_STATS_INC(dev, rx_length_errors);
  1021. break;
  1022. }
  1023. u64_stats_add(&stats->bytes, len);
  1024. xsk_buff_free(xdp);
  1025. }
  1026. }
  1027. static int xsk_append_merge_buffer(struct virtnet_info *vi,
  1028. struct receive_queue *rq,
  1029. struct sk_buff *head_skb,
  1030. u32 num_buf,
  1031. struct virtio_net_hdr_mrg_rxbuf *hdr,
  1032. struct virtnet_rq_stats *stats)
  1033. {
  1034. struct sk_buff *curr_skb;
  1035. struct xdp_buff *xdp;
  1036. u32 len, truesize;
  1037. struct page *page;
  1038. void *buf;
  1039. curr_skb = head_skb;
  1040. while (--num_buf) {
  1041. buf = virtqueue_get_buf(rq->vq, &len);
  1042. if (unlikely(!buf)) {
  1043. pr_debug("%s: rx error: %d buffers out of %d missing\n",
  1044. vi->dev->name, num_buf,
  1045. virtio16_to_cpu(vi->vdev,
  1046. hdr->num_buffers));
  1047. DEV_STATS_INC(vi->dev, rx_length_errors);
  1048. return -EINVAL;
  1049. }
  1050. u64_stats_add(&stats->bytes, len);
  1051. xdp = buf_to_xdp(vi, rq, buf, len, false);
  1052. if (!xdp)
  1053. goto err;
  1054. buf = napi_alloc_frag(len);
  1055. if (!buf) {
  1056. xsk_buff_free(xdp);
  1057. goto err;
  1058. }
  1059. memcpy(buf, xdp->data, len);
  1060. xsk_buff_free(xdp);
  1061. page = virt_to_page(buf);
  1062. truesize = len;
  1063. curr_skb = virtnet_skb_append_frag(head_skb, curr_skb, page,
  1064. buf, len, truesize);
  1065. if (!curr_skb) {
  1066. put_page(page);
  1067. goto err;
  1068. }
  1069. }
  1070. return 0;
  1071. err:
  1072. xsk_drop_follow_bufs(vi->dev, rq, num_buf, stats);
  1073. return -EINVAL;
  1074. }
  1075. static struct sk_buff *virtnet_receive_xsk_merge(struct net_device *dev, struct virtnet_info *vi,
  1076. struct receive_queue *rq, struct xdp_buff *xdp,
  1077. unsigned int *xdp_xmit,
  1078. struct virtnet_rq_stats *stats)
  1079. {
  1080. struct virtio_net_hdr_mrg_rxbuf *hdr;
  1081. struct bpf_prog *prog;
  1082. struct sk_buff *skb;
  1083. u32 ret, num_buf;
  1084. hdr = xdp->data - vi->hdr_len;
  1085. num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
  1086. ret = XDP_PASS;
  1087. rcu_read_lock();
  1088. prog = rcu_dereference(rq->xdp_prog);
  1089. if (prog) {
  1090. /* TODO: support multi buffer. */
  1091. if (num_buf == 1)
  1092. ret = virtnet_xdp_handler(prog, xdp, dev, xdp_xmit,
  1093. stats);
  1094. else
  1095. ret = XDP_ABORTED;
  1096. }
  1097. rcu_read_unlock();
  1098. switch (ret) {
  1099. case XDP_PASS:
  1100. skb = xsk_construct_skb(rq, xdp);
  1101. if (!skb)
  1102. goto drop_bufs;
  1103. if (xsk_append_merge_buffer(vi, rq, skb, num_buf, hdr, stats)) {
  1104. dev_kfree_skb(skb);
  1105. goto drop;
  1106. }
  1107. return skb;
  1108. case XDP_TX:
  1109. case XDP_REDIRECT:
  1110. return NULL;
  1111. default:
  1112. /* drop packet */
  1113. xsk_buff_free(xdp);
  1114. }
  1115. drop_bufs:
  1116. xsk_drop_follow_bufs(dev, rq, num_buf, stats);
  1117. drop:
  1118. u64_stats_inc(&stats->drops);
  1119. return NULL;
  1120. }
  1121. static void virtnet_receive_xsk_buf(struct virtnet_info *vi, struct receive_queue *rq,
  1122. void *buf, u32 len,
  1123. unsigned int *xdp_xmit,
  1124. struct virtnet_rq_stats *stats)
  1125. {
  1126. struct net_device *dev = vi->dev;
  1127. struct sk_buff *skb = NULL;
  1128. struct xdp_buff *xdp;
  1129. u8 flags;
  1130. len -= vi->hdr_len;
  1131. u64_stats_add(&stats->bytes, len);
  1132. xdp = buf_to_xdp(vi, rq, buf, len, true);
  1133. if (!xdp)
  1134. return;
  1135. if (unlikely(len < ETH_HLEN)) {
  1136. pr_debug("%s: short packet %i\n", dev->name, len);
  1137. DEV_STATS_INC(dev, rx_length_errors);
  1138. xsk_buff_free(xdp);
  1139. return;
  1140. }
  1141. flags = ((struct virtio_net_common_hdr *)(xdp->data - vi->hdr_len))->hdr.flags;
  1142. if (!vi->mergeable_rx_bufs)
  1143. skb = virtnet_receive_xsk_small(dev, vi, rq, xdp, xdp_xmit, stats);
  1144. else
  1145. skb = virtnet_receive_xsk_merge(dev, vi, rq, xdp, xdp_xmit, stats);
  1146. if (skb)
  1147. virtnet_receive_done(vi, rq, skb, flags);
  1148. }
  1149. static int virtnet_add_recvbuf_xsk(struct virtnet_info *vi, struct receive_queue *rq,
  1150. struct xsk_buff_pool *pool, gfp_t gfp)
  1151. {
  1152. struct xdp_buff **xsk_buffs;
  1153. dma_addr_t addr;
  1154. int err = 0;
  1155. u32 len, i;
  1156. int num;
  1157. xsk_buffs = rq->xsk_buffs;
  1158. num = xsk_buff_alloc_batch(pool, xsk_buffs, rq->vq->num_free);
  1159. if (!num)
  1160. return -ENOMEM;
  1161. len = xsk_pool_get_rx_frame_size(pool) + vi->hdr_len;
  1162. for (i = 0; i < num; ++i) {
  1163. /* Use the part of XDP_PACKET_HEADROOM as the virtnet hdr space.
  1164. * We assume XDP_PACKET_HEADROOM is larger than hdr->len.
  1165. * (see function virtnet_xsk_pool_enable)
  1166. */
  1167. addr = xsk_buff_xdp_get_dma(xsk_buffs[i]) - vi->hdr_len;
  1168. sg_init_table(rq->sg, 1);
  1169. sg_fill_dma(rq->sg, addr, len);
  1170. err = virtqueue_add_inbuf_premapped(rq->vq, rq->sg, 1,
  1171. xsk_buffs[i], NULL, gfp);
  1172. if (err)
  1173. goto err;
  1174. }
  1175. return num;
  1176. err:
  1177. for (; i < num; ++i)
  1178. xsk_buff_free(xsk_buffs[i]);
  1179. return err;
  1180. }
  1181. static void *virtnet_xsk_to_ptr(u32 len)
  1182. {
  1183. unsigned long p;
  1184. p = len << VIRTIO_XSK_FLAG_OFFSET;
  1185. return virtnet_xmit_ptr_pack((void *)p, VIRTNET_XMIT_TYPE_XSK);
  1186. }
  1187. static int virtnet_xsk_xmit_one(struct send_queue *sq,
  1188. struct xsk_buff_pool *pool,
  1189. struct xdp_desc *desc)
  1190. {
  1191. struct virtnet_info *vi;
  1192. dma_addr_t addr;
  1193. vi = sq->vq->vdev->priv;
  1194. addr = xsk_buff_raw_get_dma(pool, desc->addr);
  1195. xsk_buff_raw_dma_sync_for_device(pool, addr, desc->len);
  1196. sg_init_table(sq->sg, 2);
  1197. sg_fill_dma(sq->sg, sq->xsk_hdr_dma_addr, vi->hdr_len);
  1198. sg_fill_dma(sq->sg + 1, addr, desc->len);
  1199. return virtqueue_add_outbuf_premapped(sq->vq, sq->sg, 2,
  1200. virtnet_xsk_to_ptr(desc->len),
  1201. GFP_ATOMIC);
  1202. }
  1203. static int virtnet_xsk_xmit_batch(struct send_queue *sq,
  1204. struct xsk_buff_pool *pool,
  1205. unsigned int budget,
  1206. u64 *kicks)
  1207. {
  1208. struct xdp_desc *descs = pool->tx_descs;
  1209. bool kick = false;
  1210. u32 nb_pkts, i;
  1211. int err;
  1212. budget = min_t(u32, budget, sq->vq->num_free);
  1213. nb_pkts = xsk_tx_peek_release_desc_batch(pool, budget);
  1214. if (!nb_pkts)
  1215. return 0;
  1216. for (i = 0; i < nb_pkts; i++) {
  1217. err = virtnet_xsk_xmit_one(sq, pool, &descs[i]);
  1218. if (unlikely(err)) {
  1219. xsk_tx_completed(sq->xsk_pool, nb_pkts - i);
  1220. break;
  1221. }
  1222. kick = true;
  1223. }
  1224. if (kick && virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
  1225. (*kicks)++;
  1226. return i;
  1227. }
  1228. static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
  1229. int budget)
  1230. {
  1231. struct virtnet_info *vi = sq->vq->vdev->priv;
  1232. struct virtnet_sq_free_stats stats = {};
  1233. struct net_device *dev = vi->dev;
  1234. u64 kicks = 0;
  1235. int sent;
  1236. /* Avoid to wakeup napi meanless, so call __free_old_xmit instead of
  1237. * free_old_xmit().
  1238. */
  1239. __free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq), true, &stats);
  1240. if (stats.xsk)
  1241. xsk_tx_completed(sq->xsk_pool, stats.xsk);
  1242. sent = virtnet_xsk_xmit_batch(sq, pool, budget, &kicks);
  1243. if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
  1244. check_sq_full_and_disable(vi, vi->dev, sq);
  1245. if (sent) {
  1246. struct netdev_queue *txq;
  1247. txq = netdev_get_tx_queue(vi->dev, sq - vi->sq);
  1248. txq_trans_cond_update(txq);
  1249. }
  1250. u64_stats_update_begin(&sq->stats.syncp);
  1251. u64_stats_add(&sq->stats.packets, stats.packets);
  1252. u64_stats_add(&sq->stats.bytes, stats.bytes);
  1253. u64_stats_add(&sq->stats.kicks, kicks);
  1254. u64_stats_add(&sq->stats.xdp_tx, sent);
  1255. u64_stats_update_end(&sq->stats.syncp);
  1256. if (xsk_uses_need_wakeup(pool))
  1257. xsk_set_tx_need_wakeup(pool);
  1258. return sent;
  1259. }
  1260. static void xsk_wakeup(struct send_queue *sq)
  1261. {
  1262. if (napi_if_scheduled_mark_missed(&sq->napi))
  1263. return;
  1264. local_bh_disable();
  1265. virtqueue_napi_schedule(&sq->napi, sq->vq);
  1266. local_bh_enable();
  1267. }
  1268. static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
  1269. {
  1270. struct virtnet_info *vi = netdev_priv(dev);
  1271. struct send_queue *sq;
  1272. if (!netif_running(dev))
  1273. return -ENETDOWN;
  1274. if (qid >= vi->curr_queue_pairs)
  1275. return -EINVAL;
  1276. sq = &vi->sq[qid];
  1277. xsk_wakeup(sq);
  1278. return 0;
  1279. }
  1280. static void virtnet_xsk_completed(struct send_queue *sq, int num)
  1281. {
  1282. xsk_tx_completed(sq->xsk_pool, num);
  1283. /* If this is called by rx poll, start_xmit and xdp xmit we should
  1284. * wakeup the tx napi to consume the xsk tx queue, because the tx
  1285. * interrupt may not be triggered.
  1286. */
  1287. xsk_wakeup(sq);
  1288. }
  1289. static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
  1290. struct send_queue *sq,
  1291. struct xdp_frame *xdpf)
  1292. {
  1293. struct virtio_net_hdr_mrg_rxbuf *hdr;
  1294. struct skb_shared_info *shinfo;
  1295. u8 nr_frags = 0;
  1296. int err, i;
  1297. if (unlikely(xdpf->headroom < vi->hdr_len))
  1298. return -EOVERFLOW;
  1299. if (unlikely(xdp_frame_has_frags(xdpf))) {
  1300. shinfo = xdp_get_shared_info_from_frame(xdpf);
  1301. nr_frags = shinfo->nr_frags;
  1302. }
  1303. /* In wrapping function virtnet_xdp_xmit(), we need to free
  1304. * up the pending old buffers, where we need to calculate the
  1305. * position of skb_shared_info in xdp_get_frame_len() and
  1306. * xdp_return_frame(), which will involve to xdpf->data and
  1307. * xdpf->headroom. Therefore, we need to update the value of
  1308. * headroom synchronously here.
  1309. */
  1310. xdpf->headroom -= vi->hdr_len;
  1311. xdpf->data -= vi->hdr_len;
  1312. /* Zero header and leave csum up to XDP layers */
  1313. hdr = xdpf->data;
  1314. memset(hdr, 0, vi->hdr_len);
  1315. xdpf->len += vi->hdr_len;
  1316. sg_init_table(sq->sg, nr_frags + 1);
  1317. sg_set_buf(sq->sg, xdpf->data, xdpf->len);
  1318. for (i = 0; i < nr_frags; i++) {
  1319. skb_frag_t *frag = &shinfo->frags[i];
  1320. sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
  1321. skb_frag_size(frag), skb_frag_off(frag));
  1322. }
  1323. err = virtnet_add_outbuf(sq, nr_frags + 1, xdpf, VIRTNET_XMIT_TYPE_XDP);
  1324. if (unlikely(err))
  1325. return -ENOSPC; /* Caller handle free/refcnt */
  1326. return 0;
  1327. }
  1328. /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
  1329. * the current cpu, so it does not need to be locked.
  1330. *
  1331. * Here we use marco instead of inline functions because we have to deal with
  1332. * three issues at the same time: 1. the choice of sq. 2. judge and execute the
  1333. * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
  1334. * functions to perfectly solve these three problems at the same time.
  1335. */
  1336. #define virtnet_xdp_get_sq(vi) ({ \
  1337. int cpu = smp_processor_id(); \
  1338. struct netdev_queue *txq; \
  1339. typeof(vi) v = (vi); \
  1340. unsigned int qp; \
  1341. \
  1342. if (v->curr_queue_pairs > nr_cpu_ids) { \
  1343. qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
  1344. qp += cpu; \
  1345. txq = netdev_get_tx_queue(v->dev, qp); \
  1346. __netif_tx_acquire(txq); \
  1347. } else { \
  1348. qp = cpu % v->curr_queue_pairs; \
  1349. txq = netdev_get_tx_queue(v->dev, qp); \
  1350. __netif_tx_lock(txq, cpu); \
  1351. } \
  1352. v->sq + qp; \
  1353. })
  1354. #define virtnet_xdp_put_sq(vi, q) { \
  1355. struct netdev_queue *txq; \
  1356. typeof(vi) v = (vi); \
  1357. \
  1358. txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
  1359. if (v->curr_queue_pairs > nr_cpu_ids) \
  1360. __netif_tx_release(txq); \
  1361. else \
  1362. __netif_tx_unlock(txq); \
  1363. }
  1364. static int virtnet_xdp_xmit(struct net_device *dev,
  1365. int n, struct xdp_frame **frames, u32 flags)
  1366. {
  1367. struct virtnet_info *vi = netdev_priv(dev);
  1368. struct virtnet_sq_free_stats stats = {0};
  1369. struct receive_queue *rq = vi->rq;
  1370. struct bpf_prog *xdp_prog;
  1371. struct send_queue *sq;
  1372. int nxmit = 0;
  1373. int kicks = 0;
  1374. int ret;
  1375. int i;
  1376. /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
  1377. * indicate XDP resources have been successfully allocated.
  1378. */
  1379. xdp_prog = rcu_access_pointer(rq->xdp_prog);
  1380. if (!xdp_prog)
  1381. return -ENXIO;
  1382. sq = virtnet_xdp_get_sq(vi);
  1383. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
  1384. ret = -EINVAL;
  1385. goto out;
  1386. }
  1387. /* Free up any pending old buffers before queueing new ones. */
  1388. virtnet_free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq),
  1389. false, &stats);
  1390. for (i = 0; i < n; i++) {
  1391. struct xdp_frame *xdpf = frames[i];
  1392. if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
  1393. break;
  1394. nxmit++;
  1395. }
  1396. ret = nxmit;
  1397. if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
  1398. check_sq_full_and_disable(vi, dev, sq);
  1399. if (flags & XDP_XMIT_FLUSH) {
  1400. if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
  1401. kicks = 1;
  1402. }
  1403. out:
  1404. u64_stats_update_begin(&sq->stats.syncp);
  1405. u64_stats_add(&sq->stats.bytes, stats.bytes);
  1406. u64_stats_add(&sq->stats.packets, stats.packets);
  1407. u64_stats_add(&sq->stats.xdp_tx, n);
  1408. u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
  1409. u64_stats_add(&sq->stats.kicks, kicks);
  1410. u64_stats_update_end(&sq->stats.syncp);
  1411. virtnet_xdp_put_sq(vi, sq);
  1412. return ret;
  1413. }
  1414. static void put_xdp_frags(struct xdp_buff *xdp)
  1415. {
  1416. struct skb_shared_info *shinfo;
  1417. struct page *xdp_page;
  1418. int i;
  1419. if (xdp_buff_has_frags(xdp)) {
  1420. shinfo = xdp_get_shared_info_from_buff(xdp);
  1421. for (i = 0; i < shinfo->nr_frags; i++) {
  1422. xdp_page = skb_frag_page(&shinfo->frags[i]);
  1423. put_page(xdp_page);
  1424. }
  1425. }
  1426. }
  1427. static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
  1428. struct net_device *dev,
  1429. unsigned int *xdp_xmit,
  1430. struct virtnet_rq_stats *stats)
  1431. {
  1432. struct xdp_frame *xdpf;
  1433. int err;
  1434. u32 act;
  1435. act = bpf_prog_run_xdp(xdp_prog, xdp);
  1436. u64_stats_inc(&stats->xdp_packets);
  1437. switch (act) {
  1438. case XDP_PASS:
  1439. return act;
  1440. case XDP_TX:
  1441. u64_stats_inc(&stats->xdp_tx);
  1442. xdpf = xdp_convert_buff_to_frame(xdp);
  1443. if (unlikely(!xdpf)) {
  1444. netdev_dbg(dev, "convert buff to frame failed for xdp\n");
  1445. return XDP_DROP;
  1446. }
  1447. err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
  1448. if (unlikely(!err)) {
  1449. xdp_return_frame_rx_napi(xdpf);
  1450. } else if (unlikely(err < 0)) {
  1451. trace_xdp_exception(dev, xdp_prog, act);
  1452. return XDP_DROP;
  1453. }
  1454. *xdp_xmit |= VIRTIO_XDP_TX;
  1455. return act;
  1456. case XDP_REDIRECT:
  1457. u64_stats_inc(&stats->xdp_redirects);
  1458. err = xdp_do_redirect(dev, xdp, xdp_prog);
  1459. if (err)
  1460. return XDP_DROP;
  1461. *xdp_xmit |= VIRTIO_XDP_REDIR;
  1462. return act;
  1463. default:
  1464. bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
  1465. fallthrough;
  1466. case XDP_ABORTED:
  1467. trace_xdp_exception(dev, xdp_prog, act);
  1468. fallthrough;
  1469. case XDP_DROP:
  1470. return XDP_DROP;
  1471. }
  1472. }
  1473. static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
  1474. {
  1475. return vi->xdp_enabled ? XDP_PACKET_HEADROOM : 0;
  1476. }
  1477. /* We copy the packet for XDP in the following cases:
  1478. *
  1479. * 1) Packet is scattered across multiple rx buffers.
  1480. * 2) Headroom space is insufficient.
  1481. *
  1482. * This is inefficient but it's a temporary condition that
  1483. * we hit right after XDP is enabled and until queue is refilled
  1484. * with large buffers with sufficient headroom - so it should affect
  1485. * at most queue size packets.
  1486. * Afterwards, the conditions to enable
  1487. * XDP should preclude the underlying device from sending packets
  1488. * across multiple buffers (num_buf > 1), and we make sure buffers
  1489. * have enough headroom.
  1490. */
  1491. static struct page *xdp_linearize_page(struct net_device *dev,
  1492. struct receive_queue *rq,
  1493. int *num_buf,
  1494. struct page *p,
  1495. int offset,
  1496. int page_off,
  1497. unsigned int *len)
  1498. {
  1499. int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1500. struct page *page;
  1501. if (page_off + *len + tailroom > PAGE_SIZE)
  1502. return NULL;
  1503. page = alloc_page(GFP_ATOMIC);
  1504. if (!page)
  1505. return NULL;
  1506. memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
  1507. page_off += *len;
  1508. /* Only mergeable mode can go inside this while loop. In small mode,
  1509. * *num_buf == 1, so it cannot go inside.
  1510. */
  1511. while (--*num_buf) {
  1512. unsigned int buflen;
  1513. void *buf;
  1514. void *ctx;
  1515. int off;
  1516. buf = virtnet_rq_get_buf(rq, &buflen, &ctx);
  1517. if (unlikely(!buf))
  1518. goto err_buf;
  1519. p = virt_to_head_page(buf);
  1520. off = buf - page_address(p);
  1521. if (check_mergeable_len(dev, ctx, buflen)) {
  1522. put_page(p);
  1523. goto err_buf;
  1524. }
  1525. /* guard against a misconfigured or uncooperative backend that
  1526. * is sending packet larger than the MTU.
  1527. */
  1528. if ((page_off + buflen + tailroom) > PAGE_SIZE) {
  1529. put_page(p);
  1530. goto err_buf;
  1531. }
  1532. memcpy(page_address(page) + page_off,
  1533. page_address(p) + off, buflen);
  1534. page_off += buflen;
  1535. put_page(p);
  1536. }
  1537. /* Headroom does not contribute to packet length */
  1538. *len = page_off - XDP_PACKET_HEADROOM;
  1539. return page;
  1540. err_buf:
  1541. __free_pages(page, 0);
  1542. return NULL;
  1543. }
  1544. static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
  1545. unsigned int xdp_headroom,
  1546. void *buf,
  1547. unsigned int len)
  1548. {
  1549. unsigned int header_offset;
  1550. unsigned int headroom;
  1551. unsigned int buflen;
  1552. struct sk_buff *skb;
  1553. header_offset = VIRTNET_RX_PAD + xdp_headroom;
  1554. headroom = vi->hdr_len + header_offset;
  1555. buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  1556. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1557. skb = virtnet_build_skb(buf, buflen, headroom, len);
  1558. if (unlikely(!skb))
  1559. return NULL;
  1560. buf += header_offset;
  1561. memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
  1562. return skb;
  1563. }
  1564. static struct sk_buff *receive_small_xdp(struct net_device *dev,
  1565. struct virtnet_info *vi,
  1566. struct receive_queue *rq,
  1567. struct bpf_prog *xdp_prog,
  1568. void *buf,
  1569. unsigned int xdp_headroom,
  1570. unsigned int len,
  1571. unsigned int *xdp_xmit,
  1572. struct virtnet_rq_stats *stats)
  1573. {
  1574. unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
  1575. unsigned int headroom = vi->hdr_len + header_offset;
  1576. struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
  1577. struct page *page = virt_to_head_page(buf);
  1578. struct page *xdp_page;
  1579. unsigned int buflen;
  1580. struct xdp_buff xdp;
  1581. struct sk_buff *skb;
  1582. unsigned int metasize = 0;
  1583. u32 act;
  1584. if (unlikely(hdr->hdr.gso_type))
  1585. goto err_xdp;
  1586. /* Partially checksummed packets must be dropped. */
  1587. if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
  1588. goto err_xdp;
  1589. buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  1590. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1591. if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
  1592. int offset = buf - page_address(page) + header_offset;
  1593. unsigned int tlen = len + vi->hdr_len;
  1594. int num_buf = 1;
  1595. xdp_headroom = virtnet_get_headroom(vi);
  1596. header_offset = VIRTNET_RX_PAD + xdp_headroom;
  1597. headroom = vi->hdr_len + header_offset;
  1598. buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  1599. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1600. xdp_page = xdp_linearize_page(dev, rq, &num_buf, page,
  1601. offset, header_offset,
  1602. &tlen);
  1603. if (!xdp_page)
  1604. goto err_xdp;
  1605. buf = page_address(xdp_page);
  1606. put_page(page);
  1607. page = xdp_page;
  1608. }
  1609. xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
  1610. xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
  1611. xdp_headroom, len, true);
  1612. act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
  1613. switch (act) {
  1614. case XDP_PASS:
  1615. /* Recalculate length in case bpf program changed it */
  1616. len = xdp.data_end - xdp.data;
  1617. metasize = xdp.data - xdp.data_meta;
  1618. break;
  1619. case XDP_TX:
  1620. case XDP_REDIRECT:
  1621. goto xdp_xmit;
  1622. default:
  1623. goto err_xdp;
  1624. }
  1625. skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
  1626. if (unlikely(!skb))
  1627. goto err;
  1628. if (metasize)
  1629. skb_metadata_set(skb, metasize);
  1630. return skb;
  1631. err_xdp:
  1632. u64_stats_inc(&stats->xdp_drops);
  1633. err:
  1634. u64_stats_inc(&stats->drops);
  1635. put_page(page);
  1636. xdp_xmit:
  1637. return NULL;
  1638. }
  1639. static struct sk_buff *receive_small(struct net_device *dev,
  1640. struct virtnet_info *vi,
  1641. struct receive_queue *rq,
  1642. void *buf, void *ctx,
  1643. unsigned int len,
  1644. unsigned int *xdp_xmit,
  1645. struct virtnet_rq_stats *stats)
  1646. {
  1647. unsigned int xdp_headroom = (unsigned long)ctx;
  1648. struct page *page = virt_to_head_page(buf);
  1649. struct sk_buff *skb;
  1650. /* We passed the address of virtnet header to virtio-core,
  1651. * so truncate the padding.
  1652. */
  1653. buf -= VIRTNET_RX_PAD + xdp_headroom;
  1654. len -= vi->hdr_len;
  1655. u64_stats_add(&stats->bytes, len);
  1656. if (unlikely(len > GOOD_PACKET_LEN)) {
  1657. pr_debug("%s: rx error: len %u exceeds max size %d\n",
  1658. dev->name, len, GOOD_PACKET_LEN);
  1659. DEV_STATS_INC(dev, rx_length_errors);
  1660. goto err;
  1661. }
  1662. if (unlikely(vi->xdp_enabled)) {
  1663. struct bpf_prog *xdp_prog;
  1664. rcu_read_lock();
  1665. xdp_prog = rcu_dereference(rq->xdp_prog);
  1666. if (xdp_prog) {
  1667. skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf,
  1668. xdp_headroom, len, xdp_xmit,
  1669. stats);
  1670. rcu_read_unlock();
  1671. return skb;
  1672. }
  1673. rcu_read_unlock();
  1674. }
  1675. skb = receive_small_build_skb(vi, xdp_headroom, buf, len);
  1676. if (likely(skb))
  1677. return skb;
  1678. err:
  1679. u64_stats_inc(&stats->drops);
  1680. put_page(page);
  1681. return NULL;
  1682. }
  1683. static struct sk_buff *receive_big(struct net_device *dev,
  1684. struct virtnet_info *vi,
  1685. struct receive_queue *rq,
  1686. void *buf,
  1687. unsigned int len,
  1688. struct virtnet_rq_stats *stats)
  1689. {
  1690. struct page *page = buf;
  1691. struct sk_buff *skb;
  1692. /* Make sure that len does not exceed the size allocated in
  1693. * add_recvbuf_big.
  1694. */
  1695. if (unlikely(len > (vi->big_packets_num_skbfrags + 1) * PAGE_SIZE)) {
  1696. pr_debug("%s: rx error: len %u exceeds allocated size %lu\n",
  1697. dev->name, len,
  1698. (vi->big_packets_num_skbfrags + 1) * PAGE_SIZE);
  1699. goto err;
  1700. }
  1701. skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
  1702. u64_stats_add(&stats->bytes, len - vi->hdr_len);
  1703. if (unlikely(!skb))
  1704. goto err;
  1705. return skb;
  1706. err:
  1707. u64_stats_inc(&stats->drops);
  1708. give_pages(rq, page);
  1709. return NULL;
  1710. }
  1711. static void mergeable_buf_free(struct receive_queue *rq, int num_buf,
  1712. struct net_device *dev,
  1713. struct virtnet_rq_stats *stats)
  1714. {
  1715. struct page *page;
  1716. void *buf;
  1717. int len;
  1718. while (num_buf-- > 1) {
  1719. buf = virtnet_rq_get_buf(rq, &len, NULL);
  1720. if (unlikely(!buf)) {
  1721. pr_debug("%s: rx error: %d buffers missing\n",
  1722. dev->name, num_buf);
  1723. DEV_STATS_INC(dev, rx_length_errors);
  1724. break;
  1725. }
  1726. u64_stats_add(&stats->bytes, len);
  1727. page = virt_to_head_page(buf);
  1728. put_page(page);
  1729. }
  1730. }
  1731. /* Why not use xdp_build_skb_from_frame() ?
  1732. * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
  1733. * virtio-net there are 2 points that do not match its requirements:
  1734. * 1. The size of the prefilled buffer is not fixed before xdp is set.
  1735. * 2. xdp_build_skb_from_frame() does more checks that we don't need,
  1736. * like eth_type_trans() (which virtio-net does in receive_buf()).
  1737. */
  1738. static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
  1739. struct virtnet_info *vi,
  1740. struct xdp_buff *xdp,
  1741. unsigned int xdp_frags_truesz)
  1742. {
  1743. struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
  1744. unsigned int headroom, data_len;
  1745. struct sk_buff *skb;
  1746. int metasize;
  1747. u8 nr_frags;
  1748. if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
  1749. pr_debug("Error building skb as missing reserved tailroom for xdp");
  1750. return NULL;
  1751. }
  1752. if (unlikely(xdp_buff_has_frags(xdp)))
  1753. nr_frags = sinfo->nr_frags;
  1754. skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
  1755. if (unlikely(!skb))
  1756. return NULL;
  1757. headroom = xdp->data - xdp->data_hard_start;
  1758. data_len = xdp->data_end - xdp->data;
  1759. skb_reserve(skb, headroom);
  1760. __skb_put(skb, data_len);
  1761. metasize = xdp->data - xdp->data_meta;
  1762. metasize = metasize > 0 ? metasize : 0;
  1763. if (metasize)
  1764. skb_metadata_set(skb, metasize);
  1765. if (unlikely(xdp_buff_has_frags(xdp)))
  1766. xdp_update_skb_frags_info(skb, nr_frags, sinfo->xdp_frags_size,
  1767. xdp_frags_truesz,
  1768. xdp_buff_get_skb_flags(xdp));
  1769. return skb;
  1770. }
  1771. /* TODO: build xdp in big mode */
  1772. static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
  1773. struct virtnet_info *vi,
  1774. struct receive_queue *rq,
  1775. struct xdp_buff *xdp,
  1776. void *buf,
  1777. unsigned int len,
  1778. unsigned int frame_sz,
  1779. int *num_buf,
  1780. unsigned int *xdp_frags_truesize,
  1781. struct virtnet_rq_stats *stats)
  1782. {
  1783. struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
  1784. struct skb_shared_info *shinfo;
  1785. unsigned int xdp_frags_truesz = 0;
  1786. unsigned int truesize;
  1787. struct page *page;
  1788. skb_frag_t *frag;
  1789. int offset;
  1790. void *ctx;
  1791. xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
  1792. xdp_prepare_buff(xdp, buf - XDP_PACKET_HEADROOM,
  1793. XDP_PACKET_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
  1794. if (!*num_buf)
  1795. return 0;
  1796. if (*num_buf > 1) {
  1797. /* If we want to build multi-buffer xdp, we need
  1798. * to specify that the flags of xdp_buff have the
  1799. * XDP_FLAGS_HAS_FRAG bit.
  1800. */
  1801. if (!xdp_buff_has_frags(xdp))
  1802. xdp_buff_set_frags_flag(xdp);
  1803. shinfo = xdp_get_shared_info_from_buff(xdp);
  1804. shinfo->nr_frags = 0;
  1805. shinfo->xdp_frags_size = 0;
  1806. }
  1807. if (*num_buf > MAX_SKB_FRAGS + 1)
  1808. return -EINVAL;
  1809. while (--*num_buf > 0) {
  1810. buf = virtnet_rq_get_buf(rq, &len, &ctx);
  1811. if (unlikely(!buf)) {
  1812. pr_debug("%s: rx error: %d buffers out of %d missing\n",
  1813. dev->name, *num_buf,
  1814. virtio16_to_cpu(vi->vdev, hdr->num_buffers));
  1815. DEV_STATS_INC(dev, rx_length_errors);
  1816. goto err;
  1817. }
  1818. u64_stats_add(&stats->bytes, len);
  1819. page = virt_to_head_page(buf);
  1820. offset = buf - page_address(page);
  1821. if (check_mergeable_len(dev, ctx, len)) {
  1822. put_page(page);
  1823. goto err;
  1824. }
  1825. truesize = mergeable_ctx_to_truesize(ctx);
  1826. xdp_frags_truesz += truesize;
  1827. frag = &shinfo->frags[shinfo->nr_frags++];
  1828. skb_frag_fill_page_desc(frag, page, offset, len);
  1829. if (page_is_pfmemalloc(page))
  1830. xdp_buff_set_frag_pfmemalloc(xdp);
  1831. shinfo->xdp_frags_size += len;
  1832. }
  1833. *xdp_frags_truesize = xdp_frags_truesz;
  1834. return 0;
  1835. err:
  1836. put_xdp_frags(xdp);
  1837. return -EINVAL;
  1838. }
  1839. static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
  1840. struct receive_queue *rq,
  1841. struct bpf_prog *xdp_prog,
  1842. void *ctx,
  1843. unsigned int *frame_sz,
  1844. int *num_buf,
  1845. struct page **page,
  1846. int offset,
  1847. unsigned int *len,
  1848. struct virtio_net_hdr_mrg_rxbuf *hdr)
  1849. {
  1850. unsigned int truesize = mergeable_ctx_to_truesize(ctx);
  1851. unsigned int headroom = mergeable_ctx_to_headroom(ctx);
  1852. struct page *xdp_page;
  1853. unsigned int xdp_room;
  1854. /* Transient failure which in theory could occur if
  1855. * in-flight packets from before XDP was enabled reach
  1856. * the receive path after XDP is loaded.
  1857. */
  1858. if (unlikely(hdr->hdr.gso_type))
  1859. return NULL;
  1860. /* Partially checksummed packets must be dropped. */
  1861. if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
  1862. return NULL;
  1863. /* Now XDP core assumes frag size is PAGE_SIZE, but buffers
  1864. * with headroom may add hole in truesize, which
  1865. * make their length exceed PAGE_SIZE. So we disabled the
  1866. * hole mechanism for xdp. See add_recvbuf_mergeable().
  1867. */
  1868. *frame_sz = truesize;
  1869. if (likely(headroom >= virtnet_get_headroom(vi) &&
  1870. (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
  1871. return page_address(*page) + offset;
  1872. }
  1873. /* This happens when headroom is not enough because
  1874. * of the buffer was prefilled before XDP is set.
  1875. * This should only happen for the first several packets.
  1876. * In fact, vq reset can be used here to help us clean up
  1877. * the prefilled buffers, but many existing devices do not
  1878. * support it, and we don't want to bother users who are
  1879. * using xdp normally.
  1880. */
  1881. if (!xdp_prog->aux->xdp_has_frags) {
  1882. /* linearize data for XDP */
  1883. xdp_page = xdp_linearize_page(vi->dev, rq, num_buf,
  1884. *page, offset,
  1885. XDP_PACKET_HEADROOM,
  1886. len);
  1887. if (!xdp_page)
  1888. return NULL;
  1889. } else {
  1890. xdp_room = SKB_DATA_ALIGN(XDP_PACKET_HEADROOM +
  1891. sizeof(struct skb_shared_info));
  1892. if (*len + xdp_room > PAGE_SIZE)
  1893. return NULL;
  1894. xdp_page = alloc_page(GFP_ATOMIC);
  1895. if (!xdp_page)
  1896. return NULL;
  1897. memcpy(page_address(xdp_page) + XDP_PACKET_HEADROOM,
  1898. page_address(*page) + offset, *len);
  1899. }
  1900. *frame_sz = PAGE_SIZE;
  1901. put_page(*page);
  1902. *page = xdp_page;
  1903. return page_address(*page) + XDP_PACKET_HEADROOM;
  1904. }
  1905. static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
  1906. struct virtnet_info *vi,
  1907. struct receive_queue *rq,
  1908. struct bpf_prog *xdp_prog,
  1909. void *buf,
  1910. void *ctx,
  1911. unsigned int len,
  1912. unsigned int *xdp_xmit,
  1913. struct virtnet_rq_stats *stats)
  1914. {
  1915. struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
  1916. int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
  1917. struct page *page = virt_to_head_page(buf);
  1918. int offset = buf - page_address(page);
  1919. unsigned int xdp_frags_truesz = 0;
  1920. struct sk_buff *head_skb;
  1921. unsigned int frame_sz;
  1922. struct xdp_buff xdp;
  1923. void *data;
  1924. u32 act;
  1925. int err;
  1926. data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page,
  1927. offset, &len, hdr);
  1928. if (unlikely(!data))
  1929. goto err_xdp;
  1930. err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
  1931. &num_buf, &xdp_frags_truesz, stats);
  1932. if (unlikely(err))
  1933. goto err_xdp;
  1934. act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
  1935. switch (act) {
  1936. case XDP_PASS:
  1937. head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
  1938. if (unlikely(!head_skb))
  1939. break;
  1940. return head_skb;
  1941. case XDP_TX:
  1942. case XDP_REDIRECT:
  1943. return NULL;
  1944. default:
  1945. break;
  1946. }
  1947. put_xdp_frags(&xdp);
  1948. err_xdp:
  1949. put_page(page);
  1950. mergeable_buf_free(rq, num_buf, dev, stats);
  1951. u64_stats_inc(&stats->xdp_drops);
  1952. u64_stats_inc(&stats->drops);
  1953. return NULL;
  1954. }
  1955. static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
  1956. struct sk_buff *curr_skb,
  1957. struct page *page, void *buf,
  1958. int len, int truesize)
  1959. {
  1960. int num_skb_frags;
  1961. int offset;
  1962. num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
  1963. if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
  1964. struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
  1965. if (unlikely(!nskb))
  1966. return NULL;
  1967. if (curr_skb == head_skb)
  1968. skb_shinfo(curr_skb)->frag_list = nskb;
  1969. else
  1970. curr_skb->next = nskb;
  1971. curr_skb = nskb;
  1972. head_skb->truesize += nskb->truesize;
  1973. num_skb_frags = 0;
  1974. }
  1975. if (curr_skb != head_skb) {
  1976. head_skb->data_len += len;
  1977. head_skb->len += len;
  1978. head_skb->truesize += truesize;
  1979. }
  1980. offset = buf - page_address(page);
  1981. if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
  1982. put_page(page);
  1983. skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
  1984. len, truesize);
  1985. } else {
  1986. skb_add_rx_frag(curr_skb, num_skb_frags, page,
  1987. offset, len, truesize);
  1988. }
  1989. return curr_skb;
  1990. }
  1991. static struct sk_buff *receive_mergeable(struct net_device *dev,
  1992. struct virtnet_info *vi,
  1993. struct receive_queue *rq,
  1994. void *buf,
  1995. void *ctx,
  1996. unsigned int len,
  1997. unsigned int *xdp_xmit,
  1998. struct virtnet_rq_stats *stats)
  1999. {
  2000. struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
  2001. int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
  2002. struct page *page = virt_to_head_page(buf);
  2003. int offset = buf - page_address(page);
  2004. struct sk_buff *head_skb, *curr_skb;
  2005. unsigned int truesize = mergeable_ctx_to_truesize(ctx);
  2006. unsigned int headroom = mergeable_ctx_to_headroom(ctx);
  2007. head_skb = NULL;
  2008. u64_stats_add(&stats->bytes, len - vi->hdr_len);
  2009. if (check_mergeable_len(dev, ctx, len))
  2010. goto err_skb;
  2011. if (unlikely(vi->xdp_enabled)) {
  2012. struct bpf_prog *xdp_prog;
  2013. rcu_read_lock();
  2014. xdp_prog = rcu_dereference(rq->xdp_prog);
  2015. if (xdp_prog) {
  2016. head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx,
  2017. len, xdp_xmit, stats);
  2018. rcu_read_unlock();
  2019. return head_skb;
  2020. }
  2021. rcu_read_unlock();
  2022. }
  2023. head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
  2024. curr_skb = head_skb;
  2025. if (unlikely(!curr_skb))
  2026. goto err_skb;
  2027. while (--num_buf) {
  2028. buf = virtnet_rq_get_buf(rq, &len, &ctx);
  2029. if (unlikely(!buf)) {
  2030. pr_debug("%s: rx error: %d buffers out of %d missing\n",
  2031. dev->name, num_buf,
  2032. virtio16_to_cpu(vi->vdev,
  2033. hdr->num_buffers));
  2034. DEV_STATS_INC(dev, rx_length_errors);
  2035. goto err_buf;
  2036. }
  2037. u64_stats_add(&stats->bytes, len);
  2038. page = virt_to_head_page(buf);
  2039. if (check_mergeable_len(dev, ctx, len))
  2040. goto err_skb;
  2041. truesize = mergeable_ctx_to_truesize(ctx);
  2042. curr_skb = virtnet_skb_append_frag(head_skb, curr_skb, page,
  2043. buf, len, truesize);
  2044. if (!curr_skb)
  2045. goto err_skb;
  2046. }
  2047. ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
  2048. return head_skb;
  2049. err_skb:
  2050. put_page(page);
  2051. mergeable_buf_free(rq, num_buf, dev, stats);
  2052. err_buf:
  2053. u64_stats_inc(&stats->drops);
  2054. dev_kfree_skb(head_skb);
  2055. return NULL;
  2056. }
  2057. static inline u32
  2058. virtio_net_hash_value(const struct virtio_net_hdr_v1_hash *hdr_hash)
  2059. {
  2060. return __le16_to_cpu(hdr_hash->hash_value_lo) |
  2061. (__le16_to_cpu(hdr_hash->hash_value_hi) << 16);
  2062. }
  2063. static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
  2064. struct sk_buff *skb)
  2065. {
  2066. enum pkt_hash_types rss_hash_type;
  2067. if (!hdr_hash || !skb)
  2068. return;
  2069. switch (__le16_to_cpu(hdr_hash->hash_report)) {
  2070. case VIRTIO_NET_HASH_REPORT_TCPv4:
  2071. case VIRTIO_NET_HASH_REPORT_UDPv4:
  2072. case VIRTIO_NET_HASH_REPORT_TCPv6:
  2073. case VIRTIO_NET_HASH_REPORT_UDPv6:
  2074. case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
  2075. case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
  2076. rss_hash_type = PKT_HASH_TYPE_L4;
  2077. break;
  2078. case VIRTIO_NET_HASH_REPORT_IPv4:
  2079. case VIRTIO_NET_HASH_REPORT_IPv6:
  2080. case VIRTIO_NET_HASH_REPORT_IPv6_EX:
  2081. rss_hash_type = PKT_HASH_TYPE_L3;
  2082. break;
  2083. case VIRTIO_NET_HASH_REPORT_NONE:
  2084. default:
  2085. rss_hash_type = PKT_HASH_TYPE_NONE;
  2086. }
  2087. skb_set_hash(skb, virtio_net_hash_value(hdr_hash), rss_hash_type);
  2088. }
  2089. static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
  2090. struct sk_buff *skb, u8 flags)
  2091. {
  2092. struct virtio_net_common_hdr *hdr;
  2093. struct net_device *dev = vi->dev;
  2094. hdr = skb_vnet_common_hdr(skb);
  2095. if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
  2096. virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
  2097. hdr->hdr.flags = flags;
  2098. if (virtio_net_handle_csum_offload(skb, &hdr->hdr, vi->rx_tnl_csum)) {
  2099. net_warn_ratelimited("%s: bad csum: flags: %x, gso_type: %x rx_tnl_csum %d\n",
  2100. dev->name, hdr->hdr.flags,
  2101. hdr->hdr.gso_type, vi->rx_tnl_csum);
  2102. goto frame_err;
  2103. }
  2104. if (virtio_net_hdr_tnl_to_skb(skb, &hdr->tnl_hdr, vi->rx_tnl,
  2105. vi->rx_tnl_csum,
  2106. virtio_is_little_endian(vi->vdev))) {
  2107. net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n",
  2108. dev->name, hdr->hdr.gso_type,
  2109. hdr->hdr.gso_size, hdr->hdr.flags,
  2110. vi->rx_tnl, vi->rx_tnl_csum);
  2111. goto frame_err;
  2112. }
  2113. skb_record_rx_queue(skb, vq2rxq(rq->vq));
  2114. skb->protocol = eth_type_trans(skb, dev);
  2115. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  2116. ntohs(skb->protocol), skb->len, skb->pkt_type);
  2117. napi_gro_receive(&rq->napi, skb);
  2118. return;
  2119. frame_err:
  2120. DEV_STATS_INC(dev, rx_frame_errors);
  2121. dev_kfree_skb(skb);
  2122. }
  2123. static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
  2124. void *buf, unsigned int len, void **ctx,
  2125. unsigned int *xdp_xmit,
  2126. struct virtnet_rq_stats *stats)
  2127. {
  2128. struct net_device *dev = vi->dev;
  2129. struct sk_buff *skb;
  2130. u8 flags;
  2131. if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
  2132. pr_debug("%s: short packet %i\n", dev->name, len);
  2133. DEV_STATS_INC(dev, rx_length_errors);
  2134. virtnet_rq_free_buf(vi, rq, buf);
  2135. return;
  2136. }
  2137. /* About the flags below:
  2138. * 1. Save the flags early, as the XDP program might overwrite them.
  2139. * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
  2140. * stay valid after XDP processing.
  2141. * 2. XDP doesn't work with partially checksummed packets (refer to
  2142. * virtnet_xdp_set()), so packets marked as
  2143. * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
  2144. */
  2145. if (vi->mergeable_rx_bufs) {
  2146. flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
  2147. skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
  2148. stats);
  2149. } else if (vi->big_packets) {
  2150. void *p = page_address((struct page *)buf);
  2151. flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
  2152. skb = receive_big(dev, vi, rq, buf, len, stats);
  2153. } else {
  2154. flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
  2155. skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
  2156. }
  2157. if (unlikely(!skb))
  2158. return;
  2159. virtnet_receive_done(vi, rq, skb, flags);
  2160. }
  2161. /* Unlike mergeable buffers, all buffers are allocated to the
  2162. * same size, except for the headroom. For this reason we do
  2163. * not need to use mergeable_len_to_ctx here - it is enough
  2164. * to store the headroom as the context ignoring the truesize.
  2165. */
  2166. static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
  2167. gfp_t gfp)
  2168. {
  2169. char *buf;
  2170. unsigned int xdp_headroom = virtnet_get_headroom(vi);
  2171. void *ctx = (void *)(unsigned long)xdp_headroom;
  2172. int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
  2173. int err;
  2174. len = SKB_DATA_ALIGN(len) +
  2175. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2176. if (unlikely(!skb_page_frag_refill(len, &rq->alloc_frag, gfp)))
  2177. return -ENOMEM;
  2178. buf = virtnet_rq_alloc(rq, len, gfp);
  2179. if (unlikely(!buf))
  2180. return -ENOMEM;
  2181. buf += VIRTNET_RX_PAD + xdp_headroom;
  2182. virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
  2183. err = virtqueue_add_inbuf_premapped(rq->vq, rq->sg, 1, buf, ctx, gfp);
  2184. if (err < 0) {
  2185. virtnet_rq_unmap(rq, buf, 0);
  2186. put_page(virt_to_head_page(buf));
  2187. }
  2188. return err;
  2189. }
  2190. static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
  2191. gfp_t gfp)
  2192. {
  2193. struct page *first, *list = NULL;
  2194. char *p;
  2195. int i, err, offset;
  2196. sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
  2197. /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
  2198. for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
  2199. first = get_a_page(rq, gfp);
  2200. if (!first) {
  2201. if (list)
  2202. give_pages(rq, list);
  2203. return -ENOMEM;
  2204. }
  2205. sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
  2206. /* chain new page in list head to match sg */
  2207. first->private = (unsigned long)list;
  2208. list = first;
  2209. }
  2210. first = get_a_page(rq, gfp);
  2211. if (!first) {
  2212. give_pages(rq, list);
  2213. return -ENOMEM;
  2214. }
  2215. p = page_address(first);
  2216. /* rq->sg[0], rq->sg[1] share the same page */
  2217. /* a separated rq->sg[0] for header - required in case !any_header_sg */
  2218. sg_set_buf(&rq->sg[0], p, vi->hdr_len);
  2219. /* rq->sg[1] for data packet, from offset */
  2220. offset = sizeof(struct padded_vnet_hdr);
  2221. sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
  2222. /* chain first in list head */
  2223. first->private = (unsigned long)list;
  2224. err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
  2225. first, gfp);
  2226. if (err < 0)
  2227. give_pages(rq, first);
  2228. return err;
  2229. }
  2230. static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
  2231. struct ewma_pkt_len *avg_pkt_len,
  2232. unsigned int room)
  2233. {
  2234. struct virtnet_info *vi = rq->vq->vdev->priv;
  2235. const size_t hdr_len = vi->hdr_len;
  2236. unsigned int len;
  2237. if (room)
  2238. return PAGE_SIZE - room;
  2239. len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
  2240. rq->min_buf_len, PAGE_SIZE - hdr_len);
  2241. return ALIGN(len, L1_CACHE_BYTES);
  2242. }
  2243. static int add_recvbuf_mergeable(struct virtnet_info *vi,
  2244. struct receive_queue *rq, gfp_t gfp)
  2245. {
  2246. struct page_frag *alloc_frag = &rq->alloc_frag;
  2247. unsigned int headroom = virtnet_get_headroom(vi);
  2248. unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
  2249. unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
  2250. unsigned int len, hole;
  2251. void *ctx;
  2252. char *buf;
  2253. int err;
  2254. /* Extra tailroom is needed to satisfy XDP's assumption. This
  2255. * means rx frags coalescing won't work, but consider we've
  2256. * disabled GSO for XDP, it won't be a big issue.
  2257. */
  2258. len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
  2259. if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
  2260. return -ENOMEM;
  2261. if (!alloc_frag->offset && len + room + sizeof(struct virtnet_rq_dma) > alloc_frag->size)
  2262. len -= sizeof(struct virtnet_rq_dma);
  2263. buf = virtnet_rq_alloc(rq, len + room, gfp);
  2264. if (unlikely(!buf))
  2265. return -ENOMEM;
  2266. buf += headroom; /* advance address leaving hole at front of pkt */
  2267. hole = alloc_frag->size - alloc_frag->offset;
  2268. if (hole < len + room) {
  2269. /* To avoid internal fragmentation, if there is very likely not
  2270. * enough space for another buffer, add the remaining space to
  2271. * the current buffer.
  2272. * XDP core assumes that frame_size of xdp_buff and the length
  2273. * of the frag are PAGE_SIZE, so we disable the hole mechanism.
  2274. */
  2275. if (!headroom)
  2276. len += hole;
  2277. alloc_frag->offset += hole;
  2278. }
  2279. virtnet_rq_init_one_sg(rq, buf, len);
  2280. ctx = mergeable_len_to_ctx(len + room, headroom);
  2281. err = virtqueue_add_inbuf_premapped(rq->vq, rq->sg, 1, buf, ctx, gfp);
  2282. if (err < 0) {
  2283. virtnet_rq_unmap(rq, buf, 0);
  2284. put_page(virt_to_head_page(buf));
  2285. }
  2286. return err;
  2287. }
  2288. /*
  2289. * Returns false if we couldn't fill entirely (OOM).
  2290. *
  2291. * Normally run in the receive path, but can also be run from ndo_open
  2292. * before we're receiving packets, or from refill_work which is
  2293. * careful to disable receiving (using napi_disable).
  2294. */
  2295. static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
  2296. gfp_t gfp)
  2297. {
  2298. int err;
  2299. if (rq->xsk_pool) {
  2300. err = virtnet_add_recvbuf_xsk(vi, rq, rq->xsk_pool, gfp);
  2301. goto kick;
  2302. }
  2303. do {
  2304. if (vi->mergeable_rx_bufs)
  2305. err = add_recvbuf_mergeable(vi, rq, gfp);
  2306. else if (vi->big_packets)
  2307. err = add_recvbuf_big(vi, rq, gfp);
  2308. else
  2309. err = add_recvbuf_small(vi, rq, gfp);
  2310. if (err)
  2311. break;
  2312. } while (rq->vq->num_free);
  2313. kick:
  2314. if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
  2315. unsigned long flags;
  2316. flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
  2317. u64_stats_inc(&rq->stats.kicks);
  2318. u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
  2319. }
  2320. return err != -ENOMEM;
  2321. }
  2322. static void skb_recv_done(struct virtqueue *rvq)
  2323. {
  2324. struct virtnet_info *vi = rvq->vdev->priv;
  2325. struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
  2326. rq->calls++;
  2327. virtqueue_napi_schedule(&rq->napi, rvq);
  2328. }
  2329. static void virtnet_napi_do_enable(struct virtqueue *vq,
  2330. struct napi_struct *napi)
  2331. {
  2332. napi_enable(napi);
  2333. /* If all buffers were filled by other side before we napi_enabled, we
  2334. * won't get another interrupt, so process any outstanding packets now.
  2335. * Call local_bh_enable after to trigger softIRQ processing.
  2336. */
  2337. local_bh_disable();
  2338. virtqueue_napi_schedule(napi, vq);
  2339. local_bh_enable();
  2340. }
  2341. static void virtnet_napi_enable(struct receive_queue *rq)
  2342. {
  2343. struct virtnet_info *vi = rq->vq->vdev->priv;
  2344. int qidx = vq2rxq(rq->vq);
  2345. virtnet_napi_do_enable(rq->vq, &rq->napi);
  2346. netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_RX, &rq->napi);
  2347. }
  2348. static void virtnet_napi_tx_enable(struct send_queue *sq)
  2349. {
  2350. struct virtnet_info *vi = sq->vq->vdev->priv;
  2351. struct napi_struct *napi = &sq->napi;
  2352. int qidx = vq2txq(sq->vq);
  2353. if (!napi->weight)
  2354. return;
  2355. /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
  2356. * enable the feature if this is likely affine with the transmit path.
  2357. */
  2358. if (!vi->affinity_hint_set) {
  2359. napi->weight = 0;
  2360. return;
  2361. }
  2362. virtnet_napi_do_enable(sq->vq, napi);
  2363. netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_TX, napi);
  2364. }
  2365. static void virtnet_napi_tx_disable(struct send_queue *sq)
  2366. {
  2367. struct virtnet_info *vi = sq->vq->vdev->priv;
  2368. struct napi_struct *napi = &sq->napi;
  2369. int qidx = vq2txq(sq->vq);
  2370. if (napi->weight) {
  2371. netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_TX, NULL);
  2372. napi_disable(napi);
  2373. }
  2374. }
  2375. static void virtnet_napi_disable(struct receive_queue *rq)
  2376. {
  2377. struct virtnet_info *vi = rq->vq->vdev->priv;
  2378. struct napi_struct *napi = &rq->napi;
  2379. int qidx = vq2rxq(rq->vq);
  2380. netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_RX, NULL);
  2381. napi_disable(napi);
  2382. }
  2383. static int virtnet_receive_xsk_bufs(struct virtnet_info *vi,
  2384. struct receive_queue *rq,
  2385. int budget,
  2386. unsigned int *xdp_xmit,
  2387. struct virtnet_rq_stats *stats)
  2388. {
  2389. unsigned int len;
  2390. int packets = 0;
  2391. void *buf;
  2392. while (packets < budget) {
  2393. buf = virtqueue_get_buf(rq->vq, &len);
  2394. if (!buf)
  2395. break;
  2396. virtnet_receive_xsk_buf(vi, rq, buf, len, xdp_xmit, stats);
  2397. packets++;
  2398. }
  2399. return packets;
  2400. }
  2401. static int virtnet_receive_packets(struct virtnet_info *vi,
  2402. struct receive_queue *rq,
  2403. int budget,
  2404. unsigned int *xdp_xmit,
  2405. struct virtnet_rq_stats *stats)
  2406. {
  2407. unsigned int len;
  2408. int packets = 0;
  2409. void *buf;
  2410. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  2411. void *ctx;
  2412. while (packets < budget &&
  2413. (buf = virtnet_rq_get_buf(rq, &len, &ctx))) {
  2414. receive_buf(vi, rq, buf, len, ctx, xdp_xmit, stats);
  2415. packets++;
  2416. }
  2417. } else {
  2418. while (packets < budget &&
  2419. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  2420. receive_buf(vi, rq, buf, len, NULL, xdp_xmit, stats);
  2421. packets++;
  2422. }
  2423. }
  2424. return packets;
  2425. }
  2426. static int virtnet_receive(struct receive_queue *rq, int budget,
  2427. unsigned int *xdp_xmit)
  2428. {
  2429. struct virtnet_info *vi = rq->vq->vdev->priv;
  2430. struct virtnet_rq_stats stats = {};
  2431. int i, packets;
  2432. if (rq->xsk_pool)
  2433. packets = virtnet_receive_xsk_bufs(vi, rq, budget, xdp_xmit, &stats);
  2434. else
  2435. packets = virtnet_receive_packets(vi, rq, budget, xdp_xmit, &stats);
  2436. u64_stats_set(&stats.packets, packets);
  2437. if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
  2438. if (!try_fill_recv(vi, rq, GFP_ATOMIC))
  2439. /* We need to retry refilling in the next NAPI poll so
  2440. * we must return budget to make sure the NAPI is
  2441. * repolled.
  2442. */
  2443. packets = budget;
  2444. }
  2445. u64_stats_update_begin(&rq->stats.syncp);
  2446. for (i = 0; i < ARRAY_SIZE(virtnet_rq_stats_desc); i++) {
  2447. size_t offset = virtnet_rq_stats_desc[i].offset;
  2448. u64_stats_t *item, *src;
  2449. item = (u64_stats_t *)((u8 *)&rq->stats + offset);
  2450. src = (u64_stats_t *)((u8 *)&stats + offset);
  2451. u64_stats_add(item, u64_stats_read(src));
  2452. }
  2453. u64_stats_add(&rq->stats.packets, u64_stats_read(&stats.packets));
  2454. u64_stats_add(&rq->stats.bytes, u64_stats_read(&stats.bytes));
  2455. u64_stats_update_end(&rq->stats.syncp);
  2456. return packets;
  2457. }
  2458. static void virtnet_poll_cleantx(struct receive_queue *rq, int budget)
  2459. {
  2460. struct virtnet_info *vi = rq->vq->vdev->priv;
  2461. unsigned int index = vq2rxq(rq->vq);
  2462. struct send_queue *sq = &vi->sq[index];
  2463. struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
  2464. if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
  2465. return;
  2466. if (__netif_tx_trylock(txq)) {
  2467. if (sq->reset) {
  2468. __netif_tx_unlock(txq);
  2469. return;
  2470. }
  2471. do {
  2472. virtqueue_disable_cb(sq->vq);
  2473. free_old_xmit(sq, txq, !!budget);
  2474. } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
  2475. if (sq->vq->num_free >= MAX_SKB_FRAGS + 2)
  2476. virtnet_tx_wake_queue(vi, sq);
  2477. __netif_tx_unlock(txq);
  2478. }
  2479. }
  2480. static void virtnet_rx_dim_update(struct virtnet_info *vi, struct receive_queue *rq)
  2481. {
  2482. struct dim_sample cur_sample = {};
  2483. if (!rq->packets_in_napi)
  2484. return;
  2485. /* Don't need protection when fetching stats, since fetcher and
  2486. * updater of the stats are in same context
  2487. */
  2488. dim_update_sample(rq->calls,
  2489. u64_stats_read(&rq->stats.packets),
  2490. u64_stats_read(&rq->stats.bytes),
  2491. &cur_sample);
  2492. net_dim(&rq->dim, &cur_sample);
  2493. rq->packets_in_napi = 0;
  2494. }
  2495. static int virtnet_poll(struct napi_struct *napi, int budget)
  2496. {
  2497. struct receive_queue *rq =
  2498. container_of(napi, struct receive_queue, napi);
  2499. struct virtnet_info *vi = rq->vq->vdev->priv;
  2500. struct send_queue *sq;
  2501. unsigned int received;
  2502. unsigned int xdp_xmit = 0;
  2503. bool napi_complete;
  2504. virtnet_poll_cleantx(rq, budget);
  2505. received = virtnet_receive(rq, budget, &xdp_xmit);
  2506. rq->packets_in_napi += received;
  2507. if (xdp_xmit & VIRTIO_XDP_REDIR)
  2508. xdp_do_flush();
  2509. /* Out of packets? */
  2510. if (received < budget) {
  2511. napi_complete = virtqueue_napi_complete(napi, rq->vq, received);
  2512. /* Intentionally not taking dim_lock here. This may result in a
  2513. * spurious net_dim call. But if that happens virtnet_rx_dim_work
  2514. * will not act on the scheduled work.
  2515. */
  2516. if (napi_complete && rq->dim_enabled)
  2517. virtnet_rx_dim_update(vi, rq);
  2518. }
  2519. if (xdp_xmit & VIRTIO_XDP_TX) {
  2520. sq = virtnet_xdp_get_sq(vi);
  2521. if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
  2522. u64_stats_update_begin(&sq->stats.syncp);
  2523. u64_stats_inc(&sq->stats.kicks);
  2524. u64_stats_update_end(&sq->stats.syncp);
  2525. }
  2526. virtnet_xdp_put_sq(vi, sq);
  2527. }
  2528. return received;
  2529. }
  2530. static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
  2531. {
  2532. virtnet_napi_tx_disable(&vi->sq[qp_index]);
  2533. virtnet_napi_disable(&vi->rq[qp_index]);
  2534. xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
  2535. }
  2536. static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
  2537. {
  2538. struct net_device *dev = vi->dev;
  2539. int err;
  2540. err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
  2541. vi->rq[qp_index].napi.napi_id);
  2542. if (err < 0)
  2543. return err;
  2544. err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
  2545. MEM_TYPE_PAGE_SHARED, NULL);
  2546. if (err < 0)
  2547. goto err_xdp_reg_mem_model;
  2548. virtnet_napi_enable(&vi->rq[qp_index]);
  2549. virtnet_napi_tx_enable(&vi->sq[qp_index]);
  2550. return 0;
  2551. err_xdp_reg_mem_model:
  2552. xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
  2553. return err;
  2554. }
  2555. static void virtnet_cancel_dim(struct virtnet_info *vi, struct dim *dim)
  2556. {
  2557. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
  2558. return;
  2559. net_dim_work_cancel(dim);
  2560. }
  2561. static void virtnet_update_settings(struct virtnet_info *vi)
  2562. {
  2563. u32 speed;
  2564. u8 duplex;
  2565. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
  2566. return;
  2567. virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
  2568. if (ethtool_validate_speed(speed))
  2569. vi->speed = speed;
  2570. virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
  2571. if (ethtool_validate_duplex(duplex))
  2572. vi->duplex = duplex;
  2573. }
  2574. static int virtnet_open(struct net_device *dev)
  2575. {
  2576. struct virtnet_info *vi = netdev_priv(dev);
  2577. int i, err;
  2578. for (i = 0; i < vi->max_queue_pairs; i++) {
  2579. if (i < vi->curr_queue_pairs)
  2580. /* Pre-fill rq agressively, to make sure we are ready to
  2581. * get packets immediately.
  2582. */
  2583. try_fill_recv(vi, &vi->rq[i], GFP_KERNEL);
  2584. err = virtnet_enable_queue_pair(vi, i);
  2585. if (err < 0)
  2586. goto err_enable_qp;
  2587. }
  2588. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  2589. if (vi->status & VIRTIO_NET_S_LINK_UP)
  2590. netif_carrier_on(vi->dev);
  2591. virtio_config_driver_enable(vi->vdev);
  2592. } else {
  2593. vi->status = VIRTIO_NET_S_LINK_UP;
  2594. netif_carrier_on(dev);
  2595. }
  2596. return 0;
  2597. err_enable_qp:
  2598. for (i--; i >= 0; i--) {
  2599. virtnet_disable_queue_pair(vi, i);
  2600. virtnet_cancel_dim(vi, &vi->rq[i].dim);
  2601. }
  2602. return err;
  2603. }
  2604. static int virtnet_poll_tx(struct napi_struct *napi, int budget)
  2605. {
  2606. struct send_queue *sq = container_of(napi, struct send_queue, napi);
  2607. struct virtnet_info *vi = sq->vq->vdev->priv;
  2608. unsigned int index = vq2txq(sq->vq);
  2609. struct netdev_queue *txq;
  2610. int opaque, xsk_done = 0;
  2611. bool done;
  2612. if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
  2613. /* We don't need to enable cb for XDP */
  2614. napi_complete_done(napi, 0);
  2615. return 0;
  2616. }
  2617. txq = netdev_get_tx_queue(vi->dev, index);
  2618. __netif_tx_lock(txq, raw_smp_processor_id());
  2619. virtqueue_disable_cb(sq->vq);
  2620. if (sq->xsk_pool)
  2621. xsk_done = virtnet_xsk_xmit(sq, sq->xsk_pool, budget);
  2622. else
  2623. free_old_xmit(sq, txq, !!budget);
  2624. if (sq->vq->num_free >= MAX_SKB_FRAGS + 2)
  2625. virtnet_tx_wake_queue(vi, sq);
  2626. if (xsk_done >= budget) {
  2627. __netif_tx_unlock(txq);
  2628. return budget;
  2629. }
  2630. opaque = virtqueue_enable_cb_prepare(sq->vq);
  2631. done = napi_complete_done(napi, 0);
  2632. if (!done)
  2633. virtqueue_disable_cb(sq->vq);
  2634. __netif_tx_unlock(txq);
  2635. if (done) {
  2636. if (unlikely(virtqueue_poll(sq->vq, opaque))) {
  2637. if (napi_schedule_prep(napi)) {
  2638. __netif_tx_lock(txq, raw_smp_processor_id());
  2639. virtqueue_disable_cb(sq->vq);
  2640. __netif_tx_unlock(txq);
  2641. __napi_schedule(napi);
  2642. }
  2643. }
  2644. }
  2645. return 0;
  2646. }
  2647. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
  2648. {
  2649. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  2650. struct virtnet_info *vi = sq->vq->vdev->priv;
  2651. struct virtio_net_hdr_v1_hash_tunnel *hdr;
  2652. int num_sg;
  2653. unsigned hdr_len = vi->hdr_len;
  2654. bool feature_hdrlen;
  2655. bool can_push;
  2656. feature_hdrlen = virtio_has_feature(vi->vdev,
  2657. VIRTIO_NET_F_GUEST_HDRLEN);
  2658. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  2659. /* Make sure it's safe to cast between formats */
  2660. BUILD_BUG_ON(__alignof__(*hdr) != __alignof__(hdr->hash_hdr));
  2661. BUILD_BUG_ON(__alignof__(*hdr) != __alignof__(hdr->hash_hdr.hdr));
  2662. can_push = vi->any_header_sg &&
  2663. !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
  2664. !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
  2665. /* Even if we can, don't push here yet as this would skew
  2666. * csum_start offset below. */
  2667. if (can_push)
  2668. hdr = (struct virtio_net_hdr_v1_hash_tunnel *)(skb->data -
  2669. hdr_len);
  2670. else
  2671. hdr = &skb_vnet_common_hdr(skb)->tnl_hdr;
  2672. if (virtio_net_hdr_tnl_from_skb(skb, hdr, vi->tx_tnl,
  2673. virtio_is_little_endian(vi->vdev), 0,
  2674. false, feature_hdrlen))
  2675. return -EPROTO;
  2676. if (vi->mergeable_rx_bufs)
  2677. hdr->hash_hdr.hdr.num_buffers = 0;
  2678. sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
  2679. if (can_push) {
  2680. __skb_push(skb, hdr_len);
  2681. num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
  2682. if (unlikely(num_sg < 0))
  2683. return num_sg;
  2684. /* Pull header back to avoid skew in tx bytes calculations. */
  2685. __skb_pull(skb, hdr_len);
  2686. } else {
  2687. sg_set_buf(sq->sg, hdr, hdr_len);
  2688. num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
  2689. if (unlikely(num_sg < 0))
  2690. return num_sg;
  2691. num_sg++;
  2692. }
  2693. return virtnet_add_outbuf(sq, num_sg, skb,
  2694. orphan ? VIRTNET_XMIT_TYPE_SKB_ORPHAN : VIRTNET_XMIT_TYPE_SKB);
  2695. }
  2696. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  2697. {
  2698. struct virtnet_info *vi = netdev_priv(dev);
  2699. int qnum = skb_get_queue_mapping(skb);
  2700. struct send_queue *sq = &vi->sq[qnum];
  2701. int err;
  2702. struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
  2703. bool xmit_more = netdev_xmit_more();
  2704. bool use_napi = sq->napi.weight;
  2705. bool kick;
  2706. if (!use_napi)
  2707. free_old_xmit(sq, txq, false);
  2708. else
  2709. virtqueue_disable_cb(sq->vq);
  2710. /* timestamp packet in software */
  2711. skb_tx_timestamp(skb);
  2712. /* Try to transmit */
  2713. err = xmit_skb(sq, skb, !use_napi);
  2714. /* This should not happen! */
  2715. if (unlikely(err)) {
  2716. DEV_STATS_INC(dev, tx_fifo_errors);
  2717. if (net_ratelimit())
  2718. dev_warn(&dev->dev,
  2719. "Unexpected TXQ (%d) queue failure: %d\n",
  2720. qnum, err);
  2721. DEV_STATS_INC(dev, tx_dropped);
  2722. dev_kfree_skb_any(skb);
  2723. return NETDEV_TX_OK;
  2724. }
  2725. /* Don't wait up for transmitted skbs to be freed. */
  2726. if (!use_napi) {
  2727. skb_orphan(skb);
  2728. skb_dst_drop(skb);
  2729. nf_reset_ct(skb);
  2730. }
  2731. if (use_napi)
  2732. tx_may_stop(vi, dev, sq);
  2733. else
  2734. check_sq_full_and_disable(vi, dev,sq);
  2735. kick = use_napi ? __netdev_tx_sent_queue(txq, skb->len, xmit_more) :
  2736. !xmit_more || netif_xmit_stopped(txq);
  2737. if (kick) {
  2738. if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
  2739. u64_stats_update_begin(&sq->stats.syncp);
  2740. u64_stats_inc(&sq->stats.kicks);
  2741. u64_stats_update_end(&sq->stats.syncp);
  2742. }
  2743. }
  2744. if (use_napi && kick && unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
  2745. virtqueue_napi_schedule(&sq->napi, sq->vq);
  2746. return NETDEV_TX_OK;
  2747. }
  2748. static void virtnet_rx_pause(struct virtnet_info *vi,
  2749. struct receive_queue *rq)
  2750. {
  2751. bool running = netif_running(vi->dev);
  2752. if (running) {
  2753. virtnet_napi_disable(rq);
  2754. virtnet_cancel_dim(vi, &rq->dim);
  2755. }
  2756. }
  2757. static void virtnet_rx_pause_all(struct virtnet_info *vi)
  2758. {
  2759. int i;
  2760. for (i = 0; i < vi->max_queue_pairs; i++)
  2761. virtnet_rx_pause(vi, &vi->rq[i]);
  2762. }
  2763. static void virtnet_rx_resume(struct virtnet_info *vi,
  2764. struct receive_queue *rq,
  2765. bool refill)
  2766. {
  2767. if (netif_running(vi->dev)) {
  2768. /* Pre-fill rq agressively, to make sure we are ready to get
  2769. * packets immediately.
  2770. */
  2771. if (refill)
  2772. try_fill_recv(vi, rq, GFP_KERNEL);
  2773. virtnet_napi_enable(rq);
  2774. }
  2775. }
  2776. static void virtnet_rx_resume_all(struct virtnet_info *vi)
  2777. {
  2778. int i;
  2779. for (i = 0; i < vi->max_queue_pairs; i++) {
  2780. if (i < vi->curr_queue_pairs)
  2781. virtnet_rx_resume(vi, &vi->rq[i], true);
  2782. else
  2783. virtnet_rx_resume(vi, &vi->rq[i], false);
  2784. }
  2785. }
  2786. static int virtnet_rx_resize(struct virtnet_info *vi,
  2787. struct receive_queue *rq, u32 ring_num)
  2788. {
  2789. int err, qindex;
  2790. qindex = rq - vi->rq;
  2791. virtnet_rx_pause(vi, rq);
  2792. err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf, NULL);
  2793. if (err)
  2794. netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
  2795. virtnet_rx_resume(vi, rq, true);
  2796. return err;
  2797. }
  2798. static void virtnet_tx_pause(struct virtnet_info *vi, struct send_queue *sq)
  2799. {
  2800. bool running = netif_running(vi->dev);
  2801. struct netdev_queue *txq;
  2802. int qindex;
  2803. qindex = sq - vi->sq;
  2804. if (running)
  2805. virtnet_napi_tx_disable(sq);
  2806. txq = netdev_get_tx_queue(vi->dev, qindex);
  2807. /* 1. wait all ximt complete
  2808. * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
  2809. */
  2810. __netif_tx_lock_bh(txq);
  2811. /* Prevent rx poll from accessing sq. */
  2812. sq->reset = true;
  2813. /* Prevent the upper layer from trying to send packets. */
  2814. netif_stop_subqueue(vi->dev, qindex);
  2815. u64_stats_update_begin(&sq->stats.syncp);
  2816. u64_stats_inc(&sq->stats.stop);
  2817. u64_stats_update_end(&sq->stats.syncp);
  2818. __netif_tx_unlock_bh(txq);
  2819. }
  2820. static void virtnet_tx_resume(struct virtnet_info *vi, struct send_queue *sq)
  2821. {
  2822. bool running = netif_running(vi->dev);
  2823. struct netdev_queue *txq;
  2824. int qindex;
  2825. qindex = sq - vi->sq;
  2826. txq = netdev_get_tx_queue(vi->dev, qindex);
  2827. __netif_tx_lock_bh(txq);
  2828. sq->reset = false;
  2829. virtnet_tx_wake_queue(vi, sq);
  2830. __netif_tx_unlock_bh(txq);
  2831. if (running)
  2832. virtnet_napi_tx_enable(sq);
  2833. }
  2834. static int virtnet_tx_resize(struct virtnet_info *vi, struct send_queue *sq,
  2835. u32 ring_num)
  2836. {
  2837. int qindex, err;
  2838. if (ring_num <= MAX_SKB_FRAGS + 2) {
  2839. netdev_err(vi->dev, "tx size (%d) cannot be smaller than %d\n",
  2840. ring_num, MAX_SKB_FRAGS + 2);
  2841. return -EINVAL;
  2842. }
  2843. qindex = sq - vi->sq;
  2844. virtnet_tx_pause(vi, sq);
  2845. err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf,
  2846. virtnet_sq_free_unused_buf_done);
  2847. if (err)
  2848. netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
  2849. virtnet_tx_resume(vi, sq);
  2850. return err;
  2851. }
  2852. /*
  2853. * Send command via the control virtqueue and check status. Commands
  2854. * supported by the hypervisor, as indicated by feature bits, should
  2855. * never fail unless improperly formatted.
  2856. */
  2857. static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd,
  2858. struct scatterlist *out,
  2859. struct scatterlist *in)
  2860. {
  2861. struct scatterlist *sgs[5], hdr, stat;
  2862. u32 out_num = 0, tmp, in_num = 0;
  2863. bool ok;
  2864. int ret;
  2865. /* Caller should know better */
  2866. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
  2867. mutex_lock(&vi->cvq_lock);
  2868. vi->ctrl->status = ~0;
  2869. vi->ctrl->hdr.class = class;
  2870. vi->ctrl->hdr.cmd = cmd;
  2871. /* Add header */
  2872. sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
  2873. sgs[out_num++] = &hdr;
  2874. if (out)
  2875. sgs[out_num++] = out;
  2876. /* Add return status. */
  2877. sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
  2878. sgs[out_num + in_num++] = &stat;
  2879. if (in)
  2880. sgs[out_num + in_num++] = in;
  2881. BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
  2882. ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC);
  2883. if (ret < 0) {
  2884. dev_warn(&vi->vdev->dev,
  2885. "Failed to add sgs for command vq: %d\n.", ret);
  2886. mutex_unlock(&vi->cvq_lock);
  2887. return false;
  2888. }
  2889. if (unlikely(!virtqueue_kick(vi->cvq)))
  2890. goto unlock;
  2891. /* Spin for a response, the kick causes an ioport write, trapping
  2892. * into the hypervisor, so the request should be handled immediately.
  2893. */
  2894. while (!virtqueue_get_buf(vi->cvq, &tmp) &&
  2895. !virtqueue_is_broken(vi->cvq)) {
  2896. cond_resched();
  2897. cpu_relax();
  2898. }
  2899. unlock:
  2900. ok = vi->ctrl->status == VIRTIO_NET_OK;
  2901. mutex_unlock(&vi->cvq_lock);
  2902. return ok;
  2903. }
  2904. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  2905. struct scatterlist *out)
  2906. {
  2907. return virtnet_send_command_reply(vi, class, cmd, out, NULL);
  2908. }
  2909. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  2910. {
  2911. struct virtnet_info *vi = netdev_priv(dev);
  2912. struct virtio_device *vdev = vi->vdev;
  2913. int ret;
  2914. struct sockaddr *addr;
  2915. struct scatterlist sg;
  2916. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
  2917. return -EOPNOTSUPP;
  2918. addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
  2919. if (!addr)
  2920. return -ENOMEM;
  2921. ret = eth_prepare_mac_addr_change(dev, addr);
  2922. if (ret)
  2923. goto out;
  2924. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  2925. sg_init_one(&sg, addr->sa_data, dev->addr_len);
  2926. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  2927. VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
  2928. dev_warn(&vdev->dev,
  2929. "Failed to set mac address by vq command.\n");
  2930. ret = -EINVAL;
  2931. goto out;
  2932. }
  2933. } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
  2934. !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  2935. unsigned int i;
  2936. /* Naturally, this has an atomicity problem. */
  2937. for (i = 0; i < dev->addr_len; i++)
  2938. virtio_cwrite8(vdev,
  2939. offsetof(struct virtio_net_config, mac) +
  2940. i, addr->sa_data[i]);
  2941. }
  2942. eth_commit_mac_addr_change(dev, p);
  2943. ret = 0;
  2944. out:
  2945. kfree(addr);
  2946. return ret;
  2947. }
  2948. static void virtnet_stats(struct net_device *dev,
  2949. struct rtnl_link_stats64 *tot)
  2950. {
  2951. struct virtnet_info *vi = netdev_priv(dev);
  2952. unsigned int start;
  2953. int i;
  2954. for (i = 0; i < vi->max_queue_pairs; i++) {
  2955. u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
  2956. struct receive_queue *rq = &vi->rq[i];
  2957. struct send_queue *sq = &vi->sq[i];
  2958. do {
  2959. start = u64_stats_fetch_begin(&sq->stats.syncp);
  2960. tpackets = u64_stats_read(&sq->stats.packets);
  2961. tbytes = u64_stats_read(&sq->stats.bytes);
  2962. terrors = u64_stats_read(&sq->stats.tx_timeouts);
  2963. } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
  2964. do {
  2965. start = u64_stats_fetch_begin(&rq->stats.syncp);
  2966. rpackets = u64_stats_read(&rq->stats.packets);
  2967. rbytes = u64_stats_read(&rq->stats.bytes);
  2968. rdrops = u64_stats_read(&rq->stats.drops);
  2969. } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
  2970. tot->rx_packets += rpackets;
  2971. tot->tx_packets += tpackets;
  2972. tot->rx_bytes += rbytes;
  2973. tot->tx_bytes += tbytes;
  2974. tot->rx_dropped += rdrops;
  2975. tot->tx_errors += terrors;
  2976. }
  2977. tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
  2978. tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors);
  2979. tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors);
  2980. tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors);
  2981. }
  2982. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  2983. {
  2984. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  2985. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
  2986. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  2987. }
  2988. static bool virtnet_commit_rss_command(struct virtnet_info *vi);
  2989. static void virtnet_rss_update_by_qpairs(struct virtnet_info *vi, u16 queue_pairs)
  2990. {
  2991. u32 indir_val = 0;
  2992. int i = 0;
  2993. for (; i < vi->rss_indir_table_size; ++i) {
  2994. indir_val = ethtool_rxfh_indir_default(i, queue_pairs);
  2995. vi->rss_hdr->indirection_table[i] = cpu_to_le16(indir_val);
  2996. }
  2997. vi->rss_trailer.max_tx_vq = cpu_to_le16(queue_pairs);
  2998. }
  2999. static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  3000. {
  3001. struct virtio_net_ctrl_mq *mq __free(kfree) = NULL;
  3002. struct virtio_net_rss_config_hdr *old_rss_hdr;
  3003. struct virtio_net_rss_config_trailer old_rss_trailer;
  3004. struct net_device *dev = vi->dev;
  3005. struct scatterlist sg;
  3006. if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
  3007. return 0;
  3008. /* Firstly check if we need update rss. Do updating if both (1) rss enabled and
  3009. * (2) no user configuration.
  3010. *
  3011. * During rss command processing, device updates queue_pairs using rss.max_tx_vq. That is,
  3012. * the device updates queue_pairs together with rss, so we can skip the separate queue_pairs
  3013. * update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly.
  3014. */
  3015. if (vi->has_rss && !netif_is_rxfh_configured(dev)) {
  3016. old_rss_hdr = vi->rss_hdr;
  3017. old_rss_trailer = vi->rss_trailer;
  3018. vi->rss_hdr = devm_kzalloc(&vi->vdev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL);
  3019. if (!vi->rss_hdr) {
  3020. vi->rss_hdr = old_rss_hdr;
  3021. return -ENOMEM;
  3022. }
  3023. *vi->rss_hdr = *old_rss_hdr;
  3024. virtnet_rss_update_by_qpairs(vi, queue_pairs);
  3025. if (!virtnet_commit_rss_command(vi)) {
  3026. /* restore ctrl_rss if commit_rss_command failed */
  3027. devm_kfree(&vi->vdev->dev, vi->rss_hdr);
  3028. vi->rss_hdr = old_rss_hdr;
  3029. vi->rss_trailer = old_rss_trailer;
  3030. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d, because committing RSS failed\n",
  3031. queue_pairs);
  3032. return -EINVAL;
  3033. }
  3034. devm_kfree(&vi->vdev->dev, old_rss_hdr);
  3035. goto succ;
  3036. }
  3037. mq = kzalloc_obj(*mq);
  3038. if (!mq)
  3039. return -ENOMEM;
  3040. mq->virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
  3041. sg_init_one(&sg, mq, sizeof(*mq));
  3042. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  3043. VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
  3044. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
  3045. queue_pairs);
  3046. return -EINVAL;
  3047. }
  3048. succ:
  3049. vi->curr_queue_pairs = queue_pairs;
  3050. if (dev->flags & IFF_UP) {
  3051. local_bh_disable();
  3052. for (int i = 0; i < vi->curr_queue_pairs; ++i)
  3053. virtqueue_napi_schedule(&vi->rq[i].napi, vi->rq[i].vq);
  3054. local_bh_enable();
  3055. }
  3056. return 0;
  3057. }
  3058. static int virtnet_close(struct net_device *dev)
  3059. {
  3060. struct virtnet_info *vi = netdev_priv(dev);
  3061. int i;
  3062. /* Prevent the config change callback from changing carrier
  3063. * after close
  3064. */
  3065. virtio_config_driver_disable(vi->vdev);
  3066. /* Stop getting status/speed updates: we don't care until next
  3067. * open
  3068. */
  3069. cancel_work_sync(&vi->config_work);
  3070. for (i = 0; i < vi->max_queue_pairs; i++) {
  3071. virtnet_disable_queue_pair(vi, i);
  3072. virtnet_cancel_dim(vi, &vi->rq[i].dim);
  3073. }
  3074. netif_carrier_off(dev);
  3075. return 0;
  3076. }
  3077. static void virtnet_rx_mode_work(struct work_struct *work)
  3078. {
  3079. struct virtnet_info *vi =
  3080. container_of(work, struct virtnet_info, rx_mode_work);
  3081. u8 *promisc_allmulti __free(kfree) = NULL;
  3082. struct net_device *dev = vi->dev;
  3083. struct scatterlist sg[2];
  3084. struct virtio_net_ctrl_mac *mac_data;
  3085. struct netdev_hw_addr *ha;
  3086. int uc_count;
  3087. int mc_count;
  3088. void *buf;
  3089. int i;
  3090. /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
  3091. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  3092. return;
  3093. promisc_allmulti = kzalloc_obj(*promisc_allmulti);
  3094. if (!promisc_allmulti) {
  3095. dev_warn(&dev->dev, "Failed to set RX mode, no memory.\n");
  3096. return;
  3097. }
  3098. rtnl_lock();
  3099. *promisc_allmulti = !!(dev->flags & IFF_PROMISC);
  3100. sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
  3101. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  3102. VIRTIO_NET_CTRL_RX_PROMISC, sg))
  3103. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  3104. *promisc_allmulti ? "en" : "dis");
  3105. *promisc_allmulti = !!(dev->flags & IFF_ALLMULTI);
  3106. sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
  3107. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  3108. VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
  3109. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  3110. *promisc_allmulti ? "en" : "dis");
  3111. netif_addr_lock_bh(dev);
  3112. uc_count = netdev_uc_count(dev);
  3113. mc_count = netdev_mc_count(dev);
  3114. /* MAC filter - use one buffer for both lists */
  3115. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  3116. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  3117. mac_data = buf;
  3118. if (!buf) {
  3119. netif_addr_unlock_bh(dev);
  3120. rtnl_unlock();
  3121. return;
  3122. }
  3123. sg_init_table(sg, 2);
  3124. /* Store the unicast list and count in the front of the buffer */
  3125. mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
  3126. i = 0;
  3127. netdev_for_each_uc_addr(ha, dev)
  3128. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  3129. sg_set_buf(&sg[0], mac_data,
  3130. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  3131. /* multicast list and count fill the end */
  3132. mac_data = (void *)&mac_data->macs[uc_count][0];
  3133. mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
  3134. i = 0;
  3135. netdev_for_each_mc_addr(ha, dev)
  3136. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  3137. netif_addr_unlock_bh(dev);
  3138. sg_set_buf(&sg[1], mac_data,
  3139. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  3140. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  3141. VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
  3142. dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
  3143. rtnl_unlock();
  3144. kfree(buf);
  3145. }
  3146. static void virtnet_set_rx_mode(struct net_device *dev)
  3147. {
  3148. struct virtnet_info *vi = netdev_priv(dev);
  3149. if (vi->rx_mode_work_enabled)
  3150. schedule_work(&vi->rx_mode_work);
  3151. }
  3152. static int virtnet_vlan_rx_add_vid(struct net_device *dev,
  3153. __be16 proto, u16 vid)
  3154. {
  3155. struct virtnet_info *vi = netdev_priv(dev);
  3156. __virtio16 *_vid __free(kfree) = NULL;
  3157. struct scatterlist sg;
  3158. _vid = kzalloc_obj(*_vid);
  3159. if (!_vid)
  3160. return -ENOMEM;
  3161. *_vid = cpu_to_virtio16(vi->vdev, vid);
  3162. sg_init_one(&sg, _vid, sizeof(*_vid));
  3163. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  3164. VIRTIO_NET_CTRL_VLAN_ADD, &sg))
  3165. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  3166. return 0;
  3167. }
  3168. static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
  3169. __be16 proto, u16 vid)
  3170. {
  3171. struct virtnet_info *vi = netdev_priv(dev);
  3172. __virtio16 *_vid __free(kfree) = NULL;
  3173. struct scatterlist sg;
  3174. _vid = kzalloc_obj(*_vid);
  3175. if (!_vid)
  3176. return -ENOMEM;
  3177. *_vid = cpu_to_virtio16(vi->vdev, vid);
  3178. sg_init_one(&sg, _vid, sizeof(*_vid));
  3179. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  3180. VIRTIO_NET_CTRL_VLAN_DEL, &sg))
  3181. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  3182. return 0;
  3183. }
  3184. static void virtnet_clean_affinity(struct virtnet_info *vi)
  3185. {
  3186. int i;
  3187. if (vi->affinity_hint_set) {
  3188. for (i = 0; i < vi->max_queue_pairs; i++) {
  3189. virtqueue_set_affinity(vi->rq[i].vq, NULL);
  3190. virtqueue_set_affinity(vi->sq[i].vq, NULL);
  3191. }
  3192. vi->affinity_hint_set = false;
  3193. }
  3194. }
  3195. static void virtnet_set_affinity(struct virtnet_info *vi)
  3196. {
  3197. cpumask_var_t mask;
  3198. int stragglers;
  3199. int group_size;
  3200. int i, start = 0, cpu;
  3201. int num_cpu;
  3202. int stride;
  3203. if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
  3204. virtnet_clean_affinity(vi);
  3205. return;
  3206. }
  3207. num_cpu = num_online_cpus();
  3208. stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
  3209. stragglers = num_cpu >= vi->curr_queue_pairs ?
  3210. num_cpu % vi->curr_queue_pairs :
  3211. 0;
  3212. for (i = 0; i < vi->curr_queue_pairs; i++) {
  3213. group_size = stride + (i < stragglers ? 1 : 0);
  3214. for_each_online_cpu_wrap(cpu, start) {
  3215. if (!group_size--) {
  3216. start = cpu;
  3217. break;
  3218. }
  3219. cpumask_set_cpu(cpu, mask);
  3220. }
  3221. virtqueue_set_affinity(vi->rq[i].vq, mask);
  3222. virtqueue_set_affinity(vi->sq[i].vq, mask);
  3223. __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
  3224. cpumask_clear(mask);
  3225. }
  3226. vi->affinity_hint_set = true;
  3227. free_cpumask_var(mask);
  3228. }
  3229. static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
  3230. {
  3231. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  3232. node);
  3233. virtnet_set_affinity(vi);
  3234. return 0;
  3235. }
  3236. static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
  3237. {
  3238. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  3239. node_dead);
  3240. virtnet_set_affinity(vi);
  3241. return 0;
  3242. }
  3243. static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
  3244. {
  3245. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  3246. node);
  3247. virtnet_clean_affinity(vi);
  3248. return 0;
  3249. }
  3250. static enum cpuhp_state virtionet_online;
  3251. static int virtnet_cpu_notif_add(struct virtnet_info *vi)
  3252. {
  3253. int ret;
  3254. ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
  3255. if (ret)
  3256. return ret;
  3257. ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  3258. &vi->node_dead);
  3259. if (!ret)
  3260. return ret;
  3261. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  3262. return ret;
  3263. }
  3264. static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
  3265. {
  3266. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  3267. cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  3268. &vi->node_dead);
  3269. }
  3270. static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
  3271. u16 vqn, u32 max_usecs, u32 max_packets)
  3272. {
  3273. struct virtio_net_ctrl_coal_vq *coal_vq __free(kfree) = NULL;
  3274. struct scatterlist sgs;
  3275. coal_vq = kzalloc_obj(*coal_vq);
  3276. if (!coal_vq)
  3277. return -ENOMEM;
  3278. coal_vq->vqn = cpu_to_le16(vqn);
  3279. coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
  3280. coal_vq->coal.max_packets = cpu_to_le32(max_packets);
  3281. sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
  3282. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
  3283. VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
  3284. &sgs))
  3285. return -EINVAL;
  3286. return 0;
  3287. }
  3288. static int virtnet_send_rx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
  3289. u16 queue, u32 max_usecs,
  3290. u32 max_packets)
  3291. {
  3292. int err;
  3293. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
  3294. return -EOPNOTSUPP;
  3295. err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
  3296. max_usecs, max_packets);
  3297. if (err)
  3298. return err;
  3299. vi->rq[queue].intr_coal.max_usecs = max_usecs;
  3300. vi->rq[queue].intr_coal.max_packets = max_packets;
  3301. return 0;
  3302. }
  3303. static int virtnet_send_tx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
  3304. u16 queue, u32 max_usecs,
  3305. u32 max_packets)
  3306. {
  3307. int err;
  3308. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
  3309. return -EOPNOTSUPP;
  3310. err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
  3311. max_usecs, max_packets);
  3312. if (err)
  3313. return err;
  3314. vi->sq[queue].intr_coal.max_usecs = max_usecs;
  3315. vi->sq[queue].intr_coal.max_packets = max_packets;
  3316. return 0;
  3317. }
  3318. static void virtnet_get_ringparam(struct net_device *dev,
  3319. struct ethtool_ringparam *ring,
  3320. struct kernel_ethtool_ringparam *kernel_ring,
  3321. struct netlink_ext_ack *extack)
  3322. {
  3323. struct virtnet_info *vi = netdev_priv(dev);
  3324. ring->rx_max_pending = vi->rq[0].vq->num_max;
  3325. ring->tx_max_pending = vi->sq[0].vq->num_max;
  3326. ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  3327. ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  3328. }
  3329. static int virtnet_set_ringparam(struct net_device *dev,
  3330. struct ethtool_ringparam *ring,
  3331. struct kernel_ethtool_ringparam *kernel_ring,
  3332. struct netlink_ext_ack *extack)
  3333. {
  3334. struct virtnet_info *vi = netdev_priv(dev);
  3335. u32 rx_pending, tx_pending;
  3336. struct receive_queue *rq;
  3337. struct send_queue *sq;
  3338. int i, err;
  3339. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  3340. return -EINVAL;
  3341. rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  3342. tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  3343. if (ring->rx_pending == rx_pending &&
  3344. ring->tx_pending == tx_pending)
  3345. return 0;
  3346. if (ring->rx_pending > vi->rq[0].vq->num_max)
  3347. return -EINVAL;
  3348. if (ring->tx_pending > vi->sq[0].vq->num_max)
  3349. return -EINVAL;
  3350. for (i = 0; i < vi->max_queue_pairs; i++) {
  3351. rq = vi->rq + i;
  3352. sq = vi->sq + i;
  3353. if (ring->tx_pending != tx_pending) {
  3354. err = virtnet_tx_resize(vi, sq, ring->tx_pending);
  3355. if (err)
  3356. return err;
  3357. /* Upon disabling and re-enabling a transmit virtqueue, the device must
  3358. * set the coalescing parameters of the virtqueue to those configured
  3359. * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver
  3360. * did not set any TX coalescing parameters, to 0.
  3361. */
  3362. err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, i,
  3363. vi->intr_coal_tx.max_usecs,
  3364. vi->intr_coal_tx.max_packets);
  3365. /* Don't break the tx resize action if the vq coalescing is not
  3366. * supported. The same is true for rx resize below.
  3367. */
  3368. if (err && err != -EOPNOTSUPP)
  3369. return err;
  3370. }
  3371. if (ring->rx_pending != rx_pending) {
  3372. err = virtnet_rx_resize(vi, rq, ring->rx_pending);
  3373. if (err)
  3374. return err;
  3375. /* The reason is same as the transmit virtqueue reset */
  3376. mutex_lock(&vi->rq[i].dim_lock);
  3377. err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, i,
  3378. vi->intr_coal_rx.max_usecs,
  3379. vi->intr_coal_rx.max_packets);
  3380. mutex_unlock(&vi->rq[i].dim_lock);
  3381. if (err && err != -EOPNOTSUPP)
  3382. return err;
  3383. }
  3384. }
  3385. return 0;
  3386. }
  3387. static bool virtnet_commit_rss_command(struct virtnet_info *vi)
  3388. {
  3389. struct net_device *dev = vi->dev;
  3390. struct scatterlist sgs[2];
  3391. /* prepare sgs */
  3392. sg_init_table(sgs, 2);
  3393. sg_set_buf(&sgs[0], vi->rss_hdr, virtnet_rss_hdr_size(vi));
  3394. sg_set_buf(&sgs[1], &vi->rss_trailer, virtnet_rss_trailer_size(vi));
  3395. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  3396. vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
  3397. : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs))
  3398. goto err;
  3399. return true;
  3400. err:
  3401. dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
  3402. return false;
  3403. }
  3404. static void virtnet_init_default_rss(struct virtnet_info *vi)
  3405. {
  3406. vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_supported);
  3407. vi->rss_hash_types_saved = vi->rss_hash_types_supported;
  3408. vi->rss_hdr->indirection_table_mask = vi->rss_indir_table_size
  3409. ? cpu_to_le16(vi->rss_indir_table_size - 1) : 0;
  3410. vi->rss_hdr->unclassified_queue = 0;
  3411. virtnet_rss_update_by_qpairs(vi, vi->curr_queue_pairs);
  3412. vi->rss_trailer.hash_key_length = vi->rss_key_size;
  3413. netdev_rss_key_fill(vi->rss_hash_key_data, vi->rss_key_size);
  3414. }
  3415. static int virtnet_get_hashflow(struct net_device *dev,
  3416. struct ethtool_rxfh_fields *info)
  3417. {
  3418. struct virtnet_info *vi = netdev_priv(dev);
  3419. info->data = 0;
  3420. switch (info->flow_type) {
  3421. case TCP_V4_FLOW:
  3422. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
  3423. info->data = RXH_IP_SRC | RXH_IP_DST |
  3424. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  3425. } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
  3426. info->data = RXH_IP_SRC | RXH_IP_DST;
  3427. }
  3428. break;
  3429. case TCP_V6_FLOW:
  3430. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
  3431. info->data = RXH_IP_SRC | RXH_IP_DST |
  3432. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  3433. } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
  3434. info->data = RXH_IP_SRC | RXH_IP_DST;
  3435. }
  3436. break;
  3437. case UDP_V4_FLOW:
  3438. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
  3439. info->data = RXH_IP_SRC | RXH_IP_DST |
  3440. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  3441. } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
  3442. info->data = RXH_IP_SRC | RXH_IP_DST;
  3443. }
  3444. break;
  3445. case UDP_V6_FLOW:
  3446. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
  3447. info->data = RXH_IP_SRC | RXH_IP_DST |
  3448. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  3449. } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
  3450. info->data = RXH_IP_SRC | RXH_IP_DST;
  3451. }
  3452. break;
  3453. case IPV4_FLOW:
  3454. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
  3455. info->data = RXH_IP_SRC | RXH_IP_DST;
  3456. break;
  3457. case IPV6_FLOW:
  3458. if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
  3459. info->data = RXH_IP_SRC | RXH_IP_DST;
  3460. break;
  3461. default:
  3462. info->data = 0;
  3463. break;
  3464. }
  3465. return 0;
  3466. }
  3467. static int virtnet_set_hashflow(struct net_device *dev,
  3468. const struct ethtool_rxfh_fields *info,
  3469. struct netlink_ext_ack *extack)
  3470. {
  3471. struct virtnet_info *vi = netdev_priv(dev);
  3472. u32 new_hashtypes = vi->rss_hash_types_saved;
  3473. bool is_disable = info->data & RXH_DISCARD;
  3474. bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
  3475. /* supports only 'sd', 'sdfn' and 'r' */
  3476. if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
  3477. return -EINVAL;
  3478. switch (info->flow_type) {
  3479. case TCP_V4_FLOW:
  3480. new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
  3481. if (!is_disable)
  3482. new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
  3483. | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
  3484. break;
  3485. case UDP_V4_FLOW:
  3486. new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
  3487. if (!is_disable)
  3488. new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
  3489. | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
  3490. break;
  3491. case IPV4_FLOW:
  3492. new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
  3493. if (!is_disable)
  3494. new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
  3495. break;
  3496. case TCP_V6_FLOW:
  3497. new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
  3498. if (!is_disable)
  3499. new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
  3500. | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
  3501. break;
  3502. case UDP_V6_FLOW:
  3503. new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
  3504. if (!is_disable)
  3505. new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
  3506. | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
  3507. break;
  3508. case IPV6_FLOW:
  3509. new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
  3510. if (!is_disable)
  3511. new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
  3512. break;
  3513. default:
  3514. /* unsupported flow */
  3515. return -EINVAL;
  3516. }
  3517. /* if unsupported hashtype was set */
  3518. if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
  3519. return -EINVAL;
  3520. if (new_hashtypes != vi->rss_hash_types_saved) {
  3521. vi->rss_hash_types_saved = new_hashtypes;
  3522. vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved);
  3523. if (vi->dev->features & NETIF_F_RXHASH)
  3524. if (!virtnet_commit_rss_command(vi))
  3525. return -EINVAL;
  3526. }
  3527. return 0;
  3528. }
  3529. static void virtnet_get_drvinfo(struct net_device *dev,
  3530. struct ethtool_drvinfo *info)
  3531. {
  3532. struct virtnet_info *vi = netdev_priv(dev);
  3533. struct virtio_device *vdev = vi->vdev;
  3534. strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  3535. strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  3536. strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  3537. }
  3538. /* TODO: Eliminate OOO packets during switching */
  3539. static int virtnet_set_channels(struct net_device *dev,
  3540. struct ethtool_channels *channels)
  3541. {
  3542. struct virtnet_info *vi = netdev_priv(dev);
  3543. u16 queue_pairs = channels->combined_count;
  3544. int err;
  3545. /* We don't support separate rx/tx channels.
  3546. * We don't allow setting 'other' channels.
  3547. */
  3548. if (channels->rx_count || channels->tx_count || channels->other_count)
  3549. return -EINVAL;
  3550. if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
  3551. return -EINVAL;
  3552. /* For now we don't support modifying channels while XDP is loaded
  3553. * also when XDP is loaded all RX queues have XDP programs so we only
  3554. * need to check a single RX queue.
  3555. */
  3556. if (vi->rq[0].xdp_prog)
  3557. return -EINVAL;
  3558. cpus_read_lock();
  3559. err = virtnet_set_queues(vi, queue_pairs);
  3560. if (err) {
  3561. cpus_read_unlock();
  3562. goto err;
  3563. }
  3564. virtnet_set_affinity(vi);
  3565. cpus_read_unlock();
  3566. netif_set_real_num_tx_queues(dev, queue_pairs);
  3567. netif_set_real_num_rx_queues(dev, queue_pairs);
  3568. err:
  3569. return err;
  3570. }
  3571. static void virtnet_stats_sprintf(u8 **p, const char *fmt, const char *noq_fmt,
  3572. int num, int qid, const struct virtnet_stat_desc *desc)
  3573. {
  3574. int i;
  3575. if (qid < 0) {
  3576. for (i = 0; i < num; ++i)
  3577. ethtool_sprintf(p, noq_fmt, desc[i].desc);
  3578. } else {
  3579. for (i = 0; i < num; ++i)
  3580. ethtool_sprintf(p, fmt, qid, desc[i].desc);
  3581. }
  3582. }
  3583. /* qid == -1: for rx/tx queue total field */
  3584. static void virtnet_get_stats_string(struct virtnet_info *vi, int type, int qid, u8 **data)
  3585. {
  3586. const struct virtnet_stat_desc *desc;
  3587. const char *fmt, *noq_fmt;
  3588. u8 *p = *data;
  3589. u32 num;
  3590. if (type == VIRTNET_Q_TYPE_CQ && qid >= 0) {
  3591. noq_fmt = "cq_hw_%s";
  3592. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
  3593. desc = &virtnet_stats_cvq_desc[0];
  3594. num = ARRAY_SIZE(virtnet_stats_cvq_desc);
  3595. virtnet_stats_sprintf(&p, NULL, noq_fmt, num, -1, desc);
  3596. }
  3597. }
  3598. if (type == VIRTNET_Q_TYPE_RX) {
  3599. fmt = "rx%u_%s";
  3600. noq_fmt = "rx_%s";
  3601. desc = &virtnet_rq_stats_desc[0];
  3602. num = ARRAY_SIZE(virtnet_rq_stats_desc);
  3603. virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
  3604. fmt = "rx%u_hw_%s";
  3605. noq_fmt = "rx_hw_%s";
  3606. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
  3607. desc = &virtnet_stats_rx_basic_desc[0];
  3608. num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
  3609. virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
  3610. }
  3611. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
  3612. desc = &virtnet_stats_rx_csum_desc[0];
  3613. num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
  3614. virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
  3615. }
  3616. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
  3617. desc = &virtnet_stats_rx_speed_desc[0];
  3618. num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
  3619. virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
  3620. }
  3621. }
  3622. if (type == VIRTNET_Q_TYPE_TX) {
  3623. fmt = "tx%u_%s";
  3624. noq_fmt = "tx_%s";
  3625. desc = &virtnet_sq_stats_desc[0];
  3626. num = ARRAY_SIZE(virtnet_sq_stats_desc);
  3627. virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
  3628. fmt = "tx%u_hw_%s";
  3629. noq_fmt = "tx_hw_%s";
  3630. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
  3631. desc = &virtnet_stats_tx_basic_desc[0];
  3632. num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
  3633. virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
  3634. }
  3635. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
  3636. desc = &virtnet_stats_tx_gso_desc[0];
  3637. num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
  3638. virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
  3639. }
  3640. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
  3641. desc = &virtnet_stats_tx_speed_desc[0];
  3642. num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
  3643. virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
  3644. }
  3645. }
  3646. *data = p;
  3647. }
  3648. struct virtnet_stats_ctx {
  3649. /* The stats are write to qstats or ethtool -S */
  3650. bool to_qstat;
  3651. /* Used to calculate the offset inside the output buffer. */
  3652. u32 desc_num[3];
  3653. /* The actual supported stat types. */
  3654. u64 bitmap[3];
  3655. /* Used to calculate the reply buffer size. */
  3656. u32 size[3];
  3657. /* Record the output buffer. */
  3658. u64 *data;
  3659. };
  3660. static void virtnet_stats_ctx_init(struct virtnet_info *vi,
  3661. struct virtnet_stats_ctx *ctx,
  3662. u64 *data, bool to_qstat)
  3663. {
  3664. u32 queue_type;
  3665. ctx->data = data;
  3666. ctx->to_qstat = to_qstat;
  3667. if (to_qstat) {
  3668. ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
  3669. ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
  3670. queue_type = VIRTNET_Q_TYPE_RX;
  3671. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
  3672. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
  3673. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
  3674. ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
  3675. }
  3676. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
  3677. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
  3678. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
  3679. ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
  3680. }
  3681. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
  3682. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_GSO;
  3683. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
  3684. ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_gso);
  3685. }
  3686. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
  3687. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
  3688. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
  3689. ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
  3690. }
  3691. queue_type = VIRTNET_Q_TYPE_TX;
  3692. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
  3693. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
  3694. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
  3695. ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
  3696. }
  3697. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
  3698. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_CSUM;
  3699. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
  3700. ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_csum);
  3701. }
  3702. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
  3703. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
  3704. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
  3705. ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
  3706. }
  3707. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
  3708. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
  3709. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
  3710. ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
  3711. }
  3712. return;
  3713. }
  3714. ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc);
  3715. ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc);
  3716. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
  3717. queue_type = VIRTNET_Q_TYPE_CQ;
  3718. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_CVQ;
  3719. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_cvq_desc);
  3720. ctx->size[queue_type] += sizeof(struct virtio_net_stats_cvq);
  3721. }
  3722. queue_type = VIRTNET_Q_TYPE_RX;
  3723. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
  3724. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
  3725. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc);
  3726. ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
  3727. }
  3728. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
  3729. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
  3730. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc);
  3731. ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
  3732. }
  3733. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
  3734. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
  3735. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc);
  3736. ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
  3737. }
  3738. queue_type = VIRTNET_Q_TYPE_TX;
  3739. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
  3740. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
  3741. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc);
  3742. ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
  3743. }
  3744. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
  3745. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
  3746. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc);
  3747. ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
  3748. }
  3749. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
  3750. ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
  3751. ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc);
  3752. ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
  3753. }
  3754. }
  3755. /* stats_sum_queue - Calculate the sum of the same fields in sq or rq.
  3756. * @sum: the position to store the sum values
  3757. * @num: field num
  3758. * @q_value: the first queue fields
  3759. * @q_num: number of the queues
  3760. */
  3761. static void stats_sum_queue(u64 *sum, u32 num, u64 *q_value, u32 q_num)
  3762. {
  3763. u32 step = num;
  3764. int i, j;
  3765. u64 *p;
  3766. for (i = 0; i < num; ++i) {
  3767. p = sum + i;
  3768. *p = 0;
  3769. for (j = 0; j < q_num; ++j)
  3770. *p += *(q_value + i + j * step);
  3771. }
  3772. }
  3773. static void virtnet_fill_total_fields(struct virtnet_info *vi,
  3774. struct virtnet_stats_ctx *ctx)
  3775. {
  3776. u64 *data, *first_rx_q, *first_tx_q;
  3777. u32 num_cq, num_rx, num_tx;
  3778. num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
  3779. num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
  3780. num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
  3781. first_rx_q = ctx->data + num_rx + num_tx + num_cq;
  3782. first_tx_q = first_rx_q + vi->curr_queue_pairs * num_rx;
  3783. data = ctx->data;
  3784. stats_sum_queue(data, num_rx, first_rx_q, vi->curr_queue_pairs);
  3785. data = ctx->data + num_rx;
  3786. stats_sum_queue(data, num_tx, first_tx_q, vi->curr_queue_pairs);
  3787. }
  3788. static void virtnet_fill_stats_qstat(struct virtnet_info *vi, u32 qid,
  3789. struct virtnet_stats_ctx *ctx,
  3790. const u8 *base, bool drv_stats, u8 reply_type)
  3791. {
  3792. const struct virtnet_stat_desc *desc;
  3793. const u64_stats_t *v_stat;
  3794. u64 offset, bitmap;
  3795. const __le64 *v;
  3796. u32 queue_type;
  3797. int i, num;
  3798. queue_type = vq_type(vi, qid);
  3799. bitmap = ctx->bitmap[queue_type];
  3800. if (drv_stats) {
  3801. if (queue_type == VIRTNET_Q_TYPE_RX) {
  3802. desc = &virtnet_rq_stats_desc_qstat[0];
  3803. num = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
  3804. } else {
  3805. desc = &virtnet_sq_stats_desc_qstat[0];
  3806. num = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
  3807. }
  3808. for (i = 0; i < num; ++i) {
  3809. offset = desc[i].qstat_offset / sizeof(*ctx->data);
  3810. v_stat = (const u64_stats_t *)(base + desc[i].offset);
  3811. ctx->data[offset] = u64_stats_read(v_stat);
  3812. }
  3813. return;
  3814. }
  3815. if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
  3816. desc = &virtnet_stats_rx_basic_desc_qstat[0];
  3817. num = ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
  3818. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
  3819. goto found;
  3820. }
  3821. if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
  3822. desc = &virtnet_stats_rx_csum_desc_qstat[0];
  3823. num = ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
  3824. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
  3825. goto found;
  3826. }
  3827. if (bitmap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
  3828. desc = &virtnet_stats_rx_gso_desc_qstat[0];
  3829. num = ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
  3830. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_GSO)
  3831. goto found;
  3832. }
  3833. if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
  3834. desc = &virtnet_stats_rx_speed_desc_qstat[0];
  3835. num = ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
  3836. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
  3837. goto found;
  3838. }
  3839. if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
  3840. desc = &virtnet_stats_tx_basic_desc_qstat[0];
  3841. num = ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
  3842. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
  3843. goto found;
  3844. }
  3845. if (bitmap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
  3846. desc = &virtnet_stats_tx_csum_desc_qstat[0];
  3847. num = ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
  3848. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_CSUM)
  3849. goto found;
  3850. }
  3851. if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
  3852. desc = &virtnet_stats_tx_gso_desc_qstat[0];
  3853. num = ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
  3854. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
  3855. goto found;
  3856. }
  3857. if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
  3858. desc = &virtnet_stats_tx_speed_desc_qstat[0];
  3859. num = ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
  3860. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
  3861. goto found;
  3862. }
  3863. return;
  3864. found:
  3865. for (i = 0; i < num; ++i) {
  3866. offset = desc[i].qstat_offset / sizeof(*ctx->data);
  3867. v = (const __le64 *)(base + desc[i].offset);
  3868. ctx->data[offset] = le64_to_cpu(*v);
  3869. }
  3870. }
  3871. /* virtnet_fill_stats - copy the stats to qstats or ethtool -S
  3872. * The stats source is the device or the driver.
  3873. *
  3874. * @vi: virtio net info
  3875. * @qid: the vq id
  3876. * @ctx: stats ctx (initiated by virtnet_stats_ctx_init())
  3877. * @base: pointer to the device reply or the driver stats structure.
  3878. * @drv_stats: designate the base type (device reply, driver stats)
  3879. * @type: the type of the device reply (if drv_stats is true, this must be zero)
  3880. */
  3881. static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid,
  3882. struct virtnet_stats_ctx *ctx,
  3883. const u8 *base, bool drv_stats, u8 reply_type)
  3884. {
  3885. u32 queue_type, num_rx, num_tx, num_cq;
  3886. const struct virtnet_stat_desc *desc;
  3887. const u64_stats_t *v_stat;
  3888. u64 offset, bitmap;
  3889. const __le64 *v;
  3890. int i, num;
  3891. if (ctx->to_qstat)
  3892. return virtnet_fill_stats_qstat(vi, qid, ctx, base, drv_stats, reply_type);
  3893. num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
  3894. num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
  3895. num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
  3896. queue_type = vq_type(vi, qid);
  3897. bitmap = ctx->bitmap[queue_type];
  3898. /* skip the total fields of pairs */
  3899. offset = num_rx + num_tx;
  3900. if (queue_type == VIRTNET_Q_TYPE_TX) {
  3901. offset += num_cq + num_rx * vi->curr_queue_pairs + num_tx * (qid / 2);
  3902. num = ARRAY_SIZE(virtnet_sq_stats_desc);
  3903. if (drv_stats) {
  3904. desc = &virtnet_sq_stats_desc[0];
  3905. goto drv_stats;
  3906. }
  3907. offset += num;
  3908. } else if (queue_type == VIRTNET_Q_TYPE_RX) {
  3909. offset += num_cq + num_rx * (qid / 2);
  3910. num = ARRAY_SIZE(virtnet_rq_stats_desc);
  3911. if (drv_stats) {
  3912. desc = &virtnet_rq_stats_desc[0];
  3913. goto drv_stats;
  3914. }
  3915. offset += num;
  3916. }
  3917. if (bitmap & VIRTIO_NET_STATS_TYPE_CVQ) {
  3918. desc = &virtnet_stats_cvq_desc[0];
  3919. num = ARRAY_SIZE(virtnet_stats_cvq_desc);
  3920. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_CVQ)
  3921. goto found;
  3922. offset += num;
  3923. }
  3924. if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
  3925. desc = &virtnet_stats_rx_basic_desc[0];
  3926. num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
  3927. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
  3928. goto found;
  3929. offset += num;
  3930. }
  3931. if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
  3932. desc = &virtnet_stats_rx_csum_desc[0];
  3933. num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
  3934. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
  3935. goto found;
  3936. offset += num;
  3937. }
  3938. if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
  3939. desc = &virtnet_stats_rx_speed_desc[0];
  3940. num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
  3941. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
  3942. goto found;
  3943. offset += num;
  3944. }
  3945. if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
  3946. desc = &virtnet_stats_tx_basic_desc[0];
  3947. num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
  3948. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
  3949. goto found;
  3950. offset += num;
  3951. }
  3952. if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
  3953. desc = &virtnet_stats_tx_gso_desc[0];
  3954. num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
  3955. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
  3956. goto found;
  3957. offset += num;
  3958. }
  3959. if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
  3960. desc = &virtnet_stats_tx_speed_desc[0];
  3961. num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
  3962. if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
  3963. goto found;
  3964. offset += num;
  3965. }
  3966. return;
  3967. found:
  3968. for (i = 0; i < num; ++i) {
  3969. v = (const __le64 *)(base + desc[i].offset);
  3970. ctx->data[offset + i] = le64_to_cpu(*v);
  3971. }
  3972. return;
  3973. drv_stats:
  3974. for (i = 0; i < num; ++i) {
  3975. v_stat = (const u64_stats_t *)(base + desc[i].offset);
  3976. ctx->data[offset + i] = u64_stats_read(v_stat);
  3977. }
  3978. }
  3979. static int __virtnet_get_hw_stats(struct virtnet_info *vi,
  3980. struct virtnet_stats_ctx *ctx,
  3981. struct virtio_net_ctrl_queue_stats *req,
  3982. int req_size, void *reply, int res_size)
  3983. {
  3984. struct virtio_net_stats_reply_hdr *hdr;
  3985. struct scatterlist sgs_in, sgs_out;
  3986. void *p;
  3987. u32 qid;
  3988. int ok;
  3989. sg_init_one(&sgs_out, req, req_size);
  3990. sg_init_one(&sgs_in, reply, res_size);
  3991. ok = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
  3992. VIRTIO_NET_CTRL_STATS_GET,
  3993. &sgs_out, &sgs_in);
  3994. if (!ok)
  3995. return ok;
  3996. for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
  3997. hdr = p;
  3998. qid = le16_to_cpu(hdr->vq_index);
  3999. virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type);
  4000. }
  4001. return 0;
  4002. }
  4003. static void virtnet_make_stat_req(struct virtnet_info *vi,
  4004. struct virtnet_stats_ctx *ctx,
  4005. struct virtio_net_ctrl_queue_stats *req,
  4006. int qid, int *idx)
  4007. {
  4008. int qtype = vq_type(vi, qid);
  4009. u64 bitmap = ctx->bitmap[qtype];
  4010. if (!bitmap)
  4011. return;
  4012. req->stats[*idx].vq_index = cpu_to_le16(qid);
  4013. req->stats[*idx].types_bitmap[0] = cpu_to_le64(bitmap);
  4014. *idx += 1;
  4015. }
  4016. /* qid: -1: get stats of all vq.
  4017. * > 0: get the stats for the special vq. This must not be cvq.
  4018. */
  4019. static int virtnet_get_hw_stats(struct virtnet_info *vi,
  4020. struct virtnet_stats_ctx *ctx, int qid)
  4021. {
  4022. int qnum, i, j, res_size, qtype, last_vq, first_vq;
  4023. struct virtio_net_ctrl_queue_stats *req;
  4024. bool enable_cvq;
  4025. void *reply;
  4026. int ok;
  4027. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS))
  4028. return 0;
  4029. if (qid == -1) {
  4030. last_vq = vi->curr_queue_pairs * 2 - 1;
  4031. first_vq = 0;
  4032. enable_cvq = true;
  4033. } else {
  4034. last_vq = qid;
  4035. first_vq = qid;
  4036. enable_cvq = false;
  4037. }
  4038. qnum = 0;
  4039. res_size = 0;
  4040. for (i = first_vq; i <= last_vq ; ++i) {
  4041. qtype = vq_type(vi, i);
  4042. if (ctx->bitmap[qtype]) {
  4043. ++qnum;
  4044. res_size += ctx->size[qtype];
  4045. }
  4046. }
  4047. if (enable_cvq && ctx->bitmap[VIRTNET_Q_TYPE_CQ]) {
  4048. res_size += ctx->size[VIRTNET_Q_TYPE_CQ];
  4049. qnum += 1;
  4050. }
  4051. req = kzalloc_objs(*req, qnum);
  4052. if (!req)
  4053. return -ENOMEM;
  4054. reply = kmalloc(res_size, GFP_KERNEL);
  4055. if (!reply) {
  4056. kfree(req);
  4057. return -ENOMEM;
  4058. }
  4059. j = 0;
  4060. for (i = first_vq; i <= last_vq ; ++i)
  4061. virtnet_make_stat_req(vi, ctx, req, i, &j);
  4062. if (enable_cvq)
  4063. virtnet_make_stat_req(vi, ctx, req, vi->max_queue_pairs * 2, &j);
  4064. ok = __virtnet_get_hw_stats(vi, ctx, req, sizeof(*req) * j, reply, res_size);
  4065. kfree(req);
  4066. kfree(reply);
  4067. return ok;
  4068. }
  4069. static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  4070. {
  4071. struct virtnet_info *vi = netdev_priv(dev);
  4072. unsigned int i;
  4073. u8 *p = data;
  4074. switch (stringset) {
  4075. case ETH_SS_STATS:
  4076. /* Generate the total field names. */
  4077. virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, -1, &p);
  4078. virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, -1, &p);
  4079. virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_CQ, 0, &p);
  4080. for (i = 0; i < vi->curr_queue_pairs; ++i)
  4081. virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, i, &p);
  4082. for (i = 0; i < vi->curr_queue_pairs; ++i)
  4083. virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, i, &p);
  4084. break;
  4085. }
  4086. }
  4087. static int virtnet_get_sset_count(struct net_device *dev, int sset)
  4088. {
  4089. struct virtnet_info *vi = netdev_priv(dev);
  4090. struct virtnet_stats_ctx ctx = {0};
  4091. u32 pair_count;
  4092. switch (sset) {
  4093. case ETH_SS_STATS:
  4094. virtnet_stats_ctx_init(vi, &ctx, NULL, false);
  4095. pair_count = ctx.desc_num[VIRTNET_Q_TYPE_RX] + ctx.desc_num[VIRTNET_Q_TYPE_TX];
  4096. return pair_count + ctx.desc_num[VIRTNET_Q_TYPE_CQ] +
  4097. vi->curr_queue_pairs * pair_count;
  4098. default:
  4099. return -EOPNOTSUPP;
  4100. }
  4101. }
  4102. static void virtnet_get_ethtool_stats(struct net_device *dev,
  4103. struct ethtool_stats *stats, u64 *data)
  4104. {
  4105. struct virtnet_info *vi = netdev_priv(dev);
  4106. struct virtnet_stats_ctx ctx = {0};
  4107. unsigned int start, i;
  4108. const u8 *stats_base;
  4109. virtnet_stats_ctx_init(vi, &ctx, data, false);
  4110. if (virtnet_get_hw_stats(vi, &ctx, -1))
  4111. dev_warn(&vi->dev->dev, "Failed to get hw stats.\n");
  4112. for (i = 0; i < vi->curr_queue_pairs; i++) {
  4113. struct receive_queue *rq = &vi->rq[i];
  4114. struct send_queue *sq = &vi->sq[i];
  4115. stats_base = (const u8 *)&rq->stats;
  4116. do {
  4117. start = u64_stats_fetch_begin(&rq->stats.syncp);
  4118. virtnet_fill_stats(vi, i * 2, &ctx, stats_base, true, 0);
  4119. } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
  4120. stats_base = (const u8 *)&sq->stats;
  4121. do {
  4122. start = u64_stats_fetch_begin(&sq->stats.syncp);
  4123. virtnet_fill_stats(vi, i * 2 + 1, &ctx, stats_base, true, 0);
  4124. } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
  4125. }
  4126. virtnet_fill_total_fields(vi, &ctx);
  4127. }
  4128. static void virtnet_get_channels(struct net_device *dev,
  4129. struct ethtool_channels *channels)
  4130. {
  4131. struct virtnet_info *vi = netdev_priv(dev);
  4132. channels->combined_count = vi->curr_queue_pairs;
  4133. channels->max_combined = vi->max_queue_pairs;
  4134. channels->max_other = 0;
  4135. channels->rx_count = 0;
  4136. channels->tx_count = 0;
  4137. channels->other_count = 0;
  4138. }
  4139. static int virtnet_set_link_ksettings(struct net_device *dev,
  4140. const struct ethtool_link_ksettings *cmd)
  4141. {
  4142. struct virtnet_info *vi = netdev_priv(dev);
  4143. return ethtool_virtdev_set_link_ksettings(dev, cmd,
  4144. &vi->speed, &vi->duplex);
  4145. }
  4146. static int virtnet_get_link_ksettings(struct net_device *dev,
  4147. struct ethtool_link_ksettings *cmd)
  4148. {
  4149. struct virtnet_info *vi = netdev_priv(dev);
  4150. cmd->base.speed = vi->speed;
  4151. cmd->base.duplex = vi->duplex;
  4152. cmd->base.port = PORT_OTHER;
  4153. return 0;
  4154. }
  4155. static int virtnet_send_tx_notf_coal_cmds(struct virtnet_info *vi,
  4156. struct ethtool_coalesce *ec)
  4157. {
  4158. struct virtio_net_ctrl_coal_tx *coal_tx __free(kfree) = NULL;
  4159. struct scatterlist sgs_tx;
  4160. int i;
  4161. coal_tx = kzalloc_obj(*coal_tx);
  4162. if (!coal_tx)
  4163. return -ENOMEM;
  4164. coal_tx->tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
  4165. coal_tx->tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
  4166. sg_init_one(&sgs_tx, coal_tx, sizeof(*coal_tx));
  4167. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
  4168. VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
  4169. &sgs_tx))
  4170. return -EINVAL;
  4171. vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs;
  4172. vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames;
  4173. for (i = 0; i < vi->max_queue_pairs; i++) {
  4174. vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs;
  4175. vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames;
  4176. }
  4177. return 0;
  4178. }
  4179. static int virtnet_send_rx_notf_coal_cmds(struct virtnet_info *vi,
  4180. struct ethtool_coalesce *ec)
  4181. {
  4182. struct virtio_net_ctrl_coal_rx *coal_rx __free(kfree) = NULL;
  4183. bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
  4184. struct scatterlist sgs_rx;
  4185. int i;
  4186. if (rx_ctrl_dim_on && !virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
  4187. return -EOPNOTSUPP;
  4188. if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != vi->intr_coal_rx.max_usecs ||
  4189. ec->rx_max_coalesced_frames != vi->intr_coal_rx.max_packets))
  4190. return -EINVAL;
  4191. if (rx_ctrl_dim_on && !vi->rx_dim_enabled) {
  4192. vi->rx_dim_enabled = true;
  4193. for (i = 0; i < vi->max_queue_pairs; i++) {
  4194. mutex_lock(&vi->rq[i].dim_lock);
  4195. vi->rq[i].dim_enabled = true;
  4196. mutex_unlock(&vi->rq[i].dim_lock);
  4197. }
  4198. return 0;
  4199. }
  4200. coal_rx = kzalloc_obj(*coal_rx);
  4201. if (!coal_rx)
  4202. return -ENOMEM;
  4203. if (!rx_ctrl_dim_on && vi->rx_dim_enabled) {
  4204. vi->rx_dim_enabled = false;
  4205. for (i = 0; i < vi->max_queue_pairs; i++) {
  4206. mutex_lock(&vi->rq[i].dim_lock);
  4207. vi->rq[i].dim_enabled = false;
  4208. mutex_unlock(&vi->rq[i].dim_lock);
  4209. }
  4210. }
  4211. /* Since the per-queue coalescing params can be set,
  4212. * we need apply the global new params even if they
  4213. * are not updated.
  4214. */
  4215. coal_rx->rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
  4216. coal_rx->rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
  4217. sg_init_one(&sgs_rx, coal_rx, sizeof(*coal_rx));
  4218. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
  4219. VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
  4220. &sgs_rx))
  4221. return -EINVAL;
  4222. vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs;
  4223. vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames;
  4224. for (i = 0; i < vi->max_queue_pairs; i++) {
  4225. mutex_lock(&vi->rq[i].dim_lock);
  4226. vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs;
  4227. vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames;
  4228. mutex_unlock(&vi->rq[i].dim_lock);
  4229. }
  4230. return 0;
  4231. }
  4232. static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
  4233. struct ethtool_coalesce *ec)
  4234. {
  4235. int err;
  4236. err = virtnet_send_tx_notf_coal_cmds(vi, ec);
  4237. if (err)
  4238. return err;
  4239. err = virtnet_send_rx_notf_coal_cmds(vi, ec);
  4240. if (err)
  4241. return err;
  4242. return 0;
  4243. }
  4244. static int virtnet_send_rx_notf_coal_vq_cmds(struct virtnet_info *vi,
  4245. struct ethtool_coalesce *ec,
  4246. u16 queue)
  4247. {
  4248. bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
  4249. u32 max_usecs, max_packets;
  4250. bool cur_rx_dim;
  4251. int err;
  4252. mutex_lock(&vi->rq[queue].dim_lock);
  4253. cur_rx_dim = vi->rq[queue].dim_enabled;
  4254. max_usecs = vi->rq[queue].intr_coal.max_usecs;
  4255. max_packets = vi->rq[queue].intr_coal.max_packets;
  4256. if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != max_usecs ||
  4257. ec->rx_max_coalesced_frames != max_packets)) {
  4258. mutex_unlock(&vi->rq[queue].dim_lock);
  4259. return -EINVAL;
  4260. }
  4261. if (rx_ctrl_dim_on && !cur_rx_dim) {
  4262. vi->rq[queue].dim_enabled = true;
  4263. mutex_unlock(&vi->rq[queue].dim_lock);
  4264. return 0;
  4265. }
  4266. if (!rx_ctrl_dim_on && cur_rx_dim)
  4267. vi->rq[queue].dim_enabled = false;
  4268. /* If no params are updated, userspace ethtool will
  4269. * reject the modification.
  4270. */
  4271. err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, queue,
  4272. ec->rx_coalesce_usecs,
  4273. ec->rx_max_coalesced_frames);
  4274. mutex_unlock(&vi->rq[queue].dim_lock);
  4275. return err;
  4276. }
  4277. static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
  4278. struct ethtool_coalesce *ec,
  4279. u16 queue)
  4280. {
  4281. int err;
  4282. err = virtnet_send_rx_notf_coal_vq_cmds(vi, ec, queue);
  4283. if (err)
  4284. return err;
  4285. err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, queue,
  4286. ec->tx_coalesce_usecs,
  4287. ec->tx_max_coalesced_frames);
  4288. if (err)
  4289. return err;
  4290. return 0;
  4291. }
  4292. static void virtnet_rx_dim_work(struct work_struct *work)
  4293. {
  4294. struct dim *dim = container_of(work, struct dim, work);
  4295. struct receive_queue *rq = container_of(dim,
  4296. struct receive_queue, dim);
  4297. struct virtnet_info *vi = rq->vq->vdev->priv;
  4298. struct net_device *dev = vi->dev;
  4299. struct dim_cq_moder update_moder;
  4300. int qnum, err;
  4301. qnum = rq - vi->rq;
  4302. mutex_lock(&rq->dim_lock);
  4303. if (!rq->dim_enabled)
  4304. goto out;
  4305. update_moder = net_dim_get_rx_irq_moder(dev, dim);
  4306. if (update_moder.usec != rq->intr_coal.max_usecs ||
  4307. update_moder.pkts != rq->intr_coal.max_packets) {
  4308. err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, qnum,
  4309. update_moder.usec,
  4310. update_moder.pkts);
  4311. if (err)
  4312. pr_debug("%s: Failed to send dim parameters on rxq%d\n",
  4313. dev->name, qnum);
  4314. }
  4315. out:
  4316. dim->state = DIM_START_MEASURE;
  4317. mutex_unlock(&rq->dim_lock);
  4318. }
  4319. static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
  4320. {
  4321. /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
  4322. * or VIRTIO_NET_F_VQ_NOTF_COAL feature is negotiated.
  4323. */
  4324. if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
  4325. return -EOPNOTSUPP;
  4326. if (ec->tx_max_coalesced_frames > 1 ||
  4327. ec->rx_max_coalesced_frames != 1)
  4328. return -EINVAL;
  4329. return 0;
  4330. }
  4331. static int virtnet_should_update_vq_weight(int dev_flags, int weight,
  4332. int vq_weight, bool *should_update)
  4333. {
  4334. if (weight ^ vq_weight) {
  4335. if (dev_flags & IFF_UP)
  4336. return -EBUSY;
  4337. *should_update = true;
  4338. }
  4339. return 0;
  4340. }
  4341. static int virtnet_set_coalesce(struct net_device *dev,
  4342. struct ethtool_coalesce *ec,
  4343. struct kernel_ethtool_coalesce *kernel_coal,
  4344. struct netlink_ext_ack *extack)
  4345. {
  4346. struct virtnet_info *vi = netdev_priv(dev);
  4347. int ret, queue_number, napi_weight, i;
  4348. bool update_napi = false;
  4349. /* Can't change NAPI weight if the link is up */
  4350. napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
  4351. for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) {
  4352. ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
  4353. vi->sq[queue_number].napi.weight,
  4354. &update_napi);
  4355. if (ret)
  4356. return ret;
  4357. if (update_napi) {
  4358. /* All queues that belong to [queue_number, vi->max_queue_pairs] will be
  4359. * updated for the sake of simplicity, which might not be necessary
  4360. */
  4361. break;
  4362. }
  4363. }
  4364. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
  4365. ret = virtnet_send_notf_coal_cmds(vi, ec);
  4366. else
  4367. ret = virtnet_coal_params_supported(ec);
  4368. if (ret)
  4369. return ret;
  4370. if (update_napi) {
  4371. /* xsk xmit depends on the tx napi. So if xsk is active,
  4372. * prevent modifications to tx napi.
  4373. */
  4374. for (i = queue_number; i < vi->max_queue_pairs; i++) {
  4375. if (vi->sq[i].xsk_pool)
  4376. return -EBUSY;
  4377. }
  4378. for (; queue_number < vi->max_queue_pairs; queue_number++)
  4379. vi->sq[queue_number].napi.weight = napi_weight;
  4380. }
  4381. return ret;
  4382. }
  4383. static int virtnet_get_coalesce(struct net_device *dev,
  4384. struct ethtool_coalesce *ec,
  4385. struct kernel_ethtool_coalesce *kernel_coal,
  4386. struct netlink_ext_ack *extack)
  4387. {
  4388. struct virtnet_info *vi = netdev_priv(dev);
  4389. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
  4390. ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
  4391. ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
  4392. ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
  4393. ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
  4394. ec->use_adaptive_rx_coalesce = vi->rx_dim_enabled;
  4395. } else {
  4396. ec->rx_max_coalesced_frames = 1;
  4397. if (vi->sq[0].napi.weight)
  4398. ec->tx_max_coalesced_frames = 1;
  4399. }
  4400. return 0;
  4401. }
  4402. static int virtnet_set_per_queue_coalesce(struct net_device *dev,
  4403. u32 queue,
  4404. struct ethtool_coalesce *ec)
  4405. {
  4406. struct virtnet_info *vi = netdev_priv(dev);
  4407. int ret, napi_weight;
  4408. bool update_napi = false;
  4409. if (queue >= vi->max_queue_pairs)
  4410. return -EINVAL;
  4411. /* Can't change NAPI weight if the link is up */
  4412. napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
  4413. ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
  4414. vi->sq[queue].napi.weight,
  4415. &update_napi);
  4416. if (ret)
  4417. return ret;
  4418. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
  4419. ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
  4420. else
  4421. ret = virtnet_coal_params_supported(ec);
  4422. if (ret)
  4423. return ret;
  4424. if (update_napi)
  4425. vi->sq[queue].napi.weight = napi_weight;
  4426. return 0;
  4427. }
  4428. static int virtnet_get_per_queue_coalesce(struct net_device *dev,
  4429. u32 queue,
  4430. struct ethtool_coalesce *ec)
  4431. {
  4432. struct virtnet_info *vi = netdev_priv(dev);
  4433. if (queue >= vi->max_queue_pairs)
  4434. return -EINVAL;
  4435. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
  4436. mutex_lock(&vi->rq[queue].dim_lock);
  4437. ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
  4438. ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
  4439. ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
  4440. ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
  4441. ec->use_adaptive_rx_coalesce = vi->rq[queue].dim_enabled;
  4442. mutex_unlock(&vi->rq[queue].dim_lock);
  4443. } else {
  4444. ec->rx_max_coalesced_frames = 1;
  4445. if (vi->sq[queue].napi.weight)
  4446. ec->tx_max_coalesced_frames = 1;
  4447. }
  4448. return 0;
  4449. }
  4450. static void virtnet_init_settings(struct net_device *dev)
  4451. {
  4452. struct virtnet_info *vi = netdev_priv(dev);
  4453. vi->speed = SPEED_UNKNOWN;
  4454. vi->duplex = DUPLEX_UNKNOWN;
  4455. }
  4456. static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
  4457. {
  4458. return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
  4459. }
  4460. static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
  4461. {
  4462. return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
  4463. }
  4464. static int virtnet_get_rxfh(struct net_device *dev,
  4465. struct ethtool_rxfh_param *rxfh)
  4466. {
  4467. struct virtnet_info *vi = netdev_priv(dev);
  4468. int i;
  4469. if (rxfh->indir) {
  4470. for (i = 0; i < vi->rss_indir_table_size; ++i)
  4471. rxfh->indir[i] = le16_to_cpu(vi->rss_hdr->indirection_table[i]);
  4472. }
  4473. if (rxfh->key)
  4474. memcpy(rxfh->key, vi->rss_hash_key_data, vi->rss_key_size);
  4475. rxfh->hfunc = ETH_RSS_HASH_TOP;
  4476. return 0;
  4477. }
  4478. static int virtnet_set_rxfh(struct net_device *dev,
  4479. struct ethtool_rxfh_param *rxfh,
  4480. struct netlink_ext_ack *extack)
  4481. {
  4482. struct virtnet_info *vi = netdev_priv(dev);
  4483. bool update = false;
  4484. int i;
  4485. if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
  4486. rxfh->hfunc != ETH_RSS_HASH_TOP)
  4487. return -EOPNOTSUPP;
  4488. if (rxfh->indir) {
  4489. if (!vi->has_rss)
  4490. return -EOPNOTSUPP;
  4491. for (i = 0; i < vi->rss_indir_table_size; ++i)
  4492. vi->rss_hdr->indirection_table[i] = cpu_to_le16(rxfh->indir[i]);
  4493. update = true;
  4494. }
  4495. if (rxfh->key) {
  4496. /* If either _F_HASH_REPORT or _F_RSS are negotiated, the
  4497. * device provides hash calculation capabilities, that is,
  4498. * hash_key is configured.
  4499. */
  4500. if (!vi->has_rss && !vi->has_rss_hash_report)
  4501. return -EOPNOTSUPP;
  4502. memcpy(vi->rss_hash_key_data, rxfh->key, vi->rss_key_size);
  4503. update = true;
  4504. }
  4505. if (update)
  4506. virtnet_commit_rss_command(vi);
  4507. return 0;
  4508. }
  4509. static u32 virtnet_get_rx_ring_count(struct net_device *dev)
  4510. {
  4511. struct virtnet_info *vi = netdev_priv(dev);
  4512. return vi->curr_queue_pairs;
  4513. }
  4514. static const struct ethtool_ops virtnet_ethtool_ops = {
  4515. .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
  4516. ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
  4517. .get_drvinfo = virtnet_get_drvinfo,
  4518. .get_link = ethtool_op_get_link,
  4519. .get_ringparam = virtnet_get_ringparam,
  4520. .set_ringparam = virtnet_set_ringparam,
  4521. .get_strings = virtnet_get_strings,
  4522. .get_sset_count = virtnet_get_sset_count,
  4523. .get_ethtool_stats = virtnet_get_ethtool_stats,
  4524. .set_channels = virtnet_set_channels,
  4525. .get_channels = virtnet_get_channels,
  4526. .get_ts_info = ethtool_op_get_ts_info,
  4527. .get_link_ksettings = virtnet_get_link_ksettings,
  4528. .set_link_ksettings = virtnet_set_link_ksettings,
  4529. .set_coalesce = virtnet_set_coalesce,
  4530. .get_coalesce = virtnet_get_coalesce,
  4531. .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
  4532. .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
  4533. .get_rxfh_key_size = virtnet_get_rxfh_key_size,
  4534. .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
  4535. .get_rxfh = virtnet_get_rxfh,
  4536. .set_rxfh = virtnet_set_rxfh,
  4537. .get_rxfh_fields = virtnet_get_hashflow,
  4538. .set_rxfh_fields = virtnet_set_hashflow,
  4539. .get_rx_ring_count = virtnet_get_rx_ring_count,
  4540. };
  4541. static void virtnet_get_queue_stats_rx(struct net_device *dev, int i,
  4542. struct netdev_queue_stats_rx *stats)
  4543. {
  4544. struct virtnet_info *vi = netdev_priv(dev);
  4545. struct receive_queue *rq = &vi->rq[i];
  4546. struct virtnet_stats_ctx ctx = {0};
  4547. virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
  4548. virtnet_get_hw_stats(vi, &ctx, i * 2);
  4549. virtnet_fill_stats(vi, i * 2, &ctx, (void *)&rq->stats, true, 0);
  4550. }
  4551. static void virtnet_get_queue_stats_tx(struct net_device *dev, int i,
  4552. struct netdev_queue_stats_tx *stats)
  4553. {
  4554. struct virtnet_info *vi = netdev_priv(dev);
  4555. struct send_queue *sq = &vi->sq[i];
  4556. struct virtnet_stats_ctx ctx = {0};
  4557. virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
  4558. virtnet_get_hw_stats(vi, &ctx, i * 2 + 1);
  4559. virtnet_fill_stats(vi, i * 2 + 1, &ctx, (void *)&sq->stats, true, 0);
  4560. }
  4561. static void virtnet_get_base_stats(struct net_device *dev,
  4562. struct netdev_queue_stats_rx *rx,
  4563. struct netdev_queue_stats_tx *tx)
  4564. {
  4565. struct virtnet_info *vi = netdev_priv(dev);
  4566. /* The queue stats of the virtio-net will not be reset. So here we
  4567. * return 0.
  4568. */
  4569. rx->bytes = 0;
  4570. rx->packets = 0;
  4571. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
  4572. rx->hw_drops = 0;
  4573. rx->hw_drop_overruns = 0;
  4574. }
  4575. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
  4576. rx->csum_unnecessary = 0;
  4577. rx->csum_none = 0;
  4578. rx->csum_bad = 0;
  4579. }
  4580. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
  4581. rx->hw_gro_packets = 0;
  4582. rx->hw_gro_bytes = 0;
  4583. rx->hw_gro_wire_packets = 0;
  4584. rx->hw_gro_wire_bytes = 0;
  4585. }
  4586. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED)
  4587. rx->hw_drop_ratelimits = 0;
  4588. tx->bytes = 0;
  4589. tx->packets = 0;
  4590. tx->stop = 0;
  4591. tx->wake = 0;
  4592. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
  4593. tx->hw_drops = 0;
  4594. tx->hw_drop_errors = 0;
  4595. }
  4596. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
  4597. tx->csum_none = 0;
  4598. tx->needs_csum = 0;
  4599. }
  4600. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
  4601. tx->hw_gso_packets = 0;
  4602. tx->hw_gso_bytes = 0;
  4603. tx->hw_gso_wire_packets = 0;
  4604. tx->hw_gso_wire_bytes = 0;
  4605. }
  4606. if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED)
  4607. tx->hw_drop_ratelimits = 0;
  4608. netdev_stat_queue_sum(dev,
  4609. dev->real_num_rx_queues, vi->max_queue_pairs, rx,
  4610. dev->real_num_tx_queues, vi->max_queue_pairs, tx);
  4611. }
  4612. static const struct netdev_stat_ops virtnet_stat_ops = {
  4613. .get_queue_stats_rx = virtnet_get_queue_stats_rx,
  4614. .get_queue_stats_tx = virtnet_get_queue_stats_tx,
  4615. .get_base_stats = virtnet_get_base_stats,
  4616. };
  4617. static void virtnet_freeze_down(struct virtio_device *vdev)
  4618. {
  4619. struct virtnet_info *vi = vdev->priv;
  4620. /* Make sure no work handler is accessing the device */
  4621. flush_work(&vi->config_work);
  4622. disable_rx_mode_work(vi);
  4623. flush_work(&vi->rx_mode_work);
  4624. if (netif_running(vi->dev)) {
  4625. rtnl_lock();
  4626. virtnet_close(vi->dev);
  4627. rtnl_unlock();
  4628. }
  4629. netif_tx_lock_bh(vi->dev);
  4630. netif_device_detach(vi->dev);
  4631. netif_tx_unlock_bh(vi->dev);
  4632. }
  4633. static int init_vqs(struct virtnet_info *vi);
  4634. static int virtnet_restore_up(struct virtio_device *vdev)
  4635. {
  4636. struct virtnet_info *vi = vdev->priv;
  4637. int err;
  4638. err = init_vqs(vi);
  4639. if (err)
  4640. return err;
  4641. virtio_device_ready(vdev);
  4642. enable_rx_mode_work(vi);
  4643. if (netif_running(vi->dev)) {
  4644. rtnl_lock();
  4645. err = virtnet_open(vi->dev);
  4646. rtnl_unlock();
  4647. if (err)
  4648. return err;
  4649. }
  4650. netif_tx_lock_bh(vi->dev);
  4651. netif_device_attach(vi->dev);
  4652. netif_tx_unlock_bh(vi->dev);
  4653. return err;
  4654. }
  4655. static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
  4656. {
  4657. __virtio64 *_offloads __free(kfree) = NULL;
  4658. struct scatterlist sg;
  4659. _offloads = kzalloc_obj(*_offloads);
  4660. if (!_offloads)
  4661. return -ENOMEM;
  4662. *_offloads = cpu_to_virtio64(vi->vdev, offloads);
  4663. sg_init_one(&sg, _offloads, sizeof(*_offloads));
  4664. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
  4665. VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
  4666. dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
  4667. return -EINVAL;
  4668. }
  4669. return 0;
  4670. }
  4671. static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
  4672. {
  4673. u64 offloads = 0;
  4674. if (!vi->guest_offloads)
  4675. return 0;
  4676. return virtnet_set_guest_offloads(vi, offloads);
  4677. }
  4678. static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
  4679. {
  4680. u64 offloads = vi->guest_offloads;
  4681. if (!vi->guest_offloads)
  4682. return 0;
  4683. return virtnet_set_guest_offloads(vi, offloads);
  4684. }
  4685. static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queue *rq,
  4686. struct xsk_buff_pool *pool)
  4687. {
  4688. int err, qindex;
  4689. qindex = rq - vi->rq;
  4690. if (pool) {
  4691. err = xdp_rxq_info_reg(&rq->xsk_rxq_info, vi->dev, qindex, rq->napi.napi_id);
  4692. if (err < 0)
  4693. return err;
  4694. err = xdp_rxq_info_reg_mem_model(&rq->xsk_rxq_info,
  4695. MEM_TYPE_XSK_BUFF_POOL, NULL);
  4696. if (err < 0)
  4697. goto unreg;
  4698. xsk_pool_set_rxq_info(pool, &rq->xsk_rxq_info);
  4699. }
  4700. virtnet_rx_pause(vi, rq);
  4701. err = virtqueue_reset(rq->vq, virtnet_rq_unmap_free_buf, NULL);
  4702. if (err) {
  4703. netdev_err(vi->dev, "reset rx fail: rx queue index: %d err: %d\n", qindex, err);
  4704. pool = NULL;
  4705. }
  4706. rq->xsk_pool = pool;
  4707. virtnet_rx_resume(vi, rq, true);
  4708. if (pool)
  4709. return 0;
  4710. unreg:
  4711. xdp_rxq_info_unreg(&rq->xsk_rxq_info);
  4712. return err;
  4713. }
  4714. static int virtnet_sq_bind_xsk_pool(struct virtnet_info *vi,
  4715. struct send_queue *sq,
  4716. struct xsk_buff_pool *pool)
  4717. {
  4718. int err, qindex;
  4719. qindex = sq - vi->sq;
  4720. virtnet_tx_pause(vi, sq);
  4721. err = virtqueue_reset(sq->vq, virtnet_sq_free_unused_buf,
  4722. virtnet_sq_free_unused_buf_done);
  4723. if (err) {
  4724. netdev_err(vi->dev, "reset tx fail: tx queue index: %d err: %d\n", qindex, err);
  4725. pool = NULL;
  4726. }
  4727. sq->xsk_pool = pool;
  4728. virtnet_tx_resume(vi, sq);
  4729. return err;
  4730. }
  4731. static int virtnet_xsk_pool_enable(struct net_device *dev,
  4732. struct xsk_buff_pool *pool,
  4733. u16 qid)
  4734. {
  4735. struct virtnet_info *vi = netdev_priv(dev);
  4736. struct receive_queue *rq;
  4737. struct device *dma_dev;
  4738. struct send_queue *sq;
  4739. dma_addr_t hdr_dma;
  4740. int err, size;
  4741. if (vi->hdr_len > xsk_pool_get_headroom(pool))
  4742. return -EINVAL;
  4743. /* In big_packets mode, xdp cannot work, so there is no need to
  4744. * initialize xsk of rq.
  4745. */
  4746. if (vi->big_packets && !vi->mergeable_rx_bufs)
  4747. return -ENOENT;
  4748. if (qid >= vi->curr_queue_pairs)
  4749. return -EINVAL;
  4750. sq = &vi->sq[qid];
  4751. rq = &vi->rq[qid];
  4752. /* xsk assumes that tx and rx must have the same dma device. The af-xdp
  4753. * may use one buffer to receive from the rx and reuse this buffer to
  4754. * send by the tx. So the dma dev of sq and rq must be the same one.
  4755. *
  4756. * But vq->dma_dev allows every vq has the respective dma dev. So I
  4757. * check the dma dev of vq and sq is the same dev.
  4758. */
  4759. if (virtqueue_dma_dev(rq->vq) != virtqueue_dma_dev(sq->vq))
  4760. return -EINVAL;
  4761. dma_dev = virtqueue_dma_dev(rq->vq);
  4762. if (!dma_dev)
  4763. return -EINVAL;
  4764. size = virtqueue_get_vring_size(rq->vq);
  4765. rq->xsk_buffs = kvzalloc_objs(*rq->xsk_buffs, size);
  4766. if (!rq->xsk_buffs)
  4767. return -ENOMEM;
  4768. hdr_dma = virtqueue_map_single_attrs(sq->vq, &xsk_hdr, vi->hdr_len,
  4769. DMA_TO_DEVICE, 0);
  4770. if (virtqueue_map_mapping_error(sq->vq, hdr_dma)) {
  4771. err = -ENOMEM;
  4772. goto err_free_buffs;
  4773. }
  4774. err = xsk_pool_dma_map(pool, dma_dev, 0);
  4775. if (err)
  4776. goto err_xsk_map;
  4777. err = virtnet_rq_bind_xsk_pool(vi, rq, pool);
  4778. if (err)
  4779. goto err_rq;
  4780. err = virtnet_sq_bind_xsk_pool(vi, sq, pool);
  4781. if (err)
  4782. goto err_sq;
  4783. /* Now, we do not support tx offload(such as tx csum), so all the tx
  4784. * virtnet hdr is zero. So all the tx packets can share a single hdr.
  4785. */
  4786. sq->xsk_hdr_dma_addr = hdr_dma;
  4787. return 0;
  4788. err_sq:
  4789. virtnet_rq_bind_xsk_pool(vi, rq, NULL);
  4790. err_rq:
  4791. xsk_pool_dma_unmap(pool, 0);
  4792. err_xsk_map:
  4793. virtqueue_unmap_single_attrs(rq->vq, hdr_dma, vi->hdr_len,
  4794. DMA_TO_DEVICE, 0);
  4795. err_free_buffs:
  4796. kvfree(rq->xsk_buffs);
  4797. return err;
  4798. }
  4799. static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)
  4800. {
  4801. struct virtnet_info *vi = netdev_priv(dev);
  4802. struct xsk_buff_pool *pool;
  4803. struct receive_queue *rq;
  4804. struct send_queue *sq;
  4805. int err;
  4806. if (qid >= vi->curr_queue_pairs)
  4807. return -EINVAL;
  4808. sq = &vi->sq[qid];
  4809. rq = &vi->rq[qid];
  4810. pool = rq->xsk_pool;
  4811. err = virtnet_rq_bind_xsk_pool(vi, rq, NULL);
  4812. err |= virtnet_sq_bind_xsk_pool(vi, sq, NULL);
  4813. xsk_pool_dma_unmap(pool, 0);
  4814. virtqueue_unmap_single_attrs(sq->vq, sq->xsk_hdr_dma_addr,
  4815. vi->hdr_len, DMA_TO_DEVICE, 0);
  4816. kvfree(rq->xsk_buffs);
  4817. return err;
  4818. }
  4819. static int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp)
  4820. {
  4821. if (xdp->xsk.pool)
  4822. return virtnet_xsk_pool_enable(dev, xdp->xsk.pool,
  4823. xdp->xsk.queue_id);
  4824. else
  4825. return virtnet_xsk_pool_disable(dev, xdp->xsk.queue_id);
  4826. }
  4827. static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  4828. struct netlink_ext_ack *extack)
  4829. {
  4830. unsigned int room = SKB_DATA_ALIGN(XDP_PACKET_HEADROOM +
  4831. sizeof(struct skb_shared_info));
  4832. unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
  4833. struct virtnet_info *vi = netdev_priv(dev);
  4834. struct bpf_prog *old_prog;
  4835. u16 xdp_qp = 0, curr_qp;
  4836. int i, err;
  4837. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
  4838. && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  4839. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  4840. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
  4841. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
  4842. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
  4843. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
  4844. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
  4845. NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
  4846. return -EOPNOTSUPP;
  4847. }
  4848. if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
  4849. NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
  4850. return -EINVAL;
  4851. }
  4852. if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
  4853. NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
  4854. netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
  4855. return -EINVAL;
  4856. }
  4857. curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
  4858. if (prog)
  4859. xdp_qp = nr_cpu_ids;
  4860. /* XDP requires extra queues for XDP_TX */
  4861. if (curr_qp + xdp_qp > vi->max_queue_pairs) {
  4862. 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",
  4863. curr_qp + xdp_qp, vi->max_queue_pairs);
  4864. xdp_qp = 0;
  4865. }
  4866. old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
  4867. if (!prog && !old_prog)
  4868. return 0;
  4869. if (prog)
  4870. bpf_prog_add(prog, vi->max_queue_pairs - 1);
  4871. virtnet_rx_pause_all(vi);
  4872. /* Make sure NAPI is not using any XDP TX queues for RX. */
  4873. if (netif_running(dev)) {
  4874. for (i = 0; i < vi->max_queue_pairs; i++)
  4875. virtnet_napi_tx_disable(&vi->sq[i]);
  4876. }
  4877. if (!prog) {
  4878. for (i = 0; i < vi->max_queue_pairs; i++) {
  4879. rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
  4880. if (i == 0)
  4881. virtnet_restore_guest_offloads(vi);
  4882. }
  4883. synchronize_net();
  4884. }
  4885. err = virtnet_set_queues(vi, curr_qp + xdp_qp);
  4886. if (err)
  4887. goto err;
  4888. netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
  4889. vi->xdp_queue_pairs = xdp_qp;
  4890. if (prog) {
  4891. vi->xdp_enabled = true;
  4892. for (i = 0; i < vi->max_queue_pairs; i++) {
  4893. rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
  4894. if (i == 0 && !old_prog)
  4895. virtnet_clear_guest_offloads(vi);
  4896. }
  4897. if (!old_prog)
  4898. xdp_features_set_redirect_target(dev, true);
  4899. } else {
  4900. xdp_features_clear_redirect_target(dev);
  4901. vi->xdp_enabled = false;
  4902. }
  4903. virtnet_rx_resume_all(vi);
  4904. for (i = 0; i < vi->max_queue_pairs; i++) {
  4905. if (old_prog)
  4906. bpf_prog_put(old_prog);
  4907. if (netif_running(dev))
  4908. virtnet_napi_tx_enable(&vi->sq[i]);
  4909. }
  4910. return 0;
  4911. err:
  4912. if (!prog) {
  4913. virtnet_clear_guest_offloads(vi);
  4914. for (i = 0; i < vi->max_queue_pairs; i++)
  4915. rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
  4916. }
  4917. virtnet_rx_resume_all(vi);
  4918. if (netif_running(dev)) {
  4919. for (i = 0; i < vi->max_queue_pairs; i++)
  4920. virtnet_napi_tx_enable(&vi->sq[i]);
  4921. }
  4922. if (prog)
  4923. bpf_prog_sub(prog, vi->max_queue_pairs - 1);
  4924. return err;
  4925. }
  4926. static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  4927. {
  4928. switch (xdp->command) {
  4929. case XDP_SETUP_PROG:
  4930. return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
  4931. case XDP_SETUP_XSK_POOL:
  4932. return virtnet_xsk_pool_setup(dev, xdp);
  4933. default:
  4934. return -EINVAL;
  4935. }
  4936. }
  4937. static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
  4938. size_t len)
  4939. {
  4940. struct virtnet_info *vi = netdev_priv(dev);
  4941. int ret;
  4942. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
  4943. return -EOPNOTSUPP;
  4944. ret = snprintf(buf, len, "sby");
  4945. if (ret >= len)
  4946. return -EOPNOTSUPP;
  4947. return 0;
  4948. }
  4949. static int virtnet_set_features(struct net_device *dev,
  4950. netdev_features_t features)
  4951. {
  4952. struct virtnet_info *vi = netdev_priv(dev);
  4953. u64 offloads;
  4954. int err;
  4955. if ((dev->features ^ features) & NETIF_F_GRO_HW) {
  4956. if (vi->xdp_enabled)
  4957. return -EBUSY;
  4958. if (features & NETIF_F_GRO_HW)
  4959. offloads = vi->guest_offloads_capable;
  4960. else
  4961. offloads = vi->guest_offloads_capable &
  4962. ~GUEST_OFFLOAD_GRO_HW_MASK;
  4963. err = virtnet_set_guest_offloads(vi, offloads);
  4964. if (err)
  4965. return err;
  4966. vi->guest_offloads = offloads;
  4967. }
  4968. if ((dev->features ^ features) & NETIF_F_RXHASH) {
  4969. if (features & NETIF_F_RXHASH)
  4970. vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved);
  4971. else
  4972. vi->rss_hdr->hash_types = cpu_to_le32(VIRTIO_NET_HASH_REPORT_NONE);
  4973. if (!virtnet_commit_rss_command(vi))
  4974. return -EINVAL;
  4975. }
  4976. return 0;
  4977. }
  4978. static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
  4979. {
  4980. struct virtnet_info *priv = netdev_priv(dev);
  4981. struct send_queue *sq = &priv->sq[txqueue];
  4982. struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
  4983. u64_stats_update_begin(&sq->stats.syncp);
  4984. u64_stats_inc(&sq->stats.tx_timeouts);
  4985. u64_stats_update_end(&sq->stats.syncp);
  4986. netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
  4987. txqueue, sq->name, sq->vq->index, sq->vq->name,
  4988. jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
  4989. }
  4990. static int virtnet_init_irq_moder(struct virtnet_info *vi)
  4991. {
  4992. u8 profile_flags = 0, coal_flags = 0;
  4993. int ret, i;
  4994. profile_flags |= DIM_PROFILE_RX;
  4995. coal_flags |= DIM_COALESCE_USEC | DIM_COALESCE_PKTS;
  4996. ret = net_dim_init_irq_moder(vi->dev, profile_flags, coal_flags,
  4997. DIM_CQ_PERIOD_MODE_START_FROM_EQE,
  4998. 0, virtnet_rx_dim_work, NULL);
  4999. if (ret)
  5000. return ret;
  5001. for (i = 0; i < vi->max_queue_pairs; i++)
  5002. net_dim_setting(vi->dev, &vi->rq[i].dim, false);
  5003. return 0;
  5004. }
  5005. static void virtnet_free_irq_moder(struct virtnet_info *vi)
  5006. {
  5007. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
  5008. return;
  5009. rtnl_lock();
  5010. net_dim_free_irq_moder(vi->dev);
  5011. rtnl_unlock();
  5012. }
  5013. static const struct net_device_ops virtnet_netdev = {
  5014. .ndo_open = virtnet_open,
  5015. .ndo_stop = virtnet_close,
  5016. .ndo_start_xmit = start_xmit,
  5017. .ndo_validate_addr = eth_validate_addr,
  5018. .ndo_set_mac_address = virtnet_set_mac_address,
  5019. .ndo_set_rx_mode = virtnet_set_rx_mode,
  5020. .ndo_get_stats64 = virtnet_stats,
  5021. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  5022. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  5023. .ndo_bpf = virtnet_xdp,
  5024. .ndo_xdp_xmit = virtnet_xdp_xmit,
  5025. .ndo_xsk_wakeup = virtnet_xsk_wakeup,
  5026. .ndo_features_check = passthru_features_check,
  5027. .ndo_get_phys_port_name = virtnet_get_phys_port_name,
  5028. .ndo_set_features = virtnet_set_features,
  5029. .ndo_tx_timeout = virtnet_tx_timeout,
  5030. };
  5031. static void virtnet_config_changed_work(struct work_struct *work)
  5032. {
  5033. struct virtnet_info *vi =
  5034. container_of(work, struct virtnet_info, config_work);
  5035. u16 v;
  5036. if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
  5037. struct virtio_net_config, status, &v) < 0)
  5038. return;
  5039. if (v & VIRTIO_NET_S_ANNOUNCE) {
  5040. netdev_notify_peers(vi->dev);
  5041. virtnet_ack_link_announce(vi);
  5042. }
  5043. /* Ignore unknown (future) status bits */
  5044. v &= VIRTIO_NET_S_LINK_UP;
  5045. if (vi->status == v)
  5046. return;
  5047. vi->status = v;
  5048. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  5049. virtnet_update_settings(vi);
  5050. netif_carrier_on(vi->dev);
  5051. netif_tx_wake_all_queues(vi->dev);
  5052. } else {
  5053. netif_carrier_off(vi->dev);
  5054. netif_tx_stop_all_queues(vi->dev);
  5055. }
  5056. }
  5057. static void virtnet_config_changed(struct virtio_device *vdev)
  5058. {
  5059. struct virtnet_info *vi = vdev->priv;
  5060. schedule_work(&vi->config_work);
  5061. }
  5062. static void virtnet_free_queues(struct virtnet_info *vi)
  5063. {
  5064. int i;
  5065. for (i = 0; i < vi->max_queue_pairs; i++) {
  5066. __netif_napi_del(&vi->rq[i].napi);
  5067. __netif_napi_del(&vi->sq[i].napi);
  5068. }
  5069. /* We called __netif_napi_del(),
  5070. * we need to respect an RCU grace period before freeing vi->rq
  5071. */
  5072. synchronize_net();
  5073. kfree(vi->rq);
  5074. kfree(vi->sq);
  5075. kfree(vi->ctrl);
  5076. }
  5077. static void _free_receive_bufs(struct virtnet_info *vi)
  5078. {
  5079. struct bpf_prog *old_prog;
  5080. int i;
  5081. for (i = 0; i < vi->max_queue_pairs; i++) {
  5082. while (vi->rq[i].pages)
  5083. __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
  5084. old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  5085. RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
  5086. if (old_prog)
  5087. bpf_prog_put(old_prog);
  5088. }
  5089. }
  5090. static void free_receive_bufs(struct virtnet_info *vi)
  5091. {
  5092. rtnl_lock();
  5093. _free_receive_bufs(vi);
  5094. rtnl_unlock();
  5095. }
  5096. static void free_receive_page_frags(struct virtnet_info *vi)
  5097. {
  5098. int i;
  5099. for (i = 0; i < vi->max_queue_pairs; i++)
  5100. if (vi->rq[i].alloc_frag.page) {
  5101. if (vi->rq[i].last_dma)
  5102. virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0);
  5103. put_page(vi->rq[i].alloc_frag.page);
  5104. }
  5105. }
  5106. static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
  5107. {
  5108. struct virtnet_info *vi = vq->vdev->priv;
  5109. struct send_queue *sq;
  5110. int i = vq2txq(vq);
  5111. sq = &vi->sq[i];
  5112. switch (virtnet_xmit_ptr_unpack(&buf)) {
  5113. case VIRTNET_XMIT_TYPE_SKB:
  5114. case VIRTNET_XMIT_TYPE_SKB_ORPHAN:
  5115. dev_kfree_skb(buf);
  5116. break;
  5117. case VIRTNET_XMIT_TYPE_XDP:
  5118. xdp_return_frame(buf);
  5119. break;
  5120. case VIRTNET_XMIT_TYPE_XSK:
  5121. xsk_tx_completed(sq->xsk_pool, 1);
  5122. break;
  5123. }
  5124. }
  5125. static void virtnet_sq_free_unused_buf_done(struct virtqueue *vq)
  5126. {
  5127. struct virtnet_info *vi = vq->vdev->priv;
  5128. int i = vq2txq(vq);
  5129. netdev_tx_reset_queue(netdev_get_tx_queue(vi->dev, i));
  5130. }
  5131. static void free_unused_bufs(struct virtnet_info *vi)
  5132. {
  5133. void *buf;
  5134. int i;
  5135. for (i = 0; i < vi->max_queue_pairs; i++) {
  5136. struct virtqueue *vq = vi->sq[i].vq;
  5137. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
  5138. virtnet_sq_free_unused_buf(vq, buf);
  5139. cond_resched();
  5140. }
  5141. for (i = 0; i < vi->max_queue_pairs; i++) {
  5142. struct virtqueue *vq = vi->rq[i].vq;
  5143. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
  5144. virtnet_rq_unmap_free_buf(vq, buf);
  5145. cond_resched();
  5146. }
  5147. }
  5148. static void virtnet_del_vqs(struct virtnet_info *vi)
  5149. {
  5150. struct virtio_device *vdev = vi->vdev;
  5151. virtnet_clean_affinity(vi);
  5152. vdev->config->del_vqs(vdev);
  5153. virtnet_free_queues(vi);
  5154. }
  5155. /* How large should a single buffer be so a queue full of these can fit at
  5156. * least one full packet?
  5157. * Logic below assumes the mergeable buffer header is used.
  5158. */
  5159. static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
  5160. {
  5161. const unsigned int hdr_len = vi->hdr_len;
  5162. unsigned int rq_size = virtqueue_get_vring_size(vq);
  5163. unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
  5164. unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
  5165. unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
  5166. return max(max(min_buf_len, hdr_len) - hdr_len,
  5167. (unsigned int)GOOD_PACKET_LEN);
  5168. }
  5169. static int virtnet_find_vqs(struct virtnet_info *vi)
  5170. {
  5171. struct virtqueue_info *vqs_info;
  5172. struct virtqueue **vqs;
  5173. int ret = -ENOMEM;
  5174. int total_vqs;
  5175. bool *ctx;
  5176. u16 i;
  5177. /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
  5178. * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
  5179. * possible control vq.
  5180. */
  5181. total_vqs = vi->max_queue_pairs * 2 +
  5182. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
  5183. /* Allocate space for find_vqs parameters */
  5184. vqs = kzalloc_objs(*vqs, total_vqs);
  5185. if (!vqs)
  5186. goto err_vq;
  5187. vqs_info = kzalloc_objs(*vqs_info, total_vqs);
  5188. if (!vqs_info)
  5189. goto err_vqs_info;
  5190. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  5191. ctx = kzalloc_objs(*ctx, total_vqs);
  5192. if (!ctx)
  5193. goto err_ctx;
  5194. } else {
  5195. ctx = NULL;
  5196. }
  5197. /* Parameters for control virtqueue, if any */
  5198. if (vi->has_cvq) {
  5199. vqs_info[total_vqs - 1].name = "control";
  5200. }
  5201. /* Allocate/initialize parameters for send/receive virtqueues */
  5202. for (i = 0; i < vi->max_queue_pairs; i++) {
  5203. vqs_info[rxq2vq(i)].callback = skb_recv_done;
  5204. vqs_info[txq2vq(i)].callback = skb_xmit_done;
  5205. sprintf(vi->rq[i].name, "input.%u", i);
  5206. sprintf(vi->sq[i].name, "output.%u", i);
  5207. vqs_info[rxq2vq(i)].name = vi->rq[i].name;
  5208. vqs_info[txq2vq(i)].name = vi->sq[i].name;
  5209. if (ctx)
  5210. vqs_info[rxq2vq(i)].ctx = true;
  5211. }
  5212. ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, vqs_info, NULL);
  5213. if (ret)
  5214. goto err_find;
  5215. if (vi->has_cvq) {
  5216. vi->cvq = vqs[total_vqs - 1];
  5217. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  5218. vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  5219. }
  5220. for (i = 0; i < vi->max_queue_pairs; i++) {
  5221. vi->rq[i].vq = vqs[rxq2vq(i)];
  5222. vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
  5223. vi->sq[i].vq = vqs[txq2vq(i)];
  5224. }
  5225. /* run here: ret == 0. */
  5226. err_find:
  5227. kfree(ctx);
  5228. err_ctx:
  5229. kfree(vqs_info);
  5230. err_vqs_info:
  5231. kfree(vqs);
  5232. err_vq:
  5233. return ret;
  5234. }
  5235. static int virtnet_alloc_queues(struct virtnet_info *vi)
  5236. {
  5237. int i;
  5238. if (vi->has_cvq) {
  5239. vi->ctrl = kzalloc_obj(*vi->ctrl);
  5240. if (!vi->ctrl)
  5241. goto err_ctrl;
  5242. } else {
  5243. vi->ctrl = NULL;
  5244. }
  5245. vi->sq = kzalloc_objs(*vi->sq, vi->max_queue_pairs);
  5246. if (!vi->sq)
  5247. goto err_sq;
  5248. vi->rq = kzalloc_objs(*vi->rq, vi->max_queue_pairs);
  5249. if (!vi->rq)
  5250. goto err_rq;
  5251. for (i = 0; i < vi->max_queue_pairs; i++) {
  5252. vi->rq[i].pages = NULL;
  5253. netif_napi_add_config(vi->dev, &vi->rq[i].napi, virtnet_poll,
  5254. i);
  5255. vi->rq[i].napi.weight = napi_weight;
  5256. netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
  5257. virtnet_poll_tx,
  5258. napi_tx ? napi_weight : 0);
  5259. sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
  5260. ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
  5261. sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
  5262. u64_stats_init(&vi->rq[i].stats.syncp);
  5263. u64_stats_init(&vi->sq[i].stats.syncp);
  5264. mutex_init(&vi->rq[i].dim_lock);
  5265. }
  5266. return 0;
  5267. err_rq:
  5268. kfree(vi->sq);
  5269. err_sq:
  5270. kfree(vi->ctrl);
  5271. err_ctrl:
  5272. return -ENOMEM;
  5273. }
  5274. static int init_vqs(struct virtnet_info *vi)
  5275. {
  5276. int ret;
  5277. /* Allocate send & receive queues */
  5278. ret = virtnet_alloc_queues(vi);
  5279. if (ret)
  5280. goto err;
  5281. ret = virtnet_find_vqs(vi);
  5282. if (ret)
  5283. goto err_free;
  5284. cpus_read_lock();
  5285. virtnet_set_affinity(vi);
  5286. cpus_read_unlock();
  5287. return 0;
  5288. err_free:
  5289. virtnet_free_queues(vi);
  5290. err:
  5291. return ret;
  5292. }
  5293. #ifdef CONFIG_SYSFS
  5294. static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
  5295. char *buf)
  5296. {
  5297. struct virtnet_info *vi = netdev_priv(queue->dev);
  5298. unsigned int queue_index = get_netdev_rx_queue_index(queue);
  5299. unsigned int headroom = virtnet_get_headroom(vi);
  5300. unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
  5301. struct ewma_pkt_len *avg;
  5302. BUG_ON(queue_index >= vi->max_queue_pairs);
  5303. avg = &vi->rq[queue_index].mrg_avg_pkt_len;
  5304. return sprintf(buf, "%u\n",
  5305. get_mergeable_buf_len(&vi->rq[queue_index], avg,
  5306. SKB_DATA_ALIGN(headroom + tailroom)));
  5307. }
  5308. static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
  5309. __ATTR_RO(mergeable_rx_buffer_size);
  5310. static struct attribute *virtio_net_mrg_rx_attrs[] = {
  5311. &mergeable_rx_buffer_size_attribute.attr,
  5312. NULL
  5313. };
  5314. static const struct attribute_group virtio_net_mrg_rx_group = {
  5315. .name = "virtio_net",
  5316. .attrs = virtio_net_mrg_rx_attrs
  5317. };
  5318. #endif
  5319. static bool virtnet_fail_on_feature(struct virtio_device *vdev,
  5320. unsigned int fbit,
  5321. const char *fname, const char *dname)
  5322. {
  5323. if (!virtio_has_feature(vdev, fbit))
  5324. return false;
  5325. dev_err(&vdev->dev, "device advertises feature %s but not %s",
  5326. fname, dname);
  5327. return true;
  5328. }
  5329. #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
  5330. virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
  5331. static bool virtnet_validate_features(struct virtio_device *vdev)
  5332. {
  5333. if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
  5334. (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
  5335. "VIRTIO_NET_F_CTRL_VQ") ||
  5336. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
  5337. "VIRTIO_NET_F_CTRL_VQ") ||
  5338. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
  5339. "VIRTIO_NET_F_CTRL_VQ") ||
  5340. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
  5341. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
  5342. "VIRTIO_NET_F_CTRL_VQ") ||
  5343. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
  5344. "VIRTIO_NET_F_CTRL_VQ") ||
  5345. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
  5346. "VIRTIO_NET_F_CTRL_VQ") ||
  5347. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
  5348. "VIRTIO_NET_F_CTRL_VQ") ||
  5349. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL,
  5350. "VIRTIO_NET_F_CTRL_VQ"))) {
  5351. return false;
  5352. }
  5353. return true;
  5354. }
  5355. #define MIN_MTU ETH_MIN_MTU
  5356. #define MAX_MTU ETH_MAX_MTU
  5357. static int virtnet_validate(struct virtio_device *vdev)
  5358. {
  5359. if (!vdev->config->get) {
  5360. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  5361. __func__);
  5362. return -EINVAL;
  5363. }
  5364. if (!virtnet_validate_features(vdev))
  5365. return -EINVAL;
  5366. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  5367. int mtu = virtio_cread16(vdev,
  5368. offsetof(struct virtio_net_config,
  5369. mtu));
  5370. if (mtu < MIN_MTU)
  5371. __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
  5372. }
  5373. if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
  5374. !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  5375. dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
  5376. __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
  5377. }
  5378. return 0;
  5379. }
  5380. static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
  5381. {
  5382. return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  5383. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  5384. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
  5385. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
  5386. (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
  5387. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
  5388. }
  5389. static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
  5390. {
  5391. bool guest_gso = virtnet_check_guest_gso(vi);
  5392. /* If device can receive ANY guest GSO packets, regardless of mtu,
  5393. * allocate packets of maximum size, otherwise limit it to only
  5394. * mtu size worth only.
  5395. */
  5396. if (mtu > ETH_DATA_LEN || guest_gso) {
  5397. vi->big_packets = true;
  5398. vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
  5399. }
  5400. }
  5401. #define VIRTIO_NET_HASH_REPORT_MAX_TABLE 10
  5402. static enum xdp_rss_hash_type
  5403. virtnet_xdp_rss_type[VIRTIO_NET_HASH_REPORT_MAX_TABLE] = {
  5404. [VIRTIO_NET_HASH_REPORT_NONE] = XDP_RSS_TYPE_NONE,
  5405. [VIRTIO_NET_HASH_REPORT_IPv4] = XDP_RSS_TYPE_L3_IPV4,
  5406. [VIRTIO_NET_HASH_REPORT_TCPv4] = XDP_RSS_TYPE_L4_IPV4_TCP,
  5407. [VIRTIO_NET_HASH_REPORT_UDPv4] = XDP_RSS_TYPE_L4_IPV4_UDP,
  5408. [VIRTIO_NET_HASH_REPORT_IPv6] = XDP_RSS_TYPE_L3_IPV6,
  5409. [VIRTIO_NET_HASH_REPORT_TCPv6] = XDP_RSS_TYPE_L4_IPV6_TCP,
  5410. [VIRTIO_NET_HASH_REPORT_UDPv6] = XDP_RSS_TYPE_L4_IPV6_UDP,
  5411. [VIRTIO_NET_HASH_REPORT_IPv6_EX] = XDP_RSS_TYPE_L3_IPV6_EX,
  5412. [VIRTIO_NET_HASH_REPORT_TCPv6_EX] = XDP_RSS_TYPE_L4_IPV6_TCP_EX,
  5413. [VIRTIO_NET_HASH_REPORT_UDPv6_EX] = XDP_RSS_TYPE_L4_IPV6_UDP_EX
  5414. };
  5415. static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
  5416. enum xdp_rss_hash_type *rss_type)
  5417. {
  5418. const struct xdp_buff *xdp = (void *)_ctx;
  5419. struct virtio_net_hdr_v1_hash *hdr_hash;
  5420. struct virtnet_info *vi;
  5421. u16 hash_report;
  5422. if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
  5423. return -ENODATA;
  5424. vi = netdev_priv(xdp->rxq->dev);
  5425. hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
  5426. hash_report = __le16_to_cpu(hdr_hash->hash_report);
  5427. if (hash_report >= VIRTIO_NET_HASH_REPORT_MAX_TABLE)
  5428. hash_report = VIRTIO_NET_HASH_REPORT_NONE;
  5429. *rss_type = virtnet_xdp_rss_type[hash_report];
  5430. *hash = virtio_net_hash_value(hdr_hash);
  5431. return 0;
  5432. }
  5433. static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
  5434. .xmo_rx_hash = virtnet_xdp_rx_hash,
  5435. };
  5436. static int virtnet_probe(struct virtio_device *vdev)
  5437. {
  5438. int i, err = -ENOMEM;
  5439. struct net_device *dev;
  5440. struct virtnet_info *vi;
  5441. u16 max_queue_pairs;
  5442. int mtu = 0;
  5443. u16 key_sz;
  5444. /* Find if host supports multiqueue/rss virtio_net device */
  5445. max_queue_pairs = 1;
  5446. if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
  5447. max_queue_pairs =
  5448. virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
  5449. /* We need at least 2 queue's */
  5450. if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  5451. max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  5452. !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  5453. max_queue_pairs = 1;
  5454. /* Allocate ourselves a network device with room for our info */
  5455. dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
  5456. if (!dev)
  5457. return -ENOMEM;
  5458. /* Set up network device as normal. */
  5459. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
  5460. IFF_TX_SKB_NO_LINEAR;
  5461. dev->netdev_ops = &virtnet_netdev;
  5462. dev->stat_ops = &virtnet_stat_ops;
  5463. dev->features = NETIF_F_HIGHDMA;
  5464. dev->ethtool_ops = &virtnet_ethtool_ops;
  5465. SET_NETDEV_DEV(dev, &vdev->dev);
  5466. /* Do we support "hardware" checksums? */
  5467. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  5468. /* This opens up the world of extra features. */
  5469. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  5470. if (csum)
  5471. dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  5472. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  5473. dev->hw_features |= NETIF_F_TSO
  5474. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  5475. }
  5476. /* Individual feature bits: what can host handle? */
  5477. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  5478. dev->hw_features |= NETIF_F_TSO;
  5479. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  5480. dev->hw_features |= NETIF_F_TSO6;
  5481. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  5482. dev->hw_features |= NETIF_F_TSO_ECN;
  5483. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
  5484. dev->hw_features |= NETIF_F_GSO_UDP_L4;
  5485. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO)) {
  5486. dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
  5487. dev->hw_enc_features = dev->hw_features;
  5488. }
  5489. if (dev->hw_features & NETIF_F_GSO_UDP_TUNNEL &&
  5490. virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO_CSUM)) {
  5491. dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  5492. dev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  5493. }
  5494. dev->features |= NETIF_F_GSO_ROBUST;
  5495. if (gso)
  5496. dev->features |= dev->hw_features;
  5497. /* (!csum && gso) case will be fixed by register_netdev() */
  5498. }
  5499. /* 1. With VIRTIO_NET_F_GUEST_CSUM negotiation, the driver doesn't
  5500. * need to calculate checksums for partially checksummed packets,
  5501. * as they're considered valid by the upper layer.
  5502. * 2. Without VIRTIO_NET_F_GUEST_CSUM negotiation, the driver only
  5503. * receives fully checksummed packets. The device may assist in
  5504. * validating these packets' checksums, so the driver won't have to.
  5505. */
  5506. dev->features |= NETIF_F_RXCSUM;
  5507. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  5508. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
  5509. dev->features |= NETIF_F_GRO_HW;
  5510. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
  5511. dev->hw_features |= NETIF_F_GRO_HW;
  5512. dev->vlan_features = dev->features;
  5513. dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
  5514. NETDEV_XDP_ACT_XSK_ZEROCOPY;
  5515. /* MTU range: 68 - 65535 */
  5516. dev->min_mtu = MIN_MTU;
  5517. dev->max_mtu = MAX_MTU;
  5518. /* Configuration may specify what MAC to use. Otherwise random. */
  5519. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  5520. u8 addr[ETH_ALEN];
  5521. virtio_cread_bytes(vdev,
  5522. offsetof(struct virtio_net_config, mac),
  5523. addr, ETH_ALEN);
  5524. eth_hw_addr_set(dev, addr);
  5525. } else {
  5526. eth_hw_addr_random(dev);
  5527. dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
  5528. dev->dev_addr);
  5529. }
  5530. /* Set up our device-specific information */
  5531. vi = netdev_priv(dev);
  5532. vi->dev = dev;
  5533. vi->vdev = vdev;
  5534. vdev->priv = vi;
  5535. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  5536. INIT_WORK(&vi->rx_mode_work, virtnet_rx_mode_work);
  5537. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
  5538. vi->mergeable_rx_bufs = true;
  5539. dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
  5540. }
  5541. if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
  5542. vi->has_rss_hash_report = true;
  5543. if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) {
  5544. vi->has_rss = true;
  5545. vi->rss_indir_table_size =
  5546. virtio_cread16(vdev, offsetof(struct virtio_net_config,
  5547. rss_max_indirection_table_length));
  5548. }
  5549. vi->rss_hdr = devm_kzalloc(&vdev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL);
  5550. if (!vi->rss_hdr) {
  5551. err = -ENOMEM;
  5552. goto free;
  5553. }
  5554. if (vi->has_rss || vi->has_rss_hash_report) {
  5555. key_sz = virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
  5556. vi->rss_key_size = min_t(u16, key_sz, NETDEV_RSS_KEY_LEN);
  5557. if (key_sz > vi->rss_key_size)
  5558. dev_warn(&vdev->dev,
  5559. "rss_max_key_size=%u exceeds driver limit %u, clamping\n",
  5560. key_sz, vi->rss_key_size);
  5561. vi->rss_hash_types_supported =
  5562. virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
  5563. vi->rss_hash_types_supported &=
  5564. ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
  5565. VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
  5566. VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
  5567. dev->hw_features |= NETIF_F_RXHASH;
  5568. dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
  5569. }
  5570. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) ||
  5571. virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO))
  5572. vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel);
  5573. else if (vi->has_rss_hash_report)
  5574. vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
  5575. else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
  5576. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  5577. vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  5578. else
  5579. vi->hdr_len = sizeof(struct virtio_net_hdr);
  5580. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM))
  5581. vi->rx_tnl_csum = true;
  5582. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO))
  5583. vi->rx_tnl = true;
  5584. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO))
  5585. vi->tx_tnl = true;
  5586. if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
  5587. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  5588. vi->any_header_sg = true;
  5589. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  5590. vi->has_cvq = true;
  5591. mutex_init(&vi->cvq_lock);
  5592. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  5593. mtu = virtio_cread16(vdev,
  5594. offsetof(struct virtio_net_config,
  5595. mtu));
  5596. if (mtu < dev->min_mtu) {
  5597. /* Should never trigger: MTU was previously validated
  5598. * in virtnet_validate.
  5599. */
  5600. dev_err(&vdev->dev,
  5601. "device MTU appears to have changed it is now %d < %d",
  5602. mtu, dev->min_mtu);
  5603. err = -EINVAL;
  5604. goto free;
  5605. }
  5606. dev->mtu = mtu;
  5607. dev->max_mtu = mtu;
  5608. }
  5609. virtnet_set_big_packets(vi, mtu);
  5610. if (vi->any_header_sg)
  5611. dev->needed_headroom = vi->hdr_len;
  5612. /* Enable multiqueue by default */
  5613. if (num_online_cpus() >= max_queue_pairs)
  5614. vi->curr_queue_pairs = max_queue_pairs;
  5615. else
  5616. vi->curr_queue_pairs = num_online_cpus();
  5617. vi->max_queue_pairs = max_queue_pairs;
  5618. /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
  5619. err = init_vqs(vi);
  5620. if (err)
  5621. goto free;
  5622. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
  5623. vi->intr_coal_rx.max_usecs = 0;
  5624. vi->intr_coal_tx.max_usecs = 0;
  5625. vi->intr_coal_rx.max_packets = 0;
  5626. /* Keep the default values of the coalescing parameters
  5627. * aligned with the default napi_tx state.
  5628. */
  5629. if (vi->sq[0].napi.weight)
  5630. vi->intr_coal_tx.max_packets = 1;
  5631. else
  5632. vi->intr_coal_tx.max_packets = 0;
  5633. }
  5634. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
  5635. /* The reason is the same as VIRTIO_NET_F_NOTF_COAL. */
  5636. for (i = 0; i < vi->max_queue_pairs; i++)
  5637. if (vi->sq[i].napi.weight)
  5638. vi->sq[i].intr_coal.max_packets = 1;
  5639. err = virtnet_init_irq_moder(vi);
  5640. if (err)
  5641. goto free;
  5642. }
  5643. #ifdef CONFIG_SYSFS
  5644. if (vi->mergeable_rx_bufs)
  5645. dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
  5646. #endif
  5647. netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
  5648. netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
  5649. virtnet_init_settings(dev);
  5650. if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
  5651. vi->failover = net_failover_create(vi->dev);
  5652. if (IS_ERR(vi->failover)) {
  5653. err = PTR_ERR(vi->failover);
  5654. goto free_vqs;
  5655. }
  5656. }
  5657. if (vi->has_rss || vi->has_rss_hash_report)
  5658. virtnet_init_default_rss(vi);
  5659. enable_rx_mode_work(vi);
  5660. /* serialize netdev register + virtio_device_ready() with ndo_open() */
  5661. rtnl_lock();
  5662. err = register_netdevice(dev);
  5663. if (err) {
  5664. pr_debug("virtio_net: registering device failed\n");
  5665. rtnl_unlock();
  5666. goto free_failover;
  5667. }
  5668. /* Disable config change notification until ndo_open. */
  5669. virtio_config_driver_disable(vi->vdev);
  5670. virtio_device_ready(vdev);
  5671. if (vi->has_rss || vi->has_rss_hash_report) {
  5672. if (!virtnet_commit_rss_command(vi)) {
  5673. dev_warn(&vdev->dev, "RSS disabled because committing failed.\n");
  5674. dev->hw_features &= ~NETIF_F_RXHASH;
  5675. vi->has_rss_hash_report = false;
  5676. vi->has_rss = false;
  5677. }
  5678. }
  5679. virtnet_set_queues(vi, vi->curr_queue_pairs);
  5680. /* a random MAC address has been assigned, notify the device.
  5681. * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
  5682. * because many devices work fine without getting MAC explicitly
  5683. */
  5684. if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
  5685. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  5686. struct scatterlist sg;
  5687. sg_init_one(&sg, dev->dev_addr, dev->addr_len);
  5688. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  5689. VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
  5690. pr_debug("virtio_net: setting MAC address failed\n");
  5691. rtnl_unlock();
  5692. err = -EINVAL;
  5693. goto free_unregister_netdev;
  5694. }
  5695. }
  5696. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS)) {
  5697. struct virtio_net_stats_capabilities *stats_cap __free(kfree) = NULL;
  5698. struct scatterlist sg;
  5699. __le64 v;
  5700. stats_cap = kzalloc_obj(*stats_cap);
  5701. if (!stats_cap) {
  5702. rtnl_unlock();
  5703. err = -ENOMEM;
  5704. goto free_unregister_netdev;
  5705. }
  5706. sg_init_one(&sg, stats_cap, sizeof(*stats_cap));
  5707. if (!virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
  5708. VIRTIO_NET_CTRL_STATS_QUERY,
  5709. NULL, &sg)) {
  5710. pr_debug("virtio_net: fail to get stats capability\n");
  5711. rtnl_unlock();
  5712. err = -EINVAL;
  5713. goto free_unregister_netdev;
  5714. }
  5715. v = stats_cap->supported_stats_types[0];
  5716. vi->device_stats_cap = le64_to_cpu(v);
  5717. }
  5718. /* Assume link up if device can't report link status,
  5719. otherwise get link status from config. */
  5720. netif_carrier_off(dev);
  5721. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  5722. virtio_config_changed(vi->vdev);
  5723. } else {
  5724. vi->status = VIRTIO_NET_S_LINK_UP;
  5725. virtnet_update_settings(vi);
  5726. netif_carrier_on(dev);
  5727. }
  5728. for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) {
  5729. unsigned int fbit;
  5730. fbit = virtio_offload_to_feature(guest_offloads[i]);
  5731. if (virtio_has_feature(vi->vdev, fbit))
  5732. set_bit(guest_offloads[i], &vi->guest_offloads);
  5733. }
  5734. vi->guest_offloads_capable = vi->guest_offloads;
  5735. rtnl_unlock();
  5736. err = virtnet_cpu_notif_add(vi);
  5737. if (err) {
  5738. pr_debug("virtio_net: registering cpu notifier failed\n");
  5739. goto free_unregister_netdev;
  5740. }
  5741. pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
  5742. dev->name, max_queue_pairs);
  5743. return 0;
  5744. free_unregister_netdev:
  5745. unregister_netdev(dev);
  5746. free_failover:
  5747. net_failover_destroy(vi->failover);
  5748. free_vqs:
  5749. virtio_reset_device(vdev);
  5750. free_receive_page_frags(vi);
  5751. virtnet_del_vqs(vi);
  5752. free:
  5753. free_netdev(dev);
  5754. return err;
  5755. }
  5756. static void remove_vq_common(struct virtnet_info *vi)
  5757. {
  5758. int i;
  5759. virtio_reset_device(vi->vdev);
  5760. /* Free unused buffers in both send and recv, if any. */
  5761. free_unused_bufs(vi);
  5762. /*
  5763. * Rule of thumb is netdev_tx_reset_queue() should follow any
  5764. * skb freeing not followed by netdev_tx_completed_queue()
  5765. */
  5766. for (i = 0; i < vi->max_queue_pairs; i++)
  5767. netdev_tx_reset_queue(netdev_get_tx_queue(vi->dev, i));
  5768. free_receive_bufs(vi);
  5769. free_receive_page_frags(vi);
  5770. virtnet_del_vqs(vi);
  5771. }
  5772. static void virtnet_remove(struct virtio_device *vdev)
  5773. {
  5774. struct virtnet_info *vi = vdev->priv;
  5775. virtnet_cpu_notif_remove(vi);
  5776. /* Make sure no work handler is accessing the device. */
  5777. flush_work(&vi->config_work);
  5778. disable_rx_mode_work(vi);
  5779. flush_work(&vi->rx_mode_work);
  5780. virtnet_free_irq_moder(vi);
  5781. unregister_netdev(vi->dev);
  5782. net_failover_destroy(vi->failover);
  5783. remove_vq_common(vi);
  5784. free_netdev(vi->dev);
  5785. }
  5786. static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
  5787. {
  5788. struct virtnet_info *vi = vdev->priv;
  5789. virtnet_cpu_notif_remove(vi);
  5790. virtnet_freeze_down(vdev);
  5791. remove_vq_common(vi);
  5792. return 0;
  5793. }
  5794. static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
  5795. {
  5796. struct virtnet_info *vi = vdev->priv;
  5797. int err;
  5798. err = virtnet_restore_up(vdev);
  5799. if (err)
  5800. return err;
  5801. virtnet_set_queues(vi, vi->curr_queue_pairs);
  5802. err = virtnet_cpu_notif_add(vi);
  5803. if (err) {
  5804. virtnet_freeze_down(vdev);
  5805. remove_vq_common(vi);
  5806. return err;
  5807. }
  5808. return 0;
  5809. }
  5810. static struct virtio_device_id id_table[] = {
  5811. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  5812. { 0 },
  5813. };
  5814. #define VIRTNET_FEATURES \
  5815. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
  5816. VIRTIO_NET_F_MAC, \
  5817. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
  5818. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
  5819. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
  5820. VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
  5821. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
  5822. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
  5823. VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
  5824. VIRTIO_NET_F_CTRL_MAC_ADDR, \
  5825. VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
  5826. VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
  5827. VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
  5828. VIRTIO_NET_F_VQ_NOTF_COAL, \
  5829. VIRTIO_NET_F_GUEST_HDRLEN, VIRTIO_NET_F_DEVICE_STATS
  5830. static unsigned int features[] = {
  5831. VIRTNET_FEATURES,
  5832. VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO,
  5833. VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM,
  5834. VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO,
  5835. VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO_CSUM,
  5836. };
  5837. static unsigned int features_legacy[] = {
  5838. VIRTNET_FEATURES,
  5839. VIRTIO_NET_F_GSO,
  5840. VIRTIO_F_ANY_LAYOUT,
  5841. };
  5842. static struct virtio_driver virtio_net_driver = {
  5843. .feature_table = features,
  5844. .feature_table_size = ARRAY_SIZE(features),
  5845. .feature_table_legacy = features_legacy,
  5846. .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
  5847. .driver.name = KBUILD_MODNAME,
  5848. .id_table = id_table,
  5849. .validate = virtnet_validate,
  5850. .probe = virtnet_probe,
  5851. .remove = virtnet_remove,
  5852. .config_changed = virtnet_config_changed,
  5853. #ifdef CONFIG_PM_SLEEP
  5854. .freeze = virtnet_freeze,
  5855. .restore = virtnet_restore,
  5856. #endif
  5857. };
  5858. static __init int virtio_net_driver_init(void)
  5859. {
  5860. int ret;
  5861. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
  5862. virtnet_cpu_online,
  5863. virtnet_cpu_down_prep);
  5864. if (ret < 0)
  5865. goto out;
  5866. virtionet_online = ret;
  5867. ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
  5868. NULL, virtnet_cpu_dead);
  5869. if (ret)
  5870. goto err_dead;
  5871. ret = register_virtio_driver(&virtio_net_driver);
  5872. if (ret)
  5873. goto err_virtio;
  5874. return 0;
  5875. err_virtio:
  5876. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  5877. err_dead:
  5878. cpuhp_remove_multi_state(virtionet_online);
  5879. out:
  5880. return ret;
  5881. }
  5882. module_init(virtio_net_driver_init);
  5883. static __exit void virtio_net_driver_exit(void)
  5884. {
  5885. unregister_virtio_driver(&virtio_net_driver);
  5886. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  5887. cpuhp_remove_multi_state(virtionet_online);
  5888. }
  5889. module_exit(virtio_net_driver_exit);
  5890. MODULE_DEVICE_TABLE(virtio, id_table);
  5891. MODULE_DESCRIPTION("Virtio network driver");
  5892. MODULE_LICENSE("GPL");