6
6
#include " notifier.h"
7
7
#include " sai_serialize.h"
8
8
#include " directory.h"
9
+ #include " notifications.h"
9
10
10
11
using namespace std ;
11
12
using namespace swss ;
@@ -21,6 +22,7 @@ extern sai_bfd_api_t* sai_bfd_api;
21
22
extern sai_object_id_t gSwitchId ;
22
23
extern sai_object_id_t gVirtualRouterId ;
23
24
extern PortsOrch* gPortsOrch ;
25
+ extern sai_switch_api_t * sai_switch_api;
24
26
extern Directory<Orch*> gDirectory ;
25
27
26
28
const map<string, sai_bfd_session_type_t > session_type_map =
@@ -57,6 +59,7 @@ BfdOrch::BfdOrch(DBConnector *db, string tableName, TableConnector stateDbBfdSes
57
59
m_bfdStateNotificationConsumer = new swss::NotificationConsumer (notificationsDb, " NOTIFICATIONS" );
58
60
auto bfdStateNotificatier = new Notifier (m_bfdStateNotificationConsumer, this , " BFD_STATE_NOTIFICATIONS" );
59
61
Orch::addExecutor (bfdStateNotificatier);
62
+ register_state_change_notif = false ;
60
63
}
61
64
62
65
BfdOrch::~BfdOrch (void )
@@ -152,8 +155,52 @@ void BfdOrch::doTask(NotificationConsumer &consumer)
152
155
}
153
156
}
154
157
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
+
155
193
bool BfdOrch::create_bfd_session (const string& key, const vector<FieldValueTuple>& data)
156
194
{
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
+ }
157
204
if (bfd_session_map.find (key) != bfd_session_map.end ())
158
205
{
159
206
SWSS_LOG_ERROR (" BFD session for %s already exists" , key.c_str ());
0 commit comments