bond_main.c 121 KB

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