Skip to content

Commit b60f4bb

Browse files
authored
Relax metadata FDB api, and handle fdb notifications in metadata (sonic-net#128)
* Relax metadata FDB api, and handle fdb notifications in metadata * Add workaround for missing fdb event type in notification
1 parent ac3ede5 commit b60f4bb

11 files changed

+133
-31
lines changed

lib/inc/sai_redis.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ extern swss::Table *g_ridToVid;
4848

4949
extern swss::RedisClient *g_redisClient;
5050

51-
extern std::mutex g_mutex;
5251
extern std::mutex g_apimutex;
5352

5453
extern const sai_acl_api_t redis_acl_api;

lib/src/sai_redis_generic_create.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ sai_status_t internal_redis_generic_create(
3939
_In_ uint32_t attr_count,
4040
_In_ const sai_attribute_t *attr_list)
4141
{
42-
std::lock_guard<std::mutex> lock(g_mutex);
43-
4442
SWSS_LOG_ENTER();
4543

4644
if (attr_count > 0 && attr_list == NULL)

lib/src/sai_redis_generic_get.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ sai_status_t internal_redis_generic_get(
4545
_In_ uint32_t attr_count,
4646
_Out_ sai_attribute_t *attr_list)
4747
{
48-
std::lock_guard<std::mutex> lock(g_mutex);
49-
5048
SWSS_LOG_ENTER();
5149

5250
std::vector<swss::FieldValueTuple> entry = SaiAttributeList::serialize_attr_list(

lib/src/sai_redis_generic_remove.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ sai_status_t internal_redis_generic_remove(
66
_In_ sai_object_type_t object_type,
77
_In_ const std::string &serialized_object_id)
88
{
9-
std::lock_guard<std::mutex> lock(g_mutex);
10-
119
SWSS_LOG_ENTER();
1210

1311
std::string str_object_type = sai_serialize_object_type(object_type);

lib/src/sai_redis_generic_set.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ sai_status_t internal_redis_generic_set(
77
_In_ const std::string &serialized_object_id,
88
_In_ const sai_attribute_t *attr)
99
{
10-
std::lock_guard<std::mutex> lock(g_mutex);
11-
1210
SWSS_LOG_ENTER();
1311

1412
std::vector<swss::FieldValueTuple> entry = SaiAttributeList::serialize_attr_list(

lib/src/sai_redis_interfacequery.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <string.h>
22
#include "sai_redis.h"
33

4-
std::mutex g_mutex;
54
std::mutex g_apimutex;
65

76
service_method_table_t g_services;
@@ -21,7 +20,7 @@ sai_status_t sai_api_initialize(
2120
_In_ uint64_t flags,
2221
_In_ const service_method_table_t* services)
2322
{
24-
std::lock_guard<std::mutex> lock(g_mutex);
23+
std::lock_guard<std::mutex> lock(g_apimutex);
2524

2625
SWSS_LOG_ENTER();
2726

@@ -78,7 +77,7 @@ sai_status_t sai_log_set(
7877
_In_ sai_api_t sai_api_id,
7978
_In_ sai_log_level_t log_level)
8079
{
81-
std::lock_guard<std::mutex> lock(g_mutex);
80+
std::lock_guard<std::mutex> lock(g_apimutex);
8281

8382
SWSS_LOG_ENTER();
8483

@@ -169,7 +168,7 @@ sai_status_t sai_api_query(
169168
_In_ sai_api_t sai_api_id,
170169
_Out_ void** api_method_table)
171170
{
172-
std::lock_guard<std::mutex> lock(g_mutex);
171+
std::lock_guard<std::mutex> lock(g_apimutex);
173172

174173
SWSS_LOG_ENTER();
175174

@@ -298,7 +297,7 @@ sai_status_t sai_api_query(
298297

299298
sai_status_t sai_api_uninitialize(void)
300299
{
301-
std::lock_guard<std::mutex> lock(g_mutex);
300+
std::lock_guard<std::mutex> lock(g_apimutex);
302301

303302
SWSS_LOG_ENTER();
304303

lib/src/sai_redis_notifications.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ void handle_fdb_event(
3333

3434
sai_deserialize_fdb_event_ntf(data, count, &fdbevent);
3535

36+
{
37+
std::lock_guard<std::mutex> lock(g_apimutex);
38+
39+
// NOTE: this meta api must be under mutex since
40+
// it will access meta DB and notification comes
41+
// from different thread
42+
43+
meta_sai_on_fdb_event(count, fdbevent);
44+
}
45+
3646
auto on_fdb_event = redis_switch_notifications.on_fdb_event;
3747

3848
if (on_fdb_event != NULL)

lib/src/sai_redis_switch.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ sai_status_t redis_initialize_switch(
9494
{
9595
std::lock_guard<std::mutex> apilock(g_apimutex);
9696

97-
std::lock_guard<std::mutex> lock(g_mutex);
98-
9997
SWSS_LOG_ENTER();
10098

10199
if (g_switchInitialized)
@@ -151,8 +149,6 @@ void redis_shutdown_switch(
151149
{
152150
std::lock_guard<std::mutex> apilock(g_apimutex);
153151

154-
std::lock_guard<std::mutex> lock(g_mutex);
155-
156152
SWSS_LOG_ENTER();
157153

158154
if (!g_switchInitialized)
@@ -195,8 +191,6 @@ sai_status_t redis_connect_switch(
195191
{
196192
std::lock_guard<std::mutex> apilock(g_apimutex);
197193

198-
std::lock_guard<std::mutex> lock(g_mutex);
199-
200194
SWSS_LOG_ENTER();
201195

202196
SWSS_LOG_ERROR("not implemented");
@@ -217,8 +211,6 @@ void redis_disconnect_switch(void)
217211
{
218212
std::lock_guard<std::mutex> apilock(g_apimutex);
219213

220-
std::lock_guard<std::mutex> lock(g_mutex);
221-
222214
SWSS_LOG_ENTER();
223215

224216
SWSS_LOG_ERROR("not implemented");

meta/sai_meta.cpp

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ void set_object(
736736
if (!object_exists(key))
737737
{
738738
SWSS_LOG_ERROR("FATAL: object %s don't exists", key.c_str());
739-
throw;
739+
throw std::runtime_error("FATAL: object don't exists" + key);
740740
}
741741

742742
META_LOG_DEBUG(md, "set attribute %d on %s", attr->id, key.c_str());
@@ -2361,7 +2361,8 @@ void meta_generic_validation_post_create(
23612361
break;
23622362

23632363
case SAI_OBJECT_TYPE_FDB:
2364-
vlan_reference_inc(meta_key.key.fdb_entry.vlan_id);
2364+
// NOTE: we don't increase vlan reference on FDB entries, ignored
2365+
// vlan_reference_inc(meta_key.key.fdb_entry.vlan_id);
23652366
break;
23662367

23672368
case SAI_OBJECT_TYPE_VLAN:
@@ -2679,7 +2680,8 @@ void meta_generic_validation_post_remove(
26792680
break;
26802681

26812682
case SAI_OBJECT_TYPE_FDB:
2682-
vlan_reference_dec(meta_key.key.fdb_entry.vlan_id);
2683+
// NOTE: we don't decrease vlan reference on FDB entries, ignored
2684+
// vlan_reference_dec(meta_key.key.fdb_entry.vlan_id);
26832685
break;
26842686

26852687
case SAI_OBJECT_TYPE_TRAP:
@@ -3321,7 +3323,8 @@ sai_status_t meta_sai_get_switch(
33213323

33223324
sai_status_t meta_sai_validate_fdb_entry(
33233325
_In_ const sai_fdb_entry_t* fdb_entry,
3324-
_In_ bool create)
3326+
_In_ bool create,
3327+
_In_ bool get = false)
33253328
{
33263329
SWSS_LOG_ENTER();
33273330

@@ -3342,7 +3345,9 @@ sai_status_t meta_sai_validate_fdb_entry(
33423345
}
33433346

33443347
// check if vlan exists
3348+
// NOTE: this is disabled on purpose, we can create/set/get/remove fdb entris with non existing vlans
33453349

3350+
/*
33463351
sai_object_meta_key_t meta_key_vlan = { .object_type = SAI_OBJECT_TYPE_VLAN, .key = { .vlan_id = vlan_id } };
33473352
33483353
std::string key_vlan = get_object_meta_key_string(meta_key_vlan);
@@ -3353,6 +3358,7 @@ sai_status_t meta_sai_validate_fdb_entry(
33533358
33543359
return SAI_STATUS_INVALID_PARAMETER;
33553360
}
3361+
*/
33563362

33573363
// check if fdb entry exists
33583364

@@ -3374,7 +3380,7 @@ sai_status_t meta_sai_validate_fdb_entry(
33743380

33753381
// set, get, remove
33763382

3377-
if (!object_exists(key_fdb))
3383+
if (!object_exists(key_fdb) && !get)
33783384
{
33793385
SWSS_LOG_ERROR("object key %s don't exists", key_fdb.c_str());
33803386

@@ -3541,7 +3547,9 @@ sai_status_t meta_sai_get_fdb_entry(
35413547
{
35423548
SWSS_LOG_ENTER();
35433549

3544-
sai_status_t status = meta_sai_validate_fdb_entry(fdb_entry, false);
3550+
// NOTE: when doing get, entry may not exist on metadata db
3551+
3552+
sai_status_t status = meta_sai_validate_fdb_entry(fdb_entry, false, true);
35453553

35463554
if (status != SAI_STATUS_SUCCESS)
35473555
{
@@ -4838,3 +4846,99 @@ sai_status_t meta_sai_get_oid(
48384846

48394847
return status;
48404848
}
4849+
4850+
// NOTIFICATIONS
4851+
4852+
void meta_sai_on_fdb_event_single(
4853+
_In_ const sai_fdb_event_notification_data_t& data)
4854+
{
4855+
SWSS_LOG_ENTER();
4856+
4857+
// NOTE: vlan may not exists
4858+
4859+
sai_object_meta_key_t meta_key_vlan = { .object_type = SAI_OBJECT_TYPE_VLAN, .key = { .vlan_id = data.fdb_entry.vlan_id} };
4860+
4861+
std::string key_vlan = get_object_meta_key_string(meta_key_vlan);
4862+
4863+
if (!object_exists(key_vlan))
4864+
{
4865+
SWSS_LOG_WARN("object key %s don't exists", key_vlan.c_str());
4866+
}
4867+
4868+
const sai_object_meta_key_t meta_key_fdb = { .object_type = SAI_OBJECT_TYPE_FDB, .key = { .fdb_entry = data.fdb_entry } };
4869+
4870+
std::string key_fdb = get_object_meta_key_string(meta_key_fdb);
4871+
4872+
switch (data.event_type)
4873+
{
4874+
case SAI_FDB_EVENT_LEARNED:
4875+
4876+
if (object_exists(key_fdb))
4877+
{
4878+
SWSS_LOG_WARN("object key %s alearedy exists, but received LEARNED event", key_fdb.c_str());
4879+
break;
4880+
}
4881+
4882+
{
4883+
sai_attribute_t *list = data.attr;
4884+
uint32_t count = data.attr_count;
4885+
4886+
sai_attribute_t local[2]; // 2 for port id and type
4887+
4888+
if (count == 1)
4889+
{
4890+
// workaround for missing "TYPE" attribute on notification
4891+
4892+
local[0] = data.attr[0]; // copy 1st attr
4893+
local[1].id = SAI_FDB_ENTRY_ATTR_TYPE;
4894+
local[1].value.s32 = SAI_FDB_ENTRY_DYNAMIC; // assume learned entries are always dynamic
4895+
4896+
list = local;
4897+
count = 2; // now we added type
4898+
}
4899+
4900+
sai_status_t status = meta_generic_validation_create(meta_key_fdb, count, list);
4901+
4902+
if (status == SAI_STATUS_SUCCESS)
4903+
{
4904+
meta_generic_validation_post_create(meta_key_fdb, count, list);
4905+
}
4906+
else
4907+
{
4908+
SWSS_LOG_ERROR("failed to insert %s received in notification: %s", key_fdb.c_str(), sai_serialize_status(status).c_str());
4909+
}
4910+
}
4911+
4912+
break;
4913+
4914+
case SAI_FDB_EVENT_AGED:
4915+
case SAI_FDB_EVENT_FLUSHED:
4916+
4917+
if (!object_exists(key_fdb))
4918+
{
4919+
SWSS_LOG_WARN("object key %s don't exist but received AGED/FLUSHED event", key_fdb.c_str());
4920+
break;
4921+
}
4922+
4923+
meta_generic_validation_post_remove(meta_key_fdb);
4924+
4925+
break;
4926+
4927+
default:
4928+
4929+
SWSS_LOG_ERROR("got FDB_ENTRY notification with unknown event_type %d, bug?", data.event_type);
4930+
break;
4931+
}
4932+
}
4933+
4934+
void meta_sai_on_fdb_event(
4935+
_In_ uint32_t count,
4936+
_In_ sai_fdb_event_notification_data_t *data)
4937+
{
4938+
SWSS_LOG_ENTER();
4939+
4940+
for (uint32_t i = 0; i < count; ++i)
4941+
{
4942+
meta_sai_on_fdb_event_single(data[i]);
4943+
}
4944+
}

meta/sai_meta.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,4 +918,10 @@ extern sai_status_t meta_sai_get_route_entry(
918918
_Inout_ sai_attribute_t *attr_list,
919919
_In_ sai_get_route_attribute_fn get);
920920

921+
// NOTIFICATIONS
922+
923+
extern void meta_sai_on_fdb_event(
924+
_In_ uint32_t count,
925+
_In_ sai_fdb_event_notification_data_t *data);
926+
921927
#endif // __SAI_META_H__

meta/sai_meta_fdb.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ const sai_attr_metadata_t sai_fdb_attr_metadata[] = {
8888
.attrid = SAI_FDB_ENTRY_ATTR_PACKET_ACTION,
8989
.attridname = "SAI_FDB_ENTRY_ATTR_PACKET_ACTION",
9090
.serializationtype = SAI_SERIALIZATION_TYPE_INT32,
91-
.flags = SAI_ATTR_FLAGS_MANDATORY_ON_CREATE | SAI_ATTR_FLAGS_CREATE_ONLY,
91+
.flags = SAI_ATTR_FLAGS_CREATE_AND_SET,
9292
.allowedobjecttypes = { },
9393
.allownullobjectid = false,
94-
.defaultvaluetype = SAI_DEFAULT_VALUE_TYPE_NONE,
95-
.defaultvalue = { },
94+
.defaultvaluetype = SAI_DEFAULT_VALUE_TYPE_CONST,
95+
.defaultvalue = { .s32 = SAI_PACKET_ACTION_FORWARD },
9696
.enumtypestr = StringifyEnum ( sai_packet_action_t ),
9797
.enumallowedvalues = ENUM_VALUES ( sai_packet_action_t ),
9898
.enummetadata = &metadata_enum_sai_packet_action_t,

0 commit comments

Comments
 (0)