管理相关命令
查看bridge信息
ovs-ofctl show br0
# 显示结果中port前面的数字为OpenFlow port id
Dump flow table
# Dump br0所使用的flow table
ovs-ofctl dump-tables br0
Dump flow
# Dump br0的所有flow
ovs-ofctl dump-flows br0
# Dump br0上匹配xx的flow
ovs-ofctl dump-flows br0 xx
Flow Table相关命令
添加 flow
ovs-ofctl add-flow br0 in_port=1,actions=xx
# 从文件br0flow.txt中导入Flow Table
# 该文件必须一行一个flow
ovs-ofctl add-flow br0 - < br0flow.txt
修改 flow
ovs-ofctl mod-flows br0 xxx
ovs-ofctl mod-flows br0 - < br0flow.txt
删除 flow
# 删除br0上的所有flow
ovs-ofctl del-flows br0
# 删除br0上匹配xx的所有flow
ovs-ofctl del-flows br0 xx
# 删除br0上匹配文件br0flow.txt中各条目的flow
ovs-ofctl del-flows br0 - < br0flow.txt
Flow Syntax
ovs-ofctl
的Flow语法由一系列field=value
形式的键值对组成,用英文逗号和空格隔开。
在描述Flow时,必须要遵循IP stack常理。比如说,在flow中使用了L3(层3)的字段时,必须也要指明L2所用的协议。使用了L4的字段时,必须也要指明L2和L3所用的协议。
本文档不包含 ovs-ofctl
对 OpenFlow 的 VMware/Nicira 扩展 flow 语法。
in_port
in_port=<port>
匹配从OpenFlow port id <port>
进入的数据包。OpenFlow port id可以通过`ovs-ofctl ovs-ofctl
in_port=<port>
匹配IEEE 802.1Q VLAN tag为 <vlan>
的数据包。dl_vlan=0xffff
表示匹配没有VLAN tag的包。
dlvlanpcp
<port>
匹配IEEE 802.1Q Priority Code Point(PCP, 优先级代码点)为
dlsrc & dldst
<vlan>
匹配源和目标MAC地址。
dl_vlan=0xffff
匹配源和目的MAC地址。其中"/"后面的为掩码。比如说,如果掩码是:
01:00:00:00:00:00
只匹配组播MAC (第一个字节LSB为1的MAC地址为MAC组播地址)fe:ff:ff:ff:ff:ff
匹配其他所有MAC,除了组播MACff:ff:ff:ff:ff:ff
完全匹配掩码前的MAC,和省掉掩码的效果一样00:00:00:00:00:00
完全通配,相当于(dl_dst=*)
dl_type
01:00:00:00:00:00
匹配L2(Data Link Layer) header中的协议类型
dl_type=0x0800
匹配IP数据包dl_type=0x0806
匹配ARP数据包dl_type=0x8035
匹配RARP数据包
{% admonition note Note %} 需要说明的是 dltype=0x0800 可以用关键字 ip 代替。 而 dltype=0x0806 可以用关键字 arp 代替。dl_type=0x8035 可以用关键字 rarp 代替。 {% endadmonition %}
nwsrc & nwdst
fe:ff:ff:ff:ff:ff
当使用了 dl_type=0x0800
或者关键字 ip
或 tcp
时,nw_src
和 nw_dst
分配匹配IP头中的源IP地址和目的IP地址。其中netmask可以是255.255.255.0
这样的(dotted quad)形式,也可以是数字 24
这样的(CIDR)形式
当使用了 dl_type=0x0806
或者关键字 arp
时,nw_src
匹配 ARP 头中的 arspa(Sender Protocol Address)字段。nw_dst
匹配ARP头中的 artpa(Target Protocol Address)字段
当使用了 dl_type=0x8035
或者关键字 rarp
时,nw_src
匹配 RARP 头中的 arspa(Sender Protocol Address)字段。nw_dst
匹配RARP头中的 artpa(Target Protocol Address)字段
当 dl_type
使用了通配符或者除了0x0800
, 0x0806
, 0x8035
以外的值,则nw_src
, nw_dst
的值会被忽略
nwproto or ipproto
ff:ff:ff:ff:ff:ff
- 当
dl_type=0x0800
或使用了关键字ip
时,匹配IP头中的proto字段,取值区间[0, 255],比如为1时可以匹配ICMP数据包,为6时匹配TCP数据包。 - 当
dl_type=0x86dd
或使用了关键字ipv6
是,匹配IPv6头中的proto字段,取值区间[0, 255],比如为58时匹配ICMPv6数据包,为6时匹配TCP数据包 - 当
dl_type=0x0806
或者使用了关键字arp
时,匹配ARP opcode的低8位,ARP opcode大于255时,与等于0效果一样 - 当
dl_type=0x8035
或者使用了关键字rarp
时,匹配ARP opcode的低8位, ARP opcode大于255时,与等于0效果一样 - 当
dl_type
使用了通配符或这除了0x0800
,0x0806
,0x8035
以外的值,则nw_proto
的值会被忽略
nw_tos
00:00:00:00:00:00
匹配IP ToS字段或IPv6的traffic class字段取值为<toc>
的数据包。取值区间[0, 255],需要注意的是最低2位会被忽略。当dl_type
使用除0x800(IP)和0x86dd(IPv6)以为的数值时,该字段被忽略。
ip_dscp
dl_type=0x0800
匹配IP DSCP字段或IPv6的traffic class字段取值为<dscp>
的数据包。IP ToS字段的高6位为DSCP比特位。取值区间[0, 63],需要注意的是最低2位会被忽略。当dl_type
使用除0x800(IP)和0x86dd(IPv6)以为的数值时,该字段被忽略。
nwecn or ipecn
dl_type=0x0806
匹配IP ToS字段或IPv6的traffic class字段中ecn比特位为<ecn>
的数据包。IP ToS字段的低2位为ECN比特位。取值区间[0, 3]。当dl_type
使用除0x800(IP)和0x86dd(IPv6)以为的数值时,该字段被忽略。
nw_ttl
dl_type=0x8035
匹配IP和IPv6的TTL字段为dl_type
使用除0x800(IP)和0x86dd(IPv6)以为的数值时,该字段被忽略。
tcp / udp / sctp port
dl_type=0x0800
匹配TCP, UDP, SCTP 源或目的端口为<port>
的数据包。取值区间[0, 65535]。当dl_type
以及nw_proto
使用了通配符或者设置了不合理的值,该字段被忽略。
ip
按位匹配TCP, UDP, SCTP源或目的端口。
tcp flags
tcp
按位匹配TCP flags。其中
除了按位匹配TCP flags外,也可以flag的名称(见下)来进行描述匹配规则。每个flag名称前面的"+"表示匹配设置了该flag的数据包,而"-"表示匹配未设置该flag的数据包。规则中未提及的flag在匹配时忽略。比如 tcp,tcp_flags=+syn-ack
表示匹配SYN但非ACK的TCP数据包。
目前TCP协议中定义了9个flag位,3个额外的保留位(必须设置成0)。这些 flag 从 LSB(最低有效位)开始的排序如下:
0
:fin
表示发送方没有数据要传输了,要去释放连接1
:syn
同步序列数2
:rst
重置连接3
:p```language-bash
Pu```language-bash功能,指示接收方应该加快将这个报文段交给应用层而不用等待缓冲区装满4
:ack
ACK5
:urg
表示高优先级数据包6
:ece
ECN Echo7
:cwr
Congestion Windows Reduced8
:ns
Nonce Sum9-11
: 保留位12-15
: 不可用于匹配,必须设为0
icmptype & icmpcode
icmp_type=<type>
icmp_code=<code>
当 dl_type
和 nw_proto
确定数据包为 ICMP 或 ICMPv6 时,匹配 ICMP type 和 code。取值区间都为[0, 255]。如果 dl_type
和 mw_proto
使用了其他值时,该字段忽略。
协议关键字
协议关键字相当于是个alias,对应如下:
ip = dl_type=0x0800
ipv6 = dl_type=0x86dd
icmp = dl_type=0x0800,nw_proto=1
icmp6 = dl_type=0x86dd,nw_proto=58
tcp = dl_type=0x0800,nw_proto=6
tcp6 = dl_type=0x86dd,nw_proto=6
udp = dl_type=0x0800,nw_proto=17
udp6 = dl_type=0x86dd,nw_proto=17
sctp = dl_type=0x0800,nw_proto=132
sctp6 = dl_type=0x86dd,nw_proto=132
arp = dl_type=0x0806
rarp = dl_type=0x8035
mpls = dl_type=0x8847
mplsm = dl_type=0x8848
cookie
cookie=<value>
cookie 字段可以用于
命令add-flow
, add-flows
和 mod-flows
中可以使用该字段,用于给 flow 设置识别信息。
cookie=<value/mask>
actions
ovs-ofctl的 add-flow
, add-flows
以及 mod-flows
命令都需要 actions 字段,描述对匹配的数据包执行的动作
在上述的三条命令中,actions字段是flow的一部分,actions字段中可以有多个 action,它们之间用逗号隔开,一个flow的完整语法如下:
<field>=<value>,[<field>=<value>]...,actions=<action>[,<action>...]
可使用的action非常多。如下:
output
output:<port>
将数据包输出到OpenFlow port <port>
group
group:<group_id>
将数据包输出到OpenFlow group <group_id>
normal
normal
按照设备的常规L2/L3处理流程来处理数据包。这通常是OVS默认flow中的action。要注意的是,并不是所有的OpenFlow switch都支持这个action。
flood
flood
将数据包输出到所有物理端口,除了该数据包的输入口以及不可被flooding的端口。
all
all
将数据包输出到所有物理端口,除了该数据包的输入口。
local
local
将数据包输出到local port(与bridge同名的端口)
in_port
in_port
将数据包输出到其输入口
controller
controller(<key>=<value>...)
将数据包以"packet in"消息的形式发送到OpenFlow控制器。其中<key>=<value>
键值对可以是:
max_len=<nbytes>
只将数据包的个字节发送到控制器。默认发送这个数据包。 reson=<reason>
指明"pakcet in"消息中的reason字段。默认reason为action
,还可以是no_match
,invalid_ttl
。id=<controller-id>
指明控制器id,16位整数。表示要发送给那个控制器。默认使用的id是0.
controller
controller[:nbytes]
分别是controller()
和controller(max_len=<nbytes>)
的简略写法。
enqueue
enqueue(<port>,<queue>)
将数据包放到端口
drop
drop
丢掉该数据包。
vlan
mod_vlan_vid:<vlan_vid>
修改数据包的VLAN id为
mod_vlan_pcp:<vlan_pcp>
修改数据包的VLAN 优先级priority为
strip_vlan
如果数据包有VLAN tag,则剥去VLAN tag。
pu```language-bash_vlan:<ethertype>
给数据包添加新的VLAN tag。VLAN tag中ethertype设置为
mpls
pu```language-bash_mpls:<ethertype>
将数据包的Ethertype改成<ethertype>
,只能是0x8847
或者0x8848
,同时添加MPLS LSE。
pop_mpls:<ethertype>
datalink header mod
剥去数据包最外层的一个MPLS lable stack。目前的实现中<ethertype>
只能是non-MPLS Ethertype,所以pop_mpls
只能用于只有一层MPLS lable stack的数据包。
mod_dl_src:<mac>
mod_dl_dst:<mac>
将数据包的源或目的MAC地址设置成
network header mod
mod_nw_src:<ip>
mod_nw_dst:<ip>
将数据包的源或目的IP地址设置成
mod_nw_tos:<tos>
设置IPv4头ToS/DSCP或IPv6头traffic class field中DSCP比特位设置成
mod_nw_ecn:<ecn>
设置IPv4头ToS或IPv6头traffic class field中ECN比特位为<ecn>
,数值区间为[0, 3]。这个action并不会修改高6位(6 MSB)。
需要OpenFLow 1.1以上。
mod_nw_ttl:<ttl>
修改IPv4 TTL或IPv6 hop limit为
需要OpenFlow 1.1以上。
transport header mod
mod_tp_src:<port>
mod_tp_dst:<port>
将数据包的TCP/UDP/SCTP源或目的端口设置成
Examples
VLAN相关
# 添加VLAN tag
ovs-ofctl add-flow br0 in_port=1,actions=mod_vlan_vid:10,output:2
# 剥去VLAN tag
ovs-ofctl add-flow br0 in_port=2,dl_vlan=100,actions=strip_vlan,output:1
使用cookie
# add a flow
ovs-ofctl add-flow br0 cookie=0xf,tcp,tcp_dst=22,actions=mod_nw_tos:128,normal
# To delte this flow
ovs-ofctl del-flows br0 cookie=0xf/-1,tcp,tcp_dst=22
# Or simply
ovs-ofctl del-flows br0 cookie=0xf/-1
# trace a flow
ovs-appctl ofproto/trace br0 tcp,tcp_dst=22
Contributors
[hailanguage-bashanh][hai
language-bashanh]