dev.c 202 KB

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