dev.c 191 KB

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