dev.c 212 KB

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