-
Notifications
You must be signed in to change notification settings - Fork 718
[Generic-P4Orch] Add infra of supporting unique label to be used for SAI label attribute. #4176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ksravani-hcl
wants to merge
1
commit into
sonic-net:master
Choose a base branch
from
ksravani-hcl:gautam_1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| #include "namelabelmapper.h" | ||
|
|
||
| #include <limits> | ||
| #include <sstream> | ||
| #include <string> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
| #include "logger.h" | ||
| #include "sai_serialize.h" | ||
|
|
||
| extern "C" | ||
| { | ||
| #include "sai.h" | ||
| } | ||
|
|
||
| using ::nlohmann::json; | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| std::string convertToDBField(_In_ const sai_object_type_t object_type, _In_ const std::string &key) | ||
| { | ||
| return sai_serialize_object_type(object_type) + "|" + key; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| NameLabelMapper::NameLabelMapper() : m_db("STATE_DB", 0), m_table(&m_db, "SAI_KEY_LABEL_MAP") | ||
| { | ||
| } | ||
|
|
||
| bool NameLabelMapper::setLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key, _In_ std::string &label) | ||
| { | ||
| SWSS_LOG_ENTER(); | ||
|
|
||
| if (existsLabel(object_type, key)) | ||
| { | ||
| SWSS_LOG_ERROR("Key %s with SAI object type %d already exists in label mapper", key.c_str(), object_type); | ||
| return false; | ||
| } | ||
|
|
||
| m_labelTables[object_type][key] = label; | ||
| SWSS_LOG_INFO("Created new label %s for Key %s with SAI object type %d", label.c_str(), key.c_str(), object_type); | ||
| return true; | ||
| } | ||
|
|
||
| bool NameLabelMapper::getLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key, | ||
| _Out_ std::string &label) | ||
| { | ||
| SWSS_LOG_ENTER(); | ||
|
|
||
| if (!existsLabel(object_type, key)) | ||
| { | ||
| SWSS_LOG_INFO("Key %s with SAI object type %d does not exist in label mapper", key.c_str(), object_type); | ||
| return false; | ||
| } | ||
|
|
||
| label = m_labelTables[object_type][key]; | ||
| return true; | ||
| } | ||
|
|
||
| bool NameLabelMapper::eraseLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key) | ||
| { | ||
| SWSS_LOG_ENTER(); | ||
|
|
||
| if (!existsLabel(object_type, key)) | ||
| { | ||
| SWSS_LOG_ERROR("Key %s with SAI object type %d does not exist in " | ||
| "label mapper when erasing", | ||
| key.c_str(), object_type); | ||
| return false; | ||
| } | ||
|
|
||
| m_labelTables[object_type].erase(key); | ||
| return true; | ||
| } | ||
|
|
||
| void NameLabelMapper::deleteMapperInDb() | ||
| { | ||
| SWSS_LOG_ENTER(); | ||
| m_table.del(""); | ||
| } | ||
|
|
||
| size_t NameLabelMapper::getNumEntries(_In_ sai_object_type_t object_type) const | ||
| { | ||
| SWSS_LOG_ENTER(); | ||
|
|
||
| return (m_labelTables[object_type].size()); | ||
| } | ||
|
|
||
| bool NameLabelMapper::existsLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key) const | ||
| { | ||
| SWSS_LOG_ENTER(); | ||
|
|
||
| return m_labelTables[object_type].find(key) != m_labelTables[object_type].end(); | ||
| } | ||
|
|
||
| std::string NameLabelMapper::generateKeyFromTableAndObjectName(std::string table_name, std::string object_name) | ||
| { | ||
| return table_name + ":" + object_name; | ||
| } | ||
|
|
||
| std::string NameLabelMapper::generateUniqueLabel() | ||
| { | ||
| char label_buf[UNIQUE_LABEL_SIZE]; | ||
| uint64_t msec = | ||
| std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()) | ||
| .count(); | ||
| snprintf(label_buf, UNIQUE_LABEL_SIZE, "%" PRIu64 "", msec); | ||
| return std::string(reinterpret_cast<char const *>(label_buf)); | ||
| } | ||
|
|
||
| bool NameLabelMapper::allocateLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key, | ||
| _Out_ std::string &label) | ||
| { | ||
| if (!getLabel(object_type, key, label)) | ||
| { | ||
| label = generateUniqueLabel(); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool NameLabelMapper::addLabelToAttr(sai_object_type_t object_type, const std::string &table_name, | ||
| const std::string &key, sai_attribute_t &attr, sai_attr_id_t attr_id, | ||
| std::string &mapper_key, std::string &label) | ||
| { | ||
| mapper_key = generateKeyFromTableAndObjectName(table_name, key); | ||
| bool label_present = allocateLabel(object_type, mapper_key, label); | ||
| attr.id = attr_id; | ||
| auto size = sizeof(attr.value.chardata); | ||
| snprintf(attr.value.chardata, size, "%s", label.c_str()); | ||
| SWSS_LOG_NOTICE("Add ATTR_LABEL %s for sai object %s for %s", label.c_str(), | ||
| sai_serialize_object_type(object_type).c_str(), mapper_key.c_str()); | ||
| return label_present; | ||
| } | ||
|
|
||
| std::string NameLabelMapper::dumpStateCache() | ||
| { | ||
| json cache = json({}); | ||
| for (int i = 0; i < SAI_OBJECT_TYPE_MAX; i++) | ||
| { | ||
| if (m_labelTables[i].empty()) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| json label_mapper_j = json({}); | ||
| for (const auto &kv_pair : m_labelTables[i]) | ||
| { | ||
| label_mapper_j[kv_pair.first] = kv_pair.second; | ||
| } | ||
| std::string sai_object_type = sai_serialize_object_type(static_cast<sai_object_type_t>(i)); | ||
| cache[sai_object_type] = label_mapper_j; | ||
| } | ||
| return cache.dump(4); | ||
| } | ||
|
|
||
| void NameLabelMapper::saveMapperToDb() | ||
| { | ||
| for (int i = 0; i < SAI_OBJECT_TYPE_MAX; i++) | ||
| { | ||
| if (m_labelTables[i].empty()) | ||
| { | ||
| continue; | ||
| } | ||
| for (const auto &kv_pair : m_labelTables[i]) | ||
| { | ||
| auto key = kv_pair.first; | ||
| auto label = kv_pair.second; | ||
| m_table.hset("", convertToDBField(static_cast<sai_object_type_t>(i), key), label); | ||
| SWSS_LOG_INFO("label %s for Key %s with SAI object type %d save into state_db", label.c_str(), key.c_str(), | ||
| i); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void NameLabelMapper::readMapperFromDb() | ||
| { | ||
| std::vector<swss::FieldValueTuple> tuples; | ||
| m_table.get("", tuples); | ||
| SWSS_LOG_INFO("m_table->get size %zd", tuples.size()); | ||
| for (auto &fv : tuples) | ||
| { | ||
| std::string combo = fvField(fv); | ||
| std::string label = fvValue(fv); | ||
| SWSS_LOG_INFO("Got field %s label %s from db", combo.c_str(), label.c_str()); | ||
|
|
||
| size_t pos = 0; | ||
| std::string obj_type_str, key; | ||
| if ((pos = combo.find("|")) != std::string::npos) | ||
| { | ||
| obj_type_str = combo.substr(0, pos); | ||
| key = combo.substr(pos + 1, combo.size() - pos - 1); | ||
|
|
||
| sai_object_type_t sai_object_type; | ||
| sai_deserialize_object_type(obj_type_str, sai_object_type); | ||
| setLabel(sai_object_type, key, label); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| std::string NameLabelMapper::verifyLabelMapping( | ||
| _In_ sai_object_type_t object_type, _In_ const std::string& key, | ||
| _In_ std::string label) { | ||
| SWSS_LOG_ENTER(); | ||
|
|
||
| std::string mapper_label; | ||
| if (!getLabel(object_type, key, mapper_label)) { | ||
| std::stringstream msg; | ||
| msg << "Label not found in mapper for key " << key; | ||
| return msg.str(); | ||
| } | ||
| if (mapper_label != label) { | ||
| std::stringstream msg; | ||
| msg << "Label mismatched in mapper for key " << key << ": " << label | ||
| << " vs " << mapper_label; | ||
| return msg.str(); | ||
| } | ||
|
|
||
| return ""; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #pragma once | ||
|
|
||
| #include <inttypes.h> | ||
|
|
||
| #include <chrono> | ||
| #include <cstdint> | ||
| #include <ctime> | ||
| #include <string> | ||
| #include <unordered_map> | ||
|
|
||
| #include "dbconnector.h" | ||
| #include "table.h" | ||
|
|
||
| extern "C" | ||
| { | ||
| #include "sai.h" | ||
| } | ||
|
|
||
| #define UNIQUE_LABEL_SIZE 32 | ||
|
|
||
| // Interface for mapping object name/key to unique Label. | ||
| // This class is not thread safe. | ||
| class NameLabelMapper | ||
| { | ||
| public: | ||
| NameLabelMapper(); | ||
| ~NameLabelMapper() = default; | ||
|
|
||
| // Sets label for the given key for the specific object_type. Returns false if | ||
| // the key already exists. | ||
| bool setLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key, _In_ std::string &label); | ||
|
|
||
| // Return true if label present in the mapper, and copy the label in the 3rd | ||
| // argument; or false, and a new unique label is allocated and saved in mapper | ||
| bool allocateLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key, _Out_ std::string &label); | ||
|
|
||
| // Gets label from mapper for the given key for the SAI object_type. | ||
| // Returns true on success. | ||
| bool getLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key, _Out_ std::string &label); | ||
|
|
||
| // Erases label for the given key for the SAI object_type. | ||
| // Returns true on success. | ||
| bool eraseLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key); | ||
|
|
||
| // Delete mapper table in db | ||
| void deleteMapperInDb(); | ||
|
|
||
| // Gets the number of labels for the SAI object_type. | ||
| size_t getNumEntries(_In_ sai_object_type_t object_type) const; | ||
|
|
||
| // SAI object subtype and name to label mapper name | ||
| // Returns concat of (subtype + object_name) | ||
| // For example, for POLICER, there are 4 subtypes: | ||
| // COPP trap group, ACL policer, storm policer, regular policer | ||
| // One way is use APPL_DB table name in subtype field, that is | ||
| // subtype = APPL_DB table name | ||
| std::string generateKeyFromTableAndObjectName(std::string table_name, std::string object_name); | ||
|
|
||
| // Add the unique label to an SAI attribute | ||
| // return true if the label was already present; | ||
| // return false if a new label is generated. | ||
| bool addLabelToAttr(sai_object_type_t object_type, const std::string &table_name, const std::string &key, | ||
| sai_attribute_t &attr, sai_attr_id_t attr_id, std::string &mapper_key, std::string &label); | ||
|
|
||
| // Save the all entries to state db | ||
| void saveMapperToDb(); | ||
|
|
||
| // Read the all entries from state db | ||
| void readMapperFromDb(); | ||
|
|
||
| // Returns a json string that contains each non-empty label mapper. | ||
| std::string dumpStateCache(); | ||
|
|
||
| // Checks whether label mapping exists for the given key for the specific | ||
| // object type. | ||
| bool existsLabel(_In_ sai_object_type_t object_type, _In_ const std::string &key) const; | ||
|
|
||
| // Verify the given label in the label mapper | ||
| std::string verifyLabelMapping(_In_ sai_object_type_t object_type, | ||
| _In_ const std::string& key, | ||
| _In_ std::string label); | ||
|
|
||
| private: | ||
| // Generate and return a unique label | ||
| std::string generateUniqueLabel(); | ||
|
|
||
| // Buckets of map tables, one for every SAI object type. | ||
| std::unordered_map<std::string, std::string> m_labelTables[SAI_OBJECT_TYPE_MAX]; | ||
|
|
||
| swss::DBConnector m_db; | ||
| swss::Table m_table; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.