-
Notifications
You must be signed in to change notification settings - Fork 543
/
copporch.cpp
869 lines (761 loc) · 31.5 KB
/
copporch.cpp
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
#include "sai.h"
#include "copporch.h"
#include "portsorch.h"
#include "tokenize.h"
#include "logger.h"
#include <inttypes.h>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace swss;
using namespace std;
extern sai_hostif_api_t* sai_hostif_api;
extern sai_policer_api_t* sai_policer_api;
extern sai_switch_api_t* sai_switch_api;
extern sai_object_id_t gSwitchId;
extern PortsOrch* gPortsOrch;
extern bool gIsNatSupported;
static map<string, sai_meter_type_t> policer_meter_map = {
{"packets", SAI_METER_TYPE_PACKETS},
{"bytes", SAI_METER_TYPE_BYTES}
};
static map<string, sai_policer_mode_t> policer_mode_map = {
{"sr_tcm", SAI_POLICER_MODE_SR_TCM},
{"tr_tcm", SAI_POLICER_MODE_TR_TCM},
{"storm", SAI_POLICER_MODE_STORM_CONTROL}
};
static map<string, sai_policer_color_source_t> policer_color_aware_map = {
{"aware", SAI_POLICER_COLOR_SOURCE_AWARE},
{"blind", SAI_POLICER_COLOR_SOURCE_BLIND}
};
static map<string, sai_hostif_trap_type_t> trap_id_map = {
{"stp", SAI_HOSTIF_TRAP_TYPE_STP},
{"lacp", SAI_HOSTIF_TRAP_TYPE_LACP},
{"eapol", SAI_HOSTIF_TRAP_TYPE_EAPOL},
{"lldp", SAI_HOSTIF_TRAP_TYPE_LLDP},
{"pvrst", SAI_HOSTIF_TRAP_TYPE_PVRST},
{"igmp_query", SAI_HOSTIF_TRAP_TYPE_IGMP_TYPE_QUERY},
{"igmp_leave", SAI_HOSTIF_TRAP_TYPE_IGMP_TYPE_LEAVE},
{"igmp_v1_report", SAI_HOSTIF_TRAP_TYPE_IGMP_TYPE_V1_REPORT},
{"igmp_v2_report", SAI_HOSTIF_TRAP_TYPE_IGMP_TYPE_V2_REPORT},
{"igmp_v3_report", SAI_HOSTIF_TRAP_TYPE_IGMP_TYPE_V3_REPORT},
{"sample_packet", SAI_HOSTIF_TRAP_TYPE_SAMPLEPACKET},
{"switch_cust_range", SAI_HOSTIF_TRAP_TYPE_SWITCH_CUSTOM_RANGE_BASE},
{"arp_req", SAI_HOSTIF_TRAP_TYPE_ARP_REQUEST},
{"arp_resp", SAI_HOSTIF_TRAP_TYPE_ARP_RESPONSE},
{"dhcp", SAI_HOSTIF_TRAP_TYPE_DHCP},
{"ospf", SAI_HOSTIF_TRAP_TYPE_OSPF},
{"pim", SAI_HOSTIF_TRAP_TYPE_PIM},
{"vrrp", SAI_HOSTIF_TRAP_TYPE_VRRP},
{"bgp", SAI_HOSTIF_TRAP_TYPE_BGP},
{"dhcpv6", SAI_HOSTIF_TRAP_TYPE_DHCPV6},
{"ospfv6", SAI_HOSTIF_TRAP_TYPE_OSPFV6},
{"vrrpv6", SAI_HOSTIF_TRAP_TYPE_VRRPV6},
{"bgpv6", SAI_HOSTIF_TRAP_TYPE_BGPV6},
{"neigh_discovery", SAI_HOSTIF_TRAP_TYPE_IPV6_NEIGHBOR_DISCOVERY},
{"mld_v1_v2", SAI_HOSTIF_TRAP_TYPE_IPV6_MLD_V1_V2},
{"mld_v1_report", SAI_HOSTIF_TRAP_TYPE_IPV6_MLD_V1_REPORT},
{"mld_v1_done", SAI_HOSTIF_TRAP_TYPE_IPV6_MLD_V1_DONE},
{"mld_v2_report", SAI_HOSTIF_TRAP_TYPE_MLD_V2_REPORT},
{"ip2me", SAI_HOSTIF_TRAP_TYPE_IP2ME},
{"ssh", SAI_HOSTIF_TRAP_TYPE_SSH},
{"snmp", SAI_HOSTIF_TRAP_TYPE_SNMP},
{"router_custom_range", SAI_HOSTIF_TRAP_TYPE_ROUTER_CUSTOM_RANGE_BASE},
{"l3_mtu_error", SAI_HOSTIF_TRAP_TYPE_L3_MTU_ERROR},
{"ttl_error", SAI_HOSTIF_TRAP_TYPE_TTL_ERROR},
{"udld", SAI_HOSTIF_TRAP_TYPE_UDLD},
{"bfd", SAI_HOSTIF_TRAP_TYPE_BFD},
{"bfdv6", SAI_HOSTIF_TRAP_TYPE_BFDV6},
{"src_nat_miss", SAI_HOSTIF_TRAP_TYPE_SNAT_MISS},
{"dest_nat_miss", SAI_HOSTIF_TRAP_TYPE_DNAT_MISS}
};
static map<string, sai_packet_action_t> packet_action_map = {
{"drop", SAI_PACKET_ACTION_DROP},
{"forward", SAI_PACKET_ACTION_FORWARD},
{"copy", SAI_PACKET_ACTION_COPY},
{"copy_cancel", SAI_PACKET_ACTION_COPY_CANCEL},
{"trap", SAI_PACKET_ACTION_TRAP},
{"log", SAI_PACKET_ACTION_LOG},
{"deny", SAI_PACKET_ACTION_DENY},
{"transit", SAI_PACKET_ACTION_TRANSIT}
};
const string default_trap_group = "default";
const vector<sai_hostif_trap_type_t> default_trap_ids = {
SAI_HOSTIF_TRAP_TYPE_TTL_ERROR
};
CoppOrch::CoppOrch(vector<TableConnector> &tableConnectors) :
Orch( tableConnectors)
{
SWSS_LOG_ENTER();
initDefaultHostIntfTable();
initDefaultTrapGroup();
initDefaultTrapIds();
enable_sflow_trap = false;
};
void CoppOrch::initDefaultHostIntfTable()
{
SWSS_LOG_ENTER();
sai_object_id_t default_hostif_table_id;
vector<sai_attribute_t> attrs;
sai_attribute_t attr;
attr.id = SAI_HOSTIF_TABLE_ENTRY_ATTR_TYPE;
attr.value.s32 = SAI_HOSTIF_TABLE_ENTRY_TYPE_WILDCARD;
attrs.push_back(attr);
attr.id = SAI_HOSTIF_TABLE_ENTRY_ATTR_CHANNEL_TYPE;
attr.value.s32 = SAI_HOSTIF_TABLE_ENTRY_CHANNEL_TYPE_NETDEV_PHYSICAL_PORT;
attrs.push_back(attr);
sai_status_t status = sai_hostif_api->create_hostif_table_entry(
&default_hostif_table_id, gSwitchId, (uint32_t)attrs.size(), attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create default host interface table, rv:%d", status);
throw "CoppOrch initialization failure";
}
SWSS_LOG_NOTICE("Create default host interface table");
}
void CoppOrch::initDefaultTrapIds()
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
vector<sai_attribute_t> trap_id_attrs;
attr.id = SAI_HOSTIF_TRAP_ATTR_PACKET_ACTION;
attr.value.s32 = SAI_PACKET_ACTION_TRAP;
trap_id_attrs.push_back(attr);
attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_GROUP;
attr.value.oid = m_trap_group_map[default_trap_group];
trap_id_attrs.push_back(attr);
/* Mellanox platform doesn't support trap priority setting */
char *platform = getenv("platform");
if (!platform || !strstr(platform, MLNX_PLATFORM_SUBSTRING))
{
attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_PRIORITY;
attr.value.u32 = 0;
trap_id_attrs.push_back(attr);
}
if (!applyAttributesToTrapIds(m_trap_group_map[default_trap_group], default_trap_ids, trap_id_attrs))
{
SWSS_LOG_ERROR("Failed to set attributes to default trap IDs");
throw "CoppOrch initialization failure";
}
SWSS_LOG_INFO("Set attributes to default trap IDs");
}
void CoppOrch::initDefaultTrapGroup()
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
attr.id = SAI_SWITCH_ATTR_DEFAULT_TRAP_GROUP;
sai_status_t status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to get default trap group, rv:%d", status);
throw "CoppOrch initialization failure";
}
SWSS_LOG_INFO("Get default trap group");
m_trap_group_map[default_trap_group] = attr.value.oid;
}
void CoppOrch::getTrapIdList(vector<string> &trap_id_name_list, vector<sai_hostif_trap_type_t> &trap_id_list) const
{
SWSS_LOG_ENTER();
for (auto trap_id_str : trap_id_name_list)
{
sai_hostif_trap_type_t trap_id;
SWSS_LOG_DEBUG("processing trap_id:%s", trap_id_str.c_str());
trap_id = trap_id_map.at(trap_id_str);
SWSS_LOG_DEBUG("Pushing trap_id:%d", trap_id);
if (((trap_id == SAI_HOSTIF_TRAP_TYPE_SNAT_MISS) or (trap_id == SAI_HOSTIF_TRAP_TYPE_DNAT_MISS)) and
(gIsNatSupported == false))
{
SWSS_LOG_NOTICE("Ignoring the trap_id: %s, as NAT is not supported", trap_id_str.c_str());
continue;
}
trap_id_list.push_back(trap_id);
}
}
bool CoppOrch::createGenetlinkHostIfTable(vector<string> &trap_id_name_list)
{
SWSS_LOG_ENTER();
vector<sai_hostif_trap_type_t> trap_id_list;
getTrapIdList(trap_id_name_list, trap_id_list);
for (auto trap_id : trap_id_list)
{
auto host_tbl_entry = m_trapid_hostif_table_map.find(trap_id);
if (host_tbl_entry == m_trapid_hostif_table_map.end())
{
sai_object_id_t trap_group_id = m_syncdTrapIds[trap_id].trap_group_obj;
auto hostif_map = m_trap_group_hostif_map.find(trap_group_id);
if (hostif_map != m_trap_group_hostif_map.end())
{
sai_object_id_t hostif_table_entry = SAI_NULL_OBJECT_ID;
sai_attribute_t attr;
vector<sai_attribute_t> sai_host_table_attr;
attr.id = SAI_HOSTIF_TABLE_ENTRY_ATTR_TYPE;
attr.value.s32 = SAI_HOSTIF_TABLE_ENTRY_TYPE_TRAP_ID;
sai_host_table_attr.push_back(attr);
attr.id = SAI_HOSTIF_TABLE_ENTRY_ATTR_TRAP_ID;
attr.value.oid = m_syncdTrapIds[trap_id].trap_obj;
sai_host_table_attr.push_back(attr);
attr.id = SAI_HOSTIF_TABLE_ENTRY_ATTR_CHANNEL_TYPE;
attr.value.s32 = SAI_HOSTIF_TABLE_ENTRY_CHANNEL_TYPE_GENETLINK;
sai_host_table_attr.push_back(attr);
attr.id = SAI_HOSTIF_TABLE_ENTRY_ATTR_HOST_IF;
attr.value.oid = hostif_map->second;
sai_host_table_attr.push_back(attr);
sai_status_t status = sai_hostif_api->create_hostif_table_entry(&hostif_table_entry,
gSwitchId,
(uint32_t)sai_host_table_attr.size(),
sai_host_table_attr.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create hostif table entry failed, rv %d", status);
return false;
}
m_trapid_hostif_table_map[trap_id] = hostif_table_entry;
}
}
}
return true;
}
bool CoppOrch::applyAttributesToTrapIds(sai_object_id_t trap_group_id,
const vector<sai_hostif_trap_type_t> &trap_id_list,
vector<sai_attribute_t> &trap_id_attribs)
{
for (auto trap_id : trap_id_list)
{
sai_attribute_t attr;
vector<sai_attribute_t> attrs;
attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_TYPE;
attr.value.s32 = trap_id;
attrs.push_back(attr);
attrs.insert(attrs.end(), trap_id_attribs.begin(), trap_id_attribs.end());
sai_object_id_t hostif_trap_id;
sai_status_t status = sai_hostif_api->create_hostif_trap(&hostif_trap_id, gSwitchId, (uint32_t)attrs.size(), attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create trap %d, rv:%d", trap_id, status);
return false;
}
m_syncdTrapIds[trap_id].trap_group_obj = trap_group_id;
m_syncdTrapIds[trap_id].trap_obj = hostif_trap_id;
}
return true;
}
bool CoppOrch::applyTrapIds(sai_object_id_t trap_group, vector<string> &trap_id_name_list, vector<sai_attribute_t> &trap_id_attribs)
{
SWSS_LOG_ENTER();
vector<sai_hostif_trap_type_t> trap_id_list;
getTrapIdList(trap_id_name_list, trap_id_list);
sai_attribute_t attr;
attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_GROUP;
attr.value.oid = trap_group;
trap_id_attribs.push_back(attr);
return applyAttributesToTrapIds(trap_group, trap_id_list, trap_id_attribs);
}
bool CoppOrch::removePolicer(string trap_group_name)
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
sai_status_t sai_status;
sai_object_id_t policer_id = getPolicer(trap_group_name);
if (SAI_NULL_OBJECT_ID == policer_id)
{
SWSS_LOG_INFO("No policer is attached to trap group %s", trap_group_name.c_str());
return true;
}
attr.id = SAI_HOSTIF_TRAP_GROUP_ATTR_POLICER;
attr.value.oid = SAI_NULL_OBJECT_ID;
sai_status = sai_hostif_api->set_hostif_trap_group_attribute(m_trap_group_map[trap_group_name], &attr);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set policer to NULL for trap group %s, rc=%d", trap_group_name.c_str(), sai_status);
return false;
}
sai_status = sai_policer_api->remove_policer(policer_id);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove policer for trap group %s, rc=%d", trap_group_name.c_str(), sai_status);
return false;
}
SWSS_LOG_NOTICE("Remove policer for trap group %s", trap_group_name.c_str());
m_trap_group_policer_map.erase(m_trap_group_map[trap_group_name]);
return true;
}
sai_object_id_t CoppOrch::getPolicer(string trap_group_name)
{
SWSS_LOG_ENTER();
SWSS_LOG_DEBUG("trap group name:%s:", trap_group_name.c_str());
if (m_trap_group_map.find(trap_group_name) == m_trap_group_map.end())
{
return SAI_NULL_OBJECT_ID;
}
SWSS_LOG_DEBUG("trap group id:%" PRIx64, m_trap_group_map[trap_group_name]);
if (m_trap_group_policer_map.find(m_trap_group_map[trap_group_name]) == m_trap_group_policer_map.end())
{
return SAI_NULL_OBJECT_ID;
}
SWSS_LOG_DEBUG("trap group policer id:%" PRIx64, m_trap_group_policer_map[m_trap_group_map[trap_group_name]]);
return m_trap_group_policer_map[m_trap_group_map[trap_group_name]];
}
bool CoppOrch::createPolicer(string trap_group_name, vector<sai_attribute_t> &policer_attribs)
{
SWSS_LOG_ENTER();
sai_object_id_t policer_id;
sai_status_t sai_status;
sai_status = sai_policer_api->create_policer(&policer_id, gSwitchId, (uint32_t)policer_attribs.size(), policer_attribs.data());
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create policer trap group %s, rc=%d", trap_group_name.c_str(), sai_status);
return false;
}
SWSS_LOG_NOTICE("Create policer for trap group %s", trap_group_name.c_str());
sai_attribute_t attr;
attr.id = SAI_HOSTIF_TRAP_GROUP_ATTR_POLICER;
attr.value.oid = policer_id;
sai_status = sai_hostif_api->set_hostif_trap_group_attribute(m_trap_group_map[trap_group_name], &attr);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to bind policer to trap group %s, rc=%d", trap_group_name.c_str(), sai_status);
return false;
}
SWSS_LOG_NOTICE("Bind policer to trap group %s:", trap_group_name.c_str());
m_trap_group_policer_map[m_trap_group_map[trap_group_name]] = policer_id;
return true;
}
bool CoppOrch::createGenetlinkHostIf(string trap_group_name, vector<sai_attribute_t> &genetlink_attribs)
{
SWSS_LOG_ENTER();
sai_object_id_t hostif_id;
sai_status_t sai_status;
sai_status = sai_hostif_api->create_hostif(&hostif_id, gSwitchId,
(uint32_t)genetlink_attribs.size(),
genetlink_attribs.data());
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create genetlink hostif for trap group %s, rc=%d",
trap_group_name.c_str(), sai_status);
return false;
}
m_trap_group_hostif_map[m_trap_group_map[trap_group_name]] = hostif_id;
return true;
}
bool CoppOrch::removeGenetlinkHostIf(string trap_group_name)
{
SWSS_LOG_ENTER();
sai_status_t sai_status;
for (auto it : m_syncdTrapIds)
{
if (it.second.trap_group_obj == m_trap_group_map[trap_group_name])
{
auto hostTableEntry = m_trapid_hostif_table_map.find(it.first);
if (hostTableEntry != m_trapid_hostif_table_map.end())
{
sai_status = sai_hostif_api->remove_hostif_table_entry(hostTableEntry->second);
if(sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to delete hostif table entry %ld \
on trap group %s. rc=%d", hostTableEntry->second,
trap_group_name.c_str(), sai_status);
return false;
}
m_trapid_hostif_table_map.erase(it.first);
}
}
}
auto hostInfo = m_trap_group_hostif_map.find(m_trap_group_map[trap_group_name]);
if(hostInfo != m_trap_group_hostif_map.end())
{
sai_status = sai_hostif_api->remove_hostif(hostInfo->second);
if(sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to delete host info %ld on trap group %s. rc=%d",
hostInfo->second, trap_group_name.c_str(), sai_status);
return false;
}
m_trap_group_hostif_map.erase(m_trap_group_map[trap_group_name]);
}
return true;
}
task_process_status CoppOrch::processCoppRule(Consumer& consumer)
{
SWSS_LOG_ENTER();
sai_status_t sai_status;
vector<string> trap_id_list;
string queue_ind;
auto it = consumer.m_toSync.begin();
KeyOpFieldsValuesTuple tuple = it->second;
string trap_group_name = kfvKey(tuple);
string op = kfvOp(tuple);
vector<sai_attribute_t> trap_gr_attribs;
vector<sai_attribute_t> trap_id_attribs;
vector<sai_attribute_t> policer_attribs;
vector<sai_attribute_t> genetlink_attribs;
if (op == SET_COMMAND)
{
for (auto i = kfvFieldsValues(tuple).begin(); i != kfvFieldsValues(tuple).end(); i++)
{
sai_attribute_t attr;
if (fvField(*i) == copp_trap_id_list)
{
trap_id_list = tokenize(fvValue(*i), list_item_delimiter);
auto it = std::find(trap_id_list.begin(), trap_id_list.end(), "sample_packet");
if (it != trap_id_list.end())
{
if (!enable_sflow_trap)
{
return task_process_status::task_need_retry;
}
}
}
else if (fvField(*i) == copp_queue_field)
{
attr.id = SAI_HOSTIF_TRAP_GROUP_ATTR_QUEUE;
attr.value.u32 = (uint32_t)stoul(fvValue(*i));
trap_gr_attribs.push_back(attr);
}
//
// Trap related attributes
//
else if (fvField(*i) == copp_trap_action_field)
{
sai_packet_action_t trap_action = packet_action_map.at(fvValue(*i));
attr.id = SAI_HOSTIF_TRAP_ATTR_PACKET_ACTION;
attr.value.s32 = trap_action;
trap_id_attribs.push_back(attr);
}
else if (fvField(*i) == copp_trap_priority_field)
{
/* Mellanox platform doesn't support trap priority setting */
char *platform = getenv("platform");
if (!platform || !strstr(platform, MLNX_PLATFORM_SUBSTRING))
{
attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_PRIORITY,
attr.value.u32 = (uint32_t)stoul(fvValue(*i));
trap_id_attribs.push_back(attr);
}
}
//
// process policer attributes
//
else if (fvField(*i) == copp_policer_meter_type_field)
{
sai_meter_type_t meter_value = policer_meter_map.at(fvValue(*i));
attr.id = SAI_POLICER_ATTR_METER_TYPE;
attr.value.s32 = meter_value;
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_policer_mode_field)
{
sai_policer_mode_t mode = policer_mode_map.at(fvValue(*i));
attr.id = SAI_POLICER_ATTR_MODE;
attr.value.s32 = mode;
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_policer_color_field)
{
sai_policer_color_source_t color = policer_color_aware_map.at(fvValue(*i));
attr.id = SAI_POLICER_ATTR_COLOR_SOURCE;
attr.value.s32 = color;
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_policer_cbs_field)
{
attr.id = SAI_POLICER_ATTR_CBS;
attr.value.u64 = stoul(fvValue(*i));
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_policer_cir_field)
{
attr.id = SAI_POLICER_ATTR_CIR;
attr.value.u64 = stoul(fvValue(*i));
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_policer_pbs_field)
{
attr.id = SAI_POLICER_ATTR_PBS;
attr.value.u64 = stoul(fvValue(*i));
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_policer_pir_field)
{
attr.id = SAI_POLICER_ATTR_PIR;
attr.value.u64 = stoul(fvValue(*i));
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_policer_action_green_field)
{
sai_packet_action_t policer_action = packet_action_map.at(fvValue(*i));
attr.id = SAI_POLICER_ATTR_GREEN_PACKET_ACTION;
attr.value.s32 = policer_action;
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_policer_action_red_field)
{
sai_packet_action_t policer_action = packet_action_map.at(fvValue(*i));
attr.id = SAI_POLICER_ATTR_RED_PACKET_ACTION;
attr.value.s32 = policer_action;
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_policer_action_yellow_field)
{
sai_packet_action_t policer_action = packet_action_map.at(fvValue(*i));
attr.id = SAI_POLICER_ATTR_YELLOW_PACKET_ACTION;
attr.value.s32 = policer_action;
policer_attribs.push_back(attr);
}
else if (fvField(*i) == copp_genetlink_name)
{
attr.id = SAI_HOSTIF_ATTR_TYPE;
attr.value.s32 = SAI_HOSTIF_TYPE_GENETLINK;
genetlink_attribs.push_back(attr);
attr.id = SAI_HOSTIF_ATTR_NAME;
strncpy(attr.value.chardata, fvValue(*i).c_str(),
sizeof(attr.value.chardata));
genetlink_attribs.push_back(attr);
}
else if (fvField(*i) == copp_genetlink_mcgrp_name)
{
attr.id = SAI_HOSTIF_ATTR_GENETLINK_MCGRP_NAME;
strncpy(attr.value.chardata, fvValue(*i).c_str(),
sizeof(attr.value.chardata));
genetlink_attribs.push_back(attr);
}
else
{
SWSS_LOG_ERROR("Unknown copp field specified:%s\n", fvField(*i).c_str());
return task_process_status::task_invalid_entry;
}
}
/* Set host interface trap group */
if (m_trap_group_map.find(trap_group_name) != m_trap_group_map.end())
{
/* Create or set policer */
if (!policer_attribs.empty())
{
sai_object_id_t policer_id = getPolicer(trap_group_name);
if (SAI_NULL_OBJECT_ID == policer_id)
{
SWSS_LOG_WARN("Creating policer for existing Trap group:%" PRIx64 " (name:%s).", m_trap_group_map[trap_group_name], trap_group_name.c_str());
if (!createPolicer(trap_group_name, policer_attribs))
{
return task_process_status::task_failed;
}
SWSS_LOG_DEBUG("Created policer:%" PRIx64 " for existing trap group", policer_id);
}
else
{
/* TODO: We should really only set changed attributes.
The changes need to detected either by orch agent submodule or by the orch agent framework. */
for (sai_uint32_t ind = 0; ind < policer_attribs.size(); ind++)
{
auto policer_attr = policer_attribs[ind];
sai_status = sai_policer_api->set_policer_attribute(policer_id, &policer_attr);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to apply attribute[%d].id=%d to policer for trap group:%s, error:%d\n", ind, policer_attr.id, trap_group_name.c_str(), sai_status);
return task_process_status::task_failed;
}
}
}
}
for (sai_uint32_t ind = 0; ind < trap_gr_attribs.size(); ind++)
{
auto trap_gr_attr = trap_gr_attribs[ind];
sai_status = sai_hostif_api->set_hostif_trap_group_attribute(m_trap_group_map[trap_group_name], &trap_gr_attr);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to apply attribute:%d to trap group:%" PRIx64 ", name:%s, error:%d\n", trap_gr_attr.id, m_trap_group_map[trap_group_name], trap_group_name.c_str(), sai_status);
return task_process_status::task_failed;
}
SWSS_LOG_NOTICE("Set trap group %s to host interface", trap_group_name.c_str());
}
}
/* Create host interface trap group */
else
{
sai_object_id_t new_trap;
sai_status = sai_hostif_api->create_hostif_trap_group(&new_trap, gSwitchId, (uint32_t)trap_gr_attribs.size(), trap_gr_attribs.data());
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create host interface trap group %s, rc=%d", trap_group_name.c_str(), sai_status);
return task_process_status::task_failed;
}
SWSS_LOG_NOTICE("Create host interface trap group %s", trap_group_name.c_str());
m_trap_group_map[trap_group_name] = new_trap;
/* Create policer */
if (!policer_attribs.empty())
{
if (!createPolicer(trap_group_name, policer_attribs))
{
return task_process_status::task_failed;
}
}
if (!genetlink_attribs.empty())
{
if (!createGenetlinkHostIf(trap_group_name, genetlink_attribs))
{
return task_process_status::task_failed;
}
}
}
/* Apply traps to trap group */
if (!applyTrapIds(m_trap_group_map[trap_group_name], trap_id_list, trap_id_attribs))
{
return task_process_status::task_failed;
}
if (!genetlink_attribs.empty())
{
if (!createGenetlinkHostIfTable(trap_id_list))
{
return task_process_status::task_failed;
}
}
}
else if (op == DEL_COMMAND)
{
/* Remove policer if any */
if (!removePolicer(trap_group_name))
{
SWSS_LOG_ERROR("Failed to remove policer from trap group %s", trap_group_name.c_str());
return task_process_status::task_failed;
}
if (!removeGenetlinkHostIf(trap_group_name))
{
SWSS_LOG_ERROR("Failed to remove hostif from trap group %s", trap_group_name.c_str());
return task_process_status::task_failed;
}
/* Do not remove default trap group */
if (trap_group_name == default_trap_group)
{
SWSS_LOG_WARN("Cannot remove default trap group");
return task_process_status::task_ignore;
}
/* Reset the trap IDs to default trap group with default attributes */
vector<sai_hostif_trap_type_t> trap_ids_to_reset;
for (auto it : m_syncdTrapIds)
{
if (it.second.trap_group_obj == m_trap_group_map[trap_group_name])
{
trap_ids_to_reset.push_back(it.first);
}
sai_status = sai_hostif_api->remove_hostif_trap(it.second.trap_obj);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove trap object %ld", it.second.trap_obj);
return task_process_status::task_failed;
}
}
sai_attribute_t attr;
vector<sai_attribute_t> default_trap_attrs;
attr.id = SAI_HOSTIF_TRAP_ATTR_PACKET_ACTION;
attr.value.s32 = SAI_PACKET_ACTION_FORWARD;
default_trap_attrs.push_back(attr);
attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_GROUP;
attr.value.oid = m_trap_group_map[default_trap_group];
default_trap_attrs.push_back(attr);
if (!applyAttributesToTrapIds(m_trap_group_map[default_trap_group], trap_ids_to_reset, default_trap_attrs))
{
SWSS_LOG_ERROR("Failed to reset traps to default trap group with default attributes");
return task_process_status::task_failed;
}
sai_status = sai_hostif_api->remove_hostif_trap_group(m_trap_group_map[trap_group_name]);
if (sai_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove trap group %s", trap_group_name.c_str());
return task_process_status::task_failed;
}
auto it_del = m_trap_group_map.find(trap_group_name);
m_trap_group_map.erase(it_del);
}
else
{
SWSS_LOG_ERROR("Unknown copp operation type %s\n", op.c_str());
return task_process_status::task_invalid_entry;
}
return task_process_status::task_success;
}
/* Program Sflow trap once we get sflow enable command */
void CoppOrch::coppProcessSflow(Consumer &consumer)
{
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
auto tuple = it->second;
string op = kfvOp(tuple);
/*
* Need to handled just 'config sflow enable' command to install the sflow trap group
* for the first time to ensure support of genetlink attributes. Rest of the fields or
* disable value or DEL command are not required to be handled
*
*/
if (op == SET_COMMAND)
{
for (auto i : kfvFieldsValues(tuple))
{
if (fvField(i) == "admin_state")
{
if (fvValue(i) == "up")
{
enable_sflow_trap = true;
}
}
}
}
it = consumer.m_toSync.erase(it);
}
}
void CoppOrch::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
string table_name = consumer.getTableName();
if (table_name == CFG_SFLOW_TABLE_NAME)
{
coppProcessSflow(consumer);
return;
}
if (!gPortsOrch->allPortsReady())
{
return;
}
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple tuple = it->second;
task_process_status task_status;
try
{
task_status = processCoppRule(consumer);
}
catch(const out_of_range& e)
{
SWSS_LOG_ERROR("processing copp rule threw out_of_range exception:%s", e.what());
task_status = task_process_status::task_invalid_entry;
}
catch(exception& e)
{
SWSS_LOG_ERROR("processing copp rule threw exception:%s", e.what());
task_status = task_process_status::task_invalid_entry;
}
switch(task_status)
{
case task_process_status::task_success :
case task_process_status::task_ignore :
it = consumer.m_toSync.erase(it);
break;
case task_process_status::task_invalid_entry:
SWSS_LOG_ERROR("Invalid copp task item was encountered, removing from queue.");
it = consumer.m_toSync.erase(it);
break;
case task_process_status::task_failed:
it = consumer.m_toSync.erase(it);
SWSS_LOG_ERROR("Processing copp task item failed, exiting. ");
return;
case task_process_status::task_need_retry:
SWSS_LOG_ERROR("Processing copp task item failed, will retry.");
it++;
break;
default:
it = consumer.m_toSync.erase(it);
SWSS_LOG_ERROR("Invalid task status:%d", task_status);
return;
}
}
}