Python中的网络拓扑表示
这是来自 https://github.com/osrg/ryu/blob/master/ryu/topology/switches.py#L429 的 Switches 类的代码。
在 Switches 类中,有几个我特别感兴趣的成员变量。
self.dps = {} # datapath_id => Datapath class
self.port_state = {} # datapath_id => ports
self.ports = PortDataState() # Port class -> PortData class
self.links = LinkState() # Link class -> timestamp
self.is_active = True
这些成员变量是 RYU 用来缓存拓扑信息的。我正在尝试理解如何用以下变量来表示一个拓扑。
1) dps 是一个字典,它把数据路径 ID 映射到数据路径类?
- Can someone explain to me what is a datapath_id and a datapath class?
2) port_state 是一个字典,它把数据路径 ID 映射到端口。
- As per my understanding on a switch without a VLAN all the ports will belong to the same datapath id? In case of a switch with VLAN ports on the switch can have multiple datapath id's. Is my understanding correct?
3) ports 也是一个字典,它把端口类映射到端口数据类?
- what does this mean?
4) links 也是一个字典,它把链接类映射到时间戳。
- again what does this mean?
我正在努力理解 RYU 控制器是如何使用上述结构来存储拓扑信息的。任何帮助或解释都将非常感激。
2 个回答
以下是我对这些回答的看法:
为每个数据路径 ID 保存一个数据路径类。比如在 switches.py 的第 475 行:
self.dps[dp.id] = dp
这样一来,凭借一个数据路径 ID,就能很方便地找到整个类。PortState
在第 161 行定义为PortState(dict)
,这意味着它是一个字典。这个字典的键是int port_no
,值是OFPPort port
。
所以self.port_state
保存了每个数据路径 ID 的端口信息。比如self.port_state[1]
就会包含数据路径 ID 为 1 的端口。PortDataState
在第 206 行定义为PortDataState(dict)
,这同样是一个字典。它在字典的形式上有点像双向链表。 看起来这更像是一个端口列表,端口作为键,它们的状态作为值。LinkState 用来跟踪链接。 对于
src
和dst
之间的链接,它会创建一个 Link 对象,并保存dst
和src
之间的映射(这是通过 self._map 结构完成的)。此外,对于每个创建的 Link 对象,它还会记录时间(我不知道这样做的原因)。
可以查看 switches.py 的第 317 行。
这里有一个关于 Ryu 中拓扑发现 的好教程。
简单来说,每个OpenFlow交换机都会通过一个特定的端口连接到一个控制器。每个交换机和控制器之间的这种连接被称为一个独立的“数据路径”,通常用DPID(数据路径ID)来表示。
而一个链接就是指两个DPID之间的端口连接。因此,DPID 1 (PORT1) 和 DPID 2 (PORT2)之间的连接,就是SW1的P1和SW2的P1之间的链接。
我使用的是POX控制器,对Ryu了解不多,但我想你现在应该能理解这些内容了。