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 {
 
   76        ::error(ErrorType::ERR_UNSUPPORTED, 
"emitTableDeclSpinlock is not supported on %1% target",
 
   82                                  TableKind innerTableKind, 
cstring innerKeyType,
 
   84                                  TableKind outerTableKind, 
cstring outerKeyType,
 
   85                                  unsigned outerSize)
 const {
 
   96        ::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; }
 
 
  207        for (
auto anno : annotations) {
 
  208            if (anno->name != 
"tc_type") 
continue;
 
  209            for (
auto annoVal : anno->body) {
 
  210                if (annoVal->text == 
"macaddr" || annoVal->text == 
"ipv4" ||
 
  211                    annoVal->text == 
"ipv6" || annoVal->text == 
"be16" || annoVal->text == 
"be32" ||
 
  212                    annoVal->text == 
"be64") {
 
  221                         const IR::Expression *exp)
 const {
 
  222        if (
auto mem = exp->to<IR::Member>()) {
 
  223            auto type = typeMap->getType(mem->expr, 
true);
 
  224            if (type->is<IR::Type_StructLike>()) {
 
  225                auto field = type->to<IR::Type_StructLike>()->getField(mem->member);
 
  226                return getByteOrderFromAnnotation(field->getAnnotations()->annotations);
 
  229            auto paramList = action->getParameters();
 
  230            if (paramList != 
nullptr && !paramList->empty()) {
 
  231                for (
auto param : paramList->parameters) {
 
  232                    if (param->name.originalName == exp->toString()) {
 
  233                        return getByteOrderFromAnnotation(param->getAnnotations()->annotations);
 
 
  247    cstring forwardReturnCode()
 const override { 
return "XDP_PASS"_cs; }
 
  248    cstring dropReturnCode()
 const override { 
return "XDP_DROP"_cs; }
 
  249    cstring abortReturnCode()
 const override { 
return "XDP_ABORTED"_cs; }
 
  250    cstring redirectReturnCode()
 const { 
return "XDP_REDIRECT"_cs; }
 
  251    cstring sysMapPath()
 const override { 
return "/sys/fs/bpf/xdp/globals"_cs; }
 
  252    cstring packetDescriptorType()
 const override { 
return "struct xdp_md"_cs; }
 
  255        return cstring(
"(") + base + 
"->data_end - " + base + 
"->data)";
 
  258                          cstring offsetVar) 
const override;
 
  260                  cstring argName)
 const override {
 
  261        builder->appendFormat(
"int %s(%s *%s)", functionName.c_str(), packetDescriptorType(),
 
 
  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 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:115
 
void emitTraceMessage(Util::SourceCodeBuilder *builder, const char *format, int argc=0,...) const override
Definition ebpf/target.cpp:179
 
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:243
 
Definition sourceCodeBuilder.h:29
 
Definition codeGen.cpp:25