forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouteorch.cpp
More file actions
460 lines (393 loc) · 14.4 KB
/
Copy pathrouteorch.cpp
File metadata and controls
460 lines (393 loc) · 14.4 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
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
#include "routeorch.h"
#include "logger.h"
extern sai_route_api_t* sai_route_api;
extern sai_next_hop_group_api_t* sai_next_hop_group_api;
extern sai_object_id_t gVirtualRouterId;
void RouteOrch::doTask()
{
SWSS_LOG_ENTER();
if (m_toSync.empty())
return;
auto it = m_toSync.begin();
while (it != m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;
string key = kfvKey(t);
string op = kfvOp(t);
/* Get notification from application */
/* resync application:
* When routeorch receives 'resync' message, it marks all current
* routes as dirty and waits for 'resync complete' message. For all
* newly received routes, if they match current dirty routes, it unmarks
* them dirty. After receiving 'resync complete' message, it creates all
* newly added routes and removes all dirty routes.
*/
if (key == "resync")
{
if (op == "SET")
{
/* Mark all current routes as dirty (DEL) in m_toSync map */
SWSS_LOG_NOTICE("Start resync routes\n");
for (auto i = m_syncdRoutes.begin(); i != m_syncdRoutes.end(); i++)
{
vector<FieldValueTuple> v;
auto x = KeyOpFieldsValuesTuple(i->first.to_string(), DEL_COMMAND, v);
m_toSync[i->first.to_string()] = x;
}
m_resync = true;
}
else
{
SWSS_LOG_NOTICE("Complete resync routes\n");
m_resync = false;
}
it = m_toSync.erase(it);
continue;
}
if (m_resync)
{
it++;
continue;
}
IpPrefix ip_prefix = IpPrefix(key);
if (!ip_prefix.isV4())
{
it = m_toSync.erase(it);
continue;
}
if (op == SET_COMMAND)
{
IpAddresses ip_addresses;
string alias;
for (auto i = kfvFieldsValues(t).begin();
i != kfvFieldsValues(t).end(); i++)
{
if (fvField(*i) == "nexthop")
ip_addresses = IpAddresses(fvValue(*i));
if (fvField(*i) == "ifindex")
alias = fvValue(*i);
}
// XXX: set to blackhold if nexthop is empty?
if (ip_addresses.getSize() == 0)
{
it = m_toSync.erase(it);
continue;
}
// XXX: cannot trust m_portsOrch->getPortIdByAlias because sometimes alias is empty
// XXX: need to split aliases with ',' and verify the next hops?
if (alias == "eth0" || alias == "lo" || alias == "docker0")
{
it = m_toSync.erase(it);
continue;
}
if (m_syncdRoutes.find(ip_prefix) == m_syncdRoutes.end() || m_syncdRoutes[ip_prefix] != ip_addresses)
{
if (addRoute(ip_prefix, ip_addresses))
it = m_toSync.erase(it);
else
it++;
}
else
/* Duplicate entry */
it = m_toSync.erase(it);
}
else if (op == DEL_COMMAND)
{
if (m_syncdRoutes.find(ip_prefix) != m_syncdRoutes.end())
{
if (removeRoute(ip_prefix))
it = m_toSync.erase(it);
else
it++;
}
/* Cannot locate the route */
else
it = m_toSync.erase(it);
}
else
{
SWSS_LOG_ERROR("Unknown operation type %s\n", op.c_str());
it = m_toSync.erase(it);
}
}
}
bool RouteOrch::createNextHopEntry(IpAddress ipAddress, sai_object_id_t nextHopId)
{
SWSS_LOG_ENTER();
IpAddresses ip_addresses(ipAddress.to_string());
return createNextHopEntry(ip_addresses, nextHopId);
}
bool RouteOrch::createNextHopEntry(IpAddresses ipAddresses, sai_object_id_t nextHopGroupId)
{
SWSS_LOG_ENTER();
if (m_syncdNextHops.find(ipAddresses) != m_syncdNextHops.end())
{
SWSS_LOG_ERROR("Failed to create existed next hop entry ip:%s nhid:%llx\n", ipAddresses.to_string().c_str(), nextHopGroupId);
return false;
}
NextHopEntry next_hop_entry;
next_hop_entry.next_hop_id = nextHopGroupId;
next_hop_entry.ref_count = 0;
m_syncdNextHops[ipAddresses] = next_hop_entry;
return true;
}
bool RouteOrch::removeNextHopEntry(IpAddress ipAddress)
{
SWSS_LOG_ENTER();
IpAddresses ip_addresses(ipAddress.to_string());
if (m_syncdNextHops.find(ip_addresses) == m_syncdNextHops.end())
{
SWSS_LOG_ERROR("Failed to remove absent next hop entry ip:%s\n", ip_addresses.to_string().c_str());
return false;
}
if (getNextHopRefCount(ip_addresses) != 0)
{
SWSS_LOG_ERROR("Failed to remove referenced next hop entry ip:%s", ip_addresses.to_string().c_str());
return false;
}
m_syncdNextHops.erase(ip_addresses);
return true;
}
bool RouteOrch::removeNextHopEntry(IpAddresses ipAddresses)
{
SWSS_LOG_ENTER();
if (m_syncdNextHops.find(ipAddresses) == m_syncdNextHops.end())
{
SWSS_LOG_ERROR("Failed to remove absent next hop entry ip:%s\n", ipAddresses.to_string().c_str());
return false;
}
if (ipAddresses.getSize() > 1 && getNextHopRefCount(ipAddresses) == 0)
{
sai_object_id_t next_hop_group_id = m_syncdNextHops[ipAddresses].next_hop_id;
sai_status_t status = sai_next_hop_group_api->remove_next_hop_group(next_hop_group_id);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove next hop group nhgid:%llx\n", next_hop_group_id);
return false;
}
m_nextHopGroupCount --;
set<IpAddress> ip_address_set = ipAddresses.getIpAddresses();
for (auto it = ip_address_set.begin(); it != ip_address_set.end(); it++)
{
IpAddresses ip_address((*it).to_string());
(m_syncdNextHops[ip_address]).ref_count --;
}
m_syncdNextHops.erase(ipAddresses);
}
return true;
}
int RouteOrch::getNextHopRefCount(IpAddress ipAddress)
{
SWSS_LOG_ENTER();
IpAddresses ip_addresses(ipAddress.to_string());
return getNextHopRefCount(ip_addresses);
}
int RouteOrch::getNextHopRefCount(IpAddresses ipAddresses)
{
SWSS_LOG_ENTER();
return m_syncdNextHops[ipAddresses].ref_count;
}
NextHopEntry RouteOrch::getNextHopEntry(IpAddress ipAddress)
{
SWSS_LOG_ENTER();
IpAddresses ip_addresses(ipAddress.to_string());
return getNextHopEntry(ip_addresses);
}
NextHopEntry RouteOrch::getNextHopEntry(IpAddresses ipAddresses)
{
SWSS_LOG_ENTER();
return m_syncdNextHops[ipAddresses];
}
bool RouteOrch::addRoute(IpPrefix ipPrefix, IpAddresses nextHops)
{
SWSS_LOG_ENTER();
/* nhid indicates the next hop id or next hop group id of this route */
sai_object_id_t next_hop_id;
auto it_route = m_syncdRoutes.find(ipPrefix);
auto it_nxhop = m_syncdNextHops.find(nextHops);
/*
* If next hop group cannot be found in m_syncdNextHops,
* then we need to add it.
*/
if (it_nxhop == m_syncdNextHops.end())
{
NextHopEntry next_hop_entry;
vector<sai_object_id_t> next_hop_ids;
set<IpAddress> next_hop_set = nextHops.getIpAddresses();
for (auto it = next_hop_set.begin(); it != next_hop_set.end(); it++)
{
IpAddresses tmp_ip((*it).to_string());
if (m_syncdNextHops.find(tmp_ip) == m_syncdNextHops.end())
{
SWSS_LOG_ERROR("Failed to find next hop %s", (*it).to_string().c_str());
return false;
}
next_hop_id = m_syncdNextHops[tmp_ip].next_hop_id;
next_hop_ids.push_back(next_hop_id);
}
/*
* If the current number of next hop groups exceeds the threshold,
* then we need to pick up a random next hop from the next hop group as
* the route's temporary next hop.
*/
if (m_nextHopGroupCount > NHGRP_MAX_SIZE)
{
bool to_add = false;
/*
* A temporary entry is added when route is not in m_syncdRoutes,
* or it is in m_syncdRoutes but the original next hop(s) is not a
* subset of the next hop group to be added.
*/
if (it_route != m_syncdRoutes.end())
{
auto tmp_set = m_syncdRoutes[ipPrefix].getIpAddresses();
for (auto it = tmp_set.begin(); it != tmp_set.end(); it++)
{
if (next_hop_set.find(*it) == next_hop_set.end())
{
to_add = true;
}
}
}
else
{
to_add = true;
}
if (to_add)
{
/* Randomly pick an address from the address set. */
auto it = next_hop_set.begin();
advance(it, rand() % next_hop_set.size());
/*
* Set the route's temporary next hop to be the randomly picked one.
*/
IpAddresses tmp_nxhop((*it).to_string());
addRoute(ipPrefix, tmp_nxhop);
}
/*
* Return false since the original route is not successfully added.
*/
return false;
}
sai_attribute_t nhg_attr;
vector<sai_attribute_t> nhg_attrs;
nhg_attr.id = SAI_NEXT_HOP_GROUP_ATTR_TYPE;
nhg_attr.value.s32 = SAI_NEXT_HOP_GROUP_ECMP;
nhg_attrs.push_back(nhg_attr);
nhg_attr.id = SAI_NEXT_HOP_GROUP_ATTR_NEXT_HOP_LIST;
nhg_attr.value.objlist.count = next_hop_ids.size();
nhg_attr.value.objlist.list = next_hop_ids.data();
nhg_attrs.push_back(nhg_attr);
sai_status_t status = sai_next_hop_group_api->
create_next_hop_group(&next_hop_id, nhg_attrs.size(), nhg_attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create nex thop group nh:%s\n", nextHops.to_string().c_str());
return false;
}
m_nextHopGroupCount ++;
SWSS_LOG_NOTICE("Create next hop group nhgid:%llx nh:%s \n", next_hop_id, nextHops.to_string().c_str());
/*
* Increate the ref_count for the next hops used by the next hop group.
*/
for (auto it = next_hop_set.begin(); it != next_hop_set.end(); it++)
{
IpAddresses tmp_ip((*it).to_string().c_str());
m_syncdNextHops[tmp_ip].ref_count ++;
}
/*
* Initialize the next hop gruop structure with ref_count as 0. This
* count will increase once the route is successfully syncd.
*/
next_hop_entry.ref_count = 0;
next_hop_entry.next_hop_id = next_hop_id;
m_syncdNextHops[nextHops] = next_hop_entry;
}
else
{
next_hop_id = m_syncdNextHops[nextHops].next_hop_id;
}
/* Sync the route entry */
sai_unicast_route_entry_t route_entry;
route_entry.vr_id = gVirtualRouterId;
route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
route_entry.destination.addr.ip4 = ipPrefix.getIp().getV4Addr();
route_entry.destination.mask.ip4 = ipPrefix.getMask().getV4Addr();
sai_attribute_t route_attr;
route_attr.id = SAI_ROUTE_ATTR_NEXT_HOP_ID;
route_attr.value.oid = next_hop_id;
/* If the prefix is not in m_syncdRoutes, then we need to create the route
* for this prefix with the new next hop (group) id. If the prefix is already
* in m_syncdRoutes, then we need to update the route with a new next hop
* (group) id. The old next hop (group) is then not used and the reference
* count will decrease by 1.
*/
if (it_route == m_syncdRoutes.end())
{
sai_status_t status = sai_route_api->create_route(&route_entry, 1, &route_attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create route %s with next hop(s) %s",
ipPrefix.to_string().c_str(), nextHops.to_string().c_str());
/* Clean up the newly created next hop (group) entry */
removeNextHopEntry(nextHops);
return false;
}
/* Increase the ref_count for the next hop (group) entry */
m_syncdNextHops[nextHops].ref_count ++;
SWSS_LOG_INFO("Create route %s with next hop(s) %s",
ipPrefix.to_string().c_str(), nextHops.to_string().c_str());
}
else
{
sai_status_t status = sai_route_api->set_route_attribute(&route_entry, &route_attr);
if (status != SAI_STATUS_SUCCESS)
{
return false;
}
m_syncdNextHops[nextHops].ref_count ++;
auto it = m_syncdNextHops.find(it_route->second);
if (it == m_syncdNextHops.end())
{
return false;
}
it->second.ref_count --;
removeNextHopEntry(it->first);
}
m_syncdRoutes[ipPrefix] = nextHops;
return true;
}
bool RouteOrch::removeRoute(IpPrefix ipPrefix)
{
SWSS_LOG_ENTER();
sai_unicast_route_entry_t route_entry;
route_entry.vr_id = gVirtualRouterId;
route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
route_entry.destination.addr.ip4 = ipPrefix.getIp().getV4Addr();
route_entry.destination.mask.ip4 = ipPrefix.getMask().getV4Addr();
sai_status_t status = sai_route_api->remove_route(&route_entry);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove route prefix:%s\n", ipPrefix.to_string().c_str());
return false;
}
/* Remove next hop group entry if ref_count is zero */
auto it_route = m_syncdRoutes.find(ipPrefix);
if (it_route != m_syncdRoutes.end())
{
auto it_next_hop = m_syncdNextHops.find(it_route->second);
if (it_next_hop == m_syncdNextHops.end())
{
SWSS_LOG_ERROR("Failed to locate next hop entry \n");
return false;
}
it_next_hop->second.ref_count --;
if (!removeNextHopEntry(it_next_hop->first))
{
SWSS_LOG_ERROR("Failed to remove next hop group\n");
return false;
}
}
m_syncdRoutes.erase(ipPrefix);
return true;
}