diff --git a/orchagent/bfdorch.cpp b/orchagent/bfdorch.cpp index 7bc5f4bfd398..f40782ba2b4e 100644 --- a/orchagent/bfdorch.cpp +++ b/orchagent/bfdorch.cpp @@ -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; @@ -416,6 +417,12 @@ bool BfdOrch::create_bfd_session(const string& key, const vectorcreate_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); @@ -439,6 +446,38 @@ bool BfdOrch::create_bfd_session(const string& key, const vector &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 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()) @@ -487,3 +526,4 @@ uint32_t BfdOrch::bfd_src_port(void) return (port++); } + diff --git a/orchagent/bfdorch.h b/orchagent/bfdorch.h index 019af037b88f..4a0cb9edfba5 100644 --- a/orchagent/bfdorch.h +++ b/orchagent/bfdorch.h @@ -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 &attrs); + sai_status_t retry_create_bfd_session(sai_object_id_t &bfd_session_id, vector attrs); std::map bfd_session_map; std::map bfd_session_lookup;