Skip to content

Commit f28af11

Browse files
author
Shuotian Cheng
committed
Moving portsOrch to global and refactoring intfsOrch
- Moving portsOrch to global - Adding intfsOrch dependency check - Adding struct IntfsEntry - Adding increase/decrease reference count functions - Multiple IPs support - Removing trailing \n in logs
1 parent 6ed1efc commit f28af11

14 files changed

Lines changed: 326 additions & 261 deletions

orchagent/intfsorch.cpp

Lines changed: 170 additions & 127 deletions
Large diffs are not rendered by default.

orchagent/intfsorch.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,45 @@
55
#include "portsorch.h"
66

77
#include "ipaddresses.h"
8+
#include "ipprefix.h"
89
#include "macaddress.h"
910

1011
#include <map>
1112

1213
extern sai_object_id_t gVirtualRouterId;
1314
extern MacAddress gMacAddress;
1415

15-
typedef map<string, IpAddresses> IntfsTable;
16+
struct IntfsEntry
17+
{
18+
IpAddresses ip_addresses;
19+
int ref_count;
20+
};
21+
22+
typedef map<string, IntfsEntry> IntfsTable;
1623

1724
class IntfsOrch : public Orch
1825
{
1926
public:
20-
IntfsOrch(DBConnector *db, string tableName, PortsOrch *portsOrch);
27+
IntfsOrch(DBConnector *db, string tableName);
28+
29+
sai_object_id_t getRouterIntfsId(string);
30+
31+
void increaseRouterIntfsRefCount(const string);
32+
void decreaseRouterIntfsRefCount(const string);
2133
private:
22-
PortsOrch *m_portsOrch;
23-
IntfsTable m_intfs;
34+
IntfsTable m_syncdIntfses;
2435
void doTask(Consumer &consumer);
2536

26-
bool addRouterIntfs(Port &port, sai_object_id_t virtual_router_id = gVirtualRouterId,
27-
MacAddress mac_address = gMacAddress);
37+
int getRouterIntfsRefCount(string);
38+
39+
bool addRouterIntfs(Port &port);
2840
bool removeRouterIntfs(Port &port);
41+
42+
void addSubnetRoute(const Port &port, const IpPrefix &ip_prefix);
43+
void removeSubnetRoute(const Port &port, const IpPrefix &ip_prefix);
44+
45+
void addIp2MeRoute(const Port &port, const IpPrefix &ip_prefix);
46+
void removeIp2MeRoute(const Port &port, const IpPrefix &ip_prefix);
2947
};
3048

3149
#endif /* SWSS_INTFSORCH_H */

orchagent/main.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ sai_lag_api_t* sai_lag_api;
3434
sai_policer_api_t* sai_policer_api;
3535
sai_tunnel_api_t* sai_tunnel_api;
3636

37+
/* Global variables */
3738
map<string, string> gProfileMap;
3839
sai_object_id_t gVirtualRouterId;
39-
sai_object_id_t underlayIfId;
40+
sai_object_id_t gUnderlayIfId;
4041
MacAddress gMacAddress;
4142

4243
const char *test_profile_get_value (
@@ -128,15 +129,15 @@ int main(int argc, char **argv)
128129
}
129130
}
130131

131-
SWSS_LOG_NOTICE("--- Starting Orchestration Agent ---\n");
132+
SWSS_LOG_NOTICE("--- Starting Orchestration Agent ---");
132133

133134
initSaiApi();
134135

135-
SWSS_LOG_NOTICE("sai_switch_api: initializing switch\n");
136+
SWSS_LOG_NOTICE("sai_switch_api: initializing switch");
136137
status = sai_switch_api->initialize_switch(0, "", "", &switch_notifications);
137138
if (status != SAI_STATUS_SUCCESS)
138139
{
139-
SWSS_LOG_ERROR("Failed to initialize switch %d\n", status);
140+
SWSS_LOG_ERROR("Failed to initialize switch %d", status);
140141
exit(EXIT_FAILURE);
141142
}
142143

@@ -147,7 +148,7 @@ int main(int argc, char **argv)
147148
status = sai_switch_api->get_switch_attribute(1, &attr);
148149
if (status != SAI_STATUS_SUCCESS)
149150
{
150-
SWSS_LOG_ERROR("Failed to get MAC address from switch %d\n", status);
151+
SWSS_LOG_ERROR("Failed to get MAC address from switch %d", status);
151152
exit(EXIT_FAILURE);
152153
}
153154
else
@@ -161,11 +162,12 @@ int main(int argc, char **argv)
161162
status = sai_switch_api->set_switch_attribute(&attr);
162163
if (status != SAI_STATUS_SUCCESS)
163164
{
164-
SWSS_LOG_ERROR("Failed to set MAC address to switch %d\n", status);
165+
SWSS_LOG_ERROR("Failed to set MAC address to switch %d", status);
165166
exit(EXIT_FAILURE);
166167
}
167168
}
168169

170+
/* Get the default virtual router ID */
169171
attr.id = SAI_SWITCH_ATTR_DEFAULT_VIRTUAL_ROUTER_ID;
170172
status = sai_switch_api->get_switch_attribute(1, &attr);
171173
if (status != SAI_STATUS_SUCCESS)
@@ -175,29 +177,30 @@ int main(int argc, char **argv)
175177
}
176178

177179
gVirtualRouterId = attr.value.oid;
180+
SWSS_LOG_NOTICE("Get switch virtual router ID %llx", gVirtualRouterId);
178181

179-
SWSS_LOG_NOTICE("Get switch virtual router ID %llx\n", gVirtualRouterId);
180-
181-
// create the underlay router interface to create a LOOPBACK type router interface (encap)
182+
/* Create a loopback underlay router interface */
182183
sai_attribute_t underlay_intf_attrs[2];
183184
underlay_intf_attrs[0].id = SAI_ROUTER_INTERFACE_ATTR_VIRTUAL_ROUTER_ID;
184185
underlay_intf_attrs[0].value.oid = gVirtualRouterId;
185186
underlay_intf_attrs[1].id = SAI_ROUTER_INTERFACE_ATTR_TYPE;
186187
underlay_intf_attrs[1].value.s32 = SAI_ROUTER_INTERFACE_TYPE_LOOPBACK;
187188

188-
status = sai_router_intfs_api->create_router_interface(&underlayIfId, 2, underlay_intf_attrs);
189+
status = sai_router_intfs_api->create_router_interface(&gUnderlayIfId, 2, underlay_intf_attrs);
189190
if (status != SAI_STATUS_SUCCESS)
190191
{
191192
SWSS_LOG_ERROR("Failed to create underlay router interface %d", status);
192193
return false;
193194
}
194195

195-
SWSS_LOG_NOTICE("Created underlay router interface ID %llx\n", underlayIfId);
196+
SWSS_LOG_NOTICE("Created underlay router interface ID %llx", gUnderlayIfId);
196197

197-
OrchDaemon *orchDaemon = new OrchDaemon();
198+
/* Initialize orchestration components */
199+
DBConnector *appl_db = new DBConnector(APPL_DB, "localhost", 6379, 0);
200+
OrchDaemon *orchDaemon = new OrchDaemon(appl_db);
198201
if (!orchDaemon->init())
199202
{
200-
SWSS_LOG_ERROR("Failed to initialize orchstration daemon\n");
203+
SWSS_LOG_ERROR("Failed to initialize orchstration daemon");
201204
exit(EXIT_FAILURE);
202205
}
203206

@@ -206,11 +209,11 @@ int main(int argc, char **argv)
206209
}
207210
catch (char const *e)
208211
{
209-
SWSS_LOG_ERROR("Exception: %s\n", e);
212+
SWSS_LOG_ERROR("Exception: %s", e);
210213
}
211214
catch (exception& e)
212215
{
213-
SWSS_LOG_ERROR("Failed due to exception: %s\n", e.what());
216+
SWSS_LOG_ERROR("Failed due to exception: %s", e.what());
214217
}
215218

216219
return 0;

orchagent/neighorch.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,57 @@
66
extern sai_neighbor_api_t* sai_neighbor_api;
77
extern sai_next_hop_api_t* sai_next_hop_api;
88

9+
extern PortsOrch *gPortsOrch;
10+
11+
NeighOrch::NeighOrch(DBConnector *db, string tableName, IntfsOrch *intfsOrch) :
12+
Orch(db, tableName), m_intfsOrch(intfsOrch)
13+
{
14+
SWSS_LOG_ENTER();
15+
}
16+
917
bool NeighOrch::hasNextHop(IpAddress ipAddress)
1018
{
1119
return m_syncdNextHops.find(ipAddress) != m_syncdNextHops.end();
1220
}
1321

14-
bool NeighOrch::addNextHop(IpAddress ipAddress, Port port)
22+
bool NeighOrch::addNextHop(IpAddress ipAddress, string alias)
1523
{
1624
SWSS_LOG_ENTER();
1725

1826
assert(!hasNextHop(ipAddress));
19-
20-
if (port.m_rif_id == 0)
21-
return false;
27+
sai_object_id_t rif_id = m_intfsOrch->getRouterIntfsId(alias);
2228

2329
sai_attribute_t next_hop_attrs[3];
2430
next_hop_attrs[0].id = SAI_NEXT_HOP_ATTR_TYPE;
2531
next_hop_attrs[0].value.s32 = SAI_NEXT_HOP_IP;
2632
next_hop_attrs[1].id = SAI_NEXT_HOP_ATTR_IP;
2733
copy(next_hop_attrs[1].value.ipaddr, ipAddress);
2834
next_hop_attrs[2].id = SAI_NEXT_HOP_ATTR_ROUTER_INTERFACE_ID;
29-
next_hop_attrs[2].value.oid = port.m_rif_id;
35+
next_hop_attrs[2].value.oid = rif_id;
3036

3137
sai_object_id_t next_hop_id;
3238
sai_status_t status = sai_next_hop_api->create_next_hop(&next_hop_id, 3, next_hop_attrs);
3339
if (status != SAI_STATUS_SUCCESS)
3440
{
35-
SWSS_LOG_ERROR("Failed to create next hop entry ip:%s rid%llx\n",
36-
ipAddress.to_string().c_str(), port.m_rif_id);
41+
SWSS_LOG_ERROR("Failed to create next hop entry ip:%s rid:%llx",
42+
ipAddress.to_string().c_str(), rif_id);
3743
return false;
3844
}
3945

46+
SWSS_LOG_NOTICE("Create next hop entry id:%llx, ip:%s, rid:%llx\n",
47+
next_hop_id, ipAddress.to_string().c_str(), rif_id);
48+
4049
NextHopEntry next_hop_entry;
4150
next_hop_entry.next_hop_id = next_hop_id;
4251
next_hop_entry.ref_count = 0;
4352
m_syncdNextHops[ipAddress] = next_hop_entry;
4453

54+
m_intfsOrch->increaseRouterIntfsRefCount(alias);
55+
4556
return true;
4657
}
4758

48-
bool NeighOrch::removeNextHop(IpAddress ipAddress)
59+
bool NeighOrch::removeNextHop(IpAddress ipAddress, string alias)
4960
{
5061
SWSS_LOG_ENTER();
5162

@@ -59,6 +70,7 @@ bool NeighOrch::removeNextHop(IpAddress ipAddress)
5970
}
6071

6172
m_syncdNextHops.erase(ipAddress);
73+
m_intfsOrch->decreaseRouterIntfsRefCount(alias);
6274
return true;
6375
}
6476

@@ -90,9 +102,6 @@ void NeighOrch::doTask(Consumer &consumer)
90102
{
91103
SWSS_LOG_ENTER();
92104

93-
if (!m_portsOrch->isInitDone())
94-
return;
95-
96105
auto it = consumer.m_toSync.begin();
97106
while (it != consumer.m_toSync.end())
98107
{
@@ -110,7 +119,7 @@ void NeighOrch::doTask(Consumer &consumer)
110119
string alias = key.substr(0, found);
111120
Port p;
112121

113-
if (!m_portsOrch->getPort(alias, p))
122+
if (!gPortsOrch->getPort(alias, p))
114123
{
115124
it = consumer.m_toSync.erase(it);
116125
continue;
@@ -140,6 +149,7 @@ void NeighOrch::doTask(Consumer &consumer)
140149
it++;
141150
}
142151
else
152+
/* Duplicate entry */
143153
it = consumer.m_toSync.erase(it);
144154
}
145155
else if (op == DEL_COMMAND)
@@ -171,14 +181,10 @@ bool NeighOrch::addNeighbor(NeighborEntry neighborEntry, MacAddress macAddress)
171181
IpAddress ip_address = neighborEntry.ip_address;
172182
string alias = neighborEntry.alias;
173183

174-
Port p;
175-
m_portsOrch->getPort(alias, p);
176-
177-
if (p.m_rif_id == 0)
178-
return false;
184+
sai_object_id_t rif_id = m_intfsOrch->getRouterIntfsId(alias);
179185

180186
sai_neighbor_entry_t neighbor_entry;
181-
neighbor_entry.rif_id = p.m_rif_id;
187+
neighbor_entry.rif_id = rif_id;
182188
copy(neighbor_entry.ip_address, ip_address);
183189

184190
sai_attribute_t neighbor_attr;
@@ -194,15 +200,18 @@ bool NeighOrch::addNeighbor(NeighborEntry neighborEntry, MacAddress macAddress)
194200
return false;
195201
}
196202

197-
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());
203+
SWSS_LOG_NOTICE("Create neighbor entry rid:%llx alias:%s ip:%s\n", rif_id, alias.c_str(), ip_address.to_string().c_str());
204+
m_intfsOrch->increaseRouterIntfsRefCount(alias);
198205

199-
if (!addNextHop(ip_address, p))
206+
if (!addNextHop(ip_address, alias))
200207
{
201208
status = sai_neighbor_api->remove_neighbor_entry(&neighbor_entry);
202209
if (status != SAI_STATUS_SUCCESS)
203210
{
204-
SWSS_LOG_ERROR("Failed to remove neighbor entry rid:%llx alias:%s ip:%s\n", p.m_rif_id, alias.c_str(), ip_address.to_string().c_str());
211+
SWSS_LOG_ERROR("Failed to remove neighbor entry rid:%llx alias:%s ip:%s\n", rif_id, alias.c_str(), ip_address.to_string().c_str());
212+
return false;
205213
}
214+
m_intfsOrch->decreaseRouterIntfsRefCount(alias);
206215
return false;
207216
}
208217

@@ -234,15 +243,10 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)
234243
return false;
235244
}
236245

237-
Port p;
238-
if (!m_portsOrch->getPort(alias, p))
239-
{
240-
SWSS_LOG_ERROR("Failed to locate port alias:%s\n", alias.c_str());
241-
return false;
242-
}
246+
sai_object_id_t rif_id = m_intfsOrch->getRouterIntfsId(alias);
243247

244248
sai_neighbor_entry_t neighbor_entry;
245-
neighbor_entry.rif_id = p.m_rif_id;
249+
neighbor_entry.rif_id = rif_id;
246250
copy(neighbor_entry.ip_address, ip_address);
247251

248252
sai_object_id_t next_hop_id = m_syncdNextHops[ip_address].next_hop_id;
@@ -266,16 +270,17 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)
266270
{
267271
if (status == SAI_STATUS_ITEM_NOT_FOUND)
268272
{
269-
SWSS_LOG_ERROR("Failed to locate neigbor entry rid:%llx ip:%s\n", p.m_rif_id, ip_address.to_string().c_str());
273+
SWSS_LOG_ERROR("Failed to locate neigbor entry rid:%llx ip:%s\n", rif_id, ip_address.to_string().c_str());
270274
return true;
271275
}
272276

273-
SWSS_LOG_ERROR("Failed to remove neighbor entry rid:%llx ip:%s\n", p.m_rif_id, ip_address.to_string().c_str());
277+
SWSS_LOG_ERROR("Failed to remove neighbor entry rid:%llx ip:%s\n", rif_id, ip_address.to_string().c_str());
274278
return false;
275279
}
276280

277281
m_syncdNeighbors.erase(neighborEntry);
278-
removeNextHop(ip_address);
282+
m_intfsOrch->decreaseRouterIntfsRefCount(alias);
283+
removeNextHop(ip_address, alias);
279284

280285
return true;
281286
}

orchagent/neighorch.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "orch.h"
55
#include "portsorch.h"
6+
#include "intfsorch.h"
67

78
#include "ipaddress.h"
89

@@ -31,9 +32,7 @@ typedef map<IpAddress, NextHopEntry> NextHopTable;
3132
class NeighOrch : public Orch
3233
{
3334
public:
34-
NeighOrch(DBConnector *db, string tableName, PortsOrch *portsOrch) :
35-
Orch(db, tableName),
36-
m_portsOrch(portsOrch) {};
35+
NeighOrch(DBConnector *db, string tableName, IntfsOrch *intfsOrch);
3736

3837
bool hasNextHop(IpAddress);
3938

@@ -44,13 +43,13 @@ class NeighOrch : public Orch
4443
void decreaseNextHopRefCount(IpAddress);
4544

4645
private:
47-
PortsOrch *m_portsOrch;
46+
IntfsOrch *m_intfsOrch;
4847

4948
NeighborTable m_syncdNeighbors;
5049
NextHopTable m_syncdNextHops;
5150

52-
bool addNextHop(IpAddress, Port);
53-
bool removeNextHop(IpAddress);
51+
bool addNextHop(IpAddress, string);
52+
bool removeNextHop(IpAddress, string);
5453

5554
bool addNeighbor(NeighborEntry, MacAddress);
5655
bool removeNeighbor(NeighborEntry);

orchagent/orch.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include "orch.h"
2+
#include "portsorch.h"
23
#include "logger.h"
34
#include <iostream>
45
using namespace swss;
56

7+
extern PortsOrch *gPortsOrch;
8+
69
Orch::Orch(DBConnector *db, string tableName) :
710
m_db(db)
811
{
@@ -111,6 +114,9 @@ bool Orch::execute(string tableName)
111114

112115
void Orch::doTask()
113116
{
117+
if (!gPortsOrch->isInitDone())
118+
return;
119+
114120
for(auto &it : m_consumerMap)
115121
{
116122
if (!it.second.m_toSync.empty())

0 commit comments

Comments
 (0)