dev.c 163 KB

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