bond_main.c 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284
  1. /*
  2. * originally based on the dummy device.
  3. *
  4. * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
  5. * Licensed under the GPL. Based on dummy.c, and eql.c devices.
  6. *
  7. * bonding.c: an Ethernet Bonding driver
  8. *
  9. * This is useful to talk to a Cisco EtherChannel compatible equipment:
  10. * Cisco 5500
  11. * Sun Trunking (Solaris)
  12. * Alteon AceDirector Trunks
  13. * Linux Bonding
  14. * and probably many L2 switches ...
  15. *
  16. * How it works:
  17. * ifconfig bond0 ipaddress netmask up
  18. * will setup a network device, with an ip address. No mac address
  19. * will be assigned at this time. The hw mac address will come from
  20. * the first slave bonded to the channel. All slaves will then use
  21. * this hw mac address.
  22. *
  23. * ifconfig bond0 down
  24. * will release all slaves, marking them as down.
  25. *
  26. * ifenslave bond0 eth0
  27. * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
  28. * a: be used as initial mac address
  29. * b: if a hw mac address already is there, eth0's hw mac address
  30. * will then be set from bond0.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/types.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/ptrace.h>
  39. #include <linux/ioport.h>
  40. #include <linux/in.h>
  41. #include <net/ip.h>
  42. #include <linux/ip.h>
  43. #include <linux/tcp.h>
  44. #include <linux/udp.h>
  45. #include <linux/slab.h>
  46. #include <linux/string.h>
  47. #include <linux/init.h>
  48. #include <linux/timer.h>
  49. #include <linux/socket.h>
  50. #include <linux/ctype.h>
  51. #include <linux/inet.h>
  52. #include <linux/bitops.h>
  53. #include <asm/system.h>
  54. #include <asm/io.h>
  55. #include <asm/dma.h>
  56. #include <asm/uaccess.h>
  57. #include <linux/errno.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/inetdevice.h>
  60. #include <linux/igmp.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/skbuff.h>
  63. #include <net/sock.h>
  64. #include <linux/rtnetlink.h>
  65. #include <linux/proc_fs.h>
  66. #include <linux/seq_file.h>
  67. #include <linux/smp.h>
  68. #include <linux/if_ether.h>
  69. #include <net/arp.h>
  70. #include <linux/mii.h>
  71. #include <linux/ethtool.h>
  72. #include <linux/if_vlan.h>
  73. #include <linux/if_bonding.h>
  74. #include <linux/jiffies.h>
  75. #include <net/route.h>
  76. #include <net/net_namespace.h>
  77. #include "bonding.h"
  78. #include "bond_3ad.h"
  79. #include "bond_alb.h"
  80. /*---------------------------- Module parameters ----------------------------*/
  81. /* monitor all links that often (in milliseconds). <=0 disables monitoring */
  82. #define BOND_LINK_MON_INTERV 0
  83. #define BOND_LINK_ARP_INTERV 0
  84. static int max_bonds = BOND_DEFAULT_MAX_BONDS;
  85. static int num_grat_arp = 1;
  86. static int num_unsol_na = 1;
  87. static int miimon = BOND_LINK_MON_INTERV;
  88. static int updelay = 0;
  89. static int downdelay = 0;
  90. static int use_carrier = 1;
  91. static char *mode = NULL;
  92. static char *primary = NULL;
  93. static char *lacp_rate = NULL;
  94. static char *ad_select = NULL;
  95. static char *xmit_hash_policy = NULL;
  96. static int arp_interval = BOND_LINK_ARP_INTERV;
  97. static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
  98. static char *arp_validate = NULL;
  99. static char *fail_over_mac = NULL;
  100. struct bond_params bonding_defaults;
  101. module_param(max_bonds, int, 0);
  102. MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
  103. module_param(num_grat_arp, int, 0644);
  104. MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
  105. module_param(num_unsol_na, int, 0644);
  106. MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
  107. module_param(miimon, int, 0);
  108. MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
  109. module_param(updelay, int, 0);
  110. MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
  111. module_param(downdelay, int, 0);
  112. MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
  113. "in milliseconds");
  114. module_param(use_carrier, int, 0);
  115. MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
  116. "0 for off, 1 for on (default)");
  117. module_param(mode, charp, 0);
  118. MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
  119. "1 for active-backup, 2 for balance-xor, "
  120. "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
  121. "6 for balance-alb");
  122. module_param(primary, charp, 0);
  123. MODULE_PARM_DESC(primary, "Primary network device to use");
  124. module_param(lacp_rate, charp, 0);
  125. MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
  126. "(slow/fast)");
  127. module_param(ad_select, charp, 0);
  128. MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
  129. module_param(xmit_hash_policy, charp, 0);
  130. MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
  131. ", 1 for layer 3+4");
  132. module_param(arp_interval, int, 0);
  133. MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
  134. module_param_array(arp_ip_target, charp, NULL, 0);
  135. MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
  136. module_param(arp_validate, charp, 0);
  137. MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
  138. module_param(fail_over_mac, charp, 0);
  139. MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
  140. /*----------------------------- Global variables ----------------------------*/
  141. static const char * const version =
  142. DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
  143. LIST_HEAD(bond_dev_list);
  144. #ifdef CONFIG_PROC_FS
  145. static struct proc_dir_entry *bond_proc_dir = NULL;
  146. #endif
  147. static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
  148. static int arp_ip_count = 0;
  149. static int bond_mode = BOND_MODE_ROUNDROBIN;
  150. static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
  151. static int lacp_fast = 0;
  152. const struct bond_parm_tbl bond_lacp_tbl[] = {
  153. { "slow", AD_LACP_SLOW},
  154. { "fast", AD_LACP_FAST},
  155. { NULL, -1},
  156. };
  157. const struct bond_parm_tbl bond_mode_tbl[] = {
  158. { "balance-rr", BOND_MODE_ROUNDROBIN},
  159. { "active-backup", BOND_MODE_ACTIVEBACKUP},
  160. { "balance-xor", BOND_MODE_XOR},
  161. { "broadcast", BOND_MODE_BROADCAST},
  162. { "802.3ad", BOND_MODE_8023AD},
  163. { "balance-tlb", BOND_MODE_TLB},
  164. { "balance-alb", BOND_MODE_ALB},
  165. { NULL, -1},
  166. };
  167. const struct bond_parm_tbl xmit_hashtype_tbl[] = {
  168. { "layer2", BOND_XMIT_POLICY_LAYER2},
  169. { "layer3+4", BOND_XMIT_POLICY_LAYER34},
  170. { "layer2+3", BOND_XMIT_POLICY_LAYER23},
  171. { NULL, -1},
  172. };
  173. const struct bond_parm_tbl arp_validate_tbl[] = {
  174. { "none", BOND_ARP_VALIDATE_NONE},
  175. { "active", BOND_ARP_VALIDATE_ACTIVE},
  176. { "backup", BOND_ARP_VALIDATE_BACKUP},
  177. { "all", BOND_ARP_VALIDATE_ALL},
  178. { NULL, -1},
  179. };
  180. const struct bond_parm_tbl fail_over_mac_tbl[] = {
  181. { "none", BOND_FOM_NONE},
  182. { "active", BOND_FOM_ACTIVE},
  183. { "follow", BOND_FOM_FOLLOW},
  184. { NULL, -1},
  185. };
  186. struct bond_parm_tbl ad_select_tbl[] = {
  187. { "stable", BOND_AD_STABLE},
  188. { "bandwidth", BOND_AD_BANDWIDTH},
  189. { "count", BOND_AD_COUNT},
  190. { NULL, -1},
  191. };
  192. /*-------------------------- Forward declarations ---------------------------*/
  193. static void bond_send_gratuitous_arp(struct bonding *bond);
  194. static void bond_deinit(struct net_device *bond_dev);
  195. /*---------------------------- General routines -----------------------------*/
  196. static const char *bond_mode_name(int mode)
  197. {
  198. static const char *names[] = {
  199. [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
  200. [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
  201. [BOND_MODE_XOR] = "load balancing (xor)",
  202. [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
  203. [BOND_MODE_8023AD]= "IEEE 802.3ad Dynamic link aggregation",
  204. [BOND_MODE_TLB] = "transmit load balancing",
  205. [BOND_MODE_ALB] = "adaptive load balancing",
  206. };
  207. if (mode < 0 || mode > BOND_MODE_ALB)
  208. return "unknown";
  209. return names[mode];
  210. }
  211. /*---------------------------------- VLAN -----------------------------------*/
  212. /**
  213. * bond_add_vlan - add a new vlan id on bond
  214. * @bond: bond that got the notification
  215. * @vlan_id: the vlan id to add
  216. *
  217. * Returns -ENOMEM if allocation failed.
  218. */
  219. static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
  220. {
  221. struct vlan_entry *vlan;
  222. pr_debug("bond: %s, vlan id %d\n",
  223. (bond ? bond->dev->name: "None"), vlan_id);
  224. vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
  225. if (!vlan) {
  226. return -ENOMEM;
  227. }
  228. INIT_LIST_HEAD(&vlan->vlan_list);
  229. vlan->vlan_id = vlan_id;
  230. write_lock_bh(&bond->lock);
  231. list_add_tail(&vlan->vlan_list, &bond->vlan_list);
  232. write_unlock_bh(&bond->lock);
  233. pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
  234. return 0;
  235. }
  236. /**
  237. * bond_del_vlan - delete a vlan id from bond
  238. * @bond: bond that got the notification
  239. * @vlan_id: the vlan id to delete
  240. *
  241. * returns -ENODEV if @vlan_id was not found in @bond.
  242. */
  243. static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
  244. {
  245. struct vlan_entry *vlan;
  246. int res = -ENODEV;
  247. pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
  248. write_lock_bh(&bond->lock);
  249. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  250. if (vlan->vlan_id == vlan_id) {
  251. list_del(&vlan->vlan_list);
  252. if (bond_is_lb(bond))
  253. bond_alb_clear_vlan(bond, vlan_id);
  254. pr_debug("removed VLAN ID %d from bond %s\n", vlan_id,
  255. bond->dev->name);
  256. kfree(vlan);
  257. if (list_empty(&bond->vlan_list) &&
  258. (bond->slave_cnt == 0)) {
  259. /* Last VLAN removed and no slaves, so
  260. * restore block on adding VLANs. This will
  261. * be removed once new slaves that are not
  262. * VLAN challenged will be added.
  263. */
  264. bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
  265. }
  266. res = 0;
  267. goto out;
  268. }
  269. }
  270. pr_debug("couldn't find VLAN ID %d in bond %s\n", vlan_id,
  271. bond->dev->name);
  272. out:
  273. write_unlock_bh(&bond->lock);
  274. return res;
  275. }
  276. /**
  277. * bond_has_challenged_slaves
  278. * @bond: the bond we're working on
  279. *
  280. * Searches the slave list. Returns 1 if a vlan challenged slave
  281. * was found, 0 otherwise.
  282. *
  283. * Assumes bond->lock is held.
  284. */
  285. static int bond_has_challenged_slaves(struct bonding *bond)
  286. {
  287. struct slave *slave;
  288. int i;
  289. bond_for_each_slave(bond, slave, i) {
  290. if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
  291. pr_debug("found VLAN challenged slave - %s\n",
  292. slave->dev->name);
  293. return 1;
  294. }
  295. }
  296. pr_debug("no VLAN challenged slaves found\n");
  297. return 0;
  298. }
  299. /**
  300. * bond_next_vlan - safely skip to the next item in the vlans list.
  301. * @bond: the bond we're working on
  302. * @curr: item we're advancing from
  303. *
  304. * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
  305. * or @curr->next otherwise (even if it is @curr itself again).
  306. *
  307. * Caller must hold bond->lock
  308. */
  309. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
  310. {
  311. struct vlan_entry *next, *last;
  312. if (list_empty(&bond->vlan_list)) {
  313. return NULL;
  314. }
  315. if (!curr) {
  316. next = list_entry(bond->vlan_list.next,
  317. struct vlan_entry, vlan_list);
  318. } else {
  319. last = list_entry(bond->vlan_list.prev,
  320. struct vlan_entry, vlan_list);
  321. if (last == curr) {
  322. next = list_entry(bond->vlan_list.next,
  323. struct vlan_entry, vlan_list);
  324. } else {
  325. next = list_entry(curr->vlan_list.next,
  326. struct vlan_entry, vlan_list);
  327. }
  328. }
  329. return next;
  330. }
  331. /**
  332. * bond_dev_queue_xmit - Prepare skb for xmit.
  333. *
  334. * @bond: bond device that got this skb for tx.
  335. * @skb: hw accel VLAN tagged skb to transmit
  336. * @slave_dev: slave that is supposed to xmit this skbuff
  337. *
  338. * When the bond gets an skb to transmit that is
  339. * already hardware accelerated VLAN tagged, and it
  340. * needs to relay this skb to a slave that is not
  341. * hw accel capable, the skb needs to be "unaccelerated",
  342. * i.e. strip the hwaccel tag and re-insert it as part
  343. * of the payload.
  344. */
  345. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
  346. {
  347. unsigned short uninitialized_var(vlan_id);
  348. if (!list_empty(&bond->vlan_list) &&
  349. !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
  350. vlan_get_tag(skb, &vlan_id) == 0) {
  351. skb->dev = slave_dev;
  352. skb = vlan_put_tag(skb, vlan_id);
  353. if (!skb) {
  354. /* vlan_put_tag() frees the skb in case of error,
  355. * so return success here so the calling functions
  356. * won't attempt to free is again.
  357. */
  358. return 0;
  359. }
  360. } else {
  361. skb->dev = slave_dev;
  362. }
  363. skb->priority = 1;
  364. dev_queue_xmit(skb);
  365. return 0;
  366. }
  367. /*
  368. * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
  369. * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
  370. * lock because:
  371. * a. This operation is performed in IOCTL context,
  372. * b. The operation is protected by the RTNL semaphore in the 8021q code,
  373. * c. Holding a lock with BH disabled while directly calling a base driver
  374. * entry point is generally a BAD idea.
  375. *
  376. * The design of synchronization/protection for this operation in the 8021q
  377. * module is good for one or more VLAN devices over a single physical device
  378. * and cannot be extended for a teaming solution like bonding, so there is a
  379. * potential race condition here where a net device from the vlan group might
  380. * be referenced (either by a base driver or the 8021q code) while it is being
  381. * removed from the system. However, it turns out we're not making matters
  382. * worse, and if it works for regular VLAN usage it will work here too.
  383. */
  384. /**
  385. * bond_vlan_rx_register - Propagates registration to slaves
  386. * @bond_dev: bonding net device that got called
  387. * @grp: vlan group being registered
  388. */
  389. static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
  390. {
  391. struct bonding *bond = netdev_priv(bond_dev);
  392. struct slave *slave;
  393. int i;
  394. bond->vlgrp = grp;
  395. bond_for_each_slave(bond, slave, i) {
  396. struct net_device *slave_dev = slave->dev;
  397. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  398. if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
  399. slave_ops->ndo_vlan_rx_register) {
  400. slave_ops->ndo_vlan_rx_register(slave_dev, grp);
  401. }
  402. }
  403. }
  404. /**
  405. * bond_vlan_rx_add_vid - Propagates adding an id to slaves
  406. * @bond_dev: bonding net device that got called
  407. * @vid: vlan id being added
  408. */
  409. static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
  410. {
  411. struct bonding *bond = netdev_priv(bond_dev);
  412. struct slave *slave;
  413. int i, res;
  414. bond_for_each_slave(bond, slave, i) {
  415. struct net_device *slave_dev = slave->dev;
  416. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  417. if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
  418. slave_ops->ndo_vlan_rx_add_vid) {
  419. slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
  420. }
  421. }
  422. res = bond_add_vlan(bond, vid);
  423. if (res) {
  424. printk(KERN_ERR DRV_NAME
  425. ": %s: Error: Failed to add vlan id %d\n",
  426. bond_dev->name, vid);
  427. }
  428. }
  429. /**
  430. * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
  431. * @bond_dev: bonding net device that got called
  432. * @vid: vlan id being removed
  433. */
  434. static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
  435. {
  436. struct bonding *bond = netdev_priv(bond_dev);
  437. struct slave *slave;
  438. struct net_device *vlan_dev;
  439. int i, res;
  440. bond_for_each_slave(bond, slave, i) {
  441. struct net_device *slave_dev = slave->dev;
  442. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  443. if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
  444. slave_ops->ndo_vlan_rx_kill_vid) {
  445. /* Save and then restore vlan_dev in the grp array,
  446. * since the slave's driver might clear it.
  447. */
  448. vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
  449. slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
  450. vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
  451. }
  452. }
  453. res = bond_del_vlan(bond, vid);
  454. if (res) {
  455. printk(KERN_ERR DRV_NAME
  456. ": %s: Error: Failed to remove vlan id %d\n",
  457. bond_dev->name, vid);
  458. }
  459. }
  460. static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
  461. {
  462. struct vlan_entry *vlan;
  463. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  464. write_lock_bh(&bond->lock);
  465. if (list_empty(&bond->vlan_list))
  466. goto out;
  467. if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
  468. slave_ops->ndo_vlan_rx_register)
  469. slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
  470. if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
  471. !(slave_ops->ndo_vlan_rx_add_vid))
  472. goto out;
  473. list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
  474. slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
  475. out:
  476. write_unlock_bh(&bond->lock);
  477. }
  478. static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
  479. {
  480. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  481. struct vlan_entry *vlan;
  482. struct net_device *vlan_dev;
  483. write_lock_bh(&bond->lock);
  484. if (list_empty(&bond->vlan_list))
  485. goto out;
  486. if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
  487. !(slave_ops->ndo_vlan_rx_kill_vid))
  488. goto unreg;
  489. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  490. /* Save and then restore vlan_dev in the grp array,
  491. * since the slave's driver might clear it.
  492. */
  493. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  494. slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
  495. vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
  496. }
  497. unreg:
  498. if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
  499. slave_ops->ndo_vlan_rx_register)
  500. slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
  501. out:
  502. write_unlock_bh(&bond->lock);
  503. }
  504. /*------------------------------- Link status -------------------------------*/
  505. /*
  506. * Set the carrier state for the master according to the state of its
  507. * slaves. If any slaves are up, the master is up. In 802.3ad mode,
  508. * do special 802.3ad magic.
  509. *
  510. * Returns zero if carrier state does not change, nonzero if it does.
  511. */
  512. static int bond_set_carrier(struct bonding *bond)
  513. {
  514. struct slave *slave;
  515. int i;
  516. if (bond->slave_cnt == 0)
  517. goto down;
  518. if (bond->params.mode == BOND_MODE_8023AD)
  519. return bond_3ad_set_carrier(bond);
  520. bond_for_each_slave(bond, slave, i) {
  521. if (slave->link == BOND_LINK_UP) {
  522. if (!netif_carrier_ok(bond->dev)) {
  523. netif_carrier_on(bond->dev);
  524. return 1;
  525. }
  526. return 0;
  527. }
  528. }
  529. down:
  530. if (netif_carrier_ok(bond->dev)) {
  531. netif_carrier_off(bond->dev);
  532. return 1;
  533. }
  534. return 0;
  535. }
  536. /*
  537. * Get link speed and duplex from the slave's base driver
  538. * using ethtool. If for some reason the call fails or the
  539. * values are invalid, fake speed and duplex to 100/Full
  540. * and return error.
  541. */
  542. static int bond_update_speed_duplex(struct slave *slave)
  543. {
  544. struct net_device *slave_dev = slave->dev;
  545. struct ethtool_cmd etool;
  546. int res;
  547. /* Fake speed and duplex */
  548. slave->speed = SPEED_100;
  549. slave->duplex = DUPLEX_FULL;
  550. if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
  551. return -1;
  552. res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
  553. if (res < 0)
  554. return -1;
  555. switch (etool.speed) {
  556. case SPEED_10:
  557. case SPEED_100:
  558. case SPEED_1000:
  559. case SPEED_10000:
  560. break;
  561. default:
  562. return -1;
  563. }
  564. switch (etool.duplex) {
  565. case DUPLEX_FULL:
  566. case DUPLEX_HALF:
  567. break;
  568. default:
  569. return -1;
  570. }
  571. slave->speed = etool.speed;
  572. slave->duplex = etool.duplex;
  573. return 0;
  574. }
  575. /*
  576. * if <dev> supports MII link status reporting, check its link status.
  577. *
  578. * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
  579. * depening upon the setting of the use_carrier parameter.
  580. *
  581. * Return either BMSR_LSTATUS, meaning that the link is up (or we
  582. * can't tell and just pretend it is), or 0, meaning that the link is
  583. * down.
  584. *
  585. * If reporting is non-zero, instead of faking link up, return -1 if
  586. * both ETHTOOL and MII ioctls fail (meaning the device does not
  587. * support them). If use_carrier is set, return whatever it says.
  588. * It'd be nice if there was a good way to tell if a driver supports
  589. * netif_carrier, but there really isn't.
  590. */
  591. static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
  592. {
  593. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  594. static int (* ioctl)(struct net_device *, struct ifreq *, int);
  595. struct ifreq ifr;
  596. struct mii_ioctl_data *mii;
  597. if (bond->params.use_carrier)
  598. return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
  599. ioctl = slave_ops->ndo_do_ioctl;
  600. if (ioctl) {
  601. /* TODO: set pointer to correct ioctl on a per team member */
  602. /* bases to make this more efficient. that is, once */
  603. /* we determine the correct ioctl, we will always */
  604. /* call it and not the others for that team */
  605. /* member. */
  606. /*
  607. * We cannot assume that SIOCGMIIPHY will also read a
  608. * register; not all network drivers (e.g., e100)
  609. * support that.
  610. */
  611. /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
  612. strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
  613. mii = if_mii(&ifr);
  614. if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
  615. mii->reg_num = MII_BMSR;
  616. if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
  617. return (mii->val_out & BMSR_LSTATUS);
  618. }
  619. }
  620. }
  621. /*
  622. * Some drivers cache ETHTOOL_GLINK for a period of time so we only
  623. * attempt to get link status from it if the above MII ioctls fail.
  624. */
  625. if (slave_dev->ethtool_ops) {
  626. if (slave_dev->ethtool_ops->get_link) {
  627. u32 link;
  628. link = slave_dev->ethtool_ops->get_link(slave_dev);
  629. return link ? BMSR_LSTATUS : 0;
  630. }
  631. }
  632. /*
  633. * If reporting, report that either there's no dev->do_ioctl,
  634. * or both SIOCGMIIREG and get_link failed (meaning that we
  635. * cannot report link status). If not reporting, pretend
  636. * we're ok.
  637. */
  638. return (reporting ? -1 : BMSR_LSTATUS);
  639. }
  640. /*----------------------------- Multicast list ------------------------------*/
  641. /*
  642. * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
  643. */
  644. static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
  645. {
  646. return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
  647. dmi1->dmi_addrlen == dmi2->dmi_addrlen;
  648. }
  649. /*
  650. * returns dmi entry if found, NULL otherwise
  651. */
  652. static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
  653. {
  654. struct dev_mc_list *idmi;
  655. for (idmi = mc_list; idmi; idmi = idmi->next) {
  656. if (bond_is_dmi_same(dmi, idmi)) {
  657. return idmi;
  658. }
  659. }
  660. return NULL;
  661. }
  662. /*
  663. * Push the promiscuity flag down to appropriate slaves
  664. */
  665. static int bond_set_promiscuity(struct bonding *bond, int inc)
  666. {
  667. int err = 0;
  668. if (USES_PRIMARY(bond->params.mode)) {
  669. /* write lock already acquired */
  670. if (bond->curr_active_slave) {
  671. err = dev_set_promiscuity(bond->curr_active_slave->dev,
  672. inc);
  673. }
  674. } else {
  675. struct slave *slave;
  676. int i;
  677. bond_for_each_slave(bond, slave, i) {
  678. err = dev_set_promiscuity(slave->dev, inc);
  679. if (err)
  680. return err;
  681. }
  682. }
  683. return err;
  684. }
  685. /*
  686. * Push the allmulti flag down to all slaves
  687. */
  688. static int bond_set_allmulti(struct bonding *bond, int inc)
  689. {
  690. int err = 0;
  691. if (USES_PRIMARY(bond->params.mode)) {
  692. /* write lock already acquired */
  693. if (bond->curr_active_slave) {
  694. err = dev_set_allmulti(bond->curr_active_slave->dev,
  695. inc);
  696. }
  697. } else {
  698. struct slave *slave;
  699. int i;
  700. bond_for_each_slave(bond, slave, i) {
  701. err = dev_set_allmulti(slave->dev, inc);
  702. if (err)
  703. return err;
  704. }
  705. }
  706. return err;
  707. }
  708. /*
  709. * Add a Multicast address to slaves
  710. * according to mode
  711. */
  712. static void bond_mc_add(struct bonding *bond, void *addr, int alen)
  713. {
  714. if (USES_PRIMARY(bond->params.mode)) {
  715. /* write lock already acquired */
  716. if (bond->curr_active_slave) {
  717. dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
  718. }
  719. } else {
  720. struct slave *slave;
  721. int i;
  722. bond_for_each_slave(bond, slave, i) {
  723. dev_mc_add(slave->dev, addr, alen, 0);
  724. }
  725. }
  726. }
  727. /*
  728. * Remove a multicast address from slave
  729. * according to mode
  730. */
  731. static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
  732. {
  733. if (USES_PRIMARY(bond->params.mode)) {
  734. /* write lock already acquired */
  735. if (bond->curr_active_slave) {
  736. dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
  737. }
  738. } else {
  739. struct slave *slave;
  740. int i;
  741. bond_for_each_slave(bond, slave, i) {
  742. dev_mc_delete(slave->dev, addr, alen, 0);
  743. }
  744. }
  745. }
  746. /*
  747. * Retrieve the list of registered multicast addresses for the bonding
  748. * device and retransmit an IGMP JOIN request to the current active
  749. * slave.
  750. */
  751. static void bond_resend_igmp_join_requests(struct bonding *bond)
  752. {
  753. struct in_device *in_dev;
  754. struct ip_mc_list *im;
  755. rcu_read_lock();
  756. in_dev = __in_dev_get_rcu(bond->dev);
  757. if (in_dev) {
  758. for (im = in_dev->mc_list; im; im = im->next) {
  759. ip_mc_rejoin_group(im);
  760. }
  761. }
  762. rcu_read_unlock();
  763. }
  764. /*
  765. * Totally destroys the mc_list in bond
  766. */
  767. static void bond_mc_list_destroy(struct bonding *bond)
  768. {
  769. struct dev_mc_list *dmi;
  770. dmi = bond->mc_list;
  771. while (dmi) {
  772. bond->mc_list = dmi->next;
  773. kfree(dmi);
  774. dmi = bond->mc_list;
  775. }
  776. bond->mc_list = NULL;
  777. }
  778. /*
  779. * Copy all the Multicast addresses from src to the bonding device dst
  780. */
  781. static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
  782. gfp_t gfp_flag)
  783. {
  784. struct dev_mc_list *dmi, *new_dmi;
  785. for (dmi = mc_list; dmi; dmi = dmi->next) {
  786. new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
  787. if (!new_dmi) {
  788. /* FIXME: Potential memory leak !!! */
  789. return -ENOMEM;
  790. }
  791. new_dmi->next = bond->mc_list;
  792. bond->mc_list = new_dmi;
  793. new_dmi->dmi_addrlen = dmi->dmi_addrlen;
  794. memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
  795. new_dmi->dmi_users = dmi->dmi_users;
  796. new_dmi->dmi_gusers = dmi->dmi_gusers;
  797. }
  798. return 0;
  799. }
  800. /*
  801. * flush all members of flush->mc_list from device dev->mc_list
  802. */
  803. static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
  804. {
  805. struct bonding *bond = netdev_priv(bond_dev);
  806. struct dev_mc_list *dmi;
  807. for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
  808. dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  809. }
  810. if (bond->params.mode == BOND_MODE_8023AD) {
  811. /* del lacpdu mc addr from mc list */
  812. u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
  813. dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
  814. }
  815. }
  816. /*--------------------------- Active slave change ---------------------------*/
  817. /*
  818. * Update the mc list and multicast-related flags for the new and
  819. * old active slaves (if any) according to the multicast mode, and
  820. * promiscuous flags unconditionally.
  821. */
  822. static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
  823. {
  824. struct dev_mc_list *dmi;
  825. if (!USES_PRIMARY(bond->params.mode)) {
  826. /* nothing to do - mc list is already up-to-date on
  827. * all slaves
  828. */
  829. return;
  830. }
  831. if (old_active) {
  832. if (bond->dev->flags & IFF_PROMISC) {
  833. dev_set_promiscuity(old_active->dev, -1);
  834. }
  835. if (bond->dev->flags & IFF_ALLMULTI) {
  836. dev_set_allmulti(old_active->dev, -1);
  837. }
  838. for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
  839. dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  840. }
  841. }
  842. if (new_active) {
  843. /* FIXME: Signal errors upstream. */
  844. if (bond->dev->flags & IFF_PROMISC) {
  845. dev_set_promiscuity(new_active->dev, 1);
  846. }
  847. if (bond->dev->flags & IFF_ALLMULTI) {
  848. dev_set_allmulti(new_active->dev, 1);
  849. }
  850. for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
  851. dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  852. }
  853. bond_resend_igmp_join_requests(bond);
  854. }
  855. }
  856. /*
  857. * bond_do_fail_over_mac
  858. *
  859. * Perform special MAC address swapping for fail_over_mac settings
  860. *
  861. * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
  862. */
  863. static void bond_do_fail_over_mac(struct bonding *bond,
  864. struct slave *new_active,
  865. struct slave *old_active)
  866. __releases(&bond->curr_slave_lock)
  867. __releases(&bond->lock)
  868. __acquires(&bond->lock)
  869. __acquires(&bond->curr_slave_lock)
  870. {
  871. u8 tmp_mac[ETH_ALEN];
  872. struct sockaddr saddr;
  873. int rv;
  874. switch (bond->params.fail_over_mac) {
  875. case BOND_FOM_ACTIVE:
  876. if (new_active)
  877. memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
  878. new_active->dev->addr_len);
  879. break;
  880. case BOND_FOM_FOLLOW:
  881. /*
  882. * if new_active && old_active, swap them
  883. * if just old_active, do nothing (going to no active slave)
  884. * if just new_active, set new_active to bond's MAC
  885. */
  886. if (!new_active)
  887. return;
  888. write_unlock_bh(&bond->curr_slave_lock);
  889. read_unlock(&bond->lock);
  890. if (old_active) {
  891. memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
  892. memcpy(saddr.sa_data, old_active->dev->dev_addr,
  893. ETH_ALEN);
  894. saddr.sa_family = new_active->dev->type;
  895. } else {
  896. memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
  897. saddr.sa_family = bond->dev->type;
  898. }
  899. rv = dev_set_mac_address(new_active->dev, &saddr);
  900. if (rv) {
  901. printk(KERN_ERR DRV_NAME
  902. ": %s: Error %d setting MAC of slave %s\n",
  903. bond->dev->name, -rv, new_active->dev->name);
  904. goto out;
  905. }
  906. if (!old_active)
  907. goto out;
  908. memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
  909. saddr.sa_family = old_active->dev->type;
  910. rv = dev_set_mac_address(old_active->dev, &saddr);
  911. if (rv)
  912. printk(KERN_ERR DRV_NAME
  913. ": %s: Error %d setting MAC of slave %s\n",
  914. bond->dev->name, -rv, new_active->dev->name);
  915. out:
  916. read_lock(&bond->lock);
  917. write_lock_bh(&bond->curr_slave_lock);
  918. break;
  919. default:
  920. printk(KERN_ERR DRV_NAME
  921. ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
  922. bond->dev->name, bond->params.fail_over_mac);
  923. break;
  924. }
  925. }
  926. /**
  927. * find_best_interface - select the best available slave to be the active one
  928. * @bond: our bonding struct
  929. *
  930. * Warning: Caller must hold curr_slave_lock for writing.
  931. */
  932. static struct slave *bond_find_best_slave(struct bonding *bond)
  933. {
  934. struct slave *new_active, *old_active;
  935. struct slave *bestslave = NULL;
  936. int mintime = bond->params.updelay;
  937. int i;
  938. new_active = old_active = bond->curr_active_slave;
  939. if (!new_active) { /* there were no active slaves left */
  940. if (bond->slave_cnt > 0) { /* found one slave */
  941. new_active = bond->first_slave;
  942. } else {
  943. return NULL; /* still no slave, return NULL */
  944. }
  945. }
  946. /* first try the primary link; if arping, a link must tx/rx traffic
  947. * before it can be considered the curr_active_slave - also, we would skip
  948. * slaves between the curr_active_slave and primary_slave that may be up
  949. * and able to arp
  950. */
  951. if ((bond->primary_slave) &&
  952. (!bond->params.arp_interval) &&
  953. (IS_UP(bond->primary_slave->dev))) {
  954. new_active = bond->primary_slave;
  955. }
  956. /* remember where to stop iterating over the slaves */
  957. old_active = new_active;
  958. bond_for_each_slave_from(bond, new_active, i, old_active) {
  959. if (IS_UP(new_active->dev)) {
  960. if (new_active->link == BOND_LINK_UP) {
  961. return new_active;
  962. } else if (new_active->link == BOND_LINK_BACK) {
  963. /* link up, but waiting for stabilization */
  964. if (new_active->delay < mintime) {
  965. mintime = new_active->delay;
  966. bestslave = new_active;
  967. }
  968. }
  969. }
  970. }
  971. return bestslave;
  972. }
  973. /**
  974. * change_active_interface - change the active slave into the specified one
  975. * @bond: our bonding struct
  976. * @new: the new slave to make the active one
  977. *
  978. * Set the new slave to the bond's settings and unset them on the old
  979. * curr_active_slave.
  980. * Setting include flags, mc-list, promiscuity, allmulti, etc.
  981. *
  982. * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
  983. * because it is apparently the best available slave we have, even though its
  984. * updelay hasn't timed out yet.
  985. *
  986. * If new_active is not NULL, caller must hold bond->lock for read and
  987. * curr_slave_lock for write_bh.
  988. */
  989. void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
  990. {
  991. struct slave *old_active = bond->curr_active_slave;
  992. if (old_active == new_active) {
  993. return;
  994. }
  995. if (new_active) {
  996. new_active->jiffies = jiffies;
  997. if (new_active->link == BOND_LINK_BACK) {
  998. if (USES_PRIMARY(bond->params.mode)) {
  999. printk(KERN_INFO DRV_NAME
  1000. ": %s: making interface %s the new "
  1001. "active one %d ms earlier.\n",
  1002. bond->dev->name, new_active->dev->name,
  1003. (bond->params.updelay - new_active->delay) * bond->params.miimon);
  1004. }
  1005. new_active->delay = 0;
  1006. new_active->link = BOND_LINK_UP;
  1007. if (bond->params.mode == BOND_MODE_8023AD) {
  1008. bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
  1009. }
  1010. if (bond_is_lb(bond))
  1011. bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
  1012. } else {
  1013. if (USES_PRIMARY(bond->params.mode)) {
  1014. printk(KERN_INFO DRV_NAME
  1015. ": %s: making interface %s the new "
  1016. "active one.\n",
  1017. bond->dev->name, new_active->dev->name);
  1018. }
  1019. }
  1020. }
  1021. if (USES_PRIMARY(bond->params.mode)) {
  1022. bond_mc_swap(bond, new_active, old_active);
  1023. }
  1024. if (bond_is_lb(bond)) {
  1025. bond_alb_handle_active_change(bond, new_active);
  1026. if (old_active)
  1027. bond_set_slave_inactive_flags(old_active);
  1028. if (new_active)
  1029. bond_set_slave_active_flags(new_active);
  1030. } else {
  1031. bond->curr_active_slave = new_active;
  1032. }
  1033. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
  1034. if (old_active) {
  1035. bond_set_slave_inactive_flags(old_active);
  1036. }
  1037. if (new_active) {
  1038. bond_set_slave_active_flags(new_active);
  1039. if (bond->params.fail_over_mac)
  1040. bond_do_fail_over_mac(bond, new_active,
  1041. old_active);
  1042. bond->send_grat_arp = bond->params.num_grat_arp;
  1043. bond_send_gratuitous_arp(bond);
  1044. bond->send_unsol_na = bond->params.num_unsol_na;
  1045. bond_send_unsolicited_na(bond);
  1046. write_unlock_bh(&bond->curr_slave_lock);
  1047. read_unlock(&bond->lock);
  1048. netdev_bonding_change(bond->dev);
  1049. read_lock(&bond->lock);
  1050. write_lock_bh(&bond->curr_slave_lock);
  1051. }
  1052. }
  1053. }
  1054. /**
  1055. * bond_select_active_slave - select a new active slave, if needed
  1056. * @bond: our bonding struct
  1057. *
  1058. * This functions shoud be called when one of the following occurs:
  1059. * - The old curr_active_slave has been released or lost its link.
  1060. * - The primary_slave has got its link back.
  1061. * - A slave has got its link back and there's no old curr_active_slave.
  1062. *
  1063. * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
  1064. */
  1065. void bond_select_active_slave(struct bonding *bond)
  1066. {
  1067. struct slave *best_slave;
  1068. int rv;
  1069. best_slave = bond_find_best_slave(bond);
  1070. if (best_slave != bond->curr_active_slave) {
  1071. bond_change_active_slave(bond, best_slave);
  1072. rv = bond_set_carrier(bond);
  1073. if (!rv)
  1074. return;
  1075. if (netif_carrier_ok(bond->dev)) {
  1076. printk(KERN_INFO DRV_NAME
  1077. ": %s: first active interface up!\n",
  1078. bond->dev->name);
  1079. } else {
  1080. printk(KERN_INFO DRV_NAME ": %s: "
  1081. "now running without any active interface !\n",
  1082. bond->dev->name);
  1083. }
  1084. }
  1085. }
  1086. /*--------------------------- slave list handling ---------------------------*/
  1087. /*
  1088. * This function attaches the slave to the end of list.
  1089. *
  1090. * bond->lock held for writing by caller.
  1091. */
  1092. static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
  1093. {
  1094. if (bond->first_slave == NULL) { /* attaching the first slave */
  1095. new_slave->next = new_slave;
  1096. new_slave->prev = new_slave;
  1097. bond->first_slave = new_slave;
  1098. } else {
  1099. new_slave->next = bond->first_slave;
  1100. new_slave->prev = bond->first_slave->prev;
  1101. new_slave->next->prev = new_slave;
  1102. new_slave->prev->next = new_slave;
  1103. }
  1104. bond->slave_cnt++;
  1105. }
  1106. /*
  1107. * This function detaches the slave from the list.
  1108. * WARNING: no check is made to verify if the slave effectively
  1109. * belongs to <bond>.
  1110. * Nothing is freed on return, structures are just unchained.
  1111. * If any slave pointer in bond was pointing to <slave>,
  1112. * it should be changed by the calling function.
  1113. *
  1114. * bond->lock held for writing by caller.
  1115. */
  1116. static void bond_detach_slave(struct bonding *bond, struct slave *slave)
  1117. {
  1118. if (slave->next) {
  1119. slave->next->prev = slave->prev;
  1120. }
  1121. if (slave->prev) {
  1122. slave->prev->next = slave->next;
  1123. }
  1124. if (bond->first_slave == slave) { /* slave is the first slave */
  1125. if (bond->slave_cnt > 1) { /* there are more slave */
  1126. bond->first_slave = slave->next;
  1127. } else {
  1128. bond->first_slave = NULL; /* slave was the last one */
  1129. }
  1130. }
  1131. slave->next = NULL;
  1132. slave->prev = NULL;
  1133. bond->slave_cnt--;
  1134. }
  1135. /*---------------------------------- IOCTL ----------------------------------*/
  1136. static int bond_sethwaddr(struct net_device *bond_dev,
  1137. struct net_device *slave_dev)
  1138. {
  1139. pr_debug("bond_dev=%p\n", bond_dev);
  1140. pr_debug("slave_dev=%p\n", slave_dev);
  1141. pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
  1142. memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
  1143. return 0;
  1144. }
  1145. #define BOND_VLAN_FEATURES \
  1146. (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
  1147. NETIF_F_HW_VLAN_FILTER)
  1148. /*
  1149. * Compute the common dev->feature set available to all slaves. Some
  1150. * feature bits are managed elsewhere, so preserve those feature bits
  1151. * on the master device.
  1152. */
  1153. static int bond_compute_features(struct bonding *bond)
  1154. {
  1155. struct slave *slave;
  1156. struct net_device *bond_dev = bond->dev;
  1157. unsigned long features = bond_dev->features;
  1158. unsigned short max_hard_header_len = max((u16)ETH_HLEN,
  1159. bond_dev->hard_header_len);
  1160. int i;
  1161. features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
  1162. features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
  1163. if (!bond->first_slave)
  1164. goto done;
  1165. features &= ~NETIF_F_ONE_FOR_ALL;
  1166. bond_for_each_slave(bond, slave, i) {
  1167. features = netdev_increment_features(features,
  1168. slave->dev->features,
  1169. NETIF_F_ONE_FOR_ALL);
  1170. if (slave->dev->hard_header_len > max_hard_header_len)
  1171. max_hard_header_len = slave->dev->hard_header_len;
  1172. }
  1173. done:
  1174. features |= (bond_dev->features & BOND_VLAN_FEATURES);
  1175. bond_dev->features = netdev_fix_features(features, NULL);
  1176. bond_dev->hard_header_len = max_hard_header_len;
  1177. return 0;
  1178. }
  1179. static void bond_setup_by_slave(struct net_device *bond_dev,
  1180. struct net_device *slave_dev)
  1181. {
  1182. struct bonding *bond = netdev_priv(bond_dev);
  1183. bond_dev->header_ops = slave_dev->header_ops;
  1184. bond_dev->type = slave_dev->type;
  1185. bond_dev->hard_header_len = slave_dev->hard_header_len;
  1186. bond_dev->addr_len = slave_dev->addr_len;
  1187. memcpy(bond_dev->broadcast, slave_dev->broadcast,
  1188. slave_dev->addr_len);
  1189. bond->setup_by_slave = 1;
  1190. }
  1191. /* enslave device <slave> to bond device <master> */
  1192. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
  1193. {
  1194. struct bonding *bond = netdev_priv(bond_dev);
  1195. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  1196. struct slave *new_slave = NULL;
  1197. struct dev_mc_list *dmi;
  1198. struct sockaddr addr;
  1199. int link_reporting;
  1200. int old_features = bond_dev->features;
  1201. int res = 0;
  1202. if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
  1203. slave_ops->ndo_do_ioctl == NULL) {
  1204. printk(KERN_WARNING DRV_NAME
  1205. ": %s: Warning: no link monitoring support for %s\n",
  1206. bond_dev->name, slave_dev->name);
  1207. }
  1208. /* bond must be initialized by bond_open() before enslaving */
  1209. if (!(bond_dev->flags & IFF_UP)) {
  1210. printk(KERN_WARNING DRV_NAME
  1211. " %s: master_dev is not up in bond_enslave\n",
  1212. bond_dev->name);
  1213. }
  1214. /* already enslaved */
  1215. if (slave_dev->flags & IFF_SLAVE) {
  1216. pr_debug("Error, Device was already enslaved\n");
  1217. return -EBUSY;
  1218. }
  1219. /* vlan challenged mutual exclusion */
  1220. /* no need to lock since we're protected by rtnl_lock */
  1221. if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
  1222. pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
  1223. if (!list_empty(&bond->vlan_list)) {
  1224. printk(KERN_ERR DRV_NAME
  1225. ": %s: Error: cannot enslave VLAN "
  1226. "challenged slave %s on VLAN enabled "
  1227. "bond %s\n", bond_dev->name, slave_dev->name,
  1228. bond_dev->name);
  1229. return -EPERM;
  1230. } else {
  1231. printk(KERN_WARNING DRV_NAME
  1232. ": %s: Warning: enslaved VLAN challenged "
  1233. "slave %s. Adding VLANs will be blocked as "
  1234. "long as %s is part of bond %s\n",
  1235. bond_dev->name, slave_dev->name, slave_dev->name,
  1236. bond_dev->name);
  1237. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1238. }
  1239. } else {
  1240. pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
  1241. if (bond->slave_cnt == 0) {
  1242. /* First slave, and it is not VLAN challenged,
  1243. * so remove the block of adding VLANs over the bond.
  1244. */
  1245. bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
  1246. }
  1247. }
  1248. /*
  1249. * Old ifenslave binaries are no longer supported. These can
  1250. * be identified with moderate accurary by the state of the slave:
  1251. * the current ifenslave will set the interface down prior to
  1252. * enslaving it; the old ifenslave will not.
  1253. */
  1254. if ((slave_dev->flags & IFF_UP)) {
  1255. printk(KERN_ERR DRV_NAME ": %s is up. "
  1256. "This may be due to an out of date ifenslave.\n",
  1257. slave_dev->name);
  1258. res = -EPERM;
  1259. goto err_undo_flags;
  1260. }
  1261. /* set bonding device ether type by slave - bonding netdevices are
  1262. * created with ether_setup, so when the slave type is not ARPHRD_ETHER
  1263. * there is a need to override some of the type dependent attribs/funcs.
  1264. *
  1265. * bond ether type mutual exclusion - don't allow slaves of dissimilar
  1266. * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
  1267. */
  1268. if (bond->slave_cnt == 0) {
  1269. if (slave_dev->type != ARPHRD_ETHER)
  1270. bond_setup_by_slave(bond_dev, slave_dev);
  1271. } else if (bond_dev->type != slave_dev->type) {
  1272. printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
  1273. "from other slaves (%d), can not enslave it.\n",
  1274. slave_dev->name,
  1275. slave_dev->type, bond_dev->type);
  1276. res = -EINVAL;
  1277. goto err_undo_flags;
  1278. }
  1279. if (slave_ops->ndo_set_mac_address == NULL) {
  1280. if (bond->slave_cnt == 0) {
  1281. printk(KERN_WARNING DRV_NAME
  1282. ": %s: Warning: The first slave device "
  1283. "specified does not support setting the MAC "
  1284. "address. Setting fail_over_mac to active.",
  1285. bond_dev->name);
  1286. bond->params.fail_over_mac = BOND_FOM_ACTIVE;
  1287. } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
  1288. printk(KERN_ERR DRV_NAME
  1289. ": %s: Error: The slave device specified "
  1290. "does not support setting the MAC address, "
  1291. "but fail_over_mac is not set to active.\n"
  1292. , bond_dev->name);
  1293. res = -EOPNOTSUPP;
  1294. goto err_undo_flags;
  1295. }
  1296. }
  1297. new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
  1298. if (!new_slave) {
  1299. res = -ENOMEM;
  1300. goto err_undo_flags;
  1301. }
  1302. /* save slave's original flags before calling
  1303. * netdev_set_master and dev_open
  1304. */
  1305. new_slave->original_flags = slave_dev->flags;
  1306. /*
  1307. * Save slave's original ("permanent") mac address for modes
  1308. * that need it, and for restoring it upon release, and then
  1309. * set it to the master's address
  1310. */
  1311. memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
  1312. if (!bond->params.fail_over_mac) {
  1313. /*
  1314. * Set slave to master's mac address. The application already
  1315. * set the master's mac address to that of the first slave
  1316. */
  1317. memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
  1318. addr.sa_family = slave_dev->type;
  1319. res = dev_set_mac_address(slave_dev, &addr);
  1320. if (res) {
  1321. pr_debug("Error %d calling set_mac_address\n", res);
  1322. goto err_free;
  1323. }
  1324. }
  1325. res = netdev_set_master(slave_dev, bond_dev);
  1326. if (res) {
  1327. pr_debug("Error %d calling netdev_set_master\n", res);
  1328. goto err_restore_mac;
  1329. }
  1330. /* open the slave since the application closed it */
  1331. res = dev_open(slave_dev);
  1332. if (res) {
  1333. pr_debug("Openning slave %s failed\n", slave_dev->name);
  1334. goto err_unset_master;
  1335. }
  1336. new_slave->dev = slave_dev;
  1337. slave_dev->priv_flags |= IFF_BONDING;
  1338. if (bond_is_lb(bond)) {
  1339. /* bond_alb_init_slave() must be called before all other stages since
  1340. * it might fail and we do not want to have to undo everything
  1341. */
  1342. res = bond_alb_init_slave(bond, new_slave);
  1343. if (res) {
  1344. goto err_close;
  1345. }
  1346. }
  1347. /* If the mode USES_PRIMARY, then the new slave gets the
  1348. * master's promisc (and mc) settings only if it becomes the
  1349. * curr_active_slave, and that is taken care of later when calling
  1350. * bond_change_active()
  1351. */
  1352. if (!USES_PRIMARY(bond->params.mode)) {
  1353. /* set promiscuity level to new slave */
  1354. if (bond_dev->flags & IFF_PROMISC) {
  1355. res = dev_set_promiscuity(slave_dev, 1);
  1356. if (res)
  1357. goto err_close;
  1358. }
  1359. /* set allmulti level to new slave */
  1360. if (bond_dev->flags & IFF_ALLMULTI) {
  1361. res = dev_set_allmulti(slave_dev, 1);
  1362. if (res)
  1363. goto err_close;
  1364. }
  1365. netif_addr_lock_bh(bond_dev);
  1366. /* upload master's mc_list to new slave */
  1367. for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
  1368. dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  1369. }
  1370. netif_addr_unlock_bh(bond_dev);
  1371. }
  1372. if (bond->params.mode == BOND_MODE_8023AD) {
  1373. /* add lacpdu mc addr to mc list */
  1374. u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
  1375. dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
  1376. }
  1377. bond_add_vlans_on_slave(bond, slave_dev);
  1378. write_lock_bh(&bond->lock);
  1379. bond_attach_slave(bond, new_slave);
  1380. new_slave->delay = 0;
  1381. new_slave->link_failure_count = 0;
  1382. bond_compute_features(bond);
  1383. write_unlock_bh(&bond->lock);
  1384. read_lock(&bond->lock);
  1385. new_slave->last_arp_rx = jiffies;
  1386. if (bond->params.miimon && !bond->params.use_carrier) {
  1387. link_reporting = bond_check_dev_link(bond, slave_dev, 1);
  1388. if ((link_reporting == -1) && !bond->params.arp_interval) {
  1389. /*
  1390. * miimon is set but a bonded network driver
  1391. * does not support ETHTOOL/MII and
  1392. * arp_interval is not set. Note: if
  1393. * use_carrier is enabled, we will never go
  1394. * here (because netif_carrier is always
  1395. * supported); thus, we don't need to change
  1396. * the messages for netif_carrier.
  1397. */
  1398. printk(KERN_WARNING DRV_NAME
  1399. ": %s: Warning: MII and ETHTOOL support not "
  1400. "available for interface %s, and "
  1401. "arp_interval/arp_ip_target module parameters "
  1402. "not specified, thus bonding will not detect "
  1403. "link failures! see bonding.txt for details.\n",
  1404. bond_dev->name, slave_dev->name);
  1405. } else if (link_reporting == -1) {
  1406. /* unable get link status using mii/ethtool */
  1407. printk(KERN_WARNING DRV_NAME
  1408. ": %s: Warning: can't get link status from "
  1409. "interface %s; the network driver associated "
  1410. "with this interface does not support MII or "
  1411. "ETHTOOL link status reporting, thus miimon "
  1412. "has no effect on this interface.\n",
  1413. bond_dev->name, slave_dev->name);
  1414. }
  1415. }
  1416. /* check for initial state */
  1417. if (!bond->params.miimon ||
  1418. (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
  1419. if (bond->params.updelay) {
  1420. pr_debug("Initial state of slave_dev is "
  1421. "BOND_LINK_BACK\n");
  1422. new_slave->link = BOND_LINK_BACK;
  1423. new_slave->delay = bond->params.updelay;
  1424. } else {
  1425. pr_debug("Initial state of slave_dev is "
  1426. "BOND_LINK_UP\n");
  1427. new_slave->link = BOND_LINK_UP;
  1428. }
  1429. new_slave->jiffies = jiffies;
  1430. } else {
  1431. pr_debug("Initial state of slave_dev is "
  1432. "BOND_LINK_DOWN\n");
  1433. new_slave->link = BOND_LINK_DOWN;
  1434. }
  1435. if (bond_update_speed_duplex(new_slave) &&
  1436. (new_slave->link != BOND_LINK_DOWN)) {
  1437. printk(KERN_WARNING DRV_NAME
  1438. ": %s: Warning: failed to get speed and duplex from %s, "
  1439. "assumed to be 100Mb/sec and Full.\n",
  1440. bond_dev->name, new_slave->dev->name);
  1441. if (bond->params.mode == BOND_MODE_8023AD) {
  1442. printk(KERN_WARNING DRV_NAME
  1443. ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
  1444. "support in base driver for proper aggregator "
  1445. "selection.\n", bond_dev->name);
  1446. }
  1447. }
  1448. if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
  1449. /* if there is a primary slave, remember it */
  1450. if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
  1451. bond->primary_slave = new_slave;
  1452. }
  1453. }
  1454. write_lock_bh(&bond->curr_slave_lock);
  1455. switch (bond->params.mode) {
  1456. case BOND_MODE_ACTIVEBACKUP:
  1457. bond_set_slave_inactive_flags(new_slave);
  1458. bond_select_active_slave(bond);
  1459. break;
  1460. case BOND_MODE_8023AD:
  1461. /* in 802.3ad mode, the internal mechanism
  1462. * will activate the slaves in the selected
  1463. * aggregator
  1464. */
  1465. bond_set_slave_inactive_flags(new_slave);
  1466. /* if this is the first slave */
  1467. if (bond->slave_cnt == 1) {
  1468. SLAVE_AD_INFO(new_slave).id = 1;
  1469. /* Initialize AD with the number of times that the AD timer is called in 1 second
  1470. * can be called only after the mac address of the bond is set
  1471. */
  1472. bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
  1473. bond->params.lacp_fast);
  1474. } else {
  1475. SLAVE_AD_INFO(new_slave).id =
  1476. SLAVE_AD_INFO(new_slave->prev).id + 1;
  1477. }
  1478. bond_3ad_bind_slave(new_slave);
  1479. break;
  1480. case BOND_MODE_TLB:
  1481. case BOND_MODE_ALB:
  1482. new_slave->state = BOND_STATE_ACTIVE;
  1483. bond_set_slave_inactive_flags(new_slave);
  1484. break;
  1485. default:
  1486. pr_debug("This slave is always active in trunk mode\n");
  1487. /* always active in trunk mode */
  1488. new_slave->state = BOND_STATE_ACTIVE;
  1489. /* In trunking mode there is little meaning to curr_active_slave
  1490. * anyway (it holds no special properties of the bond device),
  1491. * so we can change it without calling change_active_interface()
  1492. */
  1493. if (!bond->curr_active_slave) {
  1494. bond->curr_active_slave = new_slave;
  1495. }
  1496. break;
  1497. } /* switch(bond_mode) */
  1498. write_unlock_bh(&bond->curr_slave_lock);
  1499. bond_set_carrier(bond);
  1500. read_unlock(&bond->lock);
  1501. res = bond_create_slave_symlinks(bond_dev, slave_dev);
  1502. if (res)
  1503. goto err_close;
  1504. printk(KERN_INFO DRV_NAME
  1505. ": %s: enslaving %s as a%s interface with a%s link.\n",
  1506. bond_dev->name, slave_dev->name,
  1507. new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
  1508. new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
  1509. /* enslave is successful */
  1510. return 0;
  1511. /* Undo stages on error */
  1512. err_close:
  1513. dev_close(slave_dev);
  1514. err_unset_master:
  1515. netdev_set_master(slave_dev, NULL);
  1516. err_restore_mac:
  1517. if (!bond->params.fail_over_mac) {
  1518. /* XXX TODO - fom follow mode needs to change master's
  1519. * MAC if this slave's MAC is in use by the bond, or at
  1520. * least print a warning.
  1521. */
  1522. memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
  1523. addr.sa_family = slave_dev->type;
  1524. dev_set_mac_address(slave_dev, &addr);
  1525. }
  1526. err_free:
  1527. kfree(new_slave);
  1528. err_undo_flags:
  1529. bond_dev->features = old_features;
  1530. return res;
  1531. }
  1532. /*
  1533. * Try to release the slave device <slave> from the bond device <master>
  1534. * It is legal to access curr_active_slave without a lock because all the function
  1535. * is write-locked.
  1536. *
  1537. * The rules for slave state should be:
  1538. * for Active/Backup:
  1539. * Active stays on all backups go down
  1540. * for Bonded connections:
  1541. * The first up interface should be left on and all others downed.
  1542. */
  1543. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
  1544. {
  1545. struct bonding *bond = netdev_priv(bond_dev);
  1546. struct slave *slave, *oldcurrent;
  1547. struct sockaddr addr;
  1548. int mac_addr_differ;
  1549. /* slave is not a slave or master is not master of this slave */
  1550. if (!(slave_dev->flags & IFF_SLAVE) ||
  1551. (slave_dev->master != bond_dev)) {
  1552. printk(KERN_ERR DRV_NAME
  1553. ": %s: Error: cannot release %s.\n",
  1554. bond_dev->name, slave_dev->name);
  1555. return -EINVAL;
  1556. }
  1557. write_lock_bh(&bond->lock);
  1558. slave = bond_get_slave_by_dev(bond, slave_dev);
  1559. if (!slave) {
  1560. /* not a slave of this bond */
  1561. printk(KERN_INFO DRV_NAME
  1562. ": %s: %s not enslaved\n",
  1563. bond_dev->name, slave_dev->name);
  1564. write_unlock_bh(&bond->lock);
  1565. return -EINVAL;
  1566. }
  1567. if (!bond->params.fail_over_mac) {
  1568. mac_addr_differ = memcmp(bond_dev->dev_addr, slave->perm_hwaddr,
  1569. ETH_ALEN);
  1570. if (!mac_addr_differ && (bond->slave_cnt > 1))
  1571. printk(KERN_WARNING DRV_NAME
  1572. ": %s: Warning: the permanent HWaddr of %s - "
  1573. "%pM - is still in use by %s. "
  1574. "Set the HWaddr of %s to a different address "
  1575. "to avoid conflicts.\n",
  1576. bond_dev->name, slave_dev->name,
  1577. slave->perm_hwaddr,
  1578. bond_dev->name, slave_dev->name);
  1579. }
  1580. /* Inform AD package of unbinding of slave. */
  1581. if (bond->params.mode == BOND_MODE_8023AD) {
  1582. /* must be called before the slave is
  1583. * detached from the list
  1584. */
  1585. bond_3ad_unbind_slave(slave);
  1586. }
  1587. printk(KERN_INFO DRV_NAME
  1588. ": %s: releasing %s interface %s\n",
  1589. bond_dev->name,
  1590. (slave->state == BOND_STATE_ACTIVE)
  1591. ? "active" : "backup",
  1592. slave_dev->name);
  1593. oldcurrent = bond->curr_active_slave;
  1594. bond->current_arp_slave = NULL;
  1595. /* release the slave from its bond */
  1596. bond_detach_slave(bond, slave);
  1597. bond_compute_features(bond);
  1598. if (bond->primary_slave == slave) {
  1599. bond->primary_slave = NULL;
  1600. }
  1601. if (oldcurrent == slave) {
  1602. bond_change_active_slave(bond, NULL);
  1603. }
  1604. if (bond_is_lb(bond)) {
  1605. /* Must be called only after the slave has been
  1606. * detached from the list and the curr_active_slave
  1607. * has been cleared (if our_slave == old_current),
  1608. * but before a new active slave is selected.
  1609. */
  1610. write_unlock_bh(&bond->lock);
  1611. bond_alb_deinit_slave(bond, slave);
  1612. write_lock_bh(&bond->lock);
  1613. }
  1614. if (oldcurrent == slave) {
  1615. /*
  1616. * Note that we hold RTNL over this sequence, so there
  1617. * is no concern that another slave add/remove event
  1618. * will interfere.
  1619. */
  1620. write_unlock_bh(&bond->lock);
  1621. read_lock(&bond->lock);
  1622. write_lock_bh(&bond->curr_slave_lock);
  1623. bond_select_active_slave(bond);
  1624. write_unlock_bh(&bond->curr_slave_lock);
  1625. read_unlock(&bond->lock);
  1626. write_lock_bh(&bond->lock);
  1627. }
  1628. if (bond->slave_cnt == 0) {
  1629. bond_set_carrier(bond);
  1630. /* if the last slave was removed, zero the mac address
  1631. * of the master so it will be set by the application
  1632. * to the mac address of the first slave
  1633. */
  1634. memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
  1635. if (list_empty(&bond->vlan_list)) {
  1636. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1637. } else {
  1638. printk(KERN_WARNING DRV_NAME
  1639. ": %s: Warning: clearing HW address of %s while it "
  1640. "still has VLANs.\n",
  1641. bond_dev->name, bond_dev->name);
  1642. printk(KERN_WARNING DRV_NAME
  1643. ": %s: When re-adding slaves, make sure the bond's "
  1644. "HW address matches its VLANs'.\n",
  1645. bond_dev->name);
  1646. }
  1647. } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
  1648. !bond_has_challenged_slaves(bond)) {
  1649. printk(KERN_INFO DRV_NAME
  1650. ": %s: last VLAN challenged slave %s "
  1651. "left bond %s. VLAN blocking is removed\n",
  1652. bond_dev->name, slave_dev->name, bond_dev->name);
  1653. bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
  1654. }
  1655. write_unlock_bh(&bond->lock);
  1656. /* must do this from outside any spinlocks */
  1657. bond_destroy_slave_symlinks(bond_dev, slave_dev);
  1658. bond_del_vlans_from_slave(bond, slave_dev);
  1659. /* If the mode USES_PRIMARY, then we should only remove its
  1660. * promisc and mc settings if it was the curr_active_slave, but that was
  1661. * already taken care of above when we detached the slave
  1662. */
  1663. if (!USES_PRIMARY(bond->params.mode)) {
  1664. /* unset promiscuity level from slave */
  1665. if (bond_dev->flags & IFF_PROMISC) {
  1666. dev_set_promiscuity(slave_dev, -1);
  1667. }
  1668. /* unset allmulti level from slave */
  1669. if (bond_dev->flags & IFF_ALLMULTI) {
  1670. dev_set_allmulti(slave_dev, -1);
  1671. }
  1672. /* flush master's mc_list from slave */
  1673. netif_addr_lock_bh(bond_dev);
  1674. bond_mc_list_flush(bond_dev, slave_dev);
  1675. netif_addr_unlock_bh(bond_dev);
  1676. }
  1677. netdev_set_master(slave_dev, NULL);
  1678. /* close slave before restoring its mac address */
  1679. dev_close(slave_dev);
  1680. if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
  1681. /* restore original ("permanent") mac address */
  1682. memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
  1683. addr.sa_family = slave_dev->type;
  1684. dev_set_mac_address(slave_dev, &addr);
  1685. }
  1686. slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
  1687. IFF_SLAVE_INACTIVE | IFF_BONDING |
  1688. IFF_SLAVE_NEEDARP);
  1689. kfree(slave);
  1690. return 0; /* deletion OK */
  1691. }
  1692. /*
  1693. * Destroy a bonding device.
  1694. * Must be under rtnl_lock when this function is called.
  1695. */
  1696. void bond_destroy(struct bonding *bond)
  1697. {
  1698. bond_deinit(bond->dev);
  1699. bond_destroy_sysfs_entry(bond);
  1700. unregister_netdevice(bond->dev);
  1701. }
  1702. static void bond_destructor(struct net_device *bond_dev)
  1703. {
  1704. struct bonding *bond = netdev_priv(bond_dev);
  1705. if (bond->wq)
  1706. destroy_workqueue(bond->wq);
  1707. netif_addr_lock_bh(bond_dev);
  1708. bond_mc_list_destroy(bond);
  1709. netif_addr_unlock_bh(bond_dev);
  1710. free_netdev(bond_dev);
  1711. }
  1712. /*
  1713. * First release a slave and than destroy the bond if no more slaves iare left.
  1714. * Must be under rtnl_lock when this function is called.
  1715. */
  1716. int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
  1717. {
  1718. struct bonding *bond = netdev_priv(bond_dev);
  1719. int ret;
  1720. ret = bond_release(bond_dev, slave_dev);
  1721. if ((ret == 0) && (bond->slave_cnt == 0)) {
  1722. printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
  1723. bond_dev->name, bond_dev->name);
  1724. bond_destroy(bond);
  1725. }
  1726. return ret;
  1727. }
  1728. /*
  1729. * This function releases all slaves.
  1730. */
  1731. static int bond_release_all(struct net_device *bond_dev)
  1732. {
  1733. struct bonding *bond = netdev_priv(bond_dev);
  1734. struct slave *slave;
  1735. struct net_device *slave_dev;
  1736. struct sockaddr addr;
  1737. write_lock_bh(&bond->lock);
  1738. netif_carrier_off(bond_dev);
  1739. if (bond->slave_cnt == 0) {
  1740. goto out;
  1741. }
  1742. bond->current_arp_slave = NULL;
  1743. bond->primary_slave = NULL;
  1744. bond_change_active_slave(bond, NULL);
  1745. while ((slave = bond->first_slave) != NULL) {
  1746. /* Inform AD package of unbinding of slave
  1747. * before slave is detached from the list.
  1748. */
  1749. if (bond->params.mode == BOND_MODE_8023AD) {
  1750. bond_3ad_unbind_slave(slave);
  1751. }
  1752. slave_dev = slave->dev;
  1753. bond_detach_slave(bond, slave);
  1754. /* now that the slave is detached, unlock and perform
  1755. * all the undo steps that should not be called from
  1756. * within a lock.
  1757. */
  1758. write_unlock_bh(&bond->lock);
  1759. if (bond_is_lb(bond)) {
  1760. /* must be called only after the slave
  1761. * has been detached from the list
  1762. */
  1763. bond_alb_deinit_slave(bond, slave);
  1764. }
  1765. bond_compute_features(bond);
  1766. bond_destroy_slave_symlinks(bond_dev, slave_dev);
  1767. bond_del_vlans_from_slave(bond, slave_dev);
  1768. /* If the mode USES_PRIMARY, then we should only remove its
  1769. * promisc and mc settings if it was the curr_active_slave, but that was
  1770. * already taken care of above when we detached the slave
  1771. */
  1772. if (!USES_PRIMARY(bond->params.mode)) {
  1773. /* unset promiscuity level from slave */
  1774. if (bond_dev->flags & IFF_PROMISC) {
  1775. dev_set_promiscuity(slave_dev, -1);
  1776. }
  1777. /* unset allmulti level from slave */
  1778. if (bond_dev->flags & IFF_ALLMULTI) {
  1779. dev_set_allmulti(slave_dev, -1);
  1780. }
  1781. /* flush master's mc_list from slave */
  1782. netif_addr_lock_bh(bond_dev);
  1783. bond_mc_list_flush(bond_dev, slave_dev);
  1784. netif_addr_unlock_bh(bond_dev);
  1785. }
  1786. netdev_set_master(slave_dev, NULL);
  1787. /* close slave before restoring its mac address */
  1788. dev_close(slave_dev);
  1789. if (!bond->params.fail_over_mac) {
  1790. /* restore original ("permanent") mac address*/
  1791. memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
  1792. addr.sa_family = slave_dev->type;
  1793. dev_set_mac_address(slave_dev, &addr);
  1794. }
  1795. slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
  1796. IFF_SLAVE_INACTIVE);
  1797. kfree(slave);
  1798. /* re-acquire the lock before getting the next slave */
  1799. write_lock_bh(&bond->lock);
  1800. }
  1801. /* zero the mac address of the master so it will be
  1802. * set by the application to the mac address of the
  1803. * first slave
  1804. */
  1805. memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
  1806. if (list_empty(&bond->vlan_list)) {
  1807. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1808. } else {
  1809. printk(KERN_WARNING DRV_NAME
  1810. ": %s: Warning: clearing HW address of %s while it "
  1811. "still has VLANs.\n",
  1812. bond_dev->name, bond_dev->name);
  1813. printk(KERN_WARNING DRV_NAME
  1814. ": %s: When re-adding slaves, make sure the bond's "
  1815. "HW address matches its VLANs'.\n",
  1816. bond_dev->name);
  1817. }
  1818. printk(KERN_INFO DRV_NAME
  1819. ": %s: released all slaves\n",
  1820. bond_dev->name);
  1821. out:
  1822. write_unlock_bh(&bond->lock);
  1823. return 0;
  1824. }
  1825. /*
  1826. * This function changes the active slave to slave <slave_dev>.
  1827. * It returns -EINVAL in the following cases.
  1828. * - <slave_dev> is not found in the list.
  1829. * - There is not active slave now.
  1830. * - <slave_dev> is already active.
  1831. * - The link state of <slave_dev> is not BOND_LINK_UP.
  1832. * - <slave_dev> is not running.
  1833. * In these cases, this fuction does nothing.
  1834. * In the other cases, currnt_slave pointer is changed and 0 is returned.
  1835. */
  1836. static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
  1837. {
  1838. struct bonding *bond = netdev_priv(bond_dev);
  1839. struct slave *old_active = NULL;
  1840. struct slave *new_active = NULL;
  1841. int res = 0;
  1842. if (!USES_PRIMARY(bond->params.mode)) {
  1843. return -EINVAL;
  1844. }
  1845. /* Verify that master_dev is indeed the master of slave_dev */
  1846. if (!(slave_dev->flags & IFF_SLAVE) ||
  1847. (slave_dev->master != bond_dev)) {
  1848. return -EINVAL;
  1849. }
  1850. read_lock(&bond->lock);
  1851. read_lock(&bond->curr_slave_lock);
  1852. old_active = bond->curr_active_slave;
  1853. read_unlock(&bond->curr_slave_lock);
  1854. new_active = bond_get_slave_by_dev(bond, slave_dev);
  1855. /*
  1856. * Changing to the current active: do nothing; return success.
  1857. */
  1858. if (new_active && (new_active == old_active)) {
  1859. read_unlock(&bond->lock);
  1860. return 0;
  1861. }
  1862. if ((new_active) &&
  1863. (old_active) &&
  1864. (new_active->link == BOND_LINK_UP) &&
  1865. IS_UP(new_active->dev)) {
  1866. write_lock_bh(&bond->curr_slave_lock);
  1867. bond_change_active_slave(bond, new_active);
  1868. write_unlock_bh(&bond->curr_slave_lock);
  1869. } else {
  1870. res = -EINVAL;
  1871. }
  1872. read_unlock(&bond->lock);
  1873. return res;
  1874. }
  1875. static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
  1876. {
  1877. struct bonding *bond = netdev_priv(bond_dev);
  1878. info->bond_mode = bond->params.mode;
  1879. info->miimon = bond->params.miimon;
  1880. read_lock(&bond->lock);
  1881. info->num_slaves = bond->slave_cnt;
  1882. read_unlock(&bond->lock);
  1883. return 0;
  1884. }
  1885. static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
  1886. {
  1887. struct bonding *bond = netdev_priv(bond_dev);
  1888. struct slave *slave;
  1889. int i, found = 0;
  1890. if (info->slave_id < 0) {
  1891. return -ENODEV;
  1892. }
  1893. read_lock(&bond->lock);
  1894. bond_for_each_slave(bond, slave, i) {
  1895. if (i == (int)info->slave_id) {
  1896. found = 1;
  1897. break;
  1898. }
  1899. }
  1900. read_unlock(&bond->lock);
  1901. if (found) {
  1902. strcpy(info->slave_name, slave->dev->name);
  1903. info->link = slave->link;
  1904. info->state = slave->state;
  1905. info->link_failure_count = slave->link_failure_count;
  1906. } else {
  1907. return -ENODEV;
  1908. }
  1909. return 0;
  1910. }
  1911. /*-------------------------------- Monitoring -------------------------------*/
  1912. static int bond_miimon_inspect(struct bonding *bond)
  1913. {
  1914. struct slave *slave;
  1915. int i, link_state, commit = 0;
  1916. bond_for_each_slave(bond, slave, i) {
  1917. slave->new_link = BOND_LINK_NOCHANGE;
  1918. link_state = bond_check_dev_link(bond, slave->dev, 0);
  1919. switch (slave->link) {
  1920. case BOND_LINK_UP:
  1921. if (link_state)
  1922. continue;
  1923. slave->link = BOND_LINK_FAIL;
  1924. slave->delay = bond->params.downdelay;
  1925. if (slave->delay) {
  1926. printk(KERN_INFO DRV_NAME
  1927. ": %s: link status down for %s"
  1928. "interface %s, disabling it in %d ms.\n",
  1929. bond->dev->name,
  1930. (bond->params.mode ==
  1931. BOND_MODE_ACTIVEBACKUP) ?
  1932. ((slave->state == BOND_STATE_ACTIVE) ?
  1933. "active " : "backup ") : "",
  1934. slave->dev->name,
  1935. bond->params.downdelay * bond->params.miimon);
  1936. }
  1937. /*FALLTHRU*/
  1938. case BOND_LINK_FAIL:
  1939. if (link_state) {
  1940. /*
  1941. * recovered before downdelay expired
  1942. */
  1943. slave->link = BOND_LINK_UP;
  1944. slave->jiffies = jiffies;
  1945. printk(KERN_INFO DRV_NAME
  1946. ": %s: link status up again after %d "
  1947. "ms for interface %s.\n",
  1948. bond->dev->name,
  1949. (bond->params.downdelay - slave->delay) *
  1950. bond->params.miimon,
  1951. slave->dev->name);
  1952. continue;
  1953. }
  1954. if (slave->delay <= 0) {
  1955. slave->new_link = BOND_LINK_DOWN;
  1956. commit++;
  1957. continue;
  1958. }
  1959. slave->delay--;
  1960. break;
  1961. case BOND_LINK_DOWN:
  1962. if (!link_state)
  1963. continue;
  1964. slave->link = BOND_LINK_BACK;
  1965. slave->delay = bond->params.updelay;
  1966. if (slave->delay) {
  1967. printk(KERN_INFO DRV_NAME
  1968. ": %s: link status up for "
  1969. "interface %s, enabling it in %d ms.\n",
  1970. bond->dev->name, slave->dev->name,
  1971. bond->params.updelay *
  1972. bond->params.miimon);
  1973. }
  1974. /*FALLTHRU*/
  1975. case BOND_LINK_BACK:
  1976. if (!link_state) {
  1977. slave->link = BOND_LINK_DOWN;
  1978. printk(KERN_INFO DRV_NAME
  1979. ": %s: link status down again after %d "
  1980. "ms for interface %s.\n",
  1981. bond->dev->name,
  1982. (bond->params.updelay - slave->delay) *
  1983. bond->params.miimon,
  1984. slave->dev->name);
  1985. continue;
  1986. }
  1987. if (slave->delay <= 0) {
  1988. slave->new_link = BOND_LINK_UP;
  1989. commit++;
  1990. continue;
  1991. }
  1992. slave->delay--;
  1993. break;
  1994. }
  1995. }
  1996. return commit;
  1997. }
  1998. static void bond_miimon_commit(struct bonding *bond)
  1999. {
  2000. struct slave *slave;
  2001. int i;
  2002. bond_for_each_slave(bond, slave, i) {
  2003. switch (slave->new_link) {
  2004. case BOND_LINK_NOCHANGE:
  2005. continue;
  2006. case BOND_LINK_UP:
  2007. slave->link = BOND_LINK_UP;
  2008. slave->jiffies = jiffies;
  2009. if (bond->params.mode == BOND_MODE_8023AD) {
  2010. /* prevent it from being the active one */
  2011. slave->state = BOND_STATE_BACKUP;
  2012. } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
  2013. /* make it immediately active */
  2014. slave->state = BOND_STATE_ACTIVE;
  2015. } else if (slave != bond->primary_slave) {
  2016. /* prevent it from being the active one */
  2017. slave->state = BOND_STATE_BACKUP;
  2018. }
  2019. printk(KERN_INFO DRV_NAME
  2020. ": %s: link status definitely "
  2021. "up for interface %s.\n",
  2022. bond->dev->name, slave->dev->name);
  2023. /* notify ad that the link status has changed */
  2024. if (bond->params.mode == BOND_MODE_8023AD)
  2025. bond_3ad_handle_link_change(slave, BOND_LINK_UP);
  2026. if (bond_is_lb(bond))
  2027. bond_alb_handle_link_change(bond, slave,
  2028. BOND_LINK_UP);
  2029. if (!bond->curr_active_slave ||
  2030. (slave == bond->primary_slave))
  2031. goto do_failover;
  2032. continue;
  2033. case BOND_LINK_DOWN:
  2034. if (slave->link_failure_count < UINT_MAX)
  2035. slave->link_failure_count++;
  2036. slave->link = BOND_LINK_DOWN;
  2037. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
  2038. bond->params.mode == BOND_MODE_8023AD)
  2039. bond_set_slave_inactive_flags(slave);
  2040. printk(KERN_INFO DRV_NAME
  2041. ": %s: link status definitely down for "
  2042. "interface %s, disabling it\n",
  2043. bond->dev->name, slave->dev->name);
  2044. if (bond->params.mode == BOND_MODE_8023AD)
  2045. bond_3ad_handle_link_change(slave,
  2046. BOND_LINK_DOWN);
  2047. if (bond->params.mode == BOND_MODE_TLB ||
  2048. bond->params.mode == BOND_MODE_ALB)
  2049. bond_alb_handle_link_change(bond, slave,
  2050. BOND_LINK_DOWN);
  2051. if (slave == bond->curr_active_slave)
  2052. goto do_failover;
  2053. continue;
  2054. default:
  2055. printk(KERN_ERR DRV_NAME
  2056. ": %s: invalid new link %d on slave %s\n",
  2057. bond->dev->name, slave->new_link,
  2058. slave->dev->name);
  2059. slave->new_link = BOND_LINK_NOCHANGE;
  2060. continue;
  2061. }
  2062. do_failover:
  2063. ASSERT_RTNL();
  2064. write_lock_bh(&bond->curr_slave_lock);
  2065. bond_select_active_slave(bond);
  2066. write_unlock_bh(&bond->curr_slave_lock);
  2067. }
  2068. bond_set_carrier(bond);
  2069. }
  2070. /*
  2071. * bond_mii_monitor
  2072. *
  2073. * Really a wrapper that splits the mii monitor into two phases: an
  2074. * inspection, then (if inspection indicates something needs to be done)
  2075. * an acquisition of appropriate locks followed by a commit phase to
  2076. * implement whatever link state changes are indicated.
  2077. */
  2078. void bond_mii_monitor(struct work_struct *work)
  2079. {
  2080. struct bonding *bond = container_of(work, struct bonding,
  2081. mii_work.work);
  2082. read_lock(&bond->lock);
  2083. if (bond->kill_timers)
  2084. goto out;
  2085. if (bond->slave_cnt == 0)
  2086. goto re_arm;
  2087. if (bond->send_grat_arp) {
  2088. read_lock(&bond->curr_slave_lock);
  2089. bond_send_gratuitous_arp(bond);
  2090. read_unlock(&bond->curr_slave_lock);
  2091. }
  2092. if (bond->send_unsol_na) {
  2093. read_lock(&bond->curr_slave_lock);
  2094. bond_send_unsolicited_na(bond);
  2095. read_unlock(&bond->curr_slave_lock);
  2096. }
  2097. if (bond_miimon_inspect(bond)) {
  2098. read_unlock(&bond->lock);
  2099. rtnl_lock();
  2100. read_lock(&bond->lock);
  2101. bond_miimon_commit(bond);
  2102. read_unlock(&bond->lock);
  2103. rtnl_unlock(); /* might sleep, hold no other locks */
  2104. read_lock(&bond->lock);
  2105. }
  2106. re_arm:
  2107. if (bond->params.miimon)
  2108. queue_delayed_work(bond->wq, &bond->mii_work,
  2109. msecs_to_jiffies(bond->params.miimon));
  2110. out:
  2111. read_unlock(&bond->lock);
  2112. }
  2113. static __be32 bond_glean_dev_ip(struct net_device *dev)
  2114. {
  2115. struct in_device *idev;
  2116. struct in_ifaddr *ifa;
  2117. __be32 addr = 0;
  2118. if (!dev)
  2119. return 0;
  2120. rcu_read_lock();
  2121. idev = __in_dev_get_rcu(dev);
  2122. if (!idev)
  2123. goto out;
  2124. ifa = idev->ifa_list;
  2125. if (!ifa)
  2126. goto out;
  2127. addr = ifa->ifa_local;
  2128. out:
  2129. rcu_read_unlock();
  2130. return addr;
  2131. }
  2132. static int bond_has_this_ip(struct bonding *bond, __be32 ip)
  2133. {
  2134. struct vlan_entry *vlan;
  2135. if (ip == bond->master_ip)
  2136. return 1;
  2137. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2138. if (ip == vlan->vlan_ip)
  2139. return 1;
  2140. }
  2141. return 0;
  2142. }
  2143. /*
  2144. * We go to the (large) trouble of VLAN tagging ARP frames because
  2145. * switches in VLAN mode (especially if ports are configured as
  2146. * "native" to a VLAN) might not pass non-tagged frames.
  2147. */
  2148. static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
  2149. {
  2150. struct sk_buff *skb;
  2151. pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
  2152. slave_dev->name, dest_ip, src_ip, vlan_id);
  2153. skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
  2154. NULL, slave_dev->dev_addr, NULL);
  2155. if (!skb) {
  2156. printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
  2157. return;
  2158. }
  2159. if (vlan_id) {
  2160. skb = vlan_put_tag(skb, vlan_id);
  2161. if (!skb) {
  2162. printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
  2163. return;
  2164. }
  2165. }
  2166. arp_xmit(skb);
  2167. }
  2168. static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
  2169. {
  2170. int i, vlan_id, rv;
  2171. __be32 *targets = bond->params.arp_targets;
  2172. struct vlan_entry *vlan;
  2173. struct net_device *vlan_dev;
  2174. struct flowi fl;
  2175. struct rtable *rt;
  2176. for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
  2177. if (!targets[i])
  2178. continue;
  2179. pr_debug("basa: target %x\n", targets[i]);
  2180. if (list_empty(&bond->vlan_list)) {
  2181. pr_debug("basa: empty vlan: arp_send\n");
  2182. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2183. bond->master_ip, 0);
  2184. continue;
  2185. }
  2186. /*
  2187. * If VLANs are configured, we do a route lookup to
  2188. * determine which VLAN interface would be used, so we
  2189. * can tag the ARP with the proper VLAN tag.
  2190. */
  2191. memset(&fl, 0, sizeof(fl));
  2192. fl.fl4_dst = targets[i];
  2193. fl.fl4_tos = RTO_ONLINK;
  2194. rv = ip_route_output_key(&init_net, &rt, &fl);
  2195. if (rv) {
  2196. if (net_ratelimit()) {
  2197. printk(KERN_WARNING DRV_NAME
  2198. ": %s: no route to arp_ip_target %pI4\n",
  2199. bond->dev->name, &fl.fl4_dst);
  2200. }
  2201. continue;
  2202. }
  2203. /*
  2204. * This target is not on a VLAN
  2205. */
  2206. if (rt->u.dst.dev == bond->dev) {
  2207. ip_rt_put(rt);
  2208. pr_debug("basa: rtdev == bond->dev: arp_send\n");
  2209. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2210. bond->master_ip, 0);
  2211. continue;
  2212. }
  2213. vlan_id = 0;
  2214. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2215. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  2216. if (vlan_dev == rt->u.dst.dev) {
  2217. vlan_id = vlan->vlan_id;
  2218. pr_debug("basa: vlan match on %s %d\n",
  2219. vlan_dev->name, vlan_id);
  2220. break;
  2221. }
  2222. }
  2223. if (vlan_id) {
  2224. ip_rt_put(rt);
  2225. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2226. vlan->vlan_ip, vlan_id);
  2227. continue;
  2228. }
  2229. if (net_ratelimit()) {
  2230. printk(KERN_WARNING DRV_NAME
  2231. ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
  2232. bond->dev->name, &fl.fl4_dst,
  2233. rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
  2234. }
  2235. ip_rt_put(rt);
  2236. }
  2237. }
  2238. /*
  2239. * Kick out a gratuitous ARP for an IP on the bonding master plus one
  2240. * for each VLAN above us.
  2241. *
  2242. * Caller must hold curr_slave_lock for read or better
  2243. */
  2244. static void bond_send_gratuitous_arp(struct bonding *bond)
  2245. {
  2246. struct slave *slave = bond->curr_active_slave;
  2247. struct vlan_entry *vlan;
  2248. struct net_device *vlan_dev;
  2249. pr_debug("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
  2250. slave ? slave->dev->name : "NULL");
  2251. if (!slave || !bond->send_grat_arp ||
  2252. test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
  2253. return;
  2254. bond->send_grat_arp--;
  2255. if (bond->master_ip) {
  2256. bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
  2257. bond->master_ip, 0);
  2258. }
  2259. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2260. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  2261. if (vlan->vlan_ip) {
  2262. bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
  2263. vlan->vlan_ip, vlan->vlan_id);
  2264. }
  2265. }
  2266. }
  2267. static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
  2268. {
  2269. int i;
  2270. __be32 *targets = bond->params.arp_targets;
  2271. targets = bond->params.arp_targets;
  2272. for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
  2273. pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
  2274. &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
  2275. if (sip == targets[i]) {
  2276. if (bond_has_this_ip(bond, tip))
  2277. slave->last_arp_rx = jiffies;
  2278. return;
  2279. }
  2280. }
  2281. }
  2282. static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  2283. {
  2284. struct arphdr *arp;
  2285. struct slave *slave;
  2286. struct bonding *bond;
  2287. unsigned char *arp_ptr;
  2288. __be32 sip, tip;
  2289. if (dev_net(dev) != &init_net)
  2290. goto out;
  2291. if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
  2292. goto out;
  2293. bond = netdev_priv(dev);
  2294. read_lock(&bond->lock);
  2295. pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
  2296. bond->dev->name, skb->dev ? skb->dev->name : "NULL",
  2297. orig_dev ? orig_dev->name : "NULL");
  2298. slave = bond_get_slave_by_dev(bond, orig_dev);
  2299. if (!slave || !slave_do_arp_validate(bond, slave))
  2300. goto out_unlock;
  2301. if (!pskb_may_pull(skb, arp_hdr_len(dev)))
  2302. goto out_unlock;
  2303. arp = arp_hdr(skb);
  2304. if (arp->ar_hln != dev->addr_len ||
  2305. skb->pkt_type == PACKET_OTHERHOST ||
  2306. skb->pkt_type == PACKET_LOOPBACK ||
  2307. arp->ar_hrd != htons(ARPHRD_ETHER) ||
  2308. arp->ar_pro != htons(ETH_P_IP) ||
  2309. arp->ar_pln != 4)
  2310. goto out_unlock;
  2311. arp_ptr = (unsigned char *)(arp + 1);
  2312. arp_ptr += dev->addr_len;
  2313. memcpy(&sip, arp_ptr, 4);
  2314. arp_ptr += 4 + dev->addr_len;
  2315. memcpy(&tip, arp_ptr, 4);
  2316. pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
  2317. bond->dev->name, slave->dev->name, slave->state,
  2318. bond->params.arp_validate, slave_do_arp_validate(bond, slave),
  2319. &sip, &tip);
  2320. /*
  2321. * Backup slaves won't see the ARP reply, but do come through
  2322. * here for each ARP probe (so we swap the sip/tip to validate
  2323. * the probe). In a "redundant switch, common router" type of
  2324. * configuration, the ARP probe will (hopefully) travel from
  2325. * the active, through one switch, the router, then the other
  2326. * switch before reaching the backup.
  2327. */
  2328. if (slave->state == BOND_STATE_ACTIVE)
  2329. bond_validate_arp(bond, slave, sip, tip);
  2330. else
  2331. bond_validate_arp(bond, slave, tip, sip);
  2332. out_unlock:
  2333. read_unlock(&bond->lock);
  2334. out:
  2335. dev_kfree_skb(skb);
  2336. return NET_RX_SUCCESS;
  2337. }
  2338. /*
  2339. * this function is called regularly to monitor each slave's link
  2340. * ensuring that traffic is being sent and received when arp monitoring
  2341. * is used in load-balancing mode. if the adapter has been dormant, then an
  2342. * arp is transmitted to generate traffic. see activebackup_arp_monitor for
  2343. * arp monitoring in active backup mode.
  2344. */
  2345. void bond_loadbalance_arp_mon(struct work_struct *work)
  2346. {
  2347. struct bonding *bond = container_of(work, struct bonding,
  2348. arp_work.work);
  2349. struct slave *slave, *oldcurrent;
  2350. int do_failover = 0;
  2351. int delta_in_ticks;
  2352. int i;
  2353. read_lock(&bond->lock);
  2354. delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  2355. if (bond->kill_timers) {
  2356. goto out;
  2357. }
  2358. if (bond->slave_cnt == 0) {
  2359. goto re_arm;
  2360. }
  2361. read_lock(&bond->curr_slave_lock);
  2362. oldcurrent = bond->curr_active_slave;
  2363. read_unlock(&bond->curr_slave_lock);
  2364. /* see if any of the previous devices are up now (i.e. they have
  2365. * xmt and rcv traffic). the curr_active_slave does not come into
  2366. * the picture unless it is null. also, slave->jiffies is not needed
  2367. * here because we send an arp on each slave and give a slave as
  2368. * long as it needs to get the tx/rx within the delta.
  2369. * TODO: what about up/down delay in arp mode? it wasn't here before
  2370. * so it can wait
  2371. */
  2372. bond_for_each_slave(bond, slave, i) {
  2373. if (slave->link != BOND_LINK_UP) {
  2374. if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
  2375. time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
  2376. slave->link = BOND_LINK_UP;
  2377. slave->state = BOND_STATE_ACTIVE;
  2378. /* primary_slave has no meaning in round-robin
  2379. * mode. the window of a slave being up and
  2380. * curr_active_slave being null after enslaving
  2381. * is closed.
  2382. */
  2383. if (!oldcurrent) {
  2384. printk(KERN_INFO DRV_NAME
  2385. ": %s: link status definitely "
  2386. "up for interface %s, ",
  2387. bond->dev->name,
  2388. slave->dev->name);
  2389. do_failover = 1;
  2390. } else {
  2391. printk(KERN_INFO DRV_NAME
  2392. ": %s: interface %s is now up\n",
  2393. bond->dev->name,
  2394. slave->dev->name);
  2395. }
  2396. }
  2397. } else {
  2398. /* slave->link == BOND_LINK_UP */
  2399. /* not all switches will respond to an arp request
  2400. * when the source ip is 0, so don't take the link down
  2401. * if we don't know our ip yet
  2402. */
  2403. if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
  2404. (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
  2405. slave->link = BOND_LINK_DOWN;
  2406. slave->state = BOND_STATE_BACKUP;
  2407. if (slave->link_failure_count < UINT_MAX) {
  2408. slave->link_failure_count++;
  2409. }
  2410. printk(KERN_INFO DRV_NAME
  2411. ": %s: interface %s is now down.\n",
  2412. bond->dev->name,
  2413. slave->dev->name);
  2414. if (slave == oldcurrent) {
  2415. do_failover = 1;
  2416. }
  2417. }
  2418. }
  2419. /* note: if switch is in round-robin mode, all links
  2420. * must tx arp to ensure all links rx an arp - otherwise
  2421. * links may oscillate or not come up at all; if switch is
  2422. * in something like xor mode, there is nothing we can
  2423. * do - all replies will be rx'ed on same link causing slaves
  2424. * to be unstable during low/no traffic periods
  2425. */
  2426. if (IS_UP(slave->dev)) {
  2427. bond_arp_send_all(bond, slave);
  2428. }
  2429. }
  2430. if (do_failover) {
  2431. write_lock_bh(&bond->curr_slave_lock);
  2432. bond_select_active_slave(bond);
  2433. write_unlock_bh(&bond->curr_slave_lock);
  2434. }
  2435. re_arm:
  2436. if (bond->params.arp_interval)
  2437. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  2438. out:
  2439. read_unlock(&bond->lock);
  2440. }
  2441. /*
  2442. * Called to inspect slaves for active-backup mode ARP monitor link state
  2443. * changes. Sets new_link in slaves to specify what action should take
  2444. * place for the slave. Returns 0 if no changes are found, >0 if changes
  2445. * to link states must be committed.
  2446. *
  2447. * Called with bond->lock held for read.
  2448. */
  2449. static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
  2450. {
  2451. struct slave *slave;
  2452. int i, commit = 0;
  2453. bond_for_each_slave(bond, slave, i) {
  2454. slave->new_link = BOND_LINK_NOCHANGE;
  2455. if (slave->link != BOND_LINK_UP) {
  2456. if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
  2457. delta_in_ticks)) {
  2458. slave->new_link = BOND_LINK_UP;
  2459. commit++;
  2460. }
  2461. continue;
  2462. }
  2463. /*
  2464. * Give slaves 2*delta after being enslaved or made
  2465. * active. This avoids bouncing, as the last receive
  2466. * times need a full ARP monitor cycle to be updated.
  2467. */
  2468. if (!time_after_eq(jiffies, slave->jiffies +
  2469. 2 * delta_in_ticks))
  2470. continue;
  2471. /*
  2472. * Backup slave is down if:
  2473. * - No current_arp_slave AND
  2474. * - more than 3*delta since last receive AND
  2475. * - the bond has an IP address
  2476. *
  2477. * Note: a non-null current_arp_slave indicates
  2478. * the curr_active_slave went down and we are
  2479. * searching for a new one; under this condition
  2480. * we only take the curr_active_slave down - this
  2481. * gives each slave a chance to tx/rx traffic
  2482. * before being taken out
  2483. */
  2484. if (slave->state == BOND_STATE_BACKUP &&
  2485. !bond->current_arp_slave &&
  2486. time_after(jiffies, slave_last_rx(bond, slave) +
  2487. 3 * delta_in_ticks)) {
  2488. slave->new_link = BOND_LINK_DOWN;
  2489. commit++;
  2490. }
  2491. /*
  2492. * Active slave is down if:
  2493. * - more than 2*delta since transmitting OR
  2494. * - (more than 2*delta since receive AND
  2495. * the bond has an IP address)
  2496. */
  2497. if ((slave->state == BOND_STATE_ACTIVE) &&
  2498. (time_after_eq(jiffies, slave->dev->trans_start +
  2499. 2 * delta_in_ticks) ||
  2500. (time_after_eq(jiffies, slave_last_rx(bond, slave)
  2501. + 2 * delta_in_ticks)))) {
  2502. slave->new_link = BOND_LINK_DOWN;
  2503. commit++;
  2504. }
  2505. }
  2506. read_lock(&bond->curr_slave_lock);
  2507. /*
  2508. * Trigger a commit if the primary option setting has changed.
  2509. */
  2510. if (bond->primary_slave &&
  2511. (bond->primary_slave != bond->curr_active_slave) &&
  2512. (bond->primary_slave->link == BOND_LINK_UP))
  2513. commit++;
  2514. read_unlock(&bond->curr_slave_lock);
  2515. return commit;
  2516. }
  2517. /*
  2518. * Called to commit link state changes noted by inspection step of
  2519. * active-backup mode ARP monitor.
  2520. *
  2521. * Called with RTNL and bond->lock for read.
  2522. */
  2523. static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
  2524. {
  2525. struct slave *slave;
  2526. int i;
  2527. bond_for_each_slave(bond, slave, i) {
  2528. switch (slave->new_link) {
  2529. case BOND_LINK_NOCHANGE:
  2530. continue;
  2531. case BOND_LINK_UP:
  2532. write_lock_bh(&bond->curr_slave_lock);
  2533. if (!bond->curr_active_slave &&
  2534. time_before_eq(jiffies, slave->dev->trans_start +
  2535. delta_in_ticks)) {
  2536. slave->link = BOND_LINK_UP;
  2537. bond_change_active_slave(bond, slave);
  2538. bond->current_arp_slave = NULL;
  2539. printk(KERN_INFO DRV_NAME
  2540. ": %s: %s is up and now the "
  2541. "active interface\n",
  2542. bond->dev->name, slave->dev->name);
  2543. } else if (bond->curr_active_slave != slave) {
  2544. /* this slave has just come up but we
  2545. * already have a current slave; this can
  2546. * also happen if bond_enslave adds a new
  2547. * slave that is up while we are searching
  2548. * for a new slave
  2549. */
  2550. slave->link = BOND_LINK_UP;
  2551. bond_set_slave_inactive_flags(slave);
  2552. bond->current_arp_slave = NULL;
  2553. printk(KERN_INFO DRV_NAME
  2554. ": %s: backup interface %s is now up\n",
  2555. bond->dev->name, slave->dev->name);
  2556. }
  2557. write_unlock_bh(&bond->curr_slave_lock);
  2558. break;
  2559. case BOND_LINK_DOWN:
  2560. if (slave->link_failure_count < UINT_MAX)
  2561. slave->link_failure_count++;
  2562. slave->link = BOND_LINK_DOWN;
  2563. if (slave == bond->curr_active_slave) {
  2564. printk(KERN_INFO DRV_NAME
  2565. ": %s: link status down for active "
  2566. "interface %s, disabling it\n",
  2567. bond->dev->name, slave->dev->name);
  2568. bond_set_slave_inactive_flags(slave);
  2569. write_lock_bh(&bond->curr_slave_lock);
  2570. bond_select_active_slave(bond);
  2571. if (bond->curr_active_slave)
  2572. bond->curr_active_slave->jiffies =
  2573. jiffies;
  2574. write_unlock_bh(&bond->curr_slave_lock);
  2575. bond->current_arp_slave = NULL;
  2576. } else if (slave->state == BOND_STATE_BACKUP) {
  2577. printk(KERN_INFO DRV_NAME
  2578. ": %s: backup interface %s is now down\n",
  2579. bond->dev->name, slave->dev->name);
  2580. bond_set_slave_inactive_flags(slave);
  2581. }
  2582. break;
  2583. default:
  2584. printk(KERN_ERR DRV_NAME
  2585. ": %s: impossible: new_link %d on slave %s\n",
  2586. bond->dev->name, slave->new_link,
  2587. slave->dev->name);
  2588. }
  2589. }
  2590. /*
  2591. * No race with changes to primary via sysfs, as we hold rtnl.
  2592. */
  2593. if (bond->primary_slave &&
  2594. (bond->primary_slave != bond->curr_active_slave) &&
  2595. (bond->primary_slave->link == BOND_LINK_UP)) {
  2596. write_lock_bh(&bond->curr_slave_lock);
  2597. bond_change_active_slave(bond, bond->primary_slave);
  2598. write_unlock_bh(&bond->curr_slave_lock);
  2599. }
  2600. bond_set_carrier(bond);
  2601. }
  2602. /*
  2603. * Send ARP probes for active-backup mode ARP monitor.
  2604. *
  2605. * Called with bond->lock held for read.
  2606. */
  2607. static void bond_ab_arp_probe(struct bonding *bond)
  2608. {
  2609. struct slave *slave;
  2610. int i;
  2611. read_lock(&bond->curr_slave_lock);
  2612. if (bond->current_arp_slave && bond->curr_active_slave)
  2613. printk("PROBE: c_arp %s && cas %s BAD\n",
  2614. bond->current_arp_slave->dev->name,
  2615. bond->curr_active_slave->dev->name);
  2616. if (bond->curr_active_slave) {
  2617. bond_arp_send_all(bond, bond->curr_active_slave);
  2618. read_unlock(&bond->curr_slave_lock);
  2619. return;
  2620. }
  2621. read_unlock(&bond->curr_slave_lock);
  2622. /* if we don't have a curr_active_slave, search for the next available
  2623. * backup slave from the current_arp_slave and make it the candidate
  2624. * for becoming the curr_active_slave
  2625. */
  2626. if (!bond->current_arp_slave) {
  2627. bond->current_arp_slave = bond->first_slave;
  2628. if (!bond->current_arp_slave)
  2629. return;
  2630. }
  2631. bond_set_slave_inactive_flags(bond->current_arp_slave);
  2632. /* search for next candidate */
  2633. bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
  2634. if (IS_UP(slave->dev)) {
  2635. slave->link = BOND_LINK_BACK;
  2636. bond_set_slave_active_flags(slave);
  2637. bond_arp_send_all(bond, slave);
  2638. slave->jiffies = jiffies;
  2639. bond->current_arp_slave = slave;
  2640. break;
  2641. }
  2642. /* if the link state is up at this point, we
  2643. * mark it down - this can happen if we have
  2644. * simultaneous link failures and
  2645. * reselect_active_interface doesn't make this
  2646. * one the current slave so it is still marked
  2647. * up when it is actually down
  2648. */
  2649. if (slave->link == BOND_LINK_UP) {
  2650. slave->link = BOND_LINK_DOWN;
  2651. if (slave->link_failure_count < UINT_MAX)
  2652. slave->link_failure_count++;
  2653. bond_set_slave_inactive_flags(slave);
  2654. printk(KERN_INFO DRV_NAME
  2655. ": %s: backup interface %s is now down.\n",
  2656. bond->dev->name, slave->dev->name);
  2657. }
  2658. }
  2659. }
  2660. void bond_activebackup_arp_mon(struct work_struct *work)
  2661. {
  2662. struct bonding *bond = container_of(work, struct bonding,
  2663. arp_work.work);
  2664. int delta_in_ticks;
  2665. read_lock(&bond->lock);
  2666. if (bond->kill_timers)
  2667. goto out;
  2668. delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  2669. if (bond->slave_cnt == 0)
  2670. goto re_arm;
  2671. if (bond->send_grat_arp) {
  2672. read_lock(&bond->curr_slave_lock);
  2673. bond_send_gratuitous_arp(bond);
  2674. read_unlock(&bond->curr_slave_lock);
  2675. }
  2676. if (bond->send_unsol_na) {
  2677. read_lock(&bond->curr_slave_lock);
  2678. bond_send_unsolicited_na(bond);
  2679. read_unlock(&bond->curr_slave_lock);
  2680. }
  2681. if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
  2682. read_unlock(&bond->lock);
  2683. rtnl_lock();
  2684. read_lock(&bond->lock);
  2685. bond_ab_arp_commit(bond, delta_in_ticks);
  2686. read_unlock(&bond->lock);
  2687. rtnl_unlock();
  2688. read_lock(&bond->lock);
  2689. }
  2690. bond_ab_arp_probe(bond);
  2691. re_arm:
  2692. if (bond->params.arp_interval) {
  2693. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  2694. }
  2695. out:
  2696. read_unlock(&bond->lock);
  2697. }
  2698. /*------------------------------ proc/seq_file-------------------------------*/
  2699. #ifdef CONFIG_PROC_FS
  2700. static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
  2701. __acquires(&dev_base_lock)
  2702. __acquires(&bond->lock)
  2703. {
  2704. struct bonding *bond = seq->private;
  2705. loff_t off = 0;
  2706. struct slave *slave;
  2707. int i;
  2708. /* make sure the bond won't be taken away */
  2709. read_lock(&dev_base_lock);
  2710. read_lock(&bond->lock);
  2711. if (*pos == 0) {
  2712. return SEQ_START_TOKEN;
  2713. }
  2714. bond_for_each_slave(bond, slave, i) {
  2715. if (++off == *pos) {
  2716. return slave;
  2717. }
  2718. }
  2719. return NULL;
  2720. }
  2721. static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2722. {
  2723. struct bonding *bond = seq->private;
  2724. struct slave *slave = v;
  2725. ++*pos;
  2726. if (v == SEQ_START_TOKEN) {
  2727. return bond->first_slave;
  2728. }
  2729. slave = slave->next;
  2730. return (slave == bond->first_slave) ? NULL : slave;
  2731. }
  2732. static void bond_info_seq_stop(struct seq_file *seq, void *v)
  2733. __releases(&bond->lock)
  2734. __releases(&dev_base_lock)
  2735. {
  2736. struct bonding *bond = seq->private;
  2737. read_unlock(&bond->lock);
  2738. read_unlock(&dev_base_lock);
  2739. }
  2740. static void bond_info_show_master(struct seq_file *seq)
  2741. {
  2742. struct bonding *bond = seq->private;
  2743. struct slave *curr;
  2744. int i;
  2745. read_lock(&bond->curr_slave_lock);
  2746. curr = bond->curr_active_slave;
  2747. read_unlock(&bond->curr_slave_lock);
  2748. seq_printf(seq, "Bonding Mode: %s",
  2749. bond_mode_name(bond->params.mode));
  2750. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
  2751. bond->params.fail_over_mac)
  2752. seq_printf(seq, " (fail_over_mac %s)",
  2753. fail_over_mac_tbl[bond->params.fail_over_mac].modename);
  2754. seq_printf(seq, "\n");
  2755. if (bond->params.mode == BOND_MODE_XOR ||
  2756. bond->params.mode == BOND_MODE_8023AD) {
  2757. seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
  2758. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  2759. bond->params.xmit_policy);
  2760. }
  2761. if (USES_PRIMARY(bond->params.mode)) {
  2762. seq_printf(seq, "Primary Slave: %s\n",
  2763. (bond->primary_slave) ?
  2764. bond->primary_slave->dev->name : "None");
  2765. seq_printf(seq, "Currently Active Slave: %s\n",
  2766. (curr) ? curr->dev->name : "None");
  2767. }
  2768. seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
  2769. "up" : "down");
  2770. seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
  2771. seq_printf(seq, "Up Delay (ms): %d\n",
  2772. bond->params.updelay * bond->params.miimon);
  2773. seq_printf(seq, "Down Delay (ms): %d\n",
  2774. bond->params.downdelay * bond->params.miimon);
  2775. /* ARP information */
  2776. if(bond->params.arp_interval > 0) {
  2777. int printed=0;
  2778. seq_printf(seq, "ARP Polling Interval (ms): %d\n",
  2779. bond->params.arp_interval);
  2780. seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
  2781. for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
  2782. if (!bond->params.arp_targets[i])
  2783. continue;
  2784. if (printed)
  2785. seq_printf(seq, ",");
  2786. seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
  2787. printed = 1;
  2788. }
  2789. seq_printf(seq, "\n");
  2790. }
  2791. if (bond->params.mode == BOND_MODE_8023AD) {
  2792. struct ad_info ad_info;
  2793. seq_puts(seq, "\n802.3ad info\n");
  2794. seq_printf(seq, "LACP rate: %s\n",
  2795. (bond->params.lacp_fast) ? "fast" : "slow");
  2796. seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
  2797. ad_select_tbl[bond->params.ad_select].modename);
  2798. if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
  2799. seq_printf(seq, "bond %s has no active aggregator\n",
  2800. bond->dev->name);
  2801. } else {
  2802. seq_printf(seq, "Active Aggregator Info:\n");
  2803. seq_printf(seq, "\tAggregator ID: %d\n",
  2804. ad_info.aggregator_id);
  2805. seq_printf(seq, "\tNumber of ports: %d\n",
  2806. ad_info.ports);
  2807. seq_printf(seq, "\tActor Key: %d\n",
  2808. ad_info.actor_key);
  2809. seq_printf(seq, "\tPartner Key: %d\n",
  2810. ad_info.partner_key);
  2811. seq_printf(seq, "\tPartner Mac Address: %pM\n",
  2812. ad_info.partner_system);
  2813. }
  2814. }
  2815. }
  2816. static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
  2817. {
  2818. struct bonding *bond = seq->private;
  2819. seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
  2820. seq_printf(seq, "MII Status: %s\n",
  2821. (slave->link == BOND_LINK_UP) ? "up" : "down");
  2822. seq_printf(seq, "Link Failure Count: %u\n",
  2823. slave->link_failure_count);
  2824. seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
  2825. if (bond->params.mode == BOND_MODE_8023AD) {
  2826. const struct aggregator *agg
  2827. = SLAVE_AD_INFO(slave).port.aggregator;
  2828. if (agg) {
  2829. seq_printf(seq, "Aggregator ID: %d\n",
  2830. agg->aggregator_identifier);
  2831. } else {
  2832. seq_puts(seq, "Aggregator ID: N/A\n");
  2833. }
  2834. }
  2835. }
  2836. static int bond_info_seq_show(struct seq_file *seq, void *v)
  2837. {
  2838. if (v == SEQ_START_TOKEN) {
  2839. seq_printf(seq, "%s\n", version);
  2840. bond_info_show_master(seq);
  2841. } else {
  2842. bond_info_show_slave(seq, v);
  2843. }
  2844. return 0;
  2845. }
  2846. static const struct seq_operations bond_info_seq_ops = {
  2847. .start = bond_info_seq_start,
  2848. .next = bond_info_seq_next,
  2849. .stop = bond_info_seq_stop,
  2850. .show = bond_info_seq_show,
  2851. };
  2852. static int bond_info_open(struct inode *inode, struct file *file)
  2853. {
  2854. struct seq_file *seq;
  2855. struct proc_dir_entry *proc;
  2856. int res;
  2857. res = seq_open(file, &bond_info_seq_ops);
  2858. if (!res) {
  2859. /* recover the pointer buried in proc_dir_entry data */
  2860. seq = file->private_data;
  2861. proc = PDE(inode);
  2862. seq->private = proc->data;
  2863. }
  2864. return res;
  2865. }
  2866. static const struct file_operations bond_info_fops = {
  2867. .owner = THIS_MODULE,
  2868. .open = bond_info_open,
  2869. .read = seq_read,
  2870. .llseek = seq_lseek,
  2871. .release = seq_release,
  2872. };
  2873. static int bond_create_proc_entry(struct bonding *bond)
  2874. {
  2875. struct net_device *bond_dev = bond->dev;
  2876. if (bond_proc_dir) {
  2877. bond->proc_entry = proc_create_data(bond_dev->name,
  2878. S_IRUGO, bond_proc_dir,
  2879. &bond_info_fops, bond);
  2880. if (bond->proc_entry == NULL) {
  2881. printk(KERN_WARNING DRV_NAME
  2882. ": Warning: Cannot create /proc/net/%s/%s\n",
  2883. DRV_NAME, bond_dev->name);
  2884. } else {
  2885. memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
  2886. }
  2887. }
  2888. return 0;
  2889. }
  2890. static void bond_remove_proc_entry(struct bonding *bond)
  2891. {
  2892. if (bond_proc_dir && bond->proc_entry) {
  2893. remove_proc_entry(bond->proc_file_name, bond_proc_dir);
  2894. memset(bond->proc_file_name, 0, IFNAMSIZ);
  2895. bond->proc_entry = NULL;
  2896. }
  2897. }
  2898. /* Create the bonding directory under /proc/net, if doesn't exist yet.
  2899. * Caller must hold rtnl_lock.
  2900. */
  2901. static void bond_create_proc_dir(void)
  2902. {
  2903. int len = strlen(DRV_NAME);
  2904. for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
  2905. bond_proc_dir = bond_proc_dir->next) {
  2906. if ((bond_proc_dir->namelen == len) &&
  2907. !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
  2908. break;
  2909. }
  2910. }
  2911. if (!bond_proc_dir) {
  2912. bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
  2913. if (bond_proc_dir) {
  2914. bond_proc_dir->owner = THIS_MODULE;
  2915. } else {
  2916. printk(KERN_WARNING DRV_NAME
  2917. ": Warning: cannot create /proc/net/%s\n",
  2918. DRV_NAME);
  2919. }
  2920. }
  2921. }
  2922. /* Destroy the bonding directory under /proc/net, if empty.
  2923. * Caller must hold rtnl_lock.
  2924. */
  2925. static void bond_destroy_proc_dir(void)
  2926. {
  2927. struct proc_dir_entry *de;
  2928. if (!bond_proc_dir) {
  2929. return;
  2930. }
  2931. /* verify that the /proc dir is empty */
  2932. for (de = bond_proc_dir->subdir; de; de = de->next) {
  2933. /* ignore . and .. */
  2934. if (*(de->name) != '.') {
  2935. break;
  2936. }
  2937. }
  2938. if (de) {
  2939. if (bond_proc_dir->owner == THIS_MODULE) {
  2940. bond_proc_dir->owner = NULL;
  2941. }
  2942. } else {
  2943. remove_proc_entry(DRV_NAME, init_net.proc_net);
  2944. bond_proc_dir = NULL;
  2945. }
  2946. }
  2947. #endif /* CONFIG_PROC_FS */
  2948. /*-------------------------- netdev event handling --------------------------*/
  2949. /*
  2950. * Change device name
  2951. */
  2952. static int bond_event_changename(struct bonding *bond)
  2953. {
  2954. #ifdef CONFIG_PROC_FS
  2955. bond_remove_proc_entry(bond);
  2956. bond_create_proc_entry(bond);
  2957. #endif
  2958. down_write(&(bonding_rwsem));
  2959. bond_destroy_sysfs_entry(bond);
  2960. bond_create_sysfs_entry(bond);
  2961. up_write(&(bonding_rwsem));
  2962. return NOTIFY_DONE;
  2963. }
  2964. static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
  2965. {
  2966. struct bonding *event_bond = netdev_priv(bond_dev);
  2967. switch (event) {
  2968. case NETDEV_CHANGENAME:
  2969. return bond_event_changename(event_bond);
  2970. case NETDEV_UNREGISTER:
  2971. bond_release_all(event_bond->dev);
  2972. break;
  2973. default:
  2974. break;
  2975. }
  2976. return NOTIFY_DONE;
  2977. }
  2978. static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
  2979. {
  2980. struct net_device *bond_dev = slave_dev->master;
  2981. struct bonding *bond = netdev_priv(bond_dev);
  2982. switch (event) {
  2983. case NETDEV_UNREGISTER:
  2984. if (bond_dev) {
  2985. if (bond->setup_by_slave)
  2986. bond_release_and_destroy(bond_dev, slave_dev);
  2987. else
  2988. bond_release(bond_dev, slave_dev);
  2989. }
  2990. break;
  2991. case NETDEV_CHANGE:
  2992. /*
  2993. * TODO: is this what we get if somebody
  2994. * sets up a hierarchical bond, then rmmod's
  2995. * one of the slave bonding devices?
  2996. */
  2997. break;
  2998. case NETDEV_DOWN:
  2999. /*
  3000. * ... Or is it this?
  3001. */
  3002. break;
  3003. case NETDEV_CHANGEMTU:
  3004. /*
  3005. * TODO: Should slaves be allowed to
  3006. * independently alter their MTU? For
  3007. * an active-backup bond, slaves need
  3008. * not be the same type of device, so
  3009. * MTUs may vary. For other modes,
  3010. * slaves arguably should have the
  3011. * same MTUs. To do this, we'd need to
  3012. * take over the slave's change_mtu
  3013. * function for the duration of their
  3014. * servitude.
  3015. */
  3016. break;
  3017. case NETDEV_CHANGENAME:
  3018. /*
  3019. * TODO: handle changing the primary's name
  3020. */
  3021. break;
  3022. case NETDEV_FEAT_CHANGE:
  3023. bond_compute_features(bond);
  3024. break;
  3025. default:
  3026. break;
  3027. }
  3028. return NOTIFY_DONE;
  3029. }
  3030. /*
  3031. * bond_netdev_event: handle netdev notifier chain events.
  3032. *
  3033. * This function receives events for the netdev chain. The caller (an
  3034. * ioctl handler calling blocking_notifier_call_chain) holds the necessary
  3035. * locks for us to safely manipulate the slave devices (RTNL lock,
  3036. * dev_probe_lock).
  3037. */
  3038. static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  3039. {
  3040. struct net_device *event_dev = (struct net_device *)ptr;
  3041. if (dev_net(event_dev) != &init_net)
  3042. return NOTIFY_DONE;
  3043. pr_debug("event_dev: %s, event: %lx\n",
  3044. (event_dev ? event_dev->name : "None"),
  3045. event);
  3046. if (!(event_dev->priv_flags & IFF_BONDING))
  3047. return NOTIFY_DONE;
  3048. if (event_dev->flags & IFF_MASTER) {
  3049. pr_debug("IFF_MASTER\n");
  3050. return bond_master_netdev_event(event, event_dev);
  3051. }
  3052. if (event_dev->flags & IFF_SLAVE) {
  3053. pr_debug("IFF_SLAVE\n");
  3054. return bond_slave_netdev_event(event, event_dev);
  3055. }
  3056. return NOTIFY_DONE;
  3057. }
  3058. /*
  3059. * bond_inetaddr_event: handle inetaddr notifier chain events.
  3060. *
  3061. * We keep track of device IPs primarily to use as source addresses in
  3062. * ARP monitor probes (rather than spewing out broadcasts all the time).
  3063. *
  3064. * We track one IP for the main device (if it has one), plus one per VLAN.
  3065. */
  3066. static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  3067. {
  3068. struct in_ifaddr *ifa = ptr;
  3069. struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
  3070. struct bonding *bond;
  3071. struct vlan_entry *vlan;
  3072. if (dev_net(ifa->ifa_dev->dev) != &init_net)
  3073. return NOTIFY_DONE;
  3074. list_for_each_entry(bond, &bond_dev_list, bond_list) {
  3075. if (bond->dev == event_dev) {
  3076. switch (event) {
  3077. case NETDEV_UP:
  3078. bond->master_ip = ifa->ifa_local;
  3079. return NOTIFY_OK;
  3080. case NETDEV_DOWN:
  3081. bond->master_ip = bond_glean_dev_ip(bond->dev);
  3082. return NOTIFY_OK;
  3083. default:
  3084. return NOTIFY_DONE;
  3085. }
  3086. }
  3087. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  3088. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  3089. if (vlan_dev == event_dev) {
  3090. switch (event) {
  3091. case NETDEV_UP:
  3092. vlan->vlan_ip = ifa->ifa_local;
  3093. return NOTIFY_OK;
  3094. case NETDEV_DOWN:
  3095. vlan->vlan_ip =
  3096. bond_glean_dev_ip(vlan_dev);
  3097. return NOTIFY_OK;
  3098. default:
  3099. return NOTIFY_DONE;
  3100. }
  3101. }
  3102. }
  3103. }
  3104. return NOTIFY_DONE;
  3105. }
  3106. static struct notifier_block bond_netdev_notifier = {
  3107. .notifier_call = bond_netdev_event,
  3108. };
  3109. static struct notifier_block bond_inetaddr_notifier = {
  3110. .notifier_call = bond_inetaddr_event,
  3111. };
  3112. /*-------------------------- Packet type handling ---------------------------*/
  3113. /* register to receive lacpdus on a bond */
  3114. static void bond_register_lacpdu(struct bonding *bond)
  3115. {
  3116. struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
  3117. /* initialize packet type */
  3118. pk_type->type = PKT_TYPE_LACPDU;
  3119. pk_type->dev = bond->dev;
  3120. pk_type->func = bond_3ad_lacpdu_recv;
  3121. dev_add_pack(pk_type);
  3122. }
  3123. /* unregister to receive lacpdus on a bond */
  3124. static void bond_unregister_lacpdu(struct bonding *bond)
  3125. {
  3126. dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
  3127. }
  3128. void bond_register_arp(struct bonding *bond)
  3129. {
  3130. struct packet_type *pt = &bond->arp_mon_pt;
  3131. if (pt->type)
  3132. return;
  3133. pt->type = htons(ETH_P_ARP);
  3134. pt->dev = bond->dev;
  3135. pt->func = bond_arp_rcv;
  3136. dev_add_pack(pt);
  3137. }
  3138. void bond_unregister_arp(struct bonding *bond)
  3139. {
  3140. struct packet_type *pt = &bond->arp_mon_pt;
  3141. dev_remove_pack(pt);
  3142. pt->type = 0;
  3143. }
  3144. /*---------------------------- Hashing Policies -----------------------------*/
  3145. /*
  3146. * Hash for the output device based upon layer 2 and layer 3 data. If
  3147. * the packet is not IP mimic bond_xmit_hash_policy_l2()
  3148. */
  3149. static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
  3150. struct net_device *bond_dev, int count)
  3151. {
  3152. struct ethhdr *data = (struct ethhdr *)skb->data;
  3153. struct iphdr *iph = ip_hdr(skb);
  3154. if (skb->protocol == htons(ETH_P_IP)) {
  3155. return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
  3156. (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
  3157. }
  3158. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3159. }
  3160. /*
  3161. * Hash for the output device based upon layer 3 and layer 4 data. If
  3162. * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
  3163. * altogether not IP, mimic bond_xmit_hash_policy_l2()
  3164. */
  3165. static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
  3166. struct net_device *bond_dev, int count)
  3167. {
  3168. struct ethhdr *data = (struct ethhdr *)skb->data;
  3169. struct iphdr *iph = ip_hdr(skb);
  3170. __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
  3171. int layer4_xor = 0;
  3172. if (skb->protocol == htons(ETH_P_IP)) {
  3173. if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
  3174. (iph->protocol == IPPROTO_TCP ||
  3175. iph->protocol == IPPROTO_UDP)) {
  3176. layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
  3177. }
  3178. return (layer4_xor ^
  3179. ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
  3180. }
  3181. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3182. }
  3183. /*
  3184. * Hash for the output device based upon layer 2 data
  3185. */
  3186. static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
  3187. struct net_device *bond_dev, int count)
  3188. {
  3189. struct ethhdr *data = (struct ethhdr *)skb->data;
  3190. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3191. }
  3192. /*-------------------------- Device entry points ----------------------------*/
  3193. static int bond_open(struct net_device *bond_dev)
  3194. {
  3195. struct bonding *bond = netdev_priv(bond_dev);
  3196. bond->kill_timers = 0;
  3197. if (bond_is_lb(bond)) {
  3198. /* bond_alb_initialize must be called before the timer
  3199. * is started.
  3200. */
  3201. if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
  3202. /* something went wrong - fail the open operation */
  3203. return -1;
  3204. }
  3205. INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
  3206. queue_delayed_work(bond->wq, &bond->alb_work, 0);
  3207. }
  3208. if (bond->params.miimon) { /* link check interval, in milliseconds. */
  3209. INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
  3210. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  3211. }
  3212. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3213. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
  3214. INIT_DELAYED_WORK(&bond->arp_work,
  3215. bond_activebackup_arp_mon);
  3216. else
  3217. INIT_DELAYED_WORK(&bond->arp_work,
  3218. bond_loadbalance_arp_mon);
  3219. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  3220. if (bond->params.arp_validate)
  3221. bond_register_arp(bond);
  3222. }
  3223. if (bond->params.mode == BOND_MODE_8023AD) {
  3224. INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
  3225. queue_delayed_work(bond->wq, &bond->ad_work, 0);
  3226. /* register to receive LACPDUs */
  3227. bond_register_lacpdu(bond);
  3228. bond_3ad_initiate_agg_selection(bond, 1);
  3229. }
  3230. return 0;
  3231. }
  3232. static int bond_close(struct net_device *bond_dev)
  3233. {
  3234. struct bonding *bond = netdev_priv(bond_dev);
  3235. if (bond->params.mode == BOND_MODE_8023AD) {
  3236. /* Unregister the receive of LACPDUs */
  3237. bond_unregister_lacpdu(bond);
  3238. }
  3239. if (bond->params.arp_validate)
  3240. bond_unregister_arp(bond);
  3241. write_lock_bh(&bond->lock);
  3242. bond->send_grat_arp = 0;
  3243. bond->send_unsol_na = 0;
  3244. /* signal timers not to re-arm */
  3245. bond->kill_timers = 1;
  3246. write_unlock_bh(&bond->lock);
  3247. if (bond->params.miimon) { /* link check interval, in milliseconds. */
  3248. cancel_delayed_work(&bond->mii_work);
  3249. }
  3250. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3251. cancel_delayed_work(&bond->arp_work);
  3252. }
  3253. switch (bond->params.mode) {
  3254. case BOND_MODE_8023AD:
  3255. cancel_delayed_work(&bond->ad_work);
  3256. break;
  3257. case BOND_MODE_TLB:
  3258. case BOND_MODE_ALB:
  3259. cancel_delayed_work(&bond->alb_work);
  3260. break;
  3261. default:
  3262. break;
  3263. }
  3264. if (bond_is_lb(bond)) {
  3265. /* Must be called only after all
  3266. * slaves have been released
  3267. */
  3268. bond_alb_deinitialize(bond);
  3269. }
  3270. return 0;
  3271. }
  3272. static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
  3273. {
  3274. struct bonding *bond = netdev_priv(bond_dev);
  3275. struct net_device_stats *stats = &bond->stats;
  3276. struct net_device_stats local_stats;
  3277. struct slave *slave;
  3278. int i;
  3279. memset(&local_stats, 0, sizeof(struct net_device_stats));
  3280. read_lock_bh(&bond->lock);
  3281. bond_for_each_slave(bond, slave, i) {
  3282. const struct net_device_stats *sstats = dev_get_stats(slave->dev);
  3283. local_stats.rx_packets += sstats->rx_packets;
  3284. local_stats.rx_bytes += sstats->rx_bytes;
  3285. local_stats.rx_errors += sstats->rx_errors;
  3286. local_stats.rx_dropped += sstats->rx_dropped;
  3287. local_stats.tx_packets += sstats->tx_packets;
  3288. local_stats.tx_bytes += sstats->tx_bytes;
  3289. local_stats.tx_errors += sstats->tx_errors;
  3290. local_stats.tx_dropped += sstats->tx_dropped;
  3291. local_stats.multicast += sstats->multicast;
  3292. local_stats.collisions += sstats->collisions;
  3293. local_stats.rx_length_errors += sstats->rx_length_errors;
  3294. local_stats.rx_over_errors += sstats->rx_over_errors;
  3295. local_stats.rx_crc_errors += sstats->rx_crc_errors;
  3296. local_stats.rx_frame_errors += sstats->rx_frame_errors;
  3297. local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
  3298. local_stats.rx_missed_errors += sstats->rx_missed_errors;
  3299. local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
  3300. local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
  3301. local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
  3302. local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
  3303. local_stats.tx_window_errors += sstats->tx_window_errors;
  3304. }
  3305. memcpy(stats, &local_stats, sizeof(struct net_device_stats));
  3306. read_unlock_bh(&bond->lock);
  3307. return stats;
  3308. }
  3309. static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
  3310. {
  3311. struct net_device *slave_dev = NULL;
  3312. struct ifbond k_binfo;
  3313. struct ifbond __user *u_binfo = NULL;
  3314. struct ifslave k_sinfo;
  3315. struct ifslave __user *u_sinfo = NULL;
  3316. struct mii_ioctl_data *mii = NULL;
  3317. int res = 0;
  3318. pr_debug("bond_ioctl: master=%s, cmd=%d\n",
  3319. bond_dev->name, cmd);
  3320. switch (cmd) {
  3321. case SIOCGMIIPHY:
  3322. mii = if_mii(ifr);
  3323. if (!mii) {
  3324. return -EINVAL;
  3325. }
  3326. mii->phy_id = 0;
  3327. /* Fall Through */
  3328. case SIOCGMIIREG:
  3329. /*
  3330. * We do this again just in case we were called by SIOCGMIIREG
  3331. * instead of SIOCGMIIPHY.
  3332. */
  3333. mii = if_mii(ifr);
  3334. if (!mii) {
  3335. return -EINVAL;
  3336. }
  3337. if (mii->reg_num == 1) {
  3338. struct bonding *bond = netdev_priv(bond_dev);
  3339. mii->val_out = 0;
  3340. read_lock(&bond->lock);
  3341. read_lock(&bond->curr_slave_lock);
  3342. if (netif_carrier_ok(bond->dev)) {
  3343. mii->val_out = BMSR_LSTATUS;
  3344. }
  3345. read_unlock(&bond->curr_slave_lock);
  3346. read_unlock(&bond->lock);
  3347. }
  3348. return 0;
  3349. case BOND_INFO_QUERY_OLD:
  3350. case SIOCBONDINFOQUERY:
  3351. u_binfo = (struct ifbond __user *)ifr->ifr_data;
  3352. if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
  3353. return -EFAULT;
  3354. }
  3355. res = bond_info_query(bond_dev, &k_binfo);
  3356. if (res == 0) {
  3357. if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
  3358. return -EFAULT;
  3359. }
  3360. }
  3361. return res;
  3362. case BOND_SLAVE_INFO_QUERY_OLD:
  3363. case SIOCBONDSLAVEINFOQUERY:
  3364. u_sinfo = (struct ifslave __user *)ifr->ifr_data;
  3365. if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
  3366. return -EFAULT;
  3367. }
  3368. res = bond_slave_info_query(bond_dev, &k_sinfo);
  3369. if (res == 0) {
  3370. if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
  3371. return -EFAULT;
  3372. }
  3373. }
  3374. return res;
  3375. default:
  3376. /* Go on */
  3377. break;
  3378. }
  3379. if (!capable(CAP_NET_ADMIN)) {
  3380. return -EPERM;
  3381. }
  3382. down_write(&(bonding_rwsem));
  3383. slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
  3384. pr_debug("slave_dev=%p: \n", slave_dev);
  3385. if (!slave_dev) {
  3386. res = -ENODEV;
  3387. } else {
  3388. pr_debug("slave_dev->name=%s: \n", slave_dev->name);
  3389. switch (cmd) {
  3390. case BOND_ENSLAVE_OLD:
  3391. case SIOCBONDENSLAVE:
  3392. res = bond_enslave(bond_dev, slave_dev);
  3393. break;
  3394. case BOND_RELEASE_OLD:
  3395. case SIOCBONDRELEASE:
  3396. res = bond_release(bond_dev, slave_dev);
  3397. break;
  3398. case BOND_SETHWADDR_OLD:
  3399. case SIOCBONDSETHWADDR:
  3400. res = bond_sethwaddr(bond_dev, slave_dev);
  3401. break;
  3402. case BOND_CHANGE_ACTIVE_OLD:
  3403. case SIOCBONDCHANGEACTIVE:
  3404. res = bond_ioctl_change_active(bond_dev, slave_dev);
  3405. break;
  3406. default:
  3407. res = -EOPNOTSUPP;
  3408. }
  3409. dev_put(slave_dev);
  3410. }
  3411. up_write(&(bonding_rwsem));
  3412. return res;
  3413. }
  3414. static void bond_set_multicast_list(struct net_device *bond_dev)
  3415. {
  3416. struct bonding *bond = netdev_priv(bond_dev);
  3417. struct dev_mc_list *dmi;
  3418. /*
  3419. * Do promisc before checking multicast_mode
  3420. */
  3421. if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
  3422. /*
  3423. * FIXME: Need to handle the error when one of the multi-slaves
  3424. * encounters error.
  3425. */
  3426. bond_set_promiscuity(bond, 1);
  3427. }
  3428. if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
  3429. bond_set_promiscuity(bond, -1);
  3430. }
  3431. /* set allmulti flag to slaves */
  3432. if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
  3433. /*
  3434. * FIXME: Need to handle the error when one of the multi-slaves
  3435. * encounters error.
  3436. */
  3437. bond_set_allmulti(bond, 1);
  3438. }
  3439. if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
  3440. bond_set_allmulti(bond, -1);
  3441. }
  3442. read_lock(&bond->lock);
  3443. bond->flags = bond_dev->flags;
  3444. /* looking for addresses to add to slaves' mc list */
  3445. for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
  3446. if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
  3447. bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
  3448. }
  3449. }
  3450. /* looking for addresses to delete from slaves' list */
  3451. for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
  3452. if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
  3453. bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
  3454. }
  3455. }
  3456. /* save master's multicast list */
  3457. bond_mc_list_destroy(bond);
  3458. bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
  3459. read_unlock(&bond->lock);
  3460. }
  3461. static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
  3462. {
  3463. struct bonding *bond = netdev_priv(dev);
  3464. struct slave *slave = bond->first_slave;
  3465. if (slave) {
  3466. const struct net_device_ops *slave_ops
  3467. = slave->dev->netdev_ops;
  3468. if (slave_ops->ndo_neigh_setup)
  3469. return slave_ops->ndo_neigh_setup(dev, parms);
  3470. }
  3471. return 0;
  3472. }
  3473. /*
  3474. * Change the MTU of all of a master's slaves to match the master
  3475. */
  3476. static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
  3477. {
  3478. struct bonding *bond = netdev_priv(bond_dev);
  3479. struct slave *slave, *stop_at;
  3480. int res = 0;
  3481. int i;
  3482. pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
  3483. (bond_dev ? bond_dev->name : "None"), new_mtu);
  3484. /* Can't hold bond->lock with bh disabled here since
  3485. * some base drivers panic. On the other hand we can't
  3486. * hold bond->lock without bh disabled because we'll
  3487. * deadlock. The only solution is to rely on the fact
  3488. * that we're under rtnl_lock here, and the slaves
  3489. * list won't change. This doesn't solve the problem
  3490. * of setting the slave's MTU while it is
  3491. * transmitting, but the assumption is that the base
  3492. * driver can handle that.
  3493. *
  3494. * TODO: figure out a way to safely iterate the slaves
  3495. * list, but without holding a lock around the actual
  3496. * call to the base driver.
  3497. */
  3498. bond_for_each_slave(bond, slave, i) {
  3499. pr_debug("s %p s->p %p c_m %p\n", slave,
  3500. slave->prev, slave->dev->netdev_ops->ndo_change_mtu);
  3501. res = dev_set_mtu(slave->dev, new_mtu);
  3502. if (res) {
  3503. /* If we failed to set the slave's mtu to the new value
  3504. * we must abort the operation even in ACTIVE_BACKUP
  3505. * mode, because if we allow the backup slaves to have
  3506. * different mtu values than the active slave we'll
  3507. * need to change their mtu when doing a failover. That
  3508. * means changing their mtu from timer context, which
  3509. * is probably not a good idea.
  3510. */
  3511. pr_debug("err %d %s\n", res, slave->dev->name);
  3512. goto unwind;
  3513. }
  3514. }
  3515. bond_dev->mtu = new_mtu;
  3516. return 0;
  3517. unwind:
  3518. /* unwind from head to the slave that failed */
  3519. stop_at = slave;
  3520. bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
  3521. int tmp_res;
  3522. tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
  3523. if (tmp_res) {
  3524. pr_debug("unwind err %d dev %s\n", tmp_res,
  3525. slave->dev->name);
  3526. }
  3527. }
  3528. return res;
  3529. }
  3530. /*
  3531. * Change HW address
  3532. *
  3533. * Note that many devices must be down to change the HW address, and
  3534. * downing the master releases all slaves. We can make bonds full of
  3535. * bonding devices to test this, however.
  3536. */
  3537. static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
  3538. {
  3539. struct bonding *bond = netdev_priv(bond_dev);
  3540. struct sockaddr *sa = addr, tmp_sa;
  3541. struct slave *slave, *stop_at;
  3542. int res = 0;
  3543. int i;
  3544. if (bond->params.mode == BOND_MODE_ALB)
  3545. return bond_alb_set_mac_address(bond_dev, addr);
  3546. pr_debug("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
  3547. /*
  3548. * If fail_over_mac is set to active, do nothing and return
  3549. * success. Returning an error causes ifenslave to fail.
  3550. */
  3551. if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
  3552. return 0;
  3553. if (!is_valid_ether_addr(sa->sa_data)) {
  3554. return -EADDRNOTAVAIL;
  3555. }
  3556. /* Can't hold bond->lock with bh disabled here since
  3557. * some base drivers panic. On the other hand we can't
  3558. * hold bond->lock without bh disabled because we'll
  3559. * deadlock. The only solution is to rely on the fact
  3560. * that we're under rtnl_lock here, and the slaves
  3561. * list won't change. This doesn't solve the problem
  3562. * of setting the slave's hw address while it is
  3563. * transmitting, but the assumption is that the base
  3564. * driver can handle that.
  3565. *
  3566. * TODO: figure out a way to safely iterate the slaves
  3567. * list, but without holding a lock around the actual
  3568. * call to the base driver.
  3569. */
  3570. bond_for_each_slave(bond, slave, i) {
  3571. const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
  3572. pr_debug("slave %p %s\n", slave, slave->dev->name);
  3573. if (slave_ops->ndo_set_mac_address == NULL) {
  3574. res = -EOPNOTSUPP;
  3575. pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
  3576. goto unwind;
  3577. }
  3578. res = dev_set_mac_address(slave->dev, addr);
  3579. if (res) {
  3580. /* TODO: consider downing the slave
  3581. * and retry ?
  3582. * User should expect communications
  3583. * breakage anyway until ARP finish
  3584. * updating, so...
  3585. */
  3586. pr_debug("err %d %s\n", res, slave->dev->name);
  3587. goto unwind;
  3588. }
  3589. }
  3590. /* success */
  3591. memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
  3592. return 0;
  3593. unwind:
  3594. memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
  3595. tmp_sa.sa_family = bond_dev->type;
  3596. /* unwind from head to the slave that failed */
  3597. stop_at = slave;
  3598. bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
  3599. int tmp_res;
  3600. tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
  3601. if (tmp_res) {
  3602. pr_debug("unwind err %d dev %s\n", tmp_res,
  3603. slave->dev->name);
  3604. }
  3605. }
  3606. return res;
  3607. }
  3608. static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
  3609. {
  3610. struct bonding *bond = netdev_priv(bond_dev);
  3611. struct slave *slave, *start_at;
  3612. int i, slave_no, res = 1;
  3613. read_lock(&bond->lock);
  3614. if (!BOND_IS_OK(bond)) {
  3615. goto out;
  3616. }
  3617. /*
  3618. * Concurrent TX may collide on rr_tx_counter; we accept that
  3619. * as being rare enough not to justify using an atomic op here
  3620. */
  3621. slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
  3622. bond_for_each_slave(bond, slave, i) {
  3623. slave_no--;
  3624. if (slave_no < 0) {
  3625. break;
  3626. }
  3627. }
  3628. start_at = slave;
  3629. bond_for_each_slave_from(bond, slave, i, start_at) {
  3630. if (IS_UP(slave->dev) &&
  3631. (slave->link == BOND_LINK_UP) &&
  3632. (slave->state == BOND_STATE_ACTIVE)) {
  3633. res = bond_dev_queue_xmit(bond, skb, slave->dev);
  3634. break;
  3635. }
  3636. }
  3637. out:
  3638. if (res) {
  3639. /* no suitable interface, frame not sent */
  3640. dev_kfree_skb(skb);
  3641. }
  3642. read_unlock(&bond->lock);
  3643. return 0;
  3644. }
  3645. /*
  3646. * in active-backup mode, we know that bond->curr_active_slave is always valid if
  3647. * the bond has a usable interface.
  3648. */
  3649. static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
  3650. {
  3651. struct bonding *bond = netdev_priv(bond_dev);
  3652. int res = 1;
  3653. read_lock(&bond->lock);
  3654. read_lock(&bond->curr_slave_lock);
  3655. if (!BOND_IS_OK(bond)) {
  3656. goto out;
  3657. }
  3658. if (!bond->curr_active_slave)
  3659. goto out;
  3660. res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
  3661. out:
  3662. if (res) {
  3663. /* no suitable interface, frame not sent */
  3664. dev_kfree_skb(skb);
  3665. }
  3666. read_unlock(&bond->curr_slave_lock);
  3667. read_unlock(&bond->lock);
  3668. return 0;
  3669. }
  3670. /*
  3671. * In bond_xmit_xor() , we determine the output device by using a pre-
  3672. * determined xmit_hash_policy(), If the selected device is not enabled,
  3673. * find the next active slave.
  3674. */
  3675. static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
  3676. {
  3677. struct bonding *bond = netdev_priv(bond_dev);
  3678. struct slave *slave, *start_at;
  3679. int slave_no;
  3680. int i;
  3681. int res = 1;
  3682. read_lock(&bond->lock);
  3683. if (!BOND_IS_OK(bond)) {
  3684. goto out;
  3685. }
  3686. slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
  3687. bond_for_each_slave(bond, slave, i) {
  3688. slave_no--;
  3689. if (slave_no < 0) {
  3690. break;
  3691. }
  3692. }
  3693. start_at = slave;
  3694. bond_for_each_slave_from(bond, slave, i, start_at) {
  3695. if (IS_UP(slave->dev) &&
  3696. (slave->link == BOND_LINK_UP) &&
  3697. (slave->state == BOND_STATE_ACTIVE)) {
  3698. res = bond_dev_queue_xmit(bond, skb, slave->dev);
  3699. break;
  3700. }
  3701. }
  3702. out:
  3703. if (res) {
  3704. /* no suitable interface, frame not sent */
  3705. dev_kfree_skb(skb);
  3706. }
  3707. read_unlock(&bond->lock);
  3708. return 0;
  3709. }
  3710. /*
  3711. * in broadcast mode, we send everything to all usable interfaces.
  3712. */
  3713. static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
  3714. {
  3715. struct bonding *bond = netdev_priv(bond_dev);
  3716. struct slave *slave, *start_at;
  3717. struct net_device *tx_dev = NULL;
  3718. int i;
  3719. int res = 1;
  3720. read_lock(&bond->lock);
  3721. if (!BOND_IS_OK(bond)) {
  3722. goto out;
  3723. }
  3724. read_lock(&bond->curr_slave_lock);
  3725. start_at = bond->curr_active_slave;
  3726. read_unlock(&bond->curr_slave_lock);
  3727. if (!start_at) {
  3728. goto out;
  3729. }
  3730. bond_for_each_slave_from(bond, slave, i, start_at) {
  3731. if (IS_UP(slave->dev) &&
  3732. (slave->link == BOND_LINK_UP) &&
  3733. (slave->state == BOND_STATE_ACTIVE)) {
  3734. if (tx_dev) {
  3735. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  3736. if (!skb2) {
  3737. printk(KERN_ERR DRV_NAME
  3738. ": %s: Error: bond_xmit_broadcast(): "
  3739. "skb_clone() failed\n",
  3740. bond_dev->name);
  3741. continue;
  3742. }
  3743. res = bond_dev_queue_xmit(bond, skb2, tx_dev);
  3744. if (res) {
  3745. dev_kfree_skb(skb2);
  3746. continue;
  3747. }
  3748. }
  3749. tx_dev = slave->dev;
  3750. }
  3751. }
  3752. if (tx_dev) {
  3753. res = bond_dev_queue_xmit(bond, skb, tx_dev);
  3754. }
  3755. out:
  3756. if (res) {
  3757. /* no suitable interface, frame not sent */
  3758. dev_kfree_skb(skb);
  3759. }
  3760. /* frame sent to all suitable interfaces */
  3761. read_unlock(&bond->lock);
  3762. return 0;
  3763. }
  3764. /*------------------------- Device initialization ---------------------------*/
  3765. static void bond_set_xmit_hash_policy(struct bonding *bond)
  3766. {
  3767. switch (bond->params.xmit_policy) {
  3768. case BOND_XMIT_POLICY_LAYER23:
  3769. bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
  3770. break;
  3771. case BOND_XMIT_POLICY_LAYER34:
  3772. bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
  3773. break;
  3774. case BOND_XMIT_POLICY_LAYER2:
  3775. default:
  3776. bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
  3777. break;
  3778. }
  3779. }
  3780. static int bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
  3781. {
  3782. const struct bonding *bond = netdev_priv(dev);
  3783. switch (bond->params.mode) {
  3784. case BOND_MODE_ROUNDROBIN:
  3785. return bond_xmit_roundrobin(skb, dev);
  3786. case BOND_MODE_ACTIVEBACKUP:
  3787. return bond_xmit_activebackup(skb, dev);
  3788. case BOND_MODE_XOR:
  3789. return bond_xmit_xor(skb, dev);
  3790. case BOND_MODE_BROADCAST:
  3791. return bond_xmit_broadcast(skb, dev);
  3792. case BOND_MODE_8023AD:
  3793. return bond_3ad_xmit_xor(skb, dev);
  3794. case BOND_MODE_ALB:
  3795. case BOND_MODE_TLB:
  3796. return bond_alb_xmit(skb, dev);
  3797. default:
  3798. /* Should never happen, mode already checked */
  3799. printk(KERN_ERR DRV_NAME ": %s: Error: Unknown bonding mode %d\n",
  3800. dev->name, bond->params.mode);
  3801. WARN_ON_ONCE(1);
  3802. dev_kfree_skb(skb);
  3803. return NETDEV_TX_OK;
  3804. }
  3805. }
  3806. /*
  3807. * set bond mode specific net device operations
  3808. */
  3809. void bond_set_mode_ops(struct bonding *bond, int mode)
  3810. {
  3811. struct net_device *bond_dev = bond->dev;
  3812. switch (mode) {
  3813. case BOND_MODE_ROUNDROBIN:
  3814. break;
  3815. case BOND_MODE_ACTIVEBACKUP:
  3816. break;
  3817. case BOND_MODE_XOR:
  3818. bond_set_xmit_hash_policy(bond);
  3819. break;
  3820. case BOND_MODE_BROADCAST:
  3821. break;
  3822. case BOND_MODE_8023AD:
  3823. bond_set_master_3ad_flags(bond);
  3824. bond_set_xmit_hash_policy(bond);
  3825. break;
  3826. case BOND_MODE_ALB:
  3827. bond_set_master_alb_flags(bond);
  3828. /* FALLTHRU */
  3829. case BOND_MODE_TLB:
  3830. break;
  3831. default:
  3832. /* Should never happen, mode already checked */
  3833. printk(KERN_ERR DRV_NAME
  3834. ": %s: Error: Unknown bonding mode %d\n",
  3835. bond_dev->name,
  3836. mode);
  3837. break;
  3838. }
  3839. }
  3840. static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
  3841. struct ethtool_drvinfo *drvinfo)
  3842. {
  3843. strncpy(drvinfo->driver, DRV_NAME, 32);
  3844. strncpy(drvinfo->version, DRV_VERSION, 32);
  3845. snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
  3846. }
  3847. static const struct ethtool_ops bond_ethtool_ops = {
  3848. .get_drvinfo = bond_ethtool_get_drvinfo,
  3849. .get_link = ethtool_op_get_link,
  3850. .get_tx_csum = ethtool_op_get_tx_csum,
  3851. .get_sg = ethtool_op_get_sg,
  3852. .get_tso = ethtool_op_get_tso,
  3853. .get_ufo = ethtool_op_get_ufo,
  3854. .get_flags = ethtool_op_get_flags,
  3855. };
  3856. static const struct net_device_ops bond_netdev_ops = {
  3857. .ndo_open = bond_open,
  3858. .ndo_stop = bond_close,
  3859. .ndo_start_xmit = bond_start_xmit,
  3860. .ndo_get_stats = bond_get_stats,
  3861. .ndo_do_ioctl = bond_do_ioctl,
  3862. .ndo_set_multicast_list = bond_set_multicast_list,
  3863. .ndo_change_mtu = bond_change_mtu,
  3864. .ndo_set_mac_address = bond_set_mac_address,
  3865. .ndo_neigh_setup = bond_neigh_setup,
  3866. .ndo_vlan_rx_register = bond_vlan_rx_register,
  3867. .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
  3868. .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
  3869. };
  3870. /*
  3871. * Does not allocate but creates a /proc entry.
  3872. * Allowed to fail.
  3873. */
  3874. static int bond_init(struct net_device *bond_dev, struct bond_params *params)
  3875. {
  3876. struct bonding *bond = netdev_priv(bond_dev);
  3877. pr_debug("Begin bond_init for %s\n", bond_dev->name);
  3878. /* initialize rwlocks */
  3879. rwlock_init(&bond->lock);
  3880. rwlock_init(&bond->curr_slave_lock);
  3881. bond->params = *params; /* copy params struct */
  3882. bond->wq = create_singlethread_workqueue(bond_dev->name);
  3883. if (!bond->wq)
  3884. return -ENOMEM;
  3885. /* Initialize pointers */
  3886. bond->first_slave = NULL;
  3887. bond->curr_active_slave = NULL;
  3888. bond->current_arp_slave = NULL;
  3889. bond->primary_slave = NULL;
  3890. bond->dev = bond_dev;
  3891. bond->send_grat_arp = 0;
  3892. bond->send_unsol_na = 0;
  3893. bond->setup_by_slave = 0;
  3894. INIT_LIST_HEAD(&bond->vlan_list);
  3895. /* Initialize the device entry points */
  3896. bond_dev->netdev_ops = &bond_netdev_ops;
  3897. bond_dev->ethtool_ops = &bond_ethtool_ops;
  3898. bond_set_mode_ops(bond, bond->params.mode);
  3899. bond_dev->destructor = bond_destructor;
  3900. /* Initialize the device options */
  3901. bond_dev->tx_queue_len = 0;
  3902. bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
  3903. bond_dev->priv_flags |= IFF_BONDING;
  3904. if (bond->params.arp_interval)
  3905. bond_dev->priv_flags |= IFF_MASTER_ARPMON;
  3906. /* At first, we block adding VLANs. That's the only way to
  3907. * prevent problems that occur when adding VLANs over an
  3908. * empty bond. The block will be removed once non-challenged
  3909. * slaves are enslaved.
  3910. */
  3911. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  3912. /* don't acquire bond device's netif_tx_lock when
  3913. * transmitting */
  3914. bond_dev->features |= NETIF_F_LLTX;
  3915. /* By default, we declare the bond to be fully
  3916. * VLAN hardware accelerated capable. Special
  3917. * care is taken in the various xmit functions
  3918. * when there are slaves that are not hw accel
  3919. * capable
  3920. */
  3921. bond_dev->features |= (NETIF_F_HW_VLAN_TX |
  3922. NETIF_F_HW_VLAN_RX |
  3923. NETIF_F_HW_VLAN_FILTER);
  3924. #ifdef CONFIG_PROC_FS
  3925. bond_create_proc_entry(bond);
  3926. #endif
  3927. list_add_tail(&bond->bond_list, &bond_dev_list);
  3928. return 0;
  3929. }
  3930. static void bond_work_cancel_all(struct bonding *bond)
  3931. {
  3932. write_lock_bh(&bond->lock);
  3933. bond->kill_timers = 1;
  3934. write_unlock_bh(&bond->lock);
  3935. if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
  3936. cancel_delayed_work(&bond->mii_work);
  3937. if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
  3938. cancel_delayed_work(&bond->arp_work);
  3939. if (bond->params.mode == BOND_MODE_ALB &&
  3940. delayed_work_pending(&bond->alb_work))
  3941. cancel_delayed_work(&bond->alb_work);
  3942. if (bond->params.mode == BOND_MODE_8023AD &&
  3943. delayed_work_pending(&bond->ad_work))
  3944. cancel_delayed_work(&bond->ad_work);
  3945. }
  3946. /* De-initialize device specific data.
  3947. * Caller must hold rtnl_lock.
  3948. */
  3949. static void bond_deinit(struct net_device *bond_dev)
  3950. {
  3951. struct bonding *bond = netdev_priv(bond_dev);
  3952. list_del(&bond->bond_list);
  3953. bond_work_cancel_all(bond);
  3954. #ifdef CONFIG_PROC_FS
  3955. bond_remove_proc_entry(bond);
  3956. #endif
  3957. }
  3958. /* Unregister and free all bond devices.
  3959. * Caller must hold rtnl_lock.
  3960. */
  3961. static void bond_free_all(void)
  3962. {
  3963. struct bonding *bond, *nxt;
  3964. list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
  3965. struct net_device *bond_dev = bond->dev;
  3966. bond_work_cancel_all(bond);
  3967. /* Release the bonded slaves */
  3968. bond_release_all(bond_dev);
  3969. bond_destroy(bond);
  3970. }
  3971. #ifdef CONFIG_PROC_FS
  3972. bond_destroy_proc_dir();
  3973. #endif
  3974. }
  3975. /*------------------------- Module initialization ---------------------------*/
  3976. /*
  3977. * Convert string input module parms. Accept either the
  3978. * number of the mode or its string name. A bit complicated because
  3979. * some mode names are substrings of other names, and calls from sysfs
  3980. * may have whitespace in the name (trailing newlines, for example).
  3981. */
  3982. int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
  3983. {
  3984. int modeint = -1, i, rv;
  3985. char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
  3986. for (p = (char *)buf; *p; p++)
  3987. if (!(isdigit(*p) || isspace(*p)))
  3988. break;
  3989. if (*p)
  3990. rv = sscanf(buf, "%20s", modestr);
  3991. else
  3992. rv = sscanf(buf, "%d", &modeint);
  3993. if (!rv)
  3994. return -1;
  3995. for (i = 0; tbl[i].modename; i++) {
  3996. if (modeint == tbl[i].mode)
  3997. return tbl[i].mode;
  3998. if (strcmp(modestr, tbl[i].modename) == 0)
  3999. return tbl[i].mode;
  4000. }
  4001. return -1;
  4002. }
  4003. static int bond_check_params(struct bond_params *params)
  4004. {
  4005. int arp_validate_value, fail_over_mac_value;
  4006. /*
  4007. * Convert string parameters.
  4008. */
  4009. if (mode) {
  4010. bond_mode = bond_parse_parm(mode, bond_mode_tbl);
  4011. if (bond_mode == -1) {
  4012. printk(KERN_ERR DRV_NAME
  4013. ": Error: Invalid bonding mode \"%s\"\n",
  4014. mode == NULL ? "NULL" : mode);
  4015. return -EINVAL;
  4016. }
  4017. }
  4018. if (xmit_hash_policy) {
  4019. if ((bond_mode != BOND_MODE_XOR) &&
  4020. (bond_mode != BOND_MODE_8023AD)) {
  4021. printk(KERN_INFO DRV_NAME
  4022. ": xor_mode param is irrelevant in mode %s\n",
  4023. bond_mode_name(bond_mode));
  4024. } else {
  4025. xmit_hashtype = bond_parse_parm(xmit_hash_policy,
  4026. xmit_hashtype_tbl);
  4027. if (xmit_hashtype == -1) {
  4028. printk(KERN_ERR DRV_NAME
  4029. ": Error: Invalid xmit_hash_policy \"%s\"\n",
  4030. xmit_hash_policy == NULL ? "NULL" :
  4031. xmit_hash_policy);
  4032. return -EINVAL;
  4033. }
  4034. }
  4035. }
  4036. if (lacp_rate) {
  4037. if (bond_mode != BOND_MODE_8023AD) {
  4038. printk(KERN_INFO DRV_NAME
  4039. ": lacp_rate param is irrelevant in mode %s\n",
  4040. bond_mode_name(bond_mode));
  4041. } else {
  4042. lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
  4043. if (lacp_fast == -1) {
  4044. printk(KERN_ERR DRV_NAME
  4045. ": Error: Invalid lacp rate \"%s\"\n",
  4046. lacp_rate == NULL ? "NULL" : lacp_rate);
  4047. return -EINVAL;
  4048. }
  4049. }
  4050. }
  4051. if (ad_select) {
  4052. params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
  4053. if (params->ad_select == -1) {
  4054. printk(KERN_ERR DRV_NAME
  4055. ": Error: Invalid ad_select \"%s\"\n",
  4056. ad_select == NULL ? "NULL" : ad_select);
  4057. return -EINVAL;
  4058. }
  4059. if (bond_mode != BOND_MODE_8023AD) {
  4060. printk(KERN_WARNING DRV_NAME
  4061. ": ad_select param only affects 802.3ad mode\n");
  4062. }
  4063. } else {
  4064. params->ad_select = BOND_AD_STABLE;
  4065. }
  4066. if (max_bonds < 0 || max_bonds > INT_MAX) {
  4067. printk(KERN_WARNING DRV_NAME
  4068. ": Warning: max_bonds (%d) not in range %d-%d, so it "
  4069. "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
  4070. max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
  4071. max_bonds = BOND_DEFAULT_MAX_BONDS;
  4072. }
  4073. if (miimon < 0) {
  4074. printk(KERN_WARNING DRV_NAME
  4075. ": Warning: miimon module parameter (%d), "
  4076. "not in range 0-%d, so it was reset to %d\n",
  4077. miimon, INT_MAX, BOND_LINK_MON_INTERV);
  4078. miimon = BOND_LINK_MON_INTERV;
  4079. }
  4080. if (updelay < 0) {
  4081. printk(KERN_WARNING DRV_NAME
  4082. ": Warning: updelay module parameter (%d), "
  4083. "not in range 0-%d, so it was reset to 0\n",
  4084. updelay, INT_MAX);
  4085. updelay = 0;
  4086. }
  4087. if (downdelay < 0) {
  4088. printk(KERN_WARNING DRV_NAME
  4089. ": Warning: downdelay module parameter (%d), "
  4090. "not in range 0-%d, so it was reset to 0\n",
  4091. downdelay, INT_MAX);
  4092. downdelay = 0;
  4093. }
  4094. if ((use_carrier != 0) && (use_carrier != 1)) {
  4095. printk(KERN_WARNING DRV_NAME
  4096. ": Warning: use_carrier module parameter (%d), "
  4097. "not of valid value (0/1), so it was set to 1\n",
  4098. use_carrier);
  4099. use_carrier = 1;
  4100. }
  4101. if (num_grat_arp < 0 || num_grat_arp > 255) {
  4102. printk(KERN_WARNING DRV_NAME
  4103. ": Warning: num_grat_arp (%d) not in range 0-255 so it "
  4104. "was reset to 1 \n", num_grat_arp);
  4105. num_grat_arp = 1;
  4106. }
  4107. if (num_unsol_na < 0 || num_unsol_na > 255) {
  4108. printk(KERN_WARNING DRV_NAME
  4109. ": Warning: num_unsol_na (%d) not in range 0-255 so it "
  4110. "was reset to 1 \n", num_unsol_na);
  4111. num_unsol_na = 1;
  4112. }
  4113. /* reset values for 802.3ad */
  4114. if (bond_mode == BOND_MODE_8023AD) {
  4115. if (!miimon) {
  4116. printk(KERN_WARNING DRV_NAME
  4117. ": Warning: miimon must be specified, "
  4118. "otherwise bonding will not detect link "
  4119. "failure, speed and duplex which are "
  4120. "essential for 802.3ad operation\n");
  4121. printk(KERN_WARNING "Forcing miimon to 100msec\n");
  4122. miimon = 100;
  4123. }
  4124. }
  4125. /* reset values for TLB/ALB */
  4126. if ((bond_mode == BOND_MODE_TLB) ||
  4127. (bond_mode == BOND_MODE_ALB)) {
  4128. if (!miimon) {
  4129. printk(KERN_WARNING DRV_NAME
  4130. ": Warning: miimon must be specified, "
  4131. "otherwise bonding will not detect link "
  4132. "failure and link speed which are essential "
  4133. "for TLB/ALB load balancing\n");
  4134. printk(KERN_WARNING "Forcing miimon to 100msec\n");
  4135. miimon = 100;
  4136. }
  4137. }
  4138. if (bond_mode == BOND_MODE_ALB) {
  4139. printk(KERN_NOTICE DRV_NAME
  4140. ": In ALB mode you might experience client "
  4141. "disconnections upon reconnection of a link if the "
  4142. "bonding module updelay parameter (%d msec) is "
  4143. "incompatible with the forwarding delay time of the "
  4144. "switch\n",
  4145. updelay);
  4146. }
  4147. if (!miimon) {
  4148. if (updelay || downdelay) {
  4149. /* just warn the user the up/down delay will have
  4150. * no effect since miimon is zero...
  4151. */
  4152. printk(KERN_WARNING DRV_NAME
  4153. ": Warning: miimon module parameter not set "
  4154. "and updelay (%d) or downdelay (%d) module "
  4155. "parameter is set; updelay and downdelay have "
  4156. "no effect unless miimon is set\n",
  4157. updelay, downdelay);
  4158. }
  4159. } else {
  4160. /* don't allow arp monitoring */
  4161. if (arp_interval) {
  4162. printk(KERN_WARNING DRV_NAME
  4163. ": Warning: miimon (%d) and arp_interval (%d) "
  4164. "can't be used simultaneously, disabling ARP "
  4165. "monitoring\n",
  4166. miimon, arp_interval);
  4167. arp_interval = 0;
  4168. }
  4169. if ((updelay % miimon) != 0) {
  4170. printk(KERN_WARNING DRV_NAME
  4171. ": Warning: updelay (%d) is not a multiple "
  4172. "of miimon (%d), updelay rounded to %d ms\n",
  4173. updelay, miimon, (updelay / miimon) * miimon);
  4174. }
  4175. updelay /= miimon;
  4176. if ((downdelay % miimon) != 0) {
  4177. printk(KERN_WARNING DRV_NAME
  4178. ": Warning: downdelay (%d) is not a multiple "
  4179. "of miimon (%d), downdelay rounded to %d ms\n",
  4180. downdelay, miimon,
  4181. (downdelay / miimon) * miimon);
  4182. }
  4183. downdelay /= miimon;
  4184. }
  4185. if (arp_interval < 0) {
  4186. printk(KERN_WARNING DRV_NAME
  4187. ": Warning: arp_interval module parameter (%d) "
  4188. ", not in range 0-%d, so it was reset to %d\n",
  4189. arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
  4190. arp_interval = BOND_LINK_ARP_INTERV;
  4191. }
  4192. for (arp_ip_count = 0;
  4193. (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
  4194. arp_ip_count++) {
  4195. /* not complete check, but should be good enough to
  4196. catch mistakes */
  4197. if (!isdigit(arp_ip_target[arp_ip_count][0])) {
  4198. printk(KERN_WARNING DRV_NAME
  4199. ": Warning: bad arp_ip_target module parameter "
  4200. "(%s), ARP monitoring will not be performed\n",
  4201. arp_ip_target[arp_ip_count]);
  4202. arp_interval = 0;
  4203. } else {
  4204. __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
  4205. arp_target[arp_ip_count] = ip;
  4206. }
  4207. }
  4208. if (arp_interval && !arp_ip_count) {
  4209. /* don't allow arping if no arp_ip_target given... */
  4210. printk(KERN_WARNING DRV_NAME
  4211. ": Warning: arp_interval module parameter (%d) "
  4212. "specified without providing an arp_ip_target "
  4213. "parameter, arp_interval was reset to 0\n",
  4214. arp_interval);
  4215. arp_interval = 0;
  4216. }
  4217. if (arp_validate) {
  4218. if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
  4219. printk(KERN_ERR DRV_NAME
  4220. ": arp_validate only supported in active-backup mode\n");
  4221. return -EINVAL;
  4222. }
  4223. if (!arp_interval) {
  4224. printk(KERN_ERR DRV_NAME
  4225. ": arp_validate requires arp_interval\n");
  4226. return -EINVAL;
  4227. }
  4228. arp_validate_value = bond_parse_parm(arp_validate,
  4229. arp_validate_tbl);
  4230. if (arp_validate_value == -1) {
  4231. printk(KERN_ERR DRV_NAME
  4232. ": Error: invalid arp_validate \"%s\"\n",
  4233. arp_validate == NULL ? "NULL" : arp_validate);
  4234. return -EINVAL;
  4235. }
  4236. } else
  4237. arp_validate_value = 0;
  4238. if (miimon) {
  4239. printk(KERN_INFO DRV_NAME
  4240. ": MII link monitoring set to %d ms\n",
  4241. miimon);
  4242. } else if (arp_interval) {
  4243. int i;
  4244. printk(KERN_INFO DRV_NAME
  4245. ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
  4246. arp_interval,
  4247. arp_validate_tbl[arp_validate_value].modename,
  4248. arp_ip_count);
  4249. for (i = 0; i < arp_ip_count; i++)
  4250. printk (" %s", arp_ip_target[i]);
  4251. printk("\n");
  4252. } else if (max_bonds) {
  4253. /* miimon and arp_interval not set, we need one so things
  4254. * work as expected, see bonding.txt for details
  4255. */
  4256. printk(KERN_WARNING DRV_NAME
  4257. ": Warning: either miimon or arp_interval and "
  4258. "arp_ip_target module parameters must be specified, "
  4259. "otherwise bonding will not detect link failures! see "
  4260. "bonding.txt for details.\n");
  4261. }
  4262. if (primary && !USES_PRIMARY(bond_mode)) {
  4263. /* currently, using a primary only makes sense
  4264. * in active backup, TLB or ALB modes
  4265. */
  4266. printk(KERN_WARNING DRV_NAME
  4267. ": Warning: %s primary device specified but has no "
  4268. "effect in %s mode\n",
  4269. primary, bond_mode_name(bond_mode));
  4270. primary = NULL;
  4271. }
  4272. if (fail_over_mac) {
  4273. fail_over_mac_value = bond_parse_parm(fail_over_mac,
  4274. fail_over_mac_tbl);
  4275. if (fail_over_mac_value == -1) {
  4276. printk(KERN_ERR DRV_NAME
  4277. ": Error: invalid fail_over_mac \"%s\"\n",
  4278. arp_validate == NULL ? "NULL" : arp_validate);
  4279. return -EINVAL;
  4280. }
  4281. if (bond_mode != BOND_MODE_ACTIVEBACKUP)
  4282. printk(KERN_WARNING DRV_NAME
  4283. ": Warning: fail_over_mac only affects "
  4284. "active-backup mode.\n");
  4285. } else {
  4286. fail_over_mac_value = BOND_FOM_NONE;
  4287. }
  4288. /* fill params struct with the proper values */
  4289. params->mode = bond_mode;
  4290. params->xmit_policy = xmit_hashtype;
  4291. params->miimon = miimon;
  4292. params->num_grat_arp = num_grat_arp;
  4293. params->num_unsol_na = num_unsol_na;
  4294. params->arp_interval = arp_interval;
  4295. params->arp_validate = arp_validate_value;
  4296. params->updelay = updelay;
  4297. params->downdelay = downdelay;
  4298. params->use_carrier = use_carrier;
  4299. params->lacp_fast = lacp_fast;
  4300. params->primary[0] = 0;
  4301. params->fail_over_mac = fail_over_mac_value;
  4302. if (primary) {
  4303. strncpy(params->primary, primary, IFNAMSIZ);
  4304. params->primary[IFNAMSIZ - 1] = 0;
  4305. }
  4306. memcpy(params->arp_targets, arp_target, sizeof(arp_target));
  4307. return 0;
  4308. }
  4309. static struct lock_class_key bonding_netdev_xmit_lock_key;
  4310. static struct lock_class_key bonding_netdev_addr_lock_key;
  4311. static void bond_set_lockdep_class_one(struct net_device *dev,
  4312. struct netdev_queue *txq,
  4313. void *_unused)
  4314. {
  4315. lockdep_set_class(&txq->_xmit_lock,
  4316. &bonding_netdev_xmit_lock_key);
  4317. }
  4318. static void bond_set_lockdep_class(struct net_device *dev)
  4319. {
  4320. lockdep_set_class(&dev->addr_list_lock,
  4321. &bonding_netdev_addr_lock_key);
  4322. netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
  4323. }
  4324. /* Create a new bond based on the specified name and bonding parameters.
  4325. * If name is NULL, obtain a suitable "bond%d" name for us.
  4326. * Caller must NOT hold rtnl_lock; we need to release it here before we
  4327. * set up our sysfs entries.
  4328. */
  4329. int bond_create(char *name, struct bond_params *params)
  4330. {
  4331. struct net_device *bond_dev;
  4332. struct bonding *bond;
  4333. int res;
  4334. rtnl_lock();
  4335. down_write(&bonding_rwsem);
  4336. /* Check to see if the bond already exists. */
  4337. if (name) {
  4338. list_for_each_entry(bond, &bond_dev_list, bond_list)
  4339. if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
  4340. printk(KERN_ERR DRV_NAME
  4341. ": cannot add bond %s; it already exists\n",
  4342. name);
  4343. res = -EPERM;
  4344. goto out_rtnl;
  4345. }
  4346. }
  4347. bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
  4348. ether_setup);
  4349. if (!bond_dev) {
  4350. printk(KERN_ERR DRV_NAME
  4351. ": %s: eek! can't alloc netdev!\n",
  4352. name);
  4353. res = -ENOMEM;
  4354. goto out_rtnl;
  4355. }
  4356. if (!name) {
  4357. res = dev_alloc_name(bond_dev, "bond%d");
  4358. if (res < 0)
  4359. goto out_netdev;
  4360. }
  4361. /* bond_init() must be called after dev_alloc_name() (for the
  4362. * /proc files), but before register_netdevice(), because we
  4363. * need to set function pointers.
  4364. */
  4365. res = bond_init(bond_dev, params);
  4366. if (res < 0) {
  4367. goto out_netdev;
  4368. }
  4369. res = register_netdevice(bond_dev);
  4370. if (res < 0) {
  4371. goto out_bond;
  4372. }
  4373. bond_set_lockdep_class(bond_dev);
  4374. netif_carrier_off(bond_dev);
  4375. up_write(&bonding_rwsem);
  4376. rtnl_unlock(); /* allows sysfs registration of net device */
  4377. res = bond_create_sysfs_entry(netdev_priv(bond_dev));
  4378. if (res < 0) {
  4379. rtnl_lock();
  4380. down_write(&bonding_rwsem);
  4381. bond_deinit(bond_dev);
  4382. unregister_netdevice(bond_dev);
  4383. goto out_rtnl;
  4384. }
  4385. return 0;
  4386. out_bond:
  4387. bond_deinit(bond_dev);
  4388. out_netdev:
  4389. free_netdev(bond_dev);
  4390. out_rtnl:
  4391. up_write(&bonding_rwsem);
  4392. rtnl_unlock();
  4393. return res;
  4394. }
  4395. static int __init bonding_init(void)
  4396. {
  4397. int i;
  4398. int res;
  4399. struct bonding *bond;
  4400. printk(KERN_INFO "%s", version);
  4401. res = bond_check_params(&bonding_defaults);
  4402. if (res) {
  4403. goto out;
  4404. }
  4405. #ifdef CONFIG_PROC_FS
  4406. bond_create_proc_dir();
  4407. #endif
  4408. init_rwsem(&bonding_rwsem);
  4409. for (i = 0; i < max_bonds; i++) {
  4410. res = bond_create(NULL, &bonding_defaults);
  4411. if (res)
  4412. goto err;
  4413. }
  4414. res = bond_create_sysfs();
  4415. if (res)
  4416. goto err;
  4417. register_netdevice_notifier(&bond_netdev_notifier);
  4418. register_inetaddr_notifier(&bond_inetaddr_notifier);
  4419. bond_register_ipv6_notifier();
  4420. goto out;
  4421. err:
  4422. list_for_each_entry(bond, &bond_dev_list, bond_list) {
  4423. bond_work_cancel_all(bond);
  4424. destroy_workqueue(bond->wq);
  4425. }
  4426. bond_destroy_sysfs();
  4427. rtnl_lock();
  4428. bond_free_all();
  4429. rtnl_unlock();
  4430. out:
  4431. return res;
  4432. }
  4433. static void __exit bonding_exit(void)
  4434. {
  4435. unregister_netdevice_notifier(&bond_netdev_notifier);
  4436. unregister_inetaddr_notifier(&bond_inetaddr_notifier);
  4437. bond_unregister_ipv6_notifier();
  4438. bond_destroy_sysfs();
  4439. rtnl_lock();
  4440. bond_free_all();
  4441. rtnl_unlock();
  4442. }
  4443. module_init(bonding_init);
  4444. module_exit(bonding_exit);
  4445. MODULE_LICENSE("GPL");
  4446. MODULE_VERSION(DRV_VERSION);
  4447. MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
  4448. MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
  4449. MODULE_SUPPORTED_DEVICE("most ethernet devices");
  4450. /*
  4451. * Local variables:
  4452. * c-indent-level: 8
  4453. * c-basic-offset: 8
  4454. * tab-width: 8
  4455. * End:
  4456. */