openvswitch命令行工具

ovs-vsctl

ovs-vsctl 连接到 ovsdb-server 进程,查询或更新 ovs 数据库。如果更新数据库,则默认它会等待 ovs-vswitchd 根据数据库重新配置交换机后才返回。但使用 --no-wait 选项可不等待而立即返回。
ovs-vsctl 可在单条调用中执行多条子命令,这实现为对数据库的原子事务操作。调用格式:

1
ovs-vsctl [全局选项]... [--] [子选项]... 子命令 [子参数]... [-- [子选项]... 子命令 [子参数]...]...

可以看出,子命令间使用 – 隔开,但首条子命令前可以省略 –,除非其带子选项。

操作 bridge

  • 创建网桥:
1
2
(venv)[root@network ~]# ovs-vsctl add-br br0
(venv)[root@network ~]# ovs-vsctl add-br br1

  • 查看 ovs 数据库内容的概览:
1
2
3
4
5
6
7
8
9
10
11
(venv)[root@network ~]# ovs-vsctl show
d707388c-b65e-41ae-9091-3aee56fd6fa6
Bridge "br1"
Port "br1" # 自动创建的internal类型的本地port和interface
Interface "br1"
type: internal
Bridge "br0"
Port "br0"
Interface "br0"
type: internal
ovs_version: "2.3.1"
  • 列出网桥:
1
2
3
(venv)[root@network ~]# ovs-vsctl list-br
br0
br1

  • 删除网桥:
1
(venv)[root@network ~]# ovs-vsctl del-br br1
1
2
3
4
5
6
7
(venv)[root@network ~]# ovs-vsctl show
d707388c-b65e-41ae-9091-3aee56fd6fa6
Bridge "br0"
Port "br0"
Interface "br0"
type: internal
ovs_version: "2.3.1"
  • 操作 port

创建 system 类型(默认)的 port:

1
2
(venv)[root@network ~]# ovs-vsctl add-port br0 port0
ovs-vsctl: Error detected while setting up 'port0'. See ovs-vswitchd log for details.
1
2
(venv)[root@network ~]# cat /var/log/openvswitch/ovs-vswitchd.log | tail -1
2015-06-10T13:00:08.898Z|00020|bridge|WARN|could not open network device port0 (No such device)

上面创建 system 类型的 port 后,ovs 试图打开对应的不存在的同名网卡设备(虚拟的或物理的)时报错。如果存在同名网卡,则不会报错:

1
2
3
4
(venv)[root@network ~]# ip link show dev eth2
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:f9:3a:14 brd ff:ff:ff:ff:ff:ff
(venv)[root@network ~]# ovs-vsctl add-port br0 eth2
  • 列出 port,默认是不显示本地 port 的:
1
2
3
(venv)[root@network ~]# ovs-vsctl list-ports br0
eth2
port0

  • 删除 port:
1
2
3
4
5
6
7
8
9
(venv)[root@network ~]# ovs-vsctl del-port eth2
(venv)[root@network ~]# ovs-vsctl del-port port0
(venv)[root@network ~]# ovs-vsctl show
d707388c-b65e-41ae-9091-3aee56fd6fa6
Bridge "br0"
Port "br0"
Interface "br0"
type: internal
ovs_version: "2.3.1"

  • 创建 bond:
1
2
3
4
5
6
7
8
9
10
11
(venv)[root@network ~]# ovs-vsctl add-bond br0 bond0 eth1 eth2
(venv)[root@network ~]# ovs-vsctl show
d707388c-b65e-41ae-9091-3aee56fd6fa6
Bridge "br0"
Port "br0"
Interface "br0"
type: internal
Port "bond0"
Interface "eth2" # 两个网卡
Interface "eth1"
ovs_version: "2.3.1"
  • 列出所有连接到网桥的 interface,默认是不显示本地 interface 的:
1
2
3
(venv)[root@network ~]# ovs-vsctl list-ifaces br0
eth1
eth2

操作 ovs 数据库

ovs 数据库目前定义的表:

表名 含义
Open_vSwitch ovs-vswitchd 的全局配置。只包含一条记录,记录标识为 ‘.’
Bridge 网桥配置。记录标识为表名
Port 端口配置。记录标识为端口名
Interface 网卡配置。记录标识为网卡名
Flow_Table 流表。记录标识为流表名
QoS 端口的 Quality-of-service 配置。记录标识为端口名
Queue QoS 配置中一个队列的配置。记录标识为 UUID
Mirror 端口镜像配置。记录标识为镜像名
Controller OpenFlow 控制器配置。记录标识为绑定的网桥名
Manager OVSDB 连接配置。记录标识为目标

操作 ovs 数据库的子命令:- 列出表 table 中的记录 record。不指定 record,则列出表 table 中的所有记录。

[--if-exists] [--columns=column[,column]...] list table [record]...

  • 查找表 table 中的其列 column 满足给定值或键值对的记录的相关列值。

[--columns=column[,column]...] find table [column[:key]=value]...

  • 获取表 table 的记录 record 中的列 column(中键 key)的值。如果指定 –id=@name,则 name 可以在同一条 ovs-vsctl 调用中用于引用此值。

[--if-exists] [--id=@name] get table record [column[:key]]...

  • 设置表 table 的记录 record 中的列 column

[--if-exists] set table record column[:key]=value...

  • 添加表 table 的记录 record 中的列 column

[--if-exists] add table record column [key=]value...

  • 清除表 table 的记录 record 中的匹配给定值(或键,或键值对)的列 column

[--if-exists] remove table record column [key=value | key | value]...

  • 清除表 table 的记录 record 中的列 column 为空。

[--if-exists] clear table record column...

  • 在表 table 中创建一条新记录,根据指定列值对初始化,没有指定的列赋默认值。输出记录的 UUID。如果指定 –id=@name,则 name 可以在同一条 ovs-vsctl 调用中用于引用此记录。

[--id=@name] create table column[:key]=value...

  • 删除表 table 中记录 record。

[--if-exists] destroy table record...

  • 删除表 table 中所有数据,只用于表 QoS 和表 Queue。

--all destroy table

  • 等待表 table 中记录 record 存在,且列值对符合给定。可不指定或指定多个列值对。可指定超时时间。

[--timeout=n] wait-until table record [column[:key]=value]...

ovs-ofctl

流语法

字段 匹配含义
in_port=port OpenFlow 端口号或端口关键字(例如 LOCAL)
dl_vlan=vlan IEEE 802.1q VLAN tag。0xffff 表示没打 tag;否则应指定 0 ~ 4095
dl_vlan_pcp=priority IEEE 802.1q Priority Code Point (PCP) 优先级,0 ~ 7
dl_src=xx:xx:xx:xx:xx:xx 以太网源地址
dl_dst=xx:xx:xx:xx:xx:xx 以太网目的地址