dev.c 231 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294
  1. /*
  2. * NET3 Protocol independent device support routines.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Derived from the non IP parts of dev.c 1.0.19
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Mark Evans, <evansmp@uhura.aston.ac.uk>
  13. *
  14. * Additional Authors:
  15. * Florian la Roche <rzsfl@rz.uni-sb.de>
  16. * Alan Cox <gw4pts@gw4pts.ampr.org>
  17. * David Hinds <dahinds@users.sourceforge.net>
  18. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  19. * Adam Sulmicki <adam@cfar.umd.edu>
  20. * Pekka Riikonen <priikone@poesidon.pspt.fi>
  21. *
  22. * Changes:
  23. * D.J. Barrow : Fixed bug where dev->refcnt gets set
  24. * to 2 if register_netdev gets called
  25. * before net_dev_init & also removed a
  26. * few lines of code in the process.
  27. * Alan Cox : device private ioctl copies fields back.
  28. * Alan Cox : Transmit queue code does relevant
  29. * stunts to keep the queue safe.
  30. * Alan Cox : Fixed double lock.
  31. * Alan Cox : Fixed promisc NULL pointer trap
  32. * ???????? : Support the full private ioctl range
  33. * Alan Cox : Moved ioctl permission check into
  34. * drivers
  35. * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
  36. * Alan Cox : 100 backlog just doesn't cut it when
  37. * you start doing multicast video 8)
  38. * Alan Cox : Rewrote net_bh and list manager.
  39. * Alan Cox : Fix ETH_P_ALL echoback lengths.
  40. * Alan Cox : Took out transmit every packet pass
  41. * Saved a few bytes in the ioctl handler
  42. * Alan Cox : Network driver sets packet type before
  43. * calling netif_rx. Saves a function
  44. * call a packet.
  45. * Alan Cox : Hashed net_bh()
  46. * Richard Kooijman: Timestamp fixes.
  47. * Alan Cox : Wrong field in SIOCGIFDSTADDR
  48. * Alan Cox : Device lock protection.
  49. * Alan Cox : Fixed nasty side effect of device close
  50. * changes.
  51. * Rudi Cilibrasi : Pass the right thing to
  52. * set_mac_address()
  53. * Dave Miller : 32bit quantity for the device lock to
  54. * make it work out on a Sparc.
  55. * Bjorn Ekwall : Added KERNELD hack.
  56. * Alan Cox : Cleaned up the backlog initialise.
  57. * Craig Metz : SIOCGIFCONF fix if space for under
  58. * 1 device.
  59. * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
  60. * is no device open function.
  61. * Andi Kleen : Fix error reporting for SIOCGIFCONF
  62. * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
  63. * Cyrus Durgin : Cleaned for KMOD
  64. * Adam Sulmicki : Bug Fix : Network Device Unload
  65. * A network device unload needs to purge
  66. * the backlog queue.
  67. * Paul Rusty Russell : SIOCSIFNAME
  68. * Pekka Riikonen : Netdev boot-time settings code
  69. * Andrew Morton : Make unregister_netdevice wait
  70. * indefinitely on dev->refcnt
  71. * J Hadi Salim : - Backlog queue sampling
  72. * - netif_rx() feedback
  73. */
  74. #include <linux/uaccess.h>
  75. #include <linux/bitops.h>
  76. #include <linux/capability.h>
  77. #include <linux/cpu.h>
  78. #include <linux/types.h>
  79. #include <linux/kernel.h>
  80. #include <linux/hash.h>
  81. #include <linux/slab.h>
  82. #include <linux/sched.h>
  83. #include <linux/sched/mm.h>
  84. #include <linux/mutex.h>
  85. #include <linux/string.h>
  86. #include <linux/mm.h>
  87. #include <linux/socket.h>
  88. #include <linux/sockios.h>
  89. #include <linux/errno.h>
  90. #include <linux/interrupt.h>
  91. #include <linux/if_ether.h>
  92. #include <linux/netdevice.h>
  93. #include <linux/etherdevice.h>
  94. #include <linux/ethtool.h>
  95. #include <linux/notifier.h>
  96. #include <linux/skbuff.h>
  97. #include <linux/bpf.h>
  98. #include <linux/bpf_trace.h>
  99. #include <net/net_namespace.h>
  100. #include <net/sock.h>
  101. #include <net/busy_poll.h>
  102. #include <linux/rtnetlink.h>
  103. #include <linux/stat.h>
  104. #include <net/dst.h>
  105. #include <net/dst_metadata.h>
  106. #include <net/pkt_sched.h>
  107. #include <net/pkt_cls.h>
  108. #include <net/checksum.h>
  109. #include <net/xfrm.h>
  110. #include <linux/highmem.h>
  111. #include <linux/init.h>
  112. #include <linux/module.h>
  113. #include <linux/netpoll.h>
  114. #include <linux/rcupdate.h>
  115. #include <linux/delay.h>
  116. #include <net/iw_handler.h>
  117. #include <asm/current.h>
  118. #include <linux/audit.h>
  119. #include <linux/dmaengine.h>
  120. #include <linux/err.h>
  121. #include <linux/ctype.h>
  122. #include <linux/if_arp.h>
  123. #include <linux/if_vlan.h>
  124. #include <linux/ip.h>
  125. #include <net/ip.h>
  126. #include <net/mpls.h>
  127. #include <linux/ipv6.h>
  128. #include <linux/in.h>
  129. #include <linux/jhash.h>
  130. #include <linux/random.h>
  131. #include <trace/events/napi.h>
  132. #include <trace/events/net.h>
  133. #include <trace/events/skb.h>
  134. #include <linux/pci.h>
  135. #include <linux/inetdevice.h>
  136. #include <linux/cpu_rmap.h>
  137. #include <linux/static_key.h>
  138. #include <linux/hashtable.h>
  139. #include <linux/vmalloc.h>
  140. #include <linux/if_macvlan.h>
  141. #include <linux/errqueue.h>
  142. #include <linux/hrtimer.h>
  143. #include <linux/netfilter_ingress.h>
  144. #include <linux/crash_dump.h>
  145. #include <linux/sctp.h>
  146. #include <net/udp_tunnel.h>
  147. #include <linux/net_namespace.h>
  148. #include "net-sysfs.h"
  149. /* Instead of increasing this, you should create a hash table. */
  150. #define MAX_GRO_SKBS 8
  151. /* This should be increased if a protocol with a bigger head is added. */
  152. #define GRO_MAX_HEAD (MAX_HEADER + 128)
  153. static DEFINE_SPINLOCK(ptype_lock);
  154. static DEFINE_SPINLOCK(offload_lock);
  155. struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
  156. struct list_head ptype_all __read_mostly; /* Taps */
  157. static struct list_head offload_base __read_mostly;
  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 struct napi_struct *napi_by_id(unsigned int napi_id);
  162. /*
  163. * The @dev_base_head list is protected by @dev_base_lock and the rtnl
  164. * semaphore.
  165. *
  166. * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
  167. *
  168. * Writers must hold the rtnl semaphore while they loop through the
  169. * dev_base_head list, and hold dev_base_lock for writing when they do the
  170. * actual updates. This allows pure readers to access the list even
  171. * while a writer is preparing to update it.
  172. *
  173. * To put it another way, dev_base_lock is held for writing only to
  174. * protect against pure readers; the rtnl semaphore provides the
  175. * protection against other writers.
  176. *
  177. * See, for example usages, register_netdevice() and
  178. * unregister_netdevice(), which must be called with the rtnl
  179. * semaphore held.
  180. */
  181. DEFINE_RWLOCK(dev_base_lock);
  182. EXPORT_SYMBOL(dev_base_lock);
  183. static DEFINE_MUTEX(ifalias_mutex);
  184. /* protects napi_hash addition/deletion and napi_gen_id */
  185. static DEFINE_SPINLOCK(napi_hash_lock);
  186. static unsigned int napi_gen_id = NR_CPUS;
  187. static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
  188. static seqcount_t devnet_rename_seq;
  189. static inline void dev_base_seq_inc(struct net *net)
  190. {
  191. while (++net->dev_base_seq == 0)
  192. ;
  193. }
  194. static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
  195. {
  196. unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
  197. return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
  198. }
  199. static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
  200. {
  201. return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
  202. }
  203. static inline void rps_lock(struct softnet_data *sd)
  204. {
  205. #ifdef CONFIG_RPS
  206. spin_lock(&sd->input_pkt_queue.lock);
  207. #endif
  208. }
  209. static inline void rps_unlock(struct softnet_data *sd)
  210. {
  211. #ifdef CONFIG_RPS
  212. spin_unlock(&sd->input_pkt_queue.lock);
  213. #endif
  214. }
  215. /* Device list insertion */
  216. static void list_netdevice(struct net_device *dev)
  217. {
  218. struct net *net = dev_net(dev);
  219. ASSERT_RTNL();
  220. write_lock_bh(&dev_base_lock);
  221. list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
  222. hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
  223. hlist_add_head_rcu(&dev->index_hlist,
  224. dev_index_hash(net, dev->ifindex));
  225. write_unlock_bh(&dev_base_lock);
  226. dev_base_seq_inc(net);
  227. }
  228. /* Device list removal
  229. * caller must respect a RCU grace period before freeing/reusing dev
  230. */
  231. static void unlist_netdevice(struct net_device *dev)
  232. {
  233. ASSERT_RTNL();
  234. /* Unlink dev from the device chain */
  235. write_lock_bh(&dev_base_lock);
  236. list_del_rcu(&dev->dev_list);
  237. hlist_del_rcu(&dev->name_hlist);
  238. hlist_del_rcu(&dev->index_hlist);
  239. write_unlock_bh(&dev_base_lock);
  240. dev_base_seq_inc(dev_net(dev));
  241. }
  242. /*
  243. * Our notifier list
  244. */
  245. static RAW_NOTIFIER_HEAD(netdev_chain);
  246. /*
  247. * Device drivers call our routines to queue packets here. We empty the
  248. * queue in the local softnet handler.
  249. */
  250. DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
  251. EXPORT_PER_CPU_SYMBOL(softnet_data);
  252. #ifdef CONFIG_LOCKDEP
  253. /*
  254. * register_netdevice() inits txq->_xmit_lock and sets lockdep class
  255. * according to dev->type
  256. */
  257. static const unsigned short netdev_lock_type[] = {
  258. ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
  259. ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
  260. ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
  261. ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
  262. ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
  263. ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
  264. ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
  265. ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
  266. ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
  267. ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
  268. ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
  269. ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
  270. ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
  271. ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
  272. ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
  273. static const char *const netdev_lock_name[] = {
  274. "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
  275. "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
  276. "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
  277. "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
  278. "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
  279. "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
  280. "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
  281. "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
  282. "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
  283. "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
  284. "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
  285. "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
  286. "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
  287. "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
  288. "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
  289. static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
  290. static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
  291. static inline unsigned short netdev_lock_pos(unsigned short dev_type)
  292. {
  293. int i;
  294. for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
  295. if (netdev_lock_type[i] == dev_type)
  296. return i;
  297. /* the last key is used by default */
  298. return ARRAY_SIZE(netdev_lock_type) - 1;
  299. }
  300. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  301. unsigned short dev_type)
  302. {
  303. int i;
  304. i = netdev_lock_pos(dev_type);
  305. lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
  306. netdev_lock_name[i]);
  307. }
  308. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  309. {
  310. int i;
  311. i = netdev_lock_pos(dev->type);
  312. lockdep_set_class_and_name(&dev->addr_list_lock,
  313. &netdev_addr_lock_key[i],
  314. netdev_lock_name[i]);
  315. }
  316. #else
  317. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  318. unsigned short dev_type)
  319. {
  320. }
  321. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  322. {
  323. }
  324. #endif
  325. /*******************************************************************************
  326. *
  327. * Protocol management and registration routines
  328. *
  329. *******************************************************************************/
  330. /*
  331. * Add a protocol ID to the list. Now that the input handler is
  332. * smarter we can dispense with all the messy stuff that used to be
  333. * here.
  334. *
  335. * BEWARE!!! Protocol handlers, mangling input packets,
  336. * MUST BE last in hash buckets and checking protocol handlers
  337. * MUST start from promiscuous ptype_all chain in net_bh.
  338. * It is true now, do not change it.
  339. * Explanation follows: if protocol handler, mangling packet, will
  340. * be the first on list, it is not able to sense, that packet
  341. * is cloned and should be copied-on-write, so that it will
  342. * change it and subsequent readers will get broken packet.
  343. * --ANK (980803)
  344. */
  345. static inline struct list_head *ptype_head(const struct packet_type *pt)
  346. {
  347. if (pt->type == htons(ETH_P_ALL))
  348. return pt->dev ? &pt->dev->ptype_all : &ptype_all;
  349. else
  350. return pt->dev ? &pt->dev->ptype_specific :
  351. &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
  352. }
  353. /**
  354. * dev_add_pack - add packet handler
  355. * @pt: packet type declaration
  356. *
  357. * Add a protocol handler to the networking stack. The passed &packet_type
  358. * is linked into kernel lists and may not be freed until it has been
  359. * removed from the kernel lists.
  360. *
  361. * This call does not sleep therefore it can not
  362. * guarantee all CPU's that are in middle of receiving packets
  363. * will see the new packet type (until the next received packet).
  364. */
  365. void dev_add_pack(struct packet_type *pt)
  366. {
  367. struct list_head *head = ptype_head(pt);
  368. spin_lock(&ptype_lock);
  369. list_add_rcu(&pt->list, head);
  370. spin_unlock(&ptype_lock);
  371. }
  372. EXPORT_SYMBOL(dev_add_pack);
  373. /**
  374. * __dev_remove_pack - remove packet handler
  375. * @pt: packet type declaration
  376. *
  377. * Remove a protocol handler that was previously added to the kernel
  378. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  379. * from the kernel lists and can be freed or reused once this function
  380. * returns.
  381. *
  382. * The packet type might still be in use by receivers
  383. * and must not be freed until after all the CPU's have gone
  384. * through a quiescent state.
  385. */
  386. void __dev_remove_pack(struct packet_type *pt)
  387. {
  388. struct list_head *head = ptype_head(pt);
  389. struct packet_type *pt1;
  390. spin_lock(&ptype_lock);
  391. list_for_each_entry(pt1, head, list) {
  392. if (pt == pt1) {
  393. list_del_rcu(&pt->list);
  394. goto out;
  395. }
  396. }
  397. pr_warn("dev_remove_pack: %p not found\n", pt);
  398. out:
  399. spin_unlock(&ptype_lock);
  400. }
  401. EXPORT_SYMBOL(__dev_remove_pack);
  402. /**
  403. * dev_remove_pack - remove packet handler
  404. * @pt: packet type declaration
  405. *
  406. * Remove a protocol handler that was previously added to the kernel
  407. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  408. * from the kernel lists and can be freed or reused once this function
  409. * returns.
  410. *
  411. * This call sleeps to guarantee that no CPU is looking at the packet
  412. * type after return.
  413. */
  414. void dev_remove_pack(struct packet_type *pt)
  415. {
  416. __dev_remove_pack(pt);
  417. synchronize_net();
  418. }
  419. EXPORT_SYMBOL(dev_remove_pack);
  420. /**
  421. * dev_add_offload - register offload handlers
  422. * @po: protocol offload declaration
  423. *
  424. * Add protocol offload handlers to the networking stack. The passed
  425. * &proto_offload is linked into kernel lists and may not be freed until
  426. * it has been removed from the kernel lists.
  427. *
  428. * This call does not sleep therefore it can not
  429. * guarantee all CPU's that are in middle of receiving packets
  430. * will see the new offload handlers (until the next received packet).
  431. */
  432. void dev_add_offload(struct packet_offload *po)
  433. {
  434. struct packet_offload *elem;
  435. spin_lock(&offload_lock);
  436. list_for_each_entry(elem, &offload_base, list) {
  437. if (po->priority < elem->priority)
  438. break;
  439. }
  440. list_add_rcu(&po->list, elem->list.prev);
  441. spin_unlock(&offload_lock);
  442. }
  443. EXPORT_SYMBOL(dev_add_offload);
  444. /**
  445. * __dev_remove_offload - remove offload handler
  446. * @po: packet offload declaration
  447. *
  448. * Remove a protocol offload handler that was previously added to the
  449. * kernel offload handlers by dev_add_offload(). The passed &offload_type
  450. * is removed from the kernel lists and can be freed or reused once this
  451. * function returns.
  452. *
  453. * The packet type might still be in use by receivers
  454. * and must not be freed until after all the CPU's have gone
  455. * through a quiescent state.
  456. */
  457. static void __dev_remove_offload(struct packet_offload *po)
  458. {
  459. struct list_head *head = &offload_base;
  460. struct packet_offload *po1;
  461. spin_lock(&offload_lock);
  462. list_for_each_entry(po1, head, list) {
  463. if (po == po1) {
  464. list_del_rcu(&po->list);
  465. goto out;
  466. }
  467. }
  468. pr_warn("dev_remove_offload: %p not found\n", po);
  469. out:
  470. spin_unlock(&offload_lock);
  471. }
  472. /**
  473. * dev_remove_offload - remove packet offload handler
  474. * @po: packet offload declaration
  475. *
  476. * Remove a packet offload handler that was previously added to the kernel
  477. * offload handlers by dev_add_offload(). The passed &offload_type is
  478. * removed from the kernel lists and can be freed or reused once this
  479. * function returns.
  480. *
  481. * This call sleeps to guarantee that no CPU is looking at the packet
  482. * type after return.
  483. */
  484. void dev_remove_offload(struct packet_offload *po)
  485. {
  486. __dev_remove_offload(po);
  487. synchronize_net();
  488. }
  489. EXPORT_SYMBOL(dev_remove_offload);
  490. /******************************************************************************
  491. *
  492. * Device Boot-time Settings Routines
  493. *
  494. ******************************************************************************/
  495. /* Boot time configuration table */
  496. static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
  497. /**
  498. * netdev_boot_setup_add - add new setup entry
  499. * @name: name of the device
  500. * @map: configured settings for the device
  501. *
  502. * Adds new setup entry to the dev_boot_setup list. The function
  503. * returns 0 on error and 1 on success. This is a generic routine to
  504. * all netdevices.
  505. */
  506. static int netdev_boot_setup_add(char *name, struct ifmap *map)
  507. {
  508. struct netdev_boot_setup *s;
  509. int i;
  510. s = dev_boot_setup;
  511. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  512. if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
  513. memset(s[i].name, 0, sizeof(s[i].name));
  514. strlcpy(s[i].name, name, IFNAMSIZ);
  515. memcpy(&s[i].map, map, sizeof(s[i].map));
  516. break;
  517. }
  518. }
  519. return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
  520. }
  521. /**
  522. * netdev_boot_setup_check - check boot time settings
  523. * @dev: the netdevice
  524. *
  525. * Check boot time settings for the device.
  526. * The found settings are set for the device to be used
  527. * later in the device probing.
  528. * Returns 0 if no settings found, 1 if they are.
  529. */
  530. int netdev_boot_setup_check(struct net_device *dev)
  531. {
  532. struct netdev_boot_setup *s = dev_boot_setup;
  533. int i;
  534. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  535. if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
  536. !strcmp(dev->name, s[i].name)) {
  537. dev->irq = s[i].map.irq;
  538. dev->base_addr = s[i].map.base_addr;
  539. dev->mem_start = s[i].map.mem_start;
  540. dev->mem_end = s[i].map.mem_end;
  541. return 1;
  542. }
  543. }
  544. return 0;
  545. }
  546. EXPORT_SYMBOL(netdev_boot_setup_check);
  547. /**
  548. * netdev_boot_base - get address from boot time settings
  549. * @prefix: prefix for network device
  550. * @unit: id for network device
  551. *
  552. * Check boot time settings for the base address of device.
  553. * The found settings are set for the device to be used
  554. * later in the device probing.
  555. * Returns 0 if no settings found.
  556. */
  557. unsigned long netdev_boot_base(const char *prefix, int unit)
  558. {
  559. const struct netdev_boot_setup *s = dev_boot_setup;
  560. char name[IFNAMSIZ];
  561. int i;
  562. sprintf(name, "%s%d", prefix, unit);
  563. /*
  564. * If device already registered then return base of 1
  565. * to indicate not to probe for this interface
  566. */
  567. if (__dev_get_by_name(&init_net, name))
  568. return 1;
  569. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
  570. if (!strcmp(name, s[i].name))
  571. return s[i].map.base_addr;
  572. return 0;
  573. }
  574. /*
  575. * Saves at boot time configured settings for any netdevice.
  576. */
  577. int __init netdev_boot_setup(char *str)
  578. {
  579. int ints[5];
  580. struct ifmap map;
  581. str = get_options(str, ARRAY_SIZE(ints), ints);
  582. if (!str || !*str)
  583. return 0;
  584. /* Save settings */
  585. memset(&map, 0, sizeof(map));
  586. if (ints[0] > 0)
  587. map.irq = ints[1];
  588. if (ints[0] > 1)
  589. map.base_addr = ints[2];
  590. if (ints[0] > 2)
  591. map.mem_start = ints[3];
  592. if (ints[0] > 3)
  593. map.mem_end = ints[4];
  594. /* Add new entry to the list */
  595. return netdev_boot_setup_add(str, &map);
  596. }
  597. __setup("netdev=", netdev_boot_setup);
  598. /*******************************************************************************
  599. *
  600. * Device Interface Subroutines
  601. *
  602. *******************************************************************************/
  603. /**
  604. * dev_get_iflink - get 'iflink' value of a interface
  605. * @dev: targeted interface
  606. *
  607. * Indicates the ifindex the interface is linked to.
  608. * Physical interfaces have the same 'ifindex' and 'iflink' values.
  609. */
  610. int dev_get_iflink(const struct net_device *dev)
  611. {
  612. if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
  613. return dev->netdev_ops->ndo_get_iflink(dev);
  614. return dev->ifindex;
  615. }
  616. EXPORT_SYMBOL(dev_get_iflink);
  617. /**
  618. * dev_fill_metadata_dst - Retrieve tunnel egress information.
  619. * @dev: targeted interface
  620. * @skb: The packet.
  621. *
  622. * For better visibility of tunnel traffic OVS needs to retrieve
  623. * egress tunnel information for a packet. Following API allows
  624. * user to get this info.
  625. */
  626. int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
  627. {
  628. struct ip_tunnel_info *info;
  629. if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
  630. return -EINVAL;
  631. info = skb_tunnel_info_unclone(skb);
  632. if (!info)
  633. return -ENOMEM;
  634. if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
  635. return -EINVAL;
  636. return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
  637. }
  638. EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
  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 net_device *dev;
  653. struct hlist_head *head = dev_name_hash(net, name);
  654. hlist_for_each_entry(dev, head, name_hlist)
  655. if (!strncmp(dev->name, name, IFNAMSIZ))
  656. return dev;
  657. return NULL;
  658. }
  659. EXPORT_SYMBOL(__dev_get_by_name);
  660. /**
  661. * dev_get_by_name_rcu - find a device by its name
  662. * @net: the applicable net namespace
  663. * @name: name to find
  664. *
  665. * Find an interface by name.
  666. * If the name is found a pointer to the device is returned.
  667. * If the name is not found then %NULL is returned.
  668. * The reference counters are not incremented so the caller must be
  669. * careful with locks. The caller must hold RCU lock.
  670. */
  671. struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
  672. {
  673. struct net_device *dev;
  674. struct hlist_head *head = dev_name_hash(net, name);
  675. hlist_for_each_entry_rcu(dev, head, name_hlist)
  676. if (!strncmp(dev->name, name, IFNAMSIZ))
  677. return dev;
  678. return NULL;
  679. }
  680. EXPORT_SYMBOL(dev_get_by_name_rcu);
  681. /**
  682. * dev_get_by_name - find a device by its name
  683. * @net: the applicable net namespace
  684. * @name: name to find
  685. *
  686. * Find an interface by name. This can be called from any
  687. * context and does its own locking. The returned handle has
  688. * the usage count incremented and the caller must use dev_put() to
  689. * release it when it is no longer needed. %NULL is returned if no
  690. * matching device is found.
  691. */
  692. struct net_device *dev_get_by_name(struct net *net, const char *name)
  693. {
  694. struct net_device *dev;
  695. rcu_read_lock();
  696. dev = dev_get_by_name_rcu(net, name);
  697. if (dev)
  698. dev_hold(dev);
  699. rcu_read_unlock();
  700. return dev;
  701. }
  702. EXPORT_SYMBOL(dev_get_by_name);
  703. /**
  704. * __dev_get_by_index - find a device by its ifindex
  705. * @net: the applicable net namespace
  706. * @ifindex: index of device
  707. *
  708. * Search for an interface by index. Returns %NULL if the device
  709. * is not found or a pointer to the device. The device has not
  710. * had its reference counter increased so the caller must be careful
  711. * about locking. The caller must hold either the RTNL semaphore
  712. * or @dev_base_lock.
  713. */
  714. struct net_device *__dev_get_by_index(struct net *net, int ifindex)
  715. {
  716. struct net_device *dev;
  717. struct hlist_head *head = dev_index_hash(net, ifindex);
  718. hlist_for_each_entry(dev, head, index_hlist)
  719. if (dev->ifindex == ifindex)
  720. return dev;
  721. return NULL;
  722. }
  723. EXPORT_SYMBOL(__dev_get_by_index);
  724. /**
  725. * dev_get_by_index_rcu - find a device by its ifindex
  726. * @net: the applicable net namespace
  727. * @ifindex: index of device
  728. *
  729. * Search for an interface by index. Returns %NULL if the device
  730. * is not found or a pointer to the device. The device has not
  731. * had its reference counter increased so the caller must be careful
  732. * about locking. The caller must hold RCU lock.
  733. */
  734. struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
  735. {
  736. struct net_device *dev;
  737. struct hlist_head *head = dev_index_hash(net, ifindex);
  738. hlist_for_each_entry_rcu(dev, head, index_hlist)
  739. if (dev->ifindex == ifindex)
  740. return dev;
  741. return NULL;
  742. }
  743. EXPORT_SYMBOL(dev_get_by_index_rcu);
  744. /**
  745. * dev_get_by_index - find a device by its ifindex
  746. * @net: the applicable net namespace
  747. * @ifindex: index of device
  748. *
  749. * Search for an interface by index. Returns NULL if the device
  750. * is not found or a pointer to the device. The device returned has
  751. * had a reference added and the pointer is safe until the user calls
  752. * dev_put to indicate they have finished with it.
  753. */
  754. struct net_device *dev_get_by_index(struct net *net, int ifindex)
  755. {
  756. struct net_device *dev;
  757. rcu_read_lock();
  758. dev = dev_get_by_index_rcu(net, ifindex);
  759. if (dev)
  760. dev_hold(dev);
  761. rcu_read_unlock();
  762. return dev;
  763. }
  764. EXPORT_SYMBOL(dev_get_by_index);
  765. /**
  766. * dev_get_by_napi_id - find a device by napi_id
  767. * @napi_id: ID of the NAPI struct
  768. *
  769. * Search for an interface by NAPI ID. Returns %NULL if the device
  770. * is not found or a pointer to the device. The device has not had
  771. * its reference counter increased so the caller must be careful
  772. * about locking. The caller must hold RCU lock.
  773. */
  774. struct net_device *dev_get_by_napi_id(unsigned int napi_id)
  775. {
  776. struct napi_struct *napi;
  777. WARN_ON_ONCE(!rcu_read_lock_held());
  778. if (napi_id < MIN_NAPI_ID)
  779. return NULL;
  780. napi = napi_by_id(napi_id);
  781. return napi ? napi->dev : NULL;
  782. }
  783. EXPORT_SYMBOL(dev_get_by_napi_id);
  784. /**
  785. * netdev_get_name - get a netdevice name, knowing its ifindex.
  786. * @net: network namespace
  787. * @name: a pointer to the buffer where the name will be stored.
  788. * @ifindex: the ifindex of the interface to get the name from.
  789. *
  790. * The use of raw_seqcount_begin() and cond_resched() before
  791. * retrying is required as we want to give the writers a chance
  792. * to complete when CONFIG_PREEMPT is not set.
  793. */
  794. int netdev_get_name(struct net *net, char *name, int ifindex)
  795. {
  796. struct net_device *dev;
  797. unsigned int seq;
  798. retry:
  799. seq = raw_seqcount_begin(&devnet_rename_seq);
  800. rcu_read_lock();
  801. dev = dev_get_by_index_rcu(net, ifindex);
  802. if (!dev) {
  803. rcu_read_unlock();
  804. return -ENODEV;
  805. }
  806. strcpy(name, dev->name);
  807. rcu_read_unlock();
  808. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  809. cond_resched();
  810. goto retry;
  811. }
  812. return 0;
  813. }
  814. /**
  815. * dev_getbyhwaddr_rcu - find a device by its hardware address
  816. * @net: the applicable net namespace
  817. * @type: media type of device
  818. * @ha: hardware address
  819. *
  820. * Search for an interface by MAC address. Returns NULL if the device
  821. * is not found or a pointer to the device.
  822. * The caller must hold RCU or RTNL.
  823. * The returned device has not had its ref count increased
  824. * and the caller must therefore be careful about locking
  825. *
  826. */
  827. struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
  828. const char *ha)
  829. {
  830. struct net_device *dev;
  831. for_each_netdev_rcu(net, dev)
  832. if (dev->type == type &&
  833. !memcmp(dev->dev_addr, ha, dev->addr_len))
  834. return dev;
  835. return NULL;
  836. }
  837. EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
  838. struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
  839. {
  840. struct net_device *dev;
  841. ASSERT_RTNL();
  842. for_each_netdev(net, dev)
  843. if (dev->type == type)
  844. return dev;
  845. return NULL;
  846. }
  847. EXPORT_SYMBOL(__dev_getfirstbyhwtype);
  848. struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
  849. {
  850. struct net_device *dev, *ret = NULL;
  851. rcu_read_lock();
  852. for_each_netdev_rcu(net, dev)
  853. if (dev->type == type) {
  854. dev_hold(dev);
  855. ret = dev;
  856. break;
  857. }
  858. rcu_read_unlock();
  859. return ret;
  860. }
  861. EXPORT_SYMBOL(dev_getfirstbyhwtype);
  862. /**
  863. * __dev_get_by_flags - find any device with given flags
  864. * @net: the applicable net namespace
  865. * @if_flags: IFF_* values
  866. * @mask: bitmask of bits in if_flags to check
  867. *
  868. * Search for any interface with the given flags. Returns NULL if a device
  869. * is not found or a pointer to the device. Must be called inside
  870. * rtnl_lock(), and result refcount is unchanged.
  871. */
  872. struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
  873. unsigned short mask)
  874. {
  875. struct net_device *dev, *ret;
  876. ASSERT_RTNL();
  877. ret = NULL;
  878. for_each_netdev(net, dev) {
  879. if (((dev->flags ^ if_flags) & mask) == 0) {
  880. ret = dev;
  881. break;
  882. }
  883. }
  884. return ret;
  885. }
  886. EXPORT_SYMBOL(__dev_get_by_flags);
  887. /**
  888. * dev_valid_name - check if name is okay for network device
  889. * @name: name string
  890. *
  891. * Network device names need to be valid file names to
  892. * to allow sysfs to work. We also disallow any kind of
  893. * whitespace.
  894. */
  895. bool dev_valid_name(const char *name)
  896. {
  897. if (*name == '\0')
  898. return false;
  899. if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
  900. return false;
  901. if (!strcmp(name, ".") || !strcmp(name, ".."))
  902. return false;
  903. while (*name) {
  904. if (*name == '/' || *name == ':' || isspace(*name))
  905. return false;
  906. name++;
  907. }
  908. return true;
  909. }
  910. EXPORT_SYMBOL(dev_valid_name);
  911. /**
  912. * __dev_alloc_name - allocate a name for a device
  913. * @net: network namespace to allocate the device name in
  914. * @name: name format string
  915. * @buf: scratch buffer and result name string
  916. *
  917. * Passed a format string - eg "lt%d" it will try and find a suitable
  918. * id. It scans list of devices to build up a free map, then chooses
  919. * the first empty slot. The caller must hold the dev_base or rtnl lock
  920. * while allocating the name and adding the device in order to avoid
  921. * duplicates.
  922. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  923. * Returns the number of the unit assigned or a negative errno code.
  924. */
  925. static int __dev_alloc_name(struct net *net, const char *name, char *buf)
  926. {
  927. int i = 0;
  928. const char *p;
  929. const int max_netdevices = 8*PAGE_SIZE;
  930. unsigned long *inuse;
  931. struct net_device *d;
  932. if (!dev_valid_name(name))
  933. return -EINVAL;
  934. p = strchr(name, '%');
  935. if (p) {
  936. /*
  937. * Verify the string as this thing may have come from
  938. * the user. There must be either one "%d" and no other "%"
  939. * characters.
  940. */
  941. if (p[1] != 'd' || strchr(p + 2, '%'))
  942. return -EINVAL;
  943. /* Use one page as a bit array of possible slots */
  944. inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
  945. if (!inuse)
  946. return -ENOMEM;
  947. for_each_netdev(net, d) {
  948. if (!sscanf(d->name, name, &i))
  949. continue;
  950. if (i < 0 || i >= max_netdevices)
  951. continue;
  952. /* avoid cases where sscanf is not exact inverse of printf */
  953. snprintf(buf, IFNAMSIZ, name, i);
  954. if (!strncmp(buf, d->name, IFNAMSIZ))
  955. set_bit(i, inuse);
  956. }
  957. i = find_first_zero_bit(inuse, max_netdevices);
  958. free_page((unsigned long) inuse);
  959. }
  960. snprintf(buf, IFNAMSIZ, name, i);
  961. if (!__dev_get_by_name(net, buf))
  962. return i;
  963. /* It is possible to run out of possible slots
  964. * when the name is long and there isn't enough space left
  965. * for the digits, or if all bits are used.
  966. */
  967. return -ENFILE;
  968. }
  969. static int dev_alloc_name_ns(struct net *net,
  970. struct net_device *dev,
  971. const char *name)
  972. {
  973. char buf[IFNAMSIZ];
  974. int ret;
  975. BUG_ON(!net);
  976. ret = __dev_alloc_name(net, name, buf);
  977. if (ret >= 0)
  978. strlcpy(dev->name, buf, IFNAMSIZ);
  979. return ret;
  980. }
  981. /**
  982. * dev_alloc_name - allocate a name for a device
  983. * @dev: device
  984. * @name: name format string
  985. *
  986. * Passed a format string - eg "lt%d" it will try and find a suitable
  987. * id. It scans list of devices to build up a free map, then chooses
  988. * the first empty slot. The caller must hold the dev_base or rtnl lock
  989. * while allocating the name and adding the device in order to avoid
  990. * duplicates.
  991. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  992. * Returns the number of the unit assigned or a negative errno code.
  993. */
  994. int dev_alloc_name(struct net_device *dev, const char *name)
  995. {
  996. return dev_alloc_name_ns(dev_net(dev), dev, name);
  997. }
  998. EXPORT_SYMBOL(dev_alloc_name);
  999. int dev_get_valid_name(struct net *net, struct net_device *dev,
  1000. const char *name)
  1001. {
  1002. BUG_ON(!net);
  1003. if (!dev_valid_name(name))
  1004. return -EINVAL;
  1005. if (strchr(name, '%'))
  1006. return dev_alloc_name_ns(net, dev, name);
  1007. else if (__dev_get_by_name(net, name))
  1008. return -EEXIST;
  1009. else if (dev->name != name)
  1010. strlcpy(dev->name, name, IFNAMSIZ);
  1011. return 0;
  1012. }
  1013. EXPORT_SYMBOL(dev_get_valid_name);
  1014. /**
  1015. * dev_change_name - change name of a device
  1016. * @dev: device
  1017. * @newname: name (or format string) must be at least IFNAMSIZ
  1018. *
  1019. * Change name of a device, can pass format strings "eth%d".
  1020. * for wildcarding.
  1021. */
  1022. int dev_change_name(struct net_device *dev, const char *newname)
  1023. {
  1024. unsigned char old_assign_type;
  1025. char oldname[IFNAMSIZ];
  1026. int err = 0;
  1027. int ret;
  1028. struct net *net;
  1029. ASSERT_RTNL();
  1030. BUG_ON(!dev_net(dev));
  1031. net = dev_net(dev);
  1032. if (dev->flags & IFF_UP)
  1033. return -EBUSY;
  1034. write_seqcount_begin(&devnet_rename_seq);
  1035. if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  1036. write_seqcount_end(&devnet_rename_seq);
  1037. return 0;
  1038. }
  1039. memcpy(oldname, dev->name, IFNAMSIZ);
  1040. err = dev_get_valid_name(net, dev, newname);
  1041. if (err < 0) {
  1042. write_seqcount_end(&devnet_rename_seq);
  1043. return err;
  1044. }
  1045. if (oldname[0] && !strchr(oldname, '%'))
  1046. netdev_info(dev, "renamed from %s\n", oldname);
  1047. old_assign_type = dev->name_assign_type;
  1048. dev->name_assign_type = NET_NAME_RENAMED;
  1049. rollback:
  1050. ret = device_rename(&dev->dev, dev->name);
  1051. if (ret) {
  1052. memcpy(dev->name, oldname, IFNAMSIZ);
  1053. dev->name_assign_type = old_assign_type;
  1054. write_seqcount_end(&devnet_rename_seq);
  1055. return ret;
  1056. }
  1057. write_seqcount_end(&devnet_rename_seq);
  1058. netdev_adjacent_rename_links(dev, oldname);
  1059. write_lock_bh(&dev_base_lock);
  1060. hlist_del_rcu(&dev->name_hlist);
  1061. write_unlock_bh(&dev_base_lock);
  1062. synchronize_rcu();
  1063. write_lock_bh(&dev_base_lock);
  1064. hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
  1065. write_unlock_bh(&dev_base_lock);
  1066. ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
  1067. ret = notifier_to_errno(ret);
  1068. if (ret) {
  1069. /* err >= 0 after dev_alloc_name() or stores the first errno */
  1070. if (err >= 0) {
  1071. err = ret;
  1072. write_seqcount_begin(&devnet_rename_seq);
  1073. memcpy(dev->name, oldname, IFNAMSIZ);
  1074. memcpy(oldname, newname, IFNAMSIZ);
  1075. dev->name_assign_type = old_assign_type;
  1076. old_assign_type = NET_NAME_RENAMED;
  1077. goto rollback;
  1078. } else {
  1079. pr_err("%s: name change rollback failed: %d\n",
  1080. dev->name, ret);
  1081. }
  1082. }
  1083. return err;
  1084. }
  1085. /**
  1086. * dev_set_alias - change ifalias of a device
  1087. * @dev: device
  1088. * @alias: name up to IFALIASZ
  1089. * @len: limit of bytes to copy from info
  1090. *
  1091. * Set ifalias for a device,
  1092. */
  1093. int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
  1094. {
  1095. struct dev_ifalias *new_alias = NULL;
  1096. if (len >= IFALIASZ)
  1097. return -EINVAL;
  1098. if (len) {
  1099. new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
  1100. if (!new_alias)
  1101. return -ENOMEM;
  1102. memcpy(new_alias->ifalias, alias, len);
  1103. new_alias->ifalias[len] = 0;
  1104. }
  1105. mutex_lock(&ifalias_mutex);
  1106. rcu_swap_protected(dev->ifalias, new_alias,
  1107. mutex_is_locked(&ifalias_mutex));
  1108. mutex_unlock(&ifalias_mutex);
  1109. if (new_alias)
  1110. kfree_rcu(new_alias, rcuhead);
  1111. return len;
  1112. }
  1113. EXPORT_SYMBOL(dev_set_alias);
  1114. /**
  1115. * dev_get_alias - get ifalias of a device
  1116. * @dev: device
  1117. * @name: buffer to store name of ifalias
  1118. * @len: size of buffer
  1119. *
  1120. * get ifalias for a device. Caller must make sure dev cannot go
  1121. * away, e.g. rcu read lock or own a reference count to device.
  1122. */
  1123. int dev_get_alias(const struct net_device *dev, char *name, size_t len)
  1124. {
  1125. const struct dev_ifalias *alias;
  1126. int ret = 0;
  1127. rcu_read_lock();
  1128. alias = rcu_dereference(dev->ifalias);
  1129. if (alias)
  1130. ret = snprintf(name, len, "%s", alias->ifalias);
  1131. rcu_read_unlock();
  1132. return ret;
  1133. }
  1134. /**
  1135. * netdev_features_change - device changes features
  1136. * @dev: device to cause notification
  1137. *
  1138. * Called to indicate a device has changed features.
  1139. */
  1140. void netdev_features_change(struct net_device *dev)
  1141. {
  1142. call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
  1143. }
  1144. EXPORT_SYMBOL(netdev_features_change);
  1145. /**
  1146. * netdev_state_change - device changes state
  1147. * @dev: device to cause notification
  1148. *
  1149. * Called to indicate a device has changed state. This function calls
  1150. * the notifier chains for netdev_chain and sends a NEWLINK message
  1151. * to the routing socket.
  1152. */
  1153. void netdev_state_change(struct net_device *dev)
  1154. {
  1155. if (dev->flags & IFF_UP) {
  1156. struct netdev_notifier_change_info change_info = {
  1157. .info.dev = dev,
  1158. };
  1159. call_netdevice_notifiers_info(NETDEV_CHANGE,
  1160. &change_info.info);
  1161. rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
  1162. }
  1163. }
  1164. EXPORT_SYMBOL(netdev_state_change);
  1165. /**
  1166. * netdev_notify_peers - notify network peers about existence of @dev
  1167. * @dev: network device
  1168. *
  1169. * Generate traffic such that interested network peers are aware of
  1170. * @dev, such as by generating a gratuitous ARP. This may be used when
  1171. * a device wants to inform the rest of the network about some sort of
  1172. * reconfiguration such as a failover event or virtual machine
  1173. * migration.
  1174. */
  1175. void netdev_notify_peers(struct net_device *dev)
  1176. {
  1177. rtnl_lock();
  1178. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
  1179. call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
  1180. rtnl_unlock();
  1181. }
  1182. EXPORT_SYMBOL(netdev_notify_peers);
  1183. static int __dev_open(struct net_device *dev)
  1184. {
  1185. const struct net_device_ops *ops = dev->netdev_ops;
  1186. int ret;
  1187. ASSERT_RTNL();
  1188. if (!netif_device_present(dev))
  1189. return -ENODEV;
  1190. /* Block netpoll from trying to do any rx path servicing.
  1191. * If we don't do this there is a chance ndo_poll_controller
  1192. * or ndo_poll may be running while we open the device
  1193. */
  1194. netpoll_poll_disable(dev);
  1195. ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
  1196. ret = notifier_to_errno(ret);
  1197. if (ret)
  1198. return ret;
  1199. set_bit(__LINK_STATE_START, &dev->state);
  1200. if (ops->ndo_validate_addr)
  1201. ret = ops->ndo_validate_addr(dev);
  1202. if (!ret && ops->ndo_open)
  1203. ret = ops->ndo_open(dev);
  1204. netpoll_poll_enable(dev);
  1205. if (ret)
  1206. clear_bit(__LINK_STATE_START, &dev->state);
  1207. else {
  1208. dev->flags |= IFF_UP;
  1209. dev_set_rx_mode(dev);
  1210. dev_activate(dev);
  1211. add_device_randomness(dev->dev_addr, dev->addr_len);
  1212. }
  1213. return ret;
  1214. }
  1215. /**
  1216. * dev_open - prepare an interface for use.
  1217. * @dev: device to open
  1218. *
  1219. * Takes a device from down to up state. The device's private open
  1220. * function is invoked and then the multicast lists are loaded. Finally
  1221. * the device is moved into the up state and a %NETDEV_UP message is
  1222. * sent to the netdev notifier chain.
  1223. *
  1224. * Calling this function on an active interface is a nop. On a failure
  1225. * a negative errno code is returned.
  1226. */
  1227. int dev_open(struct net_device *dev)
  1228. {
  1229. int ret;
  1230. if (dev->flags & IFF_UP)
  1231. return 0;
  1232. ret = __dev_open(dev);
  1233. if (ret < 0)
  1234. return ret;
  1235. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
  1236. call_netdevice_notifiers(NETDEV_UP, dev);
  1237. return ret;
  1238. }
  1239. EXPORT_SYMBOL(dev_open);
  1240. static void __dev_close_many(struct list_head *head)
  1241. {
  1242. struct net_device *dev;
  1243. ASSERT_RTNL();
  1244. might_sleep();
  1245. list_for_each_entry(dev, head, close_list) {
  1246. /* Temporarily disable netpoll until the interface is down */
  1247. netpoll_poll_disable(dev);
  1248. call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
  1249. clear_bit(__LINK_STATE_START, &dev->state);
  1250. /* Synchronize to scheduled poll. We cannot touch poll list, it
  1251. * can be even on different cpu. So just clear netif_running().
  1252. *
  1253. * dev->stop() will invoke napi_disable() on all of it's
  1254. * napi_struct instances on this device.
  1255. */
  1256. smp_mb__after_atomic(); /* Commit netif_running(). */
  1257. }
  1258. dev_deactivate_many(head);
  1259. list_for_each_entry(dev, head, close_list) {
  1260. const struct net_device_ops *ops = dev->netdev_ops;
  1261. /*
  1262. * Call the device specific close. This cannot fail.
  1263. * Only if device is UP
  1264. *
  1265. * We allow it to be called even after a DETACH hot-plug
  1266. * event.
  1267. */
  1268. if (ops->ndo_stop)
  1269. ops->ndo_stop(dev);
  1270. dev->flags &= ~IFF_UP;
  1271. netpoll_poll_enable(dev);
  1272. }
  1273. }
  1274. static void __dev_close(struct net_device *dev)
  1275. {
  1276. LIST_HEAD(single);
  1277. list_add(&dev->close_list, &single);
  1278. __dev_close_many(&single);
  1279. list_del(&single);
  1280. }
  1281. void dev_close_many(struct list_head *head, bool unlink)
  1282. {
  1283. struct net_device *dev, *tmp;
  1284. /* Remove the devices that don't need to be closed */
  1285. list_for_each_entry_safe(dev, tmp, head, close_list)
  1286. if (!(dev->flags & IFF_UP))
  1287. list_del_init(&dev->close_list);
  1288. __dev_close_many(head);
  1289. list_for_each_entry_safe(dev, tmp, head, close_list) {
  1290. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
  1291. call_netdevice_notifiers(NETDEV_DOWN, dev);
  1292. if (unlink)
  1293. list_del_init(&dev->close_list);
  1294. }
  1295. }
  1296. EXPORT_SYMBOL(dev_close_many);
  1297. /**
  1298. * dev_close - shutdown an interface.
  1299. * @dev: device to shutdown
  1300. *
  1301. * This function moves an active device into down state. A
  1302. * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
  1303. * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
  1304. * chain.
  1305. */
  1306. void dev_close(struct net_device *dev)
  1307. {
  1308. if (dev->flags & IFF_UP) {
  1309. LIST_HEAD(single);
  1310. list_add(&dev->close_list, &single);
  1311. dev_close_many(&single, true);
  1312. list_del(&single);
  1313. }
  1314. }
  1315. EXPORT_SYMBOL(dev_close);
  1316. /**
  1317. * dev_disable_lro - disable Large Receive Offload on a device
  1318. * @dev: device
  1319. *
  1320. * Disable Large Receive Offload (LRO) on a net device. Must be
  1321. * called under RTNL. This is needed if received packets may be
  1322. * forwarded to another interface.
  1323. */
  1324. void dev_disable_lro(struct net_device *dev)
  1325. {
  1326. struct net_device *lower_dev;
  1327. struct list_head *iter;
  1328. dev->wanted_features &= ~NETIF_F_LRO;
  1329. netdev_update_features(dev);
  1330. if (unlikely(dev->features & NETIF_F_LRO))
  1331. netdev_WARN(dev, "failed to disable LRO!\n");
  1332. netdev_for_each_lower_dev(dev, lower_dev, iter)
  1333. dev_disable_lro(lower_dev);
  1334. }
  1335. EXPORT_SYMBOL(dev_disable_lro);
  1336. /**
  1337. * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
  1338. * @dev: device
  1339. *
  1340. * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
  1341. * called under RTNL. This is needed if Generic XDP is installed on
  1342. * the device.
  1343. */
  1344. static void dev_disable_gro_hw(struct net_device *dev)
  1345. {
  1346. dev->wanted_features &= ~NETIF_F_GRO_HW;
  1347. netdev_update_features(dev);
  1348. if (unlikely(dev->features & NETIF_F_GRO_HW))
  1349. netdev_WARN(dev, "failed to disable GRO_HW!\n");
  1350. }
  1351. const char *netdev_cmd_to_name(enum netdev_cmd cmd)
  1352. {
  1353. #define N(val) \
  1354. case NETDEV_##val: \
  1355. return "NETDEV_" __stringify(val);
  1356. switch (cmd) {
  1357. N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
  1358. N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
  1359. N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
  1360. N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
  1361. N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
  1362. N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
  1363. N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
  1364. N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
  1365. N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
  1366. }
  1367. #undef N
  1368. return "UNKNOWN_NETDEV_EVENT";
  1369. }
  1370. EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
  1371. static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
  1372. struct net_device *dev)
  1373. {
  1374. struct netdev_notifier_info info = {
  1375. .dev = dev,
  1376. };
  1377. return nb->notifier_call(nb, val, &info);
  1378. }
  1379. static int dev_boot_phase = 1;
  1380. /**
  1381. * register_netdevice_notifier - register a network notifier block
  1382. * @nb: notifier
  1383. *
  1384. * Register a notifier to be called when network device events occur.
  1385. * The notifier passed is linked into the kernel structures and must
  1386. * not be reused until it has been unregistered. A negative errno code
  1387. * is returned on a failure.
  1388. *
  1389. * When registered all registration and up events are replayed
  1390. * to the new notifier to allow device to have a race free
  1391. * view of the network device list.
  1392. */
  1393. int register_netdevice_notifier(struct notifier_block *nb)
  1394. {
  1395. struct net_device *dev;
  1396. struct net_device *last;
  1397. struct net *net;
  1398. int err;
  1399. /* Close race with setup_net() and cleanup_net() */
  1400. down_write(&pernet_ops_rwsem);
  1401. rtnl_lock();
  1402. err = raw_notifier_chain_register(&netdev_chain, nb);
  1403. if (err)
  1404. goto unlock;
  1405. if (dev_boot_phase)
  1406. goto unlock;
  1407. for_each_net(net) {
  1408. for_each_netdev(net, dev) {
  1409. err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
  1410. err = notifier_to_errno(err);
  1411. if (err)
  1412. goto rollback;
  1413. if (!(dev->flags & IFF_UP))
  1414. continue;
  1415. call_netdevice_notifier(nb, NETDEV_UP, dev);
  1416. }
  1417. }
  1418. unlock:
  1419. rtnl_unlock();
  1420. up_write(&pernet_ops_rwsem);
  1421. return err;
  1422. rollback:
  1423. last = dev;
  1424. for_each_net(net) {
  1425. for_each_netdev(net, dev) {
  1426. if (dev == last)
  1427. goto outroll;
  1428. if (dev->flags & IFF_UP) {
  1429. call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
  1430. dev);
  1431. call_netdevice_notifier(nb, NETDEV_DOWN, dev);
  1432. }
  1433. call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
  1434. }
  1435. }
  1436. outroll:
  1437. raw_notifier_chain_unregister(&netdev_chain, nb);
  1438. goto unlock;
  1439. }
  1440. EXPORT_SYMBOL(register_netdevice_notifier);
  1441. /**
  1442. * unregister_netdevice_notifier - unregister a network notifier block
  1443. * @nb: notifier
  1444. *
  1445. * Unregister a notifier previously registered by
  1446. * register_netdevice_notifier(). The notifier is unlinked into the
  1447. * kernel structures and may then be reused. A negative errno code
  1448. * is returned on a failure.
  1449. *
  1450. * After unregistering unregister and down device events are synthesized
  1451. * for all devices on the device list to the removed notifier to remove
  1452. * the need for special case cleanup code.
  1453. */
  1454. int unregister_netdevice_notifier(struct notifier_block *nb)
  1455. {
  1456. struct net_device *dev;
  1457. struct net *net;
  1458. int err;
  1459. /* Close race with setup_net() and cleanup_net() */
  1460. down_write(&pernet_ops_rwsem);
  1461. rtnl_lock();
  1462. err = raw_notifier_chain_unregister(&netdev_chain, nb);
  1463. if (err)
  1464. goto unlock;
  1465. for_each_net(net) {
  1466. for_each_netdev(net, dev) {
  1467. if (dev->flags & IFF_UP) {
  1468. call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
  1469. dev);
  1470. call_netdevice_notifier(nb, NETDEV_DOWN, dev);
  1471. }
  1472. call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
  1473. }
  1474. }
  1475. unlock:
  1476. rtnl_unlock();
  1477. up_write(&pernet_ops_rwsem);
  1478. return err;
  1479. }
  1480. EXPORT_SYMBOL(unregister_netdevice_notifier);
  1481. /**
  1482. * call_netdevice_notifiers_info - call all network notifier blocks
  1483. * @val: value passed unmodified to notifier function
  1484. * @info: notifier information data
  1485. *
  1486. * Call all network notifier blocks. Parameters and return value
  1487. * are as for raw_notifier_call_chain().
  1488. */
  1489. static int call_netdevice_notifiers_info(unsigned long val,
  1490. struct netdev_notifier_info *info)
  1491. {
  1492. ASSERT_RTNL();
  1493. return raw_notifier_call_chain(&netdev_chain, val, info);
  1494. }
  1495. /**
  1496. * call_netdevice_notifiers - call all network notifier blocks
  1497. * @val: value passed unmodified to notifier function
  1498. * @dev: net_device pointer passed unmodified to notifier function
  1499. *
  1500. * Call all network notifier blocks. Parameters and return value
  1501. * are as for raw_notifier_call_chain().
  1502. */
  1503. int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
  1504. {
  1505. struct netdev_notifier_info info = {
  1506. .dev = dev,
  1507. };
  1508. return call_netdevice_notifiers_info(val, &info);
  1509. }
  1510. EXPORT_SYMBOL(call_netdevice_notifiers);
  1511. #ifdef CONFIG_NET_INGRESS
  1512. static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
  1513. void net_inc_ingress_queue(void)
  1514. {
  1515. static_branch_inc(&ingress_needed_key);
  1516. }
  1517. EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
  1518. void net_dec_ingress_queue(void)
  1519. {
  1520. static_branch_dec(&ingress_needed_key);
  1521. }
  1522. EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
  1523. #endif
  1524. #ifdef CONFIG_NET_EGRESS
  1525. static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
  1526. void net_inc_egress_queue(void)
  1527. {
  1528. static_branch_inc(&egress_needed_key);
  1529. }
  1530. EXPORT_SYMBOL_GPL(net_inc_egress_queue);
  1531. void net_dec_egress_queue(void)
  1532. {
  1533. static_branch_dec(&egress_needed_key);
  1534. }
  1535. EXPORT_SYMBOL_GPL(net_dec_egress_queue);
  1536. #endif
  1537. static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
  1538. #ifdef HAVE_JUMP_LABEL
  1539. static atomic_t netstamp_needed_deferred;
  1540. static atomic_t netstamp_wanted;
  1541. static void netstamp_clear(struct work_struct *work)
  1542. {
  1543. int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
  1544. int wanted;
  1545. wanted = atomic_add_return(deferred, &netstamp_wanted);
  1546. if (wanted > 0)
  1547. static_branch_enable(&netstamp_needed_key);
  1548. else
  1549. static_branch_disable(&netstamp_needed_key);
  1550. }
  1551. static DECLARE_WORK(netstamp_work, netstamp_clear);
  1552. #endif
  1553. void net_enable_timestamp(void)
  1554. {
  1555. #ifdef HAVE_JUMP_LABEL
  1556. int wanted;
  1557. while (1) {
  1558. wanted = atomic_read(&netstamp_wanted);
  1559. if (wanted <= 0)
  1560. break;
  1561. if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
  1562. return;
  1563. }
  1564. atomic_inc(&netstamp_needed_deferred);
  1565. schedule_work(&netstamp_work);
  1566. #else
  1567. static_branch_inc(&netstamp_needed_key);
  1568. #endif
  1569. }
  1570. EXPORT_SYMBOL(net_enable_timestamp);
  1571. void net_disable_timestamp(void)
  1572. {
  1573. #ifdef HAVE_JUMP_LABEL
  1574. int wanted;
  1575. while (1) {
  1576. wanted = atomic_read(&netstamp_wanted);
  1577. if (wanted <= 1)
  1578. break;
  1579. if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
  1580. return;
  1581. }
  1582. atomic_dec(&netstamp_needed_deferred);
  1583. schedule_work(&netstamp_work);
  1584. #else
  1585. static_branch_dec(&netstamp_needed_key);
  1586. #endif
  1587. }
  1588. EXPORT_SYMBOL(net_disable_timestamp);
  1589. static inline void net_timestamp_set(struct sk_buff *skb)
  1590. {
  1591. skb->tstamp = 0;
  1592. if (static_branch_unlikely(&netstamp_needed_key))
  1593. __net_timestamp(skb);
  1594. }
  1595. #define net_timestamp_check(COND, SKB) \
  1596. if (static_branch_unlikely(&netstamp_needed_key)) { \
  1597. if ((COND) && !(SKB)->tstamp) \
  1598. __net_timestamp(SKB); \
  1599. } \
  1600. bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
  1601. {
  1602. unsigned int len;
  1603. if (!(dev->flags & IFF_UP))
  1604. return false;
  1605. len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
  1606. if (skb->len <= len)
  1607. return true;
  1608. /* if TSO is enabled, we don't care about the length as the packet
  1609. * could be forwarded without being segmented before
  1610. */
  1611. if (skb_is_gso(skb))
  1612. return true;
  1613. return false;
  1614. }
  1615. EXPORT_SYMBOL_GPL(is_skb_forwardable);
  1616. int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
  1617. {
  1618. int ret = ____dev_forward_skb(dev, skb);
  1619. if (likely(!ret)) {
  1620. skb->protocol = eth_type_trans(skb, dev);
  1621. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  1622. }
  1623. return ret;
  1624. }
  1625. EXPORT_SYMBOL_GPL(__dev_forward_skb);
  1626. /**
  1627. * dev_forward_skb - loopback an skb to another netif
  1628. *
  1629. * @dev: destination network device
  1630. * @skb: buffer to forward
  1631. *
  1632. * return values:
  1633. * NET_RX_SUCCESS (no congestion)
  1634. * NET_RX_DROP (packet was dropped, but freed)
  1635. *
  1636. * dev_forward_skb can be used for injecting an skb from the
  1637. * start_xmit function of one device into the receive queue
  1638. * of another device.
  1639. *
  1640. * The receiving device may be in another namespace, so
  1641. * we have to clear all information in the skb that could
  1642. * impact namespace isolation.
  1643. */
  1644. int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
  1645. {
  1646. return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
  1647. }
  1648. EXPORT_SYMBOL_GPL(dev_forward_skb);
  1649. static inline int deliver_skb(struct sk_buff *skb,
  1650. struct packet_type *pt_prev,
  1651. struct net_device *orig_dev)
  1652. {
  1653. if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
  1654. return -ENOMEM;
  1655. refcount_inc(&skb->users);
  1656. return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  1657. }
  1658. static inline void deliver_ptype_list_skb(struct sk_buff *skb,
  1659. struct packet_type **pt,
  1660. struct net_device *orig_dev,
  1661. __be16 type,
  1662. struct list_head *ptype_list)
  1663. {
  1664. struct packet_type *ptype, *pt_prev = *pt;
  1665. list_for_each_entry_rcu(ptype, ptype_list, list) {
  1666. if (ptype->type != type)
  1667. continue;
  1668. if (pt_prev)
  1669. deliver_skb(skb, pt_prev, orig_dev);
  1670. pt_prev = ptype;
  1671. }
  1672. *pt = pt_prev;
  1673. }
  1674. static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
  1675. {
  1676. if (!ptype->af_packet_priv || !skb->sk)
  1677. return false;
  1678. if (ptype->id_match)
  1679. return ptype->id_match(ptype, skb->sk);
  1680. else if ((struct sock *)ptype->af_packet_priv == skb->sk)
  1681. return true;
  1682. return false;
  1683. }
  1684. /*
  1685. * Support routine. Sends outgoing frames to any network
  1686. * taps currently in use.
  1687. */
  1688. void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
  1689. {
  1690. struct packet_type *ptype;
  1691. struct sk_buff *skb2 = NULL;
  1692. struct packet_type *pt_prev = NULL;
  1693. struct list_head *ptype_list = &ptype_all;
  1694. rcu_read_lock();
  1695. again:
  1696. list_for_each_entry_rcu(ptype, ptype_list, list) {
  1697. /* Never send packets back to the socket
  1698. * they originated from - MvS (miquels@drinkel.ow.org)
  1699. */
  1700. if (skb_loop_sk(ptype, skb))
  1701. continue;
  1702. if (pt_prev) {
  1703. deliver_skb(skb2, pt_prev, skb->dev);
  1704. pt_prev = ptype;
  1705. continue;
  1706. }
  1707. /* need to clone skb, done only once */
  1708. skb2 = skb_clone(skb, GFP_ATOMIC);
  1709. if (!skb2)
  1710. goto out_unlock;
  1711. net_timestamp_set(skb2);
  1712. /* skb->nh should be correctly
  1713. * set by sender, so that the second statement is
  1714. * just protection against buggy protocols.
  1715. */
  1716. skb_reset_mac_header(skb2);
  1717. if (skb_network_header(skb2) < skb2->data ||
  1718. skb_network_header(skb2) > skb_tail_pointer(skb2)) {
  1719. net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
  1720. ntohs(skb2->protocol),
  1721. dev->name);
  1722. skb_reset_network_header(skb2);
  1723. }
  1724. skb2->transport_header = skb2->network_header;
  1725. skb2->pkt_type = PACKET_OUTGOING;
  1726. pt_prev = ptype;
  1727. }
  1728. if (ptype_list == &ptype_all) {
  1729. ptype_list = &dev->ptype_all;
  1730. goto again;
  1731. }
  1732. out_unlock:
  1733. if (pt_prev) {
  1734. if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
  1735. pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
  1736. else
  1737. kfree_skb(skb2);
  1738. }
  1739. rcu_read_unlock();
  1740. }
  1741. EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
  1742. /**
  1743. * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
  1744. * @dev: Network device
  1745. * @txq: number of queues available
  1746. *
  1747. * If real_num_tx_queues is changed the tc mappings may no longer be
  1748. * valid. To resolve this verify the tc mapping remains valid and if
  1749. * not NULL the mapping. With no priorities mapping to this
  1750. * offset/count pair it will no longer be used. In the worst case TC0
  1751. * is invalid nothing can be done so disable priority mappings. If is
  1752. * expected that drivers will fix this mapping if they can before
  1753. * calling netif_set_real_num_tx_queues.
  1754. */
  1755. static void netif_setup_tc(struct net_device *dev, unsigned int txq)
  1756. {
  1757. int i;
  1758. struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
  1759. /* If TC0 is invalidated disable TC mapping */
  1760. if (tc->offset + tc->count > txq) {
  1761. pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
  1762. dev->num_tc = 0;
  1763. return;
  1764. }
  1765. /* Invalidated prio to tc mappings set to TC0 */
  1766. for (i = 1; i < TC_BITMASK + 1; i++) {
  1767. int q = netdev_get_prio_tc_map(dev, i);
  1768. tc = &dev->tc_to_txq[q];
  1769. if (tc->offset + tc->count > txq) {
  1770. pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
  1771. i, q);
  1772. netdev_set_prio_tc_map(dev, i, 0);
  1773. }
  1774. }
  1775. }
  1776. int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
  1777. {
  1778. if (dev->num_tc) {
  1779. struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
  1780. int i;
  1781. for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
  1782. if ((txq - tc->offset) < tc->count)
  1783. return i;
  1784. }
  1785. return -1;
  1786. }
  1787. return 0;
  1788. }
  1789. EXPORT_SYMBOL(netdev_txq_to_tc);
  1790. #ifdef CONFIG_XPS
  1791. struct static_key xps_needed __read_mostly;
  1792. EXPORT_SYMBOL(xps_needed);
  1793. struct static_key xps_rxqs_needed __read_mostly;
  1794. EXPORT_SYMBOL(xps_rxqs_needed);
  1795. static DEFINE_MUTEX(xps_map_mutex);
  1796. #define xmap_dereference(P) \
  1797. rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
  1798. static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
  1799. int tci, u16 index)
  1800. {
  1801. struct xps_map *map = NULL;
  1802. int pos;
  1803. if (dev_maps)
  1804. map = xmap_dereference(dev_maps->attr_map[tci]);
  1805. if (!map)
  1806. return false;
  1807. for (pos = map->len; pos--;) {
  1808. if (map->queues[pos] != index)
  1809. continue;
  1810. if (map->len > 1) {
  1811. map->queues[pos] = map->queues[--map->len];
  1812. break;
  1813. }
  1814. RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
  1815. kfree_rcu(map, rcu);
  1816. return false;
  1817. }
  1818. return true;
  1819. }
  1820. static bool remove_xps_queue_cpu(struct net_device *dev,
  1821. struct xps_dev_maps *dev_maps,
  1822. int cpu, u16 offset, u16 count)
  1823. {
  1824. int num_tc = dev->num_tc ? : 1;
  1825. bool active = false;
  1826. int tci;
  1827. for (tci = cpu * num_tc; num_tc--; tci++) {
  1828. int i, j;
  1829. for (i = count, j = offset; i--; j++) {
  1830. if (!remove_xps_queue(dev_maps, tci, j))
  1831. break;
  1832. }
  1833. active |= i < 0;
  1834. }
  1835. return active;
  1836. }
  1837. static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
  1838. struct xps_dev_maps *dev_maps, unsigned int nr_ids,
  1839. u16 offset, u16 count, bool is_rxqs_map)
  1840. {
  1841. bool active = false;
  1842. int i, j;
  1843. for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
  1844. j < nr_ids;)
  1845. active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
  1846. count);
  1847. if (!active) {
  1848. if (is_rxqs_map) {
  1849. RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
  1850. } else {
  1851. RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
  1852. for (i = offset + (count - 1); count--; i--)
  1853. netdev_queue_numa_node_write(
  1854. netdev_get_tx_queue(dev, i),
  1855. NUMA_NO_NODE);
  1856. }
  1857. kfree_rcu(dev_maps, rcu);
  1858. }
  1859. }
  1860. static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
  1861. u16 count)
  1862. {
  1863. const unsigned long *possible_mask = NULL;
  1864. struct xps_dev_maps *dev_maps;
  1865. unsigned int nr_ids;
  1866. if (!static_key_false(&xps_needed))
  1867. return;
  1868. mutex_lock(&xps_map_mutex);
  1869. if (static_key_false(&xps_rxqs_needed)) {
  1870. dev_maps = xmap_dereference(dev->xps_rxqs_map);
  1871. if (dev_maps) {
  1872. nr_ids = dev->num_rx_queues;
  1873. clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
  1874. offset, count, true);
  1875. }
  1876. }
  1877. dev_maps = xmap_dereference(dev->xps_cpus_map);
  1878. if (!dev_maps)
  1879. goto out_no_maps;
  1880. if (num_possible_cpus() > 1)
  1881. possible_mask = cpumask_bits(cpu_possible_mask);
  1882. nr_ids = nr_cpu_ids;
  1883. clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
  1884. false);
  1885. out_no_maps:
  1886. if (static_key_enabled(&xps_rxqs_needed))
  1887. static_key_slow_dec(&xps_rxqs_needed);
  1888. static_key_slow_dec(&xps_needed);
  1889. mutex_unlock(&xps_map_mutex);
  1890. }
  1891. static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
  1892. {
  1893. netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
  1894. }
  1895. static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
  1896. u16 index, bool is_rxqs_map)
  1897. {
  1898. struct xps_map *new_map;
  1899. int alloc_len = XPS_MIN_MAP_ALLOC;
  1900. int i, pos;
  1901. for (pos = 0; map && pos < map->len; pos++) {
  1902. if (map->queues[pos] != index)
  1903. continue;
  1904. return map;
  1905. }
  1906. /* Need to add tx-queue to this CPU's/rx-queue's existing map */
  1907. if (map) {
  1908. if (pos < map->alloc_len)
  1909. return map;
  1910. alloc_len = map->alloc_len * 2;
  1911. }
  1912. /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
  1913. * map
  1914. */
  1915. if (is_rxqs_map)
  1916. new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
  1917. else
  1918. new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
  1919. cpu_to_node(attr_index));
  1920. if (!new_map)
  1921. return NULL;
  1922. for (i = 0; i < pos; i++)
  1923. new_map->queues[i] = map->queues[i];
  1924. new_map->alloc_len = alloc_len;
  1925. new_map->len = pos;
  1926. return new_map;
  1927. }
  1928. int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
  1929. u16 index, bool is_rxqs_map)
  1930. {
  1931. const unsigned long *online_mask = NULL, *possible_mask = NULL;
  1932. struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
  1933. int i, j, tci, numa_node_id = -2;
  1934. int maps_sz, num_tc = 1, tc = 0;
  1935. struct xps_map *map, *new_map;
  1936. bool active = false;
  1937. unsigned int nr_ids;
  1938. if (dev->num_tc) {
  1939. num_tc = dev->num_tc;
  1940. tc = netdev_txq_to_tc(dev, index);
  1941. if (tc < 0)
  1942. return -EINVAL;
  1943. }
  1944. mutex_lock(&xps_map_mutex);
  1945. if (is_rxqs_map) {
  1946. maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
  1947. dev_maps = xmap_dereference(dev->xps_rxqs_map);
  1948. nr_ids = dev->num_rx_queues;
  1949. } else {
  1950. maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
  1951. if (num_possible_cpus() > 1) {
  1952. online_mask = cpumask_bits(cpu_online_mask);
  1953. possible_mask = cpumask_bits(cpu_possible_mask);
  1954. }
  1955. dev_maps = xmap_dereference(dev->xps_cpus_map);
  1956. nr_ids = nr_cpu_ids;
  1957. }
  1958. if (maps_sz < L1_CACHE_BYTES)
  1959. maps_sz = L1_CACHE_BYTES;
  1960. /* allocate memory for queue storage */
  1961. for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
  1962. j < nr_ids;) {
  1963. if (!new_dev_maps)
  1964. new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
  1965. if (!new_dev_maps) {
  1966. mutex_unlock(&xps_map_mutex);
  1967. return -ENOMEM;
  1968. }
  1969. tci = j * num_tc + tc;
  1970. map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
  1971. NULL;
  1972. map = expand_xps_map(map, j, index, is_rxqs_map);
  1973. if (!map)
  1974. goto error;
  1975. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  1976. }
  1977. if (!new_dev_maps)
  1978. goto out_no_new_maps;
  1979. static_key_slow_inc(&xps_needed);
  1980. if (is_rxqs_map)
  1981. static_key_slow_inc(&xps_rxqs_needed);
  1982. for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
  1983. j < nr_ids;) {
  1984. /* copy maps belonging to foreign traffic classes */
  1985. for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
  1986. /* fill in the new device map from the old device map */
  1987. map = xmap_dereference(dev_maps->attr_map[tci]);
  1988. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  1989. }
  1990. /* We need to explicitly update tci as prevous loop
  1991. * could break out early if dev_maps is NULL.
  1992. */
  1993. tci = j * num_tc + tc;
  1994. if (netif_attr_test_mask(j, mask, nr_ids) &&
  1995. netif_attr_test_online(j, online_mask, nr_ids)) {
  1996. /* add tx-queue to CPU/rx-queue maps */
  1997. int pos = 0;
  1998. map = xmap_dereference(new_dev_maps->attr_map[tci]);
  1999. while ((pos < map->len) && (map->queues[pos] != index))
  2000. pos++;
  2001. if (pos == map->len)
  2002. map->queues[map->len++] = index;
  2003. #ifdef CONFIG_NUMA
  2004. if (!is_rxqs_map) {
  2005. if (numa_node_id == -2)
  2006. numa_node_id = cpu_to_node(j);
  2007. else if (numa_node_id != cpu_to_node(j))
  2008. numa_node_id = -1;
  2009. }
  2010. #endif
  2011. } else if (dev_maps) {
  2012. /* fill in the new device map from the old device map */
  2013. map = xmap_dereference(dev_maps->attr_map[tci]);
  2014. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  2015. }
  2016. /* copy maps belonging to foreign traffic classes */
  2017. for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
  2018. /* fill in the new device map from the old device map */
  2019. map = xmap_dereference(dev_maps->attr_map[tci]);
  2020. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  2021. }
  2022. }
  2023. if (is_rxqs_map)
  2024. rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
  2025. else
  2026. rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
  2027. /* Cleanup old maps */
  2028. if (!dev_maps)
  2029. goto out_no_old_maps;
  2030. for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
  2031. j < nr_ids;) {
  2032. for (i = num_tc, tci = j * num_tc; i--; tci++) {
  2033. new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
  2034. map = xmap_dereference(dev_maps->attr_map[tci]);
  2035. if (map && map != new_map)
  2036. kfree_rcu(map, rcu);
  2037. }
  2038. }
  2039. kfree_rcu(dev_maps, rcu);
  2040. out_no_old_maps:
  2041. dev_maps = new_dev_maps;
  2042. active = true;
  2043. out_no_new_maps:
  2044. if (!is_rxqs_map) {
  2045. /* update Tx queue numa node */
  2046. netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
  2047. (numa_node_id >= 0) ?
  2048. numa_node_id : NUMA_NO_NODE);
  2049. }
  2050. if (!dev_maps)
  2051. goto out_no_maps;
  2052. /* removes tx-queue from unused CPUs/rx-queues */
  2053. for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
  2054. j < nr_ids;) {
  2055. for (i = tc, tci = j * num_tc; i--; tci++)
  2056. active |= remove_xps_queue(dev_maps, tci, index);
  2057. if (!netif_attr_test_mask(j, mask, nr_ids) ||
  2058. !netif_attr_test_online(j, online_mask, nr_ids))
  2059. active |= remove_xps_queue(dev_maps, tci, index);
  2060. for (i = num_tc - tc, tci++; --i; tci++)
  2061. active |= remove_xps_queue(dev_maps, tci, index);
  2062. }
  2063. /* free map if not active */
  2064. if (!active) {
  2065. if (is_rxqs_map)
  2066. RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
  2067. else
  2068. RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
  2069. kfree_rcu(dev_maps, rcu);
  2070. }
  2071. out_no_maps:
  2072. mutex_unlock(&xps_map_mutex);
  2073. return 0;
  2074. error:
  2075. /* remove any maps that we added */
  2076. for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
  2077. j < nr_ids;) {
  2078. for (i = num_tc, tci = j * num_tc; i--; tci++) {
  2079. new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
  2080. map = dev_maps ?
  2081. xmap_dereference(dev_maps->attr_map[tci]) :
  2082. NULL;
  2083. if (new_map && new_map != map)
  2084. kfree(new_map);
  2085. }
  2086. }
  2087. mutex_unlock(&xps_map_mutex);
  2088. kfree(new_dev_maps);
  2089. return -ENOMEM;
  2090. }
  2091. int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
  2092. u16 index)
  2093. {
  2094. return __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
  2095. }
  2096. EXPORT_SYMBOL(netif_set_xps_queue);
  2097. #endif
  2098. void netdev_reset_tc(struct net_device *dev)
  2099. {
  2100. #ifdef CONFIG_XPS
  2101. netif_reset_xps_queues_gt(dev, 0);
  2102. #endif
  2103. dev->num_tc = 0;
  2104. memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
  2105. memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
  2106. }
  2107. EXPORT_SYMBOL(netdev_reset_tc);
  2108. int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
  2109. {
  2110. if (tc >= dev->num_tc)
  2111. return -EINVAL;
  2112. #ifdef CONFIG_XPS
  2113. netif_reset_xps_queues(dev, offset, count);
  2114. #endif
  2115. dev->tc_to_txq[tc].count = count;
  2116. dev->tc_to_txq[tc].offset = offset;
  2117. return 0;
  2118. }
  2119. EXPORT_SYMBOL(netdev_set_tc_queue);
  2120. int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
  2121. {
  2122. if (num_tc > TC_MAX_QUEUE)
  2123. return -EINVAL;
  2124. #ifdef CONFIG_XPS
  2125. netif_reset_xps_queues_gt(dev, 0);
  2126. #endif
  2127. dev->num_tc = num_tc;
  2128. return 0;
  2129. }
  2130. EXPORT_SYMBOL(netdev_set_num_tc);
  2131. /*
  2132. * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
  2133. * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
  2134. */
  2135. int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
  2136. {
  2137. bool disabling;
  2138. int rc;
  2139. disabling = txq < dev->real_num_tx_queues;
  2140. if (txq < 1 || txq > dev->num_tx_queues)
  2141. return -EINVAL;
  2142. if (dev->reg_state == NETREG_REGISTERED ||
  2143. dev->reg_state == NETREG_UNREGISTERING) {
  2144. ASSERT_RTNL();
  2145. rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
  2146. txq);
  2147. if (rc)
  2148. return rc;
  2149. if (dev->num_tc)
  2150. netif_setup_tc(dev, txq);
  2151. dev->real_num_tx_queues = txq;
  2152. if (disabling) {
  2153. synchronize_net();
  2154. qdisc_reset_all_tx_gt(dev, txq);
  2155. #ifdef CONFIG_XPS
  2156. netif_reset_xps_queues_gt(dev, txq);
  2157. #endif
  2158. }
  2159. } else {
  2160. dev->real_num_tx_queues = txq;
  2161. }
  2162. return 0;
  2163. }
  2164. EXPORT_SYMBOL(netif_set_real_num_tx_queues);
  2165. #ifdef CONFIG_SYSFS
  2166. /**
  2167. * netif_set_real_num_rx_queues - set actual number of RX queues used
  2168. * @dev: Network device
  2169. * @rxq: Actual number of RX queues
  2170. *
  2171. * This must be called either with the rtnl_lock held or before
  2172. * registration of the net device. Returns 0 on success, or a
  2173. * negative error code. If called before registration, it always
  2174. * succeeds.
  2175. */
  2176. int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
  2177. {
  2178. int rc;
  2179. if (rxq < 1 || rxq > dev->num_rx_queues)
  2180. return -EINVAL;
  2181. if (dev->reg_state == NETREG_REGISTERED) {
  2182. ASSERT_RTNL();
  2183. rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
  2184. rxq);
  2185. if (rc)
  2186. return rc;
  2187. }
  2188. dev->real_num_rx_queues = rxq;
  2189. return 0;
  2190. }
  2191. EXPORT_SYMBOL(netif_set_real_num_rx_queues);
  2192. #endif
  2193. /**
  2194. * netif_get_num_default_rss_queues - default number of RSS queues
  2195. *
  2196. * This routine should set an upper limit on the number of RSS queues
  2197. * used by default by multiqueue devices.
  2198. */
  2199. int netif_get_num_default_rss_queues(void)
  2200. {
  2201. return is_kdump_kernel() ?
  2202. 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
  2203. }
  2204. EXPORT_SYMBOL(netif_get_num_default_rss_queues);
  2205. static void __netif_reschedule(struct Qdisc *q)
  2206. {
  2207. struct softnet_data *sd;
  2208. unsigned long flags;
  2209. local_irq_save(flags);
  2210. sd = this_cpu_ptr(&softnet_data);
  2211. q->next_sched = NULL;
  2212. *sd->output_queue_tailp = q;
  2213. sd->output_queue_tailp = &q->next_sched;
  2214. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  2215. local_irq_restore(flags);
  2216. }
  2217. void __netif_schedule(struct Qdisc *q)
  2218. {
  2219. if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
  2220. __netif_reschedule(q);
  2221. }
  2222. EXPORT_SYMBOL(__netif_schedule);
  2223. struct dev_kfree_skb_cb {
  2224. enum skb_free_reason reason;
  2225. };
  2226. static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
  2227. {
  2228. return (struct dev_kfree_skb_cb *)skb->cb;
  2229. }
  2230. void netif_schedule_queue(struct netdev_queue *txq)
  2231. {
  2232. rcu_read_lock();
  2233. if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
  2234. struct Qdisc *q = rcu_dereference(txq->qdisc);
  2235. __netif_schedule(q);
  2236. }
  2237. rcu_read_unlock();
  2238. }
  2239. EXPORT_SYMBOL(netif_schedule_queue);
  2240. void netif_tx_wake_queue(struct netdev_queue *dev_queue)
  2241. {
  2242. if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
  2243. struct Qdisc *q;
  2244. rcu_read_lock();
  2245. q = rcu_dereference(dev_queue->qdisc);
  2246. __netif_schedule(q);
  2247. rcu_read_unlock();
  2248. }
  2249. }
  2250. EXPORT_SYMBOL(netif_tx_wake_queue);
  2251. void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
  2252. {
  2253. unsigned long flags;
  2254. if (unlikely(!skb))
  2255. return;
  2256. if (likely(refcount_read(&skb->users) == 1)) {
  2257. smp_rmb();
  2258. refcount_set(&skb->users, 0);
  2259. } else if (likely(!refcount_dec_and_test(&skb->users))) {
  2260. return;
  2261. }
  2262. get_kfree_skb_cb(skb)->reason = reason;
  2263. local_irq_save(flags);
  2264. skb->next = __this_cpu_read(softnet_data.completion_queue);
  2265. __this_cpu_write(softnet_data.completion_queue, skb);
  2266. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  2267. local_irq_restore(flags);
  2268. }
  2269. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  2270. void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
  2271. {
  2272. if (in_irq() || irqs_disabled())
  2273. __dev_kfree_skb_irq(skb, reason);
  2274. else
  2275. dev_kfree_skb(skb);
  2276. }
  2277. EXPORT_SYMBOL(__dev_kfree_skb_any);
  2278. /**
  2279. * netif_device_detach - mark device as removed
  2280. * @dev: network device
  2281. *
  2282. * Mark device as removed from system and therefore no longer available.
  2283. */
  2284. void netif_device_detach(struct net_device *dev)
  2285. {
  2286. if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
  2287. netif_running(dev)) {
  2288. netif_tx_stop_all_queues(dev);
  2289. }
  2290. }
  2291. EXPORT_SYMBOL(netif_device_detach);
  2292. /**
  2293. * netif_device_attach - mark device as attached
  2294. * @dev: network device
  2295. *
  2296. * Mark device as attached from system and restart if needed.
  2297. */
  2298. void netif_device_attach(struct net_device *dev)
  2299. {
  2300. if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
  2301. netif_running(dev)) {
  2302. netif_tx_wake_all_queues(dev);
  2303. __netdev_watchdog_up(dev);
  2304. }
  2305. }
  2306. EXPORT_SYMBOL(netif_device_attach);
  2307. /*
  2308. * Returns a Tx hash based on the given packet descriptor a Tx queues' number
  2309. * to be used as a distribution range.
  2310. */
  2311. static u16 skb_tx_hash(const struct net_device *dev, struct sk_buff *skb)
  2312. {
  2313. u32 hash;
  2314. u16 qoffset = 0;
  2315. u16 qcount = dev->real_num_tx_queues;
  2316. if (skb_rx_queue_recorded(skb)) {
  2317. hash = skb_get_rx_queue(skb);
  2318. while (unlikely(hash >= qcount))
  2319. hash -= qcount;
  2320. return hash;
  2321. }
  2322. if (dev->num_tc) {
  2323. u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
  2324. qoffset = dev->tc_to_txq[tc].offset;
  2325. qcount = dev->tc_to_txq[tc].count;
  2326. }
  2327. return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
  2328. }
  2329. static void skb_warn_bad_offload(const struct sk_buff *skb)
  2330. {
  2331. static const netdev_features_t null_features;
  2332. struct net_device *dev = skb->dev;
  2333. const char *name = "";
  2334. if (!net_ratelimit())
  2335. return;
  2336. if (dev) {
  2337. if (dev->dev.parent)
  2338. name = dev_driver_string(dev->dev.parent);
  2339. else
  2340. name = netdev_name(dev);
  2341. }
  2342. WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
  2343. "gso_type=%d ip_summed=%d\n",
  2344. name, dev ? &dev->features : &null_features,
  2345. skb->sk ? &skb->sk->sk_route_caps : &null_features,
  2346. skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
  2347. skb_shinfo(skb)->gso_type, skb->ip_summed);
  2348. }
  2349. /*
  2350. * Invalidate hardware checksum when packet is to be mangled, and
  2351. * complete checksum manually on outgoing path.
  2352. */
  2353. int skb_checksum_help(struct sk_buff *skb)
  2354. {
  2355. __wsum csum;
  2356. int ret = 0, offset;
  2357. if (skb->ip_summed == CHECKSUM_COMPLETE)
  2358. goto out_set_summed;
  2359. if (unlikely(skb_shinfo(skb)->gso_size)) {
  2360. skb_warn_bad_offload(skb);
  2361. return -EINVAL;
  2362. }
  2363. /* Before computing a checksum, we should make sure no frag could
  2364. * be modified by an external entity : checksum could be wrong.
  2365. */
  2366. if (skb_has_shared_frag(skb)) {
  2367. ret = __skb_linearize(skb);
  2368. if (ret)
  2369. goto out;
  2370. }
  2371. offset = skb_checksum_start_offset(skb);
  2372. BUG_ON(offset >= skb_headlen(skb));
  2373. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  2374. offset += skb->csum_offset;
  2375. BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
  2376. if (skb_cloned(skb) &&
  2377. !skb_clone_writable(skb, offset + sizeof(__sum16))) {
  2378. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2379. if (ret)
  2380. goto out;
  2381. }
  2382. *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
  2383. out_set_summed:
  2384. skb->ip_summed = CHECKSUM_NONE;
  2385. out:
  2386. return ret;
  2387. }
  2388. EXPORT_SYMBOL(skb_checksum_help);
  2389. int skb_crc32c_csum_help(struct sk_buff *skb)
  2390. {
  2391. __le32 crc32c_csum;
  2392. int ret = 0, offset, start;
  2393. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2394. goto out;
  2395. if (unlikely(skb_is_gso(skb)))
  2396. goto out;
  2397. /* Before computing a checksum, we should make sure no frag could
  2398. * be modified by an external entity : checksum could be wrong.
  2399. */
  2400. if (unlikely(skb_has_shared_frag(skb))) {
  2401. ret = __skb_linearize(skb);
  2402. if (ret)
  2403. goto out;
  2404. }
  2405. start = skb_checksum_start_offset(skb);
  2406. offset = start + offsetof(struct sctphdr, checksum);
  2407. if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
  2408. ret = -EINVAL;
  2409. goto out;
  2410. }
  2411. if (skb_cloned(skb) &&
  2412. !skb_clone_writable(skb, offset + sizeof(__le32))) {
  2413. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2414. if (ret)
  2415. goto out;
  2416. }
  2417. crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
  2418. skb->len - start, ~(__u32)0,
  2419. crc32c_csum_stub));
  2420. *(__le32 *)(skb->data + offset) = crc32c_csum;
  2421. skb->ip_summed = CHECKSUM_NONE;
  2422. skb->csum_not_inet = 0;
  2423. out:
  2424. return ret;
  2425. }
  2426. __be16 skb_network_protocol(struct sk_buff *skb, int *depth)
  2427. {
  2428. __be16 type = skb->protocol;
  2429. /* Tunnel gso handlers can set protocol to ethernet. */
  2430. if (type == htons(ETH_P_TEB)) {
  2431. struct ethhdr *eth;
  2432. if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
  2433. return 0;
  2434. eth = (struct ethhdr *)skb->data;
  2435. type = eth->h_proto;
  2436. }
  2437. return __vlan_get_protocol(skb, type, depth);
  2438. }
  2439. /**
  2440. * skb_mac_gso_segment - mac layer segmentation handler.
  2441. * @skb: buffer to segment
  2442. * @features: features for the output path (see dev->features)
  2443. */
  2444. struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
  2445. netdev_features_t features)
  2446. {
  2447. struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
  2448. struct packet_offload *ptype;
  2449. int vlan_depth = skb->mac_len;
  2450. __be16 type = skb_network_protocol(skb, &vlan_depth);
  2451. if (unlikely(!type))
  2452. return ERR_PTR(-EINVAL);
  2453. __skb_pull(skb, vlan_depth);
  2454. rcu_read_lock();
  2455. list_for_each_entry_rcu(ptype, &offload_base, list) {
  2456. if (ptype->type == type && ptype->callbacks.gso_segment) {
  2457. segs = ptype->callbacks.gso_segment(skb, features);
  2458. break;
  2459. }
  2460. }
  2461. rcu_read_unlock();
  2462. __skb_push(skb, skb->data - skb_mac_header(skb));
  2463. return segs;
  2464. }
  2465. EXPORT_SYMBOL(skb_mac_gso_segment);
  2466. /* openvswitch calls this on rx path, so we need a different check.
  2467. */
  2468. static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
  2469. {
  2470. if (tx_path)
  2471. return skb->ip_summed != CHECKSUM_PARTIAL &&
  2472. skb->ip_summed != CHECKSUM_UNNECESSARY;
  2473. return skb->ip_summed == CHECKSUM_NONE;
  2474. }
  2475. /**
  2476. * __skb_gso_segment - Perform segmentation on skb.
  2477. * @skb: buffer to segment
  2478. * @features: features for the output path (see dev->features)
  2479. * @tx_path: whether it is called in TX path
  2480. *
  2481. * This function segments the given skb and returns a list of segments.
  2482. *
  2483. * It may return NULL if the skb requires no segmentation. This is
  2484. * only possible when GSO is used for verifying header integrity.
  2485. *
  2486. * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
  2487. */
  2488. struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
  2489. netdev_features_t features, bool tx_path)
  2490. {
  2491. struct sk_buff *segs;
  2492. if (unlikely(skb_needs_check(skb, tx_path))) {
  2493. int err;
  2494. /* We're going to init ->check field in TCP or UDP header */
  2495. err = skb_cow_head(skb, 0);
  2496. if (err < 0)
  2497. return ERR_PTR(err);
  2498. }
  2499. /* Only report GSO partial support if it will enable us to
  2500. * support segmentation on this frame without needing additional
  2501. * work.
  2502. */
  2503. if (features & NETIF_F_GSO_PARTIAL) {
  2504. netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
  2505. struct net_device *dev = skb->dev;
  2506. partial_features |= dev->features & dev->gso_partial_features;
  2507. if (!skb_gso_ok(skb, features | partial_features))
  2508. features &= ~NETIF_F_GSO_PARTIAL;
  2509. }
  2510. BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
  2511. sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
  2512. SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
  2513. SKB_GSO_CB(skb)->encap_level = 0;
  2514. skb_reset_mac_header(skb);
  2515. skb_reset_mac_len(skb);
  2516. segs = skb_mac_gso_segment(skb, features);
  2517. if (unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
  2518. skb_warn_bad_offload(skb);
  2519. return segs;
  2520. }
  2521. EXPORT_SYMBOL(__skb_gso_segment);
  2522. /* Take action when hardware reception checksum errors are detected. */
  2523. #ifdef CONFIG_BUG
  2524. void netdev_rx_csum_fault(struct net_device *dev)
  2525. {
  2526. if (net_ratelimit()) {
  2527. pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
  2528. dump_stack();
  2529. }
  2530. }
  2531. EXPORT_SYMBOL(netdev_rx_csum_fault);
  2532. #endif
  2533. /* XXX: check that highmem exists at all on the given machine. */
  2534. static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
  2535. {
  2536. #ifdef CONFIG_HIGHMEM
  2537. int i;
  2538. if (!(dev->features & NETIF_F_HIGHDMA)) {
  2539. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2540. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2541. if (PageHighMem(skb_frag_page(frag)))
  2542. return 1;
  2543. }
  2544. }
  2545. #endif
  2546. return 0;
  2547. }
  2548. /* If MPLS offload request, verify we are testing hardware MPLS features
  2549. * instead of standard features for the netdev.
  2550. */
  2551. #if IS_ENABLED(CONFIG_NET_MPLS_GSO)
  2552. static netdev_features_t net_mpls_features(struct sk_buff *skb,
  2553. netdev_features_t features,
  2554. __be16 type)
  2555. {
  2556. if (eth_p_mpls(type))
  2557. features &= skb->dev->mpls_features;
  2558. return features;
  2559. }
  2560. #else
  2561. static netdev_features_t net_mpls_features(struct sk_buff *skb,
  2562. netdev_features_t features,
  2563. __be16 type)
  2564. {
  2565. return features;
  2566. }
  2567. #endif
  2568. static netdev_features_t harmonize_features(struct sk_buff *skb,
  2569. netdev_features_t features)
  2570. {
  2571. int tmp;
  2572. __be16 type;
  2573. type = skb_network_protocol(skb, &tmp);
  2574. features = net_mpls_features(skb, features, type);
  2575. if (skb->ip_summed != CHECKSUM_NONE &&
  2576. !can_checksum_protocol(features, type)) {
  2577. features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  2578. }
  2579. if (illegal_highdma(skb->dev, skb))
  2580. features &= ~NETIF_F_SG;
  2581. return features;
  2582. }
  2583. netdev_features_t passthru_features_check(struct sk_buff *skb,
  2584. struct net_device *dev,
  2585. netdev_features_t features)
  2586. {
  2587. return features;
  2588. }
  2589. EXPORT_SYMBOL(passthru_features_check);
  2590. static netdev_features_t dflt_features_check(struct sk_buff *skb,
  2591. struct net_device *dev,
  2592. netdev_features_t features)
  2593. {
  2594. return vlan_features_check(skb, features);
  2595. }
  2596. static netdev_features_t gso_features_check(const struct sk_buff *skb,
  2597. struct net_device *dev,
  2598. netdev_features_t features)
  2599. {
  2600. u16 gso_segs = skb_shinfo(skb)->gso_segs;
  2601. if (gso_segs > dev->gso_max_segs)
  2602. return features & ~NETIF_F_GSO_MASK;
  2603. /* Support for GSO partial features requires software
  2604. * intervention before we can actually process the packets
  2605. * so we need to strip support for any partial features now
  2606. * and we can pull them back in after we have partially
  2607. * segmented the frame.
  2608. */
  2609. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
  2610. features &= ~dev->gso_partial_features;
  2611. /* Make sure to clear the IPv4 ID mangling feature if the
  2612. * IPv4 header has the potential to be fragmented.
  2613. */
  2614. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  2615. struct iphdr *iph = skb->encapsulation ?
  2616. inner_ip_hdr(skb) : ip_hdr(skb);
  2617. if (!(iph->frag_off & htons(IP_DF)))
  2618. features &= ~NETIF_F_TSO_MANGLEID;
  2619. }
  2620. return features;
  2621. }
  2622. netdev_features_t netif_skb_features(struct sk_buff *skb)
  2623. {
  2624. struct net_device *dev = skb->dev;
  2625. netdev_features_t features = dev->features;
  2626. if (skb_is_gso(skb))
  2627. features = gso_features_check(skb, dev, features);
  2628. /* If encapsulation offload request, verify we are testing
  2629. * hardware encapsulation features instead of standard
  2630. * features for the netdev
  2631. */
  2632. if (skb->encapsulation)
  2633. features &= dev->hw_enc_features;
  2634. if (skb_vlan_tagged(skb))
  2635. features = netdev_intersect_features(features,
  2636. dev->vlan_features |
  2637. NETIF_F_HW_VLAN_CTAG_TX |
  2638. NETIF_F_HW_VLAN_STAG_TX);
  2639. if (dev->netdev_ops->ndo_features_check)
  2640. features &= dev->netdev_ops->ndo_features_check(skb, dev,
  2641. features);
  2642. else
  2643. features &= dflt_features_check(skb, dev, features);
  2644. return harmonize_features(skb, features);
  2645. }
  2646. EXPORT_SYMBOL(netif_skb_features);
  2647. static int xmit_one(struct sk_buff *skb, struct net_device *dev,
  2648. struct netdev_queue *txq, bool more)
  2649. {
  2650. unsigned int len;
  2651. int rc;
  2652. if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
  2653. dev_queue_xmit_nit(skb, dev);
  2654. len = skb->len;
  2655. trace_net_dev_start_xmit(skb, dev);
  2656. rc = netdev_start_xmit(skb, dev, txq, more);
  2657. trace_net_dev_xmit(skb, rc, dev, len);
  2658. return rc;
  2659. }
  2660. struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
  2661. struct netdev_queue *txq, int *ret)
  2662. {
  2663. struct sk_buff *skb = first;
  2664. int rc = NETDEV_TX_OK;
  2665. while (skb) {
  2666. struct sk_buff *next = skb->next;
  2667. skb->next = NULL;
  2668. rc = xmit_one(skb, dev, txq, next != NULL);
  2669. if (unlikely(!dev_xmit_complete(rc))) {
  2670. skb->next = next;
  2671. goto out;
  2672. }
  2673. skb = next;
  2674. if (netif_xmit_stopped(txq) && skb) {
  2675. rc = NETDEV_TX_BUSY;
  2676. break;
  2677. }
  2678. }
  2679. out:
  2680. *ret = rc;
  2681. return skb;
  2682. }
  2683. static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
  2684. netdev_features_t features)
  2685. {
  2686. if (skb_vlan_tag_present(skb) &&
  2687. !vlan_hw_offload_capable(features, skb->vlan_proto))
  2688. skb = __vlan_hwaccel_push_inside(skb);
  2689. return skb;
  2690. }
  2691. int skb_csum_hwoffload_help(struct sk_buff *skb,
  2692. const netdev_features_t features)
  2693. {
  2694. if (unlikely(skb->csum_not_inet))
  2695. return !!(features & NETIF_F_SCTP_CRC) ? 0 :
  2696. skb_crc32c_csum_help(skb);
  2697. return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
  2698. }
  2699. EXPORT_SYMBOL(skb_csum_hwoffload_help);
  2700. static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
  2701. {
  2702. netdev_features_t features;
  2703. features = netif_skb_features(skb);
  2704. skb = validate_xmit_vlan(skb, features);
  2705. if (unlikely(!skb))
  2706. goto out_null;
  2707. skb = sk_validate_xmit_skb(skb, dev);
  2708. if (unlikely(!skb))
  2709. goto out_null;
  2710. if (netif_needs_gso(skb, features)) {
  2711. struct sk_buff *segs;
  2712. segs = skb_gso_segment(skb, features);
  2713. if (IS_ERR(segs)) {
  2714. goto out_kfree_skb;
  2715. } else if (segs) {
  2716. consume_skb(skb);
  2717. skb = segs;
  2718. }
  2719. } else {
  2720. if (skb_needs_linearize(skb, features) &&
  2721. __skb_linearize(skb))
  2722. goto out_kfree_skb;
  2723. /* If packet is not checksummed and device does not
  2724. * support checksumming for this protocol, complete
  2725. * checksumming here.
  2726. */
  2727. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2728. if (skb->encapsulation)
  2729. skb_set_inner_transport_header(skb,
  2730. skb_checksum_start_offset(skb));
  2731. else
  2732. skb_set_transport_header(skb,
  2733. skb_checksum_start_offset(skb));
  2734. if (skb_csum_hwoffload_help(skb, features))
  2735. goto out_kfree_skb;
  2736. }
  2737. }
  2738. skb = validate_xmit_xfrm(skb, features, again);
  2739. return skb;
  2740. out_kfree_skb:
  2741. kfree_skb(skb);
  2742. out_null:
  2743. atomic_long_inc(&dev->tx_dropped);
  2744. return NULL;
  2745. }
  2746. struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
  2747. {
  2748. struct sk_buff *next, *head = NULL, *tail;
  2749. for (; skb != NULL; skb = next) {
  2750. next = skb->next;
  2751. skb->next = NULL;
  2752. /* in case skb wont be segmented, point to itself */
  2753. skb->prev = skb;
  2754. skb = validate_xmit_skb(skb, dev, again);
  2755. if (!skb)
  2756. continue;
  2757. if (!head)
  2758. head = skb;
  2759. else
  2760. tail->next = skb;
  2761. /* If skb was segmented, skb->prev points to
  2762. * the last segment. If not, it still contains skb.
  2763. */
  2764. tail = skb->prev;
  2765. }
  2766. return head;
  2767. }
  2768. EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
  2769. static void qdisc_pkt_len_init(struct sk_buff *skb)
  2770. {
  2771. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  2772. qdisc_skb_cb(skb)->pkt_len = skb->len;
  2773. /* To get more precise estimation of bytes sent on wire,
  2774. * we add to pkt_len the headers size of all segments
  2775. */
  2776. if (shinfo->gso_size) {
  2777. unsigned int hdr_len;
  2778. u16 gso_segs = shinfo->gso_segs;
  2779. /* mac layer + network layer */
  2780. hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
  2781. /* + transport layer */
  2782. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
  2783. const struct tcphdr *th;
  2784. struct tcphdr _tcphdr;
  2785. th = skb_header_pointer(skb, skb_transport_offset(skb),
  2786. sizeof(_tcphdr), &_tcphdr);
  2787. if (likely(th))
  2788. hdr_len += __tcp_hdrlen(th);
  2789. } else {
  2790. struct udphdr _udphdr;
  2791. if (skb_header_pointer(skb, skb_transport_offset(skb),
  2792. sizeof(_udphdr), &_udphdr))
  2793. hdr_len += sizeof(struct udphdr);
  2794. }
  2795. if (shinfo->gso_type & SKB_GSO_DODGY)
  2796. gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
  2797. shinfo->gso_size);
  2798. qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
  2799. }
  2800. }
  2801. static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  2802. struct net_device *dev,
  2803. struct netdev_queue *txq)
  2804. {
  2805. spinlock_t *root_lock = qdisc_lock(q);
  2806. struct sk_buff *to_free = NULL;
  2807. bool contended;
  2808. int rc;
  2809. qdisc_calculate_pkt_len(skb, q);
  2810. if (q->flags & TCQ_F_NOLOCK) {
  2811. if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
  2812. __qdisc_drop(skb, &to_free);
  2813. rc = NET_XMIT_DROP;
  2814. } else {
  2815. rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
  2816. qdisc_run(q);
  2817. }
  2818. if (unlikely(to_free))
  2819. kfree_skb_list(to_free);
  2820. return rc;
  2821. }
  2822. /*
  2823. * Heuristic to force contended enqueues to serialize on a
  2824. * separate lock before trying to get qdisc main lock.
  2825. * This permits qdisc->running owner to get the lock more
  2826. * often and dequeue packets faster.
  2827. */
  2828. contended = qdisc_is_running(q);
  2829. if (unlikely(contended))
  2830. spin_lock(&q->busylock);
  2831. spin_lock(root_lock);
  2832. if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
  2833. __qdisc_drop(skb, &to_free);
  2834. rc = NET_XMIT_DROP;
  2835. } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
  2836. qdisc_run_begin(q)) {
  2837. /*
  2838. * This is a work-conserving queue; there are no old skbs
  2839. * waiting to be sent out; and the qdisc is not running -
  2840. * xmit the skb directly.
  2841. */
  2842. qdisc_bstats_update(q, skb);
  2843. if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
  2844. if (unlikely(contended)) {
  2845. spin_unlock(&q->busylock);
  2846. contended = false;
  2847. }
  2848. __qdisc_run(q);
  2849. }
  2850. qdisc_run_end(q);
  2851. rc = NET_XMIT_SUCCESS;
  2852. } else {
  2853. rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
  2854. if (qdisc_run_begin(q)) {
  2855. if (unlikely(contended)) {
  2856. spin_unlock(&q->busylock);
  2857. contended = false;
  2858. }
  2859. __qdisc_run(q);
  2860. qdisc_run_end(q);
  2861. }
  2862. }
  2863. spin_unlock(root_lock);
  2864. if (unlikely(to_free))
  2865. kfree_skb_list(to_free);
  2866. if (unlikely(contended))
  2867. spin_unlock(&q->busylock);
  2868. return rc;
  2869. }
  2870. #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
  2871. static void skb_update_prio(struct sk_buff *skb)
  2872. {
  2873. const struct netprio_map *map;
  2874. const struct sock *sk;
  2875. unsigned int prioidx;
  2876. if (skb->priority)
  2877. return;
  2878. map = rcu_dereference_bh(skb->dev->priomap);
  2879. if (!map)
  2880. return;
  2881. sk = skb_to_full_sk(skb);
  2882. if (!sk)
  2883. return;
  2884. prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
  2885. if (prioidx < map->priomap_len)
  2886. skb->priority = map->priomap[prioidx];
  2887. }
  2888. #else
  2889. #define skb_update_prio(skb)
  2890. #endif
  2891. DEFINE_PER_CPU(int, xmit_recursion);
  2892. EXPORT_SYMBOL(xmit_recursion);
  2893. /**
  2894. * dev_loopback_xmit - loop back @skb
  2895. * @net: network namespace this loopback is happening in
  2896. * @sk: sk needed to be a netfilter okfn
  2897. * @skb: buffer to transmit
  2898. */
  2899. int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
  2900. {
  2901. skb_reset_mac_header(skb);
  2902. __skb_pull(skb, skb_network_offset(skb));
  2903. skb->pkt_type = PACKET_LOOPBACK;
  2904. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2905. WARN_ON(!skb_dst(skb));
  2906. skb_dst_force(skb);
  2907. netif_rx_ni(skb);
  2908. return 0;
  2909. }
  2910. EXPORT_SYMBOL(dev_loopback_xmit);
  2911. #ifdef CONFIG_NET_EGRESS
  2912. static struct sk_buff *
  2913. sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
  2914. {
  2915. struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
  2916. struct tcf_result cl_res;
  2917. if (!miniq)
  2918. return skb;
  2919. /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
  2920. mini_qdisc_bstats_cpu_update(miniq, skb);
  2921. switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
  2922. case TC_ACT_OK:
  2923. case TC_ACT_RECLASSIFY:
  2924. skb->tc_index = TC_H_MIN(cl_res.classid);
  2925. break;
  2926. case TC_ACT_SHOT:
  2927. mini_qdisc_qstats_cpu_drop(miniq);
  2928. *ret = NET_XMIT_DROP;
  2929. kfree_skb(skb);
  2930. return NULL;
  2931. case TC_ACT_STOLEN:
  2932. case TC_ACT_QUEUED:
  2933. case TC_ACT_TRAP:
  2934. *ret = NET_XMIT_SUCCESS;
  2935. consume_skb(skb);
  2936. return NULL;
  2937. case TC_ACT_REDIRECT:
  2938. /* No need to push/pop skb's mac_header here on egress! */
  2939. skb_do_redirect(skb);
  2940. *ret = NET_XMIT_SUCCESS;
  2941. return NULL;
  2942. default:
  2943. break;
  2944. }
  2945. return skb;
  2946. }
  2947. #endif /* CONFIG_NET_EGRESS */
  2948. #ifdef CONFIG_XPS
  2949. static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
  2950. struct xps_dev_maps *dev_maps, unsigned int tci)
  2951. {
  2952. struct xps_map *map;
  2953. int queue_index = -1;
  2954. if (dev->num_tc) {
  2955. tci *= dev->num_tc;
  2956. tci += netdev_get_prio_tc_map(dev, skb->priority);
  2957. }
  2958. map = rcu_dereference(dev_maps->attr_map[tci]);
  2959. if (map) {
  2960. if (map->len == 1)
  2961. queue_index = map->queues[0];
  2962. else
  2963. queue_index = map->queues[reciprocal_scale(
  2964. skb_get_hash(skb), map->len)];
  2965. if (unlikely(queue_index >= dev->real_num_tx_queues))
  2966. queue_index = -1;
  2967. }
  2968. return queue_index;
  2969. }
  2970. #endif
  2971. static int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
  2972. {
  2973. #ifdef CONFIG_XPS
  2974. struct xps_dev_maps *dev_maps;
  2975. struct sock *sk = skb->sk;
  2976. int queue_index = -1;
  2977. if (!static_key_false(&xps_needed))
  2978. return -1;
  2979. rcu_read_lock();
  2980. if (!static_key_false(&xps_rxqs_needed))
  2981. goto get_cpus_map;
  2982. dev_maps = rcu_dereference(dev->xps_rxqs_map);
  2983. if (dev_maps) {
  2984. int tci = sk_rx_queue_get(sk);
  2985. if (tci >= 0 && tci < dev->num_rx_queues)
  2986. queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
  2987. tci);
  2988. }
  2989. get_cpus_map:
  2990. if (queue_index < 0) {
  2991. dev_maps = rcu_dereference(dev->xps_cpus_map);
  2992. if (dev_maps) {
  2993. unsigned int tci = skb->sender_cpu - 1;
  2994. queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
  2995. tci);
  2996. }
  2997. }
  2998. rcu_read_unlock();
  2999. return queue_index;
  3000. #else
  3001. return -1;
  3002. #endif
  3003. }
  3004. static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
  3005. {
  3006. struct sock *sk = skb->sk;
  3007. int queue_index = sk_tx_queue_get(sk);
  3008. if (queue_index < 0 || skb->ooo_okay ||
  3009. queue_index >= dev->real_num_tx_queues) {
  3010. int new_index = get_xps_queue(dev, skb);
  3011. if (new_index < 0)
  3012. new_index = skb_tx_hash(dev, skb);
  3013. if (queue_index != new_index && sk &&
  3014. sk_fullsock(sk) &&
  3015. rcu_access_pointer(sk->sk_dst_cache))
  3016. sk_tx_queue_set(sk, new_index);
  3017. queue_index = new_index;
  3018. }
  3019. return queue_index;
  3020. }
  3021. struct netdev_queue *netdev_pick_tx(struct net_device *dev,
  3022. struct sk_buff *skb,
  3023. void *accel_priv)
  3024. {
  3025. int queue_index = 0;
  3026. #ifdef CONFIG_XPS
  3027. u32 sender_cpu = skb->sender_cpu - 1;
  3028. if (sender_cpu >= (u32)NR_CPUS)
  3029. skb->sender_cpu = raw_smp_processor_id() + 1;
  3030. #endif
  3031. if (dev->real_num_tx_queues != 1) {
  3032. const struct net_device_ops *ops = dev->netdev_ops;
  3033. if (ops->ndo_select_queue)
  3034. queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
  3035. __netdev_pick_tx);
  3036. else
  3037. queue_index = __netdev_pick_tx(dev, skb);
  3038. queue_index = netdev_cap_txqueue(dev, queue_index);
  3039. }
  3040. skb_set_queue_mapping(skb, queue_index);
  3041. return netdev_get_tx_queue(dev, queue_index);
  3042. }
  3043. /**
  3044. * __dev_queue_xmit - transmit a buffer
  3045. * @skb: buffer to transmit
  3046. * @accel_priv: private data used for L2 forwarding offload
  3047. *
  3048. * Queue a buffer for transmission to a network device. The caller must
  3049. * have set the device and priority and built the buffer before calling
  3050. * this function. The function can be called from an interrupt.
  3051. *
  3052. * A negative errno code is returned on a failure. A success does not
  3053. * guarantee the frame will be transmitted as it may be dropped due
  3054. * to congestion or traffic shaping.
  3055. *
  3056. * -----------------------------------------------------------------------------------
  3057. * I notice this method can also return errors from the queue disciplines,
  3058. * including NET_XMIT_DROP, which is a positive value. So, errors can also
  3059. * be positive.
  3060. *
  3061. * Regardless of the return value, the skb is consumed, so it is currently
  3062. * difficult to retry a send to this method. (You can bump the ref count
  3063. * before sending to hold a reference for retry if you are careful.)
  3064. *
  3065. * When calling this method, interrupts MUST be enabled. This is because
  3066. * the BH enable code must have IRQs enabled so that it will not deadlock.
  3067. * --BLG
  3068. */
  3069. static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
  3070. {
  3071. struct net_device *dev = skb->dev;
  3072. struct netdev_queue *txq;
  3073. struct Qdisc *q;
  3074. int rc = -ENOMEM;
  3075. bool again = false;
  3076. skb_reset_mac_header(skb);
  3077. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
  3078. __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
  3079. /* Disable soft irqs for various locks below. Also
  3080. * stops preemption for RCU.
  3081. */
  3082. rcu_read_lock_bh();
  3083. skb_update_prio(skb);
  3084. qdisc_pkt_len_init(skb);
  3085. #ifdef CONFIG_NET_CLS_ACT
  3086. skb->tc_at_ingress = 0;
  3087. # ifdef CONFIG_NET_EGRESS
  3088. if (static_branch_unlikely(&egress_needed_key)) {
  3089. skb = sch_handle_egress(skb, &rc, dev);
  3090. if (!skb)
  3091. goto out;
  3092. }
  3093. # endif
  3094. #endif
  3095. /* If device/qdisc don't need skb->dst, release it right now while
  3096. * its hot in this cpu cache.
  3097. */
  3098. if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
  3099. skb_dst_drop(skb);
  3100. else
  3101. skb_dst_force(skb);
  3102. txq = netdev_pick_tx(dev, skb, accel_priv);
  3103. q = rcu_dereference_bh(txq->qdisc);
  3104. trace_net_dev_queue(skb);
  3105. if (q->enqueue) {
  3106. rc = __dev_xmit_skb(skb, q, dev, txq);
  3107. goto out;
  3108. }
  3109. /* The device has no queue. Common case for software devices:
  3110. * loopback, all the sorts of tunnels...
  3111. * Really, it is unlikely that netif_tx_lock protection is necessary
  3112. * here. (f.e. loopback and IP tunnels are clean ignoring statistics
  3113. * counters.)
  3114. * However, it is possible, that they rely on protection
  3115. * made by us here.
  3116. * Check this and shot the lock. It is not prone from deadlocks.
  3117. *Either shot noqueue qdisc, it is even simpler 8)
  3118. */
  3119. if (dev->flags & IFF_UP) {
  3120. int cpu = smp_processor_id(); /* ok because BHs are off */
  3121. if (txq->xmit_lock_owner != cpu) {
  3122. if (unlikely(__this_cpu_read(xmit_recursion) >
  3123. XMIT_RECURSION_LIMIT))
  3124. goto recursion_alert;
  3125. skb = validate_xmit_skb(skb, dev, &again);
  3126. if (!skb)
  3127. goto out;
  3128. HARD_TX_LOCK(dev, txq, cpu);
  3129. if (!netif_xmit_stopped(txq)) {
  3130. __this_cpu_inc(xmit_recursion);
  3131. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  3132. __this_cpu_dec(xmit_recursion);
  3133. if (dev_xmit_complete(rc)) {
  3134. HARD_TX_UNLOCK(dev, txq);
  3135. goto out;
  3136. }
  3137. }
  3138. HARD_TX_UNLOCK(dev, txq);
  3139. net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
  3140. dev->name);
  3141. } else {
  3142. /* Recursion is detected! It is possible,
  3143. * unfortunately
  3144. */
  3145. recursion_alert:
  3146. net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
  3147. dev->name);
  3148. }
  3149. }
  3150. rc = -ENETDOWN;
  3151. rcu_read_unlock_bh();
  3152. atomic_long_inc(&dev->tx_dropped);
  3153. kfree_skb_list(skb);
  3154. return rc;
  3155. out:
  3156. rcu_read_unlock_bh();
  3157. return rc;
  3158. }
  3159. int dev_queue_xmit(struct sk_buff *skb)
  3160. {
  3161. return __dev_queue_xmit(skb, NULL);
  3162. }
  3163. EXPORT_SYMBOL(dev_queue_xmit);
  3164. int dev_queue_xmit_accel(struct sk_buff *skb, void *accel_priv)
  3165. {
  3166. return __dev_queue_xmit(skb, accel_priv);
  3167. }
  3168. EXPORT_SYMBOL(dev_queue_xmit_accel);
  3169. int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
  3170. {
  3171. struct net_device *dev = skb->dev;
  3172. struct sk_buff *orig_skb = skb;
  3173. struct netdev_queue *txq;
  3174. int ret = NETDEV_TX_BUSY;
  3175. bool again = false;
  3176. if (unlikely(!netif_running(dev) ||
  3177. !netif_carrier_ok(dev)))
  3178. goto drop;
  3179. skb = validate_xmit_skb_list(skb, dev, &again);
  3180. if (skb != orig_skb)
  3181. goto drop;
  3182. skb_set_queue_mapping(skb, queue_id);
  3183. txq = skb_get_tx_queue(dev, skb);
  3184. local_bh_disable();
  3185. HARD_TX_LOCK(dev, txq, smp_processor_id());
  3186. if (!netif_xmit_frozen_or_drv_stopped(txq))
  3187. ret = netdev_start_xmit(skb, dev, txq, false);
  3188. HARD_TX_UNLOCK(dev, txq);
  3189. local_bh_enable();
  3190. if (!dev_xmit_complete(ret))
  3191. kfree_skb(skb);
  3192. return ret;
  3193. drop:
  3194. atomic_long_inc(&dev->tx_dropped);
  3195. kfree_skb_list(skb);
  3196. return NET_XMIT_DROP;
  3197. }
  3198. EXPORT_SYMBOL(dev_direct_xmit);
  3199. /*************************************************************************
  3200. * Receiver routines
  3201. *************************************************************************/
  3202. int netdev_max_backlog __read_mostly = 1000;
  3203. EXPORT_SYMBOL(netdev_max_backlog);
  3204. int netdev_tstamp_prequeue __read_mostly = 1;
  3205. int netdev_budget __read_mostly = 300;
  3206. unsigned int __read_mostly netdev_budget_usecs = 2000;
  3207. int weight_p __read_mostly = 64; /* old backlog weight */
  3208. int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
  3209. int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
  3210. int dev_rx_weight __read_mostly = 64;
  3211. int dev_tx_weight __read_mostly = 64;
  3212. /* Called with irq disabled */
  3213. static inline void ____napi_schedule(struct softnet_data *sd,
  3214. struct napi_struct *napi)
  3215. {
  3216. list_add_tail(&napi->poll_list, &sd->poll_list);
  3217. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  3218. }
  3219. #ifdef CONFIG_RPS
  3220. /* One global table that all flow-based protocols share. */
  3221. struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
  3222. EXPORT_SYMBOL(rps_sock_flow_table);
  3223. u32 rps_cpu_mask __read_mostly;
  3224. EXPORT_SYMBOL(rps_cpu_mask);
  3225. struct static_key rps_needed __read_mostly;
  3226. EXPORT_SYMBOL(rps_needed);
  3227. struct static_key rfs_needed __read_mostly;
  3228. EXPORT_SYMBOL(rfs_needed);
  3229. static struct rps_dev_flow *
  3230. set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  3231. struct rps_dev_flow *rflow, u16 next_cpu)
  3232. {
  3233. if (next_cpu < nr_cpu_ids) {
  3234. #ifdef CONFIG_RFS_ACCEL
  3235. struct netdev_rx_queue *rxqueue;
  3236. struct rps_dev_flow_table *flow_table;
  3237. struct rps_dev_flow *old_rflow;
  3238. u32 flow_id;
  3239. u16 rxq_index;
  3240. int rc;
  3241. /* Should we steer this flow to a different hardware queue? */
  3242. if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
  3243. !(dev->features & NETIF_F_NTUPLE))
  3244. goto out;
  3245. rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
  3246. if (rxq_index == skb_get_rx_queue(skb))
  3247. goto out;
  3248. rxqueue = dev->_rx + rxq_index;
  3249. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  3250. if (!flow_table)
  3251. goto out;
  3252. flow_id = skb_get_hash(skb) & flow_table->mask;
  3253. rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
  3254. rxq_index, flow_id);
  3255. if (rc < 0)
  3256. goto out;
  3257. old_rflow = rflow;
  3258. rflow = &flow_table->flows[flow_id];
  3259. rflow->filter = rc;
  3260. if (old_rflow->filter == rflow->filter)
  3261. old_rflow->filter = RPS_NO_FILTER;
  3262. out:
  3263. #endif
  3264. rflow->last_qtail =
  3265. per_cpu(softnet_data, next_cpu).input_queue_head;
  3266. }
  3267. rflow->cpu = next_cpu;
  3268. return rflow;
  3269. }
  3270. /*
  3271. * get_rps_cpu is called from netif_receive_skb and returns the target
  3272. * CPU from the RPS map of the receiving queue for a given skb.
  3273. * rcu_read_lock must be held on entry.
  3274. */
  3275. static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  3276. struct rps_dev_flow **rflowp)
  3277. {
  3278. const struct rps_sock_flow_table *sock_flow_table;
  3279. struct netdev_rx_queue *rxqueue = dev->_rx;
  3280. struct rps_dev_flow_table *flow_table;
  3281. struct rps_map *map;
  3282. int cpu = -1;
  3283. u32 tcpu;
  3284. u32 hash;
  3285. if (skb_rx_queue_recorded(skb)) {
  3286. u16 index = skb_get_rx_queue(skb);
  3287. if (unlikely(index >= dev->real_num_rx_queues)) {
  3288. WARN_ONCE(dev->real_num_rx_queues > 1,
  3289. "%s received packet on queue %u, but number "
  3290. "of RX queues is %u\n",
  3291. dev->name, index, dev->real_num_rx_queues);
  3292. goto done;
  3293. }
  3294. rxqueue += index;
  3295. }
  3296. /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
  3297. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  3298. map = rcu_dereference(rxqueue->rps_map);
  3299. if (!flow_table && !map)
  3300. goto done;
  3301. skb_reset_network_header(skb);
  3302. hash = skb_get_hash(skb);
  3303. if (!hash)
  3304. goto done;
  3305. sock_flow_table = rcu_dereference(rps_sock_flow_table);
  3306. if (flow_table && sock_flow_table) {
  3307. struct rps_dev_flow *rflow;
  3308. u32 next_cpu;
  3309. u32 ident;
  3310. /* First check into global flow table if there is a match */
  3311. ident = sock_flow_table->ents[hash & sock_flow_table->mask];
  3312. if ((ident ^ hash) & ~rps_cpu_mask)
  3313. goto try_rps;
  3314. next_cpu = ident & rps_cpu_mask;
  3315. /* OK, now we know there is a match,
  3316. * we can look at the local (per receive queue) flow table
  3317. */
  3318. rflow = &flow_table->flows[hash & flow_table->mask];
  3319. tcpu = rflow->cpu;
  3320. /*
  3321. * If the desired CPU (where last recvmsg was done) is
  3322. * different from current CPU (one in the rx-queue flow
  3323. * table entry), switch if one of the following holds:
  3324. * - Current CPU is unset (>= nr_cpu_ids).
  3325. * - Current CPU is offline.
  3326. * - The current CPU's queue tail has advanced beyond the
  3327. * last packet that was enqueued using this table entry.
  3328. * This guarantees that all previous packets for the flow
  3329. * have been dequeued, thus preserving in order delivery.
  3330. */
  3331. if (unlikely(tcpu != next_cpu) &&
  3332. (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
  3333. ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
  3334. rflow->last_qtail)) >= 0)) {
  3335. tcpu = next_cpu;
  3336. rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
  3337. }
  3338. if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
  3339. *rflowp = rflow;
  3340. cpu = tcpu;
  3341. goto done;
  3342. }
  3343. }
  3344. try_rps:
  3345. if (map) {
  3346. tcpu = map->cpus[reciprocal_scale(hash, map->len)];
  3347. if (cpu_online(tcpu)) {
  3348. cpu = tcpu;
  3349. goto done;
  3350. }
  3351. }
  3352. done:
  3353. return cpu;
  3354. }
  3355. #ifdef CONFIG_RFS_ACCEL
  3356. /**
  3357. * rps_may_expire_flow - check whether an RFS hardware filter may be removed
  3358. * @dev: Device on which the filter was set
  3359. * @rxq_index: RX queue index
  3360. * @flow_id: Flow ID passed to ndo_rx_flow_steer()
  3361. * @filter_id: Filter ID returned by ndo_rx_flow_steer()
  3362. *
  3363. * Drivers that implement ndo_rx_flow_steer() should periodically call
  3364. * this function for each installed filter and remove the filters for
  3365. * which it returns %true.
  3366. */
  3367. bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
  3368. u32 flow_id, u16 filter_id)
  3369. {
  3370. struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
  3371. struct rps_dev_flow_table *flow_table;
  3372. struct rps_dev_flow *rflow;
  3373. bool expire = true;
  3374. unsigned int cpu;
  3375. rcu_read_lock();
  3376. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  3377. if (flow_table && flow_id <= flow_table->mask) {
  3378. rflow = &flow_table->flows[flow_id];
  3379. cpu = READ_ONCE(rflow->cpu);
  3380. if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
  3381. ((int)(per_cpu(softnet_data, cpu).input_queue_head -
  3382. rflow->last_qtail) <
  3383. (int)(10 * flow_table->mask)))
  3384. expire = false;
  3385. }
  3386. rcu_read_unlock();
  3387. return expire;
  3388. }
  3389. EXPORT_SYMBOL(rps_may_expire_flow);
  3390. #endif /* CONFIG_RFS_ACCEL */
  3391. /* Called from hardirq (IPI) context */
  3392. static void rps_trigger_softirq(void *data)
  3393. {
  3394. struct softnet_data *sd = data;
  3395. ____napi_schedule(sd, &sd->backlog);
  3396. sd->received_rps++;
  3397. }
  3398. #endif /* CONFIG_RPS */
  3399. /*
  3400. * Check if this softnet_data structure is another cpu one
  3401. * If yes, queue it to our IPI list and return 1
  3402. * If no, return 0
  3403. */
  3404. static int rps_ipi_queued(struct softnet_data *sd)
  3405. {
  3406. #ifdef CONFIG_RPS
  3407. struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
  3408. if (sd != mysd) {
  3409. sd->rps_ipi_next = mysd->rps_ipi_list;
  3410. mysd->rps_ipi_list = sd;
  3411. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  3412. return 1;
  3413. }
  3414. #endif /* CONFIG_RPS */
  3415. return 0;
  3416. }
  3417. #ifdef CONFIG_NET_FLOW_LIMIT
  3418. int netdev_flow_limit_table_len __read_mostly = (1 << 12);
  3419. #endif
  3420. static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
  3421. {
  3422. #ifdef CONFIG_NET_FLOW_LIMIT
  3423. struct sd_flow_limit *fl;
  3424. struct softnet_data *sd;
  3425. unsigned int old_flow, new_flow;
  3426. if (qlen < (netdev_max_backlog >> 1))
  3427. return false;
  3428. sd = this_cpu_ptr(&softnet_data);
  3429. rcu_read_lock();
  3430. fl = rcu_dereference(sd->flow_limit);
  3431. if (fl) {
  3432. new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
  3433. old_flow = fl->history[fl->history_head];
  3434. fl->history[fl->history_head] = new_flow;
  3435. fl->history_head++;
  3436. fl->history_head &= FLOW_LIMIT_HISTORY - 1;
  3437. if (likely(fl->buckets[old_flow]))
  3438. fl->buckets[old_flow]--;
  3439. if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
  3440. fl->count++;
  3441. rcu_read_unlock();
  3442. return true;
  3443. }
  3444. }
  3445. rcu_read_unlock();
  3446. #endif
  3447. return false;
  3448. }
  3449. /*
  3450. * enqueue_to_backlog is called to queue an skb to a per CPU backlog
  3451. * queue (may be a remote CPU queue).
  3452. */
  3453. static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
  3454. unsigned int *qtail)
  3455. {
  3456. struct softnet_data *sd;
  3457. unsigned long flags;
  3458. unsigned int qlen;
  3459. sd = &per_cpu(softnet_data, cpu);
  3460. local_irq_save(flags);
  3461. rps_lock(sd);
  3462. if (!netif_running(skb->dev))
  3463. goto drop;
  3464. qlen = skb_queue_len(&sd->input_pkt_queue);
  3465. if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
  3466. if (qlen) {
  3467. enqueue:
  3468. __skb_queue_tail(&sd->input_pkt_queue, skb);
  3469. input_queue_tail_incr_save(sd, qtail);
  3470. rps_unlock(sd);
  3471. local_irq_restore(flags);
  3472. return NET_RX_SUCCESS;
  3473. }
  3474. /* Schedule NAPI for backlog device
  3475. * We can use non atomic operation since we own the queue lock
  3476. */
  3477. if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
  3478. if (!rps_ipi_queued(sd))
  3479. ____napi_schedule(sd, &sd->backlog);
  3480. }
  3481. goto enqueue;
  3482. }
  3483. drop:
  3484. sd->dropped++;
  3485. rps_unlock(sd);
  3486. local_irq_restore(flags);
  3487. atomic_long_inc(&skb->dev->rx_dropped);
  3488. kfree_skb(skb);
  3489. return NET_RX_DROP;
  3490. }
  3491. static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
  3492. {
  3493. struct net_device *dev = skb->dev;
  3494. struct netdev_rx_queue *rxqueue;
  3495. rxqueue = dev->_rx;
  3496. if (skb_rx_queue_recorded(skb)) {
  3497. u16 index = skb_get_rx_queue(skb);
  3498. if (unlikely(index >= dev->real_num_rx_queues)) {
  3499. WARN_ONCE(dev->real_num_rx_queues > 1,
  3500. "%s received packet on queue %u, but number "
  3501. "of RX queues is %u\n",
  3502. dev->name, index, dev->real_num_rx_queues);
  3503. return rxqueue; /* Return first rxqueue */
  3504. }
  3505. rxqueue += index;
  3506. }
  3507. return rxqueue;
  3508. }
  3509. static u32 netif_receive_generic_xdp(struct sk_buff *skb,
  3510. struct xdp_buff *xdp,
  3511. struct bpf_prog *xdp_prog)
  3512. {
  3513. struct netdev_rx_queue *rxqueue;
  3514. void *orig_data, *orig_data_end;
  3515. u32 metalen, act = XDP_DROP;
  3516. int hlen, off;
  3517. u32 mac_len;
  3518. /* Reinjected packets coming from act_mirred or similar should
  3519. * not get XDP generic processing.
  3520. */
  3521. if (skb_cloned(skb))
  3522. return XDP_PASS;
  3523. /* XDP packets must be linear and must have sufficient headroom
  3524. * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
  3525. * native XDP provides, thus we need to do it here as well.
  3526. */
  3527. if (skb_is_nonlinear(skb) ||
  3528. skb_headroom(skb) < XDP_PACKET_HEADROOM) {
  3529. int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
  3530. int troom = skb->tail + skb->data_len - skb->end;
  3531. /* In case we have to go down the path and also linearize,
  3532. * then lets do the pskb_expand_head() work just once here.
  3533. */
  3534. if (pskb_expand_head(skb,
  3535. hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
  3536. troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
  3537. goto do_drop;
  3538. if (skb_linearize(skb))
  3539. goto do_drop;
  3540. }
  3541. /* The XDP program wants to see the packet starting at the MAC
  3542. * header.
  3543. */
  3544. mac_len = skb->data - skb_mac_header(skb);
  3545. hlen = skb_headlen(skb) + mac_len;
  3546. xdp->data = skb->data - mac_len;
  3547. xdp->data_meta = xdp->data;
  3548. xdp->data_end = xdp->data + hlen;
  3549. xdp->data_hard_start = skb->data - skb_headroom(skb);
  3550. orig_data_end = xdp->data_end;
  3551. orig_data = xdp->data;
  3552. rxqueue = netif_get_rxqueue(skb);
  3553. xdp->rxq = &rxqueue->xdp_rxq;
  3554. act = bpf_prog_run_xdp(xdp_prog, xdp);
  3555. off = xdp->data - orig_data;
  3556. if (off > 0)
  3557. __skb_pull(skb, off);
  3558. else if (off < 0)
  3559. __skb_push(skb, -off);
  3560. skb->mac_header += off;
  3561. /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
  3562. * pckt.
  3563. */
  3564. off = orig_data_end - xdp->data_end;
  3565. if (off != 0) {
  3566. skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
  3567. skb->len -= off;
  3568. }
  3569. switch (act) {
  3570. case XDP_REDIRECT:
  3571. case XDP_TX:
  3572. __skb_push(skb, mac_len);
  3573. break;
  3574. case XDP_PASS:
  3575. metalen = xdp->data - xdp->data_meta;
  3576. if (metalen)
  3577. skb_metadata_set(skb, metalen);
  3578. break;
  3579. default:
  3580. bpf_warn_invalid_xdp_action(act);
  3581. /* fall through */
  3582. case XDP_ABORTED:
  3583. trace_xdp_exception(skb->dev, xdp_prog, act);
  3584. /* fall through */
  3585. case XDP_DROP:
  3586. do_drop:
  3587. kfree_skb(skb);
  3588. break;
  3589. }
  3590. return act;
  3591. }
  3592. /* When doing generic XDP we have to bypass the qdisc layer and the
  3593. * network taps in order to match in-driver-XDP behavior.
  3594. */
  3595. void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
  3596. {
  3597. struct net_device *dev = skb->dev;
  3598. struct netdev_queue *txq;
  3599. bool free_skb = true;
  3600. int cpu, rc;
  3601. txq = netdev_pick_tx(dev, skb, NULL);
  3602. cpu = smp_processor_id();
  3603. HARD_TX_LOCK(dev, txq, cpu);
  3604. if (!netif_xmit_stopped(txq)) {
  3605. rc = netdev_start_xmit(skb, dev, txq, 0);
  3606. if (dev_xmit_complete(rc))
  3607. free_skb = false;
  3608. }
  3609. HARD_TX_UNLOCK(dev, txq);
  3610. if (free_skb) {
  3611. trace_xdp_exception(dev, xdp_prog, XDP_TX);
  3612. kfree_skb(skb);
  3613. }
  3614. }
  3615. EXPORT_SYMBOL_GPL(generic_xdp_tx);
  3616. static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
  3617. int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
  3618. {
  3619. if (xdp_prog) {
  3620. struct xdp_buff xdp;
  3621. u32 act;
  3622. int err;
  3623. act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
  3624. if (act != XDP_PASS) {
  3625. switch (act) {
  3626. case XDP_REDIRECT:
  3627. err = xdp_do_generic_redirect(skb->dev, skb,
  3628. &xdp, xdp_prog);
  3629. if (err)
  3630. goto out_redir;
  3631. break;
  3632. case XDP_TX:
  3633. generic_xdp_tx(skb, xdp_prog);
  3634. break;
  3635. }
  3636. return XDP_DROP;
  3637. }
  3638. }
  3639. return XDP_PASS;
  3640. out_redir:
  3641. kfree_skb(skb);
  3642. return XDP_DROP;
  3643. }
  3644. EXPORT_SYMBOL_GPL(do_xdp_generic);
  3645. static int netif_rx_internal(struct sk_buff *skb)
  3646. {
  3647. int ret;
  3648. net_timestamp_check(netdev_tstamp_prequeue, skb);
  3649. trace_netif_rx(skb);
  3650. if (static_branch_unlikely(&generic_xdp_needed_key)) {
  3651. int ret;
  3652. preempt_disable();
  3653. rcu_read_lock();
  3654. ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
  3655. rcu_read_unlock();
  3656. preempt_enable();
  3657. /* Consider XDP consuming the packet a success from
  3658. * the netdev point of view we do not want to count
  3659. * this as an error.
  3660. */
  3661. if (ret != XDP_PASS)
  3662. return NET_RX_SUCCESS;
  3663. }
  3664. #ifdef CONFIG_RPS
  3665. if (static_key_false(&rps_needed)) {
  3666. struct rps_dev_flow voidflow, *rflow = &voidflow;
  3667. int cpu;
  3668. preempt_disable();
  3669. rcu_read_lock();
  3670. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  3671. if (cpu < 0)
  3672. cpu = smp_processor_id();
  3673. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  3674. rcu_read_unlock();
  3675. preempt_enable();
  3676. } else
  3677. #endif
  3678. {
  3679. unsigned int qtail;
  3680. ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  3681. put_cpu();
  3682. }
  3683. return ret;
  3684. }
  3685. /**
  3686. * netif_rx - post buffer to the network code
  3687. * @skb: buffer to post
  3688. *
  3689. * This function receives a packet from a device driver and queues it for
  3690. * the upper (protocol) levels to process. It always succeeds. The buffer
  3691. * may be dropped during processing for congestion control or by the
  3692. * protocol layers.
  3693. *
  3694. * return values:
  3695. * NET_RX_SUCCESS (no congestion)
  3696. * NET_RX_DROP (packet was dropped)
  3697. *
  3698. */
  3699. int netif_rx(struct sk_buff *skb)
  3700. {
  3701. trace_netif_rx_entry(skb);
  3702. return netif_rx_internal(skb);
  3703. }
  3704. EXPORT_SYMBOL(netif_rx);
  3705. int netif_rx_ni(struct sk_buff *skb)
  3706. {
  3707. int err;
  3708. trace_netif_rx_ni_entry(skb);
  3709. preempt_disable();
  3710. err = netif_rx_internal(skb);
  3711. if (local_softirq_pending())
  3712. do_softirq();
  3713. preempt_enable();
  3714. return err;
  3715. }
  3716. EXPORT_SYMBOL(netif_rx_ni);
  3717. static __latent_entropy void net_tx_action(struct softirq_action *h)
  3718. {
  3719. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  3720. if (sd->completion_queue) {
  3721. struct sk_buff *clist;
  3722. local_irq_disable();
  3723. clist = sd->completion_queue;
  3724. sd->completion_queue = NULL;
  3725. local_irq_enable();
  3726. while (clist) {
  3727. struct sk_buff *skb = clist;
  3728. clist = clist->next;
  3729. WARN_ON(refcount_read(&skb->users));
  3730. if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
  3731. trace_consume_skb(skb);
  3732. else
  3733. trace_kfree_skb(skb, net_tx_action);
  3734. if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
  3735. __kfree_skb(skb);
  3736. else
  3737. __kfree_skb_defer(skb);
  3738. }
  3739. __kfree_skb_flush();
  3740. }
  3741. if (sd->output_queue) {
  3742. struct Qdisc *head;
  3743. local_irq_disable();
  3744. head = sd->output_queue;
  3745. sd->output_queue = NULL;
  3746. sd->output_queue_tailp = &sd->output_queue;
  3747. local_irq_enable();
  3748. while (head) {
  3749. struct Qdisc *q = head;
  3750. spinlock_t *root_lock = NULL;
  3751. head = head->next_sched;
  3752. if (!(q->flags & TCQ_F_NOLOCK)) {
  3753. root_lock = qdisc_lock(q);
  3754. spin_lock(root_lock);
  3755. }
  3756. /* We need to make sure head->next_sched is read
  3757. * before clearing __QDISC_STATE_SCHED
  3758. */
  3759. smp_mb__before_atomic();
  3760. clear_bit(__QDISC_STATE_SCHED, &q->state);
  3761. qdisc_run(q);
  3762. if (root_lock)
  3763. spin_unlock(root_lock);
  3764. }
  3765. }
  3766. xfrm_dev_backlog(sd);
  3767. }
  3768. #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
  3769. /* This hook is defined here for ATM LANE */
  3770. int (*br_fdb_test_addr_hook)(struct net_device *dev,
  3771. unsigned char *addr) __read_mostly;
  3772. EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
  3773. #endif
  3774. static inline struct sk_buff *
  3775. sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
  3776. struct net_device *orig_dev)
  3777. {
  3778. #ifdef CONFIG_NET_CLS_ACT
  3779. struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
  3780. struct tcf_result cl_res;
  3781. /* If there's at least one ingress present somewhere (so
  3782. * we get here via enabled static key), remaining devices
  3783. * that are not configured with an ingress qdisc will bail
  3784. * out here.
  3785. */
  3786. if (!miniq)
  3787. return skb;
  3788. if (*pt_prev) {
  3789. *ret = deliver_skb(skb, *pt_prev, orig_dev);
  3790. *pt_prev = NULL;
  3791. }
  3792. qdisc_skb_cb(skb)->pkt_len = skb->len;
  3793. skb->tc_at_ingress = 1;
  3794. mini_qdisc_bstats_cpu_update(miniq, skb);
  3795. switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
  3796. case TC_ACT_OK:
  3797. case TC_ACT_RECLASSIFY:
  3798. skb->tc_index = TC_H_MIN(cl_res.classid);
  3799. break;
  3800. case TC_ACT_SHOT:
  3801. mini_qdisc_qstats_cpu_drop(miniq);
  3802. kfree_skb(skb);
  3803. return NULL;
  3804. case TC_ACT_STOLEN:
  3805. case TC_ACT_QUEUED:
  3806. case TC_ACT_TRAP:
  3807. consume_skb(skb);
  3808. return NULL;
  3809. case TC_ACT_REDIRECT:
  3810. /* skb_mac_header check was done by cls/act_bpf, so
  3811. * we can safely push the L2 header back before
  3812. * redirecting to another netdev
  3813. */
  3814. __skb_push(skb, skb->mac_len);
  3815. skb_do_redirect(skb);
  3816. return NULL;
  3817. default:
  3818. break;
  3819. }
  3820. #endif /* CONFIG_NET_CLS_ACT */
  3821. return skb;
  3822. }
  3823. /**
  3824. * netdev_is_rx_handler_busy - check if receive handler is registered
  3825. * @dev: device to check
  3826. *
  3827. * Check if a receive handler is already registered for a given device.
  3828. * Return true if there one.
  3829. *
  3830. * The caller must hold the rtnl_mutex.
  3831. */
  3832. bool netdev_is_rx_handler_busy(struct net_device *dev)
  3833. {
  3834. ASSERT_RTNL();
  3835. return dev && rtnl_dereference(dev->rx_handler);
  3836. }
  3837. EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
  3838. /**
  3839. * netdev_rx_handler_register - register receive handler
  3840. * @dev: device to register a handler for
  3841. * @rx_handler: receive handler to register
  3842. * @rx_handler_data: data pointer that is used by rx handler
  3843. *
  3844. * Register a receive handler for a device. This handler will then be
  3845. * called from __netif_receive_skb. A negative errno code is returned
  3846. * on a failure.
  3847. *
  3848. * The caller must hold the rtnl_mutex.
  3849. *
  3850. * For a general description of rx_handler, see enum rx_handler_result.
  3851. */
  3852. int netdev_rx_handler_register(struct net_device *dev,
  3853. rx_handler_func_t *rx_handler,
  3854. void *rx_handler_data)
  3855. {
  3856. if (netdev_is_rx_handler_busy(dev))
  3857. return -EBUSY;
  3858. if (dev->priv_flags & IFF_NO_RX_HANDLER)
  3859. return -EINVAL;
  3860. /* Note: rx_handler_data must be set before rx_handler */
  3861. rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
  3862. rcu_assign_pointer(dev->rx_handler, rx_handler);
  3863. return 0;
  3864. }
  3865. EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
  3866. /**
  3867. * netdev_rx_handler_unregister - unregister receive handler
  3868. * @dev: device to unregister a handler from
  3869. *
  3870. * Unregister a receive handler from a device.
  3871. *
  3872. * The caller must hold the rtnl_mutex.
  3873. */
  3874. void netdev_rx_handler_unregister(struct net_device *dev)
  3875. {
  3876. ASSERT_RTNL();
  3877. RCU_INIT_POINTER(dev->rx_handler, NULL);
  3878. /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
  3879. * section has a guarantee to see a non NULL rx_handler_data
  3880. * as well.
  3881. */
  3882. synchronize_net();
  3883. RCU_INIT_POINTER(dev->rx_handler_data, NULL);
  3884. }
  3885. EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
  3886. /*
  3887. * Limit the use of PFMEMALLOC reserves to those protocols that implement
  3888. * the special handling of PFMEMALLOC skbs.
  3889. */
  3890. static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
  3891. {
  3892. switch (skb->protocol) {
  3893. case htons(ETH_P_ARP):
  3894. case htons(ETH_P_IP):
  3895. case htons(ETH_P_IPV6):
  3896. case htons(ETH_P_8021Q):
  3897. case htons(ETH_P_8021AD):
  3898. return true;
  3899. default:
  3900. return false;
  3901. }
  3902. }
  3903. static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
  3904. int *ret, struct net_device *orig_dev)
  3905. {
  3906. #ifdef CONFIG_NETFILTER_INGRESS
  3907. if (nf_hook_ingress_active(skb)) {
  3908. int ingress_retval;
  3909. if (*pt_prev) {
  3910. *ret = deliver_skb(skb, *pt_prev, orig_dev);
  3911. *pt_prev = NULL;
  3912. }
  3913. rcu_read_lock();
  3914. ingress_retval = nf_hook_ingress(skb);
  3915. rcu_read_unlock();
  3916. return ingress_retval;
  3917. }
  3918. #endif /* CONFIG_NETFILTER_INGRESS */
  3919. return 0;
  3920. }
  3921. static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc)
  3922. {
  3923. struct packet_type *ptype, *pt_prev;
  3924. rx_handler_func_t *rx_handler;
  3925. struct net_device *orig_dev;
  3926. bool deliver_exact = false;
  3927. int ret = NET_RX_DROP;
  3928. __be16 type;
  3929. net_timestamp_check(!netdev_tstamp_prequeue, skb);
  3930. trace_netif_receive_skb(skb);
  3931. orig_dev = skb->dev;
  3932. skb_reset_network_header(skb);
  3933. if (!skb_transport_header_was_set(skb))
  3934. skb_reset_transport_header(skb);
  3935. skb_reset_mac_len(skb);
  3936. pt_prev = NULL;
  3937. another_round:
  3938. skb->skb_iif = skb->dev->ifindex;
  3939. __this_cpu_inc(softnet_data.processed);
  3940. if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
  3941. skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
  3942. skb = skb_vlan_untag(skb);
  3943. if (unlikely(!skb))
  3944. goto out;
  3945. }
  3946. if (skb_skip_tc_classify(skb))
  3947. goto skip_classify;
  3948. if (pfmemalloc)
  3949. goto skip_taps;
  3950. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  3951. if (pt_prev)
  3952. ret = deliver_skb(skb, pt_prev, orig_dev);
  3953. pt_prev = ptype;
  3954. }
  3955. list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
  3956. if (pt_prev)
  3957. ret = deliver_skb(skb, pt_prev, orig_dev);
  3958. pt_prev = ptype;
  3959. }
  3960. skip_taps:
  3961. #ifdef CONFIG_NET_INGRESS
  3962. if (static_branch_unlikely(&ingress_needed_key)) {
  3963. skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
  3964. if (!skb)
  3965. goto out;
  3966. if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
  3967. goto out;
  3968. }
  3969. #endif
  3970. skb_reset_tc(skb);
  3971. skip_classify:
  3972. if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
  3973. goto drop;
  3974. if (skb_vlan_tag_present(skb)) {
  3975. if (pt_prev) {
  3976. ret = deliver_skb(skb, pt_prev, orig_dev);
  3977. pt_prev = NULL;
  3978. }
  3979. if (vlan_do_receive(&skb))
  3980. goto another_round;
  3981. else if (unlikely(!skb))
  3982. goto out;
  3983. }
  3984. rx_handler = rcu_dereference(skb->dev->rx_handler);
  3985. if (rx_handler) {
  3986. if (pt_prev) {
  3987. ret = deliver_skb(skb, pt_prev, orig_dev);
  3988. pt_prev = NULL;
  3989. }
  3990. switch (rx_handler(&skb)) {
  3991. case RX_HANDLER_CONSUMED:
  3992. ret = NET_RX_SUCCESS;
  3993. goto out;
  3994. case RX_HANDLER_ANOTHER:
  3995. goto another_round;
  3996. case RX_HANDLER_EXACT:
  3997. deliver_exact = true;
  3998. case RX_HANDLER_PASS:
  3999. break;
  4000. default:
  4001. BUG();
  4002. }
  4003. }
  4004. if (unlikely(skb_vlan_tag_present(skb))) {
  4005. if (skb_vlan_tag_get_id(skb))
  4006. skb->pkt_type = PACKET_OTHERHOST;
  4007. /* Note: we might in the future use prio bits
  4008. * and set skb->priority like in vlan_do_receive()
  4009. * For the time being, just ignore Priority Code Point
  4010. */
  4011. skb->vlan_tci = 0;
  4012. }
  4013. type = skb->protocol;
  4014. /* deliver only exact match when indicated */
  4015. if (likely(!deliver_exact)) {
  4016. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  4017. &ptype_base[ntohs(type) &
  4018. PTYPE_HASH_MASK]);
  4019. }
  4020. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  4021. &orig_dev->ptype_specific);
  4022. if (unlikely(skb->dev != orig_dev)) {
  4023. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  4024. &skb->dev->ptype_specific);
  4025. }
  4026. if (pt_prev) {
  4027. if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
  4028. goto drop;
  4029. else
  4030. ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  4031. } else {
  4032. drop:
  4033. if (!deliver_exact)
  4034. atomic_long_inc(&skb->dev->rx_dropped);
  4035. else
  4036. atomic_long_inc(&skb->dev->rx_nohandler);
  4037. kfree_skb(skb);
  4038. /* Jamal, now you will not able to escape explaining
  4039. * me how you were going to use this. :-)
  4040. */
  4041. ret = NET_RX_DROP;
  4042. }
  4043. out:
  4044. return ret;
  4045. }
  4046. /**
  4047. * netif_receive_skb_core - special purpose version of netif_receive_skb
  4048. * @skb: buffer to process
  4049. *
  4050. * More direct receive version of netif_receive_skb(). It should
  4051. * only be used by callers that have a need to skip RPS and Generic XDP.
  4052. * Caller must also take care of handling if (page_is_)pfmemalloc.
  4053. *
  4054. * This function may only be called from softirq context and interrupts
  4055. * should be enabled.
  4056. *
  4057. * Return values (usually ignored):
  4058. * NET_RX_SUCCESS: no congestion
  4059. * NET_RX_DROP: packet was dropped
  4060. */
  4061. int netif_receive_skb_core(struct sk_buff *skb)
  4062. {
  4063. int ret;
  4064. rcu_read_lock();
  4065. ret = __netif_receive_skb_core(skb, false);
  4066. rcu_read_unlock();
  4067. return ret;
  4068. }
  4069. EXPORT_SYMBOL(netif_receive_skb_core);
  4070. static int __netif_receive_skb(struct sk_buff *skb)
  4071. {
  4072. int ret;
  4073. if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
  4074. unsigned int noreclaim_flag;
  4075. /*
  4076. * PFMEMALLOC skbs are special, they should
  4077. * - be delivered to SOCK_MEMALLOC sockets only
  4078. * - stay away from userspace
  4079. * - have bounded memory usage
  4080. *
  4081. * Use PF_MEMALLOC as this saves us from propagating the allocation
  4082. * context down to all allocation sites.
  4083. */
  4084. noreclaim_flag = memalloc_noreclaim_save();
  4085. ret = __netif_receive_skb_core(skb, true);
  4086. memalloc_noreclaim_restore(noreclaim_flag);
  4087. } else
  4088. ret = __netif_receive_skb_core(skb, false);
  4089. return ret;
  4090. }
  4091. static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
  4092. {
  4093. struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
  4094. struct bpf_prog *new = xdp->prog;
  4095. int ret = 0;
  4096. switch (xdp->command) {
  4097. case XDP_SETUP_PROG:
  4098. rcu_assign_pointer(dev->xdp_prog, new);
  4099. if (old)
  4100. bpf_prog_put(old);
  4101. if (old && !new) {
  4102. static_branch_dec(&generic_xdp_needed_key);
  4103. } else if (new && !old) {
  4104. static_branch_inc(&generic_xdp_needed_key);
  4105. dev_disable_lro(dev);
  4106. dev_disable_gro_hw(dev);
  4107. }
  4108. break;
  4109. case XDP_QUERY_PROG:
  4110. xdp->prog_attached = !!old;
  4111. xdp->prog_id = old ? old->aux->id : 0;
  4112. break;
  4113. default:
  4114. ret = -EINVAL;
  4115. break;
  4116. }
  4117. return ret;
  4118. }
  4119. static int netif_receive_skb_internal(struct sk_buff *skb)
  4120. {
  4121. int ret;
  4122. net_timestamp_check(netdev_tstamp_prequeue, skb);
  4123. if (skb_defer_rx_timestamp(skb))
  4124. return NET_RX_SUCCESS;
  4125. if (static_branch_unlikely(&generic_xdp_needed_key)) {
  4126. int ret;
  4127. preempt_disable();
  4128. rcu_read_lock();
  4129. ret = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
  4130. rcu_read_unlock();
  4131. preempt_enable();
  4132. if (ret != XDP_PASS)
  4133. return NET_RX_DROP;
  4134. }
  4135. rcu_read_lock();
  4136. #ifdef CONFIG_RPS
  4137. if (static_key_false(&rps_needed)) {
  4138. struct rps_dev_flow voidflow, *rflow = &voidflow;
  4139. int cpu = get_rps_cpu(skb->dev, skb, &rflow);
  4140. if (cpu >= 0) {
  4141. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  4142. rcu_read_unlock();
  4143. return ret;
  4144. }
  4145. }
  4146. #endif
  4147. ret = __netif_receive_skb(skb);
  4148. rcu_read_unlock();
  4149. return ret;
  4150. }
  4151. /**
  4152. * netif_receive_skb - process receive buffer from network
  4153. * @skb: buffer to process
  4154. *
  4155. * netif_receive_skb() is the main receive data processing function.
  4156. * It always succeeds. The buffer may be dropped during processing
  4157. * for congestion control or by the protocol layers.
  4158. *
  4159. * This function may only be called from softirq context and interrupts
  4160. * should be enabled.
  4161. *
  4162. * Return values (usually ignored):
  4163. * NET_RX_SUCCESS: no congestion
  4164. * NET_RX_DROP: packet was dropped
  4165. */
  4166. int netif_receive_skb(struct sk_buff *skb)
  4167. {
  4168. trace_netif_receive_skb_entry(skb);
  4169. return netif_receive_skb_internal(skb);
  4170. }
  4171. EXPORT_SYMBOL(netif_receive_skb);
  4172. DEFINE_PER_CPU(struct work_struct, flush_works);
  4173. /* Network device is going away, flush any packets still pending */
  4174. static void flush_backlog(struct work_struct *work)
  4175. {
  4176. struct sk_buff *skb, *tmp;
  4177. struct softnet_data *sd;
  4178. local_bh_disable();
  4179. sd = this_cpu_ptr(&softnet_data);
  4180. local_irq_disable();
  4181. rps_lock(sd);
  4182. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  4183. if (skb->dev->reg_state == NETREG_UNREGISTERING) {
  4184. __skb_unlink(skb, &sd->input_pkt_queue);
  4185. kfree_skb(skb);
  4186. input_queue_head_incr(sd);
  4187. }
  4188. }
  4189. rps_unlock(sd);
  4190. local_irq_enable();
  4191. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  4192. if (skb->dev->reg_state == NETREG_UNREGISTERING) {
  4193. __skb_unlink(skb, &sd->process_queue);
  4194. kfree_skb(skb);
  4195. input_queue_head_incr(sd);
  4196. }
  4197. }
  4198. local_bh_enable();
  4199. }
  4200. static void flush_all_backlogs(void)
  4201. {
  4202. unsigned int cpu;
  4203. get_online_cpus();
  4204. for_each_online_cpu(cpu)
  4205. queue_work_on(cpu, system_highpri_wq,
  4206. per_cpu_ptr(&flush_works, cpu));
  4207. for_each_online_cpu(cpu)
  4208. flush_work(per_cpu_ptr(&flush_works, cpu));
  4209. put_online_cpus();
  4210. }
  4211. static int napi_gro_complete(struct sk_buff *skb)
  4212. {
  4213. struct packet_offload *ptype;
  4214. __be16 type = skb->protocol;
  4215. struct list_head *head = &offload_base;
  4216. int err = -ENOENT;
  4217. BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
  4218. if (NAPI_GRO_CB(skb)->count == 1) {
  4219. skb_shinfo(skb)->gso_size = 0;
  4220. goto out;
  4221. }
  4222. rcu_read_lock();
  4223. list_for_each_entry_rcu(ptype, head, list) {
  4224. if (ptype->type != type || !ptype->callbacks.gro_complete)
  4225. continue;
  4226. err = ptype->callbacks.gro_complete(skb, 0);
  4227. break;
  4228. }
  4229. rcu_read_unlock();
  4230. if (err) {
  4231. WARN_ON(&ptype->list == head);
  4232. kfree_skb(skb);
  4233. return NET_RX_SUCCESS;
  4234. }
  4235. out:
  4236. return netif_receive_skb_internal(skb);
  4237. }
  4238. static void __napi_gro_flush_chain(struct napi_struct *napi, struct list_head *head,
  4239. bool flush_old)
  4240. {
  4241. struct sk_buff *skb, *p;
  4242. list_for_each_entry_safe_reverse(skb, p, head, list) {
  4243. if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
  4244. return;
  4245. list_del_init(&skb->list);
  4246. napi_gro_complete(skb);
  4247. napi->gro_count--;
  4248. }
  4249. }
  4250. /* napi->gro_hash contains packets ordered by age.
  4251. * youngest packets at the head of it.
  4252. * Complete skbs in reverse order to reduce latencies.
  4253. */
  4254. void napi_gro_flush(struct napi_struct *napi, bool flush_old)
  4255. {
  4256. int i;
  4257. for (i = 0; i < GRO_HASH_BUCKETS; i++) {
  4258. struct list_head *head = &napi->gro_hash[i];
  4259. __napi_gro_flush_chain(napi, head, flush_old);
  4260. }
  4261. }
  4262. EXPORT_SYMBOL(napi_gro_flush);
  4263. static struct list_head *gro_list_prepare(struct napi_struct *napi,
  4264. struct sk_buff *skb)
  4265. {
  4266. unsigned int maclen = skb->dev->hard_header_len;
  4267. u32 hash = skb_get_hash_raw(skb);
  4268. struct list_head *head;
  4269. struct sk_buff *p;
  4270. head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)];
  4271. list_for_each_entry(p, head, list) {
  4272. unsigned long diffs;
  4273. NAPI_GRO_CB(p)->flush = 0;
  4274. if (hash != skb_get_hash_raw(p)) {
  4275. NAPI_GRO_CB(p)->same_flow = 0;
  4276. continue;
  4277. }
  4278. diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
  4279. diffs |= p->vlan_tci ^ skb->vlan_tci;
  4280. diffs |= skb_metadata_dst_cmp(p, skb);
  4281. diffs |= skb_metadata_differs(p, skb);
  4282. if (maclen == ETH_HLEN)
  4283. diffs |= compare_ether_header(skb_mac_header(p),
  4284. skb_mac_header(skb));
  4285. else if (!diffs)
  4286. diffs = memcmp(skb_mac_header(p),
  4287. skb_mac_header(skb),
  4288. maclen);
  4289. NAPI_GRO_CB(p)->same_flow = !diffs;
  4290. }
  4291. return head;
  4292. }
  4293. static void skb_gro_reset_offset(struct sk_buff *skb)
  4294. {
  4295. const struct skb_shared_info *pinfo = skb_shinfo(skb);
  4296. const skb_frag_t *frag0 = &pinfo->frags[0];
  4297. NAPI_GRO_CB(skb)->data_offset = 0;
  4298. NAPI_GRO_CB(skb)->frag0 = NULL;
  4299. NAPI_GRO_CB(skb)->frag0_len = 0;
  4300. if (skb_mac_header(skb) == skb_tail_pointer(skb) &&
  4301. pinfo->nr_frags &&
  4302. !PageHighMem(skb_frag_page(frag0))) {
  4303. NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
  4304. NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
  4305. skb_frag_size(frag0),
  4306. skb->end - skb->tail);
  4307. }
  4308. }
  4309. static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
  4310. {
  4311. struct skb_shared_info *pinfo = skb_shinfo(skb);
  4312. BUG_ON(skb->end - skb->tail < grow);
  4313. memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
  4314. skb->data_len -= grow;
  4315. skb->tail += grow;
  4316. pinfo->frags[0].page_offset += grow;
  4317. skb_frag_size_sub(&pinfo->frags[0], grow);
  4318. if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
  4319. skb_frag_unref(skb, 0);
  4320. memmove(pinfo->frags, pinfo->frags + 1,
  4321. --pinfo->nr_frags * sizeof(pinfo->frags[0]));
  4322. }
  4323. }
  4324. static void gro_flush_oldest(struct napi_struct *napi)
  4325. {
  4326. struct sk_buff *oldest = NULL;
  4327. unsigned long age = jiffies;
  4328. int i;
  4329. for (i = 0; i < GRO_HASH_BUCKETS; i++) {
  4330. struct list_head *head = &napi->gro_hash[i];
  4331. struct sk_buff *skb;
  4332. if (list_empty(head))
  4333. continue;
  4334. skb = list_last_entry(head, struct sk_buff, list);
  4335. if (!oldest || time_before(NAPI_GRO_CB(skb)->age, age)) {
  4336. oldest = skb;
  4337. age = NAPI_GRO_CB(skb)->age;
  4338. }
  4339. }
  4340. /* We are called with napi->gro_count >= MAX_GRO_SKBS, so this is
  4341. * impossible.
  4342. */
  4343. if (WARN_ON_ONCE(!oldest))
  4344. return;
  4345. /* Do not adjust napi->gro_count, caller is adding a new SKB to
  4346. * the chain.
  4347. */
  4348. list_del(&oldest->list);
  4349. napi_gro_complete(oldest);
  4350. }
  4351. static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  4352. {
  4353. struct list_head *head = &offload_base;
  4354. struct packet_offload *ptype;
  4355. __be16 type = skb->protocol;
  4356. struct list_head *gro_head;
  4357. struct sk_buff *pp = NULL;
  4358. enum gro_result ret;
  4359. int same_flow;
  4360. int grow;
  4361. if (netif_elide_gro(skb->dev))
  4362. goto normal;
  4363. gro_head = gro_list_prepare(napi, skb);
  4364. rcu_read_lock();
  4365. list_for_each_entry_rcu(ptype, head, list) {
  4366. if (ptype->type != type || !ptype->callbacks.gro_receive)
  4367. continue;
  4368. skb_set_network_header(skb, skb_gro_offset(skb));
  4369. skb_reset_mac_len(skb);
  4370. NAPI_GRO_CB(skb)->same_flow = 0;
  4371. NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
  4372. NAPI_GRO_CB(skb)->free = 0;
  4373. NAPI_GRO_CB(skb)->encap_mark = 0;
  4374. NAPI_GRO_CB(skb)->recursion_counter = 0;
  4375. NAPI_GRO_CB(skb)->is_fou = 0;
  4376. NAPI_GRO_CB(skb)->is_atomic = 1;
  4377. NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
  4378. /* Setup for GRO checksum validation */
  4379. switch (skb->ip_summed) {
  4380. case CHECKSUM_COMPLETE:
  4381. NAPI_GRO_CB(skb)->csum = skb->csum;
  4382. NAPI_GRO_CB(skb)->csum_valid = 1;
  4383. NAPI_GRO_CB(skb)->csum_cnt = 0;
  4384. break;
  4385. case CHECKSUM_UNNECESSARY:
  4386. NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
  4387. NAPI_GRO_CB(skb)->csum_valid = 0;
  4388. break;
  4389. default:
  4390. NAPI_GRO_CB(skb)->csum_cnt = 0;
  4391. NAPI_GRO_CB(skb)->csum_valid = 0;
  4392. }
  4393. pp = ptype->callbacks.gro_receive(gro_head, skb);
  4394. break;
  4395. }
  4396. rcu_read_unlock();
  4397. if (&ptype->list == head)
  4398. goto normal;
  4399. if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) {
  4400. ret = GRO_CONSUMED;
  4401. goto ok;
  4402. }
  4403. same_flow = NAPI_GRO_CB(skb)->same_flow;
  4404. ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
  4405. if (pp) {
  4406. list_del_init(&pp->list);
  4407. napi_gro_complete(pp);
  4408. napi->gro_count--;
  4409. }
  4410. if (same_flow)
  4411. goto ok;
  4412. if (NAPI_GRO_CB(skb)->flush)
  4413. goto normal;
  4414. if (unlikely(napi->gro_count >= MAX_GRO_SKBS)) {
  4415. gro_flush_oldest(napi);
  4416. } else {
  4417. napi->gro_count++;
  4418. }
  4419. NAPI_GRO_CB(skb)->count = 1;
  4420. NAPI_GRO_CB(skb)->age = jiffies;
  4421. NAPI_GRO_CB(skb)->last = skb;
  4422. skb_shinfo(skb)->gso_size = skb_gro_len(skb);
  4423. list_add(&skb->list, gro_head);
  4424. ret = GRO_HELD;
  4425. pull:
  4426. grow = skb_gro_offset(skb) - skb_headlen(skb);
  4427. if (grow > 0)
  4428. gro_pull_from_frag0(skb, grow);
  4429. ok:
  4430. return ret;
  4431. normal:
  4432. ret = GRO_NORMAL;
  4433. goto pull;
  4434. }
  4435. struct packet_offload *gro_find_receive_by_type(__be16 type)
  4436. {
  4437. struct list_head *offload_head = &offload_base;
  4438. struct packet_offload *ptype;
  4439. list_for_each_entry_rcu(ptype, offload_head, list) {
  4440. if (ptype->type != type || !ptype->callbacks.gro_receive)
  4441. continue;
  4442. return ptype;
  4443. }
  4444. return NULL;
  4445. }
  4446. EXPORT_SYMBOL(gro_find_receive_by_type);
  4447. struct packet_offload *gro_find_complete_by_type(__be16 type)
  4448. {
  4449. struct list_head *offload_head = &offload_base;
  4450. struct packet_offload *ptype;
  4451. list_for_each_entry_rcu(ptype, offload_head, list) {
  4452. if (ptype->type != type || !ptype->callbacks.gro_complete)
  4453. continue;
  4454. return ptype;
  4455. }
  4456. return NULL;
  4457. }
  4458. EXPORT_SYMBOL(gro_find_complete_by_type);
  4459. static void napi_skb_free_stolen_head(struct sk_buff *skb)
  4460. {
  4461. skb_dst_drop(skb);
  4462. secpath_reset(skb);
  4463. kmem_cache_free(skbuff_head_cache, skb);
  4464. }
  4465. static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
  4466. {
  4467. switch (ret) {
  4468. case GRO_NORMAL:
  4469. if (netif_receive_skb_internal(skb))
  4470. ret = GRO_DROP;
  4471. break;
  4472. case GRO_DROP:
  4473. kfree_skb(skb);
  4474. break;
  4475. case GRO_MERGED_FREE:
  4476. if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
  4477. napi_skb_free_stolen_head(skb);
  4478. else
  4479. __kfree_skb(skb);
  4480. break;
  4481. case GRO_HELD:
  4482. case GRO_MERGED:
  4483. case GRO_CONSUMED:
  4484. break;
  4485. }
  4486. return ret;
  4487. }
  4488. gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  4489. {
  4490. skb_mark_napi_id(skb, napi);
  4491. trace_napi_gro_receive_entry(skb);
  4492. skb_gro_reset_offset(skb);
  4493. return napi_skb_finish(dev_gro_receive(napi, skb), skb);
  4494. }
  4495. EXPORT_SYMBOL(napi_gro_receive);
  4496. static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
  4497. {
  4498. if (unlikely(skb->pfmemalloc)) {
  4499. consume_skb(skb);
  4500. return;
  4501. }
  4502. __skb_pull(skb, skb_headlen(skb));
  4503. /* restore the reserve we had after netdev_alloc_skb_ip_align() */
  4504. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
  4505. skb->vlan_tci = 0;
  4506. skb->dev = napi->dev;
  4507. skb->skb_iif = 0;
  4508. skb->encapsulation = 0;
  4509. skb_shinfo(skb)->gso_type = 0;
  4510. skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
  4511. secpath_reset(skb);
  4512. napi->skb = skb;
  4513. }
  4514. struct sk_buff *napi_get_frags(struct napi_struct *napi)
  4515. {
  4516. struct sk_buff *skb = napi->skb;
  4517. if (!skb) {
  4518. skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
  4519. if (skb) {
  4520. napi->skb = skb;
  4521. skb_mark_napi_id(skb, napi);
  4522. }
  4523. }
  4524. return skb;
  4525. }
  4526. EXPORT_SYMBOL(napi_get_frags);
  4527. static gro_result_t napi_frags_finish(struct napi_struct *napi,
  4528. struct sk_buff *skb,
  4529. gro_result_t ret)
  4530. {
  4531. switch (ret) {
  4532. case GRO_NORMAL:
  4533. case GRO_HELD:
  4534. __skb_push(skb, ETH_HLEN);
  4535. skb->protocol = eth_type_trans(skb, skb->dev);
  4536. if (ret == GRO_NORMAL && netif_receive_skb_internal(skb))
  4537. ret = GRO_DROP;
  4538. break;
  4539. case GRO_DROP:
  4540. napi_reuse_skb(napi, skb);
  4541. break;
  4542. case GRO_MERGED_FREE:
  4543. if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
  4544. napi_skb_free_stolen_head(skb);
  4545. else
  4546. napi_reuse_skb(napi, skb);
  4547. break;
  4548. case GRO_MERGED:
  4549. case GRO_CONSUMED:
  4550. break;
  4551. }
  4552. return ret;
  4553. }
  4554. /* Upper GRO stack assumes network header starts at gro_offset=0
  4555. * Drivers could call both napi_gro_frags() and napi_gro_receive()
  4556. * We copy ethernet header into skb->data to have a common layout.
  4557. */
  4558. static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
  4559. {
  4560. struct sk_buff *skb = napi->skb;
  4561. const struct ethhdr *eth;
  4562. unsigned int hlen = sizeof(*eth);
  4563. napi->skb = NULL;
  4564. skb_reset_mac_header(skb);
  4565. skb_gro_reset_offset(skb);
  4566. eth = skb_gro_header_fast(skb, 0);
  4567. if (unlikely(skb_gro_header_hard(skb, hlen))) {
  4568. eth = skb_gro_header_slow(skb, hlen, 0);
  4569. if (unlikely(!eth)) {
  4570. net_warn_ratelimited("%s: dropping impossible skb from %s\n",
  4571. __func__, napi->dev->name);
  4572. napi_reuse_skb(napi, skb);
  4573. return NULL;
  4574. }
  4575. } else {
  4576. gro_pull_from_frag0(skb, hlen);
  4577. NAPI_GRO_CB(skb)->frag0 += hlen;
  4578. NAPI_GRO_CB(skb)->frag0_len -= hlen;
  4579. }
  4580. __skb_pull(skb, hlen);
  4581. /*
  4582. * This works because the only protocols we care about don't require
  4583. * special handling.
  4584. * We'll fix it up properly in napi_frags_finish()
  4585. */
  4586. skb->protocol = eth->h_proto;
  4587. return skb;
  4588. }
  4589. gro_result_t napi_gro_frags(struct napi_struct *napi)
  4590. {
  4591. struct sk_buff *skb = napi_frags_skb(napi);
  4592. if (!skb)
  4593. return GRO_DROP;
  4594. trace_napi_gro_frags_entry(skb);
  4595. return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
  4596. }
  4597. EXPORT_SYMBOL(napi_gro_frags);
  4598. /* Compute the checksum from gro_offset and return the folded value
  4599. * after adding in any pseudo checksum.
  4600. */
  4601. __sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
  4602. {
  4603. __wsum wsum;
  4604. __sum16 sum;
  4605. wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
  4606. /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
  4607. sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
  4608. if (likely(!sum)) {
  4609. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
  4610. !skb->csum_complete_sw)
  4611. netdev_rx_csum_fault(skb->dev);
  4612. }
  4613. NAPI_GRO_CB(skb)->csum = wsum;
  4614. NAPI_GRO_CB(skb)->csum_valid = 1;
  4615. return sum;
  4616. }
  4617. EXPORT_SYMBOL(__skb_gro_checksum_complete);
  4618. static void net_rps_send_ipi(struct softnet_data *remsd)
  4619. {
  4620. #ifdef CONFIG_RPS
  4621. while (remsd) {
  4622. struct softnet_data *next = remsd->rps_ipi_next;
  4623. if (cpu_online(remsd->cpu))
  4624. smp_call_function_single_async(remsd->cpu, &remsd->csd);
  4625. remsd = next;
  4626. }
  4627. #endif
  4628. }
  4629. /*
  4630. * net_rps_action_and_irq_enable sends any pending IPI's for rps.
  4631. * Note: called with local irq disabled, but exits with local irq enabled.
  4632. */
  4633. static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  4634. {
  4635. #ifdef CONFIG_RPS
  4636. struct softnet_data *remsd = sd->rps_ipi_list;
  4637. if (remsd) {
  4638. sd->rps_ipi_list = NULL;
  4639. local_irq_enable();
  4640. /* Send pending IPI's to kick RPS processing on remote cpus. */
  4641. net_rps_send_ipi(remsd);
  4642. } else
  4643. #endif
  4644. local_irq_enable();
  4645. }
  4646. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  4647. {
  4648. #ifdef CONFIG_RPS
  4649. return sd->rps_ipi_list != NULL;
  4650. #else
  4651. return false;
  4652. #endif
  4653. }
  4654. static int process_backlog(struct napi_struct *napi, int quota)
  4655. {
  4656. struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
  4657. bool again = true;
  4658. int work = 0;
  4659. /* Check if we have pending ipi, its better to send them now,
  4660. * not waiting net_rx_action() end.
  4661. */
  4662. if (sd_has_rps_ipi_waiting(sd)) {
  4663. local_irq_disable();
  4664. net_rps_action_and_irq_enable(sd);
  4665. }
  4666. napi->weight = dev_rx_weight;
  4667. while (again) {
  4668. struct sk_buff *skb;
  4669. while ((skb = __skb_dequeue(&sd->process_queue))) {
  4670. rcu_read_lock();
  4671. __netif_receive_skb(skb);
  4672. rcu_read_unlock();
  4673. input_queue_head_incr(sd);
  4674. if (++work >= quota)
  4675. return work;
  4676. }
  4677. local_irq_disable();
  4678. rps_lock(sd);
  4679. if (skb_queue_empty(&sd->input_pkt_queue)) {
  4680. /*
  4681. * Inline a custom version of __napi_complete().
  4682. * only current cpu owns and manipulates this napi,
  4683. * and NAPI_STATE_SCHED is the only possible flag set
  4684. * on backlog.
  4685. * We can use a plain write instead of clear_bit(),
  4686. * and we dont need an smp_mb() memory barrier.
  4687. */
  4688. napi->state = 0;
  4689. again = false;
  4690. } else {
  4691. skb_queue_splice_tail_init(&sd->input_pkt_queue,
  4692. &sd->process_queue);
  4693. }
  4694. rps_unlock(sd);
  4695. local_irq_enable();
  4696. }
  4697. return work;
  4698. }
  4699. /**
  4700. * __napi_schedule - schedule for receive
  4701. * @n: entry to schedule
  4702. *
  4703. * The entry's receive function will be scheduled to run.
  4704. * Consider using __napi_schedule_irqoff() if hard irqs are masked.
  4705. */
  4706. void __napi_schedule(struct napi_struct *n)
  4707. {
  4708. unsigned long flags;
  4709. local_irq_save(flags);
  4710. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  4711. local_irq_restore(flags);
  4712. }
  4713. EXPORT_SYMBOL(__napi_schedule);
  4714. /**
  4715. * napi_schedule_prep - check if napi can be scheduled
  4716. * @n: napi context
  4717. *
  4718. * Test if NAPI routine is already running, and if not mark
  4719. * it as running. This is used as a condition variable
  4720. * insure only one NAPI poll instance runs. We also make
  4721. * sure there is no pending NAPI disable.
  4722. */
  4723. bool napi_schedule_prep(struct napi_struct *n)
  4724. {
  4725. unsigned long val, new;
  4726. do {
  4727. val = READ_ONCE(n->state);
  4728. if (unlikely(val & NAPIF_STATE_DISABLE))
  4729. return false;
  4730. new = val | NAPIF_STATE_SCHED;
  4731. /* Sets STATE_MISSED bit if STATE_SCHED was already set
  4732. * This was suggested by Alexander Duyck, as compiler
  4733. * emits better code than :
  4734. * if (val & NAPIF_STATE_SCHED)
  4735. * new |= NAPIF_STATE_MISSED;
  4736. */
  4737. new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
  4738. NAPIF_STATE_MISSED;
  4739. } while (cmpxchg(&n->state, val, new) != val);
  4740. return !(val & NAPIF_STATE_SCHED);
  4741. }
  4742. EXPORT_SYMBOL(napi_schedule_prep);
  4743. /**
  4744. * __napi_schedule_irqoff - schedule for receive
  4745. * @n: entry to schedule
  4746. *
  4747. * Variant of __napi_schedule() assuming hard irqs are masked
  4748. */
  4749. void __napi_schedule_irqoff(struct napi_struct *n)
  4750. {
  4751. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  4752. }
  4753. EXPORT_SYMBOL(__napi_schedule_irqoff);
  4754. bool napi_complete_done(struct napi_struct *n, int work_done)
  4755. {
  4756. unsigned long flags, val, new;
  4757. /*
  4758. * 1) Don't let napi dequeue from the cpu poll list
  4759. * just in case its running on a different cpu.
  4760. * 2) If we are busy polling, do nothing here, we have
  4761. * the guarantee we will be called later.
  4762. */
  4763. if (unlikely(n->state & (NAPIF_STATE_NPSVC |
  4764. NAPIF_STATE_IN_BUSY_POLL)))
  4765. return false;
  4766. if (n->gro_count) {
  4767. unsigned long timeout = 0;
  4768. if (work_done)
  4769. timeout = n->dev->gro_flush_timeout;
  4770. if (timeout)
  4771. hrtimer_start(&n->timer, ns_to_ktime(timeout),
  4772. HRTIMER_MODE_REL_PINNED);
  4773. else
  4774. napi_gro_flush(n, false);
  4775. }
  4776. if (unlikely(!list_empty(&n->poll_list))) {
  4777. /* If n->poll_list is not empty, we need to mask irqs */
  4778. local_irq_save(flags);
  4779. list_del_init(&n->poll_list);
  4780. local_irq_restore(flags);
  4781. }
  4782. do {
  4783. val = READ_ONCE(n->state);
  4784. WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
  4785. new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
  4786. /* If STATE_MISSED was set, leave STATE_SCHED set,
  4787. * because we will call napi->poll() one more time.
  4788. * This C code was suggested by Alexander Duyck to help gcc.
  4789. */
  4790. new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
  4791. NAPIF_STATE_SCHED;
  4792. } while (cmpxchg(&n->state, val, new) != val);
  4793. if (unlikely(val & NAPIF_STATE_MISSED)) {
  4794. __napi_schedule(n);
  4795. return false;
  4796. }
  4797. return true;
  4798. }
  4799. EXPORT_SYMBOL(napi_complete_done);
  4800. /* must be called under rcu_read_lock(), as we dont take a reference */
  4801. static struct napi_struct *napi_by_id(unsigned int napi_id)
  4802. {
  4803. unsigned int hash = napi_id % HASH_SIZE(napi_hash);
  4804. struct napi_struct *napi;
  4805. hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
  4806. if (napi->napi_id == napi_id)
  4807. return napi;
  4808. return NULL;
  4809. }
  4810. #if defined(CONFIG_NET_RX_BUSY_POLL)
  4811. #define BUSY_POLL_BUDGET 8
  4812. static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
  4813. {
  4814. int rc;
  4815. /* Busy polling means there is a high chance device driver hard irq
  4816. * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
  4817. * set in napi_schedule_prep().
  4818. * Since we are about to call napi->poll() once more, we can safely
  4819. * clear NAPI_STATE_MISSED.
  4820. *
  4821. * Note: x86 could use a single "lock and ..." instruction
  4822. * to perform these two clear_bit()
  4823. */
  4824. clear_bit(NAPI_STATE_MISSED, &napi->state);
  4825. clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
  4826. local_bh_disable();
  4827. /* All we really want here is to re-enable device interrupts.
  4828. * Ideally, a new ndo_busy_poll_stop() could avoid another round.
  4829. */
  4830. rc = napi->poll(napi, BUSY_POLL_BUDGET);
  4831. trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
  4832. netpoll_poll_unlock(have_poll_lock);
  4833. if (rc == BUSY_POLL_BUDGET)
  4834. __napi_schedule(napi);
  4835. local_bh_enable();
  4836. }
  4837. void napi_busy_loop(unsigned int napi_id,
  4838. bool (*loop_end)(void *, unsigned long),
  4839. void *loop_end_arg)
  4840. {
  4841. unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
  4842. int (*napi_poll)(struct napi_struct *napi, int budget);
  4843. void *have_poll_lock = NULL;
  4844. struct napi_struct *napi;
  4845. restart:
  4846. napi_poll = NULL;
  4847. rcu_read_lock();
  4848. napi = napi_by_id(napi_id);
  4849. if (!napi)
  4850. goto out;
  4851. preempt_disable();
  4852. for (;;) {
  4853. int work = 0;
  4854. local_bh_disable();
  4855. if (!napi_poll) {
  4856. unsigned long val = READ_ONCE(napi->state);
  4857. /* If multiple threads are competing for this napi,
  4858. * we avoid dirtying napi->state as much as we can.
  4859. */
  4860. if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
  4861. NAPIF_STATE_IN_BUSY_POLL))
  4862. goto count;
  4863. if (cmpxchg(&napi->state, val,
  4864. val | NAPIF_STATE_IN_BUSY_POLL |
  4865. NAPIF_STATE_SCHED) != val)
  4866. goto count;
  4867. have_poll_lock = netpoll_poll_lock(napi);
  4868. napi_poll = napi->poll;
  4869. }
  4870. work = napi_poll(napi, BUSY_POLL_BUDGET);
  4871. trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
  4872. count:
  4873. if (work > 0)
  4874. __NET_ADD_STATS(dev_net(napi->dev),
  4875. LINUX_MIB_BUSYPOLLRXPACKETS, work);
  4876. local_bh_enable();
  4877. if (!loop_end || loop_end(loop_end_arg, start_time))
  4878. break;
  4879. if (unlikely(need_resched())) {
  4880. if (napi_poll)
  4881. busy_poll_stop(napi, have_poll_lock);
  4882. preempt_enable();
  4883. rcu_read_unlock();
  4884. cond_resched();
  4885. if (loop_end(loop_end_arg, start_time))
  4886. return;
  4887. goto restart;
  4888. }
  4889. cpu_relax();
  4890. }
  4891. if (napi_poll)
  4892. busy_poll_stop(napi, have_poll_lock);
  4893. preempt_enable();
  4894. out:
  4895. rcu_read_unlock();
  4896. }
  4897. EXPORT_SYMBOL(napi_busy_loop);
  4898. #endif /* CONFIG_NET_RX_BUSY_POLL */
  4899. static void napi_hash_add(struct napi_struct *napi)
  4900. {
  4901. if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
  4902. test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
  4903. return;
  4904. spin_lock(&napi_hash_lock);
  4905. /* 0..NR_CPUS range is reserved for sender_cpu use */
  4906. do {
  4907. if (unlikely(++napi_gen_id < MIN_NAPI_ID))
  4908. napi_gen_id = MIN_NAPI_ID;
  4909. } while (napi_by_id(napi_gen_id));
  4910. napi->napi_id = napi_gen_id;
  4911. hlist_add_head_rcu(&napi->napi_hash_node,
  4912. &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
  4913. spin_unlock(&napi_hash_lock);
  4914. }
  4915. /* Warning : caller is responsible to make sure rcu grace period
  4916. * is respected before freeing memory containing @napi
  4917. */
  4918. bool napi_hash_del(struct napi_struct *napi)
  4919. {
  4920. bool rcu_sync_needed = false;
  4921. spin_lock(&napi_hash_lock);
  4922. if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
  4923. rcu_sync_needed = true;
  4924. hlist_del_rcu(&napi->napi_hash_node);
  4925. }
  4926. spin_unlock(&napi_hash_lock);
  4927. return rcu_sync_needed;
  4928. }
  4929. EXPORT_SYMBOL_GPL(napi_hash_del);
  4930. static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
  4931. {
  4932. struct napi_struct *napi;
  4933. napi = container_of(timer, struct napi_struct, timer);
  4934. /* Note : we use a relaxed variant of napi_schedule_prep() not setting
  4935. * NAPI_STATE_MISSED, since we do not react to a device IRQ.
  4936. */
  4937. if (napi->gro_count && !napi_disable_pending(napi) &&
  4938. !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
  4939. __napi_schedule_irqoff(napi);
  4940. return HRTIMER_NORESTART;
  4941. }
  4942. void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
  4943. int (*poll)(struct napi_struct *, int), int weight)
  4944. {
  4945. int i;
  4946. INIT_LIST_HEAD(&napi->poll_list);
  4947. hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
  4948. napi->timer.function = napi_watchdog;
  4949. napi->gro_count = 0;
  4950. for (i = 0; i < GRO_HASH_BUCKETS; i++)
  4951. INIT_LIST_HEAD(&napi->gro_hash[i]);
  4952. napi->skb = NULL;
  4953. napi->poll = poll;
  4954. if (weight > NAPI_POLL_WEIGHT)
  4955. pr_err_once("netif_napi_add() called with weight %d on device %s\n",
  4956. weight, dev->name);
  4957. napi->weight = weight;
  4958. list_add(&napi->dev_list, &dev->napi_list);
  4959. napi->dev = dev;
  4960. #ifdef CONFIG_NETPOLL
  4961. napi->poll_owner = -1;
  4962. #endif
  4963. set_bit(NAPI_STATE_SCHED, &napi->state);
  4964. napi_hash_add(napi);
  4965. }
  4966. EXPORT_SYMBOL(netif_napi_add);
  4967. void napi_disable(struct napi_struct *n)
  4968. {
  4969. might_sleep();
  4970. set_bit(NAPI_STATE_DISABLE, &n->state);
  4971. while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
  4972. msleep(1);
  4973. while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
  4974. msleep(1);
  4975. hrtimer_cancel(&n->timer);
  4976. clear_bit(NAPI_STATE_DISABLE, &n->state);
  4977. }
  4978. EXPORT_SYMBOL(napi_disable);
  4979. static void flush_gro_hash(struct napi_struct *napi)
  4980. {
  4981. int i;
  4982. for (i = 0; i < GRO_HASH_BUCKETS; i++) {
  4983. struct sk_buff *skb, *n;
  4984. list_for_each_entry_safe(skb, n, &napi->gro_hash[i], list)
  4985. kfree_skb(skb);
  4986. }
  4987. }
  4988. /* Must be called in process context */
  4989. void netif_napi_del(struct napi_struct *napi)
  4990. {
  4991. might_sleep();
  4992. if (napi_hash_del(napi))
  4993. synchronize_net();
  4994. list_del_init(&napi->dev_list);
  4995. napi_free_frags(napi);
  4996. flush_gro_hash(napi);
  4997. napi->gro_count = 0;
  4998. }
  4999. EXPORT_SYMBOL(netif_napi_del);
  5000. static int napi_poll(struct napi_struct *n, struct list_head *repoll)
  5001. {
  5002. void *have;
  5003. int work, weight;
  5004. list_del_init(&n->poll_list);
  5005. have = netpoll_poll_lock(n);
  5006. weight = n->weight;
  5007. /* This NAPI_STATE_SCHED test is for avoiding a race
  5008. * with netpoll's poll_napi(). Only the entity which
  5009. * obtains the lock and sees NAPI_STATE_SCHED set will
  5010. * actually make the ->poll() call. Therefore we avoid
  5011. * accidentally calling ->poll() when NAPI is not scheduled.
  5012. */
  5013. work = 0;
  5014. if (test_bit(NAPI_STATE_SCHED, &n->state)) {
  5015. work = n->poll(n, weight);
  5016. trace_napi_poll(n, work, weight);
  5017. }
  5018. WARN_ON_ONCE(work > weight);
  5019. if (likely(work < weight))
  5020. goto out_unlock;
  5021. /* Drivers must not modify the NAPI state if they
  5022. * consume the entire weight. In such cases this code
  5023. * still "owns" the NAPI instance and therefore can
  5024. * move the instance around on the list at-will.
  5025. */
  5026. if (unlikely(napi_disable_pending(n))) {
  5027. napi_complete(n);
  5028. goto out_unlock;
  5029. }
  5030. if (n->gro_count) {
  5031. /* flush too old packets
  5032. * If HZ < 1000, flush all packets.
  5033. */
  5034. napi_gro_flush(n, HZ >= 1000);
  5035. }
  5036. /* Some drivers may have called napi_schedule
  5037. * prior to exhausting their budget.
  5038. */
  5039. if (unlikely(!list_empty(&n->poll_list))) {
  5040. pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
  5041. n->dev ? n->dev->name : "backlog");
  5042. goto out_unlock;
  5043. }
  5044. list_add_tail(&n->poll_list, repoll);
  5045. out_unlock:
  5046. netpoll_poll_unlock(have);
  5047. return work;
  5048. }
  5049. static __latent_entropy void net_rx_action(struct softirq_action *h)
  5050. {
  5051. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  5052. unsigned long time_limit = jiffies +
  5053. usecs_to_jiffies(netdev_budget_usecs);
  5054. int budget = netdev_budget;
  5055. LIST_HEAD(list);
  5056. LIST_HEAD(repoll);
  5057. local_irq_disable();
  5058. list_splice_init(&sd->poll_list, &list);
  5059. local_irq_enable();
  5060. for (;;) {
  5061. struct napi_struct *n;
  5062. if (list_empty(&list)) {
  5063. if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
  5064. goto out;
  5065. break;
  5066. }
  5067. n = list_first_entry(&list, struct napi_struct, poll_list);
  5068. budget -= napi_poll(n, &repoll);
  5069. /* If softirq window is exhausted then punt.
  5070. * Allow this to run for 2 jiffies since which will allow
  5071. * an average latency of 1.5/HZ.
  5072. */
  5073. if (unlikely(budget <= 0 ||
  5074. time_after_eq(jiffies, time_limit))) {
  5075. sd->time_squeeze++;
  5076. break;
  5077. }
  5078. }
  5079. local_irq_disable();
  5080. list_splice_tail_init(&sd->poll_list, &list);
  5081. list_splice_tail(&repoll, &list);
  5082. list_splice(&list, &sd->poll_list);
  5083. if (!list_empty(&sd->poll_list))
  5084. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  5085. net_rps_action_and_irq_enable(sd);
  5086. out:
  5087. __kfree_skb_flush();
  5088. }
  5089. struct netdev_adjacent {
  5090. struct net_device *dev;
  5091. /* upper master flag, there can only be one master device per list */
  5092. bool master;
  5093. /* counter for the number of times this device was added to us */
  5094. u16 ref_nr;
  5095. /* private field for the users */
  5096. void *private;
  5097. struct list_head list;
  5098. struct rcu_head rcu;
  5099. };
  5100. static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
  5101. struct list_head *adj_list)
  5102. {
  5103. struct netdev_adjacent *adj;
  5104. list_for_each_entry(adj, adj_list, list) {
  5105. if (adj->dev == adj_dev)
  5106. return adj;
  5107. }
  5108. return NULL;
  5109. }
  5110. static int __netdev_has_upper_dev(struct net_device *upper_dev, void *data)
  5111. {
  5112. struct net_device *dev = data;
  5113. return upper_dev == dev;
  5114. }
  5115. /**
  5116. * netdev_has_upper_dev - Check if device is linked to an upper device
  5117. * @dev: device
  5118. * @upper_dev: upper device to check
  5119. *
  5120. * Find out if a device is linked to specified upper device and return true
  5121. * in case it is. Note that this checks only immediate upper device,
  5122. * not through a complete stack of devices. The caller must hold the RTNL lock.
  5123. */
  5124. bool netdev_has_upper_dev(struct net_device *dev,
  5125. struct net_device *upper_dev)
  5126. {
  5127. ASSERT_RTNL();
  5128. return netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
  5129. upper_dev);
  5130. }
  5131. EXPORT_SYMBOL(netdev_has_upper_dev);
  5132. /**
  5133. * netdev_has_upper_dev_all - Check if device is linked to an upper device
  5134. * @dev: device
  5135. * @upper_dev: upper device to check
  5136. *
  5137. * Find out if a device is linked to specified upper device and return true
  5138. * in case it is. Note that this checks the entire upper device chain.
  5139. * The caller must hold rcu lock.
  5140. */
  5141. bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
  5142. struct net_device *upper_dev)
  5143. {
  5144. return !!netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
  5145. upper_dev);
  5146. }
  5147. EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
  5148. /**
  5149. * netdev_has_any_upper_dev - Check if device is linked to some device
  5150. * @dev: device
  5151. *
  5152. * Find out if a device is linked to an upper device and return true in case
  5153. * it is. The caller must hold the RTNL lock.
  5154. */
  5155. bool netdev_has_any_upper_dev(struct net_device *dev)
  5156. {
  5157. ASSERT_RTNL();
  5158. return !list_empty(&dev->adj_list.upper);
  5159. }
  5160. EXPORT_SYMBOL(netdev_has_any_upper_dev);
  5161. /**
  5162. * netdev_master_upper_dev_get - Get master upper device
  5163. * @dev: device
  5164. *
  5165. * Find a master upper device and return pointer to it or NULL in case
  5166. * it's not there. The caller must hold the RTNL lock.
  5167. */
  5168. struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
  5169. {
  5170. struct netdev_adjacent *upper;
  5171. ASSERT_RTNL();
  5172. if (list_empty(&dev->adj_list.upper))
  5173. return NULL;
  5174. upper = list_first_entry(&dev->adj_list.upper,
  5175. struct netdev_adjacent, list);
  5176. if (likely(upper->master))
  5177. return upper->dev;
  5178. return NULL;
  5179. }
  5180. EXPORT_SYMBOL(netdev_master_upper_dev_get);
  5181. /**
  5182. * netdev_has_any_lower_dev - Check if device is linked to some device
  5183. * @dev: device
  5184. *
  5185. * Find out if a device is linked to a lower device and return true in case
  5186. * it is. The caller must hold the RTNL lock.
  5187. */
  5188. static bool netdev_has_any_lower_dev(struct net_device *dev)
  5189. {
  5190. ASSERT_RTNL();
  5191. return !list_empty(&dev->adj_list.lower);
  5192. }
  5193. void *netdev_adjacent_get_private(struct list_head *adj_list)
  5194. {
  5195. struct netdev_adjacent *adj;
  5196. adj = list_entry(adj_list, struct netdev_adjacent, list);
  5197. return adj->private;
  5198. }
  5199. EXPORT_SYMBOL(netdev_adjacent_get_private);
  5200. /**
  5201. * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
  5202. * @dev: device
  5203. * @iter: list_head ** of the current position
  5204. *
  5205. * Gets the next device from the dev's upper list, starting from iter
  5206. * position. The caller must hold RCU read lock.
  5207. */
  5208. struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
  5209. struct list_head **iter)
  5210. {
  5211. struct netdev_adjacent *upper;
  5212. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
  5213. upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  5214. if (&upper->list == &dev->adj_list.upper)
  5215. return NULL;
  5216. *iter = &upper->list;
  5217. return upper->dev;
  5218. }
  5219. EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
  5220. static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
  5221. struct list_head **iter)
  5222. {
  5223. struct netdev_adjacent *upper;
  5224. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
  5225. upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  5226. if (&upper->list == &dev->adj_list.upper)
  5227. return NULL;
  5228. *iter = &upper->list;
  5229. return upper->dev;
  5230. }
  5231. int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
  5232. int (*fn)(struct net_device *dev,
  5233. void *data),
  5234. void *data)
  5235. {
  5236. struct net_device *udev;
  5237. struct list_head *iter;
  5238. int ret;
  5239. for (iter = &dev->adj_list.upper,
  5240. udev = netdev_next_upper_dev_rcu(dev, &iter);
  5241. udev;
  5242. udev = netdev_next_upper_dev_rcu(dev, &iter)) {
  5243. /* first is the upper device itself */
  5244. ret = fn(udev, data);
  5245. if (ret)
  5246. return ret;
  5247. /* then look at all of its upper devices */
  5248. ret = netdev_walk_all_upper_dev_rcu(udev, fn, data);
  5249. if (ret)
  5250. return ret;
  5251. }
  5252. return 0;
  5253. }
  5254. EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
  5255. /**
  5256. * netdev_lower_get_next_private - Get the next ->private from the
  5257. * lower neighbour list
  5258. * @dev: device
  5259. * @iter: list_head ** of the current position
  5260. *
  5261. * Gets the next netdev_adjacent->private from the dev's lower neighbour
  5262. * list, starting from iter position. The caller must hold either hold the
  5263. * RTNL lock or its own locking that guarantees that the neighbour lower
  5264. * list will remain unchanged.
  5265. */
  5266. void *netdev_lower_get_next_private(struct net_device *dev,
  5267. struct list_head **iter)
  5268. {
  5269. struct netdev_adjacent *lower;
  5270. lower = list_entry(*iter, struct netdev_adjacent, list);
  5271. if (&lower->list == &dev->adj_list.lower)
  5272. return NULL;
  5273. *iter = lower->list.next;
  5274. return lower->private;
  5275. }
  5276. EXPORT_SYMBOL(netdev_lower_get_next_private);
  5277. /**
  5278. * netdev_lower_get_next_private_rcu - Get the next ->private from the
  5279. * lower neighbour list, RCU
  5280. * variant
  5281. * @dev: device
  5282. * @iter: list_head ** of the current position
  5283. *
  5284. * Gets the next netdev_adjacent->private from the dev's lower neighbour
  5285. * list, starting from iter position. The caller must hold RCU read lock.
  5286. */
  5287. void *netdev_lower_get_next_private_rcu(struct net_device *dev,
  5288. struct list_head **iter)
  5289. {
  5290. struct netdev_adjacent *lower;
  5291. WARN_ON_ONCE(!rcu_read_lock_held());
  5292. lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  5293. if (&lower->list == &dev->adj_list.lower)
  5294. return NULL;
  5295. *iter = &lower->list;
  5296. return lower->private;
  5297. }
  5298. EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
  5299. /**
  5300. * netdev_lower_get_next - Get the next device from the lower neighbour
  5301. * list
  5302. * @dev: device
  5303. * @iter: list_head ** of the current position
  5304. *
  5305. * Gets the next netdev_adjacent from the dev's lower neighbour
  5306. * list, starting from iter position. The caller must hold RTNL lock or
  5307. * its own locking that guarantees that the neighbour lower
  5308. * list will remain unchanged.
  5309. */
  5310. void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
  5311. {
  5312. struct netdev_adjacent *lower;
  5313. lower = list_entry(*iter, struct netdev_adjacent, list);
  5314. if (&lower->list == &dev->adj_list.lower)
  5315. return NULL;
  5316. *iter = lower->list.next;
  5317. return lower->dev;
  5318. }
  5319. EXPORT_SYMBOL(netdev_lower_get_next);
  5320. static struct net_device *netdev_next_lower_dev(struct net_device *dev,
  5321. struct list_head **iter)
  5322. {
  5323. struct netdev_adjacent *lower;
  5324. lower = list_entry((*iter)->next, struct netdev_adjacent, list);
  5325. if (&lower->list == &dev->adj_list.lower)
  5326. return NULL;
  5327. *iter = &lower->list;
  5328. return lower->dev;
  5329. }
  5330. int netdev_walk_all_lower_dev(struct net_device *dev,
  5331. int (*fn)(struct net_device *dev,
  5332. void *data),
  5333. void *data)
  5334. {
  5335. struct net_device *ldev;
  5336. struct list_head *iter;
  5337. int ret;
  5338. for (iter = &dev->adj_list.lower,
  5339. ldev = netdev_next_lower_dev(dev, &iter);
  5340. ldev;
  5341. ldev = netdev_next_lower_dev(dev, &iter)) {
  5342. /* first is the lower device itself */
  5343. ret = fn(ldev, data);
  5344. if (ret)
  5345. return ret;
  5346. /* then look at all of its lower devices */
  5347. ret = netdev_walk_all_lower_dev(ldev, fn, data);
  5348. if (ret)
  5349. return ret;
  5350. }
  5351. return 0;
  5352. }
  5353. EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
  5354. static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
  5355. struct list_head **iter)
  5356. {
  5357. struct netdev_adjacent *lower;
  5358. lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  5359. if (&lower->list == &dev->adj_list.lower)
  5360. return NULL;
  5361. *iter = &lower->list;
  5362. return lower->dev;
  5363. }
  5364. int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
  5365. int (*fn)(struct net_device *dev,
  5366. void *data),
  5367. void *data)
  5368. {
  5369. struct net_device *ldev;
  5370. struct list_head *iter;
  5371. int ret;
  5372. for (iter = &dev->adj_list.lower,
  5373. ldev = netdev_next_lower_dev_rcu(dev, &iter);
  5374. ldev;
  5375. ldev = netdev_next_lower_dev_rcu(dev, &iter)) {
  5376. /* first is the lower device itself */
  5377. ret = fn(ldev, data);
  5378. if (ret)
  5379. return ret;
  5380. /* then look at all of its lower devices */
  5381. ret = netdev_walk_all_lower_dev_rcu(ldev, fn, data);
  5382. if (ret)
  5383. return ret;
  5384. }
  5385. return 0;
  5386. }
  5387. EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
  5388. /**
  5389. * netdev_lower_get_first_private_rcu - Get the first ->private from the
  5390. * lower neighbour list, RCU
  5391. * variant
  5392. * @dev: device
  5393. *
  5394. * Gets the first netdev_adjacent->private from the dev's lower neighbour
  5395. * list. The caller must hold RCU read lock.
  5396. */
  5397. void *netdev_lower_get_first_private_rcu(struct net_device *dev)
  5398. {
  5399. struct netdev_adjacent *lower;
  5400. lower = list_first_or_null_rcu(&dev->adj_list.lower,
  5401. struct netdev_adjacent, list);
  5402. if (lower)
  5403. return lower->private;
  5404. return NULL;
  5405. }
  5406. EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
  5407. /**
  5408. * netdev_master_upper_dev_get_rcu - Get master upper device
  5409. * @dev: device
  5410. *
  5411. * Find a master upper device and return pointer to it or NULL in case
  5412. * it's not there. The caller must hold the RCU read lock.
  5413. */
  5414. struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
  5415. {
  5416. struct netdev_adjacent *upper;
  5417. upper = list_first_or_null_rcu(&dev->adj_list.upper,
  5418. struct netdev_adjacent, list);
  5419. if (upper && likely(upper->master))
  5420. return upper->dev;
  5421. return NULL;
  5422. }
  5423. EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
  5424. static int netdev_adjacent_sysfs_add(struct net_device *dev,
  5425. struct net_device *adj_dev,
  5426. struct list_head *dev_list)
  5427. {
  5428. char linkname[IFNAMSIZ+7];
  5429. sprintf(linkname, dev_list == &dev->adj_list.upper ?
  5430. "upper_%s" : "lower_%s", adj_dev->name);
  5431. return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
  5432. linkname);
  5433. }
  5434. static void netdev_adjacent_sysfs_del(struct net_device *dev,
  5435. char *name,
  5436. struct list_head *dev_list)
  5437. {
  5438. char linkname[IFNAMSIZ+7];
  5439. sprintf(linkname, dev_list == &dev->adj_list.upper ?
  5440. "upper_%s" : "lower_%s", name);
  5441. sysfs_remove_link(&(dev->dev.kobj), linkname);
  5442. }
  5443. static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
  5444. struct net_device *adj_dev,
  5445. struct list_head *dev_list)
  5446. {
  5447. return (dev_list == &dev->adj_list.upper ||
  5448. dev_list == &dev->adj_list.lower) &&
  5449. net_eq(dev_net(dev), dev_net(adj_dev));
  5450. }
  5451. static int __netdev_adjacent_dev_insert(struct net_device *dev,
  5452. struct net_device *adj_dev,
  5453. struct list_head *dev_list,
  5454. void *private, bool master)
  5455. {
  5456. struct netdev_adjacent *adj;
  5457. int ret;
  5458. adj = __netdev_find_adj(adj_dev, dev_list);
  5459. if (adj) {
  5460. adj->ref_nr += 1;
  5461. pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
  5462. dev->name, adj_dev->name, adj->ref_nr);
  5463. return 0;
  5464. }
  5465. adj = kmalloc(sizeof(*adj), GFP_KERNEL);
  5466. if (!adj)
  5467. return -ENOMEM;
  5468. adj->dev = adj_dev;
  5469. adj->master = master;
  5470. adj->ref_nr = 1;
  5471. adj->private = private;
  5472. dev_hold(adj_dev);
  5473. pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
  5474. dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
  5475. if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
  5476. ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
  5477. if (ret)
  5478. goto free_adj;
  5479. }
  5480. /* Ensure that master link is always the first item in list. */
  5481. if (master) {
  5482. ret = sysfs_create_link(&(dev->dev.kobj),
  5483. &(adj_dev->dev.kobj), "master");
  5484. if (ret)
  5485. goto remove_symlinks;
  5486. list_add_rcu(&adj->list, dev_list);
  5487. } else {
  5488. list_add_tail_rcu(&adj->list, dev_list);
  5489. }
  5490. return 0;
  5491. remove_symlinks:
  5492. if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
  5493. netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
  5494. free_adj:
  5495. kfree(adj);
  5496. dev_put(adj_dev);
  5497. return ret;
  5498. }
  5499. static void __netdev_adjacent_dev_remove(struct net_device *dev,
  5500. struct net_device *adj_dev,
  5501. u16 ref_nr,
  5502. struct list_head *dev_list)
  5503. {
  5504. struct netdev_adjacent *adj;
  5505. pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
  5506. dev->name, adj_dev->name, ref_nr);
  5507. adj = __netdev_find_adj(adj_dev, dev_list);
  5508. if (!adj) {
  5509. pr_err("Adjacency does not exist for device %s from %s\n",
  5510. dev->name, adj_dev->name);
  5511. WARN_ON(1);
  5512. return;
  5513. }
  5514. if (adj->ref_nr > ref_nr) {
  5515. pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
  5516. dev->name, adj_dev->name, ref_nr,
  5517. adj->ref_nr - ref_nr);
  5518. adj->ref_nr -= ref_nr;
  5519. return;
  5520. }
  5521. if (adj->master)
  5522. sysfs_remove_link(&(dev->dev.kobj), "master");
  5523. if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
  5524. netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
  5525. list_del_rcu(&adj->list);
  5526. pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
  5527. adj_dev->name, dev->name, adj_dev->name);
  5528. dev_put(adj_dev);
  5529. kfree_rcu(adj, rcu);
  5530. }
  5531. static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
  5532. struct net_device *upper_dev,
  5533. struct list_head *up_list,
  5534. struct list_head *down_list,
  5535. void *private, bool master)
  5536. {
  5537. int ret;
  5538. ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
  5539. private, master);
  5540. if (ret)
  5541. return ret;
  5542. ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
  5543. private, false);
  5544. if (ret) {
  5545. __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
  5546. return ret;
  5547. }
  5548. return 0;
  5549. }
  5550. static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
  5551. struct net_device *upper_dev,
  5552. u16 ref_nr,
  5553. struct list_head *up_list,
  5554. struct list_head *down_list)
  5555. {
  5556. __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
  5557. __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
  5558. }
  5559. static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
  5560. struct net_device *upper_dev,
  5561. void *private, bool master)
  5562. {
  5563. return __netdev_adjacent_dev_link_lists(dev, upper_dev,
  5564. &dev->adj_list.upper,
  5565. &upper_dev->adj_list.lower,
  5566. private, master);
  5567. }
  5568. static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
  5569. struct net_device *upper_dev)
  5570. {
  5571. __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
  5572. &dev->adj_list.upper,
  5573. &upper_dev->adj_list.lower);
  5574. }
  5575. static int __netdev_upper_dev_link(struct net_device *dev,
  5576. struct net_device *upper_dev, bool master,
  5577. void *upper_priv, void *upper_info,
  5578. struct netlink_ext_ack *extack)
  5579. {
  5580. struct netdev_notifier_changeupper_info changeupper_info = {
  5581. .info = {
  5582. .dev = dev,
  5583. .extack = extack,
  5584. },
  5585. .upper_dev = upper_dev,
  5586. .master = master,
  5587. .linking = true,
  5588. .upper_info = upper_info,
  5589. };
  5590. struct net_device *master_dev;
  5591. int ret = 0;
  5592. ASSERT_RTNL();
  5593. if (dev == upper_dev)
  5594. return -EBUSY;
  5595. /* To prevent loops, check if dev is not upper device to upper_dev. */
  5596. if (netdev_has_upper_dev(upper_dev, dev))
  5597. return -EBUSY;
  5598. if (!master) {
  5599. if (netdev_has_upper_dev(dev, upper_dev))
  5600. return -EEXIST;
  5601. } else {
  5602. master_dev = netdev_master_upper_dev_get(dev);
  5603. if (master_dev)
  5604. return master_dev == upper_dev ? -EEXIST : -EBUSY;
  5605. }
  5606. ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
  5607. &changeupper_info.info);
  5608. ret = notifier_to_errno(ret);
  5609. if (ret)
  5610. return ret;
  5611. ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
  5612. master);
  5613. if (ret)
  5614. return ret;
  5615. ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
  5616. &changeupper_info.info);
  5617. ret = notifier_to_errno(ret);
  5618. if (ret)
  5619. goto rollback;
  5620. return 0;
  5621. rollback:
  5622. __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
  5623. return ret;
  5624. }
  5625. /**
  5626. * netdev_upper_dev_link - Add a link to the upper device
  5627. * @dev: device
  5628. * @upper_dev: new upper device
  5629. * @extack: netlink extended ack
  5630. *
  5631. * Adds a link to device which is upper to this one. The caller must hold
  5632. * the RTNL lock. On a failure a negative errno code is returned.
  5633. * On success the reference counts are adjusted and the function
  5634. * returns zero.
  5635. */
  5636. int netdev_upper_dev_link(struct net_device *dev,
  5637. struct net_device *upper_dev,
  5638. struct netlink_ext_ack *extack)
  5639. {
  5640. return __netdev_upper_dev_link(dev, upper_dev, false,
  5641. NULL, NULL, extack);
  5642. }
  5643. EXPORT_SYMBOL(netdev_upper_dev_link);
  5644. /**
  5645. * netdev_master_upper_dev_link - Add a master link to the upper device
  5646. * @dev: device
  5647. * @upper_dev: new upper device
  5648. * @upper_priv: upper device private
  5649. * @upper_info: upper info to be passed down via notifier
  5650. * @extack: netlink extended ack
  5651. *
  5652. * Adds a link to device which is upper to this one. In this case, only
  5653. * one master upper device can be linked, although other non-master devices
  5654. * might be linked as well. The caller must hold the RTNL lock.
  5655. * On a failure a negative errno code is returned. On success the reference
  5656. * counts are adjusted and the function returns zero.
  5657. */
  5658. int netdev_master_upper_dev_link(struct net_device *dev,
  5659. struct net_device *upper_dev,
  5660. void *upper_priv, void *upper_info,
  5661. struct netlink_ext_ack *extack)
  5662. {
  5663. return __netdev_upper_dev_link(dev, upper_dev, true,
  5664. upper_priv, upper_info, extack);
  5665. }
  5666. EXPORT_SYMBOL(netdev_master_upper_dev_link);
  5667. /**
  5668. * netdev_upper_dev_unlink - Removes a link to upper device
  5669. * @dev: device
  5670. * @upper_dev: new upper device
  5671. *
  5672. * Removes a link to device which is upper to this one. The caller must hold
  5673. * the RTNL lock.
  5674. */
  5675. void netdev_upper_dev_unlink(struct net_device *dev,
  5676. struct net_device *upper_dev)
  5677. {
  5678. struct netdev_notifier_changeupper_info changeupper_info = {
  5679. .info = {
  5680. .dev = dev,
  5681. },
  5682. .upper_dev = upper_dev,
  5683. .linking = false,
  5684. };
  5685. ASSERT_RTNL();
  5686. changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
  5687. call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
  5688. &changeupper_info.info);
  5689. __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
  5690. call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
  5691. &changeupper_info.info);
  5692. }
  5693. EXPORT_SYMBOL(netdev_upper_dev_unlink);
  5694. /**
  5695. * netdev_bonding_info_change - Dispatch event about slave change
  5696. * @dev: device
  5697. * @bonding_info: info to dispatch
  5698. *
  5699. * Send NETDEV_BONDING_INFO to netdev notifiers with info.
  5700. * The caller must hold the RTNL lock.
  5701. */
  5702. void netdev_bonding_info_change(struct net_device *dev,
  5703. struct netdev_bonding_info *bonding_info)
  5704. {
  5705. struct netdev_notifier_bonding_info info = {
  5706. .info.dev = dev,
  5707. };
  5708. memcpy(&info.bonding_info, bonding_info,
  5709. sizeof(struct netdev_bonding_info));
  5710. call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
  5711. &info.info);
  5712. }
  5713. EXPORT_SYMBOL(netdev_bonding_info_change);
  5714. static void netdev_adjacent_add_links(struct net_device *dev)
  5715. {
  5716. struct netdev_adjacent *iter;
  5717. struct net *net = dev_net(dev);
  5718. list_for_each_entry(iter, &dev->adj_list.upper, list) {
  5719. if (!net_eq(net, dev_net(iter->dev)))
  5720. continue;
  5721. netdev_adjacent_sysfs_add(iter->dev, dev,
  5722. &iter->dev->adj_list.lower);
  5723. netdev_adjacent_sysfs_add(dev, iter->dev,
  5724. &dev->adj_list.upper);
  5725. }
  5726. list_for_each_entry(iter, &dev->adj_list.lower, list) {
  5727. if (!net_eq(net, dev_net(iter->dev)))
  5728. continue;
  5729. netdev_adjacent_sysfs_add(iter->dev, dev,
  5730. &iter->dev->adj_list.upper);
  5731. netdev_adjacent_sysfs_add(dev, iter->dev,
  5732. &dev->adj_list.lower);
  5733. }
  5734. }
  5735. static void netdev_adjacent_del_links(struct net_device *dev)
  5736. {
  5737. struct netdev_adjacent *iter;
  5738. struct net *net = dev_net(dev);
  5739. list_for_each_entry(iter, &dev->adj_list.upper, list) {
  5740. if (!net_eq(net, dev_net(iter->dev)))
  5741. continue;
  5742. netdev_adjacent_sysfs_del(iter->dev, dev->name,
  5743. &iter->dev->adj_list.lower);
  5744. netdev_adjacent_sysfs_del(dev, iter->dev->name,
  5745. &dev->adj_list.upper);
  5746. }
  5747. list_for_each_entry(iter, &dev->adj_list.lower, list) {
  5748. if (!net_eq(net, dev_net(iter->dev)))
  5749. continue;
  5750. netdev_adjacent_sysfs_del(iter->dev, dev->name,
  5751. &iter->dev->adj_list.upper);
  5752. netdev_adjacent_sysfs_del(dev, iter->dev->name,
  5753. &dev->adj_list.lower);
  5754. }
  5755. }
  5756. void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
  5757. {
  5758. struct netdev_adjacent *iter;
  5759. struct net *net = dev_net(dev);
  5760. list_for_each_entry(iter, &dev->adj_list.upper, list) {
  5761. if (!net_eq(net, dev_net(iter->dev)))
  5762. continue;
  5763. netdev_adjacent_sysfs_del(iter->dev, oldname,
  5764. &iter->dev->adj_list.lower);
  5765. netdev_adjacent_sysfs_add(iter->dev, dev,
  5766. &iter->dev->adj_list.lower);
  5767. }
  5768. list_for_each_entry(iter, &dev->adj_list.lower, list) {
  5769. if (!net_eq(net, dev_net(iter->dev)))
  5770. continue;
  5771. netdev_adjacent_sysfs_del(iter->dev, oldname,
  5772. &iter->dev->adj_list.upper);
  5773. netdev_adjacent_sysfs_add(iter->dev, dev,
  5774. &iter->dev->adj_list.upper);
  5775. }
  5776. }
  5777. void *netdev_lower_dev_get_private(struct net_device *dev,
  5778. struct net_device *lower_dev)
  5779. {
  5780. struct netdev_adjacent *lower;
  5781. if (!lower_dev)
  5782. return NULL;
  5783. lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
  5784. if (!lower)
  5785. return NULL;
  5786. return lower->private;
  5787. }
  5788. EXPORT_SYMBOL(netdev_lower_dev_get_private);
  5789. int dev_get_nest_level(struct net_device *dev)
  5790. {
  5791. struct net_device *lower = NULL;
  5792. struct list_head *iter;
  5793. int max_nest = -1;
  5794. int nest;
  5795. ASSERT_RTNL();
  5796. netdev_for_each_lower_dev(dev, lower, iter) {
  5797. nest = dev_get_nest_level(lower);
  5798. if (max_nest < nest)
  5799. max_nest = nest;
  5800. }
  5801. return max_nest + 1;
  5802. }
  5803. EXPORT_SYMBOL(dev_get_nest_level);
  5804. /**
  5805. * netdev_lower_change - Dispatch event about lower device state change
  5806. * @lower_dev: device
  5807. * @lower_state_info: state to dispatch
  5808. *
  5809. * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
  5810. * The caller must hold the RTNL lock.
  5811. */
  5812. void netdev_lower_state_changed(struct net_device *lower_dev,
  5813. void *lower_state_info)
  5814. {
  5815. struct netdev_notifier_changelowerstate_info changelowerstate_info = {
  5816. .info.dev = lower_dev,
  5817. };
  5818. ASSERT_RTNL();
  5819. changelowerstate_info.lower_state_info = lower_state_info;
  5820. call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
  5821. &changelowerstate_info.info);
  5822. }
  5823. EXPORT_SYMBOL(netdev_lower_state_changed);
  5824. static void dev_change_rx_flags(struct net_device *dev, int flags)
  5825. {
  5826. const struct net_device_ops *ops = dev->netdev_ops;
  5827. if (ops->ndo_change_rx_flags)
  5828. ops->ndo_change_rx_flags(dev, flags);
  5829. }
  5830. static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
  5831. {
  5832. unsigned int old_flags = dev->flags;
  5833. kuid_t uid;
  5834. kgid_t gid;
  5835. ASSERT_RTNL();
  5836. dev->flags |= IFF_PROMISC;
  5837. dev->promiscuity += inc;
  5838. if (dev->promiscuity == 0) {
  5839. /*
  5840. * Avoid overflow.
  5841. * If inc causes overflow, untouch promisc and return error.
  5842. */
  5843. if (inc < 0)
  5844. dev->flags &= ~IFF_PROMISC;
  5845. else {
  5846. dev->promiscuity -= inc;
  5847. pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
  5848. dev->name);
  5849. return -EOVERFLOW;
  5850. }
  5851. }
  5852. if (dev->flags != old_flags) {
  5853. pr_info("device %s %s promiscuous mode\n",
  5854. dev->name,
  5855. dev->flags & IFF_PROMISC ? "entered" : "left");
  5856. if (audit_enabled) {
  5857. current_uid_gid(&uid, &gid);
  5858. audit_log(audit_context(), GFP_ATOMIC,
  5859. AUDIT_ANOM_PROMISCUOUS,
  5860. "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
  5861. dev->name, (dev->flags & IFF_PROMISC),
  5862. (old_flags & IFF_PROMISC),
  5863. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  5864. from_kuid(&init_user_ns, uid),
  5865. from_kgid(&init_user_ns, gid),
  5866. audit_get_sessionid(current));
  5867. }
  5868. dev_change_rx_flags(dev, IFF_PROMISC);
  5869. }
  5870. if (notify)
  5871. __dev_notify_flags(dev, old_flags, IFF_PROMISC);
  5872. return 0;
  5873. }
  5874. /**
  5875. * dev_set_promiscuity - update promiscuity count on a device
  5876. * @dev: device
  5877. * @inc: modifier
  5878. *
  5879. * Add or remove promiscuity from a device. While the count in the device
  5880. * remains above zero the interface remains promiscuous. Once it hits zero
  5881. * the device reverts back to normal filtering operation. A negative inc
  5882. * value is used to drop promiscuity on the device.
  5883. * Return 0 if successful or a negative errno code on error.
  5884. */
  5885. int dev_set_promiscuity(struct net_device *dev, int inc)
  5886. {
  5887. unsigned int old_flags = dev->flags;
  5888. int err;
  5889. err = __dev_set_promiscuity(dev, inc, true);
  5890. if (err < 0)
  5891. return err;
  5892. if (dev->flags != old_flags)
  5893. dev_set_rx_mode(dev);
  5894. return err;
  5895. }
  5896. EXPORT_SYMBOL(dev_set_promiscuity);
  5897. static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
  5898. {
  5899. unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
  5900. ASSERT_RTNL();
  5901. dev->flags |= IFF_ALLMULTI;
  5902. dev->allmulti += inc;
  5903. if (dev->allmulti == 0) {
  5904. /*
  5905. * Avoid overflow.
  5906. * If inc causes overflow, untouch allmulti and return error.
  5907. */
  5908. if (inc < 0)
  5909. dev->flags &= ~IFF_ALLMULTI;
  5910. else {
  5911. dev->allmulti -= inc;
  5912. pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
  5913. dev->name);
  5914. return -EOVERFLOW;
  5915. }
  5916. }
  5917. if (dev->flags ^ old_flags) {
  5918. dev_change_rx_flags(dev, IFF_ALLMULTI);
  5919. dev_set_rx_mode(dev);
  5920. if (notify)
  5921. __dev_notify_flags(dev, old_flags,
  5922. dev->gflags ^ old_gflags);
  5923. }
  5924. return 0;
  5925. }
  5926. /**
  5927. * dev_set_allmulti - update allmulti count on a device
  5928. * @dev: device
  5929. * @inc: modifier
  5930. *
  5931. * Add or remove reception of all multicast frames to a device. While the
  5932. * count in the device remains above zero the interface remains listening
  5933. * to all interfaces. Once it hits zero the device reverts back to normal
  5934. * filtering operation. A negative @inc value is used to drop the counter
  5935. * when releasing a resource needing all multicasts.
  5936. * Return 0 if successful or a negative errno code on error.
  5937. */
  5938. int dev_set_allmulti(struct net_device *dev, int inc)
  5939. {
  5940. return __dev_set_allmulti(dev, inc, true);
  5941. }
  5942. EXPORT_SYMBOL(dev_set_allmulti);
  5943. /*
  5944. * Upload unicast and multicast address lists to device and
  5945. * configure RX filtering. When the device doesn't support unicast
  5946. * filtering it is put in promiscuous mode while unicast addresses
  5947. * are present.
  5948. */
  5949. void __dev_set_rx_mode(struct net_device *dev)
  5950. {
  5951. const struct net_device_ops *ops = dev->netdev_ops;
  5952. /* dev_open will call this function so the list will stay sane. */
  5953. if (!(dev->flags&IFF_UP))
  5954. return;
  5955. if (!netif_device_present(dev))
  5956. return;
  5957. if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
  5958. /* Unicast addresses changes may only happen under the rtnl,
  5959. * therefore calling __dev_set_promiscuity here is safe.
  5960. */
  5961. if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
  5962. __dev_set_promiscuity(dev, 1, false);
  5963. dev->uc_promisc = true;
  5964. } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
  5965. __dev_set_promiscuity(dev, -1, false);
  5966. dev->uc_promisc = false;
  5967. }
  5968. }
  5969. if (ops->ndo_set_rx_mode)
  5970. ops->ndo_set_rx_mode(dev);
  5971. }
  5972. void dev_set_rx_mode(struct net_device *dev)
  5973. {
  5974. netif_addr_lock_bh(dev);
  5975. __dev_set_rx_mode(dev);
  5976. netif_addr_unlock_bh(dev);
  5977. }
  5978. /**
  5979. * dev_get_flags - get flags reported to userspace
  5980. * @dev: device
  5981. *
  5982. * Get the combination of flag bits exported through APIs to userspace.
  5983. */
  5984. unsigned int dev_get_flags(const struct net_device *dev)
  5985. {
  5986. unsigned int flags;
  5987. flags = (dev->flags & ~(IFF_PROMISC |
  5988. IFF_ALLMULTI |
  5989. IFF_RUNNING |
  5990. IFF_LOWER_UP |
  5991. IFF_DORMANT)) |
  5992. (dev->gflags & (IFF_PROMISC |
  5993. IFF_ALLMULTI));
  5994. if (netif_running(dev)) {
  5995. if (netif_oper_up(dev))
  5996. flags |= IFF_RUNNING;
  5997. if (netif_carrier_ok(dev))
  5998. flags |= IFF_LOWER_UP;
  5999. if (netif_dormant(dev))
  6000. flags |= IFF_DORMANT;
  6001. }
  6002. return flags;
  6003. }
  6004. EXPORT_SYMBOL(dev_get_flags);
  6005. int __dev_change_flags(struct net_device *dev, unsigned int flags)
  6006. {
  6007. unsigned int old_flags = dev->flags;
  6008. int ret;
  6009. ASSERT_RTNL();
  6010. /*
  6011. * Set the flags on our device.
  6012. */
  6013. dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
  6014. IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
  6015. IFF_AUTOMEDIA)) |
  6016. (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
  6017. IFF_ALLMULTI));
  6018. /*
  6019. * Load in the correct multicast list now the flags have changed.
  6020. */
  6021. if ((old_flags ^ flags) & IFF_MULTICAST)
  6022. dev_change_rx_flags(dev, IFF_MULTICAST);
  6023. dev_set_rx_mode(dev);
  6024. /*
  6025. * Have we downed the interface. We handle IFF_UP ourselves
  6026. * according to user attempts to set it, rather than blindly
  6027. * setting it.
  6028. */
  6029. ret = 0;
  6030. if ((old_flags ^ flags) & IFF_UP) {
  6031. if (old_flags & IFF_UP)
  6032. __dev_close(dev);
  6033. else
  6034. ret = __dev_open(dev);
  6035. }
  6036. if ((flags ^ dev->gflags) & IFF_PROMISC) {
  6037. int inc = (flags & IFF_PROMISC) ? 1 : -1;
  6038. unsigned int old_flags = dev->flags;
  6039. dev->gflags ^= IFF_PROMISC;
  6040. if (__dev_set_promiscuity(dev, inc, false) >= 0)
  6041. if (dev->flags != old_flags)
  6042. dev_set_rx_mode(dev);
  6043. }
  6044. /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
  6045. * is important. Some (broken) drivers set IFF_PROMISC, when
  6046. * IFF_ALLMULTI is requested not asking us and not reporting.
  6047. */
  6048. if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
  6049. int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
  6050. dev->gflags ^= IFF_ALLMULTI;
  6051. __dev_set_allmulti(dev, inc, false);
  6052. }
  6053. return ret;
  6054. }
  6055. void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
  6056. unsigned int gchanges)
  6057. {
  6058. unsigned int changes = dev->flags ^ old_flags;
  6059. if (gchanges)
  6060. rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
  6061. if (changes & IFF_UP) {
  6062. if (dev->flags & IFF_UP)
  6063. call_netdevice_notifiers(NETDEV_UP, dev);
  6064. else
  6065. call_netdevice_notifiers(NETDEV_DOWN, dev);
  6066. }
  6067. if (dev->flags & IFF_UP &&
  6068. (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
  6069. struct netdev_notifier_change_info change_info = {
  6070. .info = {
  6071. .dev = dev,
  6072. },
  6073. .flags_changed = changes,
  6074. };
  6075. call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
  6076. }
  6077. }
  6078. /**
  6079. * dev_change_flags - change device settings
  6080. * @dev: device
  6081. * @flags: device state flags
  6082. *
  6083. * Change settings on device based state flags. The flags are
  6084. * in the userspace exported format.
  6085. */
  6086. int dev_change_flags(struct net_device *dev, unsigned int flags)
  6087. {
  6088. int ret;
  6089. unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
  6090. ret = __dev_change_flags(dev, flags);
  6091. if (ret < 0)
  6092. return ret;
  6093. changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
  6094. __dev_notify_flags(dev, old_flags, changes);
  6095. return ret;
  6096. }
  6097. EXPORT_SYMBOL(dev_change_flags);
  6098. int __dev_set_mtu(struct net_device *dev, int new_mtu)
  6099. {
  6100. const struct net_device_ops *ops = dev->netdev_ops;
  6101. if (ops->ndo_change_mtu)
  6102. return ops->ndo_change_mtu(dev, new_mtu);
  6103. dev->mtu = new_mtu;
  6104. return 0;
  6105. }
  6106. EXPORT_SYMBOL(__dev_set_mtu);
  6107. /**
  6108. * dev_set_mtu - Change maximum transfer unit
  6109. * @dev: device
  6110. * @new_mtu: new transfer unit
  6111. *
  6112. * Change the maximum transfer size of the network device.
  6113. */
  6114. int dev_set_mtu(struct net_device *dev, int new_mtu)
  6115. {
  6116. int err, orig_mtu;
  6117. if (new_mtu == dev->mtu)
  6118. return 0;
  6119. /* MTU must be positive, and in range */
  6120. if (new_mtu < 0 || new_mtu < dev->min_mtu) {
  6121. net_err_ratelimited("%s: Invalid MTU %d requested, hw min %d\n",
  6122. dev->name, new_mtu, dev->min_mtu);
  6123. return -EINVAL;
  6124. }
  6125. if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
  6126. net_err_ratelimited("%s: Invalid MTU %d requested, hw max %d\n",
  6127. dev->name, new_mtu, dev->max_mtu);
  6128. return -EINVAL;
  6129. }
  6130. if (!netif_device_present(dev))
  6131. return -ENODEV;
  6132. err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
  6133. err = notifier_to_errno(err);
  6134. if (err)
  6135. return err;
  6136. orig_mtu = dev->mtu;
  6137. err = __dev_set_mtu(dev, new_mtu);
  6138. if (!err) {
  6139. err = call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
  6140. err = notifier_to_errno(err);
  6141. if (err) {
  6142. /* setting mtu back and notifying everyone again,
  6143. * so that they have a chance to revert changes.
  6144. */
  6145. __dev_set_mtu(dev, orig_mtu);
  6146. call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
  6147. }
  6148. }
  6149. return err;
  6150. }
  6151. EXPORT_SYMBOL(dev_set_mtu);
  6152. /**
  6153. * dev_change_tx_queue_len - Change TX queue length of a netdevice
  6154. * @dev: device
  6155. * @new_len: new tx queue length
  6156. */
  6157. int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
  6158. {
  6159. unsigned int orig_len = dev->tx_queue_len;
  6160. int res;
  6161. if (new_len != (unsigned int)new_len)
  6162. return -ERANGE;
  6163. if (new_len != orig_len) {
  6164. dev->tx_queue_len = new_len;
  6165. res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
  6166. res = notifier_to_errno(res);
  6167. if (res) {
  6168. netdev_err(dev,
  6169. "refused to change device tx_queue_len\n");
  6170. dev->tx_queue_len = orig_len;
  6171. return res;
  6172. }
  6173. return dev_qdisc_change_tx_queue_len(dev);
  6174. }
  6175. return 0;
  6176. }
  6177. /**
  6178. * dev_set_group - Change group this device belongs to
  6179. * @dev: device
  6180. * @new_group: group this device should belong to
  6181. */
  6182. void dev_set_group(struct net_device *dev, int new_group)
  6183. {
  6184. dev->group = new_group;
  6185. }
  6186. EXPORT_SYMBOL(dev_set_group);
  6187. /**
  6188. * dev_set_mac_address - Change Media Access Control Address
  6189. * @dev: device
  6190. * @sa: new address
  6191. *
  6192. * Change the hardware (MAC) address of the device
  6193. */
  6194. int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
  6195. {
  6196. const struct net_device_ops *ops = dev->netdev_ops;
  6197. int err;
  6198. if (!ops->ndo_set_mac_address)
  6199. return -EOPNOTSUPP;
  6200. if (sa->sa_family != dev->type)
  6201. return -EINVAL;
  6202. if (!netif_device_present(dev))
  6203. return -ENODEV;
  6204. err = ops->ndo_set_mac_address(dev, sa);
  6205. if (err)
  6206. return err;
  6207. dev->addr_assign_type = NET_ADDR_SET;
  6208. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  6209. add_device_randomness(dev->dev_addr, dev->addr_len);
  6210. return 0;
  6211. }
  6212. EXPORT_SYMBOL(dev_set_mac_address);
  6213. /**
  6214. * dev_change_carrier - Change device carrier
  6215. * @dev: device
  6216. * @new_carrier: new value
  6217. *
  6218. * Change device carrier
  6219. */
  6220. int dev_change_carrier(struct net_device *dev, bool new_carrier)
  6221. {
  6222. const struct net_device_ops *ops = dev->netdev_ops;
  6223. if (!ops->ndo_change_carrier)
  6224. return -EOPNOTSUPP;
  6225. if (!netif_device_present(dev))
  6226. return -ENODEV;
  6227. return ops->ndo_change_carrier(dev, new_carrier);
  6228. }
  6229. EXPORT_SYMBOL(dev_change_carrier);
  6230. /**
  6231. * dev_get_phys_port_id - Get device physical port ID
  6232. * @dev: device
  6233. * @ppid: port ID
  6234. *
  6235. * Get device physical port ID
  6236. */
  6237. int dev_get_phys_port_id(struct net_device *dev,
  6238. struct netdev_phys_item_id *ppid)
  6239. {
  6240. const struct net_device_ops *ops = dev->netdev_ops;
  6241. if (!ops->ndo_get_phys_port_id)
  6242. return -EOPNOTSUPP;
  6243. return ops->ndo_get_phys_port_id(dev, ppid);
  6244. }
  6245. EXPORT_SYMBOL(dev_get_phys_port_id);
  6246. /**
  6247. * dev_get_phys_port_name - Get device physical port name
  6248. * @dev: device
  6249. * @name: port name
  6250. * @len: limit of bytes to copy to name
  6251. *
  6252. * Get device physical port name
  6253. */
  6254. int dev_get_phys_port_name(struct net_device *dev,
  6255. char *name, size_t len)
  6256. {
  6257. const struct net_device_ops *ops = dev->netdev_ops;
  6258. if (!ops->ndo_get_phys_port_name)
  6259. return -EOPNOTSUPP;
  6260. return ops->ndo_get_phys_port_name(dev, name, len);
  6261. }
  6262. EXPORT_SYMBOL(dev_get_phys_port_name);
  6263. /**
  6264. * dev_change_proto_down - update protocol port state information
  6265. * @dev: device
  6266. * @proto_down: new value
  6267. *
  6268. * This info can be used by switch drivers to set the phys state of the
  6269. * port.
  6270. */
  6271. int dev_change_proto_down(struct net_device *dev, bool proto_down)
  6272. {
  6273. const struct net_device_ops *ops = dev->netdev_ops;
  6274. if (!ops->ndo_change_proto_down)
  6275. return -EOPNOTSUPP;
  6276. if (!netif_device_present(dev))
  6277. return -ENODEV;
  6278. return ops->ndo_change_proto_down(dev, proto_down);
  6279. }
  6280. EXPORT_SYMBOL(dev_change_proto_down);
  6281. void __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,
  6282. struct netdev_bpf *xdp)
  6283. {
  6284. memset(xdp, 0, sizeof(*xdp));
  6285. xdp->command = XDP_QUERY_PROG;
  6286. /* Query must always succeed. */
  6287. WARN_ON(bpf_op(dev, xdp) < 0);
  6288. }
  6289. static u8 __dev_xdp_attached(struct net_device *dev, bpf_op_t bpf_op)
  6290. {
  6291. struct netdev_bpf xdp;
  6292. __dev_xdp_query(dev, bpf_op, &xdp);
  6293. return xdp.prog_attached;
  6294. }
  6295. static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,
  6296. struct netlink_ext_ack *extack, u32 flags,
  6297. struct bpf_prog *prog)
  6298. {
  6299. struct netdev_bpf xdp;
  6300. memset(&xdp, 0, sizeof(xdp));
  6301. if (flags & XDP_FLAGS_HW_MODE)
  6302. xdp.command = XDP_SETUP_PROG_HW;
  6303. else
  6304. xdp.command = XDP_SETUP_PROG;
  6305. xdp.extack = extack;
  6306. xdp.flags = flags;
  6307. xdp.prog = prog;
  6308. return bpf_op(dev, &xdp);
  6309. }
  6310. static void dev_xdp_uninstall(struct net_device *dev)
  6311. {
  6312. struct netdev_bpf xdp;
  6313. bpf_op_t ndo_bpf;
  6314. /* Remove generic XDP */
  6315. WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL));
  6316. /* Remove from the driver */
  6317. ndo_bpf = dev->netdev_ops->ndo_bpf;
  6318. if (!ndo_bpf)
  6319. return;
  6320. __dev_xdp_query(dev, ndo_bpf, &xdp);
  6321. if (xdp.prog_attached == XDP_ATTACHED_NONE)
  6322. return;
  6323. /* Program removal should always succeed */
  6324. WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags, NULL));
  6325. }
  6326. /**
  6327. * dev_change_xdp_fd - set or clear a bpf program for a device rx path
  6328. * @dev: device
  6329. * @extack: netlink extended ack
  6330. * @fd: new program fd or negative value to clear
  6331. * @flags: xdp-related flags
  6332. *
  6333. * Set or clear a bpf program for a device
  6334. */
  6335. int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
  6336. int fd, u32 flags)
  6337. {
  6338. const struct net_device_ops *ops = dev->netdev_ops;
  6339. struct bpf_prog *prog = NULL;
  6340. bpf_op_t bpf_op, bpf_chk;
  6341. int err;
  6342. ASSERT_RTNL();
  6343. bpf_op = bpf_chk = ops->ndo_bpf;
  6344. if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE)))
  6345. return -EOPNOTSUPP;
  6346. if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE))
  6347. bpf_op = generic_xdp_install;
  6348. if (bpf_op == bpf_chk)
  6349. bpf_chk = generic_xdp_install;
  6350. if (fd >= 0) {
  6351. if (bpf_chk && __dev_xdp_attached(dev, bpf_chk))
  6352. return -EEXIST;
  6353. if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
  6354. __dev_xdp_attached(dev, bpf_op))
  6355. return -EBUSY;
  6356. prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
  6357. bpf_op == ops->ndo_bpf);
  6358. if (IS_ERR(prog))
  6359. return PTR_ERR(prog);
  6360. if (!(flags & XDP_FLAGS_HW_MODE) &&
  6361. bpf_prog_is_dev_bound(prog->aux)) {
  6362. NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported");
  6363. bpf_prog_put(prog);
  6364. return -EINVAL;
  6365. }
  6366. }
  6367. err = dev_xdp_install(dev, bpf_op, extack, flags, prog);
  6368. if (err < 0 && prog)
  6369. bpf_prog_put(prog);
  6370. return err;
  6371. }
  6372. /**
  6373. * dev_new_index - allocate an ifindex
  6374. * @net: the applicable net namespace
  6375. *
  6376. * Returns a suitable unique value for a new device interface
  6377. * number. The caller must hold the rtnl semaphore or the
  6378. * dev_base_lock to be sure it remains unique.
  6379. */
  6380. static int dev_new_index(struct net *net)
  6381. {
  6382. int ifindex = net->ifindex;
  6383. for (;;) {
  6384. if (++ifindex <= 0)
  6385. ifindex = 1;
  6386. if (!__dev_get_by_index(net, ifindex))
  6387. return net->ifindex = ifindex;
  6388. }
  6389. }
  6390. /* Delayed registration/unregisteration */
  6391. static LIST_HEAD(net_todo_list);
  6392. DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
  6393. static void net_set_todo(struct net_device *dev)
  6394. {
  6395. list_add_tail(&dev->todo_list, &net_todo_list);
  6396. dev_net(dev)->dev_unreg_count++;
  6397. }
  6398. static void rollback_registered_many(struct list_head *head)
  6399. {
  6400. struct net_device *dev, *tmp;
  6401. LIST_HEAD(close_head);
  6402. BUG_ON(dev_boot_phase);
  6403. ASSERT_RTNL();
  6404. list_for_each_entry_safe(dev, tmp, head, unreg_list) {
  6405. /* Some devices call without registering
  6406. * for initialization unwind. Remove those
  6407. * devices and proceed with the remaining.
  6408. */
  6409. if (dev->reg_state == NETREG_UNINITIALIZED) {
  6410. pr_debug("unregister_netdevice: device %s/%p never was registered\n",
  6411. dev->name, dev);
  6412. WARN_ON(1);
  6413. list_del(&dev->unreg_list);
  6414. continue;
  6415. }
  6416. dev->dismantle = true;
  6417. BUG_ON(dev->reg_state != NETREG_REGISTERED);
  6418. }
  6419. /* If device is running, close it first. */
  6420. list_for_each_entry(dev, head, unreg_list)
  6421. list_add_tail(&dev->close_list, &close_head);
  6422. dev_close_many(&close_head, true);
  6423. list_for_each_entry(dev, head, unreg_list) {
  6424. /* And unlink it from device chain. */
  6425. unlist_netdevice(dev);
  6426. dev->reg_state = NETREG_UNREGISTERING;
  6427. }
  6428. flush_all_backlogs();
  6429. synchronize_net();
  6430. list_for_each_entry(dev, head, unreg_list) {
  6431. struct sk_buff *skb = NULL;
  6432. /* Shutdown queueing discipline. */
  6433. dev_shutdown(dev);
  6434. dev_xdp_uninstall(dev);
  6435. /* Notify protocols, that we are about to destroy
  6436. * this device. They should clean all the things.
  6437. */
  6438. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  6439. if (!dev->rtnl_link_ops ||
  6440. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  6441. skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
  6442. GFP_KERNEL, NULL, 0);
  6443. /*
  6444. * Flush the unicast and multicast chains
  6445. */
  6446. dev_uc_flush(dev);
  6447. dev_mc_flush(dev);
  6448. if (dev->netdev_ops->ndo_uninit)
  6449. dev->netdev_ops->ndo_uninit(dev);
  6450. if (skb)
  6451. rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
  6452. /* Notifier chain MUST detach us all upper devices. */
  6453. WARN_ON(netdev_has_any_upper_dev(dev));
  6454. WARN_ON(netdev_has_any_lower_dev(dev));
  6455. /* Remove entries from kobject tree */
  6456. netdev_unregister_kobject(dev);
  6457. #ifdef CONFIG_XPS
  6458. /* Remove XPS queueing entries */
  6459. netif_reset_xps_queues_gt(dev, 0);
  6460. #endif
  6461. }
  6462. synchronize_net();
  6463. list_for_each_entry(dev, head, unreg_list)
  6464. dev_put(dev);
  6465. }
  6466. static void rollback_registered(struct net_device *dev)
  6467. {
  6468. LIST_HEAD(single);
  6469. list_add(&dev->unreg_list, &single);
  6470. rollback_registered_many(&single);
  6471. list_del(&single);
  6472. }
  6473. static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
  6474. struct net_device *upper, netdev_features_t features)
  6475. {
  6476. netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
  6477. netdev_features_t feature;
  6478. int feature_bit;
  6479. for_each_netdev_feature(&upper_disables, feature_bit) {
  6480. feature = __NETIF_F_BIT(feature_bit);
  6481. if (!(upper->wanted_features & feature)
  6482. && (features & feature)) {
  6483. netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
  6484. &feature, upper->name);
  6485. features &= ~feature;
  6486. }
  6487. }
  6488. return features;
  6489. }
  6490. static void netdev_sync_lower_features(struct net_device *upper,
  6491. struct net_device *lower, netdev_features_t features)
  6492. {
  6493. netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
  6494. netdev_features_t feature;
  6495. int feature_bit;
  6496. for_each_netdev_feature(&upper_disables, feature_bit) {
  6497. feature = __NETIF_F_BIT(feature_bit);
  6498. if (!(features & feature) && (lower->features & feature)) {
  6499. netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
  6500. &feature, lower->name);
  6501. lower->wanted_features &= ~feature;
  6502. netdev_update_features(lower);
  6503. if (unlikely(lower->features & feature))
  6504. netdev_WARN(upper, "failed to disable %pNF on %s!\n",
  6505. &feature, lower->name);
  6506. }
  6507. }
  6508. }
  6509. static netdev_features_t netdev_fix_features(struct net_device *dev,
  6510. netdev_features_t features)
  6511. {
  6512. /* Fix illegal checksum combinations */
  6513. if ((features & NETIF_F_HW_CSUM) &&
  6514. (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  6515. netdev_warn(dev, "mixed HW and IP checksum settings.\n");
  6516. features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
  6517. }
  6518. /* TSO requires that SG is present as well. */
  6519. if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
  6520. netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
  6521. features &= ~NETIF_F_ALL_TSO;
  6522. }
  6523. if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
  6524. !(features & NETIF_F_IP_CSUM)) {
  6525. netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
  6526. features &= ~NETIF_F_TSO;
  6527. features &= ~NETIF_F_TSO_ECN;
  6528. }
  6529. if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
  6530. !(features & NETIF_F_IPV6_CSUM)) {
  6531. netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
  6532. features &= ~NETIF_F_TSO6;
  6533. }
  6534. /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
  6535. if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
  6536. features &= ~NETIF_F_TSO_MANGLEID;
  6537. /* TSO ECN requires that TSO is present as well. */
  6538. if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
  6539. features &= ~NETIF_F_TSO_ECN;
  6540. /* Software GSO depends on SG. */
  6541. if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
  6542. netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
  6543. features &= ~NETIF_F_GSO;
  6544. }
  6545. /* GSO partial features require GSO partial be set */
  6546. if ((features & dev->gso_partial_features) &&
  6547. !(features & NETIF_F_GSO_PARTIAL)) {
  6548. netdev_dbg(dev,
  6549. "Dropping partially supported GSO features since no GSO partial.\n");
  6550. features &= ~dev->gso_partial_features;
  6551. }
  6552. if (!(features & NETIF_F_RXCSUM)) {
  6553. /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
  6554. * successfully merged by hardware must also have the
  6555. * checksum verified by hardware. If the user does not
  6556. * want to enable RXCSUM, logically, we should disable GRO_HW.
  6557. */
  6558. if (features & NETIF_F_GRO_HW) {
  6559. netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
  6560. features &= ~NETIF_F_GRO_HW;
  6561. }
  6562. }
  6563. /* LRO/HW-GRO features cannot be combined with RX-FCS */
  6564. if (features & NETIF_F_RXFCS) {
  6565. if (features & NETIF_F_LRO) {
  6566. netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
  6567. features &= ~NETIF_F_LRO;
  6568. }
  6569. if (features & NETIF_F_GRO_HW) {
  6570. netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
  6571. features &= ~NETIF_F_GRO_HW;
  6572. }
  6573. }
  6574. return features;
  6575. }
  6576. int __netdev_update_features(struct net_device *dev)
  6577. {
  6578. struct net_device *upper, *lower;
  6579. netdev_features_t features;
  6580. struct list_head *iter;
  6581. int err = -1;
  6582. ASSERT_RTNL();
  6583. features = netdev_get_wanted_features(dev);
  6584. if (dev->netdev_ops->ndo_fix_features)
  6585. features = dev->netdev_ops->ndo_fix_features(dev, features);
  6586. /* driver might be less strict about feature dependencies */
  6587. features = netdev_fix_features(dev, features);
  6588. /* some features can't be enabled if they're off an an upper device */
  6589. netdev_for_each_upper_dev_rcu(dev, upper, iter)
  6590. features = netdev_sync_upper_features(dev, upper, features);
  6591. if (dev->features == features)
  6592. goto sync_lower;
  6593. netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
  6594. &dev->features, &features);
  6595. if (dev->netdev_ops->ndo_set_features)
  6596. err = dev->netdev_ops->ndo_set_features(dev, features);
  6597. else
  6598. err = 0;
  6599. if (unlikely(err < 0)) {
  6600. netdev_err(dev,
  6601. "set_features() failed (%d); wanted %pNF, left %pNF\n",
  6602. err, &features, &dev->features);
  6603. /* return non-0 since some features might have changed and
  6604. * it's better to fire a spurious notification than miss it
  6605. */
  6606. return -1;
  6607. }
  6608. sync_lower:
  6609. /* some features must be disabled on lower devices when disabled
  6610. * on an upper device (think: bonding master or bridge)
  6611. */
  6612. netdev_for_each_lower_dev(dev, lower, iter)
  6613. netdev_sync_lower_features(dev, lower, features);
  6614. if (!err) {
  6615. netdev_features_t diff = features ^ dev->features;
  6616. if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
  6617. /* udp_tunnel_{get,drop}_rx_info both need
  6618. * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
  6619. * device, or they won't do anything.
  6620. * Thus we need to update dev->features
  6621. * *before* calling udp_tunnel_get_rx_info,
  6622. * but *after* calling udp_tunnel_drop_rx_info.
  6623. */
  6624. if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
  6625. dev->features = features;
  6626. udp_tunnel_get_rx_info(dev);
  6627. } else {
  6628. udp_tunnel_drop_rx_info(dev);
  6629. }
  6630. }
  6631. if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
  6632. if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
  6633. dev->features = features;
  6634. err |= vlan_get_rx_ctag_filter_info(dev);
  6635. } else {
  6636. vlan_drop_rx_ctag_filter_info(dev);
  6637. }
  6638. }
  6639. if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
  6640. if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
  6641. dev->features = features;
  6642. err |= vlan_get_rx_stag_filter_info(dev);
  6643. } else {
  6644. vlan_drop_rx_stag_filter_info(dev);
  6645. }
  6646. }
  6647. dev->features = features;
  6648. }
  6649. return err < 0 ? 0 : 1;
  6650. }
  6651. /**
  6652. * netdev_update_features - recalculate device features
  6653. * @dev: the device to check
  6654. *
  6655. * Recalculate dev->features set and send notifications if it
  6656. * has changed. Should be called after driver or hardware dependent
  6657. * conditions might have changed that influence the features.
  6658. */
  6659. void netdev_update_features(struct net_device *dev)
  6660. {
  6661. if (__netdev_update_features(dev))
  6662. netdev_features_change(dev);
  6663. }
  6664. EXPORT_SYMBOL(netdev_update_features);
  6665. /**
  6666. * netdev_change_features - recalculate device features
  6667. * @dev: the device to check
  6668. *
  6669. * Recalculate dev->features set and send notifications even
  6670. * if they have not changed. Should be called instead of
  6671. * netdev_update_features() if also dev->vlan_features might
  6672. * have changed to allow the changes to be propagated to stacked
  6673. * VLAN devices.
  6674. */
  6675. void netdev_change_features(struct net_device *dev)
  6676. {
  6677. __netdev_update_features(dev);
  6678. netdev_features_change(dev);
  6679. }
  6680. EXPORT_SYMBOL(netdev_change_features);
  6681. /**
  6682. * netif_stacked_transfer_operstate - transfer operstate
  6683. * @rootdev: the root or lower level device to transfer state from
  6684. * @dev: the device to transfer operstate to
  6685. *
  6686. * Transfer operational state from root to device. This is normally
  6687. * called when a stacking relationship exists between the root
  6688. * device and the device(a leaf device).
  6689. */
  6690. void netif_stacked_transfer_operstate(const struct net_device *rootdev,
  6691. struct net_device *dev)
  6692. {
  6693. if (rootdev->operstate == IF_OPER_DORMANT)
  6694. netif_dormant_on(dev);
  6695. else
  6696. netif_dormant_off(dev);
  6697. if (netif_carrier_ok(rootdev))
  6698. netif_carrier_on(dev);
  6699. else
  6700. netif_carrier_off(dev);
  6701. }
  6702. EXPORT_SYMBOL(netif_stacked_transfer_operstate);
  6703. static int netif_alloc_rx_queues(struct net_device *dev)
  6704. {
  6705. unsigned int i, count = dev->num_rx_queues;
  6706. struct netdev_rx_queue *rx;
  6707. size_t sz = count * sizeof(*rx);
  6708. int err = 0;
  6709. BUG_ON(count < 1);
  6710. rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  6711. if (!rx)
  6712. return -ENOMEM;
  6713. dev->_rx = rx;
  6714. for (i = 0; i < count; i++) {
  6715. rx[i].dev = dev;
  6716. /* XDP RX-queue setup */
  6717. err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
  6718. if (err < 0)
  6719. goto err_rxq_info;
  6720. }
  6721. return 0;
  6722. err_rxq_info:
  6723. /* Rollback successful reg's and free other resources */
  6724. while (i--)
  6725. xdp_rxq_info_unreg(&rx[i].xdp_rxq);
  6726. kvfree(dev->_rx);
  6727. dev->_rx = NULL;
  6728. return err;
  6729. }
  6730. static void netif_free_rx_queues(struct net_device *dev)
  6731. {
  6732. unsigned int i, count = dev->num_rx_queues;
  6733. /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
  6734. if (!dev->_rx)
  6735. return;
  6736. for (i = 0; i < count; i++)
  6737. xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
  6738. kvfree(dev->_rx);
  6739. }
  6740. static void netdev_init_one_queue(struct net_device *dev,
  6741. struct netdev_queue *queue, void *_unused)
  6742. {
  6743. /* Initialize queue lock */
  6744. spin_lock_init(&queue->_xmit_lock);
  6745. netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
  6746. queue->xmit_lock_owner = -1;
  6747. netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
  6748. queue->dev = dev;
  6749. #ifdef CONFIG_BQL
  6750. dql_init(&queue->dql, HZ);
  6751. #endif
  6752. }
  6753. static void netif_free_tx_queues(struct net_device *dev)
  6754. {
  6755. kvfree(dev->_tx);
  6756. }
  6757. static int netif_alloc_netdev_queues(struct net_device *dev)
  6758. {
  6759. unsigned int count = dev->num_tx_queues;
  6760. struct netdev_queue *tx;
  6761. size_t sz = count * sizeof(*tx);
  6762. if (count < 1 || count > 0xffff)
  6763. return -EINVAL;
  6764. tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  6765. if (!tx)
  6766. return -ENOMEM;
  6767. dev->_tx = tx;
  6768. netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
  6769. spin_lock_init(&dev->tx_global_lock);
  6770. return 0;
  6771. }
  6772. void netif_tx_stop_all_queues(struct net_device *dev)
  6773. {
  6774. unsigned int i;
  6775. for (i = 0; i < dev->num_tx_queues; i++) {
  6776. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  6777. netif_tx_stop_queue(txq);
  6778. }
  6779. }
  6780. EXPORT_SYMBOL(netif_tx_stop_all_queues);
  6781. /**
  6782. * register_netdevice - register a network device
  6783. * @dev: device to register
  6784. *
  6785. * Take a completed network device structure and add it to the kernel
  6786. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  6787. * chain. 0 is returned on success. A negative errno code is returned
  6788. * on a failure to set up the device, or if the name is a duplicate.
  6789. *
  6790. * Callers must hold the rtnl semaphore. You may want
  6791. * register_netdev() instead of this.
  6792. *
  6793. * BUGS:
  6794. * The locking appears insufficient to guarantee two parallel registers
  6795. * will not get the same name.
  6796. */
  6797. int register_netdevice(struct net_device *dev)
  6798. {
  6799. int ret;
  6800. struct net *net = dev_net(dev);
  6801. BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
  6802. NETDEV_FEATURE_COUNT);
  6803. BUG_ON(dev_boot_phase);
  6804. ASSERT_RTNL();
  6805. might_sleep();
  6806. /* When net_device's are persistent, this will be fatal. */
  6807. BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
  6808. BUG_ON(!net);
  6809. spin_lock_init(&dev->addr_list_lock);
  6810. netdev_set_addr_lockdep_class(dev);
  6811. ret = dev_get_valid_name(net, dev, dev->name);
  6812. if (ret < 0)
  6813. goto out;
  6814. /* Init, if this function is available */
  6815. if (dev->netdev_ops->ndo_init) {
  6816. ret = dev->netdev_ops->ndo_init(dev);
  6817. if (ret) {
  6818. if (ret > 0)
  6819. ret = -EIO;
  6820. goto out;
  6821. }
  6822. }
  6823. if (((dev->hw_features | dev->features) &
  6824. NETIF_F_HW_VLAN_CTAG_FILTER) &&
  6825. (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
  6826. !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
  6827. netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
  6828. ret = -EINVAL;
  6829. goto err_uninit;
  6830. }
  6831. ret = -EBUSY;
  6832. if (!dev->ifindex)
  6833. dev->ifindex = dev_new_index(net);
  6834. else if (__dev_get_by_index(net, dev->ifindex))
  6835. goto err_uninit;
  6836. /* Transfer changeable features to wanted_features and enable
  6837. * software offloads (GSO and GRO).
  6838. */
  6839. dev->hw_features |= NETIF_F_SOFT_FEATURES;
  6840. dev->features |= NETIF_F_SOFT_FEATURES;
  6841. if (dev->netdev_ops->ndo_udp_tunnel_add) {
  6842. dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
  6843. dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
  6844. }
  6845. dev->wanted_features = dev->features & dev->hw_features;
  6846. if (!(dev->flags & IFF_LOOPBACK))
  6847. dev->hw_features |= NETIF_F_NOCACHE_COPY;
  6848. /* If IPv4 TCP segmentation offload is supported we should also
  6849. * allow the device to enable segmenting the frame with the option
  6850. * of ignoring a static IP ID value. This doesn't enable the
  6851. * feature itself but allows the user to enable it later.
  6852. */
  6853. if (dev->hw_features & NETIF_F_TSO)
  6854. dev->hw_features |= NETIF_F_TSO_MANGLEID;
  6855. if (dev->vlan_features & NETIF_F_TSO)
  6856. dev->vlan_features |= NETIF_F_TSO_MANGLEID;
  6857. if (dev->mpls_features & NETIF_F_TSO)
  6858. dev->mpls_features |= NETIF_F_TSO_MANGLEID;
  6859. if (dev->hw_enc_features & NETIF_F_TSO)
  6860. dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  6861. /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
  6862. */
  6863. dev->vlan_features |= NETIF_F_HIGHDMA;
  6864. /* Make NETIF_F_SG inheritable to tunnel devices.
  6865. */
  6866. dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
  6867. /* Make NETIF_F_SG inheritable to MPLS.
  6868. */
  6869. dev->mpls_features |= NETIF_F_SG;
  6870. ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
  6871. ret = notifier_to_errno(ret);
  6872. if (ret)
  6873. goto err_uninit;
  6874. ret = netdev_register_kobject(dev);
  6875. if (ret)
  6876. goto err_uninit;
  6877. dev->reg_state = NETREG_REGISTERED;
  6878. __netdev_update_features(dev);
  6879. /*
  6880. * Default initial state at registry is that the
  6881. * device is present.
  6882. */
  6883. set_bit(__LINK_STATE_PRESENT, &dev->state);
  6884. linkwatch_init_dev(dev);
  6885. dev_init_scheduler(dev);
  6886. dev_hold(dev);
  6887. list_netdevice(dev);
  6888. add_device_randomness(dev->dev_addr, dev->addr_len);
  6889. /* If the device has permanent device address, driver should
  6890. * set dev_addr and also addr_assign_type should be set to
  6891. * NET_ADDR_PERM (default value).
  6892. */
  6893. if (dev->addr_assign_type == NET_ADDR_PERM)
  6894. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  6895. /* Notify protocols, that a new device appeared. */
  6896. ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
  6897. ret = notifier_to_errno(ret);
  6898. if (ret) {
  6899. rollback_registered(dev);
  6900. dev->reg_state = NETREG_UNREGISTERED;
  6901. }
  6902. /*
  6903. * Prevent userspace races by waiting until the network
  6904. * device is fully setup before sending notifications.
  6905. */
  6906. if (!dev->rtnl_link_ops ||
  6907. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  6908. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
  6909. out:
  6910. return ret;
  6911. err_uninit:
  6912. if (dev->netdev_ops->ndo_uninit)
  6913. dev->netdev_ops->ndo_uninit(dev);
  6914. if (dev->priv_destructor)
  6915. dev->priv_destructor(dev);
  6916. goto out;
  6917. }
  6918. EXPORT_SYMBOL(register_netdevice);
  6919. /**
  6920. * init_dummy_netdev - init a dummy network device for NAPI
  6921. * @dev: device to init
  6922. *
  6923. * This takes a network device structure and initialize the minimum
  6924. * amount of fields so it can be used to schedule NAPI polls without
  6925. * registering a full blown interface. This is to be used by drivers
  6926. * that need to tie several hardware interfaces to a single NAPI
  6927. * poll scheduler due to HW limitations.
  6928. */
  6929. int init_dummy_netdev(struct net_device *dev)
  6930. {
  6931. /* Clear everything. Note we don't initialize spinlocks
  6932. * are they aren't supposed to be taken by any of the
  6933. * NAPI code and this dummy netdev is supposed to be
  6934. * only ever used for NAPI polls
  6935. */
  6936. memset(dev, 0, sizeof(struct net_device));
  6937. /* make sure we BUG if trying to hit standard
  6938. * register/unregister code path
  6939. */
  6940. dev->reg_state = NETREG_DUMMY;
  6941. /* NAPI wants this */
  6942. INIT_LIST_HEAD(&dev->napi_list);
  6943. /* a dummy interface is started by default */
  6944. set_bit(__LINK_STATE_PRESENT, &dev->state);
  6945. set_bit(__LINK_STATE_START, &dev->state);
  6946. /* Note : We dont allocate pcpu_refcnt for dummy devices,
  6947. * because users of this 'device' dont need to change
  6948. * its refcount.
  6949. */
  6950. return 0;
  6951. }
  6952. EXPORT_SYMBOL_GPL(init_dummy_netdev);
  6953. /**
  6954. * register_netdev - register a network device
  6955. * @dev: device to register
  6956. *
  6957. * Take a completed network device structure and add it to the kernel
  6958. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  6959. * chain. 0 is returned on success. A negative errno code is returned
  6960. * on a failure to set up the device, or if the name is a duplicate.
  6961. *
  6962. * This is a wrapper around register_netdevice that takes the rtnl semaphore
  6963. * and expands the device name if you passed a format string to
  6964. * alloc_netdev.
  6965. */
  6966. int register_netdev(struct net_device *dev)
  6967. {
  6968. int err;
  6969. if (rtnl_lock_killable())
  6970. return -EINTR;
  6971. err = register_netdevice(dev);
  6972. rtnl_unlock();
  6973. return err;
  6974. }
  6975. EXPORT_SYMBOL(register_netdev);
  6976. int netdev_refcnt_read(const struct net_device *dev)
  6977. {
  6978. int i, refcnt = 0;
  6979. for_each_possible_cpu(i)
  6980. refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
  6981. return refcnt;
  6982. }
  6983. EXPORT_SYMBOL(netdev_refcnt_read);
  6984. /**
  6985. * netdev_wait_allrefs - wait until all references are gone.
  6986. * @dev: target net_device
  6987. *
  6988. * This is called when unregistering network devices.
  6989. *
  6990. * Any protocol or device that holds a reference should register
  6991. * for netdevice notification, and cleanup and put back the
  6992. * reference if they receive an UNREGISTER event.
  6993. * We can get stuck here if buggy protocols don't correctly
  6994. * call dev_put.
  6995. */
  6996. static void netdev_wait_allrefs(struct net_device *dev)
  6997. {
  6998. unsigned long rebroadcast_time, warning_time;
  6999. int refcnt;
  7000. linkwatch_forget_dev(dev);
  7001. rebroadcast_time = warning_time = jiffies;
  7002. refcnt = netdev_refcnt_read(dev);
  7003. while (refcnt != 0) {
  7004. if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
  7005. rtnl_lock();
  7006. /* Rebroadcast unregister notification */
  7007. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  7008. __rtnl_unlock();
  7009. rcu_barrier();
  7010. rtnl_lock();
  7011. if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
  7012. &dev->state)) {
  7013. /* We must not have linkwatch events
  7014. * pending on unregister. If this
  7015. * happens, we simply run the queue
  7016. * unscheduled, resulting in a noop
  7017. * for this device.
  7018. */
  7019. linkwatch_run_queue();
  7020. }
  7021. __rtnl_unlock();
  7022. rebroadcast_time = jiffies;
  7023. }
  7024. msleep(250);
  7025. refcnt = netdev_refcnt_read(dev);
  7026. if (time_after(jiffies, warning_time + 10 * HZ)) {
  7027. pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
  7028. dev->name, refcnt);
  7029. warning_time = jiffies;
  7030. }
  7031. }
  7032. }
  7033. /* The sequence is:
  7034. *
  7035. * rtnl_lock();
  7036. * ...
  7037. * register_netdevice(x1);
  7038. * register_netdevice(x2);
  7039. * ...
  7040. * unregister_netdevice(y1);
  7041. * unregister_netdevice(y2);
  7042. * ...
  7043. * rtnl_unlock();
  7044. * free_netdev(y1);
  7045. * free_netdev(y2);
  7046. *
  7047. * We are invoked by rtnl_unlock().
  7048. * This allows us to deal with problems:
  7049. * 1) We can delete sysfs objects which invoke hotplug
  7050. * without deadlocking with linkwatch via keventd.
  7051. * 2) Since we run with the RTNL semaphore not held, we can sleep
  7052. * safely in order to wait for the netdev refcnt to drop to zero.
  7053. *
  7054. * We must not return until all unregister events added during
  7055. * the interval the lock was held have been completed.
  7056. */
  7057. void netdev_run_todo(void)
  7058. {
  7059. struct list_head list;
  7060. /* Snapshot list, allow later requests */
  7061. list_replace_init(&net_todo_list, &list);
  7062. __rtnl_unlock();
  7063. /* Wait for rcu callbacks to finish before next phase */
  7064. if (!list_empty(&list))
  7065. rcu_barrier();
  7066. while (!list_empty(&list)) {
  7067. struct net_device *dev
  7068. = list_first_entry(&list, struct net_device, todo_list);
  7069. list_del(&dev->todo_list);
  7070. if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
  7071. pr_err("network todo '%s' but state %d\n",
  7072. dev->name, dev->reg_state);
  7073. dump_stack();
  7074. continue;
  7075. }
  7076. dev->reg_state = NETREG_UNREGISTERED;
  7077. netdev_wait_allrefs(dev);
  7078. /* paranoia */
  7079. BUG_ON(netdev_refcnt_read(dev));
  7080. BUG_ON(!list_empty(&dev->ptype_all));
  7081. BUG_ON(!list_empty(&dev->ptype_specific));
  7082. WARN_ON(rcu_access_pointer(dev->ip_ptr));
  7083. WARN_ON(rcu_access_pointer(dev->ip6_ptr));
  7084. #if IS_ENABLED(CONFIG_DECNET)
  7085. WARN_ON(dev->dn_ptr);
  7086. #endif
  7087. if (dev->priv_destructor)
  7088. dev->priv_destructor(dev);
  7089. if (dev->needs_free_netdev)
  7090. free_netdev(dev);
  7091. /* Report a network device has been unregistered */
  7092. rtnl_lock();
  7093. dev_net(dev)->dev_unreg_count--;
  7094. __rtnl_unlock();
  7095. wake_up(&netdev_unregistering_wq);
  7096. /* Free network device */
  7097. kobject_put(&dev->dev.kobj);
  7098. }
  7099. }
  7100. /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
  7101. * all the same fields in the same order as net_device_stats, with only
  7102. * the type differing, but rtnl_link_stats64 may have additional fields
  7103. * at the end for newer counters.
  7104. */
  7105. void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
  7106. const struct net_device_stats *netdev_stats)
  7107. {
  7108. #if BITS_PER_LONG == 64
  7109. BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
  7110. memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
  7111. /* zero out counters that only exist in rtnl_link_stats64 */
  7112. memset((char *)stats64 + sizeof(*netdev_stats), 0,
  7113. sizeof(*stats64) - sizeof(*netdev_stats));
  7114. #else
  7115. size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
  7116. const unsigned long *src = (const unsigned long *)netdev_stats;
  7117. u64 *dst = (u64 *)stats64;
  7118. BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
  7119. for (i = 0; i < n; i++)
  7120. dst[i] = src[i];
  7121. /* zero out counters that only exist in rtnl_link_stats64 */
  7122. memset((char *)stats64 + n * sizeof(u64), 0,
  7123. sizeof(*stats64) - n * sizeof(u64));
  7124. #endif
  7125. }
  7126. EXPORT_SYMBOL(netdev_stats_to_stats64);
  7127. /**
  7128. * dev_get_stats - get network device statistics
  7129. * @dev: device to get statistics from
  7130. * @storage: place to store stats
  7131. *
  7132. * Get network statistics from device. Return @storage.
  7133. * The device driver may provide its own method by setting
  7134. * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
  7135. * otherwise the internal statistics structure is used.
  7136. */
  7137. struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
  7138. struct rtnl_link_stats64 *storage)
  7139. {
  7140. const struct net_device_ops *ops = dev->netdev_ops;
  7141. if (ops->ndo_get_stats64) {
  7142. memset(storage, 0, sizeof(*storage));
  7143. ops->ndo_get_stats64(dev, storage);
  7144. } else if (ops->ndo_get_stats) {
  7145. netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
  7146. } else {
  7147. netdev_stats_to_stats64(storage, &dev->stats);
  7148. }
  7149. storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
  7150. storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
  7151. storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
  7152. return storage;
  7153. }
  7154. EXPORT_SYMBOL(dev_get_stats);
  7155. struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
  7156. {
  7157. struct netdev_queue *queue = dev_ingress_queue(dev);
  7158. #ifdef CONFIG_NET_CLS_ACT
  7159. if (queue)
  7160. return queue;
  7161. queue = kzalloc(sizeof(*queue), GFP_KERNEL);
  7162. if (!queue)
  7163. return NULL;
  7164. netdev_init_one_queue(dev, queue, NULL);
  7165. RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
  7166. queue->qdisc_sleeping = &noop_qdisc;
  7167. rcu_assign_pointer(dev->ingress_queue, queue);
  7168. #endif
  7169. return queue;
  7170. }
  7171. static const struct ethtool_ops default_ethtool_ops;
  7172. void netdev_set_default_ethtool_ops(struct net_device *dev,
  7173. const struct ethtool_ops *ops)
  7174. {
  7175. if (dev->ethtool_ops == &default_ethtool_ops)
  7176. dev->ethtool_ops = ops;
  7177. }
  7178. EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
  7179. void netdev_freemem(struct net_device *dev)
  7180. {
  7181. char *addr = (char *)dev - dev->padded;
  7182. kvfree(addr);
  7183. }
  7184. /**
  7185. * alloc_netdev_mqs - allocate network device
  7186. * @sizeof_priv: size of private data to allocate space for
  7187. * @name: device name format string
  7188. * @name_assign_type: origin of device name
  7189. * @setup: callback to initialize device
  7190. * @txqs: the number of TX subqueues to allocate
  7191. * @rxqs: the number of RX subqueues to allocate
  7192. *
  7193. * Allocates a struct net_device with private data area for driver use
  7194. * and performs basic initialization. Also allocates subqueue structs
  7195. * for each queue on the device.
  7196. */
  7197. struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
  7198. unsigned char name_assign_type,
  7199. void (*setup)(struct net_device *),
  7200. unsigned int txqs, unsigned int rxqs)
  7201. {
  7202. struct net_device *dev;
  7203. unsigned int alloc_size;
  7204. struct net_device *p;
  7205. BUG_ON(strlen(name) >= sizeof(dev->name));
  7206. if (txqs < 1) {
  7207. pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
  7208. return NULL;
  7209. }
  7210. if (rxqs < 1) {
  7211. pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
  7212. return NULL;
  7213. }
  7214. alloc_size = sizeof(struct net_device);
  7215. if (sizeof_priv) {
  7216. /* ensure 32-byte alignment of private area */
  7217. alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
  7218. alloc_size += sizeof_priv;
  7219. }
  7220. /* ensure 32-byte alignment of whole construct */
  7221. alloc_size += NETDEV_ALIGN - 1;
  7222. p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  7223. if (!p)
  7224. return NULL;
  7225. dev = PTR_ALIGN(p, NETDEV_ALIGN);
  7226. dev->padded = (char *)dev - (char *)p;
  7227. dev->pcpu_refcnt = alloc_percpu(int);
  7228. if (!dev->pcpu_refcnt)
  7229. goto free_dev;
  7230. if (dev_addr_init(dev))
  7231. goto free_pcpu;
  7232. dev_mc_init(dev);
  7233. dev_uc_init(dev);
  7234. dev_net_set(dev, &init_net);
  7235. dev->gso_max_size = GSO_MAX_SIZE;
  7236. dev->gso_max_segs = GSO_MAX_SEGS;
  7237. INIT_LIST_HEAD(&dev->napi_list);
  7238. INIT_LIST_HEAD(&dev->unreg_list);
  7239. INIT_LIST_HEAD(&dev->close_list);
  7240. INIT_LIST_HEAD(&dev->link_watch_list);
  7241. INIT_LIST_HEAD(&dev->adj_list.upper);
  7242. INIT_LIST_HEAD(&dev->adj_list.lower);
  7243. INIT_LIST_HEAD(&dev->ptype_all);
  7244. INIT_LIST_HEAD(&dev->ptype_specific);
  7245. #ifdef CONFIG_NET_SCHED
  7246. hash_init(dev->qdisc_hash);
  7247. #endif
  7248. dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
  7249. setup(dev);
  7250. if (!dev->tx_queue_len) {
  7251. dev->priv_flags |= IFF_NO_QUEUE;
  7252. dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
  7253. }
  7254. dev->num_tx_queues = txqs;
  7255. dev->real_num_tx_queues = txqs;
  7256. if (netif_alloc_netdev_queues(dev))
  7257. goto free_all;
  7258. dev->num_rx_queues = rxqs;
  7259. dev->real_num_rx_queues = rxqs;
  7260. if (netif_alloc_rx_queues(dev))
  7261. goto free_all;
  7262. strcpy(dev->name, name);
  7263. dev->name_assign_type = name_assign_type;
  7264. dev->group = INIT_NETDEV_GROUP;
  7265. if (!dev->ethtool_ops)
  7266. dev->ethtool_ops = &default_ethtool_ops;
  7267. nf_hook_ingress_init(dev);
  7268. return dev;
  7269. free_all:
  7270. free_netdev(dev);
  7271. return NULL;
  7272. free_pcpu:
  7273. free_percpu(dev->pcpu_refcnt);
  7274. free_dev:
  7275. netdev_freemem(dev);
  7276. return NULL;
  7277. }
  7278. EXPORT_SYMBOL(alloc_netdev_mqs);
  7279. /**
  7280. * free_netdev - free network device
  7281. * @dev: device
  7282. *
  7283. * This function does the last stage of destroying an allocated device
  7284. * interface. The reference to the device object is released. If this
  7285. * is the last reference then it will be freed.Must be called in process
  7286. * context.
  7287. */
  7288. void free_netdev(struct net_device *dev)
  7289. {
  7290. struct napi_struct *p, *n;
  7291. might_sleep();
  7292. netif_free_tx_queues(dev);
  7293. netif_free_rx_queues(dev);
  7294. kfree(rcu_dereference_protected(dev->ingress_queue, 1));
  7295. /* Flush device addresses */
  7296. dev_addr_flush(dev);
  7297. list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
  7298. netif_napi_del(p);
  7299. free_percpu(dev->pcpu_refcnt);
  7300. dev->pcpu_refcnt = NULL;
  7301. /* Compatibility with error handling in drivers */
  7302. if (dev->reg_state == NETREG_UNINITIALIZED) {
  7303. netdev_freemem(dev);
  7304. return;
  7305. }
  7306. BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
  7307. dev->reg_state = NETREG_RELEASED;
  7308. /* will free via device release */
  7309. put_device(&dev->dev);
  7310. }
  7311. EXPORT_SYMBOL(free_netdev);
  7312. /**
  7313. * synchronize_net - Synchronize with packet receive processing
  7314. *
  7315. * Wait for packets currently being received to be done.
  7316. * Does not block later packets from starting.
  7317. */
  7318. void synchronize_net(void)
  7319. {
  7320. might_sleep();
  7321. if (rtnl_is_locked())
  7322. synchronize_rcu_expedited();
  7323. else
  7324. synchronize_rcu();
  7325. }
  7326. EXPORT_SYMBOL(synchronize_net);
  7327. /**
  7328. * unregister_netdevice_queue - remove device from the kernel
  7329. * @dev: device
  7330. * @head: list
  7331. *
  7332. * This function shuts down a device interface and removes it
  7333. * from the kernel tables.
  7334. * If head not NULL, device is queued to be unregistered later.
  7335. *
  7336. * Callers must hold the rtnl semaphore. You may want
  7337. * unregister_netdev() instead of this.
  7338. */
  7339. void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
  7340. {
  7341. ASSERT_RTNL();
  7342. if (head) {
  7343. list_move_tail(&dev->unreg_list, head);
  7344. } else {
  7345. rollback_registered(dev);
  7346. /* Finish processing unregister after unlock */
  7347. net_set_todo(dev);
  7348. }
  7349. }
  7350. EXPORT_SYMBOL(unregister_netdevice_queue);
  7351. /**
  7352. * unregister_netdevice_many - unregister many devices
  7353. * @head: list of devices
  7354. *
  7355. * Note: As most callers use a stack allocated list_head,
  7356. * we force a list_del() to make sure stack wont be corrupted later.
  7357. */
  7358. void unregister_netdevice_many(struct list_head *head)
  7359. {
  7360. struct net_device *dev;
  7361. if (!list_empty(head)) {
  7362. rollback_registered_many(head);
  7363. list_for_each_entry(dev, head, unreg_list)
  7364. net_set_todo(dev);
  7365. list_del(head);
  7366. }
  7367. }
  7368. EXPORT_SYMBOL(unregister_netdevice_many);
  7369. /**
  7370. * unregister_netdev - remove device from the kernel
  7371. * @dev: device
  7372. *
  7373. * This function shuts down a device interface and removes it
  7374. * from the kernel tables.
  7375. *
  7376. * This is just a wrapper for unregister_netdevice that takes
  7377. * the rtnl semaphore. In general you want to use this and not
  7378. * unregister_netdevice.
  7379. */
  7380. void unregister_netdev(struct net_device *dev)
  7381. {
  7382. rtnl_lock();
  7383. unregister_netdevice(dev);
  7384. rtnl_unlock();
  7385. }
  7386. EXPORT_SYMBOL(unregister_netdev);
  7387. /**
  7388. * dev_change_net_namespace - move device to different nethost namespace
  7389. * @dev: device
  7390. * @net: network namespace
  7391. * @pat: If not NULL name pattern to try if the current device name
  7392. * is already taken in the destination network namespace.
  7393. *
  7394. * This function shuts down a device interface and moves it
  7395. * to a new network namespace. On success 0 is returned, on
  7396. * a failure a netagive errno code is returned.
  7397. *
  7398. * Callers must hold the rtnl semaphore.
  7399. */
  7400. int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
  7401. {
  7402. int err, new_nsid, new_ifindex;
  7403. ASSERT_RTNL();
  7404. /* Don't allow namespace local devices to be moved. */
  7405. err = -EINVAL;
  7406. if (dev->features & NETIF_F_NETNS_LOCAL)
  7407. goto out;
  7408. /* Ensure the device has been registrered */
  7409. if (dev->reg_state != NETREG_REGISTERED)
  7410. goto out;
  7411. /* Get out if there is nothing todo */
  7412. err = 0;
  7413. if (net_eq(dev_net(dev), net))
  7414. goto out;
  7415. /* Pick the destination device name, and ensure
  7416. * we can use it in the destination network namespace.
  7417. */
  7418. err = -EEXIST;
  7419. if (__dev_get_by_name(net, dev->name)) {
  7420. /* We get here if we can't use the current device name */
  7421. if (!pat)
  7422. goto out;
  7423. err = dev_get_valid_name(net, dev, pat);
  7424. if (err < 0)
  7425. goto out;
  7426. }
  7427. /*
  7428. * And now a mini version of register_netdevice unregister_netdevice.
  7429. */
  7430. /* If device is running close it first. */
  7431. dev_close(dev);
  7432. /* And unlink it from device chain */
  7433. unlist_netdevice(dev);
  7434. synchronize_net();
  7435. /* Shutdown queueing discipline. */
  7436. dev_shutdown(dev);
  7437. /* Notify protocols, that we are about to destroy
  7438. * this device. They should clean all the things.
  7439. *
  7440. * Note that dev->reg_state stays at NETREG_REGISTERED.
  7441. * This is wanted because this way 8021q and macvlan know
  7442. * the device is just moving and can keep their slaves up.
  7443. */
  7444. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  7445. rcu_barrier();
  7446. new_nsid = peernet2id_alloc(dev_net(dev), net);
  7447. /* If there is an ifindex conflict assign a new one */
  7448. if (__dev_get_by_index(net, dev->ifindex))
  7449. new_ifindex = dev_new_index(net);
  7450. else
  7451. new_ifindex = dev->ifindex;
  7452. rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
  7453. new_ifindex);
  7454. /*
  7455. * Flush the unicast and multicast chains
  7456. */
  7457. dev_uc_flush(dev);
  7458. dev_mc_flush(dev);
  7459. /* Send a netdev-removed uevent to the old namespace */
  7460. kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
  7461. netdev_adjacent_del_links(dev);
  7462. /* Actually switch the network namespace */
  7463. dev_net_set(dev, net);
  7464. dev->ifindex = new_ifindex;
  7465. /* Send a netdev-add uevent to the new namespace */
  7466. kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
  7467. netdev_adjacent_add_links(dev);
  7468. /* Fixup kobjects */
  7469. err = device_rename(&dev->dev, dev->name);
  7470. WARN_ON(err);
  7471. /* Add the device back in the hashes */
  7472. list_netdevice(dev);
  7473. /* Notify protocols, that a new device appeared. */
  7474. call_netdevice_notifiers(NETDEV_REGISTER, dev);
  7475. /*
  7476. * Prevent userspace races by waiting until the network
  7477. * device is fully setup before sending notifications.
  7478. */
  7479. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
  7480. synchronize_net();
  7481. err = 0;
  7482. out:
  7483. return err;
  7484. }
  7485. EXPORT_SYMBOL_GPL(dev_change_net_namespace);
  7486. static int dev_cpu_dead(unsigned int oldcpu)
  7487. {
  7488. struct sk_buff **list_skb;
  7489. struct sk_buff *skb;
  7490. unsigned int cpu;
  7491. struct softnet_data *sd, *oldsd, *remsd = NULL;
  7492. local_irq_disable();
  7493. cpu = smp_processor_id();
  7494. sd = &per_cpu(softnet_data, cpu);
  7495. oldsd = &per_cpu(softnet_data, oldcpu);
  7496. /* Find end of our completion_queue. */
  7497. list_skb = &sd->completion_queue;
  7498. while (*list_skb)
  7499. list_skb = &(*list_skb)->next;
  7500. /* Append completion queue from offline CPU. */
  7501. *list_skb = oldsd->completion_queue;
  7502. oldsd->completion_queue = NULL;
  7503. /* Append output queue from offline CPU. */
  7504. if (oldsd->output_queue) {
  7505. *sd->output_queue_tailp = oldsd->output_queue;
  7506. sd->output_queue_tailp = oldsd->output_queue_tailp;
  7507. oldsd->output_queue = NULL;
  7508. oldsd->output_queue_tailp = &oldsd->output_queue;
  7509. }
  7510. /* Append NAPI poll list from offline CPU, with one exception :
  7511. * process_backlog() must be called by cpu owning percpu backlog.
  7512. * We properly handle process_queue & input_pkt_queue later.
  7513. */
  7514. while (!list_empty(&oldsd->poll_list)) {
  7515. struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
  7516. struct napi_struct,
  7517. poll_list);
  7518. list_del_init(&napi->poll_list);
  7519. if (napi->poll == process_backlog)
  7520. napi->state = 0;
  7521. else
  7522. ____napi_schedule(sd, napi);
  7523. }
  7524. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  7525. local_irq_enable();
  7526. #ifdef CONFIG_RPS
  7527. remsd = oldsd->rps_ipi_list;
  7528. oldsd->rps_ipi_list = NULL;
  7529. #endif
  7530. /* send out pending IPI's on offline CPU */
  7531. net_rps_send_ipi(remsd);
  7532. /* Process offline CPU's input_pkt_queue */
  7533. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  7534. netif_rx_ni(skb);
  7535. input_queue_head_incr(oldsd);
  7536. }
  7537. while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  7538. netif_rx_ni(skb);
  7539. input_queue_head_incr(oldsd);
  7540. }
  7541. return 0;
  7542. }
  7543. /**
  7544. * netdev_increment_features - increment feature set by one
  7545. * @all: current feature set
  7546. * @one: new feature set
  7547. * @mask: mask feature set
  7548. *
  7549. * Computes a new feature set after adding a device with feature set
  7550. * @one to the master device with current feature set @all. Will not
  7551. * enable anything that is off in @mask. Returns the new feature set.
  7552. */
  7553. netdev_features_t netdev_increment_features(netdev_features_t all,
  7554. netdev_features_t one, netdev_features_t mask)
  7555. {
  7556. if (mask & NETIF_F_HW_CSUM)
  7557. mask |= NETIF_F_CSUM_MASK;
  7558. mask |= NETIF_F_VLAN_CHALLENGED;
  7559. all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
  7560. all &= one | ~NETIF_F_ALL_FOR_ALL;
  7561. /* If one device supports hw checksumming, set for all. */
  7562. if (all & NETIF_F_HW_CSUM)
  7563. all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
  7564. return all;
  7565. }
  7566. EXPORT_SYMBOL(netdev_increment_features);
  7567. static struct hlist_head * __net_init netdev_create_hash(void)
  7568. {
  7569. int i;
  7570. struct hlist_head *hash;
  7571. hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
  7572. if (hash != NULL)
  7573. for (i = 0; i < NETDEV_HASHENTRIES; i++)
  7574. INIT_HLIST_HEAD(&hash[i]);
  7575. return hash;
  7576. }
  7577. /* Initialize per network namespace state */
  7578. static int __net_init netdev_init(struct net *net)
  7579. {
  7580. if (net != &init_net)
  7581. INIT_LIST_HEAD(&net->dev_base_head);
  7582. net->dev_name_head = netdev_create_hash();
  7583. if (net->dev_name_head == NULL)
  7584. goto err_name;
  7585. net->dev_index_head = netdev_create_hash();
  7586. if (net->dev_index_head == NULL)
  7587. goto err_idx;
  7588. return 0;
  7589. err_idx:
  7590. kfree(net->dev_name_head);
  7591. err_name:
  7592. return -ENOMEM;
  7593. }
  7594. /**
  7595. * netdev_drivername - network driver for the device
  7596. * @dev: network device
  7597. *
  7598. * Determine network driver for device.
  7599. */
  7600. const char *netdev_drivername(const struct net_device *dev)
  7601. {
  7602. const struct device_driver *driver;
  7603. const struct device *parent;
  7604. const char *empty = "";
  7605. parent = dev->dev.parent;
  7606. if (!parent)
  7607. return empty;
  7608. driver = parent->driver;
  7609. if (driver && driver->name)
  7610. return driver->name;
  7611. return empty;
  7612. }
  7613. static void __netdev_printk(const char *level, const struct net_device *dev,
  7614. struct va_format *vaf)
  7615. {
  7616. if (dev && dev->dev.parent) {
  7617. dev_printk_emit(level[1] - '0',
  7618. dev->dev.parent,
  7619. "%s %s %s%s: %pV",
  7620. dev_driver_string(dev->dev.parent),
  7621. dev_name(dev->dev.parent),
  7622. netdev_name(dev), netdev_reg_state(dev),
  7623. vaf);
  7624. } else if (dev) {
  7625. printk("%s%s%s: %pV",
  7626. level, netdev_name(dev), netdev_reg_state(dev), vaf);
  7627. } else {
  7628. printk("%s(NULL net_device): %pV", level, vaf);
  7629. }
  7630. }
  7631. void netdev_printk(const char *level, const struct net_device *dev,
  7632. const char *format, ...)
  7633. {
  7634. struct va_format vaf;
  7635. va_list args;
  7636. va_start(args, format);
  7637. vaf.fmt = format;
  7638. vaf.va = &args;
  7639. __netdev_printk(level, dev, &vaf);
  7640. va_end(args);
  7641. }
  7642. EXPORT_SYMBOL(netdev_printk);
  7643. #define define_netdev_printk_level(func, level) \
  7644. void func(const struct net_device *dev, const char *fmt, ...) \
  7645. { \
  7646. struct va_format vaf; \
  7647. va_list args; \
  7648. \
  7649. va_start(args, fmt); \
  7650. \
  7651. vaf.fmt = fmt; \
  7652. vaf.va = &args; \
  7653. \
  7654. __netdev_printk(level, dev, &vaf); \
  7655. \
  7656. va_end(args); \
  7657. } \
  7658. EXPORT_SYMBOL(func);
  7659. define_netdev_printk_level(netdev_emerg, KERN_EMERG);
  7660. define_netdev_printk_level(netdev_alert, KERN_ALERT);
  7661. define_netdev_printk_level(netdev_crit, KERN_CRIT);
  7662. define_netdev_printk_level(netdev_err, KERN_ERR);
  7663. define_netdev_printk_level(netdev_warn, KERN_WARNING);
  7664. define_netdev_printk_level(netdev_notice, KERN_NOTICE);
  7665. define_netdev_printk_level(netdev_info, KERN_INFO);
  7666. static void __net_exit netdev_exit(struct net *net)
  7667. {
  7668. kfree(net->dev_name_head);
  7669. kfree(net->dev_index_head);
  7670. if (net != &init_net)
  7671. WARN_ON_ONCE(!list_empty(&net->dev_base_head));
  7672. }
  7673. static struct pernet_operations __net_initdata netdev_net_ops = {
  7674. .init = netdev_init,
  7675. .exit = netdev_exit,
  7676. };
  7677. static void __net_exit default_device_exit(struct net *net)
  7678. {
  7679. struct net_device *dev, *aux;
  7680. /*
  7681. * Push all migratable network devices back to the
  7682. * initial network namespace
  7683. */
  7684. rtnl_lock();
  7685. for_each_netdev_safe(net, dev, aux) {
  7686. int err;
  7687. char fb_name[IFNAMSIZ];
  7688. /* Ignore unmoveable devices (i.e. loopback) */
  7689. if (dev->features & NETIF_F_NETNS_LOCAL)
  7690. continue;
  7691. /* Leave virtual devices for the generic cleanup */
  7692. if (dev->rtnl_link_ops)
  7693. continue;
  7694. /* Push remaining network devices to init_net */
  7695. snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
  7696. err = dev_change_net_namespace(dev, &init_net, fb_name);
  7697. if (err) {
  7698. pr_emerg("%s: failed to move %s to init_net: %d\n",
  7699. __func__, dev->name, err);
  7700. BUG();
  7701. }
  7702. }
  7703. rtnl_unlock();
  7704. }
  7705. static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
  7706. {
  7707. /* Return with the rtnl_lock held when there are no network
  7708. * devices unregistering in any network namespace in net_list.
  7709. */
  7710. struct net *net;
  7711. bool unregistering;
  7712. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  7713. add_wait_queue(&netdev_unregistering_wq, &wait);
  7714. for (;;) {
  7715. unregistering = false;
  7716. rtnl_lock();
  7717. list_for_each_entry(net, net_list, exit_list) {
  7718. if (net->dev_unreg_count > 0) {
  7719. unregistering = true;
  7720. break;
  7721. }
  7722. }
  7723. if (!unregistering)
  7724. break;
  7725. __rtnl_unlock();
  7726. wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  7727. }
  7728. remove_wait_queue(&netdev_unregistering_wq, &wait);
  7729. }
  7730. static void __net_exit default_device_exit_batch(struct list_head *net_list)
  7731. {
  7732. /* At exit all network devices most be removed from a network
  7733. * namespace. Do this in the reverse order of registration.
  7734. * Do this across as many network namespaces as possible to
  7735. * improve batching efficiency.
  7736. */
  7737. struct net_device *dev;
  7738. struct net *net;
  7739. LIST_HEAD(dev_kill_list);
  7740. /* To prevent network device cleanup code from dereferencing
  7741. * loopback devices or network devices that have been freed
  7742. * wait here for all pending unregistrations to complete,
  7743. * before unregistring the loopback device and allowing the
  7744. * network namespace be freed.
  7745. *
  7746. * The netdev todo list containing all network devices
  7747. * unregistrations that happen in default_device_exit_batch
  7748. * will run in the rtnl_unlock() at the end of
  7749. * default_device_exit_batch.
  7750. */
  7751. rtnl_lock_unregistering(net_list);
  7752. list_for_each_entry(net, net_list, exit_list) {
  7753. for_each_netdev_reverse(net, dev) {
  7754. if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
  7755. dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
  7756. else
  7757. unregister_netdevice_queue(dev, &dev_kill_list);
  7758. }
  7759. }
  7760. unregister_netdevice_many(&dev_kill_list);
  7761. rtnl_unlock();
  7762. }
  7763. static struct pernet_operations __net_initdata default_device_ops = {
  7764. .exit = default_device_exit,
  7765. .exit_batch = default_device_exit_batch,
  7766. };
  7767. /*
  7768. * Initialize the DEV module. At boot time this walks the device list and
  7769. * unhooks any devices that fail to initialise (normally hardware not
  7770. * present) and leaves us with a valid list of present and active devices.
  7771. *
  7772. */
  7773. /*
  7774. * This is called single threaded during boot, so no need
  7775. * to take the rtnl semaphore.
  7776. */
  7777. static int __init net_dev_init(void)
  7778. {
  7779. int i, rc = -ENOMEM;
  7780. BUG_ON(!dev_boot_phase);
  7781. if (dev_proc_init())
  7782. goto out;
  7783. if (netdev_kobject_init())
  7784. goto out;
  7785. INIT_LIST_HEAD(&ptype_all);
  7786. for (i = 0; i < PTYPE_HASH_SIZE; i++)
  7787. INIT_LIST_HEAD(&ptype_base[i]);
  7788. INIT_LIST_HEAD(&offload_base);
  7789. if (register_pernet_subsys(&netdev_net_ops))
  7790. goto out;
  7791. /*
  7792. * Initialise the packet receive queues.
  7793. */
  7794. for_each_possible_cpu(i) {
  7795. struct work_struct *flush = per_cpu_ptr(&flush_works, i);
  7796. struct softnet_data *sd = &per_cpu(softnet_data, i);
  7797. INIT_WORK(flush, flush_backlog);
  7798. skb_queue_head_init(&sd->input_pkt_queue);
  7799. skb_queue_head_init(&sd->process_queue);
  7800. #ifdef CONFIG_XFRM_OFFLOAD
  7801. skb_queue_head_init(&sd->xfrm_backlog);
  7802. #endif
  7803. INIT_LIST_HEAD(&sd->poll_list);
  7804. sd->output_queue_tailp = &sd->output_queue;
  7805. #ifdef CONFIG_RPS
  7806. sd->csd.func = rps_trigger_softirq;
  7807. sd->csd.info = sd;
  7808. sd->cpu = i;
  7809. #endif
  7810. sd->backlog.poll = process_backlog;
  7811. sd->backlog.weight = weight_p;
  7812. }
  7813. dev_boot_phase = 0;
  7814. /* The loopback device is special if any other network devices
  7815. * is present in a network namespace the loopback device must
  7816. * be present. Since we now dynamically allocate and free the
  7817. * loopback device ensure this invariant is maintained by
  7818. * keeping the loopback device as the first device on the
  7819. * list of network devices. Ensuring the loopback devices
  7820. * is the first device that appears and the last network device
  7821. * that disappears.
  7822. */
  7823. if (register_pernet_device(&loopback_net_ops))
  7824. goto out;
  7825. if (register_pernet_device(&default_device_ops))
  7826. goto out;
  7827. open_softirq(NET_TX_SOFTIRQ, net_tx_action);
  7828. open_softirq(NET_RX_SOFTIRQ, net_rx_action);
  7829. rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
  7830. NULL, dev_cpu_dead);
  7831. WARN_ON(rc < 0);
  7832. rc = 0;
  7833. out:
  7834. return rc;
  7835. }
  7836. subsys_initcall(net_dev_init);