2
2
3
3
#include " logger.h"
4
4
5
+ #include " assert.h"
6
+
5
7
extern sai_neighbor_api_t * sai_neighbor_api;
6
8
extern sai_next_hop_api_t * sai_next_hop_api;
7
9
10
+ bool NeighOrch::hasNextHop (IpAddress ipAddress)
11
+ {
12
+ return m_syncdNextHops.find (ipAddress) != m_syncdNextHops.end ();
13
+ }
14
+
15
+ bool NeighOrch::addNextHop (IpAddress ipAddress, Port port)
16
+ {
17
+ SWSS_LOG_ENTER ();
18
+
19
+ assert (!hasNextHop (ipAddress));
20
+
21
+ sai_attribute_t next_hop_attrs[3 ];
22
+ next_hop_attrs[0 ].id = SAI_NEXT_HOP_ATTR_TYPE;
23
+ next_hop_attrs[0 ].value .s32 = SAI_NEXT_HOP_IP;
24
+ next_hop_attrs[1 ].id = SAI_NEXT_HOP_ATTR_IP;
25
+ next_hop_attrs[1 ].value .ipaddr .addr_family = SAI_IP_ADDR_FAMILY_IPV4;
26
+ next_hop_attrs[1 ].value .ipaddr .addr .ip4 = ipAddress.getV4Addr ();
27
+ next_hop_attrs[2 ].id = SAI_NEXT_HOP_ATTR_ROUTER_INTERFACE_ID;
28
+ next_hop_attrs[2 ].value .oid = port.m_rif_id ;
29
+
30
+ sai_object_id_t next_hop_id;
31
+ sai_status_t status = sai_next_hop_api->create_next_hop (&next_hop_id, 3 , next_hop_attrs);
32
+ if (status != SAI_STATUS_SUCCESS)
33
+ {
34
+ SWSS_LOG_ERROR (" Failed to create next hop entry ip:%s rid%llx\n " ,
35
+ ipAddress.to_string ().c_str (), port.m_rif_id );
36
+ return false ;
37
+ }
38
+
39
+ NextHopEntry next_hop_entry;
40
+ next_hop_entry.next_hop_id = next_hop_id;
41
+ next_hop_entry.ref_count = 0 ;
42
+ m_syncdNextHops[ipAddress] = next_hop_entry;
43
+
44
+ return true ;
45
+ }
46
+
47
+ bool NeighOrch::removeNextHop (IpAddress ipAddress)
48
+ {
49
+ SWSS_LOG_ENTER ();
50
+
51
+ assert (hasNextHop (ipAddress));
52
+
53
+ if (m_syncdNextHops[ipAddress].ref_count > 0 )
54
+ {
55
+ SWSS_LOG_ERROR (" Failed to remove still referenced next hop entry ip:%s" ,
56
+ ipAddress.to_string ().c_str ());
57
+ return false ;
58
+ }
59
+
60
+ m_syncdNextHops.erase (ipAddress);
61
+ return true ;
62
+ }
63
+
64
+ sai_object_id_t NeighOrch::getNextHopId (IpAddress ipAddress)
65
+ {
66
+ assert (hasNextHop (ipAddress));
67
+ return m_syncdNextHops[ipAddress].next_hop_id ;
68
+ }
69
+
70
+ int NeighOrch::getNextHopRefCount (IpAddress ipAddress)
71
+ {
72
+ assert (hasNextHop (ipAddress));
73
+ return m_syncdNextHops[ipAddress].ref_count ;
74
+ }
75
+
76
+ void NeighOrch::increaseNextHopRefCount (IpAddress ipAddress)
77
+ {
78
+ assert (hasNextHop (ipAddress));
79
+ m_syncdNextHops[ipAddress].ref_count ++;
80
+ }
81
+
82
+ void NeighOrch::decreaseNextHopRefCount (IpAddress ipAddress)
83
+ {
84
+ assert (hasNextHop (ipAddress));
85
+ m_syncdNextHops[ipAddress].ref_count --;
86
+ }
87
+
8
88
void NeighOrch::doTask (Consumer &consumer)
9
89
{
10
90
SWSS_LOG_ENTER ();
@@ -28,6 +108,7 @@ void NeighOrch::doTask(Consumer &consumer)
28
108
it = consumer.m_toSync .erase (it);
29
109
continue ;
30
110
}
111
+
31
112
string alias = key.substr (0 , found);
32
113
Port p;
33
114
@@ -77,8 +158,8 @@ void NeighOrch::doTask(Consumer &consumer)
77
158
else
78
159
it++;
79
160
}
80
- /* Cannot locate the neighbor */
81
161
else
162
+ /* Cannot locate the neighbor */
82
163
it = consumer.m_toSync .erase (it);
83
164
}
84
165
else
@@ -120,21 +201,8 @@ bool NeighOrch::addNeighbor(NeighborEntry neighborEntry, MacAddress macAddress)
120
201
121
202
SWSS_LOG_NOTICE (" Create neighbor entry rid:%llx alias:%s ip:%s\n " , p.m_rif_id , alias.c_str (), ip_address.to_string ().c_str ());
122
203
123
- sai_attribute_t next_hop_attrs[3 ];
124
- next_hop_attrs[0 ].id = SAI_NEXT_HOP_ATTR_TYPE;
125
- next_hop_attrs[0 ].value .s32 = SAI_NEXT_HOP_IP;
126
- next_hop_attrs[1 ].id = SAI_NEXT_HOP_ATTR_IP;
127
- next_hop_attrs[1 ].value .ipaddr .addr_family = SAI_IP_ADDR_FAMILY_IPV4;
128
- next_hop_attrs[1 ].value .ipaddr .addr .ip4 = ip_address.getV4Addr ();
129
- next_hop_attrs[2 ].id = SAI_NEXT_HOP_ATTR_ROUTER_INTERFACE_ID;
130
- next_hop_attrs[2 ].value .oid = p.m_rif_id ;
131
-
132
- sai_object_id_t next_hop_id;
133
- status = sai_next_hop_api->create_next_hop (&next_hop_id, 3 , next_hop_attrs);
134
- if (status != SAI_STATUS_SUCCESS)
204
+ if (!addNextHop (ip_address, p))
135
205
{
136
- SWSS_LOG_ERROR (" Failed to create next hop ip:%s rid:%llx\n " , ip_address.to_string ().c_str (), p.m_rif_id );
137
-
138
206
status = sai_neighbor_api->remove_neighbor_entry (&neighbor_entry);
139
207
if (status != SAI_STATUS_SUCCESS)
140
208
{
@@ -143,9 +211,6 @@ bool NeighOrch::addNeighbor(NeighborEntry neighborEntry, MacAddress macAddress)
143
211
return false ;
144
212
}
145
213
146
- SWSS_LOG_NOTICE (" Create next hop ip:%s rid:%llx\n " , ip_address.to_string ().c_str (), p.m_rif_id );
147
- m_routeOrch->createNextHopEntry (ip_address, next_hop_id);
148
-
149
214
m_syncdNeighbors[neighborEntry] = macAddress;
150
215
}
151
216
else
@@ -168,7 +233,7 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)
168
233
if (m_syncdNeighbors.find (neighborEntry) == m_syncdNeighbors.end ())
169
234
return true ;
170
235
171
- if (m_routeOrch-> getNextHopRefCount ( ip_address) )
236
+ if (m_syncdNextHops[ ip_address]. ref_count > 0 )
172
237
{
173
238
SWSS_LOG_ERROR (" Neighbor is still referenced ip:%s\n " , ip_address.to_string ().c_str ());
174
239
return false ;
@@ -186,7 +251,7 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)
186
251
neighbor_entry.ip_address .addr_family = SAI_IP_ADDR_FAMILY_IPV4;
187
252
neighbor_entry.ip_address .addr .ip4 = ip_address.getV4Addr ();
188
253
189
- sai_object_id_t next_hop_id = m_routeOrch-> getNextHopEntry ( ip_address) .next_hop_id ;
254
+ sai_object_id_t next_hop_id = m_syncdNextHops[ ip_address] .next_hop_id ;
190
255
status = sai_next_hop_api->remove_next_hop (next_hop_id);
191
256
if (status != SAI_STATUS_SUCCESS)
192
257
{
@@ -212,21 +277,11 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)
212
277
}
213
278
214
279
SWSS_LOG_ERROR (" Failed to remove neighbor entry rid:%llx ip:%s\n " , p.m_rif_id , ip_address.to_string ().c_str ());
215
-
216
- sai_attribute_t attr;
217
- attr.id = SAI_NEIGHBOR_ATTR_DST_MAC_ADDRESS;
218
- memcpy (attr.value .mac , m_syncdNeighbors[neighborEntry].getMac (), 6 );
219
-
220
- status = sai_neighbor_api->create_neighbor_entry (&neighbor_entry, 1 , &attr);
221
- if (status != SAI_STATUS_SUCCESS)
222
- {
223
- SWSS_LOG_ERROR (" Failed to create neighbor entry mac:%s\n " , m_syncdNeighbors[neighborEntry].to_string ().c_str ());
224
- }
225
280
return false ;
226
281
}
227
282
228
283
m_syncdNeighbors.erase (neighborEntry);
229
- m_routeOrch-> removeNextHopEntry (ip_address);
284
+ removeNextHop (ip_address);
230
285
231
286
return true ;
232
287
}
0 commit comments