-
Notifications
You must be signed in to change notification settings - Fork 390
/
vlan_port_binding_ovsdb.cc
381 lines (337 loc) · 14.1 KB
/
vlan_port_binding_ovsdb.cc
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
extern "C" {
#include <ovsdb_wrapper.h>
};
#include <ovs_tor_agent/tor_agent_init.h>
#include <ovsdb_client.h>
#include <ovsdb_client_idl.h>
#include <ovsdb_client_session.h>
#include <physical_switch_ovsdb.h>
#include <logical_switch_ovsdb.h>
#include <physical_port_ovsdb.h>
#include <vlan_port_binding_ovsdb.h>
#include <vm_interface_ksync.h>
#include <oper/vn.h>
#include <oper/interface_common.h>
#include <oper/physical_device.h>
#include <oper/physical_device_vn.h>
#include <ovsdb_types.h>
using OVSDB::OvsdbDBEntry;
using OVSDB::VlanPortBindingEntry;
using OVSDB::VlanPortBindingTable;
using OVSDB::PhysicalSwitchEntry;
using OVSDB::PhysicalPortEntry;
using OVSDB::LogicalSwitchEntry;
using OVSDB::OvsdbClient;
using OVSDB::OvsdbClientSession;
VlanPortBindingEntry::VlanPortBindingEntry(VlanPortBindingTable *table,
const std::string &physical_device, const std::string &physical_port,
uint16_t vlan_tag, const std::string &logical_switch) :
OvsdbDBEntry(table_), logical_switch_name_(logical_switch),
physical_port_name_(physical_port), physical_device_name_(physical_device),
vlan_(vlan_tag), vmi_uuid_(nil_uuid()), old_logical_switch_name_() {
}
VlanPortBindingEntry::VlanPortBindingEntry(VlanPortBindingTable *table,
const VlanLogicalInterface *entry) : OvsdbDBEntry(table_),
logical_switch_name_(), physical_port_name_(),
physical_device_name_(""), vlan_(entry->vlan()), vmi_uuid_(nil_uuid()),
old_logical_switch_name_() {
RemotePhysicalInterface *phy_intf = dynamic_cast<RemotePhysicalInterface *>
(entry->physical_interface());
assert(phy_intf);
physical_port_name_ = phy_intf->display_name();
physical_device_name_ = phy_intf->physical_device()->name();
}
VlanPortBindingEntry::VlanPortBindingEntry(VlanPortBindingTable *table,
const VlanPortBindingEntry *key) : OvsdbDBEntry(table),
logical_switch_name_(key->logical_switch_name_),
physical_port_name_(key->physical_port_name_),
physical_device_name_(key->physical_device_name_), vlan_(key->vlan_),
vmi_uuid_(nil_uuid()), old_logical_switch_name_() {
}
void VlanPortBindingEntry::PreAddChange() {
if (!logical_switch_name_.empty()) {
LogicalSwitchTable *l_table =
table_->client_idl()->logical_switch_table();
LogicalSwitchEntry ls_key(l_table, logical_switch_name_.c_str());
logical_switch_ = l_table->GetReference(&ls_key);
} else {
logical_switch_ = NULL;
}
}
void VlanPortBindingEntry::PostDelete() {
logical_switch_ = NULL;
}
void VlanPortBindingEntry::AddMsg(struct ovsdb_idl_txn *txn) {
PhysicalPortTable *p_table = table_->client_idl()->physical_port_table();
PhysicalPortEntry key(p_table, physical_device_name_, physical_port_name_);
physical_port_ = p_table->GetReference(&key);
PhysicalPortEntry *port =
static_cast<PhysicalPortEntry *>(physical_port_.get());
// update logical switch name propagated to OVSDB server, this will
// be used in change Operation to reduce the unncessary computation
// when there is change which VlanPortBindingEntry is not
// interested into
old_logical_switch_name_ = logical_switch_name_;
if (!logical_switch_name_.empty()) {
port->AddBinding(vlan_,
static_cast<LogicalSwitchEntry *>(logical_switch_.get()));
OVSDB_TRACE(Trace, "Adding port vlan binding port " +
physical_port_name_ + " vlan " + integerToString(vlan_) +
" to Logical Switch " + logical_switch_name_);
} else {
OVSDB_TRACE(Trace, "Deleting port vlan binding port " +
physical_port_name_ + " vlan " + integerToString(vlan_));
port->DeleteBinding(vlan_, NULL);
}
// Don't trigger update for stale entries
if (!stale()) {
port->TriggerUpdate();
}
}
void VlanPortBindingEntry::ChangeMsg(struct ovsdb_idl_txn *txn) {
if (logical_switch_name_ == old_logical_switch_name_) {
// no change return from here.
return;
}
PhysicalPortEntry *port =
static_cast<PhysicalPortEntry *>(physical_port_.get());
OVSDB_TRACE(Trace, "Changing port vlan binding port " +
physical_port_name_ + " vlan " + integerToString(vlan_));
port->DeleteBinding(vlan_, NULL);
AddMsg(txn);
}
void VlanPortBindingEntry::DeleteMsg(struct ovsdb_idl_txn *txn) {
if (!physical_port_) {
return;
}
PhysicalPortEntry *port =
static_cast<PhysicalPortEntry *>(physical_port_.get());
OVSDB_TRACE(Trace, "Deleting port vlan binding port " +
physical_port_name_ + " vlan " + integerToString(vlan_));
port->DeleteBinding(vlan_,
static_cast<LogicalSwitchEntry *>(logical_switch_.get()));
port->TriggerUpdate();
}
bool VlanPortBindingEntry::Sync(DBEntry *db_entry) {
VlanLogicalInterface *entry =
static_cast<VlanLogicalInterface *>(db_entry);
std::string ls_name;
boost::uuids::uuid vmi_uuid(nil_uuid());
bool change = false;
if (entry->vm_interface()) {
vmi_uuid = entry->vm_interface()->GetUuid();
}
if (entry->vm_interface() && entry->vm_interface()->vn()) {
ls_name = UuidToString(entry->vm_interface()->vn()->GetUuid());
}
if (vmi_uuid_ != vmi_uuid) {
vmi_uuid_ = vmi_uuid;
change = true;
}
if (ls_name != logical_switch_name_) {
logical_switch_name_ = ls_name;
change = true;
}
return change;
}
bool VlanPortBindingEntry::IsLess(const KSyncEntry &entry) const {
const VlanPortBindingEntry &vps_entry =
static_cast<const VlanPortBindingEntry&>(entry);
if (vlan_ != vps_entry.vlan_)
return vlan_ < vps_entry.vlan_;
if (physical_device_name_ != vps_entry.physical_device_name_)
return physical_device_name_ < vps_entry.physical_device_name_;
return physical_port_name_ < vps_entry.physical_port_name_;
}
KSyncEntry *VlanPortBindingEntry::UnresolvedReference() {
PhysicalSwitchTable *ps_table =
table_->client_idl()->physical_switch_table();
PhysicalSwitchEntry ps_key(ps_table, physical_device_name_.c_str());
PhysicalSwitchEntry *p_switch =
static_cast<PhysicalSwitchEntry *>(ps_table->GetReference(&ps_key));
if (!p_switch->IsResolved()) {
OVSDB_TRACE(Trace, "Physical Switch unavailable for Port Vlan Binding "+
physical_port_name_ + " vlan " + integerToString(vlan_) +
" to Logical Switch " + logical_switch_name_);
return p_switch;
}
PhysicalPortTable *p_table = table_->client_idl()->physical_port_table();
PhysicalPortEntry key(p_table, physical_device_name_, physical_port_name_);
PhysicalPortEntry *p_port =
static_cast<PhysicalPortEntry *>(p_table->GetReference(&key));
if (!p_port->IsResolved()) {
OVSDB_TRACE(Trace, "Physical Port unavailable for Port Vlan Binding " +
physical_port_name_ + " vlan " + integerToString(vlan_) +
" to Logical Switch " + logical_switch_name_);
return p_port;
}
// check for VMI only if entry is not stale marked.
if (!stale()) {
VMInterfaceKSyncObject *vm_intf_table =
table_->client_idl()->vm_interface_table();
VMInterfaceKSyncEntry vm_intf_key(vm_intf_table, vmi_uuid_);
VMInterfaceKSyncEntry *vm_intf = static_cast<VMInterfaceKSyncEntry *>(
vm_intf_table->GetReference(&vm_intf_key));
if (!vm_intf->IsResolved()) {
OVSDB_TRACE(Trace, "VM Interface unavailable for Port Vlan Binding " +
physical_port_name_ + " vlan " + integerToString(vlan_) +
" to Logical Switch " + logical_switch_name_);
return vm_intf;
} else if (logical_switch_name_.empty()) {
// update latest name after resolution.
logical_switch_name_ = vm_intf->vn_name();
}
}
if (!logical_switch_name_.empty()) {
// Check only if logical switch name is present.
LogicalSwitchTable *l_table =
table_->client_idl()->logical_switch_table();
LogicalSwitchEntry ls_key(l_table, logical_switch_name_.c_str());
LogicalSwitchEntry *ls_entry =
static_cast<LogicalSwitchEntry *>(l_table->GetReference(&ls_key));
if (!ls_entry->IsResolved()) {
OVSDB_TRACE(Trace, "Logical Switch unavailable for Port Vlan "
"Binding " + physical_port_name_ + " vlan " +
integerToString(vlan_) + " to Logical Switch " +
logical_switch_name_);
return ls_entry;
}
}
return NULL;
}
const std::string &VlanPortBindingEntry::logical_switch_name() const {
return logical_switch_name_;
}
const std::string &VlanPortBindingEntry::physical_port_name() const {
return physical_port_name_;
}
const std::string &VlanPortBindingEntry::physical_device_name() const {
return physical_device_name_;
}
uint16_t VlanPortBindingEntry::vlan() const {
return vlan_;
}
VlanPortBindingTable::VlanPortBindingTable(OvsdbClientIdl *idl) :
OvsdbDBObject(idl, true) {
}
VlanPortBindingTable::~VlanPortBindingTable() {
}
void VlanPortBindingTable::OvsdbNotify(OvsdbClientIdl::Op op,
struct ovsdb_idl_row *row) {
}
KSyncEntry *VlanPortBindingTable::Alloc(const KSyncEntry *key, uint32_t index) {
const VlanPortBindingEntry *k_entry =
static_cast<const VlanPortBindingEntry *>(key);
VlanPortBindingEntry *entry = new VlanPortBindingEntry(this, k_entry);
return entry;
}
KSyncEntry *VlanPortBindingTable::DBToKSyncEntry(const DBEntry* db_entry) {
const VlanLogicalInterface *entry =
static_cast<const VlanLogicalInterface *>(db_entry);
VlanPortBindingEntry *key = new VlanPortBindingEntry(this, entry);
return static_cast<KSyncEntry *>(key);
}
OvsdbDBEntry *VlanPortBindingTable::AllocOvsEntry(struct ovsdb_idl_row *row) {
return NULL;
}
KSyncDBObject::DBFilterResp VlanPortBindingTable::OvsdbDBEntryFilter(
const DBEntry *entry, const OvsdbDBEntry *ovsdb_entry) {
const VlanLogicalInterface *l_port =
dynamic_cast<const VlanLogicalInterface *>(entry);
if (l_port == NULL) {
// Ignore entries other than VLanLogicalInterface.
return DBFilterIgnore;
}
// Logical interface without vm interface is incomplete entry
// for ovsdb, trigger delete.
if (l_port->vm_interface() == NULL) {
if (ovsdb_entry != NULL) {
OVSDB_TRACE(Trace, "VM Interface Unavialable, Deleting Logical "
"Port " + l_port->name());
} else {
OVSDB_TRACE(Trace, "VM Interface Unavialable, Ignoring Logical "
"Port " + l_port->name());
}
return DBFilterDelete;
}
// Since we need physical port name and device name as key, ignore entry
// if physical port or device is not yet present.
RemotePhysicalInterface *phy_intf = dynamic_cast<RemotePhysicalInterface *>
(l_port->physical_interface());
if (phy_intf == NULL) {
OVSDB_TRACE(Trace, "Ignoring Port Vlan Binding due to physical port "
"unavailablity Logical port = " + l_port->name());
return DBFilterIgnore; // TODO(Prabhjot) check if Delete is required.
}
if (phy_intf->physical_device() == NULL) {
OVSDB_TRACE(Trace, "Ignoring Port Vlan Binding due to device "
"unavailablity Logical port = " + l_port->name());
return DBFilterIgnore; // TODO(Prabhjot) check if Delete is required.
}
return DBFilterAccept;
}
/////////////////////////////////////////////////////////////////////////////
// Sandesh routines
/////////////////////////////////////////////////////////////////////////////
class VlanPortBindingSandeshTask : public Task {
public:
VlanPortBindingSandeshTask(std::string resp_ctx, const std::string &ip,
uint32_t port) :
Task((TaskScheduler::GetInstance()->GetTaskId("Agent::KSync")), 0),
resp_(new OvsdbVlanPortBindingResp()), resp_data_(resp_ctx),
ip_(ip), port_(port) {
}
virtual ~VlanPortBindingSandeshTask() {}
virtual bool Run() {
std::vector<OvsdbVlanPortBindingEntry> bindings;
OvsdbClient *ovsdb_client = Agent::GetInstance()->ovsdb_client();
OvsdbClientSession *session;
if (ip_.empty()) {
session = ovsdb_client->NextSession(NULL);
} else {
boost::system::error_code ec;
Ip4Address ip_addr = Ip4Address::from_string(ip_, ec);
session = ovsdb_client->FindSession(ip_addr, port_);
}
if (session != NULL && session->client_idl() != NULL) {
VlanPortBindingTable *table =
session->client_idl()->vlan_port_table();
VlanPortBindingEntry *entry =
static_cast<VlanPortBindingEntry *>(table->Next(NULL));
while (entry != NULL) {
OvsdbVlanPortBindingEntry oentry;
oentry.set_state(entry->StateString());
oentry.set_physical_port(entry->physical_port_name());
oentry.set_physical_device(entry->physical_device_name());
oentry.set_logical_switch(entry->logical_switch_name());
oentry.set_vlan(entry->vlan());
bindings.push_back(oentry);
entry = static_cast<VlanPortBindingEntry *>(table->Next(entry));
}
}
resp_->set_bindings(bindings);
SendResponse();
return true;
}
private:
void SendResponse() {
resp_->set_context(resp_data_);
resp_->set_more(false);
resp_->Response();
}
OvsdbVlanPortBindingResp *resp_;
std::string resp_data_;
std::string ip_;
uint32_t port_;
DISALLOW_COPY_AND_ASSIGN(VlanPortBindingSandeshTask);
};
void OvsdbVlanPortBindingReq::HandleRequest() const {
VlanPortBindingSandeshTask *task =
new VlanPortBindingSandeshTask(context(), get_session_remote_ip(),
get_session_remote_port());
TaskScheduler *scheduler = TaskScheduler::GetInstance();
scheduler->Enqueue(task);
}