@@ -93,6 +93,7 @@ CoppOrch::CoppOrch(DBConnector *db, string tableName) :
93
93
94
94
void CoppOrch::initDefaultTrapIds ()
95
95
{
96
+ SWSS_LOG_ENTER ();
96
97
97
98
sai_attribute_t attr;
98
99
vector<sai_attribute_t > trap_id_attrs;
@@ -105,29 +106,34 @@ void CoppOrch::initDefaultTrapIds()
105
106
attr.value .oid = m_trap_group_map[default_trap_group];
106
107
trap_id_attrs.push_back (attr);
107
108
109
+ /* Receive packets via OS net device */
108
110
attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_CHANNEL;
109
111
attr.value .s32 = SAI_HOSTIF_TRAP_CHANNEL_NETDEV;
110
112
trap_id_attrs.push_back (attr);
111
113
112
- if (!applyAttributesToTrapIds (default_trap_ids, trap_id_attrs))
114
+ if (!applyAttributesToTrapIds (m_trap_group_map[default_trap_group], default_trap_ids, trap_id_attrs))
113
115
{
114
- SWSS_LOG_ERROR (" Failed applying default trap Ids. " );
116
+ SWSS_LOG_ERROR (" Failed to set attributes to default trap IDs " );
115
117
}
118
+
119
+ SWSS_LOG_INFO (" Set attributes to default trap IDs" );
116
120
}
117
121
118
122
void CoppOrch::initDefaultTrapGroup ()
119
123
{
120
124
SWSS_LOG_ENTER ();
121
- sai_status_t sai_status;
122
- sai_attribute_t attrib;
123
125
124
- attrib.id = SAI_SWITCH_ATTR_DEFAULT_TRAP_GROUP;
125
- sai_status = sai_switch_api->get_switch_attribute (1 , &attrib);
126
- if (sai_status != SAI_STATUS_SUCCESS)
126
+ sai_attribute_t attr;
127
+ attr.id = SAI_SWITCH_ATTR_DEFAULT_TRAP_GROUP;
128
+
129
+ sai_status_t status = sai_switch_api->get_switch_attribute (1 , &attr);
130
+ if (status != SAI_STATUS_SUCCESS)
127
131
{
128
- SWSS_LOG_ERROR (" failed to get default trap group. error: %d" , sai_status );
132
+ SWSS_LOG_ERROR (" Failed to get default trap group, rc= %d" , status );
129
133
}
130
- m_trap_group_map[default_trap_group] = attrib.value .oid ;
134
+
135
+ SWSS_LOG_INFO (" Get default trap group" );
136
+ m_trap_group_map[default_trap_group] = attr.value .oid ;
131
137
}
132
138
133
139
void CoppOrch::getTrapIdList (vector<string> &trap_id_name_list, vector<sai_hostif_trap_id_t > &trap_id_list) const
@@ -143,21 +149,24 @@ void CoppOrch::getTrapIdList(vector<string> &trap_id_name_list, vector<sai_hosti
143
149
}
144
150
}
145
151
146
- bool CoppOrch::applyAttributesToTrapIds (const vector<sai_hostif_trap_id_t > &trap_id_list, vector<sai_attribute_t > &trap_id_attribs)
152
+ bool CoppOrch::applyAttributesToTrapIds (sai_object_id_t trap_group_id,
153
+ const vector<sai_hostif_trap_id_t > &trap_id_list,
154
+ vector<sai_attribute_t > &trap_id_attribs)
147
155
{
148
156
for (auto trap_id : trap_id_list)
149
157
{
150
- for (auto trap_id_attr : trap_id_attribs)
158
+ for (auto attr : trap_id_attribs)
151
159
{
152
- SWSS_LOG_DEBUG (" Applying trap attr:%d" , trap_id_attr.id );
153
- sai_status_t sai_status = sai_hostif_api->set_trap_attribute (trap_id, &trap_id_attr);
154
- if (sai_status != SAI_STATUS_SUCCESS)
160
+ sai_status_t status = sai_hostif_api->set_trap_attribute (trap_id, &attr);
161
+ if (status != SAI_STATUS_SUCCESS)
155
162
{
156
- SWSS_LOG_ERROR (" Failed to apply trap_id attribute: %d to trap_id: %d, error:%d \n " , trap_id_attr .id , trap_id, sai_status );
163
+ SWSS_LOG_ERROR (" Failed to set attribute %d to trap %d, rc=%d " , attr .id , trap_id, status );
157
164
return false ;
158
165
}
159
166
}
167
+ m_syncdTrapIds[trap_id] = trap_group_id;
160
168
}
169
+
161
170
return true ;
162
171
}
163
172
@@ -173,46 +182,54 @@ bool CoppOrch::applyTrapIds(sai_object_id_t trap_group, vector<string> &trap_id_
173
182
attr.value .oid = trap_group;
174
183
trap_id_attribs.push_back (attr);
175
184
176
- // The channel is always the NETDEV device.
185
+ /* Receive packets via OS net device */
177
186
attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_CHANNEL;
178
187
attr.value .s32 = SAI_HOSTIF_TRAP_CHANNEL_NETDEV;
179
188
trap_id_attribs.push_back (attr);
180
189
181
- return applyAttributesToTrapIds (trap_id_list, trap_id_attribs);
190
+ return applyAttributesToTrapIds (trap_group, trap_id_list, trap_id_attribs);
182
191
}
183
192
184
193
bool CoppOrch::removePolicer (string trap_group_name)
185
194
{
186
195
SWSS_LOG_ENTER ();
196
+
187
197
sai_attribute_t attr;
188
198
sai_status_t sai_status;
189
199
sai_object_id_t policer_id = getPolicer (trap_group_name);
200
+
190
201
if (SAI_NULL_OBJECT_ID == policer_id)
191
202
{
192
- SWSS_LOG_DEBUG (" No policer is attached to trap group:%s \n " , trap_group_name.c_str ());
203
+ SWSS_LOG_INFO (" No policer is attached to trap group %s " , trap_group_name.c_str ());
193
204
return true ;
194
205
}
206
+
195
207
attr.id = SAI_HOSTIF_TRAP_GROUP_ATTR_POLICER;
196
208
attr.value .oid = SAI_NULL_OBJECT_ID;
209
+
197
210
sai_status = sai_hostif_api->set_trap_group_attribute (m_trap_group_map[trap_group_name], &attr);
198
211
if (sai_status != SAI_STATUS_SUCCESS)
199
212
{
200
- SWSS_LOG_ERROR (" Failed to reset policer for trap group:%lx, error:%d \n " , m_trap_group_map[ trap_group_name] , sai_status);
213
+ SWSS_LOG_ERROR (" Failed to set policer to NULL for trap group %s, rc=%d " , trap_group_name. c_str () , sai_status);
201
214
return false ;
202
215
}
216
+
203
217
sai_status = sai_policer_api->remove_policer (policer_id);
204
218
if (sai_status != SAI_STATUS_SUCCESS)
205
219
{
206
- SWSS_LOG_ERROR (" Failed to remove policer:%lx, error:%d \n " , policer_id , sai_status);
220
+ SWSS_LOG_ERROR (" Failed to remove policer for trap group %s, rc=%d " , trap_group_name. c_str () , sai_status);
207
221
return false ;
208
222
}
223
+
224
+ SWSS_LOG_NOTICE (" Remove policer for trap group %s" , trap_group_name.c_str ());
209
225
m_trap_group_policer_map.erase (m_trap_group_map[trap_group_name]);
210
226
return true ;
211
227
}
212
228
213
229
sai_object_id_t CoppOrch::getPolicer (string trap_group_name)
214
230
{
215
231
SWSS_LOG_ENTER ();
232
+
216
233
SWSS_LOG_DEBUG (" trap group name:%s:" , trap_group_name.c_str ());
217
234
if (m_trap_group_map.find (trap_group_name) == m_trap_group_map.end ())
218
235
{
@@ -230,33 +247,39 @@ sai_object_id_t CoppOrch::getPolicer(string trap_group_name)
230
247
bool CoppOrch::createPolicer (string trap_group_name, vector<sai_attribute_t > &policer_attribs)
231
248
{
232
249
SWSS_LOG_ENTER ();
250
+
233
251
sai_object_id_t policer_id;
234
252
sai_status_t sai_status;
235
- SWSS_LOG_DEBUG ( " trap group name:%s: " , trap_group_name. c_str ());
253
+
236
254
sai_status = sai_policer_api->create_policer (&policer_id, policer_attribs.size (), policer_attribs.data ());
237
255
if (sai_status != SAI_STATUS_SUCCESS)
238
256
{
239
- SWSS_LOG_ERROR (" Failed to create policer for existing trap_group_name:%lx, name:% s, error: %d" , m_trap_group_map[trap_group_name] , trap_group_name.c_str (), sai_status);
257
+ SWSS_LOG_ERROR (" Failed to create policer trap group % s, rc= %d" , trap_group_name.c_str (), sai_status);
240
258
return false ;
241
259
}
242
- SWSS_LOG_DEBUG (" Created policer:%lx for trap group:%lx" , policer_id, m_trap_group_map[trap_group_name]);
260
+
261
+ SWSS_LOG_NOTICE (" Create policer for trap group %s" , trap_group_name.c_str ());
262
+
243
263
sai_attribute_t attr;
244
264
attr.id = SAI_HOSTIF_TRAP_GROUP_ATTR_POLICER;
245
265
attr.value .oid = policer_id;
266
+
246
267
sai_status = sai_hostif_api->set_trap_group_attribute (m_trap_group_map[trap_group_name], &attr);
247
268
if (sai_status != SAI_STATUS_SUCCESS)
248
269
{
249
- SWSS_LOG_ERROR (" Failed to bind policer:%lx to trap group:%lx, name: %s, error:%d \n " , policer_id, m_trap_group_map[trap_group_name] , trap_group_name.c_str (), sai_status);
270
+ SWSS_LOG_ERROR (" Failed to bind policer to trap group %s, rc=%d " , trap_group_name.c_str (), sai_status);
250
271
return false ;
251
272
}
273
+
274
+ SWSS_LOG_NOTICE (" Bind policer to trap group %s:" , trap_group_name.c_str ());
252
275
m_trap_group_policer_map[m_trap_group_map[trap_group_name]] = policer_id;
253
- SWSS_LOG_DEBUG (" Created policer:%lx for trap group name:%s:" , policer_id, trap_group_name.c_str ());
254
276
return true ;
255
277
}
256
278
257
279
task_process_status CoppOrch::processCoppRule (Consumer& consumer)
258
280
{
259
281
SWSS_LOG_ENTER ();
282
+
260
283
sai_status_t sai_status;
261
284
vector<string> trap_id_list;
262
285
string queue_ind;
@@ -265,7 +288,6 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer)
265
288
string trap_group_name = kfvKey (tuple);
266
289
string op = kfvOp (tuple);
267
290
268
- SWSS_LOG_DEBUG (" copp:processing:%s" , trap_group_name.c_str ());
269
291
vector<sai_attribute_t > trap_gr_attribs;
270
292
vector<sai_attribute_t > trap_id_attribs;
271
293
vector<sai_attribute_t > policer_attribs;
@@ -385,9 +407,10 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer)
385
407
}
386
408
}
387
409
410
+ /* Set host interface trap group */
388
411
if (m_trap_group_map.find (trap_group_name) != m_trap_group_map.end ())
389
412
{
390
- SWSS_LOG_DEBUG ( " found existing trap_group object:%s " , trap_group_name. c_str ());
413
+ /* Create or set policer */
391
414
if (!policer_attribs.empty ())
392
415
{
393
416
sai_object_id_t policer_id = getPolicer (trap_group_name);
@@ -416,67 +439,101 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer)
416
439
}
417
440
}
418
441
}
419
- SWSS_LOG_DEBUG ( " Applying trap group attributes " );
442
+
420
443
for (sai_uint32_t ind = 0 ; ind < trap_gr_attribs.size (); ind++)
421
444
{
422
445
auto trap_gr_attr = trap_gr_attribs[ind];
423
- SWSS_LOG_DEBUG ( " Applying trap group attribute:%d " , trap_gr_attr. id );
446
+
424
447
sai_status = sai_hostif_api->set_trap_group_attribute (m_trap_group_map[trap_group_name], &trap_gr_attr);
425
448
if (sai_status != SAI_STATUS_SUCCESS)
426
449
{
427
450
SWSS_LOG_ERROR (" Failed to apply attribute:%d to trap group:%lx, name:%s, error:%d\n " , trap_gr_attr.id , m_trap_group_map[trap_group_name], trap_group_name.c_str (), sai_status);
428
451
return task_process_status::task_failed;
429
452
}
453
+ SWSS_LOG_NOTICE (" Set trap group %s to host interface" , trap_group_name.c_str ());
430
454
}
431
455
}
456
+ /* Create host interface trap group */
432
457
else
433
458
{
434
- SWSS_LOG_DEBUG (" Creating new trap_group object:%s" , trap_group_name.c_str ());
435
459
sai_object_id_t new_trap;
460
+
436
461
sai_status = sai_hostif_api->create_hostif_trap_group (&new_trap, trap_gr_attribs.size (), trap_gr_attribs.data ());
437
462
if (sai_status != SAI_STATUS_SUCCESS)
438
463
{
439
- SWSS_LOG_ERROR (" Failed to create new trap_group with name:%s " , trap_group_name.c_str ());
464
+ SWSS_LOG_ERROR (" Failed to create host interface trap group %s, rc=%d " , trap_group_name.c_str (), sai_status );
440
465
return task_process_status::task_failed;
441
466
}
442
- SWSS_LOG_DEBUG (" Created new trap_group:%lx with name:%s" , new_trap, trap_group_name.c_str ());
467
+
468
+ SWSS_LOG_NOTICE (" Create host interface trap group %s" , trap_group_name.c_str ());
443
469
m_trap_group_map[trap_group_name] = new_trap;
470
+
471
+ /* Create policer */
444
472
if (!policer_attribs.empty ())
445
473
{
446
474
if (!createPolicer (trap_group_name, policer_attribs))
447
475
{
448
476
return task_process_status::task_failed;
449
477
}
450
- SWSS_LOG_DEBUG (" Bound policer to the trap group" );
451
478
}
452
479
}
480
+
481
+ /* Apply traps to trap group */
453
482
if (!applyTrapIds (m_trap_group_map[trap_group_name], trap_id_list, trap_id_attribs))
454
483
{
455
484
return task_process_status::task_failed;
456
485
}
457
486
}
458
487
else if (op == DEL_COMMAND)
459
488
{
460
- // delete trap group and its policer.
489
+ /* Remove policer if any */
461
490
if (!removePolicer (trap_group_name))
462
491
{
463
- SWSS_LOG_ERROR (" Failed to remove policer from trap group:%s \n " , trap_group_name.c_str ());
492
+ SWSS_LOG_ERROR (" Failed to remove policer from trap group %s " , trap_group_name.c_str ());
464
493
return task_process_status::task_failed;
465
494
}
466
495
467
- // default trap group is never deleted.
496
+ /* Do not remove default trap group */
468
497
if (trap_group_name == default_trap_group)
469
498
{
470
- SWSS_LOG_WARN (" Trying to delete default trap group" );
499
+ SWSS_LOG_WARN (" Cannot remove default trap group" );
471
500
return task_process_status::task_ignore;
472
501
}
473
502
503
+ /* Reset the trap IDs to default trap group with default attributes */
504
+ vector<sai_hostif_trap_id_t > trap_ids_to_reset;
505
+ for (auto it : m_syncdTrapIds)
506
+ {
507
+ if (it.second == m_trap_group_map[trap_group_name])
508
+ {
509
+ trap_ids_to_reset.push_back (it.first );
510
+ }
511
+ }
512
+
513
+ sai_attribute_t attr;
514
+ vector<sai_attribute_t > default_trap_attrs;
515
+
516
+ attr.id = SAI_HOSTIF_TRAP_ATTR_PACKET_ACTION;
517
+ attr.value .s32 = SAI_PACKET_ACTION_FORWARD;
518
+ default_trap_attrs.push_back (attr);
519
+
520
+ attr.id = SAI_HOSTIF_TRAP_ATTR_TRAP_GROUP;
521
+ attr.value .oid = m_trap_group_map[default_trap_group];
522
+ default_trap_attrs.push_back (attr);
523
+
524
+ if (!applyAttributesToTrapIds (m_trap_group_map[default_trap_group], trap_ids_to_reset, default_trap_attrs))
525
+ {
526
+ SWSS_LOG_ERROR (" Failed to reset traps to default trap group with default attributes" );
527
+ return task_process_status::task_failed;
528
+ }
529
+
474
530
sai_status = sai_hostif_api->remove_hostif_trap_group (m_trap_group_map[trap_group_name]);
475
531
if (sai_status != SAI_STATUS_SUCCESS)
476
532
{
477
- SWSS_LOG_ERROR (" Failed to remove trap group:%lx, name:%s \n " , m_trap_group_map[trap_group_name] , trap_group_name.c_str ());
533
+ SWSS_LOG_ERROR (" Failed to remove trap group %s " , trap_group_name.c_str ());
478
534
return task_process_status::task_failed;
479
535
}
536
+
480
537
auto it_del = m_trap_group_map.find (trap_group_name);
481
538
m_trap_group_map.erase (it_del);
482
539
}
@@ -491,18 +548,13 @@ task_process_status CoppOrch::processCoppRule(Consumer& consumer)
491
548
void CoppOrch::doTask (Consumer &consumer)
492
549
{
493
550
SWSS_LOG_ENTER ();
551
+
494
552
auto it = consumer.m_toSync .begin ();
495
553
while (it != consumer.m_toSync .end ())
496
554
{
497
555
KeyOpFieldsValuesTuple tuple = it->second ;
498
- string table_name = consumer.m_consumer ->getTableName ();
499
- if (table_name != APP_COPP_TABLE_NAME)
500
- {
501
- SWSS_LOG_ERROR (" Unrecognised copp table encountered:%s\n " , table_name.c_str ());
502
- it = consumer.m_toSync .erase (it);
503
- continue ;
504
- }
505
556
task_process_status task_status;
557
+
506
558
try
507
559
{
508
560
task_status = processCoppRule (consumer);
0 commit comments