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 ReferenceMap;
31class TypeMap;
32} // namespace P4
33
34namespace P4::DPDK {
35
36using namespace P4::literals;
37
40
42 DpdkProgramStructure *structure;
43
44 enum block_t {
45 MAIN_PARSER = 0,
46 PRE_CONTROL,
47 MAIN_CONTROL,
48 MAIN_DEPARSER,
49 BLOCK_COUNT,
50 };
51
52 void initPipeConstraints() override {
53 bitvec validInMainControl;
54 validInMainControl.setbit(MAIN_CONTROL);
55 setPipeConstraints("send_to_port"_cs, validInMainControl);
56 setPipeConstraints("mirror_packet"_cs, validInMainControl);
57 // Add new constraints here
58 }
59
60 cstring getBlockName(int bit) override {
61 static const std::array<cstring, BLOCK_COUNT> lookup = {
62 "main parser"_cs, "pre control"_cs, "main control"_cs, "main deparser"_cs};
63 return lookup[bit % BLOCK_COUNT];
64 }
65
66 const IR::P4Parser *getParser(const cstring parserName) {
67 if (auto p = findContext<IR::P4Parser>()) {
68 if (structure->parsers.count(parserName) != 0 &&
69 structure->parsers.at(parserName)->name == p->name) {
70 return p;
71 }
72 }
73 return nullptr;
74 }
75
76 const IR::P4Control *getControl(const cstring controlName) {
77 if (auto c = findContext<IR::P4Control>()) {
78 if (structure->pipelines.count(controlName) != 0 &&
79 structure->pipelines.at(controlName)->name == c->name) {
80 return c;
81 }
82 }
83 return nullptr;
84 }
85
86 const IR::P4Control *getDeparser(const cstring deparserName) {
87 if (auto d = findContext<IR::P4Control>()) {
88 if (structure->deparsers.count(deparserName) != 0 &&
89 structure->deparsers.at(deparserName)->name == d->name) {
90 return d;
91 }
92 }
93 return nullptr;
94 }
95
96 void checkBlock(const IR::MethodCallExpression *expr, const cstring externType,
97 const cstring externName) {
98 bitvec pos;
99
100 LOG4("externType: " << externType << ", externName: " << externName);
101
102 if (pipeConstraints.count(externType)) {
103 if (auto block = getParser("MainParserT"_cs)) {
104 LOG4("MainParser: " << (void *)block << " " << dbp(block));
105 pos.setbit(MAIN_PARSER);
106 checkPipeConstraints(externType, pos, expr, externName, block->name);
107 } else if (auto block = getControl("PreControlT"_cs)) {
108 LOG4("PreControl: " << (void *)block << " " << dbp(block));
109 pos.setbit(PRE_CONTROL);
110 checkPipeConstraints(externType, pos, expr, externName, block->name);
111 } else if (auto block = getControl("MainControlT"_cs)) {
112 LOG4("MainControl: " << (void *)block << " " << dbp(block));
113 pos.setbit(MAIN_CONTROL);
114 checkPipeConstraints(externType, pos, expr, externName, block->name);
115 } else if (auto block = getDeparser("MainDeparserT"_cs)) {
116 LOG4("MainDeparser: " << (void *)block << " " << dbp(block));
117 pos.setbit(MAIN_DEPARSER);
118 checkPipeConstraints(externType, pos, expr, externName, block->name);
119 } else {
120 LOG4("Other");
121 }
122 } else {
123 LOG1("Pipe constraints not defined for: " << externType);
124 }
125 }
126
127 void checkExtern(const P4::ExternMethod *extMethod,
128 const IR::MethodCallExpression *expr) override {
129 LOG3("ExternMethod: " << extMethod << ", MethodCallExpression: " << expr);
130 checkBlock(expr, extMethod->originalExternType->name, extMethod->object->getName().name);
131 }
132
133 void checkExtern(const P4::ExternFunction *extFunction,
134 const IR::MethodCallExpression *expr) override {
135 LOG3("ExternFunction: " << extFunction << ", MethodCallExpression: " << expr);
136 checkBlock(expr, expr->method->toString(), cstring::empty);
137 }
138
139 public:
141 DpdkProgramStructure *structure)
142 : P4::CheckExternInvocationCommon(refMap, typeMap), structure(structure) {
143 initPipeConstraints();
144 }
145};
146
150 P4::ReferenceMap *refMap;
151 P4::TypeMap *typeMap;
152 DpdkProgramStructure *structure;
153
154 public:
156 DpdkProgramStructure *structure)
157 : refMap(refMap), typeMap(typeMap), structure(structure) {}
158
159 bool preorder(const IR::P4Program *program) {
160 if (structure->isPNA()) {
161 LOG1("Checking extern invocations for PNA architecture.");
162 auto checker = new CheckPNAExternInvocation(refMap, typeMap, structure);
163 program->apply(*checker);
164 } else if (structure->isPSA()) {
165 LOG1("Checking extern invocations for PSA architecture.");
166 // Add class checking PSA constraints here.
167 } else {
168 LOG1("Unknown architecture: " << structure->p4arch << ", not checking constraints.");
169 }
170 return false;
171 }
172};
173
174} // namespace P4::DPDK
175
176#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:116
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:138
Class which chooses the correct class for checking the constraints for invocations....
Definition dpdkCheckExternInvocation.h:149
Class for checking constraints for invocations of PNA architecture extern methods and functions.
Definition dpdkCheckExternInvocation.h:41
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
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition bitvec.h:120
Definition cstring.h:85
Definition dpdk/backend.cpp:36
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:97
bool isPSA(void)
Predicate that states whether architecture is PSA or not.
Definition dpdkProgramStructure.h:89