dev.c 310 KB

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