net-next: frags: dynamic timeout under load
Subsystems (6)
PATCH 4/4 net-next: frags: dynamic timeout under load
Calculate a dynamic fragment reassembly timeout, taking into consideration the current fqdir load and the load introduced by the peer. Reintroduce low_thresh, which now acts as a knob for adjusting per-peer memory limits.
--- Documentation/networking/ip-sysctl.rst | 3 +++ include/net/inet_frag.h | 1 + net/ipv4/inet_fragment.c | 30 +++++++++++++++++++++++++- net/ipv4/ip_fragment.c | 2 +- 4 files changed, 34 insertions(+), 2 deletions(-)
Documentation/networking/ip-sysctl.rst +3 -0
@@ -247,6 +247,9 @@ ipfrag_low_thresh - LONG INTEGER begins to remove incomplete fragment queues to free up resources. The kernel still accepts new fragments for defragmentation. + (Since linux-6.1) + Maximum memory used to reassemble IP fragments sent by a single peer. + ipfrag_time - INTEGER Time in seconds to keep an IP fragment in memory.
include/net/inet_frag.h +1 -0
@@ -99,6 +99,7 @@ struct inet_frag_queue {u16 max_size;struct fqdir *fqdir;struct inet_peer *peer; +u64 timeout;struct rcu_head rcu; };
net/ipv4/inet_fragment.c +29 -1
@@ -314,6 +314,30 @@ void inet_frag_free(struct inet_frag_queue *q)call_rcu (&q->rcu, inet_frag_destroy_rcu); } +static int inet_frag_update_timeout (struct inet_frag_queue *q) +{ +u64 peer_timeout, inet_timeout; +long peer_mem, inet_mem; +long high_thresh =READ_ONCE (q->fqdir->high_thresh); +long low_thresh =READ_ONCE (q->fqdir->low_thresh); +u64 base_timeout =READ_ONCE (q->fqdir->timeout); + + peer_mem = low_thresh -peer_mem_limit (q); + inet_mem = high_thresh -frag_mem_limit (q->fqdir); + +if (peer_mem <=0 || inet_mem <=0 ) +return -ENOMEM ; + +/* Timeout changes linearly with respect to the amount of free memory. +* Choose the more permissive of the two timeouts, to avoid limiting +* the system while there is still enough memory. +*/ + peer_timeout =div64_long (base_timeout * peer_mem, low_thresh); + inet_timeout =div64_long (base_timeout * inet_mem, high_thresh); + q->timeout =max_t (u64, peer_timeout, inet_timeout); +return 0 ; +} +void inet_frag_destroy (struct inet_frag_queue *q) {struct fqdir *fqdir;
@@ -346,6 +370,10 @@ static struct inet_frag_queue *inet_frag_alloc(struct fqdir *fqdir, q->fqdir = fqdir; f->constructor (q, arg); +if (inet_frag_update_timeout (q)) { +inet_frag_free (q); +return NULL ; + }add_frag_mem_limit (q, f->qsize);timer_setup (&q->timer, f->frag_expire,0 );
@@ -367,7 +395,7 @@ static struct inet_frag_queue *inet_frag_create(struct fqdir *fqdir, *prev =ERR_PTR (-ENOMEM );return NULL ; } -mod_timer (&q->timer, jiffies + fqdir->timeout); +mod_timer (&q->timer, jiffies + q->timeout); *prev =rhashtable_lookup_get_insert_key (&fqdir->rhashtable, &q->key, &q->node, f->rhash_params);
net/ipv4/ip_fragment.c +1 -1
@@ -236,7 +236,7 @@ static int ip_frag_reinit(struct ipq *qp) {unsigned int sum_truesize =0 ; -if (!mod_timer (&qp->q.timer, jiffies + qp->q.fqdir->timeout)) { +if (!mod_timer (&qp->q.timer, jiffies + qp->q.timeout)) {refcount_inc (&qp->q.refcnt);return -ETIMEDOUT ; }
Signature · 1 line
2.36.1
Quoted 33 lines · Richard Gobert
Calculate a dynamic fragment reassembly timeout, taking into consideration the current fqdir load and the load introduced by the peer. Reintroduce
low_thresh, which now acts as a knob for adjusting per-peer memory limits.Signed-off-by Richard Gobert <[email protected]>--- Documentation/networking/ip-sysctl.rst | 3 +++ include/net/inet_frag.h | 1 + net/ipv4/inet_fragment.c | 30 +++++++++++++++++++++++++- net/ipv4/ip_fragment.c | 2 +- 4 files changed, 34 insertions(+), 2 deletions(-)Documentation/networking/ip-sysctl.rst +3
-0@@ -247,6 +247,9 @@ ipfrag_low_thresh - LONG INTEGER begins to remove incomplete fragment queues to free up resources. The kernel still accepts new fragments for defragmentation. + (Since linux-6.1) + Maximum memory used to reassemble IP fragments sent by a single peer. + ipfrag_time - INTEGER Time in seconds to keep an IP fragment in memory.include/net/inet_frag.h +0
-0
include/net/inet_frag.h struct inet_frag_queue { · line 102
Quoted 4 lines
include/net/inet_frag.h +1
-0@@ -99,6 +99,7 @@ struct inet_frag_queue {u16 max_size;struct fqdir *fqdir;struct inet_peer *peer; +u64 timeout;
Why u64 ?
This is not what the timer interface uses (look at mod_timer(), it uses "unsigned long")
Quoted 22 lines
include/net/inet_frag.h +0
-0struct rcu_head rcu; };net/ipv4/inet_fragment.c +11
-0@@ -314,6 +314,30 @@ void inet_frag_free(struct inet_frag_queue *q)call_rcu (&q->rcu, inet_frag_destroy_rcu); } +static int inet_frag_update_timeout (struct inet_frag_queue *q) +{ +u64 peer_timeout, inet_timeout; +long peer_mem, inet_mem; +long high_thresh =READ_ONCE (q->fqdir->high_thresh); +long low_thresh =READ_ONCE (q->fqdir->low_thresh); +u64 base_timeout =READ_ONCE (q->fqdir->timeout); + + peer_mem = low_thresh -peer_mem_limit (q); + inet_mem = high_thresh -frag_mem_limit (q->fqdir); +
net/ipv4/inet_fragment.c void inet_frag_free(struct inet_frag_queue *q) · line 337
Quoted 10 lines
net/ipv4/inet_fragment.c +10
-0+if (peer_mem <=0 || inet_mem <=0 ) +return -ENOMEM ; + +/* Timeout changes linearly with respect to the amount of free memory. +* Choose the more permissive of the two timeouts, to avoid limiting +* the system while there is still enough memory. +*/ + peer_timeout =div64_long (base_timeout * peer_mem, low_thresh); + inet_timeout =div64_long (base_timeout * inet_mem, high_thresh); + q->timeout =max_t (u64, peer_timeout, inet_timeout);
If/when under load, timeout is close to zero,
we would fire many timers (increased system load) and make impossible for datagrams to complete.
In contrast, a reasonable timer and probabilistic drops of new datagrams when the queue is full lets some datagrams to complete.
Make sure to test your change under a real DDOS, not only non malicious netperf
Quoted 42 lines
net/ipv4/inet_fragment.c +8
-1+return 0 ; +} +void inet_frag_destroy (struct inet_frag_queue *q) {struct fqdir *fqdir;@@ -346,6 +370,10 @@ static struct inet_frag_queue *inet_frag_alloc(struct fqdir *fqdir, q->fqdir = fqdir; f->constructor (q, arg); +if (inet_frag_update_timeout (q)) { +inet_frag_free (q); +return NULL ; + }add_frag_mem_limit (q, f->qsize);timer_setup (&q->timer, f->frag_expire,0 );@@ -367,7 +395,7 @@ static struct inet_frag_queue *inet_frag_create(struct fqdir *fqdir, *prev =ERR_PTR (-ENOMEM );return NULL ; } -mod_timer (&q->timer, jiffies + fqdir->timeout); +mod_timer (&q->timer, jiffies + q->timeout); *prev =rhashtable_lookup_get_insert_key (&fqdir->rhashtable, &q->key, &q->node, f->rhash_params);net/ipv4/ip_fragment.c +1
-2@@ -236,7 +236,7 @@ static int ip_frag_reinit(struct ipq *qp) {unsigned int sum_truesize =0 ; -if (!mod_timer (&qp->q.timer, jiffies + qp->q.fqdir->timeout)) { +if (!mod_timer (&qp->q.timer, jiffies + qp->q.timeout)) {refcount_inc (&qp->q.refcnt);return -ETIMEDOUT ; } --2.36.1