Skip to content

Commit 3516228

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

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
```
34+
std::unordered_map<std::string, std::string> m_labelTables[SAI_OBJECT_TYPE_MAX];
35+
```
36+
37+
### Database Schema
38+
39+
The mapper interacts with the STATE_DB to ensure persistence.
40+
41+
- **DB Name :** STATE_DB
42+
- **Table Name :** SAI_KEY_LABEL_MAP
43+
- **Key :** OBJECT_TYPE|TABLE_NAME:OBJECT_NAME (e.g., SAI_OBJECT_TYPE_POLICER|POLICER_TABLE:MyPolicer1)
44+
- **Field :** label
45+
- **Value :** The unique generated string (e.g., a microsecond timestamp).
46+
47+
### Label Generation
48+
49+
Labels are currently generated using high-resolution microsecond timestamps (system_clock). This provides a simple, monotonically increasing unique identifier suitable for most mapping requirements.
50+
51+
## Developer Guide
52+
53+
### Accessing the Mapper
54+
The mapper is instantiated globally in Orchdaemon. To use it in any orchestrator, include the header and reference the global pointer:
55+
```bash
56+
#include "namelabelmapper.h"
57+
extern NameLabelMapper *gLabelMapper;
58+
```
59+
60+
### Key Functions
61+
62+
| Function | Description |
63+
| -------- | ----------- |
64+
| **`allocateLabel`** | Checks if a label exists for a key. If not, generates and saves a new one. |
65+
| **`addLabelToAttr`** | A helper that allocates a label and automatically populates a sai_attribute_t structure. |
66+
| **`getLabel`** | Simple retrieval of an existing label. Returns false if not found. |
67+
| **`generateKeyFromTableAndObjectName`** | Standardizes key creation by joining a table name and object name with a colon (:). |
68+
| **`saveMapperToDb`** | Forces a synchronization of all in-memory mappings to the STATE_DB. |
69+
70+
### Code Example: Allocating a Label for a Policer
71+
72+
```
73+
std::string label;
74+
std::string mapper_key = gLabelMapper->generateKeyFromTableAndObjectName("APP_POLICER_TABLE", "Policer_A");
75+
76+
// Check if label exists, if not, it will be created
77+
if (!gLabelMapper->allocateLabel(SAI_OBJECT_TYPE_POLICER, mapper_key, label))
78+
{
79+
SWSS_LOG_NOTICE("New label %s allocated for Policer_A", label.c_str());
80+
}
81+
82+
// Alternatively, use the attribute helper
83+
sai_attribute_t attr;
84+
gLabelMapper->addLabelToAttr(SAI_OBJECT_TYPE_POLICER, "APP_POLICER_TABLE", "Policer_A", attr, SAI_POLICER_ATTR_LABEL, mapper_key, label);
85+
```
86+
87+
## Warm Boot Integration
88+
The NameLabelMapper is a critical component for Warm Boot:
89+
90+
- **Restoration :** During OrchDaemon::warmRestoreAndSyncUp, readMapperFromDb() is called to reload all previous mappings from STATE_DB into the cache.
91+
- **Reconciliation :** Orchestrators perform their "bake" and "sync" cycles. By calling getLabel, they retrieve the identifiers used before the reboot, ensuring SAI attribute consistency.
92+
- **Cleanup :** Once reconciliation is finished, deleteMapperInDb() can be called to clear stale entries, followed by a fresh saveMapperToDb() to persist only the active state.
93+
94+
## Best Practices
95+
- **Consistency :** Always use generateKeyFromTableAndObjectName to avoid manual string concatenation errors.
96+
- **Object Types :** Ensure you use the correct sai_object_type_t to avoid collisions across different types of networking objects.
97+
- **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)