17#ifndef BACKENDS_EBPF_TARGET_H_
18#define BACKENDS_EBPF_TARGET_H_
20#include "frontends/p4/typeMap.h"
22#include "lib/cstring.h"
24#include "lib/exceptions.h"
25#include "lib/sourceCodeBuilder.h"
66 unsigned size)
const = 0;
69 unsigned size)
const {
77 "emitTableDeclSpinlock is not supported on %1% target", name);
82 TableKind innerTableKind,
cstring innerKeyType,
84 TableKind outerTableKind,
cstring outerKeyType,
85 unsigned outerSize)
const {
96 ::P4::error(ErrorType::ERR_UNSUPPORTED,
"emitMapInMapDecl is not supported on %1% target",
104 virtual cstring forwardReturnCode()
const = 0;
105 virtual cstring dropReturnCode()
const = 0;
106 virtual cstring abortReturnCode()
const = 0;
108 virtual cstring sysMapPath()
const = 0;
109 virtual cstring packetDescriptorType()
const = 0;
132 mutable unsigned int innerMapIndex;
134 cstring getBPFMapType(TableKind kind)
const {
135 if (kind == TableHash) {
136 return "BPF_MAP_TYPE_HASH"_cs;
137 }
else if (kind == TableArray) {
138 return "BPF_MAP_TYPE_ARRAY"_cs;
139 }
else if (kind == TablePerCPUArray) {
140 return "BPF_MAP_TYPE_PERCPU_ARRAY"_cs;
141 }
else if (kind == TableLPMTrie) {
142 return "BPF_MAP_TYPE_LPM_TRIE"_cs;
143 }
else if (kind == TableHashLRU) {
144 return "BPF_MAP_TYPE_LRU_HASH"_cs;
145 }
else if (kind == TableProgArray) {
146 return "BPF_MAP_TYPE_PROG_ARRAY"_cs;
147 }
else if (kind == TableDevmap) {
148 return "BPF_MAP_TYPE_DEVMAP"_cs;
150 BUG(
"Unknown table kind");
154 bool emitTraceMessages;
158 :
Target(name), innerMapIndex(0), emitTraceMessages(emitTrace) {}
164 cstring offsetVar)
const override;
172 cstring keyType,
cstring valueType,
unsigned size)
const override;
175 unsigned size)
const override;
177 TableKind innerTableKind,
cstring innerKeyType,
cstring innerValueType,
178 unsigned innerSize,
cstring outerName, TableKind outerTableKind,
179 cstring outerKeyType,
unsigned outerSize)
const override;
181 cstring argName)
const override;
186 return cstring(
"((void*)(long)") + base +
"->data)";
189 return cstring(
"((void*)(long)") + base +
"->data_end)";
192 cstring forwardReturnCode()
const override {
return "TC_ACT_OK"_cs; }
193 cstring dropReturnCode()
const override {
return "TC_ACT_SHOT"_cs; }
194 cstring abortReturnCode()
const override {
return "TC_ACT_SHOT"_cs; }
195 cstring sysMapPath()
const override {
return "/sys/fs/bpf/tc/globals"_cs; }
197 cstring packetDescriptorType()
const override {
return "struct __sk_buff"_cs; }
208 static cstring getByteOrderFromAnnotation(
const IR::IAnnotated *node) {
209 if (
const auto *anno = node->getAnnotation(
"tc_type"_cs)) {
210 cstring value = anno->needsParsing()
211 ? anno->getUnparsed().at(0)->text
212 : anno->getExpr().at(0)->checkedTo<IR::StringLiteral>()->value;
213 if (value ==
"macaddr" || value ==
"ipv4" || value ==
"ipv6" || value ==
"be16" ||
214 value ==
"be32" || value ==
"be64") {
222 const IR::Expression *exp)
const {
223 if (
auto mem = exp->to<IR::Member>()) {
224 auto type = typeMap->getType(mem->expr,
true);
225 if (type->is<IR::Type_StructLike>()) {
226 auto field = type->to<IR::Type_StructLike>()->getField(mem->member);
227 return getByteOrderFromAnnotation(field);
230 auto paramList = action->getParameters();
231 if (paramList !=
nullptr && !paramList->empty()) {
232 for (
auto param : paramList->parameters) {
233 if (param->name.originalName == exp->toString()) {
234 return getByteOrderFromAnnotation(param);
248 cstring forwardReturnCode()
const override {
return "XDP_PASS"_cs; }
249 cstring dropReturnCode()
const override {
return "XDP_DROP"_cs; }
250 cstring abortReturnCode()
const override {
return "XDP_ABORTED"_cs; }
251 cstring redirectReturnCode()
const {
return "XDP_REDIRECT"_cs; }
252 cstring sysMapPath()
const override {
return "/sys/fs/bpf/xdp/globals"_cs; }
253 cstring packetDescriptorType()
const override {
return "struct xdp_md"_cs; }
256 return cstring(
"(") + base +
"->data_end - " + base +
"->data)";
259 cstring offsetVar)
const override;
261 cstring argName)
const override {
262 builder->appendFormat(
"int %v(%v *%v)", functionName, packetDescriptorType(), argName);
281 cstring keyType,
cstring valueType,
unsigned size)
const override;
283 cstring argName)
const override;
286 return cstring(
"(") + base +
" + " + base +
"->len)";
289 cstring forwardReturnCode()
const override {
return "0"_cs; }
290 cstring dropReturnCode()
const override {
return "1"_cs; }
291 cstring abortReturnCode()
const override {
return "1"_cs; }
292 cstring sysMapPath()
const override {
return "/sys/fs/bpf"_cs; }
293 cstring packetDescriptorType()
const override {
return "struct __sk_buff"_cs; }
305 cstring keyType,
cstring valueType,
unsigned size)
const override;
307 return cstring(
"((void*)(long)") + base +
"->data)";
310 return cstring(
"((void*)(long)(") + base +
"->data + " + base +
"->len))";
312 cstring forwardReturnCode()
const override {
return "true"_cs; }
313 cstring dropReturnCode()
const override {
return "false"_cs; }
314 cstring abortReturnCode()
const override {
return "false"_cs; }
315 cstring sysMapPath()
const override {
return "/sys/fs/bpf"_cs; }
316 cstring packetDescriptorType()
const override {
return "struct __sk_buff"_cs; }
Represents a target compiled by bcc that uses the TC.
Definition ebpf/target.h:267
Definition ebpf/target.h:130
void emitTraceMessage(Util::SourceCodeBuilder *builder, const char *format, int argc=0,...) const override
Definition ebpf/target.cpp:179
void emitMapInMapDecl(Util::SourceCodeBuilder *builder, cstring innerName, TableKind innerTableKind, cstring innerKeyType, cstring innerValueType, unsigned innerSize, cstring outerName, TableKind outerTableKind, cstring outerKeyType, unsigned outerSize) const override
Definition ebpf/target.cpp:114
Definition ebpf/target.h:203
Definition ebpf/target.h:44
virtual void emitTraceMessage(Util::SourceCodeBuilder *builder, const char *format, int argc,...) const
Definition ebpf/target.cpp:25
virtual void emitMapInMapDecl(Util::SourceCodeBuilder *builder, cstring innerName, TableKind innerTableKind, cstring innerKeyType, cstring innerValueType, unsigned innerSize, cstring outerName, TableKind outerTableKind, cstring outerKeyType, unsigned outerSize) const
Definition ebpf/target.h:81
Definition ebpf/target.h:298
Target XDP.
Definition ebpf/target.h:244
Definition sourceCodeBuilder.h:29
Definition codeGen.cpp:25
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51