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

testdata/p4tc_samples_outputs/calculator_parser.c Outdated Show resolved Hide resolved
testdata/p4tc_samples_outputs/calculator_parser.h Outdated Show resolved Hide resolved
backends/tc/ebpfCodeGen.cpp Outdated Show resolved Hide resolved
backends/tc/ebpfCodeGen.cpp Outdated Show resolved Hide resolved
backends/tc/ebpfCodeGen.cpp Outdated Show resolved Hide resolved
backends/tc/ebpfCodeGen.cpp Outdated Show resolved Hide resolved
@vbnogueira
Copy link
Contributor

As we discussed on the meeting, here's an example that shows the sum bug sum_example.tar.gz
You'll see the bug in generated/sum_example_control_blocks.c line 85

@komaljai komaljai force-pushed the align_key_bytewise branch 2 times, most recently from cb20ec1 to e5a365e Compare December 12, 2024 10:27
@vbnogueira
Copy link
Contributor

The PR looks ok, but can you rebase on top of main when you have some time?
There are fixes in the merged skb metadata PR which I need to test this PR with

@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 requested review from vbnogueira and fruffy and removed request for vbnogueira January 7, 2025 06:20
@komaljai
Copy link
Contributor Author

Hi @vbnogueira , @fruffy , Can you review the PR once?

@vbnogueira
Copy link
Contributor

Hi @vbnogueira , @fruffy , Can you review the PR once?

Sorry, we got a bit overwhelmed, but will do today

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.

Aside from these small issues, the PR seems ok

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.

LGTM

@komaljai komaljai requested a review from jafingerhut January 23, 2025 14:25
@komaljai
Copy link
Contributor Author

Hi @jafingerhut / @fruffy Could you review and approve the PR?

@jafingerhut
Copy link
Contributor

I have no objections to merging these changes, as long as whoever is a stakeholder in the EBPF and P4TC backends is OK with them. I am currently so unfamiliar with the EBPF and P4TC back ends that I don't think I can review C++ code changes effectively. I took a quick look at the changes in the P4TC expected output files, and it appears the changes described in the initial comment of this PR have been made there.

Is there currently any automated testing of the P4TC back end, e.g. tests that compile a P4 program, load it into the kernel, add some table entries, send some packets in, and check that the expected packets come out (and no others)? If not, is anyone considering developing such automated tests as a way to test changes like this?

I see that this PR makes some changes to some ebpf p4c files, but does not make any changes to any expected EBPF output files. Is that because there are no EBPF expected output files in the p4c repo? Or because this PR should have no effect on the output of the p4c-ebpf compiler?

@vbnogueira
Copy link
Contributor

Is there currently any automated testing of the P4TC back end, e.g. tests that compile a P4 program, load it into the kernel, add some table entries, send some packets in, and check that the expected packets come out (and no others)? If not, is anyone considering developing such automated tests as a way to test changes like this?

Yes, there is a PR we are currently finishing for that: #5011
In this case I also tested the PR manually as well

@jafingerhut
Copy link
Contributor

Hi @vbnogueira , @fruffy , Can you review the PR once?

So I don't have a lot of stake in personally using the P4TC back end at this time, and I am not the best person to review C++ code in p4c's internals.

You have one approving review from someone who is more knowledgeable of, and invested in, P4TC, and I personally think that is enough for merging (assuming all of the CI tests pass, the PR is up to date with the main branch code, etc.)

I am glad you are working towards adding more automated CI tests for the P4TC back end!

@vbnogueira
Copy link
Contributor

vbnogueira commented Jan 29, 2025

Hi @vbnogueira , @fruffy , Can you review the PR once?

So I don't have a lot of stake in personally using the P4TC back end at this time, and I am not the best person to review C++ code in p4c's internals.

You have one approving review from someone who is more knowledgeable of, and invested in, P4TC, and I personally think that is enough for merging (assuming all of the CI tests pass, the PR is up to date with the main branch code, etc.)

That's fair, but from what Komal told us she needs either your approval or Fabian's to be able to merge this PR

@jafingerhut
Copy link
Contributor

jafingerhut commented Jan 29, 2025

Hi @vbnogueira , @fruffy , Can you review the PR once?

So I don't have a lot of stake in personally using the P4TC back end at this time, and I am not the best person to review C++ code in p4c's internals.
You have one approving review from someone who is more knowledgeable of, and invested in, P4TC, and I personally think that is enough for merging (assuming all of the CI tests pass, the PR is up to date with the main branch code, etc.)

That's fair, but from what Komal told us she needs either your approval or Fabian's to be able to merge this PR

OK. @komajai I'm pretty good for reviewing changes to test cases, but not really for a thorough review of C++ code changes, except in very localized parts of the p4c repo, and not at all for ebpf or tc back ends.

If Fabian is willing to review and approve, then great. If not, we could ask if @thomascalvert-xlnx might be willing to do so? I see he has done at least some work on the EBPF and TC back ends, which is more than I can say for me.

@komaljai
Copy link
Contributor Author

komaljai commented Jan 29, 2025

Hi @vbnogueira , @fruffy , Can you review the PR once?

So I don't have a lot of stake in personally using the P4TC back end at this time, and I am not the best person to review C++ code in p4c's internals.
You have one approving review from someone who is more knowledgeable of, and invested in, P4TC, and I personally think that is enough for merging (assuming all of the CI tests pass, the PR is up to date with the main branch code, etc.)

That's fair, but from what Komal told us she needs either your approval or Fabian's to be able to merge this PR

OK. @komajai I'm pretty good for reviewing changes to test cases, but not really for a thorough review of C++ code changes, except in very localized parts of the p4c repo, and not at all for ebpf or tc back ends.

If Fabian is willing to review and approve, then great. If not, we could ask if @thomascalvert-xlnx might be willing to do so? I see he has done at least some work on the EBPF and TC back ends, which is more than I can say for me.

Hi @jafingerhut , This PR doesnt affect any ebpf testcases. And for the changes made in C++ code, there are testcases added along with the expected compiler output files. (The diff in output files reflect compiler changes due to current PR).
Like Victor mentioned above, he ran those testcases output files manually on his system as we don't have the automated framework to run testcases along with traffic yet for TC.

auto tcTarget = dynamic_cast<const P4TCTarget *>(builder->target);
auto ebpfType = EBPFTypeFactory::instance->create(ftype);
EBPFScalarType *scalar = nullptr;
if (ebpfType->is<EBPFScalarType>()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Recommend an return-based flow here instead of if else chains.

Or just a single visit(expression) call and a return in the !isPrimitive case.

return;
bool CodeGenInspector::storeBitAlignment(const IR::Type *ltype, const IR::Expression *lexpr,
cstring lpath) {
auto tcTarget = dynamic_cast<const P4TCTarget *>(builder->target);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not use dynamic cast, use P4C's handlers in lib/castable.h. The target should already be castable.

Copy link
Collaborator

@fruffy fruffy Jan 29, 2025

Choose a reason for hiding this comment

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

Actually, this code should be in eBPFCodeGen for TC, if it needs to be casted to a P4TCTarget.

return;
bool CodeGenInspector::storeBitAlignment(const IR::Type *ltype, const IR::Expression *lexpr,
cstring lpath) {
auto tcTarget = dynamic_cast<const P4TCTarget *>(builder->target);
Copy link
Collaborator

@fruffy fruffy Jan 29, 2025

Choose a reason for hiding this comment

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

Actually, this code should be in eBPFCodeGen for TC, if it needs to be casted to a P4TCTarget.

@@ -568,8 +567,19 @@ void CodeGenInspector::emitTCBinaryOperation(const IR::Operation_Binary *b, bool
return;
}

void CodeGenInspector::emitTCAssignmentEndianessConversion(const IR::Expression *lexpr,
const IR::Expression *rexpr) {
void CodeGenInspector::emitTCAssignmentEndianessConversion(const IR::Type *ltype,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, these functions should ideally be in the TC back end not here. Makes it much harder refactor this back end later on. I guess the problem is that there is no TC-specific codegen derived from the CodeGenInspector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It wasn't easy change as codeGenInspector is the base class for many classes in EBPF code, so we took this path to update in-place. But you pointed it correctly that refactoring of code would be difficult.

One solution @syogender suggested is that we treat CodeGenInspector as a base class for new class VendorCodeGenInspector. This new class will be define by each target in a separate file, which gets included depends on the target it is built for, that way we can have clean code with minimum intrusion.

Please suggest your inputs.

Copy link
Collaborator

@fruffy fruffy Jan 30, 2025

Choose a reason for hiding this comment

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

Yes that sounds great. I can approve this PR to unblock things and since there are already TC-specific functions in this class. But the next PRs should introduce such a refactoring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fruffy Could you approve the PR? I will create next PR with separate CodeGenInspector for TC.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Have you addressed the other comments?

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.

4 participants