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

Align key components to the nearest 8-bit size #30

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

komaljai
Copy link
Owner

@komaljai komaljai commented Oct 3, 2024

No description provided.

@komaljai komaljai changed the title Align key components to the nearest 8-bit size [Draft] - Align key components to the nearest 8-bit size Oct 3, 2024
Copy link

@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.

I found some small issues, but the general idea of storePrimitive* and getPrimitive* makes sense

However, I have a question.
In cases where you have something like a bit<22>.
Is getPrimitive32's first parameter guaranteed to be zeroed out?
Analogously, is the second parameter of storePrimitive32 (u32 value) guaranteed to have the remaining 10 bits zeroed out?

If it is, I think there is no issue, otherwise masking or shifting should be necessary

testdata/p4tc_samples_outputs/add_entry_1_example_parser.h Outdated Show resolved Hide resolved
testdata/p4tc_samples_outputs/add_entry_1_example_parser.h Outdated Show resolved Hide resolved
testdata/p4tc_samples_outputs/add_entry_3_example_parser.h Outdated Show resolved Hide resolved
testdata/p4tc_samples_outputs/add_entry_3_example_parser.h Outdated Show resolved Hide resolved
testdata/p4tc_samples_outputs/add_entry_3_example_parser.h Outdated Show resolved Hide resolved
@@ -114,8 +114,8 @@ if (/* hdr->ipv4.isValid() */
break;
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_SEND_NH:
{
hdr->ethernet.srcAddr = bpf_cpu_to_be64(value->u.MainControlImpl_send_nh.smac);
hdr->ethernet.dstAddr = ntohll(value->u.MainControlImpl_send_nh.dmac << 16);
storePrimitive64(&hdr->ethernet.srcAddr, 48, (bpf_cpu_to_be64(value->u.MainControlImpl_send_nh.smac)));

Choose a reason for hiding this comment

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

I think I accidentally found a bug
Was testing changing EthernetAddress' typecast[1] to a bit<39> and saw that the generated code was outputing this line as:

storePrimitive64(&hdr->ethernet.srcAddr, 40, (bpf_cpu_to_be64((u64)(u64)value->u.MainControlImpl_send_nh.smac)));

From what I understood, the second argument should be 39, not 40

[1] https://github.com/p4lang/p4c/blob/main/testdata/p4tc_samples/add_entry_1_example.p4#L4

Copy link
Owner Author

Choose a reason for hiding this comment

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

I am unable to reproduce this issue, If I change srcaddr to bit<39>, I get error about invalid ethernet packet size.

@komaljai komaljai changed the title [Draft] - Align key components to the nearest 8-bit size Align key components to the nearest 8-bit size Nov 18, 2024
Copy link

@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.

I can't show this in the diff because it wasn't changed in this PR [1], however it is causing a compilation error:

1 warning generated.                                                  
/home/vagrant/p4c/backends/tc/runtime/tmpn_esd7rd/add_entry_1_example_control_blocks.c:98:88: error: array type 'u8[6]' (aka 'unsigned char[6]') is not assignable
                                    update_act_bpf_val->u.MainControlImpl_send_nh.dmac = hdr->ethernet.dstAddr;

A similar issue happens in [2]

[1] https://github.com/komaljai/p4c/blob/align_key_bytewise/testdata/p4tc_samples_outputs/add_entry_1_example_control_blocks.c#L96
[2] https://github.com/komaljai/p4c/blob/align_key_bytewise/testdata/p4tc_samples_outputs/add_entry_3_example_control_blocks.c#L96

@komaljai komaljai force-pushed the align_key_bytewise branch 3 times, most recently from 9635f71 to 295a6ce Compare November 21, 2024 13:15
Copy link

@psivanup psivanup left a comment

Choose a reason for hiding this comment

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

Please check the nice to have comments.

@@ -238,6 +238,11 @@ class P4TCTarget : public KernelSamplesTarget {
}
return "HOST"_cs;
}

bool isPrimitiveByteAligned(int width) const {
return (width <= 8 || width <= 16 || (width > 24 && width <= 32) ||

Choose a reason for hiding this comment

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

Looks width <= 8 check is redundant

Suggested change
return (width <= 8 || width <= 16 || (width > 24 && width <= 32) ||
return (width <= 16 || (width > 24 && width <= 32) ||


public:
explicit EBPFScalarTypePNA(const IR::Type_Bits *bits) : EBPFScalarType(bits) {
isPrimitiveByteAligned = (width <= 8 || width <= 16 || (width > 24 && width <= 32) ||

Choose a reason for hiding this comment

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

Suggested change
isPrimitiveByteAligned = (width <= 8 || width <= 16 || (width > 24 && width <= 32) ||
isPrimitiveByteAligned = (width <= 16 || (width > 24 && width <= 32) ||

if (generatesScalar(width) && isPrimitiveByteAligned == true) {
builder->append("0");
} else {
builder->append("{ 0 }");

Choose a reason for hiding this comment

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

may be you can add a comment that array initializer is emitted for non byte aligned primitive types as they are emitted as array type.

: "getPrimitive64"_cs;
builder->appendFormat("%v((u8 *)", getPrimitive);
visit(expression);
builder->appendFormat(", %d)", scalar->implementationWidthInBits());

Choose a reason for hiding this comment

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

Suggested change
builder->appendFormat(", %d)", scalar->implementationWidthInBits());
builder->appendFormat(", %d)", scalar->implementationWidthInBits());
return;

By adding return to the above code, you can replace all else blocks below with just one visit(expression).

@komaljai komaljai force-pushed the align_key_bytewise branch 2 times, most recently from cb20ec1 to e5a365e Compare December 12, 2024 10:27
@komaljai komaljai force-pushed the align_key_bytewise branch 2 times, most recently from bfa6b0c to 998f749 Compare January 6, 2025 09:53
@komaljai komaljai force-pushed the align_key_bytewise branch from 998f749 to ffca7b6 Compare January 7, 2025 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants