Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: Add buffer pool to replace connection allocation #10

Merged
merged 2 commits into from
Jan 1, 2025

Conversation

wengzhe
Copy link
Member

@wengzhe wengzhe commented Dec 19, 2024

Summary

Patches included:

  • net: Add buffer pool to replace connection allocation
  • net: Optimize ipfwd and wrbuffer by buffer pool

Our net socket connection allocations are powerful but redundant because they're implemented once in each protocol. This is not good for further optimizing and extending to other allocations, so maybe we can add a common implementation for the usage.

Impact

  1. We add a struct net_bufpool_s as pool descriptor, which may use a little bit more memory than previous implementation (~28Bytes).
  2. We share same functions between pools, so code size may shrink under some scenarios.

Testing

Mainly on SIM.

  • tcp/udp/icmp/devif_callback tested with different configs, both conn and wrbuffer pools.
  • Other pools share exactly the same logic, not tested very carefully.

@CLAassistant
Copy link

CLAassistant commented Dec 19, 2024

CLA assistant check
All committers have signed the CLA.

@wengzhe
Copy link
Member Author

wengzhe commented Dec 24, 2024

Already merged as apache/nuttx#15285

Our net socket connection allocations are powerful but redundant
because they're implemented once in each protocol.  This is not good for
further optimizing and extending to other allocations, so maybe we can
add a common implementation for the usage.

Impact:
1. We add a `struct net_bufpool_s` as pool descriptor, which may use a
   little bit more memory than previous implementation (~28Bytes).
2. We share same functions between pools, so code size may shrink under
   some scenarios.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
To allow dynamic allocation of these structs, performance keeps the same
as the previous implementation.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
net/bluetooth/bluetooth_conn.c Show resolved Hide resolved
net/can/can_conn.c Show resolved Hide resolved

/* If we get here, then we didn't exceed maxalloc. */

if (pool->dynalloc > 0 && sq_peek(&pool->freebuffers) == NULL)
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we hold lock before accessing freebuffers?

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently, the bufpool is not protecting itself and the caller holds the lock(e.g. g_free_lock or net_lock) if the caller wants to use it concurrently. This patch also helps us find out that some protocol uses dedicated lock while others use net lock, which we may optimize later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

should move g_free_lock to mem pool

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not easy to do this, take UDP as an example, UDP's g_free_lock protects not only the conn alloc, but also other list like g_active_udp_connections, but active list is not common for all protocols/buffers. So I just put the lock optimization into the work item, replacing the wrb optimization.

BTW, we don't really need to make sure every data structure is thread-safe (e.g. queues, hashtables, etc.), but yes, it will perform better if we lock inside instead of lock by caller.

@@ -89,6 +116,26 @@ enum tv2ds_remainder_e
TV2DS_CEIL /* Force to next larger full decisecond */
};

/* This structure is used to manage a pool of network buffers */

struct net_bufpool_s
Copy link
Collaborator

Choose a reason for hiding this comment

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

it's useful, so we move to mm folder?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we can move it anytime because we are always using NET_BUFPOOL_* macros.

net/tcp/Kconfig Show resolved Hide resolved
net/udp/Kconfig Show resolved Hide resolved
net/utils/net_bufpool.c Show resolved Hide resolved
net/utils/net_bufpool.c Show resolved Hide resolved
@xiaoxiang781216 xiaoxiang781216 merged commit b2ca642 into open-vela:dev Jan 1, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants