Skip to content

Commit 8941cc0

Browse files
authored
[BFD]Registering BFD state change callback during session creation (sonic-net#2202)
* [BFD]Registering BFD state change callback during session creation
1 parent 680c539 commit 8941cc0

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

orchagent/bfdorch.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "notifier.h"
77
#include "sai_serialize.h"
88
#include "directory.h"
9+
#include "notifications.h"
910

1011
using namespace std;
1112
using namespace swss;
@@ -21,6 +22,7 @@ extern sai_bfd_api_t* sai_bfd_api;
2122
extern sai_object_id_t gSwitchId;
2223
extern sai_object_id_t gVirtualRouterId;
2324
extern PortsOrch* gPortsOrch;
25+
extern sai_switch_api_t* sai_switch_api;
2426
extern Directory<Orch*> gDirectory;
2527

2628
const map<string, sai_bfd_session_type_t> session_type_map =
@@ -57,6 +59,7 @@ BfdOrch::BfdOrch(DBConnector *db, string tableName, TableConnector stateDbBfdSes
5759
m_bfdStateNotificationConsumer = new swss::NotificationConsumer(notificationsDb, "NOTIFICATIONS");
5860
auto bfdStateNotificatier = new Notifier(m_bfdStateNotificationConsumer, this, "BFD_STATE_NOTIFICATIONS");
5961
Orch::addExecutor(bfdStateNotificatier);
62+
register_state_change_notif = false;
6063
}
6164

6265
BfdOrch::~BfdOrch(void)
@@ -152,8 +155,52 @@ void BfdOrch::doTask(NotificationConsumer &consumer)
152155
}
153156
}
154157

158+
bool BfdOrch::register_bfd_state_change_notification(void)
159+
{
160+
sai_attribute_t attr;
161+
sai_status_t status;
162+
sai_attr_capability_t capability;
163+
164+
status = sai_query_attribute_capability(gSwitchId, SAI_OBJECT_TYPE_SWITCH,
165+
SAI_SWITCH_ATTR_BFD_SESSION_STATE_CHANGE_NOTIFY,
166+
&capability);
167+
168+
if (status != SAI_STATUS_SUCCESS)
169+
{
170+
SWSS_LOG_ERROR("Unable to query the BFD change notification capability");
171+
return false;
172+
}
173+
174+
if (!capability.set_implemented)
175+
{
176+
SWSS_LOG_ERROR("BFD register change notification not supported");
177+
return false;
178+
}
179+
180+
attr.id = SAI_SWITCH_ATTR_BFD_SESSION_STATE_CHANGE_NOTIFY;
181+
attr.value.ptr = (void *)on_bfd_session_state_change;
182+
183+
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
184+
185+
if (status != SAI_STATUS_SUCCESS)
186+
{
187+
SWSS_LOG_ERROR("Failed to register BFD notification handler");
188+
return false;
189+
}
190+
return true;
191+
}
192+
155193
bool BfdOrch::create_bfd_session(const string& key, const vector<FieldValueTuple>& data)
156194
{
195+
if (!register_state_change_notif)
196+
{
197+
if (!register_bfd_state_change_notification())
198+
{
199+
SWSS_LOG_ERROR("BFD session for %s cannot be created", key.c_str());
200+
return false;
201+
}
202+
register_state_change_notif = true;
203+
}
157204
if (bfd_session_map.find(key) != bfd_session_map.end())
158205
{
159206
SWSS_LOG_ERROR("BFD session for %s already exists", key.c_str());

orchagent/bfdorch.h

+3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ class BfdOrch: public Orch, public Subject
2626
uint32_t bfd_gen_id(void);
2727
uint32_t bfd_src_port(void);
2828

29+
bool register_bfd_state_change_notification(void);
30+
2931
std::map<std::string, sai_object_id_t> bfd_session_map;
3032
std::map<sai_object_id_t, BfdUpdate> bfd_session_lookup;
3133

3234
swss::Table m_stateBfdSessionTable;
3335

3436
swss::NotificationConsumer* m_bfdStateNotificationConsumer;
37+
bool register_state_change_notif;
3538
};
3639

3740
#endif /* SWSS_BFDORCH_H */

orchagent/main.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,6 @@ int main(int argc, char **argv)
478478
attr.value.ptr = (void *)on_port_state_change;
479479
attrs.push_back(attr);
480480

481-
attr.id = SAI_SWITCH_ATTR_BFD_SESSION_STATE_CHANGE_NOTIFY;
482-
attr.value.ptr = (void *)on_bfd_session_state_change;
483-
attrs.push_back(attr);
484-
485481
attr.id = SAI_SWITCH_ATTR_SHUTDOWN_REQUEST_NOTIFY;
486482
attr.value.ptr = (void *)on_switch_shutdown_request;
487483
attrs.push_back(attr);

0 commit comments

Comments
 (0)