-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathsyncd_notifications.cpp
More file actions
705 lines (544 loc) · 19.9 KB
/
syncd_notifications.cpp
File metadata and controls
705 lines (544 loc) · 19.9 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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#include "syncd.h"
#include "sairedis.h"
#include <queue>
#include <memory>
#include <condition_variable>
void send_notification(
_In_ std::string op,
_In_ std::string data,
_In_ std::vector<swss::FieldValueTuple> &entry)
{
SWSS_LOG_ENTER();
SWSS_LOG_INFO("%s %s", op.c_str(), data.c_str());
notifications->send(op, data, entry);
SWSS_LOG_DEBUG("notification send successfull");
}
void send_notification(
_In_ std::string op,
_In_ std::string data)
{
SWSS_LOG_ENTER();
std::vector<swss::FieldValueTuple> entry;
send_notification(op, data, entry);
}
void process_on_switch_state_change(
_In_ sai_object_id_t switch_rid,
_In_ sai_switch_oper_status_t switch_oper_status)
{
SWSS_LOG_ENTER();
sai_object_id_t switch_vid = translate_rid_to_vid(switch_rid, SAI_NULL_OBJECT_ID);
std::string s = sai_serialize_switch_oper_status(switch_vid, switch_oper_status);
send_notification("switch_state_change", s);
}
sai_fdb_entry_type_t getFdbEntryType(
_In_ uint32_t count,
_In_ const sai_attribute_t *list)
{
for (uint32_t idx = 0; idx < count; idx++)
{
const sai_attribute_t &attr = list[idx];
if (attr.id == SAI_FDB_ENTRY_ATTR_TYPE)
{
return (sai_fdb_entry_type_t)attr.value.s32;
}
}
SWSS_LOG_WARN("unknown fdb entry type");
int ret = -1;
return (sai_fdb_entry_type_t)ret;
}
void redisPutFdbEntryToAsicView(
_In_ const sai_fdb_event_notification_data_t *fdb)
{
// NOTE: this fdb entry already contains translated RID to VID
std::vector<swss::FieldValueTuple> entry;
entry = SaiAttributeList::serialize_attr_list(
SAI_OBJECT_TYPE_FDB_ENTRY,
fdb->attr_count,
fdb->attr,
false);
sai_object_type_t objectType = SAI_OBJECT_TYPE_FDB_ENTRY;
std::string strObjectType = sai_serialize_object_type(objectType);
std::string strFdbEntry = sai_serialize_fdb_entry(fdb->fdb_entry);
std::string key = ASIC_STATE_TABLE + (":" + strObjectType + ":" + strFdbEntry);
if (fdb->fdb_entry.switch_id == SAI_NULL_OBJECT_ID ||
// fdb->fdb_entry.bridge_id == SAI_NULL_OBJECT_ID || // TODO later use bv_id
sai_metadata_get_fdb_entry_bridge_type_name(fdb->fdb_entry.bridge_type) == NULL)
{
SWSS_LOG_WARN("skipped to put int db: %s", strFdbEntry.c_str());
return;
}
if (fdb->event_type == SAI_FDB_EVENT_AGED)
{
SWSS_LOG_DEBUG("remove fdb entry %s for SAI_FDB_EVENT_AGED",key.c_str());
g_redisClient->del(key);
return;
}
if (fdb->event_type == SAI_FDB_EVENT_FLUSHED)
{
sai_vlan_id_t vlan_id = fdb->fdb_entry.vlan_id;
sai_object_id_t port_oid = 0;
bool port_oid_found = false;
for (uint32_t i = 0; i < fdb->attr_count; i++)
{
if(fdb->attr[i].id == SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID)
{
port_oid = fdb->attr[i].value.oid;
port_oid_found = true;
}
}
if (!port_oid_found)
{
SWSS_LOG_ERROR("Failed to get bridge port ID for FDB entry %s",strFdbEntry.c_str());
return;
}
if (!port_oid && !vlan_id)
{
/* we got a flush all fdb event here */
/* example of a flush all fdb event */
/*
[{
"fdb_entry":"{
\"bridge_id\":\"oid:0x0\",
\"bridge_type\":\"SAI_FDB_ENTRY_BRIDGE_TYPE_1Q\",
\"mac\":\"00:00:00:00:00:00\",
\"switch_id\":\"oid:0x21000000000000\",
\"vlan\":\"0\"}",
"fdb_event":"SAI_FDB_EVENT_FLUSHED",
"list":[
{"id":"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID","value":"oid:0x0"},
{"id":"SAI_FDB_ENTRY_ATTR_TYPE","value":"SAI_FDB_ENTRY_TYPE_DYNAMIC"},
{"id":"SAI_FDB_ENTRY_ATTR_PACKET_ACTION","value":"SAI_PACKET_ACTION_FORWARD"}
]
}]
*/
SWSS_LOG_NOTICE("received a flush all fdb event");
std::string pattern = ASIC_STATE_TABLE + std::string(":SAI_OBJECT_TYPE_FDB_ENTRY:*");
for (const auto &fdbkey: g_redisClient->keys(pattern))
{
/* we only remove dynamic fdb entries here, static fdb entries need to be deleted manually by user instead of flush */
auto pEntryType = g_redisClient->hget(fdbkey, "SAI_FDB_ENTRY_ATTR_TYPE");
if (pEntryType != NULL)
{
std::string strEntryType = *pEntryType;
if (strEntryType == "SAI_FDB_ENTRY_TYPE_DYNAMIC")
{
SWSS_LOG_DEBUG("remove fdb entry %s for SAI_FDB_EVENT_FLUSHED",fdbkey.c_str());
g_redisClient->del(fdbkey);
}
}
else
{
SWSS_LOG_ERROR("found unknown type fdb entry, key %s", fdbkey.c_str());
}
}
}
else if( port_oid && !vlan_id )
{
/* we got a flush port fdb event here */
/* not supported yet, this is a place holder */
/* example of a flush port fdb event */
/*
[{
"fdb_entry":"{
\"bridge_id\":\"oid:0x0\",
\"bridge_type\":\"SAI_FDB_ENTRY_BRIDGE_TYPE_1Q\",
\"mac\":\"00:00:00:00:00:00\",
\"switch_id\":\"oid:0x21000000000000\",
\"vlan\":\"0\"}",
"fdb_event":"SAI_FDB_EVENT_FLUSHED",
"list":[
{"id":"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID","value":"oid:0x3a0000000009cf"},
{"id":"SAI_FDB_ENTRY_ATTR_TYPE","value":"SAI_FDB_ENTRY_TYPE_DYNAMIC"},
{"id":"SAI_FDB_ENTRY_ATTR_PACKET_ACTION","value":"SAI_PACKET_ACTION_FORWARD"}
]
}]
*/
SWSS_LOG_ERROR("received a flush port fdb event, port_oid = %lu, vlan_id = %hd, unsupported", port_oid, vlan_id);
}
else if(!port_oid && vlan_id)
{
/* we got a flush vlan fdb event here */
/* not supported yet, this is a place holder */
/* example of a flush vlan event */
/*
[{
"fdb_entry":"{
\"bridge_id\":\"oid:0x0\",
\"bridge_type\":\"SAI_FDB_ENTRY_BRIDGE_TYPE_1Q\",
\"mac\":\"00:00:00:00:00:00\",
\"switch_id\":\"oid:0x21000000000000\",
\"vlan\":\"1000\"}",
"fdb_event":"SAI_FDB_EVENT_FLUSHED",
"list":[
{"id":"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID","value":"oid:0x0"},
{"id":"SAI_FDB_ENTRY_ATTR_TYPE","value":"SAI_FDB_ENTRY_TYPE_DYNAMIC"},
{"id":"SAI_FDB_ENTRY_ATTR_PACKET_ACTION","value":"SAI_PACKET_ACTION_FORWARD"}
]
}]
*/
SWSS_LOG_ERROR("received a flush vlan fdb event, port_oid = %lu, vlan_id = %hd, unsupported", port_oid, vlan_id);
}
else
{
SWSS_LOG_ERROR("received a flush fdb event, port_oid = %lu, vlan_id = %hd, unsupported", port_oid, vlan_id);
}
return;
}
for (const auto &e: entry)
{
const std::string &strField = fvField(e);
const std::string &strValue = fvValue(e);
g_redisClient->hset(key, strField, strValue);
}
// currently we need to add type manually since fdb event don't contain type
sai_attribute_t attr;
attr.id = SAI_FDB_ENTRY_ATTR_TYPE;
attr.value.s32 = SAI_FDB_ENTRY_TYPE_DYNAMIC;
auto meta = sai_metadata_get_attr_metadata(objectType, attr.id);
if (meta == NULL)
{
SWSS_LOG_THROW("unable to get metadata for object type %s, attribute %d",
sai_serialize_object_type(objectType).c_str(),
attr.id);
/*
* TODO We should notify orch agent here. And also this probably should
* not be here, but on redis side, getting through metadata.
*/
}
std::string strAttrId = sai_serialize_attr_id(*meta);
std::string strAttrValue = sai_serialize_attr_value(*meta, attr);
g_redisClient->hset(key, strAttrId, strAttrValue);
}
void process_on_fdb_event(
_In_ uint32_t count,
_In_ sai_fdb_event_notification_data_t *data)
{
SWSS_LOG_ENTER();
SWSS_LOG_DEBUG("fdb event count: %d", count);
for (uint32_t i = 0; i < count; i++)
{
sai_fdb_event_notification_data_t *fdb = &data[i];
SWSS_LOG_DEBUG("fdb %u: type: %d", i, fdb->event_type);
fdb->fdb_entry.switch_id = translate_rid_to_vid(fdb->fdb_entry.switch_id, SAI_NULL_OBJECT_ID);
// TODO later it should be bv_id
fdb->fdb_entry.bridge_id = translate_rid_to_vid(fdb->fdb_entry.bridge_id, fdb->fdb_entry.switch_id);
translate_rid_to_vid_list(SAI_OBJECT_TYPE_FDB_ENTRY, fdb->fdb_entry.switch_id, fdb->attr_count, fdb->attr);
/*
* Currently because of bcrm bug, we need to install fdb entries in
* asic view and currently this event don't have fdb type which is
* required on creation.
*/
redisPutFdbEntryToAsicView(fdb);
}
std::string s = sai_serialize_fdb_event_ntf(count, data);
send_notification("fdb_event", s);
}
void process_on_port_state_change(
_In_ uint32_t count,
_In_ sai_port_oper_status_notification_t *data)
{
SWSS_LOG_ENTER();
SWSS_LOG_DEBUG("port notification count: %u", count);
for (uint32_t i = 0; i < count; i++)
{
sai_port_oper_status_notification_t *oper_stat = &data[i];
/*
* We are using switch_rid as null, since port should be already
* defined inside local db after creation.
*
* If this will be faster than return from create port then we can use
* query switch id and extract rid of switch id and then convert it to
* switch vid.
*/
oper_stat->port_id = translate_rid_to_vid(oper_stat->port_id, SAI_NULL_OBJECT_ID);
}
std::string s = sai_serialize_port_oper_status_ntf(count, data);
send_notification("port_state_change", s);
}
void process_on_switch_shutdown_request(
_In_ sai_object_id_t switch_rid)
{
SWSS_LOG_ENTER();
sai_object_id_t switch_vid = translate_rid_to_vid(switch_rid, SAI_NULL_OBJECT_ID);
std::string s = sai_serialize_switch_shutdown_request(switch_vid);
send_notification("switch_shutdown_request", s);
}
void handle_switch_state_change(
_In_ const std::string &data)
{
SWSS_LOG_ENTER();
sai_switch_oper_status_t switch_oper_status;
sai_object_id_t switch_id;
sai_deserialize_switch_oper_status(data, switch_id, switch_oper_status);
process_on_switch_state_change(switch_id, switch_oper_status);
}
void handle_fdb_event(
_In_ const std::string &data)
{
SWSS_LOG_ENTER();
uint32_t count;
sai_fdb_event_notification_data_t *fdbevent = NULL;
sai_deserialize_fdb_event_ntf(data, count, &fdbevent);
process_on_fdb_event(count, fdbevent);
sai_deserialize_free_fdb_event_ntf(count, fdbevent);
}
void handle_port_state_change(
_In_ const std::string &data)
{
SWSS_LOG_ENTER();
uint32_t count;
sai_port_oper_status_notification_t *portoperstatus = NULL;
sai_deserialize_port_oper_status_ntf(data, count, &portoperstatus);
process_on_port_state_change(count, portoperstatus);
sai_deserialize_free_port_oper_status_ntf(count, portoperstatus);
}
void handle_switch_shutdown_request(
_In_ const std::string &data)
{
SWSS_LOG_ENTER();
sai_object_id_t switch_id;
sai_deserialize_switch_shutdown_request(data, switch_id);
process_on_switch_shutdown_request(switch_id);
}
void processNotification(
_In_ const swss::KeyOpFieldsValuesTuple &item)
{
std::lock_guard<std::mutex> lock(g_mutex);
SWSS_LOG_ENTER();
std::string notification = kfvKey(item);
std::string data = kfvOp(item);
if (notification == "switch_state_change")
{
handle_switch_state_change(data);
}
else if (notification == "fdb_event")
{
handle_fdb_event(data);
}
else if (notification == "port_state_change")
{
handle_port_state_change(data);
}
else if (notification == "switch_shutdown_request")
{
handle_switch_shutdown_request(data);
}
else
{
SWSS_LOG_ERROR("unknow notification: %s", notification.c_str());
}
}
// condition variable will be used to notify processing thread
// that some notiffication arrived
std::condition_variable cv;
std::mutex queue_mutex;
std::queue<swss::KeyOpFieldsValuesTuple> ntf_queue;
void enqueue_notification(
_In_ std::string op,
_In_ std::string data,
_In_ std::vector<swss::FieldValueTuple> &entry)
{
SWSS_LOG_ENTER();
SWSS_LOG_INFO("%s %s", op.c_str(), data.c_str());
swss::KeyOpFieldsValuesTuple item(op, data, entry);
// this is notification context, so we need to protect queue
std::lock_guard<std::mutex> lock(queue_mutex);
ntf_queue.push(item);
cv.notify_all();
}
void enqueue_notification(
_In_ std::string op,
_In_ std::string data)
{
SWSS_LOG_ENTER();
std::vector<swss::FieldValueTuple> entry;
enqueue_notification(op, data, entry);
}
void on_switch_state_change(
_In_ sai_object_id_t switch_id,
_In_ sai_switch_oper_status_t switch_oper_status)
{
SWSS_LOG_ENTER();
std::string s = sai_serialize_switch_oper_status(switch_id, switch_oper_status);
enqueue_notification("switch_state_change", s);
}
void on_fdb_event(
_In_ uint32_t count,
_In_ sai_fdb_event_notification_data_t *data)
{
SWSS_LOG_ENTER();
std::string s = sai_serialize_fdb_event_ntf(count, data);
enqueue_notification("fdb_event", s);
}
void on_port_state_change(
_In_ uint32_t count,
_In_ sai_port_oper_status_notification_t *data)
{
SWSS_LOG_ENTER();
std::string s = sai_serialize_port_oper_status_ntf(count, data);
enqueue_notification("port_state_change", s);
}
void on_switch_shutdown_request(
_In_ sai_object_id_t switch_id)
{
SWSS_LOG_ENTER();
std::string s = sai_serialize_switch_shutdown_request(switch_id);
enqueue_notification("switch_shutdown_request", "");
}
void on_packet_event(
_In_ sai_object_id_t switch_id,
_In_ const void *buffer,
_In_ sai_size_t buffer_size,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
SWSS_LOG_ERROR("not implemented");
}
// determine whether notification thread is running
volatile bool runThread;
std::mutex ntf_mutex;
std::unique_lock<std::mutex> ulock(ntf_mutex);
bool tryDequeue(
_Out_ swss::KeyOpFieldsValuesTuple &item)
{
std::lock_guard<std::mutex> lock(queue_mutex);
SWSS_LOG_ENTER();
if (ntf_queue.empty())
{
return false;
}
item = ntf_queue.front();
ntf_queue.pop();
return true;
}
void ntf_process_function()
{
SWSS_LOG_ENTER();
while (runThread)
{
cv.wait(ulock);
// this is notifications processing thread context, which is different
// from SAI notifications context, we can safe use g_mutex here,
// processing each notification is under same mutex as processing main
// events, counters and reinit
swss::KeyOpFieldsValuesTuple item;
while (tryDequeue(item))
{
processNotification(item);
}
}
}
std::shared_ptr<std::thread> ntf_process_thread;
void startNotificationsProcessingThread()
{
SWSS_LOG_ENTER();
runThread = true;
ntf_process_thread = std::make_shared<std::thread>(ntf_process_function);
ntf_process_thread->detach();
}
void stopNotificationsProcessingThread()
{
SWSS_LOG_ENTER();
runThread = false;
cv.notify_all();
if (ntf_process_thread != nullptr)
{
ntf_process_thread->join();
}
ntf_process_thread = nullptr;
}
void on_queue_deadlock(
_In_ uint32_t count,
_In_ sai_queue_deadlock_notification_data_t *data)
{
std::lock_guard<std::mutex> lock(g_mutex);
SWSS_LOG_ENTER();
SWSS_LOG_DEBUG("queue deadlock notification count: %u", count);
for (uint32_t i = 0; i < count; i++)
{
sai_queue_deadlock_notification_data_t *deadlock_data = &data[i];
/*
* We are using switch_rid as null, since queue should be already
* defined inside local db after creation.
*
* If this will be faster than return from create queue then we can use
* query switch id and extract rid of switch id and then convert it to
* switch vid.
*/
deadlock_data->queue_id = translate_rid_to_vid(deadlock_data->queue_id, SAI_NULL_OBJECT_ID);
}
std::string s = sai_serialize_queue_deadlock_ntf(count, data);
send_notification("queue_deadlock", s);
}
sai_switch_state_change_notification_fn on_switch_state_change_ntf = on_switch_state_change;
sai_switch_shutdown_request_notification_fn on_switch_shutdown_request_ntf = on_switch_shutdown_request;
sai_fdb_event_notification_fn on_fdb_event_ntf = on_fdb_event;
sai_port_state_change_notification_fn on_port_state_change_ntf = on_port_state_change;
sai_packet_event_notification_fn on_packet_event_ntf = on_packet_event;
sai_queue_pfc_deadlock_notification_fn on_queue_deadlock_ntf = on_queue_deadlock;
void check_notifications_pointers(
_In_ uint32_t attr_count,
_In_ sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
/*
* This function should only be called on CREATE/SET api when object is
* SWITCH.
*
* Notifications pointers needs to be corrected since those we receive from
* sairedis are in sairedis memory space and here we are using those ones
* we declared in syncd memory space.
*
* Also notice that we are using the same pointers for ALL switches.
*/
for (uint32_t index = 0; index < attr_count; ++index)
{
sai_attribute_t &attr = attr_list[index];
auto meta = sai_metadata_get_attr_metadata(SAI_OBJECT_TYPE_SWITCH, attr.id);
if (meta->attrvaluetype != SAI_ATTR_VALUE_TYPE_POINTER)
{
continue;
}
/*
* Does not matter if pointer is valid or not, we just want the
* previous value.
*/
sai_pointer_t prev = attr.value.ptr;
if (prev == NULL)
{
/*
* If pointer is NULL, then fine, let it be.
*/
continue;
}
switch (attr.id)
{
case SAI_SWITCH_ATTR_SWITCH_STATE_CHANGE_NOTIFY:
attr.value.ptr = (void*)on_switch_state_change_ntf;
break;
case SAI_SWITCH_ATTR_SHUTDOWN_REQUEST_NOTIFY:
attr.value.ptr = (void*)on_switch_shutdown_request_ntf;
break;
case SAI_SWITCH_ATTR_FDB_EVENT_NOTIFY:
attr.value.ptr = (void*)on_fdb_event_ntf;
break;
case SAI_SWITCH_ATTR_PORT_STATE_CHANGE_NOTIFY:
attr.value.ptr = (void*)on_port_state_change_ntf;
break;
case SAI_SWITCH_ATTR_PACKET_EVENT_NOTIFY:
attr.value.ptr = (void*)on_packet_event_ntf;
break;
case SAI_SWITCH_ATTR_QUEUE_PFC_DEADLOCK_NOTIFY:
attr.value.ptr = (void*)on_queue_deadlock_ntf;
break;
default:
SWSS_LOG_ERROR("pointer for %s is not handled, FIXME!", meta->attridname);
continue;
}
/*
* Here we translated pointer, just log it.
*/
SWSS_LOG_INFO("%s: %lp (orch) => %lp (syncd)", meta->attridname, prev, attr.value.ptr);
}
}