forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportsyncd.cpp
216 lines (189 loc) · 6.27 KB
/
portsyncd.cpp
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
#include <getopt.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <sys/stat.h>
#include "dbconnector.h"
#include "select.h"
#include "netdispatcher.h"
#include "netlink.h"
#include "producerstatetable.h"
#include "portsyncd/linksync.h"
#include "subscriberstatetable.h"
#include "exec.h"
#include "warm_restart.h"
using namespace std;
using namespace swss;
#define DEFAULT_SELECT_TIMEOUT 1000 /* ms */
/*
* This g_portSet contains all the front panel ports that the corresponding
* host interfaces needed to be created. When this LinkSync class is
* initialized, we check the database to see if some of the ports' host
* interfaces are already created and remove them from this set. We will
* remove the rest of the ports in the set when receiving the first netlink
* message indicating that the host interfaces are created. After the set
* is empty, we send out the signal PortInitDone. g_init is used to limit the
* command to be run only once.
*/
set<string> g_portSet;
bool g_init = false;
string g_switchType;
void usage()
{
cout << "Usage: portsyncd" << endl;
cout << " port lane mapping is from configDB" << endl;
cout << " this program will exit if configDB does not contain that info" << endl;
}
void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm);
int main(int argc, char **argv)
{
try
{
Logger::linkToDbNative("portsyncd");
int opt;
while ((opt = getopt(argc, argv, "v:h")) != -1 )
{
switch (opt)
{
case 'h':
usage();
return 1;
default: /* '?' */
usage();
return EXIT_FAILURE;
}
}
DBConnector cfgDb("CONFIG_DB", 0);
DBConnector appl_db("APPL_DB", 0);
DBConnector state_db("STATE_DB", 0);
ProducerStateTable p(&appl_db, APP_PORT_TABLE_NAME);
Table cfgDeviceMetaDataTable(&cfgDb, CFG_DEVICE_METADATA_TABLE_NAME);
if (!cfgDeviceMetaDataTable.hget("localhost", "switch_type", g_switchType))
{
//Switch type is not configured. Consider it default = "switch" (regular switch)
g_switchType = "switch";
}
WarmStart::initialize("portsyncd", "swss");
WarmStart::checkWarmStart("portsyncd", "swss");
const bool warm = WarmStart::isWarmStart();
NetLink netlink;
Select s;
netlink.registerGroup(RTNLGRP_LINK);
netlink.dumpRequest(RTM_GETLINK);
cout << "Listen to link messages..." << endl;
handlePortConfigFromConfigDB(p, cfgDb, warm);
LinkSync sync(&appl_db, &state_db);
NetDispatcher::getInstance().registerMessageHandler(RTM_NEWLINK, &sync);
NetDispatcher::getInstance().registerMessageHandler(RTM_DELLINK, &sync);
s.addSelectable(&netlink);
while (true)
{
Selectable *temps;
int ret;
ret = s.select(&temps, DEFAULT_SELECT_TIMEOUT);
if (ret == Select::ERROR)
{
cerr << "Error had been returned in select" << endl;
continue;
}
else if (ret == Select::TIMEOUT)
{
continue;
}
else if (ret != Select::OBJECT)
{
SWSS_LOG_ERROR("Unknown return value from Select %d", ret);
continue;
}
if (temps == static_cast<Selectable*>(&netlink))
{
/* on netlink message, check if PortInitDone should be sent out */
if (!g_init && g_portSet.empty())
{
/*
* After finishing reading port configuration file and
* creating all host interfaces, this daemon shall send
* out a signal to orchagent indicating port initialization
* procedure is done and other application could start
* syncing.
*/
FieldValueTuple finish_notice("lanes", "0");
vector<FieldValueTuple> attrs = { finish_notice };
p.set("PortInitDone", attrs);
SWSS_LOG_NOTICE("PortInitDone");
g_init = true;
}
}
else
{
SWSS_LOG_ERROR("Unknown object returned by select");
continue;
}
}
}
catch (const swss::RedisError& e)
{
cerr << "Exception \"" << e.what() << "\" was thrown in daemon" << endl;
return EXIT_FAILURE;
}
catch (const std::out_of_range& e)
{
cerr << "Exception \"" << e.what() << "\" was thrown in daemon" << endl;
return EXIT_FAILURE;
}
catch (const std::exception& e)
{
cerr << "Exception \"" << e.what() << "\" was thrown in daemon" << endl;
return EXIT_FAILURE;
}
catch (...)
{
cerr << "Exception was thrown in daemon" << endl;
return EXIT_FAILURE;
}
return 1;
}
static void notifyPortConfigDone(ProducerStateTable &p)
{
/* Notify that all ports added */
FieldValueTuple finish_notice("count", to_string(g_portSet.size()));
vector<FieldValueTuple> attrs = { finish_notice };
p.set("PortConfigDone", attrs);
}
void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm)
{
SWSS_LOG_ENTER();
SWSS_LOG_NOTICE("Getting port configuration from ConfigDB...");
Table table(&cfgDb, CFG_PORT_TABLE_NAME);
std::vector<FieldValueTuple> ovalues;
std::vector<string> keys;
table.getKeys(keys);
if (keys.empty())
{
SWSS_LOG_NOTICE("ConfigDB does not have port information, "
"however ports can be added later on, continuing...");
}
for ( auto &k : keys )
{
table.get(k, ovalues);
vector<FieldValueTuple> attrs;
for ( auto &v : ovalues )
{
FieldValueTuple attr(v.first, v.second);
attrs.push_back(attr);
}
if (!warm)
{
p.set(k, attrs);
}
g_portSet.insert(k);
}
if (!warm)
{
notifyPortConfigDone(p);
}
}