ovn学习-2-ovsdb

ovsdb协议

ovn-arch

client库

自带 C 和 Python的。其他开源的库有

  • golang
    https://github.com/socketplane/libovsdb/blob/master/example/play_with_ovs.go
    https://github.com/contiv/ofnet/blob/master/ovsdbDriver/ovsdbDriver.go
  • python
    ovsdbapp

以下是一个ovsdbapp使用的例子,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from ovsdbapp.backend.ovs_idl import connection
from ovsdbapp import constants
from ovsdbapp.schema.open_vswitch import impl_idl
ovsdb_connection = connection.Connection(
idl=connection.OvsdbIdl.from_server(
'unix:/var/run/openvswitch/db.sock', 'Open_vSwitch'),
timeout=constants.DEFAULT_TIMEOUT)
api = impl_idl.OvsdbIdl(ovsdb_connection)
result = api.br_exists("br0").execute(check_error=True)
print result
result = api.del_br("br1").execute(check_error=True)
print result

协议交互举例

ovs-vsctl操作的是ovs db。ovs-vswitchd有monitor ovs db,ovs db的变更都会发到ovs-vswitchd。ovs-vsctl和ovs-vswitchd都是通过ovsdb-server进程(unix socket或者是tcp连接)去watch或者update db的。

ovs-vsctl add-port br0 abc -- set interface abc type=internal为例,
ovs-vsctl会在ovsdb的Port表和Interface表增加一行数据,ovs-vswitchd收到通知后,会创建接口,并且把ofport,mtu,mac,link_state等信息写入Interface表里。写入是否成功呢,还是靠的监控通知机制。

ovs-vsctl -v可以打印出ovs-vsctl和ovsdb-server详细的json rpc交互过程。

ovsdb-client monitor tcp:127.0.0.1:6640 Interface name,ofport,external_ids --format=json这个命令可以watch某些表某些字段的变更通知:

1. 初始时会把所有的内容返回来。
2. 以后只会返回变化的。
3. 注意,返回的action里有initial,delete, new不是实际协议的内容,这是处理过了的。协议里只有old和new。见rfc里的4.1.6里的解释。

ovs-vswitchd消息处理过程如下,

  • 收到update通知消息,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
[null, {
"Port": {
"5943a115-162f-484f-8e45-5cbf9cd112c5": {
"new": {
"name": "abc",
"statistics": ["map", []],
"vlan_mode": ["set", []],
"qos": ["set", []],
"trunks": ["set", []],
"mac": ["set", []],
"status": ["map", []],
"interfaces": ["uuid", "9fb84aa6-ac6e-4575-b4e7-0cc87c755195"],
"bond_downdelay": 0,
"bond_mode": ["set", []],
"bond_updelay": 0,
"other_config": ["map", []],
"bond_fake_iface": false,
"tag": ["set", []],
"fake_bridge": false,
"lacp": ["set", []]
}
}
},
"Bridge": {
"3d081a41-f691-41a6-a469-d0a0100fbf8c": {
"old": {
"ports": ["set", [
["uuid", "10c55211-94b2-4bc7-aefb-e524c3f1f7eb"],
["uuid", "421029b9-2467-4946-bece-1e408524a2f9"]
]]
},
"new": {
"name": "br0",
"flood_vlans": ["set", []],
"netflow": ["set", []],
"mirrors": ["set", []],
"status": ["map", []],
"datapath_id": "0000421a083da641",
"controller": ["set", []],
"ipfix": ["set", []],
"protocols": ["set", []],
"fail_mode": ["set", []],
"ports": ["set", [
["uuid", "10c55211-94b2-4bc7-aefb-e524c3f1f7eb"],
["uuid", "421029b9-2467-4946-bece-1e408524a2f9"],
["uuid", "5943a115-162f-484f-8e45-5cbf9cd112c5"]
]],
"other_config": ["map", []],
"flow_tables": ["map", []],
"sflow": ["set", []],
"datapath_type": "",
"stp_enable": false
}
}
},
"Interface": {
"9fb84aa6-ac6e-4575-b4e7-0cc87c755195": {
"new": {
"name": "abc",
"options": ["map", []],
"mtu": ["set", []],
"link_speed": ["set", []],
"statistics": ["map", []],
"mac_in_use": ["set", []],
"type": "internal",
"cfm_remote_opstate": ["set", []],
"ingress_policing_rate": 0,
"status": ["map", []],
"mac": ["set", []],
"ofport": ["set", []],
"ifindex": ["set", []],
"cfm_fault_status": ["set", []],
"duplex": ["set", []],
"lacp_current": ["set", []],
"cfm_fault": ["set", []],
"bfd_status": ["map", []],
"link_state": ["set", []],
"admin_state": ["set", []],
"other_config": ["map", []],
"cfm_remote_mpids": ["set", []],
"ingress_policing_burst": 0,
"ofport_request": ["set", []],
"bfd": ["map", []],
"cfm_mpid": ["set", []],
"cfm_flap_count": ["set", []],
"link_resets": ["set", []],
"cfm_health": ["set", []]
}
}
},
"Open_vSwitch": {
"099e083f-aa09-4dd1-95d2-d0b4857eb3b9": {
"old": {
"next_cfg": 70
},
"new": {
"statistics": ["map", []],
"manager_options": ["uuid", "28542e8a-9886-4282-8b43-afb5b80c0cc5"],
"bridges": ["set", [
["uuid", "3d081a41-f691-41a6-a469-d0a0100fbf8c"],
["uuid", "558db8c8-bc39-4781-a441-f58e7dc6a951"]
]],
"other_config": ["map", []],
"ssl": ["set", []],
"next_cfg": 71,
"cur_cfg": 70
}
}
}
}]
  • 创建接口,bridge br0: added interface abc on port 10
  • 更新ovsdb,

发送transact请求1,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
["Open_vSwitch", {
"lock": "ovs_vswitchd",
"op": "assert"
}, {
"row": {
"ofport": 10,
"statistics": ["map", [
["collisions", 0],
["rx_bytes", 0],
["rx_crc_err", 0],
["rx_dropped", 0],
["rx_errors", 0],
["rx_frame_err", 0],
["rx_over_err", 0],
["rx_packets", 0],
["tx_bytes", 0],
["tx_dropped", 0],
["tx_errors", 0],
["tx_packets", 0]
]]
},
"table": "Interface",
"where": [
["_uuid", "==", ["uuid", "9fb84aa6-ac6e-4575-b4e7-0cc87c755195"]]
],
"op": "update"
}, {
"row": {
"cur_cfg": 71
},
"table": "Open_vSwitch",
"where": [
["_uuid", "==", ["uuid", "099e083f-aa09-4dd1-95d2-d0b4857eb3b9"]]
],
"op": "update"
}]

请求2,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
["Open_vSwitch", {
"lock": "ovs_vswitchd",
"op": "assert"
}, {
"row": {
"status": ["map", [
["driver_name", "openvswitch"]
]],
"mtu": 1500,
"mac_in_use": "6e:2b:1b:5e:23:8f",
"link_state": "down",
"link_resets": 0,
"admin_state": "down",
"ifindex": 22
},
"table": "Interface",
"where": [
["_uuid", "==", ["uuid", "9fb84aa6-ac6e-4575-b4e7-0cc87c755195"]]
],
"op": "update"
}]
  • 收到update通知消息(因为上面的update transact),

update通知消息1,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
[null, {
"Interface": {
"9fb84aa6-ac6e-4575-b4e7-0cc87c755195": {
"old": {
"statistics": ["map", []],
"ofport": ["set", []]
},
"new": {
"name": "abc",
"options": ["map", []],
"mtu": ["set", []],
"link_speed": ["set", []],
"statistics": ["map", [
["collisions", 0],
["rx_bytes", 0],
["rx_crc_err", 0],
["rx_dropped", 0],
["rx_errors", 0],
["rx_frame_err", 0],
["rx_over_err", 0],
["rx_packets", 0],
["tx_bytes", 0],
["tx_dropped", 0],
["tx_errors", 0],
["tx_packets", 0]
]],
"mac_in_use": ["set", []],
"type": "internal",
"cfm_remote_opstate": ["set", []],
"ingress_policing_rate": 0,
"status": ["map", []],
"mac": ["set", []],
"ofport": 10,
"ifindex": ["set", []],
"cfm_fault_status": ["set", []],
"duplex": ["set", []],
"lacp_current": ["set", []],
"cfm_fault": ["set", []],
"bfd_status": ["map", []],
"link_state": ["set", []],
"admin_state": ["set", []],
"other_config": ["map", []],
"cfm_remote_mpids": ["set", []],
"ingress_policing_burst": 0,
"ofport_request": ["set", []],
"bfd": ["map", []],
"cfm_mpid": ["set", []],
"cfm_flap_count": ["set", []],
"link_resets": ["set", []],
"cfm_health": ["set", []]
}
}
},
"Open_vSwitch": {
"099e083f-aa09-4dd1-95d2-d0b4857eb3b9": {
"old": {
"cur_cfg": 70
},
"new": {
"statistics": ["map", []],
"manager_options": ["uuid", "28542e8a-9886-4282-8b43-afb5b80c0cc5"],
"bridges": ["set", [
["uuid", "3d081a41-f691-41a6-a469-d0a0100fbf8c"],
["uuid", "558db8c8-bc39-4781-a441-f58e7dc6a951"]
]],
"other_config": ["map", []],
"ssl": ["set", []],
"next_cfg": 71,
"cur_cfg": 71
}
}
}
}]

update通知消息2,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[null, {
"Interface": {
"9fb84aa6-ac6e-4575-b4e7-0cc87c755195": {
"old": {
"status": ["map", []],
"mtu": ["set", []],
"mac_in_use": ["set", []],
"link_state": ["set", []],
"admin_state": ["set", []],
"link_resets": ["set", []],
"ifindex": ["set", []]
},
"new": {
"name": "abc",
"options": ["map", []],
"mtu": 1500,
"link_speed": ["set", []],
"statistics": ["map", [
["collisions", 0],
["rx_bytes", 0],
["rx_crc_err", 0],
["rx_dropped", 0],
["rx_errors", 0],
["rx_frame_err", 0],
["rx_over_err", 0],
["rx_packets", 0],
["tx_bytes", 0],
["tx_dropped", 0],
["tx_errors", 0],
["tx_packets", 0]
]],
"mac_in_use": "6e:2b:1b:5e:23:8f",
"type": "internal",
"cfm_remote_opstate": ["set", []],
"ingress_policing_rate": 0,
"status": ["map", [
["driver_name", "openvswitch"]
]],
"mac": ["set", []],
"ofport": 10,
"ifindex": 22,
"cfm_fault_status": ["set", []],
"duplex": ["set", []],
"lacp_current": ["set", []],
"cfm_fault": ["set", []],
"bfd_status": ["map", []],
"link_state": "down",
"admin_state": "down",
"other_config": ["map", []],
"cfm_remote_mpids": ["set", []],
"ingress_policing_burst": 0,
"ofport_request": ["set", []],
"bfd": ["map", []],
"cfm_mpid": ["set", []],
"cfm_flap_count": ["set", []],
"link_resets": 0,
"cfm_health": ["set", []]
}
}
}
}]