forked from sonic-net/sonic-sairedis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsai_redis_generic_create.cpp
More file actions
415 lines (319 loc) · 11.3 KB
/
sai_redis_generic_create.cpp
File metadata and controls
415 lines (319 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "sai_redis.h"
#include "meta/sai_serialize.h"
#include "meta/saiattributelist.h"
#include <inttypes.h>
bool switch_ids[MAX_SWITCHES] = {};
void redis_clear_switch_ids()
{
SWSS_LOG_ENTER();
for (int idx = 0; idx < MAX_SWITCHES; ++idx)
{
switch_ids[idx] = false;
}
}
int redis_get_free_switch_id_index()
{
SWSS_LOG_ENTER();
for (int index = 0; index < MAX_SWITCHES; ++index)
{
if (!switch_ids[index])
{
switch_ids[index] = true;
SWSS_LOG_NOTICE("got new switch index 0x%x", index);
return index;
}
}
SWSS_LOG_THROW("no more available switch id indexes");
}
/*
* NOTE: Need to be executed when removing switch.
*/
void redis_free_switch_id_index(int index)
{
SWSS_LOG_ENTER();
if (index < 0 || index >= MAX_SWITCHES)
{
SWSS_LOG_THROW("switch index is invalid 0x%x", index);
}
else
{
switch_ids[index] = false;
SWSS_LOG_DEBUG("marked switch index 0x%x as unused", index);
}
}
sai_object_id_t redis_construct_object_id(
_In_ sai_object_type_t object_type,
_In_ int switch_index,
_In_ uint64_t virtual_id)
{
SWSS_LOG_ENTER();
return (sai_object_id_t)(((uint64_t)switch_index << 56) | ((uint64_t)object_type << 48) | virtual_id);
}
sai_object_id_t redis_create_switch_virtual_object_id()
{
SWSS_LOG_ENTER();
/*
* NOTE: Switch ids are deterministic.
*/
int index = redis_get_free_switch_id_index();
return redis_construct_object_id(SAI_OBJECT_TYPE_SWITCH, index, index);
}
sai_object_type_t sai_object_type_query(
_In_ sai_object_id_t object_id)
{
SWSS_LOG_ENTER();
if (object_id == SAI_NULL_OBJECT_ID)
{
return SAI_OBJECT_TYPE_NULL;
}
sai_object_type_t ot = (sai_object_type_t)((object_id >> 48) & 0xFF);
if (ot == SAI_OBJECT_TYPE_NULL || ot >= SAI_OBJECT_TYPE_EXTENSIONS_MAX)
{
SWSS_LOG_ERROR("invalid object id 0x%" PRIx64, object_id);
/*
* We can't throw here, since it would give no meaningful message.
* Throwing at one level up is better.
*/
return SAI_OBJECT_TYPE_NULL;
}
return ot;
}
sai_object_id_t sai_switch_id_query(
_In_ sai_object_id_t oid)
{
SWSS_LOG_ENTER();
if (oid == SAI_NULL_OBJECT_ID)
{
return oid;
}
sai_object_type_t object_type = sai_object_type_query(oid);
if (object_type == SAI_OBJECT_TYPE_NULL)
{
SWSS_LOG_THROW("invalid object type of oid 0x%" PRIx64, oid);
}
if (object_type == SAI_OBJECT_TYPE_SWITCH)
{
return oid;
}
int sw_index = (int)((oid >> 56) & 0xFF);
sai_object_id_t sw_id = redis_construct_object_id(SAI_OBJECT_TYPE_SWITCH, sw_index, sw_index);
return sw_id;
}
int redis_get_switch_id_index(
_In_ sai_object_id_t switch_id)
{
SWSS_LOG_ENTER();
sai_object_type_t switch_object_type = sai_object_type_query(switch_id);
if (switch_object_type == SAI_OBJECT_TYPE_SWITCH)
{
return (int)((switch_id >> 56) & 0xFF);
}
SWSS_LOG_THROW("object type of switch %s is %s, should be SWITCH",
sai_serialize_object_id(switch_id).c_str(),
sai_serialize_object_type(switch_object_type).c_str());
}
sai_object_id_t redis_create_virtual_object_id(
_In_ sai_object_type_t object_type,
_In_ sai_object_id_t switch_id)
{
SWSS_LOG_ENTER();
if ((object_type <= SAI_OBJECT_TYPE_NULL) ||
(object_type >= SAI_OBJECT_TYPE_EXTENSIONS_MAX))
{
SWSS_LOG_THROW("invalid objct type: %d", object_type);
}
// object_id:
// bits 63..56 - switch index
// bits 55..48 - object type
// bits 47..0 - object id
if (object_type == SAI_OBJECT_TYPE_SWITCH)
{
sai_object_id_t object_id = redis_create_switch_virtual_object_id();
SWSS_LOG_DEBUG("created SWITCH VID 0x%" PRIx64, object_id);
return object_id;
}
int index = redis_get_switch_id_index(switch_id);
uint64_t virtual_id = g_redisClient->incr("VIDCOUNTER");
sai_object_id_t object_id = redis_construct_object_id(object_type, index, virtual_id);
SWSS_LOG_DEBUG("created VID 0x%" PRIx64, object_id);
return object_id;
}
void redis_free_virtual_object_id(
_In_ sai_object_id_t object_id)
{
SWSS_LOG_ENTER();
if (sai_object_type_query(object_id) == SAI_OBJECT_TYPE_SWITCH)
{
redis_free_switch_id_index(redis_get_switch_id_index(object_id));
}
}
sai_status_t internal_redis_generic_create(
_In_ sai_object_type_t object_type,
_In_ const std::string &serialized_object_id,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
std::vector<swss::FieldValueTuple> entry = SaiAttributeList::serialize_attr_list(
object_type,
attr_count,
attr_list,
false);
if (entry.size() == 0)
{
// make sure that we put object into db
// even if there are no attributes set
swss::FieldValueTuple null("NULL", "NULL");
entry.push_back(null);
}
std::string str_object_type = sai_serialize_object_type(object_type);
std::string key = str_object_type + ":" + serialized_object_id;
SWSS_LOG_DEBUG("generic create key: %s, fields: %" PRIu64, key.c_str(), entry.size());
if (g_record)
{
recordLine("c|" + key + "|" + joinFieldValues(entry));
}
g_asicState->set(key, entry, "create");
return internal_api_wait_for_response(SAI_COMMON_API_CREATE);
}
sai_status_t redis_generic_create(
_In_ sai_object_type_t object_type,
_Out_ sai_object_id_t* object_id,
_In_ sai_object_id_t switch_id,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();
// on create vid is put in db by syncd
*object_id = redis_create_virtual_object_id(object_type, switch_id);
if (*object_id == SAI_NULL_OBJECT_ID)
{
SWSS_LOG_ERROR("failed to create %s, with switch id: %s",
sai_serialize_object_type(object_type).c_str(),
sai_serialize_object_id(switch_id).c_str());
return SAI_STATUS_INSUFFICIENT_RESOURCES;
}
std::string str_object_id = sai_serialize_object_id(*object_id);
return internal_redis_generic_create(
object_type,
str_object_id,
attr_count,
attr_list);
}
sai_status_t redis_bulk_generic_create(
_In_ sai_object_type_t object_type,
_In_ uint32_t object_count,
_Out_ sai_object_id_t *object_id, /* array */
_In_ sai_object_id_t switch_id,
_In_ const uint32_t *attr_count, /* array */
_In_ const sai_attribute_t *const *attr_list, /* array */
_Inout_ sai_status_t *object_statuses) /* array */
{
SWSS_LOG_ENTER();
std::vector<std::string> serialized_object_ids;
// on create vid is put in db by syncd
for (uint32_t idx = 0; idx < object_count; idx++)
{
object_id[idx] = redis_create_virtual_object_id(object_type, switch_id);
if (object_id[idx] == SAI_NULL_OBJECT_ID)
{
SWSS_LOG_ERROR("failed to create %s, with switch id: %s",
sai_serialize_object_type(object_type).c_str(),
sai_serialize_object_id(switch_id).c_str());
return SAI_STATUS_INSUFFICIENT_RESOURCES;
}
std::string str_object_id = sai_serialize_object_id(object_id[idx]);
serialized_object_ids.push_back(str_object_id);
}
return internal_redis_bulk_generic_create(
object_type,
serialized_object_ids,
attr_count,
attr_list,
object_statuses);
}
sai_status_t internal_redis_bulk_generic_create(
_In_ sai_object_type_t object_type,
_In_ const std::vector<std::string> &serialized_object_ids,
_In_ const uint32_t *attr_count,
_In_ const sai_attribute_t *const *attr_list,
_Inout_ sai_status_t *object_statuses)
{
SWSS_LOG_ENTER();
std::string str_object_type = sai_serialize_object_type(object_type);
std::vector<swss::FieldValueTuple> entries;
std::vector<swss::FieldValueTuple> entriesWithStatus;
/*
* We are recording all entries and their statuses, but we send to sairedis
* only those that succeeded metadata check, since only those will be
* executed on syncd, so there is no need with bothering decoding statuses
* on syncd side.
*/
for (size_t idx = 0; idx < serialized_object_ids.size(); ++idx)
{
std::vector<swss::FieldValueTuple> entry =
SaiAttributeList::serialize_attr_list(object_type, attr_count[idx], &attr_list[idx][0], false);
std::string str_attr = joinFieldValues(entry);
std::string str_status = sai_serialize_status(object_statuses[idx]);
std::string joined = str_attr + "|" + str_status;
swss::FieldValueTuple fvt(serialized_object_ids[idx] , joined);
entriesWithStatus.push_back(fvt);
if (object_statuses[idx] != SAI_STATUS_SUCCESS)
{
SWSS_LOG_WARN("skipping %s since status is %s",
serialized_object_ids[idx].c_str(),
str_status.c_str());
continue;
}
swss::FieldValueTuple fvtNoStatus(serialized_object_ids[idx] , str_attr);
entries.push_back(fvtNoStatus);
}
/*
* We are adding number of entries to actually add ':' to be compatible
* with previous
*/
if (g_record)
{
std::string joined;
for (const auto &e: entriesWithStatus)
{
// ||obj_id|attr=val|attr=val|status||obj_id|attr=val|attr=val|status
joined += "||" + fvField(e) + "|" + fvValue(e);
}
/*
* Capital 'C' stands for bulk CREATE operation.
*/
recordLine("C|" + str_object_type + joined);
}
// key: object_type:count
// field: object_id
// value: object_attrs
std::string key = str_object_type + ":" + std::to_string(entries.size());
if (entries.size())
{
g_asicState->set(key, entries, "bulkcreate");
}
return internal_api_wait_for_response(SAI_COMMON_API_BULK_CREATE);
}
#define REDIS_ENTRY_CREATE(OT,ot) \
sai_status_t redis_generic_create_ ## ot( \
_In_ const sai_ ## ot ## _t * entry, \
_In_ uint32_t attr_count, \
_In_ const sai_attribute_t *attr_list) \
{ \
SWSS_LOG_ENTER(); \
std::string str = sai_serialize_ ## ot(*entry); \
return internal_redis_generic_create( \
SAI_OBJECT_TYPE_ ## OT, \
str, \
attr_count, \
attr_list); \
}
REDIS_ENTRY_CREATE(FDB_ENTRY,fdb_entry);
REDIS_ENTRY_CREATE(INSEG_ENTRY,inseg_entry);
REDIS_ENTRY_CREATE(IPMC_ENTRY,ipmc_entry);
REDIS_ENTRY_CREATE(L2MC_ENTRY,l2mc_entry);
REDIS_ENTRY_CREATE(MCAST_FDB_ENTRY,mcast_fdb_entry);
REDIS_ENTRY_CREATE(NEIGHBOR_ENTRY,neighbor_entry);
REDIS_ENTRY_CREATE(ROUTE_ENTRY,route_entry);
REDIS_ENTRY_CREATE(NAT_ENTRY,nat_entry);