Skip to content

Commit

Permalink
Define Es2kPrepFdbTunnelTableTest (#41)
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Foster <justffoulkes@gmail.com>
  • Loading branch information
ffoulkes authored Feb 25, 2025
1 parent 68f1ffe commit 6e56d50
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ovs-p4rt/sidecar/ovsp4rt_config_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef OVSP4RT_CONFIG_INT_H_
#define OVSP4RT_CONFIG_INT_H_

#include <absl/status/statusor.h>
#include <absl/status/status.h>

#include "client/ovsp4rt_client_interface.h"
#include "ovsp4rt/ovs-p4rt.h"
Expand Down
3 changes: 3 additions & 0 deletions ovs-p4rt/sidecar/ovsp4rt_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace ovsp4rt {

extern std::string EncodeByteValue(int arg_count...);

extern int GetActionId(const ::p4::config::v1::P4Info& p4info,
const std::string& a_name);

//----------------------------------------------------------------------
// Common functions
//----------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions ovs-p4rt/sidecar/tests/es2k/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ endmacro()
define_es2k_mac_map_test(es2k_config_dst_ip_mac_map_test)
define_es2k_mac_map_test(es2k_config_src_ip_mac_map_test)

define_es2k_config_test(es2k_prep_fdb_tunnel_table_test)

define_es2k_config_test(es2k_update_src_port_test)
define_es2k_config_test(es2k_update_tunnel_info_test)

Expand Down
146 changes: 146 additions & 0 deletions ovs-p4rt/sidecar/tests/es2k/es2k_prep_fdb_tunnel_table_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2025 Derek Foster
// SPDX-License-Identifier: Apache-2.0

// Unit test for PrepareFdbTunnelTableEntry().

#include <absl/status/status.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "client/ovsp4rt_test_client_mock.h"
#include "es2k/p4_name_mapping.h"
#include "logging/ovsp4rt_diag_detail.h"
#include "ovsp4rt/ovs-p4rt.h"
#include "ovsp4rt_private.h"
#include "p4/config/v1/p4info.pb.h"
#include "p4info_text.h"
#include "stratum/lib/utils.h"

using ::testing::InvokeWithoutArgs;
using ::testing::Return;

namespace ovsp4rt {

constexpr bool INSERT_ENTRY = true;
constexpr bool REMOVE_ENTRY = false;

constexpr bool DELETE_ENTRY = false;
constexpr char GRPC_ADDR[] = "1.2.3.4:5678";

class Es2kPrepFdbTunnelTableTest : public ::testing::Test {
protected:
Es2kPrepFdbTunnelTableTest() {}
virtual ~Es2kPrepFdbTunnelTableTest() = default;

static void InitTunnelLearnInfo(struct mac_learning_info& fdb_info,
uint8_t tunnel_type) {
constexpr uint8_t MAC_ADDR[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
constexpr uint8_t BRIDGE_ID = 42;

memcpy(fdb_info.mac_addr, MAC_ADDR, sizeof(MAC_ADDR));
fdb_info.bridge_id = BRIDGE_ID;
fdb_info.tnl_info.tunnel_type = tunnel_type;
fdb_info.is_tunnel = true;
}

static void InitVlanLearnInfo(struct mac_learning_info& fdb_info) {
constexpr uint8_t MAC_ADDR[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
constexpr uint8_t BRIDGE_ID = 99;
constexpr uint32_t SRC_PORT = 0x42;

memcpy(fdb_info.mac_addr, MAC_ADDR, sizeof(fdb_info.mac_addr));
fdb_info.bridge_id = BRIDGE_ID;
fdb_info.rx_src_port = SRC_PORT;
fdb_info.is_vlan = true;
}

static void InitV4NativeTagged(struct mac_learning_info& fdb_info) {
fdb_info.tnl_info.local_ip.family = AF_INET;
fdb_info.tnl_info.remote_ip.family = AF_INET;
fdb_info.vlan_info.port_vlan_mode = P4_PORT_VLAN_NATIVE_TAGGED;
fdb_info.tnl_info.vni = 0x1984U;
}

static void InitV4NativeUntagged(struct mac_learning_info& fdb_info) {
fdb_info.tnl_info.local_ip.family = AF_INET;
fdb_info.tnl_info.remote_ip.family = AF_INET;
fdb_info.vlan_info.port_vlan_mode = P4_PORT_VLAN_NATIVE_UNTAGGED;
fdb_info.tnl_info.vni = 0x1776U;
}

static void InitV6NativeTagged(struct mac_learning_info& fdb_info) {
fdb_info.tnl_info.local_ip.family = AF_INET6;
fdb_info.tnl_info.remote_ip.family = AF_INET6;
fdb_info.vlan_info.port_vlan_mode = P4_PORT_VLAN_NATIVE_TAGGED;
fdb_info.tnl_info.vni = 0xFACEU;
}

static void InitV6NativeUntagged(struct mac_learning_info& fdb_info) {
fdb_info.tnl_info.local_ip.family = AF_INET6;
fdb_info.tnl_info.remote_ip.family = AF_INET6;
fdb_info.vlan_info.port_vlan_mode = P4_PORT_VLAN_NATIVE_UNTAGGED;
fdb_info.tnl_info.vni = 0xCEDEU;
}

static void InitP4Info(::p4::config::v1::P4Info* p4info) {
auto status = stratum::ParseProtoFromString(P4INFO_TEXT, p4info);
EXPECT_TRUE(status.ok())
<< "ParseProtoFromString: " << status.error_message();
}

static void CheckActionId(const ::p4::v1::TableEntry& table_entry,
const ::p4::config::v1::P4Info& p4info,
int tunnel_type) {
std::string action_name;
if (tunnel_type == OVS_TUNNEL_VXLAN) {
action_name = L2_FWD_TX_TABLE_ACTION_SET_VXLAN_UNDERLAY_V4;
} else if (tunnel_type == OVS_TUNNEL_GENEVE) {
action_name = L2_FWD_TX_TABLE_ACTION_SET_GENEVE_UNDERLAY_V4;
} else {
EXPECT_TRUE(false) << "Invalid tunnel_type (" << tunnel_type << ")";
}

int expected_action_id = GetActionId(p4info, action_name);

auto table_action = table_entry.action();
auto action = table_action.action();
EXPECT_EQ(action.action_id(), expected_action_id);
}
};

TEST_F(Es2kPrepFdbTunnelTableTest, configV4VxlanTunnelEntry) {
::p4::v1::TableEntry table_entry;
DiagDetail detail;

struct mac_learning_info learn_info = {0};
InitTunnelLearnInfo(learn_info, OVS_TUNNEL_VXLAN);
InitV4NativeTagged(learn_info);

::p4::config::v1::P4Info p4info;
InitP4Info(&p4info);

PrepareFdbTunnelTableEntry(&table_entry, learn_info, p4info, INSERT_ENTRY,
detail);

// ASSERT
CheckActionId(table_entry, p4info, OVS_TUNNEL_VXLAN);
}

TEST_F(Es2kPrepFdbTunnelTableTest, configV4GeneveTunnelEntry) {
::p4::v1::TableEntry table_entry;
DiagDetail detail;

struct mac_learning_info learn_info = {0};
InitTunnelLearnInfo(learn_info, OVS_TUNNEL_GENEVE);
InitV4NativeTagged(learn_info);

::p4::config::v1::P4Info p4info;
InitP4Info(&p4info);

PrepareFdbTunnelTableEntry(&table_entry, learn_info, p4info, INSERT_ENTRY,
detail);

CheckActionId(table_entry, p4info, OVS_TUNNEL_GENEVE);
}

} // namespace ovsp4rt

0 comments on commit 6e56d50

Please sign in to comment.