-
Notifications
You must be signed in to change notification settings - Fork 525
/
nexthopkey.h
202 lines (186 loc) · 6.43 KB
/
nexthopkey.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
#ifndef SWSS_NEXTHOPKEY_H
#define SWSS_NEXTHOPKEY_H
#include "ipaddress.h"
#include "tokenize.h"
#include "label.h"
#include "intfsorch.h"
#define LABELSTACK_DELIMITER '+'
#define NH_DELIMITER '@'
#define NHG_DELIMITER ','
#define VRF_PREFIX "Vrf"
extern IntfsOrch *gIntfsOrch;
struct NextHopKey
{
IpAddress ip_address; // neighbor IP address
string alias; // incoming interface alias
uint32_t vni; // Encap VNI overlay nexthop
MacAddress mac_address; // Overlay Nexthop MAC.
LabelStack label_stack; // MPLS label stack
uint32_t weight; // NH weight for NHGs
string srv6_segment; // SRV6 segment string
string srv6_source; // SRV6 source address
NextHopKey() : weight(0) {}
NextHopKey(const std::string &str, const std::string &alias) :
alias(alias), vni(0), mac_address(), weight(0)
{
std::string ip_str = parseMplsNextHop(str);
ip_address = ip_str;
}
NextHopKey(const IpAddress &ip, const std::string &alias) :
ip_address(ip), alias(alias), vni(0), mac_address(), weight(0) {}
NextHopKey(const std::string &str) :
vni(0), mac_address()
{
if (str.find(NHG_DELIMITER) != string::npos)
{
std::string err = "Error converting " + str + " to NextHop";
throw std::invalid_argument(err);
}
std::string ip_str = parseMplsNextHop(str);
auto keys = tokenize(ip_str, NH_DELIMITER);
if (keys.size() == 1)
{
ip_address = keys[0];
alias = gIntfsOrch->getRouterIntfsAlias(ip_address);
}
else if (keys.size() == 2)
{
ip_address = keys[0];
alias = keys[1];
if (!alias.compare(0, strlen(VRF_PREFIX), VRF_PREFIX))
{
alias = gIntfsOrch->getRouterIntfsAlias(ip_address, alias);
}
}
else
{
std::string err = "Error converting " + str + " to NextHop";
throw std::invalid_argument(err);
}
weight = 0;
}
NextHopKey(const std::string &str, bool overlay_nh, bool srv6_nh = false)
{
if (str.find(NHG_DELIMITER) != string::npos)
{
std::string err = "Error converting " + str + " to NextHop";
throw std::invalid_argument(err);
}
if (srv6_nh == true)
{
weight = 0;
vni = 0;
weight = 0;
auto keys = tokenize(str, NH_DELIMITER);
if (keys.size() != 3)
{
std::string err = "Error converting " + str + " to Nexthop";
throw std::invalid_argument(err);
}
ip_address = keys[0];
srv6_segment = keys[1];
srv6_source = keys[2];
}
else
{
std::string ip_str = parseMplsNextHop(str);
auto keys = tokenize(ip_str, NH_DELIMITER);
if (keys.size() != 4)
{
std::string err = "Error converting " + str + " to NextHop";
throw std::invalid_argument(err);
}
ip_address = keys[0];
alias = keys[1];
vni = static_cast<uint32_t>(std::stoul(keys[2]));
mac_address = keys[3];
weight = 0;
}
}
NextHopKey(const IpAddress &ip, const MacAddress &mac, const uint32_t &vni, bool overlay_nh) : ip_address(ip), alias(""), vni(vni), mac_address(mac), weight(0){}
const std::string to_string() const
{
std::string str = formatMplsNextHop();
str += ip_address.to_string() + NH_DELIMITER + alias;
return str;
}
const std::string to_string(bool overlay_nh, bool srv6_nh) const
{
if (srv6_nh)
{
return ip_address.to_string() + NH_DELIMITER + srv6_segment + NH_DELIMITER + srv6_source;
}
std::string str = formatMplsNextHop();
str += (ip_address.to_string() + NH_DELIMITER + alias + NH_DELIMITER +
std::to_string(vni) + NH_DELIMITER + mac_address.to_string());
return str;
}
bool operator<(const NextHopKey &o) const
{
return tie(ip_address, alias, label_stack, vni, mac_address, srv6_segment, srv6_source) <
tie(o.ip_address, o.alias, o.label_stack, o.vni, o.mac_address, o.srv6_segment, o.srv6_source);
}
bool operator==(const NextHopKey &o) const
{
return (ip_address == o.ip_address) && (alias == o.alias) &&
(label_stack == o.label_stack) &&
(vni == o.vni) && (mac_address == o.mac_address) &&
(srv6_segment == o.srv6_segment) && (srv6_source == o.srv6_source);
}
bool operator!=(const NextHopKey &o) const
{
return !(*this == o);
}
bool isIntfNextHop() const
{
return (ip_address.isZero() && !isSrv6NextHop());
}
bool isMplsNextHop() const
{
return (!label_stack.empty());
}
bool isSrv6NextHop() const
{
return (srv6_segment != "");
}
std::string parseMplsNextHop(const std::string& str)
{
// parseMplsNextHop initializes MPLS-related member data of the NextHopKey
// based on content of the input param str.
// label_stack may be updated.
std::string ip_str;
auto keys = tokenize(str, LABELSTACK_DELIMITER);
if (keys.size() == 1)
{
// No MPLS info to parse
ip_str = str;
}
else if (keys.size() == 2)
{
// Expected MPLS format = "<outsegtype><labelstack>+<non-mpls-str>"
// key[0] = <outsegtype> = "swap" | "push"
// key[0] = <labelstack> = "<label0>/<label1>/../<labelN>"
// key[1] = <non-mpls-str> = returned to caller and not parsed here
// Example = "push10100/10101+10.0.0.3@Ethernet4"
label_stack = LabelStack(keys[0]);
ip_str = keys[1];
}
else
{
// Malformed string
std::string err = "Error converting " + str + " to MPLS NextHop";
throw std::invalid_argument(err);
}
return ip_str;
}
std::string formatMplsNextHop() const
{
std::string str;
if (isMplsNextHop())
{
str = label_stack.to_string() + LABELSTACK_DELIMITER;
}
return str;
}
};
#endif /* SWSS_NEXTHOPKEY_H */