Open vSwitch的ovs-vsctl命令详解 - 海筒自习社

/ 0评 / 0

Introduction

Open vSwitch中有多个命令,分别有不同的作用,大致如下:
本文主要介绍ovs-vsctl。以下命令查询主机上已有的 OVS bridge,以及其中的 port。
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl show  <br />
例子
$ ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl show<br />fc562da8<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>fb36<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>4d62<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>8b47<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>5502e72069dc  <br />    Bridge br<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vxlan<br />        Port <span style="box-sizing: border-box; color: #a6e22e; margin: 0px; padding: 0px;">"vxlan0"</span><br />            Interface <span style="box-sizing: border-box; color: #a6e22e; margin: 0px; padding: 0px;">"vxlan0"</span><br />                type<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span> vxlan<br />                options<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span> <span style="box-sizing: border-box; margin: 0px; padding: 0px;">{</span>remote_ip<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #a6e22e; margin: 0px; padding: 0px;">"10.10.10.1"</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">}</span><br />        Port br<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vxlan<br />            Interface <span style="box-sizing: border-box; margin: 0px; padding: 0px;">br</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vxlan<br />                type<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span> internal<br />        Port <span style="box-sizing: border-box; color: #a6e22e; margin: 0px; padding: 0px;">"veth2"</span><br />            Interface <span style="box-sizing: border-box; color: #a6e22e; margin: 0px; padding: 0px;">"veth2"</span><br />    ovs_version<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span> <span style="box-sizing: border-box; color: #a6e22e; margin: 0px; padding: 0px;">"2.5.0"</span><br />

Bridge 相关命令

创建 bridge

创建bridge(或switch,以下统称为bridge) br0
使用选项--may-exist后,若欲创建的bridge已存在,该命令什么也不做,也不报错。
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span>may<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>exist<span style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span> add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>br br0  <br />

删除bridge

删除 bridge br0
使用选项--if-exists后,若欲删除的bridge不存在,该命令什么也不做,也不报错。
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span><span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">if</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>exists<span style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span> del<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>br br0  <br />

查询已有的 bridge

ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl list<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>br  <br />

端口相关命令

添加端口

添加端口(物理端口或vNIC)eth1到bridge eth1中
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span>may<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>exist<span style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span> add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>port br0 eth1  <br />

创建 bond

在br0上创建一个bond了eth0,eth1和eth2的bond端口bond0
<span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># ovs-vsctl add-bond <bridge> <port> <iface...><br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>bond br0 bond0 eth0 eth1 eth2  <br />

移除端口

从br0上移除端口eth1
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span><span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">if</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>exists<span style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span> del<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>port br0 eth1  <br />

列出端口

列出br0上的端口(不包括internal port)
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl list<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>ports br0  <br />

查看端口详细数据

列出OVS中端口eth1的详细数据
$ ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl list interface <span style="box-sizing: border-box; margin: 0px; padding: 0px;">eth1</span><br />

OpenFlow 控制器相关

添加控制器

<span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># ovs-vsctl set-controller <bridge> <target...><br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>controller br0 tcp<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">1.2</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">.</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">3.4</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">6633</span><br /><br /><span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># 设置多个controller<br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>controller br0 tcp<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">1.2</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">.</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">3.4</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">6633</span> tcp<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">4.3</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">.</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">2.1</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">6633</span><br /><br /><span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># 添加使用unix socket通信的controller<br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>controller br0 unix<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">/</span>var<span style="box-sizing: border-box; margin: 0px; padding: 0px;">/</span>run<span style="box-sizing: border-box; margin: 0px; padding: 0px;">/</span>xx<span style="box-sizing: border-box; margin: 0px; padding: 0px;">/</span>xx<span style="box-sizing: border-box; margin: 0px; padding: 0px;">.</span>sock  <br />

移除控制器

ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl del<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>controller br0  <br />

查询 brige 上已配置的控制器

ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;
">-</span>vsctl get<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>controller br0  <br />

VLAN 相关

配置端口为Access口

设置br0中的端口eth0为VLAN 10的access口
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span> port eth0 tag<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10</span>  <br />
添加eth1到指定bridge br0中,同时将其配置成指定VLAN 10的access端口
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>svctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>port br0 eth1 tag<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10</span>  <br />

配置端口为Trunk口

在br0上添加port eth1为VLAN 9,10,11的trunk
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>port br0 eth1 trunk<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">9</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">,</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">,</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">11</span>  <br />

VXLAN 相关

在bridge ovs0中添加远端IP为10.10.10.1的VXLAN endpoint端口vxlan0
<span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># key=100表示设置vni为100,不设置默认为0<br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>port ovs0 vxlan0 <span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span> <span style="box-sizing: border-box;
color: #66d9ef; margin: 0px; padding: 0px;">set</span> interface <span style="box-sizing: border-box; margin: 0px; padding: 0px;">vxlan0</span> type<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span>vxlan options<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span>remote_ip<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10.10</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">.</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10.1</span> options<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span>key<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">100</span><br /><br /><span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># 不设key值,vni默认为0<br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>port ovs0 vxlan0 <span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span> <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span> interface <span style="box-sizing: border-box; margin: 0px; padding: 0px;">vxlan0</span> type<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span>vxlan options<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span>remote_ip<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10.10</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">.</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10.1</span><br /><br /><span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># key=flow的话,表示该port的vni可以通过openflow的actions来进行设置<br /></span><span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># 如: actions=set_field:100->tun_id<br /></span><span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># 或: actions=set_tunnel:100<br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>port ovs0 vxlan0 <span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span> <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span> interface <span style="box-sizing: border-box; margin: 0px; padding: 0px;">vxlan0</span> type<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span>vxlan options<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span>remote_ip<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10.10</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">.</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10.1</span> options<span style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span>key<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span>flow  <br />

其他

Atomic operation

一条命令创建bridge br0的,并添加eth0到br0中
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>br br0 <span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span> add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>port br0 eth0  <br />

创建 internal port

OVS internal port 可以配置IP地址,普通 port 上配置的IP地址是不起作用的。在 br0 上创建一个internal port in0:
ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>br br0 in0 <span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span> <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span> interface <spa
n style="box-sizing: border-box; margin: 0px; padding: 0px;">in0</span> type<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span>internal  <br />ip addr add <span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10.10</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">.</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10.10</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">/</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">24</span> dev in0<br /><br /><span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># 创建internal port的同时将其设置为VLAN 10的access port<br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>br br0 in1 tag<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">10</span> <span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span> <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span> interface <span style="box-sizing: border-box; margin: 0px; padding: 0px;">in1</span> type<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span>internal  <br />ip addr add <span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">20.20</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">.</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">20.20</span><span style="box-sizing: border-box; margin: 0px; padding: 0px;">/</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">24</span> dev in1  <br />

设置 OpenFlow port id

<span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># 将已在ovs中的端口veth1的OpenFlow端口设置成100<br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span> interface <span style="box-sizing: border-box; margin: 0px; padding: 0px;">veth1</span> ofport_request<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">100</span><br /><br /><span spellcheck="true" style="box-sizing: border-box; color: slategrey; margin: 0px; padding: 0px;"># 将端口veth1添加到bridge br0中,并将veth1的OpenFlow端口设置成200<br /></span>ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl add<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>port br0 veth1 <span style="box-sizing: border-box; margin: 0px; padding: 0px;">--</span> <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span> interface <span style="box-sizing: border-box; margin: 0px; padding: 0px;">veth1</span> ofport_request<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span style="box-sizing: border-box; color: #ae81ff; margin: 0px; padding: 0px;">200</span>  <br />
{% admonition note Note %} OpenFlow的端口 id 在设置 flow 的匹配字段 in_port 以及 actions 字段的 output 中都会用到。 
可以通过命令ovs-ofctl show br0来查看 br0 中各端口的 OpenFlow 端口 id,该 id 并不求是按顺序的。 {% endadmonition %}

设置OpenFlow版本

ovs<span style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span>vsctl <span style="box-sizing: border-box; color: #66d9ef; margin: 0px; padding: 0px;">set</span> bridge br0 protocols<span style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span>OpenFlow10<span style="box-sizing: border-box; margin: 0px; padding: 0px;">,</span>OpenFlow12<span style="box-sizing: border-box; margin: 0px; padding: 0px;">,</span>OpenFlow13  

发表评论

电子邮件地址不会被公开。 必填项已用*标注