Skip to content

Commit

Permalink
[macsec] Refactor the logic of macsec name map (#2348)
Browse files Browse the repository at this point in the history
* Add/remove macsec name map w/o gearbox correctly
* Add macsec counter unit test
  • Loading branch information
jimmyzhai authored Jun 24, 2022
1 parent f88f992 commit 1ed0b4b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
17 changes: 2 additions & 15 deletions orchagent/macsecorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2340,10 +2340,6 @@ void MACsecOrch::installCounter(
sai_object_id_t obj_id,
const std::vector<std::string> &stats)
{
FieldValueTuple tuple(obj_name, sai_serialize_object_id(obj_id));
vector<FieldValueTuple> fields;
fields.push_back(tuple);

std::unordered_set<std::string> counter_stats;
for (const auto &stat : stats)
{
Expand All @@ -2353,12 +2349,11 @@ void MACsecOrch::installCounter(
{
case CounterType::MACSEC_SA_ATTR:
MACsecSaAttrStatManager(ctx).setCounterIdList(obj_id, counter_type, counter_stats);
MACsecCountersMap(ctx).set("", fields);
break;

case CounterType::MACSEC_SA:
MACsecSaStatManager(ctx).setCounterIdList(obj_id, counter_type, counter_stats);
MACsecCountersMap(ctx).set("", fields);
MACsecCountersMap(ctx).hset("", obj_name, sai_serialize_object_id(obj_id));
break;

case CounterType::MACSEC_FLOW:
Expand All @@ -2383,19 +2378,11 @@ void MACsecOrch::uninstallCounter(
{
case CounterType::MACSEC_SA_ATTR:
MACsecSaAttrStatManager(ctx).clearCounterIdList(obj_id);
m_counter_db.hdel(COUNTERS_MACSEC_NAME_MAP, obj_name);
break;

case CounterType::MACSEC_SA:
MACsecSaStatManager(ctx).clearCounterIdList(obj_id);
if (direction == SAI_MACSEC_DIRECTION_EGRESS)
{
m_counter_db.hdel(COUNTERS_MACSEC_SA_TX_NAME_MAP, obj_name);
}
else
{
m_counter_db.hdel(COUNTERS_MACSEC_SA_RX_NAME_MAP, obj_name);
}
MACsecCountersMap(ctx).hdel("", obj_name);
break;

case CounterType::MACSEC_FLOW:
Expand Down
53 changes: 53 additions & 0 deletions tests/test_macsec.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from swsscommon import swsscommon
from swsscommon.swsscommon import CounterTable, MacsecCounter
import conftest

import functools
import typing
import re
import time


def to_string(value):
Expand Down Expand Up @@ -389,6 +391,21 @@ def get_macsec_sa(
print(info.group(0))
return info.group(0)

@macsec_sa()
def get_macsec_xpn_counter(
self,
sai: str) -> int:
counter_table = CounterTable(self.dvs.get_counters_db().db_connection)
for i in range(3):
r, value = counter_table.hget(
MacsecCounter(),
sai,
"SAI_MACSEC_SA_ATTR_CURRENT_XPN")
if r: return int(value)
time.sleep(1) # wait a moment for polling counter

return None


class TestMACsec(object):
def init_macsec(
Expand Down Expand Up @@ -658,6 +675,18 @@ def test_macsec_term_orch(self, dvs: conftest.DockerVirtualSwitch, testlog):
peer_mac_address,
macsec_port_identifier,
0))
assert(
inspector.get_macsec_xpn_counter(
port_name,
local_mac_address,
macsec_port_identifier,
0) == packet_number)
assert(
inspector.get_macsec_xpn_counter(
port_name,
peer_mac_address,
macsec_port_identifier,
0) == packet_number)
self.rekey_macsec(
wpa,
port_name,
Expand All @@ -683,6 +712,18 @@ def test_macsec_term_orch(self, dvs: conftest.DockerVirtualSwitch, testlog):
peer_mac_address,
macsec_port_identifier,
1))
assert(
inspector.get_macsec_xpn_counter(
port_name,
local_mac_address,
macsec_port_identifier,
1) == packet_number)
assert(
inspector.get_macsec_xpn_counter(
port_name,
peer_mac_address,
macsec_port_identifier,
1) == packet_number)
assert(
not inspector.get_macsec_sa(
macsec_port,
Expand All @@ -695,6 +736,18 @@ def test_macsec_term_orch(self, dvs: conftest.DockerVirtualSwitch, testlog):
peer_mac_address,
macsec_port_identifier,
0))
assert(
not inspector.get_macsec_xpn_counter(
port_name,
local_mac_address,
macsec_port_identifier,
0) == packet_number)
assert(
not inspector.get_macsec_xpn_counter(
port_name,
peer_mac_address,
macsec_port_identifier,
0) == packet_number)
# Exit MACsec port
self.deinit_macsec(
wpa,
Expand Down

0 comments on commit 1ed0b4b

Please sign in to comment.