From 195136512ac1242a4f2a038240b0fe8b098b6d96 Mon Sep 17 00:00:00 2001 From: vganesan-nokia <67648637+vganesan-nokia@users.noreply.github.com> Date: Wed, 10 Mar 2021 19:39:50 -0500 Subject: [PATCH] [vog/systemlag] Voq lagid allocator (#1603) What I did Defined class for lag id allocator and added lua script for allocating/freeing lag id in atomic fashion Why I did it For portchannels in VOQ based chassis systems we need unique lag id across the system. The lag id (aka system port aggreggator id) is allocated during portchannel creation. The changes are for a class for lag id allocation in atomic fashion. The LAG ID is allocated from central chassis app db. A lua script loaded in the redis at the time of lag id allocator instantiation ensures allocating unique lag id when multiple clients requests for lag id simultaneously. Ref: VOQ LAG HLD PR: Azure/SONiC#697 --- orchagent/lagid.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++ orchagent/lagid.h | 44 ++++++++++++++++++++ orchagent/lagids.lua | 88 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 orchagent/lagid.cpp create mode 100644 orchagent/lagid.h create mode 100644 orchagent/lagids.lua diff --git a/orchagent/lagid.cpp b/orchagent/lagid.cpp new file mode 100644 index 000000000000..45f489639f03 --- /dev/null +++ b/orchagent/lagid.cpp @@ -0,0 +1,96 @@ +#include "lagid.h" + +LagIdAllocator::LagIdAllocator( + _In_ DBConnector* chassis_app_db) +{ + SWSS_LOG_ENTER(); + + m_dbConnector = chassis_app_db; + + // Load lua script to allocate system lag id. This lua script ensures allocation + // of unique system lag id from global chassis app db in atomic fashion when allocation + // is requested by different asic instances simultaneously + + string luaScript = loadLuaScript("lagids.lua"); + m_shaLagId = loadRedisScript(m_dbConnector, luaScript); +} + +int32_t LagIdAllocator::lagIdAdd( + _In_ const string &pcname, + _In_ int32_t lag_id) +{ + SWSS_LOG_ENTER(); + + // No keys + vector keys; + + vector args; + args.push_back("add"); + args.push_back(pcname); + args.push_back(to_string(lag_id)); + + set ret = runRedisScript(*m_dbConnector, m_shaLagId, keys, args); + + if (!ret.empty()) + { + // We expect only one value in the set returned + + auto rv_lag_id = ret.begin(); + + return (stoi(*rv_lag_id)); + } + + return LAG_ID_ALLOCATOR_ERROR_DB_ERROR; +} + +int32_t LagIdAllocator::lagIdDel( + _In_ const string &pcname) +{ + SWSS_LOG_ENTER(); + + // No keys + vector keys; + + vector args; + args.push_back("del"); + args.push_back(pcname); + + set ret = runRedisScript(*m_dbConnector, m_shaLagId, keys, args); + + if (!ret.empty()) + { + // We expect only one value in the set returned + + auto rv_lag_id = ret.begin(); + + return (stoi(*rv_lag_id)); + } + + return LAG_ID_ALLOCATOR_ERROR_DB_ERROR; +} + +int32_t LagIdAllocator::lagIdGet( + _In_ const string &pcname) +{ + SWSS_LOG_ENTER(); + + // No keys + vector keys; + + vector args; + args.push_back("get"); + args.push_back(pcname); + + set ret = runRedisScript(*m_dbConnector, m_shaLagId, keys, args); + + if (!ret.empty()) + { + // We expect only one value in the set returned + + auto rv_lag_id = ret.begin(); + + return (stoi(*rv_lag_id)); + } + + return LAG_ID_ALLOCATOR_ERROR_DB_ERROR; +} diff --git a/orchagent/lagid.h b/orchagent/lagid.h new file mode 100644 index 000000000000..9b043741a1ed --- /dev/null +++ b/orchagent/lagid.h @@ -0,0 +1,44 @@ +#ifndef SWSS_LAGID_H +#define SWSS_LAGID_H + +#include "dbconnector.h" +#include "sal.h" +#include "schema.h" +#include "redisapi.h" + +using namespace swss; +using namespace std; + +#define LAG_ID_ALLOCATOR_ERROR_DELETE_ENTRY_NOT_FOUND 0 +#define LAG_ID_ALLOCATOR_ERROR_TABLE_FULL -1 +#define LAG_ID_ALLOCATOR_ERROR_GET_ENTRY_NOT_FOUND -2 +#define LAG_ID_ALLOCATOR_ERROR_INVALID_OP -3 +#define LAG_ID_ALLOCATOR_ERROR_DB_ERROR -4 + +class LagIdAllocator +{ +public: + + LagIdAllocator( + _In_ DBConnector* chassis_app_db); + +public: + + int32_t lagIdAdd( + _In_ const string &pcname, + _In_ int32_t lag_id); + + int32_t lagIdDel( + _In_ const string &pcname); + + int32_t lagIdGet( + _In_ const string &pcname); + +private: + + DBConnector* m_dbConnector; + + string m_shaLagId; +}; + +#endif // SWSS_LAGID_H diff --git a/orchagent/lagids.lua b/orchagent/lagids.lua new file mode 100644 index 000000000000..93a546cad196 --- /dev/null +++ b/orchagent/lagids.lua @@ -0,0 +1,88 @@ +-- KEYS - None +-- ARGV[1] - operation (add/del/get) +-- ARGV[2] - lag name +-- ARGV[3] - current lag id (for "add" operation only) + +-- return lagid if success for "add"/"del" +-- return 0 if lag does not exist for "del" +-- return -1 if lag table full for "add" +-- return -2 if lag does not exist for "get" +-- return -3 if invalid operation + +local op = ARGV[1] +local pcname = ARGV[2] + +local lagid_start = tonumber(redis.call("get", "SYSTEM_LAG_ID_START")) +local lagid_end = tonumber(redis.call("get", "SYSTEM_LAG_ID_END")) + +if op == "add" then + + local plagid = tonumber(ARGV[3]) + + local dblagid = redis.call("hget", "SYSTEM_LAG_ID_TABLE", pcname) + + if dblagid then + dblagid = tonumber(dblagid) + if plagid == 0 then + -- no lagid proposed. Return the existing lagid + return dblagid + end + end + + -- lagid allocation request with a lagid proposal + if plagid >= lagid_start and plagid <= lagid_end then + if plagid == dblagid then + -- proposed lagid is same as the lagid in database + return plagid + end + -- proposed lag id is different than that in database OR + -- the portchannel does not exist in the database + -- If proposed lagid is available, return the same proposed lag id + if redis.call("sismember", "SYSTEM_LAG_ID_SET", tostring(plagid)) == 0 then + redis.call("sadd", "SYSTEM_LAG_ID_SET", tostring(plagid)) + redis.call("srem", "SYSTEM_LAG_ID_SET", tostring(dblagid)) + redis.call("hset", "SYSTEM_LAG_ID_TABLE", pcname, tostring(plagid)) + return plagid + end + end + + local lagid = lagid_start + while lagid <= lagid_end do + if redis.call("sismember", "SYSTEM_LAG_ID_SET", tostring(lagid)) == 0 then + redis.call("sadd", "SYSTEM_LAG_ID_SET", tostring(lagid)) + redis.call("srem", "SYSTEM_LAG_ID_SET", tostring(dblagid)) + redis.call("hset", "SYSTEM_LAG_ID_TABLE", pcname, tostring(lagid)) + return lagid + end + lagid = lagid + 1 + end + + return -1 + +end + +if op == "del" then + + if redis.call("hexists", "SYSTEM_LAG_ID_TABLE", pcname) == 1 then + local lagid = redis.call("hget", "SYSTEM_LAG_ID_TABLE", pcname) + redis.call("srem", "SYSTEM_LAG_ID_SET", lagid) + redis.call("hdel", "SYSTEM_LAG_ID_TABLE", pcname) + return tonumber(lagid) + end + + return 0 + +end + +if op == "get" then + + if redis.call("hexists", "SYSTEM_LAG_ID_TABLE", pcname) == 1 then + local lagid = redis.call("hget", "SYSTEM_LAG_ID_TABLE", pcname) + return tonumber(lagid) + end + + return -2 + +end + +return -3