Skip to content

Commit cfc4b44

Browse files
committed
orchagent: Refacotring (sonic-net#20)
* orchagent: Refacotring - RouteOrch depends on NeighOrch - Move m_syncdNextHops to NeighOrch class * orchagent: Separating m_syncdNextHopGroups from m_syncdNextHops - NeighOrch class contains m_syncdNextHops that has next hop table - RouteOrch class contains m_syncdNextHopGruops that has next hop group table Separating m_syncdNextHops to next hops and next hop groups
1 parent e63c977 commit cfc4b44

File tree

5 files changed

+328
-235
lines changed

5 files changed

+328
-235
lines changed

orchagent/neighorch.cpp

+86-31
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,89 @@
22

33
#include "logger.h"
44

5+
#include "assert.h"
6+
57
extern sai_neighbor_api_t* sai_neighbor_api;
68
extern sai_next_hop_api_t* sai_next_hop_api;
79

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+
888
void NeighOrch::doTask(Consumer &consumer)
989
{
1090
SWSS_LOG_ENTER();
@@ -28,6 +108,7 @@ void NeighOrch::doTask(Consumer &consumer)
28108
it = consumer.m_toSync.erase(it);
29109
continue;
30110
}
111+
31112
string alias = key.substr(0, found);
32113
Port p;
33114

@@ -77,8 +158,8 @@ void NeighOrch::doTask(Consumer &consumer)
77158
else
78159
it++;
79160
}
80-
/* Cannot locate the neighbor */
81161
else
162+
/* Cannot locate the neighbor */
82163
it = consumer.m_toSync.erase(it);
83164
}
84165
else
@@ -120,21 +201,8 @@ bool NeighOrch::addNeighbor(NeighborEntry neighborEntry, MacAddress macAddress)
120201

121202
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());
122203

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))
135205
{
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-
138206
status = sai_neighbor_api->remove_neighbor_entry(&neighbor_entry);
139207
if (status != SAI_STATUS_SUCCESS)
140208
{
@@ -143,9 +211,6 @@ bool NeighOrch::addNeighbor(NeighborEntry neighborEntry, MacAddress macAddress)
143211
return false;
144212
}
145213

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-
149214
m_syncdNeighbors[neighborEntry] = macAddress;
150215
}
151216
else
@@ -168,7 +233,7 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)
168233
if (m_syncdNeighbors.find(neighborEntry) == m_syncdNeighbors.end())
169234
return true;
170235

171-
if (m_routeOrch->getNextHopRefCount(ip_address))
236+
if (m_syncdNextHops[ip_address].ref_count > 0)
172237
{
173238
SWSS_LOG_ERROR("Neighbor is still referenced ip:%s\n", ip_address.to_string().c_str());
174239
return false;
@@ -186,7 +251,7 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)
186251
neighbor_entry.ip_address.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
187252
neighbor_entry.ip_address.addr.ip4 = ip_address.getV4Addr();
188253

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;
190255
status = sai_next_hop_api->remove_next_hop(next_hop_id);
191256
if (status != SAI_STATUS_SUCCESS)
192257
{
@@ -212,21 +277,11 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)
212277
}
213278

214279
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-
}
225280
return false;
226281
}
227282

228283
m_syncdNeighbors.erase(neighborEntry);
229-
m_routeOrch->removeNextHopEntry(ip_address);
284+
removeNextHop(ip_address);
230285

231286
return true;
232287
}

orchagent/neighorch.h

+27-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
#include "orch.h"
55
#include "portsorch.h"
6-
#include "routeorch.h"
6+
7+
#include "ipaddress.h"
78

89
struct NeighborEntry
910
{
@@ -16,25 +17,45 @@ struct NeighborEntry
1617
}
1718
};
1819

20+
struct NextHopEntry
21+
{
22+
sai_object_id_t next_hop_id; // next hop id
23+
int ref_count; // reference count
24+
};
1925

2026
/* NeighborTable: NeighborEntry, neighbor MAC address */
2127
typedef map<NeighborEntry, MacAddress> NeighborTable;
28+
/* NextHopTable: next hop IP address, NextHopEntry */
29+
typedef map<IpAddress, NextHopEntry> NextHopTable;
2230

2331
class NeighOrch : public Orch
2432
{
2533
public:
26-
NeighOrch(DBConnector *db, string tableName, PortsOrch *portsOrch, RouteOrch *routeOrch) :
27-
Orch(db, tableName), m_portsOrch(portsOrch), m_routeOrch(routeOrch) {};
34+
NeighOrch(DBConnector *db, string tableName, PortsOrch *portsOrch) :
35+
Orch(db, tableName),
36+
m_portsOrch(portsOrch) {};
37+
38+
bool hasNextHop(IpAddress);
39+
40+
sai_object_id_t getNextHopId(IpAddress);
41+
int getNextHopRefCount(IpAddress);
42+
43+
void increaseNextHopRefCount(IpAddress);
44+
void decreaseNextHopRefCount(IpAddress);
45+
2846
private:
2947
PortsOrch *m_portsOrch;
30-
RouteOrch *m_routeOrch;
31-
32-
void doTask(Consumer &consumer);
3348

3449
NeighborTable m_syncdNeighbors;
50+
NextHopTable m_syncdNextHops;
51+
52+
bool addNextHop(IpAddress, Port);
53+
bool removeNextHop(IpAddress);
3554

3655
bool addNeighbor(NeighborEntry, MacAddress);
3756
bool removeNeighbor(NeighborEntry);
57+
58+
void doTask(Consumer &consumer);
3859
};
3960

4061
#endif /* SWSS_NEIGHORCH_H */

orchagent/orchdaemon.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ bool OrchDaemon::init()
3131

3232
m_portsO = new PortsOrch(m_applDb, APP_PORT_TABLE_NAME);
3333
m_intfsO = new IntfsOrch(m_applDb, APP_INTF_TABLE_NAME, m_portsO);
34-
m_routeO = new RouteOrch(m_applDb, APP_ROUTE_TABLE_NAME, m_portsO);
35-
m_neighO = new NeighOrch(m_applDb, APP_NEIGH_TABLE_NAME, m_portsO, m_routeO);
34+
m_neighO = new NeighOrch(m_applDb, APP_NEIGH_TABLE_NAME, m_portsO);
35+
m_routeO = new RouteOrch(m_applDb, APP_ROUTE_TABLE_NAME, m_portsO, m_neighO);
3636
m_select = new Select();
3737

3838
return true;

0 commit comments

Comments
 (0)