dev.c 154 KB

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