dev.c 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336
  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 <asm/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/mutex.h>
  84. #include <linux/string.h>
  85. #include <linux/mm.h>
  86. #include <linux/socket.h>
  87. #include <linux/sockios.h>
  88. #include <linux/errno.h>
  89. #include <linux/interrupt.h>
  90. #include <linux/if_ether.h>
  91. #include <linux/netdevice.h>
  92. #include <linux/etherdevice.h>
  93. #include <linux/ethtool.h>
  94. #include <linux/notifier.h>
  95. #include <linux/skbuff.h>
  96. #include <net/net_namespace.h>
  97. #include <net/sock.h>
  98. #include <linux/rtnetlink.h>
  99. #include <linux/stat.h>
  100. #include <net/dst.h>
  101. #include <net/pkt_sched.h>
  102. #include <net/checksum.h>
  103. #include <net/xfrm.h>
  104. #include <linux/highmem.h>
  105. #include <linux/init.h>
  106. #include <linux/module.h>
  107. #include <linux/netpoll.h>
  108. #include <linux/rcupdate.h>
  109. #include <linux/delay.h>
  110. #include <net/iw_handler.h>
  111. #include <asm/current.h>
  112. #include <linux/audit.h>
  113. #include <linux/dmaengine.h>
  114. #include <linux/err.h>
  115. #include <linux/ctype.h>
  116. #include <linux/if_arp.h>
  117. #include <linux/if_vlan.h>
  118. #include <linux/ip.h>
  119. #include <net/ip.h>
  120. #include <linux/ipv6.h>
  121. #include <linux/in.h>
  122. #include <linux/jhash.h>
  123. #include <linux/random.h>
  124. #include <trace/events/napi.h>
  125. #include <trace/events/net.h>
  126. #include <trace/events/skb.h>
  127. #include <linux/pci.h>
  128. #include <linux/inetdevice.h>
  129. #include <linux/cpu_rmap.h>
  130. #include <linux/static_key.h>
  131. #include "net-sysfs.h"
  132. /* Instead of increasing this, you should create a hash table. */
  133. #define MAX_GRO_SKBS 8
  134. /* This should be increased if a protocol with a bigger head is added. */
  135. #define GRO_MAX_HEAD (MAX_HEADER + 128)
  136. static DEFINE_SPINLOCK(ptype_lock);
  137. static DEFINE_SPINLOCK(offload_lock);
  138. struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
  139. struct list_head ptype_all __read_mostly; /* Taps */
  140. static struct list_head offload_base __read_mostly;
  141. /*
  142. * The @dev_base_head list is protected by @dev_base_lock and the rtnl
  143. * semaphore.
  144. *
  145. * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
  146. *
  147. * Writers must hold the rtnl semaphore while they loop through the
  148. * dev_base_head list, and hold dev_base_lock for writing when they do the
  149. * actual updates. This allows pure readers to access the list even
  150. * while a writer is preparing to update it.
  151. *
  152. * To put it another way, dev_base_lock is held for writing only to
  153. * protect against pure readers; the rtnl semaphore provides the
  154. * protection against other writers.
  155. *
  156. * See, for example usages, register_netdevice() and
  157. * unregister_netdevice(), which must be called with the rtnl
  158. * semaphore held.
  159. */
  160. DEFINE_RWLOCK(dev_base_lock);
  161. EXPORT_SYMBOL(dev_base_lock);
  162. seqcount_t devnet_rename_seq;
  163. static inline void dev_base_seq_inc(struct net *net)
  164. {
  165. while (++net->dev_base_seq == 0);
  166. }
  167. static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
  168. {
  169. unsigned int hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
  170. return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
  171. }
  172. static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
  173. {
  174. return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
  175. }
  176. static inline void rps_lock(struct softnet_data *sd)
  177. {
  178. #ifdef CONFIG_RPS
  179. spin_lock(&sd->input_pkt_queue.lock);
  180. #endif
  181. }
  182. static inline void rps_unlock(struct softnet_data *sd)
  183. {
  184. #ifdef CONFIG_RPS
  185. spin_unlock(&sd->input_pkt_queue.lock);
  186. #endif
  187. }
  188. /* Device list insertion */
  189. static void list_netdevice(struct net_device *dev)
  190. {
  191. struct net *net = dev_net(dev);
  192. ASSERT_RTNL();
  193. write_lock_bh(&dev_base_lock);
  194. list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
  195. hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
  196. hlist_add_head_rcu(&dev->index_hlist,
  197. dev_index_hash(net, dev->ifindex));
  198. write_unlock_bh(&dev_base_lock);
  199. dev_base_seq_inc(net);
  200. }
  201. /* Device list removal
  202. * caller must respect a RCU grace period before freeing/reusing dev
  203. */
  204. static void unlist_netdevice(struct net_device *dev)
  205. {
  206. ASSERT_RTNL();
  207. /* Unlink dev from the device chain */
  208. write_lock_bh(&dev_base_lock);
  209. list_del_rcu(&dev->dev_list);
  210. hlist_del_rcu(&dev->name_hlist);
  211. hlist_del_rcu(&dev->index_hlist);
  212. write_unlock_bh(&dev_base_lock);
  213. dev_base_seq_inc(dev_net(dev));
  214. }
  215. /*
  216. * Our notifier list
  217. */
  218. static RAW_NOTIFIER_HEAD(netdev_chain);
  219. /*
  220. * Device drivers call our routines to queue packets here. We empty the
  221. * queue in the local softnet handler.
  222. */
  223. DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
  224. EXPORT_PER_CPU_SYMBOL(softnet_data);
  225. #ifdef CONFIG_LOCKDEP
  226. /*
  227. * register_netdevice() inits txq->_xmit_lock and sets lockdep class
  228. * according to dev->type
  229. */
  230. static const unsigned short netdev_lock_type[] =
  231. {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
  232. ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
  233. ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
  234. ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
  235. ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
  236. ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
  237. ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
  238. ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
  239. ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
  240. ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
  241. ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
  242. ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
  243. ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
  244. ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
  245. ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
  246. static const char *const netdev_lock_name[] =
  247. {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
  248. "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
  249. "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
  250. "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
  251. "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
  252. "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
  253. "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
  254. "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
  255. "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
  256. "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
  257. "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
  258. "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
  259. "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
  260. "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
  261. "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
  262. static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
  263. static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
  264. static inline unsigned short netdev_lock_pos(unsigned short dev_type)
  265. {
  266. int i;
  267. for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
  268. if (netdev_lock_type[i] == dev_type)
  269. return i;
  270. /* the last key is used by default */
  271. return ARRAY_SIZE(netdev_lock_type) - 1;
  272. }
  273. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  274. unsigned short dev_type)
  275. {
  276. int i;
  277. i = netdev_lock_pos(dev_type);
  278. lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
  279. netdev_lock_name[i]);
  280. }
  281. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  282. {
  283. int i;
  284. i = netdev_lock_pos(dev->type);
  285. lockdep_set_class_and_name(&dev->addr_list_lock,
  286. &netdev_addr_lock_key[i],
  287. netdev_lock_name[i]);
  288. }
  289. #else
  290. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  291. unsigned short dev_type)
  292. {
  293. }
  294. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  295. {
  296. }
  297. #endif
  298. /*******************************************************************************
  299. Protocol management and registration routines
  300. *******************************************************************************/
  301. /*
  302. * Add a protocol ID to the list. Now that the input handler is
  303. * smarter we can dispense with all the messy stuff that used to be
  304. * here.
  305. *
  306. * BEWARE!!! Protocol handlers, mangling input packets,
  307. * MUST BE last in hash buckets and checking protocol handlers
  308. * MUST start from promiscuous ptype_all chain in net_bh.
  309. * It is true now, do not change it.
  310. * Explanation follows: if protocol handler, mangling packet, will
  311. * be the first on list, it is not able to sense, that packet
  312. * is cloned and should be copied-on-write, so that it will
  313. * change it and subsequent readers will get broken packet.
  314. * --ANK (980803)
  315. */
  316. static inline struct list_head *ptype_head(const struct packet_type *pt)
  317. {
  318. if (pt->type == htons(ETH_P_ALL))
  319. return &ptype_all;
  320. else
  321. return &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
  322. }
  323. /**
  324. * dev_add_pack - add packet handler
  325. * @pt: packet type declaration
  326. *
  327. * Add a protocol handler to the networking stack. The passed &packet_type
  328. * is linked into kernel lists and may not be freed until it has been
  329. * removed from the kernel lists.
  330. *
  331. * This call does not sleep therefore it can not
  332. * guarantee all CPU's that are in middle of receiving packets
  333. * will see the new packet type (until the next received packet).
  334. */
  335. void dev_add_pack(struct packet_type *pt)
  336. {
  337. struct list_head *head = ptype_head(pt);
  338. spin_lock(&ptype_lock);
  339. list_add_rcu(&pt->list, head);
  340. spin_unlock(&ptype_lock);
  341. }
  342. EXPORT_SYMBOL(dev_add_pack);
  343. /**
  344. * __dev_remove_pack - remove packet handler
  345. * @pt: packet type declaration
  346. *
  347. * Remove a protocol handler that was previously added to the kernel
  348. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  349. * from the kernel lists and can be freed or reused once this function
  350. * returns.
  351. *
  352. * The packet type might still be in use by receivers
  353. * and must not be freed until after all the CPU's have gone
  354. * through a quiescent state.
  355. */
  356. void __dev_remove_pack(struct packet_type *pt)
  357. {
  358. struct list_head *head = ptype_head(pt);
  359. struct packet_type *pt1;
  360. spin_lock(&ptype_lock);
  361. list_for_each_entry(pt1, head, list) {
  362. if (pt == pt1) {
  363. list_del_rcu(&pt->list);
  364. goto out;
  365. }
  366. }
  367. pr_warn("dev_remove_pack: %p not found\n", pt);
  368. out:
  369. spin_unlock(&ptype_lock);
  370. }
  371. EXPORT_SYMBOL(__dev_remove_pack);
  372. /**
  373. * dev_remove_pack - remove packet handler
  374. * @pt: packet type declaration
  375. *
  376. * Remove a protocol handler that was previously added to the kernel
  377. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  378. * from the kernel lists and can be freed or reused once this function
  379. * returns.
  380. *
  381. * This call sleeps to guarantee that no CPU is looking at the packet
  382. * type after return.
  383. */
  384. void dev_remove_pack(struct packet_type *pt)
  385. {
  386. __dev_remove_pack(pt);
  387. synchronize_net();
  388. }
  389. EXPORT_SYMBOL(dev_remove_pack);
  390. /**
  391. * dev_add_offload - register offload handlers
  392. * @po: protocol offload declaration
  393. *
  394. * Add protocol offload handlers to the networking stack. The passed
  395. * &proto_offload is linked into kernel lists and may not be freed until
  396. * it has been removed from the kernel lists.
  397. *
  398. * This call does not sleep therefore it can not
  399. * guarantee all CPU's that are in middle of receiving packets
  400. * will see the new offload handlers (until the next received packet).
  401. */
  402. void dev_add_offload(struct packet_offload *po)
  403. {
  404. struct list_head *head = &offload_base;
  405. spin_lock(&offload_lock);
  406. list_add_rcu(&po->list, head);
  407. spin_unlock(&offload_lock);
  408. }
  409. EXPORT_SYMBOL(dev_add_offload);
  410. /**
  411. * __dev_remove_offload - remove offload handler
  412. * @po: packet offload declaration
  413. *
  414. * Remove a protocol offload handler that was previously added to the
  415. * kernel offload handlers by dev_add_offload(). The passed &offload_type
  416. * is removed from the kernel lists and can be freed or reused once this
  417. * function returns.
  418. *
  419. * The packet type might still be in use by receivers
  420. * and must not be freed until after all the CPU's have gone
  421. * through a quiescent state.
  422. */
  423. void __dev_remove_offload(struct packet_offload *po)
  424. {
  425. struct list_head *head = &offload_base;
  426. struct packet_offload *po1;
  427. spin_lock(&offload_lock);
  428. list_for_each_entry(po1, head, list) {
  429. if (po == po1) {
  430. list_del_rcu(&po->list);
  431. goto out;
  432. }
  433. }
  434. pr_warn("dev_remove_offload: %p not found\n", po);
  435. out:
  436. spin_unlock(&offload_lock);
  437. }
  438. EXPORT_SYMBOL(__dev_remove_offload);
  439. /**
  440. * dev_remove_offload - remove packet offload handler
  441. * @po: packet offload declaration
  442. *
  443. * Remove a packet offload handler that was previously added to the kernel
  444. * offload handlers by dev_add_offload(). The passed &offload_type is
  445. * removed from the kernel lists and can be freed or reused once this
  446. * function returns.
  447. *
  448. * This call sleeps to guarantee that no CPU is looking at the packet
  449. * type after return.
  450. */
  451. void dev_remove_offload(struct packet_offload *po)
  452. {
  453. __dev_remove_offload(po);
  454. synchronize_net();
  455. }
  456. EXPORT_SYMBOL(dev_remove_offload);
  457. /******************************************************************************
  458. Device Boot-time Settings Routines
  459. *******************************************************************************/
  460. /* Boot time configuration table */
  461. static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
  462. /**
  463. * netdev_boot_setup_add - add new setup entry
  464. * @name: name of the device
  465. * @map: configured settings for the device
  466. *
  467. * Adds new setup entry to the dev_boot_setup list. The function
  468. * returns 0 on error and 1 on success. This is a generic routine to
  469. * all netdevices.
  470. */
  471. static int netdev_boot_setup_add(char *name, struct ifmap *map)
  472. {
  473. struct netdev_boot_setup *s;
  474. int i;
  475. s = dev_boot_setup;
  476. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  477. if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
  478. memset(s[i].name, 0, sizeof(s[i].name));
  479. strlcpy(s[i].name, name, IFNAMSIZ);
  480. memcpy(&s[i].map, map, sizeof(s[i].map));
  481. break;
  482. }
  483. }
  484. return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
  485. }
  486. /**
  487. * netdev_boot_setup_check - check boot time settings
  488. * @dev: the netdevice
  489. *
  490. * Check boot time settings for the device.
  491. * The found settings are set for the device to be used
  492. * later in the device probing.
  493. * Returns 0 if no settings found, 1 if they are.
  494. */
  495. int netdev_boot_setup_check(struct net_device *dev)
  496. {
  497. struct netdev_boot_setup *s = dev_boot_setup;
  498. int i;
  499. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  500. if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
  501. !strcmp(dev->name, s[i].name)) {
  502. dev->irq = s[i].map.irq;
  503. dev->base_addr = s[i].map.base_addr;
  504. dev->mem_start = s[i].map.mem_start;
  505. dev->mem_end = s[i].map.mem_end;
  506. return 1;
  507. }
  508. }
  509. return 0;
  510. }
  511. EXPORT_SYMBOL(netdev_boot_setup_check);
  512. /**
  513. * netdev_boot_base - get address from boot time settings
  514. * @prefix: prefix for network device
  515. * @unit: id for network device
  516. *
  517. * Check boot time settings for the base address of device.
  518. * The found settings are set for the device to be used
  519. * later in the device probing.
  520. * Returns 0 if no settings found.
  521. */
  522. unsigned long netdev_boot_base(const char *prefix, int unit)
  523. {
  524. const struct netdev_boot_setup *s = dev_boot_setup;
  525. char name[IFNAMSIZ];
  526. int i;
  527. sprintf(name, "%s%d", prefix, unit);
  528. /*
  529. * If device already registered then return base of 1
  530. * to indicate not to probe for this interface
  531. */
  532. if (__dev_get_by_name(&init_net, name))
  533. return 1;
  534. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
  535. if (!strcmp(name, s[i].name))
  536. return s[i].map.base_addr;
  537. return 0;
  538. }
  539. /*
  540. * Saves at boot time configured settings for any netdevice.
  541. */
  542. int __init netdev_boot_setup(char *str)
  543. {
  544. int ints[5];
  545. struct ifmap map;
  546. str = get_options(str, ARRAY_SIZE(ints), ints);
  547. if (!str || !*str)
  548. return 0;
  549. /* Save settings */
  550. memset(&map, 0, sizeof(map));
  551. if (ints[0] > 0)
  552. map.irq = ints[1];
  553. if (ints[0] > 1)
  554. map.base_addr = ints[2];
  555. if (ints[0] > 2)
  556. map.mem_start = ints[3];
  557. if (ints[0] > 3)
  558. map.mem_end = ints[4];
  559. /* Add new entry to the list */
  560. return netdev_boot_setup_add(str, &map);
  561. }
  562. __setup("netdev=", netdev_boot_setup);
  563. /*******************************************************************************
  564. Device Interface Subroutines
  565. *******************************************************************************/
  566. /**
  567. * __dev_get_by_name - find a device by its name
  568. * @net: the applicable net namespace
  569. * @name: name to find
  570. *
  571. * Find an interface by name. Must be called under RTNL semaphore
  572. * or @dev_base_lock. If the name is found a pointer to the device
  573. * is returned. If the name is not found then %NULL is returned. The
  574. * reference counters are not incremented so the caller must be
  575. * careful with locks.
  576. */
  577. struct net_device *__dev_get_by_name(struct net *net, const char *name)
  578. {
  579. struct net_device *dev;
  580. struct hlist_head *head = dev_name_hash(net, name);
  581. hlist_for_each_entry(dev, head, name_hlist)
  582. if (!strncmp(dev->name, name, IFNAMSIZ))
  583. return dev;
  584. return NULL;
  585. }
  586. EXPORT_SYMBOL(__dev_get_by_name);
  587. /**
  588. * dev_get_by_name_rcu - find a device by its name
  589. * @net: the applicable net namespace
  590. * @name: name to find
  591. *
  592. * Find an interface by name.
  593. * If the name is found a pointer to the device is returned.
  594. * If the name is not found then %NULL is returned.
  595. * The reference counters are not incremented so the caller must be
  596. * careful with locks. The caller must hold RCU lock.
  597. */
  598. struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
  599. {
  600. struct net_device *dev;
  601. struct hlist_head *head = dev_name_hash(net, name);
  602. hlist_for_each_entry_rcu(dev, head, name_hlist)
  603. if (!strncmp(dev->name, name, IFNAMSIZ))
  604. return dev;
  605. return NULL;
  606. }
  607. EXPORT_SYMBOL(dev_get_by_name_rcu);
  608. /**
  609. * dev_get_by_name - find a device by its name
  610. * @net: the applicable net namespace
  611. * @name: name to find
  612. *
  613. * Find an interface by name. This can be called from any
  614. * context and does its own locking. The returned handle has
  615. * the usage count incremented and the caller must use dev_put() to
  616. * release it when it is no longer needed. %NULL is returned if no
  617. * matching device is found.
  618. */
  619. struct net_device *dev_get_by_name(struct net *net, const char *name)
  620. {
  621. struct net_device *dev;
  622. rcu_read_lock();
  623. dev = dev_get_by_name_rcu(net, name);
  624. if (dev)
  625. dev_hold(dev);
  626. rcu_read_unlock();
  627. return dev;
  628. }
  629. EXPORT_SYMBOL(dev_get_by_name);
  630. /**
  631. * __dev_get_by_index - find a device by its ifindex
  632. * @net: the applicable net namespace
  633. * @ifindex: index of device
  634. *
  635. * Search for an interface by index. Returns %NULL if the device
  636. * is not found or a pointer to the device. The device has not
  637. * had its reference counter increased so the caller must be careful
  638. * about locking. The caller must hold either the RTNL semaphore
  639. * or @dev_base_lock.
  640. */
  641. struct net_device *__dev_get_by_index(struct net *net, int ifindex)
  642. {
  643. struct net_device *dev;
  644. struct hlist_head *head = dev_index_hash(net, ifindex);
  645. hlist_for_each_entry(dev, head, index_hlist)
  646. if (dev->ifindex == ifindex)
  647. return dev;
  648. return NULL;
  649. }
  650. EXPORT_SYMBOL(__dev_get_by_index);
  651. /**
  652. * dev_get_by_index_rcu - find a device by its ifindex
  653. * @net: the applicable net namespace
  654. * @ifindex: index of device
  655. *
  656. * Search for an interface by index. Returns %NULL if the device
  657. * is not found or a pointer to the device. The device has not
  658. * had its reference counter increased so the caller must be careful
  659. * about locking. The caller must hold RCU lock.
  660. */
  661. struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
  662. {
  663. struct net_device *dev;
  664. struct hlist_head *head = dev_index_hash(net, ifindex);
  665. hlist_for_each_entry_rcu(dev, head, index_hlist)
  666. if (dev->ifindex == ifindex)
  667. return dev;
  668. return NULL;
  669. }
  670. EXPORT_SYMBOL(dev_get_by_index_rcu);
  671. /**
  672. * dev_get_by_index - find a device by its ifindex
  673. * @net: the applicable net namespace
  674. * @ifindex: index of device
  675. *
  676. * Search for an interface by index. Returns NULL if the device
  677. * is not found or a pointer to the device. The device returned has
  678. * had a reference added and the pointer is safe until the user calls
  679. * dev_put to indicate they have finished with it.
  680. */
  681. struct net_device *dev_get_by_index(struct net *net, int ifindex)
  682. {
  683. struct net_device *dev;
  684. rcu_read_lock();
  685. dev = dev_get_by_index_rcu(net, ifindex);
  686. if (dev)
  687. dev_hold(dev);
  688. rcu_read_unlock();
  689. return dev;
  690. }
  691. EXPORT_SYMBOL(dev_get_by_index);
  692. /**
  693. * netdev_get_name - get a netdevice name, knowing its ifindex.
  694. * @net: network namespace
  695. * @name: a pointer to the buffer where the name will be stored.
  696. * @ifindex: the ifindex of the interface to get the name from.
  697. *
  698. * The use of raw_seqcount_begin() and cond_resched() before
  699. * retrying is required as we want to give the writers a chance
  700. * to complete when CONFIG_PREEMPT is not set.
  701. */
  702. int netdev_get_name(struct net *net, char *name, int ifindex)
  703. {
  704. struct net_device *dev;
  705. unsigned int seq;
  706. retry:
  707. seq = raw_seqcount_begin(&devnet_rename_seq);
  708. rcu_read_lock();
  709. dev = dev_get_by_index_rcu(net, ifindex);
  710. if (!dev) {
  711. rcu_read_unlock();
  712. return -ENODEV;
  713. }
  714. strcpy(name, dev->name);
  715. rcu_read_unlock();
  716. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  717. cond_resched();
  718. goto retry;
  719. }
  720. return 0;
  721. }
  722. /**
  723. * dev_getbyhwaddr_rcu - find a device by its hardware address
  724. * @net: the applicable net namespace
  725. * @type: media type of device
  726. * @ha: hardware address
  727. *
  728. * Search for an interface by MAC address. Returns NULL if the device
  729. * is not found or a pointer to the device.
  730. * The caller must hold RCU or RTNL.
  731. * The returned device has not had its ref count increased
  732. * and the caller must therefore be careful about locking
  733. *
  734. */
  735. struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
  736. const char *ha)
  737. {
  738. struct net_device *dev;
  739. for_each_netdev_rcu(net, dev)
  740. if (dev->type == type &&
  741. !memcmp(dev->dev_addr, ha, dev->addr_len))
  742. return dev;
  743. return NULL;
  744. }
  745. EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
  746. struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
  747. {
  748. struct net_device *dev;
  749. ASSERT_RTNL();
  750. for_each_netdev(net, dev)
  751. if (dev->type == type)
  752. return dev;
  753. return NULL;
  754. }
  755. EXPORT_SYMBOL(__dev_getfirstbyhwtype);
  756. struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
  757. {
  758. struct net_device *dev, *ret = NULL;
  759. rcu_read_lock();
  760. for_each_netdev_rcu(net, dev)
  761. if (dev->type == type) {
  762. dev_hold(dev);
  763. ret = dev;
  764. break;
  765. }
  766. rcu_read_unlock();
  767. return ret;
  768. }
  769. EXPORT_SYMBOL(dev_getfirstbyhwtype);
  770. /**
  771. * dev_get_by_flags_rcu - find any device with given flags
  772. * @net: the applicable net namespace
  773. * @if_flags: IFF_* values
  774. * @mask: bitmask of bits in if_flags to check
  775. *
  776. * Search for any interface with the given flags. Returns NULL if a device
  777. * is not found or a pointer to the device. Must be called inside
  778. * rcu_read_lock(), and result refcount is unchanged.
  779. */
  780. struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
  781. unsigned short mask)
  782. {
  783. struct net_device *dev, *ret;
  784. ret = NULL;
  785. for_each_netdev_rcu(net, dev) {
  786. if (((dev->flags ^ if_flags) & mask) == 0) {
  787. ret = dev;
  788. break;
  789. }
  790. }
  791. return ret;
  792. }
  793. EXPORT_SYMBOL(dev_get_by_flags_rcu);
  794. /**
  795. * dev_valid_name - check if name is okay for network device
  796. * @name: name string
  797. *
  798. * Network device names need to be valid file names to
  799. * to allow sysfs to work. We also disallow any kind of
  800. * whitespace.
  801. */
  802. bool dev_valid_name(const char *name)
  803. {
  804. if (*name == '\0')
  805. return false;
  806. if (strlen(name) >= IFNAMSIZ)
  807. return false;
  808. if (!strcmp(name, ".") || !strcmp(name, ".."))
  809. return false;
  810. while (*name) {
  811. if (*name == '/' || isspace(*name))
  812. return false;
  813. name++;
  814. }
  815. return true;
  816. }
  817. EXPORT_SYMBOL(dev_valid_name);
  818. /**
  819. * __dev_alloc_name - allocate a name for a device
  820. * @net: network namespace to allocate the device name in
  821. * @name: name format string
  822. * @buf: scratch buffer and result name string
  823. *
  824. * Passed a format string - eg "lt%d" it will try and find a suitable
  825. * id. It scans list of devices to build up a free map, then chooses
  826. * the first empty slot. The caller must hold the dev_base or rtnl lock
  827. * while allocating the name and adding the device in order to avoid
  828. * duplicates.
  829. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  830. * Returns the number of the unit assigned or a negative errno code.
  831. */
  832. static int __dev_alloc_name(struct net *net, const char *name, char *buf)
  833. {
  834. int i = 0;
  835. const char *p;
  836. const int max_netdevices = 8*PAGE_SIZE;
  837. unsigned long *inuse;
  838. struct net_device *d;
  839. p = strnchr(name, IFNAMSIZ-1, '%');
  840. if (p) {
  841. /*
  842. * Verify the string as this thing may have come from
  843. * the user. There must be either one "%d" and no other "%"
  844. * characters.
  845. */
  846. if (p[1] != 'd' || strchr(p + 2, '%'))
  847. return -EINVAL;
  848. /* Use one page as a bit array of possible slots */
  849. inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
  850. if (!inuse)
  851. return -ENOMEM;
  852. for_each_netdev(net, d) {
  853. if (!sscanf(d->name, name, &i))
  854. continue;
  855. if (i < 0 || i >= max_netdevices)
  856. continue;
  857. /* avoid cases where sscanf is not exact inverse of printf */
  858. snprintf(buf, IFNAMSIZ, name, i);
  859. if (!strncmp(buf, d->name, IFNAMSIZ))
  860. set_bit(i, inuse);
  861. }
  862. i = find_first_zero_bit(inuse, max_netdevices);
  863. free_page((unsigned long) inuse);
  864. }
  865. if (buf != name)
  866. snprintf(buf, IFNAMSIZ, name, i);
  867. if (!__dev_get_by_name(net, buf))
  868. return i;
  869. /* It is possible to run out of possible slots
  870. * when the name is long and there isn't enough space left
  871. * for the digits, or if all bits are used.
  872. */
  873. return -ENFILE;
  874. }
  875. /**
  876. * dev_alloc_name - allocate a name for a device
  877. * @dev: device
  878. * @name: name format string
  879. *
  880. * Passed a format string - eg "lt%d" it will try and find a suitable
  881. * id. It scans list of devices to build up a free map, then chooses
  882. * the first empty slot. The caller must hold the dev_base or rtnl lock
  883. * while allocating the name and adding the device in order to avoid
  884. * duplicates.
  885. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  886. * Returns the number of the unit assigned or a negative errno code.
  887. */
  888. int dev_alloc_name(struct net_device *dev, const char *name)
  889. {
  890. char buf[IFNAMSIZ];
  891. struct net *net;
  892. int ret;
  893. BUG_ON(!dev_net(dev));
  894. net = dev_net(dev);
  895. ret = __dev_alloc_name(net, name, buf);
  896. if (ret >= 0)
  897. strlcpy(dev->name, buf, IFNAMSIZ);
  898. return ret;
  899. }
  900. EXPORT_SYMBOL(dev_alloc_name);
  901. static int dev_alloc_name_ns(struct net *net,
  902. struct net_device *dev,
  903. const char *name)
  904. {
  905. char buf[IFNAMSIZ];
  906. int ret;
  907. ret = __dev_alloc_name(net, name, buf);
  908. if (ret >= 0)
  909. strlcpy(dev->name, buf, IFNAMSIZ);
  910. return ret;
  911. }
  912. static int dev_get_valid_name(struct net *net,
  913. struct net_device *dev,
  914. const char *name)
  915. {
  916. BUG_ON(!net);
  917. if (!dev_valid_name(name))
  918. return -EINVAL;
  919. if (strchr(name, '%'))
  920. return dev_alloc_name_ns(net, dev, name);
  921. else if (__dev_get_by_name(net, name))
  922. return -EEXIST;
  923. else if (dev->name != name)
  924. strlcpy(dev->name, name, IFNAMSIZ);
  925. return 0;
  926. }
  927. /**
  928. * dev_change_name - change name of a device
  929. * @dev: device
  930. * @newname: name (or format string) must be at least IFNAMSIZ
  931. *
  932. * Change name of a device, can pass format strings "eth%d".
  933. * for wildcarding.
  934. */
  935. int dev_change_name(struct net_device *dev, const char *newname)
  936. {
  937. char oldname[IFNAMSIZ];
  938. int err = 0;
  939. int ret;
  940. struct net *net;
  941. ASSERT_RTNL();
  942. BUG_ON(!dev_net(dev));
  943. net = dev_net(dev);
  944. if (dev->flags & IFF_UP)
  945. return -EBUSY;
  946. write_seqcount_begin(&devnet_rename_seq);
  947. if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  948. write_seqcount_end(&devnet_rename_seq);
  949. return 0;
  950. }
  951. memcpy(oldname, dev->name, IFNAMSIZ);
  952. err = dev_get_valid_name(net, dev, newname);
  953. if (err < 0) {
  954. write_seqcount_end(&devnet_rename_seq);
  955. return err;
  956. }
  957. rollback:
  958. ret = device_rename(&dev->dev, dev->name);
  959. if (ret) {
  960. memcpy(dev->name, oldname, IFNAMSIZ);
  961. write_seqcount_end(&devnet_rename_seq);
  962. return ret;
  963. }
  964. write_seqcount_end(&devnet_rename_seq);
  965. write_lock_bh(&dev_base_lock);
  966. hlist_del_rcu(&dev->name_hlist);
  967. write_unlock_bh(&dev_base_lock);
  968. synchronize_rcu();
  969. write_lock_bh(&dev_base_lock);
  970. hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
  971. write_unlock_bh(&dev_base_lock);
  972. ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
  973. ret = notifier_to_errno(ret);
  974. if (ret) {
  975. /* err >= 0 after dev_alloc_name() or stores the first errno */
  976. if (err >= 0) {
  977. err = ret;
  978. write_seqcount_begin(&devnet_rename_seq);
  979. memcpy(dev->name, oldname, IFNAMSIZ);
  980. goto rollback;
  981. } else {
  982. pr_err("%s: name change rollback failed: %d\n",
  983. dev->name, ret);
  984. }
  985. }
  986. return err;
  987. }
  988. /**
  989. * dev_set_alias - change ifalias of a device
  990. * @dev: device
  991. * @alias: name up to IFALIASZ
  992. * @len: limit of bytes to copy from info
  993. *
  994. * Set ifalias for a device,
  995. */
  996. int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
  997. {
  998. char *new_ifalias;
  999. ASSERT_RTNL();
  1000. if (len >= IFALIASZ)
  1001. return -EINVAL;
  1002. if (!len) {
  1003. kfree(dev->ifalias);
  1004. dev->ifalias = NULL;
  1005. return 0;
  1006. }
  1007. new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
  1008. if (!new_ifalias)
  1009. return -ENOMEM;
  1010. dev->ifalias = new_ifalias;
  1011. strlcpy(dev->ifalias, alias, len+1);
  1012. return len;
  1013. }
  1014. /**
  1015. * netdev_features_change - device changes features
  1016. * @dev: device to cause notification
  1017. *
  1018. * Called to indicate a device has changed features.
  1019. */
  1020. void netdev_features_change(struct net_device *dev)
  1021. {
  1022. call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
  1023. }
  1024. EXPORT_SYMBOL(netdev_features_change);
  1025. /**
  1026. * netdev_state_change - device changes state
  1027. * @dev: device to cause notification
  1028. *
  1029. * Called to indicate a device has changed state. This function calls
  1030. * the notifier chains for netdev_chain and sends a NEWLINK message
  1031. * to the routing socket.
  1032. */
  1033. void netdev_state_change(struct net_device *dev)
  1034. {
  1035. if (dev->flags & IFF_UP) {
  1036. call_netdevice_notifiers(NETDEV_CHANGE, dev);
  1037. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  1038. }
  1039. }
  1040. EXPORT_SYMBOL(netdev_state_change);
  1041. /**
  1042. * netdev_notify_peers - notify network peers about existence of @dev
  1043. * @dev: network device
  1044. *
  1045. * Generate traffic such that interested network peers are aware of
  1046. * @dev, such as by generating a gratuitous ARP. This may be used when
  1047. * a device wants to inform the rest of the network about some sort of
  1048. * reconfiguration such as a failover event or virtual machine
  1049. * migration.
  1050. */
  1051. void netdev_notify_peers(struct net_device *dev)
  1052. {
  1053. rtnl_lock();
  1054. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
  1055. rtnl_unlock();
  1056. }
  1057. EXPORT_SYMBOL(netdev_notify_peers);
  1058. static int __dev_open(struct net_device *dev)
  1059. {
  1060. const struct net_device_ops *ops = dev->netdev_ops;
  1061. int ret;
  1062. ASSERT_RTNL();
  1063. if (!netif_device_present(dev))
  1064. return -ENODEV;
  1065. /* Block netpoll from trying to do any rx path servicing.
  1066. * If we don't do this there is a chance ndo_poll_controller
  1067. * or ndo_poll may be running while we open the device
  1068. */
  1069. ret = netpoll_rx_disable(dev);
  1070. if (ret)
  1071. return ret;
  1072. ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
  1073. ret = notifier_to_errno(ret);
  1074. if (ret)
  1075. return ret;
  1076. set_bit(__LINK_STATE_START, &dev->state);
  1077. if (ops->ndo_validate_addr)
  1078. ret = ops->ndo_validate_addr(dev);
  1079. if (!ret && ops->ndo_open)
  1080. ret = ops->ndo_open(dev);
  1081. netpoll_rx_enable(dev);
  1082. if (ret)
  1083. clear_bit(__LINK_STATE_START, &dev->state);
  1084. else {
  1085. dev->flags |= IFF_UP;
  1086. net_dmaengine_get();
  1087. dev_set_rx_mode(dev);
  1088. dev_activate(dev);
  1089. add_device_randomness(dev->dev_addr, dev->addr_len);
  1090. }
  1091. return ret;
  1092. }
  1093. /**
  1094. * dev_open - prepare an interface for use.
  1095. * @dev: device to open
  1096. *
  1097. * Takes a device from down to up state. The device's private open
  1098. * function is invoked and then the multicast lists are loaded. Finally
  1099. * the device is moved into the up state and a %NETDEV_UP message is
  1100. * sent to the netdev notifier chain.
  1101. *
  1102. * Calling this function on an active interface is a nop. On a failure
  1103. * a negative errno code is returned.
  1104. */
  1105. int dev_open(struct net_device *dev)
  1106. {
  1107. int ret;
  1108. if (dev->flags & IFF_UP)
  1109. return 0;
  1110. ret = __dev_open(dev);
  1111. if (ret < 0)
  1112. return ret;
  1113. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  1114. call_netdevice_notifiers(NETDEV_UP, dev);
  1115. return ret;
  1116. }
  1117. EXPORT_SYMBOL(dev_open);
  1118. static int __dev_close_many(struct list_head *head)
  1119. {
  1120. struct net_device *dev;
  1121. ASSERT_RTNL();
  1122. might_sleep();
  1123. list_for_each_entry(dev, head, unreg_list) {
  1124. call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
  1125. clear_bit(__LINK_STATE_START, &dev->state);
  1126. /* Synchronize to scheduled poll. We cannot touch poll list, it
  1127. * can be even on different cpu. So just clear netif_running().
  1128. *
  1129. * dev->stop() will invoke napi_disable() on all of it's
  1130. * napi_struct instances on this device.
  1131. */
  1132. smp_mb__after_clear_bit(); /* Commit netif_running(). */
  1133. }
  1134. dev_deactivate_many(head);
  1135. list_for_each_entry(dev, head, unreg_list) {
  1136. const struct net_device_ops *ops = dev->netdev_ops;
  1137. /*
  1138. * Call the device specific close. This cannot fail.
  1139. * Only if device is UP
  1140. *
  1141. * We allow it to be called even after a DETACH hot-plug
  1142. * event.
  1143. */
  1144. if (ops->ndo_stop)
  1145. ops->ndo_stop(dev);
  1146. dev->flags &= ~IFF_UP;
  1147. net_dmaengine_put();
  1148. }
  1149. return 0;
  1150. }
  1151. static int __dev_close(struct net_device *dev)
  1152. {
  1153. int retval;
  1154. LIST_HEAD(single);
  1155. /* Temporarily disable netpoll until the interface is down */
  1156. retval = netpoll_rx_disable(dev);
  1157. if (retval)
  1158. return retval;
  1159. list_add(&dev->unreg_list, &single);
  1160. retval = __dev_close_many(&single);
  1161. list_del(&single);
  1162. netpoll_rx_enable(dev);
  1163. return retval;
  1164. }
  1165. static int dev_close_many(struct list_head *head)
  1166. {
  1167. struct net_device *dev, *tmp;
  1168. LIST_HEAD(tmp_list);
  1169. list_for_each_entry_safe(dev, tmp, head, unreg_list)
  1170. if (!(dev->flags & IFF_UP))
  1171. list_move(&dev->unreg_list, &tmp_list);
  1172. __dev_close_many(head);
  1173. list_for_each_entry(dev, head, unreg_list) {
  1174. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  1175. call_netdevice_notifiers(NETDEV_DOWN, dev);
  1176. }
  1177. /* rollback_registered_many needs the complete original list */
  1178. list_splice(&tmp_list, head);
  1179. return 0;
  1180. }
  1181. /**
  1182. * dev_close - shutdown an interface.
  1183. * @dev: device to shutdown
  1184. *
  1185. * This function moves an active device into down state. A
  1186. * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
  1187. * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
  1188. * chain.
  1189. */
  1190. int dev_close(struct net_device *dev)
  1191. {
  1192. int ret = 0;
  1193. if (dev->flags & IFF_UP) {
  1194. LIST_HEAD(single);
  1195. /* Block netpoll rx while the interface is going down */
  1196. ret = netpoll_rx_disable(dev);
  1197. if (ret)
  1198. return ret;
  1199. list_add(&dev->unreg_list, &single);
  1200. dev_close_many(&single);
  1201. list_del(&single);
  1202. netpoll_rx_enable(dev);
  1203. }
  1204. return ret;
  1205. }
  1206. EXPORT_SYMBOL(dev_close);
  1207. /**
  1208. * dev_disable_lro - disable Large Receive Offload on a device
  1209. * @dev: device
  1210. *
  1211. * Disable Large Receive Offload (LRO) on a net device. Must be
  1212. * called under RTNL. This is needed if received packets may be
  1213. * forwarded to another interface.
  1214. */
  1215. void dev_disable_lro(struct net_device *dev)
  1216. {
  1217. /*
  1218. * If we're trying to disable lro on a vlan device
  1219. * use the underlying physical device instead
  1220. */
  1221. if (is_vlan_dev(dev))
  1222. dev = vlan_dev_real_dev(dev);
  1223. dev->wanted_features &= ~NETIF_F_LRO;
  1224. netdev_update_features(dev);
  1225. if (unlikely(dev->features & NETIF_F_LRO))
  1226. netdev_WARN(dev, "failed to disable LRO!\n");
  1227. }
  1228. EXPORT_SYMBOL(dev_disable_lro);
  1229. static int dev_boot_phase = 1;
  1230. /**
  1231. * register_netdevice_notifier - register a network notifier block
  1232. * @nb: notifier
  1233. *
  1234. * Register a notifier to be called when network device events occur.
  1235. * The notifier passed is linked into the kernel structures and must
  1236. * not be reused until it has been unregistered. A negative errno code
  1237. * is returned on a failure.
  1238. *
  1239. * When registered all registration and up events are replayed
  1240. * to the new notifier to allow device to have a race free
  1241. * view of the network device list.
  1242. */
  1243. int register_netdevice_notifier(struct notifier_block *nb)
  1244. {
  1245. struct net_device *dev;
  1246. struct net_device *last;
  1247. struct net *net;
  1248. int err;
  1249. rtnl_lock();
  1250. err = raw_notifier_chain_register(&netdev_chain, nb);
  1251. if (err)
  1252. goto unlock;
  1253. if (dev_boot_phase)
  1254. goto unlock;
  1255. for_each_net(net) {
  1256. for_each_netdev(net, dev) {
  1257. err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
  1258. err = notifier_to_errno(err);
  1259. if (err)
  1260. goto rollback;
  1261. if (!(dev->flags & IFF_UP))
  1262. continue;
  1263. nb->notifier_call(nb, NETDEV_UP, dev);
  1264. }
  1265. }
  1266. unlock:
  1267. rtnl_unlock();
  1268. return err;
  1269. rollback:
  1270. last = dev;
  1271. for_each_net(net) {
  1272. for_each_netdev(net, dev) {
  1273. if (dev == last)
  1274. goto outroll;
  1275. if (dev->flags & IFF_UP) {
  1276. nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
  1277. nb->notifier_call(nb, NETDEV_DOWN, dev);
  1278. }
  1279. nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
  1280. }
  1281. }
  1282. outroll:
  1283. raw_notifier_chain_unregister(&netdev_chain, nb);
  1284. goto unlock;
  1285. }
  1286. EXPORT_SYMBOL(register_netdevice_notifier);
  1287. /**
  1288. * unregister_netdevice_notifier - unregister a network notifier block
  1289. * @nb: notifier
  1290. *
  1291. * Unregister a notifier previously registered by
  1292. * register_netdevice_notifier(). The notifier is unlinked into the
  1293. * kernel structures and may then be reused. A negative errno code
  1294. * is returned on a failure.
  1295. *
  1296. * After unregistering unregister and down device events are synthesized
  1297. * for all devices on the device list to the removed notifier to remove
  1298. * the need for special case cleanup code.
  1299. */
  1300. int unregister_netdevice_notifier(struct notifier_block *nb)
  1301. {
  1302. struct net_device *dev;
  1303. struct net *net;
  1304. int err;
  1305. rtnl_lock();
  1306. err = raw_notifier_chain_unregister(&netdev_chain, nb);
  1307. if (err)
  1308. goto unlock;
  1309. for_each_net(net) {
  1310. for_each_netdev(net, dev) {
  1311. if (dev->flags & IFF_UP) {
  1312. nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
  1313. nb->notifier_call(nb, NETDEV_DOWN, dev);
  1314. }
  1315. nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
  1316. }
  1317. }
  1318. unlock:
  1319. rtnl_unlock();
  1320. return err;
  1321. }
  1322. EXPORT_SYMBOL(unregister_netdevice_notifier);
  1323. /**
  1324. * call_netdevice_notifiers - call all network notifier blocks
  1325. * @val: value passed unmodified to notifier function
  1326. * @dev: net_device pointer passed unmodified to notifier function
  1327. *
  1328. * Call all network notifier blocks. Parameters and return value
  1329. * are as for raw_notifier_call_chain().
  1330. */
  1331. int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
  1332. {
  1333. ASSERT_RTNL();
  1334. return raw_notifier_call_chain(&netdev_chain, val, dev);
  1335. }
  1336. EXPORT_SYMBOL(call_netdevice_notifiers);
  1337. static struct static_key netstamp_needed __read_mostly;
  1338. #ifdef HAVE_JUMP_LABEL
  1339. /* We are not allowed to call static_key_slow_dec() from irq context
  1340. * If net_disable_timestamp() is called from irq context, defer the
  1341. * static_key_slow_dec() calls.
  1342. */
  1343. static atomic_t netstamp_needed_deferred;
  1344. #endif
  1345. void net_enable_timestamp(void)
  1346. {
  1347. #ifdef HAVE_JUMP_LABEL
  1348. int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
  1349. if (deferred) {
  1350. while (--deferred)
  1351. static_key_slow_dec(&netstamp_needed);
  1352. return;
  1353. }
  1354. #endif
  1355. static_key_slow_inc(&netstamp_needed);
  1356. }
  1357. EXPORT_SYMBOL(net_enable_timestamp);
  1358. void net_disable_timestamp(void)
  1359. {
  1360. #ifdef HAVE_JUMP_LABEL
  1361. if (in_interrupt()) {
  1362. atomic_inc(&netstamp_needed_deferred);
  1363. return;
  1364. }
  1365. #endif
  1366. static_key_slow_dec(&netstamp_needed);
  1367. }
  1368. EXPORT_SYMBOL(net_disable_timestamp);
  1369. static inline void net_timestamp_set(struct sk_buff *skb)
  1370. {
  1371. skb->tstamp.tv64 = 0;
  1372. if (static_key_false(&netstamp_needed))
  1373. __net_timestamp(skb);
  1374. }
  1375. #define net_timestamp_check(COND, SKB) \
  1376. if (static_key_false(&netstamp_needed)) { \
  1377. if ((COND) && !(SKB)->tstamp.tv64) \
  1378. __net_timestamp(SKB); \
  1379. } \
  1380. static inline bool is_skb_forwardable(struct net_device *dev,
  1381. struct sk_buff *skb)
  1382. {
  1383. unsigned int len;
  1384. if (!(dev->flags & IFF_UP))
  1385. return false;
  1386. len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
  1387. if (skb->len <= len)
  1388. return true;
  1389. /* if TSO is enabled, we don't care about the length as the packet
  1390. * could be forwarded without being segmented before
  1391. */
  1392. if (skb_is_gso(skb))
  1393. return true;
  1394. return false;
  1395. }
  1396. /**
  1397. * dev_forward_skb - loopback an skb to another netif
  1398. *
  1399. * @dev: destination network device
  1400. * @skb: buffer to forward
  1401. *
  1402. * return values:
  1403. * NET_RX_SUCCESS (no congestion)
  1404. * NET_RX_DROP (packet was dropped, but freed)
  1405. *
  1406. * dev_forward_skb can be used for injecting an skb from the
  1407. * start_xmit function of one device into the receive queue
  1408. * of another device.
  1409. *
  1410. * The receiving device may be in another namespace, so
  1411. * we have to clear all information in the skb that could
  1412. * impact namespace isolation.
  1413. */
  1414. int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
  1415. {
  1416. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  1417. if (skb_copy_ubufs(skb, GFP_ATOMIC)) {
  1418. atomic_long_inc(&dev->rx_dropped);
  1419. kfree_skb(skb);
  1420. return NET_RX_DROP;
  1421. }
  1422. }
  1423. skb_orphan(skb);
  1424. if (unlikely(!is_skb_forwardable(dev, skb))) {
  1425. atomic_long_inc(&dev->rx_dropped);
  1426. kfree_skb(skb);
  1427. return NET_RX_DROP;
  1428. }
  1429. skb->skb_iif = 0;
  1430. skb->dev = dev;
  1431. skb_dst_drop(skb);
  1432. skb->tstamp.tv64 = 0;
  1433. skb->pkt_type = PACKET_HOST;
  1434. skb->protocol = eth_type_trans(skb, dev);
  1435. skb->mark = 0;
  1436. secpath_reset(skb);
  1437. nf_reset(skb);
  1438. nf_reset_trace(skb);
  1439. return netif_rx(skb);
  1440. }
  1441. EXPORT_SYMBOL_GPL(dev_forward_skb);
  1442. static inline int deliver_skb(struct sk_buff *skb,
  1443. struct packet_type *pt_prev,
  1444. struct net_device *orig_dev)
  1445. {
  1446. if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
  1447. return -ENOMEM;
  1448. atomic_inc(&skb->users);
  1449. return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  1450. }
  1451. static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
  1452. {
  1453. if (!ptype->af_packet_priv || !skb->sk)
  1454. return false;
  1455. if (ptype->id_match)
  1456. return ptype->id_match(ptype, skb->sk);
  1457. else if ((struct sock *)ptype->af_packet_priv == skb->sk)
  1458. return true;
  1459. return false;
  1460. }
  1461. /*
  1462. * Support routine. Sends outgoing frames to any network
  1463. * taps currently in use.
  1464. */
  1465. static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
  1466. {
  1467. struct packet_type *ptype;
  1468. struct sk_buff *skb2 = NULL;
  1469. struct packet_type *pt_prev = NULL;
  1470. rcu_read_lock();
  1471. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  1472. /* Never send packets back to the socket
  1473. * they originated from - MvS (miquels@drinkel.ow.org)
  1474. */
  1475. if ((ptype->dev == dev || !ptype->dev) &&
  1476. (!skb_loop_sk(ptype, skb))) {
  1477. if (pt_prev) {
  1478. deliver_skb(skb2, pt_prev, skb->dev);
  1479. pt_prev = ptype;
  1480. continue;
  1481. }
  1482. skb2 = skb_clone(skb, GFP_ATOMIC);
  1483. if (!skb2)
  1484. break;
  1485. net_timestamp_set(skb2);
  1486. /* skb->nh should be correctly
  1487. set by sender, so that the second statement is
  1488. just protection against buggy protocols.
  1489. */
  1490. skb_reset_mac_header(skb2);
  1491. if (skb_network_header(skb2) < skb2->data ||
  1492. skb2->network_header > skb2->tail) {
  1493. net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
  1494. ntohs(skb2->protocol),
  1495. dev->name);
  1496. skb_reset_network_header(skb2);
  1497. }
  1498. skb2->transport_header = skb2->network_header;
  1499. skb2->pkt_type = PACKET_OUTGOING;
  1500. pt_prev = ptype;
  1501. }
  1502. }
  1503. if (pt_prev)
  1504. pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
  1505. rcu_read_unlock();
  1506. }
  1507. /**
  1508. * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
  1509. * @dev: Network device
  1510. * @txq: number of queues available
  1511. *
  1512. * If real_num_tx_queues is changed the tc mappings may no longer be
  1513. * valid. To resolve this verify the tc mapping remains valid and if
  1514. * not NULL the mapping. With no priorities mapping to this
  1515. * offset/count pair it will no longer be used. In the worst case TC0
  1516. * is invalid nothing can be done so disable priority mappings. If is
  1517. * expected that drivers will fix this mapping if they can before
  1518. * calling netif_set_real_num_tx_queues.
  1519. */
  1520. static void netif_setup_tc(struct net_device *dev, unsigned int txq)
  1521. {
  1522. int i;
  1523. struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
  1524. /* If TC0 is invalidated disable TC mapping */
  1525. if (tc->offset + tc->count > txq) {
  1526. pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
  1527. dev->num_tc = 0;
  1528. return;
  1529. }
  1530. /* Invalidated prio to tc mappings set to TC0 */
  1531. for (i = 1; i < TC_BITMASK + 1; i++) {
  1532. int q = netdev_get_prio_tc_map(dev, i);
  1533. tc = &dev->tc_to_txq[q];
  1534. if (tc->offset + tc->count > txq) {
  1535. pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
  1536. i, q);
  1537. netdev_set_prio_tc_map(dev, i, 0);
  1538. }
  1539. }
  1540. }
  1541. #ifdef CONFIG_XPS
  1542. static DEFINE_MUTEX(xps_map_mutex);
  1543. #define xmap_dereference(P) \
  1544. rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
  1545. static struct xps_map *remove_xps_queue(struct xps_dev_maps *dev_maps,
  1546. int cpu, u16 index)
  1547. {
  1548. struct xps_map *map = NULL;
  1549. int pos;
  1550. if (dev_maps)
  1551. map = xmap_dereference(dev_maps->cpu_map[cpu]);
  1552. for (pos = 0; map && pos < map->len; pos++) {
  1553. if (map->queues[pos] == index) {
  1554. if (map->len > 1) {
  1555. map->queues[pos] = map->queues[--map->len];
  1556. } else {
  1557. RCU_INIT_POINTER(dev_maps->cpu_map[cpu], NULL);
  1558. kfree_rcu(map, rcu);
  1559. map = NULL;
  1560. }
  1561. break;
  1562. }
  1563. }
  1564. return map;
  1565. }
  1566. static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
  1567. {
  1568. struct xps_dev_maps *dev_maps;
  1569. int cpu, i;
  1570. bool active = false;
  1571. mutex_lock(&xps_map_mutex);
  1572. dev_maps = xmap_dereference(dev->xps_maps);
  1573. if (!dev_maps)
  1574. goto out_no_maps;
  1575. for_each_possible_cpu(cpu) {
  1576. for (i = index; i < dev->num_tx_queues; i++) {
  1577. if (!remove_xps_queue(dev_maps, cpu, i))
  1578. break;
  1579. }
  1580. if (i == dev->num_tx_queues)
  1581. active = true;
  1582. }
  1583. if (!active) {
  1584. RCU_INIT_POINTER(dev->xps_maps, NULL);
  1585. kfree_rcu(dev_maps, rcu);
  1586. }
  1587. for (i = index; i < dev->num_tx_queues; i++)
  1588. netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i),
  1589. NUMA_NO_NODE);
  1590. out_no_maps:
  1591. mutex_unlock(&xps_map_mutex);
  1592. }
  1593. static struct xps_map *expand_xps_map(struct xps_map *map,
  1594. int cpu, u16 index)
  1595. {
  1596. struct xps_map *new_map;
  1597. int alloc_len = XPS_MIN_MAP_ALLOC;
  1598. int i, pos;
  1599. for (pos = 0; map && pos < map->len; pos++) {
  1600. if (map->queues[pos] != index)
  1601. continue;
  1602. return map;
  1603. }
  1604. /* Need to add queue to this CPU's existing map */
  1605. if (map) {
  1606. if (pos < map->alloc_len)
  1607. return map;
  1608. alloc_len = map->alloc_len * 2;
  1609. }
  1610. /* Need to allocate new map to store queue on this CPU's map */
  1611. new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
  1612. cpu_to_node(cpu));
  1613. if (!new_map)
  1614. return NULL;
  1615. for (i = 0; i < pos; i++)
  1616. new_map->queues[i] = map->queues[i];
  1617. new_map->alloc_len = alloc_len;
  1618. new_map->len = pos;
  1619. return new_map;
  1620. }
  1621. int netif_set_xps_queue(struct net_device *dev, struct cpumask *mask, u16 index)
  1622. {
  1623. struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
  1624. struct xps_map *map, *new_map;
  1625. int maps_sz = max_t(unsigned int, XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES);
  1626. int cpu, numa_node_id = -2;
  1627. bool active = false;
  1628. mutex_lock(&xps_map_mutex);
  1629. dev_maps = xmap_dereference(dev->xps_maps);
  1630. /* allocate memory for queue storage */
  1631. for_each_online_cpu(cpu) {
  1632. if (!cpumask_test_cpu(cpu, mask))
  1633. continue;
  1634. if (!new_dev_maps)
  1635. new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
  1636. if (!new_dev_maps) {
  1637. mutex_unlock(&xps_map_mutex);
  1638. return -ENOMEM;
  1639. }
  1640. map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
  1641. NULL;
  1642. map = expand_xps_map(map, cpu, index);
  1643. if (!map)
  1644. goto error;
  1645. RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
  1646. }
  1647. if (!new_dev_maps)
  1648. goto out_no_new_maps;
  1649. for_each_possible_cpu(cpu) {
  1650. if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) {
  1651. /* add queue to CPU maps */
  1652. int pos = 0;
  1653. map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
  1654. while ((pos < map->len) && (map->queues[pos] != index))
  1655. pos++;
  1656. if (pos == map->len)
  1657. map->queues[map->len++] = index;
  1658. #ifdef CONFIG_NUMA
  1659. if (numa_node_id == -2)
  1660. numa_node_id = cpu_to_node(cpu);
  1661. else if (numa_node_id != cpu_to_node(cpu))
  1662. numa_node_id = -1;
  1663. #endif
  1664. } else if (dev_maps) {
  1665. /* fill in the new device map from the old device map */
  1666. map = xmap_dereference(dev_maps->cpu_map[cpu]);
  1667. RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
  1668. }
  1669. }
  1670. rcu_assign_pointer(dev->xps_maps, new_dev_maps);
  1671. /* Cleanup old maps */
  1672. if (dev_maps) {
  1673. for_each_possible_cpu(cpu) {
  1674. new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
  1675. map = xmap_dereference(dev_maps->cpu_map[cpu]);
  1676. if (map && map != new_map)
  1677. kfree_rcu(map, rcu);
  1678. }
  1679. kfree_rcu(dev_maps, rcu);
  1680. }
  1681. dev_maps = new_dev_maps;
  1682. active = true;
  1683. out_no_new_maps:
  1684. /* update Tx queue numa node */
  1685. netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
  1686. (numa_node_id >= 0) ? numa_node_id :
  1687. NUMA_NO_NODE);
  1688. if (!dev_maps)
  1689. goto out_no_maps;
  1690. /* removes queue from unused CPUs */
  1691. for_each_possible_cpu(cpu) {
  1692. if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu))
  1693. continue;
  1694. if (remove_xps_queue(dev_maps, cpu, index))
  1695. active = true;
  1696. }
  1697. /* free map if not active */
  1698. if (!active) {
  1699. RCU_INIT_POINTER(dev->xps_maps, NULL);
  1700. kfree_rcu(dev_maps, rcu);
  1701. }
  1702. out_no_maps:
  1703. mutex_unlock(&xps_map_mutex);
  1704. return 0;
  1705. error:
  1706. /* remove any maps that we added */
  1707. for_each_possible_cpu(cpu) {
  1708. new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
  1709. map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
  1710. NULL;
  1711. if (new_map && new_map != map)
  1712. kfree(new_map);
  1713. }
  1714. mutex_unlock(&xps_map_mutex);
  1715. kfree(new_dev_maps);
  1716. return -ENOMEM;
  1717. }
  1718. EXPORT_SYMBOL(netif_set_xps_queue);
  1719. #endif
  1720. /*
  1721. * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
  1722. * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
  1723. */
  1724. int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
  1725. {
  1726. int rc;
  1727. if (txq < 1 || txq > dev->num_tx_queues)
  1728. return -EINVAL;
  1729. if (dev->reg_state == NETREG_REGISTERED ||
  1730. dev->reg_state == NETREG_UNREGISTERING) {
  1731. ASSERT_RTNL();
  1732. rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
  1733. txq);
  1734. if (rc)
  1735. return rc;
  1736. if (dev->num_tc)
  1737. netif_setup_tc(dev, txq);
  1738. if (txq < dev->real_num_tx_queues) {
  1739. qdisc_reset_all_tx_gt(dev, txq);
  1740. #ifdef CONFIG_XPS
  1741. netif_reset_xps_queues_gt(dev, txq);
  1742. #endif
  1743. }
  1744. }
  1745. dev->real_num_tx_queues = txq;
  1746. return 0;
  1747. }
  1748. EXPORT_SYMBOL(netif_set_real_num_tx_queues);
  1749. #ifdef CONFIG_RPS
  1750. /**
  1751. * netif_set_real_num_rx_queues - set actual number of RX queues used
  1752. * @dev: Network device
  1753. * @rxq: Actual number of RX queues
  1754. *
  1755. * This must be called either with the rtnl_lock held or before
  1756. * registration of the net device. Returns 0 on success, or a
  1757. * negative error code. If called before registration, it always
  1758. * succeeds.
  1759. */
  1760. int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
  1761. {
  1762. int rc;
  1763. if (rxq < 1 || rxq > dev->num_rx_queues)
  1764. return -EINVAL;
  1765. if (dev->reg_state == NETREG_REGISTERED) {
  1766. ASSERT_RTNL();
  1767. rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
  1768. rxq);
  1769. if (rc)
  1770. return rc;
  1771. }
  1772. dev->real_num_rx_queues = rxq;
  1773. return 0;
  1774. }
  1775. EXPORT_SYMBOL(netif_set_real_num_rx_queues);
  1776. #endif
  1777. /**
  1778. * netif_get_num_default_rss_queues - default number of RSS queues
  1779. *
  1780. * This routine should set an upper limit on the number of RSS queues
  1781. * used by default by multiqueue devices.
  1782. */
  1783. int netif_get_num_default_rss_queues(void)
  1784. {
  1785. return min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
  1786. }
  1787. EXPORT_SYMBOL(netif_get_num_default_rss_queues);
  1788. static inline void __netif_reschedule(struct Qdisc *q)
  1789. {
  1790. struct softnet_data *sd;
  1791. unsigned long flags;
  1792. local_irq_save(flags);
  1793. sd = &__get_cpu_var(softnet_data);
  1794. q->next_sched = NULL;
  1795. *sd->output_queue_tailp = q;
  1796. sd->output_queue_tailp = &q->next_sched;
  1797. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  1798. local_irq_restore(flags);
  1799. }
  1800. void __netif_schedule(struct Qdisc *q)
  1801. {
  1802. if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
  1803. __netif_reschedule(q);
  1804. }
  1805. EXPORT_SYMBOL(__netif_schedule);
  1806. void dev_kfree_skb_irq(struct sk_buff *skb)
  1807. {
  1808. if (atomic_dec_and_test(&skb->users)) {
  1809. struct softnet_data *sd;
  1810. unsigned long flags;
  1811. local_irq_save(flags);
  1812. sd = &__get_cpu_var(softnet_data);
  1813. skb->next = sd->completion_queue;
  1814. sd->completion_queue = skb;
  1815. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  1816. local_irq_restore(flags);
  1817. }
  1818. }
  1819. EXPORT_SYMBOL(dev_kfree_skb_irq);
  1820. void dev_kfree_skb_any(struct sk_buff *skb)
  1821. {
  1822. if (in_irq() || irqs_disabled())
  1823. dev_kfree_skb_irq(skb);
  1824. else
  1825. dev_kfree_skb(skb);
  1826. }
  1827. EXPORT_SYMBOL(dev_kfree_skb_any);
  1828. /**
  1829. * netif_device_detach - mark device as removed
  1830. * @dev: network device
  1831. *
  1832. * Mark device as removed from system and therefore no longer available.
  1833. */
  1834. void netif_device_detach(struct net_device *dev)
  1835. {
  1836. if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
  1837. netif_running(dev)) {
  1838. netif_tx_stop_all_queues(dev);
  1839. }
  1840. }
  1841. EXPORT_SYMBOL(netif_device_detach);
  1842. /**
  1843. * netif_device_attach - mark device as attached
  1844. * @dev: network device
  1845. *
  1846. * Mark device as attached from system and restart if needed.
  1847. */
  1848. void netif_device_attach(struct net_device *dev)
  1849. {
  1850. if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
  1851. netif_running(dev)) {
  1852. netif_tx_wake_all_queues(dev);
  1853. __netdev_watchdog_up(dev);
  1854. }
  1855. }
  1856. EXPORT_SYMBOL(netif_device_attach);
  1857. static void skb_warn_bad_offload(const struct sk_buff *skb)
  1858. {
  1859. static const netdev_features_t null_features = 0;
  1860. struct net_device *dev = skb->dev;
  1861. const char *driver = "";
  1862. if (!net_ratelimit())
  1863. return;
  1864. if (dev && dev->dev.parent)
  1865. driver = dev_driver_string(dev->dev.parent);
  1866. WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
  1867. "gso_type=%d ip_summed=%d\n",
  1868. driver, dev ? &dev->features : &null_features,
  1869. skb->sk ? &skb->sk->sk_route_caps : &null_features,
  1870. skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
  1871. skb_shinfo(skb)->gso_type, skb->ip_summed);
  1872. }
  1873. /*
  1874. * Invalidate hardware checksum when packet is to be mangled, and
  1875. * complete checksum manually on outgoing path.
  1876. */
  1877. int skb_checksum_help(struct sk_buff *skb)
  1878. {
  1879. __wsum csum;
  1880. int ret = 0, offset;
  1881. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1882. goto out_set_summed;
  1883. if (unlikely(skb_shinfo(skb)->gso_size)) {
  1884. skb_warn_bad_offload(skb);
  1885. return -EINVAL;
  1886. }
  1887. /* Before computing a checksum, we should make sure no frag could
  1888. * be modified by an external entity : checksum could be wrong.
  1889. */
  1890. if (skb_has_shared_frag(skb)) {
  1891. ret = __skb_linearize(skb);
  1892. if (ret)
  1893. goto out;
  1894. }
  1895. offset = skb_checksum_start_offset(skb);
  1896. BUG_ON(offset >= skb_headlen(skb));
  1897. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1898. offset += skb->csum_offset;
  1899. BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
  1900. if (skb_cloned(skb) &&
  1901. !skb_clone_writable(skb, offset + sizeof(__sum16))) {
  1902. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1903. if (ret)
  1904. goto out;
  1905. }
  1906. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  1907. out_set_summed:
  1908. skb->ip_summed = CHECKSUM_NONE;
  1909. out:
  1910. return ret;
  1911. }
  1912. EXPORT_SYMBOL(skb_checksum_help);
  1913. __be16 skb_network_protocol(struct sk_buff *skb)
  1914. {
  1915. __be16 type = skb->protocol;
  1916. int vlan_depth = ETH_HLEN;
  1917. /* Tunnel gso handlers can set protocol to ethernet. */
  1918. if (type == htons(ETH_P_TEB)) {
  1919. struct ethhdr *eth;
  1920. if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
  1921. return 0;
  1922. eth = (struct ethhdr *)skb_mac_header(skb);
  1923. type = eth->h_proto;
  1924. }
  1925. while (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) {
  1926. struct vlan_hdr *vh;
  1927. if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
  1928. return 0;
  1929. vh = (struct vlan_hdr *)(skb->data + vlan_depth);
  1930. type = vh->h_vlan_encapsulated_proto;
  1931. vlan_depth += VLAN_HLEN;
  1932. }
  1933. return type;
  1934. }
  1935. /**
  1936. * skb_mac_gso_segment - mac layer segmentation handler.
  1937. * @skb: buffer to segment
  1938. * @features: features for the output path (see dev->features)
  1939. */
  1940. struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
  1941. netdev_features_t features)
  1942. {
  1943. struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
  1944. struct packet_offload *ptype;
  1945. __be16 type = skb_network_protocol(skb);
  1946. if (unlikely(!type))
  1947. return ERR_PTR(-EINVAL);
  1948. __skb_pull(skb, skb->mac_len);
  1949. rcu_read_lock();
  1950. list_for_each_entry_rcu(ptype, &offload_base, list) {
  1951. if (ptype->type == type && ptype->callbacks.gso_segment) {
  1952. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  1953. int err;
  1954. err = ptype->callbacks.gso_send_check(skb);
  1955. segs = ERR_PTR(err);
  1956. if (err || skb_gso_ok(skb, features))
  1957. break;
  1958. __skb_push(skb, (skb->data -
  1959. skb_network_header(skb)));
  1960. }
  1961. segs = ptype->callbacks.gso_segment(skb, features);
  1962. break;
  1963. }
  1964. }
  1965. rcu_read_unlock();
  1966. __skb_push(skb, skb->data - skb_mac_header(skb));
  1967. return segs;
  1968. }
  1969. EXPORT_SYMBOL(skb_mac_gso_segment);
  1970. /* openvswitch calls this on rx path, so we need a different check.
  1971. */
  1972. static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
  1973. {
  1974. if (tx_path)
  1975. return skb->ip_summed != CHECKSUM_PARTIAL;
  1976. else
  1977. return skb->ip_summed == CHECKSUM_NONE;
  1978. }
  1979. /**
  1980. * __skb_gso_segment - Perform segmentation on skb.
  1981. * @skb: buffer to segment
  1982. * @features: features for the output path (see dev->features)
  1983. * @tx_path: whether it is called in TX path
  1984. *
  1985. * This function segments the given skb and returns a list of segments.
  1986. *
  1987. * It may return NULL if the skb requires no segmentation. This is
  1988. * only possible when GSO is used for verifying header integrity.
  1989. */
  1990. struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
  1991. netdev_features_t features, bool tx_path)
  1992. {
  1993. if (unlikely(skb_needs_check(skb, tx_path))) {
  1994. int err;
  1995. skb_warn_bad_offload(skb);
  1996. if (skb_header_cloned(skb) &&
  1997. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  1998. return ERR_PTR(err);
  1999. }
  2000. SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
  2001. skb_reset_mac_header(skb);
  2002. skb_reset_mac_len(skb);
  2003. return skb_mac_gso_segment(skb, features);
  2004. }
  2005. EXPORT_SYMBOL(__skb_gso_segment);
  2006. /* Take action when hardware reception checksum errors are detected. */
  2007. #ifdef CONFIG_BUG
  2008. void netdev_rx_csum_fault(struct net_device *dev)
  2009. {
  2010. if (net_ratelimit()) {
  2011. pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
  2012. dump_stack();
  2013. }
  2014. }
  2015. EXPORT_SYMBOL(netdev_rx_csum_fault);
  2016. #endif
  2017. /* Actually, we should eliminate this check as soon as we know, that:
  2018. * 1. IOMMU is present and allows to map all the memory.
  2019. * 2. No high memory really exists on this machine.
  2020. */
  2021. static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
  2022. {
  2023. #ifdef CONFIG_HIGHMEM
  2024. int i;
  2025. if (!(dev->features & NETIF_F_HIGHDMA)) {
  2026. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2027. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2028. if (PageHighMem(skb_frag_page(frag)))
  2029. return 1;
  2030. }
  2031. }
  2032. if (PCI_DMA_BUS_IS_PHYS) {
  2033. struct device *pdev = dev->dev.parent;
  2034. if (!pdev)
  2035. return 0;
  2036. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2037. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2038. dma_addr_t addr = page_to_phys(skb_frag_page(frag));
  2039. if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
  2040. return 1;
  2041. }
  2042. }
  2043. #endif
  2044. return 0;
  2045. }
  2046. struct dev_gso_cb {
  2047. void (*destructor)(struct sk_buff *skb);
  2048. };
  2049. #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
  2050. static void dev_gso_skb_destructor(struct sk_buff *skb)
  2051. {
  2052. struct dev_gso_cb *cb;
  2053. do {
  2054. struct sk_buff *nskb = skb->next;
  2055. skb->next = nskb->next;
  2056. nskb->next = NULL;
  2057. kfree_skb(nskb);
  2058. } while (skb->next);
  2059. cb = DEV_GSO_CB(skb);
  2060. if (cb->destructor)
  2061. cb->destructor(skb);
  2062. }
  2063. /**
  2064. * dev_gso_segment - Perform emulated hardware segmentation on skb.
  2065. * @skb: buffer to segment
  2066. * @features: device features as applicable to this skb
  2067. *
  2068. * This function segments the given skb and stores the list of segments
  2069. * in skb->next.
  2070. */
  2071. static int dev_gso_segment(struct sk_buff *skb, netdev_features_t features)
  2072. {
  2073. struct sk_buff *segs;
  2074. segs = skb_gso_segment(skb, features);
  2075. /* Verifying header integrity only. */
  2076. if (!segs)
  2077. return 0;
  2078. if (IS_ERR(segs))
  2079. return PTR_ERR(segs);
  2080. skb->next = segs;
  2081. DEV_GSO_CB(skb)->destructor = skb->destructor;
  2082. skb->destructor = dev_gso_skb_destructor;
  2083. return 0;
  2084. }
  2085. static netdev_features_t harmonize_features(struct sk_buff *skb,
  2086. __be16 protocol, netdev_features_t features)
  2087. {
  2088. if (skb->ip_summed != CHECKSUM_NONE &&
  2089. !can_checksum_protocol(features, protocol)) {
  2090. features &= ~NETIF_F_ALL_CSUM;
  2091. } else if (illegal_highdma(skb->dev, skb)) {
  2092. features &= ~NETIF_F_SG;
  2093. }
  2094. return features;
  2095. }
  2096. netdev_features_t netif_skb_features(struct sk_buff *skb)
  2097. {
  2098. __be16 protocol = skb->protocol;
  2099. netdev_features_t features = skb->dev->features;
  2100. if (skb_shinfo(skb)->gso_segs > skb->dev->gso_max_segs)
  2101. features &= ~NETIF_F_GSO_MASK;
  2102. if (protocol == htons(ETH_P_8021Q) || protocol == htons(ETH_P_8021AD)) {
  2103. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  2104. protocol = veh->h_vlan_encapsulated_proto;
  2105. } else if (!vlan_tx_tag_present(skb)) {
  2106. return harmonize_features(skb, protocol, features);
  2107. }
  2108. features &= (skb->dev->vlan_features | NETIF_F_HW_VLAN_CTAG_TX |
  2109. NETIF_F_HW_VLAN_STAG_TX);
  2110. if (protocol != htons(ETH_P_8021Q) && protocol != htons(ETH_P_8021AD)) {
  2111. return harmonize_features(skb, protocol, features);
  2112. } else {
  2113. features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
  2114. NETIF_F_GEN_CSUM | NETIF_F_HW_VLAN_CTAG_TX |
  2115. NETIF_F_HW_VLAN_STAG_TX;
  2116. return harmonize_features(skb, protocol, features);
  2117. }
  2118. }
  2119. EXPORT_SYMBOL(netif_skb_features);
  2120. /*
  2121. * Returns true if either:
  2122. * 1. skb has frag_list and the device doesn't support FRAGLIST, or
  2123. * 2. skb is fragmented and the device does not support SG.
  2124. */
  2125. static inline int skb_needs_linearize(struct sk_buff *skb,
  2126. netdev_features_t features)
  2127. {
  2128. return skb_is_nonlinear(skb) &&
  2129. ((skb_has_frag_list(skb) &&
  2130. !(features & NETIF_F_FRAGLIST)) ||
  2131. (skb_shinfo(skb)->nr_frags &&
  2132. !(features & NETIF_F_SG)));
  2133. }
  2134. int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
  2135. struct netdev_queue *txq)
  2136. {
  2137. const struct net_device_ops *ops = dev->netdev_ops;
  2138. int rc = NETDEV_TX_OK;
  2139. unsigned int skb_len;
  2140. if (likely(!skb->next)) {
  2141. netdev_features_t features;
  2142. /*
  2143. * If device doesn't need skb->dst, release it right now while
  2144. * its hot in this cpu cache
  2145. */
  2146. if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
  2147. skb_dst_drop(skb);
  2148. features = netif_skb_features(skb);
  2149. if (vlan_tx_tag_present(skb) &&
  2150. !vlan_hw_offload_capable(features, skb->vlan_proto)) {
  2151. skb = __vlan_put_tag(skb, skb->vlan_proto,
  2152. vlan_tx_tag_get(skb));
  2153. if (unlikely(!skb))
  2154. goto out;
  2155. skb->vlan_tci = 0;
  2156. }
  2157. /* If encapsulation offload request, verify we are testing
  2158. * hardware encapsulation features instead of standard
  2159. * features for the netdev
  2160. */
  2161. if (skb->encapsulation)
  2162. features &= dev->hw_enc_features;
  2163. if (netif_needs_gso(skb, features)) {
  2164. if (unlikely(dev_gso_segment(skb, features)))
  2165. goto out_kfree_skb;
  2166. if (skb->next)
  2167. goto gso;
  2168. } else {
  2169. if (skb_needs_linearize(skb, features) &&
  2170. __skb_linearize(skb))
  2171. goto out_kfree_skb;
  2172. /* If packet is not checksummed and device does not
  2173. * support checksumming for this protocol, complete
  2174. * checksumming here.
  2175. */
  2176. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2177. if (skb->encapsulation)
  2178. skb_set_inner_transport_header(skb,
  2179. skb_checksum_start_offset(skb));
  2180. else
  2181. skb_set_transport_header(skb,
  2182. skb_checksum_start_offset(skb));
  2183. if (!(features & NETIF_F_ALL_CSUM) &&
  2184. skb_checksum_help(skb))
  2185. goto out_kfree_skb;
  2186. }
  2187. }
  2188. if (!list_empty(&ptype_all))
  2189. dev_queue_xmit_nit(skb, dev);
  2190. skb_len = skb->len;
  2191. rc = ops->ndo_start_xmit(skb, dev);
  2192. trace_net_dev_xmit(skb, rc, dev, skb_len);
  2193. if (rc == NETDEV_TX_OK)
  2194. txq_trans_update(txq);
  2195. return rc;
  2196. }
  2197. gso:
  2198. do {
  2199. struct sk_buff *nskb = skb->next;
  2200. skb->next = nskb->next;
  2201. nskb->next = NULL;
  2202. if (!list_empty(&ptype_all))
  2203. dev_queue_xmit_nit(nskb, dev);
  2204. skb_len = nskb->len;
  2205. rc = ops->ndo_start_xmit(nskb, dev);
  2206. trace_net_dev_xmit(nskb, rc, dev, skb_len);
  2207. if (unlikely(rc != NETDEV_TX_OK)) {
  2208. if (rc & ~NETDEV_TX_MASK)
  2209. goto out_kfree_gso_skb;
  2210. nskb->next = skb->next;
  2211. skb->next = nskb;
  2212. return rc;
  2213. }
  2214. txq_trans_update(txq);
  2215. if (unlikely(netif_xmit_stopped(txq) && skb->next))
  2216. return NETDEV_TX_BUSY;
  2217. } while (skb->next);
  2218. out_kfree_gso_skb:
  2219. if (likely(skb->next == NULL)) {
  2220. skb->destructor = DEV_GSO_CB(skb)->destructor;
  2221. consume_skb(skb);
  2222. return rc;
  2223. }
  2224. out_kfree_skb:
  2225. kfree_skb(skb);
  2226. out:
  2227. return rc;
  2228. }
  2229. static void qdisc_pkt_len_init(struct sk_buff *skb)
  2230. {
  2231. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  2232. qdisc_skb_cb(skb)->pkt_len = skb->len;
  2233. /* To get more precise estimation of bytes sent on wire,
  2234. * we add to pkt_len the headers size of all segments
  2235. */
  2236. if (shinfo->gso_size) {
  2237. unsigned int hdr_len;
  2238. u16 gso_segs = shinfo->gso_segs;
  2239. /* mac layer + network layer */
  2240. hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
  2241. /* + transport layer */
  2242. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
  2243. hdr_len += tcp_hdrlen(skb);
  2244. else
  2245. hdr_len += sizeof(struct udphdr);
  2246. if (shinfo->gso_type & SKB_GSO_DODGY)
  2247. gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
  2248. shinfo->gso_size);
  2249. qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
  2250. }
  2251. }
  2252. static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  2253. struct net_device *dev,
  2254. struct netdev_queue *txq)
  2255. {
  2256. spinlock_t *root_lock = qdisc_lock(q);
  2257. bool contended;
  2258. int rc;
  2259. qdisc_pkt_len_init(skb);
  2260. qdisc_calculate_pkt_len(skb, q);
  2261. /*
  2262. * Heuristic to force contended enqueues to serialize on a
  2263. * separate lock before trying to get qdisc main lock.
  2264. * This permits __QDISC_STATE_RUNNING owner to get the lock more often
  2265. * and dequeue packets faster.
  2266. */
  2267. contended = qdisc_is_running(q);
  2268. if (unlikely(contended))
  2269. spin_lock(&q->busylock);
  2270. spin_lock(root_lock);
  2271. if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
  2272. kfree_skb(skb);
  2273. rc = NET_XMIT_DROP;
  2274. } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
  2275. qdisc_run_begin(q)) {
  2276. /*
  2277. * This is a work-conserving queue; there are no old skbs
  2278. * waiting to be sent out; and the qdisc is not running -
  2279. * xmit the skb directly.
  2280. */
  2281. if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
  2282. skb_dst_force(skb);
  2283. qdisc_bstats_update(q, skb);
  2284. if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
  2285. if (unlikely(contended)) {
  2286. spin_unlock(&q->busylock);
  2287. contended = false;
  2288. }
  2289. __qdisc_run(q);
  2290. } else
  2291. qdisc_run_end(q);
  2292. rc = NET_XMIT_SUCCESS;
  2293. } else {
  2294. skb_dst_force(skb);
  2295. rc = q->enqueue(skb, q) & NET_XMIT_MASK;
  2296. if (qdisc_run_begin(q)) {
  2297. if (unlikely(contended)) {
  2298. spin_unlock(&q->busylock);
  2299. contended = false;
  2300. }
  2301. __qdisc_run(q);
  2302. }
  2303. }
  2304. spin_unlock(root_lock);
  2305. if (unlikely(contended))
  2306. spin_unlock(&q->busylock);
  2307. return rc;
  2308. }
  2309. #if IS_ENABLED(CONFIG_NETPRIO_CGROUP)
  2310. static void skb_update_prio(struct sk_buff *skb)
  2311. {
  2312. struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap);
  2313. if (!skb->priority && skb->sk && map) {
  2314. unsigned int prioidx = skb->sk->sk_cgrp_prioidx;
  2315. if (prioidx < map->priomap_len)
  2316. skb->priority = map->priomap[prioidx];
  2317. }
  2318. }
  2319. #else
  2320. #define skb_update_prio(skb)
  2321. #endif
  2322. static DEFINE_PER_CPU(int, xmit_recursion);
  2323. #define RECURSION_LIMIT 10
  2324. /**
  2325. * dev_loopback_xmit - loop back @skb
  2326. * @skb: buffer to transmit
  2327. */
  2328. int dev_loopback_xmit(struct sk_buff *skb)
  2329. {
  2330. skb_reset_mac_header(skb);
  2331. __skb_pull(skb, skb_network_offset(skb));
  2332. skb->pkt_type = PACKET_LOOPBACK;
  2333. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2334. WARN_ON(!skb_dst(skb));
  2335. skb_dst_force(skb);
  2336. netif_rx_ni(skb);
  2337. return 0;
  2338. }
  2339. EXPORT_SYMBOL(dev_loopback_xmit);
  2340. /**
  2341. * dev_queue_xmit - transmit a buffer
  2342. * @skb: buffer to transmit
  2343. *
  2344. * Queue a buffer for transmission to a network device. The caller must
  2345. * have set the device and priority and built the buffer before calling
  2346. * this function. The function can be called from an interrupt.
  2347. *
  2348. * A negative errno code is returned on a failure. A success does not
  2349. * guarantee the frame will be transmitted as it may be dropped due
  2350. * to congestion or traffic shaping.
  2351. *
  2352. * -----------------------------------------------------------------------------------
  2353. * I notice this method can also return errors from the queue disciplines,
  2354. * including NET_XMIT_DROP, which is a positive value. So, errors can also
  2355. * be positive.
  2356. *
  2357. * Regardless of the return value, the skb is consumed, so it is currently
  2358. * difficult to retry a send to this method. (You can bump the ref count
  2359. * before sending to hold a reference for retry if you are careful.)
  2360. *
  2361. * When calling this method, interrupts MUST be enabled. This is because
  2362. * the BH enable code must have IRQs enabled so that it will not deadlock.
  2363. * --BLG
  2364. */
  2365. int dev_queue_xmit(struct sk_buff *skb)
  2366. {
  2367. struct net_device *dev = skb->dev;
  2368. struct netdev_queue *txq;
  2369. struct Qdisc *q;
  2370. int rc = -ENOMEM;
  2371. skb_reset_mac_header(skb);
  2372. /* Disable soft irqs for various locks below. Also
  2373. * stops preemption for RCU.
  2374. */
  2375. rcu_read_lock_bh();
  2376. skb_update_prio(skb);
  2377. txq = netdev_pick_tx(dev, skb);
  2378. q = rcu_dereference_bh(txq->qdisc);
  2379. #ifdef CONFIG_NET_CLS_ACT
  2380. skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
  2381. #endif
  2382. trace_net_dev_queue(skb);
  2383. if (q->enqueue) {
  2384. rc = __dev_xmit_skb(skb, q, dev, txq);
  2385. goto out;
  2386. }
  2387. /* The device has no queue. Common case for software devices:
  2388. loopback, all the sorts of tunnels...
  2389. Really, it is unlikely that netif_tx_lock protection is necessary
  2390. here. (f.e. loopback and IP tunnels are clean ignoring statistics
  2391. counters.)
  2392. However, it is possible, that they rely on protection
  2393. made by us here.
  2394. Check this and shot the lock. It is not prone from deadlocks.
  2395. Either shot noqueue qdisc, it is even simpler 8)
  2396. */
  2397. if (dev->flags & IFF_UP) {
  2398. int cpu = smp_processor_id(); /* ok because BHs are off */
  2399. if (txq->xmit_lock_owner != cpu) {
  2400. if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
  2401. goto recursion_alert;
  2402. HARD_TX_LOCK(dev, txq, cpu);
  2403. if (!netif_xmit_stopped(txq)) {
  2404. __this_cpu_inc(xmit_recursion);
  2405. rc = dev_hard_start_xmit(skb, dev, txq);
  2406. __this_cpu_dec(xmit_recursion);
  2407. if (dev_xmit_complete(rc)) {
  2408. HARD_TX_UNLOCK(dev, txq);
  2409. goto out;
  2410. }
  2411. }
  2412. HARD_TX_UNLOCK(dev, txq);
  2413. net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
  2414. dev->name);
  2415. } else {
  2416. /* Recursion is detected! It is possible,
  2417. * unfortunately
  2418. */
  2419. recursion_alert:
  2420. net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
  2421. dev->name);
  2422. }
  2423. }
  2424. rc = -ENETDOWN;
  2425. rcu_read_unlock_bh();
  2426. kfree_skb(skb);
  2427. return rc;
  2428. out:
  2429. rcu_read_unlock_bh();
  2430. return rc;
  2431. }
  2432. EXPORT_SYMBOL(dev_queue_xmit);
  2433. /*=======================================================================
  2434. Receiver routines
  2435. =======================================================================*/
  2436. int netdev_max_backlog __read_mostly = 1000;
  2437. EXPORT_SYMBOL(netdev_max_backlog);
  2438. int netdev_tstamp_prequeue __read_mostly = 1;
  2439. int netdev_budget __read_mostly = 300;
  2440. int weight_p __read_mostly = 64; /* old backlog weight */
  2441. /* Called with irq disabled */
  2442. static inline void ____napi_schedule(struct softnet_data *sd,
  2443. struct napi_struct *napi)
  2444. {
  2445. list_add_tail(&napi->poll_list, &sd->poll_list);
  2446. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  2447. }
  2448. #ifdef CONFIG_RPS
  2449. /* One global table that all flow-based protocols share. */
  2450. struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
  2451. EXPORT_SYMBOL(rps_sock_flow_table);
  2452. struct static_key rps_needed __read_mostly;
  2453. static struct rps_dev_flow *
  2454. set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  2455. struct rps_dev_flow *rflow, u16 next_cpu)
  2456. {
  2457. if (next_cpu != RPS_NO_CPU) {
  2458. #ifdef CONFIG_RFS_ACCEL
  2459. struct netdev_rx_queue *rxqueue;
  2460. struct rps_dev_flow_table *flow_table;
  2461. struct rps_dev_flow *old_rflow;
  2462. u32 flow_id;
  2463. u16 rxq_index;
  2464. int rc;
  2465. /* Should we steer this flow to a different hardware queue? */
  2466. if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
  2467. !(dev->features & NETIF_F_NTUPLE))
  2468. goto out;
  2469. rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
  2470. if (rxq_index == skb_get_rx_queue(skb))
  2471. goto out;
  2472. rxqueue = dev->_rx + rxq_index;
  2473. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  2474. if (!flow_table)
  2475. goto out;
  2476. flow_id = skb->rxhash & flow_table->mask;
  2477. rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
  2478. rxq_index, flow_id);
  2479. if (rc < 0)
  2480. goto out;
  2481. old_rflow = rflow;
  2482. rflow = &flow_table->flows[flow_id];
  2483. rflow->filter = rc;
  2484. if (old_rflow->filter == rflow->filter)
  2485. old_rflow->filter = RPS_NO_FILTER;
  2486. out:
  2487. #endif
  2488. rflow->last_qtail =
  2489. per_cpu(softnet_data, next_cpu).input_queue_head;
  2490. }
  2491. rflow->cpu = next_cpu;
  2492. return rflow;
  2493. }
  2494. /*
  2495. * get_rps_cpu is called from netif_receive_skb and returns the target
  2496. * CPU from the RPS map of the receiving queue for a given skb.
  2497. * rcu_read_lock must be held on entry.
  2498. */
  2499. static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  2500. struct rps_dev_flow **rflowp)
  2501. {
  2502. struct netdev_rx_queue *rxqueue;
  2503. struct rps_map *map;
  2504. struct rps_dev_flow_table *flow_table;
  2505. struct rps_sock_flow_table *sock_flow_table;
  2506. int cpu = -1;
  2507. u16 tcpu;
  2508. if (skb_rx_queue_recorded(skb)) {
  2509. u16 index = skb_get_rx_queue(skb);
  2510. if (unlikely(index >= dev->real_num_rx_queues)) {
  2511. WARN_ONCE(dev->real_num_rx_queues > 1,
  2512. "%s received packet on queue %u, but number "
  2513. "of RX queues is %u\n",
  2514. dev->name, index, dev->real_num_rx_queues);
  2515. goto done;
  2516. }
  2517. rxqueue = dev->_rx + index;
  2518. } else
  2519. rxqueue = dev->_rx;
  2520. map = rcu_dereference(rxqueue->rps_map);
  2521. if (map) {
  2522. if (map->len == 1 &&
  2523. !rcu_access_pointer(rxqueue->rps_flow_table)) {
  2524. tcpu = map->cpus[0];
  2525. if (cpu_online(tcpu))
  2526. cpu = tcpu;
  2527. goto done;
  2528. }
  2529. } else if (!rcu_access_pointer(rxqueue->rps_flow_table)) {
  2530. goto done;
  2531. }
  2532. skb_reset_network_header(skb);
  2533. if (!skb_get_rxhash(skb))
  2534. goto done;
  2535. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  2536. sock_flow_table = rcu_dereference(rps_sock_flow_table);
  2537. if (flow_table && sock_flow_table) {
  2538. u16 next_cpu;
  2539. struct rps_dev_flow *rflow;
  2540. rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
  2541. tcpu = rflow->cpu;
  2542. next_cpu = sock_flow_table->ents[skb->rxhash &
  2543. sock_flow_table->mask];
  2544. /*
  2545. * If the desired CPU (where last recvmsg was done) is
  2546. * different from current CPU (one in the rx-queue flow
  2547. * table entry), switch if one of the following holds:
  2548. * - Current CPU is unset (equal to RPS_NO_CPU).
  2549. * - Current CPU is offline.
  2550. * - The current CPU's queue tail has advanced beyond the
  2551. * last packet that was enqueued using this table entry.
  2552. * This guarantees that all previous packets for the flow
  2553. * have been dequeued, thus preserving in order delivery.
  2554. */
  2555. if (unlikely(tcpu != next_cpu) &&
  2556. (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
  2557. ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
  2558. rflow->last_qtail)) >= 0)) {
  2559. tcpu = next_cpu;
  2560. rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
  2561. }
  2562. if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
  2563. *rflowp = rflow;
  2564. cpu = tcpu;
  2565. goto done;
  2566. }
  2567. }
  2568. if (map) {
  2569. tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
  2570. if (cpu_online(tcpu)) {
  2571. cpu = tcpu;
  2572. goto done;
  2573. }
  2574. }
  2575. done:
  2576. return cpu;
  2577. }
  2578. #ifdef CONFIG_RFS_ACCEL
  2579. /**
  2580. * rps_may_expire_flow - check whether an RFS hardware filter may be removed
  2581. * @dev: Device on which the filter was set
  2582. * @rxq_index: RX queue index
  2583. * @flow_id: Flow ID passed to ndo_rx_flow_steer()
  2584. * @filter_id: Filter ID returned by ndo_rx_flow_steer()
  2585. *
  2586. * Drivers that implement ndo_rx_flow_steer() should periodically call
  2587. * this function for each installed filter and remove the filters for
  2588. * which it returns %true.
  2589. */
  2590. bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
  2591. u32 flow_id, u16 filter_id)
  2592. {
  2593. struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
  2594. struct rps_dev_flow_table *flow_table;
  2595. struct rps_dev_flow *rflow;
  2596. bool expire = true;
  2597. int cpu;
  2598. rcu_read_lock();
  2599. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  2600. if (flow_table && flow_id <= flow_table->mask) {
  2601. rflow = &flow_table->flows[flow_id];
  2602. cpu = ACCESS_ONCE(rflow->cpu);
  2603. if (rflow->filter == filter_id && cpu != RPS_NO_CPU &&
  2604. ((int)(per_cpu(softnet_data, cpu).input_queue_head -
  2605. rflow->last_qtail) <
  2606. (int)(10 * flow_table->mask)))
  2607. expire = false;
  2608. }
  2609. rcu_read_unlock();
  2610. return expire;
  2611. }
  2612. EXPORT_SYMBOL(rps_may_expire_flow);
  2613. #endif /* CONFIG_RFS_ACCEL */
  2614. /* Called from hardirq (IPI) context */
  2615. static void rps_trigger_softirq(void *data)
  2616. {
  2617. struct softnet_data *sd = data;
  2618. ____napi_schedule(sd, &sd->backlog);
  2619. sd->received_rps++;
  2620. }
  2621. #endif /* CONFIG_RPS */
  2622. /*
  2623. * Check if this softnet_data structure is another cpu one
  2624. * If yes, queue it to our IPI list and return 1
  2625. * If no, return 0
  2626. */
  2627. static int rps_ipi_queued(struct softnet_data *sd)
  2628. {
  2629. #ifdef CONFIG_RPS
  2630. struct softnet_data *mysd = &__get_cpu_var(softnet_data);
  2631. if (sd != mysd) {
  2632. sd->rps_ipi_next = mysd->rps_ipi_list;
  2633. mysd->rps_ipi_list = sd;
  2634. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  2635. return 1;
  2636. }
  2637. #endif /* CONFIG_RPS */
  2638. return 0;
  2639. }
  2640. /*
  2641. * enqueue_to_backlog is called to queue an skb to a per CPU backlog
  2642. * queue (may be a remote CPU queue).
  2643. */
  2644. static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
  2645. unsigned int *qtail)
  2646. {
  2647. struct softnet_data *sd;
  2648. unsigned long flags;
  2649. sd = &per_cpu(softnet_data, cpu);
  2650. local_irq_save(flags);
  2651. rps_lock(sd);
  2652. if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) {
  2653. if (skb_queue_len(&sd->input_pkt_queue)) {
  2654. enqueue:
  2655. __skb_queue_tail(&sd->input_pkt_queue, skb);
  2656. input_queue_tail_incr_save(sd, qtail);
  2657. rps_unlock(sd);
  2658. local_irq_restore(flags);
  2659. return NET_RX_SUCCESS;
  2660. }
  2661. /* Schedule NAPI for backlog device
  2662. * We can use non atomic operation since we own the queue lock
  2663. */
  2664. if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
  2665. if (!rps_ipi_queued(sd))
  2666. ____napi_schedule(sd, &sd->backlog);
  2667. }
  2668. goto enqueue;
  2669. }
  2670. sd->dropped++;
  2671. rps_unlock(sd);
  2672. local_irq_restore(flags);
  2673. atomic_long_inc(&skb->dev->rx_dropped);
  2674. kfree_skb(skb);
  2675. return NET_RX_DROP;
  2676. }
  2677. /**
  2678. * netif_rx - post buffer to the network code
  2679. * @skb: buffer to post
  2680. *
  2681. * This function receives a packet from a device driver and queues it for
  2682. * the upper (protocol) levels to process. It always succeeds. The buffer
  2683. * may be dropped during processing for congestion control or by the
  2684. * protocol layers.
  2685. *
  2686. * return values:
  2687. * NET_RX_SUCCESS (no congestion)
  2688. * NET_RX_DROP (packet was dropped)
  2689. *
  2690. */
  2691. int netif_rx(struct sk_buff *skb)
  2692. {
  2693. int ret;
  2694. /* if netpoll wants it, pretend we never saw it */
  2695. if (netpoll_rx(skb))
  2696. return NET_RX_DROP;
  2697. net_timestamp_check(netdev_tstamp_prequeue, skb);
  2698. trace_netif_rx(skb);
  2699. #ifdef CONFIG_RPS
  2700. if (static_key_false(&rps_needed)) {
  2701. struct rps_dev_flow voidflow, *rflow = &voidflow;
  2702. int cpu;
  2703. preempt_disable();
  2704. rcu_read_lock();
  2705. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  2706. if (cpu < 0)
  2707. cpu = smp_processor_id();
  2708. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  2709. rcu_read_unlock();
  2710. preempt_enable();
  2711. } else
  2712. #endif
  2713. {
  2714. unsigned int qtail;
  2715. ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  2716. put_cpu();
  2717. }
  2718. return ret;
  2719. }
  2720. EXPORT_SYMBOL(netif_rx);
  2721. int netif_rx_ni(struct sk_buff *skb)
  2722. {
  2723. int err;
  2724. preempt_disable();
  2725. err = netif_rx(skb);
  2726. if (local_softirq_pending())
  2727. do_softirq();
  2728. preempt_enable();
  2729. return err;
  2730. }
  2731. EXPORT_SYMBOL(netif_rx_ni);
  2732. static void net_tx_action(struct softirq_action *h)
  2733. {
  2734. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  2735. if (sd->completion_queue) {
  2736. struct sk_buff *clist;
  2737. local_irq_disable();
  2738. clist = sd->completion_queue;
  2739. sd->completion_queue = NULL;
  2740. local_irq_enable();
  2741. while (clist) {
  2742. struct sk_buff *skb = clist;
  2743. clist = clist->next;
  2744. WARN_ON(atomic_read(&skb->users));
  2745. trace_kfree_skb(skb, net_tx_action);
  2746. __kfree_skb(skb);
  2747. }
  2748. }
  2749. if (sd->output_queue) {
  2750. struct Qdisc *head;
  2751. local_irq_disable();
  2752. head = sd->output_queue;
  2753. sd->output_queue = NULL;
  2754. sd->output_queue_tailp = &sd->output_queue;
  2755. local_irq_enable();
  2756. while (head) {
  2757. struct Qdisc *q = head;
  2758. spinlock_t *root_lock;
  2759. head = head->next_sched;
  2760. root_lock = qdisc_lock(q);
  2761. if (spin_trylock(root_lock)) {
  2762. smp_mb__before_clear_bit();
  2763. clear_bit(__QDISC_STATE_SCHED,
  2764. &q->state);
  2765. qdisc_run(q);
  2766. spin_unlock(root_lock);
  2767. } else {
  2768. if (!test_bit(__QDISC_STATE_DEACTIVATED,
  2769. &q->state)) {
  2770. __netif_reschedule(q);
  2771. } else {
  2772. smp_mb__before_clear_bit();
  2773. clear_bit(__QDISC_STATE_SCHED,
  2774. &q->state);
  2775. }
  2776. }
  2777. }
  2778. }
  2779. }
  2780. #if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
  2781. (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
  2782. /* This hook is defined here for ATM LANE */
  2783. int (*br_fdb_test_addr_hook)(struct net_device *dev,
  2784. unsigned char *addr) __read_mostly;
  2785. EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
  2786. #endif
  2787. #ifdef CONFIG_NET_CLS_ACT
  2788. /* TODO: Maybe we should just force sch_ingress to be compiled in
  2789. * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
  2790. * a compare and 2 stores extra right now if we dont have it on
  2791. * but have CONFIG_NET_CLS_ACT
  2792. * NOTE: This doesn't stop any functionality; if you dont have
  2793. * the ingress scheduler, you just can't add policies on ingress.
  2794. *
  2795. */
  2796. static int ing_filter(struct sk_buff *skb, struct netdev_queue *rxq)
  2797. {
  2798. struct net_device *dev = skb->dev;
  2799. u32 ttl = G_TC_RTTL(skb->tc_verd);
  2800. int result = TC_ACT_OK;
  2801. struct Qdisc *q;
  2802. if (unlikely(MAX_RED_LOOP < ttl++)) {
  2803. net_warn_ratelimited("Redir loop detected Dropping packet (%d->%d)\n",
  2804. skb->skb_iif, dev->ifindex);
  2805. return TC_ACT_SHOT;
  2806. }
  2807. skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
  2808. skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
  2809. q = rxq->qdisc;
  2810. if (q != &noop_qdisc) {
  2811. spin_lock(qdisc_lock(q));
  2812. if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
  2813. result = qdisc_enqueue_root(skb, q);
  2814. spin_unlock(qdisc_lock(q));
  2815. }
  2816. return result;
  2817. }
  2818. static inline struct sk_buff *handle_ing(struct sk_buff *skb,
  2819. struct packet_type **pt_prev,
  2820. int *ret, struct net_device *orig_dev)
  2821. {
  2822. struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue);
  2823. if (!rxq || rxq->qdisc == &noop_qdisc)
  2824. goto out;
  2825. if (*pt_prev) {
  2826. *ret = deliver_skb(skb, *pt_prev, orig_dev);
  2827. *pt_prev = NULL;
  2828. }
  2829. switch (ing_filter(skb, rxq)) {
  2830. case TC_ACT_SHOT:
  2831. case TC_ACT_STOLEN:
  2832. kfree_skb(skb);
  2833. return NULL;
  2834. }
  2835. out:
  2836. skb->tc_verd = 0;
  2837. return skb;
  2838. }
  2839. #endif
  2840. /**
  2841. * netdev_rx_handler_register - register receive handler
  2842. * @dev: device to register a handler for
  2843. * @rx_handler: receive handler to register
  2844. * @rx_handler_data: data pointer that is used by rx handler
  2845. *
  2846. * Register a receive hander for a device. This handler will then be
  2847. * called from __netif_receive_skb. A negative errno code is returned
  2848. * on a failure.
  2849. *
  2850. * The caller must hold the rtnl_mutex.
  2851. *
  2852. * For a general description of rx_handler, see enum rx_handler_result.
  2853. */
  2854. int netdev_rx_handler_register(struct net_device *dev,
  2855. rx_handler_func_t *rx_handler,
  2856. void *rx_handler_data)
  2857. {
  2858. ASSERT_RTNL();
  2859. if (dev->rx_handler)
  2860. return -EBUSY;
  2861. /* Note: rx_handler_data must be set before rx_handler */
  2862. rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
  2863. rcu_assign_pointer(dev->rx_handler, rx_handler);
  2864. return 0;
  2865. }
  2866. EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
  2867. /**
  2868. * netdev_rx_handler_unregister - unregister receive handler
  2869. * @dev: device to unregister a handler from
  2870. *
  2871. * Unregister a receive handler from a device.
  2872. *
  2873. * The caller must hold the rtnl_mutex.
  2874. */
  2875. void netdev_rx_handler_unregister(struct net_device *dev)
  2876. {
  2877. ASSERT_RTNL();
  2878. RCU_INIT_POINTER(dev->rx_handler, NULL);
  2879. /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
  2880. * section has a guarantee to see a non NULL rx_handler_data
  2881. * as well.
  2882. */
  2883. synchronize_net();
  2884. RCU_INIT_POINTER(dev->rx_handler_data, NULL);
  2885. }
  2886. EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
  2887. /*
  2888. * Limit the use of PFMEMALLOC reserves to those protocols that implement
  2889. * the special handling of PFMEMALLOC skbs.
  2890. */
  2891. static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
  2892. {
  2893. switch (skb->protocol) {
  2894. case __constant_htons(ETH_P_ARP):
  2895. case __constant_htons(ETH_P_IP):
  2896. case __constant_htons(ETH_P_IPV6):
  2897. case __constant_htons(ETH_P_8021Q):
  2898. case __constant_htons(ETH_P_8021AD):
  2899. return true;
  2900. default:
  2901. return false;
  2902. }
  2903. }
  2904. static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc)
  2905. {
  2906. struct packet_type *ptype, *pt_prev;
  2907. rx_handler_func_t *rx_handler;
  2908. struct net_device *orig_dev;
  2909. struct net_device *null_or_dev;
  2910. bool deliver_exact = false;
  2911. int ret = NET_RX_DROP;
  2912. __be16 type;
  2913. net_timestamp_check(!netdev_tstamp_prequeue, skb);
  2914. trace_netif_receive_skb(skb);
  2915. /* if we've gotten here through NAPI, check netpoll */
  2916. if (netpoll_receive_skb(skb))
  2917. goto out;
  2918. orig_dev = skb->dev;
  2919. skb_reset_network_header(skb);
  2920. if (!skb_transport_header_was_set(skb))
  2921. skb_reset_transport_header(skb);
  2922. skb_reset_mac_len(skb);
  2923. pt_prev = NULL;
  2924. rcu_read_lock();
  2925. another_round:
  2926. skb->skb_iif = skb->dev->ifindex;
  2927. __this_cpu_inc(softnet_data.processed);
  2928. if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
  2929. skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
  2930. skb = vlan_untag(skb);
  2931. if (unlikely(!skb))
  2932. goto unlock;
  2933. }
  2934. #ifdef CONFIG_NET_CLS_ACT
  2935. if (skb->tc_verd & TC_NCLS) {
  2936. skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
  2937. goto ncls;
  2938. }
  2939. #endif
  2940. if (pfmemalloc)
  2941. goto skip_taps;
  2942. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  2943. if (!ptype->dev || ptype->dev == skb->dev) {
  2944. if (pt_prev)
  2945. ret = deliver_skb(skb, pt_prev, orig_dev);
  2946. pt_prev = ptype;
  2947. }
  2948. }
  2949. skip_taps:
  2950. #ifdef CONFIG_NET_CLS_ACT
  2951. skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
  2952. if (!skb)
  2953. goto unlock;
  2954. ncls:
  2955. #endif
  2956. if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
  2957. goto drop;
  2958. if (vlan_tx_tag_present(skb)) {
  2959. if (pt_prev) {
  2960. ret = deliver_skb(skb, pt_prev, orig_dev);
  2961. pt_prev = NULL;
  2962. }
  2963. if (vlan_do_receive(&skb))
  2964. goto another_round;
  2965. else if (unlikely(!skb))
  2966. goto unlock;
  2967. }
  2968. rx_handler = rcu_dereference(skb->dev->rx_handler);
  2969. if (rx_handler) {
  2970. if (pt_prev) {
  2971. ret = deliver_skb(skb, pt_prev, orig_dev);
  2972. pt_prev = NULL;
  2973. }
  2974. switch (rx_handler(&skb)) {
  2975. case RX_HANDLER_CONSUMED:
  2976. ret = NET_RX_SUCCESS;
  2977. goto unlock;
  2978. case RX_HANDLER_ANOTHER:
  2979. goto another_round;
  2980. case RX_HANDLER_EXACT:
  2981. deliver_exact = true;
  2982. case RX_HANDLER_PASS:
  2983. break;
  2984. default:
  2985. BUG();
  2986. }
  2987. }
  2988. if (vlan_tx_nonzero_tag_present(skb))
  2989. skb->pkt_type = PACKET_OTHERHOST;
  2990. /* deliver only exact match when indicated */
  2991. null_or_dev = deliver_exact ? skb->dev : NULL;
  2992. type = skb->protocol;
  2993. list_for_each_entry_rcu(ptype,
  2994. &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
  2995. if (ptype->type == type &&
  2996. (ptype->dev == null_or_dev || ptype->dev == skb->dev ||
  2997. ptype->dev == orig_dev)) {
  2998. if (pt_prev)
  2999. ret = deliver_skb(skb, pt_prev, orig_dev);
  3000. pt_prev = ptype;
  3001. }
  3002. }
  3003. if (pt_prev) {
  3004. if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
  3005. goto drop;
  3006. else
  3007. ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  3008. } else {
  3009. drop:
  3010. atomic_long_inc(&skb->dev->rx_dropped);
  3011. kfree_skb(skb);
  3012. /* Jamal, now you will not able to escape explaining
  3013. * me how you were going to use this. :-)
  3014. */
  3015. ret = NET_RX_DROP;
  3016. }
  3017. unlock:
  3018. rcu_read_unlock();
  3019. out:
  3020. return ret;
  3021. }
  3022. static int __netif_receive_skb(struct sk_buff *skb)
  3023. {
  3024. int ret;
  3025. if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
  3026. unsigned long pflags = current->flags;
  3027. /*
  3028. * PFMEMALLOC skbs are special, they should
  3029. * - be delivered to SOCK_MEMALLOC sockets only
  3030. * - stay away from userspace
  3031. * - have bounded memory usage
  3032. *
  3033. * Use PF_MEMALLOC as this saves us from propagating the allocation
  3034. * context down to all allocation sites.
  3035. */
  3036. current->flags |= PF_MEMALLOC;
  3037. ret = __netif_receive_skb_core(skb, true);
  3038. tsk_restore_flags(current, pflags, PF_MEMALLOC);
  3039. } else
  3040. ret = __netif_receive_skb_core(skb, false);
  3041. return ret;
  3042. }
  3043. /**
  3044. * netif_receive_skb - process receive buffer from network
  3045. * @skb: buffer to process
  3046. *
  3047. * netif_receive_skb() is the main receive data processing function.
  3048. * It always succeeds. The buffer may be dropped during processing
  3049. * for congestion control or by the protocol layers.
  3050. *
  3051. * This function may only be called from softirq context and interrupts
  3052. * should be enabled.
  3053. *
  3054. * Return values (usually ignored):
  3055. * NET_RX_SUCCESS: no congestion
  3056. * NET_RX_DROP: packet was dropped
  3057. */
  3058. int netif_receive_skb(struct sk_buff *skb)
  3059. {
  3060. net_timestamp_check(netdev_tstamp_prequeue, skb);
  3061. if (skb_defer_rx_timestamp(skb))
  3062. return NET_RX_SUCCESS;
  3063. #ifdef CONFIG_RPS
  3064. if (static_key_false(&rps_needed)) {
  3065. struct rps_dev_flow voidflow, *rflow = &voidflow;
  3066. int cpu, ret;
  3067. rcu_read_lock();
  3068. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  3069. if (cpu >= 0) {
  3070. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  3071. rcu_read_unlock();
  3072. return ret;
  3073. }
  3074. rcu_read_unlock();
  3075. }
  3076. #endif
  3077. return __netif_receive_skb(skb);
  3078. }
  3079. EXPORT_SYMBOL(netif_receive_skb);
  3080. /* Network device is going away, flush any packets still pending
  3081. * Called with irqs disabled.
  3082. */
  3083. static void flush_backlog(void *arg)
  3084. {
  3085. struct net_device *dev = arg;
  3086. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  3087. struct sk_buff *skb, *tmp;
  3088. rps_lock(sd);
  3089. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  3090. if (skb->dev == dev) {
  3091. __skb_unlink(skb, &sd->input_pkt_queue);
  3092. kfree_skb(skb);
  3093. input_queue_head_incr(sd);
  3094. }
  3095. }
  3096. rps_unlock(sd);
  3097. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  3098. if (skb->dev == dev) {
  3099. __skb_unlink(skb, &sd->process_queue);
  3100. kfree_skb(skb);
  3101. input_queue_head_incr(sd);
  3102. }
  3103. }
  3104. }
  3105. static int napi_gro_complete(struct sk_buff *skb)
  3106. {
  3107. struct packet_offload *ptype;
  3108. __be16 type = skb->protocol;
  3109. struct list_head *head = &offload_base;
  3110. int err = -ENOENT;
  3111. BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
  3112. if (NAPI_GRO_CB(skb)->count == 1) {
  3113. skb_shinfo(skb)->gso_size = 0;
  3114. goto out;
  3115. }
  3116. rcu_read_lock();
  3117. list_for_each_entry_rcu(ptype, head, list) {
  3118. if (ptype->type != type || !ptype->callbacks.gro_complete)
  3119. continue;
  3120. err = ptype->callbacks.gro_complete(skb);
  3121. break;
  3122. }
  3123. rcu_read_unlock();
  3124. if (err) {
  3125. WARN_ON(&ptype->list == head);
  3126. kfree_skb(skb);
  3127. return NET_RX_SUCCESS;
  3128. }
  3129. out:
  3130. return netif_receive_skb(skb);
  3131. }
  3132. /* napi->gro_list contains packets ordered by age.
  3133. * youngest packets at the head of it.
  3134. * Complete skbs in reverse order to reduce latencies.
  3135. */
  3136. void napi_gro_flush(struct napi_struct *napi, bool flush_old)
  3137. {
  3138. struct sk_buff *skb, *prev = NULL;
  3139. /* scan list and build reverse chain */
  3140. for (skb = napi->gro_list; skb != NULL; skb = skb->next) {
  3141. skb->prev = prev;
  3142. prev = skb;
  3143. }
  3144. for (skb = prev; skb; skb = prev) {
  3145. skb->next = NULL;
  3146. if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
  3147. return;
  3148. prev = skb->prev;
  3149. napi_gro_complete(skb);
  3150. napi->gro_count--;
  3151. }
  3152. napi->gro_list = NULL;
  3153. }
  3154. EXPORT_SYMBOL(napi_gro_flush);
  3155. static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb)
  3156. {
  3157. struct sk_buff *p;
  3158. unsigned int maclen = skb->dev->hard_header_len;
  3159. for (p = napi->gro_list; p; p = p->next) {
  3160. unsigned long diffs;
  3161. diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
  3162. diffs |= p->vlan_tci ^ skb->vlan_tci;
  3163. if (maclen == ETH_HLEN)
  3164. diffs |= compare_ether_header(skb_mac_header(p),
  3165. skb_gro_mac_header(skb));
  3166. else if (!diffs)
  3167. diffs = memcmp(skb_mac_header(p),
  3168. skb_gro_mac_header(skb),
  3169. maclen);
  3170. NAPI_GRO_CB(p)->same_flow = !diffs;
  3171. NAPI_GRO_CB(p)->flush = 0;
  3172. }
  3173. }
  3174. static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  3175. {
  3176. struct sk_buff **pp = NULL;
  3177. struct packet_offload *ptype;
  3178. __be16 type = skb->protocol;
  3179. struct list_head *head = &offload_base;
  3180. int same_flow;
  3181. enum gro_result ret;
  3182. if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb))
  3183. goto normal;
  3184. if (skb_is_gso(skb) || skb_has_frag_list(skb))
  3185. goto normal;
  3186. gro_list_prepare(napi, skb);
  3187. rcu_read_lock();
  3188. list_for_each_entry_rcu(ptype, head, list) {
  3189. if (ptype->type != type || !ptype->callbacks.gro_receive)
  3190. continue;
  3191. skb_set_network_header(skb, skb_gro_offset(skb));
  3192. skb_reset_mac_len(skb);
  3193. NAPI_GRO_CB(skb)->same_flow = 0;
  3194. NAPI_GRO_CB(skb)->flush = 0;
  3195. NAPI_GRO_CB(skb)->free = 0;
  3196. pp = ptype->callbacks.gro_receive(&napi->gro_list, skb);
  3197. break;
  3198. }
  3199. rcu_read_unlock();
  3200. if (&ptype->list == head)
  3201. goto normal;
  3202. same_flow = NAPI_GRO_CB(skb)->same_flow;
  3203. ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
  3204. if (pp) {
  3205. struct sk_buff *nskb = *pp;
  3206. *pp = nskb->next;
  3207. nskb->next = NULL;
  3208. napi_gro_complete(nskb);
  3209. napi->gro_count--;
  3210. }
  3211. if (same_flow)
  3212. goto ok;
  3213. if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS)
  3214. goto normal;
  3215. napi->gro_count++;
  3216. NAPI_GRO_CB(skb)->count = 1;
  3217. NAPI_GRO_CB(skb)->age = jiffies;
  3218. skb_shinfo(skb)->gso_size = skb_gro_len(skb);
  3219. skb->next = napi->gro_list;
  3220. napi->gro_list = skb;
  3221. ret = GRO_HELD;
  3222. pull:
  3223. if (skb_headlen(skb) < skb_gro_offset(skb)) {
  3224. int grow = skb_gro_offset(skb) - skb_headlen(skb);
  3225. BUG_ON(skb->end - skb->tail < grow);
  3226. memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
  3227. skb->tail += grow;
  3228. skb->data_len -= grow;
  3229. skb_shinfo(skb)->frags[0].page_offset += grow;
  3230. skb_frag_size_sub(&skb_shinfo(skb)->frags[0], grow);
  3231. if (unlikely(!skb_frag_size(&skb_shinfo(skb)->frags[0]))) {
  3232. skb_frag_unref(skb, 0);
  3233. memmove(skb_shinfo(skb)->frags,
  3234. skb_shinfo(skb)->frags + 1,
  3235. --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
  3236. }
  3237. }
  3238. ok:
  3239. return ret;
  3240. normal:
  3241. ret = GRO_NORMAL;
  3242. goto pull;
  3243. }
  3244. static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
  3245. {
  3246. switch (ret) {
  3247. case GRO_NORMAL:
  3248. if (netif_receive_skb(skb))
  3249. ret = GRO_DROP;
  3250. break;
  3251. case GRO_DROP:
  3252. kfree_skb(skb);
  3253. break;
  3254. case GRO_MERGED_FREE:
  3255. if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
  3256. kmem_cache_free(skbuff_head_cache, skb);
  3257. else
  3258. __kfree_skb(skb);
  3259. break;
  3260. case GRO_HELD:
  3261. case GRO_MERGED:
  3262. break;
  3263. }
  3264. return ret;
  3265. }
  3266. static void skb_gro_reset_offset(struct sk_buff *skb)
  3267. {
  3268. const struct skb_shared_info *pinfo = skb_shinfo(skb);
  3269. const skb_frag_t *frag0 = &pinfo->frags[0];
  3270. NAPI_GRO_CB(skb)->data_offset = 0;
  3271. NAPI_GRO_CB(skb)->frag0 = NULL;
  3272. NAPI_GRO_CB(skb)->frag0_len = 0;
  3273. if (skb->mac_header == skb->tail &&
  3274. pinfo->nr_frags &&
  3275. !PageHighMem(skb_frag_page(frag0))) {
  3276. NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
  3277. NAPI_GRO_CB(skb)->frag0_len = skb_frag_size(frag0);
  3278. }
  3279. }
  3280. gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  3281. {
  3282. skb_gro_reset_offset(skb);
  3283. return napi_skb_finish(dev_gro_receive(napi, skb), skb);
  3284. }
  3285. EXPORT_SYMBOL(napi_gro_receive);
  3286. static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
  3287. {
  3288. __skb_pull(skb, skb_headlen(skb));
  3289. /* restore the reserve we had after netdev_alloc_skb_ip_align() */
  3290. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
  3291. skb->vlan_tci = 0;
  3292. skb->dev = napi->dev;
  3293. skb->skb_iif = 0;
  3294. napi->skb = skb;
  3295. }
  3296. struct sk_buff *napi_get_frags(struct napi_struct *napi)
  3297. {
  3298. struct sk_buff *skb = napi->skb;
  3299. if (!skb) {
  3300. skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
  3301. if (skb)
  3302. napi->skb = skb;
  3303. }
  3304. return skb;
  3305. }
  3306. EXPORT_SYMBOL(napi_get_frags);
  3307. static gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb,
  3308. gro_result_t ret)
  3309. {
  3310. switch (ret) {
  3311. case GRO_NORMAL:
  3312. case GRO_HELD:
  3313. skb->protocol = eth_type_trans(skb, skb->dev);
  3314. if (ret == GRO_HELD)
  3315. skb_gro_pull(skb, -ETH_HLEN);
  3316. else if (netif_receive_skb(skb))
  3317. ret = GRO_DROP;
  3318. break;
  3319. case GRO_DROP:
  3320. case GRO_MERGED_FREE:
  3321. napi_reuse_skb(napi, skb);
  3322. break;
  3323. case GRO_MERGED:
  3324. break;
  3325. }
  3326. return ret;
  3327. }
  3328. static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
  3329. {
  3330. struct sk_buff *skb = napi->skb;
  3331. struct ethhdr *eth;
  3332. unsigned int hlen;
  3333. unsigned int off;
  3334. napi->skb = NULL;
  3335. skb_reset_mac_header(skb);
  3336. skb_gro_reset_offset(skb);
  3337. off = skb_gro_offset(skb);
  3338. hlen = off + sizeof(*eth);
  3339. eth = skb_gro_header_fast(skb, off);
  3340. if (skb_gro_header_hard(skb, hlen)) {
  3341. eth = skb_gro_header_slow(skb, hlen, off);
  3342. if (unlikely(!eth)) {
  3343. napi_reuse_skb(napi, skb);
  3344. skb = NULL;
  3345. goto out;
  3346. }
  3347. }
  3348. skb_gro_pull(skb, sizeof(*eth));
  3349. /*
  3350. * This works because the only protocols we care about don't require
  3351. * special handling. We'll fix it up properly at the end.
  3352. */
  3353. skb->protocol = eth->h_proto;
  3354. out:
  3355. return skb;
  3356. }
  3357. gro_result_t napi_gro_frags(struct napi_struct *napi)
  3358. {
  3359. struct sk_buff *skb = napi_frags_skb(napi);
  3360. if (!skb)
  3361. return GRO_DROP;
  3362. return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
  3363. }
  3364. EXPORT_SYMBOL(napi_gro_frags);
  3365. /*
  3366. * net_rps_action sends any pending IPI's for rps.
  3367. * Note: called with local irq disabled, but exits with local irq enabled.
  3368. */
  3369. static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  3370. {
  3371. #ifdef CONFIG_RPS
  3372. struct softnet_data *remsd = sd->rps_ipi_list;
  3373. if (remsd) {
  3374. sd->rps_ipi_list = NULL;
  3375. local_irq_enable();
  3376. /* Send pending IPI's to kick RPS processing on remote cpus. */
  3377. while (remsd) {
  3378. struct softnet_data *next = remsd->rps_ipi_next;
  3379. if (cpu_online(remsd->cpu))
  3380. __smp_call_function_single(remsd->cpu,
  3381. &remsd->csd, 0);
  3382. remsd = next;
  3383. }
  3384. } else
  3385. #endif
  3386. local_irq_enable();
  3387. }
  3388. static int process_backlog(struct napi_struct *napi, int quota)
  3389. {
  3390. int work = 0;
  3391. struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
  3392. #ifdef CONFIG_RPS
  3393. /* Check if we have pending ipi, its better to send them now,
  3394. * not waiting net_rx_action() end.
  3395. */
  3396. if (sd->rps_ipi_list) {
  3397. local_irq_disable();
  3398. net_rps_action_and_irq_enable(sd);
  3399. }
  3400. #endif
  3401. napi->weight = weight_p;
  3402. local_irq_disable();
  3403. while (work < quota) {
  3404. struct sk_buff *skb;
  3405. unsigned int qlen;
  3406. while ((skb = __skb_dequeue(&sd->process_queue))) {
  3407. local_irq_enable();
  3408. __netif_receive_skb(skb);
  3409. local_irq_disable();
  3410. input_queue_head_incr(sd);
  3411. if (++work >= quota) {
  3412. local_irq_enable();
  3413. return work;
  3414. }
  3415. }
  3416. rps_lock(sd);
  3417. qlen = skb_queue_len(&sd->input_pkt_queue);
  3418. if (qlen)
  3419. skb_queue_splice_tail_init(&sd->input_pkt_queue,
  3420. &sd->process_queue);
  3421. if (qlen < quota - work) {
  3422. /*
  3423. * Inline a custom version of __napi_complete().
  3424. * only current cpu owns and manipulates this napi,
  3425. * and NAPI_STATE_SCHED is the only possible flag set on backlog.
  3426. * we can use a plain write instead of clear_bit(),
  3427. * and we dont need an smp_mb() memory barrier.
  3428. */
  3429. list_del(&napi->poll_list);
  3430. napi->state = 0;
  3431. quota = work + qlen;
  3432. }
  3433. rps_unlock(sd);
  3434. }
  3435. local_irq_enable();
  3436. return work;
  3437. }
  3438. /**
  3439. * __napi_schedule - schedule for receive
  3440. * @n: entry to schedule
  3441. *
  3442. * The entry's receive function will be scheduled to run
  3443. */
  3444. void __napi_schedule(struct napi_struct *n)
  3445. {
  3446. unsigned long flags;
  3447. local_irq_save(flags);
  3448. ____napi_schedule(&__get_cpu_var(softnet_data), n);
  3449. local_irq_restore(flags);
  3450. }
  3451. EXPORT_SYMBOL(__napi_schedule);
  3452. void __napi_complete(struct napi_struct *n)
  3453. {
  3454. BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
  3455. BUG_ON(n->gro_list);
  3456. list_del(&n->poll_list);
  3457. smp_mb__before_clear_bit();
  3458. clear_bit(NAPI_STATE_SCHED, &n->state);
  3459. }
  3460. EXPORT_SYMBOL(__napi_complete);
  3461. void napi_complete(struct napi_struct *n)
  3462. {
  3463. unsigned long flags;
  3464. /*
  3465. * don't let napi dequeue from the cpu poll list
  3466. * just in case its running on a different cpu
  3467. */
  3468. if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
  3469. return;
  3470. napi_gro_flush(n, false);
  3471. local_irq_save(flags);
  3472. __napi_complete(n);
  3473. local_irq_restore(flags);
  3474. }
  3475. EXPORT_SYMBOL(napi_complete);
  3476. void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
  3477. int (*poll)(struct napi_struct *, int), int weight)
  3478. {
  3479. INIT_LIST_HEAD(&napi->poll_list);
  3480. napi->gro_count = 0;
  3481. napi->gro_list = NULL;
  3482. napi->skb = NULL;
  3483. napi->poll = poll;
  3484. if (weight > NAPI_POLL_WEIGHT)
  3485. pr_err_once("netif_napi_add() called with weight %d on device %s\n",
  3486. weight, dev->name);
  3487. napi->weight = weight;
  3488. list_add(&napi->dev_list, &dev->napi_list);
  3489. napi->dev = dev;
  3490. #ifdef CONFIG_NETPOLL
  3491. spin_lock_init(&napi->poll_lock);
  3492. napi->poll_owner = -1;
  3493. #endif
  3494. set_bit(NAPI_STATE_SCHED, &napi->state);
  3495. }
  3496. EXPORT_SYMBOL(netif_napi_add);
  3497. void netif_napi_del(struct napi_struct *napi)
  3498. {
  3499. struct sk_buff *skb, *next;
  3500. list_del_init(&napi->dev_list);
  3501. napi_free_frags(napi);
  3502. for (skb = napi->gro_list; skb; skb = next) {
  3503. next = skb->next;
  3504. skb->next = NULL;
  3505. kfree_skb(skb);
  3506. }
  3507. napi->gro_list = NULL;
  3508. napi->gro_count = 0;
  3509. }
  3510. EXPORT_SYMBOL(netif_napi_del);
  3511. static void net_rx_action(struct softirq_action *h)
  3512. {
  3513. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  3514. unsigned long time_limit = jiffies + 2;
  3515. int budget = netdev_budget;
  3516. void *have;
  3517. local_irq_disable();
  3518. while (!list_empty(&sd->poll_list)) {
  3519. struct napi_struct *n;
  3520. int work, weight;
  3521. /* If softirq window is exhuasted then punt.
  3522. * Allow this to run for 2 jiffies since which will allow
  3523. * an average latency of 1.5/HZ.
  3524. */
  3525. if (unlikely(budget <= 0 || time_after_eq(jiffies, time_limit)))
  3526. goto softnet_break;
  3527. local_irq_enable();
  3528. /* Even though interrupts have been re-enabled, this
  3529. * access is safe because interrupts can only add new
  3530. * entries to the tail of this list, and only ->poll()
  3531. * calls can remove this head entry from the list.
  3532. */
  3533. n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list);
  3534. have = netpoll_poll_lock(n);
  3535. weight = n->weight;
  3536. /* This NAPI_STATE_SCHED test is for avoiding a race
  3537. * with netpoll's poll_napi(). Only the entity which
  3538. * obtains the lock and sees NAPI_STATE_SCHED set will
  3539. * actually make the ->poll() call. Therefore we avoid
  3540. * accidentally calling ->poll() when NAPI is not scheduled.
  3541. */
  3542. work = 0;
  3543. if (test_bit(NAPI_STATE_SCHED, &n->state)) {
  3544. work = n->poll(n, weight);
  3545. trace_napi_poll(n);
  3546. }
  3547. WARN_ON_ONCE(work > weight);
  3548. budget -= work;
  3549. local_irq_disable();
  3550. /* Drivers must not modify the NAPI state if they
  3551. * consume the entire weight. In such cases this code
  3552. * still "owns" the NAPI instance and therefore can
  3553. * move the instance around on the list at-will.
  3554. */
  3555. if (unlikely(work == weight)) {
  3556. if (unlikely(napi_disable_pending(n))) {
  3557. local_irq_enable();
  3558. napi_complete(n);
  3559. local_irq_disable();
  3560. } else {
  3561. if (n->gro_list) {
  3562. /* flush too old packets
  3563. * If HZ < 1000, flush all packets.
  3564. */
  3565. local_irq_enable();
  3566. napi_gro_flush(n, HZ >= 1000);
  3567. local_irq_disable();
  3568. }
  3569. list_move_tail(&n->poll_list, &sd->poll_list);
  3570. }
  3571. }
  3572. netpoll_poll_unlock(have);
  3573. }
  3574. out:
  3575. net_rps_action_and_irq_enable(sd);
  3576. #ifdef CONFIG_NET_DMA
  3577. /*
  3578. * There may not be any more sk_buffs coming right now, so push
  3579. * any pending DMA copies to hardware
  3580. */
  3581. dma_issue_pending_all();
  3582. #endif
  3583. return;
  3584. softnet_break:
  3585. sd->time_squeeze++;
  3586. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  3587. goto out;
  3588. }
  3589. struct netdev_upper {
  3590. struct net_device *dev;
  3591. bool master;
  3592. struct list_head list;
  3593. struct rcu_head rcu;
  3594. struct list_head search_list;
  3595. };
  3596. static void __append_search_uppers(struct list_head *search_list,
  3597. struct net_device *dev)
  3598. {
  3599. struct netdev_upper *upper;
  3600. list_for_each_entry(upper, &dev->upper_dev_list, list) {
  3601. /* check if this upper is not already in search list */
  3602. if (list_empty(&upper->search_list))
  3603. list_add_tail(&upper->search_list, search_list);
  3604. }
  3605. }
  3606. static bool __netdev_search_upper_dev(struct net_device *dev,
  3607. struct net_device *upper_dev)
  3608. {
  3609. LIST_HEAD(search_list);
  3610. struct netdev_upper *upper;
  3611. struct netdev_upper *tmp;
  3612. bool ret = false;
  3613. __append_search_uppers(&search_list, dev);
  3614. list_for_each_entry(upper, &search_list, search_list) {
  3615. if (upper->dev == upper_dev) {
  3616. ret = true;
  3617. break;
  3618. }
  3619. __append_search_uppers(&search_list, upper->dev);
  3620. }
  3621. list_for_each_entry_safe(upper, tmp, &search_list, search_list)
  3622. INIT_LIST_HEAD(&upper->search_list);
  3623. return ret;
  3624. }
  3625. static struct netdev_upper *__netdev_find_upper(struct net_device *dev,
  3626. struct net_device *upper_dev)
  3627. {
  3628. struct netdev_upper *upper;
  3629. list_for_each_entry(upper, &dev->upper_dev_list, list) {
  3630. if (upper->dev == upper_dev)
  3631. return upper;
  3632. }
  3633. return NULL;
  3634. }
  3635. /**
  3636. * netdev_has_upper_dev - Check if device is linked to an upper device
  3637. * @dev: device
  3638. * @upper_dev: upper device to check
  3639. *
  3640. * Find out if a device is linked to specified upper device and return true
  3641. * in case it is. Note that this checks only immediate upper device,
  3642. * not through a complete stack of devices. The caller must hold the RTNL lock.
  3643. */
  3644. bool netdev_has_upper_dev(struct net_device *dev,
  3645. struct net_device *upper_dev)
  3646. {
  3647. ASSERT_RTNL();
  3648. return __netdev_find_upper(dev, upper_dev);
  3649. }
  3650. EXPORT_SYMBOL(netdev_has_upper_dev);
  3651. /**
  3652. * netdev_has_any_upper_dev - Check if device is linked to some device
  3653. * @dev: device
  3654. *
  3655. * Find out if a device is linked to an upper device and return true in case
  3656. * it is. The caller must hold the RTNL lock.
  3657. */
  3658. bool netdev_has_any_upper_dev(struct net_device *dev)
  3659. {
  3660. ASSERT_RTNL();
  3661. return !list_empty(&dev->upper_dev_list);
  3662. }
  3663. EXPORT_SYMBOL(netdev_has_any_upper_dev);
  3664. /**
  3665. * netdev_master_upper_dev_get - Get master upper device
  3666. * @dev: device
  3667. *
  3668. * Find a master upper device and return pointer to it or NULL in case
  3669. * it's not there. The caller must hold the RTNL lock.
  3670. */
  3671. struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
  3672. {
  3673. struct netdev_upper *upper;
  3674. ASSERT_RTNL();
  3675. if (list_empty(&dev->upper_dev_list))
  3676. return NULL;
  3677. upper = list_first_entry(&dev->upper_dev_list,
  3678. struct netdev_upper, list);
  3679. if (likely(upper->master))
  3680. return upper->dev;
  3681. return NULL;
  3682. }
  3683. EXPORT_SYMBOL(netdev_master_upper_dev_get);
  3684. /**
  3685. * netdev_master_upper_dev_get_rcu - Get master upper device
  3686. * @dev: device
  3687. *
  3688. * Find a master upper device and return pointer to it or NULL in case
  3689. * it's not there. The caller must hold the RCU read lock.
  3690. */
  3691. struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
  3692. {
  3693. struct netdev_upper *upper;
  3694. upper = list_first_or_null_rcu(&dev->upper_dev_list,
  3695. struct netdev_upper, list);
  3696. if (upper && likely(upper->master))
  3697. return upper->dev;
  3698. return NULL;
  3699. }
  3700. EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
  3701. static int __netdev_upper_dev_link(struct net_device *dev,
  3702. struct net_device *upper_dev, bool master)
  3703. {
  3704. struct netdev_upper *upper;
  3705. ASSERT_RTNL();
  3706. if (dev == upper_dev)
  3707. return -EBUSY;
  3708. /* To prevent loops, check if dev is not upper device to upper_dev. */
  3709. if (__netdev_search_upper_dev(upper_dev, dev))
  3710. return -EBUSY;
  3711. if (__netdev_find_upper(dev, upper_dev))
  3712. return -EEXIST;
  3713. if (master && netdev_master_upper_dev_get(dev))
  3714. return -EBUSY;
  3715. upper = kmalloc(sizeof(*upper), GFP_KERNEL);
  3716. if (!upper)
  3717. return -ENOMEM;
  3718. upper->dev = upper_dev;
  3719. upper->master = master;
  3720. INIT_LIST_HEAD(&upper->search_list);
  3721. /* Ensure that master upper link is always the first item in list. */
  3722. if (master)
  3723. list_add_rcu(&upper->list, &dev->upper_dev_list);
  3724. else
  3725. list_add_tail_rcu(&upper->list, &dev->upper_dev_list);
  3726. dev_hold(upper_dev);
  3727. return 0;
  3728. }
  3729. /**
  3730. * netdev_upper_dev_link - Add a link to the upper device
  3731. * @dev: device
  3732. * @upper_dev: new upper device
  3733. *
  3734. * Adds a link to device which is upper to this one. The caller must hold
  3735. * the RTNL lock. On a failure a negative errno code is returned.
  3736. * On success the reference counts are adjusted and the function
  3737. * returns zero.
  3738. */
  3739. int netdev_upper_dev_link(struct net_device *dev,
  3740. struct net_device *upper_dev)
  3741. {
  3742. return __netdev_upper_dev_link(dev, upper_dev, false);
  3743. }
  3744. EXPORT_SYMBOL(netdev_upper_dev_link);
  3745. /**
  3746. * netdev_master_upper_dev_link - Add a master link to the upper device
  3747. * @dev: device
  3748. * @upper_dev: new upper device
  3749. *
  3750. * Adds a link to device which is upper to this one. In this case, only
  3751. * one master upper device can be linked, although other non-master devices
  3752. * might be linked as well. The caller must hold the RTNL lock.
  3753. * On a failure a negative errno code is returned. On success the reference
  3754. * counts are adjusted and the function returns zero.
  3755. */
  3756. int netdev_master_upper_dev_link(struct net_device *dev,
  3757. struct net_device *upper_dev)
  3758. {
  3759. return __netdev_upper_dev_link(dev, upper_dev, true);
  3760. }
  3761. EXPORT_SYMBOL(netdev_master_upper_dev_link);
  3762. /**
  3763. * netdev_upper_dev_unlink - Removes a link to upper device
  3764. * @dev: device
  3765. * @upper_dev: new upper device
  3766. *
  3767. * Removes a link to device which is upper to this one. The caller must hold
  3768. * the RTNL lock.
  3769. */
  3770. void netdev_upper_dev_unlink(struct net_device *dev,
  3771. struct net_device *upper_dev)
  3772. {
  3773. struct netdev_upper *upper;
  3774. ASSERT_RTNL();
  3775. upper = __netdev_find_upper(dev, upper_dev);
  3776. if (!upper)
  3777. return;
  3778. list_del_rcu(&upper->list);
  3779. dev_put(upper_dev);
  3780. kfree_rcu(upper, rcu);
  3781. }
  3782. EXPORT_SYMBOL(netdev_upper_dev_unlink);
  3783. static void dev_change_rx_flags(struct net_device *dev, int flags)
  3784. {
  3785. const struct net_device_ops *ops = dev->netdev_ops;
  3786. if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags)
  3787. ops->ndo_change_rx_flags(dev, flags);
  3788. }
  3789. static int __dev_set_promiscuity(struct net_device *dev, int inc)
  3790. {
  3791. unsigned int old_flags = dev->flags;
  3792. kuid_t uid;
  3793. kgid_t gid;
  3794. ASSERT_RTNL();
  3795. dev->flags |= IFF_PROMISC;
  3796. dev->promiscuity += inc;
  3797. if (dev->promiscuity == 0) {
  3798. /*
  3799. * Avoid overflow.
  3800. * If inc causes overflow, untouch promisc and return error.
  3801. */
  3802. if (inc < 0)
  3803. dev->flags &= ~IFF_PROMISC;
  3804. else {
  3805. dev->promiscuity -= inc;
  3806. pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
  3807. dev->name);
  3808. return -EOVERFLOW;
  3809. }
  3810. }
  3811. if (dev->flags != old_flags) {
  3812. pr_info("device %s %s promiscuous mode\n",
  3813. dev->name,
  3814. dev->flags & IFF_PROMISC ? "entered" : "left");
  3815. if (audit_enabled) {
  3816. current_uid_gid(&uid, &gid);
  3817. audit_log(current->audit_context, GFP_ATOMIC,
  3818. AUDIT_ANOM_PROMISCUOUS,
  3819. "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
  3820. dev->name, (dev->flags & IFF_PROMISC),
  3821. (old_flags & IFF_PROMISC),
  3822. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  3823. from_kuid(&init_user_ns, uid),
  3824. from_kgid(&init_user_ns, gid),
  3825. audit_get_sessionid(current));
  3826. }
  3827. dev_change_rx_flags(dev, IFF_PROMISC);
  3828. }
  3829. return 0;
  3830. }
  3831. /**
  3832. * dev_set_promiscuity - update promiscuity count on a device
  3833. * @dev: device
  3834. * @inc: modifier
  3835. *
  3836. * Add or remove promiscuity from a device. While the count in the device
  3837. * remains above zero the interface remains promiscuous. Once it hits zero
  3838. * the device reverts back to normal filtering operation. A negative inc
  3839. * value is used to drop promiscuity on the device.
  3840. * Return 0 if successful or a negative errno code on error.
  3841. */
  3842. int dev_set_promiscuity(struct net_device *dev, int inc)
  3843. {
  3844. unsigned int old_flags = dev->flags;
  3845. int err;
  3846. err = __dev_set_promiscuity(dev, inc);
  3847. if (err < 0)
  3848. return err;
  3849. if (dev->flags != old_flags)
  3850. dev_set_rx_mode(dev);
  3851. return err;
  3852. }
  3853. EXPORT_SYMBOL(dev_set_promiscuity);
  3854. /**
  3855. * dev_set_allmulti - update allmulti count on a device
  3856. * @dev: device
  3857. * @inc: modifier
  3858. *
  3859. * Add or remove reception of all multicast frames to a device. While the
  3860. * count in the device remains above zero the interface remains listening
  3861. * to all interfaces. Once it hits zero the device reverts back to normal
  3862. * filtering operation. A negative @inc value is used to drop the counter
  3863. * when releasing a resource needing all multicasts.
  3864. * Return 0 if successful or a negative errno code on error.
  3865. */
  3866. int dev_set_allmulti(struct net_device *dev, int inc)
  3867. {
  3868. unsigned int old_flags = dev->flags;
  3869. ASSERT_RTNL();
  3870. dev->flags |= IFF_ALLMULTI;
  3871. dev->allmulti += inc;
  3872. if (dev->allmulti == 0) {
  3873. /*
  3874. * Avoid overflow.
  3875. * If inc causes overflow, untouch allmulti and return error.
  3876. */
  3877. if (inc < 0)
  3878. dev->flags &= ~IFF_ALLMULTI;
  3879. else {
  3880. dev->allmulti -= inc;
  3881. pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
  3882. dev->name);
  3883. return -EOVERFLOW;
  3884. }
  3885. }
  3886. if (dev->flags ^ old_flags) {
  3887. dev_change_rx_flags(dev, IFF_ALLMULTI);
  3888. dev_set_rx_mode(dev);
  3889. }
  3890. return 0;
  3891. }
  3892. EXPORT_SYMBOL(dev_set_allmulti);
  3893. /*
  3894. * Upload unicast and multicast address lists to device and
  3895. * configure RX filtering. When the device doesn't support unicast
  3896. * filtering it is put in promiscuous mode while unicast addresses
  3897. * are present.
  3898. */
  3899. void __dev_set_rx_mode(struct net_device *dev)
  3900. {
  3901. const struct net_device_ops *ops = dev->netdev_ops;
  3902. /* dev_open will call this function so the list will stay sane. */
  3903. if (!(dev->flags&IFF_UP))
  3904. return;
  3905. if (!netif_device_present(dev))
  3906. return;
  3907. if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
  3908. /* Unicast addresses changes may only happen under the rtnl,
  3909. * therefore calling __dev_set_promiscuity here is safe.
  3910. */
  3911. if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
  3912. __dev_set_promiscuity(dev, 1);
  3913. dev->uc_promisc = true;
  3914. } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
  3915. __dev_set_promiscuity(dev, -1);
  3916. dev->uc_promisc = false;
  3917. }
  3918. }
  3919. if (ops->ndo_set_rx_mode)
  3920. ops->ndo_set_rx_mode(dev);
  3921. }
  3922. void dev_set_rx_mode(struct net_device *dev)
  3923. {
  3924. netif_addr_lock_bh(dev);
  3925. __dev_set_rx_mode(dev);
  3926. netif_addr_unlock_bh(dev);
  3927. }
  3928. /**
  3929. * dev_get_flags - get flags reported to userspace
  3930. * @dev: device
  3931. *
  3932. * Get the combination of flag bits exported through APIs to userspace.
  3933. */
  3934. unsigned int dev_get_flags(const struct net_device *dev)
  3935. {
  3936. unsigned int flags;
  3937. flags = (dev->flags & ~(IFF_PROMISC |
  3938. IFF_ALLMULTI |
  3939. IFF_RUNNING |
  3940. IFF_LOWER_UP |
  3941. IFF_DORMANT)) |
  3942. (dev->gflags & (IFF_PROMISC |
  3943. IFF_ALLMULTI));
  3944. if (netif_running(dev)) {
  3945. if (netif_oper_up(dev))
  3946. flags |= IFF_RUNNING;
  3947. if (netif_carrier_ok(dev))
  3948. flags |= IFF_LOWER_UP;
  3949. if (netif_dormant(dev))
  3950. flags |= IFF_DORMANT;
  3951. }
  3952. return flags;
  3953. }
  3954. EXPORT_SYMBOL(dev_get_flags);
  3955. int __dev_change_flags(struct net_device *dev, unsigned int flags)
  3956. {
  3957. unsigned int old_flags = dev->flags;
  3958. int ret;
  3959. ASSERT_RTNL();
  3960. /*
  3961. * Set the flags on our device.
  3962. */
  3963. dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
  3964. IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
  3965. IFF_AUTOMEDIA)) |
  3966. (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
  3967. IFF_ALLMULTI));
  3968. /*
  3969. * Load in the correct multicast list now the flags have changed.
  3970. */
  3971. if ((old_flags ^ flags) & IFF_MULTICAST)
  3972. dev_change_rx_flags(dev, IFF_MULTICAST);
  3973. dev_set_rx_mode(dev);
  3974. /*
  3975. * Have we downed the interface. We handle IFF_UP ourselves
  3976. * according to user attempts to set it, rather than blindly
  3977. * setting it.
  3978. */
  3979. ret = 0;
  3980. if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
  3981. ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
  3982. if (!ret)
  3983. dev_set_rx_mode(dev);
  3984. }
  3985. if ((flags ^ dev->gflags) & IFF_PROMISC) {
  3986. int inc = (flags & IFF_PROMISC) ? 1 : -1;
  3987. dev->gflags ^= IFF_PROMISC;
  3988. dev_set_promiscuity(dev, inc);
  3989. }
  3990. /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
  3991. is important. Some (broken) drivers set IFF_PROMISC, when
  3992. IFF_ALLMULTI is requested not asking us and not reporting.
  3993. */
  3994. if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
  3995. int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
  3996. dev->gflags ^= IFF_ALLMULTI;
  3997. dev_set_allmulti(dev, inc);
  3998. }
  3999. return ret;
  4000. }
  4001. void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
  4002. {
  4003. unsigned int changes = dev->flags ^ old_flags;
  4004. if (changes & IFF_UP) {
  4005. if (dev->flags & IFF_UP)
  4006. call_netdevice_notifiers(NETDEV_UP, dev);
  4007. else
  4008. call_netdevice_notifiers(NETDEV_DOWN, dev);
  4009. }
  4010. if (dev->flags & IFF_UP &&
  4011. (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE)))
  4012. call_netdevice_notifiers(NETDEV_CHANGE, dev);
  4013. }
  4014. /**
  4015. * dev_change_flags - change device settings
  4016. * @dev: device
  4017. * @flags: device state flags
  4018. *
  4019. * Change settings on device based state flags. The flags are
  4020. * in the userspace exported format.
  4021. */
  4022. int dev_change_flags(struct net_device *dev, unsigned int flags)
  4023. {
  4024. int ret;
  4025. unsigned int changes, old_flags = dev->flags;
  4026. ret = __dev_change_flags(dev, flags);
  4027. if (ret < 0)
  4028. return ret;
  4029. changes = old_flags ^ dev->flags;
  4030. if (changes)
  4031. rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
  4032. __dev_notify_flags(dev, old_flags);
  4033. return ret;
  4034. }
  4035. EXPORT_SYMBOL(dev_change_flags);
  4036. /**
  4037. * dev_set_mtu - Change maximum transfer unit
  4038. * @dev: device
  4039. * @new_mtu: new transfer unit
  4040. *
  4041. * Change the maximum transfer size of the network device.
  4042. */
  4043. int dev_set_mtu(struct net_device *dev, int new_mtu)
  4044. {
  4045. const struct net_device_ops *ops = dev->netdev_ops;
  4046. int err;
  4047. if (new_mtu == dev->mtu)
  4048. return 0;
  4049. /* MTU must be positive. */
  4050. if (new_mtu < 0)
  4051. return -EINVAL;
  4052. if (!netif_device_present(dev))
  4053. return -ENODEV;
  4054. err = 0;
  4055. if (ops->ndo_change_mtu)
  4056. err = ops->ndo_change_mtu(dev, new_mtu);
  4057. else
  4058. dev->mtu = new_mtu;
  4059. if (!err)
  4060. call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
  4061. return err;
  4062. }
  4063. EXPORT_SYMBOL(dev_set_mtu);
  4064. /**
  4065. * dev_set_group - Change group this device belongs to
  4066. * @dev: device
  4067. * @new_group: group this device should belong to
  4068. */
  4069. void dev_set_group(struct net_device *dev, int new_group)
  4070. {
  4071. dev->group = new_group;
  4072. }
  4073. EXPORT_SYMBOL(dev_set_group);
  4074. /**
  4075. * dev_set_mac_address - Change Media Access Control Address
  4076. * @dev: device
  4077. * @sa: new address
  4078. *
  4079. * Change the hardware (MAC) address of the device
  4080. */
  4081. int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
  4082. {
  4083. const struct net_device_ops *ops = dev->netdev_ops;
  4084. int err;
  4085. if (!ops->ndo_set_mac_address)
  4086. return -EOPNOTSUPP;
  4087. if (sa->sa_family != dev->type)
  4088. return -EINVAL;
  4089. if (!netif_device_present(dev))
  4090. return -ENODEV;
  4091. err = ops->ndo_set_mac_address(dev, sa);
  4092. if (err)
  4093. return err;
  4094. dev->addr_assign_type = NET_ADDR_SET;
  4095. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  4096. add_device_randomness(dev->dev_addr, dev->addr_len);
  4097. return 0;
  4098. }
  4099. EXPORT_SYMBOL(dev_set_mac_address);
  4100. /**
  4101. * dev_change_carrier - Change device carrier
  4102. * @dev: device
  4103. * @new_carrier: new value
  4104. *
  4105. * Change device carrier
  4106. */
  4107. int dev_change_carrier(struct net_device *dev, bool new_carrier)
  4108. {
  4109. const struct net_device_ops *ops = dev->netdev_ops;
  4110. if (!ops->ndo_change_carrier)
  4111. return -EOPNOTSUPP;
  4112. if (!netif_device_present(dev))
  4113. return -ENODEV;
  4114. return ops->ndo_change_carrier(dev, new_carrier);
  4115. }
  4116. EXPORT_SYMBOL(dev_change_carrier);
  4117. /**
  4118. * dev_new_index - allocate an ifindex
  4119. * @net: the applicable net namespace
  4120. *
  4121. * Returns a suitable unique value for a new device interface
  4122. * number. The caller must hold the rtnl semaphore or the
  4123. * dev_base_lock to be sure it remains unique.
  4124. */
  4125. static int dev_new_index(struct net *net)
  4126. {
  4127. int ifindex = net->ifindex;
  4128. for (;;) {
  4129. if (++ifindex <= 0)
  4130. ifindex = 1;
  4131. if (!__dev_get_by_index(net, ifindex))
  4132. return net->ifindex = ifindex;
  4133. }
  4134. }
  4135. /* Delayed registration/unregisteration */
  4136. static LIST_HEAD(net_todo_list);
  4137. static void net_set_todo(struct net_device *dev)
  4138. {
  4139. list_add_tail(&dev->todo_list, &net_todo_list);
  4140. }
  4141. static void rollback_registered_many(struct list_head *head)
  4142. {
  4143. struct net_device *dev, *tmp;
  4144. BUG_ON(dev_boot_phase);
  4145. ASSERT_RTNL();
  4146. list_for_each_entry_safe(dev, tmp, head, unreg_list) {
  4147. /* Some devices call without registering
  4148. * for initialization unwind. Remove those
  4149. * devices and proceed with the remaining.
  4150. */
  4151. if (dev->reg_state == NETREG_UNINITIALIZED) {
  4152. pr_debug("unregister_netdevice: device %s/%p never was registered\n",
  4153. dev->name, dev);
  4154. WARN_ON(1);
  4155. list_del(&dev->unreg_list);
  4156. continue;
  4157. }
  4158. dev->dismantle = true;
  4159. BUG_ON(dev->reg_state != NETREG_REGISTERED);
  4160. }
  4161. /* If device is running, close it first. */
  4162. dev_close_many(head);
  4163. list_for_each_entry(dev, head, unreg_list) {
  4164. /* And unlink it from device chain. */
  4165. unlist_netdevice(dev);
  4166. dev->reg_state = NETREG_UNREGISTERING;
  4167. }
  4168. synchronize_net();
  4169. list_for_each_entry(dev, head, unreg_list) {
  4170. /* Shutdown queueing discipline. */
  4171. dev_shutdown(dev);
  4172. /* Notify protocols, that we are about to destroy
  4173. this device. They should clean all the things.
  4174. */
  4175. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  4176. if (!dev->rtnl_link_ops ||
  4177. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  4178. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  4179. /*
  4180. * Flush the unicast and multicast chains
  4181. */
  4182. dev_uc_flush(dev);
  4183. dev_mc_flush(dev);
  4184. if (dev->netdev_ops->ndo_uninit)
  4185. dev->netdev_ops->ndo_uninit(dev);
  4186. /* Notifier chain MUST detach us all upper devices. */
  4187. WARN_ON(netdev_has_any_upper_dev(dev));
  4188. /* Remove entries from kobject tree */
  4189. netdev_unregister_kobject(dev);
  4190. #ifdef CONFIG_XPS
  4191. /* Remove XPS queueing entries */
  4192. netif_reset_xps_queues_gt(dev, 0);
  4193. #endif
  4194. }
  4195. synchronize_net();
  4196. list_for_each_entry(dev, head, unreg_list)
  4197. dev_put(dev);
  4198. }
  4199. static void rollback_registered(struct net_device *dev)
  4200. {
  4201. LIST_HEAD(single);
  4202. list_add(&dev->unreg_list, &single);
  4203. rollback_registered_many(&single);
  4204. list_del(&single);
  4205. }
  4206. static netdev_features_t netdev_fix_features(struct net_device *dev,
  4207. netdev_features_t features)
  4208. {
  4209. /* Fix illegal checksum combinations */
  4210. if ((features & NETIF_F_HW_CSUM) &&
  4211. (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  4212. netdev_warn(dev, "mixed HW and IP checksum settings.\n");
  4213. features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
  4214. }
  4215. /* TSO requires that SG is present as well. */
  4216. if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
  4217. netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
  4218. features &= ~NETIF_F_ALL_TSO;
  4219. }
  4220. if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
  4221. !(features & NETIF_F_IP_CSUM)) {
  4222. netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
  4223. features &= ~NETIF_F_TSO;
  4224. features &= ~NETIF_F_TSO_ECN;
  4225. }
  4226. if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
  4227. !(features & NETIF_F_IPV6_CSUM)) {
  4228. netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
  4229. features &= ~NETIF_F_TSO6;
  4230. }
  4231. /* TSO ECN requires that TSO is present as well. */
  4232. if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
  4233. features &= ~NETIF_F_TSO_ECN;
  4234. /* Software GSO depends on SG. */
  4235. if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
  4236. netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
  4237. features &= ~NETIF_F_GSO;
  4238. }
  4239. /* UFO needs SG and checksumming */
  4240. if (features & NETIF_F_UFO) {
  4241. /* maybe split UFO into V4 and V6? */
  4242. if (!((features & NETIF_F_GEN_CSUM) ||
  4243. (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
  4244. == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  4245. netdev_dbg(dev,
  4246. "Dropping NETIF_F_UFO since no checksum offload features.\n");
  4247. features &= ~NETIF_F_UFO;
  4248. }
  4249. if (!(features & NETIF_F_SG)) {
  4250. netdev_dbg(dev,
  4251. "Dropping NETIF_F_UFO since no NETIF_F_SG feature.\n");
  4252. features &= ~NETIF_F_UFO;
  4253. }
  4254. }
  4255. return features;
  4256. }
  4257. int __netdev_update_features(struct net_device *dev)
  4258. {
  4259. netdev_features_t features;
  4260. int err = 0;
  4261. ASSERT_RTNL();
  4262. features = netdev_get_wanted_features(dev);
  4263. if (dev->netdev_ops->ndo_fix_features)
  4264. features = dev->netdev_ops->ndo_fix_features(dev, features);
  4265. /* driver might be less strict about feature dependencies */
  4266. features = netdev_fix_features(dev, features);
  4267. if (dev->features == features)
  4268. return 0;
  4269. netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
  4270. &dev->features, &features);
  4271. if (dev->netdev_ops->ndo_set_features)
  4272. err = dev->netdev_ops->ndo_set_features(dev, features);
  4273. if (unlikely(err < 0)) {
  4274. netdev_err(dev,
  4275. "set_features() failed (%d); wanted %pNF, left %pNF\n",
  4276. err, &features, &dev->features);
  4277. return -1;
  4278. }
  4279. if (!err)
  4280. dev->features = features;
  4281. return 1;
  4282. }
  4283. /**
  4284. * netdev_update_features - recalculate device features
  4285. * @dev: the device to check
  4286. *
  4287. * Recalculate dev->features set and send notifications if it
  4288. * has changed. Should be called after driver or hardware dependent
  4289. * conditions might have changed that influence the features.
  4290. */
  4291. void netdev_update_features(struct net_device *dev)
  4292. {
  4293. if (__netdev_update_features(dev))
  4294. netdev_features_change(dev);
  4295. }
  4296. EXPORT_SYMBOL(netdev_update_features);
  4297. /**
  4298. * netdev_change_features - recalculate device features
  4299. * @dev: the device to check
  4300. *
  4301. * Recalculate dev->features set and send notifications even
  4302. * if they have not changed. Should be called instead of
  4303. * netdev_update_features() if also dev->vlan_features might
  4304. * have changed to allow the changes to be propagated to stacked
  4305. * VLAN devices.
  4306. */
  4307. void netdev_change_features(struct net_device *dev)
  4308. {
  4309. __netdev_update_features(dev);
  4310. netdev_features_change(dev);
  4311. }
  4312. EXPORT_SYMBOL(netdev_change_features);
  4313. /**
  4314. * netif_stacked_transfer_operstate - transfer operstate
  4315. * @rootdev: the root or lower level device to transfer state from
  4316. * @dev: the device to transfer operstate to
  4317. *
  4318. * Transfer operational state from root to device. This is normally
  4319. * called when a stacking relationship exists between the root
  4320. * device and the device(a leaf device).
  4321. */
  4322. void netif_stacked_transfer_operstate(const struct net_device *rootdev,
  4323. struct net_device *dev)
  4324. {
  4325. if (rootdev->operstate == IF_OPER_DORMANT)
  4326. netif_dormant_on(dev);
  4327. else
  4328. netif_dormant_off(dev);
  4329. if (netif_carrier_ok(rootdev)) {
  4330. if (!netif_carrier_ok(dev))
  4331. netif_carrier_on(dev);
  4332. } else {
  4333. if (netif_carrier_ok(dev))
  4334. netif_carrier_off(dev);
  4335. }
  4336. }
  4337. EXPORT_SYMBOL(netif_stacked_transfer_operstate);
  4338. #ifdef CONFIG_RPS
  4339. static int netif_alloc_rx_queues(struct net_device *dev)
  4340. {
  4341. unsigned int i, count = dev->num_rx_queues;
  4342. struct netdev_rx_queue *rx;
  4343. BUG_ON(count < 1);
  4344. rx = kcalloc(count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
  4345. if (!rx)
  4346. return -ENOMEM;
  4347. dev->_rx = rx;
  4348. for (i = 0; i < count; i++)
  4349. rx[i].dev = dev;
  4350. return 0;
  4351. }
  4352. #endif
  4353. static void netdev_init_one_queue(struct net_device *dev,
  4354. struct netdev_queue *queue, void *_unused)
  4355. {
  4356. /* Initialize queue lock */
  4357. spin_lock_init(&queue->_xmit_lock);
  4358. netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
  4359. queue->xmit_lock_owner = -1;
  4360. netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
  4361. queue->dev = dev;
  4362. #ifdef CONFIG_BQL
  4363. dql_init(&queue->dql, HZ);
  4364. #endif
  4365. }
  4366. static int netif_alloc_netdev_queues(struct net_device *dev)
  4367. {
  4368. unsigned int count = dev->num_tx_queues;
  4369. struct netdev_queue *tx;
  4370. BUG_ON(count < 1);
  4371. tx = kcalloc(count, sizeof(struct netdev_queue), GFP_KERNEL);
  4372. if (!tx)
  4373. return -ENOMEM;
  4374. dev->_tx = tx;
  4375. netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
  4376. spin_lock_init(&dev->tx_global_lock);
  4377. return 0;
  4378. }
  4379. /**
  4380. * register_netdevice - register a network device
  4381. * @dev: device to register
  4382. *
  4383. * Take a completed network device structure and add it to the kernel
  4384. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  4385. * chain. 0 is returned on success. A negative errno code is returned
  4386. * on a failure to set up the device, or if the name is a duplicate.
  4387. *
  4388. * Callers must hold the rtnl semaphore. You may want
  4389. * register_netdev() instead of this.
  4390. *
  4391. * BUGS:
  4392. * The locking appears insufficient to guarantee two parallel registers
  4393. * will not get the same name.
  4394. */
  4395. int register_netdevice(struct net_device *dev)
  4396. {
  4397. int ret;
  4398. struct net *net = dev_net(dev);
  4399. BUG_ON(dev_boot_phase);
  4400. ASSERT_RTNL();
  4401. might_sleep();
  4402. /* When net_device's are persistent, this will be fatal. */
  4403. BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
  4404. BUG_ON(!net);
  4405. spin_lock_init(&dev->addr_list_lock);
  4406. netdev_set_addr_lockdep_class(dev);
  4407. dev->iflink = -1;
  4408. ret = dev_get_valid_name(net, dev, dev->name);
  4409. if (ret < 0)
  4410. goto out;
  4411. /* Init, if this function is available */
  4412. if (dev->netdev_ops->ndo_init) {
  4413. ret = dev->netdev_ops->ndo_init(dev);
  4414. if (ret) {
  4415. if (ret > 0)
  4416. ret = -EIO;
  4417. goto out;
  4418. }
  4419. }
  4420. if (((dev->hw_features | dev->features) &
  4421. NETIF_F_HW_VLAN_CTAG_FILTER) &&
  4422. (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
  4423. !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
  4424. netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
  4425. ret = -EINVAL;
  4426. goto err_uninit;
  4427. }
  4428. ret = -EBUSY;
  4429. if (!dev->ifindex)
  4430. dev->ifindex = dev_new_index(net);
  4431. else if (__dev_get_by_index(net, dev->ifindex))
  4432. goto err_uninit;
  4433. if (dev->iflink == -1)
  4434. dev->iflink = dev->ifindex;
  4435. /* Transfer changeable features to wanted_features and enable
  4436. * software offloads (GSO and GRO).
  4437. */
  4438. dev->hw_features |= NETIF_F_SOFT_FEATURES;
  4439. dev->features |= NETIF_F_SOFT_FEATURES;
  4440. dev->wanted_features = dev->features & dev->hw_features;
  4441. /* Turn on no cache copy if HW is doing checksum */
  4442. if (!(dev->flags & IFF_LOOPBACK)) {
  4443. dev->hw_features |= NETIF_F_NOCACHE_COPY;
  4444. if (dev->features & NETIF_F_ALL_CSUM) {
  4445. dev->wanted_features |= NETIF_F_NOCACHE_COPY;
  4446. dev->features |= NETIF_F_NOCACHE_COPY;
  4447. }
  4448. }
  4449. /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
  4450. */
  4451. dev->vlan_features |= NETIF_F_HIGHDMA;
  4452. /* Make NETIF_F_SG inheritable to tunnel devices.
  4453. */
  4454. dev->hw_enc_features |= NETIF_F_SG;
  4455. ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
  4456. ret = notifier_to_errno(ret);
  4457. if (ret)
  4458. goto err_uninit;
  4459. ret = netdev_register_kobject(dev);
  4460. if (ret)
  4461. goto err_uninit;
  4462. dev->reg_state = NETREG_REGISTERED;
  4463. __netdev_update_features(dev);
  4464. /*
  4465. * Default initial state at registry is that the
  4466. * device is present.
  4467. */
  4468. set_bit(__LINK_STATE_PRESENT, &dev->state);
  4469. linkwatch_init_dev(dev);
  4470. dev_init_scheduler(dev);
  4471. dev_hold(dev);
  4472. list_netdevice(dev);
  4473. add_device_randomness(dev->dev_addr, dev->addr_len);
  4474. /* If the device has permanent device address, driver should
  4475. * set dev_addr and also addr_assign_type should be set to
  4476. * NET_ADDR_PERM (default value).
  4477. */
  4478. if (dev->addr_assign_type == NET_ADDR_PERM)
  4479. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  4480. /* Notify protocols, that a new device appeared. */
  4481. ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
  4482. ret = notifier_to_errno(ret);
  4483. if (ret) {
  4484. rollback_registered(dev);
  4485. dev->reg_state = NETREG_UNREGISTERED;
  4486. }
  4487. /*
  4488. * Prevent userspace races by waiting until the network
  4489. * device is fully setup before sending notifications.
  4490. */
  4491. if (!dev->rtnl_link_ops ||
  4492. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  4493. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  4494. out:
  4495. return ret;
  4496. err_uninit:
  4497. if (dev->netdev_ops->ndo_uninit)
  4498. dev->netdev_ops->ndo_uninit(dev);
  4499. goto out;
  4500. }
  4501. EXPORT_SYMBOL(register_netdevice);
  4502. /**
  4503. * init_dummy_netdev - init a dummy network device for NAPI
  4504. * @dev: device to init
  4505. *
  4506. * This takes a network device structure and initialize the minimum
  4507. * amount of fields so it can be used to schedule NAPI polls without
  4508. * registering a full blown interface. This is to be used by drivers
  4509. * that need to tie several hardware interfaces to a single NAPI
  4510. * poll scheduler due to HW limitations.
  4511. */
  4512. int init_dummy_netdev(struct net_device *dev)
  4513. {
  4514. /* Clear everything. Note we don't initialize spinlocks
  4515. * are they aren't supposed to be taken by any of the
  4516. * NAPI code and this dummy netdev is supposed to be
  4517. * only ever used for NAPI polls
  4518. */
  4519. memset(dev, 0, sizeof(struct net_device));
  4520. /* make sure we BUG if trying to hit standard
  4521. * register/unregister code path
  4522. */
  4523. dev->reg_state = NETREG_DUMMY;
  4524. /* NAPI wants this */
  4525. INIT_LIST_HEAD(&dev->napi_list);
  4526. /* a dummy interface is started by default */
  4527. set_bit(__LINK_STATE_PRESENT, &dev->state);
  4528. set_bit(__LINK_STATE_START, &dev->state);
  4529. /* Note : We dont allocate pcpu_refcnt for dummy devices,
  4530. * because users of this 'device' dont need to change
  4531. * its refcount.
  4532. */
  4533. return 0;
  4534. }
  4535. EXPORT_SYMBOL_GPL(init_dummy_netdev);
  4536. /**
  4537. * register_netdev - register a network device
  4538. * @dev: device to register
  4539. *
  4540. * Take a completed network device structure and add it to the kernel
  4541. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  4542. * chain. 0 is returned on success. A negative errno code is returned
  4543. * on a failure to set up the device, or if the name is a duplicate.
  4544. *
  4545. * This is a wrapper around register_netdevice that takes the rtnl semaphore
  4546. * and expands the device name if you passed a format string to
  4547. * alloc_netdev.
  4548. */
  4549. int register_netdev(struct net_device *dev)
  4550. {
  4551. int err;
  4552. rtnl_lock();
  4553. err = register_netdevice(dev);
  4554. rtnl_unlock();
  4555. return err;
  4556. }
  4557. EXPORT_SYMBOL(register_netdev);
  4558. int netdev_refcnt_read(const struct net_device *dev)
  4559. {
  4560. int i, refcnt = 0;
  4561. for_each_possible_cpu(i)
  4562. refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
  4563. return refcnt;
  4564. }
  4565. EXPORT_SYMBOL(netdev_refcnt_read);
  4566. /**
  4567. * netdev_wait_allrefs - wait until all references are gone.
  4568. * @dev: target net_device
  4569. *
  4570. * This is called when unregistering network devices.
  4571. *
  4572. * Any protocol or device that holds a reference should register
  4573. * for netdevice notification, and cleanup and put back the
  4574. * reference if they receive an UNREGISTER event.
  4575. * We can get stuck here if buggy protocols don't correctly
  4576. * call dev_put.
  4577. */
  4578. static void netdev_wait_allrefs(struct net_device *dev)
  4579. {
  4580. unsigned long rebroadcast_time, warning_time;
  4581. int refcnt;
  4582. linkwatch_forget_dev(dev);
  4583. rebroadcast_time = warning_time = jiffies;
  4584. refcnt = netdev_refcnt_read(dev);
  4585. while (refcnt != 0) {
  4586. if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
  4587. rtnl_lock();
  4588. /* Rebroadcast unregister notification */
  4589. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  4590. __rtnl_unlock();
  4591. rcu_barrier();
  4592. rtnl_lock();
  4593. call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
  4594. if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
  4595. &dev->state)) {
  4596. /* We must not have linkwatch events
  4597. * pending on unregister. If this
  4598. * happens, we simply run the queue
  4599. * unscheduled, resulting in a noop
  4600. * for this device.
  4601. */
  4602. linkwatch_run_queue();
  4603. }
  4604. __rtnl_unlock();
  4605. rebroadcast_time = jiffies;
  4606. }
  4607. msleep(250);
  4608. refcnt = netdev_refcnt_read(dev);
  4609. if (time_after(jiffies, warning_time + 10 * HZ)) {
  4610. pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
  4611. dev->name, refcnt);
  4612. warning_time = jiffies;
  4613. }
  4614. }
  4615. }
  4616. /* The sequence is:
  4617. *
  4618. * rtnl_lock();
  4619. * ...
  4620. * register_netdevice(x1);
  4621. * register_netdevice(x2);
  4622. * ...
  4623. * unregister_netdevice(y1);
  4624. * unregister_netdevice(y2);
  4625. * ...
  4626. * rtnl_unlock();
  4627. * free_netdev(y1);
  4628. * free_netdev(y2);
  4629. *
  4630. * We are invoked by rtnl_unlock().
  4631. * This allows us to deal with problems:
  4632. * 1) We can delete sysfs objects which invoke hotplug
  4633. * without deadlocking with linkwatch via keventd.
  4634. * 2) Since we run with the RTNL semaphore not held, we can sleep
  4635. * safely in order to wait for the netdev refcnt to drop to zero.
  4636. *
  4637. * We must not return until all unregister events added during
  4638. * the interval the lock was held have been completed.
  4639. */
  4640. void netdev_run_todo(void)
  4641. {
  4642. struct list_head list;
  4643. /* Snapshot list, allow later requests */
  4644. list_replace_init(&net_todo_list, &list);
  4645. __rtnl_unlock();
  4646. /* Wait for rcu callbacks to finish before next phase */
  4647. if (!list_empty(&list))
  4648. rcu_barrier();
  4649. while (!list_empty(&list)) {
  4650. struct net_device *dev
  4651. = list_first_entry(&list, struct net_device, todo_list);
  4652. list_del(&dev->todo_list);
  4653. rtnl_lock();
  4654. call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
  4655. __rtnl_unlock();
  4656. if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
  4657. pr_err("network todo '%s' but state %d\n",
  4658. dev->name, dev->reg_state);
  4659. dump_stack();
  4660. continue;
  4661. }
  4662. dev->reg_state = NETREG_UNREGISTERED;
  4663. on_each_cpu(flush_backlog, dev, 1);
  4664. netdev_wait_allrefs(dev);
  4665. /* paranoia */
  4666. BUG_ON(netdev_refcnt_read(dev));
  4667. WARN_ON(rcu_access_pointer(dev->ip_ptr));
  4668. WARN_ON(rcu_access_pointer(dev->ip6_ptr));
  4669. WARN_ON(dev->dn_ptr);
  4670. if (dev->destructor)
  4671. dev->destructor(dev);
  4672. /* Free network device */
  4673. kobject_put(&dev->dev.kobj);
  4674. }
  4675. }
  4676. /* Convert net_device_stats to rtnl_link_stats64. They have the same
  4677. * fields in the same order, with only the type differing.
  4678. */
  4679. void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
  4680. const struct net_device_stats *netdev_stats)
  4681. {
  4682. #if BITS_PER_LONG == 64
  4683. BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
  4684. memcpy(stats64, netdev_stats, sizeof(*stats64));
  4685. #else
  4686. size_t i, n = sizeof(*stats64) / sizeof(u64);
  4687. const unsigned long *src = (const unsigned long *)netdev_stats;
  4688. u64 *dst = (u64 *)stats64;
  4689. BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
  4690. sizeof(*stats64) / sizeof(u64));
  4691. for (i = 0; i < n; i++)
  4692. dst[i] = src[i];
  4693. #endif
  4694. }
  4695. EXPORT_SYMBOL(netdev_stats_to_stats64);
  4696. /**
  4697. * dev_get_stats - get network device statistics
  4698. * @dev: device to get statistics from
  4699. * @storage: place to store stats
  4700. *
  4701. * Get network statistics from device. Return @storage.
  4702. * The device driver may provide its own method by setting
  4703. * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
  4704. * otherwise the internal statistics structure is used.
  4705. */
  4706. struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
  4707. struct rtnl_link_stats64 *storage)
  4708. {
  4709. const struct net_device_ops *ops = dev->netdev_ops;
  4710. if (ops->ndo_get_stats64) {
  4711. memset(storage, 0, sizeof(*storage));
  4712. ops->ndo_get_stats64(dev, storage);
  4713. } else if (ops->ndo_get_stats) {
  4714. netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
  4715. } else {
  4716. netdev_stats_to_stats64(storage, &dev->stats);
  4717. }
  4718. storage->rx_dropped += atomic_long_read(&dev->rx_dropped);
  4719. return storage;
  4720. }
  4721. EXPORT_SYMBOL(dev_get_stats);
  4722. struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
  4723. {
  4724. struct netdev_queue *queue = dev_ingress_queue(dev);
  4725. #ifdef CONFIG_NET_CLS_ACT
  4726. if (queue)
  4727. return queue;
  4728. queue = kzalloc(sizeof(*queue), GFP_KERNEL);
  4729. if (!queue)
  4730. return NULL;
  4731. netdev_init_one_queue(dev, queue, NULL);
  4732. queue->qdisc = &noop_qdisc;
  4733. queue->qdisc_sleeping = &noop_qdisc;
  4734. rcu_assign_pointer(dev->ingress_queue, queue);
  4735. #endif
  4736. return queue;
  4737. }
  4738. static const struct ethtool_ops default_ethtool_ops;
  4739. void netdev_set_default_ethtool_ops(struct net_device *dev,
  4740. const struct ethtool_ops *ops)
  4741. {
  4742. if (dev->ethtool_ops == &default_ethtool_ops)
  4743. dev->ethtool_ops = ops;
  4744. }
  4745. EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
  4746. /**
  4747. * alloc_netdev_mqs - allocate network device
  4748. * @sizeof_priv: size of private data to allocate space for
  4749. * @name: device name format string
  4750. * @setup: callback to initialize device
  4751. * @txqs: the number of TX subqueues to allocate
  4752. * @rxqs: the number of RX subqueues to allocate
  4753. *
  4754. * Allocates a struct net_device with private data area for driver use
  4755. * and performs basic initialization. Also allocates subquue structs
  4756. * for each queue on the device.
  4757. */
  4758. struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
  4759. void (*setup)(struct net_device *),
  4760. unsigned int txqs, unsigned int rxqs)
  4761. {
  4762. struct net_device *dev;
  4763. size_t alloc_size;
  4764. struct net_device *p;
  4765. BUG_ON(strlen(name) >= sizeof(dev->name));
  4766. if (txqs < 1) {
  4767. pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
  4768. return NULL;
  4769. }
  4770. #ifdef CONFIG_RPS
  4771. if (rxqs < 1) {
  4772. pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
  4773. return NULL;
  4774. }
  4775. #endif
  4776. alloc_size = sizeof(struct net_device);
  4777. if (sizeof_priv) {
  4778. /* ensure 32-byte alignment of private area */
  4779. alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
  4780. alloc_size += sizeof_priv;
  4781. }
  4782. /* ensure 32-byte alignment of whole construct */
  4783. alloc_size += NETDEV_ALIGN - 1;
  4784. p = kzalloc(alloc_size, GFP_KERNEL);
  4785. if (!p)
  4786. return NULL;
  4787. dev = PTR_ALIGN(p, NETDEV_ALIGN);
  4788. dev->padded = (char *)dev - (char *)p;
  4789. dev->pcpu_refcnt = alloc_percpu(int);
  4790. if (!dev->pcpu_refcnt)
  4791. goto free_p;
  4792. if (dev_addr_init(dev))
  4793. goto free_pcpu;
  4794. dev_mc_init(dev);
  4795. dev_uc_init(dev);
  4796. dev_net_set(dev, &init_net);
  4797. dev->gso_max_size = GSO_MAX_SIZE;
  4798. dev->gso_max_segs = GSO_MAX_SEGS;
  4799. INIT_LIST_HEAD(&dev->napi_list);
  4800. INIT_LIST_HEAD(&dev->unreg_list);
  4801. INIT_LIST_HEAD(&dev->link_watch_list);
  4802. INIT_LIST_HEAD(&dev->upper_dev_list);
  4803. dev->priv_flags = IFF_XMIT_DST_RELEASE;
  4804. setup(dev);
  4805. dev->num_tx_queues = txqs;
  4806. dev->real_num_tx_queues = txqs;
  4807. if (netif_alloc_netdev_queues(dev))
  4808. goto free_all;
  4809. #ifdef CONFIG_RPS
  4810. dev->num_rx_queues = rxqs;
  4811. dev->real_num_rx_queues = rxqs;
  4812. if (netif_alloc_rx_queues(dev))
  4813. goto free_all;
  4814. #endif
  4815. strcpy(dev->name, name);
  4816. dev->group = INIT_NETDEV_GROUP;
  4817. if (!dev->ethtool_ops)
  4818. dev->ethtool_ops = &default_ethtool_ops;
  4819. return dev;
  4820. free_all:
  4821. free_netdev(dev);
  4822. return NULL;
  4823. free_pcpu:
  4824. free_percpu(dev->pcpu_refcnt);
  4825. kfree(dev->_tx);
  4826. #ifdef CONFIG_RPS
  4827. kfree(dev->_rx);
  4828. #endif
  4829. free_p:
  4830. kfree(p);
  4831. return NULL;
  4832. }
  4833. EXPORT_SYMBOL(alloc_netdev_mqs);
  4834. /**
  4835. * free_netdev - free network device
  4836. * @dev: device
  4837. *
  4838. * This function does the last stage of destroying an allocated device
  4839. * interface. The reference to the device object is released.
  4840. * If this is the last reference then it will be freed.
  4841. */
  4842. void free_netdev(struct net_device *dev)
  4843. {
  4844. struct napi_struct *p, *n;
  4845. release_net(dev_net(dev));
  4846. kfree(dev->_tx);
  4847. #ifdef CONFIG_RPS
  4848. kfree(dev->_rx);
  4849. #endif
  4850. kfree(rcu_dereference_protected(dev->ingress_queue, 1));
  4851. /* Flush device addresses */
  4852. dev_addr_flush(dev);
  4853. list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
  4854. netif_napi_del(p);
  4855. free_percpu(dev->pcpu_refcnt);
  4856. dev->pcpu_refcnt = NULL;
  4857. /* Compatibility with error handling in drivers */
  4858. if (dev->reg_state == NETREG_UNINITIALIZED) {
  4859. kfree((char *)dev - dev->padded);
  4860. return;
  4861. }
  4862. BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
  4863. dev->reg_state = NETREG_RELEASED;
  4864. /* will free via device release */
  4865. put_device(&dev->dev);
  4866. }
  4867. EXPORT_SYMBOL(free_netdev);
  4868. /**
  4869. * synchronize_net - Synchronize with packet receive processing
  4870. *
  4871. * Wait for packets currently being received to be done.
  4872. * Does not block later packets from starting.
  4873. */
  4874. void synchronize_net(void)
  4875. {
  4876. might_sleep();
  4877. if (rtnl_is_locked())
  4878. synchronize_rcu_expedited();
  4879. else
  4880. synchronize_rcu();
  4881. }
  4882. EXPORT_SYMBOL(synchronize_net);
  4883. /**
  4884. * unregister_netdevice_queue - remove device from the kernel
  4885. * @dev: device
  4886. * @head: list
  4887. *
  4888. * This function shuts down a device interface and removes it
  4889. * from the kernel tables.
  4890. * If head not NULL, device is queued to be unregistered later.
  4891. *
  4892. * Callers must hold the rtnl semaphore. You may want
  4893. * unregister_netdev() instead of this.
  4894. */
  4895. void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
  4896. {
  4897. ASSERT_RTNL();
  4898. if (head) {
  4899. list_move_tail(&dev->unreg_list, head);
  4900. } else {
  4901. rollback_registered(dev);
  4902. /* Finish processing unregister after unlock */
  4903. net_set_todo(dev);
  4904. }
  4905. }
  4906. EXPORT_SYMBOL(unregister_netdevice_queue);
  4907. /**
  4908. * unregister_netdevice_many - unregister many devices
  4909. * @head: list of devices
  4910. */
  4911. void unregister_netdevice_many(struct list_head *head)
  4912. {
  4913. struct net_device *dev;
  4914. if (!list_empty(head)) {
  4915. rollback_registered_many(head);
  4916. list_for_each_entry(dev, head, unreg_list)
  4917. net_set_todo(dev);
  4918. }
  4919. }
  4920. EXPORT_SYMBOL(unregister_netdevice_many);
  4921. /**
  4922. * unregister_netdev - remove device from the kernel
  4923. * @dev: device
  4924. *
  4925. * This function shuts down a device interface and removes it
  4926. * from the kernel tables.
  4927. *
  4928. * This is just a wrapper for unregister_netdevice that takes
  4929. * the rtnl semaphore. In general you want to use this and not
  4930. * unregister_netdevice.
  4931. */
  4932. void unregister_netdev(struct net_device *dev)
  4933. {
  4934. rtnl_lock();
  4935. unregister_netdevice(dev);
  4936. rtnl_unlock();
  4937. }
  4938. EXPORT_SYMBOL(unregister_netdev);
  4939. /**
  4940. * dev_change_net_namespace - move device to different nethost namespace
  4941. * @dev: device
  4942. * @net: network namespace
  4943. * @pat: If not NULL name pattern to try if the current device name
  4944. * is already taken in the destination network namespace.
  4945. *
  4946. * This function shuts down a device interface and moves it
  4947. * to a new network namespace. On success 0 is returned, on
  4948. * a failure a netagive errno code is returned.
  4949. *
  4950. * Callers must hold the rtnl semaphore.
  4951. */
  4952. int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
  4953. {
  4954. int err;
  4955. ASSERT_RTNL();
  4956. /* Don't allow namespace local devices to be moved. */
  4957. err = -EINVAL;
  4958. if (dev->features & NETIF_F_NETNS_LOCAL)
  4959. goto out;
  4960. /* Ensure the device has been registrered */
  4961. if (dev->reg_state != NETREG_REGISTERED)
  4962. goto out;
  4963. /* Get out if there is nothing todo */
  4964. err = 0;
  4965. if (net_eq(dev_net(dev), net))
  4966. goto out;
  4967. /* Pick the destination device name, and ensure
  4968. * we can use it in the destination network namespace.
  4969. */
  4970. err = -EEXIST;
  4971. if (__dev_get_by_name(net, dev->name)) {
  4972. /* We get here if we can't use the current device name */
  4973. if (!pat)
  4974. goto out;
  4975. if (dev_get_valid_name(net, dev, pat) < 0)
  4976. goto out;
  4977. }
  4978. /*
  4979. * And now a mini version of register_netdevice unregister_netdevice.
  4980. */
  4981. /* If device is running close it first. */
  4982. dev_close(dev);
  4983. /* And unlink it from device chain */
  4984. err = -ENODEV;
  4985. unlist_netdevice(dev);
  4986. synchronize_net();
  4987. /* Shutdown queueing discipline. */
  4988. dev_shutdown(dev);
  4989. /* Notify protocols, that we are about to destroy
  4990. this device. They should clean all the things.
  4991. Note that dev->reg_state stays at NETREG_REGISTERED.
  4992. This is wanted because this way 8021q and macvlan know
  4993. the device is just moving and can keep their slaves up.
  4994. */
  4995. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  4996. rcu_barrier();
  4997. call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
  4998. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  4999. /*
  5000. * Flush the unicast and multicast chains
  5001. */
  5002. dev_uc_flush(dev);
  5003. dev_mc_flush(dev);
  5004. /* Send a netdev-removed uevent to the old namespace */
  5005. kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
  5006. /* Actually switch the network namespace */
  5007. dev_net_set(dev, net);
  5008. /* If there is an ifindex conflict assign a new one */
  5009. if (__dev_get_by_index(net, dev->ifindex)) {
  5010. int iflink = (dev->iflink == dev->ifindex);
  5011. dev->ifindex = dev_new_index(net);
  5012. if (iflink)
  5013. dev->iflink = dev->ifindex;
  5014. }
  5015. /* Send a netdev-add uevent to the new namespace */
  5016. kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
  5017. /* Fixup kobjects */
  5018. err = device_rename(&dev->dev, dev->name);
  5019. WARN_ON(err);
  5020. /* Add the device back in the hashes */
  5021. list_netdevice(dev);
  5022. /* Notify protocols, that a new device appeared. */
  5023. call_netdevice_notifiers(NETDEV_REGISTER, dev);
  5024. /*
  5025. * Prevent userspace races by waiting until the network
  5026. * device is fully setup before sending notifications.
  5027. */
  5028. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  5029. synchronize_net();
  5030. err = 0;
  5031. out:
  5032. return err;
  5033. }
  5034. EXPORT_SYMBOL_GPL(dev_change_net_namespace);
  5035. static int dev_cpu_callback(struct notifier_block *nfb,
  5036. unsigned long action,
  5037. void *ocpu)
  5038. {
  5039. struct sk_buff **list_skb;
  5040. struct sk_buff *skb;
  5041. unsigned int cpu, oldcpu = (unsigned long)ocpu;
  5042. struct softnet_data *sd, *oldsd;
  5043. if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
  5044. return NOTIFY_OK;
  5045. local_irq_disable();
  5046. cpu = smp_processor_id();
  5047. sd = &per_cpu(softnet_data, cpu);
  5048. oldsd = &per_cpu(softnet_data, oldcpu);
  5049. /* Find end of our completion_queue. */
  5050. list_skb = &sd->completion_queue;
  5051. while (*list_skb)
  5052. list_skb = &(*list_skb)->next;
  5053. /* Append completion queue from offline CPU. */
  5054. *list_skb = oldsd->completion_queue;
  5055. oldsd->completion_queue = NULL;
  5056. /* Append output queue from offline CPU. */
  5057. if (oldsd->output_queue) {
  5058. *sd->output_queue_tailp = oldsd->output_queue;
  5059. sd->output_queue_tailp = oldsd->output_queue_tailp;
  5060. oldsd->output_queue = NULL;
  5061. oldsd->output_queue_tailp = &oldsd->output_queue;
  5062. }
  5063. /* Append NAPI poll list from offline CPU. */
  5064. if (!list_empty(&oldsd->poll_list)) {
  5065. list_splice_init(&oldsd->poll_list, &sd->poll_list);
  5066. raise_softirq_irqoff(NET_RX_SOFTIRQ);
  5067. }
  5068. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  5069. local_irq_enable();
  5070. /* Process offline CPU's input_pkt_queue */
  5071. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  5072. netif_rx(skb);
  5073. input_queue_head_incr(oldsd);
  5074. }
  5075. while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  5076. netif_rx(skb);
  5077. input_queue_head_incr(oldsd);
  5078. }
  5079. return NOTIFY_OK;
  5080. }
  5081. /**
  5082. * netdev_increment_features - increment feature set by one
  5083. * @all: current feature set
  5084. * @one: new feature set
  5085. * @mask: mask feature set
  5086. *
  5087. * Computes a new feature set after adding a device with feature set
  5088. * @one to the master device with current feature set @all. Will not
  5089. * enable anything that is off in @mask. Returns the new feature set.
  5090. */
  5091. netdev_features_t netdev_increment_features(netdev_features_t all,
  5092. netdev_features_t one, netdev_features_t mask)
  5093. {
  5094. if (mask & NETIF_F_GEN_CSUM)
  5095. mask |= NETIF_F_ALL_CSUM;
  5096. mask |= NETIF_F_VLAN_CHALLENGED;
  5097. all |= one & (NETIF_F_ONE_FOR_ALL|NETIF_F_ALL_CSUM) & mask;
  5098. all &= one | ~NETIF_F_ALL_FOR_ALL;
  5099. /* If one device supports hw checksumming, set for all. */
  5100. if (all & NETIF_F_GEN_CSUM)
  5101. all &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_GEN_CSUM);
  5102. return all;
  5103. }
  5104. EXPORT_SYMBOL(netdev_increment_features);
  5105. static struct hlist_head *netdev_create_hash(void)
  5106. {
  5107. int i;
  5108. struct hlist_head *hash;
  5109. hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
  5110. if (hash != NULL)
  5111. for (i = 0; i < NETDEV_HASHENTRIES; i++)
  5112. INIT_HLIST_HEAD(&hash[i]);
  5113. return hash;
  5114. }
  5115. /* Initialize per network namespace state */
  5116. static int __net_init netdev_init(struct net *net)
  5117. {
  5118. if (net != &init_net)
  5119. INIT_LIST_HEAD(&net->dev_base_head);
  5120. net->dev_name_head = netdev_create_hash();
  5121. if (net->dev_name_head == NULL)
  5122. goto err_name;
  5123. net->dev_index_head = netdev_create_hash();
  5124. if (net->dev_index_head == NULL)
  5125. goto err_idx;
  5126. return 0;
  5127. err_idx:
  5128. kfree(net->dev_name_head);
  5129. err_name:
  5130. return -ENOMEM;
  5131. }
  5132. /**
  5133. * netdev_drivername - network driver for the device
  5134. * @dev: network device
  5135. *
  5136. * Determine network driver for device.
  5137. */
  5138. const char *netdev_drivername(const struct net_device *dev)
  5139. {
  5140. const struct device_driver *driver;
  5141. const struct device *parent;
  5142. const char *empty = "";
  5143. parent = dev->dev.parent;
  5144. if (!parent)
  5145. return empty;
  5146. driver = parent->driver;
  5147. if (driver && driver->name)
  5148. return driver->name;
  5149. return empty;
  5150. }
  5151. static int __netdev_printk(const char *level, const struct net_device *dev,
  5152. struct va_format *vaf)
  5153. {
  5154. int r;
  5155. if (dev && dev->dev.parent) {
  5156. r = dev_printk_emit(level[1] - '0',
  5157. dev->dev.parent,
  5158. "%s %s %s: %pV",
  5159. dev_driver_string(dev->dev.parent),
  5160. dev_name(dev->dev.parent),
  5161. netdev_name(dev), vaf);
  5162. } else if (dev) {
  5163. r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
  5164. } else {
  5165. r = printk("%s(NULL net_device): %pV", level, vaf);
  5166. }
  5167. return r;
  5168. }
  5169. int netdev_printk(const char *level, const struct net_device *dev,
  5170. const char *format, ...)
  5171. {
  5172. struct va_format vaf;
  5173. va_list args;
  5174. int r;
  5175. va_start(args, format);
  5176. vaf.fmt = format;
  5177. vaf.va = &args;
  5178. r = __netdev_printk(level, dev, &vaf);
  5179. va_end(args);
  5180. return r;
  5181. }
  5182. EXPORT_SYMBOL(netdev_printk);
  5183. #define define_netdev_printk_level(func, level) \
  5184. int func(const struct net_device *dev, const char *fmt, ...) \
  5185. { \
  5186. int r; \
  5187. struct va_format vaf; \
  5188. va_list args; \
  5189. \
  5190. va_start(args, fmt); \
  5191. \
  5192. vaf.fmt = fmt; \
  5193. vaf.va = &args; \
  5194. \
  5195. r = __netdev_printk(level, dev, &vaf); \
  5196. \
  5197. va_end(args); \
  5198. \
  5199. return r; \
  5200. } \
  5201. EXPORT_SYMBOL(func);
  5202. define_netdev_printk_level(netdev_emerg, KERN_EMERG);
  5203. define_netdev_printk_level(netdev_alert, KERN_ALERT);
  5204. define_netdev_printk_level(netdev_crit, KERN_CRIT);
  5205. define_netdev_printk_level(netdev_err, KERN_ERR);
  5206. define_netdev_printk_level(netdev_warn, KERN_WARNING);
  5207. define_netdev_printk_level(netdev_notice, KERN_NOTICE);
  5208. define_netdev_printk_level(netdev_info, KERN_INFO);
  5209. static void __net_exit netdev_exit(struct net *net)
  5210. {
  5211. kfree(net->dev_name_head);
  5212. kfree(net->dev_index_head);
  5213. }
  5214. static struct pernet_operations __net_initdata netdev_net_ops = {
  5215. .init = netdev_init,
  5216. .exit = netdev_exit,
  5217. };
  5218. static void __net_exit default_device_exit(struct net *net)
  5219. {
  5220. struct net_device *dev, *aux;
  5221. /*
  5222. * Push all migratable network devices back to the
  5223. * initial network namespace
  5224. */
  5225. rtnl_lock();
  5226. for_each_netdev_safe(net, dev, aux) {
  5227. int err;
  5228. char fb_name[IFNAMSIZ];
  5229. /* Ignore unmoveable devices (i.e. loopback) */
  5230. if (dev->features & NETIF_F_NETNS_LOCAL)
  5231. continue;
  5232. /* Leave virtual devices for the generic cleanup */
  5233. if (dev->rtnl_link_ops)
  5234. continue;
  5235. /* Push remaining network devices to init_net */
  5236. snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
  5237. err = dev_change_net_namespace(dev, &init_net, fb_name);
  5238. if (err) {
  5239. pr_emerg("%s: failed to move %s to init_net: %d\n",
  5240. __func__, dev->name, err);
  5241. BUG();
  5242. }
  5243. }
  5244. rtnl_unlock();
  5245. }
  5246. static void __net_exit default_device_exit_batch(struct list_head *net_list)
  5247. {
  5248. /* At exit all network devices most be removed from a network
  5249. * namespace. Do this in the reverse order of registration.
  5250. * Do this across as many network namespaces as possible to
  5251. * improve batching efficiency.
  5252. */
  5253. struct net_device *dev;
  5254. struct net *net;
  5255. LIST_HEAD(dev_kill_list);
  5256. rtnl_lock();
  5257. list_for_each_entry(net, net_list, exit_list) {
  5258. for_each_netdev_reverse(net, dev) {
  5259. if (dev->rtnl_link_ops)
  5260. dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
  5261. else
  5262. unregister_netdevice_queue(dev, &dev_kill_list);
  5263. }
  5264. }
  5265. unregister_netdevice_many(&dev_kill_list);
  5266. list_del(&dev_kill_list);
  5267. rtnl_unlock();
  5268. }
  5269. static struct pernet_operations __net_initdata default_device_ops = {
  5270. .exit = default_device_exit,
  5271. .exit_batch = default_device_exit_batch,
  5272. };
  5273. /*
  5274. * Initialize the DEV module. At boot time this walks the device list and
  5275. * unhooks any devices that fail to initialise (normally hardware not
  5276. * present) and leaves us with a valid list of present and active devices.
  5277. *
  5278. */
  5279. /*
  5280. * This is called single threaded during boot, so no need
  5281. * to take the rtnl semaphore.
  5282. */
  5283. static int __init net_dev_init(void)
  5284. {
  5285. int i, rc = -ENOMEM;
  5286. BUG_ON(!dev_boot_phase);
  5287. if (dev_proc_init())
  5288. goto out;
  5289. if (netdev_kobject_init())
  5290. goto out;
  5291. INIT_LIST_HEAD(&ptype_all);
  5292. for (i = 0; i < PTYPE_HASH_SIZE; i++)
  5293. INIT_LIST_HEAD(&ptype_base[i]);
  5294. INIT_LIST_HEAD(&offload_base);
  5295. if (register_pernet_subsys(&netdev_net_ops))
  5296. goto out;
  5297. /*
  5298. * Initialise the packet receive queues.
  5299. */
  5300. for_each_possible_cpu(i) {
  5301. struct softnet_data *sd = &per_cpu(softnet_data, i);
  5302. memset(sd, 0, sizeof(*sd));
  5303. skb_queue_head_init(&sd->input_pkt_queue);
  5304. skb_queue_head_init(&sd->process_queue);
  5305. sd->completion_queue = NULL;
  5306. INIT_LIST_HEAD(&sd->poll_list);
  5307. sd->output_queue = NULL;
  5308. sd->output_queue_tailp = &sd->output_queue;
  5309. #ifdef CONFIG_RPS
  5310. sd->csd.func = rps_trigger_softirq;
  5311. sd->csd.info = sd;
  5312. sd->csd.flags = 0;
  5313. sd->cpu = i;
  5314. #endif
  5315. sd->backlog.poll = process_backlog;
  5316. sd->backlog.weight = weight_p;
  5317. sd->backlog.gro_list = NULL;
  5318. sd->backlog.gro_count = 0;
  5319. }
  5320. dev_boot_phase = 0;
  5321. /* The loopback device is special if any other network devices
  5322. * is present in a network namespace the loopback device must
  5323. * be present. Since we now dynamically allocate and free the
  5324. * loopback device ensure this invariant is maintained by
  5325. * keeping the loopback device as the first device on the
  5326. * list of network devices. Ensuring the loopback devices
  5327. * is the first device that appears and the last network device
  5328. * that disappears.
  5329. */
  5330. if (register_pernet_device(&loopback_net_ops))
  5331. goto out;
  5332. if (register_pernet_device(&default_device_ops))
  5333. goto out;
  5334. open_softirq(NET_TX_SOFTIRQ, net_tx_action);
  5335. open_softirq(NET_RX_SOFTIRQ, net_rx_action);
  5336. hotcpu_notifier(dev_cpu_callback, 0);
  5337. dst_init();
  5338. rc = 0;
  5339. out:
  5340. return rc;
  5341. }
  5342. subsys_initcall(net_dev_init);