dev.c 154 KB

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