Skip to content

Commit 5ddea37

Browse files
[NAT]: Update NAT conntrack entries from natmgr instead of natorch (sonic-net#1274)
* Update NAT conntrack entries from natmgr instead of natorch Signed-off-by: Akhilesh Samineni <akhilesh.samineni@broadcom.com>
1 parent 94c622f commit 5ddea37

File tree

8 files changed

+995
-536
lines changed

8 files changed

+995
-536
lines changed

cfgmgr/natmgr.cpp

+659-52
Large diffs are not rendered by default.

cfgmgr/natmgr.h

+38-10
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
#ifndef __NATMGR__
1818
#define __NATMGR__
1919

20+
#include "selectabletimer.h"
2021
#include "dbconnector.h"
2122
#include "producerstatetable.h"
2223
#include "orch.h"
2324
#include "notificationproducer.h"
25+
#include "timer.h"
2426
#include <unistd.h>
2527
#include <set>
2628
#include <map>
@@ -60,6 +62,7 @@ namespace swss {
6062
#define NAT_TIMEOUT_MIN 300
6163
#define NAT_TIMEOUT_MAX 432000
6264
#define NAT_TIMEOUT_DEFAULT 600
65+
#define NAT_TIMEOUT_LOW 0
6366
#define NAT_TCP_TIMEOUT "nat_tcp_timeout"
6467
#define NAT_TCP_TIMEOUT_MIN 300
6568
#define NAT_TCP_TIMEOUT_MAX 432000
@@ -119,6 +122,9 @@ namespace swss {
119122
#define IS_RESERVED_ADDR(ipaddr) (ipaddr >= 0xF0000000)
120123
#define IS_ZERO_ADDR(ipaddr) (ipaddr == 0)
121124
#define IS_BROADCAST_ADDR(ipaddr) (ipaddr == 0xFFFFFFFF)
125+
#define NAT_ENTRY_REFRESH_PERIOD 86400 // 1 day
126+
#define REDIRECT_TO_DEV_NULL " &> /dev/null"
127+
#define FLUSH " -F"
122128

123129
const char ip_address_delimiter = '/';
124130

@@ -234,13 +240,15 @@ class NatMgr : public Orch
234240
void cleanupPoolIpTable();
235241
void cleanupMangleIpTables();
236242
bool isPortInitDone(DBConnector *app_db);
237-
243+
void timeoutNotifications(std::string op, std::string data);
244+
void flushNotifications(std::string op, std::string data);
245+
238246
private:
239247
/* Declare APPL_DB, CFG_DB and STATE_DB tables */
240248
ProducerStateTable m_appNatTableProducer, m_appNaptTableProducer, m_appNatGlobalTableProducer;
241249
ProducerStateTable m_appTwiceNatTableProducer, m_appTwiceNaptTableProducer;
242250
Table m_statePortTable, m_stateLagTable, m_stateVlanTable, m_stateInterfaceTable, m_appNaptPoolIpTable;
243-
std::shared_ptr<swss::NotificationProducer> flushNotifier;
251+
Table m_stateWarmRestartEnableTable, m_stateWarmRestartTable;
244252

245253
/* Declare containers to store NAT Info */
246254
int m_natTimeout;
@@ -256,9 +264,12 @@ class NatMgr : public Orch
256264
natZoneInterface_map_t m_natZoneInterfaceInfo;
257265
natAclTable_map_t m_natAclTableInfo;
258266
natAclRule_map_t m_natAclRuleInfo;
267+
SelectableTimer *m_natRefreshTimer;
259268

260269
/* Declare doTask related fucntions */
261270
void doTask(Consumer &consumer);
271+
void doTask(SelectableTimer &timer);
272+
void doNatRefreshTimerTask();
262273
void doStaticNatTask(Consumer &consumer);
263274
void doStaticNaptTask(Consumer &consumer);
264275
void doNatPoolTask(Consumer &consumer);
@@ -271,15 +282,26 @@ class NatMgr : public Orch
271282
/* Declare all NAT functionality member functions*/
272283
void enableNatFeature(void);
273284
void disableNatFeature(void);
274-
void addConntrackSingleNatEntry(const std::string &key);
275-
void addConntrackSingleNaptEntry(const std::string &key);
276-
void deleteConntrackSingleNatEntry(const std::string &key);
277-
void deleteConntrackSingleNaptEntry(const std::string &key);
278-
void addConntrackTwiceNatEntry(const std::string &snatKey, const std::string &dnatKey);
279-
void addConntrackTwiceNaptEntry(const std::string &snatKey, const std::string &dnatKey);
280-
void deleteConntrackTwiceNatEntry(const std::string &snatKey, const std::string &dnatKey);
281-
void deleteConntrackTwiceNaptEntry(const std::string &snatKey, const std::string &dnatKey);
285+
bool warmBootingInProgress(void);
286+
void flushAllNatEntries(void);
287+
void addAllStaticConntrackEntries(void);
288+
void addConntrackStaticSingleNatEntry(const std::string &key);
289+
void addConntrackStaticSingleNaptEntry(const std::string &key);
290+
void updateConntrackStaticSingleNatEntry(const std::string &key);
291+
void updateConntrackStaticSingleNaptEntry(const std::string &key);
292+
void deleteConntrackStaticSingleNatEntry(const std::string &key);
293+
void deleteConntrackStaticSingleNaptEntry(const std::string &key);
294+
void addConntrackStaticTwiceNatEntry(const std::string &snatKey, const std::string &dnatKey);
295+
void addConntrackStaticTwiceNaptEntry(const std::string &snatKey, const std::string &dnatKey);
296+
void updateConntrackStaticTwiceNatEntry(const std::string &snatKey, const std::string &dnatKey);
297+
void updateConntrackStaticTwiceNaptEntry(const std::string &snatKey, const std::string &dnatKey);
298+
void deleteConntrackStaticTwiceNatEntry(const std::string &snatKey, const std::string &dnatKey);
299+
void deleteConntrackStaticTwiceNaptEntry(const std::string &snatKey, const std::string &dnatKey);
282300
void deleteConntrackDynamicEntries(const std::string &ip_range);
301+
void updateDynamicSingleNatConnTrackTimeout(std::string key, int timeout);
302+
void updateDynamicSingleNaptConnTrackTimeout(std::string key, int timeout);
303+
void updateDynamicTwiceNatConnTrackTimeout(std::string key, int timeout);
304+
void updateDynamicTwiceNaptConnTrackTimeout(std::string key, int timeout);
283305
void addStaticNatEntry(const std::string &key);
284306
void addStaticNaptEntry(const std::string &key);
285307
void addStaticSingleNatEntry(const std::string &key);
@@ -308,6 +330,12 @@ class NatMgr : public Orch
308330
void addStaticNaptIptables(const std::string port);
309331
void removeStaticNatIptables(const std::string port);
310332
void removeStaticNaptIptables(const std::string port);
333+
void setStaticNatConntrackEntries(std::string mode);
334+
void setStaticSingleNatConntrackEntry(const std::string &key, std::string &mode);
335+
void setStaticTwiceNatConntrackEntry(const std::string &key, std::string &mode);
336+
void setStaticNaptConntrackEntries(std::string mode);
337+
void setStaticSingleNaptConntrackEntry(const std::string &key, std::string &mode);
338+
void setStaticTwiceNaptConntrackEntry(const std::string &key, std::string &mode);
311339
void addDynamicNatRule(const std::string &key);
312340
void removeDynamicNatRule(const std::string &key);
313341
void addDynamicNatRuleByAcl(const std::string &key, bool isRuleId = false);

cfgmgr/natmgrd.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ string gRecordFile;
5555
mutex gDbMutex;
5656
NatMgr *natmgr = NULL;
5757

58+
NotificationConsumer *timeoutNotificationsConsumer = NULL;
59+
NotificationConsumer *flushNotificationsConsumer = NULL;
60+
5861
std::shared_ptr<swss::NotificationProducer> cleanupNotifier;
5962

6063
void sigterm_handler(int signo)
@@ -142,6 +145,12 @@ int main(int argc, char **argv)
142145
s.addSelectables(o->getSelectables());
143146
}
144147

148+
timeoutNotificationsConsumer = new NotificationConsumer(&appDb, "SETTIMEOUTNAT");
149+
s.addSelectable(timeoutNotificationsConsumer);
150+
151+
flushNotificationsConsumer = new NotificationConsumer(&appDb, "FLUSHNATENTRIES");
152+
s.addSelectable(flushNotificationsConsumer);
153+
145154
SWSS_LOG_NOTICE("starting main loop");
146155
while (true)
147156
{
@@ -154,6 +163,29 @@ int main(int argc, char **argv)
154163
SWSS_LOG_NOTICE("Error: %s!", strerror(errno));
155164
continue;
156165
}
166+
167+
if (sel == timeoutNotificationsConsumer)
168+
{
169+
std::string op;
170+
std::string data;
171+
std::vector<swss::FieldValueTuple> values;
172+
173+
timeoutNotificationsConsumer->pop(op, data, values);
174+
natmgr->timeoutNotifications(op, data);
175+
continue;
176+
}
177+
178+
if (sel == flushNotificationsConsumer)
179+
{
180+
std::string op;
181+
std::string data;
182+
std::vector<swss::FieldValueTuple> values;
183+
184+
flushNotificationsConsumer->pop(op, data, values);
185+
natmgr->flushNotifications(op, data);
186+
continue;
187+
}
188+
157189
if (ret == Select::TIMEOUT)
158190
{
159191
natmgr->doTask();

natsyncd/natsync.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ NatSync::NatSync(RedisPipeline *pipelineAppDB, DBConnector *appDb, DBConnector *
5959
m_AppRestartAssist->registerAppTable(APP_NAT_TWICE_TABLE_NAME, &m_natTwiceTable);
6060
m_AppRestartAssist->registerAppTable(APP_NAPT_TWICE_TABLE_NAME, &m_naptTwiceTable);
6161
}
62+
63+
setTimeoutNotifier = std::make_shared<swss::NotificationProducer>(appDb, "SETTIMEOUTNAT");
6264
}
6365

6466
NatSync::~NatSync()
@@ -490,6 +492,7 @@ int NatSync::addNatEntry(struct nfnl_ct *ct, struct naptEntry &entry, bool addFl
490492
{
491493
m_naptTwiceTable.set(key, fvVector);
492494
SWSS_LOG_NOTICE("Twice NAPT entry with key %s added to APP_DB", key.c_str());
495+
setTimeoutNotifier->send("SET-TWICE-NAPT", key, fvVector);
493496
m_naptTwiceTable.set(reverseEntryKey, reverseFvVector);
494497
SWSS_LOG_NOTICE("Twice NAPT entry with reverse key %s added to APP_DB", reverseEntryKey.c_str());
495498
}
@@ -530,6 +533,7 @@ int NatSync::addNatEntry(struct nfnl_ct *ct, struct naptEntry &entry, bool addFl
530533
{
531534
m_natTwiceTable.set(key, fvVector);
532535
SWSS_LOG_NOTICE("Twice NAT entry with key %s added to APP_DB", key.c_str());
536+
setTimeoutNotifier->send("SET-TWICE-NAT", key, fvVector);
533537
m_natTwiceTable.set(reverseEntryKey, reverseFvVector);
534538
SWSS_LOG_NOTICE("Twice NAT entry with reverse key %s added to APP_DB", reverseEntryKey.c_str());
535539
}
@@ -682,6 +686,7 @@ int NatSync::addNatEntry(struct nfnl_ct *ct, struct naptEntry &entry, bool addFl
682686
{
683687
m_naptTable.set(key, fvVector);
684688
SWSS_LOG_NOTICE("SNAPT entry with key %s added to APP_DB", key.c_str());
689+
setTimeoutNotifier->send("SET-SINGLE-NAPT", key, fvVector);
685690
m_naptTable.set(reverseEntryKey, reverseFvVector);
686691
SWSS_LOG_NOTICE("Implicit DNAPT entry with key %s added to APP_DB", reverseEntryKey.c_str());
687692
}
@@ -786,6 +791,7 @@ int NatSync::addNatEntry(struct nfnl_ct *ct, struct naptEntry &entry, bool addFl
786791
{
787792
m_natTable.set(key, fvVector);
788793
SWSS_LOG_NOTICE("SNAT entry with key %s added to APP_DB", key.c_str());
794+
setTimeoutNotifier->send("SET-SINGLE-NAT", key, fvVector);
789795
m_natTable.set(reverseEntryKey, reverseFvVector);
790796
SWSS_LOG_NOTICE("Implicit DNAT entry with key %s added to APP_DB", reverseEntryKey.c_str());
791797
}
@@ -890,6 +896,7 @@ int NatSync::addNatEntry(struct nfnl_ct *ct, struct naptEntry &entry, bool addFl
890896
{
891897
m_naptTable.set(key, fvVector);
892898
SWSS_LOG_NOTICE("DNAPT entry with key %s added to APP_DB", key.c_str());
899+
setTimeoutNotifier->send("SET-SINGLE-NAPT", key, fvVector);
893900
m_naptTable.set(reverseEntryKey, reverseFvVector);
894901
SWSS_LOG_NOTICE("Implicit SNAPT entry with key %s added to APP_DB", reverseEntryKey.c_str());
895902
}
@@ -963,6 +970,7 @@ int NatSync::addNatEntry(struct nfnl_ct *ct, struct naptEntry &entry, bool addFl
963970
{
964971
m_natTable.set(key, fvVector);
965972
SWSS_LOG_NOTICE("DNAT entry with key %s added to APP_DB", key.c_str());
973+
setTimeoutNotifier->send("SET-SINGLE-NAT", key, fvVector);
966974
m_natTable.set(reverseEntryKey, reverseFvVector);
967975
SWSS_LOG_NOTICE("Implicit SNAT entry with key %s added to APP_DB", reverseEntryKey.c_str());
968976
}

natsyncd/natsync.h

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "dbconnector.h"
2121
#include "producerstatetable.h"
22+
#include "notificationproducer.h"
2223
#include "netmsg.h"
2324
#include "warmRestartAssist.h"
2425
#include "ipaddress.h"
@@ -65,6 +66,8 @@ class NatSync : public NetMsg
6566
bool matchingDnaptEntryExists(const naptEntry &entry);
6667
int addNatEntry(struct nfnl_ct *ct, struct naptEntry &entry, bool addFlag);
6768

69+
std::shared_ptr<swss::NotificationProducer> setTimeoutNotifier;
70+
6871
ProducerStateTable m_natTable;
6972
ProducerStateTable m_naptTable;
7073
ProducerStateTable m_natTwiceTable;

0 commit comments

Comments
 (0)