dev.c 338 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NET3 Protocol independent device support routines.
  4. *
  5. * Derived from the non IP parts of dev.c 1.0.19
  6. * Authors: Ross Biro
  7. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  8. * Mark Evans, <evansmp@uhura.aston.ac.uk>
  9. *
  10. * Additional Authors:
  11. * Florian la Roche <rzsfl@rz.uni-sb.de>
  12. * Alan Cox <gw4pts@gw4pts.ampr.org>
  13. * David Hinds <dahinds@users.sourceforge.net>
  14. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  15. * Adam Sulmicki <adam@cfar.umd.edu>
  16. * Pekka Riikonen <priikone@poesidon.pspt.fi>
  17. *
  18. * Changes:
  19. * D.J. Barrow : Fixed bug where dev->refcnt gets set
  20. * to 2 if register_netdev gets called
  21. * before net_dev_init & also removed a
  22. * few lines of code in the process.
  23. * Alan Cox : device private ioctl copies fields back.
  24. * Alan Cox : Transmit queue code does relevant
  25. * stunts to keep the queue safe.
  26. * Alan Cox : Fixed double lock.
  27. * Alan Cox : Fixed promisc NULL pointer trap
  28. * ???????? : Support the full private ioctl range
  29. * Alan Cox : Moved ioctl permission check into
  30. * drivers
  31. * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
  32. * Alan Cox : 100 backlog just doesn't cut it when
  33. * you start doing multicast video 8)
  34. * Alan Cox : Rewrote net_bh and list manager.
  35. * Alan Cox : Fix ETH_P_ALL echoback lengths.
  36. * Alan Cox : Took out transmit every packet pass
  37. * Saved a few bytes in the ioctl handler
  38. * Alan Cox : Network driver sets packet type before
  39. * calling netif_rx. Saves a function
  40. * call a packet.
  41. * Alan Cox : Hashed net_bh()
  42. * Richard Kooijman: Timestamp fixes.
  43. * Alan Cox : Wrong field in SIOCGIFDSTADDR
  44. * Alan Cox : Device lock protection.
  45. * Alan Cox : Fixed nasty side effect of device close
  46. * changes.
  47. * Rudi Cilibrasi : Pass the right thing to
  48. * set_mac_address()
  49. * Dave Miller : 32bit quantity for the device lock to
  50. * make it work out on a Sparc.
  51. * Bjorn Ekwall : Added KERNELD hack.
  52. * Alan Cox : Cleaned up the backlog initialise.
  53. * Craig Metz : SIOCGIFCONF fix if space for under
  54. * 1 device.
  55. * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
  56. * is no device open function.
  57. * Andi Kleen : Fix error reporting for SIOCGIFCONF
  58. * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
  59. * Cyrus Durgin : Cleaned for KMOD
  60. * Adam Sulmicki : Bug Fix : Network Device Unload
  61. * A network device unload needs to purge
  62. * the backlog queue.
  63. * Paul Rusty Russell : SIOCSIFNAME
  64. * Pekka Riikonen : Netdev boot-time settings code
  65. * Andrew Morton : Make unregister_netdevice wait
  66. * indefinitely on dev->refcnt
  67. * J Hadi Salim : - Backlog queue sampling
  68. * - netif_rx() feedback
  69. */
  70. #include <linux/uaccess.h>
  71. #include <linux/bitmap.h>
  72. #include <linux/capability.h>
  73. #include <linux/cpu.h>
  74. #include <linux/types.h>
  75. #include <linux/kernel.h>
  76. #include <linux/hash.h>
  77. #include <linux/slab.h>
  78. #include <linux/sched.h>
  79. #include <linux/sched/isolation.h>
  80. #include <linux/sched/mm.h>
  81. #include <linux/smpboot.h>
  82. #include <linux/mutex.h>
  83. #include <linux/rwsem.h>
  84. #include <linux/string.h>
  85. #include <linux/mm.h>
  86. #include <linux/socket.h>
  87. #include <linux/sockios.h>
  88. #include <linux/errno.h>
  89. #include <linux/interrupt.h>
  90. #include <linux/if_ether.h>
  91. #include <linux/netdevice.h>
  92. #include <linux/etherdevice.h>
  93. #include <linux/ethtool.h>
  94. #include <linux/ethtool_netlink.h>
  95. #include <linux/skbuff.h>
  96. #include <linux/kthread.h>
  97. #include <linux/bpf.h>
  98. #include <linux/bpf_trace.h>
  99. #include <net/net_namespace.h>
  100. #include <net/sock.h>
  101. #include <net/busy_poll.h>
  102. #include <linux/rtnetlink.h>
  103. #include <linux/stat.h>
  104. #include <net/dsa.h>
  105. #include <net/dst.h>
  106. #include <net/dst_metadata.h>
  107. #include <net/gro.h>
  108. #include <net/netdev_queues.h>
  109. #include <net/pkt_sched.h>
  110. #include <net/pkt_cls.h>
  111. #include <net/checksum.h>
  112. #include <net/xfrm.h>
  113. #include <net/tcx.h>
  114. #include <linux/highmem.h>
  115. #include <linux/init.h>
  116. #include <linux/module.h>
  117. #include <linux/netpoll.h>
  118. #include <linux/rcupdate.h>
  119. #include <linux/delay.h>
  120. #include <net/iw_handler.h>
  121. #include <asm/current.h>
  122. #include <linux/audit.h>
  123. #include <linux/dmaengine.h>
  124. #include <linux/err.h>
  125. #include <linux/ctype.h>
  126. #include <linux/if_arp.h>
  127. #include <linux/if_vlan.h>
  128. #include <linux/ip.h>
  129. #include <net/ip.h>
  130. #include <net/mpls.h>
  131. #include <linux/ipv6.h>
  132. #include <linux/in.h>
  133. #include <linux/jhash.h>
  134. #include <linux/random.h>
  135. #include <trace/events/napi.h>
  136. #include <trace/events/net.h>
  137. #include <trace/events/skb.h>
  138. #include <trace/events/qdisc.h>
  139. #include <trace/events/xdp.h>
  140. #include <linux/inetdevice.h>
  141. #include <linux/cpu_rmap.h>
  142. #include <linux/static_key.h>
  143. #include <linux/hashtable.h>
  144. #include <linux/vmalloc.h>
  145. #include <linux/if_macvlan.h>
  146. #include <linux/errqueue.h>
  147. #include <linux/hrtimer.h>
  148. #include <linux/netfilter_netdev.h>
  149. #include <linux/crash_dump.h>
  150. #include <linux/sctp.h>
  151. #include <net/udp_tunnel.h>
  152. #include <linux/net_namespace.h>
  153. #include <linux/indirect_call_wrapper.h>
  154. #include <net/devlink.h>
  155. #include <linux/pm_runtime.h>
  156. #include <linux/prandom.h>
  157. #include <linux/once_lite.h>
  158. #include <net/netdev_lock.h>
  159. #include <net/netdev_rx_queue.h>
  160. #include <net/page_pool/types.h>
  161. #include <net/page_pool/helpers.h>
  162. #include <net/page_pool/memory_provider.h>
  163. #include <net/rps.h>
  164. #include <linux/phy_link_topology.h>
  165. #include "dev.h"
  166. #include "devmem.h"
  167. #include "net-sysfs.h"
  168. static DEFINE_SPINLOCK(ptype_lock);
  169. struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
  170. static int netif_rx_internal(struct sk_buff *skb);
  171. static int call_netdevice_notifiers_extack(unsigned long val,
  172. struct net_device *dev,
  173. struct netlink_ext_ack *extack);
  174. static DEFINE_MUTEX(ifalias_mutex);
  175. /* protects napi_hash addition/deletion and napi_gen_id */
  176. static DEFINE_SPINLOCK(napi_hash_lock);
  177. static unsigned int napi_gen_id = NR_CPUS;
  178. static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
  179. static inline void dev_base_seq_inc(struct net *net)
  180. {
  181. unsigned int val = net->dev_base_seq + 1;
  182. WRITE_ONCE(net->dev_base_seq, val ?: 1);
  183. }
  184. static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
  185. {
  186. unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
  187. return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
  188. }
  189. static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
  190. {
  191. return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
  192. }
  193. #ifndef CONFIG_PREEMPT_RT
  194. static DEFINE_STATIC_KEY_FALSE(use_backlog_threads_key);
  195. static int __init setup_backlog_napi_threads(char *arg)
  196. {
  197. static_branch_enable(&use_backlog_threads_key);
  198. return 0;
  199. }
  200. early_param("thread_backlog_napi", setup_backlog_napi_threads);
  201. static bool use_backlog_threads(void)
  202. {
  203. return static_branch_unlikely(&use_backlog_threads_key);
  204. }
  205. #else
  206. static bool use_backlog_threads(void)
  207. {
  208. return true;
  209. }
  210. #endif
  211. static inline void backlog_lock_irq_save(struct softnet_data *sd,
  212. unsigned long *flags)
  213. {
  214. if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
  215. spin_lock_irqsave(&sd->input_pkt_queue.lock, *flags);
  216. } else {
  217. local_irq_save(*flags);
  218. if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads())
  219. spin_lock(&sd->input_pkt_queue.lock);
  220. }
  221. }
  222. static inline void backlog_lock_irq_disable(struct softnet_data *sd)
  223. {
  224. if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads())
  225. spin_lock_irq(&sd->input_pkt_queue.lock);
  226. else
  227. local_irq_disable();
  228. }
  229. static inline void backlog_unlock_irq_restore(struct softnet_data *sd,
  230. unsigned long flags)
  231. {
  232. if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
  233. spin_unlock_irqrestore(&sd->input_pkt_queue.lock, flags);
  234. } else {
  235. if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads())
  236. spin_unlock(&sd->input_pkt_queue.lock);
  237. local_irq_restore(flags);
  238. }
  239. }
  240. static inline void backlog_unlock_irq_enable(struct softnet_data *sd)
  241. {
  242. if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads())
  243. spin_unlock_irq(&sd->input_pkt_queue.lock);
  244. else
  245. local_irq_enable();
  246. }
  247. static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
  248. const char *name)
  249. {
  250. struct netdev_name_node *name_node;
  251. name_node = kmalloc_obj(*name_node);
  252. if (!name_node)
  253. return NULL;
  254. INIT_HLIST_NODE(&name_node->hlist);
  255. name_node->dev = dev;
  256. name_node->name = name;
  257. return name_node;
  258. }
  259. static struct netdev_name_node *
  260. netdev_name_node_head_alloc(struct net_device *dev)
  261. {
  262. struct netdev_name_node *name_node;
  263. name_node = netdev_name_node_alloc(dev, dev->name);
  264. if (!name_node)
  265. return NULL;
  266. INIT_LIST_HEAD(&name_node->list);
  267. return name_node;
  268. }
  269. static void netdev_name_node_free(struct netdev_name_node *name_node)
  270. {
  271. kfree(name_node);
  272. }
  273. static void netdev_name_node_add(struct net *net,
  274. struct netdev_name_node *name_node)
  275. {
  276. hlist_add_head_rcu(&name_node->hlist,
  277. dev_name_hash(net, name_node->name));
  278. }
  279. static void netdev_name_node_del(struct netdev_name_node *name_node)
  280. {
  281. hlist_del_rcu(&name_node->hlist);
  282. }
  283. static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
  284. const char *name)
  285. {
  286. struct hlist_head *head = dev_name_hash(net, name);
  287. struct netdev_name_node *name_node;
  288. hlist_for_each_entry(name_node, head, hlist)
  289. if (!strcmp(name_node->name, name))
  290. return name_node;
  291. return NULL;
  292. }
  293. static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
  294. const char *name)
  295. {
  296. struct hlist_head *head = dev_name_hash(net, name);
  297. struct netdev_name_node *name_node;
  298. hlist_for_each_entry_rcu(name_node, head, hlist)
  299. if (!strcmp(name_node->name, name))
  300. return name_node;
  301. return NULL;
  302. }
  303. bool netdev_name_in_use(struct net *net, const char *name)
  304. {
  305. return netdev_name_node_lookup(net, name);
  306. }
  307. EXPORT_SYMBOL(netdev_name_in_use);
  308. int netdev_name_node_alt_create(struct net_device *dev, const char *name)
  309. {
  310. struct netdev_name_node *name_node;
  311. struct net *net = dev_net(dev);
  312. name_node = netdev_name_node_lookup(net, name);
  313. if (name_node)
  314. return -EEXIST;
  315. name_node = netdev_name_node_alloc(dev, name);
  316. if (!name_node)
  317. return -ENOMEM;
  318. netdev_name_node_add(net, name_node);
  319. /* The node that holds dev->name acts as a head of per-device list. */
  320. list_add_tail_rcu(&name_node->list, &dev->name_node->list);
  321. return 0;
  322. }
  323. static void netdev_name_node_alt_free(struct rcu_head *head)
  324. {
  325. struct netdev_name_node *name_node =
  326. container_of(head, struct netdev_name_node, rcu);
  327. kfree(name_node->name);
  328. netdev_name_node_free(name_node);
  329. }
  330. static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
  331. {
  332. netdev_name_node_del(name_node);
  333. list_del(&name_node->list);
  334. call_rcu(&name_node->rcu, netdev_name_node_alt_free);
  335. }
  336. int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
  337. {
  338. struct netdev_name_node *name_node;
  339. struct net *net = dev_net(dev);
  340. name_node = netdev_name_node_lookup(net, name);
  341. if (!name_node)
  342. return -ENOENT;
  343. /* lookup might have found our primary name or a name belonging
  344. * to another device.
  345. */
  346. if (name_node == dev->name_node || name_node->dev != dev)
  347. return -EINVAL;
  348. __netdev_name_node_alt_destroy(name_node);
  349. return 0;
  350. }
  351. static void netdev_name_node_alt_flush(struct net_device *dev)
  352. {
  353. struct netdev_name_node *name_node, *tmp;
  354. list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list) {
  355. list_del(&name_node->list);
  356. netdev_name_node_alt_free(&name_node->rcu);
  357. }
  358. }
  359. /* Device list insertion */
  360. static void list_netdevice(struct net_device *dev)
  361. {
  362. struct netdev_name_node *name_node;
  363. struct net *net = dev_net(dev);
  364. ASSERT_RTNL();
  365. list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
  366. netdev_name_node_add(net, dev->name_node);
  367. hlist_add_head_rcu(&dev->index_hlist,
  368. dev_index_hash(net, dev->ifindex));
  369. netdev_for_each_altname(dev, name_node)
  370. netdev_name_node_add(net, name_node);
  371. /* We reserved the ifindex, this can't fail */
  372. WARN_ON(xa_store(&net->dev_by_index, dev->ifindex, dev, GFP_KERNEL));
  373. dev_base_seq_inc(net);
  374. }
  375. /* Device list removal
  376. * caller must respect a RCU grace period before freeing/reusing dev
  377. */
  378. static void unlist_netdevice(struct net_device *dev)
  379. {
  380. struct netdev_name_node *name_node;
  381. struct net *net = dev_net(dev);
  382. ASSERT_RTNL();
  383. xa_erase(&net->dev_by_index, dev->ifindex);
  384. netdev_for_each_altname(dev, name_node)
  385. netdev_name_node_del(name_node);
  386. /* Unlink dev from the device chain */
  387. list_del_rcu(&dev->dev_list);
  388. netdev_name_node_del(dev->name_node);
  389. hlist_del_rcu(&dev->index_hlist);
  390. dev_base_seq_inc(dev_net(dev));
  391. }
  392. /*
  393. * Our notifier list
  394. */
  395. static RAW_NOTIFIER_HEAD(netdev_chain);
  396. /*
  397. * Device drivers call our routines to queue packets here. We empty the
  398. * queue in the local softnet handler.
  399. */
  400. DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data) = {
  401. .process_queue_bh_lock = INIT_LOCAL_LOCK(process_queue_bh_lock),
  402. };
  403. EXPORT_PER_CPU_SYMBOL(softnet_data);
  404. /* Page_pool has a lockless array/stack to alloc/recycle pages.
  405. * PP consumers must pay attention to run APIs in the appropriate context
  406. * (e.g. NAPI context).
  407. */
  408. DEFINE_PER_CPU(struct page_pool_bh, system_page_pool) = {
  409. .bh_lock = INIT_LOCAL_LOCK(bh_lock),
  410. };
  411. #ifdef CONFIG_LOCKDEP
  412. /*
  413. * register_netdevice() inits txq->_xmit_lock and sets lockdep class
  414. * according to dev->type
  415. */
  416. static const unsigned short netdev_lock_type[] = {
  417. ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
  418. ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
  419. ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
  420. ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
  421. ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
  422. ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
  423. ARPHRD_CAN, ARPHRD_MCTP,
  424. ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
  425. ARPHRD_RAWHDLC, ARPHRD_RAWIP,
  426. ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
  427. ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
  428. ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
  429. ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
  430. ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
  431. ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
  432. ARPHRD_IEEE80211_RADIOTAP,
  433. ARPHRD_IEEE802154, ARPHRD_IEEE802154_MONITOR,
  434. ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
  435. ARPHRD_CAIF, ARPHRD_IP6GRE, ARPHRD_NETLINK, ARPHRD_6LOWPAN,
  436. ARPHRD_VSOCKMON,
  437. ARPHRD_VOID, ARPHRD_NONE};
  438. static const char *const netdev_lock_name[] = {
  439. "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
  440. "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
  441. "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
  442. "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
  443. "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
  444. "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
  445. "_xmit_CAN", "_xmit_MCTP",
  446. "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
  447. "_xmit_RAWHDLC", "_xmit_RAWIP",
  448. "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
  449. "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
  450. "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
  451. "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
  452. "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
  453. "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
  454. "_xmit_IEEE80211_RADIOTAP",
  455. "_xmit_IEEE802154", "_xmit_IEEE802154_MONITOR",
  456. "_xmit_PHONET", "_xmit_PHONET_PIPE",
  457. "_xmit_CAIF", "_xmit_IP6GRE", "_xmit_NETLINK", "_xmit_6LOWPAN",
  458. "_xmit_VSOCKMON",
  459. "_xmit_VOID", "_xmit_NONE"};
  460. static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
  461. static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
  462. static inline unsigned short netdev_lock_pos(unsigned short dev_type)
  463. {
  464. int i;
  465. for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
  466. if (netdev_lock_type[i] == dev_type)
  467. return i;
  468. /* the last key is used by default */
  469. WARN_ONCE(1, "netdev_lock_pos() could not find dev_type=%u\n", dev_type);
  470. return ARRAY_SIZE(netdev_lock_type) - 1;
  471. }
  472. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  473. unsigned short dev_type)
  474. {
  475. int i;
  476. i = netdev_lock_pos(dev_type);
  477. lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
  478. netdev_lock_name[i]);
  479. }
  480. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  481. {
  482. int i;
  483. i = netdev_lock_pos(dev->type);
  484. lockdep_set_class_and_name(&dev->addr_list_lock,
  485. &netdev_addr_lock_key[i],
  486. netdev_lock_name[i]);
  487. }
  488. #else
  489. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  490. unsigned short dev_type)
  491. {
  492. }
  493. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  494. {
  495. }
  496. #endif
  497. /*******************************************************************************
  498. *
  499. * Protocol management and registration routines
  500. *
  501. *******************************************************************************/
  502. /*
  503. * Add a protocol ID to the list. Now that the input handler is
  504. * smarter we can dispense with all the messy stuff that used to be
  505. * here.
  506. *
  507. * BEWARE!!! Protocol handlers, mangling input packets,
  508. * MUST BE last in hash buckets and checking protocol handlers
  509. * MUST start from promiscuous ptype_all chain in net_bh.
  510. * It is true now, do not change it.
  511. * Explanation follows: if protocol handler, mangling packet, will
  512. * be the first on list, it is not able to sense, that packet
  513. * is cloned and should be copied-on-write, so that it will
  514. * change it and subsequent readers will get broken packet.
  515. * --ANK (980803)
  516. */
  517. static inline struct list_head *ptype_head(const struct packet_type *pt)
  518. {
  519. if (pt->type == htons(ETH_P_ALL)) {
  520. if (!pt->af_packet_net && !pt->dev)
  521. return NULL;
  522. return pt->dev ? &pt->dev->ptype_all :
  523. &pt->af_packet_net->ptype_all;
  524. }
  525. if (pt->dev)
  526. return &pt->dev->ptype_specific;
  527. return pt->af_packet_net ? &pt->af_packet_net->ptype_specific :
  528. &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
  529. }
  530. /**
  531. * dev_add_pack - add packet handler
  532. * @pt: packet type declaration
  533. *
  534. * Add a protocol handler to the networking stack. The passed &packet_type
  535. * is linked into kernel lists and may not be freed until it has been
  536. * removed from the kernel lists.
  537. *
  538. * This call does not sleep therefore it can not
  539. * guarantee all CPU's that are in middle of receiving packets
  540. * will see the new packet type (until the next received packet).
  541. */
  542. void dev_add_pack(struct packet_type *pt)
  543. {
  544. struct list_head *head = ptype_head(pt);
  545. if (WARN_ON_ONCE(!head))
  546. return;
  547. spin_lock(&ptype_lock);
  548. list_add_rcu(&pt->list, head);
  549. spin_unlock(&ptype_lock);
  550. }
  551. EXPORT_SYMBOL(dev_add_pack);
  552. /**
  553. * __dev_remove_pack - remove packet handler
  554. * @pt: packet type declaration
  555. *
  556. * Remove a protocol handler that was previously added to the kernel
  557. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  558. * from the kernel lists and can be freed or reused once this function
  559. * returns.
  560. *
  561. * The packet type might still be in use by receivers
  562. * and must not be freed until after all the CPU's have gone
  563. * through a quiescent state.
  564. */
  565. void __dev_remove_pack(struct packet_type *pt)
  566. {
  567. struct list_head *head = ptype_head(pt);
  568. struct packet_type *pt1;
  569. if (!head)
  570. return;
  571. spin_lock(&ptype_lock);
  572. list_for_each_entry(pt1, head, list) {
  573. if (pt == pt1) {
  574. list_del_rcu(&pt->list);
  575. goto out;
  576. }
  577. }
  578. pr_warn("dev_remove_pack: %p not found\n", pt);
  579. out:
  580. spin_unlock(&ptype_lock);
  581. }
  582. EXPORT_SYMBOL(__dev_remove_pack);
  583. /**
  584. * dev_remove_pack - remove packet handler
  585. * @pt: packet type declaration
  586. *
  587. * Remove a protocol handler that was previously added to the kernel
  588. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  589. * from the kernel lists and can be freed or reused once this function
  590. * returns.
  591. *
  592. * This call sleeps to guarantee that no CPU is looking at the packet
  593. * type after return.
  594. */
  595. void dev_remove_pack(struct packet_type *pt)
  596. {
  597. __dev_remove_pack(pt);
  598. synchronize_net();
  599. }
  600. EXPORT_SYMBOL(dev_remove_pack);
  601. /*******************************************************************************
  602. *
  603. * Device Interface Subroutines
  604. *
  605. *******************************************************************************/
  606. /**
  607. * dev_get_iflink - get 'iflink' value of a interface
  608. * @dev: targeted interface
  609. *
  610. * Indicates the ifindex the interface is linked to.
  611. * Physical interfaces have the same 'ifindex' and 'iflink' values.
  612. */
  613. int dev_get_iflink(const struct net_device *dev)
  614. {
  615. if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
  616. return dev->netdev_ops->ndo_get_iflink(dev);
  617. return READ_ONCE(dev->ifindex);
  618. }
  619. EXPORT_SYMBOL(dev_get_iflink);
  620. /**
  621. * dev_fill_metadata_dst - Retrieve tunnel egress information.
  622. * @dev: targeted interface
  623. * @skb: The packet.
  624. *
  625. * For better visibility of tunnel traffic OVS needs to retrieve
  626. * egress tunnel information for a packet. Following API allows
  627. * user to get this info.
  628. */
  629. int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
  630. {
  631. struct ip_tunnel_info *info;
  632. if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
  633. return -EINVAL;
  634. info = skb_tunnel_info_unclone(skb);
  635. if (!info)
  636. return -ENOMEM;
  637. if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
  638. return -EINVAL;
  639. return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
  640. }
  641. EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
  642. static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack)
  643. {
  644. int k = stack->num_paths++;
  645. if (k >= NET_DEVICE_PATH_STACK_MAX)
  646. return NULL;
  647. return &stack->path[k];
  648. }
  649. int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
  650. struct net_device_path_stack *stack)
  651. {
  652. const struct net_device *last_dev;
  653. struct net_device_path_ctx ctx = {
  654. .dev = dev,
  655. };
  656. struct net_device_path *path;
  657. int ret = 0;
  658. memcpy(ctx.daddr, daddr, sizeof(ctx.daddr));
  659. stack->num_paths = 0;
  660. while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) {
  661. last_dev = ctx.dev;
  662. path = dev_fwd_path(stack);
  663. if (!path)
  664. return -1;
  665. memset(path, 0, sizeof(struct net_device_path));
  666. ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path);
  667. if (ret < 0)
  668. return -1;
  669. if (WARN_ON_ONCE(last_dev == ctx.dev))
  670. return -1;
  671. }
  672. if (!ctx.dev)
  673. return ret;
  674. path = dev_fwd_path(stack);
  675. if (!path)
  676. return -1;
  677. path->type = DEV_PATH_ETHERNET;
  678. path->dev = ctx.dev;
  679. return ret;
  680. }
  681. EXPORT_SYMBOL_GPL(dev_fill_forward_path);
  682. /* must be called under rcu_read_lock(), as we dont take a reference */
  683. static struct napi_struct *napi_by_id(unsigned int napi_id)
  684. {
  685. unsigned int hash = napi_id % HASH_SIZE(napi_hash);
  686. struct napi_struct *napi;
  687. hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
  688. if (napi->napi_id == napi_id)
  689. return napi;
  690. return NULL;
  691. }
  692. /* must be called under rcu_read_lock(), as we dont take a reference */
  693. static struct napi_struct *
  694. netdev_napi_by_id(struct net *net, unsigned int napi_id)
  695. {
  696. struct napi_struct *napi;
  697. napi = napi_by_id(napi_id);
  698. if (!napi)
  699. return NULL;
  700. if (WARN_ON_ONCE(!napi->dev))
  701. return NULL;
  702. if (!net_eq(net, dev_net(napi->dev)))
  703. return NULL;
  704. return napi;
  705. }
  706. /**
  707. * netdev_napi_by_id_lock() - find a device by NAPI ID and lock it
  708. * @net: the applicable net namespace
  709. * @napi_id: ID of a NAPI of a target device
  710. *
  711. * Find a NAPI instance with @napi_id. Lock its device.
  712. * The device must be in %NETREG_REGISTERED state for lookup to succeed.
  713. * netdev_unlock() must be called to release it.
  714. *
  715. * Return: pointer to NAPI, its device with lock held, NULL if not found.
  716. */
  717. struct napi_struct *
  718. netdev_napi_by_id_lock(struct net *net, unsigned int napi_id)
  719. {
  720. struct napi_struct *napi;
  721. struct net_device *dev;
  722. rcu_read_lock();
  723. napi = netdev_napi_by_id(net, napi_id);
  724. if (!napi || READ_ONCE(napi->dev->reg_state) != NETREG_REGISTERED) {
  725. rcu_read_unlock();
  726. return NULL;
  727. }
  728. dev = napi->dev;
  729. dev_hold(dev);
  730. rcu_read_unlock();
  731. dev = __netdev_put_lock(dev, net);
  732. if (!dev)
  733. return NULL;
  734. rcu_read_lock();
  735. napi = netdev_napi_by_id(net, napi_id);
  736. if (napi && napi->dev != dev)
  737. napi = NULL;
  738. rcu_read_unlock();
  739. if (!napi)
  740. netdev_unlock(dev);
  741. return napi;
  742. }
  743. /**
  744. * __dev_get_by_name - find a device by its name
  745. * @net: the applicable net namespace
  746. * @name: name to find
  747. *
  748. * Find an interface by name. Must be called under RTNL semaphore.
  749. * If the name is found a pointer to the device is returned.
  750. * If the name is not found then %NULL is returned. The
  751. * reference counters are not incremented so the caller must be
  752. * careful with locks.
  753. */
  754. struct net_device *__dev_get_by_name(struct net *net, const char *name)
  755. {
  756. struct netdev_name_node *node_name;
  757. node_name = netdev_name_node_lookup(net, name);
  758. return node_name ? node_name->dev : NULL;
  759. }
  760. EXPORT_SYMBOL(__dev_get_by_name);
  761. /**
  762. * dev_get_by_name_rcu - find a device by its name
  763. * @net: the applicable net namespace
  764. * @name: name to find
  765. *
  766. * Find an interface by name.
  767. * If the name is found a pointer to the device is returned.
  768. * If the name is not found then %NULL is returned.
  769. * The reference counters are not incremented so the caller must be
  770. * careful with locks. The caller must hold RCU lock.
  771. */
  772. struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
  773. {
  774. struct netdev_name_node *node_name;
  775. node_name = netdev_name_node_lookup_rcu(net, name);
  776. return node_name ? node_name->dev : NULL;
  777. }
  778. EXPORT_SYMBOL(dev_get_by_name_rcu);
  779. /* Deprecated for new users, call netdev_get_by_name() instead */
  780. struct net_device *dev_get_by_name(struct net *net, const char *name)
  781. {
  782. struct net_device *dev;
  783. rcu_read_lock();
  784. dev = dev_get_by_name_rcu(net, name);
  785. dev_hold(dev);
  786. rcu_read_unlock();
  787. return dev;
  788. }
  789. EXPORT_SYMBOL(dev_get_by_name);
  790. /**
  791. * netdev_get_by_name() - find a device by its name
  792. * @net: the applicable net namespace
  793. * @name: name to find
  794. * @tracker: tracking object for the acquired reference
  795. * @gfp: allocation flags for the tracker
  796. *
  797. * Find an interface by name. This can be called from any
  798. * context and does its own locking. The returned handle has
  799. * the usage count incremented and the caller must use netdev_put() to
  800. * release it when it is no longer needed. %NULL is returned if no
  801. * matching device is found.
  802. */
  803. struct net_device *netdev_get_by_name(struct net *net, const char *name,
  804. netdevice_tracker *tracker, gfp_t gfp)
  805. {
  806. struct net_device *dev;
  807. dev = dev_get_by_name(net, name);
  808. if (dev)
  809. netdev_tracker_alloc(dev, tracker, gfp);
  810. return dev;
  811. }
  812. EXPORT_SYMBOL(netdev_get_by_name);
  813. /**
  814. * __dev_get_by_index - find a device by its ifindex
  815. * @net: the applicable net namespace
  816. * @ifindex: index of device
  817. *
  818. * Search for an interface by index. Returns %NULL if the device
  819. * is not found or a pointer to the device. The device has not
  820. * had its reference counter increased so the caller must be careful
  821. * about locking. The caller must hold the RTNL semaphore.
  822. */
  823. struct net_device *__dev_get_by_index(struct net *net, int ifindex)
  824. {
  825. struct net_device *dev;
  826. struct hlist_head *head = dev_index_hash(net, ifindex);
  827. hlist_for_each_entry(dev, head, index_hlist)
  828. if (dev->ifindex == ifindex)
  829. return dev;
  830. return NULL;
  831. }
  832. EXPORT_SYMBOL(__dev_get_by_index);
  833. /**
  834. * dev_get_by_index_rcu - find a device by its ifindex
  835. * @net: the applicable net namespace
  836. * @ifindex: index of device
  837. *
  838. * Search for an interface by index. Returns %NULL if the device
  839. * is not found or a pointer to the device. The device has not
  840. * had its reference counter increased so the caller must be careful
  841. * about locking. The caller must hold RCU lock.
  842. */
  843. struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
  844. {
  845. struct net_device *dev;
  846. struct hlist_head *head = dev_index_hash(net, ifindex);
  847. hlist_for_each_entry_rcu(dev, head, index_hlist)
  848. if (dev->ifindex == ifindex)
  849. return dev;
  850. return NULL;
  851. }
  852. EXPORT_SYMBOL(dev_get_by_index_rcu);
  853. /* Deprecated for new users, call netdev_get_by_index() instead */
  854. struct net_device *dev_get_by_index(struct net *net, int ifindex)
  855. {
  856. struct net_device *dev;
  857. rcu_read_lock();
  858. dev = dev_get_by_index_rcu(net, ifindex);
  859. dev_hold(dev);
  860. rcu_read_unlock();
  861. return dev;
  862. }
  863. EXPORT_SYMBOL(dev_get_by_index);
  864. /**
  865. * netdev_get_by_index() - find a device by its ifindex
  866. * @net: the applicable net namespace
  867. * @ifindex: index of device
  868. * @tracker: tracking object for the acquired reference
  869. * @gfp: allocation flags for the tracker
  870. *
  871. * Search for an interface by index. Returns NULL if the device
  872. * is not found or a pointer to the device. The device returned has
  873. * had a reference added and the pointer is safe until the user calls
  874. * netdev_put() to indicate they have finished with it.
  875. */
  876. struct net_device *netdev_get_by_index(struct net *net, int ifindex,
  877. netdevice_tracker *tracker, gfp_t gfp)
  878. {
  879. struct net_device *dev;
  880. dev = dev_get_by_index(net, ifindex);
  881. if (dev)
  882. netdev_tracker_alloc(dev, tracker, gfp);
  883. return dev;
  884. }
  885. EXPORT_SYMBOL(netdev_get_by_index);
  886. /**
  887. * dev_get_by_napi_id - find a device by napi_id
  888. * @napi_id: ID of the NAPI struct
  889. *
  890. * Search for an interface by NAPI ID. Returns %NULL if the device
  891. * is not found or a pointer to the device. The device has not had
  892. * its reference counter increased so the caller must be careful
  893. * about locking. The caller must hold RCU lock.
  894. */
  895. struct net_device *dev_get_by_napi_id(unsigned int napi_id)
  896. {
  897. struct napi_struct *napi;
  898. WARN_ON_ONCE(!rcu_read_lock_held());
  899. if (!napi_id_valid(napi_id))
  900. return NULL;
  901. napi = napi_by_id(napi_id);
  902. return napi ? napi->dev : NULL;
  903. }
  904. /* Release the held reference on the net_device, and if the net_device
  905. * is still registered try to lock the instance lock. If device is being
  906. * unregistered NULL will be returned (but the reference has been released,
  907. * either way!)
  908. *
  909. * This helper is intended for locking net_device after it has been looked up
  910. * using a lockless lookup helper. Lock prevents the instance from going away.
  911. */
  912. struct net_device *__netdev_put_lock(struct net_device *dev, struct net *net)
  913. {
  914. netdev_lock(dev);
  915. if (dev->reg_state > NETREG_REGISTERED ||
  916. dev->moving_ns || !net_eq(dev_net(dev), net)) {
  917. netdev_unlock(dev);
  918. dev_put(dev);
  919. return NULL;
  920. }
  921. dev_put(dev);
  922. return dev;
  923. }
  924. static struct net_device *
  925. __netdev_put_lock_ops_compat(struct net_device *dev, struct net *net)
  926. {
  927. netdev_lock_ops_compat(dev);
  928. if (dev->reg_state > NETREG_REGISTERED ||
  929. dev->moving_ns || !net_eq(dev_net(dev), net)) {
  930. netdev_unlock_ops_compat(dev);
  931. dev_put(dev);
  932. return NULL;
  933. }
  934. dev_put(dev);
  935. return dev;
  936. }
  937. /**
  938. * netdev_get_by_index_lock() - find a device by its ifindex
  939. * @net: the applicable net namespace
  940. * @ifindex: index of device
  941. *
  942. * Search for an interface by index. If a valid device
  943. * with @ifindex is found it will be returned with netdev->lock held.
  944. * netdev_unlock() must be called to release it.
  945. *
  946. * Return: pointer to a device with lock held, NULL if not found.
  947. */
  948. struct net_device *netdev_get_by_index_lock(struct net *net, int ifindex)
  949. {
  950. struct net_device *dev;
  951. dev = dev_get_by_index(net, ifindex);
  952. if (!dev)
  953. return NULL;
  954. return __netdev_put_lock(dev, net);
  955. }
  956. struct net_device *
  957. netdev_get_by_index_lock_ops_compat(struct net *net, int ifindex)
  958. {
  959. struct net_device *dev;
  960. dev = dev_get_by_index(net, ifindex);
  961. if (!dev)
  962. return NULL;
  963. return __netdev_put_lock_ops_compat(dev, net);
  964. }
  965. struct net_device *
  966. netdev_xa_find_lock(struct net *net, struct net_device *dev,
  967. unsigned long *index)
  968. {
  969. if (dev)
  970. netdev_unlock(dev);
  971. do {
  972. rcu_read_lock();
  973. dev = xa_find(&net->dev_by_index, index, ULONG_MAX, XA_PRESENT);
  974. if (!dev) {
  975. rcu_read_unlock();
  976. return NULL;
  977. }
  978. dev_hold(dev);
  979. rcu_read_unlock();
  980. dev = __netdev_put_lock(dev, net);
  981. if (dev)
  982. return dev;
  983. (*index)++;
  984. } while (true);
  985. }
  986. struct net_device *
  987. netdev_xa_find_lock_ops_compat(struct net *net, struct net_device *dev,
  988. unsigned long *index)
  989. {
  990. if (dev)
  991. netdev_unlock_ops_compat(dev);
  992. do {
  993. rcu_read_lock();
  994. dev = xa_find(&net->dev_by_index, index, ULONG_MAX, XA_PRESENT);
  995. if (!dev) {
  996. rcu_read_unlock();
  997. return NULL;
  998. }
  999. dev_hold(dev);
  1000. rcu_read_unlock();
  1001. dev = __netdev_put_lock_ops_compat(dev, net);
  1002. if (dev)
  1003. return dev;
  1004. (*index)++;
  1005. } while (true);
  1006. }
  1007. static DEFINE_SEQLOCK(netdev_rename_lock);
  1008. void netdev_copy_name(struct net_device *dev, char *name)
  1009. {
  1010. unsigned int seq;
  1011. do {
  1012. seq = read_seqbegin(&netdev_rename_lock);
  1013. strscpy(name, dev->name, IFNAMSIZ);
  1014. } while (read_seqretry(&netdev_rename_lock, seq));
  1015. }
  1016. EXPORT_IPV6_MOD_GPL(netdev_copy_name);
  1017. /**
  1018. * netdev_get_name - get a netdevice name, knowing its ifindex.
  1019. * @net: network namespace
  1020. * @name: a pointer to the buffer where the name will be stored.
  1021. * @ifindex: the ifindex of the interface to get the name from.
  1022. */
  1023. int netdev_get_name(struct net *net, char *name, int ifindex)
  1024. {
  1025. struct net_device *dev;
  1026. int ret;
  1027. rcu_read_lock();
  1028. dev = dev_get_by_index_rcu(net, ifindex);
  1029. if (!dev) {
  1030. ret = -ENODEV;
  1031. goto out;
  1032. }
  1033. netdev_copy_name(dev, name);
  1034. ret = 0;
  1035. out:
  1036. rcu_read_unlock();
  1037. return ret;
  1038. }
  1039. static bool dev_addr_cmp(struct net_device *dev, unsigned short type,
  1040. const char *ha)
  1041. {
  1042. return dev->type == type && !memcmp(dev->dev_addr, ha, dev->addr_len);
  1043. }
  1044. /**
  1045. * dev_getbyhwaddr_rcu - find a device by its hardware address
  1046. * @net: the applicable net namespace
  1047. * @type: media type of device
  1048. * @ha: hardware address
  1049. *
  1050. * Search for an interface by MAC address. Returns NULL if the device
  1051. * is not found or a pointer to the device.
  1052. * The caller must hold RCU.
  1053. * The returned device has not had its ref count increased
  1054. * and the caller must therefore be careful about locking
  1055. *
  1056. */
  1057. struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
  1058. const char *ha)
  1059. {
  1060. struct net_device *dev;
  1061. for_each_netdev_rcu(net, dev)
  1062. if (dev_addr_cmp(dev, type, ha))
  1063. return dev;
  1064. return NULL;
  1065. }
  1066. EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
  1067. /**
  1068. * dev_getbyhwaddr() - find a device by its hardware address
  1069. * @net: the applicable net namespace
  1070. * @type: media type of device
  1071. * @ha: hardware address
  1072. *
  1073. * Similar to dev_getbyhwaddr_rcu(), but the owner needs to hold
  1074. * rtnl_lock.
  1075. *
  1076. * Context: rtnl_lock() must be held.
  1077. * Return: pointer to the net_device, or NULL if not found
  1078. */
  1079. struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type,
  1080. const char *ha)
  1081. {
  1082. struct net_device *dev;
  1083. ASSERT_RTNL();
  1084. for_each_netdev(net, dev)
  1085. if (dev_addr_cmp(dev, type, ha))
  1086. return dev;
  1087. return NULL;
  1088. }
  1089. EXPORT_SYMBOL(dev_getbyhwaddr);
  1090. struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
  1091. {
  1092. struct net_device *dev, *ret = NULL;
  1093. rcu_read_lock();
  1094. for_each_netdev_rcu(net, dev)
  1095. if (dev->type == type) {
  1096. dev_hold(dev);
  1097. ret = dev;
  1098. break;
  1099. }
  1100. rcu_read_unlock();
  1101. return ret;
  1102. }
  1103. EXPORT_SYMBOL(dev_getfirstbyhwtype);
  1104. /**
  1105. * netdev_get_by_flags_rcu - find any device with given flags
  1106. * @net: the applicable net namespace
  1107. * @tracker: tracking object for the acquired reference
  1108. * @if_flags: IFF_* values
  1109. * @mask: bitmask of bits in if_flags to check
  1110. *
  1111. * Search for any interface with the given flags.
  1112. *
  1113. * Context: rcu_read_lock() must be held.
  1114. * Returns: NULL if a device is not found or a pointer to the device.
  1115. */
  1116. struct net_device *netdev_get_by_flags_rcu(struct net *net, netdevice_tracker *tracker,
  1117. unsigned short if_flags, unsigned short mask)
  1118. {
  1119. struct net_device *dev;
  1120. for_each_netdev_rcu(net, dev) {
  1121. if (((READ_ONCE(dev->flags) ^ if_flags) & mask) == 0) {
  1122. netdev_hold(dev, tracker, GFP_ATOMIC);
  1123. return dev;
  1124. }
  1125. }
  1126. return NULL;
  1127. }
  1128. EXPORT_IPV6_MOD(netdev_get_by_flags_rcu);
  1129. /**
  1130. * dev_valid_name - check if name is okay for network device
  1131. * @name: name string
  1132. *
  1133. * Network device names need to be valid file names to
  1134. * allow sysfs to work. We also disallow any kind of
  1135. * whitespace.
  1136. */
  1137. bool dev_valid_name(const char *name)
  1138. {
  1139. if (*name == '\0')
  1140. return false;
  1141. if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
  1142. return false;
  1143. if (!strcmp(name, ".") || !strcmp(name, ".."))
  1144. return false;
  1145. while (*name) {
  1146. if (*name == '/' || *name == ':' || isspace(*name))
  1147. return false;
  1148. name++;
  1149. }
  1150. return true;
  1151. }
  1152. EXPORT_SYMBOL(dev_valid_name);
  1153. /**
  1154. * __dev_alloc_name - allocate a name for a device
  1155. * @net: network namespace to allocate the device name in
  1156. * @name: name format string
  1157. * @res: result name string
  1158. *
  1159. * Passed a format string - eg "lt%d" it will try and find a suitable
  1160. * id. It scans list of devices to build up a free map, then chooses
  1161. * the first empty slot. The caller must hold the dev_base or rtnl lock
  1162. * while allocating the name and adding the device in order to avoid
  1163. * duplicates.
  1164. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  1165. * Returns the number of the unit assigned or a negative errno code.
  1166. */
  1167. static int __dev_alloc_name(struct net *net, const char *name, char *res)
  1168. {
  1169. int i = 0;
  1170. const char *p;
  1171. const int max_netdevices = 8*PAGE_SIZE;
  1172. unsigned long *inuse;
  1173. struct net_device *d;
  1174. char buf[IFNAMSIZ];
  1175. /* Verify the string as this thing may have come from the user.
  1176. * There must be one "%d" and no other "%" characters.
  1177. */
  1178. p = strchr(name, '%');
  1179. if (!p || p[1] != 'd' || strchr(p + 2, '%'))
  1180. return -EINVAL;
  1181. /* Use one page as a bit array of possible slots */
  1182. inuse = bitmap_zalloc(max_netdevices, GFP_ATOMIC);
  1183. if (!inuse)
  1184. return -ENOMEM;
  1185. for_each_netdev(net, d) {
  1186. struct netdev_name_node *name_node;
  1187. netdev_for_each_altname(d, name_node) {
  1188. if (!sscanf(name_node->name, name, &i))
  1189. continue;
  1190. if (i < 0 || i >= max_netdevices)
  1191. continue;
  1192. /* avoid cases where sscanf is not exact inverse of printf */
  1193. snprintf(buf, IFNAMSIZ, name, i);
  1194. if (!strncmp(buf, name_node->name, IFNAMSIZ))
  1195. __set_bit(i, inuse);
  1196. }
  1197. if (!sscanf(d->name, name, &i))
  1198. continue;
  1199. if (i < 0 || i >= max_netdevices)
  1200. continue;
  1201. /* avoid cases where sscanf is not exact inverse of printf */
  1202. snprintf(buf, IFNAMSIZ, name, i);
  1203. if (!strncmp(buf, d->name, IFNAMSIZ))
  1204. __set_bit(i, inuse);
  1205. }
  1206. i = find_first_zero_bit(inuse, max_netdevices);
  1207. bitmap_free(inuse);
  1208. if (i == max_netdevices)
  1209. return -ENFILE;
  1210. /* 'res' and 'name' could overlap, use 'buf' as an intermediate buffer */
  1211. strscpy(buf, name, IFNAMSIZ);
  1212. snprintf(res, IFNAMSIZ, buf, i);
  1213. return i;
  1214. }
  1215. /* Returns negative errno or allocated unit id (see __dev_alloc_name()) */
  1216. static int dev_prep_valid_name(struct net *net, struct net_device *dev,
  1217. const char *want_name, char *out_name,
  1218. int dup_errno)
  1219. {
  1220. if (!dev_valid_name(want_name))
  1221. return -EINVAL;
  1222. if (strchr(want_name, '%'))
  1223. return __dev_alloc_name(net, want_name, out_name);
  1224. if (netdev_name_in_use(net, want_name))
  1225. return -dup_errno;
  1226. if (out_name != want_name)
  1227. strscpy(out_name, want_name, IFNAMSIZ);
  1228. return 0;
  1229. }
  1230. /**
  1231. * dev_alloc_name - allocate a name for a device
  1232. * @dev: device
  1233. * @name: name format string
  1234. *
  1235. * Passed a format string - eg "lt%d" it will try and find a suitable
  1236. * id. It scans list of devices to build up a free map, then chooses
  1237. * the first empty slot. The caller must hold the dev_base or rtnl lock
  1238. * while allocating the name and adding the device in order to avoid
  1239. * duplicates.
  1240. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  1241. * Returns the number of the unit assigned or a negative errno code.
  1242. */
  1243. int dev_alloc_name(struct net_device *dev, const char *name)
  1244. {
  1245. return dev_prep_valid_name(dev_net(dev), dev, name, dev->name, ENFILE);
  1246. }
  1247. EXPORT_SYMBOL(dev_alloc_name);
  1248. static int dev_get_valid_name(struct net *net, struct net_device *dev,
  1249. const char *name)
  1250. {
  1251. int ret;
  1252. ret = dev_prep_valid_name(net, dev, name, dev->name, EEXIST);
  1253. return ret < 0 ? ret : 0;
  1254. }
  1255. int netif_change_name(struct net_device *dev, const char *newname)
  1256. {
  1257. struct net *net = dev_net(dev);
  1258. unsigned char old_assign_type;
  1259. char oldname[IFNAMSIZ];
  1260. int err = 0;
  1261. int ret;
  1262. ASSERT_RTNL_NET(net);
  1263. if (!strncmp(newname, dev->name, IFNAMSIZ))
  1264. return 0;
  1265. memcpy(oldname, dev->name, IFNAMSIZ);
  1266. write_seqlock_bh(&netdev_rename_lock);
  1267. err = dev_get_valid_name(net, dev, newname);
  1268. write_sequnlock_bh(&netdev_rename_lock);
  1269. if (err < 0)
  1270. return err;
  1271. if (oldname[0] && !strchr(oldname, '%'))
  1272. netdev_info(dev, "renamed from %s%s\n", oldname,
  1273. dev->flags & IFF_UP ? " (while UP)" : "");
  1274. old_assign_type = dev->name_assign_type;
  1275. WRITE_ONCE(dev->name_assign_type, NET_NAME_RENAMED);
  1276. rollback:
  1277. ret = device_rename(&dev->dev, dev->name);
  1278. if (ret) {
  1279. write_seqlock_bh(&netdev_rename_lock);
  1280. memcpy(dev->name, oldname, IFNAMSIZ);
  1281. write_sequnlock_bh(&netdev_rename_lock);
  1282. WRITE_ONCE(dev->name_assign_type, old_assign_type);
  1283. return ret;
  1284. }
  1285. netdev_adjacent_rename_links(dev, oldname);
  1286. netdev_name_node_del(dev->name_node);
  1287. synchronize_net();
  1288. netdev_name_node_add(net, dev->name_node);
  1289. ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
  1290. ret = notifier_to_errno(ret);
  1291. if (ret) {
  1292. /* err >= 0 after dev_alloc_name() or stores the first errno */
  1293. if (err >= 0) {
  1294. err = ret;
  1295. write_seqlock_bh(&netdev_rename_lock);
  1296. memcpy(dev->name, oldname, IFNAMSIZ);
  1297. write_sequnlock_bh(&netdev_rename_lock);
  1298. memcpy(oldname, newname, IFNAMSIZ);
  1299. WRITE_ONCE(dev->name_assign_type, old_assign_type);
  1300. old_assign_type = NET_NAME_RENAMED;
  1301. goto rollback;
  1302. } else {
  1303. netdev_err(dev, "name change rollback failed: %d\n",
  1304. ret);
  1305. }
  1306. }
  1307. return err;
  1308. }
  1309. int netif_set_alias(struct net_device *dev, const char *alias, size_t len)
  1310. {
  1311. struct dev_ifalias *new_alias = NULL;
  1312. if (len >= IFALIASZ)
  1313. return -EINVAL;
  1314. if (len) {
  1315. new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
  1316. if (!new_alias)
  1317. return -ENOMEM;
  1318. memcpy(new_alias->ifalias, alias, len);
  1319. new_alias->ifalias[len] = 0;
  1320. }
  1321. mutex_lock(&ifalias_mutex);
  1322. new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
  1323. mutex_is_locked(&ifalias_mutex));
  1324. mutex_unlock(&ifalias_mutex);
  1325. if (new_alias)
  1326. kfree_rcu(new_alias, rcuhead);
  1327. return len;
  1328. }
  1329. /**
  1330. * dev_get_alias - get ifalias of a device
  1331. * @dev: device
  1332. * @name: buffer to store name of ifalias
  1333. * @len: size of buffer
  1334. *
  1335. * get ifalias for a device. Caller must make sure dev cannot go
  1336. * away, e.g. rcu read lock or own a reference count to device.
  1337. */
  1338. int dev_get_alias(const struct net_device *dev, char *name, size_t len)
  1339. {
  1340. const struct dev_ifalias *alias;
  1341. int ret = 0;
  1342. rcu_read_lock();
  1343. alias = rcu_dereference(dev->ifalias);
  1344. if (alias)
  1345. ret = snprintf(name, len, "%s", alias->ifalias);
  1346. rcu_read_unlock();
  1347. return ret;
  1348. }
  1349. /**
  1350. * netdev_features_change - device changes features
  1351. * @dev: device to cause notification
  1352. *
  1353. * Called to indicate a device has changed features.
  1354. */
  1355. void netdev_features_change(struct net_device *dev)
  1356. {
  1357. call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
  1358. }
  1359. EXPORT_SYMBOL(netdev_features_change);
  1360. void netif_state_change(struct net_device *dev)
  1361. {
  1362. netdev_ops_assert_locked_or_invisible(dev);
  1363. if (dev->flags & IFF_UP) {
  1364. struct netdev_notifier_change_info change_info = {
  1365. .info.dev = dev,
  1366. };
  1367. call_netdevice_notifiers_info(NETDEV_CHANGE,
  1368. &change_info.info);
  1369. rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL, 0, NULL);
  1370. }
  1371. }
  1372. /**
  1373. * __netdev_notify_peers - notify network peers about existence of @dev,
  1374. * to be called when rtnl lock is already held.
  1375. * @dev: network device
  1376. *
  1377. * Generate traffic such that interested network peers are aware of
  1378. * @dev, such as by generating a gratuitous ARP. This may be used when
  1379. * a device wants to inform the rest of the network about some sort of
  1380. * reconfiguration such as a failover event or virtual machine
  1381. * migration.
  1382. */
  1383. void __netdev_notify_peers(struct net_device *dev)
  1384. {
  1385. ASSERT_RTNL();
  1386. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
  1387. call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
  1388. }
  1389. EXPORT_SYMBOL(__netdev_notify_peers);
  1390. /**
  1391. * netdev_notify_peers - notify network peers about existence of @dev
  1392. * @dev: network device
  1393. *
  1394. * Generate traffic such that interested network peers are aware of
  1395. * @dev, such as by generating a gratuitous ARP. This may be used when
  1396. * a device wants to inform the rest of the network about some sort of
  1397. * reconfiguration such as a failover event or virtual machine
  1398. * migration.
  1399. */
  1400. void netdev_notify_peers(struct net_device *dev)
  1401. {
  1402. rtnl_lock();
  1403. __netdev_notify_peers(dev);
  1404. rtnl_unlock();
  1405. }
  1406. EXPORT_SYMBOL(netdev_notify_peers);
  1407. static int napi_threaded_poll(void *data);
  1408. static int napi_kthread_create(struct napi_struct *n)
  1409. {
  1410. int err = 0;
  1411. /* Create and wake up the kthread once to put it in
  1412. * TASK_INTERRUPTIBLE mode to avoid the blocked task
  1413. * warning and work with loadavg.
  1414. */
  1415. n->thread = kthread_run(napi_threaded_poll, n, "napi/%s-%d",
  1416. n->dev->name, n->napi_id);
  1417. if (IS_ERR(n->thread)) {
  1418. err = PTR_ERR(n->thread);
  1419. pr_err("kthread_run failed with err %d\n", err);
  1420. n->thread = NULL;
  1421. }
  1422. return err;
  1423. }
  1424. static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
  1425. {
  1426. const struct net_device_ops *ops = dev->netdev_ops;
  1427. int ret;
  1428. ASSERT_RTNL();
  1429. dev_addr_check(dev);
  1430. if (!netif_device_present(dev)) {
  1431. /* may be detached because parent is runtime-suspended */
  1432. if (dev->dev.parent)
  1433. pm_runtime_resume(dev->dev.parent);
  1434. if (!netif_device_present(dev))
  1435. return -ENODEV;
  1436. }
  1437. /* Block netpoll from trying to do any rx path servicing.
  1438. * If we don't do this there is a chance ndo_poll_controller
  1439. * or ndo_poll may be running while we open the device
  1440. */
  1441. netpoll_poll_disable(dev);
  1442. ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
  1443. ret = notifier_to_errno(ret);
  1444. if (ret)
  1445. return ret;
  1446. set_bit(__LINK_STATE_START, &dev->state);
  1447. netdev_ops_assert_locked(dev);
  1448. if (ops->ndo_validate_addr)
  1449. ret = ops->ndo_validate_addr(dev);
  1450. if (!ret && ops->ndo_open)
  1451. ret = ops->ndo_open(dev);
  1452. netpoll_poll_enable(dev);
  1453. if (ret)
  1454. clear_bit(__LINK_STATE_START, &dev->state);
  1455. else {
  1456. netif_set_up(dev, true);
  1457. dev_set_rx_mode(dev);
  1458. dev_activate(dev);
  1459. add_device_randomness(dev->dev_addr, dev->addr_len);
  1460. }
  1461. return ret;
  1462. }
  1463. int netif_open(struct net_device *dev, struct netlink_ext_ack *extack)
  1464. {
  1465. int ret;
  1466. if (dev->flags & IFF_UP)
  1467. return 0;
  1468. ret = __dev_open(dev, extack);
  1469. if (ret < 0)
  1470. return ret;
  1471. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP | IFF_RUNNING, GFP_KERNEL, 0, NULL);
  1472. call_netdevice_notifiers(NETDEV_UP, dev);
  1473. return ret;
  1474. }
  1475. static void __dev_close_many(struct list_head *head)
  1476. {
  1477. struct net_device *dev;
  1478. ASSERT_RTNL();
  1479. might_sleep();
  1480. list_for_each_entry(dev, head, close_list) {
  1481. /* Temporarily disable netpoll until the interface is down */
  1482. netpoll_poll_disable(dev);
  1483. call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
  1484. clear_bit(__LINK_STATE_START, &dev->state);
  1485. /* Synchronize to scheduled poll. We cannot touch poll list, it
  1486. * can be even on different cpu. So just clear netif_running().
  1487. *
  1488. * dev->stop() will invoke napi_disable() on all of it's
  1489. * napi_struct instances on this device.
  1490. */
  1491. smp_mb__after_atomic(); /* Commit netif_running(). */
  1492. }
  1493. dev_deactivate_many(head);
  1494. list_for_each_entry(dev, head, close_list) {
  1495. const struct net_device_ops *ops = dev->netdev_ops;
  1496. /*
  1497. * Call the device specific close. This cannot fail.
  1498. * Only if device is UP
  1499. *
  1500. * We allow it to be called even after a DETACH hot-plug
  1501. * event.
  1502. */
  1503. netdev_ops_assert_locked(dev);
  1504. if (ops->ndo_stop)
  1505. ops->ndo_stop(dev);
  1506. netif_set_up(dev, false);
  1507. netpoll_poll_enable(dev);
  1508. }
  1509. }
  1510. static void __dev_close(struct net_device *dev)
  1511. {
  1512. LIST_HEAD(single);
  1513. list_add(&dev->close_list, &single);
  1514. __dev_close_many(&single);
  1515. list_del(&single);
  1516. }
  1517. void netif_close_many(struct list_head *head, bool unlink)
  1518. {
  1519. struct net_device *dev, *tmp;
  1520. /* Remove the devices that don't need to be closed */
  1521. list_for_each_entry_safe(dev, tmp, head, close_list)
  1522. if (!(dev->flags & IFF_UP))
  1523. list_del_init(&dev->close_list);
  1524. __dev_close_many(head);
  1525. list_for_each_entry_safe(dev, tmp, head, close_list) {
  1526. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP | IFF_RUNNING, GFP_KERNEL, 0, NULL);
  1527. call_netdevice_notifiers(NETDEV_DOWN, dev);
  1528. if (unlink)
  1529. list_del_init(&dev->close_list);
  1530. }
  1531. }
  1532. EXPORT_SYMBOL_NS_GPL(netif_close_many, "NETDEV_INTERNAL");
  1533. void netif_close(struct net_device *dev)
  1534. {
  1535. if (dev->flags & IFF_UP) {
  1536. LIST_HEAD(single);
  1537. list_add(&dev->close_list, &single);
  1538. netif_close_many(&single, true);
  1539. list_del(&single);
  1540. }
  1541. }
  1542. EXPORT_SYMBOL(netif_close);
  1543. void netif_disable_lro(struct net_device *dev)
  1544. {
  1545. struct net_device *lower_dev;
  1546. struct list_head *iter;
  1547. dev->wanted_features &= ~NETIF_F_LRO;
  1548. netdev_update_features(dev);
  1549. if (unlikely(dev->features & NETIF_F_LRO))
  1550. netdev_WARN(dev, "failed to disable LRO!\n");
  1551. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  1552. netdev_lock_ops(lower_dev);
  1553. netif_disable_lro(lower_dev);
  1554. netdev_unlock_ops(lower_dev);
  1555. }
  1556. }
  1557. EXPORT_IPV6_MOD(netif_disable_lro);
  1558. /**
  1559. * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
  1560. * @dev: device
  1561. *
  1562. * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
  1563. * called under RTNL. This is needed if Generic XDP is installed on
  1564. * the device.
  1565. */
  1566. static void dev_disable_gro_hw(struct net_device *dev)
  1567. {
  1568. dev->wanted_features &= ~NETIF_F_GRO_HW;
  1569. netdev_update_features(dev);
  1570. if (unlikely(dev->features & NETIF_F_GRO_HW))
  1571. netdev_WARN(dev, "failed to disable GRO_HW!\n");
  1572. }
  1573. const char *netdev_cmd_to_name(enum netdev_cmd cmd)
  1574. {
  1575. #define N(val) \
  1576. case NETDEV_##val: \
  1577. return "NETDEV_" __stringify(val);
  1578. switch (cmd) {
  1579. N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
  1580. N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
  1581. N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
  1582. N(POST_INIT) N(PRE_UNINIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN)
  1583. N(CHANGEUPPER) N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA)
  1584. N(BONDING_INFO) N(PRECHANGEUPPER) N(CHANGELOWERSTATE)
  1585. N(UDP_TUNNEL_PUSH_INFO) N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
  1586. N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
  1587. N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
  1588. N(PRE_CHANGEADDR) N(OFFLOAD_XSTATS_ENABLE) N(OFFLOAD_XSTATS_DISABLE)
  1589. N(OFFLOAD_XSTATS_REPORT_USED) N(OFFLOAD_XSTATS_REPORT_DELTA)
  1590. N(XDP_FEAT_CHANGE)
  1591. }
  1592. #undef N
  1593. return "UNKNOWN_NETDEV_EVENT";
  1594. }
  1595. EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
  1596. static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
  1597. struct net_device *dev)
  1598. {
  1599. struct netdev_notifier_info info = {
  1600. .dev = dev,
  1601. };
  1602. return nb->notifier_call(nb, val, &info);
  1603. }
  1604. static int call_netdevice_register_notifiers(struct notifier_block *nb,
  1605. struct net_device *dev)
  1606. {
  1607. int err;
  1608. err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
  1609. err = notifier_to_errno(err);
  1610. if (err)
  1611. return err;
  1612. if (!(dev->flags & IFF_UP))
  1613. return 0;
  1614. call_netdevice_notifier(nb, NETDEV_UP, dev);
  1615. return 0;
  1616. }
  1617. static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
  1618. struct net_device *dev)
  1619. {
  1620. if (dev->flags & IFF_UP) {
  1621. call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
  1622. dev);
  1623. call_netdevice_notifier(nb, NETDEV_DOWN, dev);
  1624. }
  1625. call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
  1626. }
  1627. static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
  1628. struct net *net)
  1629. {
  1630. struct net_device *dev;
  1631. int err;
  1632. for_each_netdev(net, dev) {
  1633. netdev_lock_ops(dev);
  1634. err = call_netdevice_register_notifiers(nb, dev);
  1635. netdev_unlock_ops(dev);
  1636. if (err)
  1637. goto rollback;
  1638. }
  1639. return 0;
  1640. rollback:
  1641. for_each_netdev_continue_reverse(net, dev)
  1642. call_netdevice_unregister_notifiers(nb, dev);
  1643. return err;
  1644. }
  1645. static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
  1646. struct net *net)
  1647. {
  1648. struct net_device *dev;
  1649. for_each_netdev(net, dev)
  1650. call_netdevice_unregister_notifiers(nb, dev);
  1651. }
  1652. static int dev_boot_phase = 1;
  1653. /**
  1654. * register_netdevice_notifier - register a network notifier block
  1655. * @nb: notifier
  1656. *
  1657. * Register a notifier to be called when network device events occur.
  1658. * The notifier passed is linked into the kernel structures and must
  1659. * not be reused until it has been unregistered. A negative errno code
  1660. * is returned on a failure.
  1661. *
  1662. * When registered all registration and up events are replayed
  1663. * to the new notifier to allow device to have a race free
  1664. * view of the network device list.
  1665. */
  1666. int register_netdevice_notifier(struct notifier_block *nb)
  1667. {
  1668. struct net *net;
  1669. int err;
  1670. /* Close race with setup_net() and cleanup_net() */
  1671. down_write(&pernet_ops_rwsem);
  1672. /* When RTNL is removed, we need protection for netdev_chain. */
  1673. rtnl_lock();
  1674. err = raw_notifier_chain_register(&netdev_chain, nb);
  1675. if (err)
  1676. goto unlock;
  1677. if (dev_boot_phase)
  1678. goto unlock;
  1679. for_each_net(net) {
  1680. __rtnl_net_lock(net);
  1681. err = call_netdevice_register_net_notifiers(nb, net);
  1682. __rtnl_net_unlock(net);
  1683. if (err)
  1684. goto rollback;
  1685. }
  1686. unlock:
  1687. rtnl_unlock();
  1688. up_write(&pernet_ops_rwsem);
  1689. return err;
  1690. rollback:
  1691. for_each_net_continue_reverse(net) {
  1692. __rtnl_net_lock(net);
  1693. call_netdevice_unregister_net_notifiers(nb, net);
  1694. __rtnl_net_unlock(net);
  1695. }
  1696. raw_notifier_chain_unregister(&netdev_chain, nb);
  1697. goto unlock;
  1698. }
  1699. EXPORT_SYMBOL(register_netdevice_notifier);
  1700. /**
  1701. * unregister_netdevice_notifier - unregister a network notifier block
  1702. * @nb: notifier
  1703. *
  1704. * Unregister a notifier previously registered by
  1705. * register_netdevice_notifier(). The notifier is unlinked into the
  1706. * kernel structures and may then be reused. A negative errno code
  1707. * is returned on a failure.
  1708. *
  1709. * After unregistering unregister and down device events are synthesized
  1710. * for all devices on the device list to the removed notifier to remove
  1711. * the need for special case cleanup code.
  1712. */
  1713. int unregister_netdevice_notifier(struct notifier_block *nb)
  1714. {
  1715. struct net *net;
  1716. int err;
  1717. /* Close race with setup_net() and cleanup_net() */
  1718. down_write(&pernet_ops_rwsem);
  1719. rtnl_lock();
  1720. err = raw_notifier_chain_unregister(&netdev_chain, nb);
  1721. if (err)
  1722. goto unlock;
  1723. for_each_net(net) {
  1724. __rtnl_net_lock(net);
  1725. call_netdevice_unregister_net_notifiers(nb, net);
  1726. __rtnl_net_unlock(net);
  1727. }
  1728. unlock:
  1729. rtnl_unlock();
  1730. up_write(&pernet_ops_rwsem);
  1731. return err;
  1732. }
  1733. EXPORT_SYMBOL(unregister_netdevice_notifier);
  1734. static int __register_netdevice_notifier_net(struct net *net,
  1735. struct notifier_block *nb,
  1736. bool ignore_call_fail)
  1737. {
  1738. int err;
  1739. err = raw_notifier_chain_register(&net->netdev_chain, nb);
  1740. if (err)
  1741. return err;
  1742. if (dev_boot_phase)
  1743. return 0;
  1744. err = call_netdevice_register_net_notifiers(nb, net);
  1745. if (err && !ignore_call_fail)
  1746. goto chain_unregister;
  1747. return 0;
  1748. chain_unregister:
  1749. raw_notifier_chain_unregister(&net->netdev_chain, nb);
  1750. return err;
  1751. }
  1752. static int __unregister_netdevice_notifier_net(struct net *net,
  1753. struct notifier_block *nb)
  1754. {
  1755. int err;
  1756. err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
  1757. if (err)
  1758. return err;
  1759. call_netdevice_unregister_net_notifiers(nb, net);
  1760. return 0;
  1761. }
  1762. /**
  1763. * register_netdevice_notifier_net - register a per-netns network notifier block
  1764. * @net: network namespace
  1765. * @nb: notifier
  1766. *
  1767. * Register a notifier to be called when network device events occur.
  1768. * The notifier passed is linked into the kernel structures and must
  1769. * not be reused until it has been unregistered. A negative errno code
  1770. * is returned on a failure.
  1771. *
  1772. * When registered all registration and up events are replayed
  1773. * to the new notifier to allow device to have a race free
  1774. * view of the network device list.
  1775. */
  1776. int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
  1777. {
  1778. int err;
  1779. rtnl_net_lock(net);
  1780. err = __register_netdevice_notifier_net(net, nb, false);
  1781. rtnl_net_unlock(net);
  1782. return err;
  1783. }
  1784. EXPORT_SYMBOL(register_netdevice_notifier_net);
  1785. /**
  1786. * unregister_netdevice_notifier_net - unregister a per-netns
  1787. * network notifier block
  1788. * @net: network namespace
  1789. * @nb: notifier
  1790. *
  1791. * Unregister a notifier previously registered by
  1792. * register_netdevice_notifier_net(). The notifier is unlinked from the
  1793. * kernel structures and may then be reused. A negative errno code
  1794. * is returned on a failure.
  1795. *
  1796. * After unregistering unregister and down device events are synthesized
  1797. * for all devices on the device list to the removed notifier to remove
  1798. * the need for special case cleanup code.
  1799. */
  1800. int unregister_netdevice_notifier_net(struct net *net,
  1801. struct notifier_block *nb)
  1802. {
  1803. int err;
  1804. rtnl_net_lock(net);
  1805. err = __unregister_netdevice_notifier_net(net, nb);
  1806. rtnl_net_unlock(net);
  1807. return err;
  1808. }
  1809. EXPORT_SYMBOL(unregister_netdevice_notifier_net);
  1810. static void __move_netdevice_notifier_net(struct net *src_net,
  1811. struct net *dst_net,
  1812. struct notifier_block *nb)
  1813. {
  1814. __unregister_netdevice_notifier_net(src_net, nb);
  1815. __register_netdevice_notifier_net(dst_net, nb, true);
  1816. }
  1817. static void rtnl_net_dev_lock(struct net_device *dev)
  1818. {
  1819. bool again;
  1820. do {
  1821. struct net *net;
  1822. again = false;
  1823. /* netns might be being dismantled. */
  1824. rcu_read_lock();
  1825. net = dev_net_rcu(dev);
  1826. net_passive_inc(net);
  1827. rcu_read_unlock();
  1828. rtnl_net_lock(net);
  1829. #ifdef CONFIG_NET_NS
  1830. /* dev might have been moved to another netns. */
  1831. if (!net_eq(net, rcu_access_pointer(dev->nd_net.net))) {
  1832. rtnl_net_unlock(net);
  1833. net_passive_dec(net);
  1834. again = true;
  1835. }
  1836. #endif
  1837. } while (again);
  1838. }
  1839. static void rtnl_net_dev_unlock(struct net_device *dev)
  1840. {
  1841. struct net *net = dev_net(dev);
  1842. rtnl_net_unlock(net);
  1843. net_passive_dec(net);
  1844. }
  1845. int register_netdevice_notifier_dev_net(struct net_device *dev,
  1846. struct notifier_block *nb,
  1847. struct netdev_net_notifier *nn)
  1848. {
  1849. int err;
  1850. rtnl_net_dev_lock(dev);
  1851. err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
  1852. if (!err) {
  1853. nn->nb = nb;
  1854. list_add(&nn->list, &dev->net_notifier_list);
  1855. }
  1856. rtnl_net_dev_unlock(dev);
  1857. return err;
  1858. }
  1859. EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
  1860. int unregister_netdevice_notifier_dev_net(struct net_device *dev,
  1861. struct notifier_block *nb,
  1862. struct netdev_net_notifier *nn)
  1863. {
  1864. int err;
  1865. rtnl_net_dev_lock(dev);
  1866. list_del(&nn->list);
  1867. err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
  1868. rtnl_net_dev_unlock(dev);
  1869. return err;
  1870. }
  1871. EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
  1872. static void move_netdevice_notifiers_dev_net(struct net_device *dev,
  1873. struct net *net)
  1874. {
  1875. struct netdev_net_notifier *nn;
  1876. list_for_each_entry(nn, &dev->net_notifier_list, list)
  1877. __move_netdevice_notifier_net(dev_net(dev), net, nn->nb);
  1878. }
  1879. /**
  1880. * call_netdevice_notifiers_info - call all network notifier blocks
  1881. * @val: value passed unmodified to notifier function
  1882. * @info: notifier information data
  1883. *
  1884. * Call all network notifier blocks. Parameters and return value
  1885. * are as for raw_notifier_call_chain().
  1886. */
  1887. int call_netdevice_notifiers_info(unsigned long val,
  1888. struct netdev_notifier_info *info)
  1889. {
  1890. struct net *net = dev_net(info->dev);
  1891. int ret;
  1892. ASSERT_RTNL();
  1893. /* Run per-netns notifier block chain first, then run the global one.
  1894. * Hopefully, one day, the global one is going to be removed after
  1895. * all notifier block registrators get converted to be per-netns.
  1896. */
  1897. ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
  1898. if (ret & NOTIFY_STOP_MASK)
  1899. return ret;
  1900. return raw_notifier_call_chain(&netdev_chain, val, info);
  1901. }
  1902. /**
  1903. * call_netdevice_notifiers_info_robust - call per-netns notifier blocks
  1904. * for and rollback on error
  1905. * @val_up: value passed unmodified to notifier function
  1906. * @val_down: value passed unmodified to the notifier function when
  1907. * recovering from an error on @val_up
  1908. * @info: notifier information data
  1909. *
  1910. * Call all per-netns network notifier blocks, but not notifier blocks on
  1911. * the global notifier chain. Parameters and return value are as for
  1912. * raw_notifier_call_chain_robust().
  1913. */
  1914. static int
  1915. call_netdevice_notifiers_info_robust(unsigned long val_up,
  1916. unsigned long val_down,
  1917. struct netdev_notifier_info *info)
  1918. {
  1919. struct net *net = dev_net(info->dev);
  1920. ASSERT_RTNL();
  1921. return raw_notifier_call_chain_robust(&net->netdev_chain,
  1922. val_up, val_down, info);
  1923. }
  1924. static int call_netdevice_notifiers_extack(unsigned long val,
  1925. struct net_device *dev,
  1926. struct netlink_ext_ack *extack)
  1927. {
  1928. struct netdev_notifier_info info = {
  1929. .dev = dev,
  1930. .extack = extack,
  1931. };
  1932. return call_netdevice_notifiers_info(val, &info);
  1933. }
  1934. /**
  1935. * call_netdevice_notifiers - call all network notifier blocks
  1936. * @val: value passed unmodified to notifier function
  1937. * @dev: net_device pointer passed unmodified to notifier function
  1938. *
  1939. * Call all network notifier blocks. Parameters and return value
  1940. * are as for raw_notifier_call_chain().
  1941. */
  1942. int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
  1943. {
  1944. return call_netdevice_notifiers_extack(val, dev, NULL);
  1945. }
  1946. EXPORT_SYMBOL(call_netdevice_notifiers);
  1947. /**
  1948. * call_netdevice_notifiers_mtu - call all network notifier blocks
  1949. * @val: value passed unmodified to notifier function
  1950. * @dev: net_device pointer passed unmodified to notifier function
  1951. * @arg: additional u32 argument passed to the notifier function
  1952. *
  1953. * Call all network notifier blocks. Parameters and return value
  1954. * are as for raw_notifier_call_chain().
  1955. */
  1956. static int call_netdevice_notifiers_mtu(unsigned long val,
  1957. struct net_device *dev, u32 arg)
  1958. {
  1959. struct netdev_notifier_info_ext info = {
  1960. .info.dev = dev,
  1961. .ext.mtu = arg,
  1962. };
  1963. BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
  1964. return call_netdevice_notifiers_info(val, &info.info);
  1965. }
  1966. #ifdef CONFIG_NET_INGRESS
  1967. static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
  1968. void net_inc_ingress_queue(void)
  1969. {
  1970. static_branch_inc(&ingress_needed_key);
  1971. }
  1972. EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
  1973. void net_dec_ingress_queue(void)
  1974. {
  1975. static_branch_dec(&ingress_needed_key);
  1976. }
  1977. EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
  1978. #endif
  1979. #ifdef CONFIG_NET_EGRESS
  1980. static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
  1981. void net_inc_egress_queue(void)
  1982. {
  1983. static_branch_inc(&egress_needed_key);
  1984. }
  1985. EXPORT_SYMBOL_GPL(net_inc_egress_queue);
  1986. void net_dec_egress_queue(void)
  1987. {
  1988. static_branch_dec(&egress_needed_key);
  1989. }
  1990. EXPORT_SYMBOL_GPL(net_dec_egress_queue);
  1991. #endif
  1992. #ifdef CONFIG_NET_CLS_ACT
  1993. DEFINE_STATIC_KEY_FALSE(tcf_sw_enabled_key);
  1994. EXPORT_SYMBOL(tcf_sw_enabled_key);
  1995. #endif
  1996. DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
  1997. EXPORT_SYMBOL(netstamp_needed_key);
  1998. #ifdef CONFIG_JUMP_LABEL
  1999. static atomic_t netstamp_needed_deferred;
  2000. static atomic_t netstamp_wanted;
  2001. static void netstamp_clear(struct work_struct *work)
  2002. {
  2003. int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
  2004. int wanted;
  2005. wanted = atomic_add_return(deferred, &netstamp_wanted);
  2006. if (wanted > 0)
  2007. static_branch_enable(&netstamp_needed_key);
  2008. else
  2009. static_branch_disable(&netstamp_needed_key);
  2010. }
  2011. static DECLARE_WORK(netstamp_work, netstamp_clear);
  2012. #endif
  2013. void net_enable_timestamp(void)
  2014. {
  2015. #ifdef CONFIG_JUMP_LABEL
  2016. int wanted = atomic_read(&netstamp_wanted);
  2017. while (wanted > 0) {
  2018. if (atomic_try_cmpxchg(&netstamp_wanted, &wanted, wanted + 1))
  2019. return;
  2020. }
  2021. atomic_inc(&netstamp_needed_deferred);
  2022. schedule_work(&netstamp_work);
  2023. #else
  2024. static_branch_inc(&netstamp_needed_key);
  2025. #endif
  2026. }
  2027. EXPORT_SYMBOL(net_enable_timestamp);
  2028. void net_disable_timestamp(void)
  2029. {
  2030. #ifdef CONFIG_JUMP_LABEL
  2031. int wanted = atomic_read(&netstamp_wanted);
  2032. while (wanted > 1) {
  2033. if (atomic_try_cmpxchg(&netstamp_wanted, &wanted, wanted - 1))
  2034. return;
  2035. }
  2036. atomic_dec(&netstamp_needed_deferred);
  2037. schedule_work(&netstamp_work);
  2038. #else
  2039. static_branch_dec(&netstamp_needed_key);
  2040. #endif
  2041. }
  2042. EXPORT_SYMBOL(net_disable_timestamp);
  2043. static inline void net_timestamp_set(struct sk_buff *skb)
  2044. {
  2045. skb->tstamp = 0;
  2046. skb->tstamp_type = SKB_CLOCK_REALTIME;
  2047. if (static_branch_unlikely(&netstamp_needed_key))
  2048. skb->tstamp = ktime_get_real();
  2049. }
  2050. #define net_timestamp_check(COND, SKB) \
  2051. if (static_branch_unlikely(&netstamp_needed_key)) { \
  2052. if ((COND) && !(SKB)->tstamp) \
  2053. (SKB)->tstamp = ktime_get_real(); \
  2054. } \
  2055. bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
  2056. {
  2057. return __is_skb_forwardable(dev, skb, true);
  2058. }
  2059. EXPORT_SYMBOL_GPL(is_skb_forwardable);
  2060. static int __dev_forward_skb2(struct net_device *dev, struct sk_buff *skb,
  2061. bool check_mtu)
  2062. {
  2063. int ret = ____dev_forward_skb(dev, skb, check_mtu);
  2064. if (likely(!ret)) {
  2065. skb->protocol = eth_type_trans(skb, dev);
  2066. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  2067. }
  2068. return ret;
  2069. }
  2070. int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
  2071. {
  2072. return __dev_forward_skb2(dev, skb, true);
  2073. }
  2074. EXPORT_SYMBOL_GPL(__dev_forward_skb);
  2075. /**
  2076. * dev_forward_skb - loopback an skb to another netif
  2077. *
  2078. * @dev: destination network device
  2079. * @skb: buffer to forward
  2080. *
  2081. * return values:
  2082. * NET_RX_SUCCESS (no congestion)
  2083. * NET_RX_DROP (packet was dropped, but freed)
  2084. *
  2085. * dev_forward_skb can be used for injecting an skb from the
  2086. * start_xmit function of one device into the receive queue
  2087. * of another device.
  2088. *
  2089. * The receiving device may be in another namespace, so
  2090. * we have to clear all information in the skb that could
  2091. * impact namespace isolation.
  2092. */
  2093. int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
  2094. {
  2095. return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
  2096. }
  2097. EXPORT_SYMBOL_GPL(dev_forward_skb);
  2098. int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb)
  2099. {
  2100. return __dev_forward_skb2(dev, skb, false) ?: netif_rx_internal(skb);
  2101. }
  2102. static int deliver_skb(struct sk_buff *skb,
  2103. struct packet_type *pt_prev,
  2104. struct net_device *orig_dev)
  2105. {
  2106. if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
  2107. return -ENOMEM;
  2108. refcount_inc(&skb->users);
  2109. return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  2110. }
  2111. static inline void deliver_ptype_list_skb(struct sk_buff *skb,
  2112. struct packet_type **pt,
  2113. struct net_device *orig_dev,
  2114. __be16 type,
  2115. struct list_head *ptype_list)
  2116. {
  2117. struct packet_type *ptype, *pt_prev = *pt;
  2118. list_for_each_entry_rcu(ptype, ptype_list, list) {
  2119. if (ptype->type != type)
  2120. continue;
  2121. if (unlikely(pt_prev))
  2122. deliver_skb(skb, pt_prev, orig_dev);
  2123. pt_prev = ptype;
  2124. }
  2125. *pt = pt_prev;
  2126. }
  2127. static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
  2128. {
  2129. if (!ptype->af_packet_priv || !skb->sk)
  2130. return false;
  2131. if (ptype->id_match)
  2132. return ptype->id_match(ptype, skb->sk);
  2133. else if ((struct sock *)ptype->af_packet_priv == skb->sk)
  2134. return true;
  2135. return false;
  2136. }
  2137. /**
  2138. * dev_nit_active_rcu - return true if any network interface taps are in use
  2139. *
  2140. * The caller must hold the RCU lock
  2141. *
  2142. * @dev: network device to check for the presence of taps
  2143. */
  2144. bool dev_nit_active_rcu(const struct net_device *dev)
  2145. {
  2146. /* Callers may hold either RCU or RCU BH lock */
  2147. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
  2148. return !list_empty(&dev_net(dev)->ptype_all) ||
  2149. !list_empty(&dev->ptype_all);
  2150. }
  2151. EXPORT_SYMBOL_GPL(dev_nit_active_rcu);
  2152. /*
  2153. * Support routine. Sends outgoing frames to any network
  2154. * taps currently in use.
  2155. */
  2156. void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
  2157. {
  2158. struct packet_type *ptype, *pt_prev = NULL;
  2159. struct list_head *ptype_list;
  2160. struct sk_buff *skb2 = NULL;
  2161. rcu_read_lock();
  2162. ptype_list = &dev_net_rcu(dev)->ptype_all;
  2163. again:
  2164. list_for_each_entry_rcu(ptype, ptype_list, list) {
  2165. if (READ_ONCE(ptype->ignore_outgoing))
  2166. continue;
  2167. /* Never send packets back to the socket
  2168. * they originated from - MvS (miquels@drinkel.ow.org)
  2169. */
  2170. if (skb_loop_sk(ptype, skb))
  2171. continue;
  2172. if (unlikely(pt_prev)) {
  2173. deliver_skb(skb2, pt_prev, skb->dev);
  2174. pt_prev = ptype;
  2175. continue;
  2176. }
  2177. /* need to clone skb, done only once */
  2178. skb2 = skb_clone(skb, GFP_ATOMIC);
  2179. if (!skb2)
  2180. goto out_unlock;
  2181. net_timestamp_set(skb2);
  2182. /* skb->nh should be correctly
  2183. * set by sender, so that the second statement is
  2184. * just protection against buggy protocols.
  2185. */
  2186. skb_reset_mac_header(skb2);
  2187. if (skb_network_header(skb2) < skb2->data ||
  2188. skb_network_header(skb2) > skb_tail_pointer(skb2)) {
  2189. net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
  2190. ntohs(skb2->protocol),
  2191. dev->name);
  2192. skb_reset_network_header(skb2);
  2193. }
  2194. skb2->transport_header = skb2->network_header;
  2195. skb2->pkt_type = PACKET_OUTGOING;
  2196. pt_prev = ptype;
  2197. }
  2198. if (ptype_list != &dev->ptype_all) {
  2199. ptype_list = &dev->ptype_all;
  2200. goto again;
  2201. }
  2202. out_unlock:
  2203. if (pt_prev) {
  2204. if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
  2205. pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
  2206. else
  2207. kfree_skb(skb2);
  2208. }
  2209. rcu_read_unlock();
  2210. }
  2211. EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
  2212. /**
  2213. * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
  2214. * @dev: Network device
  2215. * @txq: number of queues available
  2216. *
  2217. * If real_num_tx_queues is changed the tc mappings may no longer be
  2218. * valid. To resolve this verify the tc mapping remains valid and if
  2219. * not NULL the mapping. With no priorities mapping to this
  2220. * offset/count pair it will no longer be used. In the worst case TC0
  2221. * is invalid nothing can be done so disable priority mappings. If is
  2222. * expected that drivers will fix this mapping if they can before
  2223. * calling netif_set_real_num_tx_queues.
  2224. */
  2225. static void netif_setup_tc(struct net_device *dev, unsigned int txq)
  2226. {
  2227. int i;
  2228. struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
  2229. /* If TC0 is invalidated disable TC mapping */
  2230. if (tc->offset + tc->count > txq) {
  2231. netdev_warn(dev, "Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
  2232. dev->num_tc = 0;
  2233. return;
  2234. }
  2235. /* Invalidated prio to tc mappings set to TC0 */
  2236. for (i = 1; i < TC_BITMASK + 1; i++) {
  2237. int q = netdev_get_prio_tc_map(dev, i);
  2238. tc = &dev->tc_to_txq[q];
  2239. if (tc->offset + tc->count > txq) {
  2240. netdev_warn(dev, "Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
  2241. i, q);
  2242. netdev_set_prio_tc_map(dev, i, 0);
  2243. }
  2244. }
  2245. }
  2246. int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
  2247. {
  2248. if (dev->num_tc) {
  2249. struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
  2250. int i;
  2251. /* walk through the TCs and see if it falls into any of them */
  2252. for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
  2253. if ((txq - tc->offset) < tc->count)
  2254. return i;
  2255. }
  2256. /* didn't find it, just return -1 to indicate no match */
  2257. return -1;
  2258. }
  2259. return 0;
  2260. }
  2261. EXPORT_SYMBOL(netdev_txq_to_tc);
  2262. #ifdef CONFIG_XPS
  2263. static struct static_key xps_needed __read_mostly;
  2264. static struct static_key xps_rxqs_needed __read_mostly;
  2265. static DEFINE_MUTEX(xps_map_mutex);
  2266. #define xmap_dereference(P) \
  2267. rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
  2268. static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
  2269. struct xps_dev_maps *old_maps, int tci, u16 index)
  2270. {
  2271. struct xps_map *map = NULL;
  2272. int pos;
  2273. map = xmap_dereference(dev_maps->attr_map[tci]);
  2274. if (!map)
  2275. return false;
  2276. for (pos = map->len; pos--;) {
  2277. if (map->queues[pos] != index)
  2278. continue;
  2279. if (map->len > 1) {
  2280. map->queues[pos] = map->queues[--map->len];
  2281. break;
  2282. }
  2283. if (old_maps)
  2284. RCU_INIT_POINTER(old_maps->attr_map[tci], NULL);
  2285. RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
  2286. kfree_rcu(map, rcu);
  2287. return false;
  2288. }
  2289. return true;
  2290. }
  2291. static bool remove_xps_queue_cpu(struct net_device *dev,
  2292. struct xps_dev_maps *dev_maps,
  2293. int cpu, u16 offset, u16 count)
  2294. {
  2295. int num_tc = dev_maps->num_tc;
  2296. bool active = false;
  2297. int tci;
  2298. for (tci = cpu * num_tc; num_tc--; tci++) {
  2299. int i, j;
  2300. for (i = count, j = offset; i--; j++) {
  2301. if (!remove_xps_queue(dev_maps, NULL, tci, j))
  2302. break;
  2303. }
  2304. active |= i < 0;
  2305. }
  2306. return active;
  2307. }
  2308. static void reset_xps_maps(struct net_device *dev,
  2309. struct xps_dev_maps *dev_maps,
  2310. enum xps_map_type type)
  2311. {
  2312. static_key_slow_dec_cpuslocked(&xps_needed);
  2313. if (type == XPS_RXQS)
  2314. static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
  2315. RCU_INIT_POINTER(dev->xps_maps[type], NULL);
  2316. kfree_rcu(dev_maps, rcu);
  2317. }
  2318. static void clean_xps_maps(struct net_device *dev, enum xps_map_type type,
  2319. u16 offset, u16 count)
  2320. {
  2321. struct xps_dev_maps *dev_maps;
  2322. bool active = false;
  2323. int i, j;
  2324. dev_maps = xmap_dereference(dev->xps_maps[type]);
  2325. if (!dev_maps)
  2326. return;
  2327. for (j = 0; j < dev_maps->nr_ids; j++)
  2328. active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, count);
  2329. if (!active)
  2330. reset_xps_maps(dev, dev_maps, type);
  2331. if (type == XPS_CPUS) {
  2332. for (i = offset + (count - 1); count--; i--)
  2333. netdev_queue_numa_node_write(
  2334. netdev_get_tx_queue(dev, i), NUMA_NO_NODE);
  2335. }
  2336. }
  2337. static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
  2338. u16 count)
  2339. {
  2340. if (!static_key_false(&xps_needed))
  2341. return;
  2342. cpus_read_lock();
  2343. mutex_lock(&xps_map_mutex);
  2344. if (static_key_false(&xps_rxqs_needed))
  2345. clean_xps_maps(dev, XPS_RXQS, offset, count);
  2346. clean_xps_maps(dev, XPS_CPUS, offset, count);
  2347. mutex_unlock(&xps_map_mutex);
  2348. cpus_read_unlock();
  2349. }
  2350. static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
  2351. {
  2352. netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
  2353. }
  2354. static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
  2355. u16 index, bool is_rxqs_map)
  2356. {
  2357. struct xps_map *new_map;
  2358. int alloc_len = XPS_MIN_MAP_ALLOC;
  2359. int i, pos;
  2360. for (pos = 0; map && pos < map->len; pos++) {
  2361. if (map->queues[pos] != index)
  2362. continue;
  2363. return map;
  2364. }
  2365. /* Need to add tx-queue to this CPU's/rx-queue's existing map */
  2366. if (map) {
  2367. if (pos < map->alloc_len)
  2368. return map;
  2369. alloc_len = map->alloc_len * 2;
  2370. }
  2371. /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
  2372. * map
  2373. */
  2374. if (is_rxqs_map)
  2375. new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
  2376. else
  2377. new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
  2378. cpu_to_node(attr_index));
  2379. if (!new_map)
  2380. return NULL;
  2381. for (i = 0; i < pos; i++)
  2382. new_map->queues[i] = map->queues[i];
  2383. new_map->alloc_len = alloc_len;
  2384. new_map->len = pos;
  2385. return new_map;
  2386. }
  2387. /* Copy xps maps at a given index */
  2388. static void xps_copy_dev_maps(struct xps_dev_maps *dev_maps,
  2389. struct xps_dev_maps *new_dev_maps, int index,
  2390. int tc, bool skip_tc)
  2391. {
  2392. int i, tci = index * dev_maps->num_tc;
  2393. struct xps_map *map;
  2394. /* copy maps belonging to foreign traffic classes */
  2395. for (i = 0; i < dev_maps->num_tc; i++, tci++) {
  2396. if (i == tc && skip_tc)
  2397. continue;
  2398. /* fill in the new device map from the old device map */
  2399. map = xmap_dereference(dev_maps->attr_map[tci]);
  2400. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  2401. }
  2402. }
  2403. /* Must be called under cpus_read_lock */
  2404. int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
  2405. u16 index, enum xps_map_type type)
  2406. {
  2407. struct xps_dev_maps *dev_maps, *new_dev_maps = NULL, *old_dev_maps = NULL;
  2408. const unsigned long *online_mask = NULL;
  2409. bool active = false, copy = false;
  2410. int i, j, tci, numa_node_id = -2;
  2411. int maps_sz, num_tc = 1, tc = 0;
  2412. struct xps_map *map, *new_map;
  2413. unsigned int nr_ids;
  2414. WARN_ON_ONCE(index >= dev->num_tx_queues);
  2415. if (dev->num_tc) {
  2416. /* Do not allow XPS on subordinate device directly */
  2417. num_tc = dev->num_tc;
  2418. if (num_tc < 0)
  2419. return -EINVAL;
  2420. /* If queue belongs to subordinate dev use its map */
  2421. dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
  2422. tc = netdev_txq_to_tc(dev, index);
  2423. if (tc < 0)
  2424. return -EINVAL;
  2425. }
  2426. mutex_lock(&xps_map_mutex);
  2427. dev_maps = xmap_dereference(dev->xps_maps[type]);
  2428. if (type == XPS_RXQS) {
  2429. maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
  2430. nr_ids = dev->num_rx_queues;
  2431. } else {
  2432. maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
  2433. if (num_possible_cpus() > 1)
  2434. online_mask = cpumask_bits(cpu_online_mask);
  2435. nr_ids = nr_cpu_ids;
  2436. }
  2437. if (maps_sz < L1_CACHE_BYTES)
  2438. maps_sz = L1_CACHE_BYTES;
  2439. /* The old dev_maps could be larger or smaller than the one we're
  2440. * setting up now, as dev->num_tc or nr_ids could have been updated in
  2441. * between. We could try to be smart, but let's be safe instead and only
  2442. * copy foreign traffic classes if the two map sizes match.
  2443. */
  2444. if (dev_maps &&
  2445. dev_maps->num_tc == num_tc && dev_maps->nr_ids == nr_ids)
  2446. copy = true;
  2447. /* allocate memory for queue storage */
  2448. for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
  2449. j < nr_ids;) {
  2450. if (!new_dev_maps) {
  2451. new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
  2452. if (!new_dev_maps) {
  2453. mutex_unlock(&xps_map_mutex);
  2454. return -ENOMEM;
  2455. }
  2456. new_dev_maps->nr_ids = nr_ids;
  2457. new_dev_maps->num_tc = num_tc;
  2458. }
  2459. tci = j * num_tc + tc;
  2460. map = copy ? xmap_dereference(dev_maps->attr_map[tci]) : NULL;
  2461. map = expand_xps_map(map, j, index, type == XPS_RXQS);
  2462. if (!map)
  2463. goto error;
  2464. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  2465. }
  2466. if (!new_dev_maps)
  2467. goto out_no_new_maps;
  2468. if (!dev_maps) {
  2469. /* Increment static keys at most once per type */
  2470. static_key_slow_inc_cpuslocked(&xps_needed);
  2471. if (type == XPS_RXQS)
  2472. static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
  2473. }
  2474. for (j = 0; j < nr_ids; j++) {
  2475. bool skip_tc = false;
  2476. tci = j * num_tc + tc;
  2477. if (netif_attr_test_mask(j, mask, nr_ids) &&
  2478. netif_attr_test_online(j, online_mask, nr_ids)) {
  2479. /* add tx-queue to CPU/rx-queue maps */
  2480. int pos = 0;
  2481. skip_tc = true;
  2482. map = xmap_dereference(new_dev_maps->attr_map[tci]);
  2483. while ((pos < map->len) && (map->queues[pos] != index))
  2484. pos++;
  2485. if (pos == map->len)
  2486. map->queues[map->len++] = index;
  2487. #ifdef CONFIG_NUMA
  2488. if (type == XPS_CPUS) {
  2489. if (numa_node_id == -2)
  2490. numa_node_id = cpu_to_node(j);
  2491. else if (numa_node_id != cpu_to_node(j))
  2492. numa_node_id = -1;
  2493. }
  2494. #endif
  2495. }
  2496. if (copy)
  2497. xps_copy_dev_maps(dev_maps, new_dev_maps, j, tc,
  2498. skip_tc);
  2499. }
  2500. rcu_assign_pointer(dev->xps_maps[type], new_dev_maps);
  2501. /* Cleanup old maps */
  2502. if (!dev_maps)
  2503. goto out_no_old_maps;
  2504. for (j = 0; j < dev_maps->nr_ids; j++) {
  2505. for (i = num_tc, tci = j * dev_maps->num_tc; i--; tci++) {
  2506. map = xmap_dereference(dev_maps->attr_map[tci]);
  2507. if (!map)
  2508. continue;
  2509. if (copy) {
  2510. new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
  2511. if (map == new_map)
  2512. continue;
  2513. }
  2514. RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
  2515. kfree_rcu(map, rcu);
  2516. }
  2517. }
  2518. old_dev_maps = dev_maps;
  2519. out_no_old_maps:
  2520. dev_maps = new_dev_maps;
  2521. active = true;
  2522. out_no_new_maps:
  2523. if (type == XPS_CPUS)
  2524. /* update Tx queue numa node */
  2525. netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
  2526. (numa_node_id >= 0) ?
  2527. numa_node_id : NUMA_NO_NODE);
  2528. if (!dev_maps)
  2529. goto out_no_maps;
  2530. /* removes tx-queue from unused CPUs/rx-queues */
  2531. for (j = 0; j < dev_maps->nr_ids; j++) {
  2532. tci = j * dev_maps->num_tc;
  2533. for (i = 0; i < dev_maps->num_tc; i++, tci++) {
  2534. if (i == tc &&
  2535. netif_attr_test_mask(j, mask, dev_maps->nr_ids) &&
  2536. netif_attr_test_online(j, online_mask, dev_maps->nr_ids))
  2537. continue;
  2538. active |= remove_xps_queue(dev_maps,
  2539. copy ? old_dev_maps : NULL,
  2540. tci, index);
  2541. }
  2542. }
  2543. if (old_dev_maps)
  2544. kfree_rcu(old_dev_maps, rcu);
  2545. /* free map if not active */
  2546. if (!active)
  2547. reset_xps_maps(dev, dev_maps, type);
  2548. out_no_maps:
  2549. mutex_unlock(&xps_map_mutex);
  2550. return 0;
  2551. error:
  2552. /* remove any maps that we added */
  2553. for (j = 0; j < nr_ids; j++) {
  2554. for (i = num_tc, tci = j * num_tc; i--; tci++) {
  2555. new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
  2556. map = copy ?
  2557. xmap_dereference(dev_maps->attr_map[tci]) :
  2558. NULL;
  2559. if (new_map && new_map != map)
  2560. kfree(new_map);
  2561. }
  2562. }
  2563. mutex_unlock(&xps_map_mutex);
  2564. kfree(new_dev_maps);
  2565. return -ENOMEM;
  2566. }
  2567. EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
  2568. int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
  2569. u16 index)
  2570. {
  2571. int ret;
  2572. cpus_read_lock();
  2573. ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, XPS_CPUS);
  2574. cpus_read_unlock();
  2575. return ret;
  2576. }
  2577. EXPORT_SYMBOL(netif_set_xps_queue);
  2578. #endif
  2579. static void netdev_unbind_all_sb_channels(struct net_device *dev)
  2580. {
  2581. struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
  2582. /* Unbind any subordinate channels */
  2583. while (txq-- != &dev->_tx[0]) {
  2584. if (txq->sb_dev)
  2585. netdev_unbind_sb_channel(dev, txq->sb_dev);
  2586. }
  2587. }
  2588. void netdev_reset_tc(struct net_device *dev)
  2589. {
  2590. #ifdef CONFIG_XPS
  2591. netif_reset_xps_queues_gt(dev, 0);
  2592. #endif
  2593. netdev_unbind_all_sb_channels(dev);
  2594. /* Reset TC configuration of device */
  2595. dev->num_tc = 0;
  2596. memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
  2597. memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
  2598. }
  2599. EXPORT_SYMBOL(netdev_reset_tc);
  2600. int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
  2601. {
  2602. if (tc >= dev->num_tc)
  2603. return -EINVAL;
  2604. #ifdef CONFIG_XPS
  2605. netif_reset_xps_queues(dev, offset, count);
  2606. #endif
  2607. dev->tc_to_txq[tc].count = count;
  2608. dev->tc_to_txq[tc].offset = offset;
  2609. return 0;
  2610. }
  2611. EXPORT_SYMBOL(netdev_set_tc_queue);
  2612. int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
  2613. {
  2614. if (num_tc > TC_MAX_QUEUE)
  2615. return -EINVAL;
  2616. #ifdef CONFIG_XPS
  2617. netif_reset_xps_queues_gt(dev, 0);
  2618. #endif
  2619. netdev_unbind_all_sb_channels(dev);
  2620. dev->num_tc = num_tc;
  2621. return 0;
  2622. }
  2623. EXPORT_SYMBOL(netdev_set_num_tc);
  2624. void netdev_unbind_sb_channel(struct net_device *dev,
  2625. struct net_device *sb_dev)
  2626. {
  2627. struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
  2628. #ifdef CONFIG_XPS
  2629. netif_reset_xps_queues_gt(sb_dev, 0);
  2630. #endif
  2631. memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
  2632. memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
  2633. while (txq-- != &dev->_tx[0]) {
  2634. if (txq->sb_dev == sb_dev)
  2635. txq->sb_dev = NULL;
  2636. }
  2637. }
  2638. EXPORT_SYMBOL(netdev_unbind_sb_channel);
  2639. int netdev_bind_sb_channel_queue(struct net_device *dev,
  2640. struct net_device *sb_dev,
  2641. u8 tc, u16 count, u16 offset)
  2642. {
  2643. /* Make certain the sb_dev and dev are already configured */
  2644. if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
  2645. return -EINVAL;
  2646. /* We cannot hand out queues we don't have */
  2647. if ((offset + count) > dev->real_num_tx_queues)
  2648. return -EINVAL;
  2649. /* Record the mapping */
  2650. sb_dev->tc_to_txq[tc].count = count;
  2651. sb_dev->tc_to_txq[tc].offset = offset;
  2652. /* Provide a way for Tx queue to find the tc_to_txq map or
  2653. * XPS map for itself.
  2654. */
  2655. while (count--)
  2656. netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
  2657. return 0;
  2658. }
  2659. EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
  2660. int netdev_set_sb_channel(struct net_device *dev, u16 channel)
  2661. {
  2662. /* Do not use a multiqueue device to represent a subordinate channel */
  2663. if (netif_is_multiqueue(dev))
  2664. return -ENODEV;
  2665. /* We allow channels 1 - 32767 to be used for subordinate channels.
  2666. * Channel 0 is meant to be "native" mode and used only to represent
  2667. * the main root device. We allow writing 0 to reset the device back
  2668. * to normal mode after being used as a subordinate channel.
  2669. */
  2670. if (channel > S16_MAX)
  2671. return -EINVAL;
  2672. dev->num_tc = -channel;
  2673. return 0;
  2674. }
  2675. EXPORT_SYMBOL(netdev_set_sb_channel);
  2676. /*
  2677. * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
  2678. * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
  2679. */
  2680. int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
  2681. {
  2682. bool disabling;
  2683. int rc;
  2684. disabling = txq < dev->real_num_tx_queues;
  2685. if (txq < 1 || txq > dev->num_tx_queues)
  2686. return -EINVAL;
  2687. if (dev->reg_state == NETREG_REGISTERED ||
  2688. dev->reg_state == NETREG_UNREGISTERING) {
  2689. netdev_ops_assert_locked(dev);
  2690. rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
  2691. txq);
  2692. if (rc)
  2693. return rc;
  2694. if (dev->num_tc)
  2695. netif_setup_tc(dev, txq);
  2696. net_shaper_set_real_num_tx_queues(dev, txq);
  2697. dev_qdisc_change_real_num_tx(dev, txq);
  2698. dev->real_num_tx_queues = txq;
  2699. if (disabling) {
  2700. synchronize_net();
  2701. qdisc_reset_all_tx_gt(dev, txq);
  2702. #ifdef CONFIG_XPS
  2703. netif_reset_xps_queues_gt(dev, txq);
  2704. #endif
  2705. }
  2706. } else {
  2707. dev->real_num_tx_queues = txq;
  2708. }
  2709. return 0;
  2710. }
  2711. EXPORT_SYMBOL(netif_set_real_num_tx_queues);
  2712. /**
  2713. * netif_set_real_num_rx_queues - set actual number of RX queues used
  2714. * @dev: Network device
  2715. * @rxq: Actual number of RX queues
  2716. *
  2717. * This must be called either with the rtnl_lock held or before
  2718. * registration of the net device. Returns 0 on success, or a
  2719. * negative error code. If called before registration, it always
  2720. * succeeds.
  2721. */
  2722. int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
  2723. {
  2724. int rc;
  2725. if (rxq < 1 || rxq > dev->num_rx_queues)
  2726. return -EINVAL;
  2727. if (dev->reg_state == NETREG_REGISTERED) {
  2728. netdev_ops_assert_locked(dev);
  2729. rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
  2730. rxq);
  2731. if (rc)
  2732. return rc;
  2733. }
  2734. dev->real_num_rx_queues = rxq;
  2735. return 0;
  2736. }
  2737. EXPORT_SYMBOL(netif_set_real_num_rx_queues);
  2738. /**
  2739. * netif_set_real_num_queues - set actual number of RX and TX queues used
  2740. * @dev: Network device
  2741. * @txq: Actual number of TX queues
  2742. * @rxq: Actual number of RX queues
  2743. *
  2744. * Set the real number of both TX and RX queues.
  2745. * Does nothing if the number of queues is already correct.
  2746. */
  2747. int netif_set_real_num_queues(struct net_device *dev,
  2748. unsigned int txq, unsigned int rxq)
  2749. {
  2750. unsigned int old_rxq = dev->real_num_rx_queues;
  2751. int err;
  2752. if (txq < 1 || txq > dev->num_tx_queues ||
  2753. rxq < 1 || rxq > dev->num_rx_queues)
  2754. return -EINVAL;
  2755. /* Start from increases, so the error path only does decreases -
  2756. * decreases can't fail.
  2757. */
  2758. if (rxq > dev->real_num_rx_queues) {
  2759. err = netif_set_real_num_rx_queues(dev, rxq);
  2760. if (err)
  2761. return err;
  2762. }
  2763. if (txq > dev->real_num_tx_queues) {
  2764. err = netif_set_real_num_tx_queues(dev, txq);
  2765. if (err)
  2766. goto undo_rx;
  2767. }
  2768. if (rxq < dev->real_num_rx_queues)
  2769. WARN_ON(netif_set_real_num_rx_queues(dev, rxq));
  2770. if (txq < dev->real_num_tx_queues)
  2771. WARN_ON(netif_set_real_num_tx_queues(dev, txq));
  2772. return 0;
  2773. undo_rx:
  2774. WARN_ON(netif_set_real_num_rx_queues(dev, old_rxq));
  2775. return err;
  2776. }
  2777. EXPORT_SYMBOL(netif_set_real_num_queues);
  2778. /**
  2779. * netif_set_tso_max_size() - set the max size of TSO frames supported
  2780. * @dev: netdev to update
  2781. * @size: max skb->len of a TSO frame
  2782. *
  2783. * Set the limit on the size of TSO super-frames the device can handle.
  2784. * Unless explicitly set the stack will assume the value of
  2785. * %GSO_LEGACY_MAX_SIZE.
  2786. */
  2787. void netif_set_tso_max_size(struct net_device *dev, unsigned int size)
  2788. {
  2789. dev->tso_max_size = min(GSO_MAX_SIZE, size);
  2790. if (size < READ_ONCE(dev->gso_max_size))
  2791. netif_set_gso_max_size(dev, size);
  2792. if (size < READ_ONCE(dev->gso_ipv4_max_size))
  2793. netif_set_gso_ipv4_max_size(dev, size);
  2794. }
  2795. EXPORT_SYMBOL(netif_set_tso_max_size);
  2796. /**
  2797. * netif_set_tso_max_segs() - set the max number of segs supported for TSO
  2798. * @dev: netdev to update
  2799. * @segs: max number of TCP segments
  2800. *
  2801. * Set the limit on the number of TCP segments the device can generate from
  2802. * a single TSO super-frame.
  2803. * Unless explicitly set the stack will assume the value of %GSO_MAX_SEGS.
  2804. */
  2805. void netif_set_tso_max_segs(struct net_device *dev, unsigned int segs)
  2806. {
  2807. dev->tso_max_segs = segs;
  2808. if (segs < READ_ONCE(dev->gso_max_segs))
  2809. netif_set_gso_max_segs(dev, segs);
  2810. }
  2811. EXPORT_SYMBOL(netif_set_tso_max_segs);
  2812. /**
  2813. * netif_inherit_tso_max() - copy all TSO limits from a lower device to an upper
  2814. * @to: netdev to update
  2815. * @from: netdev from which to copy the limits
  2816. */
  2817. void netif_inherit_tso_max(struct net_device *to, const struct net_device *from)
  2818. {
  2819. netif_set_tso_max_size(to, from->tso_max_size);
  2820. netif_set_tso_max_segs(to, from->tso_max_segs);
  2821. }
  2822. EXPORT_SYMBOL(netif_inherit_tso_max);
  2823. /**
  2824. * netif_get_num_default_rss_queues - default number of RSS queues
  2825. *
  2826. * Default value is the number of physical cores if there are only 1 or 2, or
  2827. * divided by 2 if there are more.
  2828. */
  2829. int netif_get_num_default_rss_queues(void)
  2830. {
  2831. cpumask_var_t cpus;
  2832. int cpu, count = 0;
  2833. if (unlikely(is_kdump_kernel() || !zalloc_cpumask_var(&cpus, GFP_KERNEL)))
  2834. return 1;
  2835. cpumask_copy(cpus, cpu_online_mask);
  2836. for_each_cpu(cpu, cpus) {
  2837. ++count;
  2838. cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu));
  2839. }
  2840. free_cpumask_var(cpus);
  2841. return count > 2 ? DIV_ROUND_UP(count, 2) : count;
  2842. }
  2843. EXPORT_SYMBOL(netif_get_num_default_rss_queues);
  2844. static void __netif_reschedule(struct Qdisc *q)
  2845. {
  2846. struct softnet_data *sd;
  2847. unsigned long flags;
  2848. local_irq_save(flags);
  2849. sd = this_cpu_ptr(&softnet_data);
  2850. q->next_sched = NULL;
  2851. *sd->output_queue_tailp = q;
  2852. sd->output_queue_tailp = &q->next_sched;
  2853. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  2854. local_irq_restore(flags);
  2855. }
  2856. void __netif_schedule(struct Qdisc *q)
  2857. {
  2858. /* If q->defer_list is not empty, at least one thread is
  2859. * in __dev_xmit_skb() before llist_del_all(&q->defer_list).
  2860. * This thread will attempt to run the queue.
  2861. */
  2862. if (!llist_empty(&q->defer_list))
  2863. return;
  2864. if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
  2865. __netif_reschedule(q);
  2866. }
  2867. EXPORT_SYMBOL(__netif_schedule);
  2868. struct dev_kfree_skb_cb {
  2869. enum skb_drop_reason reason;
  2870. };
  2871. static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
  2872. {
  2873. return (struct dev_kfree_skb_cb *)skb->cb;
  2874. }
  2875. void netif_schedule_queue(struct netdev_queue *txq)
  2876. {
  2877. rcu_read_lock();
  2878. if (!netif_xmit_stopped(txq)) {
  2879. struct Qdisc *q = rcu_dereference(txq->qdisc);
  2880. __netif_schedule(q);
  2881. }
  2882. rcu_read_unlock();
  2883. }
  2884. EXPORT_SYMBOL(netif_schedule_queue);
  2885. void netif_tx_wake_queue(struct netdev_queue *dev_queue)
  2886. {
  2887. if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
  2888. struct Qdisc *q;
  2889. rcu_read_lock();
  2890. q = rcu_dereference(dev_queue->qdisc);
  2891. __netif_schedule(q);
  2892. rcu_read_unlock();
  2893. }
  2894. }
  2895. EXPORT_SYMBOL(netif_tx_wake_queue);
  2896. void dev_kfree_skb_irq_reason(struct sk_buff *skb, enum skb_drop_reason reason)
  2897. {
  2898. unsigned long flags;
  2899. if (unlikely(!skb))
  2900. return;
  2901. if (likely(refcount_read(&skb->users) == 1)) {
  2902. smp_rmb();
  2903. refcount_set(&skb->users, 0);
  2904. } else if (likely(!refcount_dec_and_test(&skb->users))) {
  2905. return;
  2906. }
  2907. get_kfree_skb_cb(skb)->reason = reason;
  2908. local_irq_save(flags);
  2909. skb->next = __this_cpu_read(softnet_data.completion_queue);
  2910. __this_cpu_write(softnet_data.completion_queue, skb);
  2911. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  2912. local_irq_restore(flags);
  2913. }
  2914. EXPORT_SYMBOL(dev_kfree_skb_irq_reason);
  2915. void dev_kfree_skb_any_reason(struct sk_buff *skb, enum skb_drop_reason reason)
  2916. {
  2917. if (in_hardirq() || irqs_disabled())
  2918. dev_kfree_skb_irq_reason(skb, reason);
  2919. else
  2920. kfree_skb_reason(skb, reason);
  2921. }
  2922. EXPORT_SYMBOL(dev_kfree_skb_any_reason);
  2923. /**
  2924. * netif_device_detach - mark device as removed
  2925. * @dev: network device
  2926. *
  2927. * Mark device as removed from system and therefore no longer available.
  2928. */
  2929. void netif_device_detach(struct net_device *dev)
  2930. {
  2931. if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
  2932. netif_running(dev)) {
  2933. netif_tx_stop_all_queues(dev);
  2934. }
  2935. }
  2936. EXPORT_SYMBOL(netif_device_detach);
  2937. /**
  2938. * netif_device_attach - mark device as attached
  2939. * @dev: network device
  2940. *
  2941. * Mark device as attached from system and restart if needed.
  2942. */
  2943. void netif_device_attach(struct net_device *dev)
  2944. {
  2945. if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
  2946. netif_running(dev)) {
  2947. netif_tx_wake_all_queues(dev);
  2948. netdev_watchdog_up(dev);
  2949. }
  2950. }
  2951. EXPORT_SYMBOL(netif_device_attach);
  2952. /*
  2953. * Returns a Tx hash based on the given packet descriptor a Tx queues' number
  2954. * to be used as a distribution range.
  2955. */
  2956. static u16 skb_tx_hash(const struct net_device *dev,
  2957. const struct net_device *sb_dev,
  2958. struct sk_buff *skb)
  2959. {
  2960. u32 hash;
  2961. u16 qoffset = 0;
  2962. u16 qcount = dev->real_num_tx_queues;
  2963. if (dev->num_tc) {
  2964. u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
  2965. qoffset = sb_dev->tc_to_txq[tc].offset;
  2966. qcount = sb_dev->tc_to_txq[tc].count;
  2967. if (unlikely(!qcount)) {
  2968. net_warn_ratelimited("%s: invalid qcount, qoffset %u for tc %u\n",
  2969. sb_dev->name, qoffset, tc);
  2970. qoffset = 0;
  2971. qcount = dev->real_num_tx_queues;
  2972. }
  2973. }
  2974. if (skb_rx_queue_recorded(skb)) {
  2975. DEBUG_NET_WARN_ON_ONCE(qcount == 0);
  2976. hash = skb_get_rx_queue(skb);
  2977. if (hash >= qoffset)
  2978. hash -= qoffset;
  2979. while (unlikely(hash >= qcount))
  2980. hash -= qcount;
  2981. return hash + qoffset;
  2982. }
  2983. return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
  2984. }
  2985. void skb_warn_bad_offload(const struct sk_buff *skb)
  2986. {
  2987. static const netdev_features_t null_features;
  2988. struct net_device *dev = skb->dev;
  2989. const char *name = "";
  2990. if (!net_ratelimit())
  2991. return;
  2992. if (dev) {
  2993. if (dev->dev.parent)
  2994. name = dev_driver_string(dev->dev.parent);
  2995. else
  2996. name = netdev_name(dev);
  2997. }
  2998. skb_dump(KERN_WARNING, skb, false);
  2999. WARN(1, "%s: caps=(%pNF, %pNF)\n",
  3000. name, dev ? &dev->features : &null_features,
  3001. skb->sk ? &skb->sk->sk_route_caps : &null_features);
  3002. }
  3003. /*
  3004. * Invalidate hardware checksum when packet is to be mangled, and
  3005. * complete checksum manually on outgoing path.
  3006. */
  3007. int skb_checksum_help(struct sk_buff *skb)
  3008. {
  3009. __wsum csum;
  3010. int ret = 0, offset;
  3011. if (skb->ip_summed == CHECKSUM_COMPLETE)
  3012. goto out_set_summed;
  3013. if (unlikely(skb_is_gso(skb))) {
  3014. skb_warn_bad_offload(skb);
  3015. return -EINVAL;
  3016. }
  3017. if (!skb_frags_readable(skb)) {
  3018. return -EFAULT;
  3019. }
  3020. /* Before computing a checksum, we should make sure no frag could
  3021. * be modified by an external entity : checksum could be wrong.
  3022. */
  3023. if (skb_has_shared_frag(skb)) {
  3024. ret = __skb_linearize(skb);
  3025. if (ret)
  3026. goto out;
  3027. }
  3028. offset = skb_checksum_start_offset(skb);
  3029. ret = -EINVAL;
  3030. if (unlikely(offset >= skb_headlen(skb))) {
  3031. DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false);
  3032. WARN_ONCE(true, "offset (%d) >= skb_headlen() (%u)\n",
  3033. offset, skb_headlen(skb));
  3034. goto out;
  3035. }
  3036. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  3037. offset += skb->csum_offset;
  3038. if (unlikely(offset + sizeof(__sum16) > skb_headlen(skb))) {
  3039. DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false);
  3040. WARN_ONCE(true, "offset+2 (%zu) > skb_headlen() (%u)\n",
  3041. offset + sizeof(__sum16), skb_headlen(skb));
  3042. goto out;
  3043. }
  3044. ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
  3045. if (ret)
  3046. goto out;
  3047. *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
  3048. out_set_summed:
  3049. skb->ip_summed = CHECKSUM_NONE;
  3050. out:
  3051. return ret;
  3052. }
  3053. EXPORT_SYMBOL(skb_checksum_help);
  3054. #ifdef CONFIG_NET_CRC32C
  3055. int skb_crc32c_csum_help(struct sk_buff *skb)
  3056. {
  3057. u32 crc;
  3058. int ret = 0, offset, start;
  3059. if (skb->ip_summed != CHECKSUM_PARTIAL)
  3060. goto out;
  3061. if (unlikely(skb_is_gso(skb)))
  3062. goto out;
  3063. /* Before computing a checksum, we should make sure no frag could
  3064. * be modified by an external entity : checksum could be wrong.
  3065. */
  3066. if (unlikely(skb_has_shared_frag(skb))) {
  3067. ret = __skb_linearize(skb);
  3068. if (ret)
  3069. goto out;
  3070. }
  3071. start = skb_checksum_start_offset(skb);
  3072. offset = start + offsetof(struct sctphdr, checksum);
  3073. if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
  3074. ret = -EINVAL;
  3075. goto out;
  3076. }
  3077. ret = skb_ensure_writable(skb, offset + sizeof(__le32));
  3078. if (ret)
  3079. goto out;
  3080. crc = ~skb_crc32c(skb, start, skb->len - start, ~0);
  3081. *(__le32 *)(skb->data + offset) = cpu_to_le32(crc);
  3082. skb_reset_csum_not_inet(skb);
  3083. out:
  3084. return ret;
  3085. }
  3086. EXPORT_SYMBOL(skb_crc32c_csum_help);
  3087. #endif /* CONFIG_NET_CRC32C */
  3088. __be16 skb_network_protocol(struct sk_buff *skb, int *depth)
  3089. {
  3090. __be16 type = skb->protocol;
  3091. /* Tunnel gso handlers can set protocol to ethernet. */
  3092. if (type == htons(ETH_P_TEB)) {
  3093. struct ethhdr *eth;
  3094. if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
  3095. return 0;
  3096. eth = (struct ethhdr *)skb->data;
  3097. type = eth->h_proto;
  3098. }
  3099. return vlan_get_protocol_and_depth(skb, type, depth);
  3100. }
  3101. /* Take action when hardware reception checksum errors are detected. */
  3102. #ifdef CONFIG_BUG
  3103. static void do_netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
  3104. {
  3105. netdev_err(dev, "hw csum failure\n");
  3106. skb_dump(KERN_ERR, skb, true);
  3107. dump_stack();
  3108. }
  3109. void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
  3110. {
  3111. DO_ONCE_LITE(do_netdev_rx_csum_fault, dev, skb);
  3112. }
  3113. EXPORT_SYMBOL(netdev_rx_csum_fault);
  3114. #endif
  3115. /* XXX: check that highmem exists at all on the given machine. */
  3116. static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
  3117. {
  3118. #ifdef CONFIG_HIGHMEM
  3119. int i;
  3120. if (!(dev->features & NETIF_F_HIGHDMA)) {
  3121. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  3122. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  3123. struct page *page = skb_frag_page(frag);
  3124. if (page && PageHighMem(page))
  3125. return 1;
  3126. }
  3127. }
  3128. #endif
  3129. return 0;
  3130. }
  3131. /* If MPLS offload request, verify we are testing hardware MPLS features
  3132. * instead of standard features for the netdev.
  3133. */
  3134. #if IS_ENABLED(CONFIG_NET_MPLS_GSO)
  3135. static netdev_features_t net_mpls_features(struct sk_buff *skb,
  3136. netdev_features_t features,
  3137. __be16 type)
  3138. {
  3139. if (eth_p_mpls(type))
  3140. features &= skb->dev->mpls_features;
  3141. return features;
  3142. }
  3143. #else
  3144. static netdev_features_t net_mpls_features(struct sk_buff *skb,
  3145. netdev_features_t features,
  3146. __be16 type)
  3147. {
  3148. return features;
  3149. }
  3150. #endif
  3151. static netdev_features_t harmonize_features(struct sk_buff *skb,
  3152. netdev_features_t features)
  3153. {
  3154. __be16 type;
  3155. type = skb_network_protocol(skb, NULL);
  3156. features = net_mpls_features(skb, features, type);
  3157. if (skb->ip_summed != CHECKSUM_NONE &&
  3158. !can_checksum_protocol(features, type)) {
  3159. features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  3160. }
  3161. if (illegal_highdma(skb->dev, skb))
  3162. features &= ~NETIF_F_SG;
  3163. return features;
  3164. }
  3165. netdev_features_t passthru_features_check(struct sk_buff *skb,
  3166. struct net_device *dev,
  3167. netdev_features_t features)
  3168. {
  3169. return features;
  3170. }
  3171. EXPORT_SYMBOL(passthru_features_check);
  3172. static netdev_features_t dflt_features_check(struct sk_buff *skb,
  3173. struct net_device *dev,
  3174. netdev_features_t features)
  3175. {
  3176. return vlan_features_check(skb, features);
  3177. }
  3178. static bool skb_gso_has_extension_hdr(const struct sk_buff *skb)
  3179. {
  3180. if (!skb->encapsulation)
  3181. return ((skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6 ||
  3182. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4 &&
  3183. vlan_get_protocol(skb) == htons(ETH_P_IPV6))) &&
  3184. skb_transport_header_was_set(skb) &&
  3185. skb_network_header_len(skb) != sizeof(struct ipv6hdr));
  3186. else
  3187. return (!skb_inner_network_header_was_set(skb) ||
  3188. ((skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6 ||
  3189. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4 &&
  3190. inner_ip_hdr(skb)->version == 6)) &&
  3191. skb_inner_network_header_len(skb) != sizeof(struct ipv6hdr)));
  3192. }
  3193. static netdev_features_t gso_features_check(const struct sk_buff *skb,
  3194. struct net_device *dev,
  3195. netdev_features_t features)
  3196. {
  3197. u16 gso_segs = skb_shinfo(skb)->gso_segs;
  3198. if (gso_segs > READ_ONCE(dev->gso_max_segs))
  3199. return features & ~NETIF_F_GSO_MASK;
  3200. if (unlikely(skb->len >= netif_get_gso_max_size(dev, skb)))
  3201. return features & ~NETIF_F_GSO_MASK;
  3202. if (!skb_shinfo(skb)->gso_type) {
  3203. skb_warn_bad_offload(skb);
  3204. return features & ~NETIF_F_GSO_MASK;
  3205. }
  3206. /* Support for GSO partial features requires software
  3207. * intervention before we can actually process the packets
  3208. * so we need to strip support for any partial features now
  3209. * and we can pull them back in after we have partially
  3210. * segmented the frame.
  3211. */
  3212. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
  3213. features &= ~dev->gso_partial_features;
  3214. /* Make sure to clear the IPv4 ID mangling feature if the IPv4 header
  3215. * has the potential to be fragmented so that TSO does not generate
  3216. * segments with the same ID. For encapsulated packets, the ID mangling
  3217. * feature is guaranteed not to use the same ID for the outer IPv4
  3218. * headers of the generated segments if the headers have the potential
  3219. * to be fragmented, so there is no need to clear the IPv4 ID mangling
  3220. * feature (see the section about NETIF_F_TSO_MANGLEID in
  3221. * segmentation-offloads.rst).
  3222. */
  3223. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  3224. const struct iphdr *iph;
  3225. struct iphdr _iph;
  3226. int nhoff = skb->encapsulation ?
  3227. skb_inner_network_offset(skb) :
  3228. skb_network_offset(skb);
  3229. iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
  3230. if (!iph || !(iph->frag_off & htons(IP_DF)))
  3231. features &= ~dev->mangleid_features;
  3232. }
  3233. /* NETIF_F_IPV6_CSUM does not support IPv6 extension headers,
  3234. * so neither does TSO that depends on it.
  3235. */
  3236. if (features & NETIF_F_IPV6_CSUM &&
  3237. skb_gso_has_extension_hdr(skb))
  3238. features &= ~(NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | NETIF_F_GSO_UDP_L4);
  3239. return features;
  3240. }
  3241. netdev_features_t netif_skb_features(struct sk_buff *skb)
  3242. {
  3243. struct net_device *dev = skb->dev;
  3244. netdev_features_t features = dev->features;
  3245. if (skb_is_gso(skb))
  3246. features = gso_features_check(skb, dev, features);
  3247. /* If encapsulation offload request, verify we are testing
  3248. * hardware encapsulation features instead of standard
  3249. * features for the netdev
  3250. */
  3251. if (skb->encapsulation)
  3252. features &= dev->hw_enc_features;
  3253. if (skb_vlan_tagged(skb))
  3254. features = netdev_intersect_features(features,
  3255. dev->vlan_features |
  3256. NETIF_F_HW_VLAN_CTAG_TX |
  3257. NETIF_F_HW_VLAN_STAG_TX);
  3258. if (dev->netdev_ops->ndo_features_check)
  3259. features &= dev->netdev_ops->ndo_features_check(skb, dev,
  3260. features);
  3261. else
  3262. features &= dflt_features_check(skb, dev, features);
  3263. return harmonize_features(skb, features);
  3264. }
  3265. EXPORT_SYMBOL(netif_skb_features);
  3266. static int xmit_one(struct sk_buff *skb, struct net_device *dev,
  3267. struct netdev_queue *txq, bool more)
  3268. {
  3269. unsigned int len;
  3270. int rc;
  3271. if (dev_nit_active_rcu(dev))
  3272. dev_queue_xmit_nit(skb, dev);
  3273. len = skb->len;
  3274. trace_net_dev_start_xmit(skb, dev);
  3275. rc = netdev_start_xmit(skb, dev, txq, more);
  3276. trace_net_dev_xmit(skb, rc, dev, len);
  3277. return rc;
  3278. }
  3279. struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
  3280. struct netdev_queue *txq, int *ret)
  3281. {
  3282. struct sk_buff *skb = first;
  3283. int rc = NETDEV_TX_OK;
  3284. while (skb) {
  3285. struct sk_buff *next = skb->next;
  3286. skb_mark_not_on_list(skb);
  3287. rc = xmit_one(skb, dev, txq, next != NULL);
  3288. if (unlikely(!dev_xmit_complete(rc))) {
  3289. skb->next = next;
  3290. goto out;
  3291. }
  3292. skb = next;
  3293. if (netif_tx_queue_stopped(txq) && skb) {
  3294. rc = NETDEV_TX_BUSY;
  3295. break;
  3296. }
  3297. }
  3298. out:
  3299. *ret = rc;
  3300. return skb;
  3301. }
  3302. static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
  3303. netdev_features_t features)
  3304. {
  3305. if (skb_vlan_tag_present(skb) &&
  3306. !vlan_hw_offload_capable(features, skb->vlan_proto))
  3307. skb = __vlan_hwaccel_push_inside(skb);
  3308. return skb;
  3309. }
  3310. int skb_csum_hwoffload_help(struct sk_buff *skb,
  3311. const netdev_features_t features)
  3312. {
  3313. if (unlikely(skb_csum_is_sctp(skb)))
  3314. return !!(features & NETIF_F_SCTP_CRC) ? 0 :
  3315. skb_crc32c_csum_help(skb);
  3316. if (features & NETIF_F_HW_CSUM)
  3317. return 0;
  3318. if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
  3319. if (vlan_get_protocol(skb) == htons(ETH_P_IPV6) &&
  3320. skb_network_header_len(skb) != sizeof(struct ipv6hdr))
  3321. goto sw_checksum;
  3322. switch (skb->csum_offset) {
  3323. case offsetof(struct tcphdr, check):
  3324. case offsetof(struct udphdr, check):
  3325. return 0;
  3326. }
  3327. }
  3328. sw_checksum:
  3329. return skb_checksum_help(skb);
  3330. }
  3331. EXPORT_SYMBOL(skb_csum_hwoffload_help);
  3332. /* Checks if this SKB belongs to an HW offloaded socket
  3333. * and whether any SW fallbacks are required based on dev.
  3334. * Check decrypted mark in case skb_orphan() cleared socket.
  3335. */
  3336. static struct sk_buff *sk_validate_xmit_skb(struct sk_buff *skb,
  3337. struct net_device *dev)
  3338. {
  3339. #ifdef CONFIG_SOCK_VALIDATE_XMIT
  3340. struct sk_buff *(*sk_validate)(struct sock *sk, struct net_device *dev,
  3341. struct sk_buff *skb);
  3342. struct sock *sk = skb->sk;
  3343. sk_validate = NULL;
  3344. if (sk) {
  3345. if (sk_fullsock(sk))
  3346. sk_validate = sk->sk_validate_xmit_skb;
  3347. else if (sk_is_inet(sk) && sk->sk_state == TCP_TIME_WAIT)
  3348. sk_validate = inet_twsk(sk)->tw_validate_xmit_skb;
  3349. }
  3350. if (sk_validate) {
  3351. skb = sk_validate(sk, dev, skb);
  3352. } else if (unlikely(skb_is_decrypted(skb))) {
  3353. pr_warn_ratelimited("unencrypted skb with no associated socket - dropping\n");
  3354. kfree_skb(skb);
  3355. skb = NULL;
  3356. }
  3357. #endif
  3358. return skb;
  3359. }
  3360. static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
  3361. struct net_device *dev)
  3362. {
  3363. struct skb_shared_info *shinfo;
  3364. struct net_iov *niov;
  3365. if (likely(skb_frags_readable(skb)))
  3366. goto out;
  3367. if (!dev->netmem_tx)
  3368. goto out_free;
  3369. shinfo = skb_shinfo(skb);
  3370. if (shinfo->nr_frags > 0) {
  3371. niov = netmem_to_net_iov(skb_frag_netmem(&shinfo->frags[0]));
  3372. if (net_is_devmem_iov(niov) &&
  3373. READ_ONCE(net_devmem_iov_binding(niov)->dev) != dev)
  3374. goto out_free;
  3375. }
  3376. out:
  3377. return skb;
  3378. out_free:
  3379. kfree_skb(skb);
  3380. return NULL;
  3381. }
  3382. static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
  3383. {
  3384. netdev_features_t features;
  3385. skb = validate_xmit_unreadable_skb(skb, dev);
  3386. if (unlikely(!skb))
  3387. goto out_null;
  3388. features = netif_skb_features(skb);
  3389. skb = validate_xmit_vlan(skb, features);
  3390. if (unlikely(!skb))
  3391. goto out_null;
  3392. skb = sk_validate_xmit_skb(skb, dev);
  3393. if (unlikely(!skb))
  3394. goto out_null;
  3395. if (netif_needs_gso(skb, features)) {
  3396. struct sk_buff *segs;
  3397. segs = skb_gso_segment(skb, features);
  3398. if (IS_ERR(segs)) {
  3399. goto out_kfree_skb;
  3400. } else if (segs) {
  3401. consume_skb(skb);
  3402. skb = segs;
  3403. }
  3404. } else {
  3405. if (skb_needs_linearize(skb, features) &&
  3406. __skb_linearize(skb))
  3407. goto out_kfree_skb;
  3408. /* If packet is not checksummed and device does not
  3409. * support checksumming for this protocol, complete
  3410. * checksumming here.
  3411. */
  3412. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3413. if (skb->encapsulation)
  3414. skb_set_inner_transport_header(skb,
  3415. skb_checksum_start_offset(skb));
  3416. else
  3417. skb_set_transport_header(skb,
  3418. skb_checksum_start_offset(skb));
  3419. if (skb_csum_hwoffload_help(skb, features))
  3420. goto out_kfree_skb;
  3421. }
  3422. }
  3423. skb = validate_xmit_xfrm(skb, features, again);
  3424. return skb;
  3425. out_kfree_skb:
  3426. kfree_skb(skb);
  3427. out_null:
  3428. dev_core_stats_tx_dropped_inc(dev);
  3429. return NULL;
  3430. }
  3431. struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
  3432. {
  3433. struct sk_buff *next, *head = NULL, *tail;
  3434. for (; skb != NULL; skb = next) {
  3435. next = skb->next;
  3436. skb_mark_not_on_list(skb);
  3437. /* in case skb won't be segmented, point to itself */
  3438. skb->prev = skb;
  3439. skb = validate_xmit_skb(skb, dev, again);
  3440. if (!skb)
  3441. continue;
  3442. if (!head)
  3443. head = skb;
  3444. else
  3445. tail->next = skb;
  3446. /* If skb was segmented, skb->prev points to
  3447. * the last segment. If not, it still contains skb.
  3448. */
  3449. tail = skb->prev;
  3450. }
  3451. return head;
  3452. }
  3453. EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
  3454. static void qdisc_pkt_len_segs_init(struct sk_buff *skb)
  3455. {
  3456. struct skb_shared_info *shinfo = skb_shinfo(skb);
  3457. u16 gso_segs;
  3458. qdisc_skb_cb(skb)->pkt_len = skb->len;
  3459. if (!shinfo->gso_size) {
  3460. qdisc_skb_cb(skb)->pkt_segs = 1;
  3461. return;
  3462. }
  3463. qdisc_skb_cb(skb)->pkt_segs = gso_segs = shinfo->gso_segs;
  3464. /* To get more precise estimation of bytes sent on wire,
  3465. * we add to pkt_len the headers size of all segments
  3466. */
  3467. if (skb_transport_header_was_set(skb)) {
  3468. unsigned int hdr_len;
  3469. /* mac layer + network layer */
  3470. if (!skb->encapsulation)
  3471. hdr_len = skb_transport_offset(skb);
  3472. else
  3473. hdr_len = skb_inner_transport_offset(skb);
  3474. /* + transport layer */
  3475. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
  3476. const struct tcphdr *th;
  3477. struct tcphdr _tcphdr;
  3478. th = skb_header_pointer(skb, hdr_len,
  3479. sizeof(_tcphdr), &_tcphdr);
  3480. if (likely(th))
  3481. hdr_len += __tcp_hdrlen(th);
  3482. } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
  3483. struct udphdr _udphdr;
  3484. if (skb_header_pointer(skb, hdr_len,
  3485. sizeof(_udphdr), &_udphdr))
  3486. hdr_len += sizeof(struct udphdr);
  3487. }
  3488. if (unlikely(shinfo->gso_type & SKB_GSO_DODGY)) {
  3489. int payload = skb->len - hdr_len;
  3490. /* Malicious packet. */
  3491. if (payload <= 0)
  3492. return;
  3493. gso_segs = DIV_ROUND_UP(payload, shinfo->gso_size);
  3494. shinfo->gso_segs = gso_segs;
  3495. qdisc_skb_cb(skb)->pkt_segs = gso_segs;
  3496. }
  3497. qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
  3498. }
  3499. }
  3500. static int dev_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *q,
  3501. struct sk_buff **to_free,
  3502. struct netdev_queue *txq)
  3503. {
  3504. int rc;
  3505. rc = q->enqueue(skb, q, to_free) & NET_XMIT_MASK;
  3506. if (rc == NET_XMIT_SUCCESS)
  3507. trace_qdisc_enqueue(q, txq, skb);
  3508. return rc;
  3509. }
  3510. static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  3511. struct net_device *dev,
  3512. struct netdev_queue *txq)
  3513. {
  3514. struct sk_buff *next, *to_free = NULL, *to_free2 = NULL;
  3515. spinlock_t *root_lock = qdisc_lock(q);
  3516. struct llist_node *ll_list, *first_n;
  3517. unsigned long defer_count = 0;
  3518. int rc;
  3519. qdisc_calculate_pkt_len(skb, q);
  3520. tcf_set_drop_reason(skb, SKB_DROP_REASON_QDISC_DROP);
  3521. if (q->flags & TCQ_F_NOLOCK) {
  3522. if (q->flags & TCQ_F_CAN_BYPASS && nolock_qdisc_is_empty(q) &&
  3523. qdisc_run_begin(q)) {
  3524. /* Retest nolock_qdisc_is_empty() within the protection
  3525. * of q->seqlock to protect from racing with requeuing.
  3526. */
  3527. if (unlikely(!nolock_qdisc_is_empty(q))) {
  3528. rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
  3529. __qdisc_run(q);
  3530. to_free2 = qdisc_run_end(q);
  3531. goto free_skbs;
  3532. }
  3533. qdisc_bstats_cpu_update(q, skb);
  3534. if (sch_direct_xmit(skb, q, dev, txq, NULL, true) &&
  3535. !nolock_qdisc_is_empty(q))
  3536. __qdisc_run(q);
  3537. to_free2 = qdisc_run_end(q);
  3538. rc = NET_XMIT_SUCCESS;
  3539. goto free_skbs;
  3540. }
  3541. rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
  3542. to_free2 = qdisc_run(q);
  3543. goto free_skbs;
  3544. }
  3545. /* Open code llist_add(&skb->ll_node, &q->defer_list) + queue limit.
  3546. * In the try_cmpxchg() loop, we want to increment q->defer_count
  3547. * at most once to limit the number of skbs in defer_list.
  3548. * We perform the defer_count increment only if the list is not empty,
  3549. * because some arches have slow atomic_long_inc_return().
  3550. */
  3551. first_n = READ_ONCE(q->defer_list.first);
  3552. do {
  3553. if (first_n && !defer_count) {
  3554. defer_count = atomic_long_inc_return(&q->defer_count);
  3555. if (unlikely(defer_count > READ_ONCE(net_hotdata.qdisc_max_burst))) {
  3556. kfree_skb_reason(skb, SKB_DROP_REASON_QDISC_BURST_DROP);
  3557. return NET_XMIT_DROP;
  3558. }
  3559. }
  3560. skb->ll_node.next = first_n;
  3561. } while (!try_cmpxchg(&q->defer_list.first, &first_n, &skb->ll_node));
  3562. /* If defer_list was not empty, we know the cpu which queued
  3563. * the first skb will process the whole list for us.
  3564. */
  3565. if (first_n)
  3566. return NET_XMIT_SUCCESS;
  3567. spin_lock(root_lock);
  3568. ll_list = llist_del_all(&q->defer_list);
  3569. /* There is a small race because we clear defer_count not atomically
  3570. * with the prior llist_del_all(). This means defer_list could grow
  3571. * over qdisc_max_burst.
  3572. */
  3573. atomic_long_set(&q->defer_count, 0);
  3574. ll_list = llist_reverse_order(ll_list);
  3575. if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
  3576. llist_for_each_entry_safe(skb, next, ll_list, ll_node)
  3577. __qdisc_drop(skb, &to_free);
  3578. rc = NET_XMIT_DROP;
  3579. goto unlock;
  3580. }
  3581. if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
  3582. !llist_next(ll_list) && qdisc_run_begin(q)) {
  3583. /*
  3584. * This is a work-conserving queue; there are no old skbs
  3585. * waiting to be sent out; and the qdisc is not running -
  3586. * xmit the skb directly.
  3587. */
  3588. DEBUG_NET_WARN_ON_ONCE(skb != llist_entry(ll_list,
  3589. struct sk_buff,
  3590. ll_node));
  3591. qdisc_bstats_update(q, skb);
  3592. if (sch_direct_xmit(skb, q, dev, txq, root_lock, true))
  3593. __qdisc_run(q);
  3594. to_free2 = qdisc_run_end(q);
  3595. rc = NET_XMIT_SUCCESS;
  3596. } else {
  3597. int count = 0;
  3598. llist_for_each_entry_safe(skb, next, ll_list, ll_node) {
  3599. if (next) {
  3600. prefetch(next);
  3601. prefetch(&next->priority);
  3602. skb_mark_not_on_list(skb);
  3603. }
  3604. rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
  3605. count++;
  3606. }
  3607. to_free2 = qdisc_run(q);
  3608. if (count != 1)
  3609. rc = NET_XMIT_SUCCESS;
  3610. }
  3611. unlock:
  3612. spin_unlock(root_lock);
  3613. free_skbs:
  3614. tcf_kfree_skb_list(to_free);
  3615. tcf_kfree_skb_list(to_free2);
  3616. return rc;
  3617. }
  3618. #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
  3619. static void skb_update_prio(struct sk_buff *skb)
  3620. {
  3621. const struct netprio_map *map;
  3622. const struct sock *sk;
  3623. unsigned int prioidx;
  3624. if (skb->priority)
  3625. return;
  3626. map = rcu_dereference_bh(skb->dev->priomap);
  3627. if (!map)
  3628. return;
  3629. sk = skb_to_full_sk(skb);
  3630. if (!sk)
  3631. return;
  3632. prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
  3633. if (prioidx < map->priomap_len)
  3634. skb->priority = map->priomap[prioidx];
  3635. }
  3636. #else
  3637. #define skb_update_prio(skb)
  3638. #endif
  3639. /**
  3640. * dev_loopback_xmit - loop back @skb
  3641. * @net: network namespace this loopback is happening in
  3642. * @sk: sk needed to be a netfilter okfn
  3643. * @skb: buffer to transmit
  3644. */
  3645. int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
  3646. {
  3647. skb_reset_mac_header(skb);
  3648. __skb_pull(skb, skb_network_offset(skb));
  3649. skb->pkt_type = PACKET_LOOPBACK;
  3650. if (skb->ip_summed == CHECKSUM_NONE)
  3651. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3652. DEBUG_NET_WARN_ON_ONCE(!skb_dst(skb));
  3653. skb_dst_force(skb);
  3654. netif_rx(skb);
  3655. return 0;
  3656. }
  3657. EXPORT_SYMBOL(dev_loopback_xmit);
  3658. #ifdef CONFIG_NET_EGRESS
  3659. static struct netdev_queue *
  3660. netdev_tx_queue_mapping(struct net_device *dev, struct sk_buff *skb)
  3661. {
  3662. int qm = skb_get_queue_mapping(skb);
  3663. return netdev_get_tx_queue(dev, netdev_cap_txqueue(dev, qm));
  3664. }
  3665. #ifndef CONFIG_PREEMPT_RT
  3666. static bool netdev_xmit_txqueue_skipped(void)
  3667. {
  3668. return __this_cpu_read(softnet_data.xmit.skip_txqueue);
  3669. }
  3670. void netdev_xmit_skip_txqueue(bool skip)
  3671. {
  3672. __this_cpu_write(softnet_data.xmit.skip_txqueue, skip);
  3673. }
  3674. EXPORT_SYMBOL_GPL(netdev_xmit_skip_txqueue);
  3675. #else
  3676. static bool netdev_xmit_txqueue_skipped(void)
  3677. {
  3678. return current->net_xmit.skip_txqueue;
  3679. }
  3680. void netdev_xmit_skip_txqueue(bool skip)
  3681. {
  3682. current->net_xmit.skip_txqueue = skip;
  3683. }
  3684. EXPORT_SYMBOL_GPL(netdev_xmit_skip_txqueue);
  3685. #endif
  3686. #endif /* CONFIG_NET_EGRESS */
  3687. #ifdef CONFIG_NET_XGRESS
  3688. static int tc_run(struct tcx_entry *entry, struct sk_buff *skb,
  3689. enum skb_drop_reason *drop_reason)
  3690. {
  3691. int ret = TC_ACT_UNSPEC;
  3692. #ifdef CONFIG_NET_CLS_ACT
  3693. struct mini_Qdisc *miniq = rcu_dereference_bh(entry->miniq);
  3694. struct tcf_result res;
  3695. if (!miniq)
  3696. return ret;
  3697. /* Global bypass */
  3698. if (!static_branch_likely(&tcf_sw_enabled_key))
  3699. return ret;
  3700. /* Block-wise bypass */
  3701. if (tcf_block_bypass_sw(miniq->block))
  3702. return ret;
  3703. tc_skb_cb(skb)->mru = 0;
  3704. qdisc_skb_cb(skb)->post_ct = false;
  3705. tcf_set_drop_reason(skb, *drop_reason);
  3706. mini_qdisc_bstats_cpu_update(miniq, skb);
  3707. ret = tcf_classify(skb, miniq->block, miniq->filter_list, &res, false);
  3708. /* Only tcf related quirks below. */
  3709. switch (ret) {
  3710. case TC_ACT_SHOT:
  3711. *drop_reason = tcf_get_drop_reason(skb);
  3712. mini_qdisc_qstats_cpu_drop(miniq);
  3713. break;
  3714. case TC_ACT_OK:
  3715. case TC_ACT_RECLASSIFY:
  3716. skb->tc_index = TC_H_MIN(res.classid);
  3717. break;
  3718. }
  3719. #endif /* CONFIG_NET_CLS_ACT */
  3720. return ret;
  3721. }
  3722. static DEFINE_STATIC_KEY_FALSE(tcx_needed_key);
  3723. void tcx_inc(void)
  3724. {
  3725. static_branch_inc(&tcx_needed_key);
  3726. }
  3727. void tcx_dec(void)
  3728. {
  3729. static_branch_dec(&tcx_needed_key);
  3730. }
  3731. static __always_inline enum tcx_action_base
  3732. tcx_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
  3733. const bool needs_mac)
  3734. {
  3735. const struct bpf_mprog_fp *fp;
  3736. const struct bpf_prog *prog;
  3737. int ret = TCX_NEXT;
  3738. if (needs_mac)
  3739. __skb_push(skb, skb->mac_len);
  3740. bpf_mprog_foreach_prog(entry, fp, prog) {
  3741. bpf_compute_data_pointers(skb);
  3742. ret = bpf_prog_run(prog, skb);
  3743. if (ret != TCX_NEXT)
  3744. break;
  3745. }
  3746. if (needs_mac)
  3747. __skb_pull(skb, skb->mac_len);
  3748. return tcx_action_code(skb, ret);
  3749. }
  3750. static __always_inline struct sk_buff *
  3751. sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
  3752. struct net_device *orig_dev, bool *another)
  3753. {
  3754. struct bpf_mprog_entry *entry = rcu_dereference_bh(skb->dev->tcx_ingress);
  3755. enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_INGRESS;
  3756. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  3757. int sch_ret;
  3758. if (!entry)
  3759. return skb;
  3760. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  3761. if (unlikely(*pt_prev)) {
  3762. *ret = deliver_skb(skb, *pt_prev, orig_dev);
  3763. *pt_prev = NULL;
  3764. }
  3765. qdisc_pkt_len_segs_init(skb);
  3766. tcx_set_ingress(skb, true);
  3767. if (static_branch_unlikely(&tcx_needed_key)) {
  3768. sch_ret = tcx_run(entry, skb, true);
  3769. if (sch_ret != TC_ACT_UNSPEC)
  3770. goto ingress_verdict;
  3771. }
  3772. sch_ret = tc_run(tcx_entry(entry), skb, &drop_reason);
  3773. ingress_verdict:
  3774. switch (sch_ret) {
  3775. case TC_ACT_REDIRECT:
  3776. /* skb_mac_header check was done by BPF, so we can safely
  3777. * push the L2 header back before redirecting to another
  3778. * netdev.
  3779. */
  3780. __skb_push(skb, skb->mac_len);
  3781. if (skb_do_redirect(skb) == -EAGAIN) {
  3782. __skb_pull(skb, skb->mac_len);
  3783. *another = true;
  3784. break;
  3785. }
  3786. *ret = NET_RX_SUCCESS;
  3787. bpf_net_ctx_clear(bpf_net_ctx);
  3788. return NULL;
  3789. case TC_ACT_SHOT:
  3790. kfree_skb_reason(skb, drop_reason);
  3791. *ret = NET_RX_DROP;
  3792. bpf_net_ctx_clear(bpf_net_ctx);
  3793. return NULL;
  3794. /* used by tc_run */
  3795. case TC_ACT_STOLEN:
  3796. case TC_ACT_QUEUED:
  3797. case TC_ACT_TRAP:
  3798. consume_skb(skb);
  3799. fallthrough;
  3800. case TC_ACT_CONSUMED:
  3801. *ret = NET_RX_SUCCESS;
  3802. bpf_net_ctx_clear(bpf_net_ctx);
  3803. return NULL;
  3804. }
  3805. bpf_net_ctx_clear(bpf_net_ctx);
  3806. return skb;
  3807. }
  3808. static __always_inline struct sk_buff *
  3809. sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
  3810. {
  3811. struct bpf_mprog_entry *entry = rcu_dereference_bh(dev->tcx_egress);
  3812. enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_EGRESS;
  3813. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  3814. int sch_ret;
  3815. if (!entry)
  3816. return skb;
  3817. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  3818. /* qdisc_skb_cb(skb)->pkt_len & tcx_set_ingress() was
  3819. * already set by the caller.
  3820. */
  3821. if (static_branch_unlikely(&tcx_needed_key)) {
  3822. sch_ret = tcx_run(entry, skb, false);
  3823. if (sch_ret != TC_ACT_UNSPEC)
  3824. goto egress_verdict;
  3825. }
  3826. sch_ret = tc_run(tcx_entry(entry), skb, &drop_reason);
  3827. egress_verdict:
  3828. switch (sch_ret) {
  3829. case TC_ACT_REDIRECT:
  3830. /* No need to push/pop skb's mac_header here on egress! */
  3831. skb_do_redirect(skb);
  3832. *ret = NET_XMIT_SUCCESS;
  3833. bpf_net_ctx_clear(bpf_net_ctx);
  3834. return NULL;
  3835. case TC_ACT_SHOT:
  3836. kfree_skb_reason(skb, drop_reason);
  3837. *ret = NET_XMIT_DROP;
  3838. bpf_net_ctx_clear(bpf_net_ctx);
  3839. return NULL;
  3840. /* used by tc_run */
  3841. case TC_ACT_STOLEN:
  3842. case TC_ACT_QUEUED:
  3843. case TC_ACT_TRAP:
  3844. consume_skb(skb);
  3845. fallthrough;
  3846. case TC_ACT_CONSUMED:
  3847. *ret = NET_XMIT_SUCCESS;
  3848. bpf_net_ctx_clear(bpf_net_ctx);
  3849. return NULL;
  3850. }
  3851. bpf_net_ctx_clear(bpf_net_ctx);
  3852. return skb;
  3853. }
  3854. #else
  3855. static __always_inline struct sk_buff *
  3856. sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
  3857. struct net_device *orig_dev, bool *another)
  3858. {
  3859. return skb;
  3860. }
  3861. static __always_inline struct sk_buff *
  3862. sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
  3863. {
  3864. return skb;
  3865. }
  3866. #endif /* CONFIG_NET_XGRESS */
  3867. #ifdef CONFIG_XPS
  3868. static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
  3869. struct xps_dev_maps *dev_maps, unsigned int tci)
  3870. {
  3871. int tc = netdev_get_prio_tc_map(dev, skb->priority);
  3872. struct xps_map *map;
  3873. int queue_index = -1;
  3874. if (tc >= dev_maps->num_tc || tci >= dev_maps->nr_ids)
  3875. return queue_index;
  3876. tci *= dev_maps->num_tc;
  3877. tci += tc;
  3878. map = rcu_dereference(dev_maps->attr_map[tci]);
  3879. if (map) {
  3880. if (map->len == 1)
  3881. queue_index = map->queues[0];
  3882. else
  3883. queue_index = map->queues[reciprocal_scale(
  3884. skb_get_hash(skb), map->len)];
  3885. if (unlikely(queue_index >= dev->real_num_tx_queues))
  3886. queue_index = -1;
  3887. }
  3888. return queue_index;
  3889. }
  3890. #endif
  3891. static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
  3892. struct sk_buff *skb)
  3893. {
  3894. #ifdef CONFIG_XPS
  3895. struct xps_dev_maps *dev_maps;
  3896. struct sock *sk = skb->sk;
  3897. int queue_index = -1;
  3898. if (!static_key_false(&xps_needed))
  3899. return -1;
  3900. rcu_read_lock();
  3901. if (!static_key_false(&xps_rxqs_needed))
  3902. goto get_cpus_map;
  3903. dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_RXQS]);
  3904. if (dev_maps) {
  3905. int tci = sk_rx_queue_get(sk);
  3906. if (tci >= 0)
  3907. queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
  3908. tci);
  3909. }
  3910. get_cpus_map:
  3911. if (queue_index < 0) {
  3912. dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_CPUS]);
  3913. if (dev_maps) {
  3914. unsigned int tci = skb->sender_cpu - 1;
  3915. queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
  3916. tci);
  3917. }
  3918. }
  3919. rcu_read_unlock();
  3920. return queue_index;
  3921. #else
  3922. return -1;
  3923. #endif
  3924. }
  3925. u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
  3926. struct net_device *sb_dev)
  3927. {
  3928. return 0;
  3929. }
  3930. EXPORT_SYMBOL(dev_pick_tx_zero);
  3931. int sk_tx_queue_get(const struct sock *sk)
  3932. {
  3933. int resel, val;
  3934. if (!sk)
  3935. return -1;
  3936. /* Paired with WRITE_ONCE() in sk_tx_queue_clear()
  3937. * and sk_tx_queue_set().
  3938. */
  3939. val = READ_ONCE(sk->sk_tx_queue_mapping);
  3940. if (val == NO_QUEUE_MAPPING)
  3941. return -1;
  3942. if (!sk_fullsock(sk))
  3943. return val;
  3944. resel = READ_ONCE(sock_net(sk)->core.sysctl_txq_reselection);
  3945. if (resel && time_is_before_jiffies(
  3946. READ_ONCE(sk->sk_tx_queue_mapping_jiffies) + resel))
  3947. return -1;
  3948. return val;
  3949. }
  3950. EXPORT_SYMBOL(sk_tx_queue_get);
  3951. u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
  3952. struct net_device *sb_dev)
  3953. {
  3954. struct sock *sk = skb->sk;
  3955. int queue_index = sk_tx_queue_get(sk);
  3956. sb_dev = sb_dev ? : dev;
  3957. if (queue_index < 0 || skb->ooo_okay ||
  3958. queue_index >= dev->real_num_tx_queues) {
  3959. int new_index = get_xps_queue(dev, sb_dev, skb);
  3960. if (new_index < 0)
  3961. new_index = skb_tx_hash(dev, sb_dev, skb);
  3962. if (sk && sk_fullsock(sk) &&
  3963. rcu_access_pointer(sk->sk_dst_cache))
  3964. sk_tx_queue_set(sk, new_index);
  3965. queue_index = new_index;
  3966. }
  3967. return queue_index;
  3968. }
  3969. EXPORT_SYMBOL(netdev_pick_tx);
  3970. struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
  3971. struct sk_buff *skb,
  3972. struct net_device *sb_dev)
  3973. {
  3974. int queue_index = 0;
  3975. #ifdef CONFIG_XPS
  3976. u32 sender_cpu = skb->sender_cpu - 1;
  3977. if (sender_cpu >= (u32)NR_CPUS)
  3978. skb->sender_cpu = raw_smp_processor_id() + 1;
  3979. #endif
  3980. if (dev->real_num_tx_queues != 1) {
  3981. const struct net_device_ops *ops = dev->netdev_ops;
  3982. if (ops->ndo_select_queue)
  3983. queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
  3984. else
  3985. queue_index = netdev_pick_tx(dev, skb, sb_dev);
  3986. queue_index = netdev_cap_txqueue(dev, queue_index);
  3987. }
  3988. skb_set_queue_mapping(skb, queue_index);
  3989. return netdev_get_tx_queue(dev, queue_index);
  3990. }
  3991. /**
  3992. * __dev_queue_xmit() - transmit a buffer
  3993. * @skb: buffer to transmit
  3994. * @sb_dev: suboordinate device used for L2 forwarding offload
  3995. *
  3996. * Queue a buffer for transmission to a network device. The caller must
  3997. * have set the device and priority and built the buffer before calling
  3998. * this function. The function can be called from an interrupt.
  3999. *
  4000. * When calling this method, interrupts MUST be enabled. This is because
  4001. * the BH enable code must have IRQs enabled so that it will not deadlock.
  4002. *
  4003. * Regardless of the return value, the skb is consumed, so it is currently
  4004. * difficult to retry a send to this method. (You can bump the ref count
  4005. * before sending to hold a reference for retry if you are careful.)
  4006. *
  4007. * Return:
  4008. * * 0 - buffer successfully transmitted
  4009. * * positive qdisc return code - NET_XMIT_DROP etc.
  4010. * * negative errno - other errors
  4011. */
  4012. int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
  4013. {
  4014. struct net_device *dev = skb->dev;
  4015. struct netdev_queue *txq = NULL;
  4016. struct Qdisc *q;
  4017. int rc = -ENOMEM;
  4018. bool again = false;
  4019. skb_reset_mac_header(skb);
  4020. skb_assert_len(skb);
  4021. if (unlikely(skb_shinfo(skb)->tx_flags &
  4022. (SKBTX_SCHED_TSTAMP | SKBTX_BPF)))
  4023. __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED);
  4024. /* Disable soft irqs for various locks below. Also
  4025. * stops preemption for RCU.
  4026. */
  4027. rcu_read_lock_bh();
  4028. skb_update_prio(skb);
  4029. qdisc_pkt_len_segs_init(skb);
  4030. tcx_set_ingress(skb, false);
  4031. #ifdef CONFIG_NET_EGRESS
  4032. if (static_branch_unlikely(&egress_needed_key)) {
  4033. if (nf_hook_egress_active()) {
  4034. skb = nf_hook_egress(skb, &rc, dev);
  4035. if (!skb)
  4036. goto out;
  4037. }
  4038. netdev_xmit_skip_txqueue(false);
  4039. nf_skip_egress(skb, true);
  4040. skb = sch_handle_egress(skb, &rc, dev);
  4041. if (!skb)
  4042. goto out;
  4043. nf_skip_egress(skb, false);
  4044. if (netdev_xmit_txqueue_skipped())
  4045. txq = netdev_tx_queue_mapping(dev, skb);
  4046. }
  4047. #endif
  4048. /* If device/qdisc don't need skb->dst, release it right now while
  4049. * its hot in this cpu cache.
  4050. */
  4051. if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
  4052. skb_dst_drop(skb);
  4053. else
  4054. skb_dst_force(skb);
  4055. if (!txq)
  4056. txq = netdev_core_pick_tx(dev, skb, sb_dev);
  4057. q = rcu_dereference_bh(txq->qdisc);
  4058. trace_net_dev_queue(skb);
  4059. if (q->enqueue) {
  4060. rc = __dev_xmit_skb(skb, q, dev, txq);
  4061. goto out;
  4062. }
  4063. /* The device has no queue. Common case for software devices:
  4064. * loopback, all the sorts of tunnels...
  4065. * Really, it is unlikely that netif_tx_lock protection is necessary
  4066. * here. (f.e. loopback and IP tunnels are clean ignoring statistics
  4067. * counters.)
  4068. * However, it is possible, that they rely on protection
  4069. * made by us here.
  4070. * Check this and shot the lock. It is not prone from deadlocks.
  4071. *Either shot noqueue qdisc, it is even simpler 8)
  4072. */
  4073. if (dev->flags & IFF_UP) {
  4074. int cpu = smp_processor_id(); /* ok because BHs are off */
  4075. if (!netif_tx_owned(txq, cpu)) {
  4076. bool is_list = false;
  4077. if (dev_xmit_recursion())
  4078. goto recursion_alert;
  4079. skb = validate_xmit_skb(skb, dev, &again);
  4080. if (!skb)
  4081. goto out;
  4082. HARD_TX_LOCK(dev, txq, cpu);
  4083. if (!netif_xmit_stopped(txq)) {
  4084. is_list = !!skb->next;
  4085. dev_xmit_recursion_inc();
  4086. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  4087. dev_xmit_recursion_dec();
  4088. /* GSO segments a single SKB into
  4089. * a list of frames. TCP expects error
  4090. * to mean none of the data was sent.
  4091. */
  4092. if (is_list)
  4093. rc = NETDEV_TX_OK;
  4094. }
  4095. HARD_TX_UNLOCK(dev, txq);
  4096. if (!skb) /* xmit completed */
  4097. goto out;
  4098. net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
  4099. dev->name);
  4100. /* NETDEV_TX_BUSY or queue was stopped */
  4101. if (!is_list)
  4102. rc = -ENETDOWN;
  4103. } else {
  4104. /* Recursion is detected! It is possible,
  4105. * unfortunately
  4106. */
  4107. recursion_alert:
  4108. net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
  4109. dev->name);
  4110. rc = -ENETDOWN;
  4111. }
  4112. }
  4113. rcu_read_unlock_bh();
  4114. dev_core_stats_tx_dropped_inc(dev);
  4115. kfree_skb_list(skb);
  4116. return rc;
  4117. out:
  4118. rcu_read_unlock_bh();
  4119. return rc;
  4120. }
  4121. EXPORT_SYMBOL(__dev_queue_xmit);
  4122. int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
  4123. {
  4124. struct net_device *dev = skb->dev;
  4125. struct sk_buff *orig_skb = skb;
  4126. struct netdev_queue *txq;
  4127. int ret = NETDEV_TX_BUSY;
  4128. bool again = false;
  4129. if (unlikely(!netif_running(dev) ||
  4130. !netif_carrier_ok(dev)))
  4131. goto drop;
  4132. skb = validate_xmit_skb_list(skb, dev, &again);
  4133. if (skb != orig_skb)
  4134. goto drop;
  4135. skb_set_queue_mapping(skb, queue_id);
  4136. txq = skb_get_tx_queue(dev, skb);
  4137. local_bh_disable();
  4138. dev_xmit_recursion_inc();
  4139. HARD_TX_LOCK(dev, txq, smp_processor_id());
  4140. if (!netif_xmit_frozen_or_drv_stopped(txq))
  4141. ret = netdev_start_xmit(skb, dev, txq, false);
  4142. HARD_TX_UNLOCK(dev, txq);
  4143. dev_xmit_recursion_dec();
  4144. local_bh_enable();
  4145. return ret;
  4146. drop:
  4147. dev_core_stats_tx_dropped_inc(dev);
  4148. kfree_skb_list(skb);
  4149. return NET_XMIT_DROP;
  4150. }
  4151. EXPORT_SYMBOL(__dev_direct_xmit);
  4152. /*************************************************************************
  4153. * Receiver routines
  4154. *************************************************************************/
  4155. static DEFINE_PER_CPU(struct task_struct *, backlog_napi);
  4156. int weight_p __read_mostly = 64; /* old backlog weight */
  4157. int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
  4158. int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
  4159. /* Called with irq disabled */
  4160. static inline void ____napi_schedule(struct softnet_data *sd,
  4161. struct napi_struct *napi)
  4162. {
  4163. struct task_struct *thread;
  4164. lockdep_assert_irqs_disabled();
  4165. if (test_bit(NAPI_STATE_THREADED, &napi->state)) {
  4166. /* Paired with smp_mb__before_atomic() in
  4167. * napi_enable()/netif_set_threaded().
  4168. * Use READ_ONCE() to guarantee a complete
  4169. * read on napi->thread. Only call
  4170. * wake_up_process() when it's not NULL.
  4171. */
  4172. thread = READ_ONCE(napi->thread);
  4173. if (thread) {
  4174. if (use_backlog_threads() && thread == raw_cpu_read(backlog_napi))
  4175. goto use_local_napi;
  4176. set_bit(NAPI_STATE_SCHED_THREADED, &napi->state);
  4177. wake_up_process(thread);
  4178. return;
  4179. }
  4180. }
  4181. use_local_napi:
  4182. DEBUG_NET_WARN_ON_ONCE(!list_empty(&napi->poll_list));
  4183. list_add_tail(&napi->poll_list, &sd->poll_list);
  4184. WRITE_ONCE(napi->list_owner, smp_processor_id());
  4185. /* If not called from net_rx_action()
  4186. * we have to raise NET_RX_SOFTIRQ.
  4187. */
  4188. if (!sd->in_net_rx_action)
  4189. raise_softirq_irqoff(NET_RX_SOFTIRQ);
  4190. }
  4191. #ifdef CONFIG_RPS
  4192. struct static_key_false rps_needed __read_mostly;
  4193. EXPORT_SYMBOL(rps_needed);
  4194. struct static_key_false rfs_needed __read_mostly;
  4195. EXPORT_SYMBOL(rfs_needed);
  4196. static u32 rfs_slot(u32 hash, const struct rps_dev_flow_table *flow_table)
  4197. {
  4198. return hash_32(hash, flow_table->log);
  4199. }
  4200. #ifdef CONFIG_RFS_ACCEL
  4201. /**
  4202. * rps_flow_is_active - check whether the flow is recently active.
  4203. * @rflow: Specific flow to check activity.
  4204. * @flow_table: per-queue flowtable that @rflow belongs to.
  4205. * @cpu: CPU saved in @rflow.
  4206. *
  4207. * If the CPU has processed many packets since the flow's last activity
  4208. * (beyond 10 times the table size), the flow is considered stale.
  4209. *
  4210. * Return: true if flow was recently active.
  4211. */
  4212. static bool rps_flow_is_active(struct rps_dev_flow *rflow,
  4213. struct rps_dev_flow_table *flow_table,
  4214. unsigned int cpu)
  4215. {
  4216. unsigned int flow_last_active;
  4217. unsigned int sd_input_head;
  4218. if (cpu >= nr_cpu_ids)
  4219. return false;
  4220. sd_input_head = READ_ONCE(per_cpu(softnet_data, cpu).input_queue_head);
  4221. flow_last_active = READ_ONCE(rflow->last_qtail);
  4222. return (int)(sd_input_head - flow_last_active) <
  4223. (int)(10 << flow_table->log);
  4224. }
  4225. #endif
  4226. static struct rps_dev_flow *
  4227. set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  4228. struct rps_dev_flow *rflow, u16 next_cpu, u32 hash)
  4229. {
  4230. if (next_cpu < nr_cpu_ids) {
  4231. u32 head;
  4232. #ifdef CONFIG_RFS_ACCEL
  4233. struct netdev_rx_queue *rxqueue;
  4234. struct rps_dev_flow_table *flow_table;
  4235. struct rps_dev_flow *old_rflow;
  4236. struct rps_dev_flow *tmp_rflow;
  4237. unsigned int tmp_cpu;
  4238. u16 rxq_index;
  4239. u32 flow_id;
  4240. int rc;
  4241. /* Should we steer this flow to a different hardware queue? */
  4242. if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
  4243. !(dev->features & NETIF_F_NTUPLE))
  4244. goto out;
  4245. rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
  4246. if (rxq_index == skb_get_rx_queue(skb))
  4247. goto out;
  4248. rxqueue = dev->_rx + rxq_index;
  4249. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  4250. if (!flow_table)
  4251. goto out;
  4252. flow_id = rfs_slot(hash, flow_table);
  4253. tmp_rflow = &flow_table->flows[flow_id];
  4254. tmp_cpu = READ_ONCE(tmp_rflow->cpu);
  4255. if (READ_ONCE(tmp_rflow->filter) != RPS_NO_FILTER) {
  4256. if (rps_flow_is_active(tmp_rflow, flow_table,
  4257. tmp_cpu)) {
  4258. if (hash != READ_ONCE(tmp_rflow->hash) ||
  4259. next_cpu == tmp_cpu)
  4260. goto out;
  4261. }
  4262. }
  4263. rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
  4264. rxq_index, flow_id);
  4265. if (rc < 0)
  4266. goto out;
  4267. old_rflow = rflow;
  4268. rflow = tmp_rflow;
  4269. WRITE_ONCE(rflow->filter, rc);
  4270. WRITE_ONCE(rflow->hash, hash);
  4271. if (old_rflow->filter == rc)
  4272. WRITE_ONCE(old_rflow->filter, RPS_NO_FILTER);
  4273. out:
  4274. #endif
  4275. head = READ_ONCE(per_cpu(softnet_data, next_cpu).input_queue_head);
  4276. rps_input_queue_tail_save(&rflow->last_qtail, head);
  4277. }
  4278. WRITE_ONCE(rflow->cpu, next_cpu);
  4279. return rflow;
  4280. }
  4281. /*
  4282. * get_rps_cpu is called from netif_receive_skb and returns the target
  4283. * CPU from the RPS map of the receiving queue for a given skb.
  4284. * rcu_read_lock must be held on entry.
  4285. */
  4286. static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  4287. struct rps_dev_flow **rflowp)
  4288. {
  4289. const struct rps_sock_flow_table *sock_flow_table;
  4290. struct netdev_rx_queue *rxqueue = dev->_rx;
  4291. struct rps_dev_flow_table *flow_table;
  4292. struct rps_map *map;
  4293. int cpu = -1;
  4294. u32 tcpu;
  4295. u32 hash;
  4296. if (skb_rx_queue_recorded(skb)) {
  4297. u16 index = skb_get_rx_queue(skb);
  4298. if (unlikely(index >= dev->real_num_rx_queues)) {
  4299. WARN_ONCE(dev->real_num_rx_queues > 1,
  4300. "%s received packet on queue %u, but number "
  4301. "of RX queues is %u\n",
  4302. dev->name, index, dev->real_num_rx_queues);
  4303. goto done;
  4304. }
  4305. rxqueue += index;
  4306. }
  4307. /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
  4308. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  4309. map = rcu_dereference(rxqueue->rps_map);
  4310. if (!flow_table && !map)
  4311. goto done;
  4312. skb_reset_network_header(skb);
  4313. hash = skb_get_hash(skb);
  4314. if (!hash)
  4315. goto done;
  4316. sock_flow_table = rcu_dereference(net_hotdata.rps_sock_flow_table);
  4317. if (flow_table && sock_flow_table) {
  4318. struct rps_dev_flow *rflow;
  4319. u32 next_cpu;
  4320. u32 ident;
  4321. /* First check into global flow table if there is a match.
  4322. * This READ_ONCE() pairs with WRITE_ONCE() from rps_record_sock_flow().
  4323. */
  4324. ident = READ_ONCE(sock_flow_table->ents[hash & sock_flow_table->mask]);
  4325. if ((ident ^ hash) & ~net_hotdata.rps_cpu_mask)
  4326. goto try_rps;
  4327. next_cpu = ident & net_hotdata.rps_cpu_mask;
  4328. /* OK, now we know there is a match,
  4329. * we can look at the local (per receive queue) flow table
  4330. */
  4331. rflow = &flow_table->flows[rfs_slot(hash, flow_table)];
  4332. tcpu = rflow->cpu;
  4333. /*
  4334. * If the desired CPU (where last recvmsg was done) is
  4335. * different from current CPU (one in the rx-queue flow
  4336. * table entry), switch if one of the following holds:
  4337. * - Current CPU is unset (>= nr_cpu_ids).
  4338. * - Current CPU is offline.
  4339. * - The current CPU's queue tail has advanced beyond the
  4340. * last packet that was enqueued using this table entry.
  4341. * This guarantees that all previous packets for the flow
  4342. * have been dequeued, thus preserving in order delivery.
  4343. */
  4344. if (unlikely(tcpu != next_cpu) &&
  4345. (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
  4346. ((int)(READ_ONCE(per_cpu(softnet_data, tcpu).input_queue_head) -
  4347. rflow->last_qtail)) >= 0)) {
  4348. tcpu = next_cpu;
  4349. rflow = set_rps_cpu(dev, skb, rflow, next_cpu, hash);
  4350. }
  4351. if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
  4352. *rflowp = rflow;
  4353. cpu = tcpu;
  4354. goto done;
  4355. }
  4356. }
  4357. try_rps:
  4358. if (map) {
  4359. tcpu = map->cpus[reciprocal_scale(hash, map->len)];
  4360. if (cpu_online(tcpu)) {
  4361. cpu = tcpu;
  4362. goto done;
  4363. }
  4364. }
  4365. done:
  4366. return cpu;
  4367. }
  4368. #ifdef CONFIG_RFS_ACCEL
  4369. /**
  4370. * rps_may_expire_flow - check whether an RFS hardware filter may be removed
  4371. * @dev: Device on which the filter was set
  4372. * @rxq_index: RX queue index
  4373. * @flow_id: Flow ID passed to ndo_rx_flow_steer()
  4374. * @filter_id: Filter ID returned by ndo_rx_flow_steer()
  4375. *
  4376. * Drivers that implement ndo_rx_flow_steer() should periodically call
  4377. * this function for each installed filter and remove the filters for
  4378. * which it returns %true.
  4379. */
  4380. bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
  4381. u32 flow_id, u16 filter_id)
  4382. {
  4383. struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
  4384. struct rps_dev_flow_table *flow_table;
  4385. struct rps_dev_flow *rflow;
  4386. bool expire = true;
  4387. rcu_read_lock();
  4388. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  4389. if (flow_table && flow_id < (1UL << flow_table->log)) {
  4390. unsigned int cpu;
  4391. rflow = &flow_table->flows[flow_id];
  4392. cpu = READ_ONCE(rflow->cpu);
  4393. if (READ_ONCE(rflow->filter) == filter_id &&
  4394. rps_flow_is_active(rflow, flow_table, cpu))
  4395. expire = false;
  4396. }
  4397. rcu_read_unlock();
  4398. return expire;
  4399. }
  4400. EXPORT_SYMBOL(rps_may_expire_flow);
  4401. #endif /* CONFIG_RFS_ACCEL */
  4402. /* Called from hardirq (IPI) context */
  4403. static void rps_trigger_softirq(void *data)
  4404. {
  4405. struct softnet_data *sd = data;
  4406. ____napi_schedule(sd, &sd->backlog);
  4407. /* Pairs with READ_ONCE() in softnet_seq_show() */
  4408. WRITE_ONCE(sd->received_rps, sd->received_rps + 1);
  4409. }
  4410. #endif /* CONFIG_RPS */
  4411. /* Called from hardirq (IPI) context */
  4412. static void trigger_rx_softirq(void *data)
  4413. {
  4414. struct softnet_data *sd = data;
  4415. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  4416. smp_store_release(&sd->defer_ipi_scheduled, 0);
  4417. }
  4418. /*
  4419. * After we queued a packet into sd->input_pkt_queue,
  4420. * we need to make sure this queue is serviced soon.
  4421. *
  4422. * - If this is another cpu queue, link it to our rps_ipi_list,
  4423. * and make sure we will process rps_ipi_list from net_rx_action().
  4424. *
  4425. * - If this is our own queue, NAPI schedule our backlog.
  4426. * Note that this also raises NET_RX_SOFTIRQ.
  4427. */
  4428. static void napi_schedule_rps(struct softnet_data *sd)
  4429. {
  4430. struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
  4431. #ifdef CONFIG_RPS
  4432. if (sd != mysd) {
  4433. if (use_backlog_threads()) {
  4434. __napi_schedule_irqoff(&sd->backlog);
  4435. return;
  4436. }
  4437. sd->rps_ipi_next = mysd->rps_ipi_list;
  4438. mysd->rps_ipi_list = sd;
  4439. /* If not called from net_rx_action() or napi_threaded_poll()
  4440. * we have to raise NET_RX_SOFTIRQ.
  4441. */
  4442. if (!mysd->in_net_rx_action && !mysd->in_napi_threaded_poll)
  4443. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  4444. return;
  4445. }
  4446. #endif /* CONFIG_RPS */
  4447. __napi_schedule_irqoff(&mysd->backlog);
  4448. }
  4449. void kick_defer_list_purge(unsigned int cpu)
  4450. {
  4451. struct softnet_data *sd = &per_cpu(softnet_data, cpu);
  4452. unsigned long flags;
  4453. if (use_backlog_threads()) {
  4454. backlog_lock_irq_save(sd, &flags);
  4455. if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state))
  4456. __napi_schedule_irqoff(&sd->backlog);
  4457. backlog_unlock_irq_restore(sd, flags);
  4458. } else if (!cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) {
  4459. smp_call_function_single_async(cpu, &sd->defer_csd);
  4460. }
  4461. }
  4462. #ifdef CONFIG_NET_FLOW_LIMIT
  4463. int netdev_flow_limit_table_len __read_mostly = (1 << 12);
  4464. #endif
  4465. static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen,
  4466. int max_backlog)
  4467. {
  4468. #ifdef CONFIG_NET_FLOW_LIMIT
  4469. unsigned int old_flow, new_flow;
  4470. const struct softnet_data *sd;
  4471. struct sd_flow_limit *fl;
  4472. if (likely(qlen < (max_backlog >> 1)))
  4473. return false;
  4474. sd = this_cpu_ptr(&softnet_data);
  4475. rcu_read_lock();
  4476. fl = rcu_dereference(sd->flow_limit);
  4477. if (fl) {
  4478. new_flow = hash_32(skb_get_hash(skb), fl->log_buckets);
  4479. old_flow = fl->history[fl->history_head];
  4480. fl->history[fl->history_head] = new_flow;
  4481. fl->history_head++;
  4482. fl->history_head &= FLOW_LIMIT_HISTORY - 1;
  4483. if (likely(fl->buckets[old_flow]))
  4484. fl->buckets[old_flow]--;
  4485. if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
  4486. /* Pairs with READ_ONCE() in softnet_seq_show() */
  4487. WRITE_ONCE(fl->count, fl->count + 1);
  4488. rcu_read_unlock();
  4489. return true;
  4490. }
  4491. }
  4492. rcu_read_unlock();
  4493. #endif
  4494. return false;
  4495. }
  4496. /*
  4497. * enqueue_to_backlog is called to queue an skb to a per CPU backlog
  4498. * queue (may be a remote CPU queue).
  4499. */
  4500. static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
  4501. unsigned int *qtail)
  4502. {
  4503. enum skb_drop_reason reason;
  4504. struct softnet_data *sd;
  4505. unsigned long flags;
  4506. unsigned int qlen;
  4507. int max_backlog;
  4508. u32 tail;
  4509. reason = SKB_DROP_REASON_DEV_READY;
  4510. if (unlikely(!netif_running(skb->dev)))
  4511. goto bad_dev;
  4512. sd = &per_cpu(softnet_data, cpu);
  4513. qlen = skb_queue_len_lockless(&sd->input_pkt_queue);
  4514. max_backlog = READ_ONCE(net_hotdata.max_backlog);
  4515. if (unlikely(qlen > max_backlog) ||
  4516. skb_flow_limit(skb, qlen, max_backlog))
  4517. goto cpu_backlog_drop;
  4518. backlog_lock_irq_save(sd, &flags);
  4519. qlen = skb_queue_len(&sd->input_pkt_queue);
  4520. if (likely(qlen <= max_backlog)) {
  4521. if (!qlen) {
  4522. /* Schedule NAPI for backlog device. We can use
  4523. * non atomic operation as we own the queue lock.
  4524. */
  4525. if (!__test_and_set_bit(NAPI_STATE_SCHED,
  4526. &sd->backlog.state))
  4527. napi_schedule_rps(sd);
  4528. }
  4529. __skb_queue_tail(&sd->input_pkt_queue, skb);
  4530. tail = rps_input_queue_tail_incr(sd);
  4531. backlog_unlock_irq_restore(sd, flags);
  4532. /* save the tail outside of the critical section */
  4533. rps_input_queue_tail_save(qtail, tail);
  4534. return NET_RX_SUCCESS;
  4535. }
  4536. backlog_unlock_irq_restore(sd, flags);
  4537. cpu_backlog_drop:
  4538. reason = SKB_DROP_REASON_CPU_BACKLOG;
  4539. numa_drop_add(&sd->drop_counters, 1);
  4540. bad_dev:
  4541. dev_core_stats_rx_dropped_inc(skb->dev);
  4542. kfree_skb_reason(skb, reason);
  4543. return NET_RX_DROP;
  4544. }
  4545. static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
  4546. {
  4547. struct net_device *dev = skb->dev;
  4548. struct netdev_rx_queue *rxqueue;
  4549. rxqueue = dev->_rx;
  4550. if (skb_rx_queue_recorded(skb)) {
  4551. u16 index = skb_get_rx_queue(skb);
  4552. if (unlikely(index >= dev->real_num_rx_queues)) {
  4553. WARN_ONCE(dev->real_num_rx_queues > 1,
  4554. "%s received packet on queue %u, but number "
  4555. "of RX queues is %u\n",
  4556. dev->name, index, dev->real_num_rx_queues);
  4557. return rxqueue; /* Return first rxqueue */
  4558. }
  4559. rxqueue += index;
  4560. }
  4561. return rxqueue;
  4562. }
  4563. u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
  4564. const struct bpf_prog *xdp_prog)
  4565. {
  4566. void *orig_data, *orig_data_end, *hard_start;
  4567. struct netdev_rx_queue *rxqueue;
  4568. bool orig_bcast, orig_host;
  4569. u32 mac_len, frame_sz;
  4570. __be16 orig_eth_type;
  4571. struct ethhdr *eth;
  4572. u32 metalen, act;
  4573. int off;
  4574. /* The XDP program wants to see the packet starting at the MAC
  4575. * header.
  4576. */
  4577. mac_len = skb->data - skb_mac_header(skb);
  4578. hard_start = skb->data - skb_headroom(skb);
  4579. /* SKB "head" area always have tailroom for skb_shared_info */
  4580. frame_sz = (void *)skb_end_pointer(skb) - hard_start;
  4581. frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  4582. rxqueue = netif_get_rxqueue(skb);
  4583. xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq);
  4584. xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len,
  4585. skb_headlen(skb) + mac_len, true);
  4586. if (skb_is_nonlinear(skb)) {
  4587. skb_shinfo(skb)->xdp_frags_size = skb->data_len;
  4588. xdp_buff_set_frags_flag(xdp);
  4589. } else {
  4590. xdp_buff_clear_frags_flag(xdp);
  4591. }
  4592. orig_data_end = xdp->data_end;
  4593. orig_data = xdp->data;
  4594. eth = (struct ethhdr *)xdp->data;
  4595. orig_host = ether_addr_equal_64bits(eth->h_dest, skb->dev->dev_addr);
  4596. orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
  4597. orig_eth_type = eth->h_proto;
  4598. act = bpf_prog_run_xdp(xdp_prog, xdp);
  4599. /* check if bpf_xdp_adjust_head was used */
  4600. off = xdp->data - orig_data;
  4601. if (off) {
  4602. if (off > 0)
  4603. __skb_pull(skb, off);
  4604. else if (off < 0)
  4605. __skb_push(skb, -off);
  4606. skb->mac_header += off;
  4607. skb_reset_network_header(skb);
  4608. }
  4609. /* check if bpf_xdp_adjust_tail was used */
  4610. off = xdp->data_end - orig_data_end;
  4611. if (off != 0) {
  4612. skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
  4613. skb->len += off; /* positive on grow, negative on shrink */
  4614. }
  4615. /* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
  4616. * (e.g. bpf_xdp_adjust_tail), we need to update data_len here.
  4617. */
  4618. if (xdp_buff_has_frags(xdp))
  4619. skb->data_len = skb_shinfo(skb)->xdp_frags_size;
  4620. else
  4621. skb->data_len = 0;
  4622. /* check if XDP changed eth hdr such SKB needs update */
  4623. eth = (struct ethhdr *)xdp->data;
  4624. if ((orig_eth_type != eth->h_proto) ||
  4625. (orig_host != ether_addr_equal_64bits(eth->h_dest,
  4626. skb->dev->dev_addr)) ||
  4627. (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
  4628. __skb_push(skb, ETH_HLEN);
  4629. skb->pkt_type = PACKET_HOST;
  4630. skb->protocol = eth_type_trans(skb, skb->dev);
  4631. }
  4632. /* Redirect/Tx gives L2 packet, code that will reuse skb must __skb_pull
  4633. * before calling us again on redirect path. We do not call do_redirect
  4634. * as we leave that up to the caller.
  4635. *
  4636. * Caller is responsible for managing lifetime of skb (i.e. calling
  4637. * kfree_skb in response to actions it cannot handle/XDP_DROP).
  4638. */
  4639. switch (act) {
  4640. case XDP_REDIRECT:
  4641. case XDP_TX:
  4642. __skb_push(skb, mac_len);
  4643. break;
  4644. case XDP_PASS:
  4645. metalen = xdp->data - xdp->data_meta;
  4646. if (metalen)
  4647. skb_metadata_set(skb, metalen);
  4648. break;
  4649. }
  4650. return act;
  4651. }
  4652. static int
  4653. netif_skb_check_for_xdp(struct sk_buff **pskb, const struct bpf_prog *prog)
  4654. {
  4655. struct sk_buff *skb = *pskb;
  4656. int err, hroom, troom;
  4657. local_lock_nested_bh(&system_page_pool.bh_lock);
  4658. err = skb_cow_data_for_xdp(this_cpu_read(system_page_pool.pool), pskb, prog);
  4659. local_unlock_nested_bh(&system_page_pool.bh_lock);
  4660. if (!err)
  4661. return 0;
  4662. /* In case we have to go down the path and also linearize,
  4663. * then lets do the pskb_expand_head() work just once here.
  4664. */
  4665. hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
  4666. troom = skb->tail + skb->data_len - skb->end;
  4667. err = pskb_expand_head(skb,
  4668. hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
  4669. troom > 0 ? troom + 128 : 0, GFP_ATOMIC);
  4670. if (err)
  4671. return err;
  4672. return skb_linearize(skb);
  4673. }
  4674. static u32 netif_receive_generic_xdp(struct sk_buff **pskb,
  4675. struct xdp_buff *xdp,
  4676. const struct bpf_prog *xdp_prog)
  4677. {
  4678. struct sk_buff *skb = *pskb;
  4679. u32 mac_len, act = XDP_DROP;
  4680. /* Reinjected packets coming from act_mirred or similar should
  4681. * not get XDP generic processing.
  4682. */
  4683. if (skb_is_redirected(skb))
  4684. return XDP_PASS;
  4685. /* XDP packets must have sufficient headroom of XDP_PACKET_HEADROOM
  4686. * bytes. This is the guarantee that also native XDP provides,
  4687. * thus we need to do it here as well.
  4688. */
  4689. mac_len = skb->data - skb_mac_header(skb);
  4690. __skb_push(skb, mac_len);
  4691. if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
  4692. skb_headroom(skb) < XDP_PACKET_HEADROOM) {
  4693. if (netif_skb_check_for_xdp(pskb, xdp_prog))
  4694. goto do_drop;
  4695. }
  4696. __skb_pull(*pskb, mac_len);
  4697. act = bpf_prog_run_generic_xdp(*pskb, xdp, xdp_prog);
  4698. switch (act) {
  4699. case XDP_REDIRECT:
  4700. case XDP_TX:
  4701. case XDP_PASS:
  4702. break;
  4703. default:
  4704. bpf_warn_invalid_xdp_action((*pskb)->dev, xdp_prog, act);
  4705. fallthrough;
  4706. case XDP_ABORTED:
  4707. trace_xdp_exception((*pskb)->dev, xdp_prog, act);
  4708. fallthrough;
  4709. case XDP_DROP:
  4710. do_drop:
  4711. kfree_skb(*pskb);
  4712. break;
  4713. }
  4714. return act;
  4715. }
  4716. /* When doing generic XDP we have to bypass the qdisc layer and the
  4717. * network taps in order to match in-driver-XDP behavior. This also means
  4718. * that XDP packets are able to starve other packets going through a qdisc,
  4719. * and DDOS attacks will be more effective. In-driver-XDP use dedicated TX
  4720. * queues, so they do not have this starvation issue.
  4721. */
  4722. void generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog)
  4723. {
  4724. struct net_device *dev = skb->dev;
  4725. struct netdev_queue *txq;
  4726. bool free_skb = true;
  4727. int cpu, rc;
  4728. txq = netdev_core_pick_tx(dev, skb, NULL);
  4729. cpu = smp_processor_id();
  4730. HARD_TX_LOCK(dev, txq, cpu);
  4731. if (!netif_xmit_frozen_or_drv_stopped(txq)) {
  4732. rc = netdev_start_xmit(skb, dev, txq, 0);
  4733. if (dev_xmit_complete(rc))
  4734. free_skb = false;
  4735. }
  4736. HARD_TX_UNLOCK(dev, txq);
  4737. if (free_skb) {
  4738. trace_xdp_exception(dev, xdp_prog, XDP_TX);
  4739. dev_core_stats_tx_dropped_inc(dev);
  4740. kfree_skb(skb);
  4741. }
  4742. }
  4743. static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
  4744. int do_xdp_generic(const struct bpf_prog *xdp_prog, struct sk_buff **pskb)
  4745. {
  4746. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  4747. if (xdp_prog) {
  4748. struct xdp_buff xdp;
  4749. u32 act;
  4750. int err;
  4751. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  4752. act = netif_receive_generic_xdp(pskb, &xdp, xdp_prog);
  4753. if (act != XDP_PASS) {
  4754. switch (act) {
  4755. case XDP_REDIRECT:
  4756. err = xdp_do_generic_redirect((*pskb)->dev, *pskb,
  4757. &xdp, xdp_prog);
  4758. if (err)
  4759. goto out_redir;
  4760. break;
  4761. case XDP_TX:
  4762. generic_xdp_tx(*pskb, xdp_prog);
  4763. break;
  4764. }
  4765. bpf_net_ctx_clear(bpf_net_ctx);
  4766. return XDP_DROP;
  4767. }
  4768. bpf_net_ctx_clear(bpf_net_ctx);
  4769. }
  4770. return XDP_PASS;
  4771. out_redir:
  4772. bpf_net_ctx_clear(bpf_net_ctx);
  4773. kfree_skb_reason(*pskb, SKB_DROP_REASON_XDP);
  4774. return XDP_DROP;
  4775. }
  4776. EXPORT_SYMBOL_GPL(do_xdp_generic);
  4777. static int netif_rx_internal(struct sk_buff *skb)
  4778. {
  4779. int ret;
  4780. net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), skb);
  4781. trace_netif_rx(skb);
  4782. #ifdef CONFIG_RPS
  4783. if (static_branch_unlikely(&rps_needed)) {
  4784. struct rps_dev_flow voidflow, *rflow = &voidflow;
  4785. int cpu;
  4786. rcu_read_lock();
  4787. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  4788. if (cpu < 0)
  4789. cpu = smp_processor_id();
  4790. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  4791. rcu_read_unlock();
  4792. } else
  4793. #endif
  4794. {
  4795. unsigned int qtail;
  4796. ret = enqueue_to_backlog(skb, smp_processor_id(), &qtail);
  4797. }
  4798. return ret;
  4799. }
  4800. /**
  4801. * __netif_rx - Slightly optimized version of netif_rx
  4802. * @skb: buffer to post
  4803. *
  4804. * This behaves as netif_rx except that it does not disable bottom halves.
  4805. * As a result this function may only be invoked from the interrupt context
  4806. * (either hard or soft interrupt).
  4807. */
  4808. int __netif_rx(struct sk_buff *skb)
  4809. {
  4810. int ret;
  4811. lockdep_assert_once(hardirq_count() | softirq_count());
  4812. trace_netif_rx_entry(skb);
  4813. ret = netif_rx_internal(skb);
  4814. trace_netif_rx_exit(ret);
  4815. return ret;
  4816. }
  4817. EXPORT_SYMBOL(__netif_rx);
  4818. /**
  4819. * netif_rx - post buffer to the network code
  4820. * @skb: buffer to post
  4821. *
  4822. * This function receives a packet from a device driver and queues it for
  4823. * the upper (protocol) levels to process via the backlog NAPI device. It
  4824. * always succeeds. The buffer may be dropped during processing for
  4825. * congestion control or by the protocol layers.
  4826. * The network buffer is passed via the backlog NAPI device. Modern NIC
  4827. * driver should use NAPI and GRO.
  4828. * This function can used from interrupt and from process context. The
  4829. * caller from process context must not disable interrupts before invoking
  4830. * this function.
  4831. *
  4832. * return values:
  4833. * NET_RX_SUCCESS (no congestion)
  4834. * NET_RX_DROP (packet was dropped)
  4835. *
  4836. */
  4837. int netif_rx(struct sk_buff *skb)
  4838. {
  4839. bool need_bh_off = !(hardirq_count() | softirq_count());
  4840. int ret;
  4841. if (need_bh_off)
  4842. local_bh_disable();
  4843. trace_netif_rx_entry(skb);
  4844. ret = netif_rx_internal(skb);
  4845. trace_netif_rx_exit(ret);
  4846. if (need_bh_off)
  4847. local_bh_enable();
  4848. return ret;
  4849. }
  4850. EXPORT_SYMBOL(netif_rx);
  4851. static __latent_entropy void net_tx_action(void)
  4852. {
  4853. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  4854. if (sd->completion_queue) {
  4855. struct sk_buff *clist;
  4856. local_irq_disable();
  4857. clist = sd->completion_queue;
  4858. sd->completion_queue = NULL;
  4859. local_irq_enable();
  4860. while (clist) {
  4861. struct sk_buff *skb = clist;
  4862. clist = clist->next;
  4863. WARN_ON(refcount_read(&skb->users));
  4864. if (likely(get_kfree_skb_cb(skb)->reason == SKB_CONSUMED))
  4865. trace_consume_skb(skb, net_tx_action);
  4866. else
  4867. trace_kfree_skb(skb, net_tx_action,
  4868. get_kfree_skb_cb(skb)->reason, NULL);
  4869. if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
  4870. __kfree_skb(skb);
  4871. else
  4872. __napi_kfree_skb(skb,
  4873. get_kfree_skb_cb(skb)->reason);
  4874. }
  4875. }
  4876. if (sd->output_queue) {
  4877. struct Qdisc *head;
  4878. local_irq_disable();
  4879. head = sd->output_queue;
  4880. sd->output_queue = NULL;
  4881. sd->output_queue_tailp = &sd->output_queue;
  4882. local_irq_enable();
  4883. rcu_read_lock();
  4884. while (head) {
  4885. spinlock_t *root_lock = NULL;
  4886. struct sk_buff *to_free;
  4887. struct Qdisc *q = head;
  4888. head = head->next_sched;
  4889. /* We need to make sure head->next_sched is read
  4890. * before clearing __QDISC_STATE_SCHED
  4891. */
  4892. smp_mb__before_atomic();
  4893. if (!(q->flags & TCQ_F_NOLOCK)) {
  4894. root_lock = qdisc_lock(q);
  4895. spin_lock(root_lock);
  4896. } else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
  4897. &q->state))) {
  4898. /* There is a synchronize_net() between
  4899. * STATE_DEACTIVATED flag being set and
  4900. * qdisc_reset()/some_qdisc_is_busy() in
  4901. * dev_deactivate(), so we can safely bail out
  4902. * early here to avoid data race between
  4903. * qdisc_deactivate() and some_qdisc_is_busy()
  4904. * for lockless qdisc.
  4905. */
  4906. clear_bit(__QDISC_STATE_SCHED, &q->state);
  4907. continue;
  4908. }
  4909. clear_bit(__QDISC_STATE_SCHED, &q->state);
  4910. to_free = qdisc_run(q);
  4911. if (root_lock)
  4912. spin_unlock(root_lock);
  4913. tcf_kfree_skb_list(to_free);
  4914. }
  4915. rcu_read_unlock();
  4916. }
  4917. xfrm_dev_backlog(sd);
  4918. }
  4919. #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
  4920. /* This hook is defined here for ATM LANE */
  4921. int (*br_fdb_test_addr_hook)(struct net_device *dev,
  4922. unsigned char *addr) __read_mostly;
  4923. EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
  4924. #endif
  4925. /**
  4926. * netdev_is_rx_handler_busy - check if receive handler is registered
  4927. * @dev: device to check
  4928. *
  4929. * Check if a receive handler is already registered for a given device.
  4930. * Return true if there one.
  4931. *
  4932. * The caller must hold the rtnl_mutex.
  4933. */
  4934. bool netdev_is_rx_handler_busy(struct net_device *dev)
  4935. {
  4936. ASSERT_RTNL();
  4937. return dev && rtnl_dereference(dev->rx_handler);
  4938. }
  4939. EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
  4940. /**
  4941. * netdev_rx_handler_register - register receive handler
  4942. * @dev: device to register a handler for
  4943. * @rx_handler: receive handler to register
  4944. * @rx_handler_data: data pointer that is used by rx handler
  4945. *
  4946. * Register a receive handler for a device. This handler will then be
  4947. * called from __netif_receive_skb. A negative errno code is returned
  4948. * on a failure.
  4949. *
  4950. * The caller must hold the rtnl_mutex.
  4951. *
  4952. * For a general description of rx_handler, see enum rx_handler_result.
  4953. */
  4954. int netdev_rx_handler_register(struct net_device *dev,
  4955. rx_handler_func_t *rx_handler,
  4956. void *rx_handler_data)
  4957. {
  4958. if (netdev_is_rx_handler_busy(dev))
  4959. return -EBUSY;
  4960. if (dev->priv_flags & IFF_NO_RX_HANDLER)
  4961. return -EINVAL;
  4962. /* Note: rx_handler_data must be set before rx_handler */
  4963. rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
  4964. rcu_assign_pointer(dev->rx_handler, rx_handler);
  4965. return 0;
  4966. }
  4967. EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
  4968. /**
  4969. * netdev_rx_handler_unregister - unregister receive handler
  4970. * @dev: device to unregister a handler from
  4971. *
  4972. * Unregister a receive handler from a device.
  4973. *
  4974. * The caller must hold the rtnl_mutex.
  4975. */
  4976. void netdev_rx_handler_unregister(struct net_device *dev)
  4977. {
  4978. ASSERT_RTNL();
  4979. RCU_INIT_POINTER(dev->rx_handler, NULL);
  4980. /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
  4981. * section has a guarantee to see a non NULL rx_handler_data
  4982. * as well.
  4983. */
  4984. synchronize_net();
  4985. RCU_INIT_POINTER(dev->rx_handler_data, NULL);
  4986. }
  4987. EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
  4988. /*
  4989. * Limit the use of PFMEMALLOC reserves to those protocols that implement
  4990. * the special handling of PFMEMALLOC skbs.
  4991. */
  4992. static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
  4993. {
  4994. switch (skb->protocol) {
  4995. case htons(ETH_P_ARP):
  4996. case htons(ETH_P_IP):
  4997. case htons(ETH_P_IPV6):
  4998. case htons(ETH_P_8021Q):
  4999. case htons(ETH_P_8021AD):
  5000. return true;
  5001. default:
  5002. return false;
  5003. }
  5004. }
  5005. static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
  5006. int *ret, struct net_device *orig_dev)
  5007. {
  5008. if (nf_hook_ingress_active(skb)) {
  5009. int ingress_retval;
  5010. if (unlikely(*pt_prev)) {
  5011. *ret = deliver_skb(skb, *pt_prev, orig_dev);
  5012. *pt_prev = NULL;
  5013. }
  5014. rcu_read_lock();
  5015. ingress_retval = nf_hook_ingress(skb);
  5016. rcu_read_unlock();
  5017. return ingress_retval;
  5018. }
  5019. return 0;
  5020. }
  5021. static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
  5022. struct packet_type **ppt_prev)
  5023. {
  5024. enum skb_drop_reason drop_reason = SKB_DROP_REASON_UNHANDLED_PROTO;
  5025. struct packet_type *ptype, *pt_prev;
  5026. rx_handler_func_t *rx_handler;
  5027. struct sk_buff *skb = *pskb;
  5028. struct net_device *orig_dev;
  5029. bool deliver_exact = false;
  5030. int ret = NET_RX_DROP;
  5031. __be16 type;
  5032. net_timestamp_check(!READ_ONCE(net_hotdata.tstamp_prequeue), skb);
  5033. trace_netif_receive_skb(skb);
  5034. orig_dev = skb->dev;
  5035. skb_reset_network_header(skb);
  5036. #if !defined(CONFIG_DEBUG_NET)
  5037. /* We plan to no longer reset the transport header here.
  5038. * Give some time to fuzzers and dev build to catch bugs
  5039. * in network stacks.
  5040. */
  5041. if (!skb_transport_header_was_set(skb))
  5042. skb_reset_transport_header(skb);
  5043. #endif
  5044. skb_reset_mac_len(skb);
  5045. pt_prev = NULL;
  5046. another_round:
  5047. skb->skb_iif = skb->dev->ifindex;
  5048. __this_cpu_inc(softnet_data.processed);
  5049. if (static_branch_unlikely(&generic_xdp_needed_key)) {
  5050. int ret2;
  5051. migrate_disable();
  5052. ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog),
  5053. &skb);
  5054. migrate_enable();
  5055. if (ret2 != XDP_PASS) {
  5056. ret = NET_RX_DROP;
  5057. goto out;
  5058. }
  5059. }
  5060. if (eth_type_vlan(skb->protocol)) {
  5061. skb = skb_vlan_untag(skb);
  5062. if (unlikely(!skb))
  5063. goto out;
  5064. }
  5065. if (skb_skip_tc_classify(skb))
  5066. goto skip_classify;
  5067. if (pfmemalloc)
  5068. goto skip_taps;
  5069. list_for_each_entry_rcu(ptype, &dev_net_rcu(skb->dev)->ptype_all,
  5070. list) {
  5071. if (unlikely(pt_prev))
  5072. ret = deliver_skb(skb, pt_prev, orig_dev);
  5073. pt_prev = ptype;
  5074. }
  5075. list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
  5076. if (unlikely(pt_prev))
  5077. ret = deliver_skb(skb, pt_prev, orig_dev);
  5078. pt_prev = ptype;
  5079. }
  5080. skip_taps:
  5081. #ifdef CONFIG_NET_INGRESS
  5082. if (static_branch_unlikely(&ingress_needed_key)) {
  5083. bool another = false;
  5084. nf_skip_egress(skb, true);
  5085. skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev,
  5086. &another);
  5087. if (another)
  5088. goto another_round;
  5089. if (!skb)
  5090. goto out;
  5091. nf_skip_egress(skb, false);
  5092. if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
  5093. goto out;
  5094. }
  5095. #endif
  5096. skb_reset_redirect(skb);
  5097. skip_classify:
  5098. if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) {
  5099. drop_reason = SKB_DROP_REASON_PFMEMALLOC;
  5100. goto drop;
  5101. }
  5102. if (skb_vlan_tag_present(skb)) {
  5103. if (unlikely(pt_prev)) {
  5104. ret = deliver_skb(skb, pt_prev, orig_dev);
  5105. pt_prev = NULL;
  5106. }
  5107. if (vlan_do_receive(&skb))
  5108. goto another_round;
  5109. else if (unlikely(!skb))
  5110. goto out;
  5111. }
  5112. rx_handler = rcu_dereference(skb->dev->rx_handler);
  5113. if (rx_handler) {
  5114. if (unlikely(pt_prev)) {
  5115. ret = deliver_skb(skb, pt_prev, orig_dev);
  5116. pt_prev = NULL;
  5117. }
  5118. switch (rx_handler(&skb)) {
  5119. case RX_HANDLER_CONSUMED:
  5120. ret = NET_RX_SUCCESS;
  5121. goto out;
  5122. case RX_HANDLER_ANOTHER:
  5123. goto another_round;
  5124. case RX_HANDLER_EXACT:
  5125. deliver_exact = true;
  5126. break;
  5127. case RX_HANDLER_PASS:
  5128. break;
  5129. default:
  5130. BUG();
  5131. }
  5132. }
  5133. if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) {
  5134. check_vlan_id:
  5135. if (skb_vlan_tag_get_id(skb)) {
  5136. /* Vlan id is non 0 and vlan_do_receive() above couldn't
  5137. * find vlan device.
  5138. */
  5139. skb->pkt_type = PACKET_OTHERHOST;
  5140. } else if (eth_type_vlan(skb->protocol)) {
  5141. /* Outer header is 802.1P with vlan 0, inner header is
  5142. * 802.1Q or 802.1AD and vlan_do_receive() above could
  5143. * not find vlan dev for vlan id 0.
  5144. */
  5145. __vlan_hwaccel_clear_tag(skb);
  5146. skb = skb_vlan_untag(skb);
  5147. if (unlikely(!skb))
  5148. goto out;
  5149. if (vlan_do_receive(&skb))
  5150. /* After stripping off 802.1P header with vlan 0
  5151. * vlan dev is found for inner header.
  5152. */
  5153. goto another_round;
  5154. else if (unlikely(!skb))
  5155. goto out;
  5156. else
  5157. /* We have stripped outer 802.1P vlan 0 header.
  5158. * But could not find vlan dev.
  5159. * check again for vlan id to set OTHERHOST.
  5160. */
  5161. goto check_vlan_id;
  5162. }
  5163. /* Note: we might in the future use prio bits
  5164. * and set skb->priority like in vlan_do_receive()
  5165. * For the time being, just ignore Priority Code Point
  5166. */
  5167. __vlan_hwaccel_clear_tag(skb);
  5168. }
  5169. type = skb->protocol;
  5170. /* deliver only exact match when indicated */
  5171. if (likely(!deliver_exact)) {
  5172. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  5173. &ptype_base[ntohs(type) &
  5174. PTYPE_HASH_MASK]);
  5175. /* orig_dev and skb->dev could belong to different netns;
  5176. * Even in such case we need to traverse only the list
  5177. * coming from skb->dev, as the ptype owner (packet socket)
  5178. * will use dev_net(skb->dev) to do namespace filtering.
  5179. */
  5180. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  5181. &dev_net_rcu(skb->dev)->ptype_specific);
  5182. }
  5183. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  5184. &orig_dev->ptype_specific);
  5185. if (unlikely(skb->dev != orig_dev)) {
  5186. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  5187. &skb->dev->ptype_specific);
  5188. }
  5189. if (pt_prev) {
  5190. *ppt_prev = pt_prev;
  5191. } else {
  5192. drop:
  5193. if (!deliver_exact)
  5194. dev_core_stats_rx_dropped_inc(skb->dev);
  5195. else
  5196. dev_core_stats_rx_nohandler_inc(skb->dev);
  5197. kfree_skb_reason(skb, drop_reason);
  5198. /* Jamal, now you will not able to escape explaining
  5199. * me how you were going to use this. :-)
  5200. */
  5201. ret = NET_RX_DROP;
  5202. }
  5203. out:
  5204. /* The invariant here is that if *ppt_prev is not NULL
  5205. * then skb should also be non-NULL.
  5206. *
  5207. * Apparently *ppt_prev assignment above holds this invariant due to
  5208. * skb dereferencing near it.
  5209. */
  5210. *pskb = skb;
  5211. return ret;
  5212. }
  5213. static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
  5214. {
  5215. struct net_device *orig_dev = skb->dev;
  5216. struct packet_type *pt_prev = NULL;
  5217. int ret;
  5218. ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
  5219. if (pt_prev)
  5220. ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
  5221. skb->dev, pt_prev, orig_dev);
  5222. return ret;
  5223. }
  5224. /**
  5225. * netif_receive_skb_core - special purpose version of netif_receive_skb
  5226. * @skb: buffer to process
  5227. *
  5228. * More direct receive version of netif_receive_skb(). It should
  5229. * only be used by callers that have a need to skip RPS and Generic XDP.
  5230. * Caller must also take care of handling if ``(page_is_)pfmemalloc``.
  5231. *
  5232. * This function may only be called from softirq context and interrupts
  5233. * should be enabled.
  5234. *
  5235. * Return values (usually ignored):
  5236. * NET_RX_SUCCESS: no congestion
  5237. * NET_RX_DROP: packet was dropped
  5238. */
  5239. int netif_receive_skb_core(struct sk_buff *skb)
  5240. {
  5241. int ret;
  5242. rcu_read_lock();
  5243. ret = __netif_receive_skb_one_core(skb, false);
  5244. rcu_read_unlock();
  5245. return ret;
  5246. }
  5247. EXPORT_SYMBOL(netif_receive_skb_core);
  5248. static inline void __netif_receive_skb_list_ptype(struct list_head *head,
  5249. struct packet_type *pt_prev,
  5250. struct net_device *orig_dev)
  5251. {
  5252. struct sk_buff *skb, *next;
  5253. if (!pt_prev)
  5254. return;
  5255. if (list_empty(head))
  5256. return;
  5257. if (pt_prev->list_func != NULL)
  5258. INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
  5259. ip_list_rcv, head, pt_prev, orig_dev);
  5260. else
  5261. list_for_each_entry_safe(skb, next, head, list) {
  5262. skb_list_del_init(skb);
  5263. pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  5264. }
  5265. }
  5266. static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
  5267. {
  5268. /* Fast-path assumptions:
  5269. * - There is no RX handler.
  5270. * - Only one packet_type matches.
  5271. * If either of these fails, we will end up doing some per-packet
  5272. * processing in-line, then handling the 'last ptype' for the whole
  5273. * sublist. This can't cause out-of-order delivery to any single ptype,
  5274. * because the 'last ptype' must be constant across the sublist, and all
  5275. * other ptypes are handled per-packet.
  5276. */
  5277. /* Current (common) ptype of sublist */
  5278. struct packet_type *pt_curr = NULL;
  5279. /* Current (common) orig_dev of sublist */
  5280. struct net_device *od_curr = NULL;
  5281. struct sk_buff *skb, *next;
  5282. LIST_HEAD(sublist);
  5283. list_for_each_entry_safe(skb, next, head, list) {
  5284. struct net_device *orig_dev = skb->dev;
  5285. struct packet_type *pt_prev = NULL;
  5286. skb_list_del_init(skb);
  5287. __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
  5288. if (!pt_prev)
  5289. continue;
  5290. if (pt_curr != pt_prev || od_curr != orig_dev) {
  5291. /* dispatch old sublist */
  5292. __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
  5293. /* start new sublist */
  5294. INIT_LIST_HEAD(&sublist);
  5295. pt_curr = pt_prev;
  5296. od_curr = orig_dev;
  5297. }
  5298. list_add_tail(&skb->list, &sublist);
  5299. }
  5300. /* dispatch final sublist */
  5301. __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
  5302. }
  5303. static int __netif_receive_skb(struct sk_buff *skb)
  5304. {
  5305. int ret;
  5306. if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
  5307. unsigned int noreclaim_flag;
  5308. /*
  5309. * PFMEMALLOC skbs are special, they should
  5310. * - be delivered to SOCK_MEMALLOC sockets only
  5311. * - stay away from userspace
  5312. * - have bounded memory usage
  5313. *
  5314. * Use PF_MEMALLOC as this saves us from propagating the allocation
  5315. * context down to all allocation sites.
  5316. */
  5317. noreclaim_flag = memalloc_noreclaim_save();
  5318. ret = __netif_receive_skb_one_core(skb, true);
  5319. memalloc_noreclaim_restore(noreclaim_flag);
  5320. } else
  5321. ret = __netif_receive_skb_one_core(skb, false);
  5322. return ret;
  5323. }
  5324. static void __netif_receive_skb_list(struct list_head *head)
  5325. {
  5326. unsigned long noreclaim_flag = 0;
  5327. struct sk_buff *skb, *next;
  5328. bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
  5329. list_for_each_entry_safe(skb, next, head, list) {
  5330. if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
  5331. struct list_head sublist;
  5332. /* Handle the previous sublist */
  5333. list_cut_before(&sublist, head, &skb->list);
  5334. if (!list_empty(&sublist))
  5335. __netif_receive_skb_list_core(&sublist, pfmemalloc);
  5336. pfmemalloc = !pfmemalloc;
  5337. /* See comments in __netif_receive_skb */
  5338. if (pfmemalloc)
  5339. noreclaim_flag = memalloc_noreclaim_save();
  5340. else
  5341. memalloc_noreclaim_restore(noreclaim_flag);
  5342. }
  5343. }
  5344. /* Handle the remaining sublist */
  5345. if (!list_empty(head))
  5346. __netif_receive_skb_list_core(head, pfmemalloc);
  5347. /* Restore pflags */
  5348. if (pfmemalloc)
  5349. memalloc_noreclaim_restore(noreclaim_flag);
  5350. }
  5351. static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
  5352. {
  5353. struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
  5354. struct bpf_prog *new = xdp->prog;
  5355. int ret = 0;
  5356. switch (xdp->command) {
  5357. case XDP_SETUP_PROG:
  5358. rcu_assign_pointer(dev->xdp_prog, new);
  5359. if (old)
  5360. bpf_prog_put(old);
  5361. if (old && !new) {
  5362. static_branch_dec(&generic_xdp_needed_key);
  5363. } else if (new && !old) {
  5364. static_branch_inc(&generic_xdp_needed_key);
  5365. netif_disable_lro(dev);
  5366. dev_disable_gro_hw(dev);
  5367. }
  5368. break;
  5369. default:
  5370. ret = -EINVAL;
  5371. break;
  5372. }
  5373. return ret;
  5374. }
  5375. static int netif_receive_skb_internal(struct sk_buff *skb)
  5376. {
  5377. int ret;
  5378. net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), skb);
  5379. if (skb_defer_rx_timestamp(skb))
  5380. return NET_RX_SUCCESS;
  5381. rcu_read_lock();
  5382. #ifdef CONFIG_RPS
  5383. if (static_branch_unlikely(&rps_needed)) {
  5384. struct rps_dev_flow voidflow, *rflow = &voidflow;
  5385. int cpu = get_rps_cpu(skb->dev, skb, &rflow);
  5386. if (cpu >= 0) {
  5387. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  5388. rcu_read_unlock();
  5389. return ret;
  5390. }
  5391. }
  5392. #endif
  5393. ret = __netif_receive_skb(skb);
  5394. rcu_read_unlock();
  5395. return ret;
  5396. }
  5397. void netif_receive_skb_list_internal(struct list_head *head)
  5398. {
  5399. struct sk_buff *skb, *next;
  5400. LIST_HEAD(sublist);
  5401. list_for_each_entry_safe(skb, next, head, list) {
  5402. net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue),
  5403. skb);
  5404. skb_list_del_init(skb);
  5405. if (!skb_defer_rx_timestamp(skb))
  5406. list_add_tail(&skb->list, &sublist);
  5407. }
  5408. list_splice_init(&sublist, head);
  5409. rcu_read_lock();
  5410. #ifdef CONFIG_RPS
  5411. if (static_branch_unlikely(&rps_needed)) {
  5412. list_for_each_entry_safe(skb, next, head, list) {
  5413. struct rps_dev_flow voidflow, *rflow = &voidflow;
  5414. int cpu = get_rps_cpu(skb->dev, skb, &rflow);
  5415. if (cpu >= 0) {
  5416. /* Will be handled, remove from list */
  5417. skb_list_del_init(skb);
  5418. enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  5419. }
  5420. }
  5421. }
  5422. #endif
  5423. __netif_receive_skb_list(head);
  5424. rcu_read_unlock();
  5425. }
  5426. /**
  5427. * netif_receive_skb - process receive buffer from network
  5428. * @skb: buffer to process
  5429. *
  5430. * netif_receive_skb() is the main receive data processing function.
  5431. * It always succeeds. The buffer may be dropped during processing
  5432. * for congestion control or by the protocol layers.
  5433. *
  5434. * This function may only be called from softirq context and interrupts
  5435. * should be enabled.
  5436. *
  5437. * Return values (usually ignored):
  5438. * NET_RX_SUCCESS: no congestion
  5439. * NET_RX_DROP: packet was dropped
  5440. */
  5441. int netif_receive_skb(struct sk_buff *skb)
  5442. {
  5443. int ret;
  5444. trace_netif_receive_skb_entry(skb);
  5445. ret = netif_receive_skb_internal(skb);
  5446. trace_netif_receive_skb_exit(ret);
  5447. return ret;
  5448. }
  5449. EXPORT_SYMBOL(netif_receive_skb);
  5450. /**
  5451. * netif_receive_skb_list - process many receive buffers from network
  5452. * @head: list of skbs to process.
  5453. *
  5454. * Since return value of netif_receive_skb() is normally ignored, and
  5455. * wouldn't be meaningful for a list, this function returns void.
  5456. *
  5457. * This function may only be called from softirq context and interrupts
  5458. * should be enabled.
  5459. */
  5460. void netif_receive_skb_list(struct list_head *head)
  5461. {
  5462. struct sk_buff *skb;
  5463. if (list_empty(head))
  5464. return;
  5465. if (trace_netif_receive_skb_list_entry_enabled()) {
  5466. list_for_each_entry(skb, head, list)
  5467. trace_netif_receive_skb_list_entry(skb);
  5468. }
  5469. netif_receive_skb_list_internal(head);
  5470. trace_netif_receive_skb_list_exit(0);
  5471. }
  5472. EXPORT_SYMBOL(netif_receive_skb_list);
  5473. /* Network device is going away, flush any packets still pending */
  5474. static void flush_backlog(struct work_struct *work)
  5475. {
  5476. struct sk_buff *skb, *tmp;
  5477. struct sk_buff_head list;
  5478. struct softnet_data *sd;
  5479. __skb_queue_head_init(&list);
  5480. local_bh_disable();
  5481. sd = this_cpu_ptr(&softnet_data);
  5482. backlog_lock_irq_disable(sd);
  5483. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  5484. if (READ_ONCE(skb->dev->reg_state) == NETREG_UNREGISTERING) {
  5485. __skb_unlink(skb, &sd->input_pkt_queue);
  5486. __skb_queue_tail(&list, skb);
  5487. rps_input_queue_head_incr(sd);
  5488. }
  5489. }
  5490. backlog_unlock_irq_enable(sd);
  5491. local_lock_nested_bh(&softnet_data.process_queue_bh_lock);
  5492. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  5493. if (READ_ONCE(skb->dev->reg_state) == NETREG_UNREGISTERING) {
  5494. __skb_unlink(skb, &sd->process_queue);
  5495. __skb_queue_tail(&list, skb);
  5496. rps_input_queue_head_incr(sd);
  5497. }
  5498. }
  5499. local_unlock_nested_bh(&softnet_data.process_queue_bh_lock);
  5500. local_bh_enable();
  5501. __skb_queue_purge_reason(&list, SKB_DROP_REASON_DEV_READY);
  5502. }
  5503. static bool flush_required(int cpu)
  5504. {
  5505. #if IS_ENABLED(CONFIG_RPS)
  5506. struct softnet_data *sd = &per_cpu(softnet_data, cpu);
  5507. bool do_flush;
  5508. backlog_lock_irq_disable(sd);
  5509. /* as insertion into process_queue happens with the rps lock held,
  5510. * process_queue access may race only with dequeue
  5511. */
  5512. do_flush = !skb_queue_empty(&sd->input_pkt_queue) ||
  5513. !skb_queue_empty_lockless(&sd->process_queue);
  5514. backlog_unlock_irq_enable(sd);
  5515. return do_flush;
  5516. #endif
  5517. /* without RPS we can't safely check input_pkt_queue: during a
  5518. * concurrent remote skb_queue_splice() we can detect as empty both
  5519. * input_pkt_queue and process_queue even if the latter could end-up
  5520. * containing a lot of packets.
  5521. */
  5522. return true;
  5523. }
  5524. struct flush_backlogs {
  5525. cpumask_t flush_cpus;
  5526. struct work_struct w[];
  5527. };
  5528. static struct flush_backlogs *flush_backlogs_alloc(void)
  5529. {
  5530. return kmalloc_flex(struct flush_backlogs, w, nr_cpu_ids);
  5531. }
  5532. static struct flush_backlogs *flush_backlogs_fallback;
  5533. static DEFINE_MUTEX(flush_backlogs_mutex);
  5534. static void flush_all_backlogs(void)
  5535. {
  5536. struct flush_backlogs *ptr = flush_backlogs_alloc();
  5537. unsigned int cpu;
  5538. if (!ptr) {
  5539. mutex_lock(&flush_backlogs_mutex);
  5540. ptr = flush_backlogs_fallback;
  5541. }
  5542. cpumask_clear(&ptr->flush_cpus);
  5543. cpus_read_lock();
  5544. for_each_online_cpu(cpu) {
  5545. if (flush_required(cpu)) {
  5546. INIT_WORK(&ptr->w[cpu], flush_backlog);
  5547. queue_work_on(cpu, system_highpri_wq, &ptr->w[cpu]);
  5548. __cpumask_set_cpu(cpu, &ptr->flush_cpus);
  5549. }
  5550. }
  5551. /* we can have in flight packet[s] on the cpus we are not flushing,
  5552. * synchronize_net() in unregister_netdevice_many() will take care of
  5553. * them.
  5554. */
  5555. for_each_cpu(cpu, &ptr->flush_cpus)
  5556. flush_work(&ptr->w[cpu]);
  5557. cpus_read_unlock();
  5558. if (ptr != flush_backlogs_fallback)
  5559. kfree(ptr);
  5560. else
  5561. mutex_unlock(&flush_backlogs_mutex);
  5562. }
  5563. static void net_rps_send_ipi(struct softnet_data *remsd)
  5564. {
  5565. #ifdef CONFIG_RPS
  5566. while (remsd) {
  5567. struct softnet_data *next = remsd->rps_ipi_next;
  5568. if (cpu_online(remsd->cpu))
  5569. smp_call_function_single_async(remsd->cpu, &remsd->csd);
  5570. remsd = next;
  5571. }
  5572. #endif
  5573. }
  5574. /*
  5575. * net_rps_action_and_irq_enable sends any pending IPI's for rps.
  5576. * Note: called with local irq disabled, but exits with local irq enabled.
  5577. */
  5578. static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  5579. {
  5580. #ifdef CONFIG_RPS
  5581. struct softnet_data *remsd = sd->rps_ipi_list;
  5582. if (!use_backlog_threads() && remsd) {
  5583. sd->rps_ipi_list = NULL;
  5584. local_irq_enable();
  5585. /* Send pending IPI's to kick RPS processing on remote cpus. */
  5586. net_rps_send_ipi(remsd);
  5587. } else
  5588. #endif
  5589. local_irq_enable();
  5590. }
  5591. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  5592. {
  5593. #ifdef CONFIG_RPS
  5594. return !use_backlog_threads() && sd->rps_ipi_list;
  5595. #else
  5596. return false;
  5597. #endif
  5598. }
  5599. static int process_backlog(struct napi_struct *napi, int quota)
  5600. {
  5601. struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
  5602. bool again = true;
  5603. int work = 0;
  5604. /* Check if we have pending ipi, its better to send them now,
  5605. * not waiting net_rx_action() end.
  5606. */
  5607. if (sd_has_rps_ipi_waiting(sd)) {
  5608. local_irq_disable();
  5609. net_rps_action_and_irq_enable(sd);
  5610. }
  5611. napi->weight = READ_ONCE(net_hotdata.dev_rx_weight);
  5612. while (again) {
  5613. struct sk_buff *skb;
  5614. local_lock_nested_bh(&softnet_data.process_queue_bh_lock);
  5615. while ((skb = __skb_dequeue(&sd->process_queue))) {
  5616. local_unlock_nested_bh(&softnet_data.process_queue_bh_lock);
  5617. rcu_read_lock();
  5618. __netif_receive_skb(skb);
  5619. rcu_read_unlock();
  5620. if (++work >= quota) {
  5621. rps_input_queue_head_add(sd, work);
  5622. return work;
  5623. }
  5624. local_lock_nested_bh(&softnet_data.process_queue_bh_lock);
  5625. }
  5626. local_unlock_nested_bh(&softnet_data.process_queue_bh_lock);
  5627. backlog_lock_irq_disable(sd);
  5628. if (skb_queue_empty(&sd->input_pkt_queue)) {
  5629. /*
  5630. * Inline a custom version of __napi_complete().
  5631. * only current cpu owns and manipulates this napi,
  5632. * and NAPI_STATE_SCHED is the only possible flag set
  5633. * on backlog.
  5634. * We can use a plain write instead of clear_bit(),
  5635. * and we dont need an smp_mb() memory barrier.
  5636. */
  5637. napi->state &= NAPIF_STATE_THREADED;
  5638. again = false;
  5639. } else {
  5640. local_lock_nested_bh(&softnet_data.process_queue_bh_lock);
  5641. skb_queue_splice_tail_init(&sd->input_pkt_queue,
  5642. &sd->process_queue);
  5643. local_unlock_nested_bh(&softnet_data.process_queue_bh_lock);
  5644. }
  5645. backlog_unlock_irq_enable(sd);
  5646. }
  5647. if (work)
  5648. rps_input_queue_head_add(sd, work);
  5649. return work;
  5650. }
  5651. /**
  5652. * __napi_schedule - schedule for receive
  5653. * @n: entry to schedule
  5654. *
  5655. * The entry's receive function will be scheduled to run.
  5656. * Consider using __napi_schedule_irqoff() if hard irqs are masked.
  5657. */
  5658. void __napi_schedule(struct napi_struct *n)
  5659. {
  5660. unsigned long flags;
  5661. local_irq_save(flags);
  5662. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  5663. local_irq_restore(flags);
  5664. }
  5665. EXPORT_SYMBOL(__napi_schedule);
  5666. /**
  5667. * napi_schedule_prep - check if napi can be scheduled
  5668. * @n: napi context
  5669. *
  5670. * Test if NAPI routine is already running, and if not mark
  5671. * it as running. This is used as a condition variable to
  5672. * insure only one NAPI poll instance runs. We also make
  5673. * sure there is no pending NAPI disable.
  5674. */
  5675. bool napi_schedule_prep(struct napi_struct *n)
  5676. {
  5677. unsigned long new, val = READ_ONCE(n->state);
  5678. do {
  5679. if (unlikely(val & NAPIF_STATE_DISABLE))
  5680. return false;
  5681. new = val | NAPIF_STATE_SCHED;
  5682. /* Sets STATE_MISSED bit if STATE_SCHED was already set
  5683. * This was suggested by Alexander Duyck, as compiler
  5684. * emits better code than :
  5685. * if (val & NAPIF_STATE_SCHED)
  5686. * new |= NAPIF_STATE_MISSED;
  5687. */
  5688. new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
  5689. NAPIF_STATE_MISSED;
  5690. } while (!try_cmpxchg(&n->state, &val, new));
  5691. return !(val & NAPIF_STATE_SCHED);
  5692. }
  5693. EXPORT_SYMBOL(napi_schedule_prep);
  5694. /**
  5695. * __napi_schedule_irqoff - schedule for receive
  5696. * @n: entry to schedule
  5697. *
  5698. * Variant of __napi_schedule() assuming hard irqs are masked.
  5699. *
  5700. * On PREEMPT_RT enabled kernels this maps to __napi_schedule()
  5701. * because the interrupt disabled assumption might not be true
  5702. * due to force-threaded interrupts and spinlock substitution.
  5703. */
  5704. void __napi_schedule_irqoff(struct napi_struct *n)
  5705. {
  5706. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  5707. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  5708. else
  5709. __napi_schedule(n);
  5710. }
  5711. EXPORT_SYMBOL(__napi_schedule_irqoff);
  5712. bool napi_complete_done(struct napi_struct *n, int work_done)
  5713. {
  5714. unsigned long flags, val, new, timeout = 0;
  5715. bool ret = true;
  5716. /*
  5717. * 1) Don't let napi dequeue from the cpu poll list
  5718. * just in case its running on a different cpu.
  5719. * 2) If we are busy polling, do nothing here, we have
  5720. * the guarantee we will be called later.
  5721. */
  5722. if (unlikely(n->state & (NAPIF_STATE_NPSVC |
  5723. NAPIF_STATE_IN_BUSY_POLL)))
  5724. return false;
  5725. if (work_done) {
  5726. if (n->gro.bitmask)
  5727. timeout = napi_get_gro_flush_timeout(n);
  5728. n->defer_hard_irqs_count = napi_get_defer_hard_irqs(n);
  5729. }
  5730. if (n->defer_hard_irqs_count > 0) {
  5731. n->defer_hard_irqs_count--;
  5732. timeout = napi_get_gro_flush_timeout(n);
  5733. if (timeout)
  5734. ret = false;
  5735. }
  5736. /*
  5737. * When the NAPI instance uses a timeout and keeps postponing
  5738. * it, we need to bound somehow the time packets are kept in
  5739. * the GRO layer.
  5740. */
  5741. gro_flush_normal(&n->gro, !!timeout);
  5742. if (unlikely(!list_empty(&n->poll_list))) {
  5743. /* If n->poll_list is not empty, we need to mask irqs */
  5744. local_irq_save(flags);
  5745. list_del_init(&n->poll_list);
  5746. local_irq_restore(flags);
  5747. }
  5748. WRITE_ONCE(n->list_owner, -1);
  5749. val = READ_ONCE(n->state);
  5750. do {
  5751. WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
  5752. new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
  5753. NAPIF_STATE_SCHED_THREADED |
  5754. NAPIF_STATE_PREFER_BUSY_POLL);
  5755. /* If STATE_MISSED was set, leave STATE_SCHED set,
  5756. * because we will call napi->poll() one more time.
  5757. * This C code was suggested by Alexander Duyck to help gcc.
  5758. */
  5759. new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
  5760. NAPIF_STATE_SCHED;
  5761. } while (!try_cmpxchg(&n->state, &val, new));
  5762. if (unlikely(val & NAPIF_STATE_MISSED)) {
  5763. __napi_schedule(n);
  5764. return false;
  5765. }
  5766. if (timeout)
  5767. hrtimer_start(&n->timer, ns_to_ktime(timeout),
  5768. HRTIMER_MODE_REL_PINNED);
  5769. return ret;
  5770. }
  5771. EXPORT_SYMBOL(napi_complete_done);
  5772. static void skb_defer_free_flush(void)
  5773. {
  5774. struct llist_node *free_list;
  5775. struct sk_buff *skb, *next;
  5776. struct skb_defer_node *sdn;
  5777. int node;
  5778. for_each_node(node) {
  5779. sdn = this_cpu_ptr(net_hotdata.skb_defer_nodes) + node;
  5780. if (llist_empty(&sdn->defer_list))
  5781. continue;
  5782. atomic_long_set(&sdn->defer_count, 0);
  5783. free_list = llist_del_all(&sdn->defer_list);
  5784. llist_for_each_entry_safe(skb, next, free_list, ll_node) {
  5785. prefetch(next);
  5786. napi_consume_skb(skb, 1);
  5787. }
  5788. }
  5789. }
  5790. #if defined(CONFIG_NET_RX_BUSY_POLL)
  5791. static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule)
  5792. {
  5793. if (!skip_schedule) {
  5794. gro_normal_list(&napi->gro);
  5795. __napi_schedule(napi);
  5796. return;
  5797. }
  5798. /* Flush too old packets. If HZ < 1000, flush all packets */
  5799. gro_flush_normal(&napi->gro, HZ >= 1000);
  5800. clear_bit(NAPI_STATE_SCHED, &napi->state);
  5801. }
  5802. enum {
  5803. NAPI_F_PREFER_BUSY_POLL = 1,
  5804. NAPI_F_END_ON_RESCHED = 2,
  5805. };
  5806. static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock,
  5807. unsigned flags, u16 budget)
  5808. {
  5809. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  5810. bool skip_schedule = false;
  5811. unsigned long timeout;
  5812. int rc;
  5813. /* Busy polling means there is a high chance device driver hard irq
  5814. * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
  5815. * set in napi_schedule_prep().
  5816. * Since we are about to call napi->poll() once more, we can safely
  5817. * clear NAPI_STATE_MISSED.
  5818. *
  5819. * Note: x86 could use a single "lock and ..." instruction
  5820. * to perform these two clear_bit()
  5821. */
  5822. clear_bit(NAPI_STATE_MISSED, &napi->state);
  5823. clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
  5824. local_bh_disable();
  5825. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  5826. if (flags & NAPI_F_PREFER_BUSY_POLL) {
  5827. napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
  5828. timeout = napi_get_gro_flush_timeout(napi);
  5829. if (napi->defer_hard_irqs_count && timeout) {
  5830. hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
  5831. skip_schedule = true;
  5832. }
  5833. }
  5834. /* All we really want here is to re-enable device interrupts.
  5835. * Ideally, a new ndo_busy_poll_stop() could avoid another round.
  5836. */
  5837. rc = napi->poll(napi, budget);
  5838. /* We can't gro_normal_list() here, because napi->poll() might have
  5839. * rearmed the napi (napi_complete_done()) in which case it could
  5840. * already be running on another CPU.
  5841. */
  5842. trace_napi_poll(napi, rc, budget);
  5843. netpoll_poll_unlock(have_poll_lock);
  5844. if (rc == budget)
  5845. __busy_poll_stop(napi, skip_schedule);
  5846. bpf_net_ctx_clear(bpf_net_ctx);
  5847. local_bh_enable();
  5848. }
  5849. static void __napi_busy_loop(unsigned int napi_id,
  5850. bool (*loop_end)(void *, unsigned long),
  5851. void *loop_end_arg, unsigned flags, u16 budget)
  5852. {
  5853. unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
  5854. int (*napi_poll)(struct napi_struct *napi, int budget);
  5855. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  5856. void *have_poll_lock = NULL;
  5857. struct napi_struct *napi;
  5858. WARN_ON_ONCE(!rcu_read_lock_held());
  5859. restart:
  5860. napi_poll = NULL;
  5861. napi = napi_by_id(napi_id);
  5862. if (!napi)
  5863. return;
  5864. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  5865. preempt_disable();
  5866. for (;;) {
  5867. int work = 0;
  5868. local_bh_disable();
  5869. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  5870. if (!napi_poll) {
  5871. unsigned long val = READ_ONCE(napi->state);
  5872. /* If multiple threads are competing for this napi,
  5873. * we avoid dirtying napi->state as much as we can.
  5874. */
  5875. if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
  5876. NAPIF_STATE_IN_BUSY_POLL)) {
  5877. if (flags & NAPI_F_PREFER_BUSY_POLL)
  5878. set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
  5879. goto count;
  5880. }
  5881. if (cmpxchg(&napi->state, val,
  5882. val | NAPIF_STATE_IN_BUSY_POLL |
  5883. NAPIF_STATE_SCHED) != val) {
  5884. if (flags & NAPI_F_PREFER_BUSY_POLL)
  5885. set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
  5886. goto count;
  5887. }
  5888. have_poll_lock = netpoll_poll_lock(napi);
  5889. napi_poll = napi->poll;
  5890. }
  5891. work = napi_poll(napi, budget);
  5892. trace_napi_poll(napi, work, budget);
  5893. gro_normal_list(&napi->gro);
  5894. count:
  5895. if (work > 0)
  5896. __NET_ADD_STATS(dev_net(napi->dev),
  5897. LINUX_MIB_BUSYPOLLRXPACKETS, work);
  5898. skb_defer_free_flush();
  5899. bpf_net_ctx_clear(bpf_net_ctx);
  5900. local_bh_enable();
  5901. if (!loop_end || loop_end(loop_end_arg, start_time))
  5902. break;
  5903. if (unlikely(need_resched())) {
  5904. if (flags & NAPI_F_END_ON_RESCHED)
  5905. break;
  5906. if (napi_poll)
  5907. busy_poll_stop(napi, have_poll_lock, flags, budget);
  5908. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  5909. preempt_enable();
  5910. rcu_read_unlock();
  5911. cond_resched();
  5912. rcu_read_lock();
  5913. if (loop_end(loop_end_arg, start_time))
  5914. return;
  5915. goto restart;
  5916. }
  5917. cpu_relax();
  5918. }
  5919. if (napi_poll)
  5920. busy_poll_stop(napi, have_poll_lock, flags, budget);
  5921. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  5922. preempt_enable();
  5923. }
  5924. void napi_busy_loop_rcu(unsigned int napi_id,
  5925. bool (*loop_end)(void *, unsigned long),
  5926. void *loop_end_arg, bool prefer_busy_poll, u16 budget)
  5927. {
  5928. unsigned flags = NAPI_F_END_ON_RESCHED;
  5929. if (prefer_busy_poll)
  5930. flags |= NAPI_F_PREFER_BUSY_POLL;
  5931. __napi_busy_loop(napi_id, loop_end, loop_end_arg, flags, budget);
  5932. }
  5933. void napi_busy_loop(unsigned int napi_id,
  5934. bool (*loop_end)(void *, unsigned long),
  5935. void *loop_end_arg, bool prefer_busy_poll, u16 budget)
  5936. {
  5937. unsigned flags = prefer_busy_poll ? NAPI_F_PREFER_BUSY_POLL : 0;
  5938. rcu_read_lock();
  5939. __napi_busy_loop(napi_id, loop_end, loop_end_arg, flags, budget);
  5940. rcu_read_unlock();
  5941. }
  5942. EXPORT_SYMBOL(napi_busy_loop);
  5943. void napi_suspend_irqs(unsigned int napi_id)
  5944. {
  5945. struct napi_struct *napi;
  5946. rcu_read_lock();
  5947. napi = napi_by_id(napi_id);
  5948. if (napi) {
  5949. unsigned long timeout = napi_get_irq_suspend_timeout(napi);
  5950. if (timeout)
  5951. hrtimer_start(&napi->timer, ns_to_ktime(timeout),
  5952. HRTIMER_MODE_REL_PINNED);
  5953. }
  5954. rcu_read_unlock();
  5955. }
  5956. void napi_resume_irqs(unsigned int napi_id)
  5957. {
  5958. struct napi_struct *napi;
  5959. rcu_read_lock();
  5960. napi = napi_by_id(napi_id);
  5961. if (napi) {
  5962. /* If irq_suspend_timeout is set to 0 between the call to
  5963. * napi_suspend_irqs and now, the original value still
  5964. * determines the safety timeout as intended and napi_watchdog
  5965. * will resume irq processing.
  5966. */
  5967. if (napi_get_irq_suspend_timeout(napi)) {
  5968. local_bh_disable();
  5969. napi_schedule(napi);
  5970. local_bh_enable();
  5971. }
  5972. }
  5973. rcu_read_unlock();
  5974. }
  5975. #endif /* CONFIG_NET_RX_BUSY_POLL */
  5976. static void __napi_hash_add_with_id(struct napi_struct *napi,
  5977. unsigned int napi_id)
  5978. {
  5979. napi->gro.cached_napi_id = napi_id;
  5980. WRITE_ONCE(napi->napi_id, napi_id);
  5981. hlist_add_head_rcu(&napi->napi_hash_node,
  5982. &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
  5983. }
  5984. static void napi_hash_add_with_id(struct napi_struct *napi,
  5985. unsigned int napi_id)
  5986. {
  5987. unsigned long flags;
  5988. spin_lock_irqsave(&napi_hash_lock, flags);
  5989. WARN_ON_ONCE(napi_by_id(napi_id));
  5990. __napi_hash_add_with_id(napi, napi_id);
  5991. spin_unlock_irqrestore(&napi_hash_lock, flags);
  5992. }
  5993. static void napi_hash_add(struct napi_struct *napi)
  5994. {
  5995. unsigned long flags;
  5996. if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state))
  5997. return;
  5998. spin_lock_irqsave(&napi_hash_lock, flags);
  5999. /* 0..NR_CPUS range is reserved for sender_cpu use */
  6000. do {
  6001. if (unlikely(!napi_id_valid(++napi_gen_id)))
  6002. napi_gen_id = MIN_NAPI_ID;
  6003. } while (napi_by_id(napi_gen_id));
  6004. __napi_hash_add_with_id(napi, napi_gen_id);
  6005. spin_unlock_irqrestore(&napi_hash_lock, flags);
  6006. }
  6007. /* Warning : caller is responsible to make sure rcu grace period
  6008. * is respected before freeing memory containing @napi
  6009. */
  6010. static void napi_hash_del(struct napi_struct *napi)
  6011. {
  6012. unsigned long flags;
  6013. spin_lock_irqsave(&napi_hash_lock, flags);
  6014. hlist_del_init_rcu(&napi->napi_hash_node);
  6015. spin_unlock_irqrestore(&napi_hash_lock, flags);
  6016. }
  6017. static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
  6018. {
  6019. struct napi_struct *napi;
  6020. napi = container_of(timer, struct napi_struct, timer);
  6021. /* Note : we use a relaxed variant of napi_schedule_prep() not setting
  6022. * NAPI_STATE_MISSED, since we do not react to a device IRQ.
  6023. */
  6024. if (!napi_disable_pending(napi) &&
  6025. !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) {
  6026. clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
  6027. __napi_schedule_irqoff(napi);
  6028. }
  6029. return HRTIMER_NORESTART;
  6030. }
  6031. static void napi_stop_kthread(struct napi_struct *napi)
  6032. {
  6033. unsigned long val, new;
  6034. /* Wait until the napi STATE_THREADED is unset. */
  6035. while (true) {
  6036. val = READ_ONCE(napi->state);
  6037. /* If napi kthread own this napi or the napi is idle,
  6038. * STATE_THREADED can be unset here.
  6039. */
  6040. if ((val & NAPIF_STATE_SCHED_THREADED) ||
  6041. !(val & NAPIF_STATE_SCHED)) {
  6042. new = val & (~(NAPIF_STATE_THREADED |
  6043. NAPIF_STATE_THREADED_BUSY_POLL));
  6044. } else {
  6045. msleep(20);
  6046. continue;
  6047. }
  6048. if (try_cmpxchg(&napi->state, &val, new))
  6049. break;
  6050. }
  6051. /* Once STATE_THREADED is unset, wait for SCHED_THREADED to be unset by
  6052. * the kthread.
  6053. */
  6054. while (true) {
  6055. if (!test_bit(NAPI_STATE_SCHED_THREADED, &napi->state))
  6056. break;
  6057. msleep(20);
  6058. }
  6059. kthread_stop(napi->thread);
  6060. napi->thread = NULL;
  6061. }
  6062. static void napi_set_threaded_state(struct napi_struct *napi,
  6063. enum netdev_napi_threaded threaded_mode)
  6064. {
  6065. bool threaded = threaded_mode != NETDEV_NAPI_THREADED_DISABLED;
  6066. bool busy_poll = threaded_mode == NETDEV_NAPI_THREADED_BUSY_POLL;
  6067. assign_bit(NAPI_STATE_THREADED, &napi->state, threaded);
  6068. assign_bit(NAPI_STATE_THREADED_BUSY_POLL, &napi->state, busy_poll);
  6069. }
  6070. int napi_set_threaded(struct napi_struct *napi,
  6071. enum netdev_napi_threaded threaded)
  6072. {
  6073. if (threaded) {
  6074. if (!napi->thread) {
  6075. int err = napi_kthread_create(napi);
  6076. if (err)
  6077. return err;
  6078. }
  6079. }
  6080. if (napi->config)
  6081. napi->config->threaded = threaded;
  6082. /* Setting/unsetting threaded mode on a napi might not immediately
  6083. * take effect, if the current napi instance is actively being
  6084. * polled. In this case, the switch between threaded mode and
  6085. * softirq mode will happen in the next round of napi_schedule().
  6086. * This should not cause hiccups/stalls to the live traffic.
  6087. */
  6088. if (!threaded && napi->thread) {
  6089. napi_stop_kthread(napi);
  6090. } else {
  6091. /* Make sure kthread is created before THREADED bit is set. */
  6092. smp_mb__before_atomic();
  6093. napi_set_threaded_state(napi, threaded);
  6094. }
  6095. return 0;
  6096. }
  6097. int netif_set_threaded(struct net_device *dev,
  6098. enum netdev_napi_threaded threaded)
  6099. {
  6100. struct napi_struct *napi;
  6101. int i, err = 0;
  6102. netdev_assert_locked_or_invisible(dev);
  6103. if (threaded) {
  6104. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  6105. if (!napi->thread) {
  6106. err = napi_kthread_create(napi);
  6107. if (err) {
  6108. threaded = NETDEV_NAPI_THREADED_DISABLED;
  6109. break;
  6110. }
  6111. }
  6112. }
  6113. }
  6114. WRITE_ONCE(dev->threaded, threaded);
  6115. /* The error should not occur as the kthreads are already created. */
  6116. list_for_each_entry(napi, &dev->napi_list, dev_list)
  6117. WARN_ON_ONCE(napi_set_threaded(napi, threaded));
  6118. /* Override the config for all NAPIs even if currently not listed */
  6119. for (i = 0; i < dev->num_napi_configs; i++)
  6120. dev->napi_config[i].threaded = threaded;
  6121. return err;
  6122. }
  6123. /**
  6124. * netif_threaded_enable() - enable threaded NAPIs
  6125. * @dev: net_device instance
  6126. *
  6127. * Enable threaded mode for the NAPI instances of the device. This may be useful
  6128. * for devices where multiple NAPI instances get scheduled by a single
  6129. * interrupt. Threaded NAPI allows moving the NAPI processing to cores other
  6130. * than the core where IRQ is mapped.
  6131. *
  6132. * This function should be called before @dev is registered.
  6133. */
  6134. void netif_threaded_enable(struct net_device *dev)
  6135. {
  6136. WARN_ON_ONCE(netif_set_threaded(dev, NETDEV_NAPI_THREADED_ENABLED));
  6137. }
  6138. EXPORT_SYMBOL(netif_threaded_enable);
  6139. /**
  6140. * netif_queue_set_napi - Associate queue with the napi
  6141. * @dev: device to which NAPI and queue belong
  6142. * @queue_index: Index of queue
  6143. * @type: queue type as RX or TX
  6144. * @napi: NAPI context, pass NULL to clear previously set NAPI
  6145. *
  6146. * Set queue with its corresponding napi context. This should be done after
  6147. * registering the NAPI handler for the queue-vector and the queues have been
  6148. * mapped to the corresponding interrupt vector.
  6149. */
  6150. void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index,
  6151. enum netdev_queue_type type, struct napi_struct *napi)
  6152. {
  6153. struct netdev_rx_queue *rxq;
  6154. struct netdev_queue *txq;
  6155. if (WARN_ON_ONCE(napi && !napi->dev))
  6156. return;
  6157. netdev_ops_assert_locked_or_invisible(dev);
  6158. switch (type) {
  6159. case NETDEV_QUEUE_TYPE_RX:
  6160. rxq = __netif_get_rx_queue(dev, queue_index);
  6161. rxq->napi = napi;
  6162. return;
  6163. case NETDEV_QUEUE_TYPE_TX:
  6164. txq = netdev_get_tx_queue(dev, queue_index);
  6165. txq->napi = napi;
  6166. return;
  6167. default:
  6168. return;
  6169. }
  6170. }
  6171. EXPORT_SYMBOL(netif_queue_set_napi);
  6172. static void
  6173. netif_napi_irq_notify(struct irq_affinity_notify *notify,
  6174. const cpumask_t *mask)
  6175. {
  6176. struct napi_struct *napi =
  6177. container_of(notify, struct napi_struct, notify);
  6178. #ifdef CONFIG_RFS_ACCEL
  6179. struct cpu_rmap *rmap = napi->dev->rx_cpu_rmap;
  6180. int err;
  6181. #endif
  6182. if (napi->config && napi->dev->irq_affinity_auto)
  6183. cpumask_copy(&napi->config->affinity_mask, mask);
  6184. #ifdef CONFIG_RFS_ACCEL
  6185. if (napi->dev->rx_cpu_rmap_auto) {
  6186. err = cpu_rmap_update(rmap, napi->napi_rmap_idx, mask);
  6187. if (err)
  6188. netdev_warn(napi->dev, "RMAP update failed (%d)\n",
  6189. err);
  6190. }
  6191. #endif
  6192. }
  6193. #ifdef CONFIG_RFS_ACCEL
  6194. static void netif_napi_affinity_release(struct kref *ref)
  6195. {
  6196. struct napi_struct *napi =
  6197. container_of(ref, struct napi_struct, notify.kref);
  6198. struct cpu_rmap *rmap = napi->dev->rx_cpu_rmap;
  6199. netdev_assert_locked(napi->dev);
  6200. WARN_ON(test_and_clear_bit(NAPI_STATE_HAS_NOTIFIER,
  6201. &napi->state));
  6202. if (!napi->dev->rx_cpu_rmap_auto)
  6203. return;
  6204. rmap->obj[napi->napi_rmap_idx] = NULL;
  6205. napi->napi_rmap_idx = -1;
  6206. cpu_rmap_put(rmap);
  6207. }
  6208. int netif_enable_cpu_rmap(struct net_device *dev, unsigned int num_irqs)
  6209. {
  6210. if (dev->rx_cpu_rmap_auto)
  6211. return 0;
  6212. dev->rx_cpu_rmap = alloc_irq_cpu_rmap(num_irqs);
  6213. if (!dev->rx_cpu_rmap)
  6214. return -ENOMEM;
  6215. dev->rx_cpu_rmap_auto = true;
  6216. return 0;
  6217. }
  6218. EXPORT_SYMBOL(netif_enable_cpu_rmap);
  6219. static void netif_del_cpu_rmap(struct net_device *dev)
  6220. {
  6221. struct cpu_rmap *rmap = dev->rx_cpu_rmap;
  6222. if (!dev->rx_cpu_rmap_auto)
  6223. return;
  6224. /* Free the rmap */
  6225. cpu_rmap_put(rmap);
  6226. dev->rx_cpu_rmap = NULL;
  6227. dev->rx_cpu_rmap_auto = false;
  6228. }
  6229. #else
  6230. static void netif_napi_affinity_release(struct kref *ref)
  6231. {
  6232. }
  6233. int netif_enable_cpu_rmap(struct net_device *dev, unsigned int num_irqs)
  6234. {
  6235. return 0;
  6236. }
  6237. EXPORT_SYMBOL(netif_enable_cpu_rmap);
  6238. static void netif_del_cpu_rmap(struct net_device *dev)
  6239. {
  6240. }
  6241. #endif
  6242. void netif_set_affinity_auto(struct net_device *dev)
  6243. {
  6244. unsigned int i, maxqs, numa;
  6245. maxqs = max(dev->num_tx_queues, dev->num_rx_queues);
  6246. numa = dev_to_node(&dev->dev);
  6247. for (i = 0; i < maxqs; i++)
  6248. cpumask_set_cpu(cpumask_local_spread(i, numa),
  6249. &dev->napi_config[i].affinity_mask);
  6250. dev->irq_affinity_auto = true;
  6251. }
  6252. EXPORT_SYMBOL(netif_set_affinity_auto);
  6253. void netif_napi_set_irq_locked(struct napi_struct *napi, int irq)
  6254. {
  6255. int rc;
  6256. netdev_assert_locked_or_invisible(napi->dev);
  6257. if (napi->irq == irq)
  6258. return;
  6259. /* Remove existing resources */
  6260. if (test_and_clear_bit(NAPI_STATE_HAS_NOTIFIER, &napi->state))
  6261. irq_set_affinity_notifier(napi->irq, NULL);
  6262. napi->irq = irq;
  6263. if (irq < 0 ||
  6264. (!napi->dev->rx_cpu_rmap_auto && !napi->dev->irq_affinity_auto))
  6265. return;
  6266. /* Abort for buggy drivers */
  6267. if (napi->dev->irq_affinity_auto && WARN_ON_ONCE(!napi->config))
  6268. return;
  6269. #ifdef CONFIG_RFS_ACCEL
  6270. if (napi->dev->rx_cpu_rmap_auto) {
  6271. rc = cpu_rmap_add(napi->dev->rx_cpu_rmap, napi);
  6272. if (rc < 0)
  6273. return;
  6274. cpu_rmap_get(napi->dev->rx_cpu_rmap);
  6275. napi->napi_rmap_idx = rc;
  6276. }
  6277. #endif
  6278. /* Use core IRQ notifier */
  6279. napi->notify.notify = netif_napi_irq_notify;
  6280. napi->notify.release = netif_napi_affinity_release;
  6281. rc = irq_set_affinity_notifier(irq, &napi->notify);
  6282. if (rc) {
  6283. netdev_warn(napi->dev, "Unable to set IRQ notifier (%d)\n",
  6284. rc);
  6285. goto put_rmap;
  6286. }
  6287. set_bit(NAPI_STATE_HAS_NOTIFIER, &napi->state);
  6288. return;
  6289. put_rmap:
  6290. #ifdef CONFIG_RFS_ACCEL
  6291. if (napi->dev->rx_cpu_rmap_auto) {
  6292. napi->dev->rx_cpu_rmap->obj[napi->napi_rmap_idx] = NULL;
  6293. cpu_rmap_put(napi->dev->rx_cpu_rmap);
  6294. napi->napi_rmap_idx = -1;
  6295. }
  6296. #endif
  6297. napi->notify.notify = NULL;
  6298. napi->notify.release = NULL;
  6299. }
  6300. EXPORT_SYMBOL(netif_napi_set_irq_locked);
  6301. static void napi_restore_config(struct napi_struct *n)
  6302. {
  6303. n->defer_hard_irqs = n->config->defer_hard_irqs;
  6304. n->gro_flush_timeout = n->config->gro_flush_timeout;
  6305. n->irq_suspend_timeout = n->config->irq_suspend_timeout;
  6306. if (n->dev->irq_affinity_auto &&
  6307. test_bit(NAPI_STATE_HAS_NOTIFIER, &n->state))
  6308. irq_set_affinity(n->irq, &n->config->affinity_mask);
  6309. /* a NAPI ID might be stored in the config, if so use it. if not, use
  6310. * napi_hash_add to generate one for us.
  6311. */
  6312. if (n->config->napi_id) {
  6313. napi_hash_add_with_id(n, n->config->napi_id);
  6314. } else {
  6315. napi_hash_add(n);
  6316. n->config->napi_id = n->napi_id;
  6317. }
  6318. WARN_ON_ONCE(napi_set_threaded(n, n->config->threaded));
  6319. }
  6320. static void napi_save_config(struct napi_struct *n)
  6321. {
  6322. n->config->defer_hard_irqs = n->defer_hard_irqs;
  6323. n->config->gro_flush_timeout = n->gro_flush_timeout;
  6324. n->config->irq_suspend_timeout = n->irq_suspend_timeout;
  6325. napi_hash_del(n);
  6326. }
  6327. /* Netlink wants the NAPI list to be sorted by ID, if adding a NAPI which will
  6328. * inherit an existing ID try to insert it at the right position.
  6329. */
  6330. static void
  6331. netif_napi_dev_list_add(struct net_device *dev, struct napi_struct *napi)
  6332. {
  6333. unsigned int new_id, pos_id;
  6334. struct list_head *higher;
  6335. struct napi_struct *pos;
  6336. new_id = UINT_MAX;
  6337. if (napi->config && napi->config->napi_id)
  6338. new_id = napi->config->napi_id;
  6339. higher = &dev->napi_list;
  6340. list_for_each_entry(pos, &dev->napi_list, dev_list) {
  6341. if (napi_id_valid(pos->napi_id))
  6342. pos_id = pos->napi_id;
  6343. else if (pos->config)
  6344. pos_id = pos->config->napi_id;
  6345. else
  6346. pos_id = UINT_MAX;
  6347. if (pos_id <= new_id)
  6348. break;
  6349. higher = &pos->dev_list;
  6350. }
  6351. list_add_rcu(&napi->dev_list, higher); /* adds after higher */
  6352. }
  6353. /* Double check that napi_get_frags() allocates skbs with
  6354. * skb->head being backed by slab, not a page fragment.
  6355. * This is to make sure bug fixed in 3226b158e67c
  6356. * ("net: avoid 32 x truesize under-estimation for tiny skbs")
  6357. * does not accidentally come back.
  6358. */
  6359. static void napi_get_frags_check(struct napi_struct *napi)
  6360. {
  6361. struct sk_buff *skb;
  6362. local_bh_disable();
  6363. skb = napi_get_frags(napi);
  6364. WARN_ON_ONCE(skb && skb->head_frag);
  6365. napi_free_frags(napi);
  6366. local_bh_enable();
  6367. }
  6368. void netif_napi_add_weight_locked(struct net_device *dev,
  6369. struct napi_struct *napi,
  6370. int (*poll)(struct napi_struct *, int),
  6371. int weight)
  6372. {
  6373. netdev_assert_locked(dev);
  6374. if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state)))
  6375. return;
  6376. INIT_LIST_HEAD(&napi->poll_list);
  6377. INIT_HLIST_NODE(&napi->napi_hash_node);
  6378. hrtimer_setup(&napi->timer, napi_watchdog, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
  6379. gro_init(&napi->gro);
  6380. napi->skb = NULL;
  6381. napi->poll = poll;
  6382. if (weight > NAPI_POLL_WEIGHT)
  6383. netdev_err_once(dev, "%s() called with weight %d\n", __func__,
  6384. weight);
  6385. napi->weight = weight;
  6386. napi->dev = dev;
  6387. #ifdef CONFIG_NETPOLL
  6388. napi->poll_owner = -1;
  6389. #endif
  6390. napi->list_owner = -1;
  6391. set_bit(NAPI_STATE_SCHED, &napi->state);
  6392. set_bit(NAPI_STATE_NPSVC, &napi->state);
  6393. netif_napi_dev_list_add(dev, napi);
  6394. /* default settings from sysfs are applied to all NAPIs. any per-NAPI
  6395. * configuration will be loaded in napi_enable
  6396. */
  6397. napi_set_defer_hard_irqs(napi, READ_ONCE(dev->napi_defer_hard_irqs));
  6398. napi_set_gro_flush_timeout(napi, READ_ONCE(dev->gro_flush_timeout));
  6399. napi_get_frags_check(napi);
  6400. /* Create kthread for this napi if dev->threaded is set.
  6401. * Clear dev->threaded if kthread creation failed so that
  6402. * threaded mode will not be enabled in napi_enable().
  6403. */
  6404. if (napi_get_threaded_config(dev, napi))
  6405. if (napi_kthread_create(napi))
  6406. dev->threaded = NETDEV_NAPI_THREADED_DISABLED;
  6407. netif_napi_set_irq_locked(napi, -1);
  6408. }
  6409. EXPORT_SYMBOL(netif_napi_add_weight_locked);
  6410. void napi_disable_locked(struct napi_struct *n)
  6411. {
  6412. unsigned long val, new;
  6413. might_sleep();
  6414. netdev_assert_locked(n->dev);
  6415. set_bit(NAPI_STATE_DISABLE, &n->state);
  6416. val = READ_ONCE(n->state);
  6417. do {
  6418. while (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) {
  6419. usleep_range(20, 200);
  6420. val = READ_ONCE(n->state);
  6421. }
  6422. new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC;
  6423. new &= ~(NAPIF_STATE_THREADED |
  6424. NAPIF_STATE_THREADED_BUSY_POLL |
  6425. NAPIF_STATE_PREFER_BUSY_POLL);
  6426. } while (!try_cmpxchg(&n->state, &val, new));
  6427. hrtimer_cancel(&n->timer);
  6428. if (n->config)
  6429. napi_save_config(n);
  6430. else
  6431. napi_hash_del(n);
  6432. clear_bit(NAPI_STATE_DISABLE, &n->state);
  6433. }
  6434. EXPORT_SYMBOL(napi_disable_locked);
  6435. /**
  6436. * napi_disable() - prevent NAPI from scheduling
  6437. * @n: NAPI context
  6438. *
  6439. * Stop NAPI from being scheduled on this context.
  6440. * Waits till any outstanding processing completes.
  6441. * Takes netdev_lock() for associated net_device.
  6442. */
  6443. void napi_disable(struct napi_struct *n)
  6444. {
  6445. netdev_lock(n->dev);
  6446. napi_disable_locked(n);
  6447. netdev_unlock(n->dev);
  6448. }
  6449. EXPORT_SYMBOL(napi_disable);
  6450. void napi_enable_locked(struct napi_struct *n)
  6451. {
  6452. unsigned long new, val = READ_ONCE(n->state);
  6453. if (n->config)
  6454. napi_restore_config(n);
  6455. else
  6456. napi_hash_add(n);
  6457. do {
  6458. BUG_ON(!test_bit(NAPI_STATE_SCHED, &val));
  6459. new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC);
  6460. if (n->dev->threaded && n->thread)
  6461. new |= NAPIF_STATE_THREADED;
  6462. } while (!try_cmpxchg(&n->state, &val, new));
  6463. }
  6464. EXPORT_SYMBOL(napi_enable_locked);
  6465. /**
  6466. * napi_enable() - enable NAPI scheduling
  6467. * @n: NAPI context
  6468. *
  6469. * Enable scheduling of a NAPI instance.
  6470. * Must be paired with napi_disable().
  6471. * Takes netdev_lock() for associated net_device.
  6472. */
  6473. void napi_enable(struct napi_struct *n)
  6474. {
  6475. netdev_lock(n->dev);
  6476. napi_enable_locked(n);
  6477. netdev_unlock(n->dev);
  6478. }
  6479. EXPORT_SYMBOL(napi_enable);
  6480. /* Must be called in process context */
  6481. void __netif_napi_del_locked(struct napi_struct *napi)
  6482. {
  6483. netdev_assert_locked(napi->dev);
  6484. if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state))
  6485. return;
  6486. /* Make sure NAPI is disabled (or was never enabled). */
  6487. WARN_ON(!test_bit(NAPI_STATE_SCHED, &napi->state));
  6488. if (test_and_clear_bit(NAPI_STATE_HAS_NOTIFIER, &napi->state))
  6489. irq_set_affinity_notifier(napi->irq, NULL);
  6490. if (napi->config) {
  6491. napi->index = -1;
  6492. napi->config = NULL;
  6493. }
  6494. list_del_rcu(&napi->dev_list);
  6495. napi_free_frags(napi);
  6496. gro_cleanup(&napi->gro);
  6497. if (napi->thread) {
  6498. kthread_stop(napi->thread);
  6499. napi->thread = NULL;
  6500. }
  6501. }
  6502. EXPORT_SYMBOL(__netif_napi_del_locked);
  6503. static int __napi_poll(struct napi_struct *n, bool *repoll)
  6504. {
  6505. int work, weight;
  6506. weight = n->weight;
  6507. /* This NAPI_STATE_SCHED test is for avoiding a race
  6508. * with netpoll's poll_napi(). Only the entity which
  6509. * obtains the lock and sees NAPI_STATE_SCHED set will
  6510. * actually make the ->poll() call. Therefore we avoid
  6511. * accidentally calling ->poll() when NAPI is not scheduled.
  6512. */
  6513. work = 0;
  6514. if (napi_is_scheduled(n)) {
  6515. work = n->poll(n, weight);
  6516. trace_napi_poll(n, work, weight);
  6517. xdp_do_check_flushed(n);
  6518. }
  6519. if (unlikely(work > weight))
  6520. netdev_err_once(n->dev, "NAPI poll function %pS returned %d, exceeding its budget of %d.\n",
  6521. n->poll, work, weight);
  6522. if (likely(work < weight))
  6523. return work;
  6524. /* Drivers must not modify the NAPI state if they
  6525. * consume the entire weight. In such cases this code
  6526. * still "owns" the NAPI instance and therefore can
  6527. * move the instance around on the list at-will.
  6528. */
  6529. if (unlikely(napi_disable_pending(n))) {
  6530. napi_complete(n);
  6531. return work;
  6532. }
  6533. /* The NAPI context has more processing work, but busy-polling
  6534. * is preferred. Exit early.
  6535. */
  6536. if (napi_prefer_busy_poll(n)) {
  6537. if (napi_complete_done(n, work)) {
  6538. /* If timeout is not set, we need to make sure
  6539. * that the NAPI is re-scheduled.
  6540. */
  6541. napi_schedule(n);
  6542. }
  6543. return work;
  6544. }
  6545. /* Flush too old packets. If HZ < 1000, flush all packets */
  6546. gro_flush_normal(&n->gro, HZ >= 1000);
  6547. /* Some drivers may have called napi_schedule
  6548. * prior to exhausting their budget.
  6549. */
  6550. if (unlikely(!list_empty(&n->poll_list))) {
  6551. pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
  6552. n->dev ? n->dev->name : "backlog");
  6553. return work;
  6554. }
  6555. *repoll = true;
  6556. return work;
  6557. }
  6558. static int napi_poll(struct napi_struct *n, struct list_head *repoll)
  6559. {
  6560. bool do_repoll = false;
  6561. void *have;
  6562. int work;
  6563. list_del_init(&n->poll_list);
  6564. have = netpoll_poll_lock(n);
  6565. work = __napi_poll(n, &do_repoll);
  6566. if (do_repoll) {
  6567. #if defined(CONFIG_DEBUG_NET)
  6568. if (unlikely(!napi_is_scheduled(n)))
  6569. pr_crit("repoll requested for device %s %ps but napi is not scheduled.\n",
  6570. n->dev->name, n->poll);
  6571. #endif
  6572. list_add_tail(&n->poll_list, repoll);
  6573. }
  6574. netpoll_poll_unlock(have);
  6575. return work;
  6576. }
  6577. static int napi_thread_wait(struct napi_struct *napi)
  6578. {
  6579. set_current_state(TASK_INTERRUPTIBLE);
  6580. while (!kthread_should_stop()) {
  6581. /* Testing SCHED_THREADED bit here to make sure the current
  6582. * kthread owns this napi and could poll on this napi.
  6583. * Testing SCHED bit is not enough because SCHED bit might be
  6584. * set by some other busy poll thread or by napi_disable().
  6585. */
  6586. if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state)) {
  6587. WARN_ON(!list_empty(&napi->poll_list));
  6588. __set_current_state(TASK_RUNNING);
  6589. return 0;
  6590. }
  6591. schedule();
  6592. set_current_state(TASK_INTERRUPTIBLE);
  6593. }
  6594. __set_current_state(TASK_RUNNING);
  6595. return -1;
  6596. }
  6597. static void napi_threaded_poll_loop(struct napi_struct *napi,
  6598. unsigned long *busy_poll_last_qs)
  6599. {
  6600. unsigned long last_qs = busy_poll_last_qs ? *busy_poll_last_qs : jiffies;
  6601. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  6602. struct softnet_data *sd;
  6603. for (;;) {
  6604. bool repoll = false;
  6605. void *have;
  6606. local_bh_disable();
  6607. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  6608. sd = this_cpu_ptr(&softnet_data);
  6609. sd->in_napi_threaded_poll = true;
  6610. have = netpoll_poll_lock(napi);
  6611. __napi_poll(napi, &repoll);
  6612. netpoll_poll_unlock(have);
  6613. sd->in_napi_threaded_poll = false;
  6614. barrier();
  6615. if (sd_has_rps_ipi_waiting(sd)) {
  6616. local_irq_disable();
  6617. net_rps_action_and_irq_enable(sd);
  6618. }
  6619. skb_defer_free_flush();
  6620. bpf_net_ctx_clear(bpf_net_ctx);
  6621. /* When busy poll is enabled, the old packets are not flushed in
  6622. * napi_complete_done. So flush them here.
  6623. */
  6624. if (busy_poll_last_qs)
  6625. gro_flush_normal(&napi->gro, HZ >= 1000);
  6626. local_bh_enable();
  6627. /* Call cond_resched here to avoid watchdog warnings. */
  6628. if (repoll || busy_poll_last_qs) {
  6629. rcu_softirq_qs_periodic(last_qs);
  6630. cond_resched();
  6631. }
  6632. if (!repoll)
  6633. break;
  6634. }
  6635. if (busy_poll_last_qs)
  6636. *busy_poll_last_qs = last_qs;
  6637. }
  6638. static int napi_threaded_poll(void *data)
  6639. {
  6640. struct napi_struct *napi = data;
  6641. unsigned long last_qs = jiffies;
  6642. bool want_busy_poll;
  6643. bool in_busy_poll;
  6644. unsigned long val;
  6645. while (!napi_thread_wait(napi)) {
  6646. val = READ_ONCE(napi->state);
  6647. want_busy_poll = val & NAPIF_STATE_THREADED_BUSY_POLL;
  6648. in_busy_poll = val & NAPIF_STATE_IN_BUSY_POLL;
  6649. if (unlikely(val & NAPIF_STATE_DISABLE))
  6650. want_busy_poll = false;
  6651. if (want_busy_poll != in_busy_poll)
  6652. assign_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state,
  6653. want_busy_poll);
  6654. napi_threaded_poll_loop(napi, want_busy_poll ? &last_qs : NULL);
  6655. }
  6656. return 0;
  6657. }
  6658. static __latent_entropy void net_rx_action(void)
  6659. {
  6660. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  6661. unsigned long time_limit = jiffies +
  6662. usecs_to_jiffies(READ_ONCE(net_hotdata.netdev_budget_usecs));
  6663. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  6664. int budget = READ_ONCE(net_hotdata.netdev_budget);
  6665. LIST_HEAD(list);
  6666. LIST_HEAD(repoll);
  6667. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  6668. start:
  6669. sd->in_net_rx_action = true;
  6670. local_irq_disable();
  6671. list_splice_init(&sd->poll_list, &list);
  6672. local_irq_enable();
  6673. for (;;) {
  6674. struct napi_struct *n;
  6675. skb_defer_free_flush();
  6676. if (list_empty(&list)) {
  6677. if (list_empty(&repoll)) {
  6678. sd->in_net_rx_action = false;
  6679. barrier();
  6680. /* We need to check if ____napi_schedule()
  6681. * had refilled poll_list while
  6682. * sd->in_net_rx_action was true.
  6683. */
  6684. if (!list_empty(&sd->poll_list))
  6685. goto start;
  6686. if (!sd_has_rps_ipi_waiting(sd))
  6687. goto end;
  6688. }
  6689. break;
  6690. }
  6691. n = list_first_entry(&list, struct napi_struct, poll_list);
  6692. budget -= napi_poll(n, &repoll);
  6693. /* If softirq window is exhausted then punt.
  6694. * Allow this to run for 2 jiffies since which will allow
  6695. * an average latency of 1.5/HZ.
  6696. */
  6697. if (unlikely(budget <= 0 ||
  6698. time_after_eq(jiffies, time_limit))) {
  6699. /* Pairs with READ_ONCE() in softnet_seq_show() */
  6700. WRITE_ONCE(sd->time_squeeze, sd->time_squeeze + 1);
  6701. break;
  6702. }
  6703. }
  6704. local_irq_disable();
  6705. list_splice_tail_init(&sd->poll_list, &list);
  6706. list_splice_tail(&repoll, &list);
  6707. list_splice(&list, &sd->poll_list);
  6708. if (!list_empty(&sd->poll_list))
  6709. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  6710. else
  6711. sd->in_net_rx_action = false;
  6712. net_rps_action_and_irq_enable(sd);
  6713. end:
  6714. bpf_net_ctx_clear(bpf_net_ctx);
  6715. }
  6716. struct netdev_adjacent {
  6717. struct net_device *dev;
  6718. netdevice_tracker dev_tracker;
  6719. /* upper master flag, there can only be one master device per list */
  6720. bool master;
  6721. /* lookup ignore flag */
  6722. bool ignore;
  6723. /* counter for the number of times this device was added to us */
  6724. u16 ref_nr;
  6725. /* private field for the users */
  6726. void *private;
  6727. struct list_head list;
  6728. struct rcu_head rcu;
  6729. };
  6730. static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
  6731. struct list_head *adj_list)
  6732. {
  6733. struct netdev_adjacent *adj;
  6734. list_for_each_entry(adj, adj_list, list) {
  6735. if (adj->dev == adj_dev)
  6736. return adj;
  6737. }
  6738. return NULL;
  6739. }
  6740. static int ____netdev_has_upper_dev(struct net_device *upper_dev,
  6741. struct netdev_nested_priv *priv)
  6742. {
  6743. struct net_device *dev = (struct net_device *)priv->data;
  6744. return upper_dev == dev;
  6745. }
  6746. /**
  6747. * netdev_has_upper_dev - Check if device is linked to an upper device
  6748. * @dev: device
  6749. * @upper_dev: upper device to check
  6750. *
  6751. * Find out if a device is linked to specified upper device and return true
  6752. * in case it is. Note that this checks only immediate upper device,
  6753. * not through a complete stack of devices. The caller must hold the RTNL lock.
  6754. */
  6755. bool netdev_has_upper_dev(struct net_device *dev,
  6756. struct net_device *upper_dev)
  6757. {
  6758. struct netdev_nested_priv priv = {
  6759. .data = (void *)upper_dev,
  6760. };
  6761. ASSERT_RTNL();
  6762. return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
  6763. &priv);
  6764. }
  6765. EXPORT_SYMBOL(netdev_has_upper_dev);
  6766. /**
  6767. * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device
  6768. * @dev: device
  6769. * @upper_dev: upper device to check
  6770. *
  6771. * Find out if a device is linked to specified upper device and return true
  6772. * in case it is. Note that this checks the entire upper device chain.
  6773. * The caller must hold rcu lock.
  6774. */
  6775. bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
  6776. struct net_device *upper_dev)
  6777. {
  6778. struct netdev_nested_priv priv = {
  6779. .data = (void *)upper_dev,
  6780. };
  6781. return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
  6782. &priv);
  6783. }
  6784. EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
  6785. /**
  6786. * netdev_has_any_upper_dev - Check if device is linked to some device
  6787. * @dev: device
  6788. *
  6789. * Find out if a device is linked to an upper device and return true in case
  6790. * it is. The caller must hold the RTNL lock.
  6791. */
  6792. bool netdev_has_any_upper_dev(struct net_device *dev)
  6793. {
  6794. ASSERT_RTNL();
  6795. return !list_empty(&dev->adj_list.upper);
  6796. }
  6797. EXPORT_SYMBOL(netdev_has_any_upper_dev);
  6798. /**
  6799. * netdev_master_upper_dev_get - Get master upper device
  6800. * @dev: device
  6801. *
  6802. * Find a master upper device and return pointer to it or NULL in case
  6803. * it's not there. The caller must hold the RTNL lock.
  6804. */
  6805. struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
  6806. {
  6807. struct netdev_adjacent *upper;
  6808. ASSERT_RTNL();
  6809. if (list_empty(&dev->adj_list.upper))
  6810. return NULL;
  6811. upper = list_first_entry(&dev->adj_list.upper,
  6812. struct netdev_adjacent, list);
  6813. if (likely(upper->master))
  6814. return upper->dev;
  6815. return NULL;
  6816. }
  6817. EXPORT_SYMBOL(netdev_master_upper_dev_get);
  6818. static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
  6819. {
  6820. struct netdev_adjacent *upper;
  6821. ASSERT_RTNL();
  6822. if (list_empty(&dev->adj_list.upper))
  6823. return NULL;
  6824. upper = list_first_entry(&dev->adj_list.upper,
  6825. struct netdev_adjacent, list);
  6826. if (likely(upper->master) && !upper->ignore)
  6827. return upper->dev;
  6828. return NULL;
  6829. }
  6830. /**
  6831. * netdev_has_any_lower_dev - Check if device is linked to some device
  6832. * @dev: device
  6833. *
  6834. * Find out if a device is linked to a lower device and return true in case
  6835. * it is. The caller must hold the RTNL lock.
  6836. */
  6837. static bool netdev_has_any_lower_dev(struct net_device *dev)
  6838. {
  6839. ASSERT_RTNL();
  6840. return !list_empty(&dev->adj_list.lower);
  6841. }
  6842. void *netdev_adjacent_get_private(struct list_head *adj_list)
  6843. {
  6844. struct netdev_adjacent *adj;
  6845. adj = list_entry(adj_list, struct netdev_adjacent, list);
  6846. return adj->private;
  6847. }
  6848. EXPORT_SYMBOL(netdev_adjacent_get_private);
  6849. /**
  6850. * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
  6851. * @dev: device
  6852. * @iter: list_head ** of the current position
  6853. *
  6854. * Gets the next device from the dev's upper list, starting from iter
  6855. * position. The caller must hold RCU read lock.
  6856. */
  6857. struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
  6858. struct list_head **iter)
  6859. {
  6860. struct netdev_adjacent *upper;
  6861. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
  6862. upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  6863. if (&upper->list == &dev->adj_list.upper)
  6864. return NULL;
  6865. *iter = &upper->list;
  6866. return upper->dev;
  6867. }
  6868. EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
  6869. static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
  6870. struct list_head **iter,
  6871. bool *ignore)
  6872. {
  6873. struct netdev_adjacent *upper;
  6874. upper = list_entry((*iter)->next, struct netdev_adjacent, list);
  6875. if (&upper->list == &dev->adj_list.upper)
  6876. return NULL;
  6877. *iter = &upper->list;
  6878. *ignore = upper->ignore;
  6879. return upper->dev;
  6880. }
  6881. static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
  6882. struct list_head **iter)
  6883. {
  6884. struct netdev_adjacent *upper;
  6885. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
  6886. upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  6887. if (&upper->list == &dev->adj_list.upper)
  6888. return NULL;
  6889. *iter = &upper->list;
  6890. return upper->dev;
  6891. }
  6892. static int __netdev_walk_all_upper_dev(struct net_device *dev,
  6893. int (*fn)(struct net_device *dev,
  6894. struct netdev_nested_priv *priv),
  6895. struct netdev_nested_priv *priv)
  6896. {
  6897. struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  6898. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  6899. int ret, cur = 0;
  6900. bool ignore;
  6901. now = dev;
  6902. iter = &dev->adj_list.upper;
  6903. while (1) {
  6904. if (now != dev) {
  6905. ret = fn(now, priv);
  6906. if (ret)
  6907. return ret;
  6908. }
  6909. next = NULL;
  6910. while (1) {
  6911. udev = __netdev_next_upper_dev(now, &iter, &ignore);
  6912. if (!udev)
  6913. break;
  6914. if (ignore)
  6915. continue;
  6916. next = udev;
  6917. niter = &udev->adj_list.upper;
  6918. dev_stack[cur] = now;
  6919. iter_stack[cur++] = iter;
  6920. break;
  6921. }
  6922. if (!next) {
  6923. if (!cur)
  6924. return 0;
  6925. next = dev_stack[--cur];
  6926. niter = iter_stack[cur];
  6927. }
  6928. now = next;
  6929. iter = niter;
  6930. }
  6931. return 0;
  6932. }
  6933. int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
  6934. int (*fn)(struct net_device *dev,
  6935. struct netdev_nested_priv *priv),
  6936. struct netdev_nested_priv *priv)
  6937. {
  6938. struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  6939. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  6940. int ret, cur = 0;
  6941. now = dev;
  6942. iter = &dev->adj_list.upper;
  6943. while (1) {
  6944. if (now != dev) {
  6945. ret = fn(now, priv);
  6946. if (ret)
  6947. return ret;
  6948. }
  6949. next = NULL;
  6950. while (1) {
  6951. udev = netdev_next_upper_dev_rcu(now, &iter);
  6952. if (!udev)
  6953. break;
  6954. next = udev;
  6955. niter = &udev->adj_list.upper;
  6956. dev_stack[cur] = now;
  6957. iter_stack[cur++] = iter;
  6958. break;
  6959. }
  6960. if (!next) {
  6961. if (!cur)
  6962. return 0;
  6963. next = dev_stack[--cur];
  6964. niter = iter_stack[cur];
  6965. }
  6966. now = next;
  6967. iter = niter;
  6968. }
  6969. return 0;
  6970. }
  6971. EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
  6972. static bool __netdev_has_upper_dev(struct net_device *dev,
  6973. struct net_device *upper_dev)
  6974. {
  6975. struct netdev_nested_priv priv = {
  6976. .flags = 0,
  6977. .data = (void *)upper_dev,
  6978. };
  6979. ASSERT_RTNL();
  6980. return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
  6981. &priv);
  6982. }
  6983. /**
  6984. * netdev_lower_get_next_private - Get the next ->private from the
  6985. * lower neighbour list
  6986. * @dev: device
  6987. * @iter: list_head ** of the current position
  6988. *
  6989. * Gets the next netdev_adjacent->private from the dev's lower neighbour
  6990. * list, starting from iter position. The caller must hold either hold the
  6991. * RTNL lock or its own locking that guarantees that the neighbour lower
  6992. * list will remain unchanged.
  6993. */
  6994. void *netdev_lower_get_next_private(struct net_device *dev,
  6995. struct list_head **iter)
  6996. {
  6997. struct netdev_adjacent *lower;
  6998. lower = list_entry(*iter, struct netdev_adjacent, list);
  6999. if (&lower->list == &dev->adj_list.lower)
  7000. return NULL;
  7001. *iter = lower->list.next;
  7002. return lower->private;
  7003. }
  7004. EXPORT_SYMBOL(netdev_lower_get_next_private);
  7005. /**
  7006. * netdev_lower_get_next_private_rcu - Get the next ->private from the
  7007. * lower neighbour list, RCU
  7008. * variant
  7009. * @dev: device
  7010. * @iter: list_head ** of the current position
  7011. *
  7012. * Gets the next netdev_adjacent->private from the dev's lower neighbour
  7013. * list, starting from iter position. The caller must hold RCU read lock.
  7014. */
  7015. void *netdev_lower_get_next_private_rcu(struct net_device *dev,
  7016. struct list_head **iter)
  7017. {
  7018. struct netdev_adjacent *lower;
  7019. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
  7020. lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  7021. if (&lower->list == &dev->adj_list.lower)
  7022. return NULL;
  7023. *iter = &lower->list;
  7024. return lower->private;
  7025. }
  7026. EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
  7027. /**
  7028. * netdev_lower_get_next - Get the next device from the lower neighbour
  7029. * list
  7030. * @dev: device
  7031. * @iter: list_head ** of the current position
  7032. *
  7033. * Gets the next netdev_adjacent from the dev's lower neighbour
  7034. * list, starting from iter position. The caller must hold RTNL lock or
  7035. * its own locking that guarantees that the neighbour lower
  7036. * list will remain unchanged.
  7037. */
  7038. void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
  7039. {
  7040. struct netdev_adjacent *lower;
  7041. lower = list_entry(*iter, struct netdev_adjacent, list);
  7042. if (&lower->list == &dev->adj_list.lower)
  7043. return NULL;
  7044. *iter = lower->list.next;
  7045. return lower->dev;
  7046. }
  7047. EXPORT_SYMBOL(netdev_lower_get_next);
  7048. static struct net_device *netdev_next_lower_dev(struct net_device *dev,
  7049. struct list_head **iter)
  7050. {
  7051. struct netdev_adjacent *lower;
  7052. lower = list_entry((*iter)->next, struct netdev_adjacent, list);
  7053. if (&lower->list == &dev->adj_list.lower)
  7054. return NULL;
  7055. *iter = &lower->list;
  7056. return lower->dev;
  7057. }
  7058. static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
  7059. struct list_head **iter,
  7060. bool *ignore)
  7061. {
  7062. struct netdev_adjacent *lower;
  7063. lower = list_entry((*iter)->next, struct netdev_adjacent, list);
  7064. if (&lower->list == &dev->adj_list.lower)
  7065. return NULL;
  7066. *iter = &lower->list;
  7067. *ignore = lower->ignore;
  7068. return lower->dev;
  7069. }
  7070. int netdev_walk_all_lower_dev(struct net_device *dev,
  7071. int (*fn)(struct net_device *dev,
  7072. struct netdev_nested_priv *priv),
  7073. struct netdev_nested_priv *priv)
  7074. {
  7075. struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  7076. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  7077. int ret, cur = 0;
  7078. now = dev;
  7079. iter = &dev->adj_list.lower;
  7080. while (1) {
  7081. if (now != dev) {
  7082. ret = fn(now, priv);
  7083. if (ret)
  7084. return ret;
  7085. }
  7086. next = NULL;
  7087. while (1) {
  7088. ldev = netdev_next_lower_dev(now, &iter);
  7089. if (!ldev)
  7090. break;
  7091. next = ldev;
  7092. niter = &ldev->adj_list.lower;
  7093. dev_stack[cur] = now;
  7094. iter_stack[cur++] = iter;
  7095. break;
  7096. }
  7097. if (!next) {
  7098. if (!cur)
  7099. return 0;
  7100. next = dev_stack[--cur];
  7101. niter = iter_stack[cur];
  7102. }
  7103. now = next;
  7104. iter = niter;
  7105. }
  7106. return 0;
  7107. }
  7108. EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
  7109. static int __netdev_walk_all_lower_dev(struct net_device *dev,
  7110. int (*fn)(struct net_device *dev,
  7111. struct netdev_nested_priv *priv),
  7112. struct netdev_nested_priv *priv)
  7113. {
  7114. struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  7115. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  7116. int ret, cur = 0;
  7117. bool ignore;
  7118. now = dev;
  7119. iter = &dev->adj_list.lower;
  7120. while (1) {
  7121. if (now != dev) {
  7122. ret = fn(now, priv);
  7123. if (ret)
  7124. return ret;
  7125. }
  7126. next = NULL;
  7127. while (1) {
  7128. ldev = __netdev_next_lower_dev(now, &iter, &ignore);
  7129. if (!ldev)
  7130. break;
  7131. if (ignore)
  7132. continue;
  7133. next = ldev;
  7134. niter = &ldev->adj_list.lower;
  7135. dev_stack[cur] = now;
  7136. iter_stack[cur++] = iter;
  7137. break;
  7138. }
  7139. if (!next) {
  7140. if (!cur)
  7141. return 0;
  7142. next = dev_stack[--cur];
  7143. niter = iter_stack[cur];
  7144. }
  7145. now = next;
  7146. iter = niter;
  7147. }
  7148. return 0;
  7149. }
  7150. struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
  7151. struct list_head **iter)
  7152. {
  7153. struct netdev_adjacent *lower;
  7154. lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  7155. if (&lower->list == &dev->adj_list.lower)
  7156. return NULL;
  7157. *iter = &lower->list;
  7158. return lower->dev;
  7159. }
  7160. EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
  7161. static u8 __netdev_upper_depth(struct net_device *dev)
  7162. {
  7163. struct net_device *udev;
  7164. struct list_head *iter;
  7165. u8 max_depth = 0;
  7166. bool ignore;
  7167. for (iter = &dev->adj_list.upper,
  7168. udev = __netdev_next_upper_dev(dev, &iter, &ignore);
  7169. udev;
  7170. udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
  7171. if (ignore)
  7172. continue;
  7173. if (max_depth < udev->upper_level)
  7174. max_depth = udev->upper_level;
  7175. }
  7176. return max_depth;
  7177. }
  7178. static u8 __netdev_lower_depth(struct net_device *dev)
  7179. {
  7180. struct net_device *ldev;
  7181. struct list_head *iter;
  7182. u8 max_depth = 0;
  7183. bool ignore;
  7184. for (iter = &dev->adj_list.lower,
  7185. ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
  7186. ldev;
  7187. ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
  7188. if (ignore)
  7189. continue;
  7190. if (max_depth < ldev->lower_level)
  7191. max_depth = ldev->lower_level;
  7192. }
  7193. return max_depth;
  7194. }
  7195. static int __netdev_update_upper_level(struct net_device *dev,
  7196. struct netdev_nested_priv *__unused)
  7197. {
  7198. dev->upper_level = __netdev_upper_depth(dev) + 1;
  7199. return 0;
  7200. }
  7201. #ifdef CONFIG_LOCKDEP
  7202. static LIST_HEAD(net_unlink_list);
  7203. static void net_unlink_todo(struct net_device *dev)
  7204. {
  7205. if (list_empty(&dev->unlink_list))
  7206. list_add_tail(&dev->unlink_list, &net_unlink_list);
  7207. }
  7208. #endif
  7209. static int __netdev_update_lower_level(struct net_device *dev,
  7210. struct netdev_nested_priv *priv)
  7211. {
  7212. dev->lower_level = __netdev_lower_depth(dev) + 1;
  7213. #ifdef CONFIG_LOCKDEP
  7214. if (!priv)
  7215. return 0;
  7216. if (priv->flags & NESTED_SYNC_IMM)
  7217. dev->nested_level = dev->lower_level - 1;
  7218. if (priv->flags & NESTED_SYNC_TODO)
  7219. net_unlink_todo(dev);
  7220. #endif
  7221. return 0;
  7222. }
  7223. int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
  7224. int (*fn)(struct net_device *dev,
  7225. struct netdev_nested_priv *priv),
  7226. struct netdev_nested_priv *priv)
  7227. {
  7228. struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  7229. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  7230. int ret, cur = 0;
  7231. now = dev;
  7232. iter = &dev->adj_list.lower;
  7233. while (1) {
  7234. if (now != dev) {
  7235. ret = fn(now, priv);
  7236. if (ret)
  7237. return ret;
  7238. }
  7239. next = NULL;
  7240. while (1) {
  7241. ldev = netdev_next_lower_dev_rcu(now, &iter);
  7242. if (!ldev)
  7243. break;
  7244. next = ldev;
  7245. niter = &ldev->adj_list.lower;
  7246. dev_stack[cur] = now;
  7247. iter_stack[cur++] = iter;
  7248. break;
  7249. }
  7250. if (!next) {
  7251. if (!cur)
  7252. return 0;
  7253. next = dev_stack[--cur];
  7254. niter = iter_stack[cur];
  7255. }
  7256. now = next;
  7257. iter = niter;
  7258. }
  7259. return 0;
  7260. }
  7261. EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
  7262. /**
  7263. * netdev_lower_get_first_private_rcu - Get the first ->private from the
  7264. * lower neighbour list, RCU
  7265. * variant
  7266. * @dev: device
  7267. *
  7268. * Gets the first netdev_adjacent->private from the dev's lower neighbour
  7269. * list. The caller must hold RCU read lock.
  7270. */
  7271. void *netdev_lower_get_first_private_rcu(struct net_device *dev)
  7272. {
  7273. struct netdev_adjacent *lower;
  7274. lower = list_first_or_null_rcu(&dev->adj_list.lower,
  7275. struct netdev_adjacent, list);
  7276. if (lower)
  7277. return lower->private;
  7278. return NULL;
  7279. }
  7280. EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
  7281. /**
  7282. * netdev_master_upper_dev_get_rcu - Get master upper device
  7283. * @dev: device
  7284. *
  7285. * Find a master upper device and return pointer to it or NULL in case
  7286. * it's not there. The caller must hold the RCU read lock.
  7287. */
  7288. struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
  7289. {
  7290. struct netdev_adjacent *upper;
  7291. upper = list_first_or_null_rcu(&dev->adj_list.upper,
  7292. struct netdev_adjacent, list);
  7293. if (upper && likely(upper->master))
  7294. return upper->dev;
  7295. return NULL;
  7296. }
  7297. EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
  7298. static int netdev_adjacent_sysfs_add(struct net_device *dev,
  7299. struct net_device *adj_dev,
  7300. struct list_head *dev_list)
  7301. {
  7302. char linkname[IFNAMSIZ+7];
  7303. sprintf(linkname, dev_list == &dev->adj_list.upper ?
  7304. "upper_%s" : "lower_%s", adj_dev->name);
  7305. return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
  7306. linkname);
  7307. }
  7308. static void netdev_adjacent_sysfs_del(struct net_device *dev,
  7309. char *name,
  7310. struct list_head *dev_list)
  7311. {
  7312. char linkname[IFNAMSIZ+7];
  7313. sprintf(linkname, dev_list == &dev->adj_list.upper ?
  7314. "upper_%s" : "lower_%s", name);
  7315. sysfs_remove_link(&(dev->dev.kobj), linkname);
  7316. }
  7317. static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
  7318. struct net_device *adj_dev,
  7319. struct list_head *dev_list)
  7320. {
  7321. return (dev_list == &dev->adj_list.upper ||
  7322. dev_list == &dev->adj_list.lower) &&
  7323. net_eq(dev_net(dev), dev_net(adj_dev));
  7324. }
  7325. static int __netdev_adjacent_dev_insert(struct net_device *dev,
  7326. struct net_device *adj_dev,
  7327. struct list_head *dev_list,
  7328. void *private, bool master)
  7329. {
  7330. struct netdev_adjacent *adj;
  7331. int ret;
  7332. adj = __netdev_find_adj(adj_dev, dev_list);
  7333. if (adj) {
  7334. adj->ref_nr += 1;
  7335. pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
  7336. dev->name, adj_dev->name, adj->ref_nr);
  7337. return 0;
  7338. }
  7339. adj = kmalloc_obj(*adj);
  7340. if (!adj)
  7341. return -ENOMEM;
  7342. adj->dev = adj_dev;
  7343. adj->master = master;
  7344. adj->ref_nr = 1;
  7345. adj->private = private;
  7346. adj->ignore = false;
  7347. netdev_hold(adj_dev, &adj->dev_tracker, GFP_KERNEL);
  7348. pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
  7349. dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
  7350. if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
  7351. ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
  7352. if (ret)
  7353. goto free_adj;
  7354. }
  7355. /* Ensure that master link is always the first item in list. */
  7356. if (master) {
  7357. ret = sysfs_create_link(&(dev->dev.kobj),
  7358. &(adj_dev->dev.kobj), "master");
  7359. if (ret)
  7360. goto remove_symlinks;
  7361. list_add_rcu(&adj->list, dev_list);
  7362. } else {
  7363. list_add_tail_rcu(&adj->list, dev_list);
  7364. }
  7365. return 0;
  7366. remove_symlinks:
  7367. if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
  7368. netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
  7369. free_adj:
  7370. netdev_put(adj_dev, &adj->dev_tracker);
  7371. kfree(adj);
  7372. return ret;
  7373. }
  7374. static void __netdev_adjacent_dev_remove(struct net_device *dev,
  7375. struct net_device *adj_dev,
  7376. u16 ref_nr,
  7377. struct list_head *dev_list)
  7378. {
  7379. struct netdev_adjacent *adj;
  7380. pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
  7381. dev->name, adj_dev->name, ref_nr);
  7382. adj = __netdev_find_adj(adj_dev, dev_list);
  7383. if (!adj) {
  7384. pr_err("Adjacency does not exist for device %s from %s\n",
  7385. dev->name, adj_dev->name);
  7386. WARN_ON(1);
  7387. return;
  7388. }
  7389. if (adj->ref_nr > ref_nr) {
  7390. pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
  7391. dev->name, adj_dev->name, ref_nr,
  7392. adj->ref_nr - ref_nr);
  7393. adj->ref_nr -= ref_nr;
  7394. return;
  7395. }
  7396. if (adj->master)
  7397. sysfs_remove_link(&(dev->dev.kobj), "master");
  7398. if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
  7399. netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
  7400. list_del_rcu(&adj->list);
  7401. pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
  7402. adj_dev->name, dev->name, adj_dev->name);
  7403. netdev_put(adj_dev, &adj->dev_tracker);
  7404. kfree_rcu(adj, rcu);
  7405. }
  7406. static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
  7407. struct net_device *upper_dev,
  7408. struct list_head *up_list,
  7409. struct list_head *down_list,
  7410. void *private, bool master)
  7411. {
  7412. int ret;
  7413. ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
  7414. private, master);
  7415. if (ret)
  7416. return ret;
  7417. ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
  7418. private, false);
  7419. if (ret) {
  7420. __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
  7421. return ret;
  7422. }
  7423. return 0;
  7424. }
  7425. static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
  7426. struct net_device *upper_dev,
  7427. u16 ref_nr,
  7428. struct list_head *up_list,
  7429. struct list_head *down_list)
  7430. {
  7431. __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
  7432. __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
  7433. }
  7434. static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
  7435. struct net_device *upper_dev,
  7436. void *private, bool master)
  7437. {
  7438. return __netdev_adjacent_dev_link_lists(dev, upper_dev,
  7439. &dev->adj_list.upper,
  7440. &upper_dev->adj_list.lower,
  7441. private, master);
  7442. }
  7443. static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
  7444. struct net_device *upper_dev)
  7445. {
  7446. __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
  7447. &dev->adj_list.upper,
  7448. &upper_dev->adj_list.lower);
  7449. }
  7450. static int __netdev_upper_dev_link(struct net_device *dev,
  7451. struct net_device *upper_dev, bool master,
  7452. void *upper_priv, void *upper_info,
  7453. struct netdev_nested_priv *priv,
  7454. struct netlink_ext_ack *extack)
  7455. {
  7456. struct netdev_notifier_changeupper_info changeupper_info = {
  7457. .info = {
  7458. .dev = dev,
  7459. .extack = extack,
  7460. },
  7461. .upper_dev = upper_dev,
  7462. .master = master,
  7463. .linking = true,
  7464. .upper_info = upper_info,
  7465. };
  7466. struct net_device *master_dev;
  7467. int ret = 0;
  7468. ASSERT_RTNL();
  7469. if (dev == upper_dev)
  7470. return -EBUSY;
  7471. /* To prevent loops, check if dev is not upper device to upper_dev. */
  7472. if (__netdev_has_upper_dev(upper_dev, dev))
  7473. return -EBUSY;
  7474. if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
  7475. return -EMLINK;
  7476. if (!master) {
  7477. if (__netdev_has_upper_dev(dev, upper_dev))
  7478. return -EEXIST;
  7479. } else {
  7480. master_dev = __netdev_master_upper_dev_get(dev);
  7481. if (master_dev)
  7482. return master_dev == upper_dev ? -EEXIST : -EBUSY;
  7483. }
  7484. ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
  7485. &changeupper_info.info);
  7486. ret = notifier_to_errno(ret);
  7487. if (ret)
  7488. return ret;
  7489. ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
  7490. master);
  7491. if (ret)
  7492. return ret;
  7493. ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
  7494. &changeupper_info.info);
  7495. ret = notifier_to_errno(ret);
  7496. if (ret)
  7497. goto rollback;
  7498. __netdev_update_upper_level(dev, NULL);
  7499. __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
  7500. __netdev_update_lower_level(upper_dev, priv);
  7501. __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
  7502. priv);
  7503. return 0;
  7504. rollback:
  7505. __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
  7506. return ret;
  7507. }
  7508. /**
  7509. * netdev_upper_dev_link - Add a link to the upper device
  7510. * @dev: device
  7511. * @upper_dev: new upper device
  7512. * @extack: netlink extended ack
  7513. *
  7514. * Adds a link to device which is upper to this one. The caller must hold
  7515. * the RTNL lock. On a failure a negative errno code is returned.
  7516. * On success the reference counts are adjusted and the function
  7517. * returns zero.
  7518. */
  7519. int netdev_upper_dev_link(struct net_device *dev,
  7520. struct net_device *upper_dev,
  7521. struct netlink_ext_ack *extack)
  7522. {
  7523. struct netdev_nested_priv priv = {
  7524. .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
  7525. .data = NULL,
  7526. };
  7527. return __netdev_upper_dev_link(dev, upper_dev, false,
  7528. NULL, NULL, &priv, extack);
  7529. }
  7530. EXPORT_SYMBOL(netdev_upper_dev_link);
  7531. /**
  7532. * netdev_master_upper_dev_link - Add a master link to the upper device
  7533. * @dev: device
  7534. * @upper_dev: new upper device
  7535. * @upper_priv: upper device private
  7536. * @upper_info: upper info to be passed down via notifier
  7537. * @extack: netlink extended ack
  7538. *
  7539. * Adds a link to device which is upper to this one. In this case, only
  7540. * one master upper device can be linked, although other non-master devices
  7541. * might be linked as well. The caller must hold the RTNL lock.
  7542. * On a failure a negative errno code is returned. On success the reference
  7543. * counts are adjusted and the function returns zero.
  7544. */
  7545. int netdev_master_upper_dev_link(struct net_device *dev,
  7546. struct net_device *upper_dev,
  7547. void *upper_priv, void *upper_info,
  7548. struct netlink_ext_ack *extack)
  7549. {
  7550. struct netdev_nested_priv priv = {
  7551. .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
  7552. .data = NULL,
  7553. };
  7554. return __netdev_upper_dev_link(dev, upper_dev, true,
  7555. upper_priv, upper_info, &priv, extack);
  7556. }
  7557. EXPORT_SYMBOL(netdev_master_upper_dev_link);
  7558. static void __netdev_upper_dev_unlink(struct net_device *dev,
  7559. struct net_device *upper_dev,
  7560. struct netdev_nested_priv *priv)
  7561. {
  7562. struct netdev_notifier_changeupper_info changeupper_info = {
  7563. .info = {
  7564. .dev = dev,
  7565. },
  7566. .upper_dev = upper_dev,
  7567. .linking = false,
  7568. };
  7569. ASSERT_RTNL();
  7570. changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
  7571. call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
  7572. &changeupper_info.info);
  7573. __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
  7574. call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
  7575. &changeupper_info.info);
  7576. __netdev_update_upper_level(dev, NULL);
  7577. __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
  7578. __netdev_update_lower_level(upper_dev, priv);
  7579. __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
  7580. priv);
  7581. }
  7582. /**
  7583. * netdev_upper_dev_unlink - Removes a link to upper device
  7584. * @dev: device
  7585. * @upper_dev: new upper device
  7586. *
  7587. * Removes a link to device which is upper to this one. The caller must hold
  7588. * the RTNL lock.
  7589. */
  7590. void netdev_upper_dev_unlink(struct net_device *dev,
  7591. struct net_device *upper_dev)
  7592. {
  7593. struct netdev_nested_priv priv = {
  7594. .flags = NESTED_SYNC_TODO,
  7595. .data = NULL,
  7596. };
  7597. __netdev_upper_dev_unlink(dev, upper_dev, &priv);
  7598. }
  7599. EXPORT_SYMBOL(netdev_upper_dev_unlink);
  7600. static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
  7601. struct net_device *lower_dev,
  7602. bool val)
  7603. {
  7604. struct netdev_adjacent *adj;
  7605. adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
  7606. if (adj)
  7607. adj->ignore = val;
  7608. adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
  7609. if (adj)
  7610. adj->ignore = val;
  7611. }
  7612. static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
  7613. struct net_device *lower_dev)
  7614. {
  7615. __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
  7616. }
  7617. static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
  7618. struct net_device *lower_dev)
  7619. {
  7620. __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
  7621. }
  7622. int netdev_adjacent_change_prepare(struct net_device *old_dev,
  7623. struct net_device *new_dev,
  7624. struct net_device *dev,
  7625. struct netlink_ext_ack *extack)
  7626. {
  7627. struct netdev_nested_priv priv = {
  7628. .flags = 0,
  7629. .data = NULL,
  7630. };
  7631. int err;
  7632. if (!new_dev)
  7633. return 0;
  7634. if (old_dev && new_dev != old_dev)
  7635. netdev_adjacent_dev_disable(dev, old_dev);
  7636. err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv,
  7637. extack);
  7638. if (err) {
  7639. if (old_dev && new_dev != old_dev)
  7640. netdev_adjacent_dev_enable(dev, old_dev);
  7641. return err;
  7642. }
  7643. return 0;
  7644. }
  7645. EXPORT_SYMBOL(netdev_adjacent_change_prepare);
  7646. void netdev_adjacent_change_commit(struct net_device *old_dev,
  7647. struct net_device *new_dev,
  7648. struct net_device *dev)
  7649. {
  7650. struct netdev_nested_priv priv = {
  7651. .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
  7652. .data = NULL,
  7653. };
  7654. if (!new_dev || !old_dev)
  7655. return;
  7656. if (new_dev == old_dev)
  7657. return;
  7658. netdev_adjacent_dev_enable(dev, old_dev);
  7659. __netdev_upper_dev_unlink(old_dev, dev, &priv);
  7660. }
  7661. EXPORT_SYMBOL(netdev_adjacent_change_commit);
  7662. void netdev_adjacent_change_abort(struct net_device *old_dev,
  7663. struct net_device *new_dev,
  7664. struct net_device *dev)
  7665. {
  7666. struct netdev_nested_priv priv = {
  7667. .flags = 0,
  7668. .data = NULL,
  7669. };
  7670. if (!new_dev)
  7671. return;
  7672. if (old_dev && new_dev != old_dev)
  7673. netdev_adjacent_dev_enable(dev, old_dev);
  7674. __netdev_upper_dev_unlink(new_dev, dev, &priv);
  7675. }
  7676. EXPORT_SYMBOL(netdev_adjacent_change_abort);
  7677. /**
  7678. * netdev_bonding_info_change - Dispatch event about slave change
  7679. * @dev: device
  7680. * @bonding_info: info to dispatch
  7681. *
  7682. * Send NETDEV_BONDING_INFO to netdev notifiers with info.
  7683. * The caller must hold the RTNL lock.
  7684. */
  7685. void netdev_bonding_info_change(struct net_device *dev,
  7686. struct netdev_bonding_info *bonding_info)
  7687. {
  7688. struct netdev_notifier_bonding_info info = {
  7689. .info.dev = dev,
  7690. };
  7691. memcpy(&info.bonding_info, bonding_info,
  7692. sizeof(struct netdev_bonding_info));
  7693. call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
  7694. &info.info);
  7695. }
  7696. EXPORT_SYMBOL(netdev_bonding_info_change);
  7697. static int netdev_offload_xstats_enable_l3(struct net_device *dev,
  7698. struct netlink_ext_ack *extack)
  7699. {
  7700. struct netdev_notifier_offload_xstats_info info = {
  7701. .info.dev = dev,
  7702. .info.extack = extack,
  7703. .type = NETDEV_OFFLOAD_XSTATS_TYPE_L3,
  7704. };
  7705. int err;
  7706. int rc;
  7707. dev->offload_xstats_l3 = kzalloc_obj(*dev->offload_xstats_l3);
  7708. if (!dev->offload_xstats_l3)
  7709. return -ENOMEM;
  7710. rc = call_netdevice_notifiers_info_robust(NETDEV_OFFLOAD_XSTATS_ENABLE,
  7711. NETDEV_OFFLOAD_XSTATS_DISABLE,
  7712. &info.info);
  7713. err = notifier_to_errno(rc);
  7714. if (err)
  7715. goto free_stats;
  7716. return 0;
  7717. free_stats:
  7718. kfree(dev->offload_xstats_l3);
  7719. dev->offload_xstats_l3 = NULL;
  7720. return err;
  7721. }
  7722. int netdev_offload_xstats_enable(struct net_device *dev,
  7723. enum netdev_offload_xstats_type type,
  7724. struct netlink_ext_ack *extack)
  7725. {
  7726. ASSERT_RTNL();
  7727. if (netdev_offload_xstats_enabled(dev, type))
  7728. return -EALREADY;
  7729. switch (type) {
  7730. case NETDEV_OFFLOAD_XSTATS_TYPE_L3:
  7731. return netdev_offload_xstats_enable_l3(dev, extack);
  7732. }
  7733. WARN_ON(1);
  7734. return -EINVAL;
  7735. }
  7736. EXPORT_SYMBOL(netdev_offload_xstats_enable);
  7737. static void netdev_offload_xstats_disable_l3(struct net_device *dev)
  7738. {
  7739. struct netdev_notifier_offload_xstats_info info = {
  7740. .info.dev = dev,
  7741. .type = NETDEV_OFFLOAD_XSTATS_TYPE_L3,
  7742. };
  7743. call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_DISABLE,
  7744. &info.info);
  7745. kfree(dev->offload_xstats_l3);
  7746. dev->offload_xstats_l3 = NULL;
  7747. }
  7748. int netdev_offload_xstats_disable(struct net_device *dev,
  7749. enum netdev_offload_xstats_type type)
  7750. {
  7751. ASSERT_RTNL();
  7752. if (!netdev_offload_xstats_enabled(dev, type))
  7753. return -EALREADY;
  7754. switch (type) {
  7755. case NETDEV_OFFLOAD_XSTATS_TYPE_L3:
  7756. netdev_offload_xstats_disable_l3(dev);
  7757. return 0;
  7758. }
  7759. WARN_ON(1);
  7760. return -EINVAL;
  7761. }
  7762. EXPORT_SYMBOL(netdev_offload_xstats_disable);
  7763. static void netdev_offload_xstats_disable_all(struct net_device *dev)
  7764. {
  7765. netdev_offload_xstats_disable(dev, NETDEV_OFFLOAD_XSTATS_TYPE_L3);
  7766. }
  7767. static struct rtnl_hw_stats64 *
  7768. netdev_offload_xstats_get_ptr(const struct net_device *dev,
  7769. enum netdev_offload_xstats_type type)
  7770. {
  7771. switch (type) {
  7772. case NETDEV_OFFLOAD_XSTATS_TYPE_L3:
  7773. return dev->offload_xstats_l3;
  7774. }
  7775. WARN_ON(1);
  7776. return NULL;
  7777. }
  7778. bool netdev_offload_xstats_enabled(const struct net_device *dev,
  7779. enum netdev_offload_xstats_type type)
  7780. {
  7781. ASSERT_RTNL();
  7782. return netdev_offload_xstats_get_ptr(dev, type);
  7783. }
  7784. EXPORT_SYMBOL(netdev_offload_xstats_enabled);
  7785. struct netdev_notifier_offload_xstats_ru {
  7786. bool used;
  7787. };
  7788. struct netdev_notifier_offload_xstats_rd {
  7789. struct rtnl_hw_stats64 stats;
  7790. bool used;
  7791. };
  7792. static void netdev_hw_stats64_add(struct rtnl_hw_stats64 *dest,
  7793. const struct rtnl_hw_stats64 *src)
  7794. {
  7795. dest->rx_packets += src->rx_packets;
  7796. dest->tx_packets += src->tx_packets;
  7797. dest->rx_bytes += src->rx_bytes;
  7798. dest->tx_bytes += src->tx_bytes;
  7799. dest->rx_errors += src->rx_errors;
  7800. dest->tx_errors += src->tx_errors;
  7801. dest->rx_dropped += src->rx_dropped;
  7802. dest->tx_dropped += src->tx_dropped;
  7803. dest->multicast += src->multicast;
  7804. }
  7805. static int netdev_offload_xstats_get_used(struct net_device *dev,
  7806. enum netdev_offload_xstats_type type,
  7807. bool *p_used,
  7808. struct netlink_ext_ack *extack)
  7809. {
  7810. struct netdev_notifier_offload_xstats_ru report_used = {};
  7811. struct netdev_notifier_offload_xstats_info info = {
  7812. .info.dev = dev,
  7813. .info.extack = extack,
  7814. .type = type,
  7815. .report_used = &report_used,
  7816. };
  7817. int rc;
  7818. WARN_ON(!netdev_offload_xstats_enabled(dev, type));
  7819. rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_USED,
  7820. &info.info);
  7821. *p_used = report_used.used;
  7822. return notifier_to_errno(rc);
  7823. }
  7824. static int netdev_offload_xstats_get_stats(struct net_device *dev,
  7825. enum netdev_offload_xstats_type type,
  7826. struct rtnl_hw_stats64 *p_stats,
  7827. bool *p_used,
  7828. struct netlink_ext_ack *extack)
  7829. {
  7830. struct netdev_notifier_offload_xstats_rd report_delta = {};
  7831. struct netdev_notifier_offload_xstats_info info = {
  7832. .info.dev = dev,
  7833. .info.extack = extack,
  7834. .type = type,
  7835. .report_delta = &report_delta,
  7836. };
  7837. struct rtnl_hw_stats64 *stats;
  7838. int rc;
  7839. stats = netdev_offload_xstats_get_ptr(dev, type);
  7840. if (WARN_ON(!stats))
  7841. return -EINVAL;
  7842. rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_DELTA,
  7843. &info.info);
  7844. /* Cache whatever we got, even if there was an error, otherwise the
  7845. * successful stats retrievals would get lost.
  7846. */
  7847. netdev_hw_stats64_add(stats, &report_delta.stats);
  7848. if (p_stats)
  7849. *p_stats = *stats;
  7850. *p_used = report_delta.used;
  7851. return notifier_to_errno(rc);
  7852. }
  7853. int netdev_offload_xstats_get(struct net_device *dev,
  7854. enum netdev_offload_xstats_type type,
  7855. struct rtnl_hw_stats64 *p_stats, bool *p_used,
  7856. struct netlink_ext_ack *extack)
  7857. {
  7858. ASSERT_RTNL();
  7859. if (p_stats)
  7860. return netdev_offload_xstats_get_stats(dev, type, p_stats,
  7861. p_used, extack);
  7862. else
  7863. return netdev_offload_xstats_get_used(dev, type, p_used,
  7864. extack);
  7865. }
  7866. EXPORT_SYMBOL(netdev_offload_xstats_get);
  7867. void
  7868. netdev_offload_xstats_report_delta(struct netdev_notifier_offload_xstats_rd *report_delta,
  7869. const struct rtnl_hw_stats64 *stats)
  7870. {
  7871. report_delta->used = true;
  7872. netdev_hw_stats64_add(&report_delta->stats, stats);
  7873. }
  7874. EXPORT_SYMBOL(netdev_offload_xstats_report_delta);
  7875. void
  7876. netdev_offload_xstats_report_used(struct netdev_notifier_offload_xstats_ru *report_used)
  7877. {
  7878. report_used->used = true;
  7879. }
  7880. EXPORT_SYMBOL(netdev_offload_xstats_report_used);
  7881. void netdev_offload_xstats_push_delta(struct net_device *dev,
  7882. enum netdev_offload_xstats_type type,
  7883. const struct rtnl_hw_stats64 *p_stats)
  7884. {
  7885. struct rtnl_hw_stats64 *stats;
  7886. ASSERT_RTNL();
  7887. stats = netdev_offload_xstats_get_ptr(dev, type);
  7888. if (WARN_ON(!stats))
  7889. return;
  7890. netdev_hw_stats64_add(stats, p_stats);
  7891. }
  7892. EXPORT_SYMBOL(netdev_offload_xstats_push_delta);
  7893. /**
  7894. * netdev_get_xmit_slave - Get the xmit slave of master device
  7895. * @dev: device
  7896. * @skb: The packet
  7897. * @all_slaves: assume all the slaves are active
  7898. *
  7899. * The reference counters are not incremented so the caller must be
  7900. * careful with locks. The caller must hold RCU lock.
  7901. * %NULL is returned if no slave is found.
  7902. */
  7903. struct net_device *netdev_get_xmit_slave(struct net_device *dev,
  7904. struct sk_buff *skb,
  7905. bool all_slaves)
  7906. {
  7907. const struct net_device_ops *ops = dev->netdev_ops;
  7908. if (!ops->ndo_get_xmit_slave)
  7909. return NULL;
  7910. return ops->ndo_get_xmit_slave(dev, skb, all_slaves);
  7911. }
  7912. EXPORT_SYMBOL(netdev_get_xmit_slave);
  7913. static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev,
  7914. struct sock *sk)
  7915. {
  7916. const struct net_device_ops *ops = dev->netdev_ops;
  7917. if (!ops->ndo_sk_get_lower_dev)
  7918. return NULL;
  7919. return ops->ndo_sk_get_lower_dev(dev, sk);
  7920. }
  7921. /**
  7922. * netdev_sk_get_lowest_dev - Get the lowest device in chain given device and socket
  7923. * @dev: device
  7924. * @sk: the socket
  7925. *
  7926. * %NULL is returned if no lower device is found.
  7927. */
  7928. struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev,
  7929. struct sock *sk)
  7930. {
  7931. struct net_device *lower;
  7932. lower = netdev_sk_get_lower_dev(dev, sk);
  7933. while (lower) {
  7934. dev = lower;
  7935. lower = netdev_sk_get_lower_dev(dev, sk);
  7936. }
  7937. return dev;
  7938. }
  7939. EXPORT_SYMBOL(netdev_sk_get_lowest_dev);
  7940. static void netdev_adjacent_add_links(struct net_device *dev)
  7941. {
  7942. struct netdev_adjacent *iter;
  7943. struct net *net = dev_net(dev);
  7944. list_for_each_entry(iter, &dev->adj_list.upper, list) {
  7945. if (!net_eq(net, dev_net(iter->dev)))
  7946. continue;
  7947. netdev_adjacent_sysfs_add(iter->dev, dev,
  7948. &iter->dev->adj_list.lower);
  7949. netdev_adjacent_sysfs_add(dev, iter->dev,
  7950. &dev->adj_list.upper);
  7951. }
  7952. list_for_each_entry(iter, &dev->adj_list.lower, list) {
  7953. if (!net_eq(net, dev_net(iter->dev)))
  7954. continue;
  7955. netdev_adjacent_sysfs_add(iter->dev, dev,
  7956. &iter->dev->adj_list.upper);
  7957. netdev_adjacent_sysfs_add(dev, iter->dev,
  7958. &dev->adj_list.lower);
  7959. }
  7960. }
  7961. static void netdev_adjacent_del_links(struct net_device *dev)
  7962. {
  7963. struct netdev_adjacent *iter;
  7964. struct net *net = dev_net(dev);
  7965. list_for_each_entry(iter, &dev->adj_list.upper, list) {
  7966. if (!net_eq(net, dev_net(iter->dev)))
  7967. continue;
  7968. netdev_adjacent_sysfs_del(iter->dev, dev->name,
  7969. &iter->dev->adj_list.lower);
  7970. netdev_adjacent_sysfs_del(dev, iter->dev->name,
  7971. &dev->adj_list.upper);
  7972. }
  7973. list_for_each_entry(iter, &dev->adj_list.lower, list) {
  7974. if (!net_eq(net, dev_net(iter->dev)))
  7975. continue;
  7976. netdev_adjacent_sysfs_del(iter->dev, dev->name,
  7977. &iter->dev->adj_list.upper);
  7978. netdev_adjacent_sysfs_del(dev, iter->dev->name,
  7979. &dev->adj_list.lower);
  7980. }
  7981. }
  7982. void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
  7983. {
  7984. struct netdev_adjacent *iter;
  7985. struct net *net = dev_net(dev);
  7986. list_for_each_entry(iter, &dev->adj_list.upper, list) {
  7987. if (!net_eq(net, dev_net(iter->dev)))
  7988. continue;
  7989. netdev_adjacent_sysfs_del(iter->dev, oldname,
  7990. &iter->dev->adj_list.lower);
  7991. netdev_adjacent_sysfs_add(iter->dev, dev,
  7992. &iter->dev->adj_list.lower);
  7993. }
  7994. list_for_each_entry(iter, &dev->adj_list.lower, list) {
  7995. if (!net_eq(net, dev_net(iter->dev)))
  7996. continue;
  7997. netdev_adjacent_sysfs_del(iter->dev, oldname,
  7998. &iter->dev->adj_list.upper);
  7999. netdev_adjacent_sysfs_add(iter->dev, dev,
  8000. &iter->dev->adj_list.upper);
  8001. }
  8002. }
  8003. void *netdev_lower_dev_get_private(struct net_device *dev,
  8004. struct net_device *lower_dev)
  8005. {
  8006. struct netdev_adjacent *lower;
  8007. if (!lower_dev)
  8008. return NULL;
  8009. lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
  8010. if (!lower)
  8011. return NULL;
  8012. return lower->private;
  8013. }
  8014. EXPORT_SYMBOL(netdev_lower_dev_get_private);
  8015. /**
  8016. * netdev_lower_state_changed - Dispatch event about lower device state change
  8017. * @lower_dev: device
  8018. * @lower_state_info: state to dispatch
  8019. *
  8020. * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
  8021. * The caller must hold the RTNL lock.
  8022. */
  8023. void netdev_lower_state_changed(struct net_device *lower_dev,
  8024. void *lower_state_info)
  8025. {
  8026. struct netdev_notifier_changelowerstate_info changelowerstate_info = {
  8027. .info.dev = lower_dev,
  8028. };
  8029. ASSERT_RTNL();
  8030. changelowerstate_info.lower_state_info = lower_state_info;
  8031. call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
  8032. &changelowerstate_info.info);
  8033. }
  8034. EXPORT_SYMBOL(netdev_lower_state_changed);
  8035. static void dev_change_rx_flags(struct net_device *dev, int flags)
  8036. {
  8037. const struct net_device_ops *ops = dev->netdev_ops;
  8038. if (ops->ndo_change_rx_flags)
  8039. ops->ndo_change_rx_flags(dev, flags);
  8040. }
  8041. static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
  8042. {
  8043. unsigned int old_flags = dev->flags;
  8044. unsigned int promiscuity, flags;
  8045. kuid_t uid;
  8046. kgid_t gid;
  8047. ASSERT_RTNL();
  8048. promiscuity = dev->promiscuity + inc;
  8049. if (promiscuity == 0) {
  8050. /*
  8051. * Avoid overflow.
  8052. * If inc causes overflow, untouch promisc and return error.
  8053. */
  8054. if (unlikely(inc > 0)) {
  8055. netdev_warn(dev, "promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n");
  8056. return -EOVERFLOW;
  8057. }
  8058. flags = old_flags & ~IFF_PROMISC;
  8059. } else {
  8060. flags = old_flags | IFF_PROMISC;
  8061. }
  8062. WRITE_ONCE(dev->promiscuity, promiscuity);
  8063. if (flags != old_flags) {
  8064. WRITE_ONCE(dev->flags, flags);
  8065. netdev_info(dev, "%s promiscuous mode\n",
  8066. dev->flags & IFF_PROMISC ? "entered" : "left");
  8067. if (audit_enabled) {
  8068. current_uid_gid(&uid, &gid);
  8069. audit_log(audit_context(), GFP_ATOMIC,
  8070. AUDIT_ANOM_PROMISCUOUS,
  8071. "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
  8072. dev->name, (dev->flags & IFF_PROMISC),
  8073. (old_flags & IFF_PROMISC),
  8074. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  8075. from_kuid(&init_user_ns, uid),
  8076. from_kgid(&init_user_ns, gid),
  8077. audit_get_sessionid(current));
  8078. }
  8079. dev_change_rx_flags(dev, IFF_PROMISC);
  8080. }
  8081. if (notify) {
  8082. /* The ops lock is only required to ensure consistent locking
  8083. * for `NETDEV_CHANGE` notifiers. This function is sometimes
  8084. * called without the lock, even for devices that are ops
  8085. * locked, such as in `dev_uc_sync_multiple` when using
  8086. * bonding or teaming.
  8087. */
  8088. netdev_ops_assert_locked(dev);
  8089. __dev_notify_flags(dev, old_flags, IFF_PROMISC, 0, NULL);
  8090. }
  8091. return 0;
  8092. }
  8093. int netif_set_promiscuity(struct net_device *dev, int inc)
  8094. {
  8095. unsigned int old_flags = dev->flags;
  8096. int err;
  8097. err = __dev_set_promiscuity(dev, inc, true);
  8098. if (err < 0)
  8099. return err;
  8100. if (dev->flags != old_flags)
  8101. dev_set_rx_mode(dev);
  8102. return err;
  8103. }
  8104. int netif_set_allmulti(struct net_device *dev, int inc, bool notify)
  8105. {
  8106. unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
  8107. unsigned int allmulti, flags;
  8108. ASSERT_RTNL();
  8109. allmulti = dev->allmulti + inc;
  8110. if (allmulti == 0) {
  8111. /*
  8112. * Avoid overflow.
  8113. * If inc causes overflow, untouch allmulti and return error.
  8114. */
  8115. if (unlikely(inc > 0)) {
  8116. netdev_warn(dev, "allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n");
  8117. return -EOVERFLOW;
  8118. }
  8119. flags = old_flags & ~IFF_ALLMULTI;
  8120. } else {
  8121. flags = old_flags | IFF_ALLMULTI;
  8122. }
  8123. WRITE_ONCE(dev->allmulti, allmulti);
  8124. if (flags != old_flags) {
  8125. WRITE_ONCE(dev->flags, flags);
  8126. netdev_info(dev, "%s allmulticast mode\n",
  8127. dev->flags & IFF_ALLMULTI ? "entered" : "left");
  8128. dev_change_rx_flags(dev, IFF_ALLMULTI);
  8129. dev_set_rx_mode(dev);
  8130. if (notify)
  8131. __dev_notify_flags(dev, old_flags,
  8132. dev->gflags ^ old_gflags, 0, NULL);
  8133. }
  8134. return 0;
  8135. }
  8136. /*
  8137. * Upload unicast and multicast address lists to device and
  8138. * configure RX filtering. When the device doesn't support unicast
  8139. * filtering it is put in promiscuous mode while unicast addresses
  8140. * are present.
  8141. */
  8142. void __dev_set_rx_mode(struct net_device *dev)
  8143. {
  8144. const struct net_device_ops *ops = dev->netdev_ops;
  8145. /* dev_open will call this function so the list will stay sane. */
  8146. if (!(dev->flags&IFF_UP))
  8147. return;
  8148. if (!netif_device_present(dev))
  8149. return;
  8150. if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
  8151. /* Unicast addresses changes may only happen under the rtnl,
  8152. * therefore calling __dev_set_promiscuity here is safe.
  8153. */
  8154. if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
  8155. __dev_set_promiscuity(dev, 1, false);
  8156. dev->uc_promisc = true;
  8157. } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
  8158. __dev_set_promiscuity(dev, -1, false);
  8159. dev->uc_promisc = false;
  8160. }
  8161. }
  8162. if (ops->ndo_set_rx_mode)
  8163. ops->ndo_set_rx_mode(dev);
  8164. }
  8165. void dev_set_rx_mode(struct net_device *dev)
  8166. {
  8167. netif_addr_lock_bh(dev);
  8168. __dev_set_rx_mode(dev);
  8169. netif_addr_unlock_bh(dev);
  8170. }
  8171. /**
  8172. * netif_get_flags() - get flags reported to userspace
  8173. * @dev: device
  8174. *
  8175. * Get the combination of flag bits exported through APIs to userspace.
  8176. */
  8177. unsigned int netif_get_flags(const struct net_device *dev)
  8178. {
  8179. unsigned int flags;
  8180. flags = (READ_ONCE(dev->flags) & ~(IFF_PROMISC |
  8181. IFF_ALLMULTI |
  8182. IFF_RUNNING |
  8183. IFF_LOWER_UP |
  8184. IFF_DORMANT)) |
  8185. (READ_ONCE(dev->gflags) & (IFF_PROMISC |
  8186. IFF_ALLMULTI));
  8187. if (netif_running(dev)) {
  8188. if (netif_oper_up(dev))
  8189. flags |= IFF_RUNNING;
  8190. if (netif_carrier_ok(dev))
  8191. flags |= IFF_LOWER_UP;
  8192. if (netif_dormant(dev))
  8193. flags |= IFF_DORMANT;
  8194. }
  8195. return flags;
  8196. }
  8197. EXPORT_SYMBOL(netif_get_flags);
  8198. int __dev_change_flags(struct net_device *dev, unsigned int flags,
  8199. struct netlink_ext_ack *extack)
  8200. {
  8201. unsigned int old_flags = dev->flags;
  8202. int ret;
  8203. ASSERT_RTNL();
  8204. /*
  8205. * Set the flags on our device.
  8206. */
  8207. dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
  8208. IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
  8209. IFF_AUTOMEDIA)) |
  8210. (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
  8211. IFF_ALLMULTI));
  8212. /*
  8213. * Load in the correct multicast list now the flags have changed.
  8214. */
  8215. if ((old_flags ^ flags) & IFF_MULTICAST)
  8216. dev_change_rx_flags(dev, IFF_MULTICAST);
  8217. dev_set_rx_mode(dev);
  8218. /*
  8219. * Have we downed the interface. We handle IFF_UP ourselves
  8220. * according to user attempts to set it, rather than blindly
  8221. * setting it.
  8222. */
  8223. ret = 0;
  8224. if ((old_flags ^ flags) & IFF_UP) {
  8225. if (old_flags & IFF_UP)
  8226. __dev_close(dev);
  8227. else
  8228. ret = __dev_open(dev, extack);
  8229. }
  8230. if ((flags ^ dev->gflags) & IFF_PROMISC) {
  8231. int inc = (flags & IFF_PROMISC) ? 1 : -1;
  8232. old_flags = dev->flags;
  8233. dev->gflags ^= IFF_PROMISC;
  8234. if (__dev_set_promiscuity(dev, inc, false) >= 0)
  8235. if (dev->flags != old_flags)
  8236. dev_set_rx_mode(dev);
  8237. }
  8238. /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
  8239. * is important. Some (broken) drivers set IFF_PROMISC, when
  8240. * IFF_ALLMULTI is requested not asking us and not reporting.
  8241. */
  8242. if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
  8243. int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
  8244. dev->gflags ^= IFF_ALLMULTI;
  8245. netif_set_allmulti(dev, inc, false);
  8246. }
  8247. return ret;
  8248. }
  8249. void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
  8250. unsigned int gchanges, u32 portid,
  8251. const struct nlmsghdr *nlh)
  8252. {
  8253. unsigned int changes = dev->flags ^ old_flags;
  8254. if (gchanges)
  8255. rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC, portid, nlh);
  8256. if (changes & IFF_UP) {
  8257. if (dev->flags & IFF_UP)
  8258. call_netdevice_notifiers(NETDEV_UP, dev);
  8259. else
  8260. call_netdevice_notifiers(NETDEV_DOWN, dev);
  8261. }
  8262. if (dev->flags & IFF_UP &&
  8263. (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
  8264. struct netdev_notifier_change_info change_info = {
  8265. .info = {
  8266. .dev = dev,
  8267. },
  8268. .flags_changed = changes,
  8269. };
  8270. call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
  8271. }
  8272. }
  8273. int netif_change_flags(struct net_device *dev, unsigned int flags,
  8274. struct netlink_ext_ack *extack)
  8275. {
  8276. int ret;
  8277. unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
  8278. ret = __dev_change_flags(dev, flags, extack);
  8279. if (ret < 0)
  8280. return ret;
  8281. changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
  8282. __dev_notify_flags(dev, old_flags, changes, 0, NULL);
  8283. return ret;
  8284. }
  8285. int __netif_set_mtu(struct net_device *dev, int new_mtu)
  8286. {
  8287. const struct net_device_ops *ops = dev->netdev_ops;
  8288. if (ops->ndo_change_mtu)
  8289. return ops->ndo_change_mtu(dev, new_mtu);
  8290. /* Pairs with all the lockless reads of dev->mtu in the stack */
  8291. WRITE_ONCE(dev->mtu, new_mtu);
  8292. return 0;
  8293. }
  8294. EXPORT_SYMBOL_NS_GPL(__netif_set_mtu, "NETDEV_INTERNAL");
  8295. int dev_validate_mtu(struct net_device *dev, int new_mtu,
  8296. struct netlink_ext_ack *extack)
  8297. {
  8298. /* MTU must be positive, and in range */
  8299. if (new_mtu < 0 || new_mtu < dev->min_mtu) {
  8300. NL_SET_ERR_MSG(extack, "mtu less than device minimum");
  8301. return -EINVAL;
  8302. }
  8303. if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
  8304. NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
  8305. return -EINVAL;
  8306. }
  8307. return 0;
  8308. }
  8309. /**
  8310. * netif_set_mtu_ext() - Change maximum transfer unit
  8311. * @dev: device
  8312. * @new_mtu: new transfer unit
  8313. * @extack: netlink extended ack
  8314. *
  8315. * Change the maximum transfer size of the network device.
  8316. *
  8317. * Return: 0 on success, -errno on failure.
  8318. */
  8319. int netif_set_mtu_ext(struct net_device *dev, int new_mtu,
  8320. struct netlink_ext_ack *extack)
  8321. {
  8322. int err, orig_mtu;
  8323. netdev_ops_assert_locked(dev);
  8324. if (new_mtu == dev->mtu)
  8325. return 0;
  8326. err = dev_validate_mtu(dev, new_mtu, extack);
  8327. if (err)
  8328. return err;
  8329. if (!netif_device_present(dev))
  8330. return -ENODEV;
  8331. err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
  8332. err = notifier_to_errno(err);
  8333. if (err)
  8334. return err;
  8335. orig_mtu = dev->mtu;
  8336. err = __netif_set_mtu(dev, new_mtu);
  8337. if (!err) {
  8338. err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
  8339. orig_mtu);
  8340. err = notifier_to_errno(err);
  8341. if (err) {
  8342. /* setting mtu back and notifying everyone again,
  8343. * so that they have a chance to revert changes.
  8344. */
  8345. __netif_set_mtu(dev, orig_mtu);
  8346. call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
  8347. new_mtu);
  8348. }
  8349. }
  8350. return err;
  8351. }
  8352. int netif_set_mtu(struct net_device *dev, int new_mtu)
  8353. {
  8354. struct netlink_ext_ack extack;
  8355. int err;
  8356. memset(&extack, 0, sizeof(extack));
  8357. err = netif_set_mtu_ext(dev, new_mtu, &extack);
  8358. if (err && extack._msg)
  8359. net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
  8360. return err;
  8361. }
  8362. EXPORT_SYMBOL(netif_set_mtu);
  8363. int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
  8364. {
  8365. unsigned int orig_len = dev->tx_queue_len;
  8366. int res;
  8367. if (new_len != (unsigned int)new_len)
  8368. return -ERANGE;
  8369. if (new_len != orig_len) {
  8370. WRITE_ONCE(dev->tx_queue_len, new_len);
  8371. res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
  8372. res = notifier_to_errno(res);
  8373. if (res)
  8374. goto err_rollback;
  8375. res = dev_qdisc_change_tx_queue_len(dev);
  8376. if (res)
  8377. goto err_rollback;
  8378. }
  8379. return 0;
  8380. err_rollback:
  8381. netdev_err(dev, "refused to change device tx_queue_len\n");
  8382. WRITE_ONCE(dev->tx_queue_len, orig_len);
  8383. return res;
  8384. }
  8385. void netif_set_group(struct net_device *dev, int new_group)
  8386. {
  8387. dev->group = new_group;
  8388. }
  8389. /**
  8390. * netif_pre_changeaddr_notify() - Call NETDEV_PRE_CHANGEADDR.
  8391. * @dev: device
  8392. * @addr: new address
  8393. * @extack: netlink extended ack
  8394. *
  8395. * Return: 0 on success, -errno on failure.
  8396. */
  8397. int netif_pre_changeaddr_notify(struct net_device *dev, const char *addr,
  8398. struct netlink_ext_ack *extack)
  8399. {
  8400. struct netdev_notifier_pre_changeaddr_info info = {
  8401. .info.dev = dev,
  8402. .info.extack = extack,
  8403. .dev_addr = addr,
  8404. };
  8405. int rc;
  8406. rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
  8407. return notifier_to_errno(rc);
  8408. }
  8409. EXPORT_SYMBOL_NS_GPL(netif_pre_changeaddr_notify, "NETDEV_INTERNAL");
  8410. int netif_set_mac_address(struct net_device *dev, struct sockaddr_storage *ss,
  8411. struct netlink_ext_ack *extack)
  8412. {
  8413. const struct net_device_ops *ops = dev->netdev_ops;
  8414. int err;
  8415. if (!ops->ndo_set_mac_address)
  8416. return -EOPNOTSUPP;
  8417. if (ss->ss_family != dev->type)
  8418. return -EINVAL;
  8419. if (!netif_device_present(dev))
  8420. return -ENODEV;
  8421. err = netif_pre_changeaddr_notify(dev, ss->__data, extack);
  8422. if (err)
  8423. return err;
  8424. if (memcmp(dev->dev_addr, ss->__data, dev->addr_len)) {
  8425. err = ops->ndo_set_mac_address(dev, ss);
  8426. if (err)
  8427. return err;
  8428. }
  8429. dev->addr_assign_type = NET_ADDR_SET;
  8430. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  8431. add_device_randomness(dev->dev_addr, dev->addr_len);
  8432. return 0;
  8433. }
  8434. DECLARE_RWSEM(dev_addr_sem);
  8435. /* "sa" is a true struct sockaddr with limited "sa_data" member. */
  8436. int netif_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name)
  8437. {
  8438. size_t size = sizeof(sa->sa_data);
  8439. struct net_device *dev;
  8440. int ret = 0;
  8441. down_read(&dev_addr_sem);
  8442. rcu_read_lock();
  8443. dev = dev_get_by_name_rcu(net, dev_name);
  8444. if (!dev) {
  8445. ret = -ENODEV;
  8446. goto unlock;
  8447. }
  8448. if (!dev->addr_len)
  8449. memset(sa->sa_data, 0, size);
  8450. else
  8451. memcpy(sa->sa_data, dev->dev_addr,
  8452. min_t(size_t, size, dev->addr_len));
  8453. sa->sa_family = dev->type;
  8454. unlock:
  8455. rcu_read_unlock();
  8456. up_read(&dev_addr_sem);
  8457. return ret;
  8458. }
  8459. EXPORT_SYMBOL_NS_GPL(netif_get_mac_address, "NETDEV_INTERNAL");
  8460. int netif_change_carrier(struct net_device *dev, bool new_carrier)
  8461. {
  8462. const struct net_device_ops *ops = dev->netdev_ops;
  8463. if (!ops->ndo_change_carrier)
  8464. return -EOPNOTSUPP;
  8465. if (!netif_device_present(dev))
  8466. return -ENODEV;
  8467. return ops->ndo_change_carrier(dev, new_carrier);
  8468. }
  8469. /**
  8470. * dev_get_phys_port_id - Get device physical port ID
  8471. * @dev: device
  8472. * @ppid: port ID
  8473. *
  8474. * Get device physical port ID
  8475. */
  8476. int dev_get_phys_port_id(struct net_device *dev,
  8477. struct netdev_phys_item_id *ppid)
  8478. {
  8479. const struct net_device_ops *ops = dev->netdev_ops;
  8480. if (!ops->ndo_get_phys_port_id)
  8481. return -EOPNOTSUPP;
  8482. return ops->ndo_get_phys_port_id(dev, ppid);
  8483. }
  8484. /**
  8485. * dev_get_phys_port_name - Get device physical port name
  8486. * @dev: device
  8487. * @name: port name
  8488. * @len: limit of bytes to copy to name
  8489. *
  8490. * Get device physical port name
  8491. */
  8492. int dev_get_phys_port_name(struct net_device *dev,
  8493. char *name, size_t len)
  8494. {
  8495. const struct net_device_ops *ops = dev->netdev_ops;
  8496. int err;
  8497. if (ops->ndo_get_phys_port_name) {
  8498. err = ops->ndo_get_phys_port_name(dev, name, len);
  8499. if (err != -EOPNOTSUPP)
  8500. return err;
  8501. }
  8502. return devlink_compat_phys_port_name_get(dev, name, len);
  8503. }
  8504. /**
  8505. * netif_get_port_parent_id() - Get the device's port parent identifier
  8506. * @dev: network device
  8507. * @ppid: pointer to a storage for the port's parent identifier
  8508. * @recurse: allow/disallow recursion to lower devices
  8509. *
  8510. * Get the devices's port parent identifier.
  8511. *
  8512. * Return: 0 on success, -errno on failure.
  8513. */
  8514. int netif_get_port_parent_id(struct net_device *dev,
  8515. struct netdev_phys_item_id *ppid, bool recurse)
  8516. {
  8517. const struct net_device_ops *ops = dev->netdev_ops;
  8518. struct netdev_phys_item_id first = { };
  8519. struct net_device *lower_dev;
  8520. struct list_head *iter;
  8521. int err;
  8522. if (ops->ndo_get_port_parent_id) {
  8523. err = ops->ndo_get_port_parent_id(dev, ppid);
  8524. if (err != -EOPNOTSUPP)
  8525. return err;
  8526. }
  8527. err = devlink_compat_switch_id_get(dev, ppid);
  8528. if (!recurse || err != -EOPNOTSUPP)
  8529. return err;
  8530. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  8531. err = netif_get_port_parent_id(lower_dev, ppid, true);
  8532. if (err)
  8533. break;
  8534. if (!first.id_len)
  8535. first = *ppid;
  8536. else if (memcmp(&first, ppid, sizeof(*ppid)))
  8537. return -EOPNOTSUPP;
  8538. }
  8539. return err;
  8540. }
  8541. EXPORT_SYMBOL(netif_get_port_parent_id);
  8542. /**
  8543. * netdev_port_same_parent_id - Indicate if two network devices have
  8544. * the same port parent identifier
  8545. * @a: first network device
  8546. * @b: second network device
  8547. */
  8548. bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
  8549. {
  8550. struct netdev_phys_item_id a_id = { };
  8551. struct netdev_phys_item_id b_id = { };
  8552. if (netif_get_port_parent_id(a, &a_id, true) ||
  8553. netif_get_port_parent_id(b, &b_id, true))
  8554. return false;
  8555. return netdev_phys_item_id_same(&a_id, &b_id);
  8556. }
  8557. EXPORT_SYMBOL(netdev_port_same_parent_id);
  8558. int netif_change_proto_down(struct net_device *dev, bool proto_down)
  8559. {
  8560. if (!dev->change_proto_down)
  8561. return -EOPNOTSUPP;
  8562. if (!netif_device_present(dev))
  8563. return -ENODEV;
  8564. if (proto_down)
  8565. netif_carrier_off(dev);
  8566. else
  8567. netif_carrier_on(dev);
  8568. WRITE_ONCE(dev->proto_down, proto_down);
  8569. return 0;
  8570. }
  8571. /**
  8572. * netdev_change_proto_down_reason_locked - proto down reason
  8573. *
  8574. * @dev: device
  8575. * @mask: proto down mask
  8576. * @value: proto down value
  8577. */
  8578. void netdev_change_proto_down_reason_locked(struct net_device *dev,
  8579. unsigned long mask, u32 value)
  8580. {
  8581. u32 proto_down_reason;
  8582. int b;
  8583. if (!mask) {
  8584. proto_down_reason = value;
  8585. } else {
  8586. proto_down_reason = dev->proto_down_reason;
  8587. for_each_set_bit(b, &mask, 32) {
  8588. if (value & (1 << b))
  8589. proto_down_reason |= BIT(b);
  8590. else
  8591. proto_down_reason &= ~BIT(b);
  8592. }
  8593. }
  8594. WRITE_ONCE(dev->proto_down_reason, proto_down_reason);
  8595. }
  8596. struct bpf_xdp_link {
  8597. struct bpf_link link;
  8598. struct net_device *dev; /* protected by rtnl_lock, no refcnt held */
  8599. int flags;
  8600. };
  8601. static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
  8602. {
  8603. if (flags & XDP_FLAGS_HW_MODE)
  8604. return XDP_MODE_HW;
  8605. if (flags & XDP_FLAGS_DRV_MODE)
  8606. return XDP_MODE_DRV;
  8607. if (flags & XDP_FLAGS_SKB_MODE)
  8608. return XDP_MODE_SKB;
  8609. return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
  8610. }
  8611. static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
  8612. {
  8613. switch (mode) {
  8614. case XDP_MODE_SKB:
  8615. return generic_xdp_install;
  8616. case XDP_MODE_DRV:
  8617. case XDP_MODE_HW:
  8618. return dev->netdev_ops->ndo_bpf;
  8619. default:
  8620. return NULL;
  8621. }
  8622. }
  8623. static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev,
  8624. enum bpf_xdp_mode mode)
  8625. {
  8626. return dev->xdp_state[mode].link;
  8627. }
  8628. static struct bpf_prog *dev_xdp_prog(struct net_device *dev,
  8629. enum bpf_xdp_mode mode)
  8630. {
  8631. struct bpf_xdp_link *link = dev_xdp_link(dev, mode);
  8632. if (link)
  8633. return link->link.prog;
  8634. return dev->xdp_state[mode].prog;
  8635. }
  8636. u8 dev_xdp_prog_count(struct net_device *dev)
  8637. {
  8638. u8 count = 0;
  8639. int i;
  8640. for (i = 0; i < __MAX_XDP_MODE; i++)
  8641. if (dev->xdp_state[i].prog || dev->xdp_state[i].link)
  8642. count++;
  8643. return count;
  8644. }
  8645. EXPORT_SYMBOL_GPL(dev_xdp_prog_count);
  8646. u8 dev_xdp_sb_prog_count(struct net_device *dev)
  8647. {
  8648. u8 count = 0;
  8649. int i;
  8650. for (i = 0; i < __MAX_XDP_MODE; i++)
  8651. if (dev->xdp_state[i].prog &&
  8652. !dev->xdp_state[i].prog->aux->xdp_has_frags)
  8653. count++;
  8654. return count;
  8655. }
  8656. int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
  8657. {
  8658. if (!dev->netdev_ops->ndo_bpf)
  8659. return -EOPNOTSUPP;
  8660. if (dev->cfg->hds_config == ETHTOOL_TCP_DATA_SPLIT_ENABLED &&
  8661. bpf->command == XDP_SETUP_PROG &&
  8662. bpf->prog && !bpf->prog->aux->xdp_has_frags) {
  8663. NL_SET_ERR_MSG(bpf->extack,
  8664. "unable to propagate XDP to device using tcp-data-split");
  8665. return -EBUSY;
  8666. }
  8667. if (dev_get_min_mp_channel_count(dev)) {
  8668. NL_SET_ERR_MSG(bpf->extack, "unable to propagate XDP to device using memory provider");
  8669. return -EBUSY;
  8670. }
  8671. return dev->netdev_ops->ndo_bpf(dev, bpf);
  8672. }
  8673. EXPORT_SYMBOL_GPL(netif_xdp_propagate);
  8674. u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode)
  8675. {
  8676. struct bpf_prog *prog = dev_xdp_prog(dev, mode);
  8677. return prog ? prog->aux->id : 0;
  8678. }
  8679. static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode,
  8680. struct bpf_xdp_link *link)
  8681. {
  8682. dev->xdp_state[mode].link = link;
  8683. dev->xdp_state[mode].prog = NULL;
  8684. }
  8685. static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
  8686. struct bpf_prog *prog)
  8687. {
  8688. dev->xdp_state[mode].link = NULL;
  8689. dev->xdp_state[mode].prog = prog;
  8690. }
  8691. static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
  8692. bpf_op_t bpf_op, struct netlink_ext_ack *extack,
  8693. u32 flags, struct bpf_prog *prog)
  8694. {
  8695. struct netdev_bpf xdp;
  8696. int err;
  8697. netdev_ops_assert_locked(dev);
  8698. if (dev->cfg->hds_config == ETHTOOL_TCP_DATA_SPLIT_ENABLED &&
  8699. prog && !prog->aux->xdp_has_frags) {
  8700. NL_SET_ERR_MSG(extack, "unable to install XDP to device using tcp-data-split");
  8701. return -EBUSY;
  8702. }
  8703. if (dev_get_min_mp_channel_count(dev)) {
  8704. NL_SET_ERR_MSG(extack, "unable to install XDP to device using memory provider");
  8705. return -EBUSY;
  8706. }
  8707. memset(&xdp, 0, sizeof(xdp));
  8708. xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG;
  8709. xdp.extack = extack;
  8710. xdp.flags = flags;
  8711. xdp.prog = prog;
  8712. /* Drivers assume refcnt is already incremented (i.e, prog pointer is
  8713. * "moved" into driver), so they don't increment it on their own, but
  8714. * they do decrement refcnt when program is detached or replaced.
  8715. * Given net_device also owns link/prog, we need to bump refcnt here
  8716. * to prevent drivers from underflowing it.
  8717. */
  8718. if (prog)
  8719. bpf_prog_inc(prog);
  8720. err = bpf_op(dev, &xdp);
  8721. if (err) {
  8722. if (prog)
  8723. bpf_prog_put(prog);
  8724. return err;
  8725. }
  8726. if (mode != XDP_MODE_HW)
  8727. bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog);
  8728. return 0;
  8729. }
  8730. static void dev_xdp_uninstall(struct net_device *dev)
  8731. {
  8732. struct bpf_xdp_link *link;
  8733. struct bpf_prog *prog;
  8734. enum bpf_xdp_mode mode;
  8735. bpf_op_t bpf_op;
  8736. ASSERT_RTNL();
  8737. for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) {
  8738. prog = dev_xdp_prog(dev, mode);
  8739. if (!prog)
  8740. continue;
  8741. bpf_op = dev_xdp_bpf_op(dev, mode);
  8742. if (!bpf_op)
  8743. continue;
  8744. WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
  8745. /* auto-detach link from net device */
  8746. link = dev_xdp_link(dev, mode);
  8747. if (link)
  8748. link->dev = NULL;
  8749. else
  8750. bpf_prog_put(prog);
  8751. dev_xdp_set_link(dev, mode, NULL);
  8752. }
  8753. }
  8754. static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
  8755. struct bpf_xdp_link *link, struct bpf_prog *new_prog,
  8756. struct bpf_prog *old_prog, u32 flags)
  8757. {
  8758. unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES);
  8759. struct bpf_prog *cur_prog;
  8760. struct net_device *upper;
  8761. struct list_head *iter;
  8762. enum bpf_xdp_mode mode;
  8763. bpf_op_t bpf_op;
  8764. int err;
  8765. ASSERT_RTNL();
  8766. /* either link or prog attachment, never both */
  8767. if (link && (new_prog || old_prog))
  8768. return -EINVAL;
  8769. /* link supports only XDP mode flags */
  8770. if (link && (flags & ~XDP_FLAGS_MODES)) {
  8771. NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment");
  8772. return -EINVAL;
  8773. }
  8774. /* just one XDP mode bit should be set, zero defaults to drv/skb mode */
  8775. if (num_modes > 1) {
  8776. NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set");
  8777. return -EINVAL;
  8778. }
  8779. /* avoid ambiguity if offload + drv/skb mode progs are both loaded */
  8780. if (!num_modes && dev_xdp_prog_count(dev) > 1) {
  8781. NL_SET_ERR_MSG(extack,
  8782. "More than one program loaded, unset mode is ambiguous");
  8783. return -EINVAL;
  8784. }
  8785. /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */
  8786. if (old_prog && !(flags & XDP_FLAGS_REPLACE)) {
  8787. NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified");
  8788. return -EINVAL;
  8789. }
  8790. mode = dev_xdp_mode(dev, flags);
  8791. /* can't replace attached link */
  8792. if (dev_xdp_link(dev, mode)) {
  8793. NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
  8794. return -EBUSY;
  8795. }
  8796. /* don't allow if an upper device already has a program */
  8797. netdev_for_each_upper_dev_rcu(dev, upper, iter) {
  8798. if (dev_xdp_prog_count(upper) > 0) {
  8799. NL_SET_ERR_MSG(extack, "Cannot attach when an upper device already has a program");
  8800. return -EEXIST;
  8801. }
  8802. }
  8803. cur_prog = dev_xdp_prog(dev, mode);
  8804. /* can't replace attached prog with link */
  8805. if (link && cur_prog) {
  8806. NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link");
  8807. return -EBUSY;
  8808. }
  8809. if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) {
  8810. NL_SET_ERR_MSG(extack, "Active program does not match expected");
  8811. return -EEXIST;
  8812. }
  8813. /* put effective new program into new_prog */
  8814. if (link)
  8815. new_prog = link->link.prog;
  8816. if (new_prog) {
  8817. bool offload = mode == XDP_MODE_HW;
  8818. enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB
  8819. ? XDP_MODE_DRV : XDP_MODE_SKB;
  8820. if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) {
  8821. NL_SET_ERR_MSG(extack, "XDP program already attached");
  8822. return -EBUSY;
  8823. }
  8824. if (!offload && dev_xdp_prog(dev, other_mode)) {
  8825. NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
  8826. return -EEXIST;
  8827. }
  8828. if (!offload && bpf_prog_is_offloaded(new_prog->aux)) {
  8829. NL_SET_ERR_MSG(extack, "Using offloaded program without HW_MODE flag is not supported");
  8830. return -EINVAL;
  8831. }
  8832. if (bpf_prog_is_dev_bound(new_prog->aux) && !bpf_offload_dev_match(new_prog, dev)) {
  8833. NL_SET_ERR_MSG(extack, "Program bound to different device");
  8834. return -EINVAL;
  8835. }
  8836. if (bpf_prog_is_dev_bound(new_prog->aux) && mode == XDP_MODE_SKB) {
  8837. NL_SET_ERR_MSG(extack, "Can't attach device-bound programs in generic mode");
  8838. return -EINVAL;
  8839. }
  8840. if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
  8841. NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
  8842. return -EINVAL;
  8843. }
  8844. if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) {
  8845. NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device");
  8846. return -EINVAL;
  8847. }
  8848. }
  8849. /* don't call drivers if the effective program didn't change */
  8850. if (new_prog != cur_prog) {
  8851. bpf_op = dev_xdp_bpf_op(dev, mode);
  8852. if (!bpf_op) {
  8853. NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode");
  8854. return -EOPNOTSUPP;
  8855. }
  8856. err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog);
  8857. if (err)
  8858. return err;
  8859. }
  8860. if (link)
  8861. dev_xdp_set_link(dev, mode, link);
  8862. else
  8863. dev_xdp_set_prog(dev, mode, new_prog);
  8864. if (cur_prog)
  8865. bpf_prog_put(cur_prog);
  8866. return 0;
  8867. }
  8868. static int dev_xdp_attach_link(struct net_device *dev,
  8869. struct netlink_ext_ack *extack,
  8870. struct bpf_xdp_link *link)
  8871. {
  8872. return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags);
  8873. }
  8874. static int dev_xdp_detach_link(struct net_device *dev,
  8875. struct netlink_ext_ack *extack,
  8876. struct bpf_xdp_link *link)
  8877. {
  8878. enum bpf_xdp_mode mode;
  8879. bpf_op_t bpf_op;
  8880. ASSERT_RTNL();
  8881. mode = dev_xdp_mode(dev, link->flags);
  8882. if (dev_xdp_link(dev, mode) != link)
  8883. return -EINVAL;
  8884. bpf_op = dev_xdp_bpf_op(dev, mode);
  8885. WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
  8886. dev_xdp_set_link(dev, mode, NULL);
  8887. return 0;
  8888. }
  8889. static void bpf_xdp_link_release(struct bpf_link *link)
  8890. {
  8891. struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
  8892. rtnl_lock();
  8893. /* if racing with net_device's tear down, xdp_link->dev might be
  8894. * already NULL, in which case link was already auto-detached
  8895. */
  8896. if (xdp_link->dev) {
  8897. netdev_lock_ops(xdp_link->dev);
  8898. WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
  8899. netdev_unlock_ops(xdp_link->dev);
  8900. xdp_link->dev = NULL;
  8901. }
  8902. rtnl_unlock();
  8903. }
  8904. static int bpf_xdp_link_detach(struct bpf_link *link)
  8905. {
  8906. bpf_xdp_link_release(link);
  8907. return 0;
  8908. }
  8909. static void bpf_xdp_link_dealloc(struct bpf_link *link)
  8910. {
  8911. struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
  8912. kfree(xdp_link);
  8913. }
  8914. static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link,
  8915. struct seq_file *seq)
  8916. {
  8917. struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
  8918. u32 ifindex = 0;
  8919. rtnl_lock();
  8920. if (xdp_link->dev)
  8921. ifindex = xdp_link->dev->ifindex;
  8922. rtnl_unlock();
  8923. seq_printf(seq, "ifindex:\t%u\n", ifindex);
  8924. }
  8925. static int bpf_xdp_link_fill_link_info(const struct bpf_link *link,
  8926. struct bpf_link_info *info)
  8927. {
  8928. struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
  8929. u32 ifindex = 0;
  8930. rtnl_lock();
  8931. if (xdp_link->dev)
  8932. ifindex = xdp_link->dev->ifindex;
  8933. rtnl_unlock();
  8934. info->xdp.ifindex = ifindex;
  8935. return 0;
  8936. }
  8937. static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
  8938. struct bpf_prog *old_prog)
  8939. {
  8940. struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
  8941. enum bpf_xdp_mode mode;
  8942. bpf_op_t bpf_op;
  8943. int err = 0;
  8944. rtnl_lock();
  8945. /* link might have been auto-released already, so fail */
  8946. if (!xdp_link->dev) {
  8947. err = -ENOLINK;
  8948. goto out_unlock;
  8949. }
  8950. if (old_prog && link->prog != old_prog) {
  8951. err = -EPERM;
  8952. goto out_unlock;
  8953. }
  8954. old_prog = link->prog;
  8955. if (old_prog->type != new_prog->type ||
  8956. old_prog->expected_attach_type != new_prog->expected_attach_type) {
  8957. err = -EINVAL;
  8958. goto out_unlock;
  8959. }
  8960. if (old_prog == new_prog) {
  8961. /* no-op, don't disturb drivers */
  8962. bpf_prog_put(new_prog);
  8963. goto out_unlock;
  8964. }
  8965. netdev_lock_ops(xdp_link->dev);
  8966. mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
  8967. bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
  8968. err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
  8969. xdp_link->flags, new_prog);
  8970. netdev_unlock_ops(xdp_link->dev);
  8971. if (err)
  8972. goto out_unlock;
  8973. old_prog = xchg(&link->prog, new_prog);
  8974. bpf_prog_put(old_prog);
  8975. out_unlock:
  8976. rtnl_unlock();
  8977. return err;
  8978. }
  8979. static const struct bpf_link_ops bpf_xdp_link_lops = {
  8980. .release = bpf_xdp_link_release,
  8981. .dealloc = bpf_xdp_link_dealloc,
  8982. .detach = bpf_xdp_link_detach,
  8983. .show_fdinfo = bpf_xdp_link_show_fdinfo,
  8984. .fill_link_info = bpf_xdp_link_fill_link_info,
  8985. .update_prog = bpf_xdp_link_update,
  8986. };
  8987. int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  8988. {
  8989. struct net *net = current->nsproxy->net_ns;
  8990. struct bpf_link_primer link_primer;
  8991. struct netlink_ext_ack extack = {};
  8992. struct bpf_xdp_link *link;
  8993. struct net_device *dev;
  8994. int err, fd;
  8995. rtnl_lock();
  8996. dev = dev_get_by_index(net, attr->link_create.target_ifindex);
  8997. if (!dev) {
  8998. rtnl_unlock();
  8999. return -EINVAL;
  9000. }
  9001. link = kzalloc_obj(*link, GFP_USER);
  9002. if (!link) {
  9003. err = -ENOMEM;
  9004. goto unlock;
  9005. }
  9006. bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog,
  9007. attr->link_create.attach_type);
  9008. link->dev = dev;
  9009. link->flags = attr->link_create.flags;
  9010. err = bpf_link_prime(&link->link, &link_primer);
  9011. if (err) {
  9012. kfree(link);
  9013. goto unlock;
  9014. }
  9015. netdev_lock_ops(dev);
  9016. err = dev_xdp_attach_link(dev, &extack, link);
  9017. netdev_unlock_ops(dev);
  9018. rtnl_unlock();
  9019. if (err) {
  9020. link->dev = NULL;
  9021. bpf_link_cleanup(&link_primer);
  9022. trace_bpf_xdp_link_attach_failed(extack._msg);
  9023. goto out_put_dev;
  9024. }
  9025. fd = bpf_link_settle(&link_primer);
  9026. /* link itself doesn't hold dev's refcnt to not complicate shutdown */
  9027. dev_put(dev);
  9028. return fd;
  9029. unlock:
  9030. rtnl_unlock();
  9031. out_put_dev:
  9032. dev_put(dev);
  9033. return err;
  9034. }
  9035. /**
  9036. * dev_change_xdp_fd - set or clear a bpf program for a device rx path
  9037. * @dev: device
  9038. * @extack: netlink extended ack
  9039. * @fd: new program fd or negative value to clear
  9040. * @expected_fd: old program fd that userspace expects to replace or clear
  9041. * @flags: xdp-related flags
  9042. *
  9043. * Set or clear a bpf program for a device
  9044. */
  9045. int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
  9046. int fd, int expected_fd, u32 flags)
  9047. {
  9048. enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
  9049. struct bpf_prog *new_prog = NULL, *old_prog = NULL;
  9050. int err;
  9051. ASSERT_RTNL();
  9052. if (fd >= 0) {
  9053. new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
  9054. mode != XDP_MODE_SKB);
  9055. if (IS_ERR(new_prog))
  9056. return PTR_ERR(new_prog);
  9057. }
  9058. if (expected_fd >= 0) {
  9059. old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP,
  9060. mode != XDP_MODE_SKB);
  9061. if (IS_ERR(old_prog)) {
  9062. err = PTR_ERR(old_prog);
  9063. old_prog = NULL;
  9064. goto err_out;
  9065. }
  9066. }
  9067. err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags);
  9068. err_out:
  9069. if (err && new_prog)
  9070. bpf_prog_put(new_prog);
  9071. if (old_prog)
  9072. bpf_prog_put(old_prog);
  9073. return err;
  9074. }
  9075. u32 dev_get_min_mp_channel_count(const struct net_device *dev)
  9076. {
  9077. int i;
  9078. netdev_ops_assert_locked(dev);
  9079. for (i = dev->real_num_rx_queues - 1; i >= 0; i--)
  9080. if (dev->_rx[i].mp_params.mp_priv)
  9081. /* The channel count is the idx plus 1. */
  9082. return i + 1;
  9083. return 0;
  9084. }
  9085. /**
  9086. * dev_index_reserve() - allocate an ifindex in a namespace
  9087. * @net: the applicable net namespace
  9088. * @ifindex: requested ifindex, pass %0 to get one allocated
  9089. *
  9090. * Allocate a ifindex for a new device. Caller must either use the ifindex
  9091. * to store the device (via list_netdevice()) or call dev_index_release()
  9092. * to give the index up.
  9093. *
  9094. * Return: a suitable unique value for a new device interface number or -errno.
  9095. */
  9096. static int dev_index_reserve(struct net *net, u32 ifindex)
  9097. {
  9098. int err;
  9099. if (ifindex > INT_MAX) {
  9100. DEBUG_NET_WARN_ON_ONCE(1);
  9101. return -EINVAL;
  9102. }
  9103. if (!ifindex)
  9104. err = xa_alloc_cyclic(&net->dev_by_index, &ifindex, NULL,
  9105. xa_limit_31b, &net->ifindex, GFP_KERNEL);
  9106. else
  9107. err = xa_insert(&net->dev_by_index, ifindex, NULL, GFP_KERNEL);
  9108. if (err < 0)
  9109. return err;
  9110. return ifindex;
  9111. }
  9112. static void dev_index_release(struct net *net, int ifindex)
  9113. {
  9114. /* Expect only unused indexes, unlist_netdevice() removes the used */
  9115. WARN_ON(xa_erase(&net->dev_by_index, ifindex));
  9116. }
  9117. static bool from_cleanup_net(void)
  9118. {
  9119. #ifdef CONFIG_NET_NS
  9120. return current == READ_ONCE(cleanup_net_task);
  9121. #else
  9122. return false;
  9123. #endif
  9124. }
  9125. /* Delayed registration/unregisteration */
  9126. LIST_HEAD(net_todo_list);
  9127. DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
  9128. atomic_t dev_unreg_count = ATOMIC_INIT(0);
  9129. static void net_set_todo(struct net_device *dev)
  9130. {
  9131. list_add_tail(&dev->todo_list, &net_todo_list);
  9132. }
  9133. static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
  9134. struct net_device *upper, netdev_features_t features)
  9135. {
  9136. netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
  9137. netdev_features_t feature;
  9138. int feature_bit;
  9139. for_each_netdev_feature(upper_disables, feature_bit) {
  9140. feature = __NETIF_F_BIT(feature_bit);
  9141. if (!(upper->wanted_features & feature)
  9142. && (features & feature)) {
  9143. netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
  9144. &feature, upper->name);
  9145. features &= ~feature;
  9146. }
  9147. }
  9148. return features;
  9149. }
  9150. static void netdev_sync_lower_features(struct net_device *upper,
  9151. struct net_device *lower, netdev_features_t features)
  9152. {
  9153. netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
  9154. netdev_features_t feature;
  9155. int feature_bit;
  9156. for_each_netdev_feature(upper_disables, feature_bit) {
  9157. feature = __NETIF_F_BIT(feature_bit);
  9158. if (!(features & feature) && (lower->features & feature)) {
  9159. netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
  9160. &feature, lower->name);
  9161. netdev_lock_ops(lower);
  9162. lower->wanted_features &= ~feature;
  9163. __netdev_update_features(lower);
  9164. if (unlikely(lower->features & feature))
  9165. netdev_WARN(upper, "failed to disable %pNF on %s!\n",
  9166. &feature, lower->name);
  9167. else
  9168. netdev_features_change(lower);
  9169. netdev_unlock_ops(lower);
  9170. }
  9171. }
  9172. }
  9173. static bool netdev_has_ip_or_hw_csum(netdev_features_t features)
  9174. {
  9175. netdev_features_t ip_csum_mask = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  9176. bool ip_csum = (features & ip_csum_mask) == ip_csum_mask;
  9177. bool hw_csum = features & NETIF_F_HW_CSUM;
  9178. return ip_csum || hw_csum;
  9179. }
  9180. static netdev_features_t netdev_fix_features(struct net_device *dev,
  9181. netdev_features_t features)
  9182. {
  9183. /* Fix illegal checksum combinations */
  9184. if ((features & NETIF_F_HW_CSUM) &&
  9185. (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  9186. netdev_warn(dev, "mixed HW and IP checksum settings.\n");
  9187. features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
  9188. }
  9189. /* TSO requires that SG is present as well. */
  9190. if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
  9191. netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
  9192. features &= ~NETIF_F_ALL_TSO;
  9193. }
  9194. if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
  9195. !(features & NETIF_F_IP_CSUM)) {
  9196. netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
  9197. features &= ~NETIF_F_TSO;
  9198. features &= ~NETIF_F_TSO_ECN;
  9199. }
  9200. if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
  9201. !(features & NETIF_F_IPV6_CSUM)) {
  9202. netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
  9203. features &= ~NETIF_F_TSO6;
  9204. }
  9205. /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
  9206. if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
  9207. features &= ~NETIF_F_TSO_MANGLEID;
  9208. /* TSO ECN requires that TSO is present as well. */
  9209. if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
  9210. features &= ~NETIF_F_TSO_ECN;
  9211. /* Software GSO depends on SG. */
  9212. if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
  9213. netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
  9214. features &= ~NETIF_F_GSO;
  9215. }
  9216. /* GSO partial features require GSO partial be set */
  9217. if ((features & dev->gso_partial_features) &&
  9218. !(features & NETIF_F_GSO_PARTIAL)) {
  9219. netdev_dbg(dev,
  9220. "Dropping partially supported GSO features since no GSO partial.\n");
  9221. features &= ~dev->gso_partial_features;
  9222. }
  9223. if (!(features & NETIF_F_RXCSUM)) {
  9224. /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
  9225. * successfully merged by hardware must also have the
  9226. * checksum verified by hardware. If the user does not
  9227. * want to enable RXCSUM, logically, we should disable GRO_HW.
  9228. */
  9229. if (features & NETIF_F_GRO_HW) {
  9230. netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
  9231. features &= ~NETIF_F_GRO_HW;
  9232. }
  9233. }
  9234. /* LRO/HW-GRO features cannot be combined with RX-FCS */
  9235. if (features & NETIF_F_RXFCS) {
  9236. if (features & NETIF_F_LRO) {
  9237. netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
  9238. features &= ~NETIF_F_LRO;
  9239. }
  9240. if (features & NETIF_F_GRO_HW) {
  9241. netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
  9242. features &= ~NETIF_F_GRO_HW;
  9243. }
  9244. }
  9245. if ((features & NETIF_F_GRO_HW) && (features & NETIF_F_LRO)) {
  9246. netdev_dbg(dev, "Dropping LRO feature since HW-GRO is requested.\n");
  9247. features &= ~NETIF_F_LRO;
  9248. }
  9249. if ((features & NETIF_F_HW_TLS_TX) && !netdev_has_ip_or_hw_csum(features)) {
  9250. netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n");
  9251. features &= ~NETIF_F_HW_TLS_TX;
  9252. }
  9253. if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) {
  9254. netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n");
  9255. features &= ~NETIF_F_HW_TLS_RX;
  9256. }
  9257. if ((features & NETIF_F_GSO_UDP_L4) && !netdev_has_ip_or_hw_csum(features)) {
  9258. netdev_dbg(dev, "Dropping USO feature since no CSUM feature.\n");
  9259. features &= ~NETIF_F_GSO_UDP_L4;
  9260. }
  9261. return features;
  9262. }
  9263. int __netdev_update_features(struct net_device *dev)
  9264. {
  9265. struct net_device *upper, *lower;
  9266. netdev_features_t features;
  9267. struct list_head *iter;
  9268. int err = -1;
  9269. ASSERT_RTNL();
  9270. netdev_ops_assert_locked(dev);
  9271. features = netdev_get_wanted_features(dev);
  9272. if (dev->netdev_ops->ndo_fix_features)
  9273. features = dev->netdev_ops->ndo_fix_features(dev, features);
  9274. /* driver might be less strict about feature dependencies */
  9275. features = netdev_fix_features(dev, features);
  9276. /* some features can't be enabled if they're off on an upper device */
  9277. netdev_for_each_upper_dev_rcu(dev, upper, iter)
  9278. features = netdev_sync_upper_features(dev, upper, features);
  9279. if (dev->features == features)
  9280. goto sync_lower;
  9281. netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
  9282. &dev->features, &features);
  9283. if (dev->netdev_ops->ndo_set_features)
  9284. err = dev->netdev_ops->ndo_set_features(dev, features);
  9285. else
  9286. err = 0;
  9287. if (unlikely(err < 0)) {
  9288. netdev_err(dev,
  9289. "set_features() failed (%d); wanted %pNF, left %pNF\n",
  9290. err, &features, &dev->features);
  9291. /* return non-0 since some features might have changed and
  9292. * it's better to fire a spurious notification than miss it
  9293. */
  9294. return -1;
  9295. }
  9296. sync_lower:
  9297. /* some features must be disabled on lower devices when disabled
  9298. * on an upper device (think: bonding master or bridge)
  9299. */
  9300. netdev_for_each_lower_dev(dev, lower, iter)
  9301. netdev_sync_lower_features(dev, lower, features);
  9302. if (!err) {
  9303. netdev_features_t diff = features ^ dev->features;
  9304. if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
  9305. /* udp_tunnel_{get,drop}_rx_info both need
  9306. * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
  9307. * device, or they won't do anything.
  9308. * Thus we need to update dev->features
  9309. * *before* calling udp_tunnel_get_rx_info,
  9310. * but *after* calling udp_tunnel_drop_rx_info.
  9311. */
  9312. udp_tunnel_nic_lock(dev);
  9313. if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
  9314. dev->features = features;
  9315. udp_tunnel_get_rx_info(dev);
  9316. } else {
  9317. udp_tunnel_drop_rx_info(dev);
  9318. }
  9319. udp_tunnel_nic_unlock(dev);
  9320. }
  9321. if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
  9322. if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
  9323. dev->features = features;
  9324. err |= vlan_get_rx_ctag_filter_info(dev);
  9325. } else {
  9326. vlan_drop_rx_ctag_filter_info(dev);
  9327. }
  9328. }
  9329. if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
  9330. if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
  9331. dev->features = features;
  9332. err |= vlan_get_rx_stag_filter_info(dev);
  9333. } else {
  9334. vlan_drop_rx_stag_filter_info(dev);
  9335. }
  9336. }
  9337. dev->features = features;
  9338. }
  9339. return err < 0 ? 0 : 1;
  9340. }
  9341. /**
  9342. * netdev_update_features - recalculate device features
  9343. * @dev: the device to check
  9344. *
  9345. * Recalculate dev->features set and send notifications if it
  9346. * has changed. Should be called after driver or hardware dependent
  9347. * conditions might have changed that influence the features.
  9348. */
  9349. void netdev_update_features(struct net_device *dev)
  9350. {
  9351. if (__netdev_update_features(dev))
  9352. netdev_features_change(dev);
  9353. }
  9354. EXPORT_SYMBOL(netdev_update_features);
  9355. /**
  9356. * netdev_change_features - recalculate device features
  9357. * @dev: the device to check
  9358. *
  9359. * Recalculate dev->features set and send notifications even
  9360. * if they have not changed. Should be called instead of
  9361. * netdev_update_features() if also dev->vlan_features might
  9362. * have changed to allow the changes to be propagated to stacked
  9363. * VLAN devices.
  9364. */
  9365. void netdev_change_features(struct net_device *dev)
  9366. {
  9367. __netdev_update_features(dev);
  9368. netdev_features_change(dev);
  9369. }
  9370. EXPORT_SYMBOL(netdev_change_features);
  9371. /**
  9372. * netif_stacked_transfer_operstate - transfer operstate
  9373. * @rootdev: the root or lower level device to transfer state from
  9374. * @dev: the device to transfer operstate to
  9375. *
  9376. * Transfer operational state from root to device. This is normally
  9377. * called when a stacking relationship exists between the root
  9378. * device and the device(a leaf device).
  9379. */
  9380. void netif_stacked_transfer_operstate(const struct net_device *rootdev,
  9381. struct net_device *dev)
  9382. {
  9383. if (rootdev->operstate == IF_OPER_DORMANT)
  9384. netif_dormant_on(dev);
  9385. else
  9386. netif_dormant_off(dev);
  9387. if (rootdev->operstate == IF_OPER_TESTING)
  9388. netif_testing_on(dev);
  9389. else
  9390. netif_testing_off(dev);
  9391. if (netif_carrier_ok(rootdev))
  9392. netif_carrier_on(dev);
  9393. else
  9394. netif_carrier_off(dev);
  9395. }
  9396. EXPORT_SYMBOL(netif_stacked_transfer_operstate);
  9397. static int netif_alloc_rx_queues(struct net_device *dev)
  9398. {
  9399. unsigned int i, count = dev->num_rx_queues;
  9400. struct netdev_rx_queue *rx;
  9401. size_t sz = count * sizeof(*rx);
  9402. int err = 0;
  9403. BUG_ON(count < 1);
  9404. rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
  9405. if (!rx)
  9406. return -ENOMEM;
  9407. dev->_rx = rx;
  9408. for (i = 0; i < count; i++) {
  9409. rx[i].dev = dev;
  9410. /* XDP RX-queue setup */
  9411. err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0);
  9412. if (err < 0)
  9413. goto err_rxq_info;
  9414. }
  9415. return 0;
  9416. err_rxq_info:
  9417. /* Rollback successful reg's and free other resources */
  9418. while (i--)
  9419. xdp_rxq_info_unreg(&rx[i].xdp_rxq);
  9420. kvfree(dev->_rx);
  9421. dev->_rx = NULL;
  9422. return err;
  9423. }
  9424. static void netif_free_rx_queues(struct net_device *dev)
  9425. {
  9426. unsigned int i, count = dev->num_rx_queues;
  9427. /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
  9428. if (!dev->_rx)
  9429. return;
  9430. for (i = 0; i < count; i++)
  9431. xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
  9432. kvfree(dev->_rx);
  9433. }
  9434. static void netdev_init_one_queue(struct net_device *dev,
  9435. struct netdev_queue *queue, void *_unused)
  9436. {
  9437. /* Initialize queue lock */
  9438. spin_lock_init(&queue->_xmit_lock);
  9439. netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
  9440. queue->xmit_lock_owner = -1;
  9441. netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
  9442. queue->dev = dev;
  9443. #ifdef CONFIG_BQL
  9444. dql_init(&queue->dql, HZ);
  9445. #endif
  9446. }
  9447. static void netif_free_tx_queues(struct net_device *dev)
  9448. {
  9449. kvfree(dev->_tx);
  9450. }
  9451. static int netif_alloc_netdev_queues(struct net_device *dev)
  9452. {
  9453. unsigned int count = dev->num_tx_queues;
  9454. struct netdev_queue *tx;
  9455. size_t sz = count * sizeof(*tx);
  9456. if (count < 1 || count > 0xffff)
  9457. return -EINVAL;
  9458. tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
  9459. if (!tx)
  9460. return -ENOMEM;
  9461. dev->_tx = tx;
  9462. netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
  9463. spin_lock_init(&dev->tx_global_lock);
  9464. return 0;
  9465. }
  9466. void netif_tx_stop_all_queues(struct net_device *dev)
  9467. {
  9468. unsigned int i;
  9469. for (i = 0; i < dev->num_tx_queues; i++) {
  9470. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  9471. netif_tx_stop_queue(txq);
  9472. }
  9473. }
  9474. EXPORT_SYMBOL(netif_tx_stop_all_queues);
  9475. static int netdev_do_alloc_pcpu_stats(struct net_device *dev)
  9476. {
  9477. void __percpu *v;
  9478. /* Drivers implementing ndo_get_peer_dev must support tstat
  9479. * accounting, so that skb_do_redirect() can bump the dev's
  9480. * RX stats upon network namespace switch.
  9481. */
  9482. if (dev->netdev_ops->ndo_get_peer_dev &&
  9483. dev->pcpu_stat_type != NETDEV_PCPU_STAT_TSTATS)
  9484. return -EOPNOTSUPP;
  9485. switch (dev->pcpu_stat_type) {
  9486. case NETDEV_PCPU_STAT_NONE:
  9487. return 0;
  9488. case NETDEV_PCPU_STAT_LSTATS:
  9489. v = dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
  9490. break;
  9491. case NETDEV_PCPU_STAT_TSTATS:
  9492. v = dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  9493. break;
  9494. case NETDEV_PCPU_STAT_DSTATS:
  9495. v = dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
  9496. break;
  9497. default:
  9498. return -EINVAL;
  9499. }
  9500. return v ? 0 : -ENOMEM;
  9501. }
  9502. static void netdev_do_free_pcpu_stats(struct net_device *dev)
  9503. {
  9504. switch (dev->pcpu_stat_type) {
  9505. case NETDEV_PCPU_STAT_NONE:
  9506. return;
  9507. case NETDEV_PCPU_STAT_LSTATS:
  9508. free_percpu(dev->lstats);
  9509. break;
  9510. case NETDEV_PCPU_STAT_TSTATS:
  9511. free_percpu(dev->tstats);
  9512. break;
  9513. case NETDEV_PCPU_STAT_DSTATS:
  9514. free_percpu(dev->dstats);
  9515. break;
  9516. }
  9517. }
  9518. static void netdev_free_phy_link_topology(struct net_device *dev)
  9519. {
  9520. struct phy_link_topology *topo = dev->link_topo;
  9521. if (IS_ENABLED(CONFIG_PHYLIB) && topo) {
  9522. xa_destroy(&topo->phys);
  9523. kfree(topo);
  9524. dev->link_topo = NULL;
  9525. }
  9526. }
  9527. /**
  9528. * register_netdevice() - register a network device
  9529. * @dev: device to register
  9530. *
  9531. * Take a prepared network device structure and make it externally accessible.
  9532. * A %NETDEV_REGISTER message is sent to the netdev notifier chain.
  9533. * Callers must hold the rtnl lock - you may want register_netdev()
  9534. * instead of this.
  9535. */
  9536. int register_netdevice(struct net_device *dev)
  9537. {
  9538. int ret;
  9539. struct net *net = dev_net(dev);
  9540. BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
  9541. NETDEV_FEATURE_COUNT);
  9542. BUG_ON(dev_boot_phase);
  9543. ASSERT_RTNL();
  9544. might_sleep();
  9545. /* When net_device's are persistent, this will be fatal. */
  9546. BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
  9547. BUG_ON(!net);
  9548. ret = ethtool_check_ops(dev->ethtool_ops);
  9549. if (ret)
  9550. return ret;
  9551. /* rss ctx ID 0 is reserved for the default context, start from 1 */
  9552. xa_init_flags(&dev->ethtool->rss_ctx, XA_FLAGS_ALLOC1);
  9553. mutex_init(&dev->ethtool->rss_lock);
  9554. spin_lock_init(&dev->addr_list_lock);
  9555. netdev_set_addr_lockdep_class(dev);
  9556. ret = dev_get_valid_name(net, dev, dev->name);
  9557. if (ret < 0)
  9558. goto out;
  9559. ret = -ENOMEM;
  9560. dev->name_node = netdev_name_node_head_alloc(dev);
  9561. if (!dev->name_node)
  9562. goto out;
  9563. /* Init, if this function is available */
  9564. if (dev->netdev_ops->ndo_init) {
  9565. ret = dev->netdev_ops->ndo_init(dev);
  9566. if (ret) {
  9567. if (ret > 0)
  9568. ret = -EIO;
  9569. goto err_free_name;
  9570. }
  9571. }
  9572. if (((dev->hw_features | dev->features) &
  9573. NETIF_F_HW_VLAN_CTAG_FILTER) &&
  9574. (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
  9575. !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
  9576. netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
  9577. ret = -EINVAL;
  9578. goto err_uninit;
  9579. }
  9580. ret = netdev_do_alloc_pcpu_stats(dev);
  9581. if (ret)
  9582. goto err_uninit;
  9583. ret = dev_index_reserve(net, dev->ifindex);
  9584. if (ret < 0)
  9585. goto err_free_pcpu;
  9586. dev->ifindex = ret;
  9587. /* Transfer changeable features to wanted_features and enable
  9588. * software offloads (GSO and GRO).
  9589. */
  9590. dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
  9591. dev->features |= NETIF_F_SOFT_FEATURES;
  9592. if (dev->udp_tunnel_nic_info) {
  9593. dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
  9594. dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
  9595. }
  9596. dev->wanted_features = dev->features & dev->hw_features;
  9597. if (!(dev->flags & IFF_LOOPBACK))
  9598. dev->hw_features |= NETIF_F_NOCACHE_COPY;
  9599. /* If IPv4 TCP segmentation offload is supported we should also
  9600. * allow the device to enable segmenting the frame with the option
  9601. * of ignoring a static IP ID value. This doesn't enable the
  9602. * feature itself but allows the user to enable it later.
  9603. */
  9604. if (dev->hw_features & NETIF_F_TSO)
  9605. dev->hw_features |= NETIF_F_TSO_MANGLEID;
  9606. if (dev->vlan_features & NETIF_F_TSO)
  9607. dev->vlan_features |= NETIF_F_TSO_MANGLEID;
  9608. if (dev->mpls_features & NETIF_F_TSO)
  9609. dev->mpls_features |= NETIF_F_TSO_MANGLEID;
  9610. if (dev->hw_enc_features & NETIF_F_TSO)
  9611. dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  9612. /* TSO_MANGLEID belongs in mangleid_features by definition */
  9613. dev->mangleid_features |= NETIF_F_TSO_MANGLEID;
  9614. /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
  9615. */
  9616. dev->vlan_features |= NETIF_F_HIGHDMA;
  9617. /* Make NETIF_F_SG inheritable to tunnel devices.
  9618. */
  9619. dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
  9620. /* Make NETIF_F_SG inheritable to MPLS.
  9621. */
  9622. dev->mpls_features |= NETIF_F_SG;
  9623. ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
  9624. ret = notifier_to_errno(ret);
  9625. if (ret)
  9626. goto err_ifindex_release;
  9627. ret = netdev_register_kobject(dev);
  9628. netdev_lock(dev);
  9629. WRITE_ONCE(dev->reg_state, ret ? NETREG_UNREGISTERED : NETREG_REGISTERED);
  9630. netdev_unlock(dev);
  9631. if (ret)
  9632. goto err_uninit_notify;
  9633. netdev_lock_ops(dev);
  9634. __netdev_update_features(dev);
  9635. netdev_unlock_ops(dev);
  9636. /*
  9637. * Default initial state at registry is that the
  9638. * device is present.
  9639. */
  9640. set_bit(__LINK_STATE_PRESENT, &dev->state);
  9641. linkwatch_init_dev(dev);
  9642. dev_init_scheduler(dev);
  9643. netdev_hold(dev, &dev->dev_registered_tracker, GFP_KERNEL);
  9644. list_netdevice(dev);
  9645. add_device_randomness(dev->dev_addr, dev->addr_len);
  9646. /* If the device has permanent device address, driver should
  9647. * set dev_addr and also addr_assign_type should be set to
  9648. * NET_ADDR_PERM (default value).
  9649. */
  9650. if (dev->addr_assign_type == NET_ADDR_PERM)
  9651. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  9652. /* Notify protocols, that a new device appeared. */
  9653. netdev_lock_ops(dev);
  9654. ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
  9655. netdev_unlock_ops(dev);
  9656. ret = notifier_to_errno(ret);
  9657. if (ret) {
  9658. /* Expect explicit free_netdev() on failure */
  9659. dev->needs_free_netdev = false;
  9660. unregister_netdevice_queue(dev, NULL);
  9661. goto out;
  9662. }
  9663. /*
  9664. * Prevent userspace races by waiting until the network
  9665. * device is fully setup before sending notifications.
  9666. */
  9667. if (!(dev->rtnl_link_ops && dev->rtnl_link_initializing))
  9668. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL, 0, NULL);
  9669. out:
  9670. return ret;
  9671. err_uninit_notify:
  9672. call_netdevice_notifiers(NETDEV_PRE_UNINIT, dev);
  9673. err_ifindex_release:
  9674. dev_index_release(net, dev->ifindex);
  9675. err_free_pcpu:
  9676. netdev_do_free_pcpu_stats(dev);
  9677. err_uninit:
  9678. if (dev->netdev_ops->ndo_uninit)
  9679. dev->netdev_ops->ndo_uninit(dev);
  9680. if (dev->priv_destructor)
  9681. dev->priv_destructor(dev);
  9682. err_free_name:
  9683. netdev_name_node_free(dev->name_node);
  9684. goto out;
  9685. }
  9686. EXPORT_SYMBOL(register_netdevice);
  9687. /* Initialize the core of a dummy net device.
  9688. * The setup steps dummy netdevs need which normal netdevs get by going
  9689. * through register_netdevice().
  9690. */
  9691. static void init_dummy_netdev(struct net_device *dev)
  9692. {
  9693. /* make sure we BUG if trying to hit standard
  9694. * register/unregister code path
  9695. */
  9696. dev->reg_state = NETREG_DUMMY;
  9697. /* a dummy interface is started by default */
  9698. set_bit(__LINK_STATE_PRESENT, &dev->state);
  9699. set_bit(__LINK_STATE_START, &dev->state);
  9700. /* Note : We dont allocate pcpu_refcnt for dummy devices,
  9701. * because users of this 'device' dont need to change
  9702. * its refcount.
  9703. */
  9704. }
  9705. /**
  9706. * register_netdev - register a network device
  9707. * @dev: device to register
  9708. *
  9709. * Take a completed network device structure and add it to the kernel
  9710. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  9711. * chain. 0 is returned on success. A negative errno code is returned
  9712. * on a failure to set up the device, or if the name is a duplicate.
  9713. *
  9714. * This is a wrapper around register_netdevice that takes the rtnl semaphore
  9715. * and expands the device name if you passed a format string to
  9716. * alloc_netdev.
  9717. */
  9718. int register_netdev(struct net_device *dev)
  9719. {
  9720. struct net *net = dev_net(dev);
  9721. int err;
  9722. if (rtnl_net_lock_killable(net))
  9723. return -EINTR;
  9724. err = register_netdevice(dev);
  9725. rtnl_net_unlock(net);
  9726. return err;
  9727. }
  9728. EXPORT_SYMBOL(register_netdev);
  9729. int netdev_refcnt_read(const struct net_device *dev)
  9730. {
  9731. #ifdef CONFIG_PCPU_DEV_REFCNT
  9732. int i, refcnt = 0;
  9733. for_each_possible_cpu(i)
  9734. refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
  9735. return refcnt;
  9736. #else
  9737. return refcount_read(&dev->dev_refcnt);
  9738. #endif
  9739. }
  9740. EXPORT_SYMBOL(netdev_refcnt_read);
  9741. int netdev_unregister_timeout_secs __read_mostly = 10;
  9742. #define WAIT_REFS_MIN_MSECS 1
  9743. #define WAIT_REFS_MAX_MSECS 250
  9744. /**
  9745. * netdev_wait_allrefs_any - wait until all references are gone.
  9746. * @list: list of net_devices to wait on
  9747. *
  9748. * This is called when unregistering network devices.
  9749. *
  9750. * Any protocol or device that holds a reference should register
  9751. * for netdevice notification, and cleanup and put back the
  9752. * reference if they receive an UNREGISTER event.
  9753. * We can get stuck here if buggy protocols don't correctly
  9754. * call dev_put.
  9755. */
  9756. static struct net_device *netdev_wait_allrefs_any(struct list_head *list)
  9757. {
  9758. unsigned long rebroadcast_time, warning_time;
  9759. struct net_device *dev;
  9760. int wait = 0;
  9761. rebroadcast_time = warning_time = jiffies;
  9762. list_for_each_entry(dev, list, todo_list)
  9763. if (netdev_refcnt_read(dev) == 1)
  9764. return dev;
  9765. while (true) {
  9766. if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
  9767. rtnl_lock();
  9768. /* Rebroadcast unregister notification */
  9769. list_for_each_entry(dev, list, todo_list)
  9770. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  9771. __rtnl_unlock();
  9772. rcu_barrier();
  9773. rtnl_lock();
  9774. list_for_each_entry(dev, list, todo_list)
  9775. if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
  9776. &dev->state)) {
  9777. /* We must not have linkwatch events
  9778. * pending on unregister. If this
  9779. * happens, we simply run the queue
  9780. * unscheduled, resulting in a noop
  9781. * for this device.
  9782. */
  9783. linkwatch_run_queue();
  9784. break;
  9785. }
  9786. __rtnl_unlock();
  9787. rebroadcast_time = jiffies;
  9788. }
  9789. rcu_barrier();
  9790. if (!wait) {
  9791. wait = WAIT_REFS_MIN_MSECS;
  9792. } else {
  9793. msleep(wait);
  9794. wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
  9795. }
  9796. list_for_each_entry(dev, list, todo_list)
  9797. if (netdev_refcnt_read(dev) == 1)
  9798. return dev;
  9799. if (time_after(jiffies, warning_time +
  9800. READ_ONCE(netdev_unregister_timeout_secs) * HZ)) {
  9801. list_for_each_entry(dev, list, todo_list) {
  9802. pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
  9803. dev->name, netdev_refcnt_read(dev));
  9804. ref_tracker_dir_print(&dev->refcnt_tracker, 10);
  9805. }
  9806. warning_time = jiffies;
  9807. }
  9808. }
  9809. }
  9810. /* The sequence is:
  9811. *
  9812. * rtnl_lock();
  9813. * ...
  9814. * register_netdevice(x1);
  9815. * register_netdevice(x2);
  9816. * ...
  9817. * unregister_netdevice(y1);
  9818. * unregister_netdevice(y2);
  9819. * ...
  9820. * rtnl_unlock();
  9821. * free_netdev(y1);
  9822. * free_netdev(y2);
  9823. *
  9824. * We are invoked by rtnl_unlock().
  9825. * This allows us to deal with problems:
  9826. * 1) We can delete sysfs objects which invoke hotplug
  9827. * without deadlocking with linkwatch via keventd.
  9828. * 2) Since we run with the RTNL semaphore not held, we can sleep
  9829. * safely in order to wait for the netdev refcnt to drop to zero.
  9830. *
  9831. * We must not return until all unregister events added during
  9832. * the interval the lock was held have been completed.
  9833. */
  9834. void netdev_run_todo(void)
  9835. {
  9836. struct net_device *dev, *tmp;
  9837. struct list_head list;
  9838. int cnt;
  9839. #ifdef CONFIG_LOCKDEP
  9840. struct list_head unlink_list;
  9841. list_replace_init(&net_unlink_list, &unlink_list);
  9842. while (!list_empty(&unlink_list)) {
  9843. dev = list_first_entry(&unlink_list, struct net_device,
  9844. unlink_list);
  9845. list_del_init(&dev->unlink_list);
  9846. dev->nested_level = dev->lower_level - 1;
  9847. }
  9848. #endif
  9849. /* Snapshot list, allow later requests */
  9850. list_replace_init(&net_todo_list, &list);
  9851. __rtnl_unlock();
  9852. /* Wait for rcu callbacks to finish before next phase */
  9853. if (!list_empty(&list))
  9854. rcu_barrier();
  9855. list_for_each_entry_safe(dev, tmp, &list, todo_list) {
  9856. if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
  9857. netdev_WARN(dev, "run_todo but not unregistering\n");
  9858. list_del(&dev->todo_list);
  9859. continue;
  9860. }
  9861. netdev_lock(dev);
  9862. WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERED);
  9863. netdev_unlock(dev);
  9864. linkwatch_sync_dev(dev);
  9865. }
  9866. cnt = 0;
  9867. while (!list_empty(&list)) {
  9868. dev = netdev_wait_allrefs_any(&list);
  9869. list_del(&dev->todo_list);
  9870. /* paranoia */
  9871. BUG_ON(netdev_refcnt_read(dev) != 1);
  9872. BUG_ON(!list_empty(&dev->ptype_all));
  9873. BUG_ON(!list_empty(&dev->ptype_specific));
  9874. WARN_ON(rcu_access_pointer(dev->ip_ptr));
  9875. WARN_ON(rcu_access_pointer(dev->ip6_ptr));
  9876. netdev_do_free_pcpu_stats(dev);
  9877. if (dev->priv_destructor)
  9878. dev->priv_destructor(dev);
  9879. if (dev->needs_free_netdev)
  9880. free_netdev(dev);
  9881. cnt++;
  9882. /* Free network device */
  9883. kobject_put(&dev->dev.kobj);
  9884. }
  9885. if (cnt && atomic_sub_and_test(cnt, &dev_unreg_count))
  9886. wake_up(&netdev_unregistering_wq);
  9887. }
  9888. /* Collate per-cpu network dstats statistics
  9889. *
  9890. * Read per-cpu network statistics from dev->dstats and populate the related
  9891. * fields in @s.
  9892. */
  9893. static void dev_fetch_dstats(struct rtnl_link_stats64 *s,
  9894. const struct pcpu_dstats __percpu *dstats)
  9895. {
  9896. int cpu;
  9897. for_each_possible_cpu(cpu) {
  9898. u64 rx_packets, rx_bytes, rx_drops;
  9899. u64 tx_packets, tx_bytes, tx_drops;
  9900. const struct pcpu_dstats *stats;
  9901. unsigned int start;
  9902. stats = per_cpu_ptr(dstats, cpu);
  9903. do {
  9904. start = u64_stats_fetch_begin(&stats->syncp);
  9905. rx_packets = u64_stats_read(&stats->rx_packets);
  9906. rx_bytes = u64_stats_read(&stats->rx_bytes);
  9907. rx_drops = u64_stats_read(&stats->rx_drops);
  9908. tx_packets = u64_stats_read(&stats->tx_packets);
  9909. tx_bytes = u64_stats_read(&stats->tx_bytes);
  9910. tx_drops = u64_stats_read(&stats->tx_drops);
  9911. } while (u64_stats_fetch_retry(&stats->syncp, start));
  9912. s->rx_packets += rx_packets;
  9913. s->rx_bytes += rx_bytes;
  9914. s->rx_dropped += rx_drops;
  9915. s->tx_packets += tx_packets;
  9916. s->tx_bytes += tx_bytes;
  9917. s->tx_dropped += tx_drops;
  9918. }
  9919. }
  9920. /* ndo_get_stats64 implementation for dtstats-based accounting.
  9921. *
  9922. * Populate @s from dev->stats and dev->dstats. This is used internally by the
  9923. * core for NETDEV_PCPU_STAT_DSTAT-type stats collection.
  9924. */
  9925. static void dev_get_dstats64(const struct net_device *dev,
  9926. struct rtnl_link_stats64 *s)
  9927. {
  9928. netdev_stats_to_stats64(s, &dev->stats);
  9929. dev_fetch_dstats(s, dev->dstats);
  9930. }
  9931. /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
  9932. * all the same fields in the same order as net_device_stats, with only
  9933. * the type differing, but rtnl_link_stats64 may have additional fields
  9934. * at the end for newer counters.
  9935. */
  9936. void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
  9937. const struct net_device_stats *netdev_stats)
  9938. {
  9939. size_t i, n = sizeof(*netdev_stats) / sizeof(atomic_long_t);
  9940. const atomic_long_t *src = (atomic_long_t *)netdev_stats;
  9941. u64 *dst = (u64 *)stats64;
  9942. BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
  9943. for (i = 0; i < n; i++)
  9944. dst[i] = (unsigned long)atomic_long_read(&src[i]);
  9945. /* zero out counters that only exist in rtnl_link_stats64 */
  9946. memset((char *)stats64 + n * sizeof(u64), 0,
  9947. sizeof(*stats64) - n * sizeof(u64));
  9948. }
  9949. EXPORT_SYMBOL(netdev_stats_to_stats64);
  9950. static __cold struct net_device_core_stats __percpu *netdev_core_stats_alloc(
  9951. struct net_device *dev)
  9952. {
  9953. struct net_device_core_stats __percpu *p;
  9954. p = alloc_percpu_gfp(struct net_device_core_stats,
  9955. GFP_ATOMIC | __GFP_NOWARN);
  9956. if (p && cmpxchg(&dev->core_stats, NULL, p))
  9957. free_percpu(p);
  9958. /* This READ_ONCE() pairs with the cmpxchg() above */
  9959. return READ_ONCE(dev->core_stats);
  9960. }
  9961. noinline void netdev_core_stats_inc(struct net_device *dev, u32 offset)
  9962. {
  9963. /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
  9964. struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
  9965. unsigned long __percpu *field;
  9966. if (unlikely(!p)) {
  9967. p = netdev_core_stats_alloc(dev);
  9968. if (!p)
  9969. return;
  9970. }
  9971. field = (unsigned long __percpu *)((void __percpu *)p + offset);
  9972. this_cpu_inc(*field);
  9973. }
  9974. EXPORT_SYMBOL_GPL(netdev_core_stats_inc);
  9975. /**
  9976. * dev_get_stats - get network device statistics
  9977. * @dev: device to get statistics from
  9978. * @storage: place to store stats
  9979. *
  9980. * Get network statistics from device. Return @storage.
  9981. * The device driver may provide its own method by setting
  9982. * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
  9983. * otherwise the internal statistics structure is used.
  9984. */
  9985. struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
  9986. struct rtnl_link_stats64 *storage)
  9987. {
  9988. const struct net_device_ops *ops = dev->netdev_ops;
  9989. const struct net_device_core_stats __percpu *p;
  9990. /*
  9991. * IPv{4,6} and udp tunnels share common stat helpers and use
  9992. * different stat type (NETDEV_PCPU_STAT_TSTATS vs
  9993. * NETDEV_PCPU_STAT_DSTATS). Ensure the accounting is consistent.
  9994. */
  9995. BUILD_BUG_ON(offsetof(struct pcpu_sw_netstats, rx_bytes) !=
  9996. offsetof(struct pcpu_dstats, rx_bytes));
  9997. BUILD_BUG_ON(offsetof(struct pcpu_sw_netstats, rx_packets) !=
  9998. offsetof(struct pcpu_dstats, rx_packets));
  9999. BUILD_BUG_ON(offsetof(struct pcpu_sw_netstats, tx_bytes) !=
  10000. offsetof(struct pcpu_dstats, tx_bytes));
  10001. BUILD_BUG_ON(offsetof(struct pcpu_sw_netstats, tx_packets) !=
  10002. offsetof(struct pcpu_dstats, tx_packets));
  10003. if (ops->ndo_get_stats64) {
  10004. memset(storage, 0, sizeof(*storage));
  10005. ops->ndo_get_stats64(dev, storage);
  10006. } else if (ops->ndo_get_stats) {
  10007. netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
  10008. } else if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_TSTATS) {
  10009. dev_get_tstats64(dev, storage);
  10010. } else if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_DSTATS) {
  10011. dev_get_dstats64(dev, storage);
  10012. } else {
  10013. netdev_stats_to_stats64(storage, &dev->stats);
  10014. }
  10015. /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
  10016. p = READ_ONCE(dev->core_stats);
  10017. if (p) {
  10018. const struct net_device_core_stats *core_stats;
  10019. int i;
  10020. for_each_possible_cpu(i) {
  10021. core_stats = per_cpu_ptr(p, i);
  10022. storage->rx_dropped += READ_ONCE(core_stats->rx_dropped);
  10023. storage->tx_dropped += READ_ONCE(core_stats->tx_dropped);
  10024. storage->rx_nohandler += READ_ONCE(core_stats->rx_nohandler);
  10025. storage->rx_otherhost_dropped += READ_ONCE(core_stats->rx_otherhost_dropped);
  10026. }
  10027. }
  10028. return storage;
  10029. }
  10030. EXPORT_SYMBOL(dev_get_stats);
  10031. /**
  10032. * dev_fetch_sw_netstats - get per-cpu network device statistics
  10033. * @s: place to store stats
  10034. * @netstats: per-cpu network stats to read from
  10035. *
  10036. * Read per-cpu network statistics and populate the related fields in @s.
  10037. */
  10038. void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
  10039. const struct pcpu_sw_netstats __percpu *netstats)
  10040. {
  10041. int cpu;
  10042. for_each_possible_cpu(cpu) {
  10043. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  10044. const struct pcpu_sw_netstats *stats;
  10045. unsigned int start;
  10046. stats = per_cpu_ptr(netstats, cpu);
  10047. do {
  10048. start = u64_stats_fetch_begin(&stats->syncp);
  10049. rx_packets = u64_stats_read(&stats->rx_packets);
  10050. rx_bytes = u64_stats_read(&stats->rx_bytes);
  10051. tx_packets = u64_stats_read(&stats->tx_packets);
  10052. tx_bytes = u64_stats_read(&stats->tx_bytes);
  10053. } while (u64_stats_fetch_retry(&stats->syncp, start));
  10054. s->rx_packets += rx_packets;
  10055. s->rx_bytes += rx_bytes;
  10056. s->tx_packets += tx_packets;
  10057. s->tx_bytes += tx_bytes;
  10058. }
  10059. }
  10060. EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats);
  10061. /**
  10062. * dev_get_tstats64 - ndo_get_stats64 implementation
  10063. * @dev: device to get statistics from
  10064. * @s: place to store stats
  10065. *
  10066. * Populate @s from dev->stats and dev->tstats. Can be used as
  10067. * ndo_get_stats64() callback.
  10068. */
  10069. void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s)
  10070. {
  10071. netdev_stats_to_stats64(s, &dev->stats);
  10072. dev_fetch_sw_netstats(s, dev->tstats);
  10073. }
  10074. EXPORT_SYMBOL_GPL(dev_get_tstats64);
  10075. struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
  10076. {
  10077. struct netdev_queue *queue = dev_ingress_queue(dev);
  10078. #ifdef CONFIG_NET_CLS_ACT
  10079. if (queue)
  10080. return queue;
  10081. queue = kzalloc_obj(*queue);
  10082. if (!queue)
  10083. return NULL;
  10084. netdev_init_one_queue(dev, queue, NULL);
  10085. RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
  10086. RCU_INIT_POINTER(queue->qdisc_sleeping, &noop_qdisc);
  10087. rcu_assign_pointer(dev->ingress_queue, queue);
  10088. #endif
  10089. return queue;
  10090. }
  10091. static const struct ethtool_ops default_ethtool_ops;
  10092. void netdev_set_default_ethtool_ops(struct net_device *dev,
  10093. const struct ethtool_ops *ops)
  10094. {
  10095. if (dev->ethtool_ops == &default_ethtool_ops)
  10096. dev->ethtool_ops = ops;
  10097. }
  10098. EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
  10099. /**
  10100. * netdev_sw_irq_coalesce_default_on() - enable SW IRQ coalescing by default
  10101. * @dev: netdev to enable the IRQ coalescing on
  10102. *
  10103. * Sets a conservative default for SW IRQ coalescing. Users can use
  10104. * sysfs attributes to override the default values.
  10105. */
  10106. void netdev_sw_irq_coalesce_default_on(struct net_device *dev)
  10107. {
  10108. WARN_ON(dev->reg_state == NETREG_REGISTERED);
  10109. if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
  10110. netdev_set_gro_flush_timeout(dev, 20000);
  10111. netdev_set_defer_hard_irqs(dev, 1);
  10112. }
  10113. }
  10114. EXPORT_SYMBOL_GPL(netdev_sw_irq_coalesce_default_on);
  10115. /**
  10116. * alloc_netdev_mqs - allocate network device
  10117. * @sizeof_priv: size of private data to allocate space for
  10118. * @name: device name format string
  10119. * @name_assign_type: origin of device name
  10120. * @setup: callback to initialize device
  10121. * @txqs: the number of TX subqueues to allocate
  10122. * @rxqs: the number of RX subqueues to allocate
  10123. *
  10124. * Allocates a struct net_device with private data area for driver use
  10125. * and performs basic initialization. Also allocates subqueue structs
  10126. * for each queue on the device.
  10127. */
  10128. struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
  10129. unsigned char name_assign_type,
  10130. void (*setup)(struct net_device *),
  10131. unsigned int txqs, unsigned int rxqs)
  10132. {
  10133. struct net_device *dev;
  10134. size_t napi_config_sz;
  10135. unsigned int maxqs;
  10136. BUG_ON(strlen(name) >= sizeof(dev->name));
  10137. if (txqs < 1) {
  10138. pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
  10139. return NULL;
  10140. }
  10141. if (rxqs < 1) {
  10142. pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
  10143. return NULL;
  10144. }
  10145. maxqs = max(txqs, rxqs);
  10146. dev = kvzalloc_flex(*dev, priv, sizeof_priv,
  10147. GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
  10148. if (!dev)
  10149. return NULL;
  10150. dev->priv_len = sizeof_priv;
  10151. ref_tracker_dir_init(&dev->refcnt_tracker, 128, "netdev");
  10152. #ifdef CONFIG_PCPU_DEV_REFCNT
  10153. dev->pcpu_refcnt = alloc_percpu(int);
  10154. if (!dev->pcpu_refcnt)
  10155. goto free_dev;
  10156. __dev_hold(dev);
  10157. #else
  10158. refcount_set(&dev->dev_refcnt, 1);
  10159. #endif
  10160. if (dev_addr_init(dev))
  10161. goto free_pcpu;
  10162. dev_mc_init(dev);
  10163. dev_uc_init(dev);
  10164. dev_net_set(dev, &init_net);
  10165. dev->gso_max_size = GSO_LEGACY_MAX_SIZE;
  10166. dev->xdp_zc_max_segs = 1;
  10167. dev->gso_max_segs = GSO_MAX_SEGS;
  10168. dev->gro_max_size = GRO_LEGACY_MAX_SIZE;
  10169. dev->gso_ipv4_max_size = GSO_LEGACY_MAX_SIZE;
  10170. dev->gro_ipv4_max_size = GRO_LEGACY_MAX_SIZE;
  10171. dev->tso_max_size = TSO_LEGACY_MAX_SIZE;
  10172. dev->tso_max_segs = TSO_MAX_SEGS;
  10173. dev->upper_level = 1;
  10174. dev->lower_level = 1;
  10175. #ifdef CONFIG_LOCKDEP
  10176. dev->nested_level = 0;
  10177. INIT_LIST_HEAD(&dev->unlink_list);
  10178. #endif
  10179. INIT_LIST_HEAD(&dev->napi_list);
  10180. INIT_LIST_HEAD(&dev->unreg_list);
  10181. INIT_LIST_HEAD(&dev->close_list);
  10182. INIT_LIST_HEAD(&dev->link_watch_list);
  10183. INIT_LIST_HEAD(&dev->adj_list.upper);
  10184. INIT_LIST_HEAD(&dev->adj_list.lower);
  10185. INIT_LIST_HEAD(&dev->ptype_all);
  10186. INIT_LIST_HEAD(&dev->ptype_specific);
  10187. INIT_LIST_HEAD(&dev->net_notifier_list);
  10188. #ifdef CONFIG_NET_SCHED
  10189. hash_init(dev->qdisc_hash);
  10190. #endif
  10191. mutex_init(&dev->lock);
  10192. dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
  10193. setup(dev);
  10194. if (!dev->tx_queue_len) {
  10195. dev->priv_flags |= IFF_NO_QUEUE;
  10196. dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
  10197. }
  10198. dev->num_tx_queues = txqs;
  10199. dev->real_num_tx_queues = txqs;
  10200. if (netif_alloc_netdev_queues(dev))
  10201. goto free_all;
  10202. dev->num_rx_queues = rxqs;
  10203. dev->real_num_rx_queues = rxqs;
  10204. if (netif_alloc_rx_queues(dev))
  10205. goto free_all;
  10206. dev->ethtool = kzalloc_obj(*dev->ethtool, GFP_KERNEL_ACCOUNT);
  10207. if (!dev->ethtool)
  10208. goto free_all;
  10209. dev->cfg = kzalloc_obj(*dev->cfg, GFP_KERNEL_ACCOUNT);
  10210. if (!dev->cfg)
  10211. goto free_all;
  10212. dev->cfg_pending = dev->cfg;
  10213. dev->num_napi_configs = maxqs;
  10214. napi_config_sz = array_size(maxqs, sizeof(*dev->napi_config));
  10215. dev->napi_config = kvzalloc(napi_config_sz, GFP_KERNEL_ACCOUNT);
  10216. if (!dev->napi_config)
  10217. goto free_all;
  10218. strscpy(dev->name, name);
  10219. dev->name_assign_type = name_assign_type;
  10220. dev->group = INIT_NETDEV_GROUP;
  10221. if (!dev->ethtool_ops)
  10222. dev->ethtool_ops = &default_ethtool_ops;
  10223. nf_hook_netdev_init(dev);
  10224. return dev;
  10225. free_all:
  10226. free_netdev(dev);
  10227. return NULL;
  10228. free_pcpu:
  10229. #ifdef CONFIG_PCPU_DEV_REFCNT
  10230. free_percpu(dev->pcpu_refcnt);
  10231. free_dev:
  10232. #endif
  10233. kvfree(dev);
  10234. return NULL;
  10235. }
  10236. EXPORT_SYMBOL(alloc_netdev_mqs);
  10237. static void netdev_napi_exit(struct net_device *dev)
  10238. {
  10239. if (!list_empty(&dev->napi_list)) {
  10240. struct napi_struct *p, *n;
  10241. netdev_lock(dev);
  10242. list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
  10243. __netif_napi_del_locked(p);
  10244. netdev_unlock(dev);
  10245. synchronize_net();
  10246. }
  10247. kvfree(dev->napi_config);
  10248. }
  10249. /**
  10250. * free_netdev - free network device
  10251. * @dev: device
  10252. *
  10253. * This function does the last stage of destroying an allocated device
  10254. * interface. The reference to the device object is released. If this
  10255. * is the last reference then it will be freed.Must be called in process
  10256. * context.
  10257. */
  10258. void free_netdev(struct net_device *dev)
  10259. {
  10260. might_sleep();
  10261. /* When called immediately after register_netdevice() failed the unwind
  10262. * handling may still be dismantling the device. Handle that case by
  10263. * deferring the free.
  10264. */
  10265. if (dev->reg_state == NETREG_UNREGISTERING) {
  10266. ASSERT_RTNL();
  10267. dev->needs_free_netdev = true;
  10268. return;
  10269. }
  10270. WARN_ON(dev->cfg != dev->cfg_pending);
  10271. kfree(dev->cfg);
  10272. kfree(dev->ethtool);
  10273. netif_free_tx_queues(dev);
  10274. netif_free_rx_queues(dev);
  10275. kfree(rcu_dereference_protected(dev->ingress_queue, 1));
  10276. /* Flush device addresses */
  10277. dev_addr_flush(dev);
  10278. netdev_napi_exit(dev);
  10279. netif_del_cpu_rmap(dev);
  10280. ref_tracker_dir_exit(&dev->refcnt_tracker);
  10281. #ifdef CONFIG_PCPU_DEV_REFCNT
  10282. free_percpu(dev->pcpu_refcnt);
  10283. dev->pcpu_refcnt = NULL;
  10284. #endif
  10285. free_percpu(dev->core_stats);
  10286. dev->core_stats = NULL;
  10287. free_percpu(dev->xdp_bulkq);
  10288. dev->xdp_bulkq = NULL;
  10289. netdev_free_phy_link_topology(dev);
  10290. mutex_destroy(&dev->lock);
  10291. /* Compatibility with error handling in drivers */
  10292. if (dev->reg_state == NETREG_UNINITIALIZED ||
  10293. dev->reg_state == NETREG_DUMMY) {
  10294. kvfree(dev);
  10295. return;
  10296. }
  10297. BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
  10298. WRITE_ONCE(dev->reg_state, NETREG_RELEASED);
  10299. /* will free via device release */
  10300. put_device(&dev->dev);
  10301. }
  10302. EXPORT_SYMBOL(free_netdev);
  10303. /**
  10304. * alloc_netdev_dummy - Allocate and initialize a dummy net device.
  10305. * @sizeof_priv: size of private data to allocate space for
  10306. *
  10307. * Return: the allocated net_device on success, NULL otherwise
  10308. */
  10309. struct net_device *alloc_netdev_dummy(int sizeof_priv)
  10310. {
  10311. return alloc_netdev(sizeof_priv, "dummy#", NET_NAME_UNKNOWN,
  10312. init_dummy_netdev);
  10313. }
  10314. EXPORT_SYMBOL_GPL(alloc_netdev_dummy);
  10315. /**
  10316. * synchronize_net - Synchronize with packet receive processing
  10317. *
  10318. * Wait for packets currently being received to be done.
  10319. * Does not block later packets from starting.
  10320. */
  10321. void synchronize_net(void)
  10322. {
  10323. might_sleep();
  10324. if (from_cleanup_net() || rtnl_is_locked())
  10325. synchronize_rcu_expedited();
  10326. else
  10327. synchronize_rcu();
  10328. }
  10329. EXPORT_SYMBOL(synchronize_net);
  10330. static void netdev_rss_contexts_free(struct net_device *dev)
  10331. {
  10332. struct ethtool_rxfh_context *ctx;
  10333. unsigned long context;
  10334. mutex_lock(&dev->ethtool->rss_lock);
  10335. xa_for_each(&dev->ethtool->rss_ctx, context, ctx) {
  10336. xa_erase(&dev->ethtool->rss_ctx, context);
  10337. dev->ethtool_ops->remove_rxfh_context(dev, ctx, context, NULL);
  10338. kfree(ctx);
  10339. }
  10340. xa_destroy(&dev->ethtool->rss_ctx);
  10341. mutex_unlock(&dev->ethtool->rss_lock);
  10342. }
  10343. /**
  10344. * unregister_netdevice_queue - remove device from the kernel
  10345. * @dev: device
  10346. * @head: list
  10347. *
  10348. * This function shuts down a device interface and removes it
  10349. * from the kernel tables.
  10350. * If head not NULL, device is queued to be unregistered later.
  10351. *
  10352. * Callers must hold the rtnl semaphore. You may want
  10353. * unregister_netdev() instead of this.
  10354. */
  10355. void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
  10356. {
  10357. ASSERT_RTNL();
  10358. if (head) {
  10359. list_move_tail(&dev->unreg_list, head);
  10360. } else {
  10361. LIST_HEAD(single);
  10362. list_add(&dev->unreg_list, &single);
  10363. unregister_netdevice_many(&single);
  10364. }
  10365. }
  10366. EXPORT_SYMBOL(unregister_netdevice_queue);
  10367. static void dev_memory_provider_uninstall(struct net_device *dev)
  10368. {
  10369. unsigned int i;
  10370. for (i = 0; i < dev->real_num_rx_queues; i++) {
  10371. struct netdev_rx_queue *rxq = &dev->_rx[i];
  10372. struct pp_memory_provider_params *p = &rxq->mp_params;
  10373. if (p->mp_ops && p->mp_ops->uninstall)
  10374. p->mp_ops->uninstall(rxq->mp_params.mp_priv, rxq);
  10375. }
  10376. }
  10377. /* devices must be UP and netdev_lock()'d */
  10378. static void netif_close_many_and_unlock(struct list_head *close_head)
  10379. {
  10380. struct net_device *dev, *tmp;
  10381. netif_close_many(close_head, false);
  10382. /* ... now unlock them */
  10383. list_for_each_entry_safe(dev, tmp, close_head, close_list) {
  10384. netdev_unlock(dev);
  10385. list_del_init(&dev->close_list);
  10386. }
  10387. }
  10388. static void netif_close_many_and_unlock_cond(struct list_head *close_head)
  10389. {
  10390. #ifdef CONFIG_LOCKDEP
  10391. /* We can only track up to MAX_LOCK_DEPTH locks per task.
  10392. *
  10393. * Reserve half the available slots for additional locks possibly
  10394. * taken by notifiers and (soft)irqs.
  10395. */
  10396. unsigned int limit = MAX_LOCK_DEPTH / 2;
  10397. if (lockdep_depth(current) > limit)
  10398. netif_close_many_and_unlock(close_head);
  10399. #endif
  10400. }
  10401. void unregister_netdevice_many_notify(struct list_head *head,
  10402. u32 portid, const struct nlmsghdr *nlh)
  10403. {
  10404. struct net_device *dev, *tmp;
  10405. LIST_HEAD(close_head);
  10406. int cnt = 0;
  10407. BUG_ON(dev_boot_phase);
  10408. ASSERT_RTNL();
  10409. if (list_empty(head))
  10410. return;
  10411. list_for_each_entry_safe(dev, tmp, head, unreg_list) {
  10412. /* Some devices call without registering
  10413. * for initialization unwind. Remove those
  10414. * devices and proceed with the remaining.
  10415. */
  10416. if (dev->reg_state == NETREG_UNINITIALIZED) {
  10417. pr_debug("unregister_netdevice: device %s/%p never was registered\n",
  10418. dev->name, dev);
  10419. WARN_ON(1);
  10420. list_del(&dev->unreg_list);
  10421. continue;
  10422. }
  10423. dev->dismantle = true;
  10424. BUG_ON(dev->reg_state != NETREG_REGISTERED);
  10425. }
  10426. /* If device is running, close it first. Start with ops locked... */
  10427. list_for_each_entry(dev, head, unreg_list) {
  10428. if (!(dev->flags & IFF_UP))
  10429. continue;
  10430. if (netdev_need_ops_lock(dev)) {
  10431. list_add_tail(&dev->close_list, &close_head);
  10432. netdev_lock(dev);
  10433. }
  10434. netif_close_many_and_unlock_cond(&close_head);
  10435. }
  10436. netif_close_many_and_unlock(&close_head);
  10437. /* ... now go over the rest. */
  10438. list_for_each_entry(dev, head, unreg_list) {
  10439. if (!netdev_need_ops_lock(dev))
  10440. list_add_tail(&dev->close_list, &close_head);
  10441. }
  10442. netif_close_many(&close_head, true);
  10443. list_for_each_entry(dev, head, unreg_list) {
  10444. /* And unlink it from device chain. */
  10445. unlist_netdevice(dev);
  10446. netdev_lock(dev);
  10447. WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERING);
  10448. netdev_unlock(dev);
  10449. }
  10450. flush_all_backlogs();
  10451. synchronize_net();
  10452. list_for_each_entry(dev, head, unreg_list) {
  10453. struct sk_buff *skb = NULL;
  10454. /* Shutdown queueing discipline. */
  10455. netdev_lock_ops(dev);
  10456. dev_shutdown(dev);
  10457. dev_tcx_uninstall(dev);
  10458. dev_xdp_uninstall(dev);
  10459. dev_memory_provider_uninstall(dev);
  10460. netdev_unlock_ops(dev);
  10461. bpf_dev_bound_netdev_unregister(dev);
  10462. netdev_offload_xstats_disable_all(dev);
  10463. /* Notify protocols, that we are about to destroy
  10464. * this device. They should clean all the things.
  10465. */
  10466. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  10467. if (!(dev->rtnl_link_ops && dev->rtnl_link_initializing))
  10468. skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
  10469. GFP_KERNEL, NULL, 0,
  10470. portid, nlh);
  10471. /*
  10472. * Flush the unicast and multicast chains
  10473. */
  10474. dev_uc_flush(dev);
  10475. dev_mc_flush(dev);
  10476. netdev_name_node_alt_flush(dev);
  10477. netdev_name_node_free(dev->name_node);
  10478. netdev_rss_contexts_free(dev);
  10479. call_netdevice_notifiers(NETDEV_PRE_UNINIT, dev);
  10480. if (dev->netdev_ops->ndo_uninit)
  10481. dev->netdev_ops->ndo_uninit(dev);
  10482. mutex_destroy(&dev->ethtool->rss_lock);
  10483. net_shaper_flush_netdev(dev);
  10484. if (skb)
  10485. rtmsg_ifinfo_send(skb, dev, GFP_KERNEL, portid, nlh);
  10486. /* Notifier chain MUST detach us all upper devices. */
  10487. WARN_ON(netdev_has_any_upper_dev(dev));
  10488. WARN_ON(netdev_has_any_lower_dev(dev));
  10489. /* Remove entries from kobject tree */
  10490. netdev_unregister_kobject(dev);
  10491. #ifdef CONFIG_XPS
  10492. /* Remove XPS queueing entries */
  10493. netif_reset_xps_queues_gt(dev, 0);
  10494. #endif
  10495. }
  10496. synchronize_net();
  10497. list_for_each_entry(dev, head, unreg_list) {
  10498. netdev_put(dev, &dev->dev_registered_tracker);
  10499. net_set_todo(dev);
  10500. cnt++;
  10501. }
  10502. atomic_add(cnt, &dev_unreg_count);
  10503. list_del(head);
  10504. }
  10505. /**
  10506. * unregister_netdevice_many - unregister many devices
  10507. * @head: list of devices
  10508. *
  10509. * Note: As most callers use a stack allocated list_head,
  10510. * we force a list_del() to make sure stack won't be corrupted later.
  10511. */
  10512. void unregister_netdevice_many(struct list_head *head)
  10513. {
  10514. unregister_netdevice_many_notify(head, 0, NULL);
  10515. }
  10516. EXPORT_SYMBOL(unregister_netdevice_many);
  10517. /**
  10518. * unregister_netdev - remove device from the kernel
  10519. * @dev: device
  10520. *
  10521. * This function shuts down a device interface and removes it
  10522. * from the kernel tables.
  10523. *
  10524. * This is just a wrapper for unregister_netdevice that takes
  10525. * the rtnl semaphore. In general you want to use this and not
  10526. * unregister_netdevice.
  10527. */
  10528. void unregister_netdev(struct net_device *dev)
  10529. {
  10530. rtnl_net_dev_lock(dev);
  10531. unregister_netdevice(dev);
  10532. rtnl_net_dev_unlock(dev);
  10533. }
  10534. EXPORT_SYMBOL(unregister_netdev);
  10535. int __dev_change_net_namespace(struct net_device *dev, struct net *net,
  10536. const char *pat, int new_ifindex,
  10537. struct netlink_ext_ack *extack)
  10538. {
  10539. struct netdev_name_node *name_node;
  10540. struct net *net_old = dev_net(dev);
  10541. char new_name[IFNAMSIZ] = {};
  10542. int err, new_nsid;
  10543. ASSERT_RTNL();
  10544. /* Don't allow namespace local devices to be moved. */
  10545. err = -EINVAL;
  10546. if (dev->netns_immutable) {
  10547. NL_SET_ERR_MSG(extack, "The interface netns is immutable");
  10548. goto out;
  10549. }
  10550. /* Ensure the device has been registered */
  10551. if (dev->reg_state != NETREG_REGISTERED) {
  10552. NL_SET_ERR_MSG(extack, "The interface isn't registered");
  10553. goto out;
  10554. }
  10555. /* Get out if there is nothing todo */
  10556. err = 0;
  10557. if (net_eq(net_old, net))
  10558. goto out;
  10559. /* Pick the destination device name, and ensure
  10560. * we can use it in the destination network namespace.
  10561. */
  10562. err = -EEXIST;
  10563. if (netdev_name_in_use(net, dev->name)) {
  10564. /* We get here if we can't use the current device name */
  10565. if (!pat) {
  10566. NL_SET_ERR_MSG(extack,
  10567. "An interface with the same name exists in the target netns");
  10568. goto out;
  10569. }
  10570. err = dev_prep_valid_name(net, dev, pat, new_name, EEXIST);
  10571. if (err < 0) {
  10572. NL_SET_ERR_MSG_FMT(extack,
  10573. "Unable to use '%s' for the new interface name in the target netns",
  10574. pat);
  10575. goto out;
  10576. }
  10577. }
  10578. /* Check that none of the altnames conflicts. */
  10579. err = -EEXIST;
  10580. netdev_for_each_altname(dev, name_node) {
  10581. if (netdev_name_in_use(net, name_node->name)) {
  10582. NL_SET_ERR_MSG_FMT(extack,
  10583. "An interface with the altname %s exists in the target netns",
  10584. name_node->name);
  10585. goto out;
  10586. }
  10587. }
  10588. /* Check that new_ifindex isn't used yet. */
  10589. if (new_ifindex) {
  10590. err = dev_index_reserve(net, new_ifindex);
  10591. if (err < 0) {
  10592. NL_SET_ERR_MSG_FMT(extack,
  10593. "The ifindex %d is not available in the target netns",
  10594. new_ifindex);
  10595. goto out;
  10596. }
  10597. } else {
  10598. /* If there is an ifindex conflict assign a new one */
  10599. err = dev_index_reserve(net, dev->ifindex);
  10600. if (err == -EBUSY)
  10601. err = dev_index_reserve(net, 0);
  10602. if (err < 0) {
  10603. NL_SET_ERR_MSG(extack,
  10604. "Unable to allocate a new ifindex in the target netns");
  10605. goto out;
  10606. }
  10607. new_ifindex = err;
  10608. }
  10609. /*
  10610. * And now a mini version of register_netdevice unregister_netdevice.
  10611. */
  10612. netdev_lock_ops(dev);
  10613. /* If device is running close it first. */
  10614. netif_close(dev);
  10615. /* And unlink it from device chain */
  10616. unlist_netdevice(dev);
  10617. if (!netdev_need_ops_lock(dev))
  10618. netdev_lock(dev);
  10619. dev->moving_ns = true;
  10620. netdev_unlock(dev);
  10621. synchronize_net();
  10622. /* Shutdown queueing discipline. */
  10623. netdev_lock_ops(dev);
  10624. dev_shutdown(dev);
  10625. netdev_unlock_ops(dev);
  10626. /* Notify protocols, that we are about to destroy
  10627. * this device. They should clean all the things.
  10628. *
  10629. * Note that dev->reg_state stays at NETREG_REGISTERED.
  10630. * This is wanted because this way 8021q and macvlan know
  10631. * the device is just moving and can keep their slaves up.
  10632. */
  10633. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  10634. rcu_barrier();
  10635. new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
  10636. rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
  10637. new_ifindex);
  10638. /*
  10639. * Flush the unicast and multicast chains
  10640. */
  10641. dev_uc_flush(dev);
  10642. dev_mc_flush(dev);
  10643. /* Send a netdev-removed uevent to the old namespace */
  10644. kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
  10645. netdev_adjacent_del_links(dev);
  10646. /* Move per-net netdevice notifiers that are following the netdevice */
  10647. move_netdevice_notifiers_dev_net(dev, net);
  10648. /* Actually switch the network namespace */
  10649. netdev_lock(dev);
  10650. dev_net_set(dev, net);
  10651. netdev_unlock(dev);
  10652. dev->ifindex = new_ifindex;
  10653. if (new_name[0]) {
  10654. /* Rename the netdev to prepared name */
  10655. write_seqlock_bh(&netdev_rename_lock);
  10656. strscpy(dev->name, new_name, IFNAMSIZ);
  10657. write_sequnlock_bh(&netdev_rename_lock);
  10658. }
  10659. /* Fixup kobjects */
  10660. dev_set_uevent_suppress(&dev->dev, 1);
  10661. err = device_rename(&dev->dev, dev->name);
  10662. dev_set_uevent_suppress(&dev->dev, 0);
  10663. WARN_ON(err);
  10664. /* Send a netdev-add uevent to the new namespace */
  10665. kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
  10666. netdev_adjacent_add_links(dev);
  10667. /* Adapt owner in case owning user namespace of target network
  10668. * namespace is different from the original one.
  10669. */
  10670. err = netdev_change_owner(dev, net_old, net);
  10671. WARN_ON(err);
  10672. netdev_lock(dev);
  10673. dev->moving_ns = false;
  10674. if (!netdev_need_ops_lock(dev))
  10675. netdev_unlock(dev);
  10676. /* Add the device back in the hashes */
  10677. list_netdevice(dev);
  10678. /* Notify protocols, that a new device appeared. */
  10679. call_netdevice_notifiers(NETDEV_REGISTER, dev);
  10680. netdev_unlock_ops(dev);
  10681. /*
  10682. * Prevent userspace races by waiting until the network
  10683. * device is fully setup before sending notifications.
  10684. */
  10685. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL, 0, NULL);
  10686. synchronize_net();
  10687. err = 0;
  10688. out:
  10689. return err;
  10690. }
  10691. static int dev_cpu_dead(unsigned int oldcpu)
  10692. {
  10693. struct sk_buff **list_skb;
  10694. struct sk_buff *skb;
  10695. unsigned int cpu;
  10696. struct softnet_data *sd, *oldsd, *remsd = NULL;
  10697. local_irq_disable();
  10698. cpu = smp_processor_id();
  10699. sd = &per_cpu(softnet_data, cpu);
  10700. oldsd = &per_cpu(softnet_data, oldcpu);
  10701. /* Find end of our completion_queue. */
  10702. list_skb = &sd->completion_queue;
  10703. while (*list_skb)
  10704. list_skb = &(*list_skb)->next;
  10705. /* Append completion queue from offline CPU. */
  10706. *list_skb = oldsd->completion_queue;
  10707. oldsd->completion_queue = NULL;
  10708. /* Append output queue from offline CPU. */
  10709. if (oldsd->output_queue) {
  10710. *sd->output_queue_tailp = oldsd->output_queue;
  10711. sd->output_queue_tailp = oldsd->output_queue_tailp;
  10712. oldsd->output_queue = NULL;
  10713. oldsd->output_queue_tailp = &oldsd->output_queue;
  10714. }
  10715. /* Append NAPI poll list from offline CPU, with one exception :
  10716. * process_backlog() must be called by cpu owning percpu backlog.
  10717. * We properly handle process_queue & input_pkt_queue later.
  10718. */
  10719. while (!list_empty(&oldsd->poll_list)) {
  10720. struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
  10721. struct napi_struct,
  10722. poll_list);
  10723. list_del_init(&napi->poll_list);
  10724. if (napi->poll == process_backlog)
  10725. napi->state &= NAPIF_STATE_THREADED;
  10726. else
  10727. ____napi_schedule(sd, napi);
  10728. }
  10729. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  10730. local_irq_enable();
  10731. if (!use_backlog_threads()) {
  10732. #ifdef CONFIG_RPS
  10733. remsd = oldsd->rps_ipi_list;
  10734. oldsd->rps_ipi_list = NULL;
  10735. #endif
  10736. /* send out pending IPI's on offline CPU */
  10737. net_rps_send_ipi(remsd);
  10738. }
  10739. /* Process offline CPU's input_pkt_queue */
  10740. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  10741. netif_rx(skb);
  10742. rps_input_queue_head_incr(oldsd);
  10743. }
  10744. while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  10745. netif_rx(skb);
  10746. rps_input_queue_head_incr(oldsd);
  10747. }
  10748. return 0;
  10749. }
  10750. /**
  10751. * netdev_increment_features - increment feature set by one
  10752. * @all: current feature set
  10753. * @one: new feature set
  10754. * @mask: mask feature set
  10755. *
  10756. * Computes a new feature set after adding a device with feature set
  10757. * @one to the master device with current feature set @all. Will not
  10758. * enable anything that is off in @mask. Returns the new feature set.
  10759. */
  10760. netdev_features_t netdev_increment_features(netdev_features_t all,
  10761. netdev_features_t one, netdev_features_t mask)
  10762. {
  10763. if (mask & NETIF_F_HW_CSUM)
  10764. mask |= NETIF_F_CSUM_MASK;
  10765. mask |= NETIF_F_VLAN_CHALLENGED;
  10766. all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
  10767. all &= one | ~NETIF_F_ALL_FOR_ALL;
  10768. /* If one device supports hw checksumming, set for all. */
  10769. if (all & NETIF_F_HW_CSUM)
  10770. all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
  10771. return all;
  10772. }
  10773. EXPORT_SYMBOL(netdev_increment_features);
  10774. /**
  10775. * netdev_compute_master_upper_features - compute feature from lowers
  10776. * @dev: the upper device
  10777. * @update_header: whether to update upper device's header_len/headroom/tailroom
  10778. *
  10779. * Recompute the upper device's feature based on all lower devices.
  10780. */
  10781. void netdev_compute_master_upper_features(struct net_device *dev, bool update_header)
  10782. {
  10783. unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
  10784. netdev_features_t gso_partial_features = MASTER_UPPER_DEV_GSO_PARTIAL_FEATURES;
  10785. netdev_features_t xfrm_features = MASTER_UPPER_DEV_XFRM_FEATURES;
  10786. netdev_features_t mpls_features = MASTER_UPPER_DEV_MPLS_FEATURES;
  10787. netdev_features_t vlan_features = MASTER_UPPER_DEV_VLAN_FEATURES;
  10788. netdev_features_t enc_features = MASTER_UPPER_DEV_ENC_FEATURES;
  10789. unsigned short max_header_len = ETH_HLEN;
  10790. unsigned int tso_max_size = TSO_MAX_SIZE;
  10791. unsigned short max_headroom = 0;
  10792. unsigned short max_tailroom = 0;
  10793. u16 tso_max_segs = TSO_MAX_SEGS;
  10794. struct net_device *lower_dev;
  10795. struct list_head *iter;
  10796. mpls_features = netdev_base_features(mpls_features);
  10797. vlan_features = netdev_base_features(vlan_features);
  10798. enc_features = netdev_base_features(enc_features);
  10799. netdev_for_each_lower_dev(dev, lower_dev, iter) {
  10800. gso_partial_features = netdev_increment_features(gso_partial_features,
  10801. lower_dev->gso_partial_features,
  10802. MASTER_UPPER_DEV_GSO_PARTIAL_FEATURES);
  10803. vlan_features = netdev_increment_features(vlan_features,
  10804. lower_dev->vlan_features,
  10805. MASTER_UPPER_DEV_VLAN_FEATURES);
  10806. enc_features = netdev_increment_features(enc_features,
  10807. lower_dev->hw_enc_features,
  10808. MASTER_UPPER_DEV_ENC_FEATURES);
  10809. if (IS_ENABLED(CONFIG_XFRM_OFFLOAD))
  10810. xfrm_features = netdev_increment_features(xfrm_features,
  10811. lower_dev->hw_enc_features,
  10812. MASTER_UPPER_DEV_XFRM_FEATURES);
  10813. mpls_features = netdev_increment_features(mpls_features,
  10814. lower_dev->mpls_features,
  10815. MASTER_UPPER_DEV_MPLS_FEATURES);
  10816. dst_release_flag &= lower_dev->priv_flags;
  10817. if (update_header) {
  10818. max_header_len = max(max_header_len, lower_dev->hard_header_len);
  10819. max_headroom = max(max_headroom, lower_dev->needed_headroom);
  10820. max_tailroom = max(max_tailroom, lower_dev->needed_tailroom);
  10821. }
  10822. tso_max_size = min(tso_max_size, lower_dev->tso_max_size);
  10823. tso_max_segs = min(tso_max_segs, lower_dev->tso_max_segs);
  10824. }
  10825. dev->gso_partial_features = gso_partial_features;
  10826. dev->vlan_features = vlan_features;
  10827. dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
  10828. NETIF_F_HW_VLAN_CTAG_TX |
  10829. NETIF_F_HW_VLAN_STAG_TX;
  10830. if (IS_ENABLED(CONFIG_XFRM_OFFLOAD))
  10831. dev->hw_enc_features |= xfrm_features;
  10832. dev->mpls_features = mpls_features;
  10833. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  10834. if ((dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) &&
  10835. dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
  10836. dev->priv_flags |= IFF_XMIT_DST_RELEASE;
  10837. if (update_header) {
  10838. dev->hard_header_len = max_header_len;
  10839. dev->needed_headroom = max_headroom;
  10840. dev->needed_tailroom = max_tailroom;
  10841. }
  10842. netif_set_tso_max_segs(dev, tso_max_segs);
  10843. netif_set_tso_max_size(dev, tso_max_size);
  10844. netdev_change_features(dev);
  10845. }
  10846. EXPORT_SYMBOL(netdev_compute_master_upper_features);
  10847. static struct hlist_head * __net_init netdev_create_hash(void)
  10848. {
  10849. int i;
  10850. struct hlist_head *hash;
  10851. hash = kmalloc_objs(*hash, NETDEV_HASHENTRIES);
  10852. if (hash != NULL)
  10853. for (i = 0; i < NETDEV_HASHENTRIES; i++)
  10854. INIT_HLIST_HEAD(&hash[i]);
  10855. return hash;
  10856. }
  10857. /* Initialize per network namespace state */
  10858. static int __net_init netdev_init(struct net *net)
  10859. {
  10860. BUILD_BUG_ON(GRO_HASH_BUCKETS >
  10861. BITS_PER_BYTE * sizeof_field(struct gro_node, bitmask));
  10862. INIT_LIST_HEAD(&net->dev_base_head);
  10863. net->dev_name_head = netdev_create_hash();
  10864. if (net->dev_name_head == NULL)
  10865. goto err_name;
  10866. net->dev_index_head = netdev_create_hash();
  10867. if (net->dev_index_head == NULL)
  10868. goto err_idx;
  10869. xa_init_flags(&net->dev_by_index, XA_FLAGS_ALLOC1);
  10870. RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
  10871. return 0;
  10872. err_idx:
  10873. kfree(net->dev_name_head);
  10874. err_name:
  10875. return -ENOMEM;
  10876. }
  10877. /**
  10878. * netdev_drivername - network driver for the device
  10879. * @dev: network device
  10880. *
  10881. * Determine network driver for device.
  10882. */
  10883. const char *netdev_drivername(const struct net_device *dev)
  10884. {
  10885. const struct device_driver *driver;
  10886. const struct device *parent;
  10887. const char *empty = "";
  10888. parent = dev->dev.parent;
  10889. if (!parent)
  10890. return empty;
  10891. driver = parent->driver;
  10892. if (driver && driver->name)
  10893. return driver->name;
  10894. return empty;
  10895. }
  10896. static void __netdev_printk(const char *level, const struct net_device *dev,
  10897. struct va_format *vaf)
  10898. {
  10899. if (dev && dev->dev.parent) {
  10900. dev_printk_emit(level[1] - '0',
  10901. dev->dev.parent,
  10902. "%s %s %s%s: %pV",
  10903. dev_driver_string(dev->dev.parent),
  10904. dev_name(dev->dev.parent),
  10905. netdev_name(dev), netdev_reg_state(dev),
  10906. vaf);
  10907. } else if (dev) {
  10908. printk("%s%s%s: %pV",
  10909. level, netdev_name(dev), netdev_reg_state(dev), vaf);
  10910. } else {
  10911. printk("%s(NULL net_device): %pV", level, vaf);
  10912. }
  10913. }
  10914. void netdev_printk(const char *level, const struct net_device *dev,
  10915. const char *format, ...)
  10916. {
  10917. struct va_format vaf;
  10918. va_list args;
  10919. va_start(args, format);
  10920. vaf.fmt = format;
  10921. vaf.va = &args;
  10922. __netdev_printk(level, dev, &vaf);
  10923. va_end(args);
  10924. }
  10925. EXPORT_SYMBOL(netdev_printk);
  10926. #define define_netdev_printk_level(func, level) \
  10927. void func(const struct net_device *dev, const char *fmt, ...) \
  10928. { \
  10929. struct va_format vaf; \
  10930. va_list args; \
  10931. \
  10932. va_start(args, fmt); \
  10933. \
  10934. vaf.fmt = fmt; \
  10935. vaf.va = &args; \
  10936. \
  10937. __netdev_printk(level, dev, &vaf); \
  10938. \
  10939. va_end(args); \
  10940. } \
  10941. EXPORT_SYMBOL(func);
  10942. define_netdev_printk_level(netdev_emerg, KERN_EMERG);
  10943. define_netdev_printk_level(netdev_alert, KERN_ALERT);
  10944. define_netdev_printk_level(netdev_crit, KERN_CRIT);
  10945. define_netdev_printk_level(netdev_err, KERN_ERR);
  10946. define_netdev_printk_level(netdev_warn, KERN_WARNING);
  10947. define_netdev_printk_level(netdev_notice, KERN_NOTICE);
  10948. define_netdev_printk_level(netdev_info, KERN_INFO);
  10949. static void __net_exit netdev_exit(struct net *net)
  10950. {
  10951. kfree(net->dev_name_head);
  10952. kfree(net->dev_index_head);
  10953. xa_destroy(&net->dev_by_index);
  10954. if (net != &init_net)
  10955. WARN_ON_ONCE(!list_empty(&net->dev_base_head));
  10956. }
  10957. static struct pernet_operations __net_initdata netdev_net_ops = {
  10958. .init = netdev_init,
  10959. .exit = netdev_exit,
  10960. };
  10961. static void __net_exit default_device_exit_net(struct net *net)
  10962. {
  10963. struct netdev_name_node *name_node, *tmp;
  10964. struct net_device *dev, *aux;
  10965. /*
  10966. * Push all migratable network devices back to the
  10967. * initial network namespace
  10968. */
  10969. ASSERT_RTNL();
  10970. for_each_netdev_safe(net, dev, aux) {
  10971. int err;
  10972. char fb_name[IFNAMSIZ];
  10973. /* Ignore unmoveable devices (i.e. loopback) */
  10974. if (dev->netns_immutable)
  10975. continue;
  10976. /* Leave virtual devices for the generic cleanup */
  10977. if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund)
  10978. continue;
  10979. /* Push remaining network devices to init_net */
  10980. snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
  10981. if (netdev_name_in_use(&init_net, fb_name))
  10982. snprintf(fb_name, IFNAMSIZ, "dev%%d");
  10983. netdev_for_each_altname_safe(dev, name_node, tmp)
  10984. if (netdev_name_in_use(&init_net, name_node->name))
  10985. __netdev_name_node_alt_destroy(name_node);
  10986. err = dev_change_net_namespace(dev, &init_net, fb_name);
  10987. if (err) {
  10988. pr_emerg("%s: failed to move %s to init_net: %d\n",
  10989. __func__, dev->name, err);
  10990. BUG();
  10991. }
  10992. }
  10993. }
  10994. static void __net_exit default_device_exit_batch(struct list_head *net_list)
  10995. {
  10996. /* At exit all network devices most be removed from a network
  10997. * namespace. Do this in the reverse order of registration.
  10998. * Do this across as many network namespaces as possible to
  10999. * improve batching efficiency.
  11000. */
  11001. struct net_device *dev;
  11002. struct net *net;
  11003. LIST_HEAD(dev_kill_list);
  11004. rtnl_lock();
  11005. list_for_each_entry(net, net_list, exit_list) {
  11006. default_device_exit_net(net);
  11007. cond_resched();
  11008. }
  11009. list_for_each_entry(net, net_list, exit_list) {
  11010. for_each_netdev_reverse(net, dev) {
  11011. if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
  11012. dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
  11013. else
  11014. unregister_netdevice_queue(dev, &dev_kill_list);
  11015. }
  11016. }
  11017. unregister_netdevice_many(&dev_kill_list);
  11018. rtnl_unlock();
  11019. }
  11020. static struct pernet_operations __net_initdata default_device_ops = {
  11021. .exit_batch = default_device_exit_batch,
  11022. };
  11023. static void __init net_dev_struct_check(void)
  11024. {
  11025. /* TX read-mostly hotpath */
  11026. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, priv_flags_fast);
  11027. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, netdev_ops);
  11028. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, header_ops);
  11029. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, _tx);
  11030. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, real_num_tx_queues);
  11031. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_max_size);
  11032. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_ipv4_max_size);
  11033. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_max_segs);
  11034. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_partial_features);
  11035. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, num_tc);
  11036. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, mtu);
  11037. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, needed_headroom);
  11038. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, tc_to_txq);
  11039. #ifdef CONFIG_XPS
  11040. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, xps_maps);
  11041. #endif
  11042. #ifdef CONFIG_NETFILTER_EGRESS
  11043. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, nf_hooks_egress);
  11044. #endif
  11045. #ifdef CONFIG_NET_XGRESS
  11046. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, tcx_egress);
  11047. #endif
  11048. CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_tx, 160);
  11049. /* TXRX read-mostly hotpath */
  11050. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, lstats);
  11051. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, state);
  11052. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, flags);
  11053. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, hard_header_len);
  11054. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, features);
  11055. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, ip6_ptr);
  11056. CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_txrx, 46);
  11057. /* RX read-mostly hotpath */
  11058. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, ptype_specific);
  11059. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, ifindex);
  11060. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, real_num_rx_queues);
  11061. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, _rx);
  11062. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, gro_max_size);
  11063. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, gro_ipv4_max_size);
  11064. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, rx_handler);
  11065. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, rx_handler_data);
  11066. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, nd_net);
  11067. #ifdef CONFIG_NETPOLL
  11068. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, npinfo);
  11069. #endif
  11070. #ifdef CONFIG_NET_XGRESS
  11071. CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, tcx_ingress);
  11072. #endif
  11073. CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_rx, 92);
  11074. }
  11075. /*
  11076. * Initialize the DEV module. At boot time this walks the device list and
  11077. * unhooks any devices that fail to initialise (normally hardware not
  11078. * present) and leaves us with a valid list of present and active devices.
  11079. *
  11080. */
  11081. /* We allocate 256 pages for each CPU if PAGE_SHIFT is 12 */
  11082. #define SYSTEM_PERCPU_PAGE_POOL_SIZE ((1 << 20) / PAGE_SIZE)
  11083. static int net_page_pool_create(int cpuid)
  11084. {
  11085. #if IS_ENABLED(CONFIG_PAGE_POOL)
  11086. struct page_pool_params page_pool_params = {
  11087. .pool_size = SYSTEM_PERCPU_PAGE_POOL_SIZE,
  11088. .flags = PP_FLAG_SYSTEM_POOL,
  11089. .nid = cpu_to_mem(cpuid),
  11090. };
  11091. struct page_pool *pp_ptr;
  11092. int err;
  11093. pp_ptr = page_pool_create_percpu(&page_pool_params, cpuid);
  11094. if (IS_ERR(pp_ptr))
  11095. return -ENOMEM;
  11096. err = xdp_reg_page_pool(pp_ptr);
  11097. if (err) {
  11098. page_pool_destroy(pp_ptr);
  11099. return err;
  11100. }
  11101. per_cpu(system_page_pool.pool, cpuid) = pp_ptr;
  11102. #endif
  11103. return 0;
  11104. }
  11105. static int backlog_napi_should_run(unsigned int cpu)
  11106. {
  11107. struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu);
  11108. struct napi_struct *napi = &sd->backlog;
  11109. return test_bit(NAPI_STATE_SCHED_THREADED, &napi->state);
  11110. }
  11111. static void run_backlog_napi(unsigned int cpu)
  11112. {
  11113. struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu);
  11114. napi_threaded_poll_loop(&sd->backlog, NULL);
  11115. }
  11116. static void backlog_napi_setup(unsigned int cpu)
  11117. {
  11118. struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu);
  11119. struct napi_struct *napi = &sd->backlog;
  11120. napi->thread = this_cpu_read(backlog_napi);
  11121. set_bit(NAPI_STATE_THREADED, &napi->state);
  11122. }
  11123. static struct smp_hotplug_thread backlog_threads = {
  11124. .store = &backlog_napi,
  11125. .thread_should_run = backlog_napi_should_run,
  11126. .thread_fn = run_backlog_napi,
  11127. .thread_comm = "backlog_napi/%u",
  11128. .setup = backlog_napi_setup,
  11129. };
  11130. /*
  11131. * This is called single threaded during boot, so no need
  11132. * to take the rtnl semaphore.
  11133. */
  11134. static int __init net_dev_init(void)
  11135. {
  11136. int i, rc = -ENOMEM;
  11137. BUG_ON(!dev_boot_phase);
  11138. net_dev_struct_check();
  11139. if (dev_proc_init())
  11140. goto out;
  11141. if (netdev_kobject_init())
  11142. goto out;
  11143. for (i = 0; i < PTYPE_HASH_SIZE; i++)
  11144. INIT_LIST_HEAD(&ptype_base[i]);
  11145. if (register_pernet_subsys(&netdev_net_ops))
  11146. goto out;
  11147. /*
  11148. * Initialise the packet receive queues.
  11149. */
  11150. flush_backlogs_fallback = flush_backlogs_alloc();
  11151. if (!flush_backlogs_fallback)
  11152. goto out;
  11153. for_each_possible_cpu(i) {
  11154. struct softnet_data *sd = &per_cpu(softnet_data, i);
  11155. skb_queue_head_init(&sd->input_pkt_queue);
  11156. skb_queue_head_init(&sd->process_queue);
  11157. #ifdef CONFIG_XFRM_OFFLOAD
  11158. skb_queue_head_init(&sd->xfrm_backlog);
  11159. #endif
  11160. INIT_LIST_HEAD(&sd->poll_list);
  11161. sd->output_queue_tailp = &sd->output_queue;
  11162. #ifdef CONFIG_RPS
  11163. INIT_CSD(&sd->csd, rps_trigger_softirq, sd);
  11164. sd->cpu = i;
  11165. #endif
  11166. INIT_CSD(&sd->defer_csd, trigger_rx_softirq, sd);
  11167. gro_init(&sd->backlog.gro);
  11168. sd->backlog.poll = process_backlog;
  11169. sd->backlog.weight = weight_p;
  11170. INIT_LIST_HEAD(&sd->backlog.poll_list);
  11171. if (net_page_pool_create(i))
  11172. goto out;
  11173. }
  11174. net_hotdata.skb_defer_nodes =
  11175. __alloc_percpu(sizeof(struct skb_defer_node) * nr_node_ids,
  11176. __alignof__(struct skb_defer_node));
  11177. if (!net_hotdata.skb_defer_nodes)
  11178. goto out;
  11179. if (use_backlog_threads())
  11180. smpboot_register_percpu_thread(&backlog_threads);
  11181. dev_boot_phase = 0;
  11182. /* The loopback device is special if any other network devices
  11183. * is present in a network namespace the loopback device must
  11184. * be present. Since we now dynamically allocate and free the
  11185. * loopback device ensure this invariant is maintained by
  11186. * keeping the loopback device as the first device on the
  11187. * list of network devices. Ensuring the loopback devices
  11188. * is the first device that appears and the last network device
  11189. * that disappears.
  11190. */
  11191. if (register_pernet_device(&loopback_net_ops))
  11192. goto out;
  11193. if (register_pernet_device(&default_device_ops))
  11194. goto out;
  11195. open_softirq(NET_TX_SOFTIRQ, net_tx_action);
  11196. open_softirq(NET_RX_SOFTIRQ, net_rx_action);
  11197. rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
  11198. NULL, dev_cpu_dead);
  11199. WARN_ON(rc < 0);
  11200. rc = 0;
  11201. /* avoid static key IPIs to isolated CPUs */
  11202. if (housekeeping_enabled(HK_TYPE_MISC))
  11203. net_enable_timestamp();
  11204. out:
  11205. if (rc < 0) {
  11206. for_each_possible_cpu(i) {
  11207. struct page_pool *pp_ptr;
  11208. pp_ptr = per_cpu(system_page_pool.pool, i);
  11209. if (!pp_ptr)
  11210. continue;
  11211. xdp_unreg_page_pool(pp_ptr);
  11212. page_pool_destroy(pp_ptr);
  11213. per_cpu(system_page_pool.pool, i) = NULL;
  11214. }
  11215. }
  11216. return rc;
  11217. }
  11218. subsys_initcall(net_dev_init);