- Notifications
You must be signed in to change notification settings - Fork 8.2k
lib: net_buf: usage helper functions #98725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
pdgendt left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit
lib/net_buf/buf.c Outdated
| { | ||
| int num_free = 0; | ||
| | ||
| __ASSERT_NO_MSG(pool); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| __ASSERT_NO_MSG(pool); | |
| __ASSERT_NO_MSG(pool != NULL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied the pattern from the next function, I can update both or neither
| I'm a bit hesitant about adding this, for a couple of reasons:
Unrelated to my reservations, but just something I initially reacted to: not having "pool" in the name (i.e. |
It's not racy, its an instantaneous snapshot. Nothing will be corrupted with multiple users. Its the same with querying any other shared object in Zephyr. We still have the atomic API even though This is solving an issue that can't currently be solved (without wrapping the entire net_buf API). For a given pool, is it close to running out of buffers to hand out? The goal is to be proactive about rate limiting, instead of only reacting once allocation fails, which in some cases is way too late due to data in flight. In that scenario it doesn't really matter whether the value is 2, 3, or 4 because some other thread freed a buffer, it just matters that the number is low, not high. |
I didn't intend to suggest that anything would be corrupted. My point was that the moment the caller gets the value it may not reflect reality anymore, so any code that makes such assumptions would be prone to unexpected behavior.
This is very useful information. Knowing the usecase opens up the possibility for considering various other kinds of solutions, even if the proposal here would be the best one. Would it e.g. make sense to have some low water mark callback for the pool, where you define some minimum and when the available buffer count goes below that the app gets notified? Since the net_buf API allows defining custom pool implementations, that could also potentially have been another way to approach solving this. |
Possibly, but it would be quite a different solution:
Possible? sure. As simple as a |
| IMO this is roughly the same behavior as every kernel function that can take |
include/zephyr/net_buf.h Outdated
| /** Number of free buffers */ | ||
| uint16_t free_count; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that there is already avail_count variable that gives this information, one just needs to enable CONFIG_NET_BUF_POOL_USAGE to access it.
10b88eb to c56d708 Compare | Thanks to @jukkar's comment, PR has been updated to simply provide wrapper functions around the existing functionality. I should have paid attention to the code 4 lines below where I was typing. |
net_buf_num_freeAdd helper functions for querying the internal fields of `struct net_buf_pool` when `CONFIG_NET_BUF_POOL_USAGE` is enabled. Signed-off-by: Jordan Yates <jordan@embeint.com>
c56d708 to 6fc4c58 Compare |
| CI will fail until #98755 is merged (weird co-incidence, the PRs had nothing to do with each other) |
jhedberg left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to get back to this after 4.3 (as this is a feature and not a bug fix), but since there are starting to be approvals from others I thought I'd save your time and get this API detail objection posted already now. Other than that I don't have any objections to this anymore, however I'm not sure what we'll do with these when we have a pool that allocates the net_buf structs from a heap, i.e. where you really don't know these limits until you hit an allocation failure.
| static inline uint16_t net_buf_get_available(struct net_buf_pool *pool) | ||
| { | ||
| return (uint16_t)atomic_get(&pool->avail_count); | ||
| } | ||
| | ||
| /** | ||
| * @brief Get the maximum number of buffers simultaneously claimed from a pool. | ||
| * | ||
| * @kconfig_dep{CONFIG_NET_BUF_POOL_USAGE} | ||
| * | ||
| * @param pool Which pool to check | ||
| * | ||
| * @return Maximum number of buffers simultaneously claimed from the pool | ||
| */ | ||
| static inline uint16_t net_buf_get_max_used(struct net_buf_pool *pool) | ||
| { | ||
| return pool->max_used; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value types of these should likely be size_t. That has been the general approach with net_buf APIs from the beginning, i.e. the internal types may be uint16_t, but as that's a pure memory footprint optimization it shouldn't be visible in the public API. Also see #98144.



Add helper functions for querying the internal fields of
struct net_buf_poolwhenCONFIG_NET_BUF_POOL_USAGEis enabled.