Skip to content

Conversation

@EvgeniiMekhanik
Copy link
Contributor

No description provided.

@EvgeniiMekhanik EvgeniiMekhanik force-pushed the MekhanikEvgenii/fix-1346-1 branch from a62787c to c9bba36 Compare July 2, 2025 09:46
fw/ss_skb.c Outdated
__u8 pfmemalloc = skb->pfmemalloc;

WARN_ON_ONCE(skb->sk);
skb_orphan(skb);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please pay attention on this place. Here we release skb owner and decrease client->mem. This function ss_skb_init_for_xmit is called before push skb to the socket write queue. So all skbs in socket write queue are not taken into account for client memory calculation. We release skb owner here, because if don't do it we need a rather big kernel patch to adjust skb memory before it will be passed to socket write queue. @krizhanovsky @const-t what do you think about it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't we make a pointer to a client accounting in skb->cb instead of to play with skb_orphan()? I'd prefer to avoid this since we can get plenty of crashes in this patch or in later kernel version migrations due to breaking kernel logic about orphaned skbs.

@krizhanovsky krizhanovsky mentioned this pull request Jul 7, 2025
2 tasks
@EvgeniiMekhanik EvgeniiMekhanik marked this pull request as draft July 8, 2025 09:12
@EvgeniiMekhanik EvgeniiMekhanik force-pushed the MekhanikEvgenii/fix-1346-1 branch from c9bba36 to 26e3525 Compare July 8, 2025 09:12
@EvgeniiMekhanik EvgeniiMekhanik force-pushed the MekhanikEvgenii/fix-1346-1 branch 16 times, most recently from 40654f8 to e2de424 Compare July 11, 2025 14:09
@EvgeniiMekhanik EvgeniiMekhanik marked this pull request as ready for review July 11, 2025 14:09
@EvgeniiMekhanik EvgeniiMekhanik force-pushed the MekhanikEvgenii/fix-1346-1 branch 5 times, most recently from 7b5e367 to ac06de7 Compare July 14, 2025 21:12
@EvgeniiMekhanik EvgeniiMekhanik force-pushed the MekhanikEvgenii/fix-1346-1 branch from 4ea17b3 to dedff25 Compare January 23, 2026 08:30
@EvgeniiMekhanik EvgeniiMekhanik marked this pull request as ready for review January 23, 2026 08:30
client_mem <soft_limit> <hard_limit> - controls haw many
memory is used to store unanswered client requests and
requests with linked responses which can not be forwarded
to a client.
To track socket memory we should pass TfwHttpMsg * not
TfwMsgIter * to most of http_nsg_* functions, because
TfwHttpMsg has a pointer to connection and socket.
In task #498 we decide to use `client_mem` option to limit
count of memory used by client. This commit is a part of
this task - now Tempesta FW uses `sk->sk_rmem_alloc` to
adjust memory used by Tempesta FW for this client connection.
In task #498 we decide to use `client_mem` option to limit
count of memory used by client. This commit is a part of
this task and the next step of implementaion. Previosly
Tempesta FW uses `sk->sk_rmem_alloc` to adjust memory
used by Tempesta FW for this client connection, now we
adjust memory for the whole TfwClient, because the can
be a lot of connection for one client and for all other
cases we use limitation for TfwClient and block it if
necessary.
If administrator specify `client_mem` and the memory
used by all connection of current client exceeded this
value Tempesta FW drops connection and block client
by ip if `ip_block on;` is specified.
Previosuly we get connection when we adjust memory
for skb, but it leads to several problems:
- we can't adjust memory for skb before tls decryption,
because skb from `tls->io_in.skb_list` are freed
during connection released (but connection will be
never released if we increment it's reference counter
for these skbs).
- We have the same problems for skbs, which are wait
for appropriate tcp window to be pushed in socket
write queue.
Now we increment/decrement reference counter for
TfwClient and adjust skb memory for requests before
tls decryption.
Previously we adjust tcp send window only for http2
connection and only during making HEADER or DATA frames,
but if we want to control client memory usage we should
do it for all type of sending data. (We orphane skb and
decrease memory usage when we pass skb to the socket
write queue, so we we don't adjust tcp send window we
push a lot of skbs in socket write queue and don't
adjust it's memory).
- remove `client_get_light/client_put_light` functions,
  because after removing lock from `client` structure
  we don't need these functions at all.
- Adjust memory usage of skb in `skb->cb`. Usually it
  is equal to `skb->truesize, but for some cases (
  skb which was created by `pskb_copy_for_clone` for
  example it is different).
@EvgeniiMekhanik EvgeniiMekhanik force-pushed the MekhanikEvgenii/fix-1346-1 branch 4 times, most recently from 2ad5039 to 33eacfa Compare January 26, 2026 10:11
- use `skb_shift` instead of `skb_try_coalesce` to
  correctly adjust send window during entailing skb
  to socket write queue.
- adjust FRAME_HEADER_SIZE during calculation send window
  during making frames. (There was a mistake with accuracy
  of send window calculation, we don't take into account,
  that each frame also contains frame header).
- change BUG_ON to WARN_ON.
- rename tfw_cli_*_limit to tfw_cli_*_mem_limit
- rename `ss_skb_can_collapce` to `ss_skb_can_collapse`
- rename `tfw_h2_or_stream_wnd_is_exceeded` to
  `tfw_h2_conn_or_stream_wnd_is_exceeded`.
- move braces `{` to the next line.
- rename `ss_skb_adjust_sk_mem` to `ss_skb_adjust_client_mem`.
- Do not dublicate code for http1 and http2 in
  `tfw_connection_push`.
- Change BUG_ON to WARN_ON in some places.
We should not call `tcp_skb_pcount_set` during skb splitting,
if skb belongs to Tempesta (not pushed to socket write queue),
because we use skb->cb for our own purposes. And `ss_skb_split`
never called from `tfw_tls_encrypt` (`tfw_tls_encrypt` is only
one function which is called from Tempesta FW code, when skb is
already pushed to socket write queue), so we can totally removed
`tcp_skb_pcount_set`.
Do not use `skb->sk` and `skb->destructor` to check
memory used by skb, use `skb->cb` for this purposes.
- Implement our own version of `skb_orphan` with
name `ss_skb_orphan` which is called when skb is freed
in Tempesta FW code our just before pushing skb to
socket write queue.
- Implement wrappers over `__kfree_skb` and `kfree_skb`
  where we call `ss_skb_orphan` before free skb.
- Check that skb is pushed to socket write queue, using
  new ipmlemented function `skb_tfw_is_in_socket_write_queue`
  from linux kernel, to skip adjusting memory used be skb,
  when it belongs to kernel (when `ss_skb_*` functions
  called from `tls_encrypt`).
@EvgeniiMekhanik EvgeniiMekhanik force-pushed the MekhanikEvgenii/fix-1346-1 branch from 33eacfa to 72b72c4 Compare January 26, 2026 10:33
@EvgeniiMekhanik EvgeniiMekhanik marked this pull request as draft January 26, 2026 11:40
- Usually we use callbacks which are set in `skb->cb`
  for different purposes. So remove to callbacks, which
  was added in previous patches and use callbacks saved
  in `skb->cb`.
@EvgeniiMekhanik EvgeniiMekhanik force-pushed the MekhanikEvgenii/fix-1346-1 branch from fc229fb to 443d7da Compare January 26, 2026 14:12
@EvgeniiMekhanik EvgeniiMekhanik marked this pull request as ready for review January 26, 2026 14:13
- Since we use pool for http memory allocation, change
  api of all `tfw_pool_*` functions to pass `TfwClient`
  and accounting memory in this structure.
- Remove `TfwClient` refcounter (it not used, can be done
  in previous commits).
- Fix unit tests to check memory accounting, cleanup memory
  after each test, to check that client memory is equal to
  zero after test.
@EvgeniiMekhanik EvgeniiMekhanik force-pushed the MekhanikEvgenii/fix-1346-1 branch from 3b27521 to 19362d3 Compare January 29, 2026 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants