Skip to content

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

Open
komaljai wants to merge 1 commit intomainfrom
align_key_bytewise
Open

Align key components to the nearest 8-bit size#30
komaljai wants to merge 1 commit intomainfrom
align_key_bytewise

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

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

}

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 3 times, most recently from 998f749 to ffca7b6 Compare January 7, 2025 06:20
@komaljai komaljai force-pushed the align_key_bytewise branch 2 times, most recently from 3f9f128 to 1234582 Compare January 16, 2025 13:12
Signed-off-by: Komal, Jain <komal.jain@intel.com>
@komaljai komaljai force-pushed the align_key_bytewise branch 2 times, most recently from 85c46fd to ff5d749 Compare February 6, 2025 12:00
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.

3 participants