dev.c 173 KB

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