bond_main.c 135 KB

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