forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfdborch.cpp
More file actions
547 lines (448 loc) · 16 KB
/
Copy pathfdborch.cpp
File metadata and controls
547 lines (448 loc) · 16 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include <assert.h>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <utility>
#include "logger.h"
#include "tokenize.h"
#include "fdborch.h"
#include "crmorch.h"
#include "notifier.h"
#include "sai_serialize.h"
extern sai_fdb_api_t *sai_fdb_api;
extern sai_object_id_t gSwitchId;
extern PortsOrch* gPortsOrch;
extern CrmOrch * gCrmOrch;
const int fdborch_pri = 20;
FdbOrch::FdbOrch(TableConnector applDbConnector, TableConnector stateDbConnector, PortsOrch *port) :
Orch(applDbConnector.first, applDbConnector.second, fdborch_pri),
m_portsOrch(port),
m_table(applDbConnector.first, applDbConnector.second),
m_fdbStateTable(stateDbConnector.first, stateDbConnector.second)
{
m_portsOrch->attach(this);
m_flushNotificationsConsumer = new NotificationConsumer(applDbConnector.first, "FLUSHFDBREQUEST");
auto flushNotifier = new Notifier(m_flushNotificationsConsumer, this, "FLUSHFDBREQUEST");
Orch::addExecutor(flushNotifier);
/* Add FDB notifications support from ASIC */
DBConnector *notificationsDb = new DBConnector(ASIC_DB, DBConnector::DEFAULT_UNIXSOCKET, 0);
m_fdbNotificationConsumer = new swss::NotificationConsumer(notificationsDb, "NOTIFICATIONS");
auto fdbNotifier = new Notifier(m_fdbNotificationConsumer, this, "FDB_NOTIFICATIONS");
Orch::addExecutor(fdbNotifier);
}
bool FdbOrch::bake()
{
Orch::bake();
auto consumer = dynamic_cast<Consumer *>(getExecutor(APP_FDB_TABLE_NAME));
if (consumer == NULL)
{
SWSS_LOG_ERROR("No consumer %s in Orch", APP_FDB_TABLE_NAME);
return false;
}
size_t refilled = consumer->refillToSync(&m_fdbStateTable);
SWSS_LOG_NOTICE("Add warm input FDB State: %s, %zd", APP_FDB_TABLE_NAME, refilled);
return true;
}
bool FdbOrch::storeFdbEntryState(const FdbUpdate& update)
{
const FdbEntry& entry = update.entry;
const Port& port = update.port;
sai_vlan_id_t vlan_id = port.m_port_vlan_id;
const MacAddress& mac = entry.mac;
string portName = port.m_alias;
// ref: https://github.com/Azure/sonic-swss/blob/master/doc/swss-schema.md#fdb_table
string key = "Vlan" + to_string(vlan_id) + ":" + mac.to_string();
if (update.add)
{
auto inserted = m_entries.insert(entry);
SWSS_LOG_DEBUG("FdbOrch notification: mac %s was inserted into bv_id 0x%lx",
entry.mac.to_string().c_str(), entry.bv_id);
if (!inserted.second)
{
SWSS_LOG_INFO("FdbOrch notification: mac %s is duplicate", entry.mac.to_string().c_str());
return false;
}
// Write to StateDb
std::vector<FieldValueTuple> fvs;
fvs.push_back(FieldValueTuple("port", portName));
fvs.push_back(FieldValueTuple("type", "dynamic"));
m_fdbStateTable.set(key, fvs);
gCrmOrch->incCrmResUsedCounter(CrmResourceType::CRM_FDB_ENTRY);
return true;
}
else
{
size_t erased = m_entries.erase(entry);
SWSS_LOG_DEBUG("FdbOrch notification: mac %s was removed from bv_id 0x%lx", entry.mac.to_string().c_str(), entry.bv_id);
if (erased == 0)
{
return false;
}
// Remove in StateDb
m_fdbStateTable.del(key);
gCrmOrch->decCrmResUsedCounter(CrmResourceType::CRM_FDB_ENTRY);
return true;
}
}
void FdbOrch::update(sai_fdb_event_t type, const sai_fdb_entry_t* entry, sai_object_id_t bridge_port_id)
{
SWSS_LOG_ENTER();
FdbUpdate update;
update.entry.mac = entry->mac_address;
update.entry.bv_id = entry->bv_id;
switch (type)
{
case SAI_FDB_EVENT_LEARNED:
if (!m_portsOrch->getPortByBridgePortId(bridge_port_id, update.port))
{
SWSS_LOG_ERROR("Failed to get port by bridge port ID 0x%lx", bridge_port_id);
return;
}
// we already have such entries
if (m_entries.find(update.entry) != m_entries.end())
{
SWSS_LOG_INFO("FdbOrch notification: mac %s is already in bv_id 0x%lx",
update.entry.mac.to_string().c_str(), entry->bv_id);
break;
}
update.add = true;
storeFdbEntryState(update);
for (auto observer: m_observers)
{
observer->update(SUBJECT_TYPE_FDB_CHANGE, &update);
}
break;
case SAI_FDB_EVENT_AGED:
case SAI_FDB_EVENT_MOVE:
update.add = false;
storeFdbEntryState(update);
for (auto observer: m_observers)
{
observer->update(SUBJECT_TYPE_FDB_CHANGE, &update);
}
break;
case SAI_FDB_EVENT_FLUSHED:
if (bridge_port_id == SAI_NULL_OBJECT_ID && entry->bv_id == SAI_NULL_OBJECT_ID)
{
for (auto itr = m_entries.begin(); itr != m_entries.end();)
{
/*
TODO: here should only delete the dynamic fdb entries,
but unfortunately in structure FdbEntry currently have
no member to indicate the fdb entry type,
if there is static mac added, here will have issue.
*/
update.entry.mac = itr->mac;
update.entry.bv_id = itr->bv_id;
update.add = false;
itr++;
storeFdbEntryState(update);
SWSS_LOG_DEBUG("FdbOrch notification: mac %s was removed", update.entry.mac.to_string().c_str());
for (auto observer: m_observers)
{
observer->update(SUBJECT_TYPE_FDB_CHANGE, &update);
}
}
}
else if (bridge_port_id && entry->bv_id == SAI_NULL_OBJECT_ID)
{
/*this is a placeholder for flush port fdb case, not supported yet.*/
SWSS_LOG_ERROR("FdbOrch notification: not supported flush port fdb action, port_id = 0x%lx, bv_id = 0x%lx.", bridge_port_id, entry->bv_id);
}
else if (bridge_port_id == SAI_NULL_OBJECT_ID && entry->bv_id != SAI_NULL_OBJECT_ID)
{
/*this is a placeholder for flush vlan fdb case, not supported yet.*/
SWSS_LOG_ERROR("FdbOrch notification: not supported flush vlan fdb action, port_id = 0x%lx, bv_id = 0x%lx.", bridge_port_id, entry->bv_id);
}
else
{
SWSS_LOG_ERROR("FdbOrch notification: not supported flush fdb action, port_id = 0x%lx, bv_id = 0x%lx.", bridge_port_id, entry->bv_id);
}
break;
}
return;
}
void FdbOrch::update(SubjectType type, void *cntx)
{
SWSS_LOG_ENTER();
assert(cntx);
switch(type) {
case SUBJECT_TYPE_VLAN_MEMBER_CHANGE:
{
VlanMemberUpdate *update = reinterpret_cast<VlanMemberUpdate *>(cntx);
updateVlanMember(*update);
break;
}
default:
break;
}
return;
}
bool FdbOrch::getPort(const MacAddress& mac, uint16_t vlan, Port& port)
{
SWSS_LOG_ENTER();
if (!m_portsOrch->getVlanByVlanId(vlan, port))
{
SWSS_LOG_ERROR("Failed to get vlan by vlan ID %d", vlan);
return false;
}
sai_fdb_entry_t entry;
entry.switch_id = gSwitchId;
memcpy(entry.mac_address, mac.getMac(), sizeof(sai_mac_t));
entry.bv_id = port.m_vlan_info.vlan_oid;
sai_attribute_t attr;
attr.id = SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID;
sai_status_t status = sai_fdb_api->get_fdb_entry_attribute(&entry, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get bridge port ID for FDB entry %s, rv:%d",
mac.to_string().c_str(), status);
return false;
}
if (!m_portsOrch->getPortByBridgePortId(attr.value.oid, port))
{
SWSS_LOG_ERROR("Failed to get port by bridge port ID 0x%lx", attr.value.oid);
return false;
}
return true;
}
void FdbOrch::doTask(Consumer& consumer)
{
SWSS_LOG_ENTER();
if (!gPortsOrch->isPortReady())
{
return;
}
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;
/* format: <VLAN_name>:<MAC_address> */
vector<string> keys = tokenize(kfvKey(t), ':', 1);
string op = kfvOp(t);
Port vlan;
if (!m_portsOrch->getPort(keys[0], vlan))
{
SWSS_LOG_INFO("Failed to locate %s", keys[0].c_str());
it++;
continue;
}
FdbEntry entry;
entry.mac = MacAddress(keys[1]);
entry.bv_id = vlan.m_vlan_info.vlan_oid;
if (op == SET_COMMAND)
{
string port;
string type;
for (auto i : kfvFieldsValues(t))
{
if (fvField(i) == "port")
{
port = fvValue(i);
}
if (fvField(i) == "type")
{
type = fvValue(i);
}
}
/* FDB type is either dynamic or static */
assert(type == "dynamic" || type == "static");
if (addFdbEntry(entry, port, type))
it = consumer.m_toSync.erase(it);
else
it++;
/* Remove corresponding APP_DB entry if type is 'dynamic' */
// FIXME: The modification of table is not thread safe.
// Uncomment this after this issue is fixed.
// if (type == "dynamic")
// {
// m_table.del(kfvKey(t));
// }
}
else if (op == DEL_COMMAND)
{
if (removeFdbEntry(entry))
it = consumer.m_toSync.erase(it);
else
it++;
}
else
{
SWSS_LOG_ERROR("Unknown operation type %s", op.c_str());
it = consumer.m_toSync.erase(it);
}
}
}
void FdbOrch::doTask(NotificationConsumer& consumer)
{
SWSS_LOG_ENTER();
if (!gPortsOrch->isPortReady())
{
return;
}
sai_status_t status;
std::string op;
std::string data;
std::vector<swss::FieldValueTuple> values;
consumer.pop(op, data, values);
if (&consumer == m_flushNotificationsConsumer)
{
if (op == "ALL")
{
/*
* so far only support flush all the FDB entris
* flush per port and flush per vlan will be added later.
*/
status = sai_fdb_api->flush_fdb_entries(gSwitchId, 0, NULL);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Flush fdb failed, return code %x", status);
}
return;
}
else if (op == "PORT")
{
/*place holder for flush port fdb*/
SWSS_LOG_ERROR("Received unsupported flush port fdb request");
return;
}
else if (op == "VLAN")
{
/*place holder for flush vlan fdb*/
SWSS_LOG_ERROR("Received unsupported flush vlan fdb request");
return;
}
else
{
SWSS_LOG_ERROR("Received unknown flush fdb request");
return;
}
}
else if (&consumer == m_fdbNotificationConsumer && op == "fdb_event")
{
uint32_t count;
sai_fdb_event_notification_data_t *fdbevent = nullptr;
sai_deserialize_fdb_event_ntf(data, count, &fdbevent);
for (uint32_t i = 0; i < count; ++i)
{
sai_object_id_t oid = SAI_NULL_OBJECT_ID;
for (uint32_t j = 0; j < fdbevent[i].attr_count; ++j)
{
if (fdbevent[i].attr[j].id == SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID)
{
oid = fdbevent[i].attr[j].value.oid;
break;
}
}
this->update(fdbevent[i].event_type, &fdbevent[i].fdb_entry, oid);
}
sai_deserialize_free_fdb_event_ntf(count, fdbevent);
}
}
void FdbOrch::updateVlanMember(const VlanMemberUpdate& update)
{
SWSS_LOG_ENTER();
if (!update.add)
{
return; // we need additions only
}
string port_name = update.member.m_alias;
auto fdb_list = std::move(saved_fdb_entries[port_name]);
if(!fdb_list.empty())
{
for (const auto& fdb: fdb_list)
{
// try to insert an FDB entry. If the FDB entry is not ready to be inserted yet,
// it would be added back to the saved_fdb_entries structure by addFDBEntry()
(void)addFdbEntry(fdb.entry, port_name, fdb.type);
}
}
}
bool FdbOrch::addFdbEntry(const FdbEntry& entry, const string& port_name, const string& type)
{
SWSS_LOG_ENTER();
if (m_entries.count(entry) != 0) // we already have such entries
{
// FIXME: should we check that the entry are moving to another port?
// FIXME: should we check that the entry are changing its type?
SWSS_LOG_ERROR("FDB entry already exists. mac=%s bv_id=0x%lx", entry.mac.to_string().c_str(), entry.bv_id);
return true;
}
sai_fdb_entry_t fdb_entry;
fdb_entry.switch_id = gSwitchId;
memcpy(fdb_entry.mac_address, entry.mac.getMac(), sizeof(sai_mac_t));
fdb_entry.bv_id = entry.bv_id;
Port port;
/* Retry until port is created */
if (!m_portsOrch->getPort(port_name, port))
{
SWSS_LOG_DEBUG("Saving a fdb entry until port %s becomes active", port_name.c_str());
saved_fdb_entries[port_name].push_back({entry, type});
return true;
}
/* Retry until port is added to the VLAN */
if (!port.m_bridge_port_id)
{
SWSS_LOG_DEBUG("Saving a fdb entry until port %s has got a bridge port ID", port_name.c_str());
saved_fdb_entries[port_name].push_back({entry, type});
return true;
}
sai_attribute_t attr;
vector<sai_attribute_t> attrs;
attr.id = SAI_FDB_ENTRY_ATTR_TYPE;
attr.value.s32 = (type == "dynamic") ? SAI_FDB_ENTRY_TYPE_DYNAMIC : SAI_FDB_ENTRY_TYPE_STATIC;
attrs.push_back(attr);
attr.id = SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID;
attr.value.oid = port.m_bridge_port_id;
attrs.push_back(attr);
attr.id = SAI_FDB_ENTRY_ATTR_PACKET_ACTION;
attr.value.s32 = SAI_PACKET_ACTION_FORWARD;
attrs.push_back(attr);
sai_status_t status = sai_fdb_api->create_fdb_entry(&fdb_entry, (uint32_t)attrs.size(), attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create %s FDB %s on %s, rv:%d",
type.c_str(), entry.mac.to_string().c_str(), port_name.c_str(), status);
return false; //FIXME: it should be based on status. Some could be retried, some not
}
SWSS_LOG_NOTICE("Create %s FDB %s on %s", type.c_str(), entry.mac.to_string().c_str(), port_name.c_str());
(void) m_entries.insert(entry);
gCrmOrch->incCrmResUsedCounter(CrmResourceType::CRM_FDB_ENTRY);
FdbUpdate update = {entry, port, true};
for (auto observer: m_observers)
{
observer->update(SUBJECT_TYPE_FDB_CHANGE, &update);
}
return true;
}
bool FdbOrch::removeFdbEntry(const FdbEntry& entry)
{
SWSS_LOG_ENTER();
if (m_entries.count(entry) == 0)
{
SWSS_LOG_ERROR("FDB entry isn't found. mac=%s bv_id=0x%lx", entry.mac.to_string().c_str(), entry.bv_id);
return true;
}
sai_status_t status;
sai_fdb_entry_t fdb_entry;
fdb_entry.switch_id = gSwitchId;
memcpy(fdb_entry.mac_address, entry.mac.getMac(), sizeof(sai_mac_t));
fdb_entry.bv_id = entry.bv_id;
status = sai_fdb_api->remove_fdb_entry(&fdb_entry);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove FDB entry. mac=%s, bv_id=0x%lx",
entry.mac.to_string().c_str(), entry.bv_id);
return true; //FIXME: it should be based on status. Some could be retried. some not
}
(void)m_entries.erase(entry);
gCrmOrch->decCrmResUsedCounter(CrmResourceType::CRM_FDB_ENTRY);
Port port;
m_portsOrch->getPortByBridgePortId(entry.bv_id, port);
FdbUpdate update = {entry, port, false};
for (auto observer: m_observers)
{
observer->update(SUBJECT_TYPE_FDB_CHANGE, &update);
}
return true;
}