dev.c 246 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865
  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 <linux/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/sched/mm.h>
  84. #include <linux/mutex.h>
  85. #include <linux/string.h>
  86. #include <linux/mm.h>
  87. #include <linux/socket.h>
  88. #include <linux/sockios.h>
  89. #include <linux/errno.h>
  90. #include <linux/interrupt.h>
  91. #include <linux/if_ether.h>
  92. #include <linux/netdevice.h>
  93. #include <linux/etherdevice.h>
  94. #include <linux/ethtool.h>
  95. #include <linux/skbuff.h>
  96. #include <linux/bpf.h>
  97. #include <linux/bpf_trace.h>
  98. #include <net/net_namespace.h>
  99. #include <net/sock.h>
  100. #include <net/busy_poll.h>
  101. #include <linux/rtnetlink.h>
  102. #include <linux/stat.h>
  103. #include <net/dst.h>
  104. #include <net/dst_metadata.h>
  105. #include <net/pkt_sched.h>
  106. #include <net/pkt_cls.h>
  107. #include <net/checksum.h>
  108. #include <net/xfrm.h>
  109. #include <linux/highmem.h>
  110. #include <linux/init.h>
  111. #include <linux/module.h>
  112. #include <linux/netpoll.h>
  113. #include <linux/rcupdate.h>
  114. #include <linux/delay.h>
  115. #include <net/iw_handler.h>
  116. #include <asm/current.h>
  117. #include <linux/audit.h>
  118. #include <linux/dmaengine.h>
  119. #include <linux/err.h>
  120. #include <linux/ctype.h>
  121. #include <linux/if_arp.h>
  122. #include <linux/if_vlan.h>
  123. #include <linux/ip.h>
  124. #include <net/ip.h>
  125. #include <net/mpls.h>
  126. #include <linux/ipv6.h>
  127. #include <linux/in.h>
  128. #include <linux/jhash.h>
  129. #include <linux/random.h>
  130. #include <trace/events/napi.h>
  131. #include <trace/events/net.h>
  132. #include <trace/events/skb.h>
  133. #include <linux/pci.h>
  134. #include <linux/inetdevice.h>
  135. #include <linux/cpu_rmap.h>
  136. #include <linux/static_key.h>
  137. #include <linux/hashtable.h>
  138. #include <linux/vmalloc.h>
  139. #include <linux/if_macvlan.h>
  140. #include <linux/errqueue.h>
  141. #include <linux/hrtimer.h>
  142. #include <linux/netfilter_ingress.h>
  143. #include <linux/crash_dump.h>
  144. #include <linux/sctp.h>
  145. #include <net/udp_tunnel.h>
  146. #include <linux/net_namespace.h>
  147. #include "net-sysfs.h"
  148. #define MAX_GRO_SKBS 8
  149. #define MAX_NEST_DEV 8
  150. /* This should be increased if a protocol with a bigger head is added. */
  151. #define GRO_MAX_HEAD (MAX_HEADER + 128)
  152. static DEFINE_SPINLOCK(ptype_lock);
  153. static DEFINE_SPINLOCK(offload_lock);
  154. struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
  155. struct list_head ptype_all __read_mostly; /* Taps */
  156. static struct list_head offload_base __read_mostly;
  157. static int netif_rx_internal(struct sk_buff *skb);
  158. static int call_netdevice_notifiers_info(unsigned long val,
  159. struct netdev_notifier_info *info);
  160. static struct napi_struct *napi_by_id(unsigned int napi_id);
  161. /*
  162. * The @dev_base_head list is protected by @dev_base_lock and the rtnl
  163. * semaphore.
  164. *
  165. * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
  166. *
  167. * Writers must hold the rtnl semaphore while they loop through the
  168. * dev_base_head list, and hold dev_base_lock for writing when they do the
  169. * actual updates. This allows pure readers to access the list even
  170. * while a writer is preparing to update it.
  171. *
  172. * To put it another way, dev_base_lock is held for writing only to
  173. * protect against pure readers; the rtnl semaphore provides the
  174. * protection against other writers.
  175. *
  176. * See, for example usages, register_netdevice() and
  177. * unregister_netdevice(), which must be called with the rtnl
  178. * semaphore held.
  179. */
  180. DEFINE_RWLOCK(dev_base_lock);
  181. EXPORT_SYMBOL(dev_base_lock);
  182. static DEFINE_MUTEX(ifalias_mutex);
  183. /* protects napi_hash addition/deletion and napi_gen_id */
  184. static DEFINE_SPINLOCK(napi_hash_lock);
  185. static unsigned int napi_gen_id = NR_CPUS;
  186. static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
  187. static seqcount_t devnet_rename_seq;
  188. static inline void dev_base_seq_inc(struct net *net)
  189. {
  190. while (++net->dev_base_seq == 0)
  191. ;
  192. }
  193. static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
  194. {
  195. unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
  196. return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
  197. }
  198. static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
  199. {
  200. return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
  201. }
  202. static inline void rps_lock(struct softnet_data *sd)
  203. {
  204. #ifdef CONFIG_RPS
  205. spin_lock(&sd->input_pkt_queue.lock);
  206. #endif
  207. }
  208. static inline void rps_unlock(struct softnet_data *sd)
  209. {
  210. #ifdef CONFIG_RPS
  211. spin_unlock(&sd->input_pkt_queue.lock);
  212. #endif
  213. }
  214. /* Device list insertion */
  215. static void list_netdevice(struct net_device *dev)
  216. {
  217. struct net *net = dev_net(dev);
  218. ASSERT_RTNL();
  219. write_lock_bh(&dev_base_lock);
  220. list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
  221. hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
  222. hlist_add_head_rcu(&dev->index_hlist,
  223. dev_index_hash(net, dev->ifindex));
  224. write_unlock_bh(&dev_base_lock);
  225. dev_base_seq_inc(net);
  226. }
  227. /* Device list removal
  228. * caller must respect a RCU grace period before freeing/reusing dev
  229. */
  230. static void unlist_netdevice(struct net_device *dev)
  231. {
  232. ASSERT_RTNL();
  233. /* Unlink dev from the device chain */
  234. write_lock_bh(&dev_base_lock);
  235. list_del_rcu(&dev->dev_list);
  236. hlist_del_rcu(&dev->name_hlist);
  237. hlist_del_rcu(&dev->index_hlist);
  238. write_unlock_bh(&dev_base_lock);
  239. dev_base_seq_inc(dev_net(dev));
  240. }
  241. /*
  242. * Our notifier list
  243. */
  244. static RAW_NOTIFIER_HEAD(netdev_chain);
  245. /*
  246. * Device drivers call our routines to queue packets here. We empty the
  247. * queue in the local softnet handler.
  248. */
  249. DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
  250. EXPORT_PER_CPU_SYMBOL(softnet_data);
  251. #ifdef CONFIG_LOCKDEP
  252. /*
  253. * register_netdevice() inits txq->_xmit_lock and sets lockdep class
  254. * according to dev->type
  255. */
  256. static const unsigned short netdev_lock_type[] = {
  257. ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
  258. ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
  259. ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
  260. ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
  261. ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
  262. ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
  263. ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
  264. ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
  265. ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
  266. ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
  267. ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
  268. ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
  269. ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
  270. ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
  271. ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
  272. static const char *const netdev_lock_name[] = {
  273. "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
  274. "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
  275. "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
  276. "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
  277. "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
  278. "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
  279. "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
  280. "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
  281. "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
  282. "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
  283. "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
  284. "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
  285. "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
  286. "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
  287. "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
  288. static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
  289. static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
  290. static inline unsigned short netdev_lock_pos(unsigned short dev_type)
  291. {
  292. int i;
  293. for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
  294. if (netdev_lock_type[i] == dev_type)
  295. return i;
  296. /* the last key is used by default */
  297. return ARRAY_SIZE(netdev_lock_type) - 1;
  298. }
  299. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  300. unsigned short dev_type)
  301. {
  302. int i;
  303. i = netdev_lock_pos(dev_type);
  304. lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
  305. netdev_lock_name[i]);
  306. }
  307. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  308. {
  309. int i;
  310. i = netdev_lock_pos(dev->type);
  311. lockdep_set_class_and_name(&dev->addr_list_lock,
  312. &netdev_addr_lock_key[i],
  313. netdev_lock_name[i]);
  314. }
  315. #else
  316. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  317. unsigned short dev_type)
  318. {
  319. }
  320. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  321. {
  322. }
  323. #endif
  324. /*******************************************************************************
  325. *
  326. * Protocol management and registration routines
  327. *
  328. *******************************************************************************/
  329. /*
  330. * Add a protocol ID to the list. Now that the input handler is
  331. * smarter we can dispense with all the messy stuff that used to be
  332. * here.
  333. *
  334. * BEWARE!!! Protocol handlers, mangling input packets,
  335. * MUST BE last in hash buckets and checking protocol handlers
  336. * MUST start from promiscuous ptype_all chain in net_bh.
  337. * It is true now, do not change it.
  338. * Explanation follows: if protocol handler, mangling packet, will
  339. * be the first on list, it is not able to sense, that packet
  340. * is cloned and should be copied-on-write, so that it will
  341. * change it and subsequent readers will get broken packet.
  342. * --ANK (980803)
  343. */
  344. static inline struct list_head *ptype_head(const struct packet_type *pt)
  345. {
  346. if (pt->type == htons(ETH_P_ALL))
  347. return pt->dev ? &pt->dev->ptype_all : &ptype_all;
  348. else
  349. return pt->dev ? &pt->dev->ptype_specific :
  350. &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
  351. }
  352. /**
  353. * dev_add_pack - add packet handler
  354. * @pt: packet type declaration
  355. *
  356. * Add a protocol handler to the networking stack. The passed &packet_type
  357. * is linked into kernel lists and may not be freed until it has been
  358. * removed from the kernel lists.
  359. *
  360. * This call does not sleep therefore it can not
  361. * guarantee all CPU's that are in middle of receiving packets
  362. * will see the new packet type (until the next received packet).
  363. */
  364. void dev_add_pack(struct packet_type *pt)
  365. {
  366. struct list_head *head = ptype_head(pt);
  367. spin_lock(&ptype_lock);
  368. list_add_rcu(&pt->list, head);
  369. spin_unlock(&ptype_lock);
  370. }
  371. EXPORT_SYMBOL(dev_add_pack);
  372. /**
  373. * __dev_remove_pack - remove packet handler
  374. * @pt: packet type declaration
  375. *
  376. * Remove a protocol handler that was previously added to the kernel
  377. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  378. * from the kernel lists and can be freed or reused once this function
  379. * returns.
  380. *
  381. * The packet type might still be in use by receivers
  382. * and must not be freed until after all the CPU's have gone
  383. * through a quiescent state.
  384. */
  385. void __dev_remove_pack(struct packet_type *pt)
  386. {
  387. struct list_head *head = ptype_head(pt);
  388. struct packet_type *pt1;
  389. spin_lock(&ptype_lock);
  390. list_for_each_entry(pt1, head, list) {
  391. if (pt == pt1) {
  392. list_del_rcu(&pt->list);
  393. goto out;
  394. }
  395. }
  396. pr_warn("dev_remove_pack: %p not found\n", pt);
  397. out:
  398. spin_unlock(&ptype_lock);
  399. }
  400. EXPORT_SYMBOL(__dev_remove_pack);
  401. /**
  402. * dev_remove_pack - remove packet handler
  403. * @pt: packet type declaration
  404. *
  405. * Remove a protocol handler that was previously added to the kernel
  406. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  407. * from the kernel lists and can be freed or reused once this function
  408. * returns.
  409. *
  410. * This call sleeps to guarantee that no CPU is looking at the packet
  411. * type after return.
  412. */
  413. void dev_remove_pack(struct packet_type *pt)
  414. {
  415. __dev_remove_pack(pt);
  416. synchronize_net();
  417. }
  418. EXPORT_SYMBOL(dev_remove_pack);
  419. /**
  420. * dev_add_offload - register offload handlers
  421. * @po: protocol offload declaration
  422. *
  423. * Add protocol offload handlers to the networking stack. The passed
  424. * &proto_offload is linked into kernel lists and may not be freed until
  425. * it has been removed from the kernel lists.
  426. *
  427. * This call does not sleep therefore it can not
  428. * guarantee all CPU's that are in middle of receiving packets
  429. * will see the new offload handlers (until the next received packet).
  430. */
  431. void dev_add_offload(struct packet_offload *po)
  432. {
  433. struct packet_offload *elem;
  434. spin_lock(&offload_lock);
  435. list_for_each_entry(elem, &offload_base, list) {
  436. if (po->priority < elem->priority)
  437. break;
  438. }
  439. list_add_rcu(&po->list, elem->list.prev);
  440. spin_unlock(&offload_lock);
  441. }
  442. EXPORT_SYMBOL(dev_add_offload);
  443. /**
  444. * __dev_remove_offload - remove offload handler
  445. * @po: packet offload declaration
  446. *
  447. * Remove a protocol offload handler that was previously added to the
  448. * kernel offload handlers by dev_add_offload(). The passed &offload_type
  449. * is removed from the kernel lists and can be freed or reused once this
  450. * function returns.
  451. *
  452. * The packet type might still be in use by receivers
  453. * and must not be freed until after all the CPU's have gone
  454. * through a quiescent state.
  455. */
  456. static void __dev_remove_offload(struct packet_offload *po)
  457. {
  458. struct list_head *head = &offload_base;
  459. struct packet_offload *po1;
  460. spin_lock(&offload_lock);
  461. list_for_each_entry(po1, head, list) {
  462. if (po == po1) {
  463. list_del_rcu(&po->list);
  464. goto out;
  465. }
  466. }
  467. pr_warn("dev_remove_offload: %p not found\n", po);
  468. out:
  469. spin_unlock(&offload_lock);
  470. }
  471. /**
  472. * dev_remove_offload - remove packet offload handler
  473. * @po: packet offload declaration
  474. *
  475. * Remove a packet offload handler that was previously added to the kernel
  476. * offload handlers by dev_add_offload(). The passed &offload_type is
  477. * removed from the kernel lists and can be freed or reused once this
  478. * function returns.
  479. *
  480. * This call sleeps to guarantee that no CPU is looking at the packet
  481. * type after return.
  482. */
  483. void dev_remove_offload(struct packet_offload *po)
  484. {
  485. __dev_remove_offload(po);
  486. synchronize_net();
  487. }
  488. EXPORT_SYMBOL(dev_remove_offload);
  489. /******************************************************************************
  490. *
  491. * Device Boot-time Settings Routines
  492. *
  493. ******************************************************************************/
  494. /* Boot time configuration table */
  495. static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
  496. /**
  497. * netdev_boot_setup_add - add new setup entry
  498. * @name: name of the device
  499. * @map: configured settings for the device
  500. *
  501. * Adds new setup entry to the dev_boot_setup list. The function
  502. * returns 0 on error and 1 on success. This is a generic routine to
  503. * all netdevices.
  504. */
  505. static int netdev_boot_setup_add(char *name, struct ifmap *map)
  506. {
  507. struct netdev_boot_setup *s;
  508. int i;
  509. s = dev_boot_setup;
  510. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  511. if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
  512. memset(s[i].name, 0, sizeof(s[i].name));
  513. strlcpy(s[i].name, name, IFNAMSIZ);
  514. memcpy(&s[i].map, map, sizeof(s[i].map));
  515. break;
  516. }
  517. }
  518. return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
  519. }
  520. /**
  521. * netdev_boot_setup_check - check boot time settings
  522. * @dev: the netdevice
  523. *
  524. * Check boot time settings for the device.
  525. * The found settings are set for the device to be used
  526. * later in the device probing.
  527. * Returns 0 if no settings found, 1 if they are.
  528. */
  529. int netdev_boot_setup_check(struct net_device *dev)
  530. {
  531. struct netdev_boot_setup *s = dev_boot_setup;
  532. int i;
  533. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  534. if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
  535. !strcmp(dev->name, s[i].name)) {
  536. dev->irq = s[i].map.irq;
  537. dev->base_addr = s[i].map.base_addr;
  538. dev->mem_start = s[i].map.mem_start;
  539. dev->mem_end = s[i].map.mem_end;
  540. return 1;
  541. }
  542. }
  543. return 0;
  544. }
  545. EXPORT_SYMBOL(netdev_boot_setup_check);
  546. /**
  547. * netdev_boot_base - get address from boot time settings
  548. * @prefix: prefix for network device
  549. * @unit: id for network device
  550. *
  551. * Check boot time settings for the base address of device.
  552. * The found settings are set for the device to be used
  553. * later in the device probing.
  554. * Returns 0 if no settings found.
  555. */
  556. unsigned long netdev_boot_base(const char *prefix, int unit)
  557. {
  558. const struct netdev_boot_setup *s = dev_boot_setup;
  559. char name[IFNAMSIZ];
  560. int i;
  561. sprintf(name, "%s%d", prefix, unit);
  562. /*
  563. * If device already registered then return base of 1
  564. * to indicate not to probe for this interface
  565. */
  566. if (__dev_get_by_name(&init_net, name))
  567. return 1;
  568. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
  569. if (!strcmp(name, s[i].name))
  570. return s[i].map.base_addr;
  571. return 0;
  572. }
  573. /*
  574. * Saves at boot time configured settings for any netdevice.
  575. */
  576. int __init netdev_boot_setup(char *str)
  577. {
  578. int ints[5];
  579. struct ifmap map;
  580. str = get_options(str, ARRAY_SIZE(ints), ints);
  581. if (!str || !*str)
  582. return 0;
  583. /* Save settings */
  584. memset(&map, 0, sizeof(map));
  585. if (ints[0] > 0)
  586. map.irq = ints[1];
  587. if (ints[0] > 1)
  588. map.base_addr = ints[2];
  589. if (ints[0] > 2)
  590. map.mem_start = ints[3];
  591. if (ints[0] > 3)
  592. map.mem_end = ints[4];
  593. /* Add new entry to the list */
  594. return netdev_boot_setup_add(str, &map);
  595. }
  596. __setup("netdev=", netdev_boot_setup);
  597. /*******************************************************************************
  598. *
  599. * Device Interface Subroutines
  600. *
  601. *******************************************************************************/
  602. /**
  603. * dev_get_iflink - get 'iflink' value of a interface
  604. * @dev: targeted interface
  605. *
  606. * Indicates the ifindex the interface is linked to.
  607. * Physical interfaces have the same 'ifindex' and 'iflink' values.
  608. */
  609. int dev_get_iflink(const struct net_device *dev)
  610. {
  611. if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
  612. return dev->netdev_ops->ndo_get_iflink(dev);
  613. return dev->ifindex;
  614. }
  615. EXPORT_SYMBOL(dev_get_iflink);
  616. /**
  617. * dev_fill_metadata_dst - Retrieve tunnel egress information.
  618. * @dev: targeted interface
  619. * @skb: The packet.
  620. *
  621. * For better visibility of tunnel traffic OVS needs to retrieve
  622. * egress tunnel information for a packet. Following API allows
  623. * user to get this info.
  624. */
  625. int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
  626. {
  627. struct ip_tunnel_info *info;
  628. if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
  629. return -EINVAL;
  630. info = skb_tunnel_info_unclone(skb);
  631. if (!info)
  632. return -ENOMEM;
  633. if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
  634. return -EINVAL;
  635. return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
  636. }
  637. EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
  638. /**
  639. * __dev_get_by_name - find a device by its name
  640. * @net: the applicable net namespace
  641. * @name: name to find
  642. *
  643. * Find an interface by name. Must be called under RTNL semaphore
  644. * or @dev_base_lock. If the name is found a pointer to the device
  645. * is returned. If the name is not found then %NULL is returned. The
  646. * reference counters are not incremented so the caller must be
  647. * careful with locks.
  648. */
  649. struct net_device *__dev_get_by_name(struct net *net, const char *name)
  650. {
  651. struct net_device *dev;
  652. struct hlist_head *head = dev_name_hash(net, name);
  653. hlist_for_each_entry(dev, head, name_hlist)
  654. if (!strncmp(dev->name, name, IFNAMSIZ))
  655. return dev;
  656. return NULL;
  657. }
  658. EXPORT_SYMBOL(__dev_get_by_name);
  659. /**
  660. * dev_get_by_name_rcu - find a device by its name
  661. * @net: the applicable net namespace
  662. * @name: name to find
  663. *
  664. * Find an interface by name.
  665. * If the name is found a pointer to the device is returned.
  666. * If the name is not found then %NULL is returned.
  667. * The reference counters are not incremented so the caller must be
  668. * careful with locks. The caller must hold RCU lock.
  669. */
  670. struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
  671. {
  672. struct net_device *dev;
  673. struct hlist_head *head = dev_name_hash(net, name);
  674. hlist_for_each_entry_rcu(dev, head, name_hlist)
  675. if (!strncmp(dev->name, name, IFNAMSIZ))
  676. return dev;
  677. return NULL;
  678. }
  679. EXPORT_SYMBOL(dev_get_by_name_rcu);
  680. /**
  681. * dev_get_by_name - find a device by its name
  682. * @net: the applicable net namespace
  683. * @name: name to find
  684. *
  685. * Find an interface by name. This can be called from any
  686. * context and does its own locking. The returned handle has
  687. * the usage count incremented and the caller must use dev_put() to
  688. * release it when it is no longer needed. %NULL is returned if no
  689. * matching device is found.
  690. */
  691. struct net_device *dev_get_by_name(struct net *net, const char *name)
  692. {
  693. struct net_device *dev;
  694. rcu_read_lock();
  695. dev = dev_get_by_name_rcu(net, name);
  696. if (dev)
  697. dev_hold(dev);
  698. rcu_read_unlock();
  699. return dev;
  700. }
  701. EXPORT_SYMBOL(dev_get_by_name);
  702. /**
  703. * __dev_get_by_index - find a device by its ifindex
  704. * @net: the applicable net namespace
  705. * @ifindex: index of device
  706. *
  707. * Search for an interface by index. Returns %NULL if the device
  708. * is not found or a pointer to the device. The device has not
  709. * had its reference counter increased so the caller must be careful
  710. * about locking. The caller must hold either the RTNL semaphore
  711. * or @dev_base_lock.
  712. */
  713. struct net_device *__dev_get_by_index(struct net *net, int ifindex)
  714. {
  715. struct net_device *dev;
  716. struct hlist_head *head = dev_index_hash(net, ifindex);
  717. hlist_for_each_entry(dev, head, index_hlist)
  718. if (dev->ifindex == ifindex)
  719. return dev;
  720. return NULL;
  721. }
  722. EXPORT_SYMBOL(__dev_get_by_index);
  723. /**
  724. * dev_get_by_index_rcu - find a device by its ifindex
  725. * @net: the applicable net namespace
  726. * @ifindex: index of device
  727. *
  728. * Search for an interface by index. Returns %NULL if the device
  729. * is not found or a pointer to the device. The device has not
  730. * had its reference counter increased so the caller must be careful
  731. * about locking. The caller must hold RCU lock.
  732. */
  733. struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
  734. {
  735. struct net_device *dev;
  736. struct hlist_head *head = dev_index_hash(net, ifindex);
  737. hlist_for_each_entry_rcu(dev, head, index_hlist)
  738. if (dev->ifindex == ifindex)
  739. return dev;
  740. return NULL;
  741. }
  742. EXPORT_SYMBOL(dev_get_by_index_rcu);
  743. /**
  744. * dev_get_by_index - find a device by its ifindex
  745. * @net: the applicable net namespace
  746. * @ifindex: index of device
  747. *
  748. * Search for an interface by index. Returns NULL if the device
  749. * is not found or a pointer to the device. The device returned has
  750. * had a reference added and the pointer is safe until the user calls
  751. * dev_put to indicate they have finished with it.
  752. */
  753. struct net_device *dev_get_by_index(struct net *net, int ifindex)
  754. {
  755. struct net_device *dev;
  756. rcu_read_lock();
  757. dev = dev_get_by_index_rcu(net, ifindex);
  758. if (dev)
  759. dev_hold(dev);
  760. rcu_read_unlock();
  761. return dev;
  762. }
  763. EXPORT_SYMBOL(dev_get_by_index);
  764. /**
  765. * dev_get_by_napi_id - find a device by napi_id
  766. * @napi_id: ID of the NAPI struct
  767. *
  768. * Search for an interface by NAPI ID. Returns %NULL if the device
  769. * is not found or a pointer to the device. The device has not had
  770. * its reference counter increased so the caller must be careful
  771. * about locking. The caller must hold RCU lock.
  772. */
  773. struct net_device *dev_get_by_napi_id(unsigned int napi_id)
  774. {
  775. struct napi_struct *napi;
  776. WARN_ON_ONCE(!rcu_read_lock_held());
  777. if (napi_id < MIN_NAPI_ID)
  778. return NULL;
  779. napi = napi_by_id(napi_id);
  780. return napi ? napi->dev : NULL;
  781. }
  782. EXPORT_SYMBOL(dev_get_by_napi_id);
  783. /**
  784. * netdev_get_name - get a netdevice name, knowing its ifindex.
  785. * @net: network namespace
  786. * @name: a pointer to the buffer where the name will be stored.
  787. * @ifindex: the ifindex of the interface to get the name from.
  788. *
  789. * The use of raw_seqcount_begin() and cond_resched() before
  790. * retrying is required as we want to give the writers a chance
  791. * to complete when CONFIG_PREEMPT is not set.
  792. */
  793. int netdev_get_name(struct net *net, char *name, int ifindex)
  794. {
  795. struct net_device *dev;
  796. unsigned int seq;
  797. retry:
  798. seq = raw_seqcount_begin(&devnet_rename_seq);
  799. rcu_read_lock();
  800. dev = dev_get_by_index_rcu(net, ifindex);
  801. if (!dev) {
  802. rcu_read_unlock();
  803. return -ENODEV;
  804. }
  805. strcpy(name, dev->name);
  806. rcu_read_unlock();
  807. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  808. cond_resched();
  809. goto retry;
  810. }
  811. return 0;
  812. }
  813. /**
  814. * dev_getbyhwaddr_rcu - find a device by its hardware address
  815. * @net: the applicable net namespace
  816. * @type: media type of device
  817. * @ha: hardware address
  818. *
  819. * Search for an interface by MAC address. Returns NULL if the device
  820. * is not found or a pointer to the device.
  821. * The caller must hold RCU or RTNL.
  822. * The returned device has not had its ref count increased
  823. * and the caller must therefore be careful about locking
  824. *
  825. */
  826. struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
  827. const char *ha)
  828. {
  829. struct net_device *dev;
  830. for_each_netdev_rcu(net, dev)
  831. if (dev->type == type &&
  832. !memcmp(dev->dev_addr, ha, dev->addr_len))
  833. return dev;
  834. return NULL;
  835. }
  836. EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
  837. struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
  838. {
  839. struct net_device *dev;
  840. ASSERT_RTNL();
  841. for_each_netdev(net, dev)
  842. if (dev->type == type)
  843. return dev;
  844. return NULL;
  845. }
  846. EXPORT_SYMBOL(__dev_getfirstbyhwtype);
  847. struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
  848. {
  849. struct net_device *dev, *ret = NULL;
  850. rcu_read_lock();
  851. for_each_netdev_rcu(net, dev)
  852. if (dev->type == type) {
  853. dev_hold(dev);
  854. ret = dev;
  855. break;
  856. }
  857. rcu_read_unlock();
  858. return ret;
  859. }
  860. EXPORT_SYMBOL(dev_getfirstbyhwtype);
  861. /**
  862. * __dev_get_by_flags - find any device with given flags
  863. * @net: the applicable net namespace
  864. * @if_flags: IFF_* values
  865. * @mask: bitmask of bits in if_flags to check
  866. *
  867. * Search for any interface with the given flags. Returns NULL if a device
  868. * is not found or a pointer to the device. Must be called inside
  869. * rtnl_lock(), and result refcount is unchanged.
  870. */
  871. struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
  872. unsigned short mask)
  873. {
  874. struct net_device *dev, *ret;
  875. ASSERT_RTNL();
  876. ret = NULL;
  877. for_each_netdev(net, dev) {
  878. if (((dev->flags ^ if_flags) & mask) == 0) {
  879. ret = dev;
  880. break;
  881. }
  882. }
  883. return ret;
  884. }
  885. EXPORT_SYMBOL(__dev_get_by_flags);
  886. /**
  887. * dev_valid_name - check if name is okay for network device
  888. * @name: name string
  889. *
  890. * Network device names need to be valid file names to
  891. * to allow sysfs to work. We also disallow any kind of
  892. * whitespace.
  893. */
  894. bool dev_valid_name(const char *name)
  895. {
  896. if (*name == '\0')
  897. return false;
  898. if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
  899. return false;
  900. if (!strcmp(name, ".") || !strcmp(name, ".."))
  901. return false;
  902. while (*name) {
  903. if (*name == '/' || *name == ':' || isspace(*name))
  904. return false;
  905. name++;
  906. }
  907. return true;
  908. }
  909. EXPORT_SYMBOL(dev_valid_name);
  910. /**
  911. * __dev_alloc_name - allocate a name for a device
  912. * @net: network namespace to allocate the device name in
  913. * @name: name format string
  914. * @buf: scratch buffer and result name string
  915. *
  916. * Passed a format string - eg "lt%d" it will try and find a suitable
  917. * id. It scans list of devices to build up a free map, then chooses
  918. * the first empty slot. The caller must hold the dev_base or rtnl lock
  919. * while allocating the name and adding the device in order to avoid
  920. * duplicates.
  921. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  922. * Returns the number of the unit assigned or a negative errno code.
  923. */
  924. static int __dev_alloc_name(struct net *net, const char *name, char *buf)
  925. {
  926. int i = 0;
  927. const char *p;
  928. const int max_netdevices = 8*PAGE_SIZE;
  929. unsigned long *inuse;
  930. struct net_device *d;
  931. if (!dev_valid_name(name))
  932. return -EINVAL;
  933. p = strchr(name, '%');
  934. if (p) {
  935. /*
  936. * Verify the string as this thing may have come from
  937. * the user. There must be either one "%d" and no other "%"
  938. * characters.
  939. */
  940. if (p[1] != 'd' || strchr(p + 2, '%'))
  941. return -EINVAL;
  942. /* Use one page as a bit array of possible slots */
  943. inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
  944. if (!inuse)
  945. return -ENOMEM;
  946. for_each_netdev(net, d) {
  947. if (!sscanf(d->name, name, &i))
  948. continue;
  949. if (i < 0 || i >= max_netdevices)
  950. continue;
  951. /* avoid cases where sscanf is not exact inverse of printf */
  952. snprintf(buf, IFNAMSIZ, name, i);
  953. if (!strncmp(buf, d->name, IFNAMSIZ))
  954. set_bit(i, inuse);
  955. }
  956. i = find_first_zero_bit(inuse, max_netdevices);
  957. free_page((unsigned long) inuse);
  958. }
  959. snprintf(buf, IFNAMSIZ, name, i);
  960. if (!__dev_get_by_name(net, buf))
  961. return i;
  962. /* It is possible to run out of possible slots
  963. * when the name is long and there isn't enough space left
  964. * for the digits, or if all bits are used.
  965. */
  966. return -ENFILE;
  967. }
  968. static int dev_alloc_name_ns(struct net *net,
  969. struct net_device *dev,
  970. const char *name)
  971. {
  972. char buf[IFNAMSIZ];
  973. int ret;
  974. BUG_ON(!net);
  975. ret = __dev_alloc_name(net, name, buf);
  976. if (ret >= 0)
  977. strlcpy(dev->name, buf, IFNAMSIZ);
  978. return ret;
  979. }
  980. /**
  981. * dev_alloc_name - allocate a name for a device
  982. * @dev: device
  983. * @name: name format string
  984. *
  985. * Passed a format string - eg "lt%d" it will try and find a suitable
  986. * id. It scans list of devices to build up a free map, then chooses
  987. * the first empty slot. The caller must hold the dev_base or rtnl lock
  988. * while allocating the name and adding the device in order to avoid
  989. * duplicates.
  990. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  991. * Returns the number of the unit assigned or a negative errno code.
  992. */
  993. int dev_alloc_name(struct net_device *dev, const char *name)
  994. {
  995. return dev_alloc_name_ns(dev_net(dev), dev, name);
  996. }
  997. EXPORT_SYMBOL(dev_alloc_name);
  998. int dev_get_valid_name(struct net *net, struct net_device *dev,
  999. const char *name)
  1000. {
  1001. BUG_ON(!net);
  1002. if (!dev_valid_name(name))
  1003. return -EINVAL;
  1004. if (strchr(name, '%'))
  1005. return dev_alloc_name_ns(net, dev, name);
  1006. else if (__dev_get_by_name(net, name))
  1007. return -EEXIST;
  1008. else if (dev->name != name)
  1009. strlcpy(dev->name, name, IFNAMSIZ);
  1010. return 0;
  1011. }
  1012. EXPORT_SYMBOL(dev_get_valid_name);
  1013. /**
  1014. * dev_change_name - change name of a device
  1015. * @dev: device
  1016. * @newname: name (or format string) must be at least IFNAMSIZ
  1017. *
  1018. * Change name of a device, can pass format strings "eth%d".
  1019. * for wildcarding.
  1020. */
  1021. int dev_change_name(struct net_device *dev, const char *newname)
  1022. {
  1023. unsigned char old_assign_type;
  1024. char oldname[IFNAMSIZ];
  1025. int err = 0;
  1026. int ret;
  1027. struct net *net;
  1028. ASSERT_RTNL();
  1029. BUG_ON(!dev_net(dev));
  1030. net = dev_net(dev);
  1031. /* Some auto-enslaved devices e.g. failover slaves are
  1032. * special, as userspace might rename the device after
  1033. * the interface had been brought up and running since
  1034. * the point kernel initiated auto-enslavement. Allow
  1035. * live name change even when these slave devices are
  1036. * up and running.
  1037. *
  1038. * Typically, users of these auto-enslaving devices
  1039. * don't actually care about slave name change, as
  1040. * they are supposed to operate on master interface
  1041. * directly.
  1042. */
  1043. if (dev->flags & IFF_UP &&
  1044. likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
  1045. return -EBUSY;
  1046. write_seqcount_begin(&devnet_rename_seq);
  1047. if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  1048. write_seqcount_end(&devnet_rename_seq);
  1049. return 0;
  1050. }
  1051. memcpy(oldname, dev->name, IFNAMSIZ);
  1052. err = dev_get_valid_name(net, dev, newname);
  1053. if (err < 0) {
  1054. write_seqcount_end(&devnet_rename_seq);
  1055. return err;
  1056. }
  1057. if (oldname[0] && !strchr(oldname, '%'))
  1058. netdev_info(dev, "renamed from %s\n", oldname);
  1059. old_assign_type = dev->name_assign_type;
  1060. dev->name_assign_type = NET_NAME_RENAMED;
  1061. rollback:
  1062. ret = device_rename(&dev->dev, dev->name);
  1063. if (ret) {
  1064. memcpy(dev->name, oldname, IFNAMSIZ);
  1065. dev->name_assign_type = old_assign_type;
  1066. write_seqcount_end(&devnet_rename_seq);
  1067. return ret;
  1068. }
  1069. write_seqcount_end(&devnet_rename_seq);
  1070. netdev_adjacent_rename_links(dev, oldname);
  1071. write_lock_bh(&dev_base_lock);
  1072. hlist_del_rcu(&dev->name_hlist);
  1073. write_unlock_bh(&dev_base_lock);
  1074. synchronize_rcu();
  1075. write_lock_bh(&dev_base_lock);
  1076. hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
  1077. write_unlock_bh(&dev_base_lock);
  1078. ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
  1079. ret = notifier_to_errno(ret);
  1080. if (ret) {
  1081. /* err >= 0 after dev_alloc_name() or stores the first errno */
  1082. if (err >= 0) {
  1083. err = ret;
  1084. write_seqcount_begin(&devnet_rename_seq);
  1085. memcpy(dev->name, oldname, IFNAMSIZ);
  1086. memcpy(oldname, newname, IFNAMSIZ);
  1087. dev->name_assign_type = old_assign_type;
  1088. old_assign_type = NET_NAME_RENAMED;
  1089. goto rollback;
  1090. } else {
  1091. pr_err("%s: name change rollback failed: %d\n",
  1092. dev->name, ret);
  1093. }
  1094. }
  1095. return err;
  1096. }
  1097. /**
  1098. * dev_set_alias - change ifalias of a device
  1099. * @dev: device
  1100. * @alias: name up to IFALIASZ
  1101. * @len: limit of bytes to copy from info
  1102. *
  1103. * Set ifalias for a device,
  1104. */
  1105. int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
  1106. {
  1107. struct dev_ifalias *new_alias = NULL;
  1108. if (len >= IFALIASZ)
  1109. return -EINVAL;
  1110. if (len) {
  1111. new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
  1112. if (!new_alias)
  1113. return -ENOMEM;
  1114. memcpy(new_alias->ifalias, alias, len);
  1115. new_alias->ifalias[len] = 0;
  1116. }
  1117. mutex_lock(&ifalias_mutex);
  1118. rcu_swap_protected(dev->ifalias, new_alias,
  1119. mutex_is_locked(&ifalias_mutex));
  1120. mutex_unlock(&ifalias_mutex);
  1121. if (new_alias)
  1122. kfree_rcu(new_alias, rcuhead);
  1123. return len;
  1124. }
  1125. EXPORT_SYMBOL(dev_set_alias);
  1126. /**
  1127. * dev_get_alias - get ifalias of a device
  1128. * @dev: device
  1129. * @name: buffer to store name of ifalias
  1130. * @len: size of buffer
  1131. *
  1132. * get ifalias for a device. Caller must make sure dev cannot go
  1133. * away, e.g. rcu read lock or own a reference count to device.
  1134. */
  1135. int dev_get_alias(const struct net_device *dev, char *name, size_t len)
  1136. {
  1137. const struct dev_ifalias *alias;
  1138. int ret = 0;
  1139. rcu_read_lock();
  1140. alias = rcu_dereference(dev->ifalias);
  1141. if (alias)
  1142. ret = snprintf(name, len, "%s", alias->ifalias);
  1143. rcu_read_unlock();
  1144. return ret;
  1145. }
  1146. /**
  1147. * netdev_features_change - device changes features
  1148. * @dev: device to cause notification
  1149. *
  1150. * Called to indicate a device has changed features.
  1151. */
  1152. void netdev_features_change(struct net_device *dev)
  1153. {
  1154. call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
  1155. }
  1156. EXPORT_SYMBOL(netdev_features_change);
  1157. /**
  1158. * netdev_state_change - device changes state
  1159. * @dev: device to cause notification
  1160. *
  1161. * Called to indicate a device has changed state. This function calls
  1162. * the notifier chains for netdev_chain and sends a NEWLINK message
  1163. * to the routing socket.
  1164. */
  1165. void netdev_state_change(struct net_device *dev)
  1166. {
  1167. if (dev->flags & IFF_UP) {
  1168. struct netdev_notifier_change_info change_info = {
  1169. .info.dev = dev,
  1170. };
  1171. call_netdevice_notifiers_info(NETDEV_CHANGE,
  1172. &change_info.info);
  1173. rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
  1174. }
  1175. }
  1176. EXPORT_SYMBOL(netdev_state_change);
  1177. /**
  1178. * netdev_notify_peers - notify network peers about existence of @dev
  1179. * @dev: network device
  1180. *
  1181. * Generate traffic such that interested network peers are aware of
  1182. * @dev, such as by generating a gratuitous ARP. This may be used when
  1183. * a device wants to inform the rest of the network about some sort of
  1184. * reconfiguration such as a failover event or virtual machine
  1185. * migration.
  1186. */
  1187. void netdev_notify_peers(struct net_device *dev)
  1188. {
  1189. rtnl_lock();
  1190. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
  1191. call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
  1192. rtnl_unlock();
  1193. }
  1194. EXPORT_SYMBOL(netdev_notify_peers);
  1195. static int __dev_open(struct net_device *dev)
  1196. {
  1197. const struct net_device_ops *ops = dev->netdev_ops;
  1198. int ret;
  1199. ASSERT_RTNL();
  1200. if (!netif_device_present(dev))
  1201. return -ENODEV;
  1202. /* Block netpoll from trying to do any rx path servicing.
  1203. * If we don't do this there is a chance ndo_poll_controller
  1204. * or ndo_poll may be running while we open the device
  1205. */
  1206. netpoll_poll_disable(dev);
  1207. ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
  1208. ret = notifier_to_errno(ret);
  1209. if (ret)
  1210. return ret;
  1211. set_bit(__LINK_STATE_START, &dev->state);
  1212. if (ops->ndo_validate_addr)
  1213. ret = ops->ndo_validate_addr(dev);
  1214. if (!ret && ops->ndo_open)
  1215. ret = ops->ndo_open(dev);
  1216. netpoll_poll_enable(dev);
  1217. if (ret)
  1218. clear_bit(__LINK_STATE_START, &dev->state);
  1219. else {
  1220. dev->flags |= IFF_UP;
  1221. dev_set_rx_mode(dev);
  1222. dev_activate(dev);
  1223. add_device_randomness(dev->dev_addr, dev->addr_len);
  1224. }
  1225. return ret;
  1226. }
  1227. /**
  1228. * dev_open - prepare an interface for use.
  1229. * @dev: device to open
  1230. *
  1231. * Takes a device from down to up state. The device's private open
  1232. * function is invoked and then the multicast lists are loaded. Finally
  1233. * the device is moved into the up state and a %NETDEV_UP message is
  1234. * sent to the netdev notifier chain.
  1235. *
  1236. * Calling this function on an active interface is a nop. On a failure
  1237. * a negative errno code is returned.
  1238. */
  1239. int dev_open(struct net_device *dev)
  1240. {
  1241. int ret;
  1242. if (dev->flags & IFF_UP)
  1243. return 0;
  1244. ret = __dev_open(dev);
  1245. if (ret < 0)
  1246. return ret;
  1247. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
  1248. call_netdevice_notifiers(NETDEV_UP, dev);
  1249. return ret;
  1250. }
  1251. EXPORT_SYMBOL(dev_open);
  1252. static void __dev_close_many(struct list_head *head)
  1253. {
  1254. struct net_device *dev;
  1255. ASSERT_RTNL();
  1256. might_sleep();
  1257. list_for_each_entry(dev, head, close_list) {
  1258. /* Temporarily disable netpoll until the interface is down */
  1259. netpoll_poll_disable(dev);
  1260. call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
  1261. clear_bit(__LINK_STATE_START, &dev->state);
  1262. /* Synchronize to scheduled poll. We cannot touch poll list, it
  1263. * can be even on different cpu. So just clear netif_running().
  1264. *
  1265. * dev->stop() will invoke napi_disable() on all of it's
  1266. * napi_struct instances on this device.
  1267. */
  1268. smp_mb__after_atomic(); /* Commit netif_running(). */
  1269. }
  1270. dev_deactivate_many(head);
  1271. list_for_each_entry(dev, head, close_list) {
  1272. const struct net_device_ops *ops = dev->netdev_ops;
  1273. /*
  1274. * Call the device specific close. This cannot fail.
  1275. * Only if device is UP
  1276. *
  1277. * We allow it to be called even after a DETACH hot-plug
  1278. * event.
  1279. */
  1280. if (ops->ndo_stop)
  1281. ops->ndo_stop(dev);
  1282. dev->flags &= ~IFF_UP;
  1283. netpoll_poll_enable(dev);
  1284. }
  1285. }
  1286. static void __dev_close(struct net_device *dev)
  1287. {
  1288. LIST_HEAD(single);
  1289. list_add(&dev->close_list, &single);
  1290. __dev_close_many(&single);
  1291. list_del(&single);
  1292. }
  1293. void dev_close_many(struct list_head *head, bool unlink)
  1294. {
  1295. struct net_device *dev, *tmp;
  1296. /* Remove the devices that don't need to be closed */
  1297. list_for_each_entry_safe(dev, tmp, head, close_list)
  1298. if (!(dev->flags & IFF_UP))
  1299. list_del_init(&dev->close_list);
  1300. __dev_close_many(head);
  1301. list_for_each_entry_safe(dev, tmp, head, close_list) {
  1302. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
  1303. call_netdevice_notifiers(NETDEV_DOWN, dev);
  1304. if (unlink)
  1305. list_del_init(&dev->close_list);
  1306. }
  1307. }
  1308. EXPORT_SYMBOL(dev_close_many);
  1309. /**
  1310. * dev_close - shutdown an interface.
  1311. * @dev: device to shutdown
  1312. *
  1313. * This function moves an active device into down state. A
  1314. * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
  1315. * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
  1316. * chain.
  1317. */
  1318. void dev_close(struct net_device *dev)
  1319. {
  1320. if (dev->flags & IFF_UP) {
  1321. LIST_HEAD(single);
  1322. list_add(&dev->close_list, &single);
  1323. dev_close_many(&single, true);
  1324. list_del(&single);
  1325. }
  1326. }
  1327. EXPORT_SYMBOL(dev_close);
  1328. /**
  1329. * dev_disable_lro - disable Large Receive Offload on a device
  1330. * @dev: device
  1331. *
  1332. * Disable Large Receive Offload (LRO) on a net device. Must be
  1333. * called under RTNL. This is needed if received packets may be
  1334. * forwarded to another interface.
  1335. */
  1336. void dev_disable_lro(struct net_device *dev)
  1337. {
  1338. struct net_device *lower_dev;
  1339. struct list_head *iter;
  1340. dev->wanted_features &= ~NETIF_F_LRO;
  1341. netdev_update_features(dev);
  1342. if (unlikely(dev->features & NETIF_F_LRO))
  1343. netdev_WARN(dev, "failed to disable LRO!\n");
  1344. netdev_for_each_lower_dev(dev, lower_dev, iter)
  1345. dev_disable_lro(lower_dev);
  1346. }
  1347. EXPORT_SYMBOL(dev_disable_lro);
  1348. /**
  1349. * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
  1350. * @dev: device
  1351. *
  1352. * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
  1353. * called under RTNL. This is needed if Generic XDP is installed on
  1354. * the device.
  1355. */
  1356. static void dev_disable_gro_hw(struct net_device *dev)
  1357. {
  1358. dev->wanted_features &= ~NETIF_F_GRO_HW;
  1359. netdev_update_features(dev);
  1360. if (unlikely(dev->features & NETIF_F_GRO_HW))
  1361. netdev_WARN(dev, "failed to disable GRO_HW!\n");
  1362. }
  1363. const char *netdev_cmd_to_name(enum netdev_cmd cmd)
  1364. {
  1365. #define N(val) \
  1366. case NETDEV_##val: \
  1367. return "NETDEV_" __stringify(val);
  1368. switch (cmd) {
  1369. N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
  1370. N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
  1371. N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
  1372. N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
  1373. N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
  1374. N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
  1375. N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
  1376. N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
  1377. N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
  1378. }
  1379. #undef N
  1380. return "UNKNOWN_NETDEV_EVENT";
  1381. }
  1382. EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
  1383. static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
  1384. struct net_device *dev)
  1385. {
  1386. struct netdev_notifier_info info = {
  1387. .dev = dev,
  1388. };
  1389. return nb->notifier_call(nb, val, &info);
  1390. }
  1391. static int dev_boot_phase = 1;
  1392. /**
  1393. * register_netdevice_notifier - register a network notifier block
  1394. * @nb: notifier
  1395. *
  1396. * Register a notifier to be called when network device events occur.
  1397. * The notifier passed is linked into the kernel structures and must
  1398. * not be reused until it has been unregistered. A negative errno code
  1399. * is returned on a failure.
  1400. *
  1401. * When registered all registration and up events are replayed
  1402. * to the new notifier to allow device to have a race free
  1403. * view of the network device list.
  1404. */
  1405. int register_netdevice_notifier(struct notifier_block *nb)
  1406. {
  1407. struct net_device *dev;
  1408. struct net_device *last;
  1409. struct net *net;
  1410. int err;
  1411. /* Close race with setup_net() and cleanup_net() */
  1412. down_write(&pernet_ops_rwsem);
  1413. rtnl_lock();
  1414. err = raw_notifier_chain_register(&netdev_chain, nb);
  1415. if (err)
  1416. goto unlock;
  1417. if (dev_boot_phase)
  1418. goto unlock;
  1419. for_each_net(net) {
  1420. for_each_netdev(net, dev) {
  1421. err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
  1422. err = notifier_to_errno(err);
  1423. if (err)
  1424. goto rollback;
  1425. if (!(dev->flags & IFF_UP))
  1426. continue;
  1427. call_netdevice_notifier(nb, NETDEV_UP, dev);
  1428. }
  1429. }
  1430. unlock:
  1431. rtnl_unlock();
  1432. up_write(&pernet_ops_rwsem);
  1433. return err;
  1434. rollback:
  1435. last = dev;
  1436. for_each_net(net) {
  1437. for_each_netdev(net, dev) {
  1438. if (dev == last)
  1439. goto outroll;
  1440. if (dev->flags & IFF_UP) {
  1441. call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
  1442. dev);
  1443. call_netdevice_notifier(nb, NETDEV_DOWN, dev);
  1444. }
  1445. call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
  1446. }
  1447. }
  1448. outroll:
  1449. raw_notifier_chain_unregister(&netdev_chain, nb);
  1450. goto unlock;
  1451. }
  1452. EXPORT_SYMBOL(register_netdevice_notifier);
  1453. /**
  1454. * unregister_netdevice_notifier - unregister a network notifier block
  1455. * @nb: notifier
  1456. *
  1457. * Unregister a notifier previously registered by
  1458. * register_netdevice_notifier(). The notifier is unlinked into the
  1459. * kernel structures and may then be reused. A negative errno code
  1460. * is returned on a failure.
  1461. *
  1462. * After unregistering unregister and down device events are synthesized
  1463. * for all devices on the device list to the removed notifier to remove
  1464. * the need for special case cleanup code.
  1465. */
  1466. int unregister_netdevice_notifier(struct notifier_block *nb)
  1467. {
  1468. struct net_device *dev;
  1469. struct net *net;
  1470. int err;
  1471. /* Close race with setup_net() and cleanup_net() */
  1472. down_write(&pernet_ops_rwsem);
  1473. rtnl_lock();
  1474. err = raw_notifier_chain_unregister(&netdev_chain, nb);
  1475. if (err)
  1476. goto unlock;
  1477. for_each_net(net) {
  1478. for_each_netdev(net, dev) {
  1479. if (dev->flags & IFF_UP) {
  1480. call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
  1481. dev);
  1482. call_netdevice_notifier(nb, NETDEV_DOWN, dev);
  1483. }
  1484. call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
  1485. }
  1486. }
  1487. unlock:
  1488. rtnl_unlock();
  1489. up_write(&pernet_ops_rwsem);
  1490. return err;
  1491. }
  1492. EXPORT_SYMBOL(unregister_netdevice_notifier);
  1493. /**
  1494. * call_netdevice_notifiers_info - call all network notifier blocks
  1495. * @val: value passed unmodified to notifier function
  1496. * @info: notifier information data
  1497. *
  1498. * Call all network notifier blocks. Parameters and return value
  1499. * are as for raw_notifier_call_chain().
  1500. */
  1501. static int call_netdevice_notifiers_info(unsigned long val,
  1502. struct netdev_notifier_info *info)
  1503. {
  1504. ASSERT_RTNL();
  1505. return raw_notifier_call_chain(&netdev_chain, val, info);
  1506. }
  1507. /**
  1508. * call_netdevice_notifiers - call all network notifier blocks
  1509. * @val: value passed unmodified to notifier function
  1510. * @dev: net_device pointer passed unmodified to notifier function
  1511. *
  1512. * Call all network notifier blocks. Parameters and return value
  1513. * are as for raw_notifier_call_chain().
  1514. */
  1515. int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
  1516. {
  1517. struct netdev_notifier_info info = {
  1518. .dev = dev,
  1519. };
  1520. return call_netdevice_notifiers_info(val, &info);
  1521. }
  1522. EXPORT_SYMBOL(call_netdevice_notifiers);
  1523. /**
  1524. * call_netdevice_notifiers_mtu - call all network notifier blocks
  1525. * @val: value passed unmodified to notifier function
  1526. * @dev: net_device pointer passed unmodified to notifier function
  1527. * @arg: additional u32 argument passed to the notifier function
  1528. *
  1529. * Call all network notifier blocks. Parameters and return value
  1530. * are as for raw_notifier_call_chain().
  1531. */
  1532. static int call_netdevice_notifiers_mtu(unsigned long val,
  1533. struct net_device *dev, u32 arg)
  1534. {
  1535. struct netdev_notifier_info_ext info = {
  1536. .info.dev = dev,
  1537. .ext.mtu = arg,
  1538. };
  1539. BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
  1540. return call_netdevice_notifiers_info(val, &info.info);
  1541. }
  1542. #ifdef CONFIG_NET_INGRESS
  1543. static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
  1544. void net_inc_ingress_queue(void)
  1545. {
  1546. static_branch_inc(&ingress_needed_key);
  1547. }
  1548. EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
  1549. void net_dec_ingress_queue(void)
  1550. {
  1551. static_branch_dec(&ingress_needed_key);
  1552. }
  1553. EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
  1554. #endif
  1555. #ifdef CONFIG_NET_EGRESS
  1556. static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
  1557. void net_inc_egress_queue(void)
  1558. {
  1559. static_branch_inc(&egress_needed_key);
  1560. }
  1561. EXPORT_SYMBOL_GPL(net_inc_egress_queue);
  1562. void net_dec_egress_queue(void)
  1563. {
  1564. static_branch_dec(&egress_needed_key);
  1565. }
  1566. EXPORT_SYMBOL_GPL(net_dec_egress_queue);
  1567. #endif
  1568. static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
  1569. #ifdef CONFIG_JUMP_LABEL
  1570. static atomic_t netstamp_needed_deferred;
  1571. static atomic_t netstamp_wanted;
  1572. static void netstamp_clear(struct work_struct *work)
  1573. {
  1574. int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
  1575. int wanted;
  1576. wanted = atomic_add_return(deferred, &netstamp_wanted);
  1577. if (wanted > 0)
  1578. static_branch_enable(&netstamp_needed_key);
  1579. else
  1580. static_branch_disable(&netstamp_needed_key);
  1581. }
  1582. static DECLARE_WORK(netstamp_work, netstamp_clear);
  1583. #endif
  1584. void net_enable_timestamp(void)
  1585. {
  1586. #ifdef CONFIG_JUMP_LABEL
  1587. int wanted;
  1588. while (1) {
  1589. wanted = atomic_read(&netstamp_wanted);
  1590. if (wanted <= 0)
  1591. break;
  1592. if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
  1593. return;
  1594. }
  1595. atomic_inc(&netstamp_needed_deferred);
  1596. schedule_work(&netstamp_work);
  1597. #else
  1598. static_branch_inc(&netstamp_needed_key);
  1599. #endif
  1600. }
  1601. EXPORT_SYMBOL(net_enable_timestamp);
  1602. void net_disable_timestamp(void)
  1603. {
  1604. #ifdef CONFIG_JUMP_LABEL
  1605. int wanted;
  1606. while (1) {
  1607. wanted = atomic_read(&netstamp_wanted);
  1608. if (wanted <= 1)
  1609. break;
  1610. if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
  1611. return;
  1612. }
  1613. atomic_dec(&netstamp_needed_deferred);
  1614. schedule_work(&netstamp_work);
  1615. #else
  1616. static_branch_dec(&netstamp_needed_key);
  1617. #endif
  1618. }
  1619. EXPORT_SYMBOL(net_disable_timestamp);
  1620. static inline void net_timestamp_set(struct sk_buff *skb)
  1621. {
  1622. skb->tstamp = 0;
  1623. if (static_branch_unlikely(&netstamp_needed_key))
  1624. __net_timestamp(skb);
  1625. }
  1626. #define net_timestamp_check(COND, SKB) \
  1627. if (static_branch_unlikely(&netstamp_needed_key)) { \
  1628. if ((COND) && !(SKB)->tstamp) \
  1629. __net_timestamp(SKB); \
  1630. } \
  1631. bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
  1632. {
  1633. unsigned int len;
  1634. if (!(dev->flags & IFF_UP))
  1635. return false;
  1636. len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
  1637. if (skb->len <= len)
  1638. return true;
  1639. /* if TSO is enabled, we don't care about the length as the packet
  1640. * could be forwarded without being segmented before
  1641. */
  1642. if (skb_is_gso(skb))
  1643. return true;
  1644. return false;
  1645. }
  1646. EXPORT_SYMBOL_GPL(is_skb_forwardable);
  1647. int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
  1648. {
  1649. int ret = ____dev_forward_skb(dev, skb);
  1650. if (likely(!ret)) {
  1651. skb->protocol = eth_type_trans(skb, dev);
  1652. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  1653. }
  1654. return ret;
  1655. }
  1656. EXPORT_SYMBOL_GPL(__dev_forward_skb);
  1657. /**
  1658. * dev_forward_skb - loopback an skb to another netif
  1659. *
  1660. * @dev: destination network device
  1661. * @skb: buffer to forward
  1662. *
  1663. * return values:
  1664. * NET_RX_SUCCESS (no congestion)
  1665. * NET_RX_DROP (packet was dropped, but freed)
  1666. *
  1667. * dev_forward_skb can be used for injecting an skb from the
  1668. * start_xmit function of one device into the receive queue
  1669. * of another device.
  1670. *
  1671. * The receiving device may be in another namespace, so
  1672. * we have to clear all information in the skb that could
  1673. * impact namespace isolation.
  1674. */
  1675. int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
  1676. {
  1677. return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
  1678. }
  1679. EXPORT_SYMBOL_GPL(dev_forward_skb);
  1680. static inline int deliver_skb(struct sk_buff *skb,
  1681. struct packet_type *pt_prev,
  1682. struct net_device *orig_dev)
  1683. {
  1684. if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
  1685. return -ENOMEM;
  1686. refcount_inc(&skb->users);
  1687. return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  1688. }
  1689. static inline void deliver_ptype_list_skb(struct sk_buff *skb,
  1690. struct packet_type **pt,
  1691. struct net_device *orig_dev,
  1692. __be16 type,
  1693. struct list_head *ptype_list)
  1694. {
  1695. struct packet_type *ptype, *pt_prev = *pt;
  1696. list_for_each_entry_rcu(ptype, ptype_list, list) {
  1697. if (ptype->type != type)
  1698. continue;
  1699. if (pt_prev)
  1700. deliver_skb(skb, pt_prev, orig_dev);
  1701. pt_prev = ptype;
  1702. }
  1703. *pt = pt_prev;
  1704. }
  1705. static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
  1706. {
  1707. if (!ptype->af_packet_priv || !skb->sk)
  1708. return false;
  1709. if (ptype->id_match)
  1710. return ptype->id_match(ptype, skb->sk);
  1711. else if ((struct sock *)ptype->af_packet_priv == skb->sk)
  1712. return true;
  1713. return false;
  1714. }
  1715. /*
  1716. * Support routine. Sends outgoing frames to any network
  1717. * taps currently in use.
  1718. */
  1719. void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
  1720. {
  1721. struct packet_type *ptype;
  1722. struct sk_buff *skb2 = NULL;
  1723. struct packet_type *pt_prev = NULL;
  1724. struct list_head *ptype_list = &ptype_all;
  1725. rcu_read_lock();
  1726. again:
  1727. list_for_each_entry_rcu(ptype, ptype_list, list) {
  1728. /* Never send packets back to the socket
  1729. * they originated from - MvS (miquels@drinkel.ow.org)
  1730. */
  1731. if (skb_loop_sk(ptype, skb))
  1732. continue;
  1733. if (pt_prev) {
  1734. deliver_skb(skb2, pt_prev, skb->dev);
  1735. pt_prev = ptype;
  1736. continue;
  1737. }
  1738. /* need to clone skb, done only once */
  1739. skb2 = skb_clone(skb, GFP_ATOMIC);
  1740. if (!skb2)
  1741. goto out_unlock;
  1742. net_timestamp_set(skb2);
  1743. /* skb->nh should be correctly
  1744. * set by sender, so that the second statement is
  1745. * just protection against buggy protocols.
  1746. */
  1747. skb_reset_mac_header(skb2);
  1748. if (skb_network_header(skb2) < skb2->data ||
  1749. skb_network_header(skb2) > skb_tail_pointer(skb2)) {
  1750. net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
  1751. ntohs(skb2->protocol),
  1752. dev->name);
  1753. skb_reset_network_header(skb2);
  1754. }
  1755. skb2->transport_header = skb2->network_header;
  1756. skb2->pkt_type = PACKET_OUTGOING;
  1757. pt_prev = ptype;
  1758. }
  1759. if (ptype_list == &ptype_all) {
  1760. ptype_list = &dev->ptype_all;
  1761. goto again;
  1762. }
  1763. out_unlock:
  1764. if (pt_prev) {
  1765. if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
  1766. pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
  1767. else
  1768. kfree_skb(skb2);
  1769. }
  1770. rcu_read_unlock();
  1771. }
  1772. EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
  1773. /**
  1774. * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
  1775. * @dev: Network device
  1776. * @txq: number of queues available
  1777. *
  1778. * If real_num_tx_queues is changed the tc mappings may no longer be
  1779. * valid. To resolve this verify the tc mapping remains valid and if
  1780. * not NULL the mapping. With no priorities mapping to this
  1781. * offset/count pair it will no longer be used. In the worst case TC0
  1782. * is invalid nothing can be done so disable priority mappings. If is
  1783. * expected that drivers will fix this mapping if they can before
  1784. * calling netif_set_real_num_tx_queues.
  1785. */
  1786. static void netif_setup_tc(struct net_device *dev, unsigned int txq)
  1787. {
  1788. int i;
  1789. struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
  1790. /* If TC0 is invalidated disable TC mapping */
  1791. if (tc->offset + tc->count > txq) {
  1792. pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
  1793. dev->num_tc = 0;
  1794. return;
  1795. }
  1796. /* Invalidated prio to tc mappings set to TC0 */
  1797. for (i = 1; i < TC_BITMASK + 1; i++) {
  1798. int q = netdev_get_prio_tc_map(dev, i);
  1799. tc = &dev->tc_to_txq[q];
  1800. if (tc->offset + tc->count > txq) {
  1801. pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
  1802. i, q);
  1803. netdev_set_prio_tc_map(dev, i, 0);
  1804. }
  1805. }
  1806. }
  1807. int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
  1808. {
  1809. if (dev->num_tc) {
  1810. struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
  1811. int i;
  1812. /* walk through the TCs and see if it falls into any of them */
  1813. for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
  1814. if ((txq - tc->offset) < tc->count)
  1815. return i;
  1816. }
  1817. /* didn't find it, just return -1 to indicate no match */
  1818. return -1;
  1819. }
  1820. return 0;
  1821. }
  1822. EXPORT_SYMBOL(netdev_txq_to_tc);
  1823. #ifdef CONFIG_XPS
  1824. struct static_key xps_needed __read_mostly;
  1825. EXPORT_SYMBOL(xps_needed);
  1826. struct static_key xps_rxqs_needed __read_mostly;
  1827. EXPORT_SYMBOL(xps_rxqs_needed);
  1828. static DEFINE_MUTEX(xps_map_mutex);
  1829. #define xmap_dereference(P) \
  1830. rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
  1831. static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
  1832. int tci, u16 index)
  1833. {
  1834. struct xps_map *map = NULL;
  1835. int pos;
  1836. if (dev_maps)
  1837. map = xmap_dereference(dev_maps->attr_map[tci]);
  1838. if (!map)
  1839. return false;
  1840. for (pos = map->len; pos--;) {
  1841. if (map->queues[pos] != index)
  1842. continue;
  1843. if (map->len > 1) {
  1844. map->queues[pos] = map->queues[--map->len];
  1845. break;
  1846. }
  1847. RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
  1848. kfree_rcu(map, rcu);
  1849. return false;
  1850. }
  1851. return true;
  1852. }
  1853. static bool remove_xps_queue_cpu(struct net_device *dev,
  1854. struct xps_dev_maps *dev_maps,
  1855. int cpu, u16 offset, u16 count)
  1856. {
  1857. int num_tc = dev->num_tc ? : 1;
  1858. bool active = false;
  1859. int tci;
  1860. for (tci = cpu * num_tc; num_tc--; tci++) {
  1861. int i, j;
  1862. for (i = count, j = offset; i--; j++) {
  1863. if (!remove_xps_queue(dev_maps, tci, j))
  1864. break;
  1865. }
  1866. active |= i < 0;
  1867. }
  1868. return active;
  1869. }
  1870. static void reset_xps_maps(struct net_device *dev,
  1871. struct xps_dev_maps *dev_maps,
  1872. bool is_rxqs_map)
  1873. {
  1874. if (is_rxqs_map) {
  1875. static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
  1876. RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
  1877. } else {
  1878. RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
  1879. }
  1880. static_key_slow_dec_cpuslocked(&xps_needed);
  1881. kfree_rcu(dev_maps, rcu);
  1882. }
  1883. static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
  1884. struct xps_dev_maps *dev_maps, unsigned int nr_ids,
  1885. u16 offset, u16 count, bool is_rxqs_map)
  1886. {
  1887. bool active = false;
  1888. int i, j;
  1889. for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
  1890. j < nr_ids;)
  1891. active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
  1892. count);
  1893. if (!active)
  1894. reset_xps_maps(dev, dev_maps, is_rxqs_map);
  1895. if (!is_rxqs_map) {
  1896. for (i = offset + (count - 1); count--; i--) {
  1897. netdev_queue_numa_node_write(
  1898. netdev_get_tx_queue(dev, i),
  1899. NUMA_NO_NODE);
  1900. }
  1901. }
  1902. }
  1903. static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
  1904. u16 count)
  1905. {
  1906. const unsigned long *possible_mask = NULL;
  1907. struct xps_dev_maps *dev_maps;
  1908. unsigned int nr_ids;
  1909. if (!static_key_false(&xps_needed))
  1910. return;
  1911. cpus_read_lock();
  1912. mutex_lock(&xps_map_mutex);
  1913. if (static_key_false(&xps_rxqs_needed)) {
  1914. dev_maps = xmap_dereference(dev->xps_rxqs_map);
  1915. if (dev_maps) {
  1916. nr_ids = dev->num_rx_queues;
  1917. clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
  1918. offset, count, true);
  1919. }
  1920. }
  1921. dev_maps = xmap_dereference(dev->xps_cpus_map);
  1922. if (!dev_maps)
  1923. goto out_no_maps;
  1924. if (num_possible_cpus() > 1)
  1925. possible_mask = cpumask_bits(cpu_possible_mask);
  1926. nr_ids = nr_cpu_ids;
  1927. clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
  1928. false);
  1929. out_no_maps:
  1930. mutex_unlock(&xps_map_mutex);
  1931. cpus_read_unlock();
  1932. }
  1933. static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
  1934. {
  1935. netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
  1936. }
  1937. static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
  1938. u16 index, bool is_rxqs_map)
  1939. {
  1940. struct xps_map *new_map;
  1941. int alloc_len = XPS_MIN_MAP_ALLOC;
  1942. int i, pos;
  1943. for (pos = 0; map && pos < map->len; pos++) {
  1944. if (map->queues[pos] != index)
  1945. continue;
  1946. return map;
  1947. }
  1948. /* Need to add tx-queue to this CPU's/rx-queue's existing map */
  1949. if (map) {
  1950. if (pos < map->alloc_len)
  1951. return map;
  1952. alloc_len = map->alloc_len * 2;
  1953. }
  1954. /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
  1955. * map
  1956. */
  1957. if (is_rxqs_map)
  1958. new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
  1959. else
  1960. new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
  1961. cpu_to_node(attr_index));
  1962. if (!new_map)
  1963. return NULL;
  1964. for (i = 0; i < pos; i++)
  1965. new_map->queues[i] = map->queues[i];
  1966. new_map->alloc_len = alloc_len;
  1967. new_map->len = pos;
  1968. return new_map;
  1969. }
  1970. /* Must be called under cpus_read_lock */
  1971. int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
  1972. u16 index, bool is_rxqs_map)
  1973. {
  1974. const unsigned long *online_mask = NULL, *possible_mask = NULL;
  1975. struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
  1976. int i, j, tci, numa_node_id = -2;
  1977. int maps_sz, num_tc = 1, tc = 0;
  1978. struct xps_map *map, *new_map;
  1979. bool active = false;
  1980. unsigned int nr_ids;
  1981. if (dev->num_tc) {
  1982. /* Do not allow XPS on subordinate device directly */
  1983. num_tc = dev->num_tc;
  1984. if (num_tc < 0)
  1985. return -EINVAL;
  1986. /* If queue belongs to subordinate dev use its map */
  1987. dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
  1988. tc = netdev_txq_to_tc(dev, index);
  1989. if (tc < 0)
  1990. return -EINVAL;
  1991. }
  1992. mutex_lock(&xps_map_mutex);
  1993. if (is_rxqs_map) {
  1994. maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
  1995. dev_maps = xmap_dereference(dev->xps_rxqs_map);
  1996. nr_ids = dev->num_rx_queues;
  1997. } else {
  1998. maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
  1999. if (num_possible_cpus() > 1) {
  2000. online_mask = cpumask_bits(cpu_online_mask);
  2001. possible_mask = cpumask_bits(cpu_possible_mask);
  2002. }
  2003. dev_maps = xmap_dereference(dev->xps_cpus_map);
  2004. nr_ids = nr_cpu_ids;
  2005. }
  2006. if (maps_sz < L1_CACHE_BYTES)
  2007. maps_sz = L1_CACHE_BYTES;
  2008. /* allocate memory for queue storage */
  2009. for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
  2010. j < nr_ids;) {
  2011. if (!new_dev_maps)
  2012. new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
  2013. if (!new_dev_maps) {
  2014. mutex_unlock(&xps_map_mutex);
  2015. return -ENOMEM;
  2016. }
  2017. tci = j * num_tc + tc;
  2018. map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
  2019. NULL;
  2020. map = expand_xps_map(map, j, index, is_rxqs_map);
  2021. if (!map)
  2022. goto error;
  2023. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  2024. }
  2025. if (!new_dev_maps)
  2026. goto out_no_new_maps;
  2027. if (!dev_maps) {
  2028. /* Increment static keys at most once per type */
  2029. static_key_slow_inc_cpuslocked(&xps_needed);
  2030. if (is_rxqs_map)
  2031. static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
  2032. }
  2033. for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
  2034. j < nr_ids;) {
  2035. /* copy maps belonging to foreign traffic classes */
  2036. for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
  2037. /* fill in the new device map from the old device map */
  2038. map = xmap_dereference(dev_maps->attr_map[tci]);
  2039. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  2040. }
  2041. /* We need to explicitly update tci as prevous loop
  2042. * could break out early if dev_maps is NULL.
  2043. */
  2044. tci = j * num_tc + tc;
  2045. if (netif_attr_test_mask(j, mask, nr_ids) &&
  2046. netif_attr_test_online(j, online_mask, nr_ids)) {
  2047. /* add tx-queue to CPU/rx-queue maps */
  2048. int pos = 0;
  2049. map = xmap_dereference(new_dev_maps->attr_map[tci]);
  2050. while ((pos < map->len) && (map->queues[pos] != index))
  2051. pos++;
  2052. if (pos == map->len)
  2053. map->queues[map->len++] = index;
  2054. #ifdef CONFIG_NUMA
  2055. if (!is_rxqs_map) {
  2056. if (numa_node_id == -2)
  2057. numa_node_id = cpu_to_node(j);
  2058. else if (numa_node_id != cpu_to_node(j))
  2059. numa_node_id = -1;
  2060. }
  2061. #endif
  2062. } else if (dev_maps) {
  2063. /* fill in the new device map from the old device map */
  2064. map = xmap_dereference(dev_maps->attr_map[tci]);
  2065. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  2066. }
  2067. /* copy maps belonging to foreign traffic classes */
  2068. for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
  2069. /* fill in the new device map from the old device map */
  2070. map = xmap_dereference(dev_maps->attr_map[tci]);
  2071. RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
  2072. }
  2073. }
  2074. if (is_rxqs_map)
  2075. rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
  2076. else
  2077. rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
  2078. /* Cleanup old maps */
  2079. if (!dev_maps)
  2080. goto out_no_old_maps;
  2081. for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
  2082. j < nr_ids;) {
  2083. for (i = num_tc, tci = j * num_tc; i--; tci++) {
  2084. new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
  2085. map = xmap_dereference(dev_maps->attr_map[tci]);
  2086. if (map && map != new_map)
  2087. kfree_rcu(map, rcu);
  2088. }
  2089. }
  2090. kfree_rcu(dev_maps, rcu);
  2091. out_no_old_maps:
  2092. dev_maps = new_dev_maps;
  2093. active = true;
  2094. out_no_new_maps:
  2095. if (!is_rxqs_map) {
  2096. /* update Tx queue numa node */
  2097. netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
  2098. (numa_node_id >= 0) ?
  2099. numa_node_id : NUMA_NO_NODE);
  2100. }
  2101. if (!dev_maps)
  2102. goto out_no_maps;
  2103. /* removes tx-queue from unused CPUs/rx-queues */
  2104. for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
  2105. j < nr_ids;) {
  2106. for (i = tc, tci = j * num_tc; i--; tci++)
  2107. active |= remove_xps_queue(dev_maps, tci, index);
  2108. if (!netif_attr_test_mask(j, mask, nr_ids) ||
  2109. !netif_attr_test_online(j, online_mask, nr_ids))
  2110. active |= remove_xps_queue(dev_maps, tci, index);
  2111. for (i = num_tc - tc, tci++; --i; tci++)
  2112. active |= remove_xps_queue(dev_maps, tci, index);
  2113. }
  2114. /* free map if not active */
  2115. if (!active)
  2116. reset_xps_maps(dev, dev_maps, is_rxqs_map);
  2117. out_no_maps:
  2118. mutex_unlock(&xps_map_mutex);
  2119. return 0;
  2120. error:
  2121. /* remove any maps that we added */
  2122. for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
  2123. j < nr_ids;) {
  2124. for (i = num_tc, tci = j * num_tc; i--; tci++) {
  2125. new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
  2126. map = dev_maps ?
  2127. xmap_dereference(dev_maps->attr_map[tci]) :
  2128. NULL;
  2129. if (new_map && new_map != map)
  2130. kfree(new_map);
  2131. }
  2132. }
  2133. mutex_unlock(&xps_map_mutex);
  2134. kfree(new_dev_maps);
  2135. return -ENOMEM;
  2136. }
  2137. EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
  2138. int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
  2139. u16 index)
  2140. {
  2141. int ret;
  2142. cpus_read_lock();
  2143. ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
  2144. cpus_read_unlock();
  2145. return ret;
  2146. }
  2147. EXPORT_SYMBOL(netif_set_xps_queue);
  2148. #endif
  2149. static void netdev_unbind_all_sb_channels(struct net_device *dev)
  2150. {
  2151. struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
  2152. /* Unbind any subordinate channels */
  2153. while (txq-- != &dev->_tx[0]) {
  2154. if (txq->sb_dev)
  2155. netdev_unbind_sb_channel(dev, txq->sb_dev);
  2156. }
  2157. }
  2158. void netdev_reset_tc(struct net_device *dev)
  2159. {
  2160. #ifdef CONFIG_XPS
  2161. netif_reset_xps_queues_gt(dev, 0);
  2162. #endif
  2163. netdev_unbind_all_sb_channels(dev);
  2164. /* Reset TC configuration of device */
  2165. dev->num_tc = 0;
  2166. memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
  2167. memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
  2168. }
  2169. EXPORT_SYMBOL(netdev_reset_tc);
  2170. int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
  2171. {
  2172. if (tc >= dev->num_tc)
  2173. return -EINVAL;
  2174. #ifdef CONFIG_XPS
  2175. netif_reset_xps_queues(dev, offset, count);
  2176. #endif
  2177. dev->tc_to_txq[tc].count = count;
  2178. dev->tc_to_txq[tc].offset = offset;
  2179. return 0;
  2180. }
  2181. EXPORT_SYMBOL(netdev_set_tc_queue);
  2182. int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
  2183. {
  2184. if (num_tc > TC_MAX_QUEUE)
  2185. return -EINVAL;
  2186. #ifdef CONFIG_XPS
  2187. netif_reset_xps_queues_gt(dev, 0);
  2188. #endif
  2189. netdev_unbind_all_sb_channels(dev);
  2190. dev->num_tc = num_tc;
  2191. return 0;
  2192. }
  2193. EXPORT_SYMBOL(netdev_set_num_tc);
  2194. void netdev_unbind_sb_channel(struct net_device *dev,
  2195. struct net_device *sb_dev)
  2196. {
  2197. struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
  2198. #ifdef CONFIG_XPS
  2199. netif_reset_xps_queues_gt(sb_dev, 0);
  2200. #endif
  2201. memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
  2202. memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
  2203. while (txq-- != &dev->_tx[0]) {
  2204. if (txq->sb_dev == sb_dev)
  2205. txq->sb_dev = NULL;
  2206. }
  2207. }
  2208. EXPORT_SYMBOL(netdev_unbind_sb_channel);
  2209. int netdev_bind_sb_channel_queue(struct net_device *dev,
  2210. struct net_device *sb_dev,
  2211. u8 tc, u16 count, u16 offset)
  2212. {
  2213. /* Make certain the sb_dev and dev are already configured */
  2214. if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
  2215. return -EINVAL;
  2216. /* We cannot hand out queues we don't have */
  2217. if ((offset + count) > dev->real_num_tx_queues)
  2218. return -EINVAL;
  2219. /* Record the mapping */
  2220. sb_dev->tc_to_txq[tc].count = count;
  2221. sb_dev->tc_to_txq[tc].offset = offset;
  2222. /* Provide a way for Tx queue to find the tc_to_txq map or
  2223. * XPS map for itself.
  2224. */
  2225. while (count--)
  2226. netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
  2227. return 0;
  2228. }
  2229. EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
  2230. int netdev_set_sb_channel(struct net_device *dev, u16 channel)
  2231. {
  2232. /* Do not use a multiqueue device to represent a subordinate channel */
  2233. if (netif_is_multiqueue(dev))
  2234. return -ENODEV;
  2235. /* We allow channels 1 - 32767 to be used for subordinate channels.
  2236. * Channel 0 is meant to be "native" mode and used only to represent
  2237. * the main root device. We allow writing 0 to reset the device back
  2238. * to normal mode after being used as a subordinate channel.
  2239. */
  2240. if (channel > S16_MAX)
  2241. return -EINVAL;
  2242. dev->num_tc = -channel;
  2243. return 0;
  2244. }
  2245. EXPORT_SYMBOL(netdev_set_sb_channel);
  2246. /*
  2247. * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
  2248. * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
  2249. */
  2250. int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
  2251. {
  2252. bool disabling;
  2253. int rc;
  2254. disabling = txq < dev->real_num_tx_queues;
  2255. if (txq < 1 || txq > dev->num_tx_queues)
  2256. return -EINVAL;
  2257. if (dev->reg_state == NETREG_REGISTERED ||
  2258. dev->reg_state == NETREG_UNREGISTERING) {
  2259. ASSERT_RTNL();
  2260. rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
  2261. txq);
  2262. if (rc)
  2263. return rc;
  2264. if (dev->num_tc)
  2265. netif_setup_tc(dev, txq);
  2266. dev->real_num_tx_queues = txq;
  2267. if (disabling) {
  2268. synchronize_net();
  2269. qdisc_reset_all_tx_gt(dev, txq);
  2270. #ifdef CONFIG_XPS
  2271. netif_reset_xps_queues_gt(dev, txq);
  2272. #endif
  2273. }
  2274. } else {
  2275. dev->real_num_tx_queues = txq;
  2276. }
  2277. return 0;
  2278. }
  2279. EXPORT_SYMBOL(netif_set_real_num_tx_queues);
  2280. #ifdef CONFIG_SYSFS
  2281. /**
  2282. * netif_set_real_num_rx_queues - set actual number of RX queues used
  2283. * @dev: Network device
  2284. * @rxq: Actual number of RX queues
  2285. *
  2286. * This must be called either with the rtnl_lock held or before
  2287. * registration of the net device. Returns 0 on success, or a
  2288. * negative error code. If called before registration, it always
  2289. * succeeds.
  2290. */
  2291. int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
  2292. {
  2293. int rc;
  2294. if (rxq < 1 || rxq > dev->num_rx_queues)
  2295. return -EINVAL;
  2296. if (dev->reg_state == NETREG_REGISTERED) {
  2297. ASSERT_RTNL();
  2298. rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
  2299. rxq);
  2300. if (rc)
  2301. return rc;
  2302. }
  2303. dev->real_num_rx_queues = rxq;
  2304. return 0;
  2305. }
  2306. EXPORT_SYMBOL(netif_set_real_num_rx_queues);
  2307. #endif
  2308. /**
  2309. * netif_get_num_default_rss_queues - default number of RSS queues
  2310. *
  2311. * This routine should set an upper limit on the number of RSS queues
  2312. * used by default by multiqueue devices.
  2313. */
  2314. int netif_get_num_default_rss_queues(void)
  2315. {
  2316. return is_kdump_kernel() ?
  2317. 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
  2318. }
  2319. EXPORT_SYMBOL(netif_get_num_default_rss_queues);
  2320. static void __netif_reschedule(struct Qdisc *q)
  2321. {
  2322. struct softnet_data *sd;
  2323. unsigned long flags;
  2324. local_irq_save(flags);
  2325. sd = this_cpu_ptr(&softnet_data);
  2326. q->next_sched = NULL;
  2327. *sd->output_queue_tailp = q;
  2328. sd->output_queue_tailp = &q->next_sched;
  2329. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  2330. local_irq_restore(flags);
  2331. }
  2332. void __netif_schedule(struct Qdisc *q)
  2333. {
  2334. if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
  2335. __netif_reschedule(q);
  2336. }
  2337. EXPORT_SYMBOL(__netif_schedule);
  2338. struct dev_kfree_skb_cb {
  2339. enum skb_free_reason reason;
  2340. };
  2341. static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
  2342. {
  2343. return (struct dev_kfree_skb_cb *)skb->cb;
  2344. }
  2345. void netif_schedule_queue(struct netdev_queue *txq)
  2346. {
  2347. rcu_read_lock();
  2348. if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
  2349. struct Qdisc *q = rcu_dereference(txq->qdisc);
  2350. __netif_schedule(q);
  2351. }
  2352. rcu_read_unlock();
  2353. }
  2354. EXPORT_SYMBOL(netif_schedule_queue);
  2355. void netif_tx_wake_queue(struct netdev_queue *dev_queue)
  2356. {
  2357. if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
  2358. struct Qdisc *q;
  2359. rcu_read_lock();
  2360. q = rcu_dereference(dev_queue->qdisc);
  2361. __netif_schedule(q);
  2362. rcu_read_unlock();
  2363. }
  2364. }
  2365. EXPORT_SYMBOL(netif_tx_wake_queue);
  2366. void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
  2367. {
  2368. unsigned long flags;
  2369. if (unlikely(!skb))
  2370. return;
  2371. if (likely(refcount_read(&skb->users) == 1)) {
  2372. smp_rmb();
  2373. refcount_set(&skb->users, 0);
  2374. } else if (likely(!refcount_dec_and_test(&skb->users))) {
  2375. return;
  2376. }
  2377. get_kfree_skb_cb(skb)->reason = reason;
  2378. local_irq_save(flags);
  2379. skb->next = __this_cpu_read(softnet_data.completion_queue);
  2380. __this_cpu_write(softnet_data.completion_queue, skb);
  2381. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  2382. local_irq_restore(flags);
  2383. }
  2384. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  2385. void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
  2386. {
  2387. if (in_irq() || irqs_disabled())
  2388. __dev_kfree_skb_irq(skb, reason);
  2389. else
  2390. dev_kfree_skb(skb);
  2391. }
  2392. EXPORT_SYMBOL(__dev_kfree_skb_any);
  2393. /**
  2394. * netif_device_detach - mark device as removed
  2395. * @dev: network device
  2396. *
  2397. * Mark device as removed from system and therefore no longer available.
  2398. */
  2399. void netif_device_detach(struct net_device *dev)
  2400. {
  2401. if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
  2402. netif_running(dev)) {
  2403. netif_tx_stop_all_queues(dev);
  2404. }
  2405. }
  2406. EXPORT_SYMBOL(netif_device_detach);
  2407. /**
  2408. * netif_device_attach - mark device as attached
  2409. * @dev: network device
  2410. *
  2411. * Mark device as attached from system and restart if needed.
  2412. */
  2413. void netif_device_attach(struct net_device *dev)
  2414. {
  2415. if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
  2416. netif_running(dev)) {
  2417. netif_tx_wake_all_queues(dev);
  2418. __netdev_watchdog_up(dev);
  2419. }
  2420. }
  2421. EXPORT_SYMBOL(netif_device_attach);
  2422. /*
  2423. * Returns a Tx hash based on the given packet descriptor a Tx queues' number
  2424. * to be used as a distribution range.
  2425. */
  2426. static u16 skb_tx_hash(const struct net_device *dev,
  2427. const struct net_device *sb_dev,
  2428. struct sk_buff *skb)
  2429. {
  2430. u32 hash;
  2431. u16 qoffset = 0;
  2432. u16 qcount = dev->real_num_tx_queues;
  2433. if (dev->num_tc) {
  2434. u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
  2435. qoffset = sb_dev->tc_to_txq[tc].offset;
  2436. qcount = sb_dev->tc_to_txq[tc].count;
  2437. }
  2438. if (skb_rx_queue_recorded(skb)) {
  2439. hash = skb_get_rx_queue(skb);
  2440. if (hash >= qoffset)
  2441. hash -= qoffset;
  2442. while (unlikely(hash >= qcount))
  2443. hash -= qcount;
  2444. return hash + qoffset;
  2445. }
  2446. return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
  2447. }
  2448. static void skb_warn_bad_offload(const struct sk_buff *skb)
  2449. {
  2450. static const netdev_features_t null_features;
  2451. struct net_device *dev = skb->dev;
  2452. const char *name = "";
  2453. if (!net_ratelimit())
  2454. return;
  2455. if (dev) {
  2456. if (dev->dev.parent)
  2457. name = dev_driver_string(dev->dev.parent);
  2458. else
  2459. name = netdev_name(dev);
  2460. }
  2461. WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
  2462. "gso_type=%d ip_summed=%d\n",
  2463. name, dev ? &dev->features : &null_features,
  2464. skb->sk ? &skb->sk->sk_route_caps : &null_features,
  2465. skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
  2466. skb_shinfo(skb)->gso_type, skb->ip_summed);
  2467. }
  2468. /*
  2469. * Invalidate hardware checksum when packet is to be mangled, and
  2470. * complete checksum manually on outgoing path.
  2471. */
  2472. int skb_checksum_help(struct sk_buff *skb)
  2473. {
  2474. __wsum csum;
  2475. int ret = 0, offset;
  2476. if (skb->ip_summed == CHECKSUM_COMPLETE)
  2477. goto out_set_summed;
  2478. if (unlikely(skb_shinfo(skb)->gso_size)) {
  2479. skb_warn_bad_offload(skb);
  2480. return -EINVAL;
  2481. }
  2482. /* Before computing a checksum, we should make sure no frag could
  2483. * be modified by an external entity : checksum could be wrong.
  2484. */
  2485. if (skb_has_shared_frag(skb)) {
  2486. ret = __skb_linearize(skb);
  2487. if (ret)
  2488. goto out;
  2489. }
  2490. offset = skb_checksum_start_offset(skb);
  2491. BUG_ON(offset >= skb_headlen(skb));
  2492. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  2493. offset += skb->csum_offset;
  2494. BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
  2495. if (skb_cloned(skb) &&
  2496. !skb_clone_writable(skb, offset + sizeof(__sum16))) {
  2497. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2498. if (ret)
  2499. goto out;
  2500. }
  2501. *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
  2502. out_set_summed:
  2503. skb->ip_summed = CHECKSUM_NONE;
  2504. out:
  2505. return ret;
  2506. }
  2507. EXPORT_SYMBOL(skb_checksum_help);
  2508. int skb_crc32c_csum_help(struct sk_buff *skb)
  2509. {
  2510. __le32 crc32c_csum;
  2511. int ret = 0, offset, start;
  2512. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2513. goto out;
  2514. if (unlikely(skb_is_gso(skb)))
  2515. goto out;
  2516. /* Before computing a checksum, we should make sure no frag could
  2517. * be modified by an external entity : checksum could be wrong.
  2518. */
  2519. if (unlikely(skb_has_shared_frag(skb))) {
  2520. ret = __skb_linearize(skb);
  2521. if (ret)
  2522. goto out;
  2523. }
  2524. start = skb_checksum_start_offset(skb);
  2525. offset = start + offsetof(struct sctphdr, checksum);
  2526. if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
  2527. ret = -EINVAL;
  2528. goto out;
  2529. }
  2530. if (skb_cloned(skb) &&
  2531. !skb_clone_writable(skb, offset + sizeof(__le32))) {
  2532. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2533. if (ret)
  2534. goto out;
  2535. }
  2536. crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
  2537. skb->len - start, ~(__u32)0,
  2538. crc32c_csum_stub));
  2539. *(__le32 *)(skb->data + offset) = crc32c_csum;
  2540. skb->ip_summed = CHECKSUM_NONE;
  2541. skb->csum_not_inet = 0;
  2542. out:
  2543. return ret;
  2544. }
  2545. __be16 skb_network_protocol(struct sk_buff *skb, int *depth)
  2546. {
  2547. __be16 type = skb->protocol;
  2548. /* Tunnel gso handlers can set protocol to ethernet. */
  2549. if (type == htons(ETH_P_TEB)) {
  2550. struct ethhdr *eth;
  2551. if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
  2552. return 0;
  2553. eth = (struct ethhdr *)skb->data;
  2554. type = eth->h_proto;
  2555. }
  2556. return __vlan_get_protocol(skb, type, depth);
  2557. }
  2558. /**
  2559. * skb_mac_gso_segment - mac layer segmentation handler.
  2560. * @skb: buffer to segment
  2561. * @features: features for the output path (see dev->features)
  2562. */
  2563. struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
  2564. netdev_features_t features)
  2565. {
  2566. struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
  2567. struct packet_offload *ptype;
  2568. int vlan_depth = skb->mac_len;
  2569. __be16 type = skb_network_protocol(skb, &vlan_depth);
  2570. if (unlikely(!type))
  2571. return ERR_PTR(-EINVAL);
  2572. __skb_pull(skb, vlan_depth);
  2573. rcu_read_lock();
  2574. list_for_each_entry_rcu(ptype, &offload_base, list) {
  2575. if (ptype->type == type && ptype->callbacks.gso_segment) {
  2576. segs = ptype->callbacks.gso_segment(skb, features);
  2577. break;
  2578. }
  2579. }
  2580. rcu_read_unlock();
  2581. __skb_push(skb, skb->data - skb_mac_header(skb));
  2582. return segs;
  2583. }
  2584. EXPORT_SYMBOL(skb_mac_gso_segment);
  2585. /* openvswitch calls this on rx path, so we need a different check.
  2586. */
  2587. static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
  2588. {
  2589. if (tx_path)
  2590. return skb->ip_summed != CHECKSUM_PARTIAL &&
  2591. skb->ip_summed != CHECKSUM_UNNECESSARY;
  2592. return skb->ip_summed == CHECKSUM_NONE;
  2593. }
  2594. /**
  2595. * __skb_gso_segment - Perform segmentation on skb.
  2596. * @skb: buffer to segment
  2597. * @features: features for the output path (see dev->features)
  2598. * @tx_path: whether it is called in TX path
  2599. *
  2600. * This function segments the given skb and returns a list of segments.
  2601. *
  2602. * It may return NULL if the skb requires no segmentation. This is
  2603. * only possible when GSO is used for verifying header integrity.
  2604. *
  2605. * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
  2606. */
  2607. struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
  2608. netdev_features_t features, bool tx_path)
  2609. {
  2610. struct sk_buff *segs;
  2611. if (unlikely(skb_needs_check(skb, tx_path))) {
  2612. int err;
  2613. /* We're going to init ->check field in TCP or UDP header */
  2614. err = skb_cow_head(skb, 0);
  2615. if (err < 0)
  2616. return ERR_PTR(err);
  2617. }
  2618. /* Only report GSO partial support if it will enable us to
  2619. * support segmentation on this frame without needing additional
  2620. * work.
  2621. */
  2622. if (features & NETIF_F_GSO_PARTIAL) {
  2623. netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
  2624. struct net_device *dev = skb->dev;
  2625. partial_features |= dev->features & dev->gso_partial_features;
  2626. if (!skb_gso_ok(skb, features | partial_features))
  2627. features &= ~NETIF_F_GSO_PARTIAL;
  2628. }
  2629. BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
  2630. sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
  2631. SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
  2632. SKB_GSO_CB(skb)->encap_level = 0;
  2633. skb_reset_mac_header(skb);
  2634. skb_reset_mac_len(skb);
  2635. segs = skb_mac_gso_segment(skb, features);
  2636. if (unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
  2637. skb_warn_bad_offload(skb);
  2638. return segs;
  2639. }
  2640. EXPORT_SYMBOL(__skb_gso_segment);
  2641. /* Take action when hardware reception checksum errors are detected. */
  2642. #ifdef CONFIG_BUG
  2643. void netdev_rx_csum_fault(struct net_device *dev)
  2644. {
  2645. if (net_ratelimit()) {
  2646. pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
  2647. dump_stack();
  2648. }
  2649. }
  2650. EXPORT_SYMBOL(netdev_rx_csum_fault);
  2651. #endif
  2652. /* XXX: check that highmem exists at all on the given machine. */
  2653. static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
  2654. {
  2655. #ifdef CONFIG_HIGHMEM
  2656. int i;
  2657. if (!(dev->features & NETIF_F_HIGHDMA)) {
  2658. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2659. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2660. if (PageHighMem(skb_frag_page(frag)))
  2661. return 1;
  2662. }
  2663. }
  2664. #endif
  2665. return 0;
  2666. }
  2667. /* If MPLS offload request, verify we are testing hardware MPLS features
  2668. * instead of standard features for the netdev.
  2669. */
  2670. #if IS_ENABLED(CONFIG_NET_MPLS_GSO)
  2671. static netdev_features_t net_mpls_features(struct sk_buff *skb,
  2672. netdev_features_t features,
  2673. __be16 type)
  2674. {
  2675. if (eth_p_mpls(type))
  2676. features &= skb->dev->mpls_features;
  2677. return features;
  2678. }
  2679. #else
  2680. static netdev_features_t net_mpls_features(struct sk_buff *skb,
  2681. netdev_features_t features,
  2682. __be16 type)
  2683. {
  2684. return features;
  2685. }
  2686. #endif
  2687. static netdev_features_t harmonize_features(struct sk_buff *skb,
  2688. netdev_features_t features)
  2689. {
  2690. int tmp;
  2691. __be16 type;
  2692. type = skb_network_protocol(skb, &tmp);
  2693. features = net_mpls_features(skb, features, type);
  2694. if (skb->ip_summed != CHECKSUM_NONE &&
  2695. !can_checksum_protocol(features, type)) {
  2696. features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  2697. }
  2698. if (illegal_highdma(skb->dev, skb))
  2699. features &= ~NETIF_F_SG;
  2700. return features;
  2701. }
  2702. netdev_features_t passthru_features_check(struct sk_buff *skb,
  2703. struct net_device *dev,
  2704. netdev_features_t features)
  2705. {
  2706. return features;
  2707. }
  2708. EXPORT_SYMBOL(passthru_features_check);
  2709. static netdev_features_t dflt_features_check(struct sk_buff *skb,
  2710. struct net_device *dev,
  2711. netdev_features_t features)
  2712. {
  2713. return vlan_features_check(skb, features);
  2714. }
  2715. static netdev_features_t gso_features_check(const struct sk_buff *skb,
  2716. struct net_device *dev,
  2717. netdev_features_t features)
  2718. {
  2719. u16 gso_segs = skb_shinfo(skb)->gso_segs;
  2720. if (gso_segs > dev->gso_max_segs)
  2721. return features & ~NETIF_F_GSO_MASK;
  2722. /* Support for GSO partial features requires software
  2723. * intervention before we can actually process the packets
  2724. * so we need to strip support for any partial features now
  2725. * and we can pull them back in after we have partially
  2726. * segmented the frame.
  2727. */
  2728. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
  2729. features &= ~dev->gso_partial_features;
  2730. /* Make sure to clear the IPv4 ID mangling feature if the
  2731. * IPv4 header has the potential to be fragmented.
  2732. */
  2733. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  2734. struct iphdr *iph = skb->encapsulation ?
  2735. inner_ip_hdr(skb) : ip_hdr(skb);
  2736. if (!(iph->frag_off & htons(IP_DF)))
  2737. features &= ~NETIF_F_TSO_MANGLEID;
  2738. }
  2739. return features;
  2740. }
  2741. netdev_features_t netif_skb_features(struct sk_buff *skb)
  2742. {
  2743. struct net_device *dev = skb->dev;
  2744. netdev_features_t features = dev->features;
  2745. if (skb_is_gso(skb))
  2746. features = gso_features_check(skb, dev, features);
  2747. /* If encapsulation offload request, verify we are testing
  2748. * hardware encapsulation features instead of standard
  2749. * features for the netdev
  2750. */
  2751. if (skb->encapsulation)
  2752. features &= dev->hw_enc_features;
  2753. if (skb_vlan_tagged(skb))
  2754. features = netdev_intersect_features(features,
  2755. dev->vlan_features |
  2756. NETIF_F_HW_VLAN_CTAG_TX |
  2757. NETIF_F_HW_VLAN_STAG_TX);
  2758. if (dev->netdev_ops->ndo_features_check)
  2759. features &= dev->netdev_ops->ndo_features_check(skb, dev,
  2760. features);
  2761. else
  2762. features &= dflt_features_check(skb, dev, features);
  2763. return harmonize_features(skb, features);
  2764. }
  2765. EXPORT_SYMBOL(netif_skb_features);
  2766. static int xmit_one(struct sk_buff *skb, struct net_device *dev,
  2767. struct netdev_queue *txq, bool more)
  2768. {
  2769. unsigned int len;
  2770. int rc;
  2771. if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
  2772. dev_queue_xmit_nit(skb, dev);
  2773. len = skb->len;
  2774. trace_net_dev_start_xmit(skb, dev);
  2775. rc = netdev_start_xmit(skb, dev, txq, more);
  2776. trace_net_dev_xmit(skb, rc, dev, len);
  2777. return rc;
  2778. }
  2779. struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
  2780. struct netdev_queue *txq, int *ret)
  2781. {
  2782. struct sk_buff *skb = first;
  2783. int rc = NETDEV_TX_OK;
  2784. while (skb) {
  2785. struct sk_buff *next = skb->next;
  2786. skb->next = NULL;
  2787. rc = xmit_one(skb, dev, txq, next != NULL);
  2788. if (unlikely(!dev_xmit_complete(rc))) {
  2789. skb->next = next;
  2790. goto out;
  2791. }
  2792. skb = next;
  2793. if (netif_tx_queue_stopped(txq) && skb) {
  2794. rc = NETDEV_TX_BUSY;
  2795. break;
  2796. }
  2797. }
  2798. out:
  2799. *ret = rc;
  2800. return skb;
  2801. }
  2802. static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
  2803. netdev_features_t features)
  2804. {
  2805. if (skb_vlan_tag_present(skb) &&
  2806. !vlan_hw_offload_capable(features, skb->vlan_proto))
  2807. skb = __vlan_hwaccel_push_inside(skb);
  2808. return skb;
  2809. }
  2810. int skb_csum_hwoffload_help(struct sk_buff *skb,
  2811. const netdev_features_t features)
  2812. {
  2813. if (unlikely(skb->csum_not_inet))
  2814. return !!(features & NETIF_F_SCTP_CRC) ? 0 :
  2815. skb_crc32c_csum_help(skb);
  2816. return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
  2817. }
  2818. EXPORT_SYMBOL(skb_csum_hwoffload_help);
  2819. static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
  2820. {
  2821. netdev_features_t features;
  2822. features = netif_skb_features(skb);
  2823. skb = validate_xmit_vlan(skb, features);
  2824. if (unlikely(!skb))
  2825. goto out_null;
  2826. skb = sk_validate_xmit_skb(skb, dev);
  2827. if (unlikely(!skb))
  2828. goto out_null;
  2829. if (netif_needs_gso(skb, features)) {
  2830. struct sk_buff *segs;
  2831. segs = skb_gso_segment(skb, features);
  2832. if (IS_ERR(segs)) {
  2833. goto out_kfree_skb;
  2834. } else if (segs) {
  2835. consume_skb(skb);
  2836. skb = segs;
  2837. }
  2838. } else {
  2839. if (skb_needs_linearize(skb, features) &&
  2840. __skb_linearize(skb))
  2841. goto out_kfree_skb;
  2842. /* If packet is not checksummed and device does not
  2843. * support checksumming for this protocol, complete
  2844. * checksumming here.
  2845. */
  2846. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2847. if (skb->encapsulation)
  2848. skb_set_inner_transport_header(skb,
  2849. skb_checksum_start_offset(skb));
  2850. else
  2851. skb_set_transport_header(skb,
  2852. skb_checksum_start_offset(skb));
  2853. if (skb_csum_hwoffload_help(skb, features))
  2854. goto out_kfree_skb;
  2855. }
  2856. }
  2857. skb = validate_xmit_xfrm(skb, features, again);
  2858. return skb;
  2859. out_kfree_skb:
  2860. kfree_skb(skb);
  2861. out_null:
  2862. atomic_long_inc(&dev->tx_dropped);
  2863. return NULL;
  2864. }
  2865. struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
  2866. {
  2867. struct sk_buff *next, *head = NULL, *tail;
  2868. for (; skb != NULL; skb = next) {
  2869. next = skb->next;
  2870. skb->next = NULL;
  2871. /* in case skb wont be segmented, point to itself */
  2872. skb->prev = skb;
  2873. skb = validate_xmit_skb(skb, dev, again);
  2874. if (!skb)
  2875. continue;
  2876. if (!head)
  2877. head = skb;
  2878. else
  2879. tail->next = skb;
  2880. /* If skb was segmented, skb->prev points to
  2881. * the last segment. If not, it still contains skb.
  2882. */
  2883. tail = skb->prev;
  2884. }
  2885. return head;
  2886. }
  2887. EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
  2888. static void qdisc_pkt_len_init(struct sk_buff *skb)
  2889. {
  2890. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  2891. qdisc_skb_cb(skb)->pkt_len = skb->len;
  2892. /* To get more precise estimation of bytes sent on wire,
  2893. * we add to pkt_len the headers size of all segments
  2894. */
  2895. if (shinfo->gso_size) {
  2896. unsigned int hdr_len;
  2897. u16 gso_segs = shinfo->gso_segs;
  2898. /* mac layer + network layer */
  2899. hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
  2900. /* + transport layer */
  2901. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
  2902. const struct tcphdr *th;
  2903. struct tcphdr _tcphdr;
  2904. th = skb_header_pointer(skb, skb_transport_offset(skb),
  2905. sizeof(_tcphdr), &_tcphdr);
  2906. if (likely(th))
  2907. hdr_len += __tcp_hdrlen(th);
  2908. } else {
  2909. struct udphdr _udphdr;
  2910. if (skb_header_pointer(skb, skb_transport_offset(skb),
  2911. sizeof(_udphdr), &_udphdr))
  2912. hdr_len += sizeof(struct udphdr);
  2913. }
  2914. if (shinfo->gso_type & SKB_GSO_DODGY)
  2915. gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
  2916. shinfo->gso_size);
  2917. qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
  2918. }
  2919. }
  2920. static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  2921. struct net_device *dev,
  2922. struct netdev_queue *txq)
  2923. {
  2924. spinlock_t *root_lock = qdisc_lock(q);
  2925. struct sk_buff *to_free = NULL;
  2926. bool contended;
  2927. int rc;
  2928. qdisc_calculate_pkt_len(skb, q);
  2929. if (q->flags & TCQ_F_NOLOCK) {
  2930. if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
  2931. __qdisc_drop(skb, &to_free);
  2932. rc = NET_XMIT_DROP;
  2933. } else {
  2934. rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
  2935. qdisc_run(q);
  2936. }
  2937. if (unlikely(to_free))
  2938. kfree_skb_list(to_free);
  2939. return rc;
  2940. }
  2941. /*
  2942. * Heuristic to force contended enqueues to serialize on a
  2943. * separate lock before trying to get qdisc main lock.
  2944. * This permits qdisc->running owner to get the lock more
  2945. * often and dequeue packets faster.
  2946. */
  2947. contended = qdisc_is_running(q);
  2948. if (unlikely(contended))
  2949. spin_lock(&q->busylock);
  2950. spin_lock(root_lock);
  2951. if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
  2952. __qdisc_drop(skb, &to_free);
  2953. rc = NET_XMIT_DROP;
  2954. } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
  2955. qdisc_run_begin(q)) {
  2956. /*
  2957. * This is a work-conserving queue; there are no old skbs
  2958. * waiting to be sent out; and the qdisc is not running -
  2959. * xmit the skb directly.
  2960. */
  2961. qdisc_bstats_update(q, skb);
  2962. if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
  2963. if (unlikely(contended)) {
  2964. spin_unlock(&q->busylock);
  2965. contended = false;
  2966. }
  2967. __qdisc_run(q);
  2968. }
  2969. qdisc_run_end(q);
  2970. rc = NET_XMIT_SUCCESS;
  2971. } else {
  2972. rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
  2973. if (qdisc_run_begin(q)) {
  2974. if (unlikely(contended)) {
  2975. spin_unlock(&q->busylock);
  2976. contended = false;
  2977. }
  2978. __qdisc_run(q);
  2979. qdisc_run_end(q);
  2980. }
  2981. }
  2982. spin_unlock(root_lock);
  2983. if (unlikely(to_free))
  2984. kfree_skb_list(to_free);
  2985. if (unlikely(contended))
  2986. spin_unlock(&q->busylock);
  2987. return rc;
  2988. }
  2989. #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
  2990. static void skb_update_prio(struct sk_buff *skb)
  2991. {
  2992. const struct netprio_map *map;
  2993. const struct sock *sk;
  2994. unsigned int prioidx;
  2995. if (skb->priority)
  2996. return;
  2997. map = rcu_dereference_bh(skb->dev->priomap);
  2998. if (!map)
  2999. return;
  3000. sk = skb_to_full_sk(skb);
  3001. if (!sk)
  3002. return;
  3003. prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
  3004. if (prioidx < map->priomap_len)
  3005. skb->priority = map->priomap[prioidx];
  3006. }
  3007. #else
  3008. #define skb_update_prio(skb)
  3009. #endif
  3010. DEFINE_PER_CPU(int, xmit_recursion);
  3011. EXPORT_SYMBOL(xmit_recursion);
  3012. /**
  3013. * dev_loopback_xmit - loop back @skb
  3014. * @net: network namespace this loopback is happening in
  3015. * @sk: sk needed to be a netfilter okfn
  3016. * @skb: buffer to transmit
  3017. */
  3018. int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
  3019. {
  3020. skb_reset_mac_header(skb);
  3021. __skb_pull(skb, skb_network_offset(skb));
  3022. skb->pkt_type = PACKET_LOOPBACK;
  3023. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3024. WARN_ON(!skb_dst(skb));
  3025. skb_dst_force(skb);
  3026. netif_rx_ni(skb);
  3027. return 0;
  3028. }
  3029. EXPORT_SYMBOL(dev_loopback_xmit);
  3030. #ifdef CONFIG_NET_EGRESS
  3031. static struct sk_buff *
  3032. sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
  3033. {
  3034. struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
  3035. struct tcf_result cl_res;
  3036. if (!miniq)
  3037. return skb;
  3038. /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
  3039. mini_qdisc_bstats_cpu_update(miniq, skb);
  3040. switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
  3041. case TC_ACT_OK:
  3042. case TC_ACT_RECLASSIFY:
  3043. skb->tc_index = TC_H_MIN(cl_res.classid);
  3044. break;
  3045. case TC_ACT_SHOT:
  3046. mini_qdisc_qstats_cpu_drop(miniq);
  3047. *ret = NET_XMIT_DROP;
  3048. kfree_skb(skb);
  3049. return NULL;
  3050. case TC_ACT_STOLEN:
  3051. case TC_ACT_QUEUED:
  3052. case TC_ACT_TRAP:
  3053. *ret = NET_XMIT_SUCCESS;
  3054. consume_skb(skb);
  3055. return NULL;
  3056. case TC_ACT_REDIRECT:
  3057. /* No need to push/pop skb's mac_header here on egress! */
  3058. skb_do_redirect(skb);
  3059. *ret = NET_XMIT_SUCCESS;
  3060. return NULL;
  3061. default:
  3062. break;
  3063. }
  3064. return skb;
  3065. }
  3066. #endif /* CONFIG_NET_EGRESS */
  3067. #ifdef CONFIG_XPS
  3068. static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
  3069. struct xps_dev_maps *dev_maps, unsigned int tci)
  3070. {
  3071. struct xps_map *map;
  3072. int queue_index = -1;
  3073. if (dev->num_tc) {
  3074. tci *= dev->num_tc;
  3075. tci += netdev_get_prio_tc_map(dev, skb->priority);
  3076. }
  3077. map = rcu_dereference(dev_maps->attr_map[tci]);
  3078. if (map) {
  3079. if (map->len == 1)
  3080. queue_index = map->queues[0];
  3081. else
  3082. queue_index = map->queues[reciprocal_scale(
  3083. skb_get_hash(skb), map->len)];
  3084. if (unlikely(queue_index >= dev->real_num_tx_queues))
  3085. queue_index = -1;
  3086. }
  3087. return queue_index;
  3088. }
  3089. #endif
  3090. static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
  3091. struct sk_buff *skb)
  3092. {
  3093. #ifdef CONFIG_XPS
  3094. struct xps_dev_maps *dev_maps;
  3095. struct sock *sk = skb->sk;
  3096. int queue_index = -1;
  3097. if (!static_key_false(&xps_needed))
  3098. return -1;
  3099. rcu_read_lock();
  3100. if (!static_key_false(&xps_rxqs_needed))
  3101. goto get_cpus_map;
  3102. dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
  3103. if (dev_maps) {
  3104. int tci = sk_rx_queue_get(sk);
  3105. if (tci >= 0 && tci < dev->num_rx_queues)
  3106. queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
  3107. tci);
  3108. }
  3109. get_cpus_map:
  3110. if (queue_index < 0) {
  3111. dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
  3112. if (dev_maps) {
  3113. unsigned int tci = skb->sender_cpu - 1;
  3114. queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
  3115. tci);
  3116. }
  3117. }
  3118. rcu_read_unlock();
  3119. return queue_index;
  3120. #else
  3121. return -1;
  3122. #endif
  3123. }
  3124. u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
  3125. struct net_device *sb_dev,
  3126. select_queue_fallback_t fallback)
  3127. {
  3128. return 0;
  3129. }
  3130. EXPORT_SYMBOL(dev_pick_tx_zero);
  3131. u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
  3132. struct net_device *sb_dev,
  3133. select_queue_fallback_t fallback)
  3134. {
  3135. return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
  3136. }
  3137. EXPORT_SYMBOL(dev_pick_tx_cpu_id);
  3138. static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
  3139. struct net_device *sb_dev)
  3140. {
  3141. struct sock *sk = skb->sk;
  3142. int queue_index = sk_tx_queue_get(sk);
  3143. sb_dev = sb_dev ? : dev;
  3144. if (queue_index < 0 || skb->ooo_okay ||
  3145. queue_index >= dev->real_num_tx_queues) {
  3146. int new_index = get_xps_queue(dev, sb_dev, skb);
  3147. if (new_index < 0)
  3148. new_index = skb_tx_hash(dev, sb_dev, skb);
  3149. if (queue_index != new_index && sk &&
  3150. sk_fullsock(sk) &&
  3151. rcu_access_pointer(sk->sk_dst_cache))
  3152. sk_tx_queue_set(sk, new_index);
  3153. queue_index = new_index;
  3154. }
  3155. return queue_index;
  3156. }
  3157. struct netdev_queue *netdev_pick_tx(struct net_device *dev,
  3158. struct sk_buff *skb,
  3159. struct net_device *sb_dev)
  3160. {
  3161. int queue_index = 0;
  3162. #ifdef CONFIG_XPS
  3163. u32 sender_cpu = skb->sender_cpu - 1;
  3164. if (sender_cpu >= (u32)NR_CPUS)
  3165. skb->sender_cpu = raw_smp_processor_id() + 1;
  3166. #endif
  3167. if (dev->real_num_tx_queues != 1) {
  3168. const struct net_device_ops *ops = dev->netdev_ops;
  3169. if (ops->ndo_select_queue)
  3170. queue_index = ops->ndo_select_queue(dev, skb, sb_dev,
  3171. __netdev_pick_tx);
  3172. else
  3173. queue_index = __netdev_pick_tx(dev, skb, sb_dev);
  3174. queue_index = netdev_cap_txqueue(dev, queue_index);
  3175. }
  3176. skb_set_queue_mapping(skb, queue_index);
  3177. return netdev_get_tx_queue(dev, queue_index);
  3178. }
  3179. /**
  3180. * __dev_queue_xmit - transmit a buffer
  3181. * @skb: buffer to transmit
  3182. * @sb_dev: suboordinate device used for L2 forwarding offload
  3183. *
  3184. * Queue a buffer for transmission to a network device. The caller must
  3185. * have set the device and priority and built the buffer before calling
  3186. * this function. The function can be called from an interrupt.
  3187. *
  3188. * A negative errno code is returned on a failure. A success does not
  3189. * guarantee the frame will be transmitted as it may be dropped due
  3190. * to congestion or traffic shaping.
  3191. *
  3192. * -----------------------------------------------------------------------------------
  3193. * I notice this method can also return errors from the queue disciplines,
  3194. * including NET_XMIT_DROP, which is a positive value. So, errors can also
  3195. * be positive.
  3196. *
  3197. * Regardless of the return value, the skb is consumed, so it is currently
  3198. * difficult to retry a send to this method. (You can bump the ref count
  3199. * before sending to hold a reference for retry if you are careful.)
  3200. *
  3201. * When calling this method, interrupts MUST be enabled. This is because
  3202. * the BH enable code must have IRQs enabled so that it will not deadlock.
  3203. * --BLG
  3204. */
  3205. static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
  3206. {
  3207. struct net_device *dev = skb->dev;
  3208. struct netdev_queue *txq;
  3209. struct Qdisc *q;
  3210. int rc = -ENOMEM;
  3211. bool again = false;
  3212. skb_reset_mac_header(skb);
  3213. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
  3214. __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
  3215. /* Disable soft irqs for various locks below. Also
  3216. * stops preemption for RCU.
  3217. */
  3218. rcu_read_lock_bh();
  3219. skb_update_prio(skb);
  3220. qdisc_pkt_len_init(skb);
  3221. #ifdef CONFIG_NET_CLS_ACT
  3222. skb->tc_at_ingress = 0;
  3223. # ifdef CONFIG_NET_EGRESS
  3224. if (static_branch_unlikely(&egress_needed_key)) {
  3225. skb = sch_handle_egress(skb, &rc, dev);
  3226. if (!skb)
  3227. goto out;
  3228. }
  3229. # endif
  3230. #endif
  3231. /* If device/qdisc don't need skb->dst, release it right now while
  3232. * its hot in this cpu cache.
  3233. */
  3234. if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
  3235. skb_dst_drop(skb);
  3236. else
  3237. skb_dst_force(skb);
  3238. txq = netdev_pick_tx(dev, skb, sb_dev);
  3239. q = rcu_dereference_bh(txq->qdisc);
  3240. trace_net_dev_queue(skb);
  3241. if (q->enqueue) {
  3242. rc = __dev_xmit_skb(skb, q, dev, txq);
  3243. goto out;
  3244. }
  3245. /* The device has no queue. Common case for software devices:
  3246. * loopback, all the sorts of tunnels...
  3247. * Really, it is unlikely that netif_tx_lock protection is necessary
  3248. * here. (f.e. loopback and IP tunnels are clean ignoring statistics
  3249. * counters.)
  3250. * However, it is possible, that they rely on protection
  3251. * made by us here.
  3252. * Check this and shot the lock. It is not prone from deadlocks.
  3253. *Either shot noqueue qdisc, it is even simpler 8)
  3254. */
  3255. if (dev->flags & IFF_UP) {
  3256. int cpu = smp_processor_id(); /* ok because BHs are off */
  3257. if (txq->xmit_lock_owner != cpu) {
  3258. if (unlikely(__this_cpu_read(xmit_recursion) >
  3259. XMIT_RECURSION_LIMIT))
  3260. goto recursion_alert;
  3261. skb = validate_xmit_skb(skb, dev, &again);
  3262. if (!skb)
  3263. goto out;
  3264. HARD_TX_LOCK(dev, txq, cpu);
  3265. if (!netif_xmit_stopped(txq)) {
  3266. __this_cpu_inc(xmit_recursion);
  3267. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  3268. __this_cpu_dec(xmit_recursion);
  3269. if (dev_xmit_complete(rc)) {
  3270. HARD_TX_UNLOCK(dev, txq);
  3271. goto out;
  3272. }
  3273. }
  3274. HARD_TX_UNLOCK(dev, txq);
  3275. net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
  3276. dev->name);
  3277. } else {
  3278. /* Recursion is detected! It is possible,
  3279. * unfortunately
  3280. */
  3281. recursion_alert:
  3282. net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
  3283. dev->name);
  3284. }
  3285. }
  3286. rc = -ENETDOWN;
  3287. rcu_read_unlock_bh();
  3288. atomic_long_inc(&dev->tx_dropped);
  3289. kfree_skb_list(skb);
  3290. return rc;
  3291. out:
  3292. rcu_read_unlock_bh();
  3293. return rc;
  3294. }
  3295. int dev_queue_xmit(struct sk_buff *skb)
  3296. {
  3297. return __dev_queue_xmit(skb, NULL);
  3298. }
  3299. EXPORT_SYMBOL(dev_queue_xmit);
  3300. int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
  3301. {
  3302. return __dev_queue_xmit(skb, sb_dev);
  3303. }
  3304. EXPORT_SYMBOL(dev_queue_xmit_accel);
  3305. int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
  3306. {
  3307. struct net_device *dev = skb->dev;
  3308. struct sk_buff *orig_skb = skb;
  3309. struct netdev_queue *txq;
  3310. int ret = NETDEV_TX_BUSY;
  3311. bool again = false;
  3312. if (unlikely(!netif_running(dev) ||
  3313. !netif_carrier_ok(dev)))
  3314. goto drop;
  3315. skb = validate_xmit_skb_list(skb, dev, &again);
  3316. if (skb != orig_skb)
  3317. goto drop;
  3318. skb_set_queue_mapping(skb, queue_id);
  3319. txq = skb_get_tx_queue(dev, skb);
  3320. local_bh_disable();
  3321. HARD_TX_LOCK(dev, txq, smp_processor_id());
  3322. if (!netif_xmit_frozen_or_drv_stopped(txq))
  3323. ret = netdev_start_xmit(skb, dev, txq, false);
  3324. HARD_TX_UNLOCK(dev, txq);
  3325. local_bh_enable();
  3326. if (!dev_xmit_complete(ret))
  3327. kfree_skb(skb);
  3328. return ret;
  3329. drop:
  3330. atomic_long_inc(&dev->tx_dropped);
  3331. kfree_skb_list(skb);
  3332. return NET_XMIT_DROP;
  3333. }
  3334. EXPORT_SYMBOL(dev_direct_xmit);
  3335. /*************************************************************************
  3336. * Receiver routines
  3337. *************************************************************************/
  3338. int netdev_max_backlog __read_mostly = 1000;
  3339. EXPORT_SYMBOL(netdev_max_backlog);
  3340. int netdev_tstamp_prequeue __read_mostly = 1;
  3341. int netdev_budget __read_mostly = 300;
  3342. unsigned int __read_mostly netdev_budget_usecs = 2000;
  3343. int weight_p __read_mostly = 64; /* old backlog weight */
  3344. int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
  3345. int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
  3346. int dev_rx_weight __read_mostly = 64;
  3347. int dev_tx_weight __read_mostly = 64;
  3348. /* Called with irq disabled */
  3349. static inline void ____napi_schedule(struct softnet_data *sd,
  3350. struct napi_struct *napi)
  3351. {
  3352. list_add_tail(&napi->poll_list, &sd->poll_list);
  3353. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  3354. }
  3355. #ifdef CONFIG_RPS
  3356. /* One global table that all flow-based protocols share. */
  3357. struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
  3358. EXPORT_SYMBOL(rps_sock_flow_table);
  3359. u32 rps_cpu_mask __read_mostly;
  3360. EXPORT_SYMBOL(rps_cpu_mask);
  3361. struct static_key rps_needed __read_mostly;
  3362. EXPORT_SYMBOL(rps_needed);
  3363. struct static_key rfs_needed __read_mostly;
  3364. EXPORT_SYMBOL(rfs_needed);
  3365. static struct rps_dev_flow *
  3366. set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  3367. struct rps_dev_flow *rflow, u16 next_cpu)
  3368. {
  3369. if (next_cpu < nr_cpu_ids) {
  3370. #ifdef CONFIG_RFS_ACCEL
  3371. struct netdev_rx_queue *rxqueue;
  3372. struct rps_dev_flow_table *flow_table;
  3373. struct rps_dev_flow *old_rflow;
  3374. u32 flow_id;
  3375. u16 rxq_index;
  3376. int rc;
  3377. /* Should we steer this flow to a different hardware queue? */
  3378. if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
  3379. !(dev->features & NETIF_F_NTUPLE))
  3380. goto out;
  3381. rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
  3382. if (rxq_index == skb_get_rx_queue(skb))
  3383. goto out;
  3384. rxqueue = dev->_rx + rxq_index;
  3385. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  3386. if (!flow_table)
  3387. goto out;
  3388. flow_id = skb_get_hash(skb) & flow_table->mask;
  3389. rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
  3390. rxq_index, flow_id);
  3391. if (rc < 0)
  3392. goto out;
  3393. old_rflow = rflow;
  3394. rflow = &flow_table->flows[flow_id];
  3395. rflow->filter = rc;
  3396. if (old_rflow->filter == rflow->filter)
  3397. old_rflow->filter = RPS_NO_FILTER;
  3398. out:
  3399. #endif
  3400. rflow->last_qtail =
  3401. per_cpu(softnet_data, next_cpu).input_queue_head;
  3402. }
  3403. rflow->cpu = next_cpu;
  3404. return rflow;
  3405. }
  3406. /*
  3407. * get_rps_cpu is called from netif_receive_skb and returns the target
  3408. * CPU from the RPS map of the receiving queue for a given skb.
  3409. * rcu_read_lock must be held on entry.
  3410. */
  3411. static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  3412. struct rps_dev_flow **rflowp)
  3413. {
  3414. const struct rps_sock_flow_table *sock_flow_table;
  3415. struct netdev_rx_queue *rxqueue = dev->_rx;
  3416. struct rps_dev_flow_table *flow_table;
  3417. struct rps_map *map;
  3418. int cpu = -1;
  3419. u32 tcpu;
  3420. u32 hash;
  3421. if (skb_rx_queue_recorded(skb)) {
  3422. u16 index = skb_get_rx_queue(skb);
  3423. if (unlikely(index >= dev->real_num_rx_queues)) {
  3424. WARN_ONCE(dev->real_num_rx_queues > 1,
  3425. "%s received packet on queue %u, but number "
  3426. "of RX queues is %u\n",
  3427. dev->name, index, dev->real_num_rx_queues);
  3428. goto done;
  3429. }
  3430. rxqueue += index;
  3431. }
  3432. /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
  3433. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  3434. map = rcu_dereference(rxqueue->rps_map);
  3435. if (!flow_table && !map)
  3436. goto done;
  3437. skb_reset_network_header(skb);
  3438. hash = skb_get_hash(skb);
  3439. if (!hash)
  3440. goto done;
  3441. sock_flow_table = rcu_dereference(rps_sock_flow_table);
  3442. if (flow_table && sock_flow_table) {
  3443. struct rps_dev_flow *rflow;
  3444. u32 next_cpu;
  3445. u32 ident;
  3446. /* First check into global flow table if there is a match */
  3447. ident = sock_flow_table->ents[hash & sock_flow_table->mask];
  3448. if ((ident ^ hash) & ~rps_cpu_mask)
  3449. goto try_rps;
  3450. next_cpu = ident & rps_cpu_mask;
  3451. /* OK, now we know there is a match,
  3452. * we can look at the local (per receive queue) flow table
  3453. */
  3454. rflow = &flow_table->flows[hash & flow_table->mask];
  3455. tcpu = rflow->cpu;
  3456. /*
  3457. * If the desired CPU (where last recvmsg was done) is
  3458. * different from current CPU (one in the rx-queue flow
  3459. * table entry), switch if one of the following holds:
  3460. * - Current CPU is unset (>= nr_cpu_ids).
  3461. * - Current CPU is offline.
  3462. * - The current CPU's queue tail has advanced beyond the
  3463. * last packet that was enqueued using this table entry.
  3464. * This guarantees that all previous packets for the flow
  3465. * have been dequeued, thus preserving in order delivery.
  3466. */
  3467. if (unlikely(tcpu != next_cpu) &&
  3468. (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
  3469. ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
  3470. rflow->last_qtail)) >= 0)) {
  3471. tcpu = next_cpu;
  3472. rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
  3473. }
  3474. if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
  3475. *rflowp = rflow;
  3476. cpu = tcpu;
  3477. goto done;
  3478. }
  3479. }
  3480. try_rps:
  3481. if (map) {
  3482. tcpu = map->cpus[reciprocal_scale(hash, map->len)];
  3483. if (cpu_online(tcpu)) {
  3484. cpu = tcpu;
  3485. goto done;
  3486. }
  3487. }
  3488. done:
  3489. return cpu;
  3490. }
  3491. #ifdef CONFIG_RFS_ACCEL
  3492. /**
  3493. * rps_may_expire_flow - check whether an RFS hardware filter may be removed
  3494. * @dev: Device on which the filter was set
  3495. * @rxq_index: RX queue index
  3496. * @flow_id: Flow ID passed to ndo_rx_flow_steer()
  3497. * @filter_id: Filter ID returned by ndo_rx_flow_steer()
  3498. *
  3499. * Drivers that implement ndo_rx_flow_steer() should periodically call
  3500. * this function for each installed filter and remove the filters for
  3501. * which it returns %true.
  3502. */
  3503. bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
  3504. u32 flow_id, u16 filter_id)
  3505. {
  3506. struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
  3507. struct rps_dev_flow_table *flow_table;
  3508. struct rps_dev_flow *rflow;
  3509. bool expire = true;
  3510. unsigned int cpu;
  3511. rcu_read_lock();
  3512. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  3513. if (flow_table && flow_id <= flow_table->mask) {
  3514. rflow = &flow_table->flows[flow_id];
  3515. cpu = READ_ONCE(rflow->cpu);
  3516. if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
  3517. ((int)(per_cpu(softnet_data, cpu).input_queue_head -
  3518. rflow->last_qtail) <
  3519. (int)(10 * flow_table->mask)))
  3520. expire = false;
  3521. }
  3522. rcu_read_unlock();
  3523. return expire;
  3524. }
  3525. EXPORT_SYMBOL(rps_may_expire_flow);
  3526. #endif /* CONFIG_RFS_ACCEL */
  3527. /* Called from hardirq (IPI) context */
  3528. static void rps_trigger_softirq(void *data)
  3529. {
  3530. struct softnet_data *sd = data;
  3531. ____napi_schedule(sd, &sd->backlog);
  3532. sd->received_rps++;
  3533. }
  3534. #endif /* CONFIG_RPS */
  3535. /*
  3536. * Check if this softnet_data structure is another cpu one
  3537. * If yes, queue it to our IPI list and return 1
  3538. * If no, return 0
  3539. */
  3540. static int rps_ipi_queued(struct softnet_data *sd)
  3541. {
  3542. #ifdef CONFIG_RPS
  3543. struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
  3544. if (sd != mysd) {
  3545. sd->rps_ipi_next = mysd->rps_ipi_list;
  3546. mysd->rps_ipi_list = sd;
  3547. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  3548. return 1;
  3549. }
  3550. #endif /* CONFIG_RPS */
  3551. return 0;
  3552. }
  3553. #ifdef CONFIG_NET_FLOW_LIMIT
  3554. int netdev_flow_limit_table_len __read_mostly = (1 << 12);
  3555. #endif
  3556. static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
  3557. {
  3558. #ifdef CONFIG_NET_FLOW_LIMIT
  3559. struct sd_flow_limit *fl;
  3560. struct softnet_data *sd;
  3561. unsigned int old_flow, new_flow;
  3562. if (qlen < (netdev_max_backlog >> 1))
  3563. return false;
  3564. sd = this_cpu_ptr(&softnet_data);
  3565. rcu_read_lock();
  3566. fl = rcu_dereference(sd->flow_limit);
  3567. if (fl) {
  3568. new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
  3569. old_flow = fl->history[fl->history_head];
  3570. fl->history[fl->history_head] = new_flow;
  3571. fl->history_head++;
  3572. fl->history_head &= FLOW_LIMIT_HISTORY - 1;
  3573. if (likely(fl->buckets[old_flow]))
  3574. fl->buckets[old_flow]--;
  3575. if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
  3576. fl->count++;
  3577. rcu_read_unlock();
  3578. return true;
  3579. }
  3580. }
  3581. rcu_read_unlock();
  3582. #endif
  3583. return false;
  3584. }
  3585. /*
  3586. * enqueue_to_backlog is called to queue an skb to a per CPU backlog
  3587. * queue (may be a remote CPU queue).
  3588. */
  3589. static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
  3590. unsigned int *qtail)
  3591. {
  3592. struct softnet_data *sd;
  3593. unsigned long flags;
  3594. unsigned int qlen;
  3595. sd = &per_cpu(softnet_data, cpu);
  3596. local_irq_save(flags);
  3597. rps_lock(sd);
  3598. if (!netif_running(skb->dev))
  3599. goto drop;
  3600. qlen = skb_queue_len(&sd->input_pkt_queue);
  3601. if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
  3602. if (qlen) {
  3603. enqueue:
  3604. __skb_queue_tail(&sd->input_pkt_queue, skb);
  3605. input_queue_tail_incr_save(sd, qtail);
  3606. rps_unlock(sd);
  3607. local_irq_restore(flags);
  3608. return NET_RX_SUCCESS;
  3609. }
  3610. /* Schedule NAPI for backlog device
  3611. * We can use non atomic operation since we own the queue lock
  3612. */
  3613. if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
  3614. if (!rps_ipi_queued(sd))
  3615. ____napi_schedule(sd, &sd->backlog);
  3616. }
  3617. goto enqueue;
  3618. }
  3619. drop:
  3620. sd->dropped++;
  3621. rps_unlock(sd);
  3622. local_irq_restore(flags);
  3623. atomic_long_inc(&skb->dev->rx_dropped);
  3624. kfree_skb(skb);
  3625. return NET_RX_DROP;
  3626. }
  3627. static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
  3628. {
  3629. struct net_device *dev = skb->dev;
  3630. struct netdev_rx_queue *rxqueue;
  3631. rxqueue = dev->_rx;
  3632. if (skb_rx_queue_recorded(skb)) {
  3633. u16 index = skb_get_rx_queue(skb);
  3634. if (unlikely(index >= dev->real_num_rx_queues)) {
  3635. WARN_ONCE(dev->real_num_rx_queues > 1,
  3636. "%s received packet on queue %u, but number "
  3637. "of RX queues is %u\n",
  3638. dev->name, index, dev->real_num_rx_queues);
  3639. return rxqueue; /* Return first rxqueue */
  3640. }
  3641. rxqueue += index;
  3642. }
  3643. return rxqueue;
  3644. }
  3645. static u32 netif_receive_generic_xdp(struct sk_buff *skb,
  3646. struct xdp_buff *xdp,
  3647. struct bpf_prog *xdp_prog)
  3648. {
  3649. struct netdev_rx_queue *rxqueue;
  3650. void *orig_data, *orig_data_end;
  3651. u32 metalen, act = XDP_DROP;
  3652. __be16 orig_eth_type;
  3653. struct ethhdr *eth;
  3654. bool orig_bcast;
  3655. int hlen, off;
  3656. u32 mac_len;
  3657. /* Reinjected packets coming from act_mirred or similar should
  3658. * not get XDP generic processing.
  3659. */
  3660. if (skb_is_tc_redirected(skb))
  3661. return XDP_PASS;
  3662. /* XDP packets must be linear and must have sufficient headroom
  3663. * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
  3664. * native XDP provides, thus we need to do it here as well.
  3665. */
  3666. if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
  3667. skb_headroom(skb) < XDP_PACKET_HEADROOM) {
  3668. int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
  3669. int troom = skb->tail + skb->data_len - skb->end;
  3670. /* In case we have to go down the path and also linearize,
  3671. * then lets do the pskb_expand_head() work just once here.
  3672. */
  3673. if (pskb_expand_head(skb,
  3674. hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
  3675. troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
  3676. goto do_drop;
  3677. if (skb_linearize(skb))
  3678. goto do_drop;
  3679. }
  3680. /* The XDP program wants to see the packet starting at the MAC
  3681. * header.
  3682. */
  3683. mac_len = skb->data - skb_mac_header(skb);
  3684. hlen = skb_headlen(skb) + mac_len;
  3685. xdp->data = skb->data - mac_len;
  3686. xdp->data_meta = xdp->data;
  3687. xdp->data_end = xdp->data + hlen;
  3688. xdp->data_hard_start = skb->data - skb_headroom(skb);
  3689. orig_data_end = xdp->data_end;
  3690. orig_data = xdp->data;
  3691. eth = (struct ethhdr *)xdp->data;
  3692. orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
  3693. orig_eth_type = eth->h_proto;
  3694. rxqueue = netif_get_rxqueue(skb);
  3695. xdp->rxq = &rxqueue->xdp_rxq;
  3696. act = bpf_prog_run_xdp(xdp_prog, xdp);
  3697. /* check if bpf_xdp_adjust_head was used */
  3698. off = xdp->data - orig_data;
  3699. if (off) {
  3700. if (off > 0)
  3701. __skb_pull(skb, off);
  3702. else if (off < 0)
  3703. __skb_push(skb, -off);
  3704. skb->mac_header += off;
  3705. skb_reset_network_header(skb);
  3706. }
  3707. /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
  3708. * pckt.
  3709. */
  3710. off = orig_data_end - xdp->data_end;
  3711. if (off != 0) {
  3712. skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
  3713. skb->len -= off;
  3714. }
  3715. /* check if XDP changed eth hdr such SKB needs update */
  3716. eth = (struct ethhdr *)xdp->data;
  3717. if ((orig_eth_type != eth->h_proto) ||
  3718. (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
  3719. __skb_push(skb, ETH_HLEN);
  3720. skb->protocol = eth_type_trans(skb, skb->dev);
  3721. }
  3722. switch (act) {
  3723. case XDP_REDIRECT:
  3724. case XDP_TX:
  3725. __skb_push(skb, mac_len);
  3726. break;
  3727. case XDP_PASS:
  3728. metalen = xdp->data - xdp->data_meta;
  3729. if (metalen)
  3730. skb_metadata_set(skb, metalen);
  3731. break;
  3732. default:
  3733. bpf_warn_invalid_xdp_action(act);
  3734. /* fall through */
  3735. case XDP_ABORTED:
  3736. trace_xdp_exception(skb->dev, xdp_prog, act);
  3737. /* fall through */
  3738. case XDP_DROP:
  3739. do_drop:
  3740. kfree_skb(skb);
  3741. break;
  3742. }
  3743. return act;
  3744. }
  3745. /* When doing generic XDP we have to bypass the qdisc layer and the
  3746. * network taps in order to match in-driver-XDP behavior.
  3747. */
  3748. void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
  3749. {
  3750. struct net_device *dev = skb->dev;
  3751. struct netdev_queue *txq;
  3752. bool free_skb = true;
  3753. int cpu, rc;
  3754. txq = netdev_pick_tx(dev, skb, NULL);
  3755. cpu = smp_processor_id();
  3756. HARD_TX_LOCK(dev, txq, cpu);
  3757. if (!netif_xmit_stopped(txq)) {
  3758. rc = netdev_start_xmit(skb, dev, txq, 0);
  3759. if (dev_xmit_complete(rc))
  3760. free_skb = false;
  3761. }
  3762. HARD_TX_UNLOCK(dev, txq);
  3763. if (free_skb) {
  3764. trace_xdp_exception(dev, xdp_prog, XDP_TX);
  3765. kfree_skb(skb);
  3766. }
  3767. }
  3768. EXPORT_SYMBOL_GPL(generic_xdp_tx);
  3769. static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
  3770. int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
  3771. {
  3772. if (xdp_prog) {
  3773. struct xdp_buff xdp;
  3774. u32 act;
  3775. int err;
  3776. act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
  3777. if (act != XDP_PASS) {
  3778. switch (act) {
  3779. case XDP_REDIRECT:
  3780. err = xdp_do_generic_redirect(skb->dev, skb,
  3781. &xdp, xdp_prog);
  3782. if (err)
  3783. goto out_redir;
  3784. break;
  3785. case XDP_TX:
  3786. generic_xdp_tx(skb, xdp_prog);
  3787. break;
  3788. }
  3789. return XDP_DROP;
  3790. }
  3791. }
  3792. return XDP_PASS;
  3793. out_redir:
  3794. kfree_skb(skb);
  3795. return XDP_DROP;
  3796. }
  3797. EXPORT_SYMBOL_GPL(do_xdp_generic);
  3798. static int netif_rx_internal(struct sk_buff *skb)
  3799. {
  3800. int ret;
  3801. net_timestamp_check(netdev_tstamp_prequeue, skb);
  3802. trace_netif_rx(skb);
  3803. #ifdef CONFIG_RPS
  3804. if (static_key_false(&rps_needed)) {
  3805. struct rps_dev_flow voidflow, *rflow = &voidflow;
  3806. int cpu;
  3807. preempt_disable();
  3808. rcu_read_lock();
  3809. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  3810. if (cpu < 0)
  3811. cpu = smp_processor_id();
  3812. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  3813. rcu_read_unlock();
  3814. preempt_enable();
  3815. } else
  3816. #endif
  3817. {
  3818. unsigned int qtail;
  3819. ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  3820. put_cpu();
  3821. }
  3822. return ret;
  3823. }
  3824. /**
  3825. * netif_rx - post buffer to the network code
  3826. * @skb: buffer to post
  3827. *
  3828. * This function receives a packet from a device driver and queues it for
  3829. * the upper (protocol) levels to process. It always succeeds. The buffer
  3830. * may be dropped during processing for congestion control or by the
  3831. * protocol layers.
  3832. *
  3833. * return values:
  3834. * NET_RX_SUCCESS (no congestion)
  3835. * NET_RX_DROP (packet was dropped)
  3836. *
  3837. */
  3838. int netif_rx(struct sk_buff *skb)
  3839. {
  3840. trace_netif_rx_entry(skb);
  3841. return netif_rx_internal(skb);
  3842. }
  3843. EXPORT_SYMBOL(netif_rx);
  3844. int netif_rx_ni(struct sk_buff *skb)
  3845. {
  3846. int err;
  3847. trace_netif_rx_ni_entry(skb);
  3848. preempt_disable();
  3849. err = netif_rx_internal(skb);
  3850. if (local_softirq_pending())
  3851. do_softirq();
  3852. preempt_enable();
  3853. return err;
  3854. }
  3855. EXPORT_SYMBOL(netif_rx_ni);
  3856. static __latent_entropy void net_tx_action(struct softirq_action *h)
  3857. {
  3858. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  3859. if (sd->completion_queue) {
  3860. struct sk_buff *clist;
  3861. local_irq_disable();
  3862. clist = sd->completion_queue;
  3863. sd->completion_queue = NULL;
  3864. local_irq_enable();
  3865. while (clist) {
  3866. struct sk_buff *skb = clist;
  3867. clist = clist->next;
  3868. WARN_ON(refcount_read(&skb->users));
  3869. if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
  3870. trace_consume_skb(skb);
  3871. else
  3872. trace_kfree_skb(skb, net_tx_action);
  3873. if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
  3874. __kfree_skb(skb);
  3875. else
  3876. __kfree_skb_defer(skb);
  3877. }
  3878. __kfree_skb_flush();
  3879. }
  3880. if (sd->output_queue) {
  3881. struct Qdisc *head;
  3882. local_irq_disable();
  3883. head = sd->output_queue;
  3884. sd->output_queue = NULL;
  3885. sd->output_queue_tailp = &sd->output_queue;
  3886. local_irq_enable();
  3887. while (head) {
  3888. struct Qdisc *q = head;
  3889. spinlock_t *root_lock = NULL;
  3890. head = head->next_sched;
  3891. if (!(q->flags & TCQ_F_NOLOCK)) {
  3892. root_lock = qdisc_lock(q);
  3893. spin_lock(root_lock);
  3894. }
  3895. /* We need to make sure head->next_sched is read
  3896. * before clearing __QDISC_STATE_SCHED
  3897. */
  3898. smp_mb__before_atomic();
  3899. clear_bit(__QDISC_STATE_SCHED, &q->state);
  3900. qdisc_run(q);
  3901. if (root_lock)
  3902. spin_unlock(root_lock);
  3903. }
  3904. }
  3905. xfrm_dev_backlog(sd);
  3906. }
  3907. #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
  3908. /* This hook is defined here for ATM LANE */
  3909. int (*br_fdb_test_addr_hook)(struct net_device *dev,
  3910. unsigned char *addr) __read_mostly;
  3911. EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
  3912. #endif
  3913. static inline struct sk_buff *
  3914. sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
  3915. struct net_device *orig_dev)
  3916. {
  3917. #ifdef CONFIG_NET_CLS_ACT
  3918. struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
  3919. struct tcf_result cl_res;
  3920. /* If there's at least one ingress present somewhere (so
  3921. * we get here via enabled static key), remaining devices
  3922. * that are not configured with an ingress qdisc will bail
  3923. * out here.
  3924. */
  3925. if (!miniq)
  3926. return skb;
  3927. if (*pt_prev) {
  3928. *ret = deliver_skb(skb, *pt_prev, orig_dev);
  3929. *pt_prev = NULL;
  3930. }
  3931. qdisc_skb_cb(skb)->pkt_len = skb->len;
  3932. skb->tc_at_ingress = 1;
  3933. mini_qdisc_bstats_cpu_update(miniq, skb);
  3934. switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
  3935. case TC_ACT_OK:
  3936. case TC_ACT_RECLASSIFY:
  3937. skb->tc_index = TC_H_MIN(cl_res.classid);
  3938. break;
  3939. case TC_ACT_SHOT:
  3940. mini_qdisc_qstats_cpu_drop(miniq);
  3941. kfree_skb(skb);
  3942. return NULL;
  3943. case TC_ACT_STOLEN:
  3944. case TC_ACT_QUEUED:
  3945. case TC_ACT_TRAP:
  3946. consume_skb(skb);
  3947. return NULL;
  3948. case TC_ACT_REDIRECT:
  3949. /* skb_mac_header check was done by cls/act_bpf, so
  3950. * we can safely push the L2 header back before
  3951. * redirecting to another netdev
  3952. */
  3953. __skb_push(skb, skb->mac_len);
  3954. skb_do_redirect(skb);
  3955. return NULL;
  3956. case TC_ACT_REINSERT:
  3957. /* this does not scrub the packet, and updates stats on error */
  3958. skb_tc_reinsert(skb, &cl_res);
  3959. return NULL;
  3960. default:
  3961. break;
  3962. }
  3963. #endif /* CONFIG_NET_CLS_ACT */
  3964. return skb;
  3965. }
  3966. /**
  3967. * netdev_is_rx_handler_busy - check if receive handler is registered
  3968. * @dev: device to check
  3969. *
  3970. * Check if a receive handler is already registered for a given device.
  3971. * Return true if there one.
  3972. *
  3973. * The caller must hold the rtnl_mutex.
  3974. */
  3975. bool netdev_is_rx_handler_busy(struct net_device *dev)
  3976. {
  3977. ASSERT_RTNL();
  3978. return dev && rtnl_dereference(dev->rx_handler);
  3979. }
  3980. EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
  3981. /**
  3982. * netdev_rx_handler_register - register receive handler
  3983. * @dev: device to register a handler for
  3984. * @rx_handler: receive handler to register
  3985. * @rx_handler_data: data pointer that is used by rx handler
  3986. *
  3987. * Register a receive handler for a device. This handler will then be
  3988. * called from __netif_receive_skb. A negative errno code is returned
  3989. * on a failure.
  3990. *
  3991. * The caller must hold the rtnl_mutex.
  3992. *
  3993. * For a general description of rx_handler, see enum rx_handler_result.
  3994. */
  3995. int netdev_rx_handler_register(struct net_device *dev,
  3996. rx_handler_func_t *rx_handler,
  3997. void *rx_handler_data)
  3998. {
  3999. if (netdev_is_rx_handler_busy(dev))
  4000. return -EBUSY;
  4001. if (dev->priv_flags & IFF_NO_RX_HANDLER)
  4002. return -EINVAL;
  4003. /* Note: rx_handler_data must be set before rx_handler */
  4004. rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
  4005. rcu_assign_pointer(dev->rx_handler, rx_handler);
  4006. return 0;
  4007. }
  4008. EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
  4009. /**
  4010. * netdev_rx_handler_unregister - unregister receive handler
  4011. * @dev: device to unregister a handler from
  4012. *
  4013. * Unregister a receive handler from a device.
  4014. *
  4015. * The caller must hold the rtnl_mutex.
  4016. */
  4017. void netdev_rx_handler_unregister(struct net_device *dev)
  4018. {
  4019. ASSERT_RTNL();
  4020. RCU_INIT_POINTER(dev->rx_handler, NULL);
  4021. /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
  4022. * section has a guarantee to see a non NULL rx_handler_data
  4023. * as well.
  4024. */
  4025. synchronize_net();
  4026. RCU_INIT_POINTER(dev->rx_handler_data, NULL);
  4027. }
  4028. EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
  4029. /*
  4030. * Limit the use of PFMEMALLOC reserves to those protocols that implement
  4031. * the special handling of PFMEMALLOC skbs.
  4032. */
  4033. static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
  4034. {
  4035. switch (skb->protocol) {
  4036. case htons(ETH_P_ARP):
  4037. case htons(ETH_P_IP):
  4038. case htons(ETH_P_IPV6):
  4039. case htons(ETH_P_8021Q):
  4040. case htons(ETH_P_8021AD):
  4041. return true;
  4042. default:
  4043. return false;
  4044. }
  4045. }
  4046. static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
  4047. int *ret, struct net_device *orig_dev)
  4048. {
  4049. #ifdef CONFIG_NETFILTER_INGRESS
  4050. if (nf_hook_ingress_active(skb)) {
  4051. int ingress_retval;
  4052. if (*pt_prev) {
  4053. *ret = deliver_skb(skb, *pt_prev, orig_dev);
  4054. *pt_prev = NULL;
  4055. }
  4056. rcu_read_lock();
  4057. ingress_retval = nf_hook_ingress(skb);
  4058. rcu_read_unlock();
  4059. return ingress_retval;
  4060. }
  4061. #endif /* CONFIG_NETFILTER_INGRESS */
  4062. return 0;
  4063. }
  4064. static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc,
  4065. struct packet_type **ppt_prev)
  4066. {
  4067. struct packet_type *ptype, *pt_prev;
  4068. rx_handler_func_t *rx_handler;
  4069. struct net_device *orig_dev;
  4070. bool deliver_exact = false;
  4071. int ret = NET_RX_DROP;
  4072. __be16 type;
  4073. net_timestamp_check(!netdev_tstamp_prequeue, skb);
  4074. trace_netif_receive_skb(skb);
  4075. orig_dev = skb->dev;
  4076. skb_reset_network_header(skb);
  4077. if (!skb_transport_header_was_set(skb))
  4078. skb_reset_transport_header(skb);
  4079. skb_reset_mac_len(skb);
  4080. pt_prev = NULL;
  4081. another_round:
  4082. skb->skb_iif = skb->dev->ifindex;
  4083. __this_cpu_inc(softnet_data.processed);
  4084. if (static_branch_unlikely(&generic_xdp_needed_key)) {
  4085. int ret2;
  4086. preempt_disable();
  4087. ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
  4088. preempt_enable();
  4089. if (ret2 != XDP_PASS)
  4090. return NET_RX_DROP;
  4091. skb_reset_mac_len(skb);
  4092. }
  4093. if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
  4094. skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
  4095. skb = skb_vlan_untag(skb);
  4096. if (unlikely(!skb))
  4097. goto out;
  4098. }
  4099. if (skb_skip_tc_classify(skb))
  4100. goto skip_classify;
  4101. if (pfmemalloc)
  4102. goto skip_taps;
  4103. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  4104. if (pt_prev)
  4105. ret = deliver_skb(skb, pt_prev, orig_dev);
  4106. pt_prev = ptype;
  4107. }
  4108. list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
  4109. if (pt_prev)
  4110. ret = deliver_skb(skb, pt_prev, orig_dev);
  4111. pt_prev = ptype;
  4112. }
  4113. skip_taps:
  4114. #ifdef CONFIG_NET_INGRESS
  4115. if (static_branch_unlikely(&ingress_needed_key)) {
  4116. skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
  4117. if (!skb)
  4118. goto out;
  4119. if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
  4120. goto out;
  4121. }
  4122. #endif
  4123. skb_reset_tc(skb);
  4124. skip_classify:
  4125. if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
  4126. goto drop;
  4127. if (skb_vlan_tag_present(skb)) {
  4128. if (pt_prev) {
  4129. ret = deliver_skb(skb, pt_prev, orig_dev);
  4130. pt_prev = NULL;
  4131. }
  4132. if (vlan_do_receive(&skb))
  4133. goto another_round;
  4134. else if (unlikely(!skb))
  4135. goto out;
  4136. }
  4137. rx_handler = rcu_dereference(skb->dev->rx_handler);
  4138. if (rx_handler) {
  4139. if (pt_prev) {
  4140. ret = deliver_skb(skb, pt_prev, orig_dev);
  4141. pt_prev = NULL;
  4142. }
  4143. switch (rx_handler(&skb)) {
  4144. case RX_HANDLER_CONSUMED:
  4145. ret = NET_RX_SUCCESS;
  4146. goto out;
  4147. case RX_HANDLER_ANOTHER:
  4148. goto another_round;
  4149. case RX_HANDLER_EXACT:
  4150. deliver_exact = true;
  4151. case RX_HANDLER_PASS:
  4152. break;
  4153. default:
  4154. BUG();
  4155. }
  4156. }
  4157. if (unlikely(skb_vlan_tag_present(skb))) {
  4158. if (skb_vlan_tag_get_id(skb))
  4159. skb->pkt_type = PACKET_OTHERHOST;
  4160. /* Note: we might in the future use prio bits
  4161. * and set skb->priority like in vlan_do_receive()
  4162. * For the time being, just ignore Priority Code Point
  4163. */
  4164. skb->vlan_tci = 0;
  4165. }
  4166. type = skb->protocol;
  4167. /* deliver only exact match when indicated */
  4168. if (likely(!deliver_exact)) {
  4169. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  4170. &ptype_base[ntohs(type) &
  4171. PTYPE_HASH_MASK]);
  4172. }
  4173. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  4174. &orig_dev->ptype_specific);
  4175. if (unlikely(skb->dev != orig_dev)) {
  4176. deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
  4177. &skb->dev->ptype_specific);
  4178. }
  4179. if (pt_prev) {
  4180. if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
  4181. goto drop;
  4182. *ppt_prev = pt_prev;
  4183. } else {
  4184. drop:
  4185. if (!deliver_exact)
  4186. atomic_long_inc(&skb->dev->rx_dropped);
  4187. else
  4188. atomic_long_inc(&skb->dev->rx_nohandler);
  4189. kfree_skb(skb);
  4190. /* Jamal, now you will not able to escape explaining
  4191. * me how you were going to use this. :-)
  4192. */
  4193. ret = NET_RX_DROP;
  4194. }
  4195. out:
  4196. return ret;
  4197. }
  4198. static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
  4199. {
  4200. struct net_device *orig_dev = skb->dev;
  4201. struct packet_type *pt_prev = NULL;
  4202. int ret;
  4203. ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
  4204. if (pt_prev)
  4205. ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  4206. return ret;
  4207. }
  4208. /**
  4209. * netif_receive_skb_core - special purpose version of netif_receive_skb
  4210. * @skb: buffer to process
  4211. *
  4212. * More direct receive version of netif_receive_skb(). It should
  4213. * only be used by callers that have a need to skip RPS and Generic XDP.
  4214. * Caller must also take care of handling if (page_is_)pfmemalloc.
  4215. *
  4216. * This function may only be called from softirq context and interrupts
  4217. * should be enabled.
  4218. *
  4219. * Return values (usually ignored):
  4220. * NET_RX_SUCCESS: no congestion
  4221. * NET_RX_DROP: packet was dropped
  4222. */
  4223. int netif_receive_skb_core(struct sk_buff *skb)
  4224. {
  4225. int ret;
  4226. rcu_read_lock();
  4227. ret = __netif_receive_skb_one_core(skb, false);
  4228. rcu_read_unlock();
  4229. return ret;
  4230. }
  4231. EXPORT_SYMBOL(netif_receive_skb_core);
  4232. static inline void __netif_receive_skb_list_ptype(struct list_head *head,
  4233. struct packet_type *pt_prev,
  4234. struct net_device *orig_dev)
  4235. {
  4236. struct sk_buff *skb, *next;
  4237. if (!pt_prev)
  4238. return;
  4239. if (list_empty(head))
  4240. return;
  4241. if (pt_prev->list_func != NULL)
  4242. pt_prev->list_func(head, pt_prev, orig_dev);
  4243. else
  4244. list_for_each_entry_safe(skb, next, head, list) {
  4245. skb_list_del_init(skb);
  4246. pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  4247. }
  4248. }
  4249. static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
  4250. {
  4251. /* Fast-path assumptions:
  4252. * - There is no RX handler.
  4253. * - Only one packet_type matches.
  4254. * If either of these fails, we will end up doing some per-packet
  4255. * processing in-line, then handling the 'last ptype' for the whole
  4256. * sublist. This can't cause out-of-order delivery to any single ptype,
  4257. * because the 'last ptype' must be constant across the sublist, and all
  4258. * other ptypes are handled per-packet.
  4259. */
  4260. /* Current (common) ptype of sublist */
  4261. struct packet_type *pt_curr = NULL;
  4262. /* Current (common) orig_dev of sublist */
  4263. struct net_device *od_curr = NULL;
  4264. struct list_head sublist;
  4265. struct sk_buff *skb, *next;
  4266. INIT_LIST_HEAD(&sublist);
  4267. list_for_each_entry_safe(skb, next, head, list) {
  4268. struct net_device *orig_dev = skb->dev;
  4269. struct packet_type *pt_prev = NULL;
  4270. skb_list_del_init(skb);
  4271. __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
  4272. if (!pt_prev)
  4273. continue;
  4274. if (pt_curr != pt_prev || od_curr != orig_dev) {
  4275. /* dispatch old sublist */
  4276. __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
  4277. /* start new sublist */
  4278. INIT_LIST_HEAD(&sublist);
  4279. pt_curr = pt_prev;
  4280. od_curr = orig_dev;
  4281. }
  4282. list_add_tail(&skb->list, &sublist);
  4283. }
  4284. /* dispatch final sublist */
  4285. __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
  4286. }
  4287. static int __netif_receive_skb(struct sk_buff *skb)
  4288. {
  4289. int ret;
  4290. if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
  4291. unsigned int noreclaim_flag;
  4292. /*
  4293. * PFMEMALLOC skbs are special, they should
  4294. * - be delivered to SOCK_MEMALLOC sockets only
  4295. * - stay away from userspace
  4296. * - have bounded memory usage
  4297. *
  4298. * Use PF_MEMALLOC as this saves us from propagating the allocation
  4299. * context down to all allocation sites.
  4300. */
  4301. noreclaim_flag = memalloc_noreclaim_save();
  4302. ret = __netif_receive_skb_one_core(skb, true);
  4303. memalloc_noreclaim_restore(noreclaim_flag);
  4304. } else
  4305. ret = __netif_receive_skb_one_core(skb, false);
  4306. return ret;
  4307. }
  4308. static void __netif_receive_skb_list(struct list_head *head)
  4309. {
  4310. unsigned long noreclaim_flag = 0;
  4311. struct sk_buff *skb, *next;
  4312. bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
  4313. list_for_each_entry_safe(skb, next, head, list) {
  4314. if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
  4315. struct list_head sublist;
  4316. /* Handle the previous sublist */
  4317. list_cut_before(&sublist, head, &skb->list);
  4318. if (!list_empty(&sublist))
  4319. __netif_receive_skb_list_core(&sublist, pfmemalloc);
  4320. pfmemalloc = !pfmemalloc;
  4321. /* See comments in __netif_receive_skb */
  4322. if (pfmemalloc)
  4323. noreclaim_flag = memalloc_noreclaim_save();
  4324. else
  4325. memalloc_noreclaim_restore(noreclaim_flag);
  4326. }
  4327. }
  4328. /* Handle the remaining sublist */
  4329. if (!list_empty(head))
  4330. __netif_receive_skb_list_core(head, pfmemalloc);
  4331. /* Restore pflags */
  4332. if (pfmemalloc)
  4333. memalloc_noreclaim_restore(noreclaim_flag);
  4334. }
  4335. static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
  4336. {
  4337. struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
  4338. struct bpf_prog *new = xdp->prog;
  4339. int ret = 0;
  4340. switch (xdp->command) {
  4341. case XDP_SETUP_PROG:
  4342. rcu_assign_pointer(dev->xdp_prog, new);
  4343. if (old)
  4344. bpf_prog_put(old);
  4345. if (old && !new) {
  4346. static_branch_dec(&generic_xdp_needed_key);
  4347. } else if (new && !old) {
  4348. static_branch_inc(&generic_xdp_needed_key);
  4349. dev_disable_lro(dev);
  4350. dev_disable_gro_hw(dev);
  4351. }
  4352. break;
  4353. case XDP_QUERY_PROG:
  4354. xdp->prog_id = old ? old->aux->id : 0;
  4355. break;
  4356. default:
  4357. ret = -EINVAL;
  4358. break;
  4359. }
  4360. return ret;
  4361. }
  4362. static int netif_receive_skb_internal(struct sk_buff *skb)
  4363. {
  4364. int ret;
  4365. net_timestamp_check(netdev_tstamp_prequeue, skb);
  4366. if (skb_defer_rx_timestamp(skb))
  4367. return NET_RX_SUCCESS;
  4368. rcu_read_lock();
  4369. #ifdef CONFIG_RPS
  4370. if (static_key_false(&rps_needed)) {
  4371. struct rps_dev_flow voidflow, *rflow = &voidflow;
  4372. int cpu = get_rps_cpu(skb->dev, skb, &rflow);
  4373. if (cpu >= 0) {
  4374. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  4375. rcu_read_unlock();
  4376. return ret;
  4377. }
  4378. }
  4379. #endif
  4380. ret = __netif_receive_skb(skb);
  4381. rcu_read_unlock();
  4382. return ret;
  4383. }
  4384. static void netif_receive_skb_list_internal(struct list_head *head)
  4385. {
  4386. struct sk_buff *skb, *next;
  4387. struct list_head sublist;
  4388. INIT_LIST_HEAD(&sublist);
  4389. list_for_each_entry_safe(skb, next, head, list) {
  4390. net_timestamp_check(netdev_tstamp_prequeue, skb);
  4391. skb_list_del_init(skb);
  4392. if (!skb_defer_rx_timestamp(skb))
  4393. list_add_tail(&skb->list, &sublist);
  4394. }
  4395. list_splice_init(&sublist, head);
  4396. rcu_read_lock();
  4397. #ifdef CONFIG_RPS
  4398. if (static_key_false(&rps_needed)) {
  4399. list_for_each_entry_safe(skb, next, head, list) {
  4400. struct rps_dev_flow voidflow, *rflow = &voidflow;
  4401. int cpu = get_rps_cpu(skb->dev, skb, &rflow);
  4402. if (cpu >= 0) {
  4403. /* Will be handled, remove from list */
  4404. skb_list_del_init(skb);
  4405. enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  4406. }
  4407. }
  4408. }
  4409. #endif
  4410. __netif_receive_skb_list(head);
  4411. rcu_read_unlock();
  4412. }
  4413. /**
  4414. * netif_receive_skb - process receive buffer from network
  4415. * @skb: buffer to process
  4416. *
  4417. * netif_receive_skb() is the main receive data processing function.
  4418. * It always succeeds. The buffer may be dropped during processing
  4419. * for congestion control or by the protocol layers.
  4420. *
  4421. * This function may only be called from softirq context and interrupts
  4422. * should be enabled.
  4423. *
  4424. * Return values (usually ignored):
  4425. * NET_RX_SUCCESS: no congestion
  4426. * NET_RX_DROP: packet was dropped
  4427. */
  4428. int netif_receive_skb(struct sk_buff *skb)
  4429. {
  4430. trace_netif_receive_skb_entry(skb);
  4431. return netif_receive_skb_internal(skb);
  4432. }
  4433. EXPORT_SYMBOL(netif_receive_skb);
  4434. /**
  4435. * netif_receive_skb_list - process many receive buffers from network
  4436. * @head: list of skbs to process.
  4437. *
  4438. * Since return value of netif_receive_skb() is normally ignored, and
  4439. * wouldn't be meaningful for a list, this function returns void.
  4440. *
  4441. * This function may only be called from softirq context and interrupts
  4442. * should be enabled.
  4443. */
  4444. void netif_receive_skb_list(struct list_head *head)
  4445. {
  4446. struct sk_buff *skb;
  4447. if (list_empty(head))
  4448. return;
  4449. list_for_each_entry(skb, head, list)
  4450. trace_netif_receive_skb_list_entry(skb);
  4451. netif_receive_skb_list_internal(head);
  4452. }
  4453. EXPORT_SYMBOL(netif_receive_skb_list);
  4454. DEFINE_PER_CPU(struct work_struct, flush_works);
  4455. /* Network device is going away, flush any packets still pending */
  4456. static void flush_backlog(struct work_struct *work)
  4457. {
  4458. struct sk_buff *skb, *tmp;
  4459. struct softnet_data *sd;
  4460. local_bh_disable();
  4461. sd = this_cpu_ptr(&softnet_data);
  4462. local_irq_disable();
  4463. rps_lock(sd);
  4464. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  4465. if (skb->dev->reg_state == NETREG_UNREGISTERING) {
  4466. __skb_unlink(skb, &sd->input_pkt_queue);
  4467. kfree_skb(skb);
  4468. input_queue_head_incr(sd);
  4469. }
  4470. }
  4471. rps_unlock(sd);
  4472. local_irq_enable();
  4473. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  4474. if (skb->dev->reg_state == NETREG_UNREGISTERING) {
  4475. __skb_unlink(skb, &sd->process_queue);
  4476. kfree_skb(skb);
  4477. input_queue_head_incr(sd);
  4478. }
  4479. }
  4480. local_bh_enable();
  4481. }
  4482. static void flush_all_backlogs(void)
  4483. {
  4484. unsigned int cpu;
  4485. get_online_cpus();
  4486. for_each_online_cpu(cpu)
  4487. queue_work_on(cpu, system_highpri_wq,
  4488. per_cpu_ptr(&flush_works, cpu));
  4489. for_each_online_cpu(cpu)
  4490. flush_work(per_cpu_ptr(&flush_works, cpu));
  4491. put_online_cpus();
  4492. }
  4493. static int napi_gro_complete(struct sk_buff *skb)
  4494. {
  4495. struct packet_offload *ptype;
  4496. __be16 type = skb->protocol;
  4497. struct list_head *head = &offload_base;
  4498. int err = -ENOENT;
  4499. BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
  4500. if (NAPI_GRO_CB(skb)->count == 1) {
  4501. skb_shinfo(skb)->gso_size = 0;
  4502. goto out;
  4503. }
  4504. rcu_read_lock();
  4505. list_for_each_entry_rcu(ptype, head, list) {
  4506. if (ptype->type != type || !ptype->callbacks.gro_complete)
  4507. continue;
  4508. err = ptype->callbacks.gro_complete(skb, 0);
  4509. break;
  4510. }
  4511. rcu_read_unlock();
  4512. if (err) {
  4513. WARN_ON(&ptype->list == head);
  4514. kfree_skb(skb);
  4515. return NET_RX_SUCCESS;
  4516. }
  4517. out:
  4518. return netif_receive_skb_internal(skb);
  4519. }
  4520. static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
  4521. bool flush_old)
  4522. {
  4523. struct list_head *head = &napi->gro_hash[index].list;
  4524. struct sk_buff *skb, *p;
  4525. list_for_each_entry_safe_reverse(skb, p, head, list) {
  4526. if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
  4527. return;
  4528. list_del(&skb->list);
  4529. skb->next = NULL;
  4530. napi_gro_complete(skb);
  4531. napi->gro_hash[index].count--;
  4532. }
  4533. if (!napi->gro_hash[index].count)
  4534. __clear_bit(index, &napi->gro_bitmask);
  4535. }
  4536. /* napi->gro_hash[].list contains packets ordered by age.
  4537. * youngest packets at the head of it.
  4538. * Complete skbs in reverse order to reduce latencies.
  4539. */
  4540. void napi_gro_flush(struct napi_struct *napi, bool flush_old)
  4541. {
  4542. u32 i;
  4543. for (i = 0; i < GRO_HASH_BUCKETS; i++) {
  4544. if (test_bit(i, &napi->gro_bitmask))
  4545. __napi_gro_flush_chain(napi, i, flush_old);
  4546. }
  4547. }
  4548. EXPORT_SYMBOL(napi_gro_flush);
  4549. static struct list_head *gro_list_prepare(struct napi_struct *napi,
  4550. struct sk_buff *skb)
  4551. {
  4552. unsigned int maclen = skb->dev->hard_header_len;
  4553. u32 hash = skb_get_hash_raw(skb);
  4554. struct list_head *head;
  4555. struct sk_buff *p;
  4556. head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
  4557. list_for_each_entry(p, head, list) {
  4558. unsigned long diffs;
  4559. NAPI_GRO_CB(p)->flush = 0;
  4560. if (hash != skb_get_hash_raw(p)) {
  4561. NAPI_GRO_CB(p)->same_flow = 0;
  4562. continue;
  4563. }
  4564. diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
  4565. diffs |= p->vlan_tci ^ skb->vlan_tci;
  4566. diffs |= skb_metadata_dst_cmp(p, skb);
  4567. diffs |= skb_metadata_differs(p, skb);
  4568. if (maclen == ETH_HLEN)
  4569. diffs |= compare_ether_header(skb_mac_header(p),
  4570. skb_mac_header(skb));
  4571. else if (!diffs)
  4572. diffs = memcmp(skb_mac_header(p),
  4573. skb_mac_header(skb),
  4574. maclen);
  4575. NAPI_GRO_CB(p)->same_flow = !diffs;
  4576. }
  4577. return head;
  4578. }
  4579. static void skb_gro_reset_offset(struct sk_buff *skb)
  4580. {
  4581. const struct skb_shared_info *pinfo = skb_shinfo(skb);
  4582. const skb_frag_t *frag0 = &pinfo->frags[0];
  4583. NAPI_GRO_CB(skb)->data_offset = 0;
  4584. NAPI_GRO_CB(skb)->frag0 = NULL;
  4585. NAPI_GRO_CB(skb)->frag0_len = 0;
  4586. if (skb_mac_header(skb) == skb_tail_pointer(skb) &&
  4587. pinfo->nr_frags &&
  4588. !PageHighMem(skb_frag_page(frag0))) {
  4589. NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
  4590. NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
  4591. skb_frag_size(frag0),
  4592. skb->end - skb->tail);
  4593. }
  4594. }
  4595. static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
  4596. {
  4597. struct skb_shared_info *pinfo = skb_shinfo(skb);
  4598. BUG_ON(skb->end - skb->tail < grow);
  4599. memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
  4600. skb->data_len -= grow;
  4601. skb->tail += grow;
  4602. pinfo->frags[0].page_offset += grow;
  4603. skb_frag_size_sub(&pinfo->frags[0], grow);
  4604. if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
  4605. skb_frag_unref(skb, 0);
  4606. memmove(pinfo->frags, pinfo->frags + 1,
  4607. --pinfo->nr_frags * sizeof(pinfo->frags[0]));
  4608. }
  4609. }
  4610. static void gro_flush_oldest(struct list_head *head)
  4611. {
  4612. struct sk_buff *oldest;
  4613. oldest = list_last_entry(head, struct sk_buff, list);
  4614. /* We are called with head length >= MAX_GRO_SKBS, so this is
  4615. * impossible.
  4616. */
  4617. if (WARN_ON_ONCE(!oldest))
  4618. return;
  4619. /* Do not adjust napi->gro_hash[].count, caller is adding a new
  4620. * SKB to the chain.
  4621. */
  4622. list_del(&oldest->list);
  4623. oldest->next = NULL;
  4624. napi_gro_complete(oldest);
  4625. }
  4626. static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  4627. {
  4628. u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
  4629. struct list_head *head = &offload_base;
  4630. struct packet_offload *ptype;
  4631. __be16 type = skb->protocol;
  4632. struct list_head *gro_head;
  4633. struct sk_buff *pp = NULL;
  4634. enum gro_result ret;
  4635. int same_flow;
  4636. int grow;
  4637. if (netif_elide_gro(skb->dev))
  4638. goto normal;
  4639. gro_head = gro_list_prepare(napi, skb);
  4640. rcu_read_lock();
  4641. list_for_each_entry_rcu(ptype, head, list) {
  4642. if (ptype->type != type || !ptype->callbacks.gro_receive)
  4643. continue;
  4644. skb_set_network_header(skb, skb_gro_offset(skb));
  4645. skb_reset_mac_len(skb);
  4646. NAPI_GRO_CB(skb)->same_flow = 0;
  4647. NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
  4648. NAPI_GRO_CB(skb)->free = 0;
  4649. NAPI_GRO_CB(skb)->encap_mark = 0;
  4650. NAPI_GRO_CB(skb)->recursion_counter = 0;
  4651. NAPI_GRO_CB(skb)->is_fou = 0;
  4652. NAPI_GRO_CB(skb)->is_atomic = 1;
  4653. NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
  4654. /* Setup for GRO checksum validation */
  4655. switch (skb->ip_summed) {
  4656. case CHECKSUM_COMPLETE:
  4657. NAPI_GRO_CB(skb)->csum = skb->csum;
  4658. NAPI_GRO_CB(skb)->csum_valid = 1;
  4659. NAPI_GRO_CB(skb)->csum_cnt = 0;
  4660. break;
  4661. case CHECKSUM_UNNECESSARY:
  4662. NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
  4663. NAPI_GRO_CB(skb)->csum_valid = 0;
  4664. break;
  4665. default:
  4666. NAPI_GRO_CB(skb)->csum_cnt = 0;
  4667. NAPI_GRO_CB(skb)->csum_valid = 0;
  4668. }
  4669. pp = ptype->callbacks.gro_receive(gro_head, skb);
  4670. break;
  4671. }
  4672. rcu_read_unlock();
  4673. if (&ptype->list == head)
  4674. goto normal;
  4675. if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) {
  4676. ret = GRO_CONSUMED;
  4677. goto ok;
  4678. }
  4679. same_flow = NAPI_GRO_CB(skb)->same_flow;
  4680. ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
  4681. if (pp) {
  4682. list_del(&pp->list);
  4683. pp->next = NULL;
  4684. napi_gro_complete(pp);
  4685. napi->gro_hash[hash].count--;
  4686. }
  4687. if (same_flow)
  4688. goto ok;
  4689. if (NAPI_GRO_CB(skb)->flush)
  4690. goto normal;
  4691. if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
  4692. gro_flush_oldest(gro_head);
  4693. } else {
  4694. napi->gro_hash[hash].count++;
  4695. }
  4696. NAPI_GRO_CB(skb)->count = 1;
  4697. NAPI_GRO_CB(skb)->age = jiffies;
  4698. NAPI_GRO_CB(skb)->last = skb;
  4699. skb_shinfo(skb)->gso_size = skb_gro_len(skb);
  4700. list_add(&skb->list, gro_head);
  4701. ret = GRO_HELD;
  4702. pull:
  4703. grow = skb_gro_offset(skb) - skb_headlen(skb);
  4704. if (grow > 0)
  4705. gro_pull_from_frag0(skb, grow);
  4706. ok:
  4707. if (napi->gro_hash[hash].count) {
  4708. if (!test_bit(hash, &napi->gro_bitmask))
  4709. __set_bit(hash, &napi->gro_bitmask);
  4710. } else if (test_bit(hash, &napi->gro_bitmask)) {
  4711. __clear_bit(hash, &napi->gro_bitmask);
  4712. }
  4713. return ret;
  4714. normal:
  4715. ret = GRO_NORMAL;
  4716. goto pull;
  4717. }
  4718. struct packet_offload *gro_find_receive_by_type(__be16 type)
  4719. {
  4720. struct list_head *offload_head = &offload_base;
  4721. struct packet_offload *ptype;
  4722. list_for_each_entry_rcu(ptype, offload_head, list) {
  4723. if (ptype->type != type || !ptype->callbacks.gro_receive)
  4724. continue;
  4725. return ptype;
  4726. }
  4727. return NULL;
  4728. }
  4729. EXPORT_SYMBOL(gro_find_receive_by_type);
  4730. struct packet_offload *gro_find_complete_by_type(__be16 type)
  4731. {
  4732. struct list_head *offload_head = &offload_base;
  4733. struct packet_offload *ptype;
  4734. list_for_each_entry_rcu(ptype, offload_head, list) {
  4735. if (ptype->type != type || !ptype->callbacks.gro_complete)
  4736. continue;
  4737. return ptype;
  4738. }
  4739. return NULL;
  4740. }
  4741. EXPORT_SYMBOL(gro_find_complete_by_type);
  4742. static void napi_skb_free_stolen_head(struct sk_buff *skb)
  4743. {
  4744. skb_dst_drop(skb);
  4745. secpath_reset(skb);
  4746. kmem_cache_free(skbuff_head_cache, skb);
  4747. }
  4748. static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
  4749. {
  4750. switch (ret) {
  4751. case GRO_NORMAL:
  4752. if (netif_receive_skb_internal(skb))
  4753. ret = GRO_DROP;
  4754. break;
  4755. case GRO_DROP:
  4756. kfree_skb(skb);
  4757. break;
  4758. case GRO_MERGED_FREE:
  4759. if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
  4760. napi_skb_free_stolen_head(skb);
  4761. else
  4762. __kfree_skb(skb);
  4763. break;
  4764. case GRO_HELD:
  4765. case GRO_MERGED:
  4766. case GRO_CONSUMED:
  4767. break;
  4768. }
  4769. return ret;
  4770. }
  4771. gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  4772. {
  4773. skb_mark_napi_id(skb, napi);
  4774. trace_napi_gro_receive_entry(skb);
  4775. skb_gro_reset_offset(skb);
  4776. return napi_skb_finish(dev_gro_receive(napi, skb), skb);
  4777. }
  4778. EXPORT_SYMBOL(napi_gro_receive);
  4779. static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
  4780. {
  4781. if (unlikely(skb->pfmemalloc)) {
  4782. consume_skb(skb);
  4783. return;
  4784. }
  4785. __skb_pull(skb, skb_headlen(skb));
  4786. /* restore the reserve we had after netdev_alloc_skb_ip_align() */
  4787. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
  4788. skb->vlan_tci = 0;
  4789. skb->dev = napi->dev;
  4790. skb->skb_iif = 0;
  4791. /* eth_type_trans() assumes pkt_type is PACKET_HOST */
  4792. skb->pkt_type = PACKET_HOST;
  4793. skb->encapsulation = 0;
  4794. skb_shinfo(skb)->gso_type = 0;
  4795. skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
  4796. secpath_reset(skb);
  4797. napi->skb = skb;
  4798. }
  4799. struct sk_buff *napi_get_frags(struct napi_struct *napi)
  4800. {
  4801. struct sk_buff *skb = napi->skb;
  4802. if (!skb) {
  4803. skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
  4804. if (skb) {
  4805. napi->skb = skb;
  4806. skb_mark_napi_id(skb, napi);
  4807. }
  4808. }
  4809. return skb;
  4810. }
  4811. EXPORT_SYMBOL(napi_get_frags);
  4812. static gro_result_t napi_frags_finish(struct napi_struct *napi,
  4813. struct sk_buff *skb,
  4814. gro_result_t ret)
  4815. {
  4816. switch (ret) {
  4817. case GRO_NORMAL:
  4818. case GRO_HELD:
  4819. __skb_push(skb, ETH_HLEN);
  4820. skb->protocol = eth_type_trans(skb, skb->dev);
  4821. if (ret == GRO_NORMAL && netif_receive_skb_internal(skb))
  4822. ret = GRO_DROP;
  4823. break;
  4824. case GRO_DROP:
  4825. napi_reuse_skb(napi, skb);
  4826. break;
  4827. case GRO_MERGED_FREE:
  4828. if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
  4829. napi_skb_free_stolen_head(skb);
  4830. else
  4831. napi_reuse_skb(napi, skb);
  4832. break;
  4833. case GRO_MERGED:
  4834. case GRO_CONSUMED:
  4835. break;
  4836. }
  4837. return ret;
  4838. }
  4839. /* Upper GRO stack assumes network header starts at gro_offset=0
  4840. * Drivers could call both napi_gro_frags() and napi_gro_receive()
  4841. * We copy ethernet header into skb->data to have a common layout.
  4842. */
  4843. static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
  4844. {
  4845. struct sk_buff *skb = napi->skb;
  4846. const struct ethhdr *eth;
  4847. unsigned int hlen = sizeof(*eth);
  4848. napi->skb = NULL;
  4849. skb_reset_mac_header(skb);
  4850. skb_gro_reset_offset(skb);
  4851. if (unlikely(skb_gro_header_hard(skb, hlen))) {
  4852. eth = skb_gro_header_slow(skb, hlen, 0);
  4853. if (unlikely(!eth)) {
  4854. net_warn_ratelimited("%s: dropping impossible skb from %s\n",
  4855. __func__, napi->dev->name);
  4856. napi_reuse_skb(napi, skb);
  4857. return NULL;
  4858. }
  4859. } else {
  4860. eth = (const struct ethhdr *)skb->data;
  4861. gro_pull_from_frag0(skb, hlen);
  4862. NAPI_GRO_CB(skb)->frag0 += hlen;
  4863. NAPI_GRO_CB(skb)->frag0_len -= hlen;
  4864. }
  4865. __skb_pull(skb, hlen);
  4866. /*
  4867. * This works because the only protocols we care about don't require
  4868. * special handling.
  4869. * We'll fix it up properly in napi_frags_finish()
  4870. */
  4871. skb->protocol = eth->h_proto;
  4872. return skb;
  4873. }
  4874. gro_result_t napi_gro_frags(struct napi_struct *napi)
  4875. {
  4876. struct sk_buff *skb = napi_frags_skb(napi);
  4877. if (!skb)
  4878. return GRO_DROP;
  4879. trace_napi_gro_frags_entry(skb);
  4880. return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
  4881. }
  4882. EXPORT_SYMBOL(napi_gro_frags);
  4883. /* Compute the checksum from gro_offset and return the folded value
  4884. * after adding in any pseudo checksum.
  4885. */
  4886. __sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
  4887. {
  4888. __wsum wsum;
  4889. __sum16 sum;
  4890. wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
  4891. /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
  4892. sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
  4893. if (likely(!sum)) {
  4894. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
  4895. !skb->csum_complete_sw)
  4896. netdev_rx_csum_fault(skb->dev);
  4897. }
  4898. NAPI_GRO_CB(skb)->csum = wsum;
  4899. NAPI_GRO_CB(skb)->csum_valid = 1;
  4900. return sum;
  4901. }
  4902. EXPORT_SYMBOL(__skb_gro_checksum_complete);
  4903. static void net_rps_send_ipi(struct softnet_data *remsd)
  4904. {
  4905. #ifdef CONFIG_RPS
  4906. while (remsd) {
  4907. struct softnet_data *next = remsd->rps_ipi_next;
  4908. if (cpu_online(remsd->cpu))
  4909. smp_call_function_single_async(remsd->cpu, &remsd->csd);
  4910. remsd = next;
  4911. }
  4912. #endif
  4913. }
  4914. /*
  4915. * net_rps_action_and_irq_enable sends any pending IPI's for rps.
  4916. * Note: called with local irq disabled, but exits with local irq enabled.
  4917. */
  4918. static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  4919. {
  4920. #ifdef CONFIG_RPS
  4921. struct softnet_data *remsd = sd->rps_ipi_list;
  4922. if (remsd) {
  4923. sd->rps_ipi_list = NULL;
  4924. local_irq_enable();
  4925. /* Send pending IPI's to kick RPS processing on remote cpus. */
  4926. net_rps_send_ipi(remsd);
  4927. } else
  4928. #endif
  4929. local_irq_enable();
  4930. }
  4931. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  4932. {
  4933. #ifdef CONFIG_RPS
  4934. return sd->rps_ipi_list != NULL;
  4935. #else
  4936. return false;
  4937. #endif
  4938. }
  4939. static int process_backlog(struct napi_struct *napi, int quota)
  4940. {
  4941. struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
  4942. bool again = true;
  4943. int work = 0;
  4944. /* Check if we have pending ipi, its better to send them now,
  4945. * not waiting net_rx_action() end.
  4946. */
  4947. if (sd_has_rps_ipi_waiting(sd)) {
  4948. local_irq_disable();
  4949. net_rps_action_and_irq_enable(sd);
  4950. }
  4951. napi->weight = dev_rx_weight;
  4952. while (again) {
  4953. struct sk_buff *skb;
  4954. while ((skb = __skb_dequeue(&sd->process_queue))) {
  4955. rcu_read_lock();
  4956. __netif_receive_skb(skb);
  4957. rcu_read_unlock();
  4958. input_queue_head_incr(sd);
  4959. if (++work >= quota)
  4960. return work;
  4961. }
  4962. local_irq_disable();
  4963. rps_lock(sd);
  4964. if (skb_queue_empty(&sd->input_pkt_queue)) {
  4965. /*
  4966. * Inline a custom version of __napi_complete().
  4967. * only current cpu owns and manipulates this napi,
  4968. * and NAPI_STATE_SCHED is the only possible flag set
  4969. * on backlog.
  4970. * We can use a plain write instead of clear_bit(),
  4971. * and we dont need an smp_mb() memory barrier.
  4972. */
  4973. napi->state = 0;
  4974. again = false;
  4975. } else {
  4976. skb_queue_splice_tail_init(&sd->input_pkt_queue,
  4977. &sd->process_queue);
  4978. }
  4979. rps_unlock(sd);
  4980. local_irq_enable();
  4981. }
  4982. return work;
  4983. }
  4984. /**
  4985. * __napi_schedule - schedule for receive
  4986. * @n: entry to schedule
  4987. *
  4988. * The entry's receive function will be scheduled to run.
  4989. * Consider using __napi_schedule_irqoff() if hard irqs are masked.
  4990. */
  4991. void __napi_schedule(struct napi_struct *n)
  4992. {
  4993. unsigned long flags;
  4994. local_irq_save(flags);
  4995. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  4996. local_irq_restore(flags);
  4997. }
  4998. EXPORT_SYMBOL(__napi_schedule);
  4999. /**
  5000. * napi_schedule_prep - check if napi can be scheduled
  5001. * @n: napi context
  5002. *
  5003. * Test if NAPI routine is already running, and if not mark
  5004. * it as running. This is used as a condition variable
  5005. * insure only one NAPI poll instance runs. We also make
  5006. * sure there is no pending NAPI disable.
  5007. */
  5008. bool napi_schedule_prep(struct napi_struct *n)
  5009. {
  5010. unsigned long val, new;
  5011. do {
  5012. val = READ_ONCE(n->state);
  5013. if (unlikely(val & NAPIF_STATE_DISABLE))
  5014. return false;
  5015. new = val | NAPIF_STATE_SCHED;
  5016. /* Sets STATE_MISSED bit if STATE_SCHED was already set
  5017. * This was suggested by Alexander Duyck, as compiler
  5018. * emits better code than :
  5019. * if (val & NAPIF_STATE_SCHED)
  5020. * new |= NAPIF_STATE_MISSED;
  5021. */
  5022. new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
  5023. NAPIF_STATE_MISSED;
  5024. } while (cmpxchg(&n->state, val, new) != val);
  5025. return !(val & NAPIF_STATE_SCHED);
  5026. }
  5027. EXPORT_SYMBOL(napi_schedule_prep);
  5028. /**
  5029. * __napi_schedule_irqoff - schedule for receive
  5030. * @n: entry to schedule
  5031. *
  5032. * Variant of __napi_schedule() assuming hard irqs are masked
  5033. */
  5034. void __napi_schedule_irqoff(struct napi_struct *n)
  5035. {
  5036. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  5037. }
  5038. EXPORT_SYMBOL(__napi_schedule_irqoff);
  5039. bool napi_complete_done(struct napi_struct *n, int work_done)
  5040. {
  5041. unsigned long flags, val, new;
  5042. /*
  5043. * 1) Don't let napi dequeue from the cpu poll list
  5044. * just in case its running on a different cpu.
  5045. * 2) If we are busy polling, do nothing here, we have
  5046. * the guarantee we will be called later.
  5047. */
  5048. if (unlikely(n->state & (NAPIF_STATE_NPSVC |
  5049. NAPIF_STATE_IN_BUSY_POLL)))
  5050. return false;
  5051. if (n->gro_bitmask) {
  5052. unsigned long timeout = 0;
  5053. if (work_done)
  5054. timeout = n->dev->gro_flush_timeout;
  5055. /* When the NAPI instance uses a timeout and keeps postponing
  5056. * it, we need to bound somehow the time packets are kept in
  5057. * the GRO layer
  5058. */
  5059. napi_gro_flush(n, !!timeout);
  5060. if (timeout)
  5061. hrtimer_start(&n->timer, ns_to_ktime(timeout),
  5062. HRTIMER_MODE_REL_PINNED);
  5063. }
  5064. if (unlikely(!list_empty(&n->poll_list))) {
  5065. /* If n->poll_list is not empty, we need to mask irqs */
  5066. local_irq_save(flags);
  5067. list_del_init(&n->poll_list);
  5068. local_irq_restore(flags);
  5069. }
  5070. do {
  5071. val = READ_ONCE(n->state);
  5072. WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
  5073. new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
  5074. /* If STATE_MISSED was set, leave STATE_SCHED set,
  5075. * because we will call napi->poll() one more time.
  5076. * This C code was suggested by Alexander Duyck to help gcc.
  5077. */
  5078. new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
  5079. NAPIF_STATE_SCHED;
  5080. } while (cmpxchg(&n->state, val, new) != val);
  5081. if (unlikely(val & NAPIF_STATE_MISSED)) {
  5082. __napi_schedule(n);
  5083. return false;
  5084. }
  5085. return true;
  5086. }
  5087. EXPORT_SYMBOL(napi_complete_done);
  5088. /* must be called under rcu_read_lock(), as we dont take a reference */
  5089. static struct napi_struct *napi_by_id(unsigned int napi_id)
  5090. {
  5091. unsigned int hash = napi_id % HASH_SIZE(napi_hash);
  5092. struct napi_struct *napi;
  5093. hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
  5094. if (napi->napi_id == napi_id)
  5095. return napi;
  5096. return NULL;
  5097. }
  5098. #if defined(CONFIG_NET_RX_BUSY_POLL)
  5099. #define BUSY_POLL_BUDGET 8
  5100. static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
  5101. {
  5102. int rc;
  5103. /* Busy polling means there is a high chance device driver hard irq
  5104. * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
  5105. * set in napi_schedule_prep().
  5106. * Since we are about to call napi->poll() once more, we can safely
  5107. * clear NAPI_STATE_MISSED.
  5108. *
  5109. * Note: x86 could use a single "lock and ..." instruction
  5110. * to perform these two clear_bit()
  5111. */
  5112. clear_bit(NAPI_STATE_MISSED, &napi->state);
  5113. clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
  5114. local_bh_disable();
  5115. /* All we really want here is to re-enable device interrupts.
  5116. * Ideally, a new ndo_busy_poll_stop() could avoid another round.
  5117. */
  5118. rc = napi->poll(napi, BUSY_POLL_BUDGET);
  5119. trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
  5120. netpoll_poll_unlock(have_poll_lock);
  5121. if (rc == BUSY_POLL_BUDGET)
  5122. __napi_schedule(napi);
  5123. local_bh_enable();
  5124. }
  5125. void napi_busy_loop(unsigned int napi_id,
  5126. bool (*loop_end)(void *, unsigned long),
  5127. void *loop_end_arg)
  5128. {
  5129. unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
  5130. int (*napi_poll)(struct napi_struct *napi, int budget);
  5131. void *have_poll_lock = NULL;
  5132. struct napi_struct *napi;
  5133. restart:
  5134. napi_poll = NULL;
  5135. rcu_read_lock();
  5136. napi = napi_by_id(napi_id);
  5137. if (!napi)
  5138. goto out;
  5139. preempt_disable();
  5140. for (;;) {
  5141. int work = 0;
  5142. local_bh_disable();
  5143. if (!napi_poll) {
  5144. unsigned long val = READ_ONCE(napi->state);
  5145. /* If multiple threads are competing for this napi,
  5146. * we avoid dirtying napi->state as much as we can.
  5147. */
  5148. if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
  5149. NAPIF_STATE_IN_BUSY_POLL))
  5150. goto count;
  5151. if (cmpxchg(&napi->state, val,
  5152. val | NAPIF_STATE_IN_BUSY_POLL |
  5153. NAPIF_STATE_SCHED) != val)
  5154. goto count;
  5155. have_poll_lock = netpoll_poll_lock(napi);
  5156. napi_poll = napi->poll;
  5157. }
  5158. work = napi_poll(napi, BUSY_POLL_BUDGET);
  5159. trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
  5160. count:
  5161. if (work > 0)
  5162. __NET_ADD_STATS(dev_net(napi->dev),
  5163. LINUX_MIB_BUSYPOLLRXPACKETS, work);
  5164. local_bh_enable();
  5165. if (!loop_end || loop_end(loop_end_arg, start_time))
  5166. break;
  5167. if (unlikely(need_resched())) {
  5168. if (napi_poll)
  5169. busy_poll_stop(napi, have_poll_lock);
  5170. preempt_enable();
  5171. rcu_read_unlock();
  5172. cond_resched();
  5173. if (loop_end(loop_end_arg, start_time))
  5174. return;
  5175. goto restart;
  5176. }
  5177. cpu_relax();
  5178. }
  5179. if (napi_poll)
  5180. busy_poll_stop(napi, have_poll_lock);
  5181. preempt_enable();
  5182. out:
  5183. rcu_read_unlock();
  5184. }
  5185. EXPORT_SYMBOL(napi_busy_loop);
  5186. #endif /* CONFIG_NET_RX_BUSY_POLL */
  5187. static void napi_hash_add(struct napi_struct *napi)
  5188. {
  5189. if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
  5190. test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
  5191. return;
  5192. spin_lock(&napi_hash_lock);
  5193. /* 0..NR_CPUS range is reserved for sender_cpu use */
  5194. do {
  5195. if (unlikely(++napi_gen_id < MIN_NAPI_ID))
  5196. napi_gen_id = MIN_NAPI_ID;
  5197. } while (napi_by_id(napi_gen_id));
  5198. napi->napi_id = napi_gen_id;
  5199. hlist_add_head_rcu(&napi->napi_hash_node,
  5200. &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
  5201. spin_unlock(&napi_hash_lock);
  5202. }
  5203. /* Warning : caller is responsible to make sure rcu grace period
  5204. * is respected before freeing memory containing @napi
  5205. */
  5206. bool napi_hash_del(struct napi_struct *napi)
  5207. {
  5208. bool rcu_sync_needed = false;
  5209. spin_lock(&napi_hash_lock);
  5210. if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
  5211. rcu_sync_needed = true;
  5212. hlist_del_rcu(&napi->napi_hash_node);
  5213. }
  5214. spin_unlock(&napi_hash_lock);
  5215. return rcu_sync_needed;
  5216. }
  5217. EXPORT_SYMBOL_GPL(napi_hash_del);
  5218. static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
  5219. {
  5220. struct napi_struct *napi;
  5221. napi = container_of(timer, struct napi_struct, timer);
  5222. /* Note : we use a relaxed variant of napi_schedule_prep() not setting
  5223. * NAPI_STATE_MISSED, since we do not react to a device IRQ.
  5224. */
  5225. if (napi->gro_bitmask && !napi_disable_pending(napi) &&
  5226. !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
  5227. __napi_schedule_irqoff(napi);
  5228. return HRTIMER_NORESTART;
  5229. }
  5230. static void init_gro_hash(struct napi_struct *napi)
  5231. {
  5232. int i;
  5233. for (i = 0; i < GRO_HASH_BUCKETS; i++) {
  5234. INIT_LIST_HEAD(&napi->gro_hash[i].list);
  5235. napi->gro_hash[i].count = 0;
  5236. }
  5237. napi->gro_bitmask = 0;
  5238. }
  5239. void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
  5240. int (*poll)(struct napi_struct *, int), int weight)
  5241. {
  5242. INIT_LIST_HEAD(&napi->poll_list);
  5243. hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
  5244. napi->timer.function = napi_watchdog;
  5245. init_gro_hash(napi);
  5246. napi->skb = NULL;
  5247. napi->poll = poll;
  5248. if (weight > NAPI_POLL_WEIGHT)
  5249. pr_err_once("netif_napi_add() called with weight %d on device %s\n",
  5250. weight, dev->name);
  5251. napi->weight = weight;
  5252. list_add(&napi->dev_list, &dev->napi_list);
  5253. napi->dev = dev;
  5254. #ifdef CONFIG_NETPOLL
  5255. napi->poll_owner = -1;
  5256. #endif
  5257. set_bit(NAPI_STATE_SCHED, &napi->state);
  5258. napi_hash_add(napi);
  5259. }
  5260. EXPORT_SYMBOL(netif_napi_add);
  5261. void napi_disable(struct napi_struct *n)
  5262. {
  5263. might_sleep();
  5264. set_bit(NAPI_STATE_DISABLE, &n->state);
  5265. while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
  5266. msleep(1);
  5267. while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
  5268. msleep(1);
  5269. hrtimer_cancel(&n->timer);
  5270. clear_bit(NAPI_STATE_DISABLE, &n->state);
  5271. }
  5272. EXPORT_SYMBOL(napi_disable);
  5273. static void flush_gro_hash(struct napi_struct *napi)
  5274. {
  5275. int i;
  5276. for (i = 0; i < GRO_HASH_BUCKETS; i++) {
  5277. struct sk_buff *skb, *n;
  5278. list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
  5279. kfree_skb(skb);
  5280. napi->gro_hash[i].count = 0;
  5281. }
  5282. }
  5283. /* Must be called in process context */
  5284. void netif_napi_del(struct napi_struct *napi)
  5285. {
  5286. might_sleep();
  5287. if (napi_hash_del(napi))
  5288. synchronize_net();
  5289. list_del_init(&napi->dev_list);
  5290. napi_free_frags(napi);
  5291. flush_gro_hash(napi);
  5292. napi->gro_bitmask = 0;
  5293. }
  5294. EXPORT_SYMBOL(netif_napi_del);
  5295. static int napi_poll(struct napi_struct *n, struct list_head *repoll)
  5296. {
  5297. void *have;
  5298. int work, weight;
  5299. list_del_init(&n->poll_list);
  5300. have = netpoll_poll_lock(n);
  5301. weight = n->weight;
  5302. /* This NAPI_STATE_SCHED test is for avoiding a race
  5303. * with netpoll's poll_napi(). Only the entity which
  5304. * obtains the lock and sees NAPI_STATE_SCHED set will
  5305. * actually make the ->poll() call. Therefore we avoid
  5306. * accidentally calling ->poll() when NAPI is not scheduled.
  5307. */
  5308. work = 0;
  5309. if (test_bit(NAPI_STATE_SCHED, &n->state)) {
  5310. work = n->poll(n, weight);
  5311. trace_napi_poll(n, work, weight);
  5312. }
  5313. WARN_ON_ONCE(work > weight);
  5314. if (likely(work < weight))
  5315. goto out_unlock;
  5316. /* Drivers must not modify the NAPI state if they
  5317. * consume the entire weight. In such cases this code
  5318. * still "owns" the NAPI instance and therefore can
  5319. * move the instance around on the list at-will.
  5320. */
  5321. if (unlikely(napi_disable_pending(n))) {
  5322. napi_complete(n);
  5323. goto out_unlock;
  5324. }
  5325. if (n->gro_bitmask) {
  5326. /* flush too old packets
  5327. * If HZ < 1000, flush all packets.
  5328. */
  5329. napi_gro_flush(n, HZ >= 1000);
  5330. }
  5331. /* Some drivers may have called napi_schedule
  5332. * prior to exhausting their budget.
  5333. */
  5334. if (unlikely(!list_empty(&n->poll_list))) {
  5335. pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
  5336. n->dev ? n->dev->name : "backlog");
  5337. goto out_unlock;
  5338. }
  5339. list_add_tail(&n->poll_list, repoll);
  5340. out_unlock:
  5341. netpoll_poll_unlock(have);
  5342. return work;
  5343. }
  5344. static __latent_entropy void net_rx_action(struct softirq_action *h)
  5345. {
  5346. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  5347. unsigned long time_limit = jiffies +
  5348. usecs_to_jiffies(netdev_budget_usecs);
  5349. int budget = netdev_budget;
  5350. LIST_HEAD(list);
  5351. LIST_HEAD(repoll);
  5352. local_irq_disable();
  5353. list_splice_init(&sd->poll_list, &list);
  5354. local_irq_enable();
  5355. for (;;) {
  5356. struct napi_struct *n;
  5357. if (list_empty(&list)) {
  5358. if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
  5359. goto out;
  5360. break;
  5361. }
  5362. n = list_first_entry(&list, struct napi_struct, poll_list);
  5363. budget -= napi_poll(n, &repoll);
  5364. /* If softirq window is exhausted then punt.
  5365. * Allow this to run for 2 jiffies since which will allow
  5366. * an average latency of 1.5/HZ.
  5367. */
  5368. if (unlikely(budget <= 0 ||
  5369. time_after_eq(jiffies, time_limit))) {
  5370. sd->time_squeeze++;
  5371. break;
  5372. }
  5373. }
  5374. local_irq_disable();
  5375. list_splice_tail_init(&sd->poll_list, &list);
  5376. list_splice_tail(&repoll, &list);
  5377. list_splice(&list, &sd->poll_list);
  5378. if (!list_empty(&sd->poll_list))
  5379. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  5380. net_rps_action_and_irq_enable(sd);
  5381. out:
  5382. __kfree_skb_flush();
  5383. }
  5384. struct netdev_adjacent {
  5385. struct net_device *dev;
  5386. /* upper master flag, there can only be one master device per list */
  5387. bool master;
  5388. /* counter for the number of times this device was added to us */
  5389. u16 ref_nr;
  5390. /* private field for the users */
  5391. void *private;
  5392. struct list_head list;
  5393. struct rcu_head rcu;
  5394. };
  5395. static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
  5396. struct list_head *adj_list)
  5397. {
  5398. struct netdev_adjacent *adj;
  5399. list_for_each_entry(adj, adj_list, list) {
  5400. if (adj->dev == adj_dev)
  5401. return adj;
  5402. }
  5403. return NULL;
  5404. }
  5405. static int __netdev_has_upper_dev(struct net_device *upper_dev, void *data)
  5406. {
  5407. struct net_device *dev = data;
  5408. return upper_dev == dev;
  5409. }
  5410. /**
  5411. * netdev_has_upper_dev - Check if device is linked to an upper device
  5412. * @dev: device
  5413. * @upper_dev: upper device to check
  5414. *
  5415. * Find out if a device is linked to specified upper device and return true
  5416. * in case it is. Note that this checks only immediate upper device,
  5417. * not through a complete stack of devices. The caller must hold the RTNL lock.
  5418. */
  5419. bool netdev_has_upper_dev(struct net_device *dev,
  5420. struct net_device *upper_dev)
  5421. {
  5422. ASSERT_RTNL();
  5423. return netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
  5424. upper_dev);
  5425. }
  5426. EXPORT_SYMBOL(netdev_has_upper_dev);
  5427. /**
  5428. * netdev_has_upper_dev_all - Check if device is linked to an upper device
  5429. * @dev: device
  5430. * @upper_dev: upper device to check
  5431. *
  5432. * Find out if a device is linked to specified upper device and return true
  5433. * in case it is. Note that this checks the entire upper device chain.
  5434. * The caller must hold rcu lock.
  5435. */
  5436. bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
  5437. struct net_device *upper_dev)
  5438. {
  5439. return !!netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
  5440. upper_dev);
  5441. }
  5442. EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
  5443. /**
  5444. * netdev_has_any_upper_dev - Check if device is linked to some device
  5445. * @dev: device
  5446. *
  5447. * Find out if a device is linked to an upper device and return true in case
  5448. * it is. The caller must hold the RTNL lock.
  5449. */
  5450. bool netdev_has_any_upper_dev(struct net_device *dev)
  5451. {
  5452. ASSERT_RTNL();
  5453. return !list_empty(&dev->adj_list.upper);
  5454. }
  5455. EXPORT_SYMBOL(netdev_has_any_upper_dev);
  5456. /**
  5457. * netdev_master_upper_dev_get - Get master upper device
  5458. * @dev: device
  5459. *
  5460. * Find a master upper device and return pointer to it or NULL in case
  5461. * it's not there. The caller must hold the RTNL lock.
  5462. */
  5463. struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
  5464. {
  5465. struct netdev_adjacent *upper;
  5466. ASSERT_RTNL();
  5467. if (list_empty(&dev->adj_list.upper))
  5468. return NULL;
  5469. upper = list_first_entry(&dev->adj_list.upper,
  5470. struct netdev_adjacent, list);
  5471. if (likely(upper->master))
  5472. return upper->dev;
  5473. return NULL;
  5474. }
  5475. EXPORT_SYMBOL(netdev_master_upper_dev_get);
  5476. /**
  5477. * netdev_has_any_lower_dev - Check if device is linked to some device
  5478. * @dev: device
  5479. *
  5480. * Find out if a device is linked to a lower device and return true in case
  5481. * it is. The caller must hold the RTNL lock.
  5482. */
  5483. static bool netdev_has_any_lower_dev(struct net_device *dev)
  5484. {
  5485. ASSERT_RTNL();
  5486. return !list_empty(&dev->adj_list.lower);
  5487. }
  5488. void *netdev_adjacent_get_private(struct list_head *adj_list)
  5489. {
  5490. struct netdev_adjacent *adj;
  5491. adj = list_entry(adj_list, struct netdev_adjacent, list);
  5492. return adj->private;
  5493. }
  5494. EXPORT_SYMBOL(netdev_adjacent_get_private);
  5495. /**
  5496. * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
  5497. * @dev: device
  5498. * @iter: list_head ** of the current position
  5499. *
  5500. * Gets the next device from the dev's upper list, starting from iter
  5501. * position. The caller must hold RCU read lock.
  5502. */
  5503. struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
  5504. struct list_head **iter)
  5505. {
  5506. struct netdev_adjacent *upper;
  5507. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
  5508. upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  5509. if (&upper->list == &dev->adj_list.upper)
  5510. return NULL;
  5511. *iter = &upper->list;
  5512. return upper->dev;
  5513. }
  5514. EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
  5515. static struct net_device *netdev_next_upper_dev(struct net_device *dev,
  5516. struct list_head **iter)
  5517. {
  5518. struct netdev_adjacent *upper;
  5519. upper = list_entry((*iter)->next, struct netdev_adjacent, list);
  5520. if (&upper->list == &dev->adj_list.upper)
  5521. return NULL;
  5522. *iter = &upper->list;
  5523. return upper->dev;
  5524. }
  5525. static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
  5526. struct list_head **iter)
  5527. {
  5528. struct netdev_adjacent *upper;
  5529. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
  5530. upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  5531. if (&upper->list == &dev->adj_list.upper)
  5532. return NULL;
  5533. *iter = &upper->list;
  5534. return upper->dev;
  5535. }
  5536. static int netdev_walk_all_upper_dev(struct net_device *dev,
  5537. int (*fn)(struct net_device *dev,
  5538. void *data),
  5539. void *data)
  5540. {
  5541. struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  5542. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  5543. int ret, cur = 0;
  5544. now = dev;
  5545. iter = &dev->adj_list.upper;
  5546. while (1) {
  5547. if (now != dev) {
  5548. ret = fn(now, data);
  5549. if (ret)
  5550. return ret;
  5551. }
  5552. next = NULL;
  5553. while (1) {
  5554. udev = netdev_next_upper_dev(now, &iter);
  5555. if (!udev)
  5556. break;
  5557. next = udev;
  5558. niter = &udev->adj_list.upper;
  5559. dev_stack[cur] = now;
  5560. iter_stack[cur++] = iter;
  5561. break;
  5562. }
  5563. if (!next) {
  5564. if (!cur)
  5565. return 0;
  5566. next = dev_stack[--cur];
  5567. niter = iter_stack[cur];
  5568. }
  5569. now = next;
  5570. iter = niter;
  5571. }
  5572. return 0;
  5573. }
  5574. int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
  5575. int (*fn)(struct net_device *dev,
  5576. void *data),
  5577. void *data)
  5578. {
  5579. struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  5580. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  5581. int ret, cur = 0;
  5582. now = dev;
  5583. iter = &dev->adj_list.upper;
  5584. while (1) {
  5585. if (now != dev) {
  5586. ret = fn(now, data);
  5587. if (ret)
  5588. return ret;
  5589. }
  5590. next = NULL;
  5591. while (1) {
  5592. udev = netdev_next_upper_dev_rcu(now, &iter);
  5593. if (!udev)
  5594. break;
  5595. next = udev;
  5596. niter = &udev->adj_list.upper;
  5597. dev_stack[cur] = now;
  5598. iter_stack[cur++] = iter;
  5599. break;
  5600. }
  5601. if (!next) {
  5602. if (!cur)
  5603. return 0;
  5604. next = dev_stack[--cur];
  5605. niter = iter_stack[cur];
  5606. }
  5607. now = next;
  5608. iter = niter;
  5609. }
  5610. return 0;
  5611. }
  5612. EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
  5613. /**
  5614. * netdev_lower_get_next_private - Get the next ->private from the
  5615. * lower neighbour list
  5616. * @dev: device
  5617. * @iter: list_head ** of the current position
  5618. *
  5619. * Gets the next netdev_adjacent->private from the dev's lower neighbour
  5620. * list, starting from iter position. The caller must hold either hold the
  5621. * RTNL lock or its own locking that guarantees that the neighbour lower
  5622. * list will remain unchanged.
  5623. */
  5624. void *netdev_lower_get_next_private(struct net_device *dev,
  5625. struct list_head **iter)
  5626. {
  5627. struct netdev_adjacent *lower;
  5628. lower = list_entry(*iter, struct netdev_adjacent, list);
  5629. if (&lower->list == &dev->adj_list.lower)
  5630. return NULL;
  5631. *iter = lower->list.next;
  5632. return lower->private;
  5633. }
  5634. EXPORT_SYMBOL(netdev_lower_get_next_private);
  5635. /**
  5636. * netdev_lower_get_next_private_rcu - Get the next ->private from the
  5637. * lower neighbour list, RCU
  5638. * variant
  5639. * @dev: device
  5640. * @iter: list_head ** of the current position
  5641. *
  5642. * Gets the next netdev_adjacent->private from the dev's lower neighbour
  5643. * list, starting from iter position. The caller must hold RCU read lock.
  5644. */
  5645. void *netdev_lower_get_next_private_rcu(struct net_device *dev,
  5646. struct list_head **iter)
  5647. {
  5648. struct netdev_adjacent *lower;
  5649. WARN_ON_ONCE(!rcu_read_lock_held());
  5650. lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  5651. if (&lower->list == &dev->adj_list.lower)
  5652. return NULL;
  5653. *iter = &lower->list;
  5654. return lower->private;
  5655. }
  5656. EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
  5657. /**
  5658. * netdev_lower_get_next - Get the next device from the lower neighbour
  5659. * list
  5660. * @dev: device
  5661. * @iter: list_head ** of the current position
  5662. *
  5663. * Gets the next netdev_adjacent from the dev's lower neighbour
  5664. * list, starting from iter position. The caller must hold RTNL lock or
  5665. * its own locking that guarantees that the neighbour lower
  5666. * list will remain unchanged.
  5667. */
  5668. void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
  5669. {
  5670. struct netdev_adjacent *lower;
  5671. lower = list_entry(*iter, struct netdev_adjacent, list);
  5672. if (&lower->list == &dev->adj_list.lower)
  5673. return NULL;
  5674. *iter = lower->list.next;
  5675. return lower->dev;
  5676. }
  5677. EXPORT_SYMBOL(netdev_lower_get_next);
  5678. static struct net_device *netdev_next_lower_dev(struct net_device *dev,
  5679. struct list_head **iter)
  5680. {
  5681. struct netdev_adjacent *lower;
  5682. lower = list_entry((*iter)->next, struct netdev_adjacent, list);
  5683. if (&lower->list == &dev->adj_list.lower)
  5684. return NULL;
  5685. *iter = &lower->list;
  5686. return lower->dev;
  5687. }
  5688. int netdev_walk_all_lower_dev(struct net_device *dev,
  5689. int (*fn)(struct net_device *dev,
  5690. void *data),
  5691. void *data)
  5692. {
  5693. struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  5694. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  5695. int ret, cur = 0;
  5696. now = dev;
  5697. iter = &dev->adj_list.lower;
  5698. while (1) {
  5699. if (now != dev) {
  5700. ret = fn(now, data);
  5701. if (ret)
  5702. return ret;
  5703. }
  5704. next = NULL;
  5705. while (1) {
  5706. ldev = netdev_next_lower_dev(now, &iter);
  5707. if (!ldev)
  5708. break;
  5709. next = ldev;
  5710. niter = &ldev->adj_list.lower;
  5711. dev_stack[cur] = now;
  5712. iter_stack[cur++] = iter;
  5713. break;
  5714. }
  5715. if (!next) {
  5716. if (!cur)
  5717. return 0;
  5718. next = dev_stack[--cur];
  5719. niter = iter_stack[cur];
  5720. }
  5721. now = next;
  5722. iter = niter;
  5723. }
  5724. return 0;
  5725. }
  5726. EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
  5727. static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
  5728. struct list_head **iter)
  5729. {
  5730. struct netdev_adjacent *lower;
  5731. lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
  5732. if (&lower->list == &dev->adj_list.lower)
  5733. return NULL;
  5734. *iter = &lower->list;
  5735. return lower->dev;
  5736. }
  5737. static u8 __netdev_upper_depth(struct net_device *dev)
  5738. {
  5739. struct net_device *udev;
  5740. struct list_head *iter;
  5741. u8 max_depth = 0;
  5742. for (iter = &dev->adj_list.upper,
  5743. udev = netdev_next_upper_dev(dev, &iter);
  5744. udev;
  5745. udev = netdev_next_upper_dev(dev, &iter)) {
  5746. if (max_depth < udev->upper_level)
  5747. max_depth = udev->upper_level;
  5748. }
  5749. return max_depth;
  5750. }
  5751. static u8 __netdev_lower_depth(struct net_device *dev)
  5752. {
  5753. struct net_device *ldev;
  5754. struct list_head *iter;
  5755. u8 max_depth = 0;
  5756. for (iter = &dev->adj_list.lower,
  5757. ldev = netdev_next_lower_dev(dev, &iter);
  5758. ldev;
  5759. ldev = netdev_next_lower_dev(dev, &iter)) {
  5760. if (max_depth < ldev->lower_level)
  5761. max_depth = ldev->lower_level;
  5762. }
  5763. return max_depth;
  5764. }
  5765. static int __netdev_update_upper_level(struct net_device *dev, void *data)
  5766. {
  5767. dev->upper_level = __netdev_upper_depth(dev) + 1;
  5768. return 0;
  5769. }
  5770. static int __netdev_update_lower_level(struct net_device *dev, void *data)
  5771. {
  5772. dev->lower_level = __netdev_lower_depth(dev) + 1;
  5773. return 0;
  5774. }
  5775. int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
  5776. int (*fn)(struct net_device *dev,
  5777. void *data),
  5778. void *data)
  5779. {
  5780. struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  5781. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  5782. int ret, cur = 0;
  5783. now = dev;
  5784. iter = &dev->adj_list.lower;
  5785. while (1) {
  5786. if (now != dev) {
  5787. ret = fn(now, data);
  5788. if (ret)
  5789. return ret;
  5790. }
  5791. next = NULL;
  5792. while (1) {
  5793. ldev = netdev_next_lower_dev_rcu(now, &iter);
  5794. if (!ldev)
  5795. break;
  5796. next = ldev;
  5797. niter = &ldev->adj_list.lower;
  5798. dev_stack[cur] = now;
  5799. iter_stack[cur++] = iter;
  5800. break;
  5801. }
  5802. if (!next) {
  5803. if (!cur)
  5804. return 0;
  5805. next = dev_stack[--cur];
  5806. niter = iter_stack[cur];
  5807. }
  5808. now = next;
  5809. iter = niter;
  5810. }
  5811. return 0;
  5812. }
  5813. EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
  5814. /**
  5815. * netdev_lower_get_first_private_rcu - Get the first ->private from the
  5816. * lower neighbour list, RCU
  5817. * variant
  5818. * @dev: device
  5819. *
  5820. * Gets the first netdev_adjacent->private from the dev's lower neighbour
  5821. * list. The caller must hold RCU read lock.
  5822. */
  5823. void *netdev_lower_get_first_private_rcu(struct net_device *dev)
  5824. {
  5825. struct netdev_adjacent *lower;
  5826. lower = list_first_or_null_rcu(&dev->adj_list.lower,
  5827. struct netdev_adjacent, list);
  5828. if (lower)
  5829. return lower->private;
  5830. return NULL;
  5831. }
  5832. EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
  5833. /**
  5834. * netdev_master_upper_dev_get_rcu - Get master upper device
  5835. * @dev: device
  5836. *
  5837. * Find a master upper device and return pointer to it or NULL in case
  5838. * it's not there. The caller must hold the RCU read lock.
  5839. */
  5840. struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
  5841. {
  5842. struct netdev_adjacent *upper;
  5843. upper = list_first_or_null_rcu(&dev->adj_list.upper,
  5844. struct netdev_adjacent, list);
  5845. if (upper && likely(upper->master))
  5846. return upper->dev;
  5847. return NULL;
  5848. }
  5849. EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
  5850. static int netdev_adjacent_sysfs_add(struct net_device *dev,
  5851. struct net_device *adj_dev,
  5852. struct list_head *dev_list)
  5853. {
  5854. char linkname[IFNAMSIZ+7];
  5855. sprintf(linkname, dev_list == &dev->adj_list.upper ?
  5856. "upper_%s" : "lower_%s", adj_dev->name);
  5857. return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
  5858. linkname);
  5859. }
  5860. static void netdev_adjacent_sysfs_del(struct net_device *dev,
  5861. char *name,
  5862. struct list_head *dev_list)
  5863. {
  5864. char linkname[IFNAMSIZ+7];
  5865. sprintf(linkname, dev_list == &dev->adj_list.upper ?
  5866. "upper_%s" : "lower_%s", name);
  5867. sysfs_remove_link(&(dev->dev.kobj), linkname);
  5868. }
  5869. static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
  5870. struct net_device *adj_dev,
  5871. struct list_head *dev_list)
  5872. {
  5873. return (dev_list == &dev->adj_list.upper ||
  5874. dev_list == &dev->adj_list.lower) &&
  5875. net_eq(dev_net(dev), dev_net(adj_dev));
  5876. }
  5877. static int __netdev_adjacent_dev_insert(struct net_device *dev,
  5878. struct net_device *adj_dev,
  5879. struct list_head *dev_list,
  5880. void *private, bool master)
  5881. {
  5882. struct netdev_adjacent *adj;
  5883. int ret;
  5884. adj = __netdev_find_adj(adj_dev, dev_list);
  5885. if (adj) {
  5886. adj->ref_nr += 1;
  5887. pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
  5888. dev->name, adj_dev->name, adj->ref_nr);
  5889. return 0;
  5890. }
  5891. adj = kmalloc(sizeof(*adj), GFP_KERNEL);
  5892. if (!adj)
  5893. return -ENOMEM;
  5894. adj->dev = adj_dev;
  5895. adj->master = master;
  5896. adj->ref_nr = 1;
  5897. adj->private = private;
  5898. dev_hold(adj_dev);
  5899. pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
  5900. dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
  5901. if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
  5902. ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
  5903. if (ret)
  5904. goto free_adj;
  5905. }
  5906. /* Ensure that master link is always the first item in list. */
  5907. if (master) {
  5908. ret = sysfs_create_link(&(dev->dev.kobj),
  5909. &(adj_dev->dev.kobj), "master");
  5910. if (ret)
  5911. goto remove_symlinks;
  5912. list_add_rcu(&adj->list, dev_list);
  5913. } else {
  5914. list_add_tail_rcu(&adj->list, dev_list);
  5915. }
  5916. return 0;
  5917. remove_symlinks:
  5918. if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
  5919. netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
  5920. free_adj:
  5921. kfree(adj);
  5922. dev_put(adj_dev);
  5923. return ret;
  5924. }
  5925. static void __netdev_adjacent_dev_remove(struct net_device *dev,
  5926. struct net_device *adj_dev,
  5927. u16 ref_nr,
  5928. struct list_head *dev_list)
  5929. {
  5930. struct netdev_adjacent *adj;
  5931. pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
  5932. dev->name, adj_dev->name, ref_nr);
  5933. adj = __netdev_find_adj(adj_dev, dev_list);
  5934. if (!adj) {
  5935. pr_err("Adjacency does not exist for device %s from %s\n",
  5936. dev->name, adj_dev->name);
  5937. WARN_ON(1);
  5938. return;
  5939. }
  5940. if (adj->ref_nr > ref_nr) {
  5941. pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
  5942. dev->name, adj_dev->name, ref_nr,
  5943. adj->ref_nr - ref_nr);
  5944. adj->ref_nr -= ref_nr;
  5945. return;
  5946. }
  5947. if (adj->master)
  5948. sysfs_remove_link(&(dev->dev.kobj), "master");
  5949. if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
  5950. netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
  5951. list_del_rcu(&adj->list);
  5952. pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
  5953. adj_dev->name, dev->name, adj_dev->name);
  5954. dev_put(adj_dev);
  5955. kfree_rcu(adj, rcu);
  5956. }
  5957. static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
  5958. struct net_device *upper_dev,
  5959. struct list_head *up_list,
  5960. struct list_head *down_list,
  5961. void *private, bool master)
  5962. {
  5963. int ret;
  5964. ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
  5965. private, master);
  5966. if (ret)
  5967. return ret;
  5968. ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
  5969. private, false);
  5970. if (ret) {
  5971. __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
  5972. return ret;
  5973. }
  5974. return 0;
  5975. }
  5976. static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
  5977. struct net_device *upper_dev,
  5978. u16 ref_nr,
  5979. struct list_head *up_list,
  5980. struct list_head *down_list)
  5981. {
  5982. __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
  5983. __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
  5984. }
  5985. static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
  5986. struct net_device *upper_dev,
  5987. void *private, bool master)
  5988. {
  5989. return __netdev_adjacent_dev_link_lists(dev, upper_dev,
  5990. &dev->adj_list.upper,
  5991. &upper_dev->adj_list.lower,
  5992. private, master);
  5993. }
  5994. static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
  5995. struct net_device *upper_dev)
  5996. {
  5997. __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
  5998. &dev->adj_list.upper,
  5999. &upper_dev->adj_list.lower);
  6000. }
  6001. static int __netdev_upper_dev_link(struct net_device *dev,
  6002. struct net_device *upper_dev, bool master,
  6003. void *upper_priv, void *upper_info,
  6004. struct netlink_ext_ack *extack)
  6005. {
  6006. struct netdev_notifier_changeupper_info changeupper_info = {
  6007. .info = {
  6008. .dev = dev,
  6009. .extack = extack,
  6010. },
  6011. .upper_dev = upper_dev,
  6012. .master = master,
  6013. .linking = true,
  6014. .upper_info = upper_info,
  6015. };
  6016. struct net_device *master_dev;
  6017. int ret = 0;
  6018. ASSERT_RTNL();
  6019. if (dev == upper_dev)
  6020. return -EBUSY;
  6021. /* To prevent loops, check if dev is not upper device to upper_dev. */
  6022. if (netdev_has_upper_dev(upper_dev, dev))
  6023. return -EBUSY;
  6024. if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
  6025. return -EMLINK;
  6026. if (!master) {
  6027. if (netdev_has_upper_dev(dev, upper_dev))
  6028. return -EEXIST;
  6029. } else {
  6030. master_dev = netdev_master_upper_dev_get(dev);
  6031. if (master_dev)
  6032. return master_dev == upper_dev ? -EEXIST : -EBUSY;
  6033. }
  6034. ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
  6035. &changeupper_info.info);
  6036. ret = notifier_to_errno(ret);
  6037. if (ret)
  6038. return ret;
  6039. ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
  6040. master);
  6041. if (ret)
  6042. return ret;
  6043. ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
  6044. &changeupper_info.info);
  6045. ret = notifier_to_errno(ret);
  6046. if (ret)
  6047. goto rollback;
  6048. __netdev_update_upper_level(dev, NULL);
  6049. netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
  6050. __netdev_update_lower_level(upper_dev, NULL);
  6051. netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, NULL);
  6052. return 0;
  6053. rollback:
  6054. __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
  6055. return ret;
  6056. }
  6057. /**
  6058. * netdev_upper_dev_link - Add a link to the upper device
  6059. * @dev: device
  6060. * @upper_dev: new upper device
  6061. * @extack: netlink extended ack
  6062. *
  6063. * Adds a link to device which is upper to this one. The caller must hold
  6064. * the RTNL lock. On a failure a negative errno code is returned.
  6065. * On success the reference counts are adjusted and the function
  6066. * returns zero.
  6067. */
  6068. int netdev_upper_dev_link(struct net_device *dev,
  6069. struct net_device *upper_dev,
  6070. struct netlink_ext_ack *extack)
  6071. {
  6072. return __netdev_upper_dev_link(dev, upper_dev, false,
  6073. NULL, NULL, extack);
  6074. }
  6075. EXPORT_SYMBOL(netdev_upper_dev_link);
  6076. /**
  6077. * netdev_master_upper_dev_link - Add a master link to the upper device
  6078. * @dev: device
  6079. * @upper_dev: new upper device
  6080. * @upper_priv: upper device private
  6081. * @upper_info: upper info to be passed down via notifier
  6082. * @extack: netlink extended ack
  6083. *
  6084. * Adds a link to device which is upper to this one. In this case, only
  6085. * one master upper device can be linked, although other non-master devices
  6086. * might be linked as well. The caller must hold the RTNL lock.
  6087. * On a failure a negative errno code is returned. On success the reference
  6088. * counts are adjusted and the function returns zero.
  6089. */
  6090. int netdev_master_upper_dev_link(struct net_device *dev,
  6091. struct net_device *upper_dev,
  6092. void *upper_priv, void *upper_info,
  6093. struct netlink_ext_ack *extack)
  6094. {
  6095. return __netdev_upper_dev_link(dev, upper_dev, true,
  6096. upper_priv, upper_info, extack);
  6097. }
  6098. EXPORT_SYMBOL(netdev_master_upper_dev_link);
  6099. /**
  6100. * netdev_upper_dev_unlink - Removes a link to upper device
  6101. * @dev: device
  6102. * @upper_dev: new upper device
  6103. *
  6104. * Removes a link to device which is upper to this one. The caller must hold
  6105. * the RTNL lock.
  6106. */
  6107. void netdev_upper_dev_unlink(struct net_device *dev,
  6108. struct net_device *upper_dev)
  6109. {
  6110. struct netdev_notifier_changeupper_info changeupper_info = {
  6111. .info = {
  6112. .dev = dev,
  6113. },
  6114. .upper_dev = upper_dev,
  6115. .linking = false,
  6116. };
  6117. ASSERT_RTNL();
  6118. changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
  6119. call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
  6120. &changeupper_info.info);
  6121. __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
  6122. call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
  6123. &changeupper_info.info);
  6124. __netdev_update_upper_level(dev, NULL);
  6125. netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
  6126. __netdev_update_lower_level(upper_dev, NULL);
  6127. netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, NULL);
  6128. }
  6129. EXPORT_SYMBOL(netdev_upper_dev_unlink);
  6130. /**
  6131. * netdev_bonding_info_change - Dispatch event about slave change
  6132. * @dev: device
  6133. * @bonding_info: info to dispatch
  6134. *
  6135. * Send NETDEV_BONDING_INFO to netdev notifiers with info.
  6136. * The caller must hold the RTNL lock.
  6137. */
  6138. void netdev_bonding_info_change(struct net_device *dev,
  6139. struct netdev_bonding_info *bonding_info)
  6140. {
  6141. struct netdev_notifier_bonding_info info = {
  6142. .info.dev = dev,
  6143. };
  6144. memcpy(&info.bonding_info, bonding_info,
  6145. sizeof(struct netdev_bonding_info));
  6146. call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
  6147. &info.info);
  6148. }
  6149. EXPORT_SYMBOL(netdev_bonding_info_change);
  6150. static void netdev_adjacent_add_links(struct net_device *dev)
  6151. {
  6152. struct netdev_adjacent *iter;
  6153. struct net *net = dev_net(dev);
  6154. list_for_each_entry(iter, &dev->adj_list.upper, list) {
  6155. if (!net_eq(net, dev_net(iter->dev)))
  6156. continue;
  6157. netdev_adjacent_sysfs_add(iter->dev, dev,
  6158. &iter->dev->adj_list.lower);
  6159. netdev_adjacent_sysfs_add(dev, iter->dev,
  6160. &dev->adj_list.upper);
  6161. }
  6162. list_for_each_entry(iter, &dev->adj_list.lower, list) {
  6163. if (!net_eq(net, dev_net(iter->dev)))
  6164. continue;
  6165. netdev_adjacent_sysfs_add(iter->dev, dev,
  6166. &iter->dev->adj_list.upper);
  6167. netdev_adjacent_sysfs_add(dev, iter->dev,
  6168. &dev->adj_list.lower);
  6169. }
  6170. }
  6171. static void netdev_adjacent_del_links(struct net_device *dev)
  6172. {
  6173. struct netdev_adjacent *iter;
  6174. struct net *net = dev_net(dev);
  6175. list_for_each_entry(iter, &dev->adj_list.upper, list) {
  6176. if (!net_eq(net, dev_net(iter->dev)))
  6177. continue;
  6178. netdev_adjacent_sysfs_del(iter->dev, dev->name,
  6179. &iter->dev->adj_list.lower);
  6180. netdev_adjacent_sysfs_del(dev, iter->dev->name,
  6181. &dev->adj_list.upper);
  6182. }
  6183. list_for_each_entry(iter, &dev->adj_list.lower, list) {
  6184. if (!net_eq(net, dev_net(iter->dev)))
  6185. continue;
  6186. netdev_adjacent_sysfs_del(iter->dev, dev->name,
  6187. &iter->dev->adj_list.upper);
  6188. netdev_adjacent_sysfs_del(dev, iter->dev->name,
  6189. &dev->adj_list.lower);
  6190. }
  6191. }
  6192. void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
  6193. {
  6194. struct netdev_adjacent *iter;
  6195. struct net *net = dev_net(dev);
  6196. list_for_each_entry(iter, &dev->adj_list.upper, list) {
  6197. if (!net_eq(net, dev_net(iter->dev)))
  6198. continue;
  6199. netdev_adjacent_sysfs_del(iter->dev, oldname,
  6200. &iter->dev->adj_list.lower);
  6201. netdev_adjacent_sysfs_add(iter->dev, dev,
  6202. &iter->dev->adj_list.lower);
  6203. }
  6204. list_for_each_entry(iter, &dev->adj_list.lower, list) {
  6205. if (!net_eq(net, dev_net(iter->dev)))
  6206. continue;
  6207. netdev_adjacent_sysfs_del(iter->dev, oldname,
  6208. &iter->dev->adj_list.upper);
  6209. netdev_adjacent_sysfs_add(iter->dev, dev,
  6210. &iter->dev->adj_list.upper);
  6211. }
  6212. }
  6213. void *netdev_lower_dev_get_private(struct net_device *dev,
  6214. struct net_device *lower_dev)
  6215. {
  6216. struct netdev_adjacent *lower;
  6217. if (!lower_dev)
  6218. return NULL;
  6219. lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
  6220. if (!lower)
  6221. return NULL;
  6222. return lower->private;
  6223. }
  6224. EXPORT_SYMBOL(netdev_lower_dev_get_private);
  6225. int dev_get_nest_level(struct net_device *dev)
  6226. {
  6227. struct net_device *lower = NULL;
  6228. struct list_head *iter;
  6229. int max_nest = -1;
  6230. int nest;
  6231. ASSERT_RTNL();
  6232. netdev_for_each_lower_dev(dev, lower, iter) {
  6233. nest = dev_get_nest_level(lower);
  6234. if (max_nest < nest)
  6235. max_nest = nest;
  6236. }
  6237. return max_nest + 1;
  6238. }
  6239. EXPORT_SYMBOL(dev_get_nest_level);
  6240. /**
  6241. * netdev_lower_change - Dispatch event about lower device state change
  6242. * @lower_dev: device
  6243. * @lower_state_info: state to dispatch
  6244. *
  6245. * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
  6246. * The caller must hold the RTNL lock.
  6247. */
  6248. void netdev_lower_state_changed(struct net_device *lower_dev,
  6249. void *lower_state_info)
  6250. {
  6251. struct netdev_notifier_changelowerstate_info changelowerstate_info = {
  6252. .info.dev = lower_dev,
  6253. };
  6254. ASSERT_RTNL();
  6255. changelowerstate_info.lower_state_info = lower_state_info;
  6256. call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
  6257. &changelowerstate_info.info);
  6258. }
  6259. EXPORT_SYMBOL(netdev_lower_state_changed);
  6260. static void dev_change_rx_flags(struct net_device *dev, int flags)
  6261. {
  6262. const struct net_device_ops *ops = dev->netdev_ops;
  6263. if (ops->ndo_change_rx_flags)
  6264. ops->ndo_change_rx_flags(dev, flags);
  6265. }
  6266. static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
  6267. {
  6268. unsigned int old_flags = dev->flags;
  6269. kuid_t uid;
  6270. kgid_t gid;
  6271. ASSERT_RTNL();
  6272. dev->flags |= IFF_PROMISC;
  6273. dev->promiscuity += inc;
  6274. if (dev->promiscuity == 0) {
  6275. /*
  6276. * Avoid overflow.
  6277. * If inc causes overflow, untouch promisc and return error.
  6278. */
  6279. if (inc < 0)
  6280. dev->flags &= ~IFF_PROMISC;
  6281. else {
  6282. dev->promiscuity -= inc;
  6283. pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
  6284. dev->name);
  6285. return -EOVERFLOW;
  6286. }
  6287. }
  6288. if (dev->flags != old_flags) {
  6289. pr_info("device %s %s promiscuous mode\n",
  6290. dev->name,
  6291. dev->flags & IFF_PROMISC ? "entered" : "left");
  6292. if (audit_enabled) {
  6293. current_uid_gid(&uid, &gid);
  6294. audit_log(audit_context(), GFP_ATOMIC,
  6295. AUDIT_ANOM_PROMISCUOUS,
  6296. "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
  6297. dev->name, (dev->flags & IFF_PROMISC),
  6298. (old_flags & IFF_PROMISC),
  6299. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  6300. from_kuid(&init_user_ns, uid),
  6301. from_kgid(&init_user_ns, gid),
  6302. audit_get_sessionid(current));
  6303. }
  6304. dev_change_rx_flags(dev, IFF_PROMISC);
  6305. }
  6306. if (notify)
  6307. __dev_notify_flags(dev, old_flags, IFF_PROMISC);
  6308. return 0;
  6309. }
  6310. /**
  6311. * dev_set_promiscuity - update promiscuity count on a device
  6312. * @dev: device
  6313. * @inc: modifier
  6314. *
  6315. * Add or remove promiscuity from a device. While the count in the device
  6316. * remains above zero the interface remains promiscuous. Once it hits zero
  6317. * the device reverts back to normal filtering operation. A negative inc
  6318. * value is used to drop promiscuity on the device.
  6319. * Return 0 if successful or a negative errno code on error.
  6320. */
  6321. int dev_set_promiscuity(struct net_device *dev, int inc)
  6322. {
  6323. unsigned int old_flags = dev->flags;
  6324. int err;
  6325. err = __dev_set_promiscuity(dev, inc, true);
  6326. if (err < 0)
  6327. return err;
  6328. if (dev->flags != old_flags)
  6329. dev_set_rx_mode(dev);
  6330. return err;
  6331. }
  6332. EXPORT_SYMBOL(dev_set_promiscuity);
  6333. static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
  6334. {
  6335. unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
  6336. ASSERT_RTNL();
  6337. dev->flags |= IFF_ALLMULTI;
  6338. dev->allmulti += inc;
  6339. if (dev->allmulti == 0) {
  6340. /*
  6341. * Avoid overflow.
  6342. * If inc causes overflow, untouch allmulti and return error.
  6343. */
  6344. if (inc < 0)
  6345. dev->flags &= ~IFF_ALLMULTI;
  6346. else {
  6347. dev->allmulti -= inc;
  6348. pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
  6349. dev->name);
  6350. return -EOVERFLOW;
  6351. }
  6352. }
  6353. if (dev->flags ^ old_flags) {
  6354. dev_change_rx_flags(dev, IFF_ALLMULTI);
  6355. dev_set_rx_mode(dev);
  6356. if (notify)
  6357. __dev_notify_flags(dev, old_flags,
  6358. dev->gflags ^ old_gflags);
  6359. }
  6360. return 0;
  6361. }
  6362. /**
  6363. * dev_set_allmulti - update allmulti count on a device
  6364. * @dev: device
  6365. * @inc: modifier
  6366. *
  6367. * Add or remove reception of all multicast frames to a device. While the
  6368. * count in the device remains above zero the interface remains listening
  6369. * to all interfaces. Once it hits zero the device reverts back to normal
  6370. * filtering operation. A negative @inc value is used to drop the counter
  6371. * when releasing a resource needing all multicasts.
  6372. * Return 0 if successful or a negative errno code on error.
  6373. */
  6374. int dev_set_allmulti(struct net_device *dev, int inc)
  6375. {
  6376. return __dev_set_allmulti(dev, inc, true);
  6377. }
  6378. EXPORT_SYMBOL(dev_set_allmulti);
  6379. /*
  6380. * Upload unicast and multicast address lists to device and
  6381. * configure RX filtering. When the device doesn't support unicast
  6382. * filtering it is put in promiscuous mode while unicast addresses
  6383. * are present.
  6384. */
  6385. void __dev_set_rx_mode(struct net_device *dev)
  6386. {
  6387. const struct net_device_ops *ops = dev->netdev_ops;
  6388. /* dev_open will call this function so the list will stay sane. */
  6389. if (!(dev->flags&IFF_UP))
  6390. return;
  6391. if (!netif_device_present(dev))
  6392. return;
  6393. if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
  6394. /* Unicast addresses changes may only happen under the rtnl,
  6395. * therefore calling __dev_set_promiscuity here is safe.
  6396. */
  6397. if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
  6398. __dev_set_promiscuity(dev, 1, false);
  6399. dev->uc_promisc = true;
  6400. } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
  6401. __dev_set_promiscuity(dev, -1, false);
  6402. dev->uc_promisc = false;
  6403. }
  6404. }
  6405. if (ops->ndo_set_rx_mode)
  6406. ops->ndo_set_rx_mode(dev);
  6407. }
  6408. void dev_set_rx_mode(struct net_device *dev)
  6409. {
  6410. netif_addr_lock_bh(dev);
  6411. __dev_set_rx_mode(dev);
  6412. netif_addr_unlock_bh(dev);
  6413. }
  6414. /**
  6415. * dev_get_flags - get flags reported to userspace
  6416. * @dev: device
  6417. *
  6418. * Get the combination of flag bits exported through APIs to userspace.
  6419. */
  6420. unsigned int dev_get_flags(const struct net_device *dev)
  6421. {
  6422. unsigned int flags;
  6423. flags = (dev->flags & ~(IFF_PROMISC |
  6424. IFF_ALLMULTI |
  6425. IFF_RUNNING |
  6426. IFF_LOWER_UP |
  6427. IFF_DORMANT)) |
  6428. (dev->gflags & (IFF_PROMISC |
  6429. IFF_ALLMULTI));
  6430. if (netif_running(dev)) {
  6431. if (netif_oper_up(dev))
  6432. flags |= IFF_RUNNING;
  6433. if (netif_carrier_ok(dev))
  6434. flags |= IFF_LOWER_UP;
  6435. if (netif_dormant(dev))
  6436. flags |= IFF_DORMANT;
  6437. }
  6438. return flags;
  6439. }
  6440. EXPORT_SYMBOL(dev_get_flags);
  6441. int __dev_change_flags(struct net_device *dev, unsigned int flags)
  6442. {
  6443. unsigned int old_flags = dev->flags;
  6444. int ret;
  6445. ASSERT_RTNL();
  6446. /*
  6447. * Set the flags on our device.
  6448. */
  6449. dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
  6450. IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
  6451. IFF_AUTOMEDIA)) |
  6452. (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
  6453. IFF_ALLMULTI));
  6454. /*
  6455. * Load in the correct multicast list now the flags have changed.
  6456. */
  6457. if ((old_flags ^ flags) & IFF_MULTICAST)
  6458. dev_change_rx_flags(dev, IFF_MULTICAST);
  6459. dev_set_rx_mode(dev);
  6460. /*
  6461. * Have we downed the interface. We handle IFF_UP ourselves
  6462. * according to user attempts to set it, rather than blindly
  6463. * setting it.
  6464. */
  6465. ret = 0;
  6466. if ((old_flags ^ flags) & IFF_UP) {
  6467. if (old_flags & IFF_UP)
  6468. __dev_close(dev);
  6469. else
  6470. ret = __dev_open(dev);
  6471. }
  6472. if ((flags ^ dev->gflags) & IFF_PROMISC) {
  6473. int inc = (flags & IFF_PROMISC) ? 1 : -1;
  6474. unsigned int old_flags = dev->flags;
  6475. dev->gflags ^= IFF_PROMISC;
  6476. if (__dev_set_promiscuity(dev, inc, false) >= 0)
  6477. if (dev->flags != old_flags)
  6478. dev_set_rx_mode(dev);
  6479. }
  6480. /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
  6481. * is important. Some (broken) drivers set IFF_PROMISC, when
  6482. * IFF_ALLMULTI is requested not asking us and not reporting.
  6483. */
  6484. if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
  6485. int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
  6486. dev->gflags ^= IFF_ALLMULTI;
  6487. __dev_set_allmulti(dev, inc, false);
  6488. }
  6489. return ret;
  6490. }
  6491. void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
  6492. unsigned int gchanges)
  6493. {
  6494. unsigned int changes = dev->flags ^ old_flags;
  6495. if (gchanges)
  6496. rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
  6497. if (changes & IFF_UP) {
  6498. if (dev->flags & IFF_UP)
  6499. call_netdevice_notifiers(NETDEV_UP, dev);
  6500. else
  6501. call_netdevice_notifiers(NETDEV_DOWN, dev);
  6502. }
  6503. if (dev->flags & IFF_UP &&
  6504. (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
  6505. struct netdev_notifier_change_info change_info = {
  6506. .info = {
  6507. .dev = dev,
  6508. },
  6509. .flags_changed = changes,
  6510. };
  6511. call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
  6512. }
  6513. }
  6514. /**
  6515. * dev_change_flags - change device settings
  6516. * @dev: device
  6517. * @flags: device state flags
  6518. *
  6519. * Change settings on device based state flags. The flags are
  6520. * in the userspace exported format.
  6521. */
  6522. int dev_change_flags(struct net_device *dev, unsigned int flags)
  6523. {
  6524. int ret;
  6525. unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
  6526. ret = __dev_change_flags(dev, flags);
  6527. if (ret < 0)
  6528. return ret;
  6529. changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
  6530. __dev_notify_flags(dev, old_flags, changes);
  6531. return ret;
  6532. }
  6533. EXPORT_SYMBOL(dev_change_flags);
  6534. int __dev_set_mtu(struct net_device *dev, int new_mtu)
  6535. {
  6536. const struct net_device_ops *ops = dev->netdev_ops;
  6537. if (ops->ndo_change_mtu)
  6538. return ops->ndo_change_mtu(dev, new_mtu);
  6539. /* Pairs with all the lockless reads of dev->mtu in the stack */
  6540. WRITE_ONCE(dev->mtu, new_mtu);
  6541. return 0;
  6542. }
  6543. EXPORT_SYMBOL(__dev_set_mtu);
  6544. int dev_validate_mtu(struct net_device *dev, int new_mtu,
  6545. struct netlink_ext_ack *extack)
  6546. {
  6547. /* MTU must be positive, and in range */
  6548. if (new_mtu < 0 || new_mtu < dev->min_mtu) {
  6549. NL_SET_ERR_MSG(extack, "mtu less than device minimum");
  6550. return -EINVAL;
  6551. }
  6552. if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
  6553. NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
  6554. return -EINVAL;
  6555. }
  6556. return 0;
  6557. }
  6558. /**
  6559. * dev_set_mtu_ext - Change maximum transfer unit
  6560. * @dev: device
  6561. * @new_mtu: new transfer unit
  6562. * @extack: netlink extended ack
  6563. *
  6564. * Change the maximum transfer size of the network device.
  6565. */
  6566. int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
  6567. struct netlink_ext_ack *extack)
  6568. {
  6569. int err, orig_mtu;
  6570. if (new_mtu == dev->mtu)
  6571. return 0;
  6572. err = dev_validate_mtu(dev, new_mtu, extack);
  6573. if (err)
  6574. return err;
  6575. if (!netif_device_present(dev))
  6576. return -ENODEV;
  6577. err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
  6578. err = notifier_to_errno(err);
  6579. if (err)
  6580. return err;
  6581. orig_mtu = dev->mtu;
  6582. err = __dev_set_mtu(dev, new_mtu);
  6583. if (!err) {
  6584. err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
  6585. orig_mtu);
  6586. err = notifier_to_errno(err);
  6587. if (err) {
  6588. /* setting mtu back and notifying everyone again,
  6589. * so that they have a chance to revert changes.
  6590. */
  6591. __dev_set_mtu(dev, orig_mtu);
  6592. call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
  6593. new_mtu);
  6594. }
  6595. }
  6596. return err;
  6597. }
  6598. int dev_set_mtu(struct net_device *dev, int new_mtu)
  6599. {
  6600. struct netlink_ext_ack extack;
  6601. int err;
  6602. memset(&extack, 0, sizeof(extack));
  6603. err = dev_set_mtu_ext(dev, new_mtu, &extack);
  6604. if (err && extack._msg)
  6605. net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
  6606. return err;
  6607. }
  6608. EXPORT_SYMBOL(dev_set_mtu);
  6609. /**
  6610. * dev_change_tx_queue_len - Change TX queue length of a netdevice
  6611. * @dev: device
  6612. * @new_len: new tx queue length
  6613. */
  6614. int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
  6615. {
  6616. unsigned int orig_len = dev->tx_queue_len;
  6617. int res;
  6618. if (new_len != (unsigned int)new_len)
  6619. return -ERANGE;
  6620. if (new_len != orig_len) {
  6621. dev->tx_queue_len = new_len;
  6622. res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
  6623. res = notifier_to_errno(res);
  6624. if (res)
  6625. goto err_rollback;
  6626. res = dev_qdisc_change_tx_queue_len(dev);
  6627. if (res)
  6628. goto err_rollback;
  6629. }
  6630. return 0;
  6631. err_rollback:
  6632. netdev_err(dev, "refused to change device tx_queue_len\n");
  6633. dev->tx_queue_len = orig_len;
  6634. return res;
  6635. }
  6636. /**
  6637. * dev_set_group - Change group this device belongs to
  6638. * @dev: device
  6639. * @new_group: group this device should belong to
  6640. */
  6641. void dev_set_group(struct net_device *dev, int new_group)
  6642. {
  6643. dev->group = new_group;
  6644. }
  6645. EXPORT_SYMBOL(dev_set_group);
  6646. /**
  6647. * dev_set_mac_address - Change Media Access Control Address
  6648. * @dev: device
  6649. * @sa: new address
  6650. *
  6651. * Change the hardware (MAC) address of the device
  6652. */
  6653. int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
  6654. {
  6655. const struct net_device_ops *ops = dev->netdev_ops;
  6656. int err;
  6657. if (!ops->ndo_set_mac_address)
  6658. return -EOPNOTSUPP;
  6659. if (sa->sa_family != dev->type)
  6660. return -EINVAL;
  6661. if (!netif_device_present(dev))
  6662. return -ENODEV;
  6663. err = ops->ndo_set_mac_address(dev, sa);
  6664. if (err)
  6665. return err;
  6666. dev->addr_assign_type = NET_ADDR_SET;
  6667. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  6668. add_device_randomness(dev->dev_addr, dev->addr_len);
  6669. return 0;
  6670. }
  6671. EXPORT_SYMBOL(dev_set_mac_address);
  6672. /**
  6673. * dev_change_carrier - Change device carrier
  6674. * @dev: device
  6675. * @new_carrier: new value
  6676. *
  6677. * Change device carrier
  6678. */
  6679. int dev_change_carrier(struct net_device *dev, bool new_carrier)
  6680. {
  6681. const struct net_device_ops *ops = dev->netdev_ops;
  6682. if (!ops->ndo_change_carrier)
  6683. return -EOPNOTSUPP;
  6684. if (!netif_device_present(dev))
  6685. return -ENODEV;
  6686. return ops->ndo_change_carrier(dev, new_carrier);
  6687. }
  6688. EXPORT_SYMBOL(dev_change_carrier);
  6689. /**
  6690. * dev_get_phys_port_id - Get device physical port ID
  6691. * @dev: device
  6692. * @ppid: port ID
  6693. *
  6694. * Get device physical port ID
  6695. */
  6696. int dev_get_phys_port_id(struct net_device *dev,
  6697. struct netdev_phys_item_id *ppid)
  6698. {
  6699. const struct net_device_ops *ops = dev->netdev_ops;
  6700. if (!ops->ndo_get_phys_port_id)
  6701. return -EOPNOTSUPP;
  6702. return ops->ndo_get_phys_port_id(dev, ppid);
  6703. }
  6704. EXPORT_SYMBOL(dev_get_phys_port_id);
  6705. /**
  6706. * dev_get_phys_port_name - Get device physical port name
  6707. * @dev: device
  6708. * @name: port name
  6709. * @len: limit of bytes to copy to name
  6710. *
  6711. * Get device physical port name
  6712. */
  6713. int dev_get_phys_port_name(struct net_device *dev,
  6714. char *name, size_t len)
  6715. {
  6716. const struct net_device_ops *ops = dev->netdev_ops;
  6717. if (!ops->ndo_get_phys_port_name)
  6718. return -EOPNOTSUPP;
  6719. return ops->ndo_get_phys_port_name(dev, name, len);
  6720. }
  6721. EXPORT_SYMBOL(dev_get_phys_port_name);
  6722. /**
  6723. * dev_change_proto_down - update protocol port state information
  6724. * @dev: device
  6725. * @proto_down: new value
  6726. *
  6727. * This info can be used by switch drivers to set the phys state of the
  6728. * port.
  6729. */
  6730. int dev_change_proto_down(struct net_device *dev, bool proto_down)
  6731. {
  6732. const struct net_device_ops *ops = dev->netdev_ops;
  6733. if (!ops->ndo_change_proto_down)
  6734. return -EOPNOTSUPP;
  6735. if (!netif_device_present(dev))
  6736. return -ENODEV;
  6737. return ops->ndo_change_proto_down(dev, proto_down);
  6738. }
  6739. EXPORT_SYMBOL(dev_change_proto_down);
  6740. u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,
  6741. enum bpf_netdev_command cmd)
  6742. {
  6743. struct netdev_bpf xdp;
  6744. if (!bpf_op)
  6745. return 0;
  6746. memset(&xdp, 0, sizeof(xdp));
  6747. xdp.command = cmd;
  6748. /* Query must always succeed. */
  6749. WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG);
  6750. return xdp.prog_id;
  6751. }
  6752. static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,
  6753. struct netlink_ext_ack *extack, u32 flags,
  6754. struct bpf_prog *prog)
  6755. {
  6756. struct netdev_bpf xdp;
  6757. memset(&xdp, 0, sizeof(xdp));
  6758. if (flags & XDP_FLAGS_HW_MODE)
  6759. xdp.command = XDP_SETUP_PROG_HW;
  6760. else
  6761. xdp.command = XDP_SETUP_PROG;
  6762. xdp.extack = extack;
  6763. xdp.flags = flags;
  6764. xdp.prog = prog;
  6765. return bpf_op(dev, &xdp);
  6766. }
  6767. static void dev_xdp_uninstall(struct net_device *dev)
  6768. {
  6769. struct netdev_bpf xdp;
  6770. bpf_op_t ndo_bpf;
  6771. /* Remove generic XDP */
  6772. WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL));
  6773. /* Remove from the driver */
  6774. ndo_bpf = dev->netdev_ops->ndo_bpf;
  6775. if (!ndo_bpf)
  6776. return;
  6777. memset(&xdp, 0, sizeof(xdp));
  6778. xdp.command = XDP_QUERY_PROG;
  6779. WARN_ON(ndo_bpf(dev, &xdp));
  6780. if (xdp.prog_id)
  6781. WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
  6782. NULL));
  6783. /* Remove HW offload */
  6784. memset(&xdp, 0, sizeof(xdp));
  6785. xdp.command = XDP_QUERY_PROG_HW;
  6786. if (!ndo_bpf(dev, &xdp) && xdp.prog_id)
  6787. WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
  6788. NULL));
  6789. }
  6790. /**
  6791. * dev_change_xdp_fd - set or clear a bpf program for a device rx path
  6792. * @dev: device
  6793. * @extack: netlink extended ack
  6794. * @fd: new program fd or negative value to clear
  6795. * @flags: xdp-related flags
  6796. *
  6797. * Set or clear a bpf program for a device
  6798. */
  6799. int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
  6800. int fd, u32 flags)
  6801. {
  6802. const struct net_device_ops *ops = dev->netdev_ops;
  6803. enum bpf_netdev_command query;
  6804. struct bpf_prog *prog = NULL;
  6805. bpf_op_t bpf_op, bpf_chk;
  6806. int err;
  6807. ASSERT_RTNL();
  6808. query = flags & XDP_FLAGS_HW_MODE ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG;
  6809. bpf_op = bpf_chk = ops->ndo_bpf;
  6810. if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE)))
  6811. return -EOPNOTSUPP;
  6812. if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE))
  6813. bpf_op = generic_xdp_install;
  6814. if (bpf_op == bpf_chk)
  6815. bpf_chk = generic_xdp_install;
  6816. if (fd >= 0) {
  6817. if (__dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG) ||
  6818. __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG_HW))
  6819. return -EEXIST;
  6820. if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
  6821. __dev_xdp_query(dev, bpf_op, query))
  6822. return -EBUSY;
  6823. prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
  6824. bpf_op == ops->ndo_bpf);
  6825. if (IS_ERR(prog))
  6826. return PTR_ERR(prog);
  6827. if (!(flags & XDP_FLAGS_HW_MODE) &&
  6828. bpf_prog_is_dev_bound(prog->aux)) {
  6829. NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported");
  6830. bpf_prog_put(prog);
  6831. return -EINVAL;
  6832. }
  6833. }
  6834. err = dev_xdp_install(dev, bpf_op, extack, flags, prog);
  6835. if (err < 0 && prog)
  6836. bpf_prog_put(prog);
  6837. return err;
  6838. }
  6839. /**
  6840. * dev_new_index - allocate an ifindex
  6841. * @net: the applicable net namespace
  6842. *
  6843. * Returns a suitable unique value for a new device interface
  6844. * number. The caller must hold the rtnl semaphore or the
  6845. * dev_base_lock to be sure it remains unique.
  6846. */
  6847. static int dev_new_index(struct net *net)
  6848. {
  6849. int ifindex = net->ifindex;
  6850. for (;;) {
  6851. if (++ifindex <= 0)
  6852. ifindex = 1;
  6853. if (!__dev_get_by_index(net, ifindex))
  6854. return net->ifindex = ifindex;
  6855. }
  6856. }
  6857. /* Delayed registration/unregisteration */
  6858. static LIST_HEAD(net_todo_list);
  6859. DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
  6860. static void net_set_todo(struct net_device *dev)
  6861. {
  6862. list_add_tail(&dev->todo_list, &net_todo_list);
  6863. dev_net(dev)->dev_unreg_count++;
  6864. }
  6865. static void rollback_registered_many(struct list_head *head)
  6866. {
  6867. struct net_device *dev, *tmp;
  6868. LIST_HEAD(close_head);
  6869. BUG_ON(dev_boot_phase);
  6870. ASSERT_RTNL();
  6871. list_for_each_entry_safe(dev, tmp, head, unreg_list) {
  6872. /* Some devices call without registering
  6873. * for initialization unwind. Remove those
  6874. * devices and proceed with the remaining.
  6875. */
  6876. if (dev->reg_state == NETREG_UNINITIALIZED) {
  6877. pr_debug("unregister_netdevice: device %s/%p never was registered\n",
  6878. dev->name, dev);
  6879. WARN_ON(1);
  6880. list_del(&dev->unreg_list);
  6881. continue;
  6882. }
  6883. dev->dismantle = true;
  6884. BUG_ON(dev->reg_state != NETREG_REGISTERED);
  6885. }
  6886. /* If device is running, close it first. */
  6887. list_for_each_entry(dev, head, unreg_list)
  6888. list_add_tail(&dev->close_list, &close_head);
  6889. dev_close_many(&close_head, true);
  6890. list_for_each_entry(dev, head, unreg_list) {
  6891. /* And unlink it from device chain. */
  6892. unlist_netdevice(dev);
  6893. dev->reg_state = NETREG_UNREGISTERING;
  6894. }
  6895. flush_all_backlogs();
  6896. synchronize_net();
  6897. list_for_each_entry(dev, head, unreg_list) {
  6898. struct sk_buff *skb = NULL;
  6899. /* Shutdown queueing discipline. */
  6900. dev_shutdown(dev);
  6901. dev_xdp_uninstall(dev);
  6902. /* Notify protocols, that we are about to destroy
  6903. * this device. They should clean all the things.
  6904. */
  6905. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  6906. if (!dev->rtnl_link_ops ||
  6907. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  6908. skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
  6909. GFP_KERNEL, NULL, 0);
  6910. /*
  6911. * Flush the unicast and multicast chains
  6912. */
  6913. dev_uc_flush(dev);
  6914. dev_mc_flush(dev);
  6915. if (dev->netdev_ops->ndo_uninit)
  6916. dev->netdev_ops->ndo_uninit(dev);
  6917. if (skb)
  6918. rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
  6919. /* Notifier chain MUST detach us all upper devices. */
  6920. WARN_ON(netdev_has_any_upper_dev(dev));
  6921. WARN_ON(netdev_has_any_lower_dev(dev));
  6922. /* Remove entries from kobject tree */
  6923. netdev_unregister_kobject(dev);
  6924. #ifdef CONFIG_XPS
  6925. /* Remove XPS queueing entries */
  6926. netif_reset_xps_queues_gt(dev, 0);
  6927. #endif
  6928. }
  6929. synchronize_net();
  6930. list_for_each_entry(dev, head, unreg_list)
  6931. dev_put(dev);
  6932. }
  6933. static void rollback_registered(struct net_device *dev)
  6934. {
  6935. LIST_HEAD(single);
  6936. list_add(&dev->unreg_list, &single);
  6937. rollback_registered_many(&single);
  6938. list_del(&single);
  6939. }
  6940. static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
  6941. struct net_device *upper, netdev_features_t features)
  6942. {
  6943. netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
  6944. netdev_features_t feature;
  6945. int feature_bit;
  6946. for_each_netdev_feature(upper_disables, feature_bit) {
  6947. feature = __NETIF_F_BIT(feature_bit);
  6948. if (!(upper->wanted_features & feature)
  6949. && (features & feature)) {
  6950. netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
  6951. &feature, upper->name);
  6952. features &= ~feature;
  6953. }
  6954. }
  6955. return features;
  6956. }
  6957. static void netdev_sync_lower_features(struct net_device *upper,
  6958. struct net_device *lower, netdev_features_t features)
  6959. {
  6960. netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
  6961. netdev_features_t feature;
  6962. int feature_bit;
  6963. for_each_netdev_feature(upper_disables, feature_bit) {
  6964. feature = __NETIF_F_BIT(feature_bit);
  6965. if (!(features & feature) && (lower->features & feature)) {
  6966. netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
  6967. &feature, lower->name);
  6968. lower->wanted_features &= ~feature;
  6969. netdev_update_features(lower);
  6970. if (unlikely(lower->features & feature))
  6971. netdev_WARN(upper, "failed to disable %pNF on %s!\n",
  6972. &feature, lower->name);
  6973. }
  6974. }
  6975. }
  6976. static netdev_features_t netdev_fix_features(struct net_device *dev,
  6977. netdev_features_t features)
  6978. {
  6979. /* Fix illegal checksum combinations */
  6980. if ((features & NETIF_F_HW_CSUM) &&
  6981. (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  6982. netdev_warn(dev, "mixed HW and IP checksum settings.\n");
  6983. features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
  6984. }
  6985. /* TSO requires that SG is present as well. */
  6986. if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
  6987. netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
  6988. features &= ~NETIF_F_ALL_TSO;
  6989. }
  6990. if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
  6991. !(features & NETIF_F_IP_CSUM)) {
  6992. netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
  6993. features &= ~NETIF_F_TSO;
  6994. features &= ~NETIF_F_TSO_ECN;
  6995. }
  6996. if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
  6997. !(features & NETIF_F_IPV6_CSUM)) {
  6998. netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
  6999. features &= ~NETIF_F_TSO6;
  7000. }
  7001. /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
  7002. if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
  7003. features &= ~NETIF_F_TSO_MANGLEID;
  7004. /* TSO ECN requires that TSO is present as well. */
  7005. if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
  7006. features &= ~NETIF_F_TSO_ECN;
  7007. /* Software GSO depends on SG. */
  7008. if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
  7009. netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
  7010. features &= ~NETIF_F_GSO;
  7011. }
  7012. /* GSO partial features require GSO partial be set */
  7013. if ((features & dev->gso_partial_features) &&
  7014. !(features & NETIF_F_GSO_PARTIAL)) {
  7015. netdev_dbg(dev,
  7016. "Dropping partially supported GSO features since no GSO partial.\n");
  7017. features &= ~dev->gso_partial_features;
  7018. }
  7019. if (!(features & NETIF_F_RXCSUM)) {
  7020. /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
  7021. * successfully merged by hardware must also have the
  7022. * checksum verified by hardware. If the user does not
  7023. * want to enable RXCSUM, logically, we should disable GRO_HW.
  7024. */
  7025. if (features & NETIF_F_GRO_HW) {
  7026. netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
  7027. features &= ~NETIF_F_GRO_HW;
  7028. }
  7029. }
  7030. /* LRO/HW-GRO features cannot be combined with RX-FCS */
  7031. if (features & NETIF_F_RXFCS) {
  7032. if (features & NETIF_F_LRO) {
  7033. netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
  7034. features &= ~NETIF_F_LRO;
  7035. }
  7036. if (features & NETIF_F_GRO_HW) {
  7037. netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
  7038. features &= ~NETIF_F_GRO_HW;
  7039. }
  7040. }
  7041. return features;
  7042. }
  7043. int __netdev_update_features(struct net_device *dev)
  7044. {
  7045. struct net_device *upper, *lower;
  7046. netdev_features_t features;
  7047. struct list_head *iter;
  7048. int err = -1;
  7049. ASSERT_RTNL();
  7050. features = netdev_get_wanted_features(dev);
  7051. if (dev->netdev_ops->ndo_fix_features)
  7052. features = dev->netdev_ops->ndo_fix_features(dev, features);
  7053. /* driver might be less strict about feature dependencies */
  7054. features = netdev_fix_features(dev, features);
  7055. /* some features can't be enabled if they're off an an upper device */
  7056. netdev_for_each_upper_dev_rcu(dev, upper, iter)
  7057. features = netdev_sync_upper_features(dev, upper, features);
  7058. if (dev->features == features)
  7059. goto sync_lower;
  7060. netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
  7061. &dev->features, &features);
  7062. if (dev->netdev_ops->ndo_set_features)
  7063. err = dev->netdev_ops->ndo_set_features(dev, features);
  7064. else
  7065. err = 0;
  7066. if (unlikely(err < 0)) {
  7067. netdev_err(dev,
  7068. "set_features() failed (%d); wanted %pNF, left %pNF\n",
  7069. err, &features, &dev->features);
  7070. /* return non-0 since some features might have changed and
  7071. * it's better to fire a spurious notification than miss it
  7072. */
  7073. return -1;
  7074. }
  7075. sync_lower:
  7076. /* some features must be disabled on lower devices when disabled
  7077. * on an upper device (think: bonding master or bridge)
  7078. */
  7079. netdev_for_each_lower_dev(dev, lower, iter)
  7080. netdev_sync_lower_features(dev, lower, features);
  7081. if (!err) {
  7082. netdev_features_t diff = features ^ dev->features;
  7083. if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
  7084. /* udp_tunnel_{get,drop}_rx_info both need
  7085. * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
  7086. * device, or they won't do anything.
  7087. * Thus we need to update dev->features
  7088. * *before* calling udp_tunnel_get_rx_info,
  7089. * but *after* calling udp_tunnel_drop_rx_info.
  7090. */
  7091. if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
  7092. dev->features = features;
  7093. udp_tunnel_get_rx_info(dev);
  7094. } else {
  7095. udp_tunnel_drop_rx_info(dev);
  7096. }
  7097. }
  7098. if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
  7099. if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
  7100. dev->features = features;
  7101. err |= vlan_get_rx_ctag_filter_info(dev);
  7102. } else {
  7103. vlan_drop_rx_ctag_filter_info(dev);
  7104. }
  7105. }
  7106. if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
  7107. if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
  7108. dev->features = features;
  7109. err |= vlan_get_rx_stag_filter_info(dev);
  7110. } else {
  7111. vlan_drop_rx_stag_filter_info(dev);
  7112. }
  7113. }
  7114. dev->features = features;
  7115. }
  7116. return err < 0 ? 0 : 1;
  7117. }
  7118. /**
  7119. * netdev_update_features - recalculate device features
  7120. * @dev: the device to check
  7121. *
  7122. * Recalculate dev->features set and send notifications if it
  7123. * has changed. Should be called after driver or hardware dependent
  7124. * conditions might have changed that influence the features.
  7125. */
  7126. void netdev_update_features(struct net_device *dev)
  7127. {
  7128. if (__netdev_update_features(dev))
  7129. netdev_features_change(dev);
  7130. }
  7131. EXPORT_SYMBOL(netdev_update_features);
  7132. /**
  7133. * netdev_change_features - recalculate device features
  7134. * @dev: the device to check
  7135. *
  7136. * Recalculate dev->features set and send notifications even
  7137. * if they have not changed. Should be called instead of
  7138. * netdev_update_features() if also dev->vlan_features might
  7139. * have changed to allow the changes to be propagated to stacked
  7140. * VLAN devices.
  7141. */
  7142. void netdev_change_features(struct net_device *dev)
  7143. {
  7144. __netdev_update_features(dev);
  7145. netdev_features_change(dev);
  7146. }
  7147. EXPORT_SYMBOL(netdev_change_features);
  7148. /**
  7149. * netif_stacked_transfer_operstate - transfer operstate
  7150. * @rootdev: the root or lower level device to transfer state from
  7151. * @dev: the device to transfer operstate to
  7152. *
  7153. * Transfer operational state from root to device. This is normally
  7154. * called when a stacking relationship exists between the root
  7155. * device and the device(a leaf device).
  7156. */
  7157. void netif_stacked_transfer_operstate(const struct net_device *rootdev,
  7158. struct net_device *dev)
  7159. {
  7160. if (rootdev->operstate == IF_OPER_DORMANT)
  7161. netif_dormant_on(dev);
  7162. else
  7163. netif_dormant_off(dev);
  7164. if (netif_carrier_ok(rootdev))
  7165. netif_carrier_on(dev);
  7166. else
  7167. netif_carrier_off(dev);
  7168. }
  7169. EXPORT_SYMBOL(netif_stacked_transfer_operstate);
  7170. static int netif_alloc_rx_queues(struct net_device *dev)
  7171. {
  7172. unsigned int i, count = dev->num_rx_queues;
  7173. struct netdev_rx_queue *rx;
  7174. size_t sz = count * sizeof(*rx);
  7175. int err = 0;
  7176. BUG_ON(count < 1);
  7177. rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  7178. if (!rx)
  7179. return -ENOMEM;
  7180. dev->_rx = rx;
  7181. for (i = 0; i < count; i++) {
  7182. rx[i].dev = dev;
  7183. /* XDP RX-queue setup */
  7184. err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
  7185. if (err < 0)
  7186. goto err_rxq_info;
  7187. }
  7188. return 0;
  7189. err_rxq_info:
  7190. /* Rollback successful reg's and free other resources */
  7191. while (i--)
  7192. xdp_rxq_info_unreg(&rx[i].xdp_rxq);
  7193. kvfree(dev->_rx);
  7194. dev->_rx = NULL;
  7195. return err;
  7196. }
  7197. static void netif_free_rx_queues(struct net_device *dev)
  7198. {
  7199. unsigned int i, count = dev->num_rx_queues;
  7200. /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
  7201. if (!dev->_rx)
  7202. return;
  7203. for (i = 0; i < count; i++)
  7204. xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
  7205. kvfree(dev->_rx);
  7206. }
  7207. static void netdev_init_one_queue(struct net_device *dev,
  7208. struct netdev_queue *queue, void *_unused)
  7209. {
  7210. /* Initialize queue lock */
  7211. spin_lock_init(&queue->_xmit_lock);
  7212. netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
  7213. queue->xmit_lock_owner = -1;
  7214. netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
  7215. queue->dev = dev;
  7216. #ifdef CONFIG_BQL
  7217. dql_init(&queue->dql, HZ);
  7218. #endif
  7219. }
  7220. static void netif_free_tx_queues(struct net_device *dev)
  7221. {
  7222. kvfree(dev->_tx);
  7223. }
  7224. static int netif_alloc_netdev_queues(struct net_device *dev)
  7225. {
  7226. unsigned int count = dev->num_tx_queues;
  7227. struct netdev_queue *tx;
  7228. size_t sz = count * sizeof(*tx);
  7229. if (count < 1 || count > 0xffff)
  7230. return -EINVAL;
  7231. tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  7232. if (!tx)
  7233. return -ENOMEM;
  7234. dev->_tx = tx;
  7235. netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
  7236. spin_lock_init(&dev->tx_global_lock);
  7237. return 0;
  7238. }
  7239. void netif_tx_stop_all_queues(struct net_device *dev)
  7240. {
  7241. unsigned int i;
  7242. for (i = 0; i < dev->num_tx_queues; i++) {
  7243. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  7244. netif_tx_stop_queue(txq);
  7245. }
  7246. }
  7247. EXPORT_SYMBOL(netif_tx_stop_all_queues);
  7248. /**
  7249. * register_netdevice - register a network device
  7250. * @dev: device to register
  7251. *
  7252. * Take a completed network device structure and add it to the kernel
  7253. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  7254. * chain. 0 is returned on success. A negative errno code is returned
  7255. * on a failure to set up the device, or if the name is a duplicate.
  7256. *
  7257. * Callers must hold the rtnl semaphore. You may want
  7258. * register_netdev() instead of this.
  7259. *
  7260. * BUGS:
  7261. * The locking appears insufficient to guarantee two parallel registers
  7262. * will not get the same name.
  7263. */
  7264. int register_netdevice(struct net_device *dev)
  7265. {
  7266. int ret;
  7267. struct net *net = dev_net(dev);
  7268. BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
  7269. NETDEV_FEATURE_COUNT);
  7270. BUG_ON(dev_boot_phase);
  7271. ASSERT_RTNL();
  7272. might_sleep();
  7273. /* When net_device's are persistent, this will be fatal. */
  7274. BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
  7275. BUG_ON(!net);
  7276. spin_lock_init(&dev->addr_list_lock);
  7277. netdev_set_addr_lockdep_class(dev);
  7278. ret = dev_get_valid_name(net, dev, dev->name);
  7279. if (ret < 0)
  7280. goto out;
  7281. /* Init, if this function is available */
  7282. if (dev->netdev_ops->ndo_init) {
  7283. ret = dev->netdev_ops->ndo_init(dev);
  7284. if (ret) {
  7285. if (ret > 0)
  7286. ret = -EIO;
  7287. goto out;
  7288. }
  7289. }
  7290. if (((dev->hw_features | dev->features) &
  7291. NETIF_F_HW_VLAN_CTAG_FILTER) &&
  7292. (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
  7293. !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
  7294. netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
  7295. ret = -EINVAL;
  7296. goto err_uninit;
  7297. }
  7298. ret = -EBUSY;
  7299. if (!dev->ifindex)
  7300. dev->ifindex = dev_new_index(net);
  7301. else if (__dev_get_by_index(net, dev->ifindex))
  7302. goto err_uninit;
  7303. /* Transfer changeable features to wanted_features and enable
  7304. * software offloads (GSO and GRO).
  7305. */
  7306. dev->hw_features |= NETIF_F_SOFT_FEATURES;
  7307. dev->features |= NETIF_F_SOFT_FEATURES;
  7308. if (dev->netdev_ops->ndo_udp_tunnel_add) {
  7309. dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
  7310. dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
  7311. }
  7312. dev->wanted_features = dev->features & dev->hw_features;
  7313. if (!(dev->flags & IFF_LOOPBACK))
  7314. dev->hw_features |= NETIF_F_NOCACHE_COPY;
  7315. /* If IPv4 TCP segmentation offload is supported we should also
  7316. * allow the device to enable segmenting the frame with the option
  7317. * of ignoring a static IP ID value. This doesn't enable the
  7318. * feature itself but allows the user to enable it later.
  7319. */
  7320. if (dev->hw_features & NETIF_F_TSO)
  7321. dev->hw_features |= NETIF_F_TSO_MANGLEID;
  7322. if (dev->vlan_features & NETIF_F_TSO)
  7323. dev->vlan_features |= NETIF_F_TSO_MANGLEID;
  7324. if (dev->mpls_features & NETIF_F_TSO)
  7325. dev->mpls_features |= NETIF_F_TSO_MANGLEID;
  7326. if (dev->hw_enc_features & NETIF_F_TSO)
  7327. dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  7328. /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
  7329. */
  7330. dev->vlan_features |= NETIF_F_HIGHDMA;
  7331. /* Make NETIF_F_SG inheritable to tunnel devices.
  7332. */
  7333. dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
  7334. /* Make NETIF_F_SG inheritable to MPLS.
  7335. */
  7336. dev->mpls_features |= NETIF_F_SG;
  7337. ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
  7338. ret = notifier_to_errno(ret);
  7339. if (ret)
  7340. goto err_uninit;
  7341. ret = netdev_register_kobject(dev);
  7342. if (ret) {
  7343. dev->reg_state = NETREG_UNREGISTERED;
  7344. goto err_uninit;
  7345. }
  7346. dev->reg_state = NETREG_REGISTERED;
  7347. __netdev_update_features(dev);
  7348. /*
  7349. * Default initial state at registry is that the
  7350. * device is present.
  7351. */
  7352. set_bit(__LINK_STATE_PRESENT, &dev->state);
  7353. linkwatch_init_dev(dev);
  7354. dev_init_scheduler(dev);
  7355. dev_hold(dev);
  7356. list_netdevice(dev);
  7357. add_device_randomness(dev->dev_addr, dev->addr_len);
  7358. /* If the device has permanent device address, driver should
  7359. * set dev_addr and also addr_assign_type should be set to
  7360. * NET_ADDR_PERM (default value).
  7361. */
  7362. if (dev->addr_assign_type == NET_ADDR_PERM)
  7363. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  7364. /* Notify protocols, that a new device appeared. */
  7365. ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
  7366. ret = notifier_to_errno(ret);
  7367. if (ret) {
  7368. rollback_registered(dev);
  7369. rcu_barrier();
  7370. dev->reg_state = NETREG_UNREGISTERED;
  7371. }
  7372. /*
  7373. * Prevent userspace races by waiting until the network
  7374. * device is fully setup before sending notifications.
  7375. */
  7376. if (!dev->rtnl_link_ops ||
  7377. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  7378. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
  7379. out:
  7380. return ret;
  7381. err_uninit:
  7382. if (dev->netdev_ops->ndo_uninit)
  7383. dev->netdev_ops->ndo_uninit(dev);
  7384. if (dev->priv_destructor)
  7385. dev->priv_destructor(dev);
  7386. goto out;
  7387. }
  7388. EXPORT_SYMBOL(register_netdevice);
  7389. /**
  7390. * init_dummy_netdev - init a dummy network device for NAPI
  7391. * @dev: device to init
  7392. *
  7393. * This takes a network device structure and initialize the minimum
  7394. * amount of fields so it can be used to schedule NAPI polls without
  7395. * registering a full blown interface. This is to be used by drivers
  7396. * that need to tie several hardware interfaces to a single NAPI
  7397. * poll scheduler due to HW limitations.
  7398. */
  7399. int init_dummy_netdev(struct net_device *dev)
  7400. {
  7401. /* Clear everything. Note we don't initialize spinlocks
  7402. * are they aren't supposed to be taken by any of the
  7403. * NAPI code and this dummy netdev is supposed to be
  7404. * only ever used for NAPI polls
  7405. */
  7406. memset(dev, 0, sizeof(struct net_device));
  7407. /* make sure we BUG if trying to hit standard
  7408. * register/unregister code path
  7409. */
  7410. dev->reg_state = NETREG_DUMMY;
  7411. /* NAPI wants this */
  7412. INIT_LIST_HEAD(&dev->napi_list);
  7413. /* a dummy interface is started by default */
  7414. set_bit(__LINK_STATE_PRESENT, &dev->state);
  7415. set_bit(__LINK_STATE_START, &dev->state);
  7416. /* napi_busy_loop stats accounting wants this */
  7417. dev_net_set(dev, &init_net);
  7418. /* Note : We dont allocate pcpu_refcnt for dummy devices,
  7419. * because users of this 'device' dont need to change
  7420. * its refcount.
  7421. */
  7422. return 0;
  7423. }
  7424. EXPORT_SYMBOL_GPL(init_dummy_netdev);
  7425. /**
  7426. * register_netdev - register a network device
  7427. * @dev: device to register
  7428. *
  7429. * Take a completed network device structure and add it to the kernel
  7430. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  7431. * chain. 0 is returned on success. A negative errno code is returned
  7432. * on a failure to set up the device, or if the name is a duplicate.
  7433. *
  7434. * This is a wrapper around register_netdevice that takes the rtnl semaphore
  7435. * and expands the device name if you passed a format string to
  7436. * alloc_netdev.
  7437. */
  7438. int register_netdev(struct net_device *dev)
  7439. {
  7440. int err;
  7441. if (rtnl_lock_killable())
  7442. return -EINTR;
  7443. err = register_netdevice(dev);
  7444. rtnl_unlock();
  7445. return err;
  7446. }
  7447. EXPORT_SYMBOL(register_netdev);
  7448. int netdev_refcnt_read(const struct net_device *dev)
  7449. {
  7450. int i, refcnt = 0;
  7451. for_each_possible_cpu(i)
  7452. refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
  7453. return refcnt;
  7454. }
  7455. EXPORT_SYMBOL(netdev_refcnt_read);
  7456. /**
  7457. * netdev_wait_allrefs - wait until all references are gone.
  7458. * @dev: target net_device
  7459. *
  7460. * This is called when unregistering network devices.
  7461. *
  7462. * Any protocol or device that holds a reference should register
  7463. * for netdevice notification, and cleanup and put back the
  7464. * reference if they receive an UNREGISTER event.
  7465. * We can get stuck here if buggy protocols don't correctly
  7466. * call dev_put.
  7467. */
  7468. static void netdev_wait_allrefs(struct net_device *dev)
  7469. {
  7470. unsigned long rebroadcast_time, warning_time;
  7471. int refcnt;
  7472. linkwatch_forget_dev(dev);
  7473. rebroadcast_time = warning_time = jiffies;
  7474. refcnt = netdev_refcnt_read(dev);
  7475. while (refcnt != 0) {
  7476. if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
  7477. rtnl_lock();
  7478. /* Rebroadcast unregister notification */
  7479. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  7480. __rtnl_unlock();
  7481. rcu_barrier();
  7482. rtnl_lock();
  7483. if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
  7484. &dev->state)) {
  7485. /* We must not have linkwatch events
  7486. * pending on unregister. If this
  7487. * happens, we simply run the queue
  7488. * unscheduled, resulting in a noop
  7489. * for this device.
  7490. */
  7491. linkwatch_run_queue();
  7492. }
  7493. __rtnl_unlock();
  7494. rebroadcast_time = jiffies;
  7495. }
  7496. msleep(250);
  7497. refcnt = netdev_refcnt_read(dev);
  7498. if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
  7499. pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
  7500. dev->name, refcnt);
  7501. warning_time = jiffies;
  7502. }
  7503. }
  7504. }
  7505. /* The sequence is:
  7506. *
  7507. * rtnl_lock();
  7508. * ...
  7509. * register_netdevice(x1);
  7510. * register_netdevice(x2);
  7511. * ...
  7512. * unregister_netdevice(y1);
  7513. * unregister_netdevice(y2);
  7514. * ...
  7515. * rtnl_unlock();
  7516. * free_netdev(y1);
  7517. * free_netdev(y2);
  7518. *
  7519. * We are invoked by rtnl_unlock().
  7520. * This allows us to deal with problems:
  7521. * 1) We can delete sysfs objects which invoke hotplug
  7522. * without deadlocking with linkwatch via keventd.
  7523. * 2) Since we run with the RTNL semaphore not held, we can sleep
  7524. * safely in order to wait for the netdev refcnt to drop to zero.
  7525. *
  7526. * We must not return until all unregister events added during
  7527. * the interval the lock was held have been completed.
  7528. */
  7529. void netdev_run_todo(void)
  7530. {
  7531. struct list_head list;
  7532. /* Snapshot list, allow later requests */
  7533. list_replace_init(&net_todo_list, &list);
  7534. __rtnl_unlock();
  7535. /* Wait for rcu callbacks to finish before next phase */
  7536. if (!list_empty(&list))
  7537. rcu_barrier();
  7538. while (!list_empty(&list)) {
  7539. struct net_device *dev
  7540. = list_first_entry(&list, struct net_device, todo_list);
  7541. list_del(&dev->todo_list);
  7542. if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
  7543. pr_err("network todo '%s' but state %d\n",
  7544. dev->name, dev->reg_state);
  7545. dump_stack();
  7546. continue;
  7547. }
  7548. dev->reg_state = NETREG_UNREGISTERED;
  7549. netdev_wait_allrefs(dev);
  7550. /* paranoia */
  7551. BUG_ON(netdev_refcnt_read(dev));
  7552. BUG_ON(!list_empty(&dev->ptype_all));
  7553. BUG_ON(!list_empty(&dev->ptype_specific));
  7554. WARN_ON(rcu_access_pointer(dev->ip_ptr));
  7555. WARN_ON(rcu_access_pointer(dev->ip6_ptr));
  7556. #if IS_ENABLED(CONFIG_DECNET)
  7557. WARN_ON(dev->dn_ptr);
  7558. #endif
  7559. if (dev->priv_destructor)
  7560. dev->priv_destructor(dev);
  7561. if (dev->needs_free_netdev)
  7562. free_netdev(dev);
  7563. /* Report a network device has been unregistered */
  7564. rtnl_lock();
  7565. dev_net(dev)->dev_unreg_count--;
  7566. __rtnl_unlock();
  7567. wake_up(&netdev_unregistering_wq);
  7568. /* Free network device */
  7569. kobject_put(&dev->dev.kobj);
  7570. }
  7571. }
  7572. /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
  7573. * all the same fields in the same order as net_device_stats, with only
  7574. * the type differing, but rtnl_link_stats64 may have additional fields
  7575. * at the end for newer counters.
  7576. */
  7577. void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
  7578. const struct net_device_stats *netdev_stats)
  7579. {
  7580. #if BITS_PER_LONG == 64
  7581. BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
  7582. memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
  7583. /* zero out counters that only exist in rtnl_link_stats64 */
  7584. memset((char *)stats64 + sizeof(*netdev_stats), 0,
  7585. sizeof(*stats64) - sizeof(*netdev_stats));
  7586. #else
  7587. size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
  7588. const unsigned long *src = (const unsigned long *)netdev_stats;
  7589. u64 *dst = (u64 *)stats64;
  7590. BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
  7591. for (i = 0; i < n; i++)
  7592. dst[i] = src[i];
  7593. /* zero out counters that only exist in rtnl_link_stats64 */
  7594. memset((char *)stats64 + n * sizeof(u64), 0,
  7595. sizeof(*stats64) - n * sizeof(u64));
  7596. #endif
  7597. }
  7598. EXPORT_SYMBOL(netdev_stats_to_stats64);
  7599. /**
  7600. * dev_get_stats - get network device statistics
  7601. * @dev: device to get statistics from
  7602. * @storage: place to store stats
  7603. *
  7604. * Get network statistics from device. Return @storage.
  7605. * The device driver may provide its own method by setting
  7606. * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
  7607. * otherwise the internal statistics structure is used.
  7608. */
  7609. struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
  7610. struct rtnl_link_stats64 *storage)
  7611. {
  7612. const struct net_device_ops *ops = dev->netdev_ops;
  7613. if (ops->ndo_get_stats64) {
  7614. memset(storage, 0, sizeof(*storage));
  7615. ops->ndo_get_stats64(dev, storage);
  7616. } else if (ops->ndo_get_stats) {
  7617. netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
  7618. } else {
  7619. netdev_stats_to_stats64(storage, &dev->stats);
  7620. }
  7621. storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
  7622. storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
  7623. storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
  7624. return storage;
  7625. }
  7626. EXPORT_SYMBOL(dev_get_stats);
  7627. struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
  7628. {
  7629. struct netdev_queue *queue = dev_ingress_queue(dev);
  7630. #ifdef CONFIG_NET_CLS_ACT
  7631. if (queue)
  7632. return queue;
  7633. queue = kzalloc(sizeof(*queue), GFP_KERNEL);
  7634. if (!queue)
  7635. return NULL;
  7636. netdev_init_one_queue(dev, queue, NULL);
  7637. RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
  7638. queue->qdisc_sleeping = &noop_qdisc;
  7639. rcu_assign_pointer(dev->ingress_queue, queue);
  7640. #endif
  7641. return queue;
  7642. }
  7643. static const struct ethtool_ops default_ethtool_ops;
  7644. void netdev_set_default_ethtool_ops(struct net_device *dev,
  7645. const struct ethtool_ops *ops)
  7646. {
  7647. if (dev->ethtool_ops == &default_ethtool_ops)
  7648. dev->ethtool_ops = ops;
  7649. }
  7650. EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
  7651. void netdev_freemem(struct net_device *dev)
  7652. {
  7653. char *addr = (char *)dev - dev->padded;
  7654. kvfree(addr);
  7655. }
  7656. /**
  7657. * alloc_netdev_mqs - allocate network device
  7658. * @sizeof_priv: size of private data to allocate space for
  7659. * @name: device name format string
  7660. * @name_assign_type: origin of device name
  7661. * @setup: callback to initialize device
  7662. * @txqs: the number of TX subqueues to allocate
  7663. * @rxqs: the number of RX subqueues to allocate
  7664. *
  7665. * Allocates a struct net_device with private data area for driver use
  7666. * and performs basic initialization. Also allocates subqueue structs
  7667. * for each queue on the device.
  7668. */
  7669. struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
  7670. unsigned char name_assign_type,
  7671. void (*setup)(struct net_device *),
  7672. unsigned int txqs, unsigned int rxqs)
  7673. {
  7674. struct net_device *dev;
  7675. unsigned int alloc_size;
  7676. struct net_device *p;
  7677. BUG_ON(strlen(name) >= sizeof(dev->name));
  7678. if (txqs < 1) {
  7679. pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
  7680. return NULL;
  7681. }
  7682. if (rxqs < 1) {
  7683. pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
  7684. return NULL;
  7685. }
  7686. alloc_size = sizeof(struct net_device);
  7687. if (sizeof_priv) {
  7688. /* ensure 32-byte alignment of private area */
  7689. alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
  7690. alloc_size += sizeof_priv;
  7691. }
  7692. /* ensure 32-byte alignment of whole construct */
  7693. alloc_size += NETDEV_ALIGN - 1;
  7694. p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  7695. if (!p)
  7696. return NULL;
  7697. dev = PTR_ALIGN(p, NETDEV_ALIGN);
  7698. dev->padded = (char *)dev - (char *)p;
  7699. dev->pcpu_refcnt = alloc_percpu(int);
  7700. if (!dev->pcpu_refcnt)
  7701. goto free_dev;
  7702. if (dev_addr_init(dev))
  7703. goto free_pcpu;
  7704. dev_mc_init(dev);
  7705. dev_uc_init(dev);
  7706. dev_net_set(dev, &init_net);
  7707. dev->gso_max_size = GSO_MAX_SIZE;
  7708. dev->gso_max_segs = GSO_MAX_SEGS;
  7709. dev->upper_level = 1;
  7710. dev->lower_level = 1;
  7711. INIT_LIST_HEAD(&dev->napi_list);
  7712. INIT_LIST_HEAD(&dev->unreg_list);
  7713. INIT_LIST_HEAD(&dev->close_list);
  7714. INIT_LIST_HEAD(&dev->link_watch_list);
  7715. INIT_LIST_HEAD(&dev->adj_list.upper);
  7716. INIT_LIST_HEAD(&dev->adj_list.lower);
  7717. INIT_LIST_HEAD(&dev->ptype_all);
  7718. INIT_LIST_HEAD(&dev->ptype_specific);
  7719. #ifdef CONFIG_NET_SCHED
  7720. hash_init(dev->qdisc_hash);
  7721. #endif
  7722. dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
  7723. setup(dev);
  7724. if (!dev->tx_queue_len) {
  7725. dev->priv_flags |= IFF_NO_QUEUE;
  7726. dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
  7727. }
  7728. dev->num_tx_queues = txqs;
  7729. dev->real_num_tx_queues = txqs;
  7730. if (netif_alloc_netdev_queues(dev))
  7731. goto free_all;
  7732. dev->num_rx_queues = rxqs;
  7733. dev->real_num_rx_queues = rxqs;
  7734. if (netif_alloc_rx_queues(dev))
  7735. goto free_all;
  7736. strcpy(dev->name, name);
  7737. dev->name_assign_type = name_assign_type;
  7738. dev->group = INIT_NETDEV_GROUP;
  7739. if (!dev->ethtool_ops)
  7740. dev->ethtool_ops = &default_ethtool_ops;
  7741. nf_hook_ingress_init(dev);
  7742. return dev;
  7743. free_all:
  7744. free_netdev(dev);
  7745. return NULL;
  7746. free_pcpu:
  7747. free_percpu(dev->pcpu_refcnt);
  7748. free_dev:
  7749. netdev_freemem(dev);
  7750. return NULL;
  7751. }
  7752. EXPORT_SYMBOL(alloc_netdev_mqs);
  7753. /**
  7754. * free_netdev - free network device
  7755. * @dev: device
  7756. *
  7757. * This function does the last stage of destroying an allocated device
  7758. * interface. The reference to the device object is released. If this
  7759. * is the last reference then it will be freed.Must be called in process
  7760. * context.
  7761. */
  7762. void free_netdev(struct net_device *dev)
  7763. {
  7764. struct napi_struct *p, *n;
  7765. might_sleep();
  7766. netif_free_tx_queues(dev);
  7767. netif_free_rx_queues(dev);
  7768. kfree(rcu_dereference_protected(dev->ingress_queue, 1));
  7769. /* Flush device addresses */
  7770. dev_addr_flush(dev);
  7771. list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
  7772. netif_napi_del(p);
  7773. free_percpu(dev->pcpu_refcnt);
  7774. dev->pcpu_refcnt = NULL;
  7775. /* Compatibility with error handling in drivers */
  7776. if (dev->reg_state == NETREG_UNINITIALIZED) {
  7777. netdev_freemem(dev);
  7778. return;
  7779. }
  7780. BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
  7781. dev->reg_state = NETREG_RELEASED;
  7782. /* will free via device release */
  7783. put_device(&dev->dev);
  7784. }
  7785. EXPORT_SYMBOL(free_netdev);
  7786. /**
  7787. * synchronize_net - Synchronize with packet receive processing
  7788. *
  7789. * Wait for packets currently being received to be done.
  7790. * Does not block later packets from starting.
  7791. */
  7792. void synchronize_net(void)
  7793. {
  7794. might_sleep();
  7795. if (rtnl_is_locked())
  7796. synchronize_rcu_expedited();
  7797. else
  7798. synchronize_rcu();
  7799. }
  7800. EXPORT_SYMBOL(synchronize_net);
  7801. /**
  7802. * unregister_netdevice_queue - remove device from the kernel
  7803. * @dev: device
  7804. * @head: list
  7805. *
  7806. * This function shuts down a device interface and removes it
  7807. * from the kernel tables.
  7808. * If head not NULL, device is queued to be unregistered later.
  7809. *
  7810. * Callers must hold the rtnl semaphore. You may want
  7811. * unregister_netdev() instead of this.
  7812. */
  7813. void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
  7814. {
  7815. ASSERT_RTNL();
  7816. if (head) {
  7817. list_move_tail(&dev->unreg_list, head);
  7818. } else {
  7819. rollback_registered(dev);
  7820. /* Finish processing unregister after unlock */
  7821. net_set_todo(dev);
  7822. }
  7823. }
  7824. EXPORT_SYMBOL(unregister_netdevice_queue);
  7825. /**
  7826. * unregister_netdevice_many - unregister many devices
  7827. * @head: list of devices
  7828. *
  7829. * Note: As most callers use a stack allocated list_head,
  7830. * we force a list_del() to make sure stack wont be corrupted later.
  7831. */
  7832. void unregister_netdevice_many(struct list_head *head)
  7833. {
  7834. struct net_device *dev;
  7835. if (!list_empty(head)) {
  7836. rollback_registered_many(head);
  7837. list_for_each_entry(dev, head, unreg_list)
  7838. net_set_todo(dev);
  7839. list_del(head);
  7840. }
  7841. }
  7842. EXPORT_SYMBOL(unregister_netdevice_many);
  7843. /**
  7844. * unregister_netdev - remove device from the kernel
  7845. * @dev: device
  7846. *
  7847. * This function shuts down a device interface and removes it
  7848. * from the kernel tables.
  7849. *
  7850. * This is just a wrapper for unregister_netdevice that takes
  7851. * the rtnl semaphore. In general you want to use this and not
  7852. * unregister_netdevice.
  7853. */
  7854. void unregister_netdev(struct net_device *dev)
  7855. {
  7856. rtnl_lock();
  7857. unregister_netdevice(dev);
  7858. rtnl_unlock();
  7859. }
  7860. EXPORT_SYMBOL(unregister_netdev);
  7861. /**
  7862. * dev_change_net_namespace - move device to different nethost namespace
  7863. * @dev: device
  7864. * @net: network namespace
  7865. * @pat: If not NULL name pattern to try if the current device name
  7866. * is already taken in the destination network namespace.
  7867. *
  7868. * This function shuts down a device interface and moves it
  7869. * to a new network namespace. On success 0 is returned, on
  7870. * a failure a netagive errno code is returned.
  7871. *
  7872. * Callers must hold the rtnl semaphore.
  7873. */
  7874. int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
  7875. {
  7876. int err, new_nsid, new_ifindex;
  7877. ASSERT_RTNL();
  7878. /* Don't allow namespace local devices to be moved. */
  7879. err = -EINVAL;
  7880. if (dev->features & NETIF_F_NETNS_LOCAL)
  7881. goto out;
  7882. /* Ensure the device has been registrered */
  7883. if (dev->reg_state != NETREG_REGISTERED)
  7884. goto out;
  7885. /* Get out if there is nothing todo */
  7886. err = 0;
  7887. if (net_eq(dev_net(dev), net))
  7888. goto out;
  7889. /* Pick the destination device name, and ensure
  7890. * we can use it in the destination network namespace.
  7891. */
  7892. err = -EEXIST;
  7893. if (__dev_get_by_name(net, dev->name)) {
  7894. /* We get here if we can't use the current device name */
  7895. if (!pat)
  7896. goto out;
  7897. err = dev_get_valid_name(net, dev, pat);
  7898. if (err < 0)
  7899. goto out;
  7900. }
  7901. /*
  7902. * And now a mini version of register_netdevice unregister_netdevice.
  7903. */
  7904. /* If device is running close it first. */
  7905. dev_close(dev);
  7906. /* And unlink it from device chain */
  7907. unlist_netdevice(dev);
  7908. synchronize_net();
  7909. /* Shutdown queueing discipline. */
  7910. dev_shutdown(dev);
  7911. /* Notify protocols, that we are about to destroy
  7912. * this device. They should clean all the things.
  7913. *
  7914. * Note that dev->reg_state stays at NETREG_REGISTERED.
  7915. * This is wanted because this way 8021q and macvlan know
  7916. * the device is just moving and can keep their slaves up.
  7917. */
  7918. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  7919. rcu_barrier();
  7920. new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
  7921. /* If there is an ifindex conflict assign a new one */
  7922. if (__dev_get_by_index(net, dev->ifindex))
  7923. new_ifindex = dev_new_index(net);
  7924. else
  7925. new_ifindex = dev->ifindex;
  7926. rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
  7927. new_ifindex);
  7928. /*
  7929. * Flush the unicast and multicast chains
  7930. */
  7931. dev_uc_flush(dev);
  7932. dev_mc_flush(dev);
  7933. /* Send a netdev-removed uevent to the old namespace */
  7934. kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
  7935. netdev_adjacent_del_links(dev);
  7936. /* Actually switch the network namespace */
  7937. dev_net_set(dev, net);
  7938. dev->ifindex = new_ifindex;
  7939. /* Send a netdev-add uevent to the new namespace */
  7940. kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
  7941. netdev_adjacent_add_links(dev);
  7942. /* Fixup kobjects */
  7943. err = device_rename(&dev->dev, dev->name);
  7944. WARN_ON(err);
  7945. /* Add the device back in the hashes */
  7946. list_netdevice(dev);
  7947. /* Notify protocols, that a new device appeared. */
  7948. call_netdevice_notifiers(NETDEV_REGISTER, dev);
  7949. /*
  7950. * Prevent userspace races by waiting until the network
  7951. * device is fully setup before sending notifications.
  7952. */
  7953. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
  7954. synchronize_net();
  7955. err = 0;
  7956. out:
  7957. return err;
  7958. }
  7959. EXPORT_SYMBOL_GPL(dev_change_net_namespace);
  7960. static int dev_cpu_dead(unsigned int oldcpu)
  7961. {
  7962. struct sk_buff **list_skb;
  7963. struct sk_buff *skb;
  7964. unsigned int cpu;
  7965. struct softnet_data *sd, *oldsd, *remsd = NULL;
  7966. local_irq_disable();
  7967. cpu = smp_processor_id();
  7968. sd = &per_cpu(softnet_data, cpu);
  7969. oldsd = &per_cpu(softnet_data, oldcpu);
  7970. /* Find end of our completion_queue. */
  7971. list_skb = &sd->completion_queue;
  7972. while (*list_skb)
  7973. list_skb = &(*list_skb)->next;
  7974. /* Append completion queue from offline CPU. */
  7975. *list_skb = oldsd->completion_queue;
  7976. oldsd->completion_queue = NULL;
  7977. /* Append output queue from offline CPU. */
  7978. if (oldsd->output_queue) {
  7979. *sd->output_queue_tailp = oldsd->output_queue;
  7980. sd->output_queue_tailp = oldsd->output_queue_tailp;
  7981. oldsd->output_queue = NULL;
  7982. oldsd->output_queue_tailp = &oldsd->output_queue;
  7983. }
  7984. /* Append NAPI poll list from offline CPU, with one exception :
  7985. * process_backlog() must be called by cpu owning percpu backlog.
  7986. * We properly handle process_queue & input_pkt_queue later.
  7987. */
  7988. while (!list_empty(&oldsd->poll_list)) {
  7989. struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
  7990. struct napi_struct,
  7991. poll_list);
  7992. list_del_init(&napi->poll_list);
  7993. if (napi->poll == process_backlog)
  7994. napi->state = 0;
  7995. else
  7996. ____napi_schedule(sd, napi);
  7997. }
  7998. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  7999. local_irq_enable();
  8000. #ifdef CONFIG_RPS
  8001. remsd = oldsd->rps_ipi_list;
  8002. oldsd->rps_ipi_list = NULL;
  8003. #endif
  8004. /* send out pending IPI's on offline CPU */
  8005. net_rps_send_ipi(remsd);
  8006. /* Process offline CPU's input_pkt_queue */
  8007. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  8008. netif_rx_ni(skb);
  8009. input_queue_head_incr(oldsd);
  8010. }
  8011. while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  8012. netif_rx_ni(skb);
  8013. input_queue_head_incr(oldsd);
  8014. }
  8015. return 0;
  8016. }
  8017. /**
  8018. * netdev_increment_features - increment feature set by one
  8019. * @all: current feature set
  8020. * @one: new feature set
  8021. * @mask: mask feature set
  8022. *
  8023. * Computes a new feature set after adding a device with feature set
  8024. * @one to the master device with current feature set @all. Will not
  8025. * enable anything that is off in @mask. Returns the new feature set.
  8026. */
  8027. netdev_features_t netdev_increment_features(netdev_features_t all,
  8028. netdev_features_t one, netdev_features_t mask)
  8029. {
  8030. if (mask & NETIF_F_HW_CSUM)
  8031. mask |= NETIF_F_CSUM_MASK;
  8032. mask |= NETIF_F_VLAN_CHALLENGED;
  8033. all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
  8034. all &= one | ~NETIF_F_ALL_FOR_ALL;
  8035. /* If one device supports hw checksumming, set for all. */
  8036. if (all & NETIF_F_HW_CSUM)
  8037. all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
  8038. return all;
  8039. }
  8040. EXPORT_SYMBOL(netdev_increment_features);
  8041. static struct hlist_head * __net_init netdev_create_hash(void)
  8042. {
  8043. int i;
  8044. struct hlist_head *hash;
  8045. hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
  8046. if (hash != NULL)
  8047. for (i = 0; i < NETDEV_HASHENTRIES; i++)
  8048. INIT_HLIST_HEAD(&hash[i]);
  8049. return hash;
  8050. }
  8051. /* Initialize per network namespace state */
  8052. static int __net_init netdev_init(struct net *net)
  8053. {
  8054. BUILD_BUG_ON(GRO_HASH_BUCKETS >
  8055. 8 * FIELD_SIZEOF(struct napi_struct, gro_bitmask));
  8056. if (net != &init_net)
  8057. INIT_LIST_HEAD(&net->dev_base_head);
  8058. net->dev_name_head = netdev_create_hash();
  8059. if (net->dev_name_head == NULL)
  8060. goto err_name;
  8061. net->dev_index_head = netdev_create_hash();
  8062. if (net->dev_index_head == NULL)
  8063. goto err_idx;
  8064. return 0;
  8065. err_idx:
  8066. kfree(net->dev_name_head);
  8067. err_name:
  8068. return -ENOMEM;
  8069. }
  8070. /**
  8071. * netdev_drivername - network driver for the device
  8072. * @dev: network device
  8073. *
  8074. * Determine network driver for device.
  8075. */
  8076. const char *netdev_drivername(const struct net_device *dev)
  8077. {
  8078. const struct device_driver *driver;
  8079. const struct device *parent;
  8080. const char *empty = "";
  8081. parent = dev->dev.parent;
  8082. if (!parent)
  8083. return empty;
  8084. driver = parent->driver;
  8085. if (driver && driver->name)
  8086. return driver->name;
  8087. return empty;
  8088. }
  8089. static void __netdev_printk(const char *level, const struct net_device *dev,
  8090. struct va_format *vaf)
  8091. {
  8092. if (dev && dev->dev.parent) {
  8093. dev_printk_emit(level[1] - '0',
  8094. dev->dev.parent,
  8095. "%s %s %s%s: %pV",
  8096. dev_driver_string(dev->dev.parent),
  8097. dev_name(dev->dev.parent),
  8098. netdev_name(dev), netdev_reg_state(dev),
  8099. vaf);
  8100. } else if (dev) {
  8101. printk("%s%s%s: %pV",
  8102. level, netdev_name(dev), netdev_reg_state(dev), vaf);
  8103. } else {
  8104. printk("%s(NULL net_device): %pV", level, vaf);
  8105. }
  8106. }
  8107. void netdev_printk(const char *level, const struct net_device *dev,
  8108. const char *format, ...)
  8109. {
  8110. struct va_format vaf;
  8111. va_list args;
  8112. va_start(args, format);
  8113. vaf.fmt = format;
  8114. vaf.va = &args;
  8115. __netdev_printk(level, dev, &vaf);
  8116. va_end(args);
  8117. }
  8118. EXPORT_SYMBOL(netdev_printk);
  8119. #define define_netdev_printk_level(func, level) \
  8120. void func(const struct net_device *dev, const char *fmt, ...) \
  8121. { \
  8122. struct va_format vaf; \
  8123. va_list args; \
  8124. \
  8125. va_start(args, fmt); \
  8126. \
  8127. vaf.fmt = fmt; \
  8128. vaf.va = &args; \
  8129. \
  8130. __netdev_printk(level, dev, &vaf); \
  8131. \
  8132. va_end(args); \
  8133. } \
  8134. EXPORT_SYMBOL(func);
  8135. define_netdev_printk_level(netdev_emerg, KERN_EMERG);
  8136. define_netdev_printk_level(netdev_alert, KERN_ALERT);
  8137. define_netdev_printk_level(netdev_crit, KERN_CRIT);
  8138. define_netdev_printk_level(netdev_err, KERN_ERR);
  8139. define_netdev_printk_level(netdev_warn, KERN_WARNING);
  8140. define_netdev_printk_level(netdev_notice, KERN_NOTICE);
  8141. define_netdev_printk_level(netdev_info, KERN_INFO);
  8142. static void __net_exit netdev_exit(struct net *net)
  8143. {
  8144. kfree(net->dev_name_head);
  8145. kfree(net->dev_index_head);
  8146. if (net != &init_net)
  8147. WARN_ON_ONCE(!list_empty(&net->dev_base_head));
  8148. }
  8149. static struct pernet_operations __net_initdata netdev_net_ops = {
  8150. .init = netdev_init,
  8151. .exit = netdev_exit,
  8152. };
  8153. static void __net_exit default_device_exit(struct net *net)
  8154. {
  8155. struct net_device *dev, *aux;
  8156. /*
  8157. * Push all migratable network devices back to the
  8158. * initial network namespace
  8159. */
  8160. rtnl_lock();
  8161. for_each_netdev_safe(net, dev, aux) {
  8162. int err;
  8163. char fb_name[IFNAMSIZ];
  8164. /* Ignore unmoveable devices (i.e. loopback) */
  8165. if (dev->features & NETIF_F_NETNS_LOCAL)
  8166. continue;
  8167. /* Leave virtual devices for the generic cleanup */
  8168. if (dev->rtnl_link_ops)
  8169. continue;
  8170. /* Push remaining network devices to init_net */
  8171. snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
  8172. if (__dev_get_by_name(&init_net, fb_name))
  8173. snprintf(fb_name, IFNAMSIZ, "dev%%d");
  8174. err = dev_change_net_namespace(dev, &init_net, fb_name);
  8175. if (err) {
  8176. pr_emerg("%s: failed to move %s to init_net: %d\n",
  8177. __func__, dev->name, err);
  8178. BUG();
  8179. }
  8180. }
  8181. rtnl_unlock();
  8182. }
  8183. static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
  8184. {
  8185. /* Return with the rtnl_lock held when there are no network
  8186. * devices unregistering in any network namespace in net_list.
  8187. */
  8188. struct net *net;
  8189. bool unregistering;
  8190. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  8191. add_wait_queue(&netdev_unregistering_wq, &wait);
  8192. for (;;) {
  8193. unregistering = false;
  8194. rtnl_lock();
  8195. list_for_each_entry(net, net_list, exit_list) {
  8196. if (net->dev_unreg_count > 0) {
  8197. unregistering = true;
  8198. break;
  8199. }
  8200. }
  8201. if (!unregistering)
  8202. break;
  8203. __rtnl_unlock();
  8204. wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  8205. }
  8206. remove_wait_queue(&netdev_unregistering_wq, &wait);
  8207. }
  8208. static void __net_exit default_device_exit_batch(struct list_head *net_list)
  8209. {
  8210. /* At exit all network devices most be removed from a network
  8211. * namespace. Do this in the reverse order of registration.
  8212. * Do this across as many network namespaces as possible to
  8213. * improve batching efficiency.
  8214. */
  8215. struct net_device *dev;
  8216. struct net *net;
  8217. LIST_HEAD(dev_kill_list);
  8218. /* To prevent network device cleanup code from dereferencing
  8219. * loopback devices or network devices that have been freed
  8220. * wait here for all pending unregistrations to complete,
  8221. * before unregistring the loopback device and allowing the
  8222. * network namespace be freed.
  8223. *
  8224. * The netdev todo list containing all network devices
  8225. * unregistrations that happen in default_device_exit_batch
  8226. * will run in the rtnl_unlock() at the end of
  8227. * default_device_exit_batch.
  8228. */
  8229. rtnl_lock_unregistering(net_list);
  8230. list_for_each_entry(net, net_list, exit_list) {
  8231. for_each_netdev_reverse(net, dev) {
  8232. if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
  8233. dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
  8234. else
  8235. unregister_netdevice_queue(dev, &dev_kill_list);
  8236. }
  8237. }
  8238. unregister_netdevice_many(&dev_kill_list);
  8239. rtnl_unlock();
  8240. }
  8241. static struct pernet_operations __net_initdata default_device_ops = {
  8242. .exit = default_device_exit,
  8243. .exit_batch = default_device_exit_batch,
  8244. };
  8245. /*
  8246. * Initialize the DEV module. At boot time this walks the device list and
  8247. * unhooks any devices that fail to initialise (normally hardware not
  8248. * present) and leaves us with a valid list of present and active devices.
  8249. *
  8250. */
  8251. /*
  8252. * This is called single threaded during boot, so no need
  8253. * to take the rtnl semaphore.
  8254. */
  8255. static int __init net_dev_init(void)
  8256. {
  8257. int i, rc = -ENOMEM;
  8258. BUG_ON(!dev_boot_phase);
  8259. if (dev_proc_init())
  8260. goto out;
  8261. if (netdev_kobject_init())
  8262. goto out;
  8263. INIT_LIST_HEAD(&ptype_all);
  8264. for (i = 0; i < PTYPE_HASH_SIZE; i++)
  8265. INIT_LIST_HEAD(&ptype_base[i]);
  8266. INIT_LIST_HEAD(&offload_base);
  8267. if (register_pernet_subsys(&netdev_net_ops))
  8268. goto out;
  8269. /*
  8270. * Initialise the packet receive queues.
  8271. */
  8272. for_each_possible_cpu(i) {
  8273. struct work_struct *flush = per_cpu_ptr(&flush_works, i);
  8274. struct softnet_data *sd = &per_cpu(softnet_data, i);
  8275. INIT_WORK(flush, flush_backlog);
  8276. skb_queue_head_init(&sd->input_pkt_queue);
  8277. skb_queue_head_init(&sd->process_queue);
  8278. #ifdef CONFIG_XFRM_OFFLOAD
  8279. skb_queue_head_init(&sd->xfrm_backlog);
  8280. #endif
  8281. INIT_LIST_HEAD(&sd->poll_list);
  8282. sd->output_queue_tailp = &sd->output_queue;
  8283. #ifdef CONFIG_RPS
  8284. sd->csd.func = rps_trigger_softirq;
  8285. sd->csd.info = sd;
  8286. sd->cpu = i;
  8287. #endif
  8288. init_gro_hash(&sd->backlog);
  8289. sd->backlog.poll = process_backlog;
  8290. sd->backlog.weight = weight_p;
  8291. }
  8292. dev_boot_phase = 0;
  8293. /* The loopback device is special if any other network devices
  8294. * is present in a network namespace the loopback device must
  8295. * be present. Since we now dynamically allocate and free the
  8296. * loopback device ensure this invariant is maintained by
  8297. * keeping the loopback device as the first device on the
  8298. * list of network devices. Ensuring the loopback devices
  8299. * is the first device that appears and the last network device
  8300. * that disappears.
  8301. */
  8302. if (register_pernet_device(&loopback_net_ops))
  8303. goto out;
  8304. if (register_pernet_device(&default_device_ops))
  8305. goto out;
  8306. open_softirq(NET_TX_SOFTIRQ, net_tx_action);
  8307. open_softirq(NET_RX_SOFTIRQ, net_rx_action);
  8308. rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
  8309. NULL, dev_cpu_dead);
  8310. WARN_ON(rc < 0);
  8311. rc = 0;
  8312. out:
  8313. return rc;
  8314. }
  8315. subsys_initcall(net_dev_init);