xdp: hints via kfuncs
Subsystems (9)
Quoted 144 lines · Toke Høiland-Jørgensen
Quoted 105 lines · Jesper Dangaard Brouer
Quoted 84 lines · Toke Høiland-Jørgensen
Quoted 82 lines · Stanislav Fomichev
Quoted 81 lines · Toke Høiland-Jørgensen
Quoted 78 lines · Jesper Dangaard Brouer
Quoted 53 lines · Martin KaFai Lau
Quoted 42 lines · Stanislav Fomichev
Quoted 31 lines · Martin KaFai Lau
Quoted 27 lines · Stanislav Fomichev
Quoted 10 lines
2.
AF_XDPprograms won't be able to access the metadata without using a
custom XDP program that calls the kfuncs and puts the data into the metadata area. We could solve this with some code in libxdp, though; if
this code can be made generic enough (so it just dumps the available metadata functions from the running kernel at load time), it may be possible to make it generic enough that it will be forward-compatible with new versions of the kernel that add new fields, which should alleviate Florian's concern about keeping things in sync.Good point. I had to convert to a custom program to use the kfuncs :-( But your suggestion sounds good; maybe libxdp can accept some extra info about at which offset the user would like to place the metadata and the library can generate the required bytecode?
Quoted 10 lines
3. It will make it harder to consume the metadata when building SKBs. I
think the CPUMAP and veth use cases are also quite important, and that we want metadata to be available for building SKBs in this path. Maybe this can be resolved by having a convenient kfunc for this that can be used for programs doing such redirects. E.g., you could just callxdp_copy_metadata_for_skb()before doing thebpf_redirect, and that would recursively expand into all the kfunc calls needed to extract the
metadata supported by the SKB path?Quoted 1 line
So this
xdp_copy_metadata_for_skbwill create a metadata layout thatQuoted 8 lines
Can the
xdp_copy_metadata_for_skbbe written as a bpf prog itself? Not sure where is the best point to specify this prog though. Somehow duringbpf_xdp_redirect_map?
or this prog belongs to the target cpumap and the xdp prog redirecting to this
cpumap has to write the meta layout in a way that the cpumap is expecting?Quoted 9 lines
We're probably interested in triggering it from the places where xdp frames can eventually be converted into skbs?
So for plain 'returnXDP_PASS' and things like bpf_redirect/etc? (IOW, anything that's notXDP_DROP/AF_XDPredirect).
We can probably make it magically work, and can generate kernel-digestible metadata whenever data ==data_meta, but the question - should we?
(need to make sure we won't regress any existing cases that are not relying on the metadata)Quoted 5 lines
Instead of having some kernel-digestible meta data, how about calling another bpf prog to initialize the skb fields from the meta area after
__xdp_build_skb_from_frame()in the cpumap, sorun_xdp_set_skb_fileds_from_metadata()may be a better name.I very much like this idea of calling another bpf prog to initialize the SKB fields from the meta area. (As a reminder, data need to come from meta area, because at this point the hardware RX-desc is out-of-scope). I'm onboard with
xdp_copy_metadata_for_skb()populating the meta area.We could invoke this BPF-prog inside
__xdp_build_skb_from_frame().We might need a new
BPF_PROG_TYPE_XDP2SKBas this new BPF-progrun_xdp_set_skb_fields_from_metadata()would need bothxdp_buff+ SKB as context inputs. Right? (Not sure, if this is acceptable with the BPF maintainers new rules)Quoted 5 lines
The xdp_prog@rx sets the meta data and then redirect. If the xdp_prog@rx can also specify a xdp prog to initialize the skb fields from the meta area, then there is no need to have a kfunc to enforce a kernel-digestible layout. Not sure what is a good way to specify this
xdp_progthough...Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
The challenge of running this (
BPF_PROG_TYPE_XDP2SKB) BPF-prog inside__xdp_build_skb_from_frame()is that it need to know howto decode the meta area for every device driver or XDP-prog populating this (as veth and cpumap can get redirected packets from multiple device drivers).If we have the helper to copy the data "out of" the drivers, why do we need a second BPF program to copy data to the SKB?
IMHO the second BPF program to populate the SKB is needed to add flexibility and extensibility.
My end-goal here is to speedup packet parsing.
This BPF-prog should (in time) be able to updateskb->transport_headerandskb->network_header. As I mentioned before, HW RX-hash already tell us the L3 and L4 protocols and in-most-cases header-len. Even without HW-hints, the XDP-prog likely have parsed the packet once. This parse information is lost today, and redone by netstack. What about storing this header parse info in meta data and re-use in this new XDP2SKB hook?Quoted 5 lines
The reason for suggesting this BPF-prog to be a callback, associated with the
net_device, were that HW is going to differ on what HW hints that HW support. Thus, we can avoid a generic C-function that need to check for all the possible hints, and instead have a BPF-prog that only contains the code that is relevant for thisnet_device.But that's exactly what the
xdp_copy_metadata_for_skb()is! It's a dynamic "BPF program" (generated as unrolled kfunc calls) just running in the helper and stashing the results in an intermediate struct in the metadata area. And once it's done that, we don't need *another* dynamic BPF program to read it back out and populate the SKB, because the intermediate format it's been stashed into is under the control of the kernel (we just need a flag to indicate that it's there).Quoted 15 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
I.e., the XDP program calls
xdp_copy_metadata_for_skb(); this invokes each of the kfuncs needed for the metadata used by SKBs, all of which get unrolled. The helper takes the output of these metadata-extracting kfuncs and stores it "somewhere". This "somewhere" could well be the metadata area; but in any case, since it's hidden away inside a helper (or kfunc) from the calling XDP program's PoV, the helper can just stash all the data in a fixed format, which__xdp_build_skb_from_frame()can then just read statically. We could even make this format match the field layout of structsk_buff, so all we have to do is memcpy a contiguous chunk of memory when building the SKB.Quoted 1 line
Quoted 1 line
+1
Sorry, I think this "hiding" layout trick is going in a wrong direction.
Quoted 7 lines
Imagine the use-case of cpumap redirect. The physical device XDP-prog calls
xdp_copy_metadata_for_skb()to extract info from RX-desc, then it calls redirect into cpumap. On remote CPU, thexdp_frameis picked up, and then I want to run another XDP-prog that want to look at these HW-hints, and then likely callXDP_PASSwhich creates the SKB, also using these HW-hints. I take it, it would not be possible when using thexdp_copy_metadata_for_skb()helper?
Quoted 6 lines
You're right that it should be possible to read the values back out again later. That is totally possible with this scheme, though; the '
xdp_to_skb_metadata' is going to be in the vmlinux BTF, so an XDP program can just read that. We can explicitly support it by using the BTF ID as the "magic value" as you suggest, which would be fine by me.
I'm on-board if we as you suggest, add the BTF_ID as the "magic value" (as last member due to AF_XDP processing). When xdp_copy_metadata_for_skb() writes 'xdp_to_skb_metadata' in metadata area. We should simply see this BTF-ID as a 'cookie' or magic number.
Quoted 5 lines
I still think we should be using the
__randomize_layouttrick, though, precisely so that BPF consumers are forced to use BTF relocations to read it; otherwise we risk the struct layout ossifying into UAPI because people are just going to assume it's static...
I'm also on-board with some level of randomization to the struct to force consumers to use BTF for relocations. e.g BTF-ID cookie/magic should be at a fixed location.
Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 11 lines
I'm currently doing exactly what you're suggesting (minus matching skb layout):
struct
xdp_to_skb_metadata{
u32 magic; // randomized at boot
... skb-consumable-metadata in fixed format
}__randomize_layout;
bpf_xdp_copy_metadata_for_skb()doesbpf_xdp_adjust_meta(ctx, -sizeof(struct xdp_to_skb_metadata))and then calls a bunch of kfuncs to fill in the actual data.Quoted 8 lines
Then, at
__xdp_build_skb_from_frametime, I'm having a regular kernel C code that parses that 'structxdp_to_skb_metadata'.
(To be precise, I'm trying to parse the metadata fromskb_metadata_set; it's called from__xdp_build_skb_from_frame, but not 100% sure that's the right place).
(I also randomize the layout and magic to make sure userspace doesn't depend on it because nothing stops this packet to be routed into xsk socket..)Ah, nice trick with
__randomize_layout- I agree we need to do something to prevent userspace from inadvertently starting to rely on this, and this seems like a great solution!
Quoted 6 lines
Quoted 2 lines
Sorry, I disagree where this is going. Why do all of a sudden want to prevent userspace (e.g.
AF_XDP) from using this data?!?See above: I don't think we should prevent userspace from using it (and we're not), but we should prevent the struct layout from ossifying.
Okay, then we are in agreement, avoid ossifying struct layout.
Quoted 13 lines
Quoted 5 lines
The hole exercise started with wanting to provide
AF_XDPwith these HW-hints. The hints a standardAF_XDPuser wants is likely very similar to what the SKB user wants. Why do theAF_XDPuser need to open code this?Quoted 7 lines
The BTF-ID scheme precisely allows us to expose this layout to userspace, and at the same time have freedom to change this in kernel space, as userspace must decode the BTF-layout before reading this. I was hoping
xdp_copy_metadata_for_skb()could simply use the BTF-ID scheme, with the BTF-ID of structxdp_hints_rx_common, is to too much to ask for? You can just consider the BTF-ID as the magic number, as it will be more-or-less random per kernel build (and module load order).
Quoted 4 lines
As mentioned above, I would be totally fine with just having the
xdp_to_skb_metadatabe part of BTF, enabling both XDP programs andAF_XDPconsumers to re-use it.
My use-case is that AF_XDP will need to key on this runtime BTF-ID magic value anyway to read out 'xdp_to_skb_metadata' values. I have an XDP-prog running, that will RX-timestamp only the time-sync protocol packets. Code wise, I will simply add my RX-timestamp on top of struct 'xdp_to_skb_metadata' and then update the BTF-ID magic value. I don't need to add a real BTF-ID but just some magic value that my AF_XDP userspace prog knows about. In my current code[1], I'm playing nice and adds the BPF-prog's own local BTF-ID via bpf_core_type_id_local().
[1] https://github.com/xdp-project/bpf-examples/blob/master/AF_XDP-interaction/af_xdp_kern.c#L80
Quoted 20 lines
Quoted 7 lines
Quoted 2 lines
Look forward to seeing what the whole thing looks like in a more complete form :)
I'm sort of on-board with the kfuncs and unroll-tricks, if I can see some driver code that handles the issues of getting HW setup state exposed needed to decode the RX-desc format.
Quoted 5 lines
I sense that I myself, haven't been good enough to explain/convey the BTF-ID scheme. Next week, I will code some examples that demo how BTF-IDs can be used from BPF-progs, even as a communication channel between different BPF-progs (e.g. drv XDP-prog -> cpumap XDP-prog -> TC-BPF).
For my part at least, it's not a lack of understanding that makes me prefer the kfunc approach. Rather, it's the complexity of having to resolve the multiple BTF IDs, and the risk of ossifying the struct layouts because people are going to do that wrong. Using kfuncs gives us much more control of the API, especially if we combine it with struct randomisation for the bits we do expose.
Quoted 9 lines
Translating what we've discussed above into the terms used in your patch series, this would correspond to *only* having the
xdp_metadata_commonstruct exposed via BTF, and not bothering with all the other driver-specific layouts. So an XDP/AF_XDP user that only wants to use the metadata that's also used by the stack can just callxdp_copy_metadata_for_skb(), and then read the resulting metadata area (using BTF). And if someone wants to access metadata that's *not* used by the stack, they'd have to call additional kfuncs to extract that.
I agree, that this patchset does/will simplify my patchset. My driver specific structs for BTF-IDs will no-longer be needed. As it is now up-to XDP-prog to explicitly extend with fields. This should reduce your concern with resolving multiple BTF IDs.
Maybe after this patchset, I would suggest that we could create some "kernel-central" struct's that have e.g. RX-timestamp and mark (mlx5 have HW support for mark) and protocol types (via RX-hash). As these could be used by XDP-prog's that explicitly extract these, and communicate the layout via setting the BTF-ID (via calling bpf_core_type_id_kernel()). Making it easier to consume from chained BPF-progs, AF_XDP and even via kernel C-code that updates SKB fields as the number of these magic BTF-ID's will be small enough.
Quoted 8 lines
And similarly, if someone wants only a subset of the metadata used by an SKB, they can just *not* call
xdp_copy_metadata_for_skb(), and instead just call the individual kfuncs to extract just the fields they want.I think this strikes a nice balance between the flexibility by the kernel to change things, the flexibility of XDP consumers to request only the data they want, and the ability for the same metadata to be consumed at different points. WDYT?
With above comments, I think we are closer to an agreement again.
--Jesper
Quoted 117 lines · Toke Høiland-Jørgensen
Quoted 101 lines · Florian
Hi all,
I was closely following this discussion for some time now. Seems we reached the point where it's getting interesting for me.
Quoted 87 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 28 Oct 2022 16:16:17 -0700 John Fastabend wrote:
Quoted 2 lines
Quoted 2 lines
And it's actually harder to abstract away inter HW generation differences if the user space code has to handle all of it.
I don't see how its any harder in practice though?
You need to find out what HW/FW/config you're running, right? And all you have is a pointer to a blob of unknown type.
Take timestamps for example, some NICs support adjusting the PHC or doing SW corrections (with different versions of hw/fw/server platforms being capable of both/one/neither).
Sure you can extract all this info with tracing and careful inspection via uAPI. But I don't think that's _easier_. And the vendors can't run the results thru their validation (for whatever that's worth).
Quoted 21 lines
Quoted 12 lines
I've had the same concern:
Until we have some userspace library that abstracts all these details, it's not really convenient to use. IIUC, with a kptr, I'd get a blob of data and I need to go through the code and see what particular type it represents for my particular device and how the data I need is represented there. There are also these "if this is device v1 -> use v1 descriptor format; if it's a
v2->usethis another struct; etc" complexities that we'll be pushing onto the users. With kfuncs, we put this burden on the driver developers, but I agree that the drawback here is that we actually have to wait for the implementations to catch up.I agree with everything there, you will get a blob of data and then will need to know what field you want to read using BTF. But, we already do this for BPF programs all over the place so its not a big lift for us. All other BPF tracing/observability requires the same logic. I think users of BPF in general perhaps XDP/tc are the only place left to write BPF programs without thinking about BTF and kernel data structures.
Quoted 7 lines
But, with proposed kptr the complexity lives in userspace and can be fixed, added, updated without having to bother with kernel updates, etc.
From my point of view of supporting Cilium its a win and much preferred
to having to deal with driver owners on all cloud vendors, distributions, and so on.If vendor updates firmware with new fields I get those immediately.
Conversely it's a valid concern that those who *do* actually update their kernel regularly will have more things to worry about.
Quoted 8 lines
Quoted 4 lines
Jakub mentions FW and I haven't even thought about that; so yeah, bpf programs might have to take a lot of other state into consideration when parsing the descriptors; all those details do seem like they belong to the driver code.
I would prefer to avoid being stuck on requiring driver writers to be involved. With just a kptr I can support the device and any firwmare versions without requiring help.
1) where are you getting all those HW / FW specs :S
2) maybe *you* can but you're not exactly not an ex-driver developer :SQuoted 10 lines
Quoted 4 lines
Feel free to send it early with just a handful of drivers implemented; I'm more interested about bpf/af_xdp/user api story; if we have some nice sample/test case that shows how the metadata can be used, that might push us closer to the agreement on the best way to proceed.
I'll try to do a intel and mlx implementation to get a cross section. I have a good collection of nics here so should be able to show a couple firmware versions. It could be fine I think to have the raw kptr access and then also kfuncs for some things perhaps.
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
I'd prefer if we left the door open for new vendors. Punting descriptor parsing to user space will indeed result in what you just said - major vendors are supported and that's it.
I'm not sure about why it would make it harder for new vendors? I think the opposite,
TBH I'm only replying to the email because of the above part :) I thought this would be self evident, but I guess our perspectives are different.
Perhaps you look at it from the perspective of SW running on someone else's cloud, an being able to move to another cloud, without having to worry if feature X is available in xdp or just skb.
Quoted 5 lines
I look at it from the perspective of maintaining a cloud, with people writing random XDP applications. If I swap a NIC from an incumbent to a (superior) startup, and cloud users are messing with raw descriptor - I'd need to go find every XDP program out there and make sure it understands the new descriptors.
Here is another perspective:
Quoted 8 lines
As
AF_XDPapplication developer I don't wan't to deal with the underlying hardware in detail. I like to request a feature from the OS (in this case rx/tx timestamping). If the feature is available I will simply use it, if not I might have to work around it - maybe by falling back to SW timestamping.All parts of my application (BPF program included) should not be optimized/adjusted for all the different HW variants out there.
Yes, absolutely agreed. Abstracting away those kinds of hardware differences is the whole *point* of having an OS/driver model. I.e., it's what the kernel is there for! If people want to bypass that and get direct access to the hardware, they can already do that by using DPDK.
Quoted 8 lines
So in other words, 100% agreed that we should not expect the BPF developers to deal with hardware details as would be required with a kptr-based interface.
As for the kfunc-based interface, I think it shows some promise. Exposing a list of function names to retrieve individual metadata items instead of a struct layout is sorta comparable in terms of developer UI accessibility etc (IMO).
Looks like there are quite some use cases for hw_timestamp. Do you think we could add it to the uapi like struct xdp_md?
The following is the current xdp_md:
struct xdp_md {
__u32 data;
__u32 data_end;
__u32 data_meta;
/* Below access go through struct xdp_rxq_info */
__u32 ingress_ifindex; /* rxq->dev->ifindex */
__u32 rx_queue_index; /* rxq->queue_index */
__u32 egress_ifindex; /* txq->dev->ifindex */
};We could add __u64 hw_timestamp to the xdp_md so user can just do xdp_md->hw_timestamp to get the value. xdp_md->hw_timestamp == 0 means hw_timestamp is not available.
Inside the kernel, the ctx rewriter can generate code to call driver specific function to retrieve the data.
The kfunc approach can be used to *less* common use cases?
Quoted 30 lines
There are three main drawbacks, AFAICT:
1. It requires driver developers to write and maintain the code that generates the unrolled BPF bytecode to access the metadata fields, which is a non-trivial amount of complexity. Maybe this can be abstracted away with some internal helpers though (like, e.g., a
bpf_xdp_metadata_copy_u64(dst, src, offset)helper which would spit out the required JMP/MOV/LDX instructions?2.
AF_XDPprograms won't be able to access the metadata without using a custom XDP program that calls the kfuncs and puts the data into the metadata area. We could solve this with some code in libxdp, though; if this code can be made generic enough (so it just dumps the available metadata functions from the running kernel at load time), it may be possible to make it generic enough that it will be forward-compatible with new versions of the kernel that add new fields, which should alleviate Florian's concern about keeping things in sync.3. It will make it harder to consume the metadata when building SKBs. I think the CPUMAP and veth use cases are also quite important, and that we want metadata to be available for building SKBs in this path. Maybe this can be resolved by having a convenient kfunc for this that can be used for programs doing such redirects. E.g., you could just call
xdp_copy_metadata_for_skb()before doing thebpf_redirect, and that would recursively expand into all the kfunc calls needed to extract the metadata supported by the SKB path?-Toke
Quoted 145 lines · Yonghong Song
Quoted 117 lines · Toke Høiland-Jørgensen
Quoted 101 lines · Florian
Hi all,
I was closely following this discussion for some time now. Seems we reached the point where it's getting interesting for me.
Quoted 87 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 28 Oct 2022 16:16:17 -0700 John Fastabend wrote:
Quoted 2 lines
Quoted 2 lines
And it's actually harder to abstract away inter HW generation differences if the user space code has to handle all of it.
I don't see how its any harder in practice though?
You need to find out what HW/FW/config you're running, right? And all you have is a pointer to a blob of unknown type.
Take timestamps for example, some NICs support adjusting the PHC or doing SW corrections (with different versions of hw/fw/server platforms being capable of both/one/neither).
Sure you can extract all this info with tracing and careful inspection via uAPI. But I don't think that's _easier_. And the vendors can't run the results thru their validation (for whatever that's worth).
Quoted 21 lines
Quoted 12 lines
I've had the same concern:
Until we have some userspace library that abstracts all these details, it's not really convenient to use. IIUC, with a kptr, I'd get a blob of data and I need to go through the code and see what particular type it represents for my particular device and how the data I need is represented there. There are also these "if this is device v1 -> use v1 descriptor format; if it's a
v2->usethis another struct; etc" complexities that we'll be pushing onto the users. With kfuncs, we put this burden on the driver developers, but I agree that the drawback here is that we actually have to wait for the implementations to catch up.I agree with everything there, you will get a blob of data and then will need to know what field you want to read using BTF. But, we already do this for BPF programs all over the place so its not a big lift for us. All other BPF tracing/observability requires the same logic. I think users of BPF in general perhaps XDP/tc are the only place left to write BPF programs without thinking about BTF and kernel data structures.
Quoted 7 lines
But, with proposed kptr the complexity lives in userspace and can be fixed, added, updated without having to bother with kernel updates, etc.
From my point of view of supporting Cilium its a win and much preferred
to having to deal with driver owners on all cloud vendors, distributions, and so on.If vendor updates firmware with new fields I get those immediately.
Conversely it's a valid concern that those who *do* actually update their kernel regularly will have more things to worry about.
Quoted 8 lines
Quoted 4 lines
Jakub mentions FW and I haven't even thought about that; so yeah, bpf programs might have to take a lot of other state into consideration when parsing the descriptors; all those details do seem like they belong to the driver code.
I would prefer to avoid being stuck on requiring driver writers to be involved. With just a kptr I can support the device and any firwmare versions without requiring help.
1) where are you getting all those HW / FW specs :S
2) maybe *you* can but you're not exactly not an ex-driver developer :SQuoted 10 lines
Quoted 4 lines
Feel free to send it early with just a handful of drivers implemented; I'm more interested about bpf/af_xdp/user api story; if we have some nice sample/test case that shows how the metadata can be used, that might push us closer to the agreement on the best way to proceed.
I'll try to do a intel and mlx implementation to get a cross section. I have a good collection of nics here so should be able to show a couple firmware versions. It could be fine I think to have the raw kptr access and then also kfuncs for some things perhaps.
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
I'd prefer if we left the door open for new vendors. Punting descriptor parsing to user space will indeed result in what you just said - major vendors are supported and that's it.
I'm not sure about why it would make it harder for new vendors? I think the opposite,
TBH I'm only replying to the email because of the above part :) I thought this would be self evident, but I guess our perspectives are different.
Perhaps you look at it from the perspective of SW running on someone else's cloud, an being able to move to another cloud, without having to worry if feature X is available in xdp or just skb.
Quoted 5 lines
I look at it from the perspective of maintaining a cloud, with people writing random XDP applications. If I swap a NIC from an incumbent to a (superior) startup, and cloud users are messing with raw descriptor - I'd need to go find every XDP program out there and make sure it understands the new descriptors.
Here is another perspective:
Quoted 8 lines
As
AF_XDPapplication developer I don't wan't to deal with the underlying hardware in detail. I like to request a feature from the OS (in this case rx/tx timestamping). If the feature is available I will simply use it, if not I might have to work around it - maybe by falling back to SW timestamping.All parts of my application (BPF program included) should not be optimized/adjusted for all the different HW variants out there.
Yes, absolutely agreed. Abstracting away those kinds of hardware differences is the whole *point* of having an OS/driver model. I.e., it's what the kernel is there for! If people want to bypass that and get direct access to the hardware, they can already do that by using DPDK.
Quoted 8 lines
So in other words, 100% agreed that we should not expect the BPF developers to deal with hardware details as would be required with a kptr-based interface.
As for the kfunc-based interface, I think it shows some promise. Exposing a list of function names to retrieve individual metadata items instead of a struct layout is sorta comparable in terms of developer UI accessibility etc (IMO).
Looks like there are quite some use cases for
hw_timestamp. Do you think we could add it to the uapi like structxdp_md?The following is the current
xdp_md:struct xdp_md { __u32 data; __u32 data_end; __u32 data_meta; /* Below access go through struct xdp_rxq_info */ __u32 ingress_ifindex; /* rxq->dev->ifindex */ __u32 rx_queue_index; /* rxq->queue_index */ __u32 egress_ifindex; /* txq->dev->ifindex */ };
Quoted 7 lines
We could add
__u64hw_timestampto thexdp_mdso user can just doxdp_md->hw_timestampto get the value.xdp_md->hw_timestamp== 0 meanshw_timestampis not available.Inside the kernel, the ctx rewriter can generate code to call driver specific function to retrieve the data.
If the driver generates the code to retrieve the data, how's that different from the kfunc approach?
The only difference I see is that it would be a more strong UAPI than the kfuncs?
Quoted 1 line
The kfunc approach can be used to *less* common use cases?
What's the advantage of having two approaches when one can cover common and uncommon cases?
Quoted 29 lines
Quoted 29 lines
There are three main drawbacks, AFAICT:
1. It requires driver developers to write and maintain the code that generates the unrolled BPF bytecode to access the metadata fields, which is a non-trivial amount of complexity. Maybe this can be abstracted away with some internal helpers though (like, e.g., a
bpf_xdp_metadata_copy_u64(dst, src, offset)helper which would spit out the required JMP/MOV/LDX instructions?2.
AF_XDPprograms won't be able to access the metadata without using a custom XDP program that calls the kfuncs and puts the data into the metadata area. We could solve this with some code in libxdp, though; if this code can be made generic enough (so it just dumps the available metadata functions from the running kernel at load time), it may be possible to make it generic enough that it will be forward-compatible with new versions of the kernel that add new fields, which should alleviate Florian's concern about keeping things in sync.3. It will make it harder to consume the metadata when building SKBs. I think the CPUMAP and veth use cases are also quite important, and that we want metadata to be available for building SKBs in this path. Maybe this can be resolved by having a convenient kfunc for this that can be used for programs doing such redirects. E.g., you could just call
xdp_copy_metadata_for_skb()before doing thebpf_redirect, and that would recursively expand into all the kfunc calls needed to extract the metadata supported by the SKB path?-Toke
Quoted 151 lines · Stanislav Fomichev
Quoted 150 lines · Yonghong Song
Quoted 117 lines · Toke Høiland-Jørgensen
Quoted 101 lines · Florian
Hi all,
I was closely following this discussion for some time now. Seems we reached the point where it's getting interesting for me.
Quoted 87 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 28 Oct 2022 16:16:17 -0700 John Fastabend wrote:
Quoted 2 lines
Quoted 2 lines
And it's actually harder to abstract away inter HW generation differences if the user space code has to handle all of it.
I don't see how its any harder in practice though?
You need to find out what HW/FW/config you're running, right? And all you have is a pointer to a blob of unknown type.
Take timestamps for example, some NICs support adjusting the PHC or doing SW corrections (with different versions of hw/fw/server platforms being capable of both/one/neither).
Sure you can extract all this info with tracing and careful inspection via uAPI. But I don't think that's _easier_. And the vendors can't run the results thru their validation (for whatever that's worth).
Quoted 21 lines
Quoted 12 lines
I've had the same concern:
Until we have some userspace library that abstracts all these details, it's not really convenient to use. IIUC, with a kptr, I'd get a blob of data and I need to go through the code and see what particular type it represents for my particular device and how the data I need is represented there. There are also these "if this is device v1 -> use v1 descriptor format; if it's a
v2->usethis another struct; etc" complexities that we'll be pushing onto the users. With kfuncs, we put this burden on the driver developers, but I agree that the drawback here is that we actually have to wait for the implementations to catch up.I agree with everything there, you will get a blob of data and then will need to know what field you want to read using BTF. But, we already do this for BPF programs all over the place so its not a big lift for us. All other BPF tracing/observability requires the same logic. I think users of BPF in general perhaps XDP/tc are the only place left to write BPF programs without thinking about BTF and kernel data structures.
Quoted 7 lines
But, with proposed kptr the complexity lives in userspace and can be fixed, added, updated without having to bother with kernel updates, etc.
From my point of view of supporting Cilium its a win and much preferred
to having to deal with driver owners on all cloud vendors, distributions, and so on.If vendor updates firmware with new fields I get those immediately.
Conversely it's a valid concern that those who *do* actually update their kernel regularly will have more things to worry about.
Quoted 8 lines
Quoted 4 lines
Jakub mentions FW and I haven't even thought about that; so yeah, bpf programs might have to take a lot of other state into consideration when parsing the descriptors; all those details do seem like they belong to the driver code.
I would prefer to avoid being stuck on requiring driver writers to be involved. With just a kptr I can support the device and any firwmare versions without requiring help.
1) where are you getting all those HW / FW specs :S
2) maybe *you* can but you're not exactly not an ex-driver developer :SQuoted 10 lines
Quoted 4 lines
Feel free to send it early with just a handful of drivers implemented; I'm more interested about bpf/af_xdp/user api story; if we have some nice sample/test case that shows how the metadata can be used, that might push us closer to the agreement on the best way to proceed.
I'll try to do a intel and mlx implementation to get a cross section. I have a good collection of nics here so should be able to show a couple firmware versions. It could be fine I think to have the raw kptr access and then also kfuncs for some things perhaps.
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
I'd prefer if we left the door open for new vendors. Punting descriptor parsing to user space will indeed result in what you just said - major vendors are supported and that's it.
I'm not sure about why it would make it harder for new vendors? I think the opposite,
TBH I'm only replying to the email because of the above part :) I thought this would be self evident, but I guess our perspectives are different.
Perhaps you look at it from the perspective of SW running on someone else's cloud, an being able to move to another cloud, without having to worry if feature X is available in xdp or just skb.
Quoted 5 lines
I look at it from the perspective of maintaining a cloud, with people writing random XDP applications. If I swap a NIC from an incumbent to a (superior) startup, and cloud users are messing with raw descriptor - I'd need to go find every XDP program out there and make sure it understands the new descriptors.
Here is another perspective:
Quoted 8 lines
As
AF_XDPapplication developer I don't wan't to deal with the underlying hardware in detail. I like to request a feature from the OS (in this case rx/tx timestamping). If the feature is available I will simply use it, if not I might have to work around it - maybe by falling back to SW timestamping.All parts of my application (BPF program included) should not be optimized/adjusted for all the different HW variants out there.
Yes, absolutely agreed. Abstracting away those kinds of hardware differences is the whole *point* of having an OS/driver model. I.e., it's what the kernel is there for! If people want to bypass that and get direct access to the hardware, they can already do that by using DPDK.
Quoted 8 lines
So in other words, 100% agreed that we should not expect the BPF developers to deal with hardware details as would be required with a kptr-based interface.
As for the kfunc-based interface, I think it shows some promise. Exposing a list of function names to retrieve individual metadata items instead of a struct layout is sorta comparable in terms of developer UI accessibility etc (IMO).
Looks like there are quite some use cases for
hw_timestamp. Do you think we could add it to the uapi like structxdp_md?The following is the current
xdp_md:struct xdp_md { __u32 data; __u32 data_end; __u32 data_meta; /* Below access go through struct xdp_rxq_info */ __u32 ingress_ifindex; /* rxq->dev->ifindex */ __u32 rx_queue_index; /* rxq->queue_index */ __u32 egress_ifindex; /* txq->dev->ifindex */ };We could add
__u64hw_timestampto thexdp_mdso user can just doxdp_md->hw_timestampto get the value.xdp_md->hw_timestamp== 0 meanshw_timestampis not available.
Quoted 7 lines
Quoted 2 lines
Inside the kernel, the ctx rewriter can generate code to call driver specific function to retrieve the data.
If the driver generates the code to retrieve the data, how's that different from the kfunc approach?
The only difference I see is that it would be a more strong UAPI than the kfuncs?
Right. it is a strong uapi.
Quoted 5 lines
Quoted 2 lines
The kfunc approach can be used to *less* common use cases?
What's the advantage of having two approaches when one can cover common and uncommon cases?
Beyond hw_timestamp, do we have any other fields ready to support?
If it ends up with lots of fields to be accessed by the bpf program, and bpf program actually intends to access these fields, using a strong uapi might be a good thing as it can make code much streamlined.
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
There are three main drawbacks, AFAICT:
1. It requires driver developers to write and maintain the code that generates the unrolled BPF bytecode to access the metadata fields, which is a non-trivial amount of complexity. Maybe this can be abstracted away with some internal helpers though (like, e.g., a
bpf_xdp_metadata_copy_u64(dst, src, offset)helper which would spit out the required JMP/MOV/LDX instructions?2.
AF_XDPprograms won't be able to access the metadata without using a custom XDP program that calls the kfuncs and puts the data into the metadata area. We could solve this with some code in libxdp, though; if this code can be made generic enough (so it just dumps the available metadata functions from the running kernel at load time), it may be possible to make it generic enough that it will be forward-compatible with new versions of the kernel that add new fields, which should alleviate Florian's concern about keeping things in sync.3. It will make it harder to consume the metadata when building SKBs. I think the CPUMAP and veth use cases are also quite important, and that we want metadata to be available for building SKBs in this path. Maybe this can be resolved by having a convenient kfunc for this that can be used for programs doing such redirects. E.g., you could just call
xdp_copy_metadata_for_skb()before doing thebpf_redirect, and that would recursively expand into all the kfunc calls needed to extract the metadata supported by the SKB path?-Toke
Quoted 171 lines · Yonghong Song
Quoted 151 lines · Stanislav Fomichev
Quoted 150 lines · Yonghong Song
Quoted 117 lines · Toke Høiland-Jørgensen
Quoted 101 lines · Florian
Hi all,
I was closely following this discussion for some time now. Seems we reached the point where it's getting interesting for me.
Quoted 87 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 28 Oct 2022 16:16:17 -0700 John Fastabend wrote:
Quoted 2 lines
Quoted 2 lines
And it's actually harder to abstract away inter HW generation differences if the user space code has to handle all of it.
I don't see how its any harder in practice though?
You need to find out what HW/FW/config you're running, right? And all you have is a pointer to a blob of unknown type.
Take timestamps for example, some NICs support adjusting the PHC or doing SW corrections (with different versions of hw/fw/server platforms being capable of both/one/neither).
Sure you can extract all this info with tracing and careful inspection via uAPI. But I don't think that's _easier_. And the vendors can't run the results thru their validation (for whatever that's worth).
Quoted 21 lines
Quoted 12 lines
I've had the same concern:
Until we have some userspace library that abstracts all these details, it's not really convenient to use. IIUC, with a kptr, I'd get a blob of data and I need to go through the code and see what particular type it represents for my particular device and how the data I need is represented there. There are also these "if this is device v1 -> use v1 descriptor format; if it's a
v2->usethis another struct; etc" complexities that we'll be pushing onto the users. With kfuncs, we put this burden on the driver developers, but I agree that the drawback here is that we actually have to wait for the implementations to catch up.I agree with everything there, you will get a blob of data and then will need to know what field you want to read using BTF. But, we already do this for BPF programs all over the place so its not a big lift for us. All other BPF tracing/observability requires the same logic. I think users of BPF in general perhaps XDP/tc are the only place left to write BPF programs without thinking about BTF and kernel data structures.
Quoted 7 lines
But, with proposed kptr the complexity lives in userspace and can be fixed, added, updated without having to bother with kernel updates, etc.
From my point of view of supporting Cilium its a win and much preferred
to having to deal with driver owners on all cloud vendors, distributions, and so on.If vendor updates firmware with new fields I get those immediately.
Conversely it's a valid concern that those who *do* actually update their kernel regularly will have more things to worry about.
Quoted 8 lines
Quoted 4 lines
Jakub mentions FW and I haven't even thought about that; so yeah, bpf programs might have to take a lot of other state into consideration when parsing the descriptors; all those details do seem like they belong to the driver code.
I would prefer to avoid being stuck on requiring driver writers to be involved. With just a kptr I can support the device and any firwmare versions without requiring help.
1) where are you getting all those HW / FW specs :S
2) maybe *you* can but you're not exactly not an ex-driver developer :SQuoted 10 lines
Quoted 4 lines
Feel free to send it early with just a handful of drivers implemented; I'm more interested about bpf/af_xdp/user api story; if we have some nice sample/test case that shows how the metadata can be used, that might push us closer to the agreement on the best way to proceed.
I'll try to do a intel and mlx implementation to get a cross section. I have a good collection of nics here so should be able to show a couple firmware versions. It could be fine I think to have the raw kptr access and then also kfuncs for some things perhaps.
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
I'd prefer if we left the door open for new vendors. Punting descriptor parsing to user space will indeed result in what you just said - major vendors are supported and that's it.
I'm not sure about why it would make it harder for new vendors? I think the opposite,
TBH I'm only replying to the email because of the above part :) I thought this would be self evident, but I guess our perspectives are different.
Perhaps you look at it from the perspective of SW running on someone else's cloud, an being able to move to another cloud, without having to worry if feature X is available in xdp or just skb.
Quoted 5 lines
I look at it from the perspective of maintaining a cloud, with people writing random XDP applications. If I swap a NIC from an incumbent to a (superior) startup, and cloud users are messing with raw descriptor - I'd need to go find every XDP program out there and make sure it understands the new descriptors.
Here is another perspective:
Quoted 8 lines
As
AF_XDPapplication developer I don't wan't to deal with the underlying hardware in detail. I like to request a feature from the OS (in this case rx/tx timestamping). If the feature is available I will simply use it, if not I might have to work around it - maybe by falling back to SW timestamping.All parts of my application (BPF program included) should not be optimized/adjusted for all the different HW variants out there.
Yes, absolutely agreed. Abstracting away those kinds of hardware differences is the whole *point* of having an OS/driver model. I.e., it's what the kernel is there for! If people want to bypass that and get direct access to the hardware, they can already do that by using DPDK.
Quoted 8 lines
So in other words, 100% agreed that we should not expect the BPF developers to deal with hardware details as would be required with a kptr-based interface.
As for the kfunc-based interface, I think it shows some promise. Exposing a list of function names to retrieve individual metadata items instead of a struct layout is sorta comparable in terms of developer UI accessibility etc (IMO).
Looks like there are quite some use cases for
hw_timestamp. Do you think we could add it to the uapi like structxdp_md?The following is the current
xdp_md:struct xdp_md { __u32 data; __u32 data_end; __u32 data_meta; /* Below access go through struct xdp_rxq_info */ __u32 ingress_ifindex; /* rxq->dev->ifindex */ __u32 rx_queue_index; /* rxq->queue_index */ __u32 egress_ifindex; /* txq->dev->ifindex */ };We could add
__u64hw_timestampto thexdp_mdso user can just doxdp_md->hw_timestampto get the value.xdp_md->hw_timestamp== 0 meanshw_timestampis not available.Quoted 7 lines
Quoted 2 lines
Inside the kernel, the ctx rewriter can generate code to call driver specific function to retrieve the data.
If the driver generates the code to retrieve the data, how's that different from the kfunc approach?
The only difference I see is that it would be a more strong UAPI than the kfuncs?Right. it is a strong uapi.
Quoted 5 lines
Quoted 2 lines
The kfunc approach can be used to *less* common use cases?
What's the advantage of having two approaches when one can cover common and uncommon cases?
Quoted 6 lines
Beyond
hw_timestamp, do we have any other fields ready to support?If it ends up with lots of fields to be accessed by the bpf program, and bpf program actually intends to access these fields, using a strong uapi might be a good thing as it can make code much streamlined.
There are a bunch. Alexander's series has a good list:
https://github.com/alobakin/linux/commit/31bfe8035c995fdf4f1e378b3429d24b96846cc8
We can definitely call some of them more "common" than the others, but not sure how strong of a definition that would be.
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
There are three main drawbacks, AFAICT:
1. It requires driver developers to write and maintain the code that generates the unrolled BPF bytecode to access the metadata fields, which is a non-trivial amount of complexity. Maybe this can be abstracted away with some internal helpers though (like, e.g., a
bpf_xdp_metadata_copy_u64(dst, src, offset)helper which would spit out the required JMP/MOV/LDX instructions?2.
AF_XDPprograms won't be able to access the metadata without using a custom XDP program that calls the kfuncs and puts the data into the metadata area. We could solve this with some code in libxdp, though; if this code can be made generic enough (so it just dumps the available metadata functions from the running kernel at load time), it may be possible to make it generic enough that it will be forward-compatible with new versions of the kernel that add new fields, which should alleviate Florian's concern about keeping things in sync.3. It will make it harder to consume the metadata when building SKBs. I think the CPUMAP and veth use cases are also quite important, and that we want metadata to be available for building SKBs in this path. Maybe this can be resolved by having a convenient kfunc for this that can be used for programs doing such redirects. E.g., you could just call
xdp_copy_metadata_for_skb()before doing thebpf_redirect, and that would recursively expand into all the kfunc calls needed to extract the metadata supported by the SKB path?-Toke
Quoted 10 lines · Stanislav Fomichev
Quoted 9 lines · Yonghong Song
Quoted 7 lines · Stanislav Fomichev
Quoted 6 lines · Yonghong Song
Quoted 4 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Florian
Quoted 1 line · Jakub Kicinski
On Fri, 28 Oct 2022 16:16:17 -0700 John Fastabend wrote:
[...]
Quoted 49 lines
Quoted 49 lines
Quoted 35 lines
Quoted 35 lines
Quoted 7 lines
Quoted 2 lines
All parts of my application (BPF program included) should not be optimized/adjusted for all the different HW variants out there.
Yes, absolutely agreed. Abstracting away those kinds of hardware differences is the whole*point* of having an OS/driver model. I.e., it's what the kernel is there for! If people want to bypass that and get direct access to the hardware, they can already do that by using DPDK.
Quoted 9 lines
So in other words, 100% agreed that we should not expect the BPF developers to deal with hardware details as would be required with a kptr-based interface.
As for the kfunc-based interface, I think it shows some promise. Exposing a list of function names to retrieve individual metadata items instead of a struct layout is sorta comparable in terms of developer UI accessibility etc (IMO).
Quoted 1 line
Quoted 1 line
Quoted 1 line
Looks like there are quite some use cases for
hw_timestamp.Do you think we could add it to the uapi like struct
xdp_md?The following is the current
xdp_md:struct xdp_md { __u32 data; __u32 data_end; __u32 data_meta; /* Below access go through struct xdp_rxq_info */ __u32 ingress_ifindex; /* rxq->dev->ifindex */ __u32 rx_queue_index; /* rxq->queue_index */ __u32 egress_ifindex; /* txq->dev->ifindex */ };We could add
__u64hw_timestampto thexdp_mdso user can just doxdp_md->hw_timestampto get the value.xdp_md->hw_timestamp== 0 meanshw_timestampis not available.Quoted 6 lines
Quoted 2 lines
Inside the kernel, the ctx rewriter can generate code to call driver specific function to retrieve the data.
If the driver generates the code to retrieve the data, how's that different from the kfunc approach?
The only difference I see is that it would be a more strong UAPI than the kfuncs?Right. it is a strong uapi.
Quoted 3 lines
Quoted 1 line
The kfunc approach can be used to*less* common use cases?
What's the advantage of having two approaches when one can cover common and uncommon cases?
Beyond
hw_timestamp, do we have any other fields ready to support?
Quoted 8 lines
Quoted 5 lines
If it ends up with lots of fields to be accessed by the bpf program, and bpf program actually intends to access these fields, using a strong uapi might be a good thing as it can make code much streamlined.
There are a bunch. Alexander's series has a good list:https://github.com/alobakin/linux/commit/31bfe8035c995fdf4f1e378b3429d24b96846cc8
Below are the fields I've identified, which are close to what Alexander also found.
struct xdp_hints_common {
union {
__wsum csum;
struct {
__u16 csum_start;
__u16 csum_offset;
};
};
u16 rx_queue;
u16 vlan_tci;
u32 rx_hash32;
u32 xdp_hints_flags;
u64 btf_full_id; /* BTF object + type ID */} __attribute__((aligned(4))) __attribute__((packed));
Some of the fields are encoded via flags:
enum xdp_hints_flags {
HINT_FLAG_CSUM_TYPE_BIT0 = BIT(0),
HINT_FLAG_CSUM_TYPE_BIT1 = BIT(1),
HINT_FLAG_CSUM_TYPE_MASK = 0x3,
HINT_FLAG_CSUM_LEVEL_BIT0 = BIT(2),
HINT_FLAG_CSUM_LEVEL_BIT1 = BIT(3),
HINT_FLAG_CSUM_LEVEL_MASK = 0xC,
HINT_FLAG_CSUM_LEVEL_SHIFT = 2,
HINT_FLAG_RX_HASH_TYPE_BIT0 = BIT(4),
HINT_FLAG_RX_HASH_TYPE_BIT1 = BIT(5),
HINT_FLAG_RX_HASH_TYPE_MASK = 0x30,
HINT_FLAG_RX_HASH_TYPE_SHIFT = 0x4,
HINT_FLAG_RX_QUEUE = BIT(7),
HINT_FLAG_VLAN_PRESENT = BIT(8),
HINT_FLAG_VLAN_PROTO_ETH_P_8021Q = BIT(9),
HINT_FLAG_VLAN_PROTO_ETH_P_8021AD = BIT(10),
/* Flags from BIT(16) can be used by drivers */};
Quoted 2 lines
We can definitely call some of them more "common" than the others, but not sure how strong of a definition that would be.
The important fields that would be worth considering as UAPI candidates
are: (1) RX-hash, (2) Hash-type and (3) RX-checksum.
With these three we can avoid calling the flow-dissector and GRO frame aggregations works. (This currently hurts xdp_frame to SKB performance a lot in practice).
*BUT* in it's current form above (incl. Alexanders approach/patch) it would be a mistake to UAPI standardize the "(2) Hash-type" in this simplified "reduced" form (which is what the SKB "needs").
There is a huge untapped potential in the Hash-type. Thanks to Microsoft almost all NIC hardware provided a Hash-type that gives us the L3-protocol (IPv4 or IPv6) and the L4-protocol (UDP or TCP and sometimes SCTP), plus info if extention-headers are provided. (Digging in datasheets, we can often also get the header-size).
Think about how many cycles XDP BPF-prog can save parsing protocol headers. I'm also hoping we can leveregate this to allow SKBs created from an xdp_frame to have skb->transport_header and skb->network_header pre-populated (and skip some of these netstack layers).
--Jesper
p.s. in my patchset, I exposed the "raw" Hash-type bits from the descriptor in hope this would evolve.
Martin KaFai Lau
reviewerQuoted 151 lines · Stanislav Fomichev
Quoted 150 lines · Yonghong Song
Quoted 117 lines · Toke Høiland-Jørgensen
Quoted 101 lines · Florian
Hi all,
I was closely following this discussion for some time now. Seems we reached the point where it's getting interesting for me.
Quoted 87 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 28 Oct 2022 16:16:17 -0700 John Fastabend wrote:
Quoted 2 lines
Quoted 2 lines
And it's actually harder to abstract away inter HW generation differences if the user space code has to handle all of it.
I don't see how its any harder in practice though?
You need to find out what HW/FW/config you're running, right? And all you have is a pointer to a blob of unknown type.
Take timestamps for example, some NICs support adjusting the PHC or doing SW corrections (with different versions of hw/fw/server platforms being capable of both/one/neither).
Sure you can extract all this info with tracing and careful inspection via uAPI. But I don't think that's _easier_. And the vendors can't run the results thru their validation (for whatever that's worth).
Quoted 21 lines
Quoted 12 lines
I've had the same concern:
Until we have some userspace library that abstracts all these details, it's not really convenient to use. IIUC, with a kptr, I'd get a blob of data and I need to go through the code and see what particular type it represents for my particular device and how the data I need is represented there. There are also these "if this is device v1 -> use v1 descriptor format; if it's a
v2->usethis another struct; etc" complexities that we'll be pushing onto the users. With kfuncs, we put this burden on the driver developers, but I agree that the drawback here is that we actually have to wait for the implementations to catch up.I agree with everything there, you will get a blob of data and then will need to know what field you want to read using BTF. But, we already do this for BPF programs all over the place so its not a big lift for us. All other BPF tracing/observability requires the same logic. I think users of BPF in general perhaps XDP/tc are the only place left to write BPF programs without thinking about BTF and kernel data structures.
Quoted 7 lines
But, with proposed kptr the complexity lives in userspace and can be fixed, added, updated without having to bother with kernel updates, etc.
From my point of view of supporting Cilium its a win and much preferred
to having to deal with driver owners on all cloud vendors, distributions, and so on.If vendor updates firmware with new fields I get those immediately.
Conversely it's a valid concern that those who *do* actually update their kernel regularly will have more things to worry about.
Quoted 8 lines
Quoted 4 lines
Jakub mentions FW and I haven't even thought about that; so yeah, bpf programs might have to take a lot of other state into consideration when parsing the descriptors; all those details do seem like they belong to the driver code.
I would prefer to avoid being stuck on requiring driver writers to be involved. With just a kptr I can support the device and any firwmare versions without requiring help.
1) where are you getting all those HW / FW specs :S
2) maybe *you* can but you're not exactly not an ex-driver developer :SQuoted 10 lines
Quoted 4 lines
Feel free to send it early with just a handful of drivers implemented; I'm more interested about bpf/af_xdp/user api story; if we have some nice sample/test case that shows how the metadata can be used, that might push us closer to the agreement on the best way to proceed.
I'll try to do a intel and mlx implementation to get a cross section. I have a good collection of nics here so should be able to show a couple firmware versions. It could be fine I think to have the raw kptr access and then also kfuncs for some things perhaps.
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
I'd prefer if we left the door open for new vendors. Punting descriptor parsing to user space will indeed result in what you just said - major vendors are supported and that's it.
I'm not sure about why it would make it harder for new vendors? I think the opposite,
TBH I'm only replying to the email because of the above part :) I thought this would be self evident, but I guess our perspectives are different.
Perhaps you look at it from the perspective of SW running on someone else's cloud, an being able to move to another cloud, without having to worry if feature X is available in xdp or just skb.
Quoted 5 lines
I look at it from the perspective of maintaining a cloud, with people writing random XDP applications. If I swap a NIC from an incumbent to a (superior) startup, and cloud users are messing with raw descriptor - I'd need to go find every XDP program out there and make sure it understands the new descriptors.
Here is another perspective:
Quoted 8 lines
As
AF_XDPapplication developer I don't wan't to deal with the underlying hardware in detail. I like to request a feature from the OS (in this case rx/tx timestamping). If the feature is available I will simply use it, if not I might have to work around it - maybe by falling back to SW timestamping.All parts of my application (BPF program included) should not be optimized/adjusted for all the different HW variants out there.
Yes, absolutely agreed. Abstracting away those kinds of hardware differences is the whole *point* of having an OS/driver model. I.e., it's what the kernel is there for! If people want to bypass that and get direct access to the hardware, they can already do that by using DPDK.
Quoted 8 lines
So in other words, 100% agreed that we should not expect the BPF developers to deal with hardware details as would be required with a kptr-based interface.
As for the kfunc-based interface, I think it shows some promise. Exposing a list of function names to retrieve individual metadata items instead of a struct layout is sorta comparable in terms of developer UI accessibility etc (IMO).
Looks like there are quite some use cases for
hw_timestamp. Do you think we could add it to the uapi like structxdp_md?The following is the current
xdp_md:struct xdp_md { __u32 data; __u32 data_end; __u32 data_meta; /* Below access go through struct xdp_rxq_info */ __u32 ingress_ifindex; /* rxq->dev->ifindex */ __u32 rx_queue_index; /* rxq->queue_index */ __u32 egress_ifindex; /* txq->dev->ifindex */ };We could add
__u64hw_timestampto thexdp_mdso user can just doxdp_md->hw_timestampto get the value.xdp_md->hw_timestamp== 0 meanshw_timestampis not available.
Quoted 7 lines
Quoted 2 lines
Inside the kernel, the ctx rewriter can generate code to call driver specific function to retrieve the data.
If the driver generates the code to retrieve the data, how's that different from the kfunc approach?
The only difference I see is that it would be a more strong UAPI than the kfuncs?
Another thing may be worth considering, some hints for some HW/driver may be harder (or may not worth) to unroll/inline. For example, I see driver is doing spin_lock_bh while getting the hwtstamp. For this case, keep calling a kfunc and avoid the unroll/inline may be the right thing to do.
Quoted 35 lines
Quoted 2 lines
The kfunc approach can be used to *less* common use cases?
What's the advantage of having two approaches when one can cover common and uncommon cases?
Quoted 29 lines
Quoted 29 lines
There are three main drawbacks, AFAICT:
1. It requires driver developers to write and maintain the code that generates the unrolled BPF bytecode to access the metadata fields, which is a non-trivial amount of complexity. Maybe this can be abstracted away with some internal helpers though (like, e.g., a
bpf_xdp_metadata_copy_u64(dst, src, offset)helper which would spit out the required JMP/MOV/LDX instructions?2.
AF_XDPprograms won't be able to access the metadata without using a custom XDP program that calls the kfuncs and puts the data into the metadata area. We could solve this with some code in libxdp, though; if this code can be made generic enough (so it just dumps the available metadata functions from the running kernel at load time), it may be possible to make it generic enough that it will be forward-compatible with new versions of the kernel that add new fields, which should alleviate Florian's concern about keeping things in sync.3. It will make it harder to consume the metadata when building SKBs. I think the CPUMAP and veth use cases are also quite important, and that we want metadata to be available for building SKBs in this path. Maybe this can be resolved by having a convenient kfunc for this that can be used for programs doing such redirects. E.g., you could just call
xdp_copy_metadata_for_skb()before doing thebpf_redirect, and that would recursively expand into all the kfunc calls needed to extract the metadata supported by the SKB path?-Toke
Quoted 161 lines · Martin KaFai Lau
Quoted 151 lines · Stanislav Fomichev
Quoted 150 lines · Yonghong Song
Quoted 117 lines · Toke Høiland-Jørgensen
Quoted 101 lines · Florian
Hi all,
I was closely following this discussion for some time now. Seems we reached the point where it's getting interesting for me.
Quoted 87 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 28 Oct 2022 16:16:17 -0700 John Fastabend wrote:
Quoted 2 lines
Quoted 2 lines
And it's actually harder to abstract away inter HW generation differences if the user space code has to handle all of it.
I don't see how its any harder in practice though?
You need to find out what HW/FW/config you're running, right? And all you have is a pointer to a blob of unknown type.
Take timestamps for example, some NICs support adjusting the PHC or doing SW corrections (with different versions of hw/fw/server platforms being capable of both/one/neither).
Sure you can extract all this info with tracing and careful inspection via uAPI. But I don't think that's _easier_. And the vendors can't run the results thru their validation (for whatever that's worth).
Quoted 21 lines
Quoted 12 lines
I've had the same concern:
Until we have some userspace library that abstracts all these details, it's not really convenient to use. IIUC, with a kptr, I'd get a blob of data and I need to go through the code and see what particular type it represents for my particular device and how the data I need is represented there. There are also these "if this is device v1 -> use v1 descriptor format; if it's a
v2->usethis another struct; etc" complexities that we'll be pushing onto the users. With kfuncs, we put this burden on the driver developers, but I agree that the drawback here is that we actually have to wait for the implementations to catch up.I agree with everything there, you will get a blob of data and then will need to know what field you want to read using BTF. But, we already do this for BPF programs all over the place so its not a big lift for us. All other BPF tracing/observability requires the same logic. I think users of BPF in general perhaps XDP/tc are the only place left to write BPF programs without thinking about BTF and kernel data structures.
Quoted 7 lines
But, with proposed kptr the complexity lives in userspace and can be fixed, added, updated without having to bother with kernel updates, etc.
From my point of view of supporting Cilium its a win and much preferred
to having to deal with driver owners on all cloud vendors, distributions, and so on.If vendor updates firmware with new fields I get those immediately.
Conversely it's a valid concern that those who *do* actually update their kernel regularly will have more things to worry about.
Quoted 8 lines
Quoted 4 lines
Jakub mentions FW and I haven't even thought about that; so yeah, bpf programs might have to take a lot of other state into consideration when parsing the descriptors; all those details do seem like they belong to the driver code.
I would prefer to avoid being stuck on requiring driver writers to be involved. With just a kptr I can support the device and any firwmare versions without requiring help.
1) where are you getting all those HW / FW specs :S
2) maybe *you* can but you're not exactly not an ex-driver developer :SQuoted 10 lines
Quoted 4 lines
Feel free to send it early with just a handful of drivers implemented; I'm more interested about bpf/af_xdp/user api story; if we have some nice sample/test case that shows how the metadata can be used, that might push us closer to the agreement on the best way to proceed.
I'll try to do a intel and mlx implementation to get a cross section. I have a good collection of nics here so should be able to show a couple firmware versions. It could be fine I think to have the raw kptr access and then also kfuncs for some things perhaps.
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
I'd prefer if we left the door open for new vendors. Punting descriptor parsing to user space will indeed result in what you just said - major vendors are supported and that's it.
I'm not sure about why it would make it harder for new vendors? I think the opposite,
TBH I'm only replying to the email because of the above part :) I thought this would be self evident, but I guess our perspectives are different.
Perhaps you look at it from the perspective of SW running on someone else's cloud, an being able to move to another cloud, without having to worry if feature X is available in xdp or just skb.
Quoted 5 lines
I look at it from the perspective of maintaining a cloud, with people writing random XDP applications. If I swap a NIC from an incumbent to a (superior) startup, and cloud users are messing with raw descriptor - I'd need to go find every XDP program out there and make sure it understands the new descriptors.
Here is another perspective:
Quoted 8 lines
As
AF_XDPapplication developer I don't wan't to deal with the underlying hardware in detail. I like to request a feature from the OS (in this case rx/tx timestamping). If the feature is available I will simply use it, if not I might have to work around it - maybe by falling back to SW timestamping.All parts of my application (BPF program included) should not be optimized/adjusted for all the different HW variants out there.
Yes, absolutely agreed. Abstracting away those kinds of hardware differences is the whole *point* of having an OS/driver model. I.e., it's what the kernel is there for! If people want to bypass that and get direct access to the hardware, they can already do that by using DPDK.
Quoted 8 lines
So in other words, 100% agreed that we should not expect the BPF developers to deal with hardware details as would be required with a kptr-based interface.
As for the kfunc-based interface, I think it shows some promise. Exposing a list of function names to retrieve individual metadata items instead of a struct layout is sorta comparable in terms of developer UI accessibility etc (IMO).
Looks like there are quite some use cases for
hw_timestamp. Do you think we could add it to the uapi like structxdp_md?The following is the current
xdp_md:struct xdp_md { __u32 data; __u32 data_end; __u32 data_meta; /* Below access go through struct xdp_rxq_info */ __u32 ingress_ifindex; /* rxq->dev->ifindex */ __u32 rx_queue_index; /* rxq->queue_index */ __u32 egress_ifindex; /* txq->dev->ifindex */ };We could add
__u64hw_timestampto thexdp_mdso user can just doxdp_md->hw_timestampto get the value.xdp_md->hw_timestamp== 0 meanshw_timestampis not available.Quoted 7 lines
Quoted 2 lines
Inside the kernel, the ctx rewriter can generate code to call driver specific function to retrieve the data.
If the driver generates the code to retrieve the data, how's that different from the kfunc approach?
The only difference I see is that it would be a more strong UAPI than the kfuncs?
Quoted 4 lines
Another thing may be worth considering, some hints for some HW/driver may be harder (or may not worth) to unroll/inline. For example, I see driver is doing
spin_lock_bhwhile getting the hwtstamp. For this case, keep calling a kfunc and avoid the unroll/inline may be the right thing to do.
Yeah, I'm trying to look at the drivers right now and doing spinlocks/seqlocks might complicate the story...
But it seems like in the worst case, the unrolled bytecode can always resort to calling a kernel function?
(we might need to have some scratch area to preserve r1-r5 and we can't touch r6-r9 because we are not in a real call, but seems doable; I'll try to illustrate with a bunch of examples)
Quoted 35 lines
Quoted 34 lines
Quoted 1 line
The kfunc approach can be used to *less* common use cases?
What's the advantage of having two approaches when one can cover common and uncommon cases?
Quoted 29 lines
Quoted 29 lines
There are three main drawbacks, AFAICT:
1. It requires driver developers to write and maintain the code that generates the unrolled BPF bytecode to access the metadata fields, which is a non-trivial amount of complexity. Maybe this can be abstracted away with some internal helpers though (like, e.g., a
bpf_xdp_metadata_copy_u64(dst, src, offset)helper which would spit out the required JMP/MOV/LDX instructions?2.
AF_XDPprograms won't be able to access the metadata without using a custom XDP program that calls the kfuncs and puts the data into the metadata area. We could solve this with some code in libxdp, though; if this code can be made generic enough (so it just dumps the available metadata functions from the running kernel at load time), it may be possible to make it generic enough that it will be forward-compatible with new versions of the kernel that add new fields, which should alleviate Florian's concern about keeping things in sync.3. It will make it harder to consume the metadata when building SKBs. I think the CPUMAP and veth use cases are also quite important, and that we want metadata to be available for building SKBs in this path. Maybe this can be resolved by having a convenient kfunc for this that can be used for programs doing such redirects. E.g., you could just call
xdp_copy_metadata_for_skb()before doing thebpf_redirect, and that would recursively expand into all the kfunc calls needed to extract the metadata supported by the SKB path?-Toke
Martin KaFai Lau
reviewerQuoted 38 lines · Stanislav Fomichev
Quoted 33 lines
Quoted 25 lines
Quoted 25 lines
Quoted 4 lines
As for the kfunc-based interface, I think it shows some promise. Exposing a list of function names to retrieve individual metadata items instead of a struct layout is sorta comparable in terms of developer UI accessibility etc (IMO).
Looks like there are quite some use cases for
hw_timestamp. Do you think we could add it to the uapi like structxdp_md?The following is the current
xdp_md:struct xdp_md { __u32 data; __u32 data_end; __u32 data_meta; /* Below access go through struct xdp_rxq_info */ __u32 ingress_ifindex; /* rxq->dev->ifindex */ __u32 rx_queue_index; /* rxq->queue_index */ __u32 egress_ifindex; /* txq->dev->ifindex */ };We could add
__u64hw_timestampto thexdp_mdso user can just doxdp_md->hw_timestampto get the value.xdp_md->hw_timestamp== 0 meanshw_timestampis not available.Quoted 7 lines
Quoted 2 lines
Inside the kernel, the ctx rewriter can generate code to call driver specific function to retrieve the data.
If the driver generates the code to retrieve the data, how's that different from the kfunc approach?
The only difference I see is that it would be a more strong UAPI than the kfuncs?Quoted 4 lines
Another thing may be worth considering, some hints for some HW/driver may be harder (or may not worth) to unroll/inline. For example, I see driver is doing
spin_lock_bhwhile getting the hwtstamp. For this case, keep calling a kfunc and avoid the unroll/inline may be the right thing to do.
Quoted 4 lines
Yeah, I'm trying to look at the drivers right now and doing spinlocks/seqlocks might complicate the story...
But it seems like in the worst case, the unrolled bytecode can always resort to calling a kernel function?
unroll the common cases and call kernel function for everything else? that should be doable. The bpf prog calling it as kfunc will have more flexibility like this here.
Quoted 40 lines
(we might need to have some scratch area to preserve r1-r5 and we can't touch r6-r9 because we are not in a real call, but seems doable; I'll try to illustrate with a bunch of examples)
Quoted 35 lines
Quoted 34 lines
Quoted 1 line
The kfunc approach can be used to *less* common use cases?
What's the advantage of having two approaches when one can cover common and uncommon cases?
Quoted 29 lines
Quoted 29 lines
There are three main drawbacks, AFAICT:
1. It requires driver developers to write and maintain the code that generates the unrolled BPF bytecode to access the metadata fields, which is a non-trivial amount of complexity. Maybe this can be abstracted away with some internal helpers though (like, e.g., a
bpf_xdp_metadata_copy_u64(dst, src, offset)helper which would spit out the required JMP/MOV/LDX instructions?2.
AF_XDPprograms won't be able to access the metadata without using a custom XDP program that calls the kfuncs and puts the data into the metadata area. We could solve this with some code in libxdp, though; if this code can be made generic enough (so it just dumps the available metadata functions from the running kernel at load time), it may be possible to make it generic enough that it will be forward-compatible with new versions of the kernel that add new fields, which should alleviate Florian's concern about keeping things in sync.3. It will make it harder to consume the metadata when building SKBs. I think the CPUMAP and veth use cases are also quite important, and that we want metadata to be available for building SKBs in this path. Maybe this can be resolved by having a convenient kfunc for this that can be used for programs doing such redirects. E.g., you could just call
xdp_copy_metadata_for_skb()before doing thebpf_redirect, and that would recursively expand into all the kfunc calls needed to extract the metadata supported by the SKB path?-Toke
John Fastabend
maintainerQuoted 8 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 28 Oct 2022 16:16:17 -0700 John Fastabend wrote:
Quoted 2 lines
Quoted 2 lines
And it's actually harder to abstract away inter HW generation differences if the user space code has to handle all of it.
I don't see how its any harder in practice though?
You need to find out what HW/FW/config you're running, right? And all you have is a pointer to a blob of unknown type.
Yep. I guess I'm in the position of already having to do this somewhat to collect stats from the device. Although its maybe a bit more involved here by vendors that are versioning the descriptors.
Also nit, its not unknown type we know the full type by BTF.
Quoted 4 lines
Take timestamps for example, some NICs support adjusting the PHC or doing SW corrections (with different versions of hw/fw/server platforms being capable of both/one/neither).
Its worse actually.
Having started to do this timestamping it is not at all consistent across nics so I think we are stuck having to know hw specifics here regardless. Also some nics will timestamp all RX pkts, some specific pkts, some require configuration to decide which mode to run in and so on. You then end up with a matrix of features supported by hw/fw/sw and desired state and I can't see any way around this.
Quoted 5 lines
Sure you can extract all this info with tracing and careful inspection via uAPI. But I don't think that's _easier_. And the vendors can't run the results thru their validation (for whatever that's worth).
I think you hit our point of view differences below. See I don't want to depend on the vendor. I want access to the fields otherwise I'm stuck working with vendors on their time frames. You have the other perspective of supporting the NIC and ability to update kernels where as I still live with 4.18/4.19 kernels (even 4.14 sometimes). So what we land now needs to work in 5 years still.
Quoted 28 lines
Quoted 28 lines
Quoted 13 lines
I've had the same concern:
Until we have some userspace library that abstracts all these details, it's not really convenient to use. IIUC, with a kptr, I'd get a blob of data and I need to go through the code and see what particular type it represents for my particular device and how the data I need is represented there. There are also these "if this is device v1 -> use v1 descriptor format; if it's a
v2->usethis another struct; etc" complexities that we'll be pushing onto the users. With kfuncs, we put this burden on the driver developers, but I agree that the drawback here is that we actually have to wait for the implementations to catch up.I agree with everything there, you will get a blob of data and then will need to know what field you want to read using BTF. But, we already do this for BPF programs all over the place so its not a big lift for us. All other BPF tracing/observability requires the same logic. I think users of BPF in general perhaps XDP/tc are the only place left to write BPF programs without thinking about BTF and kernel data structures.
But, with proposed kptr the complexity lives in userspace and can be fixed, added, updated without having to bother with kernel updates, etc. From my point of view of supporting Cilium its a win and much preferred to having to deal with driver owners on all cloud vendors, distributions, and so on.
Quoted 4 lines
Quoted 1 line
If vendor updates firmware with new fields I get those immediately.
Conversely it's a valid concern that those who *do* actually update their kernel regularly will have more things to worry about.
I'm not sure if a kptr_func is any harder to write than a user space relocation for that func? In tetragon and cilium we've done userspace rewrites for some time. Happy to generalize that infra into kernel repo if that helps. IMO having a libhw.h in kernel tree ./tools/bpf directory would work.
Quoted 11 lines
Quoted 9 lines
Quoted 5 lines
Jakub mentions FW and I haven't even thought about that; so yeah, bpf programs might have to take a lot of other state into consideration when parsing the descriptors; all those details do seem like they belong to the driver code.
I would prefer to avoid being stuck on requiring driver writers to be involved. With just a kptr I can support the device and any firwmare versions without requiring help.
1) where are you getting all those HW / FW specs :S
Most are public docs of course vendors have internal docs with more details but what can you do. Also source code has the structs.
Quoted 1 line
2) maybe *you* can but you're not exactly not an ex-driver developer :S
Sure :) but we put a libhw.h file in kernel and test with selftests (which will be hard without hardware) and then not everyone needs to be a driver internals expert.
Quoted 15 lines
Quoted 15 lines
Quoted 5 lines
Feel free to send it early with just a handful of drivers implemented; I'm more interested about bpf/af_xdp/user api story; if we have some nice sample/test case that shows how the metadata can be used, that might push us closer to the agreement on the best way to proceed.
I'll try to do a intel and mlx implementation to get a cross section. I have a good collection of nics here so should be able to show a couple firmware versions. It could be fine I think to have the raw kptr access and then also kfuncs for some things perhaps.
Quoted 3 lines
Quoted 3 lines
I'd prefer if we left the door open for new vendors. Punting descriptor parsing to user space will indeed result in what you just said - major vendors are supported and that's it.
Quoted 6 lines
Quoted 2 lines
I'm not sure about why it would make it harder for new vendors? I think the opposite,
TBH I'm only replying to the email because of the above part :) I thought this would be self evident, but I guess our perspectives are different.
Yep.
Quoted 4 lines
Perhaps you look at it from the perspective of SW running on someone else's cloud, an being able to move to another cloud, without having to worry if feature X is available in xdp or just skb.
Exactly. I have SW running in a data center or cloud for a security team or ops team and they don't own the platform usually. Anyways the platform team is going to stay on a LTS kernel for at least a year or two most likely. I maintain the SW and some 3rd party nic vendor may not even know about my SW (cilium/tetragon).
Quoted 6 lines
I look at it from the perspective of maintaining a cloud, with people writing random XDP applications. If I swap a NIC from an incumbent to a (superior) startup, and cloud users are messing with raw descriptor - I'd need to go find every XDP program out there and make sure it understands the new descriptors.
I get it. Its interesting that you wouldn't tell the XDP programmers to deal with it which is my case. My $.02 is a userspace lib could abstract this easier than a kernel func and also add new features without rolling new kernels.
Quoted 4 lines
There is a BPF foundation or whatnot now - what about starting a certification program for cloud providers and making it clear what features must be supported to be compatible with XDP 1.0, XDP 2.0 etc?
Maybe but still stuck on kernel versions.
Quoted 5 lines
Quoted 2 lines
it would be easier because I don't need vendor support at all.
Can you support the enfabrica NIC on day 1? :) To an extent, its just shifting the responsibility from the HW vendor to the middleware vendor.
Yep. With important detail that I can run new features on old kernels even if vendor didn't add rxhash or timestamp out the gate.
Nothing stops the hw vendor from contributing to this in kernel source bpf lib with the features though.
Quoted 6 lines
Quoted 2 lines
Thinking it over seems there could be room for both.
Are you thinking more or less Stan's proposal but with one of the callbacks being "give me the raw thing"? Probably as a ro dynptr? Possible, but I don't think we need to hold off Stan's work.
Yeah that was my thinking. Both could coexist. OTOH doing it in BPF program lib side seems cleaner to me from a kernel maitenance and hardware support. We've had trouble getting drivers to support XDP features so adding more requirements of entry seems problematic to me when we can avoid it.