Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
bocon13 committed Nov 18, 2021
2 parents b80112d + 74264d9 commit 597d092
Show file tree
Hide file tree
Showing 8 changed files with 515 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .azure-pipelines/test-docker-sonic-vs-template.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
- name: timeout
type: number
default: 180
default: 240

- name: log_artifact_name
type: string
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ stages:
sudo apt-get install -y python3-pip
sudo pip3 install pytest
sudo apt-get install -y python
sudo apt-get install cmake libgtest-dev
sudo apt-get install cmake libgtest-dev libgmock-dev
cd /usr/src/gtest && sudo cmake . && sudo make
ARCH=$(dpkg --print-architecture)
set -x
Expand Down
1 change: 1 addition & 0 deletions common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ libswsscommon_la_SOURCES = \
pubsub.cpp \
tokenize.cpp \
exec.cpp \
saiaclschema.cpp \
subscriberstatetable.cpp \
timestamp.cpp \
warm_restart.cpp \
Expand Down
332 changes: 332 additions & 0 deletions common/saiaclschema.cpp

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions common/saiaclschema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef SONIC_SWSS_COMMON_COMMON_SAIACLSCHEMA_H_
#define SONIC_SWSS_COMMON_COMMON_SAIACLSCHEMA_H_

#include <set>
#include <string>
#include <unordered_map>

// This file describes the AppDB ACL schema values between the P4RT app and
// OrchAgent and the capabilities of OrchAgent.

namespace swss
{
namespace acl
{

// Enumeration of ACL stages.
enum class Stage
{
kLookup,
kIngress,
kEgress
};

// Enumeration of expected data formats for match fields and action parameters.
enum class Format
{
kNone, // No value is expected (e.g. DROP action).
kHexString, // Numerical value represented as hexadecimal. "0x1234"
kMac, // MAC address. "11:22:33:44:55:66"
kIPv4, // IPv4 address in canonical format. "128.0.0.1"
kIPv6, // IPv6 address in canonical format. "2002:1:128::"
kString // String value. "Port0"
};

// Schema information for deciphering match fields.
struct MatchFieldSchema
{
std::set<Stage> stages; // Stages where this match field can be used.
Format format; // Expected format for this match field in AppDB.
int bitwidth; // Bitwidth of the match field (if not string).
};

// Schema information for deciphering actions and action parameters.
struct ActionSchema
{
Format format; // Expected format for the action parameter in AppDB.
int bitwidth; // Bitwidth of the action parameter (if not string & not none).
};

// Returns the SAI stage enum matching the AppDB name.
// "LOOKUP" --> SAI_ACL_STAGE_LOOKUP (Not yet standard)
// "INGRESS" --> SAI_ACL_STAGE_INGRESS
// "EGRESS" --> SAI_ACL_STAGE_EGRESS
//
// Throws std::invalid_argument if the name does not match a stage.
Stage StageFromName(const std::string &name);

// Returns the AppDB name for a SAI stage enum. See StageFromName.
const std::string &StageName(Stage stage);

// Returns the Format enum matching the AppDB format name.
// "HEX_STRING" --> kHexString
// "MAC" --> kMac
// "IP" --> kIP
// "IPV6" --> kIPv6
// "IPV4" --> kIPv4
// "STRING" --> kString
//
// Throws std::invalid_argument if the name does not match a Format.
Format FormatFromName(const std::string &name);

// Returns the AppDB format name for a Format enum.
const std::string &FormatName(Format format);

// Returns the schema for a match field.
//
// Throws std::invalid_argument for unknown match fields and match fields
// without schemas.
const MatchFieldSchema &MatchFieldSchemaByName(const std::string &match_field_name);

// Returns the schema for a action.
//
// Throws std::invalid_argument for unknown actions and actions without schemas.
const ActionSchema &ActionSchemaByName(const std::string &action_name);

} // namespace acl
} // namespace swss

#endif // SONIC_SWSS_COMMON_COMMON_SAIACLSCHEMA_H_
2 changes: 2 additions & 0 deletions common/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ namespace swss {

#define STATE_BFD_SESSION_TABLE_NAME "BFD_SESSION_TABLE"
#define STATE_ROUTE_TABLE_NAME "ROUTE_TABLE"
#define STATE_VNET_RT_TUNNEL_TABLE_NAME "VNET_ROUTE_TUNNEL_TABLE"
#define STATE_ADVERTISE_NETWORK_TABLE_NAME "ADVERTISE_NETWORK_TABLE"

/***** MISC *****/

Expand Down
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DBGFLAGS = -g -DNDEBUG
endif

CFLAGS_GTEST =
LDADD_GTEST = -L/usr/src/gtest -lgtest -lgtest_main
LDADD_GTEST = -L/usr/src/gtest -lgtest -lgtest_main -lgmock -lgmock_main

tests_SOURCES = redis_ut.cpp \
redis_piped_ut.cpp \
Expand All @@ -33,6 +33,7 @@ tests_SOURCES = redis_ut.cpp \
stringutility_ut.cpp \
redisutility_ut.cpp \
boolean_ut.cpp \
saiaclschema_ut.cpp \
main.cpp

tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(LIBNL_CFLAGS)
Expand Down
87 changes: 87 additions & 0 deletions tests/saiaclschema_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <stdexcept>

#include "common/saiaclschema.h"

namespace swss
{
namespace acl
{
namespace
{

using ::testing::AllOf;
using ::testing::Field;
using ::testing::UnorderedElementsAre;

class FormatTest : public testing::TestWithParam<Format>
{
};

TEST_P(FormatTest, StringConversionIsConsistent)
{
EXPECT_EQ(FormatFromName(FormatName(GetParam())), GetParam());
}

INSTANTIATE_TEST_CASE_P(SaiAclSchemaTest, FormatTest,
testing::Values(Format::kNone, Format::kHexString, Format::kMac, Format::kIPv4, Format::kIPv6,
Format::kString),
[](const testing::TestParamInfo<FormatTest::ParamType> &param_info) {
return FormatName(param_info.param);
});

class StageTest : public testing::TestWithParam<Stage>
{
};

TEST_P(StageTest, StringConversionIsConsistent)
{
EXPECT_EQ(StageFromName(StageName(GetParam())), GetParam());
}

INSTANTIATE_TEST_CASE_P(SaiAclSchemaTest, StageTest, testing::Values(Stage::kLookup, Stage::kIngress, Stage::kEgress),
[](const testing::TestParamInfo<StageTest::ParamType> &param_info) {
return StageName(param_info.param);
});

TEST(SaiAclSchemaTest, MatchFieldSchemaByNameSucceeds)
{
EXPECT_THAT(
MatchFieldSchemaByName("SAI_ACL_TABLE_ATTR_FIELD_SRC_IPV6"),
AllOf(Field(&MatchFieldSchema::stages, UnorderedElementsAre(Stage::kLookup, Stage::kIngress, Stage::kEgress)),
Field(&MatchFieldSchema::format, Format::kIPv6), Field(&MatchFieldSchema::bitwidth, 128)));
}

TEST(SaiAclSchemaTest, ActionSchemaByNameSucceeds)
{
EXPECT_THAT(ActionSchemaByName("SAI_ACL_ENTRY_ATTR_ACTION_SET_INNER_VLAN_ID"),
AllOf(Field(&ActionSchema::format, Format::kHexString), Field(&ActionSchema::bitwidth, 12)));
}

// Invalid Lookup Tests

TEST(SaiAclSchemaTest, InvalidFormatNameThrowsException)
{
EXPECT_THROW(FormatFromName("Foo"), std::invalid_argument);
}

TEST(SaiAclSchemaTest, InvalidStageNameThrowsException)
{
EXPECT_THROW(StageFromName("Foo"), std::invalid_argument);
}

TEST(SaiAclSchemaTest, InvalidMatchFieldNameThrowsException)
{
EXPECT_THROW(MatchFieldSchemaByName("Foo"), std::invalid_argument);
}

TEST(SaiAclSchemaTest, InvalidActionNameThrowsException)
{
EXPECT_THROW(ActionSchemaByName("Foo"), std::invalid_argument);
}

} // namespace
} // namespace acl
} // namespace swss

0 comments on commit 597d092

Please sign in to comment.