-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathSwitchStateBaseRoute.cpp
More file actions
310 lines (254 loc) · 9.85 KB
/
Copy pathSwitchStateBaseRoute.cpp
File metadata and controls
310 lines (254 loc) · 9.85 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
/*
* Copyright (c) 2023 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SwitchStateBase.h"
#include "swss/logger.h"
#include "swss/exec.h"
#include "swss/converter.h"
#include "sai_serialize.h"
#include "NotificationPortStateChange.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include "SwitchStateBaseNexthop.h"
#include "vppxlate/SaiVppXlate.h"
#include "SwitchStateBaseUtils.h"
using namespace saivpp;
void create_route_prefix_entry (
sai_route_entry_t *route_entry,
vpp_ip_route_t *ip_route)
{
const sai_ip_prefix_t *ip_address = &route_entry->destination;
switch (ip_address->addr_family) {
case SAI_IP_ADDR_FAMILY_IPV4:
{
struct sockaddr_in *sin = &ip_route->prefix_addr.addr.ip4;
ip_route->prefix_addr.sa_family = AF_INET;
sin->sin_addr.s_addr = ip_address->addr.ip4;
ip_route->prefix_len = getPrefixLenFromAddrMask(reinterpret_cast<const uint8_t*>(&ip_address->mask.ip4), 4);
break;
}
case SAI_IP_ADDR_FAMILY_IPV6:
{
struct sockaddr_in6 *sin6 = &ip_route->prefix_addr.addr.ip6;
ip_route->prefix_addr.sa_family = AF_INET6;
memcpy(sin6->sin6_addr.s6_addr, ip_address->addr.ip6, sizeof(sin6->sin6_addr.s6_addr));
ip_route->prefix_len = getPrefixLenFromAddrMask(ip_address->mask.ip6, 16);
break;
}
}
}
void create_vpp_nexthop_entry (
nexthop_grp_member_t *nxt_grp_member,
vpp_nexthop_type_e type,
vpp_ip_nexthop_t *vpp_nexthop)
{
sai_ip_address_t *ip_address = &nxt_grp_member->addr;
switch (ip_address->addr_family) {
case SAI_IP_ADDR_FAMILY_IPV4:
{
struct sockaddr_in *sin = &vpp_nexthop->addr.addr.ip4;
vpp_nexthop->addr.sa_family = AF_INET;
sin->sin_addr.s_addr = ip_address->addr.ip4;
break;
}
case SAI_IP_ADDR_FAMILY_IPV6:
{
struct sockaddr_in6 *sin6 = &vpp_nexthop->addr.addr.ip6;
vpp_nexthop->addr.sa_family = AF_INET6;
memcpy(sin6->sin6_addr.s6_addr, ip_address->addr.ip6, sizeof(sin6->sin6_addr.s6_addr));
break;
}
}
vpp_nexthop->type = type;
if (strlen(nxt_grp_member->if_name) > 0) {
vpp_nexthop->hwif_name = nxt_grp_member->if_name;
} else {
vpp_nexthop->hwif_name = NULL;
}
vpp_nexthop->sw_if_index = nxt_grp_member->sw_if_index;
vpp_nexthop->weight = (uint8_t) nxt_grp_member->weight;
vpp_nexthop->preference = 0;
}
sai_status_t SwitchStateBase::IpRouteAddRemove(
_In_ const SaiObject* route_obj,
_In_ bool is_add)
{
SWSS_LOG_ENTER();
int ret = SAI_STATUS_SUCCESS;
sai_status_t status;
sai_object_id_t next_hop_oid;
sai_attribute_t attr;
std::string serializedObjectId = route_obj->get_id();
attr.id = SAI_ROUTE_ENTRY_ATTR_NEXT_HOP_ID;
CHECK_STATUS_QUIET(route_obj->get_mandatory_attr(attr));
next_hop_oid = attr.value.oid;
sai_route_entry_t route_entry;
vpp_nexthop_type_e nexthop_type = VPP_NEXTHOP_NORMAL;
bool config_ip_route = false;
sai_deserialize_route_entry(serializedObjectId, route_entry);
nexthop_grp_config_t *nxthop_group = NULL;
if (SAI_OBJECT_TYPE_ROUTER_INTERFACE == sai_object_type_query(next_hop_oid))
{
// vpp_add_del_intf_ip_addr(route_entry.destination, next_hop_oid, is_add);
}
else if (SAI_OBJECT_TYPE_PORT == sai_object_type_query(next_hop_oid))
{
attr.id = SAI_ROUTE_ENTRY_ATTR_PACKET_ACTION;
status = route_obj->get_attr(attr);
if (status == SAI_STATUS_SUCCESS && SAI_PACKET_ACTION_FORWARD == attr.value.s32) {
vpp_add_del_intf_ip_addr_norif(serializedObjectId, route_entry, is_add);
}
}
else if (SAI_OBJECT_TYPE_NEXT_HOP == sai_object_type_query(next_hop_oid))
{
if (IpRouteNexthopEntry(next_hop_oid, &nxthop_group) == SAI_STATUS_SUCCESS)
{
config_ip_route = true;
}
}
else if (SAI_OBJECT_TYPE_NEXT_HOP_GROUP == sai_object_type_query(next_hop_oid))
{
if (IpRouteNexthopGroupEntry(next_hop_oid, &nxthop_group) == SAI_STATUS_SUCCESS)
{
config_ip_route = true;
}
}
if (config_ip_route == true)
{
std::shared_ptr<IpVrfInfo> vrf;
uint32_t vrf_id;
vrf = vpp_get_ip_vrf(route_entry.vr_id);
if (vrf == nullptr) {
vrf_id = 0;
} else {
vrf_id = vrf->m_vrf_id;
}
vpp_ip_route_t *ip_route = (vpp_ip_route_t *)
calloc(1, sizeof(vpp_ip_route_t) + (nxthop_group->nmembers * sizeof(vpp_ip_nexthop_t)));
if (!ip_route) {
return SAI_STATUS_FAILURE;
}
create_route_prefix_entry(&route_entry, ip_route);
ip_route->vrf_id = vrf_id;
ip_route->is_multipath = (nxthop_group->nmembers > 1) ? true : false;
nexthop_grp_member_t *nxt_grp_member;
nxt_grp_member = nxthop_group->grp_members;
size_t i;
for (i = 0; i < nxthop_group->nmembers; i++) {
create_vpp_nexthop_entry(nxt_grp_member, nexthop_type, &ip_route->nexthop[i]);
nxt_grp_member++;
}
ip_route->nexthop_cnt = nxthop_group->nmembers;
ret = ip_route_add_del(ip_route, is_add);
SWSS_LOG_NOTICE("%s ip route in VPP %s status %d table %u", (is_add ? "Add" : "Remove"),
serializedObjectId.c_str(), ret, vrf_id);
SWSS_LOG_NOTICE("%s route nexthop type %s count %u", (is_add ? "Add" : "Remove"),
sai_serialize_object_type(sai_object_type_query(next_hop_oid)).c_str(),
nxthop_group->nmembers);
free(ip_route);
free(nxthop_group);
} else {
SWSS_LOG_NOTICE("Ignoring VPP ip route %s", serializedObjectId.c_str());
}
return ret;
}
sai_status_t SwitchStateBase::addIpRoute(
_In_ const std::string &serializedObjectId,
_In_ sai_object_id_t switch_id,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
bool isLoopback = false;
bool isTunnelNh = false;
bool isSRv6Nh = false;
SWSS_LOG_ENTER();
SaiCachedObject ip_route_obj(this, SAI_OBJECT_TYPE_ROUTE_ENTRY, serializedObjectId, attr_count, attr_list);
auto nh_obj = ip_route_obj.get_linked_object(SAI_OBJECT_TYPE_NEXT_HOP, SAI_ROUTE_ENTRY_ATTR_NEXT_HOP_ID);
if (nh_obj != nullptr) {
sai_attribute_t attr;
attr.id = SAI_NEXT_HOP_ATTR_TYPE;
CHECK_STATUS_W_MSG(nh_obj->get_attr(attr), "Missing SAI_NEXT_HOP_ATTR_TYPE in tunnel obj");
isTunnelNh = (attr.value.s32 == SAI_NEXT_HOP_TYPE_TUNNEL_ENCAP);
isSRv6Nh = (attr.value.s32 == SAI_NEXT_HOP_TYPE_SRV6_SIDLIST);
}
if (isTunnelNh) {
IpRouteAddRemove(&ip_route_obj, true);
} else {
process_interface_loopback(serializedObjectId, isLoopback, true);
if (isLoopback == false && is_ip_nbr_active() == true)
{
if (isSRv6Nh) {
m_tunnel_mgr_srv6.create_sidlist_route_entry(serializedObjectId, switch_id, attr_count, attr_list);
} else {
IpRouteAddRemove(&ip_route_obj, true);
}
}
}
CHECK_STATUS(create_internal(SAI_OBJECT_TYPE_ROUTE_ENTRY, serializedObjectId, switch_id, attr_count, attr_list));
return SAI_STATUS_SUCCESS;
}
sai_status_t SwitchStateBase::updateIpRoute(
_In_ const std::string &serializedObjectId,
_In_ const sai_attribute_t *attr)
{
SWSS_LOG_ENTER();
if (is_ip_nbr_active() == true) {
SWSS_LOG_NOTICE("ip route entry update %s", serializedObjectId.c_str());
SaiModDBObject route_mod_obj(this, SAI_OBJECT_TYPE_ROUTE_ENTRY, serializedObjectId, 1, attr);
auto route_db_obj = route_mod_obj.get_db_obj();
if (!route_db_obj) {
SWSS_LOG_ERROR("Failed to find SAI_OBJECT_TYPE_ROUTE_ENTRY SaiObject: %s", serializedObjectId.c_str());
return SAI_STATUS_FAILURE;
} else {
IpRouteAddRemove(route_db_obj.get(), false);
}
IpRouteAddRemove(&route_mod_obj, true);
}
set_internal(SAI_OBJECT_TYPE_ROUTE_ENTRY, serializedObjectId, attr);
return SAI_STATUS_SUCCESS;
}
sai_status_t SwitchStateBase::removeIpRoute(
_In_ const std::string &serializedObjectId)
{
SWSS_LOG_ENTER();
bool isLoopback = false;
bool isSRv6Nh = false;
sai_attribute_t attr;
sai_object_id_t nh_oid;
process_interface_loopback(serializedObjectId, isLoopback, false);
if (isLoopback == false && is_ip_nbr_active() == true) {
auto route_obj = get_sai_object(SAI_OBJECT_TYPE_ROUTE_ENTRY, serializedObjectId);
if (route_obj) {
attr.id = SAI_ROUTE_ENTRY_ATTR_NEXT_HOP_ID;
if(route_obj->get_attr(attr) == SAI_STATUS_SUCCESS) {
nh_oid = attr.value.oid;
attr.id = SAI_NEXT_HOP_ATTR_TYPE;
if(get(SAI_OBJECT_TYPE_NEXT_HOP, nh_oid, 1, &attr) == SAI_STATUS_SUCCESS) {
isSRv6Nh = (attr.value.s32 == SAI_NEXT_HOP_TYPE_SRV6_SIDLIST);
}
}
if (isSRv6Nh) {
m_tunnel_mgr_srv6.remove_sidlist_route_entry(serializedObjectId, nh_oid);
} else {
IpRouteAddRemove(route_obj.get(), false);
}
}
}
CHECK_STATUS(remove_internal(SAI_OBJECT_TYPE_ROUTE_ENTRY, serializedObjectId));
return SAI_STATUS_SUCCESS;
}