P4C
The P4 Compiler
Loading...
Searching...
No Matches
dpdkCheckExternInvocation.h
1/*
2Copyright 2021 Intel Corp.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef BACKENDS_DPDK_DPDKCHECKEXTERNINVOCATION_H_
18#define BACKENDS_DPDK_DPDKCHECKEXTERNINVOCATION_H_
19
20#include <array>
21
22#include "dpdkProgramStructure.h"
23#include "frontends/p4/methodInstance.h"
24#include "ir/ir.h"
25#include "ir/visitor.h"
26#include "lib/cstring.h"
27#include "midend/checkExternInvocationCommon.h"
28
29namespace P4 {
30class TypeMap;
31} // namespace P4
32
33namespace P4::DPDK {
34
35using namespace P4::literals;
36
39
41 DpdkProgramStructure *structure;
42
43 enum block_t {
44 MAIN_PARSER = 0,
45 PRE_CONTROL,
46 MAIN_CONTROL,
47 MAIN_DEPARSER,
48 BLOCK_COUNT,
49 };
50
51 void initPipeConstraints() override {
52 bitvec validInMainControl;
53 validInMainControl.setbit(MAIN_CONTROL);
54 setPipeConstraints("send_to_port"_cs, validInMainControl);
55 setPipeConstraints("mirror_packet"_cs, validInMainControl);
56 // Add new constraints here
57 }
58
59 cstring getBlockName(int bit) override {
60 static const std::array<cstring, BLOCK_COUNT> lookup = {
61 "main parser"_cs, "pre control"_cs, "main control"_cs, "main deparser"_cs};
62 return lookup[bit % BLOCK_COUNT];
63 }
64
65 const IR::P4Parser *getParser(const cstring parserName) {
66 if (auto p = findContext<IR::P4Parser>()) {
67 if (structure->parsers.count(parserName) != 0 &&
68 structure->parsers.at(parserName)->name == p->name) {
69 return p;
70 }
71 }
72 return nullptr;
73 }
74
75 const IR::P4Control *getControl(const cstring controlName) {
76 if (auto c = findContext<IR::P4Control>()) {
77 if (structure->pipelines.count(controlName) != 0 &&
78 structure->pipelines.at(controlName)->name == c->name) {
79 return c;
80 }
81 }
82 return nullptr;
83 }
84
85 const IR::P4Control *getDeparser(const cstring deparserName) {
86 if (auto d = findContext<IR::P4Control>()) {
87 if (structure->deparsers.count(deparserName) != 0 &&
88 structure->deparsers.at(deparserName)->name == d->name) {
89 return d;
90 }
91 }
92 return nullptr;
93 }
94
95 void checkBlock(const IR::MethodCallExpression *expr, const cstring externType,
96 const cstring externName) {
97 bitvec pos;
98
99 LOG4("externType: " << externType << ", externName: " << externName);
100
101 if (pipeConstraints.count(externType)) {
102 if (auto block = getParser("MainParserT"_cs)) {
103 LOG4("MainParser: " << (void *)block << " " << dbp(block));
104 pos.setbit(MAIN_PARSER);
105 checkPipeConstraints(externType, pos, expr, externName, block->name);
106 } else if (auto block = getControl("PreControlT"_cs)) {
107 LOG4("PreControl: " << (void *)block << " " << dbp(block));
108 pos.setbit(PRE_CONTROL);
109 checkPipeConstraints(externType, pos, expr, externName, block->name);
110 } else if (auto block = getControl("MainControlT"_cs)) {
111 LOG4("MainControl: " << (void *)block << " " << dbp(block));
112 pos.setbit(MAIN_CONTROL);
113 checkPipeConstraints(externType, pos, expr, externName, block->name);
114 } else if (auto block = getDeparser("MainDeparserT"_cs)) {
115 LOG4("MainDeparser: " << (void *)block << " " << dbp(block));
116 pos.setbit(MAIN_DEPARSER);
117 checkPipeConstraints(externType, pos, expr, externName, block->name);
118 } else {
119 LOG4("Other");
120 }
121 } else {
122 LOG1("Pipe constraints not defined for: " << externType);
123 }
124 }
125
126 void checkExtern(const P4::ExternMethod *extMethod,
127 const IR::MethodCallExpression *expr) override {
128 LOG3("ExternMethod: " << extMethod << ", MethodCallExpression: " << expr);
129 checkBlock(expr, extMethod->originalExternType->name, extMethod->object->getName().name);
130 }
131
132 void checkExtern(const P4::ExternFunction *extFunction,
133 const IR::MethodCallExpression *expr) override {
134 LOG3("ExternFunction: " << extFunction << ", MethodCallExpression: " << expr);
135 checkBlock(expr, expr->method->toString(), cstring::empty);
136 }
137
138 public:
140 : P4::CheckExternInvocationCommon(typeMap), structure(structure) {
141 initPipeConstraints();
142 }
143};
144
148 P4::TypeMap *typeMap;
149 DpdkProgramStructure *structure;
150
151 public:
153 : typeMap(typeMap), structure(structure) {}
154
155 bool preorder(const IR::P4Program *program) {
156 if (structure->isPNA()) {
157 LOG1("Checking extern invocations for PNA architecture.");
158 CheckPNAExternInvocation checker(typeMap, structure);
159 program->apply(checker, getChildContext());
160 } else if (structure->isPSA()) {
161 LOG1("Checking extern invocations for PSA architecture.");
162 // Add class checking PSA constraints here.
163 } else {
164 LOG1("Unknown architecture: " << structure->p4arch << ", not checking constraints.");
165 }
166 return false;
167 }
168};
169
170} // namespace P4::DPDK
171
172#endif /* BACKENDS_DPDK_DPDKCHECKEXTERNINVOCATION_H_ */
Base class which can be used to prepare classes for checking constraints for invocations of externs (...
Definition checkExternInvocationCommon.h:41
void setPipeConstraints(cstring extType, bitvec vec)
Set the pipe (parser/control block) constraints.
Definition checkExternInvocationCommon.h:115
void checkPipeConstraints(cstring extType, bitvec bv, const IR::MethodCallExpression *expr, cstring extName, cstring pipe)
Check if the invocation of extern object method or extern function is valid in the block where it is ...
Definition checkExternInvocationCommon.h:137
Class which chooses the correct class for checking the constraints for invocations....
Definition dpdkCheckExternInvocation.h:147
Class for checking constraints for invocations of PNA architecture extern methods and functions.
Definition dpdkCheckExternInvocation.h:40
Definition methodInstance.h:194
Definition methodInstance.h:168
virtual ID getName() const =0
Definition visitor.h:400
const IR::IDeclaration * object
Definition methodInstance.h:79
Definition typeMap.h:41
Definition bitvec.h:120
Definition cstring.h:85
Definition dpdk/backend.cpp:37
Definition cstring.h:80
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Collect information related to P4 programs targeting dpdk.
Definition dpdkProgramStructure.h:16
bool isPNA(void)
Predicate that states whether architecture is PNA or not.
Definition dpdkProgramStructure.h:104
bool isPSA(void)
Predicate that states whether architecture is PSA or not.
Definition dpdkProgramStructure.h:96