dev.c 220 KB

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