实现目标环境下的静态流表设置:
1 单个ovs上实现多个主机hosts之间的通信
2多ovs上多主机之间的通信
1 单个ovs上实现多个主机hosts之间的通信
使用函数定义的方式创建一个如下的拓扑,并使用静态流表
from mininet.net import Mininet
from mininet.node import OVSSwitch, Host
from mininet.cli import CLI
from mininet.link import Link
import networkx as nx
import matplotlib.pyplot as plt
def create_network():
net = Mininet()
# 创建单个OVS交换机
switch1 = net.addSwitch('s1', cls=OVSSwitch)
# 创建2个主机
host1 = net.addHost('h1', cls=Host, ip='192.168.0.1/24', defaultRoute='via 192.168.0.254')
host2 = net.addHost('h2', cls=Host, ip='192.168.0.2/24', defaultRoute='via 192.168.0.254')
# 连接主机到交换机
net.addLink(host1, switch1)
net.addLink(host2, switch1)
# 启动网络
net.start()
# 在OVS交换机上添加静态流表
switch1.cmd('ovs-ofctl add-flow s1 in_port=1,actions=output:2')
switch1.cmd('ovs-ofctl add-flow s1 in_port=2,actions=output:1')
# 构建拓扑图
G = nx.DiGraph()
for switch in net.switches:
G.add_node(switch.name)
for host in net.hosts:
G.add_node(host.name)
for link in net.links:
G.add_edge(link.intf1.node.name, link.intf2.node.name)
# 保存拓扑图
nx.draw(G, with_labels=True, node_size=1000, node_color='lightblue', font_weight='bold')
plt.savefig('topology1.png')
plt.show() # 显示网络拓扑
# 打开命令行界面
CLI(net)
# 关闭网络
net.stop()
if __name__ == '__main__':
create_network()
运行以及测试``
2多ovs上多主机之间的通信
使用2_3_1.py代码修改成如下图2的拓扑结构
代码:
from mininet.net import Mininet
from mininet.node import OVSSwitch, Host
from mininet.cli import CLI
from mininet.link import Link
import networkx as nx
import matplotlib.pyplot as plt
def create_network():
net = Mininet()
# 创建单个OVS交换机
switch1 = net.addSwitch('s1', cls=OVSSwitch)
switch2 = net.addSwitch('s2', cls=OVSSwitch)
# 创建2个主机
host1 = net.addHost('h1', cls=Host, ip='192.168.0.1/24', defaultRoute='via 192.168.0.254')
host2 = net.addHost('h2', cls=Host, ip='192.168.0.2/24', defaultRoute='via 192.168.0.254')
host3 = net.addHost('h3', cls=Host, ip='192.168.0.3/24', defaultRoute='via 192.168.0.254')
host4 = net.addHost('h4', cls=Host, ip='192.168.0.4/24', defaultRoute='via 192.168.0.254')
# 连接主机到交换机
net.addLink(host1, switch1)
net.addLink(host2, switch1)
net.addLink(host3, switch2)
net.addLink(host4, switch2)
#交换机连接交换机
net.addLink(switch1, switch2)
# 启动网络
net.start()
# 在OVS交换机上添加静态流表
switch1.cmd('ovs-ofctl add-flow s1 in_port=1,actions=output:2,3')
switch1.cmd('ovs-ofctl add-flow s1 in_port=2,actions=output:1,3')
switch1.cmd('ovs-ofctl add-flow s1 in_port=3,actions=output:1,2')
switch2.cmd('ovs-ofctl add-flow s2 in_port=1,actions=output:2,3')
switch2.cmd('ovs-ofctl add-flow s2 in_port=2,actions=output:1,3')
switch2.cmd('ovs-ofctl add-flow s2 in_port=3,actions=output:1,2')
# 构建拓扑图
G = nx.DiGraph()
for switch in net.switches:
G.add_node(switch.name)
for host in net.hosts:
G.add_node(host.name)
for link in net.links:
G.add_edge(link.intf1.node.name, link.intf2.node.name)
# 保存拓扑图
nx.draw(G, with_labels=True, node_size=1000, node_color='lightblue', font_weight='bold')
plt.savefig('topology1.png')
plt.show() # 显示网络拓扑
# 打开命令行界面
CLI(net)
# 关闭网络
net.stop()
if __name__ == '__main__':
create_network()
多个ovs时需要加入arp泛洪流允许
在交换机上允许arp广播流(请求和回复,跨交换机时如果arp包没有通过,一般每个ovs都需要来回)
s1.cmd(‘ovs-ofctl add-flow s1 table=0,priority=100,dl_type=0x0806,nw_proto=1,actions=flood’)
s1.cmd(‘ovs-ofctl add-flow s1 table=0,priority=100,dl_type=0x0806,nw_proto=2,actions=flood’)
使用静态流表优点是即使当前网络存在环路(假设给两个交换机之间增加一条链路,也不会产生广播风暴,流量的转发完全按照人为设定的流表转发。缺点是当节点增加时,流表配置工作量剧增。