dev.c 225 KB

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