Skip to content

Conversation

zb-gong
Copy link
Contributor

@zb-gong zb-gong commented Jul 31, 2025

Add support for generating rt-addr:

  1. There is a duplicate space when generating ynl_cpp::ynl_socket& ys.
  2. Current implementation uses reference instead of pointer as function arguments, so member access should use . instead of ->
  3. operation can have empty attr_list with a fixed header. In that case, the struct should be generated as well.
  4. rt-addr uses protocol of netlink-raw instead of netlink. netlink-raw is properly handled in this pr.
  5. We add a simple rt-addr program in samples to check the correctness.

The other changes are auto-formatted by vscode.

@zb-gong zb-gong changed the title Duplicate space ynl-gen-cpp: add support for generating rt-addr Jul 31, 2025
@zb-gong zb-gong force-pushed the cpp-feature branch 9 times, most recently from 518e170 to 73012a4 Compare August 7, 2025 04:31
@kuba-moo
Copy link
Contributor

kuba-moo commented Aug 7, 2025

Could you rebase and reorder these two patches? Add the cleanup first? There are some merge marker removals there which TBH I don't fully understand (as in I don't see where they were commited but now they are being removed).

The commits should follow the Linux kernel format:

one line summary

Full description of the reason / goal of the changes.

Signed-off-by: ...

@zb-gong
Copy link
Contributor Author

zb-gong commented Aug 7, 2025

Thanks for the review! I reordered the patches and updated the commits. I did not see any merge markers removed in either patch, please let me know if I miss anything.

@kuba-moo
Copy link
Contributor

kuba-moo commented Aug 7, 2025

Please remove the binary file.

The values reported by the sample look quite different to what the C sample from the kernel prints :(

@zb-gong zb-gong force-pushed the cpp-feature branch 4 times, most recently from 7c33824 to 0b0eec0 Compare August 8, 2025 06:25
@zb-gong
Copy link
Contributor Author

zb-gong commented Aug 8, 2025

Removed the binary.
Corrected the generation of struct binary.
Modified the sample file to match the output of rt-addr.c

Copy link
Contributor

@kuba-moo kuba-moo left a comment

Choose a reason for hiding this comment

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

I applied the second patch since it's pretty obviously correct. Please fetch and rebase before pushing.

ynl-gen-cpp.py Outdated
f'struct {c_lower(self.get("struct"))}*data = (struct {c_lower(self.get("struct"))}*)ynl_attr_data(attr);',
f"count = (len % struct_sz > 0) ? len / struct_sz + 1 : len / struct_sz;",
f"{var}->{self.c_name}.resize(count);",
f"memcpy({var}->{self.c_name}.data(), data, len);",
Copy link
Contributor

Choose a reason for hiding this comment

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

The length stuff doesn't look right here.. Do you see kernel responding with more than one cache info?

The reason for the length related dance in the C code is that the struct may grow or shrink (new members added at the end), and the library must be compatible with that. So we carefully allocate "at least" the number of bytes that the struct has in the uAPI headers that the build is against. And we set the len member to how much of the struct kernel actually sent us. The user of the library can then do something like

if (rsp->_len.field < offsetofend(struct x, member))
   return x; // member of interest is not reported by the kernel

Having said that, obviously for C it's a pointer. So if the kernel returned an array of structs and user knows that they can simply index the pointer like an array...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Thanks for the context. Fixed it!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fix memcpy the partial data if the struct grows (len > struct_sz) and copies len bytes to cacheinfo if the struct shrinks (len < struct_sz).

Please let me know if the current fix makes sense. This is not exactly the same as C implementation. If it has to follow the exact behavior of C implementation, I think it has to play with pointer like C does and adds an extra length field.

Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason for sticking to the vector? The vector would only ever have zero or one entry, right?
cc: @fomichev

Copy link

Choose a reason for hiding this comment

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

Haven't looked for too long, but yes, we've been using std::optional for these types of things. Should be similar here: emplace(empty_value) and memcpy the parts you want.

Copy link
Contributor Author

@zb-gong zb-gong Aug 8, 2025

Choose a reason for hiding this comment

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

Yeah I was thinking about it, but I found there were some dependencies (e.g. .size()). So there will be more changes due to that.

Yeah I will make it optional and deal with the dependencies. Probably will split the patch.

@zb-gong zb-gong force-pushed the cpp-feature branch 2 times, most recently from 28c481f to 0e12297 Compare August 8, 2025 17:56
@zb-gong zb-gong requested a review from kuba-moo August 8, 2025 17:58
This patch introduces the `TypeBinaryStruct` class to handle attributes that are of binary type and
structured as a `struct` in the `rt-addr`. Unlike the standard `TypeBinary`, `TypeBinaryStruct` has
a defined structure, so the `struct_member` should be `optional<T>` and the `presence_type` is `flag`.

Signed-off-by: Zibo Gong <[email protected]>
@zb-gong
Copy link
Contributor Author

zb-gong commented Aug 8, 2025

Split the patch: the first introduced the TypeBinaryClass and the second add support for generating rt-addr.

This patch add basic support for netlink-raw protocol. The support can
correctly generate rt-addr. To test that, a rt-addr test added in samples/.

Signed-off-by: Zibo Gong <[email protected]>
@kuba-moo kuba-moo merged commit 37eea16 into linux-netdev:main Aug 8, 2025
1 check passed
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