From 964e2fe243bc63868d9cb9ebede7e886a0b1af39 Mon Sep 17 00:00:00 2001 From: Sudharsan Dhamal Gopalarathnam Date: Thu, 7 Apr 2022 15:59:56 -0700 Subject: [PATCH] [BFD]Retry create BFD with different source UDP port on failure (#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 --- orchagent/bfdorch.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ orchagent/bfdorch.h | 2 ++ 2 files changed, 42 insertions(+) diff --git a/orchagent/bfdorch.cpp b/orchagent/bfdorch.cpp index 7bc5f4bfd3..f40782ba2b 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 019af037b8..4a0cb9edfb 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;