Skip to content

Commit 419ab1b

Browse files
authored
[macsecmgr]: Fix cleanup macsec objs if container stop (sonic-net#2376)
What I did Introduce SIGTERM handlers in macsecmgrd. When the macsecmgrd exit with signal SIGTERM, all existing MACsec objs will clean up. Adjust the cleanup order to follow the wpa_supplicant did (Remove Ingress objs firstly and Egress objs then). Why I did it When “docker stop”, macsecmgrd need also to cleanup all exiting MACsec objs. How I verified it Try "sudo config feature state macsec disabled`, the MACsec objs were removed. Signed-off-by: Ze Gan <[email protected]>
1 parent 9045995 commit 419ab1b

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

cfgmgr/macsecmgrd.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <unistd.h>
2+
#include <signal.h>
23
#include <vector>
34
#include <sstream>
45
#include <fstream>
@@ -45,6 +46,20 @@ string gResponsePublisherRecordFile;
4546
/* Global database mutex */
4647
mutex gDbMutex;
4748

49+
static bool received_sigterm = false;
50+
static struct sigaction old_sigaction;
51+
52+
static void sig_handler(int signo)
53+
{
54+
SWSS_LOG_ENTER();
55+
56+
if (old_sigaction.sa_handler != SIG_IGN && old_sigaction.sa_handler != SIG_DFL) {
57+
old_sigaction.sa_handler(signo);
58+
}
59+
60+
received_sigterm = true;
61+
return;
62+
}
4863

4964
int main(int argc, char **argv)
5065
{
@@ -54,6 +69,15 @@ int main(int argc, char **argv)
5469
Logger::linkToDbNative("macsecmgrd");
5570
SWSS_LOG_NOTICE("--- Starting macsecmgrd ---");
5671

72+
/* Register the signal handler for SIGTERM */
73+
struct sigaction sigact = {};
74+
sigact.sa_handler = sig_handler;
75+
if (sigaction(SIGTERM, &sigact, &old_sigaction))
76+
{
77+
SWSS_LOG_ERROR("failed to setup SIGTERM action handler");
78+
exit(EXIT_FAILURE);
79+
}
80+
5781
swss::DBConnector cfgDb("CONFIG_DB", 0);
5882
swss::DBConnector stateDb("STATE_DB", 0);
5983

@@ -73,7 +97,7 @@ int main(int argc, char **argv)
7397
}
7498

7599
SWSS_LOG_NOTICE("starting main loop");
76-
while (true)
100+
while (!received_sigterm)
77101
{
78102
Selectable *sel;
79103
int ret;

orchagent/macsecorch.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1482,22 +1482,22 @@ bool MACsecOrch::deleteMACsecPort(
14821482

14831483
bool result = true;
14841484

1485-
auto sc = macsec_port.m_egress_scs.begin();
1486-
while (sc != macsec_port.m_egress_scs.end())
1485+
auto sc = macsec_port.m_ingress_scs.begin();
1486+
while (sc != macsec_port.m_ingress_scs.end())
14871487
{
14881488
const std::string port_sci = swss::join(':', port_name, MACsecSCI(sc->first));
14891489
sc ++;
1490-
if (deleteMACsecSC(port_sci, SAI_MACSEC_DIRECTION_EGRESS) != task_success)
1490+
if (deleteMACsecSC(port_sci, SAI_MACSEC_DIRECTION_INGRESS) != task_success)
14911491
{
14921492
result &= false;
14931493
}
14941494
}
1495-
sc = macsec_port.m_ingress_scs.begin();
1496-
while (sc != macsec_port.m_ingress_scs.end())
1495+
sc = macsec_port.m_egress_scs.begin();
1496+
while (sc != macsec_port.m_egress_scs.end())
14971497
{
14981498
const std::string port_sci = swss::join(':', port_name, MACsecSCI(sc->first));
14991499
sc ++;
1500-
if (deleteMACsecSC(port_sci, SAI_MACSEC_DIRECTION_INGRESS) != task_success)
1500+
if (deleteMACsecSC(port_sci, SAI_MACSEC_DIRECTION_EGRESS) != task_success)
15011501
{
15021502
result &= false;
15031503
}

0 commit comments

Comments
 (0)