Skip to content

Commit

Permalink
tc struct fields addition and memory initialization (p4lang#4524)
Browse files Browse the repository at this point in the history
* Addition of fields in table key value structure in tc backend

* Memory initialization of key structure

* Updated testcase outputs
  • Loading branch information
Sosutha authored Mar 13, 2024
1 parent b256c2a commit 98d166f
Show file tree
Hide file tree
Showing 29 changed files with 357 additions and 114 deletions.
20 changes: 18 additions & 2 deletions backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,18 @@ void EBPFTablePNA::emitValueStructStructure(EBPF::CodeBuilder *builder) {
builder->append("unsigned int action;");
builder->newline();

builder->emitIndent();
builder->append("u32 hit:1,");
builder->newline();

builder->emitIndent();
builder->append("is_default_miss_act:1,");
builder->newline();

builder->emitIndent();
builder->append("is_default_hit_act:1;");
builder->newline();

if (isTernaryTable()) {
builder->emitIndent();
builder->append("__u32 priority;");
Expand Down Expand Up @@ -1501,7 +1513,11 @@ void ControlBodyTranslatorPNA::processApply(const P4::ApplyMethod *method) {
builder->emitIndent();
builder->appendLine("};");
builder->emitIndent();
builder->appendFormat("struct %s %s = {}", table->keyTypeName.c_str(), keyname.c_str());
builder->appendFormat("struct %s %s", table->keyTypeName.c_str(), keyname.c_str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("__builtin_memset(&%s, 0, sizeof(%s))", keyname.c_str(),
keyname.c_str());
builder->endOfStatement(true);
unsigned int keysize = tcIR->getTableKeysize(tblId);
builder->emitIndent();
Expand Down Expand Up @@ -1605,7 +1621,7 @@ void ControlBodyTranslatorPNA::processApply(const P4::ApplyMethod *method) {
builder->append(" else ");
builder->blockStart();
builder->emitIndent();
builder->appendFormat("%s = 1", control->hitVariable.c_str());
builder->appendFormat("%s = value->hit", control->hitVariable.c_str());
builder->endOfStatement(true);
builder->blockEnd(true);

Expand Down
3 changes: 3 additions & 0 deletions backends/tc/runtime/pna.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ struct p4tc_table_entry_act_bpf_params__local {

struct __attribute__((__packed__)) p4tc_table_entry_act_bpf {
u32 act_id;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
u8 params[124];
};

Expand Down
8 changes: 6 additions & 2 deletions testdata/p4tc_samples_outputs/calculator_control_blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ struct __attribute__((__packed__)) MainControlImpl_calculate_key {
#define MAINCONTROLIMPL_CALCULATE_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_calculate_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand Down Expand Up @@ -72,7 +75,8 @@ if (/* hdr->p4calc.isValid() */
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 1
};
struct MainControlImpl_calculate_key key = {};
struct MainControlImpl_calculate_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 8;
key.field0 = hdr->p4calc.op;
struct p4tc_table_entry_act_bpf *act_bpf;
Expand All @@ -85,7 +89,7 @@ if (/* hdr->p4calc.isValid() */
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down
8 changes: 6 additions & 2 deletions testdata/p4tc_samples_outputs/checksum_control_blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct __attribute__((__packed__)) ingress_nh_table_key {
#define INGRESS_NH_TABLE_ACT_NOACTION 0
struct __attribute__((__packed__)) ingress_nh_table_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand Down Expand Up @@ -60,7 +63,8 @@ static __always_inline int process(struct __sk_buff *skb, struct my_ingress_head
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 1
};
struct ingress_nh_table_key key = {};
struct ingress_nh_table_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 32;
key.field0 = hdr->ipv4.srcAddr;
struct p4tc_table_entry_act_bpf *act_bpf;
Expand All @@ -73,7 +77,7 @@ static __always_inline int process(struct __sk_buff *skb, struct my_ingress_head
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct __attribute__((__packed__)) MainControlImpl_t_range_key {
#define MAINCONTROLIMPL_T_RANGE_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_t_range_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand Down Expand Up @@ -58,7 +61,8 @@ static __always_inline int process(struct __sk_buff *skb, struct Header_t *h, st
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 1
};
struct MainControlImpl_t_range_key key = {};
struct MainControlImpl_t_range_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 8;
key.field0 = h->h.r;
struct p4tc_table_entry_act_bpf *act_bpf;
Expand All @@ -71,7 +75,7 @@ static __always_inline int process(struct __sk_buff *skb, struct Header_t *h, st
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_key {
#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand All @@ -38,6 +41,9 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_key {
#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand Down Expand Up @@ -83,7 +89,8 @@ if (/* hdr->ipv4.isValid() */
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 1
};
struct MainControlImpl_ipv4_tbl_1_key key = {};
struct MainControlImpl_ipv4_tbl_1_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 64;
key.field0 = hdr->ipv4.dstAddr;
key.field1 = skb->ifindex;
Expand All @@ -97,7 +104,7 @@ if (/* hdr->ipv4.isValid() */
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down Expand Up @@ -131,7 +138,8 @@ if (/* hdr->ipv4.isValid() */
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 2
};
struct MainControlImpl_ipv4_tbl_2_key key = {};
struct MainControlImpl_ipv4_tbl_2_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 72;
key.field0 = hdr->ipv4.dstAddr;
key.field1 = hdr->ipv4.srcAddr;
Expand All @@ -146,7 +154,7 @@ if (/* hdr->ipv4.isValid() */
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_key {
#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand All @@ -37,6 +40,9 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_key {
#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand Down Expand Up @@ -83,7 +89,8 @@ static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr,
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 1
};
struct MainControlImpl_ipv4_tbl_1_key key = {};
struct MainControlImpl_ipv4_tbl_1_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 64;
key.field0 = hdr->ipv4.dstAddr;
key.field1 = skb->ifindex;
Expand All @@ -97,7 +104,7 @@ static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr,
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down Expand Up @@ -131,7 +138,8 @@ static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr,
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 2
};
struct MainControlImpl_ipv4_tbl_2_key key = {};
struct MainControlImpl_ipv4_tbl_2_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 72;
key.field0 = hdr->ipv4.dstAddr;
key.field1 = hdr->ipv4.srcAddr;
Expand All @@ -146,7 +154,7 @@ static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr,
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_key {
#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand All @@ -38,6 +41,9 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_key {
#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand Down Expand Up @@ -83,7 +89,8 @@ if (/* hdr->ipv4.isValid() */
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 1
};
struct MainControlImpl_ipv4_tbl_1_key key = {};
struct MainControlImpl_ipv4_tbl_1_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 64;
key.field0 = hdr->ipv4.dstAddr;
key.field1 = skb->ifindex;
Expand All @@ -97,7 +104,7 @@ if (/* hdr->ipv4.isValid() */
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down Expand Up @@ -131,7 +138,8 @@ if (/* hdr->ipv4.isValid() */
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 2
};
struct MainControlImpl_ipv4_tbl_2_key key = {};
struct MainControlImpl_ipv4_tbl_2_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 72;
key.field0 = hdr->ipv4.dstAddr;
key.field1 = hdr->ipv4.srcAddr;
Expand All @@ -146,7 +154,7 @@ if (/* hdr->ipv4.isValid() */
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ struct MainControlImpl_set_ct_options_key_mask {
#define MAINCONTROLIMPL_SET_CT_OPTIONS_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_set_ct_options_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
__u32 priority;
union {
struct {
Expand Down Expand Up @@ -68,7 +71,8 @@ if (/* hdr->ipv4.isValid() */
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 1
};
struct MainControlImpl_set_ct_options_key key = {};
struct MainControlImpl_set_ct_options_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 8;
key.field0 = hdr->tcp.flags;
struct p4tc_table_entry_act_bpf *act_bpf;
Expand All @@ -81,7 +85,7 @@ if (/* hdr->ipv4.isValid() */
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_key {
#define MAINCONTROLIMPL_IPV4_TBL_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_value {
unsigned int action;
u32 hit:1,
is_default_miss_act:1,
is_default_hit_act:1;
union {
struct {
} _NoAction;
Expand Down Expand Up @@ -60,7 +63,8 @@ if (/* hdr->ipv4.isValid() */
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 1
};
struct MainControlImpl_ipv4_tbl_key key = {};
struct MainControlImpl_ipv4_tbl_key key;
__builtin_memset(&key, 0, sizeof(key));
key.keysz = 32;
key.field0 = hdr->ipv4.dstAddr;
struct p4tc_table_entry_act_bpf *act_bpf;
Expand All @@ -73,7 +77,7 @@ if (/* hdr->ipv4.isValid() */
/* miss; find default action */
hit = 0;
} else {
hit = 1;
hit = value->hit;
}
if (value != NULL) {
/* run action */
Expand Down
Loading

0 comments on commit 98d166f

Please sign in to comment.