Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[P4TC] Align key components to the nearest 8-bit size #5024

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

komaljai
Copy link
Contributor

@komaljai komaljai commented Nov 21, 2024

As of now, whenever there is a key component that is a bit-type which doesn't correspond to primitive C type (u8, u16, u32, ...),
the compiler generates it as using the closest primitive C type that is larger then X in bit
For example -

bit<24> dest_op;

dest_op here is converted into u32 like below -

u32 dest_op;

This won't work, because the P4TC side is expecting the bit<24> (dest_op) to be contained in 3 bytes and not 4. All key components are expected to be 8-bit aligned. The new declaration for 'dest_op' would be -

u8 dest_op[3];

For all operations, compiler will call function 'getPrimitiveXX()' which will convert u8[] into primitive type (u32 or u64).
For reverse assignment operations compiler calls 'storePrimitiveXX()' which converts primitive types to u8 [ ] array.

@vbnogueira @jhsmt

@komaljai komaljai added the p4tc Topics related to the P4-TC back end label Nov 21, 2024
Copy link
Contributor

@vbnogueira vbnogueira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small compilation issues
Will do another round after running more complex tests

u32 select_0;
select_0 = (((((u32)(((u16)tmp_0.p << 8) | ((u16)tmp_2.four & 0xff)) << 8) & ((1 << 24) - 1)) | (((u32)tmp_4.ver & 0xff) & ((1 << 24) - 1))) & ((1 << 24) - 1));
u8 select_0[3];
storePrimitive32((u8 *)&select_0, 24, ((((((u32)(((u16)tmp_0.p << 8) | ((u16)tmp_2.four & 0xff)) << 8) & ((1 << 24) - 1)) | (((u32)tmp_4.ver & 0xff) & ((1 << 24) - 1))) & ((1 << 24) - 1))));
if (select_0 == 0x503401)goto parse_p4calc;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I think you need to convert select_0 back to a u32 or do a byte by byte comparison.
Since select_0 is a u8 array, the compiler is complaining

@@ -56,3 +56,48 @@ REGISTER_TABLE(hdr_md_cpumap, BPF_MAP_TYPE_PERCPU_ARRAY, u32, struct hdr_md, 2)
BPF_ANNOTATE_KV_PAIR(hdr_md_cpumap, u32, struct hdr_md)
REGISTER_END()

u32 getPrimitive32(u8 *a, int size) {
if(size <=32 || size >=56) {
error("Invalid size.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler is complaining about this also

warning: call to undeclared function 'error'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        error("Invalid size.");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4tc Topics related to the P4-TC back end
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants