Skip to content

Commit bba7aea

Browse files
vvbaefruffy
andauthored
Add Tofino target to RTSmith. (#11)
* add target tofino * Fix formatting. --------- Co-authored-by: fruffy <fruffy@nyu.edu>
1 parent 98c36f7 commit bba7aea

File tree

12 files changed

+294
-0
lines changed

12 files changed

+294
-0
lines changed

targets/tofino/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Source files for the main P4RtSmith.
2+
set(RTSMITH_SOURCES
3+
${RTSMITH_SOURCES}
4+
${CMAKE_CURRENT_SOURCE_DIR}/constants.cpp
5+
${CMAKE_CURRENT_SOURCE_DIR}/fuzzer.cpp
6+
${CMAKE_CURRENT_SOURCE_DIR}/program_info.cpp
7+
${CMAKE_CURRENT_SOURCE_DIR}/target.cpp
8+
${CMAKE_CURRENT_SOURCE_DIR}/tna.cpp
9+
PARENT_SCOPE
10+
)
11+
12+
set(RTSMITH_LIBS ${RTSMITH_LIBS} ${P4C_LIBRARIES} ${P4C_LIB_DEPS} ${CMAKE_THREAD_LIBS_INIT} PARENT_SCOPE)

targets/tofino/constants.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/constants.h"
2+
3+
namespace P4Tools::RTSmith::Tna {}

targets/tofino/constants.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_CONSTANTS_H_
2+
#define BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_CONSTANTS_H_
3+
4+
#include <cstdint>
5+
6+
namespace P4Tools::RTSmith::Tna {
7+
8+
enum Vtofino_gress_t { TOFINO_INGRESS, TOFINO_EGRESS };
9+
10+
/// TODO: Merge these with the P4Testgen constants. They are the same.
11+
class TnaConstants {
12+
public:
13+
/// Match bits exactly or not at all.
14+
static constexpr const char *MATCH_KIND_OPT = "optional";
15+
/// A match that is used as an argument for the selector.
16+
static constexpr const char *MATCH_KIND_SELECTOR = "selector";
17+
/// Entries that can match a range.
18+
static constexpr const char *MATCH_KIND_RANGE = "range";
19+
/// These definitions are derived from the numerical values of the enum
20+
/// named "PktInstanceType" in the p4lang/behavioral-model source file
21+
/// targets/simple_switch/simple_switch.h
22+
/// https://github.com/p4lang/behavioral-model/blob/main/targets/simple_switch/simple_switch.h#L146
23+
static constexpr uint64_t PKT_INSTANCE_TYPE_NORMAL = 0x0000;
24+
static constexpr uint64_t PKT_INSTANCE_TYPE_INGRESS_CLONE = 0x0001;
25+
static constexpr uint64_t PKT_INSTANCE_TYPE_EGRESS_CLONE = 0x0002;
26+
static constexpr uint64_t PKT_INSTANCE_TYPE_COALESCED = 0x0003;
27+
static constexpr uint64_t PKT_INSTANCE_TYPE_RECIRC = 0x0004;
28+
static constexpr uint64_t PKT_INSTANCE_TYPE_REPLICATION = 0x005;
29+
static constexpr uint64_t PKT_INSTANCE_TYPE_RESUBMIT = 0x006;
30+
/// The session IDs for clone are limited to a specific range.
31+
/// Details: https://github.com/p4lang/PI/pull/588
32+
static constexpr uint16_t CLONE_SESSION_ID_MIN = 1;
33+
static constexpr uint16_t CLONE_SESSION_ID_MAX = 32767;
34+
/// Clone type is derived from v1model.p4
35+
enum CloneType { I2E = 0, E2E = 1 };
36+
/// Meter colors are defined in v1model.p4
37+
enum METER_COLOR { GREEN = 0, YELLOW = 1, RED = 2 };
38+
39+
/// Other useful constants
40+
static constexpr int STF_MIN_PKT_SIZE = 22;
41+
static constexpr int ETH_HDR_SIZE = 112;
42+
static constexpr int DROP_PORT = 511;
43+
};
44+
45+
} // namespace P4Tools::RTSmith::Tna
46+
47+
#endif /* BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_CONSTANTS_H_ */

targets/tofino/fuzzer.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/fuzzer.h"
2+
3+
#include "backends/p4tools/common/lib/util.h"
4+
#include "backends/p4tools/modules/p4rtsmith/core/fuzzer.h"
5+
#include "control-plane/p4infoApi.h"
6+
7+
namespace P4Tools::RTSmith::Tna {
8+
9+
TofinoTnaFuzzer::TofinoTnaFuzzer(const TofinoTnaProgramInfo &programInfo)
10+
: P4RuntimeFuzzer(programInfo) {}
11+
12+
const TofinoTnaProgramInfo &TofinoTnaFuzzer::getProgramInfo() const {
13+
return *P4RuntimeFuzzer::getProgramInfo().checkedTo<TofinoTnaProgramInfo>();
14+
}
15+
16+
InitialP4RuntimeConfig TofinoTnaFuzzer::produceInitialConfig() {
17+
p4::v1::WriteRequest request;
18+
19+
auto p4Info = getProgramInfo().getP4RuntimeApi().p4Info;
20+
21+
const auto tables = p4Info->tables();
22+
const auto actions = p4Info->actions();
23+
24+
auto tableCnt = tables.size();
25+
auto tableGenCnt = Utils::getRandInt(tableCnt);
26+
27+
for (auto i = 0; (uint64_t)i < tableGenCnt; i++) {
28+
auto tableId = Utils::getRandInt(tableCnt - 1);
29+
auto table = tables.Get(tableId);
30+
auto maxEntryGenCnt = table.size();
31+
32+
p4::v1::Update update;
33+
update.set_type(p4::v1::Update_Type::Update_Type_INSERT);
34+
35+
auto tableEntry = produceTableEntry(table, actions, maxEntryGenCnt);
36+
*update.mutable_entity()->mutable_table_entry() = tableEntry;
37+
*request.add_updates() = update;
38+
}
39+
40+
std::vector<p4::v1::WriteRequest> requests{request};
41+
return requests;
42+
}
43+
44+
P4RuntimeUpdateSeries TofinoTnaFuzzer::produceUpdateTimeSeries() { return {}; }
45+
46+
} // namespace P4Tools::RTSmith::Tna

targets/tofino/fuzzer.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_FUZZER_H_
2+
#define BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_FUZZER_H_
3+
4+
#include "backends/p4tools/modules/p4rtsmith/core/fuzzer.h"
5+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/program_info.h"
6+
7+
namespace P4Tools::RTSmith::Tna {
8+
9+
class TofinoTnaFuzzer : public P4RuntimeFuzzer {
10+
private:
11+
/// @returns the program info associated with the current target.
12+
[[nodiscard]] const TofinoTnaProgramInfo &getProgramInfo() const override;
13+
14+
public:
15+
explicit TofinoTnaFuzzer(const TofinoTnaProgramInfo &programInfo);
16+
17+
InitialP4RuntimeConfig produceInitialConfig() override;
18+
19+
P4RuntimeUpdateSeries produceUpdateTimeSeries() override;
20+
};
21+
22+
} // namespace P4Tools::RTSmith::Tna
23+
24+
#endif /* BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_FUZZER_H_ */

targets/tofino/program_info.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/program_info.h"
2+
3+
#include "backends/p4tools/modules/p4rtsmith/core/program_info.h"
4+
#include "control-plane/p4RuntimeSerializer.h"
5+
#include "ir/ir.h"
6+
7+
#pragma GCC diagnostic push
8+
#pragma GCC diagnostic ignored "-Wunused-parameter"
9+
#pragma GCC diagnostic ignored "-Wpedantic"
10+
#include "p4/v1/p4runtime.pb.h"
11+
#pragma GCC diagnostic pop
12+
13+
namespace P4Tools::RTSmith::Tna {
14+
15+
TofinoTnaProgramInfo::TofinoTnaProgramInfo(const CompilerResult &compilerResult)
16+
: ProgramInfo(compilerResult, P4::P4RuntimeSerializer::get()->generateP4Runtime(
17+
&compilerResult.getProgram(), "tna")) {}
18+
19+
} // namespace P4Tools::RTSmith::Tna

targets/tofino/program_info.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_PROGRAM_INFO_H_
2+
#define BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_PROGRAM_INFO_H_
3+
4+
#include "backends/p4tools/common/compiler/compiler_target.h"
5+
#include "backends/p4tools/modules/p4rtsmith/core/program_info.h"
6+
#include "ir/ir.h"
7+
#include "ir/node.h"
8+
9+
namespace P4Tools::RTSmith::Tna {
10+
11+
class TofinoTnaProgramInfo : public ProgramInfo {
12+
public:
13+
explicit TofinoTnaProgramInfo(const CompilerResult &compilerResult);
14+
15+
DECLARE_TYPEINFO(TofinoTnaProgramInfo);
16+
};
17+
18+
} // namespace P4Tools::RTSmith::Tna
19+
20+
#endif /* BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_PROGRAM_INFO_H_ */

targets/tofino/register.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_REGISTER_H_
2+
#define BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_REGISTER_H_
3+
4+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/target.h"
5+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/tna.h"
6+
7+
namespace P4Tools::RTSmith {
8+
9+
/// Register the Tna compiler target with the tools framework.
10+
inline void tofino_registerCompilerTarget() { Tna::TofinoTnaCompilerTarget::make(); }
11+
12+
/// Register the Tna RtSmith target with the P4RuntimeSmith framework.
13+
inline void tofino_registerRtSmithTarget() { Tna::TofinoTnaRtSmithTarget::make(); }
14+
15+
} // namespace P4Tools::RTSmith
16+
17+
#endif /* BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_REGISTER_H_ */

targets/tofino/target.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/target.h"
2+
3+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/fuzzer.h"
4+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/program_info.h"
5+
#include "ir/ir.h"
6+
7+
namespace P4Tools::RTSmith::Tna {
8+
9+
/* =============================================================================================
10+
* TofinoTnaRtSmithTarget implementation
11+
* ============================================================================================= */
12+
13+
TofinoTnaRtSmithTarget::TofinoTnaRtSmithTarget() : RtSmithTarget("tofino", "tna") {}
14+
15+
void TofinoTnaRtSmithTarget::make() {
16+
static TofinoTnaRtSmithTarget *INSTANCE = nullptr;
17+
if (INSTANCE == nullptr) {
18+
INSTANCE = new TofinoTnaRtSmithTarget();
19+
}
20+
}
21+
22+
const ProgramInfo *TofinoTnaRtSmithTarget::produceProgramInfoImpl(
23+
const CompilerResult &compilerResult, const IR::Declaration_Instance * /*mainDecl*/) const {
24+
return new TofinoTnaProgramInfo(compilerResult);
25+
}
26+
27+
TofinoTnaFuzzer &TofinoTnaRtSmithTarget::getFuzzerImpl(const ProgramInfo &programInfo) const {
28+
return *new TofinoTnaFuzzer(*programInfo.checkedTo<TofinoTnaProgramInfo>());
29+
}
30+
31+
} // namespace P4Tools::RTSmith::Tna

targets/tofino/target.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_TARGET_H_
2+
#define BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_TARGET_H_
3+
4+
#include "backends/p4tools/modules/p4rtsmith/core/program_info.h"
5+
#include "backends/p4tools/modules/p4rtsmith/core/target.h"
6+
#include "backends/p4tools/modules/p4rtsmith/targets/tofino/fuzzer.h"
7+
#include "ir/ir.h"
8+
9+
namespace P4Tools::RTSmith::Tna {
10+
11+
class TofinoTnaRtSmithTarget : public RtSmithTarget {
12+
private:
13+
TofinoTnaRtSmithTarget();
14+
15+
public:
16+
/// Registers this target.
17+
static void make();
18+
19+
protected:
20+
const ProgramInfo *produceProgramInfoImpl(
21+
const CompilerResult &compilerResult,
22+
const IR::Declaration_Instance *mainDecl) const override;
23+
24+
[[nodiscard]] TofinoTnaFuzzer &getFuzzerImpl(const ProgramInfo &programInfo) const override;
25+
};
26+
27+
} // namespace P4Tools::RTSmith::Tna
28+
29+
#endif /* BACKENDS_P4TOOLS_MODULES_P4RTSMITH_TARGETS_TOFINO_TARGET_H_ */

0 commit comments

Comments
 (0)