Skip to contentSkip to conversation

rps: selective flow shedding during softnet overflow

PATCH Superseded Willem de Bruijn 5 participants 41 messages Apr 19, 2013 – May 20, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rps: selective flow shedding during softnet overflow

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 1024 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---
 include/linux/netdevice.h  |  16 ++++++++
 net/Kconfig                |  10 +++++
 net/core/dev.c             |  49 +++++++++++++++++++++-
 net/core/net-procfs.c      |  16 +++++++-
 net/core/sysctl_net_core.c | 100 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 188 insertions(+), 3 deletions(-)
include/linux/netdevice.h +16 -0
index 623b57b..d70afcc 100644
@@ -1779,6 +1779,18 @@ static inline int unregister_gifconf(unsigned int family)
 	return register_gifconf(family, NULL);
 }
 
+#ifdef CONFIG_NET_FLOW_LIMIT
+#define FLOW_LIMIT_HISTORY	(1 << 8)	/* must be ^2 */
+struct sd_flow_limit {
+	u64			count;
+	unsigned int		history_head;
+	u16			history[FLOW_LIMIT_HISTORY];
+	u8			buckets[];
+};
+
+extern int netdev_flow_limit_table_len;
+#endif /* CONFIG_NET_FLOW_LIMIT */
+
 /*
  * Incoming packets are placed on per-cpu queues
  */
@@ -1808,6 +1820,10 @@ struct softnet_data {
 	unsigned int		dropped;
 	struct sk_buff_head	input_pkt_queue;
 	struct napi_struct	backlog;
+
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit	*flow_limit;
+#endif
 };
 
 static inline void input_queue_head_incr(struct softnet_data *sd)
net/Kconfig +10 -0
index 2ddc904..ff66a4f 100644
@@ -259,6 +259,16 @@ config BPF_JIT
 	  packet sniffing (libpcap/tcpdump). Note : Admin should enable
 	  this feature changing /proc/sys/net/core/bpf_jit_enable
 
+config NET_FLOW_LIMIT
+	bool "Flow shedding under load"
+	---help---
+	  The network stack has to drop packets when a receive processing CPUs
+	  backlog reaches netdev_max_backlog. If a few out of many active flows
+	  generate the vast majority of load, drop their traffic earlier to
+	  maintain capacity for the other flows. This feature provides servers
+	  with many clients some protection against DoS by a single (spoofed)
+	  flow that greatly exceeds average workload.
+
 menu "Network testing"
 
 config NET_PKTGEN
net/core/dev.c +48 -1

73 lines not on this page · raw message

net/core/net-procfs.c +14 -2
index 569d355..2bf8329 100644
@@ -146,11 +146,23 @@ static void softnet_seq_stop(struct seq_file *seq, void *v)
 static int softnet_seq_show(struct seq_file *seq, void *v)
 {
 	struct softnet_data *sd = v;
+	unsigned int flow_limit_count = 0;
 
-	seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit *fl;
+
+	rcu_read_lock();
+	fl = rcu_dereference(sd->flow_limit);
+	if (fl)
+		flow_limit_count = fl->count;
+	rcu_read_unlock();
+#endif
+
+	seq_printf(seq,
+		   "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
 		   sd->processed, sd->dropped, sd->time_squeeze, 0,
 		   0, 0, 0, 0, /* was fastroute */
-		   sd->cpu_collision, sd->received_rps);
+		   sd->cpu_collision, sd->received_rps, flow_limit_count);
 	return 0;
 }
 
net/core/sysctl_net_core.c +100 -0

112 lines not on this page · raw message

Signature · 1 line
1.8.2.1
#1 lore raw
Quoted 93 lines · Willem de Bruijn

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 1024 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---

include/linux/netdevice.h +16 -0
+#ifdef CONFIG_NET_FLOW_LIMIT
+#define FLOW_LIMIT_HISTORY	(1 << 8)	/* must be ^2 */
+struct sd_flow_limit {
+	u64			count;
+	unsigned int		history_head;
+	u16			history[FLOW_LIMIT_HISTORY];
+	u8			buckets[];
+};
+
+extern int netdev_flow_limit_table_len;
+#endif /* CONFIG_NET_FLOW_LIMIT */
+
 /*
  * Incoming packets are placed on per-cpu queues
  */
@@ -1808,6 +1820,10 @@ struct softnet_data {
 	unsigned int		dropped;
 	struct sk_buff_head	input_pkt_queue;
 	struct napi_struct	backlog;
+
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit	*flow_limit;
+#endif
 };
 
 static inline void input_queue_head_incr(struct softnet_data *sd)
net/Kconfig +10 -0
index 2ddc904..ff66a4f 100644
@@ -259,6 +259,16 @@ config BPF_JIT
 	  packet sniffing (libpcap/tcpdump). Note : Admin should enable
 	  this feature changing /proc/sys/net/core/bpf_jit_enable
 
+config NET_FLOW_LIMIT
+	bool "Flow shedding under load"
+	---help---
+	  The network stack has to drop packets when a receive processing CPUs
+	  backlog reaches netdev_max_backlog. If a few out of many active flows
+	  generate the vast majority of load, drop their traffic earlier to
+	  maintain capacity for the other flows. This feature provides servers
+	  with many clients some protection against DoS by a single (spoofed)
+	  flow that greatly exceeds average workload.
+
 menu "Network testing"
 
 config NET_PKTGEN
net/core/dev.c +11 -0
index 3655ff9..67a4ae0 100644
@@ -3054,6 +3054,47 @@ static int rps_ipi_queued(struct softnet_data *sd)
 	return 0;
 }
 
+#ifdef CONFIG_NET_FLOW_LIMIT
+int netdev_flow_limit_table_len __read_mostly = (1 << 12);
+#endif
+
+static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
+{
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit *fl;
+	struct softnet_data *sd;
+	unsigned int old_flow, new_flow;
+

net/core/dev.c static int rps_ipi_queued(struct softnet_data *sd) · line 3077

Quoted 10 lines
net/core/dev.c +10 -0
+	if (qlen < (netdev_max_backlog >> 1))
+		return false;
+
+	sd = &per_cpu(softnet_data, smp_processor_id());
+
+	rcu_read_lock();
+	fl = rcu_dereference(sd->flow_limit);
+	if (fl) {
+		new_flow = skb_get_rxhash(skb) &
+			   (netdev_flow_limit_table_len - 1);

There is a race accessing netdev_flow_limit_table_len

(the admin might change the value, and we might do an out of bound access)

This should be a field in fl, aka fl->mask, so thats its safe

Quoted 10 lines
net/core/dev.c +10 -0
+		old_flow = fl->history[fl->history_head];
+		fl->history[fl->history_head] = new_flow;
+
+		fl->history_head++;
+		fl->history_head &= FLOW_LIMIT_HISTORY - 1;
+
+		if (likely(fl->buckets[old_flow]))
+			fl->buckets[old_flow]--;
+
+		if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {

net/core/dev.c static int rps_ipi_queued(struct softnet_data *sd) · line 3097

Quoted 10 lines
net/core/dev.c +10 -0
+			fl->count++;
+			rcu_read_unlock();
+			return true;
+		}
+	}
+	rcu_read_unlock();
+#endif
+	return false;
+}
+

Very nice work by the way !

#38 lore raw
In reply to Willem de Bruijn
Quoted 19 lines · On Fri, 19 Apr 2013 13:46:52 -0400 Willem de Bruijn <[email protected]> wrote:

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 1024 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

Quoted 6 lines

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

The netdev_backlog only applies for RPS and non-NAPI devices. So this won't help if receive packet steering is not enabled. Seems like a deficiency in the receive steering design rather than the netdev_backlog.

Can't you do this with existing ingress stuff?
The trend seems to be put in more fixed infrastructure to deal with performance and server problems rather than building general purpose solutions.

#39 lore raw
Quoted 2 lines · Stephen Hemminger

The netdev_backlog only applies for RPS and non-NAPI devices. So this won't help if receive packet steering is not enabled.

Yes, the point is exactly using RPS as a proxy to control the behavior and better distribute the load.

Quoted 2 lines

Seems like a deficiency in the receive steering design rather than the netdev_backlog.

Well, RPS works well, even for multiqueue NICS. But nobody said it was perfect.

Back to NAPI (without RPS) :

If one RX queue (multiqueue device or not) is hit by a single flow, how do you plan to do anything, since without RPS we process each packet after another.

There is no queue building up in our stack. (Only queue is in the NIC RX ring). Eventually NIC drops packets.

Quoted 2 lines

Can't you do this with existing ingress stuff?

ingress is not yet multiqueue enabled, AFAIK.

Expect very poor performance from it.

Quoted 3 lines

The trend seems to be put in more fixed infrastructure to deal with performance and server problems rather than building general purpose solutions.

OK, I see you want to push netmap. Lets talk about general purpose solutions.

#40 lore raw
In reply to Stephen Hemminger
Quoted 27 lines · Stephen Hemminger
Quoted 24 lines · On Fri, 19 Apr 2013 13:46:52 -0400 Willem de Bruijn <[email protected]> wrote:

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 1024 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

Quoted 6 lines
Quoted 1 line

The netdev_backlog only applies for RPS and non-NAPI devices. So this won't help if receive packet steering is not enabled. Seems like a deficiency in the receive steering design rather than the netdev_backlog.

The patch specifically intends to address a consequence of perfect flow-hashing: that unbalanced input translates into cpu load imbalance. It is less relevant to servers that do not use flow hashing to spread traffic (i.e., no rps/rfs).

In normal server workloads, hashing works well, but it makes machine state subject to external influence. In particular, to local resource exhaustion (partial DoS). This patch hardens against these extreme input patterns that should not occur in normal workloads. The netdev backlog is the clearest indicator of unsustainable load due to imbalance.

Quoted 4 lines

Can't you do this with existing ingress stuff?
The trend seems to be put in more fixed infrastructure to deal with performance and server problems rather than building general purpose solutions.

This isn't necessarily mutually exclusive with iptables/policing/.. mechanisms to filter out bad flows, of course. The earlier in the pipeline packets are dropped, the fewer cycles are spent, so this is another layer of (early) defense.

For instance, I recently sent a patch to handle load imbalance in packet sockets. Those socket queues fill up if the application threads are the bottleneck instead of the kernel receive path, so this rps fix would not be relevant.

Quoted 1 line
#2 lore raw
In reply to Eric Dumazet
Quoted 107 lines · Eric Dumazet
Quoted 93 lines · Willem de Bruijn

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 1024 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---

include/linux/netdevice.h +16 -0
+#ifdef CONFIG_NET_FLOW_LIMIT
+#define FLOW_LIMIT_HISTORY   (1 << 8)        /* must be ^2 */
+struct sd_flow_limit {
+     u64                     count;
+     unsigned int            history_head;
+     u16                     history[FLOW_LIMIT_HISTORY];
+     u8                      buckets[];
+};
+
+extern int netdev_flow_limit_table_len;
+#endif /* CONFIG_NET_FLOW_LIMIT */
+
 /*
  * Incoming packets are placed on per-cpu queues
  */
@@ -1808,6 +1820,10 @@ struct softnet_data {
      unsigned int            dropped;
      struct sk_buff_head     input_pkt_queue;
      struct napi_struct      backlog;
+
+#ifdef CONFIG_NET_FLOW_LIMIT
+     struct sd_flow_limit    *flow_limit;
+#endif
 };
 
 static inline void input_queue_head_incr(struct softnet_data *sd)
net/Kconfig +10 -0
index 2ddc904..ff66a4f 100644
@@ -259,6 +259,16 @@ config BPF_JIT
        packet sniffing (libpcap/tcpdump). Note : Admin should enable
        this feature changing /proc/sys/net/core/bpf_jit_enable
 
+config NET_FLOW_LIMIT
+     bool "Flow shedding under load"
+     ---help---
+       The network stack has to drop packets when a receive processing CPUs
+       backlog reaches netdev_max_backlog. If a few out of many active flows
+       generate the vast majority of load, drop their traffic earlier to
+       maintain capacity for the other flows. This feature provides servers
+       with many clients some protection against DoS by a single (spoofed)
+       flow that greatly exceeds average workload.
+
 menu "Network testing"
 
 config NET_PKTGEN
net/core/dev.c +11 -0
index 3655ff9..67a4ae0 100644
@@ -3054,6 +3054,47 @@ static int rps_ipi_queued(struct softnet_data *sd)
      return 0;
 }
 
+#ifdef CONFIG_NET_FLOW_LIMIT
+int netdev_flow_limit_table_len __read_mostly = (1 << 12);
+#endif
+
+static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
+{
+#ifdef CONFIG_NET_FLOW_LIMIT
+     struct sd_flow_limit *fl;
+     struct softnet_data *sd;
+     unsigned int old_flow, new_flow;
+

net/core/dev.c static int rps_ipi_queued(struct softnet_data *sd) · line 3077

Quoted 10 lines
net/core/dev.c +10 -0
+     if (qlen < (netdev_max_backlog >> 1))
+             return false;
+
+     sd = &per_cpu(softnet_data, smp_processor_id());
+
+     rcu_read_lock();
+     fl = rcu_dereference(sd->flow_limit);
+     if (fl) {
+             new_flow = skb_get_rxhash(skb) &
+                        (netdev_flow_limit_table_len - 1);

There is a race accessing netdev_flow_limit_table_len

Quoted 4 lines

(the admin might change the value, and we might do an out of bound access)

This should be a field in fl, aka fl->mask, so thats its safe

Ah, of course. Thanks, Eric!

I held off a new patch for a few days to wait for comments. Just updated it with this change and will send it as v2.

Quoted 26 lines
Quoted 12 lines
net/core/dev.c +10 -0
+             old_flow = fl->history[fl->history_head];
+             fl->history[fl->history_head] = new_flow;
+
+             fl->history_head++;
+             fl->history_head &= FLOW_LIMIT_HISTORY - 1;
+
+             if (likely(fl->buckets[old_flow]))
+                     fl->buckets[old_flow]--;
+
+             if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {

net/core/dev.c static int rps_ipi_queued(struct softnet_data *sd) · line 3097

Quoted 10 lines
net/core/dev.c +10 -0
+                     fl->count++;
+                     rcu_read_unlock();
+                     return true;
+             }
+     }
+     rcu_read_unlock();
+#endif
+     return false;
+}
+

Very nice work by the way !

#3 lore raw

PATCH v2 1/1 rps: selective flow shedding during softnet overflow

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---

Changed v1->v2
- add fl->num_buckets element to use the actual allocated table length.
- disable the kconfig option by default, as it is workload specific.

---
 include/linux/netdevice.h  |  17 ++++++++
 net/Kconfig                |  11 +++++
 net/core/dev.c             |  48 ++++++++++++++++++++-
 net/core/net-procfs.c      |  16 ++++++-
 net/core/sysctl_net_core.c | 101 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 190 insertions(+), 3 deletions(-)
include/linux/netdevice.h +17 -0
index f8898a4..d781cf1 100644
@@ -1779,6 +1779,19 @@ static inline int unregister_gifconf(unsigned int family)
 	return register_gifconf(family, NULL);
 }
 
+#ifdef CONFIG_NET_FLOW_LIMIT
+#define FLOW_LIMIT_HISTORY	(1 << 8)	/* must be ^2 */
+struct sd_flow_limit {
+	u64			count;
+	unsigned int		num_buckets;
+	unsigned int		history_head;
+	u16			history[FLOW_LIMIT_HISTORY];
+	u8			buckets[];
+};
+
+extern int netdev_flow_limit_table_len;
+#endif /* CONFIG_NET_FLOW_LIMIT */
+
 /*
  * Incoming packets are placed on per-cpu queues
  */
@@ -1808,6 +1821,10 @@ struct softnet_data {
 	unsigned int		dropped;
 	struct sk_buff_head	input_pkt_queue;
 	struct napi_struct	backlog;
+
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit	*flow_limit;
+#endif
 };
 
 static inline void input_queue_head_incr(struct softnet_data *sd)
net/Kconfig +11 -0
index 1a22216..a0cbd3b 100644
@@ -268,6 +268,17 @@ config BPF_JIT
 	  packet sniffing (libpcap/tcpdump). Note : Admin should enable
 	  this feature changing /proc/sys/net/core/bpf_jit_enable
 
+config NET_FLOW_LIMIT
+	bool "Flow shedding under load"
+	default n
+	---help---
+	  The network stack has to drop packets when a receive processing CPUs
+	  backlog reaches netdev_max_backlog. If a few out of many active flows
+	  generate the vast majority of load, drop their traffic earlier to
+	  maintain capacity for the other flows. This feature provides servers
+	  with many clients some protection against DoS by a single (spoofed)
+	  flow that greatly exceeds average workload.
+
 menu "Network testing"
 
 config NET_PKTGEN
net/core/dev.c +47 -1

72 lines not on this page · raw message

net/core/net-procfs.c +14 -2
index 569d355..2bf8329 100644
@@ -146,11 +146,23 @@ static void softnet_seq_stop(struct seq_file *seq, void *v)
 static int softnet_seq_show(struct seq_file *seq, void *v)
 {
 	struct softnet_data *sd = v;
+	unsigned int flow_limit_count = 0;
 
-	seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit *fl;
+
+	rcu_read_lock();
+	fl = rcu_dereference(sd->flow_limit);
+	if (fl)
+		flow_limit_count = fl->count;
+	rcu_read_unlock();
+#endif
+
+	seq_printf(seq,
+		   "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
 		   sd->processed, sd->dropped, sd->time_squeeze, 0,
 		   0, 0, 0, 0, /* was fastroute */
-		   sd->cpu_collision, sd->received_rps);
+		   sd->cpu_collision, sd->received_rps, flow_limit_count);
 	return 0;
 }
 
net/core/sysctl_net_core.c +101 -0

113 lines not on this page · raw message

Signature · 1 line
1.8.2.1
#4 lore raw

net/core/sysctl_net_core.c static int rps_sock_flow_sysctl(ctl_table *table, int write, · line 109

Quoted 1 line · Willem de Bruijn
net/core/sysctl_net_core.c +1 -0
+		len = sizeof(*cur) + netdev_flow_limit_table_len;

[1]

net/core/sysctl_net_core.c static int rps_sock_flow_sysctl(ctl_table *table, int write, · line 110

Quoted 1 line
net/core/sysctl_net_core.c +1 -0
+		mutex_lock(&flow_limit_update_mutex);

[2]

net/core/sysctl_net_core.c static int rps_sock_flow_sysctl(ctl_table *table, int write, · line 121

Quoted 11 lines
net/core/sysctl_net_core.c +11 -0
+		for_each_possible_cpu(i) {
+			sd = &per_cpu(softnet_data, i);
+			cur = rcu_dereference_protected(sd->flow_limit,
+				      lockdep_is_held(flow_limit_update_mutex));
+			if (cur && !cpumask_test_cpu(i, mask)) {
+				RCU_INIT_POINTER(sd->flow_limit, NULL);
+				synchronize_rcu();
+				kfree(cur);
+			} else if (!cur && cpumask_test_cpu(i, mask)) {
+				cur = kzalloc(len, GFP_KERNEL);
+				cur->num_buckets = netdev_flow_limit_table_len;

[3]

Its a bit tricky, but the value of netdev_flow_limit_table_len could change between [1] and [3]

So you should read its value once, or protect the whole thing using mutex_lock(&flow_limit_update_mutex) in sysctl code ( and move [1] after [2])

#5 lore raw
Quoted 17 lines · Eric Dumazet
Quoted 1 line · Willem de Bruijn

+ len = sizeof(*cur) + netdev_flow_limit_table_len;

[1]

Quoted 1 line

+ mutex_lock(&flow_limit_update_mutex);

[2]

net/core/sysctl_net_core.c static int rps_sock_flow_sysctl(ctl_table *table, int write, · line 121

Quoted 11 lines
net/core/sysctl_net_core.c +11 -0
+             for_each_possible_cpu(i) {
+                     sd = &per_cpu(softnet_data, i);
+                     cur = rcu_dereference_protected(sd->flow_limit,
+                                   lockdep_is_held(flow_limit_update_mutex));
+                     if (cur && !cpumask_test_cpu(i, mask)) {
+                             RCU_INIT_POINTER(sd->flow_limit, NULL);
+                             synchronize_rcu();
+                             kfree(cur);
+                     } else if (!cur && cpumask_test_cpu(i, mask)) {
+                             cur = kzalloc(len, GFP_KERNEL);
+                             cur->num_buckets = netdev_flow_limit_table_len;
Quoted 8 lines

[3]

Its a bit tricky, but the value of netdev_flow_limit_table_len could change between [1] and [3]

So you should read its value once, or protect the whole thing using mutex_lock(&flow_limit_update_mutex) in sysctl code ( and move [1] after [2])

Thanks for the detailed explanation. I implemented the second
solution: make writes to the two sysctls mutually exclusive.

Quoted 2 lines
#6 lore raw

PATCH v3 1/1 rps: selective flow shedding during softnet overflow

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---

Changes
v3
- fix race between updates to table_len sysctl during bitmap sysctl.
- fix NULL pointer dereference on alloc failure.
v2
- add fl->num_buckets element to use the actual allocated table length.
- disable the kconfig option by default, as it is workload specific.

---
 include/linux/netdevice.h  |  17 ++++++++
 net/Kconfig                |  11 +++++
 net/core/dev.c             |  48 ++++++++++++++++++++-
 net/core/net-procfs.c      |  16 ++++++-
 net/core/sysctl_net_core.c | 105 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 194 insertions(+), 3 deletions(-)
include/linux/netdevice.h +17 -0
index f8898a4..d781cf1 100644
@@ -1779,6 +1779,19 @@ static inline int unregister_gifconf(unsigned int family)
 	return register_gifconf(family, NULL);
 }
 
+#ifdef CONFIG_NET_FLOW_LIMIT
+#define FLOW_LIMIT_HISTORY	(1 << 8)	/* must be ^2 */
+struct sd_flow_limit {
+	u64			count;
+	unsigned int		num_buckets;
+	unsigned int		history_head;
+	u16			history[FLOW_LIMIT_HISTORY];
+	u8			buckets[];
+};
+
+extern int netdev_flow_limit_table_len;
+#endif /* CONFIG_NET_FLOW_LIMIT */
+
 /*
  * Incoming packets are placed on per-cpu queues
  */
@@ -1808,6 +1821,10 @@ struct softnet_data {
 	unsigned int		dropped;
 	struct sk_buff_head	input_pkt_queue;
 	struct napi_struct	backlog;
+
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit	*flow_limit;
+#endif
 };
 
 static inline void input_queue_head_incr(struct softnet_data *sd)
net/Kconfig +11 -0
index 1a22216..a0cbd3b 100644
@@ -268,6 +268,17 @@ config BPF_JIT
 	  packet sniffing (libpcap/tcpdump). Note : Admin should enable
 	  this feature changing /proc/sys/net/core/bpf_jit_enable
 
+config NET_FLOW_LIMIT
+	bool "Flow shedding under load"
+	default n
+	---help---
+	  The network stack has to drop packets when a receive processing CPUs
+	  backlog reaches netdev_max_backlog. If a few out of many active flows
+	  generate the vast majority of load, drop their traffic earlier to
+	  maintain capacity for the other flows. This feature provides servers
+	  with many clients some protection against DoS by a single (spoofed)
+	  flow that greatly exceeds average workload.
+
 menu "Network testing"
 
 config NET_PKTGEN
net/core/dev.c +47 -1

72 lines not on this page · raw message

net/core/net-procfs.c +14 -2
index 569d355..2bf8329 100644
@@ -146,11 +146,23 @@ static void softnet_seq_stop(struct seq_file *seq, void *v)
 static int softnet_seq_show(struct seq_file *seq, void *v)
 {
 	struct softnet_data *sd = v;
+	unsigned int flow_limit_count = 0;
 
-	seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit *fl;
+
+	rcu_read_lock();
+	fl = rcu_dereference(sd->flow_limit);
+	if (fl)
+		flow_limit_count = fl->count;
+	rcu_read_unlock();
+#endif
+
+	seq_printf(seq,
+		   "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
 		   sd->processed, sd->dropped, sd->time_squeeze, 0,
 		   0, 0, 0, 0, /* was fastroute */
-		   sd->cpu_collision, sd->received_rps);
+		   sd->cpu_collision, sd->received_rps, flow_limit_count);
 	return 0;
 }
 
net/core/sysctl_net_core.c +105 -0

117 lines not on this page · raw message

Signature · 1 line
1.8.2.1
#7 lore raw

Hi Willem

net/core/dev.c static int rps_ipi_queued(struct softnet_data *sd) · line 3074

Quoted 12 lines · Willem de Bruijn
net/core/dev.c +12 -0
+
+static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
+{
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit *fl;
+	struct softnet_data *sd;
+	unsigned int old_flow, new_flow;
+
+	if (qlen < (netdev_max_backlog >> 1))
+		return false;
+
+	sd = &per_cpu(softnet_data, smp_processor_id());

sd = __get_cpu_var(softnet_data);

Quoted 25 lines
net/core/dev.c +25 -0
+
+	rcu_read_lock();
+	fl = rcu_dereference(sd->flow_limit);
+	if (fl) {
+		new_flow = skb_get_rxhash(skb) & (fl->num_buckets - 1);
+		old_flow = fl->history[fl->history_head];
+		fl->history[fl->history_head] = new_flow;
+
+		fl->history_head++;
+		fl->history_head &= FLOW_LIMIT_HISTORY - 1;
+
+		if (likely(fl->buckets[old_flow]))
+			fl->buckets[old_flow]--;
+
+		if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
+			fl->count++;
+			rcu_read_unlock();
+			return true;
+		}
+	}
+	rcu_read_unlock();
+#endif
+	return false;
+}
+

...

Quoted 33 lines
net/core/sysctl_net_core.c +33 -0
+#ifdef CONFIG_NET_FLOW_LIMIT
+static DEFINE_MUTEX(flow_limit_update_mutex);
+
+static int flow_limit_cpu_sysctl(ctl_table *table, int write,
+				 void __user *buffer, size_t *lenp,
+				 loff_t *ppos)
+{
+	struct sd_flow_limit *cur;
+	struct softnet_data *sd;
+	cpumask_var_t mask;
+	int i, len, ret = 0;
+
+	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
+		return -ENOMEM;
+
+	if (write) {
+		ret = cpumask_parse_user(buffer, *lenp, mask);
+		if (ret)
+			goto done;
+
+		mutex_lock(&flow_limit_update_mutex);
+		len = sizeof(*cur) + netdev_flow_limit_table_len;
+		for_each_possible_cpu(i) {
+			sd = &per_cpu(softnet_data, i);
+			cur = rcu_dereference_protected(sd->flow_limit,
+				      lockdep_is_held(flow_limit_update_mutex));
+			if (cur && !cpumask_test_cpu(i, mask)) {
+				RCU_INIT_POINTER(sd->flow_limit, NULL);
+				synchronize_rcu();
+				kfree(cur);
+			} else if (!cur && cpumask_test_cpu(i, mask)) {
+				cur = kzalloc(len, GFP_KERNEL);
+				if (!cur) {

net/core/sysctl_net_core.c static int rps_sock_flow_sysctl(ctl_table *table, int write, · line 132

Quoted 10 lines
net/core/sysctl_net_core.c +10 -0
+					/* not unwinding previous changes */
+					ret = -ENOMEM;
+					goto write_unlock;
+				}
+				cur->num_buckets = netdev_flow_limit_table_len;
+				rcu_assign_pointer(sd->flow_limit, cur);
+			}
+		}
+write_unlock:
+		synchronize_rcu();

I believe you do not need this synchronize_rcu() call.

Quoted 16 lines
net/core/sysctl_net_core.c +16 -0
+		mutex_unlock(&flow_limit_update_mutex);
+	} else {
+		if (*ppos || !*lenp) {
+			*lenp = 0;
+			goto done;
+		}
+
+		cpumask_clear(mask);
+		rcu_read_lock();
+		for_each_possible_cpu(i) {
+			sd = &per_cpu(softnet_data, i);
+			if (rcu_dereference(sd->flow_limit))
+				cpumask_set_cpu(i, mask);
+		}
+		rcu_read_unlock();
+

net/core/sysctl_net_core.c static int rps_sock_flow_sysctl(ctl_table *table, int write, · line 158

Quoted 10 lines
net/core/sysctl_net_core.c +10 -0
+		len = cpumask_scnprintf(buffer, *lenp, mask);
+		*lenp = len + 1;
+		*ppos += len + 1;
+	}
+
+done:
+	free_cpumask_var(mask);
+	return ret;
+}
+

Thanks !

#8 lore raw
Quoted 85 lines · Eric Dumazet

Hi Willem

net/core/dev.c static int rps_ipi_queued(struct softnet_data *sd) · line 3074

Quoted 12 lines · Willem de Bruijn
net/core/dev.c +12 -0
+
+static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
+{
+#ifdef CONFIG_NET_FLOW_LIMIT
+     struct sd_flow_limit *fl;
+     struct softnet_data *sd;
+     unsigned int old_flow, new_flow;
+
+     if (qlen < (netdev_max_backlog >> 1))
+             return false;
+
+     sd = &per_cpu(softnet_data, smp_processor_id());

sd = __get_cpu_var(softnet_data);

Quoted 25 lines
net/core/dev.c +25 -0
+
+     rcu_read_lock();
+     fl = rcu_dereference(sd->flow_limit);
+     if (fl) {
+             new_flow = skb_get_rxhash(skb) & (fl->num_buckets - 1);
+             old_flow = fl->history[fl->history_head];
+             fl->history[fl->history_head] = new_flow;
+
+             fl->history_head++;
+             fl->history_head &= FLOW_LIMIT_HISTORY - 1;
+
+             if (likely(fl->buckets[old_flow]))
+                     fl->buckets[old_flow]--;
+
+             if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
+                     fl->count++;
+                     rcu_read_unlock();
+                     return true;
+             }
+     }
+     rcu_read_unlock();
+#endif
+     return false;
+}
+

...

Quoted 37 lines
net/core/sysctl_net_core.c +37 -0
+#ifdef CONFIG_NET_FLOW_LIMIT
+static DEFINE_MUTEX(flow_limit_update_mutex);
+
+static int flow_limit_cpu_sysctl(ctl_table *table, int write,
+                              void __user *buffer, size_t *lenp,
+                              loff_t *ppos)
+{
+     struct sd_flow_limit *cur;
+     struct softnet_data *sd;
+     cpumask_var_t mask;
+     int i, len, ret = 0;
+
+     if (!alloc_cpumask_var(&mask, GFP_KERNEL))
+             return -ENOMEM;
+
+     if (write) {
+             ret = cpumask_parse_user(buffer, *lenp, mask);
+             if (ret)
+                     goto done;
+
+             mutex_lock(&flow_limit_update_mutex);
+             len = sizeof(*cur) + netdev_flow_limit_table_len;
+             for_each_possible_cpu(i) {
+                     sd = &per_cpu(softnet_data, i);
+                     cur = rcu_dereference_protected(sd->flow_limit,
+                                   lockdep_is_held(flow_limit_update_mutex));
+                     if (cur && !cpumask_test_cpu(i, mask)) {
+                             RCU_INIT_POINTER(sd->flow_limit, NULL);
+                             synchronize_rcu();
+                             kfree(cur);
+                     } else if (!cur && cpumask_test_cpu(i, mask)) {
+                             cur = kzalloc(len, GFP_KERNEL);
+                             if (!cur) {
+                                     /* not unwinding previous changes */
+                                     ret = -ENOMEM;
+                                     goto write_unlock;
+                             }
Quoted 8 lines

net/core/sysctl_net_core.c static int rps_sock_flow_sysctl(ctl_table *table, int write, · line 132

Quoted 6 lines
net/core/sysctl_net_core.c +6 -0
+                             cur->num_buckets = netdev_flow_limit_table_len;
+                             rcu_assign_pointer(sd->flow_limit, cur);
+                     }
+             }
+write_unlock:
+             synchronize_rcu();

I believe you do not need this synchronize_rcu() call.

Because in this special case rcu_assign_pointer always replaces a NULL value, correct? Thanks again for the feedback! I rebased, reran the tests and will send v4 with these two changes (only).

Quoted 31 lines
Quoted 17 lines
net/core/sysctl_net_core.c +16 -0
+             mutex_unlock(&flow_limit_update_mutex);
+     } else {
+             if (*ppos || !*lenp) {
+                     *lenp = 0;
+                     goto done;
+             }
+
+             cpumask_clear(mask);
+             rcu_read_lock();
+             for_each_possible_cpu(i) {
+                     sd = &per_cpu(softnet_data, i);
+                     if (rcu_dereference(sd->flow_limit))
+                             cpumask_set_cpu(i, mask);
+             }
+             rcu_read_unlock();
+

net/core/sysctl_net_core.c static int rps_sock_flow_sysctl(ctl_table *table, int write, · line 158

Quoted 10 lines
net/core/sysctl_net_core.c +10 -0
+             len = cpumask_scnprintf(buffer, *lenp, mask);
+             *lenp = len + 1;
+             *ppos += len + 1;
+     }
+
+done:
+     free_cpumask_var(mask);
+     return ret;
+}
+

Thanks !

#9 lore raw

PATCH v4 1/1 rps: selective flow shedding during softnet overflow

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---

Changes
v4
- remove unnecessary synchronize_rcu after rcu_assign_pointer to NULL ptr
- simplify lookup of current cpu's softnet
v3
- fix race between updates to table_len sysctl during bitmap sysctl.
- fix NULL pointer dereference on alloc failure.
v2
- add fl->num_buckets element to use the actual allocated table length.
- disable the kconfig option by default, as it is workload specific.

---
 include/linux/netdevice.h  |  17 ++++++++
 net/Kconfig                |  11 +++++
 net/core/dev.c             |  48 ++++++++++++++++++++-
 net/core/net-procfs.c      |  16 ++++++-
 net/core/sysctl_net_core.c | 104 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 193 insertions(+), 3 deletions(-)
include/linux/netdevice.h +17 -0
index f8898a4..d781cf1 100644
@@ -1779,6 +1779,19 @@ static inline int unregister_gifconf(unsigned int family)
 	return register_gifconf(family, NULL);
 }
 
+#ifdef CONFIG_NET_FLOW_LIMIT
+#define FLOW_LIMIT_HISTORY	(1 << 8)	/* must be ^2 */
+struct sd_flow_limit {
+	u64			count;
+	unsigned int		num_buckets;
+	unsigned int		history_head;
+	u16			history[FLOW_LIMIT_HISTORY];
+	u8			buckets[];
+};
+
+extern int netdev_flow_limit_table_len;
+#endif /* CONFIG_NET_FLOW_LIMIT */
+
 /*
  * Incoming packets are placed on per-cpu queues
  */
@@ -1808,6 +1821,10 @@ struct softnet_data {
 	unsigned int		dropped;
 	struct sk_buff_head	input_pkt_queue;
 	struct napi_struct	backlog;
+
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit	*flow_limit;
+#endif
 };
 
 static inline void input_queue_head_incr(struct softnet_data *sd)
net/Kconfig +11 -0
index 1a22216..a0cbd3b 100644
@@ -268,6 +268,17 @@ config BPF_JIT
 	  packet sniffing (libpcap/tcpdump). Note : Admin should enable
 	  this feature changing /proc/sys/net/core/bpf_jit_enable
 
+config NET_FLOW_LIMIT
+	bool "Flow shedding under load"
+	default n
+	---help---
+	  The network stack has to drop packets when a receive processing CPUs
+	  backlog reaches netdev_max_backlog. If a few out of many active flows
+	  generate the vast majority of load, drop their traffic earlier to
+	  maintain capacity for the other flows. This feature provides servers
+	  with many clients some protection against DoS by a single (spoofed)
+	  flow that greatly exceeds average workload.
+
 menu "Network testing"
 
 config NET_PKTGEN
net/core/dev.c +47 -1

72 lines not on this page · raw message

net/core/net-procfs.c +14 -2
index 569d355..2bf8329 100644
@@ -146,11 +146,23 @@ static void softnet_seq_stop(struct seq_file *seq, void *v)
 static int softnet_seq_show(struct seq_file *seq, void *v)
 {
 	struct softnet_data *sd = v;
+	unsigned int flow_limit_count = 0;
 
-	seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit *fl;
+
+	rcu_read_lock();
+	fl = rcu_dereference(sd->flow_limit);
+	if (fl)
+		flow_limit_count = fl->count;
+	rcu_read_unlock();
+#endif
+
+	seq_printf(seq,
+		   "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
 		   sd->processed, sd->dropped, sd->time_squeeze, 0,
 		   0, 0, 0, 0, /* was fastroute */
-		   sd->cpu_collision, sd->received_rps);
+		   sd->cpu_collision, sd->received_rps, flow_limit_count);
 	return 0;
 }
 
net/core/sysctl_net_core.c +104 -0

116 lines not on this page · raw message

Signature · 1 line
1.8.2.1
#37 lore raw
In reply to Willem de Bruijn
Quoted 10 lines · Willem de Bruijn
Quoted 4 lines · Eric Dumazet

net/core/sysctl_net_core.c static int rps_sock_flow_sysctl(ctl_table *table, int write, · line 132

Quoted 2 lines
net/core/sysctl_net_core.c +2 -0
+write_unlock:
+             synchronize_rcu();

I believe you do not need this synchronize_rcu() call.

Because in this special case rcu_assign_pointer always replaces a NULL value, correct? Thanks again for the feedback! I rebased, reran the tests and will send v4 with these two changes (only).

Well, there is no assignment or freeing after the synchronize_rcu();

(mask is a local var only)

This looks as a leftover.

#10 lore raw
In reply to Willem de Bruijn
Quoted 19 lines · On Tue, 23 Apr 2013 16:31:34 -0400 Willem de Bruijn <[email protected]> wrote:

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

Quoted 6 lines

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

What about just having a smarter ingress qdisc?

Acked-by on v4 1/1
#30 lore raw
In reply to Willem de Bruijn
Quoted 19 lines · Willem de Bruijn

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

Quoted 8 lines

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---

#11 lore raw
In reply to Stephen Hemminger
Quoted 1 line · Stephen Hemminger

What about just having a smarter ingress qdisc?

What are your ideas ?

Setting ingress qdisc on linux is no fun, and not scalable.

Its ok for playing with netem and low bandwidth.

#28 lore raw
In reply to Stephen Hemminger
Quoted 22 lines · Stephen Hemminger
Quoted 19 lines · On Tue, 23 Apr 2013 16:31:34 -0400 Willem de Bruijn <[email protected]> wrote:

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

Quoted 8 lines
Quoted 6 lines

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

What about just having a smarter ingress qdisc?

For filtering, as this patch does, that is an interesting approach. Similar to fanout rollover, I plan to evaluate redistributing overload instead of filtering. That is not acceptable for TCP connections due to reordering, but may help protect against tcp synfloods, where most packets will not be part of a connection and can be processed by any cpu with cycles to spare. Moreover, all this processing takes place in the kernel receive path, so this is the type of workload that is most likely to overflow the input_pkt_queue. Frankly, that part requires much more evaluation to see if it makes sense, which is why I had not made this context clear: filtering by itself is already useful. An ingress qdisc is worth evaluating in that regard.

#12 lore raw
In reply to Eric Dumazet
Quoted 11 lines · On Tue, 23 Apr 2013 14:37:43 -0700 Eric Dumazet <[email protected]> wrote:
Quoted 1 line · Stephen Hemminger

What about just having a smarter ingress qdisc?

What are your ideas ?

Setting ingress qdisc on linux is no fun, and not scalable.

Its ok for playing with netem and low bandwidth.

I just don't want to get tied down to one hard coded policy.
User seem have different ideas about what constitutes a flow and what policy for drop should be. Existing ingress qdisc is inflexible and ifb is a pain to setup and adds
another queue transistion.

maintainer
#29 lore raw
In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>
Date: Tue, 23 Apr 2013 14:23:33 -0700

Quoted 1 line

What about just having a smarter ingress qdisc?

Ingress qdiscs go through a single lock, and that is not likely to change any time soon, nor do I find it reasonable to be required to use ingress qdiscs to handle this problem.

I find Willem's changes extremely reasonable, and a good improvement we should integrate now rather than pretending ingress qdiscs are an acceptable alternative. They aren't.

maintainer
#13 lore raw
In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>
Date: Tue, 23 Apr 2013 14:52:22 -0700

Quoted 4 lines

I just don't want to get tied down to one hard coded policy. User seem have different ideas about what constitutes a flow and what policy for drop should be. Existing ingress qdisc is inflexible and ifb is a pain to setup and adds another queue transistion.

There is no hard coded policy.

User defines his policy by what cpu he executes his socket reads and writes upon, and RFS reacts in kind.

maintainer
#31 lore raw
In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>
Date: Tue, 23 Apr 2013 16:31:34 -0400

Quoted 1 line

- disable the kconfig option by default, as it is workload specific.

If this logic only kicks in during overload, I see no reason why we shouldn't have this protection enabled unconditionally, all the time.

Does it hurt performance under normal workloads?

If the new logic is controlled by overload thresholds then it should have no impact whatsoever in non-overload scenerios.

#32 lore raw
Quoted 5 lines · David Miller

From: Willem de Bruijn <[email protected]>
Date: Tue, 23 Apr 2013 16:31:34 -0400

Quoted 1 line

- disable the kconfig option by default, as it is workload specific.

Quoted 7 lines

If this logic only kicks in during overload, I see no reason why we shouldn't have this protection enabled unconditionally, all the time.

Does it hurt performance under normal workloads?

If the new logic is controlled by overload thresholds then it should have no impact whatsoever in non-overload scenerios.

Exact.

I guess we should at least not include this code on !SMP builds

This adds some code, but as long as you don't write into flow_limit_cpu_bitmap/flow_limit_table_len we wont allocate the memory, thus this is disabled.

Then, if enabled, it only kicks if the number of queued packets is above half the max backlog (netdev_max_backlog >> 1)

So on non overload scenario, cost is 0.

On overload scenario, the loaded cpu keeps a 512 bytes array hot in his cache. (fl->history[])

maintainer
#33 lore raw

From: Eric Dumazet <[email protected]>
Date: Tue, 23 Apr 2013 16:11:23 -0700

Quoted 1 line

I guess we should at least not include this code on !SMP builds

I think CONFIG_RPS already an appropriate guard for something like this. And it depends upon SMP et al.

#34 lore raw
Quoted 7 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Tue, 23 Apr 2013 16:11:23 -0700

Quoted 1 line

I guess we should at least not include this code on !SMP builds

I think CONFIG_RPS already an appropriate guard for something like this. And it depends upon SMP et al.

Yes, something like :

config NET_FLOW_LIMIT
bool "Flow shedding under load"
depends on RPS
default y

#36 lore raw
In reply to David Miller
Quoted 5 lines · David Miller

From: Willem de Bruijn <[email protected]>
Date: Tue, 23 Apr 2013 16:31:34 -0400

Quoted 1 line

- disable the kconfig option by default, as it is workload specific.

Quoted 7 lines

If this logic only kicks in during overload, I see no reason why we shouldn't have this protection enabled unconditionally, all the time.

Does it hurt performance under normal workloads?

If the new logic is controlled by overload thresholds then it should have no impact whatsoever in non-overload scenerios.

It only adds one branch in that case. I was (probably too) conservative.

Do you want me to resubmit the entire patch with the Kconfig dependency on RPS that Eric proposed?

#35 lore raw
In reply to David Miller
Quoted 8 lines · On Tue, 23 Apr 2013 19:15:40 -0400 (EDT) David Miller <[email protected]>…

From: Eric Dumazet <[email protected]>
Date: Tue, 23 Apr 2013 16:11:23 -0700

Quoted 1 line

I guess we should at least not include this code on !SMP builds

I think CONFIG_RPS already an appropriate guard for something like this. And it depends upon SMP et al.

I wasn't meaning to say flow shedding is a bad idea, just would love to see a general solution. We are all concerned with DoS attacks. If you haven't been hit yet, somebody you know is already.

#14 lore raw
In reply to Stephen Hemminger
Quoted 4 lines · Stephen Hemminger

I just don't want to get tied down to one hard coded policy.
User seem have different ideas about what constitutes a flow and what policy for drop should be. Existing ingress qdisc is inflexible and ifb is a pain to setup and adds
another queue transistion.

qdisc code has a hardcoded dev_hard_start_xmit() call, thats why ifb hack is used. Not mentioning device flow control.

It might be possible to use a q->xmit() method instead, so that it can be used on ingress without ifb.

Then we would have to allow one qdisc per RX queue, and not use qdisc lock (assuming NAPI protects us from reentrancy).

So napi device handler would queue skbs in qdisc (q->enqueue()), (allowing a standing queue to build so that some clever qdisc can drop some selected packets)

Not really clear how we would allow packets being delivered to another queue (RPS/RFS), and not clear how/when doing the qdisc_run() to dequeue packets and deliver them to stack.

I don't know, this looks like a lot of changes.

#15 lore raw

PATCH v5 1/1 rps: selective flow shedding during softnet overflow

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---

Changes
v5
- depend on RPS, automatically build if RPS is enabled.
v4
- remove unnecessary synchronize_rcu after rcu_assign_pointer to NULL ptr
- simplify lookup of current cpu's softnet
v3
- fix race between updates to table_len sysctl during bitmap sysctl.
- fix NULL pointer dereference on alloc failure.
v2
- add fl->num_buckets element to use the actual allocated table length.
- disable the kconfig option by default, as it is workload specific.

---
 include/linux/netdevice.h  |  17 ++++++++
 net/Kconfig                |  12 ++++++
 net/core/dev.c             |  48 ++++++++++++++++++++-
 net/core/net-procfs.c      |  16 ++++++-
 net/core/sysctl_net_core.c | 104 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 194 insertions(+), 3 deletions(-)
include/linux/netdevice.h +17 -0
index f8898a4..d781cf1 100644
@@ -1779,6 +1779,19 @@ static inline int unregister_gifconf(unsigned int family)
 	return register_gifconf(family, NULL);
 }
 
+#ifdef CONFIG_NET_FLOW_LIMIT
+#define FLOW_LIMIT_HISTORY	(1 << 8)	/* must be ^2 */
+struct sd_flow_limit {
+	u64			count;
+	unsigned int		num_buckets;
+	unsigned int		history_head;
+	u16			history[FLOW_LIMIT_HISTORY];
+	u8			buckets[];
+};
+
+extern int netdev_flow_limit_table_len;
+#endif /* CONFIG_NET_FLOW_LIMIT */
+
 /*
  * Incoming packets are placed on per-cpu queues
  */
@@ -1808,6 +1821,10 @@ struct softnet_data {
 	unsigned int		dropped;
 	struct sk_buff_head	input_pkt_queue;
 	struct napi_struct	backlog;
+
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit	*flow_limit;
+#endif
 };
 
 static inline void input_queue_head_incr(struct softnet_data *sd)
net/Kconfig +12 -0
index 1a22216..02ebc71 100644
@@ -268,6 +268,18 @@ config BPF_JIT
 	  packet sniffing (libpcap/tcpdump). Note : Admin should enable
 	  this feature changing /proc/sys/net/core/bpf_jit_enable
 
+config NET_FLOW_LIMIT
+	bool "Flow shedding under load"
+	depends on RPS
+	default y
+	---help---
+	  The network stack has to drop packets when a receive processing CPUs
+	  backlog reaches netdev_max_backlog. If a few out of many active flows
+	  generate the vast majority of load, drop their traffic earlier to
+	  maintain capacity for the other flows. This feature provides servers
+	  with many clients some protection against DoS by a single (spoofed)
+	  flow that greatly exceeds average workload.
+
 menu "Network testing"
 
 config NET_PKTGEN
net/core/dev.c +47 -1

72 lines not on this page · raw message

net/core/net-procfs.c +14 -2
index 569d355..2bf8329 100644
@@ -146,11 +146,23 @@ static void softnet_seq_stop(struct seq_file *seq, void *v)
 static int softnet_seq_show(struct seq_file *seq, void *v)
 {
 	struct softnet_data *sd = v;
+	unsigned int flow_limit_count = 0;
 
-	seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit *fl;
+
+	rcu_read_lock();
+	fl = rcu_dereference(sd->flow_limit);
+	if (fl)
+		flow_limit_count = fl->count;
+	rcu_read_unlock();
+#endif
+
+	seq_printf(seq,
+		   "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
 		   sd->processed, sd->dropped, sd->time_squeeze, 0,
 		   0, 0, 0, 0, /* was fastroute */
-		   sd->cpu_collision, sd->received_rps);
+		   sd->cpu_collision, sd->received_rps, flow_limit_count);
 	return 0;
 }
 
net/core/sysctl_net_core.c +104 -0

116 lines not on this page · raw message

Signature · 1 line
1.8.2.1
Acked-by on v5 1/1
#16 lore raw
Quoted 32 lines · Willem de Bruijn

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

/*
* Incoming packets are placed on per-cpu queues
*/

include/linux/netdevice.h struct softnet_data { · line 1828

Quoted 8 lines
include/linux/netdevice.h +4 -0
@@ -1808,6 +1821,10 @@ struct softnet_data {
 	unsigned int		dropped;
 	struct sk_buff_head	input_pkt_queue;
 	struct napi_struct	backlog;
+
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit	*flow_limit;
+#endif
 };

I guess flow_limit could be put before csd, as its a read only field by its owner.
This need more testing and is a minor detail.

#22 lore raw
In reply to Eric Dumazet
Quoted 6 lines · Eric Dumazet

qdisc code has a hardcoded dev_hard_start_xmit() call, thats why ifb hack is used. Not mentioning device flow control.

It might be possible to use a q->xmit() method instead, so that it can be used on ingress without ifb.

If i understood correctly what you are trying to achieve:
I dont think one qdisc per rx queue/ring will work well in presence of qdisc since the qdisc is attached per netdev. i.e when packets are fanned out across cpu backlogs, as long as they came in via same netdev queue, they are going to share the same lock with all other cpus such packets have been fanned out to the moment you attach an ingress qdisc to that netdev ring/queue.

One unorthodox approach is to have a qdisc per backlog queue since the backlog is per cpu; given it is abstracted as a netdev, it becomes a natural fit (sans the fact backlog queue is unidirectional).

cheers,
jamal

#23 lore raw
Quoted 5 lines · Jamal Hadi Salim
Quoted 3 lines · Eric Dumazet

qdisc code has a hardcoded dev_hard_start_xmit() call, thats why ifb hack is used. Not mentioning device flow control.

Quoted 7 lines
Quoted 3 lines

It might be possible to use a q->xmit() method instead, so that it can be used on ingress without ifb.

If i understood correctly what you are trying to achieve:
I dont think one qdisc per rx queue/ring will work well in presence of qdisc since the qdisc is attached per netdev.

MQ permits to have one qdisc per TX queue.

It would be the same concept in ingress.

Quoted 5 lines

i.e when packets are fanned out across cpu backlogs, as long as they came in via same netdev queue, they are going to share the same lock with all other cpus such packets have been fanned out to the moment you attach an ingress qdisc to that netdev ring/queue.

Not sure what you mean. The qdisc stuff would replace the 'cpu backlog', not be added to it. Think of having possibility to control backlog using standard qdiscs, like fq_codel ;)

Quoted 4 lines

One unorthodox approach is to have a qdisc per backlog queue since the backlog is per cpu; given it is abstracted as a netdev, it becomes a natural fit (sans the fact backlog queue is unidirectional).

Yes, but the per cpu backlog is shared for all devices. We probably want different qdisc for gre tunnel, eth0, ...

#24 lore raw
Quoted 4 lines · Eric Dumazet

On Tue, 2013-04-23 at 21:25 -0400, Jamal Hadi Salim wrote:

Not sure what you mean. The qdisc stuff would replace the 'cpu backlog',

Aha ;->
So you would have many little backlogs one per ring per cpu, correct?

Quoted 2 lines

not be added to it. Think of having possibility to control backlog using standard qdiscs, like fq_codel ;)

Excellent. So this is not as a big surgery as it sounds then. the backloglets just need to be exposed as netdevs.

Quoted 2 lines

Yes, but the per cpu backlog is shared for all devices. We probably want different qdisc for gre tunnel, eth0, ...

Makes sense.

BTW, looking at __skb_get_rxhash(), if i had a driver that sets either
skb->rxhash (picks it off the dma descriptor), could i not use that instead of computing the hash? something like attached patch.

cheers,
jamal

net/core/flow_dissector.c +2 -1
index e187bf0..a6abee0 100644
@@ -159,8 +159,9 @@ void __skb_get_rxhash(struct sk_buff *skb)
 	struct flow_keys keys;
 	u32 hash;
 
-	if (!skb_flow_dissect(skb, &keys))
+	if (skb->rxhash || !skb_flow_dissect(skb, &keys)) {
 		return;
+	}
 
 	if (keys.ports)
 		skb->l4_rxhash = 1;
#25 lore raw
Quoted 5 lines · Jamal Hadi Salim

BTW, looking at __skb_get_rxhash(), if i had a driver that sets either skb->rxhash (picks it off the dma descriptor), could i not use that instead of computing the hash? something like attached patch.

The caller does this already ;)

static inline __u32 skb_get_rxhash(struct sk_buff *skb)

{
        if (!skb->l4_rxhash)
                __skb_get_rxhash(skb);

        return skb->rxhash;
}

Rationale being : if l4 rxhash was already provided, use it.

AFAIK, only bnx2x provides this.

For other cases, we prefer trying a software rxhash, as it gives us more capabilities than the standard Toepliz hash (Not l4 for UDP flows for example)

#26 lore raw
Quoted 2 lines · Eric Dumazet

The caller does this already ;)

[..]

Quoted 10 lines

Rationale being : if l4 rxhash was already provided, use it.

AFAIK, only bnx2x provides this.

For other cases, we prefer trying a software rxhash, as it gives us more
capabilities than the standard Toepliz hash (Not l4 for UDP flows for example)

I forgot about the Toepliz hash connection. I can see it makes sense here.

Let me clarify:
In the scenario i am thinking of, I have clever hardware which is smart enough to deal with details of identifying flow state(including fragementation etc) and tagging it in a DMA descriptor with 32 bit id. I want to be able to take the tag produced by the hardware and use
that for rps cpu selection i.e assume the hardware has already done the
hashing and is giving me a 32 bit id. My initial thought was skb->rxhash
is the right spot to store this; then make get_rps_cpu() do the
selection based on this. l4 rxhash is 1 bit which is too small.

cheers,
jamal

#27 lore raw
Quoted 11 lines · Jamal Hadi Salim

I forgot about the Toepliz hash connection. I can see it makes sense here.

Let me clarify:
In the scenario i am thinking of, I have clever hardware which is smart enough to deal with details of identifying flow state(including fragementation etc) and tagging it in a DMA descriptor with 32 bit id. I want to be able to take the tag produced by the hardware and use that for rps cpu selection i.e assume the hardware has already done the hashing and is giving me a 32 bit id. My initial thought was skb->rxhash is the right spot to store this; then make get_rps_cpu() do the selection based on this. l4 rxhash is 1 bit which is too small.

Set skb->rxrhash to the hash your hardware computed, and skb->l4_rxhash to 1.

Then get_rps_cpu() will use skb->rxhash happily

(and other callers of skb_get_rxhash() as well)

Not clear what you mean by fragmentation : fragmented frames have no flow information (but the first fragment)

maintainer
#17 lore raw
In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>
Date: Tue, 23 Apr 2013 20:37:27 -0400

Quoted 19 lines

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

Quoted 6 lines

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

This does't compile:

net/core/sysctl_net_core.c: In function ‘flow_limit_cpu_sysctl’:
net/core/sysctl_net_core.c:114:10: error: invalid type argument of ‘->’ (have ‘struct mutex’)

Also, please change the Kconfig entry to be:

config NET_FLOW_LIMIT
boolean
depends on RPS
default y

like RPS et al. are.

Thanks.

#18 lore raw

PATCH v6 1/1 rps: selective flow shedding during softnet overflow

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---

Changes
v6
- bugfix: lockdep_is_held bug with CONFIG_PROVE_RCU
(now built with allyesconfig)
- kconfig: update to follow format of RPS
v5
- depend on RPS, automatically build if RPS is enabled.
v4
- remove unnecessary synchronize_rcu after rcu_assign_pointer to NULL ptr
- simplify lookup of current cpu's softnet
v3
- fix race between updates to table_len sysctl during bitmap sysctl.
- fix NULL pointer dereference on alloc failure.
v2
- add fl->num_buckets element to use the actual allocated table length.
- disable the kconfig option by default, as it is workload specific.

---
 include/linux/netdevice.h  |  17 ++++++++
 net/Kconfig                |  12 ++++++
 net/core/dev.c             |  48 ++++++++++++++++++++-
 net/core/net-procfs.c      |  16 ++++++-
 net/core/sysctl_net_core.c | 104 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 194 insertions(+), 3 deletions(-)
include/linux/netdevice.h +17 -0
index a94a5a0..7dd535d 100644
@@ -1778,6 +1778,19 @@ static inline int unregister_gifconf(unsigned int family)
 	return register_gifconf(family, NULL);
 }
 
+#ifdef CONFIG_NET_FLOW_LIMIT
+#define FLOW_LIMIT_HISTORY	(1 << 8)	/* must be ^2 */
+struct sd_flow_limit {
+	u64			count;
+	unsigned int		num_buckets;
+	unsigned int		history_head;
+	u16			history[FLOW_LIMIT_HISTORY];
+	u8			buckets[];
+};
+
+extern int netdev_flow_limit_table_len;
+#endif /* CONFIG_NET_FLOW_LIMIT */
+
 /*
  * Incoming packets are placed on per-cpu queues
  */
@@ -1807,6 +1820,10 @@ struct softnet_data {
 	unsigned int		dropped;
 	struct sk_buff_head	input_pkt_queue;
 	struct napi_struct	backlog;
+
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit	*flow_limit;
+#endif
 };
 
 static inline void input_queue_head_incr(struct softnet_data *sd)
net/Kconfig +12 -0
index 2ddc904..08de901 100644
@@ -259,6 +259,18 @@ config BPF_JIT
 	  packet sniffing (libpcap/tcpdump). Note : Admin should enable
 	  this feature changing /proc/sys/net/core/bpf_jit_enable
 
+config NET_FLOW_LIMIT
+	boolean
+	depends on RPS
+	default y
+	---help---
+	  The network stack has to drop packets when a receive processing CPU's
+	  backlog reaches netdev_max_backlog. If a few out of many active flows
+	  generate the vast majority of load, drop their traffic earlier to
+	  maintain capacity for the other flows. This feature provides servers
+	  with many clients some protection against DoS by a single (spoofed)
+	  flow that greatly exceeds average workload.
+
 menu "Network testing"
 
 config NET_PKTGEN
net/core/dev.c +47 -1

72 lines not on this page · raw message

net/core/net-procfs.c +14 -2
index 569d355..2bf8329 100644
@@ -146,11 +146,23 @@ static void softnet_seq_stop(struct seq_file *seq, void *v)
 static int softnet_seq_show(struct seq_file *seq, void *v)
 {
 	struct softnet_data *sd = v;
+	unsigned int flow_limit_count = 0;
 
-	seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
+#ifdef CONFIG_NET_FLOW_LIMIT
+	struct sd_flow_limit *fl;
+
+	rcu_read_lock();
+	fl = rcu_dereference(sd->flow_limit);
+	if (fl)
+		flow_limit_count = fl->count;
+	rcu_read_unlock();
+#endif
+
+	seq_printf(seq,
+		   "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
 		   sd->processed, sd->dropped, sd->time_squeeze, 0,
 		   0, 0, 0, 0, /* was fastroute */
-		   sd->cpu_collision, sd->received_rps);
+		   sd->cpu_collision, sd->received_rps, flow_limit_count);
 	return 0;
 }
 
net/core/sysctl_net_core.c +104 -0

116 lines not on this page · raw message

Signature · 1 line
1.8.2.1
Acked-by on v6 1/1
#19 lore raw
Quoted 19 lines · Willem de Bruijn

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

Quoted 8 lines

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

---

Willemb, are you planning to add a section in
Documentation/networking/scaling.txt ?

Thanks

#20 lore raw
Quoted 2 lines

Willemb, are you planning to add a section in
Documentation/networking/scaling.txt ?

Good point, I should. I will send a follow-on patch documenting the mechanism and sysctls.

maintainer Applied
#21 lore raw
In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>
Date: Mon, 20 May 2013 09:00:54 -0700

Quoted 25 lines · Willem de Bruijn
Quoted 24 lines

A cpu executing the network receive path sheds packets when its input queue grows to netdev_max_backlog. A single high rate flow (such as a spoofed source DoS) can exceed a single cpu processing rate and will degrade throughput of other flows hashed onto the same cpu.

This patch adds a more fine grained hashtable. If the netdev backlog is above a threshold, IRQ cpus track the ratio of total traffic of each flow (using 4096 buckets, configurable). The ratio is measured by counting the number of packets per flow over the last 256 packets from the source cpu. Any flow that occupies a large fraction of this (set at 50%) will see packet drop while above the threshold.

Tested:
Setup is a muli-threaded UDP echo server with network rx IRQ on cpu0, kernel receive (RPS) on cpu0 and application threads on cpus 2--7 each handling 20k req/s. Throughput halves when hit with a 400 kpps antagonist storm. With this patch applied, antagonist overload is dropped and the server processes its complete load.

The patch is effective when kernel receive processing is the bottleneck. The above RPS scenario is a extreme, but the same is reached with RFS and sufficient kernel processing (iptables, packet socket tap, ..).

Quoted 5 lines
Quoted 3 lines

---

Applied, thanks guys.