Skip to content

Commit

Permalink
Small TC fixes (p4lang#5002)
Browse files Browse the repository at this point in the history
* Fix action parameter declaration in p4tc_filter_fields in tc_may_override cases

In cases where the tc_may_override annotation is associate with an
action that has a parameter with a tc_type annotation, the compiler
is emitting the parameters declaration in p4tc_filter_fields incorrectly.
For example in tc_may_override_example_04:

struct p4tc_filter_fields {
    __u32 pipeid;
    __u32 handle;
    __u32 classid;
    __u32 chain;
    __u32 blockid;
    __be16 proto;
    __u16 prio;
    ipv4 ipv4_tbl_1_next_hop_ipv4addr;
};

"ipv4" is not a valid C type, so this won't compile

Also, in the case of bit types that are not equivalent to a standard C
type (u8, u16, u32, u64), the compiler is emitting the type incorrectly.
Let's say, in the previous example, that the type for ipv4addr was bit<33>,
the compiler would've emitted the following:

struct p4tc_filter_fields {
    __u32 pipeid;
    __u32 handle;
    __u32 classid;
    __u32 chain;
    __u32 blockid;
    __be16 proto;
    __u16 prio;
    __u33 ipv4_tbl_1_next_hop_ipv4addr;
};

This commit fixes this by converting tc_type annotated action parameters
to their corresponding C type and converts any non C standard bit type
into a u8 array rounded up to the nearest byte

Signed-off-by: Victor Nogueira <[email protected]>

* Use profile_id instead of aging_ms

In struct p4tc_table_entry_create_bpf_params__local we've adopted
profile_id instead of aging_ms. This commits updates the field name in
the generated output

Signed-off-by: Victor Nogueira <[email protected]>

* Update pna.h

Add missing kfuncs and remove some deprecated ones

Signed-off-by: Victor Nogueira <[email protected]>

---------

Signed-off-by: Victor Nogueira <[email protected]>
  • Loading branch information
vbnogueira authored Nov 11, 2024
1 parent 6f7353f commit dbe0f7e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 39 deletions.
10 changes: 5 additions & 5 deletions backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ void PNAEbpfGenerator::emitP4TCActionParam(EBPF::CodeBuilder *builder) const {
for (auto param : table->defaultMissActionParams) {
cstring paramName = param->paramDetail->getParamName();
cstring placeholder = tblName + "_" + defaultActionName + "_" + paramName;
cstring typeName = param->paramDetail->getParamType();
cstring paramDecl = param->paramDetail->getParamDecl(placeholder);
builder->emitIndent();
builder->appendFormat("%v %v", typeName, placeholder);
builder->append(paramDecl);
builder->endOfStatement(true);
}
}
Expand All @@ -149,9 +149,9 @@ void PNAEbpfGenerator::emitP4TCActionParam(EBPF::CodeBuilder *builder) const {
for (auto param : table->defaultHitActionParams) {
cstring paramName = param->paramDetail->getParamName();
cstring placeholder = tblName + "_" + defaultActionName + "_" + paramName;
cstring typeName = param->paramDetail->getParamType();
cstring paramDecl = param->paramDetail->getParamDecl(placeholder);
builder->emitIndent();
builder->appendFormat("%v %v", typeName, placeholder);
builder->append(paramDecl);
builder->endOfStatement(true);
}
}
Expand Down Expand Up @@ -1679,7 +1679,7 @@ void ControlBodyTranslatorPNA::processFunction(const P4::ExternFunction *functio
builder->appendFormat(" .tblid = %d,", tblId);
builder->newline();
builder->emitIndent();
builder->append(" .aging_ms = ");
builder->append(" .profile_id = ");
for (auto a : *function->expr->arguments) {
visit(a);
}
Expand Down
45 changes: 29 additions & 16 deletions backends/tc/runtime/pna.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct clone_session_entry {
struct p4tc_table_entry_act_bpf_params__local {
u32 pipeid;
u32 tblid;
} __attribute__((preserve_access_index));
};

struct __attribute__((__packed__)) p4tc_table_entry_act_bpf {
u32 act_id;
Expand Down Expand Up @@ -160,6 +160,19 @@ xdp_p4tc_entry_create_on_miss(struct xdp_md *xdp_ctx,
const u32 params__sz,
void *key, const u32 key__sz) __ksym;

/* No mapping to PNA, but are useful utilities */
extern int
bpf_p4tc_entry_update(struct __sk_buff *skb_ctx,
struct p4tc_table_entry_create_bpf_params__local *params,
const u32 params__sz,
void *key, const u32 key__sz) __ksym;

extern int
xdp_p4tc_entry_update(struct xdp_md *xdp_ctx,
struct p4tc_table_entry_create_bpf_params__local *params,
const u32 params__sz,
void *key, const u32 key__sz) __ksym;

/* No mapping to PNA, but are useful utilities */
extern int
bpf_p4tc_entry_delete(struct __sk_buff *skb_ctx,
Expand Down Expand Up @@ -200,33 +213,33 @@ struct p4tc_ext_bpf_val {
/* Equivalent to PNA indirect counters */
extern int
bpf_p4tc_extern_count_pktsnbytes(struct __sk_buff *skb_ctx,
struct p4tc_ext_bpf_params *params,
const u32 params__sz, void *key, const u32 key__sz) __ksym;
struct p4tc_ext_bpf_params *params,
const u32 params__sz, void *key, const u32 key__sz) __ksym;

extern int
bpf_p4tc_extern_count_pkts(struct __sk_buff *skb_ctx,
struct p4tc_ext_bpf_params *params,
const u32 params__sz, void *key, const u32 key__sz) __ksym;
struct p4tc_ext_bpf_params *params,
const u32 params__sz, void *key, const u32 key__sz) __ksym;

extern int
bpf_p4tc_extern_count_bytes(struct __sk_buff *skb_ctx,
struct p4tc_ext_bpf_params *params,
const u32 params__sz, void *key, const u32 key__sz) __ksym;
struct p4tc_ext_bpf_params *params,
const u32 params__sz, void *key, const u32 key__sz) __ksym;

extern int
xdp_p4tc_extern_indirect_count_pktsnbytes(struct xdp_md *xdp_ctx,
struct p4tc_ext_bpf_params *params,
const u32 params__sz) __ksym;
xdp_p4tc_extern_count_pktsnbytes(struct xdp_md *xdp_ctx,
struct p4tc_ext_bpf_params *params,
const u32 params__sz) __ksym;

extern int
xdp_p4tc_extern_indirect_count_pktsonly(struct xdp_md *xdp_ctx,
struct p4tc_ext_bpf_params *params,
const u32 params__sz) __ksym;
xdp_p4tc_extern_count_pkts(struct xdp_md *xdp_ctx,
struct p4tc_ext_bpf_params *params,
const u32 params__sz) __ksym;

extern int
xdp_p4tc_extern_indirect_count_bytesonly(struct xdp_md *xdp_ctx,
struct p4tc_ext_bpf_params *params,
const u32 params__sz) __ksym;
xdp_p4tc_extern_count_bytes(struct xdp_md *xdp_ctx,
struct p4tc_ext_bpf_params *params,
const u32 params__sz) __ksym;

extern int bpf_p4tc_extern_meter_bytes_color(struct __sk_buff *skb_ctx,
struct p4tc_ext_bpf_params *params,
Expand Down
30 changes: 18 additions & 12 deletions backends/tc/tc.def
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,41 @@ class TCActionParam {
unsigned getDirection() const {
return direction;
}
cstring getParamType() const {
std::string paramType = "";
cstring getParamDecl(cstring placeholderName) const {
std::string paramDecl = "";
switch(dataType) {
case TC::BIT_TYPE :
paramType = absl::StrCat("__u", bitSize);
if (bitSize == 8 || bitSize == 16 || bitSize == 32 || bitSize == 64) {
paramDecl = absl::StrCat("__u", bitSize, " ", placeholderName);
} else {
unsigned byteSize = bitSize / 8 + (bitSize % 8 != 0);

paramDecl = absl::StrCat("__u8 ", placeholderName, "[", byteSize, "]");
}
break;
case TC::DEV_TYPE :
paramType = "dev";
paramDecl = absl::StrCat("__u32 ", placeholderName);
break;
case TC::MACADDR_TYPE :
paramType = "macaddr";
paramDecl = absl::StrCat("__u8 ", placeholderName, "[6]");
break;
case TC::IPV4_TYPE :
paramType = "ipv4";
paramDecl = absl::StrCat("__u32 ", placeholderName);
break;
case TC::IPV6_TYPE :
paramType = "ipv6";
paramDecl = absl::StrCat("__u8 ", placeholderName, "[16]");
break;
case TC::BE16_TYPE :
paramType = "__be16";
paramDecl = absl::StrCat("__be16 ", placeholderName);
break;
case TC::BE32_TYPE :
paramType = "__be32";
paramDecl = absl::StrCat("__be32 ", placeholderName);
break;
case TC::BE64_TYPE :
paramType = "__be64";
paramDecl = absl::StrCat("__be64 ", placeholderName);
break;
}
return paramType;
}
return paramDecl;
}
toString {
std::string tcActionParam = absl::StrCat("\n\tparam ", paramName, " type ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ if (/* hdr->ipv4.isValid() */
struct p4tc_table_entry_create_bpf_params__local update_params = {
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 1,
.aging_ms = 2
.profile_id = 2
};
bpf_p4tc_entry_update(skb, &update_params, sizeof(params), &key, sizeof(key));
}
Expand Down Expand Up @@ -169,7 +169,7 @@ if (/* hdr->ipv4.isValid() */
struct p4tc_table_entry_create_bpf_params__local update_params = {
.pipeid = p4tc_filter_fields.pipeid,
.tblid = 2,
.aging_ms = 2
.profile_id = 2
};
bpf_p4tc_entry_update(skb, &update_params, sizeof(params), &key, sizeof(key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct p4tc_filter_fields {
__u32 blockid;
__be16 proto;
__u16 prio;
ipv4 ipv4_tbl_1_next_hop_ipv4addr;
__u32 ipv4_tbl_1_next_hop_ipv4addr;
};

REGISTER_START()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct p4tc_filter_fields {
__u32 blockid;
__be16 proto;
__u16 prio;
dev ipv4_tbl_1_next_hop_vport;
__u32 ipv4_tbl_1_next_hop_vport;
};

REGISTER_START()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ struct p4tc_filter_fields {
__u32 blockid;
__be16 proto;
__u16 prio;
dev ipv4_tbl_1_next_hop_vport;
dev ipv4_tbl_2_next_hop_vport;
__u32 ipv4_tbl_1_next_hop_vport;
__u32 ipv4_tbl_2_next_hop_vport;
};

REGISTER_START()
Expand Down

0 comments on commit dbe0f7e

Please sign in to comment.