-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test to check whether pna_nic parses a sample_pna.json
- Loading branch information
1 parent
04e4a33
commit 6e0f6cd
Showing
6 changed files
with
420 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
test* | ||
!test*.cpp | ||
!test*.cpp | ||
!testdata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
178
targets/pna_nic/tests/testdata/pna-demo-L2-one-table.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* Copyright 2013-present Barefoot Networks, 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. | ||
*/ | ||
|
||
/* | ||
* Antonin Bas ([email protected]) | ||
* | ||
*/ | ||
|
||
#include <chrono> | ||
#include <string> | ||
#include <vector> | ||
#include <algorithm> // for std::copy, std::min | ||
|
||
#include "utils.h" | ||
|
||
PacketInReceiver::PacketInReceiver() { } | ||
|
||
void | ||
PacketInReceiver::receive(int port_num, const char *buffer, int len, | ||
void *cookie) { | ||
(void) cookie; | ||
std::unique_lock<std::mutex> lock(mutex); | ||
while (status != Status::CAN_RECEIVE) { | ||
can_receive.wait(lock); | ||
} | ||
buffer_.insert(buffer_.end(), buffer, buffer + len); | ||
port = port_num; | ||
status = Status::CAN_READ; | ||
can_read.notify_one(); | ||
} | ||
|
||
size_t | ||
PacketInReceiver::read(char *dst, size_t max_size, int *recv_port) { | ||
std::unique_lock<std::mutex> lock(mutex); | ||
while (status != Status::CAN_READ) { | ||
can_read.wait(lock); | ||
} | ||
size_t size = std::min(max_size, buffer_.size()); | ||
std::copy(buffer_.begin(), buffer_.begin() + size, dst); | ||
buffer_.clear(); | ||
*recv_port = port; | ||
status = Status::CAN_RECEIVE; | ||
can_receive.notify_one(); | ||
return size; | ||
} | ||
|
||
PacketInReceiver::Status | ||
PacketInReceiver::check_status() { | ||
std::unique_lock<std::mutex> lock(mutex); | ||
return status; | ||
} |
Oops, something went wrong.