bond_main.c 134 KB

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