Skip to content

Commit

Permalink
[BFD]Retry create BFD with different source UDP port on failure (soni…
Browse files Browse the repository at this point in the history
…c-net#2225)

* [BFD]Retry create BFD with different source UDP port on failure. The create_bfd_session API can fail when a source port already used by a different application is passed to SAI create. To avoid failure, a retry logic is added to try with a different port number three times before failing
  • Loading branch information
dgsudharsan authored and preetham-singh committed Aug 6, 2022
1 parent 4742096 commit 964e2fe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions orchagent/bfdorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using namespace swss;
#define BFD_SESSION_MILLISECOND_TO_MICROSECOND 1000
#define BFD_SRCPORTINIT 49152
#define BFD_SRCPORTMAX 65536
#define NUM_BFD_SRCPORT_RETRIES 3

extern sai_bfd_api_t* sai_bfd_api;
extern sai_object_id_t gSwitchId;
Expand Down Expand Up @@ -416,6 +417,12 @@ bool BfdOrch::create_bfd_session(const string& key, const vector<FieldValueTuple

sai_object_id_t bfd_session_id = SAI_NULL_OBJECT_ID;
sai_status_t status = sai_bfd_api->create_bfd_session(&bfd_session_id, gSwitchId, (uint32_t)attrs.size(), attrs.data());

if (status != SAI_STATUS_SUCCESS)
{
status = retry_create_bfd_session(bfd_session_id, attrs);
}

if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create bfd session %s, rv:%d", key.c_str(), status);
Expand All @@ -439,6 +446,38 @@ bool BfdOrch::create_bfd_session(const string& key, const vector<FieldValueTuple
return true;
}

void BfdOrch::update_port_number(vector<sai_attribute_t> &attrs)
{
for (uint32_t attr_idx = 0; attr_idx < (uint32_t)attrs.size(); attr_idx++)
{
if (attrs[attr_idx].id == SAI_BFD_SESSION_ATTR_UDP_SRC_PORT)
{
auto old_num = attrs[attr_idx].value.u32;
attrs[attr_idx].value.u32 = bfd_src_port();
SWSS_LOG_WARN("BFD create using port number %d failed. Retrying with port number %d",
old_num, attrs[attr_idx].value.u32);
return;
}
}
}

sai_status_t BfdOrch::retry_create_bfd_session(sai_object_id_t &bfd_session_id, vector<sai_attribute_t> attrs)
{
sai_status_t status = SAI_STATUS_FAILURE;

for (int retry = 0; retry < NUM_BFD_SRCPORT_RETRIES; retry++)
{
update_port_number(attrs);
status = sai_bfd_api->create_bfd_session(&bfd_session_id, gSwitchId,
(uint32_t)attrs.size(), attrs.data());
if (status == SAI_STATUS_SUCCESS)
{
return status;
}
}
return status;
}

bool BfdOrch::remove_bfd_session(const string& key)
{
if (bfd_session_map.find(key) == bfd_session_map.end())
Expand Down Expand Up @@ -487,3 +526,4 @@ uint32_t BfdOrch::bfd_src_port(void)

return (port++);
}

2 changes: 2 additions & 0 deletions orchagent/bfdorch.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class BfdOrch: public Orch, public Subject
uint32_t bfd_src_port(void);

bool register_bfd_state_change_notification(void);
void update_port_number(std::vector<sai_attribute_t> &attrs);
sai_status_t retry_create_bfd_session(sai_object_id_t &bfd_session_id, vector<sai_attribute_t> attrs);

std::map<std::string, sai_object_id_t> bfd_session_map;
std::map<sai_object_id_t, BfdUpdate> bfd_session_lookup;
Expand Down

0 comments on commit 964e2fe

Please sign in to comment.