dev.c 189 KB

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