dev.c 242 KB

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