ospf的DR以及BDR選舉

最近翻了一下網(wǎng)上關(guān)于DR以及BDR選舉的文章,發(fā)現(xiàn)很多文章的描述都存在問題,而且很多東西都沒有描述清楚,所以,我自己打算寫一篇文章,將事情徹底講清楚.

參考

最權(quán)威的參考資料還是RFC文檔,網(wǎng)上大部分文章都不靠譜,甚至TCP/IP路由卷一,都沒有講清楚.有興趣的同學(xué)可以翻一下RFC2328,我這里干脆引用一下得了.

This section describes the algorithm used for calculating a network’s Designated Router and Backup Designated Router. This algorithm is invoked by the Interface state machine. The initial time a router runs the election algorithm for a network,
the network’s Designated Router and Backup Designated Router are initialized to 0.0.0.0. This indicates the lack of both a Designated Router and a Backup Designated Router.
The Designated Router election algorithm proceeds as follows:
Call the router doing the calculation Router X. The list of neighbors attached to the network and having established bidirectional communication with Router X is examined. This list is precisely the collection of Router X’s neighbors (on this network) whose state is greater than or equal to 2-Way . Router X itself is also considered to be on the list. Discard all routers from the list that are ineligible to
become Designated Router. (Routers having Router Priority of 0 are ineligible to become Designated Router.) The following steps are then executed, considering only those routers that remain on the list:
(1) Note the current values for the network’s Designated Router
and Backup Designated Router. This is used later for comparison purposes.
(2) Calculate the new Backup Designated Router for the network as follows. Only those routers on the list that have not declared themselves to be Designated Router are eligible to become Backup Designated Router. If one or more of these routers have declared themselves Backup Designated Router (i.e., they are currently listing themselves as Backup Designated Router, but not as Designated Router, in their
Hello Packets) the one having highest Router Priority is declared to be Backup Designated Router. In case of a tie, the one having the highest Router ID is chosen. If no routers have declared themselves Backup Designated Router, Moy Standards Track [Page 75]RFC 2328 OSPF Version 2 April 1998 choose the router having highest Router Priority, (again excluding those routers who have declared themselves Designated Router), and again use the Router ID to break ties.
(3) Calculate the new Designated Router for the network as follows. If one or more of the routers have declared themselves Designated Router (i.e., they are currently
listing themselves as Designated Router in their Hello Packets) the one having highest Router Priority is declared to be Designated Router. In case of a tie, the one having the highest Router ID is chosen. If no routers have declared themselves Designated Router, assign the Designated Router to be the same as the newly elected Backup Designated Router.
(4) If Router X is now newly the Designated Router or newly the Backup Designated Router, or is now no longer the Designated Router or no longer the Backup Designated Router, repeat steps 2 and 3, and then proceed to step 5. For example, if Router X is now the Designated Router, when step 2 is repeated X will no longer be eligible for Backup Designated Router election. Among other things, this will ensure that no router will declare itself both Backup Designated Router
and Designated Router.
(5) As a result of these calculations, the router itself may now be Designated Router or Backup Designated Router. See Sections 7.3 and 7.4 for the additional duties this would entail. The router’s interface state should be set accordingly. If the router itself is now Designated Router, the new interface state is DR. If the router itself is now Backup Designated Router, the new interface state is Backup.
Otherwise, the new interface state is DR Other.
(6) If the attached network is an NBMA network, and the router itself has just become either Designated Router or Backup Designated Router, it must start sending Hello Packets to those neighbors that are not eligible to become Designated Router (see Section 9.5.1). This is done by invoking the neighbor event Start for each neighbor having a Router Priority of 0.
(7) If the above calculations have caused the identity of either the Designated Router or Backup Designated Router to change,the set of adjacencies associated with this interface will need to be modified. Some adjacencies may need to be
formed, and others may need to be broken. To accomplish this, invoke the event AdjOK? on all neighbors whose state is at least 2-Way. This will cause their eligibility for adjacency to be reexamined (see Sections 10.3 and 10.4).
The reason behind the election algorithm’s complexity is the desire for an orderly transition from Backup Designated Router to Designated Router, when the current Designated Router fails.
This orderly transition is ensured through the introduction of hysteresis: no new Backup Designated Router can be chosen until the old Backup accepts its new Designated Router responsibilities.
The above procedure may elect the same router to be both Designated Router and Backup Designated Router, although that router will never be the calculating router (Router X) itself.
The elected Designated Router may not be the router having the highest Router Priority, nor will the Backup Designated Router necessarily have the second highest Router Priority. If Router X is not itself eligible to become Designated Router, it is
possible that neither a Backup Designated Router nor a Designated Router will be selected in the above procedure. Note also that if Router X is the only attached router that is eligible to become Designated Router, it will select itself as Designated Router and there will be no Backup Designated Router for the network.

算法

以下算法基本是上面內(nèi)容的翻譯.
一臺(tái)ospf接口起來的時(shí)候,它默認(rèn)的dr以及bdr都是0.0.0.0,這意味著,這個(gè)時(shí)候,此接口認(rèn)為還網(wǎng)絡(luò)中不存在DR以及BDR.

接下來開始DR的選舉算法,假定我們有一臺(tái)路由器X,它正在運(yùn)行DR選舉算法,它首先會(huì)開始檢查同一網(wǎng)段上,和X建立了雙向連接關(guān)系的鄰居(鄰居狀態(tài)大于等于two-way),當(dāng)然,X自己也在這些鄰居之中,去掉那些優(yōu)先級(jí)為0的鄰居(優(yōu)先級(jí)為0,表示其并不想?yún)⑴c選舉),去掉那些Router ID為0的鄰居,將其余的鄰居加入候選列表.

(1) 先將當(dāng)前路由器認(rèn)為的DR以及BDR的值記錄下來,這個(gè)將由于后面的比較;

(2) 首先選舉新的BDR,首先選擇BDR的候選者,那些宣稱自己為DR的鄰居不能作為BDR,如果有1個(gè)或者多個(gè)候選候選鄰居宣稱自己為BDR(沒有宣稱自己為DR),那么它們之中,優(yōu)先級(jí)最高的鄰居作為BDR,如果優(yōu)先級(jí)一致,Router ID大的將成為BDR.如果沒有鄰居宣稱自己為BDR, 那就挑選一個(gè)擁有最高優(yōu)先級(jí)的作為BDR,優(yōu)先級(jí)一致的話,router-id大的成為BDR(同樣的,要排除那些宣稱自己為DR的鄰居);

(3) 接下來選舉DR,流程如下,如果有一個(gè)或多個(gè)候選鄰居宣稱自己為DR(也就是說,Hello包中,它們將自己的Router ID填入了DR字段),擁有最高優(yōu)先級(jí)的鄰居成為DR,如果優(yōu)先級(jí)一致,則比較Router ID,Router ID大的鄰居成為DR,如果沒有鄰居宣稱自己為DR,那么前面選舉的BDR成為DR;

(4) 使用(1)中保存的值,如果X成為了新的DR/BDR(之前不是),或者現(xiàn)在已經(jīng)不是DR/BDR(原來是),重復(fù)算法(2),(3)步,然后執(zhí)行第(5)步.為什么要這么做呢?很簡單,經(jīng)歷(2),(3)之后,X現(xiàn)在可能即是DR也是BDR,重新執(zhí)行之后,我們可以保證,X只有一個(gè)角色;

(5) ... (接下來就不是選舉算法部分了,省略)

問題

為什么DR以及BDR一般只要不掛,就不會(huì)改變?

假定一個(gè)局域網(wǎng),已經(jīng)存在了一個(gè)DR以及一個(gè)BDR,現(xiàn)在另外一臺(tái)優(yōu)先級(jí)更高的路由器R3加入:

R1[DR]-------R2[BDR]
          |
         R3

需要說明一下,真實(shí)環(huán)境收包發(fā)包時(shí)序可能并非如此,這里僅僅只是為了演示而已.

  1. 首先,R3剛起來,認(rèn)為DR以及BDR為0,開始發(fā)送Hello包,DR為0,BDR為0;
  2. 接下來R2接收到了R1的Hello包,里面DR為R1, BDR為R2, 以及受到了R2發(fā)來的Hello包,里面DR為R1, BDR為R2;
  3. R2開始運(yùn)行選舉算法,先選舉BDR, BDR只有R2一個(gè)候選者,R2成為BDR;
  4. 開始選舉DR,DR只有R1這一個(gè)候選者,因此,R1為DR.

繼續(xù)上面的拓?fù)?如果R1掛掉了呢?

R1[DR]-X------R2[BDR]
          |
         R3

等待一段時(shí)間,R2和R3沒有收到R1的Hello報(bào)文,就會(huì)將R1從鄰居列表中移除.然后重新開始選舉.假定這個(gè)時(shí)候R3將R1移除,但是還沒有收到R2的Hello報(bào)文.

  1. 首先選舉BDR,因?yàn)镽2宣稱自己為BDR,選舉R2為BDR;
  2. 開始選舉DR,因?yàn)闆]有鄰居宣稱自己為DR,那么R2成為DR;
  3. 因?yàn)镽3一直都是DROther,因此就不執(zhí)行算法步驟(2)和(3);

R2同樣也會(huì)執(zhí)行選舉:

  1. 首先選舉BDR,因?yàn)镽2宣稱自己為BDR,選舉R2為BDR;
  2. 開始選舉DR,因?yàn)闆]有鄰居宣稱自己為DR,那么R2成為DR;
  3. R2成為了DR,重新執(zhí)行算法步驟(2)和(3);
  4. 首先,因?yàn)镽2成為了DR,它不會(huì)再成為BDR,因此BDR為R3;
  5. R2經(jīng)過DR選舉仍然是DR.

接著R2和R3會(huì)互相洪泛Hello報(bào)文,R3收到R2的Hello報(bào)文后,會(huì)再次進(jìn)行選舉:

  1. 首先選舉BDR,因?yàn)镽2為DR,因此R3成為了BDR;
  2. R2仍然是DR;
  3. 因?yàn)镽3新成為了BDR,重新執(zhí)行算法步驟(2)和(3);
  4. 選舉BDR,R3為BDR;
  5. 選舉DR,R2為DR;
    最終R2和R3會(huì)達(dá)成一致.

什么時(shí)候會(huì)進(jìn)行選舉過程?

1.新增two-way及以上的鄰居;
2.two-way及以上的鄰居減少;
3.two-way及以上的鄰居宣稱自己為DR/BDR (原來不是);
4.two-way及以上的鄰居不再宣稱自己為DR/BDR (原來是);
5.two-way及以上的鄰居的優(yōu)先級(jí)發(fā)生變化.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末视事,一起剝皮案震驚了整個(gè)濱河市软舌,隨后出現(xiàn)的幾起案子控漠,更是在濱河造成了極大的恐慌绍撞,老刑警劉巖罐旗,帶你破解...
    沈念sama閱讀 206,126評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)翅溺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來髓抑,“玉大人咙崎,你說我怎么就攤上這事《峙模” “怎么了褪猛?”我有些...
    開封第一講書人閱讀 152,445評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長羹饰。 經(jīng)常有香客問我伊滋,道長,這世上最難降的妖魔是什么队秩? 我笑而不...
    開封第一講書人閱讀 55,185評(píng)論 1 278
  • 正文 為了忘掉前任笑旺,我火速辦了婚禮,結(jié)果婚禮上刹碾,老公的妹妹穿的比我還像新娘燥撞。我一直安慰自己,他們只是感情好迷帜,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評(píng)論 5 371
  • 文/花漫 我一把揭開白布物舒。 她就那樣靜靜地躺著,像睡著了一般戏锹。 火紅的嫁衣襯著肌膚如雪冠胯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評(píng)論 1 284
  • 那天锦针,我揣著相機(jī)與錄音荠察,去河邊找鬼。 笑死奈搜,一個(gè)胖子當(dāng)著我的面吹牛悉盆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播馋吗,決...
    沈念sama閱讀 38,276評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼焕盟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了宏粤?” 一聲冷哼從身側(cè)響起脚翘,我...
    開封第一講書人閱讀 36,927評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎绍哎,沒想到半個(gè)月后来农,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡崇堰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評(píng)論 2 323
  • 正文 我和宋清朗相戀三年沃于,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片海诲。...
    茶點(diǎn)故事閱讀 37,997評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡繁莹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出饿肺,到底是詐尸還是另有隱情蒋困,我是刑警寧澤,帶...
    沈念sama閱讀 33,646評(píng)論 4 322
  • 正文 年R本政府宣布敬辣,位于F島的核電站雪标,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏溉跃。R本人自食惡果不足惜村刨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望撰茎。 院中可真熱鬧嵌牺,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至僻弹,卻和暖如春阿浓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蹋绽。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評(píng)論 1 260
  • 我被黑心中介騙來泰國打工芭毙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人卸耘。 一個(gè)月前我還...
    沈念sama閱讀 45,423評(píng)論 2 352
  • 正文 我出身青樓退敦,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蚣抗。 傳聞我的和親對(duì)象是個(gè)殘疾皇子侈百,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • 開放式最短路徑優(yōu)先 wiki Open Shortest Path First,縮寫為 OSPF)是對(duì)鏈路狀態(tài)路由...
    LiamBao閱讀 738評(píng)論 0 1
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,808評(píng)論 0 0
  • ping error: Destination off-link but no route作者 codercjg ...
    codercjg閱讀 1,033評(píng)論 0 1
  • 了解基督的人忠聚,或者信奉基督的人设哗,抑或了解西方文化的人,都應(yīng)該了解所羅門王两蟀。即使對(duì)基督毫無興趣网梢,或者對(duì)西方文化也不甚...
    有孚颙若閱讀 1,383評(píng)論 5 6
  • 今年五月份,在操辦完妹妹的婚事后赂毯,母親隨我來了楊凌战虏。在此之前,母親來過一兩次党涕,但那時(shí)候我還沒有成家烦感。這次特意把母親...
    花裊閱讀 691評(píng)論 0 8