Skip to content

Commit 8808b0f

Browse files
mint570SRAVANI KANASANI
authored andcommitted
Adding usage documentation for Namelabelmapper
Signed-off-by: SRAVANI KANASANI <kanasanis@google.com>
1 parent 866a768 commit 8808b0f

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# SONiC NameLabelMapper Utility
2+
3+
## Table of Contents
4+
- [Introduction](#introduction)
5+
- [Purpose and Scope](#purpose and scope)
6+
- [Architecture and Design](#architecture and design)
7+
- [Developer Guide](#developer guide)
8+
- [Warm Boot Integration](#warm boot integration)
9+
- [Best Practices](#best practices)
10+
11+
## Introduction
12+
13+
The NameLabelMapper is a utility class within the orchagent designed to bridge the gap between human-readable/application-level object keys and fixed-length unique labels required by certain SAI attributes or hardware entries.
14+
15+
In SONiC, objects are often identified by complex strings (e.g., a combination of table names and entry keys). However, some SAI objects require a unique identifier that is easier to manage, persistent across restarts, and consistent during Warm Boot operations.
16+
17+
## Purpose and Scope
18+
19+
The primary goals of the NameLabelMapper are:
20+
21+
- **Persistent Mapping :** Maintain a 1:1 mapping between a complex object key and a unique label.
22+
- **Warm Boot Continuity :** Ensure that after an orchagent restart, the same object key receives the same label it had previously, preventing hardware/software mismatches.
23+
- **Centralized Management :** Provide a unified interface for different Orchestrators (e.g., P4Orch) to handle label allocation.
24+
- **Database Synchronization :** Automatically back up mappings to the STATE_DB to survive process crashes or planned reboots.
25+
26+
## Architecture and Design
27+
28+
### Data Storage
29+
30+
Mappings are stored in-memory using an array of hash maps, indexed by the SAI object type. This ensures that lookups are O(1) and namespaced by their functional category.
31+
32+
// Internal cache structure
33+
std::unordered_map<std::string, std::string> m_labelTables[SAI_OBJECT_TYPE_MAX];
34+
35+
### Database Schema
36+
37+
The mapper interacts with the STATE_DB to ensure persistence.
38+
39+
- **DB Name :** STATE_DB
40+
- **Table Name :** SAI_KEY_LABEL_MAP
41+
- **Key :** OBJECT_TYPE|TABLE_NAME:OBJECT_NAME (e.g., SAI_OBJECT_TYPE_POLICER|POLICER_TABLE:MyPolicer1)
42+
- **Field :** label
43+
- **Value :** The unique generated string (e.g., a microsecond timestamp).
44+
45+
### Label Generation
46+
47+
Labels are currently generated using high-resolution microsecond timestamps (system_clock). This provides a simple, monotonically increasing unique identifier suitable for most mapping requirements.
48+
49+
## Developer Guide
50+
51+
### Accessing the Mapper
52+
The mapper is instantiated globally in Orchdaemon. To use it in any orchestrator, include the header and reference the global pointer:
53+
```bash
54+
#include "namelabelmapper.h"
55+
```
56+
extern NameLabelMapper *gLabelMapper;
57+
58+
### Key Functions
59+
- **allocateLabel ** - Checks if a label exists for a key. If not, generates and saves a new one.
60+
- **addLabelToAttr ** - A helper that allocates a label and automatically populates a sai_attribute_t structure.
61+
- **getLabel** - Simple retrieval of an existing label. Returns false if not found.
62+
- **generateKeyFromTableAndObjectName** - Standardizes key creation by joining a table name and object name with a colon (:).
63+
- **saveMapperToDb** - Forces a synchronization of all in-memory mappings to the STATE_DB.
64+
65+
### Code Example: Allocating a Label for a Policer
66+
67+
std::string label;
68+
std::string mapper_key = gLabelMapper->generateKeyFromTableAndObjectName("APP_POLICER_TABLE", "Policer_A");
69+
70+
// Check if label exists, if not, it will be created
71+
if (!gLabelMapper->allocateLabel(SAI_OBJECT_TYPE_POLICER, mapper_key, label))
72+
{
73+
SWSS_LOG_NOTICE("New label %s allocated for Policer_A", label.c_str());
74+
}
75+
76+
// Alternatively, use the attribute helper
77+
sai_attribute_t attr;
78+
gLabelMapper->addLabelToAttr(SAI_OBJECT_TYPE_POLICER, "APP_POLICER_TABLE", "Policer_A", attr, SAI_POLICER_ATTR_LABEL, mapper_key, label);
79+
80+
## Warm Boot Integration
81+
The NameLabelMapper is a critical component for Warm Boot:
82+
83+
- **Restoration :** During OrchDaemon::warmRestoreAndSyncUp, readMapperFromDb() is called to reload all previous mappings from STATE_DB into the cache.
84+
- **Reconciliation :** Orchestrators perform their "bake" and "sync" cycles. By calling getLabel, they retrieve the identifiers used before the reboot, ensuring SAI attribute consistency.
85+
- **Cleanup :** Once reconciliation is finished, deleteMapperInDb() can be called to clear stale entries, followed by a fresh saveMapperToDb() to persist only the active state.
86+
87+
## Best Practices
88+
- **Consistency :** Always use generateKeyFromTableAndObjectName to avoid manual string concatenation errors.
89+
- **Object Types :** Ensure you use the correct sai_object_type_t to avoid collisions across different types of networking objects.
90+
- **Memory Management :** The mapper is intended to live for the duration of the orchagent process. Do not manually delete the global instance.

0 commit comments

Comments
 (0)