dev.c 155 KB

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