bond_main.c 135 KB

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