forked from Mellanox/SAI-P4-BM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch_meta_data.h
584 lines (541 loc) · 18.1 KB
/
switch_meta_data.h
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#ifndef SWITCH_META_DATA_H
#define SWITCH_META_DATA_H
#define NULL_HANDLE -1
#include "spdlog/spdlog.h"
#include <iostream>
#include <list>
#include <map>
#include <sai.h>
#include <standard_types.h>
#include <simple_pre_lag_types.h>
#include <vector>
using namespace bm_runtime::standard;
using namespace bm_runtime::simple_pre_lag;
class sai_id_map_t { // object pointer and it's id
protected:
std::map<sai_object_id_t, void *> id_map;
std::vector<sai_object_id_t> unused_id;
public:
sai_id_map_t() {
// init
unused_id.clear();
id_map.clear();
}
~sai_id_map_t() {}
void free_id(sai_object_id_t sai_object_id) {
spdlog::get("logger")->debug("freeing object with sai_id {}",
sai_object_id);
delete id_map[sai_object_id];
id_map.erase(sai_object_id);
unused_id.push_back(sai_object_id);
}
sai_object_id_t get_new_id(void *obj_ptr) { // pointer to object
sai_object_id_t id;
if (!unused_id.empty()) {
id = unused_id.back();
unused_id.pop_back();
} else {
id = id_map.size() + 1;
}
id_map[id] = obj_ptr;
return id;
}
void *get_object(sai_object_id_t sai_object_id) {
return id_map[sai_object_id];
}
};
class Sai_obj {
public:
sai_object_id_t sai_object_id; // TODO maybe use the map and don't save here
Sai_obj(sai_id_map_t *sai_id_map_ptr) {
sai_object_id =
sai_id_map_ptr->get_new_id(this); // sai_id_map. set map to true.
}
~Sai_obj() {
// free_id(sai_object_id); TODO: fix this
}
};
class Port_obj : public Sai_obj {
public:
uint32_t hw_port;
uint32_t l2_if;
uint32_t pvid;
uint32_t bind_mode;
uint32_t mtu;
uint32_t drop_tagged;
uint32_t drop_untagged;
bool is_lag;
BmEntryHandle handle_lag_if;
BmEntryHandle handle_port_cfg;
BmEntryHandle handle_ingress_lag;
Port_obj(sai_id_map_t *sai_id_map_ptr) : Sai_obj(sai_id_map_ptr) {
// printf("create port object");
this->mtu = 1512;
this->drop_tagged = 0;
this->drop_untagged = 0;
this->hw_port = 0;
this->l2_if = 0;
this->pvid = 1;
this->bind_mode = SAI_PORT_BIND_MODE_PORT;
this->is_lag = false;
this->handle_ingress_lag = NULL_HANDLE;
this->handle_port_cfg = NULL_HANDLE;
this->handle_lag_if = NULL_HANDLE;
}
};
class BridgePort_obj : public Sai_obj {
public:
uint32_t port_id;
uint32_t vlan_id;
uint32_t bridge_port;
sai_bridge_port_type_t bridge_port_type;
sai_object_id_t bridge_id;
BmEntryHandle handle_id_1d;
BmEntryHandle handle_egress_set_vlan;
BmEntryHandle handle_egress_br_port_to_if;
BmEntryHandle handle_subport_ingress_interface_type;
BmEntryHandle handle_port_ingress_interface_type;
std::map<uint32_t, BmEntryHandle>
handle_fdb_port; // per bridge_id, valid for .1Q
std::map<uint32_t, BmEntryHandle>
handle_fdb_learn_port; // per bridge_id, valid for .1Q
BmEntryHandle handle_fdb_sub_port; // valid for .1D
BmEntryHandle handle_fdb_learn_sub_port; // valid for .1D
// BmEntryHandle handle_cfg; // TODO
BridgePort_obj(sai_id_map_t *sai_id_map_ptr) : Sai_obj(sai_id_map_ptr) {
this->port_id = 0;
this->vlan_id = 1;
this->bridge_port = NULL;
this->bridge_id = NULL;
this->bridge_port_type = SAI_BRIDGE_PORT_TYPE_PORT;
// TODO NULL_HANDLE is inavlid. consider other notation
this->handle_id_1d = NULL_HANDLE;
this->handle_egress_set_vlan = NULL_HANDLE;
this->handle_egress_br_port_to_if = NULL_HANDLE;
this->handle_subport_ingress_interface_type = NULL_HANDLE;
this->handle_port_ingress_interface_type = NULL_HANDLE;
this->handle_fdb_sub_port = NULL_HANDLE;
this->handle_fdb_learn_sub_port = NULL_HANDLE;
}
bool does_fdb_exist(uint32_t bridge_id) {
if (bridge_port_type == SAI_BRIDGE_PORT_TYPE_SUB_PORT) {
return (handle_fdb_sub_port != NULL_HANDLE);
} else {
return (handle_fdb_port.count(bridge_id) == 1);
}
}
void set_fdb_handle(BmEntryHandle handle_fdb, BmEntryHandle handle_learn_fdb,
uint32_t bridge_id) {
if (bridge_port_type == SAI_BRIDGE_PORT_TYPE_SUB_PORT) {
handle_fdb_sub_port = handle_fdb;
handle_fdb_learn_sub_port = handle_learn_fdb;
} else {
handle_fdb_port[bridge_id] = handle_fdb;
handle_fdb_learn_port[bridge_id] = handle_learn_fdb;
}
}
void remove_fdb_handle(uint32_t bridge_id) {
if (bridge_port_type == SAI_BRIDGE_PORT_TYPE_SUB_PORT) {
handle_fdb_sub_port = NULL_HANDLE;
handle_fdb_learn_sub_port = NULL_HANDLE;
} else {
handle_fdb_port.erase(bridge_id);
handle_fdb_learn_port.erase(bridge_id);
}
}
};
class Bridge_obj : public Sai_obj {
public:
sai_bridge_type_t bridge_type;
std::vector<sai_object_id_t> bridge_port_list;
uint32_t bridge_id; // Valid for .1D bridges.
BmMcMgrpHandle handle_mc_mgrp; // Valid for .1D bridge
BmMcL1Handle handle_mc_l1; // Valid for .1D bridge
Bridge_obj(sai_id_map_t *sai_id_map_ptr) : Sai_obj(sai_id_map_ptr) {
this->bridge_type = SAI_BRIDGE_TYPE_1Q;
this->bridge_port_list.clear();
this->bridge_id = 0;
this->handle_mc_mgrp = NULL_HANDLE;
this->handle_mc_l1 = NULL_HANDLE;
}
};
class Vlan_obj : public Sai_obj {
public:
uint16_t vid;
std::vector<sai_object_id_t> vlan_members;
BmEntryHandle handle_id_1q;
BmEntryHandle handle_router_ingress_vlan_filtering;
BmEntryHandle handle_router_egress_vlan_filtering;
uint32_t bridge_id; // Valid for .1Q bridge
BmEntryHandle handle_broadcast; // Valid for .1Q bridge
BmEntryHandle handle_flood; // Valid for .1Q bridge
BmMcMgrpHandle handle_mc_mgrp; // Valid for .1Q bridge
BmMcL1Handle handle_mc_l1; // Valid for .1Q bridge
Vlan_obj(sai_id_map_t *sai_id_map_ptr) : Sai_obj(sai_id_map_ptr) {
this->vlan_members.clear();
this->vid = 0;
this->handle_id_1q = NULL_HANDLE;
this->handle_router_ingress_vlan_filtering = NULL_HANDLE;
this->handle_router_egress_vlan_filtering = NULL_HANDLE;
this->handle_mc_mgrp = NULL_HANDLE;
this->handle_mc_l1 = NULL_HANDLE;
this->handle_flood = NULL_HANDLE;
this->handle_broadcast = NULL_HANDLE;
}
};
class Vlan_member_obj : public Sai_obj {
public:
sai_object_id_t bridge_port_id;
sai_object_id_t vlan_oid;
uint32_t tagging_mode;
uint16_t vid;
BmEntryHandle handle_egress_vlan_tag;
BmEntryHandle handle_egress_vlan_filtering;
BmEntryHandle handle_ingress_vlan_filtering;
Vlan_member_obj(sai_id_map_t *sai_id_map_ptr) : Sai_obj(sai_id_map_ptr) {
this->vid = NULL_HANDLE;
this->vlan_oid = NULL_HANDLE; // TODO needed? consider remove.
this->tagging_mode = SAI_VLAN_TAGGING_MODE_UNTAGGED;
this->bridge_port_id = NULL_HANDLE;
}
};
class Lag_obj : public Sai_obj {
public:
uint32_t l2_if;
Port_obj *port_obj;
std::vector<sai_object_id_t> lag_members;
BmEntryHandle handle_lag_hash;
BmEntryHandle handle_port_configurations;
Lag_obj(sai_id_map_t *sai_id_map_ptr) : Sai_obj(sai_id_map_ptr) {
this->lag_members.clear();
this->l2_if = 0;
this->handle_lag_hash = NULL_HANDLE;
this->handle_port_configurations = NULL_HANDLE;
this->port_obj = nullptr;
}
};
class Lag_member_obj : public Sai_obj {
public:
Port_obj *port;
Lag_obj *lag;
BmEntryHandle handle_egress_lag;
Lag_member_obj(sai_id_map_t *sai_id_map_ptr) : Sai_obj(sai_id_map_ptr) {
this->port = NULL;
this->lag = NULL;
this->handle_egress_lag = NULL_HANDLE;
}
};
class HostIF_obj : public Sai_obj {
public:
Port_obj *port;
sai_hostif_type_t hostif_type;
std::string netdev_name;
int netdev_fd;
std::thread netdev_thread;
HostIF_obj(sai_id_map_t *sai_id_map_ptr) : Sai_obj(sai_id_map_ptr) {
this->port = nullptr;
this->hostif_type = SAI_HOSTIF_TYPE_NETDEV;
this->netdev_name = "";
}
};
class HostIF_Table_Entry_obj : public Sai_obj {
public:
sai_hostif_table_entry_type_t entry_type;
sai_hostif_table_entry_channel_type_t channel_type;
uint16_t trap_id;
HostIF_Table_Entry_obj(sai_id_map_t *sai_id_map_ptr)
: Sai_obj(sai_id_map_ptr) {
this->trap_id = 0;
this->entry_type = SAI_HOSTIF_TABLE_ENTRY_TYPE_TRAP_ID;
this->channel_type =
SAI_HOSTIF_TABLE_ENTRY_CHANNEL_TYPE_NETDEV_PHYSICAL_PORT;
}
};
class HostIF_Trap_obj : public Sai_obj {
public:
sai_hostif_trap_type_t trap_type;
sai_packet_action_t trap_action;
uint16_t trap_id;
BmEntryHandle handle_l2_trap;
BmEntryHandle handle_trap_id;
HostIF_Trap_obj(sai_id_map_t *sai_id_map_ptr) : Sai_obj(sai_id_map_ptr) {
this->trap_id = 0;
}
};
class HostIF_Trap_Group_obj : public Sai_obj {
public:
HostIF_Trap_Group_obj(sai_id_map_t *sai_id_map_ptr)
: Sai_obj(sai_id_map_ptr) {}
};
class VirtualRouter_obj : public Sai_obj {
public:
bool v4_state;
bool v6_state;
uint32_t vrf;
VirtualRouter_obj(sai_id_map_t *sai_id_map_ptr)
: Sai_obj(sai_id_map_ptr) {
this->vrf = 0;
this->v4_state = true;
this->v6_state = true;
}
};
class RouterInterface_obj : public Sai_obj {
public:
sai_mac_t mac;
bool mac_valid;
uint16_t vid;
uint32_t rif_id;
sai_router_interface_type_t type;
BmEntryHandle handle_l3_interface;
BmEntryHandle handle_egress_vlan_tag;
BmEntryHandle handle_egress_l3;
BmEntryHandle handle_ingress_l3;
RouterInterface_obj(sai_id_map_t *sai_id_map_ptr)
: Sai_obj(sai_id_map_ptr) {
this->vid = 1;
this->mac_valid = false;
this->rif_id = 0;
this->type = SAI_ROUTER_INTERFACE_TYPE_VLAN;
this->handle_l3_interface = NULL_HANDLE;
this->handle_egress_vlan_tag = NULL_HANDLE;
this->handle_egress_l3 = NULL_HANDLE;
this->handle_ingress_l3 = NULL_HANDLE;
}
};
class NextHop_obj : public Sai_obj {
public:
uint32_t nhop_id;
sai_next_hop_type_t type;
sai_ip_address_t ip;
RouterInterface_obj *rif;
BmEntryHandle hanlde_table_nhop;
NextHop_obj(sai_id_map_t *sai_id_map_ptr)
: Sai_obj(sai_id_map_ptr) {
this->nhop_id = 0;
this->type = SAI_NEXT_HOP_TYPE_IP;
this->rif = NULL;
this->hanlde_table_nhop = NULL_HANDLE;
}
};
typedef std::map<sai_object_id_t, BridgePort_obj *> bridge_port_id_map_t;
typedef std::map<sai_object_id_t, Port_obj *> port_id_map_t;
typedef std::map<sai_object_id_t, Bridge_obj *> bridge_id_map_t;
typedef std::map<sai_object_id_t, Vlan_obj *> vlan_id_map_t;
typedef std::map<sai_object_id_t, Vlan_member_obj *> vlan_member_id_map_t;
typedef std::map<sai_object_id_t, Lag_obj *> lag_id_map_t;
typedef std::map<sai_object_id_t, uint32_t> l2_if_map_t;
typedef std::map<sai_object_id_t, Lag_member_obj *> lag_member_id_map_t;
typedef std::map<sai_object_id_t, HostIF_obj *> hostif_id_map_t;
typedef std::map<sai_object_id_t, HostIF_Table_Entry_obj *>
hostif_table_entry_id_map_t;
typedef std::map<sai_object_id_t, HostIF_Trap_obj *> hostif_trap_id_map_t;
typedef std::map<sai_object_id_t, HostIF_Trap_Group_obj *>
hostif_trap_group_id_map_t;
typedef std::map<sai_object_id_t, VirtualRouter_obj *> vr_id_map_t;
typedef std::map<sai_object_id_t, RouterInterface_obj *> rif_id_map_t;
typedef std::map<sai_object_id_t, NextHop_obj *> nhop_id_map_t;
class Switch_metadata {
public:
sai_u32_list_t hw_port_list;
port_id_map_t ports;
bridge_port_id_map_t bridge_ports;
bridge_id_map_t bridges;
vlan_id_map_t vlans;
vlan_member_id_map_t vlan_members;
lag_id_map_t lags;
lag_member_id_map_t lag_members;
hostif_id_map_t hostifs;
hostif_table_entry_id_map_t hostif_table_entries;
hostif_trap_id_map_t hostif_traps;
hostif_trap_group_id_map_t hostif_trap_groups;
vr_id_map_t vrs;
rif_id_map_t rifs;
nhop_id_map_t nhops;
sai_object_id_t default_bridge_id;
sai_object_id_t default_vlan_oid;
BridgePort_obj *router_bridge_port;
sai_mac_t default_switch_mac;
Switch_metadata() {
ports.clear();
bridge_ports.clear();
bridges.clear();
vlans.clear();
vlan_members.clear();
lags.clear();
}
HostIF_Table_Entry_obj *GetTableEntryFromTrapID(uint16_t trap_id) {
for (hostif_table_entry_id_map_t::iterator it =
hostif_table_entries.begin();
it != hostif_table_entries.end(); ++it) {
if (it->second->trap_id == trap_id) {
return it->second;
}
}
spdlog::get("logger")->error("hostif_table_entry not found for trap_id {} ",
trap_id);
return nullptr;
}
BridgePort_obj *GetBrPortObjFromBrPort(uint16_t bridge_port) {
for (bridge_port_id_map_t::iterator it = bridge_ports.begin();
it != bridge_ports.end(); ++it) {
if (it->second->bridge_port == bridge_port) {
return it->second;
}
}
spdlog::get("logger")->error(
"bridge_port object not found for bridge_port {} ", bridge_port);
return nullptr;
}
sai_object_id_t GetVlanObjIdFromVid(uint16_t vid) {
for (vlan_id_map_t::iterator it = vlans.begin(); it != vlans.end(); ++it) {
if (it->second->vid == vid) {
return it->first;
}
}
spdlog::get("logger")->error("vlan object not found for vid {} ", vid);
return SAI_NULL_OBJECT_ID;
}
HostIF_obj *GetHostIFFromPhysicalPort(int port_num) {
for (hostif_id_map_t::iterator it = hostifs.begin(); it != hostifs.end();
++it) {
spdlog::get("logger")->debug("hostif hw_port {} ",
it->second->port->hw_port);
if (it->second->port->hw_port == port_num) {
return it->second;
}
}
spdlog::get("logger")->error("hostif not found for physical port {} ",
port_num);
return nullptr;
}
uint32_t GetNewVrf() {
std::vector<uint32_t> vrfs;
for (vr_id_map_t::iterator it = vrs.begin();
it != vrs.end(); ++it) {
vrfs.push_back(it->second->vrf);
spdlog::get("logger")->debug("{} ", it->second->vrf);
}
for (int i = 0; i < vrfs.size(); ++i) {
if (std::find(vrfs.begin(), vrfs.end(), i) ==
vrfs.end()) {
spdlog::get("logger")->debug("-->GetNewNextHopID: vrf is: {} ",
i);
return i;
}
}
spdlog::get("logger")->debug("--> GetNewNextHopID: vrf is: {} ",
vrfs.size());
return vrfs.size();
}
uint32_t GetNewRifId() {
std::vector<uint32_t> rif_ids;
for (rif_id_map_t::iterator it = rifs.begin();
it != rifs.end(); ++it) {
rif_ids.push_back(it->second->rif_id);
spdlog::get("logger")->debug("{} ", it->second->rif_id);
}
for (int i = 0; i < rif_ids.size(); ++i) {
if (std::find(rif_ids.begin(), rif_ids.end(), i) ==
rif_ids.end()) {
spdlog::get("logger")->debug("-->GetNewNextHopID: rif_id is: {} ",
i);
return i;
}
}
spdlog::get("logger")->debug("--> GetNewNextHopID: rif_id is: {} ",
rif_ids.size());
return rif_ids.size();
}
uint32_t GetNewNextHopID() {
std::vector<uint32_t> nexthop_ids;
for (nhop_id_map_t::iterator it = nhops.begin();
it != nhops.end(); ++it) {
nexthop_ids.push_back(it->second->nhop_id);
spdlog::get("logger")->debug("{} ", it->second->nhop_id);
}
for (int i = 0; i < nexthop_ids.size(); ++i) {
if (std::find(nexthop_ids.begin(), nexthop_ids.end(), i) ==
nexthop_ids.end()) {
spdlog::get("logger")->debug("-->GetNewNextHopID: nhop_id is: {} ",
i);
return i;
}
}
spdlog::get("logger")->debug("--> GetNewNextHopID: nhop_id is: {} ",
nexthop_ids.size());
return nexthop_ids.size();
}
uint32_t GetNewBridgePort() {
std::vector<uint32_t> bridge_port_nums;
for (bridge_port_id_map_t::iterator it = bridge_ports.begin();
it != bridge_ports.end(); ++it) {
bridge_port_nums.push_back(it->second->bridge_port);
spdlog::get("logger")->debug("{} ", it->second->bridge_port);
}
for (int i = 0; i < bridge_port_nums.size(); ++i) {
if (std::find(bridge_port_nums.begin(), bridge_port_nums.end(), i) ==
bridge_port_nums.end()) {
spdlog::get("logger")->debug("-->GetNewBridgePort: bridge_port is: {} ",
i);
return i;
}
}
spdlog::get("logger")->debug("--> GetNewBridgePort: bridge_port is: {} ",
bridge_port_nums.size());
return bridge_port_nums.size();
}
uint32_t GetNewBridgeID(uint32_t prefered_id) {
std::vector<uint32_t> bridge_ids;
for (bridge_id_map_t::iterator it = bridges.begin(); it != bridges.end();
++it) {
bridge_ids.push_back(it->second->bridge_id);
}
for (vlan_id_map_t::iterator it = vlans.begin(); it != vlans.end(); ++it) {
bridge_ids.push_back(it->second->bridge_id);
}
if (std::find(bridge_ids.begin(), bridge_ids.end(), prefered_id) ==
bridge_ids.end()) {
return prefered_id;
}
for (int i = 0; i < bridge_ids.size(); ++i) {
if (std::find(bridge_ids.begin(), bridge_ids.end(), i) ==
bridge_ids.end()) {
return i;
}
}
return bridge_ids.size();
}
uint32_t GetNewL2IF() {
std::vector<uint32_t> l2_ifs_nums;
for (port_id_map_t::iterator it = ports.begin(); it != ports.end(); ++it) {
l2_ifs_nums.push_back(it->second->l2_if);
}
for (lag_id_map_t::iterator it = lags.begin(); it != lags.end(); ++it) {
l2_ifs_nums.push_back(it->second->l2_if);
}
for (int i = 0; i < l2_ifs_nums.size(); ++i) {
if (std::find(l2_ifs_nums.begin(), l2_ifs_nums.end(), i) ==
l2_ifs_nums.end()) {
spdlog::get("logger")->debug("--> Get_New_L2_if: new if is: {} ", i);
return i;
}
}
spdlog::get("logger")->debug("--> Get_New_L2_if: new if is: {} ",
l2_ifs_nums.size());
return l2_ifs_nums.size();
}
uint16_t GetNewTrapID() {
std::vector<uint16_t> trap_ids;
for (hostif_trap_id_map_t::iterator it = hostif_traps.begin();
it != hostif_traps.end(); ++it) {
trap_ids.push_back(it->second->trap_id);
}
for (int i = 0; i < trap_ids.size(); ++i) {
if (std::find(trap_ids.begin(), trap_ids.end(), i) == trap_ids.end()) {
return i;
}
}
return trap_ids.size();
}
};
#endif