Skip to content

Conversation

@hedrok
Copy link
Contributor

@hedrok hedrok commented Nov 27, 2025

  • Move stream_put_labeled_prefix and stream_put_prefix_addpath (lib/stream.c) to bgp_attr_stream_put_labeled_prefix and bgp_attr_stream_put_prefix_addpath in bgpd/bgp_attr.c
  • Merge these two function implementations as stream_put_labeled_prefix is more general

This is continuation of #19961, where @mjstapp suggested to make changes to lib/stream* a separate PR.

In next PR I'll add sending of multiple labels in BGP-LU at last :)

@frrbot frrbot bot added bgp tests Topotests, make check, etc labels Nov 27, 2025
bgpd/bgp_attr.c Outdated
/* Encode labels to buffer and pass to stream_put_labeled_prefix, truncate
* labels if needed, set/restore BOS of label in this case
*/
static void stream_bgp_put_labeled_prefix(struct stream *s, const struct prefix *p,
Copy link
Member

Choose a reason for hiding this comment

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

let's call this function bgp_attr_stream_put_labeled_prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🫡

@donaldsharp
Copy link
Member

one minor nit and then we can get this in.

Copy link
Contributor

@mjstapp mjstapp left a comment

Choose a reason for hiding this comment

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

the PR title - looks unrelated, or vaguely related? Please, say in the title what's being changed (lib, bgpd) and what the change is (moving some bgp-only label-encoding code into bgp)
same with the commit title: the commits and the PR are used to produce release notes, so they need to be meaningful and accurate

Copy link
Contributor

@mjstapp mjstapp left a comment

Choose a reason for hiding this comment

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

can we just do the whole thing here? just remove the bgp api from the stream module; it's only used in bgpd (right?)

@hedrok
Copy link
Contributor Author

hedrok commented Dec 3, 2025

stream_put_labeled_prefix is used only in bgp once (and after this PR in stream_put_prefix_addpath).
stream_put_prefix_addpath is used in bgp only in multiple places (and in stream_put_prefix).
stream_put_prefix is used in bgp AND:

  • lib/plist.c: prefix_bgp_orf_entry
  • lib/zclient.c: zapi_labels_encode
  • zebra/zebra_mpls.c: fec_send

I can't quickly say whether all those instances can or should be moved to bgp.

Moving even just stream_put_labeled_prefix without refactoring out of lib/stream would require making a lot of macro public (I've already tried that in commit I've shown to you here bb14416).

The stream_put_labeled_prefix is the most general, moving it out and leaving others will either lead to lib/stream using bgp code or copy-paste of functionality that I've removed in this PR.

I think this PR is at least a step forward - merging diverged stream_put_prefix_addpath and stream_put_labeled_prefix into one, making less code in lib/stream.

Regarding commit message I tried my best and it is now: "lib, bgpd: Move label manipulation out of lib + support sending multiple labels", which is long, but not longest in FRR history )

Also I would like to make it clear: the title is very related, it is not only refactoring here, it is also "Support sending multiple labels in BGP-LU" - I've removed possibility to SEND multiple labels from previous PR so that there were no changes in lib/stream, it is added here, and currently in master it is possible only to RECEIVE multiple labels.

If needed, I can split these changes further to separate commits/PRs with only refactoring as first commit/PR and support of sending multiple labels as second one.

@hedrok hedrok force-pushed the bgp-multiple-labels-send-and-refactor branch from 17b0213 to 82938ee Compare December 3, 2025 15:37
@frrbot frrbot bot added the libfrr label Dec 3, 2025
@hedrok hedrok changed the title bgpd: Support sending multiple labels in BGP-LU + topotest lib, bgpd: Move label manipulation out of lib + support sending multiple labels + topotest Dec 3, 2025
@mjstapp
Copy link
Contributor

mjstapp commented Dec 3, 2025

so here's my suggestion: this problem arose because some folks decided to put this code directly into lib/stream. what they should have done, imo, was to create the bgp-specific blob, and then use the generic stream apis to put that blob into a struct stream. you don't need a big buffer to encode the blob - right? it's only a couple of hundred bytes at most. the bgp code could encode the addpath, if present, then encode the prefix length, then encode the labels and prefix. it's not necessary to do that in the lib/stream code directly, at least I don't think it is.

and if we're really missing some api that we need in the lib to support bgpd, then that's a good thing to learn; I'd rather enhance the library to help daemons who want to encode their protocol messages rather than embed protocol code in the lib because that's the only way the daemons can use a stream?

stream_put_labeled_prefix is used only in bgp once (and after this PR in stream_put_prefix_addpath). [...]

* Move stream_put_labeled_prefix and stream_put_prefix_addpath
  (lib/stream.c) to bgp_attr_stream_put_labeled_prefix and
  bgp_attr_stream_put_prefix_addpath in bgpd/bgp_attr.c
* Merge these two function implementations as
  stream_put_labeled_prefix is more general

Signed-off-by: Kyrylo Yatsenko <hedrok@gmail.com>
@hedrok hedrok force-pushed the bgp-multiple-labels-send-and-refactor branch from 82938ee to 199c6bf Compare December 3, 2025 21:42
@hedrok
Copy link
Contributor Author

hedrok commented Dec 3, 2025

Ok, stream_put_prefix is used (among others) in zapi_labels_encode that is used in zebra_send_mpls_labels that is used in isis, ldp, ospf. So not BGP-specific, so should be left in lib/stream.

But it is the simplest of the three - just writes prefix length in bits and the prefix itself (currently it uses more general but BGP-specific stream_put_prefix_addpath.

lib/stream has

size_t stream_write(struct stream *s, const void *ptr, size_t size)

which is absolutely enough for stream_put_labeled_prefix and stream_put_prefix_addpath - maximum buffer to send is 37 bytes for multilabel case and 24 for one-label case.

So I have updated PR and now:

  1. stream_put_labeled_prefix - moved to bgp_attr as bgp_attr_stream_put_labeled_prefix
  2. stream_put_prefix_addpath - moved to bgp_attr as bgp_attr_stream_put_prefix_addpath
  3. Merged these two function implementations
  4. Reimplemented simple version of stream_put_prefix in lib/stream - without usage of more general stream_put_prefix_addpath.

I've removed changes that add ability to send multiple labels in BGP-LU - I'll create yet another PR when this is merged so that this PR has only refactoring changes.

@hedrok hedrok changed the title lib, bgpd: Move label manipulation out of lib + support sending multiple labels + topotest lib, bgpd: Move BGP-specific funcs out of lib and refactor Dec 3, 2025
@hedrok hedrok requested a review from mjstapp December 4, 2025 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants