dev.c 179 KB

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