Skip to content

Commit

Permalink
Fixed ebpf_packetOffsetInBits field in parser and control_block C file (
Browse files Browse the repository at this point in the history
#4233)

* Fixed ebpf_packetOffsetInBits field in parser and control_block C file
* Move pna.h from tc runtime to ebpf runtime
* Change Permisson for Default Action.
* Reverse endianess conversion - Convert header fields to host endian
* Reverse movement of pna.h file from ebpf/runtime to tc/runtime
  • Loading branch information
komaljai authored and fruffy committed Nov 21, 2023
1 parent 9e64057 commit 1406aa6
Show file tree
Hide file tree
Showing 70 changed files with 300 additions and 278 deletions.
5 changes: 0 additions & 5 deletions backends/ebpf/runtime/ebpf_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ limitations under the License.
#define load_word(data, b) bpf_ntohl(*(u32 *)((u8*)(data) + (b)))
#define load_dword(data, b) bpf_be64_to_cpu(*(u64 *)((u8*)(data) + (b)))

// Load without endianess conversion
#define load_half_ne(data, b) (*(u16 *)((u8*)(data) + (b)))
#define load_word_ne(data, b) (*(u32 *)((u8*)(data) + (b)))
#define load_dword_ne(data, b) (*(u64 *)((u8*)(data) + (b)))


/* If we operate in user space we only need to include bpf.h and
* define the userspace API macros.
Expand Down
24 changes: 16 additions & 8 deletions backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ void PNAEbpfGenerator::emitGlobalHeadersMetadata(EBPF::CodeBuilder *builder) con
userMetadataType->declare(builder, "cpumap_usermeta", false);
builder->endOfStatement(true);

builder->emitIndent();
builder->appendFormat("unsigned %s", pipeline->offsetVar.c_str());
builder->endOfStatement(true);

// additional field to avoid compiler errors when both headers and user_metadata are empty.
builder->emitIndent();
builder->append("__u8 __hook");
Expand Down Expand Up @@ -280,6 +284,9 @@ void TCIngressPipelinePNA::emit(EBPF::CodeBuilder *builder) {
builder->blockEnd(true);
}

builder->emitIndent();
builder->appendFormat("hdrMd->%s = %s", offsetVar.c_str(), offsetVar.c_str());
builder->newline();
builder->emitIndent();
builder->appendFormat("return %d;", actUnspecCode);
builder->newline();
Expand Down Expand Up @@ -455,10 +462,11 @@ void TCIngressPipelinePNA::emitTrafficManager(EBPF::CodeBuilder *builder) {

void TCIngressPipelinePNA::emitLocalVariables(EBPF::CodeBuilder *builder) {
builder->emitIndent();
builder->appendFormat("unsigned %s = 0;", offsetVar.c_str());
builder->newline();
builder->emitIndent();
builder->appendFormat("unsigned %s_save = 0;", offsetVar.c_str());
if (name == "tc-parse") {
builder->appendFormat("unsigned %s = 0;", offsetVar.c_str());
} else if (name == "tc-ingress") {
builder->appendFormat("unsigned %s = hdrMd->%s;", offsetVar.c_str(), offsetVar.c_str());
}
builder->newline();
builder->emitIndent();
builder->appendFormat("%s %s = %s;", errorEnum.c_str(), errorVar.c_str(),
Expand Down Expand Up @@ -548,14 +556,14 @@ void PnaStateTranslationVisitor::compileExtractField(const IR::Expression *expr,
helper = "load_byte";
loadSize = 8;
} else if (wordsToRead <= 2) {
helper = "load_half_ne";
helper = "load_half";
loadSize = 16;
} else if (wordsToRead <= 4) {
helper = "load_word_ne";
helper = "load_word";
loadSize = 32;
} else {
if (wordsToRead > 64) BUG("Unexpected width %d", widthToExtract);
helper = "load_dword_ne";
helper = "load_dword";
loadSize = 64;
}

Expand Down Expand Up @@ -612,7 +620,7 @@ void PnaStateTranslationVisitor::compileExtractField(const IR::Expression *expr,
if (shift == 0)
helper = "load_byte";
else
helper = "load_half_ne";
helper = "load_half";
auto bt = EBPF::EBPFTypeFactory::instance->create(IR::Type_Bits::get(8));
unsigned bytes = ROUNDUP(widthToExtract, 8);
for (unsigned i = 0; i < bytes; i++) {
Expand Down
4 changes: 2 additions & 2 deletions backends/tc/tc.def
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class TCTable {
+ "/" + controlName + "/" + tableName
+ " default_hit_action";
if (isDefaultHitConst) {
tcTable += " permissions 0x109";
tcTable += " permissions 0x1024";
}
tcTable += " action " + defaultHitAction->getName();
}
Expand All @@ -284,7 +284,7 @@ class TCTable {
+ "/" + controlName + "/" + tableName
+ " default_miss_action";
if (isDefaultMissConst) {
tcTable += " permissions 0x109";
tcTable += " permissions 0x1024";
}
tcTable += " action " + defaultMissAction->getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ int xdp_func(struct xdp_md *skb) {
}
static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__)
{
unsigned ebpf_packetOffsetInBits = 0;
unsigned ebpf_packetOffsetInBits_save = 0;
unsigned ebpf_packetOffsetInBits = hdrMd->ebpf_packetOffsetInBits;
ParserError_t ebpf_errorCode = NoError;
void* pkt = ((void*)(long)skb->data);
void* ebpf_packetEnd = ((void*)(long)skb->data_end);
Expand Down Expand Up @@ -360,6 +359,7 @@ if (/* hdr->ipv4.isValid() */
}
;
}
hdrMd->ebpf_packetOffsetInBits = ebpf_packetOffsetInBits
return -1;
}
SEC("classifier/tc-ingress")
Expand Down
20 changes: 10 additions & 10 deletions testdata/p4tc_samples_outputs/default_action_example_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ REGISTER_END()
static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__)
{
unsigned ebpf_packetOffsetInBits = 0;
unsigned ebpf_packetOffsetInBits_save = 0;
ParserError_t ebpf_errorCode = NoError;
void* pkt = ((void*)(long)skb->data);
void* ebpf_packetEnd = ((void*)(long)skb->data_end);
Expand Down Expand Up @@ -49,16 +48,16 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
hdr->ipv4.diffserv = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 8;

hdr->ipv4.totalLen = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.totalLen = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->ipv4.identification = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.identification = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->ipv4.flags = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 5) & EBPF_MASK(u8, 3));
ebpf_packetOffsetInBits += 3;

hdr->ipv4.fragOffset = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u16, 13));
hdr->ipv4.fragOffset = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u16, 13));
ebpf_packetOffsetInBits += 13;

hdr->ipv4.ttl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))));
Expand All @@ -67,13 +66,13 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
hdr->ipv4.protocol = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 8;

hdr->ipv4.hdrChecksum = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.hdrChecksum = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->ipv4.srcAddr = (u32)((load_word_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.srcAddr = (u32)((load_word(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 32;

hdr->ipv4.dstAddr = (u32)((load_word_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.dstAddr = (u32)((load_word(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 32;

hdr->ipv4.ebpf_valid = 1;
Expand All @@ -88,13 +87,13 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
goto reject;
}

hdr->ethernet.dstAddr = (u64)((load_dword_ne(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48));
hdr->ethernet.dstAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48));
ebpf_packetOffsetInBits += 48;

hdr->ethernet.srcAddr = (u64)((load_dword_ne(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48));
hdr->ethernet.srcAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48));
ebpf_packetOffsetInBits += 48;

hdr->ethernet.etherType = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ethernet.etherType = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->ethernet.ebpf_valid = 1;
Expand All @@ -117,6 +116,7 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
}

accept:
hdrMd->ebpf_packetOffsetInBits = ebpf_packetOffsetInBits
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct headers_t {
struct hdr_md {
struct headers_t cpumap_hdr;
struct main_metadata_t cpumap_usermeta;
unsigned ebpf_packetOffsetInBits;
__u8 __hook;
};

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ $TC p4template create table/default_hit_const_example/MainControlImpl/set_ct_opt
table_acts act name default_hit_const_example/MainControlImpl/tcp_syn_packet \
act name default_hit_const_example/MainControlImpl/tcp_fin_or_rst_packet \
act name default_hit_const_example/MainControlImpl/tcp_other_packets
$TC p4template update table/default_hit_const_example/MainControlImpl/set_ct_options default_hit_action permissions 0x109 action default_hit_const_example/MainControlImpl/tcp_fin_or_rst_packet
$TC p4template update table/default_hit_const_example/MainControlImpl/set_ct_options default_miss_action permissions 0x109 action default_hit_const_example/MainControlImpl/tcp_other_packets
$TC p4template update table/default_hit_const_example/MainControlImpl/set_ct_options default_hit_action permissions 0x1024 action default_hit_const_example/MainControlImpl/tcp_fin_or_rst_packet
$TC p4template update table/default_hit_const_example/MainControlImpl/set_ct_options default_miss_action permissions 0x1024 action default_hit_const_example/MainControlImpl/tcp_other_packets
$TC p4template update pipeline/default_hit_const_example state ready
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ int xdp_func(struct xdp_md *skb) {
}
static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__)
{
unsigned ebpf_packetOffsetInBits = 0;
unsigned ebpf_packetOffsetInBits_save = 0;
unsigned ebpf_packetOffsetInBits = hdrMd->ebpf_packetOffsetInBits;
ParserError_t ebpf_errorCode = NoError;
void* pkt = ((void*)(long)skb->data);
void* ebpf_packetEnd = ((void*)(long)skb->data_end);
Expand Down Expand Up @@ -212,6 +211,7 @@ if (/* hdr->ipv4.isValid() */
}
;
}
hdrMd->ebpf_packetOffsetInBits = ebpf_packetOffsetInBits
return -1;
}
SEC("classifier/tc-ingress")
Expand Down
34 changes: 17 additions & 17 deletions testdata/p4tc_samples_outputs/default_hit_const_example_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ REGISTER_END()
static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__)
{
unsigned ebpf_packetOffsetInBits = 0;
unsigned ebpf_packetOffsetInBits_save = 0;
ParserError_t ebpf_errorCode = NoError;
void* pkt = ((void*)(long)skb->data);
void* ebpf_packetEnd = ((void*)(long)skb->data_end);
Expand Down Expand Up @@ -49,16 +48,16 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
hdr->ipv4.diffserv = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 8;

hdr->ipv4.totalLen = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.totalLen = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->ipv4.identification = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.identification = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->ipv4.flags = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 5) & EBPF_MASK(u8, 3));
ebpf_packetOffsetInBits += 3;

hdr->ipv4.fragOffset = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u16, 13));
hdr->ipv4.fragOffset = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u16, 13));
ebpf_packetOffsetInBits += 13;

hdr->ipv4.ttl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))));
Expand All @@ -67,13 +66,13 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
hdr->ipv4.protocol = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 8;

hdr->ipv4.hdrChecksum = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.hdrChecksum = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->ipv4.srcAddr = (u32)((load_word_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.srcAddr = (u32)((load_word(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 32;

hdr->ipv4.dstAddr = (u32)((load_word_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->ipv4.dstAddr = (u32)((load_word(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 32;

hdr->ipv4.ebpf_valid = 1;
Expand All @@ -92,16 +91,16 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
goto reject;
}

hdr->tcp.srcPort = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->tcp.srcPort = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->tcp.dstPort = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->tcp.dstPort = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->tcp.seqNo = (u32)((load_word_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->tcp.seqNo = (u32)((load_word(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 32;

hdr->tcp.ackNo = (u32)((load_word_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->tcp.ackNo = (u32)((load_word(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 32;

hdr->tcp.dataOffset = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 4) & EBPF_MASK(u8, 4));
Expand All @@ -113,13 +112,13 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
hdr->tcp.flags = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 8;

hdr->tcp.window = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->tcp.window = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->tcp.checksum = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->tcp.checksum = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->tcp.urgentPtr = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->tcp.urgentPtr = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->tcp.ebpf_valid = 1;
Expand All @@ -134,13 +133,13 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
goto reject;
}

hdr->eth.dstAddr = (u64)((load_dword_ne(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48));
hdr->eth.dstAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48));
ebpf_packetOffsetInBits += 48;

hdr->eth.srcAddr = (u64)((load_dword_ne(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48));
hdr->eth.srcAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48));
ebpf_packetOffsetInBits += 48;

hdr->eth.etherType = (u16)((load_half_ne(pkt, BYTES(ebpf_packetOffsetInBits))));
hdr->eth.etherType = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))));
ebpf_packetOffsetInBits += 16;

hdr->eth.ebpf_valid = 1;
Expand All @@ -163,6 +162,7 @@ static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *h
}

accept:
hdrMd->ebpf_packetOffsetInBits = ebpf_packetOffsetInBits
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct headers_t {
struct hdr_md {
struct headers_t cpumap_hdr;
struct metadata_t cpumap_usermeta;
unsigned ebpf_packetOffsetInBits;
__u8 __hook;
};

Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ int xdp_func(struct xdp_md *skb) {
}
static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__)
{
unsigned ebpf_packetOffsetInBits = 0;
unsigned ebpf_packetOffsetInBits_save = 0;
unsigned ebpf_packetOffsetInBits = hdrMd->ebpf_packetOffsetInBits;
ParserError_t ebpf_errorCode = NoError;
void* pkt = ((void*)(long)skb->data);
void* ebpf_packetEnd = ((void*)(long)skb->data_end);
Expand Down Expand Up @@ -290,6 +289,7 @@ if (/* hdr->ipv4.isValid() */
}
;
}
hdrMd->ebpf_packetOffsetInBits = ebpf_packetOffsetInBits
return -1;
}
SEC("classifier/tc-ingress")
Expand Down
Loading

0 comments on commit 1406aa6

Please sign in to comment.