dev.c 172 KB

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