dev.c 288 KB

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