Ryu 拓墣探勘程式碼分析
0x00 前言
Ryu 的拓樸主程式為 ryu/topology/switches.py
0x01 Ryu Application 進入點
<html>
<style>
pre { box-shadow: none; }
table, td { border: none; }
</style>
<body>
<script type='text/javascript' src='gistfy-app.herokuapp.com/github/osrg/ryu/ryu/topology/switches.py?branch=master&slice=496:533&lang=python&style=github'></script>
</body>
</html>
在 line:497 OFP_VERSIONS 可以看到這支程式支援的 OpenFlow 版本
line: 499 _EVENTS 則宣告了這個類別會產生的 Event
這些 event 是來自 ryu/topology/event.py, 與 switches.py 在同層目錄,裡頭的每個 Event 類別都繼承了 ryu/controller/event.py 中的 EventBase 類別,在 Ryu 中 Event 類別是可以自訂的
在初始化一些變數後
line: 532 開了一隻執行緒來執行 lldp_loop
line: 533 則開了一隻執行緒來執行 link_loop
這兩個函式我們稍後分析
—-
=====0x02 Switch 進入與離開=====
在 SDN 中,Switch 可以透過 OpenFlow 與 Controller 溝通,在這裡我們檢視 switches.py 中的相關程式碼
<html>
<style>
pre { box-shadow: none; }
table, td { border: none; }
</style>
<body>
<script type='text/javascript' src='gistfy-app.herokuapp.com/github/osrg/ryu/ryu/topology/switches.py?branch=master&slice=599:691&lang=python&style=github'></script>
</body>
</html>
在程式 line: 599 的地方有一個 state_change_handler 函式,它使用了 @set_ev_cls 修飾字表示自己為一個 Event Handler,監聽了 ofp_event.EventOFPStateChange 事件的 MAIN_DISPATCHER 與 DEAD_DISPATCHER 兩個階段
這兩個階段分別表示 Switch 與 Controller 做完 OpenFlow 協商,以及 Switch 與 Controller 連線中斷
在 MAIN_DISPATCHER 時
0x0 switches.py 程式碼
switches.py 程式碼大約一千行,我們在後續分段解析
<html>
<style>
pre { box-shadow: none; }
table, td { border: none; }
</style>
<body>
<script type='text/javascript' src='gistfy-app.herokuapp.com/github/osrg/ryu/ryu/topology/switches.py?branch=master&lang=python&style=github'></script>
</body>
</html>
—-