/
mpls.cc
414 lines (351 loc) · 12.1 KB
/
mpls.cc
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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <boost/uuid/uuid_io.hpp>
#include <cmn/agent_cmn.h>
#include <init/agent_param.h>
#include <base/task_annotations.h>
#include <oper/interface_common.h>
#include <oper/vrf.h>
#include <oper/nexthop.h>
#include <oper/mpls.h>
#include <oper/mirror_table.h>
#include <oper/agent_sandesh.h>
using namespace std;
MplsTable *MplsTable::mpls_table_;
MplsLabel::~MplsLabel() {
if (label_ == MplsTable::kInvalidLabel) {
return;
}
if ((type_ != MplsLabel::MCAST_NH) &&
free_label_) {
agent_->mpls_table()->FreeLabel(label_);
}
}
DBEntryBase::KeyPtr MplsLabel::GetDBRequestKey() const {
MplsLabelKey *key = new MplsLabelKey(type_, label_);
return DBEntryBase::KeyPtr(key);
}
void MplsLabel::SetKey(const DBRequestKey *k) {
const MplsLabelKey *key = static_cast<const MplsLabelKey *>(k);
type_ = key->type_;
label_ = key->label_;
}
std::auto_ptr<DBEntry> MplsTable::AllocEntry(const DBRequestKey *k) const {
const MplsLabelKey *key = static_cast<const MplsLabelKey *>(k);
MplsLabel *mpls = new MplsLabel(agent(), key->type_, key->label_);
return std::auto_ptr<DBEntry>(static_cast<DBEntry *>(mpls));
}
DBEntry *MplsTable::Add(const DBRequest *req) {
MplsLabelKey *key = static_cast<MplsLabelKey *>(req->key.get());
MplsLabel *mpls = new MplsLabel(agent(), key->type_, key->label_);
mpls->free_label_ = true;
if (mpls->type_ != MplsLabel::MCAST_NH) {
UpdateLabel(key->label_, mpls);
}
ChangeHandler(mpls, req);
mpls->SendObjectLog(AgentLogEvent::ADD);
return mpls;
}
bool MplsTable::OnChange(DBEntry *entry, const DBRequest *req) {
bool ret;
MplsLabel *mpls = static_cast<MplsLabel *>(entry);
ret = ChangeHandler(mpls, req);
mpls->SendObjectLog(AgentLogEvent::CHANGE);
return ret;
}
bool MplsTable::ChangeNH(MplsLabel *mpls, NextHop *nh) {
if (mpls == NULL)
return false;
if (mpls->nh_ != nh) {
mpls->nh_ = nh;
assert(nh);
mpls->SyncDependentPath();
return true;
}
return false;
}
// No Change expected for MPLS Label
bool MplsTable::ChangeHandler(MplsLabel *mpls, const DBRequest *req) {
bool ret = false;
MplsLabelData *data = static_cast<MplsLabelData *>(req->data.get());
NextHop *nh = static_cast<NextHop *>
(agent()->nexthop_table()->FindActiveEntry(data->nh_key));
if (!nh) {
//NextHop not found, point mpls label to discard
DiscardNH key;
nh = static_cast<NextHop *>
(agent()->nexthop_table()->FindActiveEntry(&key));
}
ret = ChangeNH(mpls, nh);
return ret;
}
bool MplsTable::Delete(DBEntry *entry, const DBRequest *req) {
MplsLabel *mpls = static_cast<MplsLabel *>(entry);
mpls->SendObjectLog(AgentLogEvent::DELETE);
return true;
}
void MplsTable::CreateTableLabel(const Agent *agent,
uint32_t label,
const std::string &vrf_name,
bool policy) {
DBRequest nh_req;
nh_req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
VrfNHKey *vrf_nh_key = new VrfNHKey(vrf_name, false, false);
nh_req.key.reset(vrf_nh_key);
nh_req.data.reset(new VrfNHData(false));
agent->nexthop_table()->Process(nh_req);
DBRequest req;
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MplsLabelKey *key = new MplsLabelKey(MplsLabel::VPORT_NH, label);
req.key.reset(key);
MplsLabelData *data = new MplsLabelData(vrf_name, policy);
req.data.reset(data);
agent->mpls_table()->Process(req);
return;
}
void MplsTable::ReserveLabel() {
if (agent()->vrouter_on_host_dpdk() == true) {
//In case of vrouter running in dpdk mode,
//label allocation has to be done at interval
//of 16, for example 16,32 and 48 are valid label
//Reserver label 0
AllocLabel();
set_mpls_shift_bits(kDpdkShiftBits);
} else {
// We want to allocate labels from an offset
// Pre-allocate entries
for (unsigned int i = 0; i < kStartLabel; i++) {
AllocLabel();
}
}
}
DBTableBase *MplsTable::CreateTable(DB *db, const std::string &name) {
mpls_table_ = new MplsTable(db, name);
mpls_table_->Init();
return mpls_table_;
};
void MplsTable::Process(DBRequest &req) {
agent()->ConcurrencyCheck();
DBTablePartition *tpart =
static_cast<DBTablePartition *>(GetTablePartition(req.key.get()));
tpart->Process(NULL, &req);
}
void MplsLabel::CreateVlanNh(const Agent *agent,
uint32_t label,
const uuid &intf_uuid,
uint16_t tag) {
DBRequest req;
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MplsLabelKey *key = new MplsLabelKey(MplsLabel::VPORT_NH, label);
req.key.reset(key);
MplsLabelData *data = new MplsLabelData(intf_uuid, tag);
req.data.reset(data);
agent->mpls_table()->Process(req);
return;
}
void MplsLabel::CreateInetInterfaceLabel(const Agent *agent,
uint32_t label,
const string &ifname,
bool policy,
InterfaceNHFlags::Type type) {
DBRequest req;
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MplsLabelKey *key = new MplsLabelKey(MplsLabel::VPORT_NH, label);
req.key.reset(key);
MplsLabelData *data = new MplsLabelData(ifname, policy, type);
req.data.reset(data);
agent->mpls_table()->Process(req);
return;
}
void MplsLabel::CreateVPortLabel(const Agent *agent,
uint32_t label,
const uuid &intf_uuid,
bool policy,
InterfaceNHFlags::Type type) {
DBRequest req;
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MplsLabelKey *key = new MplsLabelKey(MplsLabel::VPORT_NH, label);
req.key.reset(key);
MplsLabelData *data = new MplsLabelData(intf_uuid, policy, type);
req.data.reset(data);
agent->mpls_table()->Process(req);
return;
}
void MplsLabel::CreateMcastLabelReq(const Agent *agent,
uint32_t label,
COMPOSITETYPE type,
ComponentNHKeyList &component_nh_key_list,
const std::string vrf_name) {
if (label == 0 || label == MplsTable::kInvalidLabel)
return;
DBRequest req;
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MplsLabelKey *key = new MplsLabelKey(MplsLabel::MCAST_NH, label);
req.key.reset(key);
MplsLabelData *data = new MplsLabelData(type, false, component_nh_key_list,
vrf_name);
req.data.reset(data);
agent->mpls_table()->Enqueue(&req);
return;
}
void MplsLabel::CreateEcmpLabel(const Agent *agent,
uint32_t label, COMPOSITETYPE type,
ComponentNHKeyList &component_nh_key_list,
const std::string vrf_name) {
DBRequest req;
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MplsLabelKey *key = new MplsLabelKey(MplsLabel::VPORT_NH, label);
req.key.reset(key);
MplsLabelData *data = new MplsLabelData(type, true, component_nh_key_list,
vrf_name);
req.data.reset(data);
agent->mpls_table()->Process(req);
return;
}
void MplsLabel::DeleteMcastLabelReq(const Agent *agent, uint32_t src_label) {
DBRequest req;
req.oper = DBRequest::DB_ENTRY_DELETE;
MplsLabelKey *key = new MplsLabelKey(MplsLabel::MCAST_NH, src_label);
req.key.reset(key);
req.data.reset(NULL);
agent->mpls_table()->Enqueue(&req);
return;
}
void MplsLabel::Delete(const Agent *agent, uint32_t label) {
DBRequest req;
req.oper = DBRequest::DB_ENTRY_DELETE;
MplsLabelKey *key = new MplsLabelKey(MplsLabel::INVALID, label);
req.key.reset(key);
req.data.reset(NULL);
agent->mpls_table()->Process(req);
}
void MplsLabel::DeleteReq(const Agent *agent, uint32_t label) {
DBRequest req;
req.oper = DBRequest::DB_ENTRY_DELETE;
MplsLabelKey *key = new MplsLabelKey(MplsLabel::INVALID, label);
req.key.reset(key);
req.data.reset(NULL);
agent->mpls_table()->Enqueue(&req);
}
bool MplsLabel::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
MplsResp *resp = static_cast<MplsResp *>(sresp);
MplsSandeshData data;
data.set_label(label_);
switch (type_) {
case MplsLabel::INVALID:
data.set_type("invalid");
break;
case MplsLabel::MCAST_NH:
data.set_type("mcast_nh");
break;
case MplsLabel::VPORT_NH:
data.set_type("vport_nh");
break;
default:
assert(0);
}
nh_->SetNHSandeshData(data.nh);
std::vector<MplsSandeshData> &list =
const_cast<std::vector<MplsSandeshData>&>(resp->get_mpls_list());
list.push_back(data);
return true;
}
void MplsLabel::SendObjectLog(AgentLogEvent::type event) const {
MplsObjectLogInfo info;
string str, type_str, nh_type;
switch(type_) {
case INVALID:
type_str.assign("Invalid ");
break;
case VPORT_NH:
type_str.assign("Virtual Port");
break;
case MplsLabel::MCAST_NH:
type_str.assign("mcast_nh");
break;
}
info.set_type(type_str);
info.set_label((int)label_);
switch (event) {
case AgentLogEvent::ADD:
str.assign("Addition ");
break;
case AgentLogEvent::DELETE:
str.assign("Deletion ");
info.set_event(str);
OPER_TRACE(Mpls, info);
return;
case AgentLogEvent::CHANGE:
str.assign("Modification ");
break;
default:
str.assign("Unknown");
break;
}
info.set_event(str);
const NextHop *nh = nexthop();
const Interface *intf = NULL;
/* Mpls is not expected to have any other nexthop apart from Interface
or Vlan */
if (nh != NULL) {
string policy_str("Disabled");
const InterfaceNH *if_nh;
const VlanNH *vlan_nh;
switch(nh->GetType()) {
case NextHop::INTERFACE:
nh_type.assign("INTERFACE");
if_nh = static_cast<const InterfaceNH *>(nh);
intf = if_nh->GetInterface();
if (if_nh->PolicyEnabled()) {
policy_str.assign("Enabled");
}
info.set_policy(policy_str);
break;
case NextHop::VLAN:
nh_type.assign("VLAN");
vlan_nh = static_cast<const VlanNH *>(nh);
intf = vlan_nh->GetInterface();
info.set_vlan_tag(vlan_nh->GetVlanTag());
break;
case NextHop::COMPOSITE:
nh_type.assign("Composite");
break;
default:
nh_type.assign("unknown");
break;
}
}
info.set_nh_type(nh_type);
/* Interface Nexthop pointed by Mpls object will always be of type VMPORT */
if (intf) {
string if_type_str;
switch(intf->type()) {
case Interface::VM_INTERFACE:
if_type_str.assign("VM_INTERFACE");
break;
default:
if_type_str.assign("Invalid");
break;
}
info.set_intf_type(if_type_str);
info.set_intf_uuid(UuidToString(intf->GetUuid()));
info.set_intf_name(intf->name());
}
OPER_TRACE(Mpls, info);
}
void MplsLabel::SyncDependentPath() {
for (DependentPathList::iterator iter =
mpls_label_.begin(); iter != mpls_label_.end(); iter++) {
AgentRoute *rt = iter.operator->();
LOG(DEBUG, "Syncing route" << rt->ToString());
rt->EnqueueRouteResync();
}
}
void MplsReq::HandleRequest() const {
AgentSandeshPtr sand(new AgentMplsSandesh(context()));
sand->DoSandesh(0, AgentSandesh::kEntriesPerPage);
}
AgentSandesh *MplsTable::GetAgentSandesh(const std::string &context) {
return new AgentMplsSandesh(context);
}