-
Notifications
You must be signed in to change notification settings - Fork 533
/
nbrmgr.cpp
561 lines (473 loc) · 15.7 KB
/
nbrmgr.cpp
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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
#include <netlink/cache.h>
#include "logger.h"
#include "tokenize.h"
#include "ipprefix.h"
#include "macaddress.h"
#include "nbrmgr.h"
#include "exec.h"
#include "shellcmd.h"
#include "subscriberstatetable.h"
using namespace swss;
static bool send_message(struct nl_sock *sk, struct nl_msg *msg)
{
bool rc = false;
int err = 0;
do
{
if (!sk)
{
SWSS_LOG_ERROR("Netlink socket null pointer");
break;
}
if ((err = nl_send_auto(sk, msg)) < 0)
{
SWSS_LOG_ERROR("Netlink send message failed, error '%s'", nl_geterror(err));
break;
}
rc = true;
} while(0);
nlmsg_free(msg);
return rc;
}
NbrMgr::NbrMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames) :
Orch(cfgDb, tableNames),
m_statePortTable(stateDb, STATE_PORT_TABLE_NAME),
m_stateLagTable(stateDb, STATE_LAG_TABLE_NAME),
m_stateVlanTable(stateDb, STATE_VLAN_TABLE_NAME),
m_stateIntfTable(stateDb, STATE_INTERFACE_TABLE_NAME),
m_stateNeighRestoreTable(stateDb, STATE_NEIGH_RESTORE_TABLE_NAME)
{
int err = 0;
m_nl_sock = nl_socket_alloc();
if (!m_nl_sock)
{
SWSS_LOG_ERROR("Netlink socket alloc failed");
}
else if ((err = nl_connect(m_nl_sock, NETLINK_ROUTE)) < 0)
{
SWSS_LOG_ERROR("Netlink socket connect failed, error '%s'", nl_geterror(err));
}
auto consumerStateTable = new swss::ConsumerStateTable(appDb, APP_NEIGH_RESOLVE_TABLE_NAME,
TableConsumable::DEFAULT_POP_BATCH_SIZE, default_orch_pri);
auto consumer = new Consumer(consumerStateTable, this, APP_NEIGH_RESOLVE_TABLE_NAME);
Orch::addExecutor(consumer);
string swtype;
Table cfgDeviceMetaDataTable(cfgDb, CFG_DEVICE_METADATA_TABLE_NAME);
if(cfgDeviceMetaDataTable.hget("localhost", "switch_type", swtype))
{
//If this is voq system, let the neighbor manager subscribe to state of SYSTEM_NEIGH
//entries. This is used to program static neigh and static route in kernel for remote neighbors.
if(swtype == "voq")
{
string tableName = STATE_SYSTEM_NEIGH_TABLE_NAME;
Orch::addExecutor(new Consumer(new SubscriberStateTable(stateDb, tableName, TableConsumable::DEFAULT_POP_BATCH_SIZE, 0), this, tableName));
m_cfgVoqInbandInterfaceTable = unique_ptr<Table>(new Table(cfgDb, CFG_VOQ_INBAND_INTERFACE_TABLE_NAME));
}
}
}
bool NbrMgr::isIntfStateOk(const string &alias)
{
vector<FieldValueTuple> temp;
if (m_stateIntfTable.get(alias, temp))
{
SWSS_LOG_DEBUG("Intf %s is ready", alias.c_str());
return true;
}
return false;
}
bool NbrMgr::isNeighRestoreDone()
{
string value;
m_stateNeighRestoreTable.hget("Flags", "restored", value);
if (value == "true")
{
SWSS_LOG_INFO("Kernel neighbor table restore is done");
return true;
}
return false;
}
bool NbrMgr::setNeighbor(const string& alias, const IpAddress& ip, const MacAddress& mac)
{
SWSS_LOG_ENTER();
struct nl_msg *msg = nlmsg_alloc();
if (!msg)
{
SWSS_LOG_ERROR("Netlink message alloc failed for '%s'", ip.to_string().c_str());
return false;
}
auto flags = (NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_REPLACE);
struct nlmsghdr *hdr = nlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, RTM_NEWNEIGH, 0, flags);
if (!hdr)
{
SWSS_LOG_ERROR("Netlink message header alloc failed for '%s'", ip.to_string().c_str());
nlmsg_free(msg);
return false;
}
struct ndmsg *nd_msg = static_cast<struct ndmsg *>
(nlmsg_reserve(msg, sizeof(struct ndmsg), NLMSG_ALIGNTO));
if (!nd_msg)
{
SWSS_LOG_ERROR("Netlink ndmsg reserve failed for '%s'", ip.to_string().c_str());
nlmsg_free(msg);
return false;
}
memset(nd_msg, 0, sizeof(struct ndmsg));
nd_msg->ndm_ifindex = if_nametoindex(alias.c_str());
auto addr_len = ip.isV4()? sizeof(struct in_addr) : sizeof(struct in6_addr);
struct rtattr *rta = static_cast<struct rtattr *>
(nlmsg_reserve(msg, sizeof(struct rtattr) + addr_len, NLMSG_ALIGNTO));
if (!rta)
{
SWSS_LOG_ERROR("Netlink rtattr (IP) failed for '%s'", ip.to_string().c_str());
nlmsg_free(msg);
return false;
}
rta->rta_type = NDA_DST;
rta->rta_len = static_cast<short>(RTA_LENGTH(addr_len));
nd_msg->ndm_type = RTN_UNICAST;
auto ip_addr = ip.getIp();
if (ip.isV4())
{
nd_msg->ndm_family = AF_INET;
memcpy(RTA_DATA(rta), &ip_addr.ip_addr.ipv4_addr, addr_len);
}
else
{
nd_msg->ndm_family = AF_INET6;
memcpy(RTA_DATA(rta), &ip_addr.ip_addr.ipv6_addr, addr_len);
}
if (!mac)
{
/*
* If mac is not provided, expected to resolve the MAC
*/
nd_msg->ndm_state = NUD_DELAY;
nd_msg->ndm_flags = NTF_USE;
SWSS_LOG_INFO("Resolve request for '%s'", ip.to_string().c_str());
}
else
{
SWSS_LOG_INFO("Set mac address '%s'", mac.to_string().c_str());
nd_msg->ndm_state = NUD_PERMANENT;
auto mac_len = ETHER_ADDR_LEN;
auto mac_addr = mac.getMac();
struct rtattr *rta = static_cast<struct rtattr *>
(nlmsg_reserve(msg, sizeof(struct rtattr) + mac_len, NLMSG_ALIGNTO));
if (!rta)
{
SWSS_LOG_ERROR("Netlink rtattr (MAC) failed for '%s'", ip.to_string().c_str());
nlmsg_free(msg);
return false;
}
rta->rta_type = NDA_LLADDR;
rta->rta_len = static_cast<short>(RTA_LENGTH(mac_len));
memcpy(RTA_DATA(rta), mac_addr, mac_len);
}
return send_message(m_nl_sock, msg);
}
void NbrMgr::doResolveNeighTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;
vector<string> keys = tokenize(kfvKey(t),delimiter);
MacAddress mac;
IpAddress ip(keys[1]);
string alias(keys[0]);
if (!setNeighbor(alias, ip, mac))
{
SWSS_LOG_ERROR("Neigh entry resolve failed for '%s'", kfvKey(t).c_str());
}
it = consumer.m_toSync.erase(it);
}
}
void NbrMgr::doSetNeighTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;
vector<string> keys = tokenize(kfvKey(t), config_db_key_delimiter);
const vector<FieldValueTuple>& data = kfvFieldsValues(t);
string alias(keys[0]);
IpAddress ip(keys[1]);
string op = kfvOp(t);
MacAddress mac;
bool invalid_mac = false;
for (auto idx : data)
{
const auto &field = fvField(idx);
const auto &value = fvValue(idx);
if (field == "neigh")
{
try
{
mac = value;
}
catch (const std::invalid_argument& e)
{
SWSS_LOG_ERROR("Invalid Mac addr '%s' for '%s'", value.c_str(), kfvKey(t).c_str());
invalid_mac = true;
break;
}
}
}
if (invalid_mac)
{
it = consumer.m_toSync.erase(it);
continue;
}
if (op == SET_COMMAND)
{
if (!isIntfStateOk(alias))
{
SWSS_LOG_DEBUG("Interface is not yet ready, skipping '%s'", kfvKey(t).c_str());
it++;
continue;
}
if (!setNeighbor(alias, ip, mac))
{
SWSS_LOG_ERROR("Neigh entry add failed for '%s'", kfvKey(t).c_str());
}
else
{
SWSS_LOG_NOTICE("Neigh entry added for '%s'", kfvKey(t).c_str());
}
}
else if (op == DEL_COMMAND)
{
SWSS_LOG_NOTICE("Not yet implemented, key '%s'", kfvKey(t).c_str());
}
else
{
SWSS_LOG_ERROR("Unknown operation: '%s'", op.c_str());
}
it = consumer.m_toSync.erase(it);
}
}
void NbrMgr::doTask(Consumer &consumer)
{
string table_name = consumer.getTableName();
if (table_name == CFG_NEIGH_TABLE_NAME)
{
doSetNeighTask(consumer);
} else if (table_name == APP_NEIGH_RESOLVE_TABLE_NAME)
{
doResolveNeighTask(consumer);
} else if(table_name == STATE_SYSTEM_NEIGH_TABLE_NAME)
{
doStateSystemNeighTask(consumer);
}
else
{
SWSS_LOG_ERROR("Unknown REDIS table %s ", table_name.c_str());
}
}
void NbrMgr::doStateSystemNeighTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
//Get the name of the device on which the neigh and route are
//going to be programmed.
string nbr_odev;
if(!getVoqInbandInterfaceName(nbr_odev))
{
//The inband interface is not available yet
return;
}
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;
string key = kfvKey(t);
string op = kfvOp(t);
size_t found = key.find_last_of(state_db_key_delimiter);
if (found == string::npos)
{
SWSS_LOG_ERROR("Failed to parse key %s", key.c_str());
it = consumer.m_toSync.erase(it);
continue;
}
IpAddress ip_address(key.substr(found+1));
if (op == SET_COMMAND)
{
MacAddress mac_address;
for (auto i = kfvFieldsValues(t).begin();
i != kfvFieldsValues(t).end(); i++)
{
if (fvField(*i) == "neigh")
mac_address = MacAddress(fvValue(*i));
}
if (!isIntfStateOk(nbr_odev))
{
SWSS_LOG_DEBUG("Interface %s is not ready, skipping system neigh %s'", nbr_odev.c_str(), kfvKey(t).c_str());
it++;
continue;
}
if (!addKernelNeigh(nbr_odev, ip_address, mac_address))
{
SWSS_LOG_ERROR("Neigh entry add on dev %s failed for '%s'", nbr_odev.c_str(), kfvKey(t).c_str());
it++;
continue;
}
else
{
SWSS_LOG_NOTICE("Neigh entry added on dev %s for '%s'", nbr_odev.c_str(), kfvKey(t).c_str());
}
if (!addKernelRoute(nbr_odev, ip_address))
{
SWSS_LOG_ERROR("Route entry add on dev %s failed for '%s'", nbr_odev.c_str(), kfvKey(t).c_str());
delKernelNeigh(nbr_odev, ip_address);
it++;
continue;
}
else
{
SWSS_LOG_NOTICE("Route entry added on dev %s for '%s'", nbr_odev.c_str(), kfvKey(t).c_str());
}
SWSS_LOG_NOTICE("Added voq neighbor %s to kernel", kfvKey(t).c_str());
}
else if (op == DEL_COMMAND)
{
if (!delKernelRoute(ip_address))
{
SWSS_LOG_ERROR("Route entry on dev %s delete failed for '%s'", nbr_odev.c_str(), kfvKey(t).c_str());
}
else
{
SWSS_LOG_NOTICE("Route entry on dev %s deleted for '%s'", nbr_odev.c_str(), kfvKey(t).c_str());
}
if (!delKernelNeigh(nbr_odev, ip_address))
{
SWSS_LOG_ERROR("Neigh entry on dev %s delete failed for '%s'", nbr_odev.c_str(), kfvKey(t).c_str());
}
else
{
SWSS_LOG_NOTICE("Neigh entry on dev %s deleted for '%s'", nbr_odev.c_str(), kfvKey(t).c_str());
}
SWSS_LOG_DEBUG("Deleted voq neighbor %s from kernel", kfvKey(t).c_str());
}
it = consumer.m_toSync.erase(it);
}
}
bool NbrMgr::getVoqInbandInterfaceName(string &ibif)
{
vector<string> keys;
m_cfgVoqInbandInterfaceTable->getKeys(keys);
if (keys.empty())
{
SWSS_LOG_NOTICE("Voq Inband interface is not configured!");
return false;
}
//key:"alias" = inband interface name
vector<string> if_keys = tokenize(keys[0], config_db_key_delimiter);
ibif = if_keys[0];
return true;
}
bool NbrMgr::addKernelRoute(string odev, IpAddress ip_addr)
{
string cmd, res;
SWSS_LOG_ENTER();
string ip_str = ip_addr.to_string();
if(ip_addr.isV4())
{
cmd = string("") + IP_CMD + " route add " + ip_str + "/32 dev " + odev;
SWSS_LOG_NOTICE("IPv4 Route Add cmd: %s",cmd.c_str());
}
else
{
cmd = string("") + IP_CMD + " -6 route add " + ip_str + "/128 dev " + odev;
SWSS_LOG_NOTICE("IPv6 Route Add cmd: %s",cmd.c_str());
}
int32_t ret = swss::exec(cmd, res);
if(ret)
{
/* Just log error and return */
SWSS_LOG_ERROR("Failed to add route for %s, error: %d", ip_str.c_str(), ret);
return false;
}
SWSS_LOG_INFO("Added route for %s on device %s", ip_str.c_str(), odev.c_str());
return true;
}
bool NbrMgr::delKernelRoute(IpAddress ip_addr)
{
string cmd, res;
SWSS_LOG_ENTER();
string ip_str = ip_addr.to_string();
if(ip_addr.isV4())
{
cmd = string("") + IP_CMD + " route del " + ip_str + "/32";
SWSS_LOG_NOTICE("IPv4 Route Del cmd: %s",cmd.c_str());
}
else
{
cmd = string("") + IP_CMD + " -6 route del " + ip_str + "/128";
SWSS_LOG_NOTICE("IPv6 Route Del cmd: %s",cmd.c_str());
}
int32_t ret = swss::exec(cmd, res);
if(ret)
{
/* Just log error and return */
SWSS_LOG_ERROR("Failed to delete route for %s, error: %d", ip_str.c_str(), ret);
return false;
}
SWSS_LOG_INFO("Deleted route for %s", ip_str.c_str());
return true;
}
bool NbrMgr::addKernelNeigh(string odev, IpAddress ip_addr, MacAddress mac_addr)
{
SWSS_LOG_ENTER();
string cmd, res;
string ip_str = ip_addr.to_string();
string mac_str = mac_addr.to_string();
if(ip_addr.isV4())
{
cmd = string("") + IP_CMD + " neigh add " + ip_str + " lladdr " + mac_str + " dev " + odev;
SWSS_LOG_NOTICE("IPv4 Nbr Add cmd: %s",cmd.c_str());
}
else
{
cmd = string("") + IP_CMD + " -6 neigh add " + ip_str + " lladdr " + mac_str + " dev " + odev;
SWSS_LOG_NOTICE("IPv6 Nbr Add cmd: %s",cmd.c_str());
}
int32_t ret = swss::exec(cmd, res);
if(ret)
{
/* Just log error and return */
SWSS_LOG_ERROR("Failed to add Nbr for %s, error: %d", ip_str.c_str(), ret);
return false;
}
SWSS_LOG_INFO("Added Nbr for %s on interface %s", ip_str.c_str(), odev.c_str());
return true;
}
bool NbrMgr::delKernelNeigh(string odev, IpAddress ip_addr)
{
string cmd, res;
SWSS_LOG_ENTER();
string ip_str = ip_addr.to_string();
if(ip_addr.isV4())
{
cmd = string("") + IP_CMD + " neigh del " + ip_str + " dev " + odev;
SWSS_LOG_NOTICE("IPv4 Nbr Del cmd: %s",cmd.c_str());
}
else
{
cmd = string("") + IP_CMD + " -6 neigh del " + ip_str + " dev " + odev;
SWSS_LOG_NOTICE("IPv6 Nbr Del cmd: %s",cmd.c_str());
}
int32_t ret = swss::exec(cmd, res);
if(ret)
{
/* Just log error and return */
SWSS_LOG_ERROR("Failed to delete Nbr for %s, error: %d", ip_str.c_str(), ret);
return false;
}
SWSS_LOG_INFO("Deleted Nbr for %s on interface %s", ip_str.c_str(), odev.c_str());
return true;
}