dev.c 197 KB

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