Skip to content

Commit

Permalink
Added test to check whether pna_nic parses a sample_pna.json
Browse files Browse the repository at this point in the history
  • Loading branch information
rupesh-chiluka-marvell committed Jul 29, 2024
1 parent 04e4a33 commit ac0ebd6
Show file tree
Hide file tree
Showing 6 changed files with 580 additions and 4 deletions.
3 changes: 2 additions & 1 deletion targets/pna_nic/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test*
!test*.cpp
!test*.cpp
!testdata
13 changes: 10 additions & 3 deletions targets/pna_nic/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ $(top_builddir)/src/bm_apps/libbmapps.la \
-lboost_filesystem

# Define unit tests
common_source = main.cpp
common_source = main.cpp utils.cpp utils.h
TESTS = test_internet_checksum \
test_hash
test_hash \
test_parse_pna

check_PROGRAMS = $(TESTS) test_all

# Sources for tests
test_internet_checksum_SOURCES = $(common_source) test_internet_checksum.cpp
test_hash_SOURCES = $(common_source) test_hash.cpp
test_parse_pna_SOURCES = $(common_source) test_parse_pna.cpp

test_all_SOURCES = $(common_source) \
test_internet_checksum.cpp \
test_hash.cpp
test_hash.cpp \
test_parse_pna.cpp

EXTRA_DIST = \
testdata/pna-demo-L2-one-table.json \
testdata/pna-demo-L2-one-table.p4
111 changes: 111 additions & 0 deletions targets/pna_nic/tests/test_parse_pna.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* Copyright 2024 Marvell Technology, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <gtest/gtest.h>

#include <bm/bm_apps/packet_pipe.h>

#include <string>
#include <memory>
#include <vector>
#include <algorithm>

#include <boost/filesystem.hpp>

#include "pna_nic.h"

#include "utils.h"

namespace fs = boost::filesystem;

using bm::MatchErrorCode;
using bm::ActionData;
using bm::MatchKeyParam;
using bm::entry_handle_t;

namespace {

void packet_handler(int port_num, const char *buffer, int len, void *cookie) {
static_cast<bm::pna::PnaNic *>(cookie)->receive(port_num, buffer, len);
}

} // namespace

class PNA_ParseTest: public ::testing::Test {
protected:
static constexpr size_t kMaxBufSize = 512;

static constexpr bm::device_id_t device_id{0};

PNA_ParseTest()
: packet_inject(packet_in_addr) { }

// Per-test-case set-up.
static void SetUpTestCase() {
test_pna_nic = new bm::pna::PnaNic();

// load JSON
fs::path json_path = fs::path(testdata_dir) / fs::path(test_json);
test_pna_nic->init_objects(json_path.string());

// packet in -packet out
test_pna_nic->set_dev_mgr_packet_in(device_id, packet_in_addr, nullptr);
test_pna_nic->Switch::start(); // there is a start member in PnaNic
test_pna_nic->set_packet_handler(packet_handler, static_cast<void *>(test_pna_nic));
test_pna_nic->start_and_return();
}

// Per-test-case tear-down.
static void TearDownTestCase() {
delete test_pna_nic;
}

virtual void SetUp() {
packet_inject.start();
auto cb = std::bind(&PacketInReceiver::receive, &receiver,
std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4);
packet_inject.set_packet_receiver(cb, nullptr);
}

protected:
static const char packet_in_addr[];
static bm::pna::PnaNic *test_pna_nic;
bm_apps::PacketInject packet_inject;
PacketInReceiver receiver{};

private:
static const char testdata_dir[];
static const char test_json[];
};

const char PNA_ParseTest::packet_in_addr[] = "inproc://packets";

bm::pna::PnaNic *PNA_ParseTest::test_pna_nic = nullptr;

const char PNA_ParseTest::testdata_dir[] = TESTDATADIR;
const char PNA_ParseTest::test_json[] = "pna-demo-L2-one-table.json";

TEST_F(PNA_ParseTest, Parse) {
static constexpr int port = 1;

const char pkt[] = {'\x00'};
packet_inject.send(port, pkt, sizeof(pkt));
char recv_buffer[] = {'\x00', '\x00'};
int recv_port = -1;
receiver.read(
recv_buffer, sizeof(recv_buffer), &recv_port);
ASSERT_TRUE(true);
}
178 changes: 178 additions & 0 deletions targets/pna_nic/tests/testdata/pna-demo-L2-one-table.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{
"header_types" : [
{
"name" : "ethernet_t",
"id" : 0,
"fields" : [
["dstAddr", 48, false],
["srcAddr", 48, false],
["etherType", 16, false]
]
},
{
"name" : "pna_main_parser_input_metadata_t",
"id" : 1,
"fields" : [
["recirculated", 1, false],
["input_port", 32, false],
["_padding", 7, false]
]
},
{
"name" : "pna_main_input_metadata_t",
"id" : 2,
"fields" : [
["recirculated", 1, false],
["timestamp", 64, false],
["parser_error", 32, false],
["class_of_service", 8, false],
["input_port", 32, false],
["_padding_0", 7, false]
]
},
{
"name" : "pna_main_output_metadata_t",
"id" : 3,
"fields" : [
["class_of_service", 8, false]
]
},
{
"name" : "scalars_t",
"id" : 4,
"fields" : []
}
],
"headers" : [
{
"name" : "eth",
"id" : 0,
"header_type" : "ethernet_t",
"metadata" : false,
"pi_omit" : true
},
{
"name" : "pna_main_parser_input_metadata",
"id" : 1,
"header_type" : "pna_main_parser_input_metadata_t",
"metadata" : true,
"pi_omit" : true
},
{
"name" : "pna_main_input_metadata",
"id" : 2,
"header_type" : "pna_main_input_metadata_t",
"metadata" : true,
"pi_omit" : true
},
{
"name" : "pna_main_output_metadata",
"id" : 3,
"header_type" : "pna_main_output_metadata_t",
"metadata" : true,
"pi_omit" : true
},
{
"name" : "<null>",
"id" : 4,
"header_type" : "scalars_t",
"metadata" : false,
"pi_omit" : true
}
],
"header_stacks" : [],
"header_union_types" : [],
"header_unions" : [],
"header_union_stacks" : [],
"field_lists" : [],
"errors" : [],
"enums" : [
{
"name" : "PNA_MeterColor_t",
"entries" : [
["GREEN", 1],
["RED", 0],
["YELLOW", 2]
]
}
],
"parsers" : [
{
"name" : "main_parser",
"id" : 0,
"init_state" : "start",
"parse_states" : [
{
"name" : "start",
"id" : 0,
"parser_ops" : [
{
"parameters" : [
{
"type" : "regular",
"value" : "eth"
}
],
"op" : "extract"
}
],
"transitions" : [
{
"type" : "default",
"value" : null,
"mask" : null,
"next_state" : null
}
],
"transition_key" : []
}
]
}
],
"parse_vsets" : [],
"deparsers" : [
{
"name" : "main_deparser",
"id" : 0,
"source_info" : {
"filename" : "pna-demo-L2-one-table.p4",
"line" : 98,
"column" : 8,
"source_fragment" : "MainDeparserImpl"
},
"order" : ["eth"],
"primitives" : []
}
],
"meter_arrays" : [],
"counter_arrays" : [],
"register_arrays" : [],
"calculations" : [],
"learn_lists" : [],
"actions" : [],
"pipelines" : [
{
"name" : "main_control",
"id" : 0,
"source_info" : {
"filename" : "pna-demo-L2-one-table.p4",
"line" : 58,
"column" : 8,
"source_fragment" : "MainControlImpl"
},
"init_table" : null,
"tables" : [],
"action_profiles" : [],
"conditionals" : []
}
],
"checksums" : [],
"force_arith" : [],
"extern_instances" : [],
"field_aliases" : [],
"program" : "pna-demo-L2-one-table.p4",
"__meta__" : {
"version" : [2, 23],
"compiler" : "https://github.com/p4lang/p4c"
}
}
Loading

0 comments on commit ac0ebd6

Please sign in to comment.