Skip to contentSkip to conversation

net-tcp: TCP/IP stack bypass for loopback connections

PATCH Superseded Bruce "Brutus" Curtis 8 participants 16 messages Aug 10, 2012 – Sep 9, 2012

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net-tcp: TCP/IP stack bypass for loopback connections

From: "Bruce \"Brutus\" Curtis" <[email protected]>

TCP/IP loopback socket pair stack bypass, based on an idea by, and rough upstream patch from, David Miller <[email protected]> called "friends", the data structure modifcations and connection scheme are reused with extensive data-path changes.

A new sysctl, net.ipv4.tcp_friends, is added:
0: disable friends and use the stock data path.
1: enable friends and bypass the stack data path, the default.

Note, when friends is enabled any loopback interpose, e.g. tcpdump, will only see the TCP/IP packets during connection establishment and finish, all data bypasses the stack and instead is delivered to the destination socket directly.

Testing done on a 4 socket 2.2GHz "Quad-Core AMD Opteron(tm) Processor 8354 CPU" based system, netperf results for a single connection show increased TCP_STREAM throughput, increased TCP_RR and TCP_CRR transaction rate for most message sizes vs baseline and comparable to AF_UNIX.

Significant increase (up to 4.88x) in aggregate throughput for multiple netperf runs (STREAM 32KB I/O x N) is seen.

Some results:

Default netperf: netperf
netperf -t STREAM_STREAM
netperf -t STREAM_STREAM -- -s 51882 -m 16384 -M 87380 netperf

Baseline AF_UNIX AF_UNIX Friends
Mbits/S Mbits/S Mbits/S Mbits/S
7152 669 7% 9322 130% 1393% 10642 149% 1591% 114%

Note, for the AF_UNIX (STREAM_STREAM) test 2 results are listed, 1st with no options but as the defaults for AF_UNIX sockets are much lower performaning a 2nd set of runs with a socket buffer size and send/recv buffer sizes equivalent to AF_INET (TCP_STREAM) are done.

Note, all subsequent AF_UNIX (STREAM_STREAM, STREAM_RR) tests are done with "-s 51882" such that the same total effective socket buffering is used as for the AF_INET runs defaults (16384+87380/2).

STREAM 32KB I/O x N: netperf -l 100 -t TCP_STREAM -- -m 32K -M 32K

		     netperf -l 100 -t STREAM_STREAM -- -s 51882 -m 32K -M 3
		     netperf -l 100 -t TCP_STREAM -- -m 32K -M 32K

	  Baseline  AF_UNIX      Friends
   N  COC Mbits/S   Mbits/S      Mbits/S
   1   -    9054      8753  97%   10697 118% 122%
   2   -   18671     16097  86%   19280 103% 120%
  16   2   72033    289222 402%  351253 488% 121%
  32   4   64789    215364 332%  256848 396% 119%
 256  32   71660     99081 138%  239952 335% 242%
 512  64   80243     93453 116%  230425 287% 247%
1600 200  112352    251095 223%  373718 333% 149%

COC = Cpu Over Commit ratio (16 core platform)

STREAM: netperf -l 100 -t TCP_STREAM

	netperf -l 100 -t STREAM_STREAM -- -s 51882 -m 32K -M 32K
	netperf -l 100 -t TCP_STREAM
netperf  Baseline  AF_UNIX      Friends
-m/-M N  Mbits/S   Mbits/S      Mbits/S
  64        860       430  50%     533  62% 124%
  1K       4599      4296  93%    5111 111% 119%
  8K       5957      7663 129%    9738 163% 127%
 32K       8355      9255 111%   11004 132% 119%
 64K       9188      9498 103%   11094 121% 117%
128K       9240      9799 106%   12959 140% 132%
256K       9987     10351 104%   13940 140% 135%
512K      10326     10032  97%   13492 131% 134%
  1M       8825      9492 108%   12393 140% 131%
 16M       7240      9229 127%   11214 155% 122%

RR: netperf -l 100 -t TCP_RR
netperf -l 100 -t STREAM_RR -- -s 51882 -m 16384 -M 87380 netperf -l 100 -t TCP_RR

netperf  Baseline  AF_UNIX      Friends
-r N,N   Trans./S  Trans./S     Trans./S
  64      46928     87522 187%   84995 181%  97%
  1K      43646     85426 196%   82056 188%  96%
  8K      26492     29943 113%   30875 117% 103%
 32K      10933     12080 110%   13103 120% 108%
 64K       7048      6274  89%    7069 100% 113%
128K       4374      3275  75%    3633  83% 111%
256K       2393      1889  79%    2120  89% 112%
512K        995      1060 107%    1165 117% 110%

1M 414 505 122% 499 121% 99%
16M 26.1 33.1 127% 32.6 125% 98%

CRR: netperf -l 100 -t TCP_CRR
netperf -l 100 -t TCP_CRR

netperf  Baseline  AF_UNIX      Friends
  -r N   Trans./S  Trans./S     Trans./S
  64      16167         -        18647 115%   -
  1K      14834         -        18274 123%   -
  8K      11880         -        14719 124%   -
 32K       7247         -         8956 124%   -
 64K       4456         -         5595 126%   -
128K       2344         -         3144 134%   -
256K       1286         -         1962 153%   -
512K        626         -         1047 167%   -
  1M        361         -          459 127%   -
 16M       27.4         -         32.2 118%   -

Note, "-" denotes test not supported for transport.

SPLICE 32KB I/O:

Source

 Sink   Baseline  Friends
 FSFS   Mbits/S   Mbits/S
 ----     9300      9686 104%
 Z---     8656      9670 112%
 --N-     9636     10704 111%
 Z-N-     8200      8017  98%
 -S--    20480     30101 147%
 ZS--     8834      9221 104%
 -SN-    20198     32122 159%
 ZSN-     8557      9267 108%
 ---S     8874      9805 110%
 Z--S     8088      9487 117%
 --NS    12881     11265  87%
 Z-NS    10700      8147  76%
 -S-S    14964     21975 147%
 ZS-S     8261      8809 107%
 -SNS    17394     29366 169%
 ZSNS    11456     10674  93%

Note, "Z" source File /dev/zero, "-" source user memory
"N" sink File /dev/null, "-" sink user memory "S" Splice on, "-" Splice off

---
 Documentation/networking/ip-sysctl.txt |    8 +
 include/linux/skbuff.h                 |    2 +
 include/net/request_sock.h             |    1 +
 include/net/sock.h                     |   32 ++-
 include/net/tcp.h                      |    3 +-
 net/core/skbuff.c                      |    1 +
 net/core/sock.c                        |    1 +
 net/core/stream.c                      |   36 +++
 net/ipv4/inet_connection_sock.c        |   20 ++
 net/ipv4/sysctl_net_ipv4.c             |    7 +
 net/ipv4/tcp.c                         |  500 +++++++++++++++++++++++++++----
 net/ipv4/tcp_input.c                   |   22 ++-
 net/ipv4/tcp_ipv4.c                    |    2 +
 net/ipv4/tcp_minisocks.c               |    5 +
 net/ipv4/tcp_output.c                  |   18 +-
 net/ipv6/tcp_ipv6.c                    |    1 +
 16 files changed, 584 insertions(+), 75 deletions(-)
Documentation/networking/ip-sysctl.txt +8 -0
index ca447b3..8344c05 100644
@@ -214,6 +214,14 @@ tcp_fack - BOOLEAN
 	Enable FACK congestion avoidance and fast retransmission.
 	The value is not used, if tcp_sack is not enabled.
 
+tcp_friends - BOOLEAN
+	If set, TCP loopback socket pair stack bypass is enabled such
+	that all data sent will be directly queued to the receiver's
+	socket for receive. Note, normal connection establishment and
+	finish is used to make friends so any loopback interpose, e.g.
+	tcpdump, will see these TCP segements but no data segments.
+	Default: 1
+
 tcp_fin_timeout - INTEGER
 	Time to hold socket in state FIN-WAIT-2, if it was closed
 	by our side. Peer can be broken and never close its side,
include/linux/skbuff.h +2 -0
index b33a3a1..a2e86a6 100644
@@ -332,6 +332,7 @@ typedef unsigned char *sk_buff_data_t;
  *	@cb: Control buffer. Free for use by every layer. Put private vars here
  *	@_skb_refdst: destination entry (with norefcount bit)
  *	@sp: the security path, used for xfrm
+ *	@friend: loopback friend socket
  *	@len: Length of actual data
  *	@data_len: Data length
  *	@mac_len: Length of link layer header
@@ -407,6 +408,7 @@ struct sk_buff {
 #ifdef CONFIG_XFRM
 	struct	sec_path	*sp;
 #endif
+	struct sock		*friend;
 	unsigned int		len,
 				data_len;
 	__u16			mac_len,
include/net/request_sock.h +1 -0
index 4c0766e..2c74420 100644
@@ -63,6 +63,7 @@ struct request_sock {
 	unsigned long			expires;
 	const struct request_sock_ops	*rsk_ops;
 	struct sock			*sk;
+	struct sock			*friend;
 	u32				secid;
 	u32				peer_secid;
 };
include/net/sock.h +29 -3
index 72132ae..0913dff 100644
@@ -197,6 +197,7 @@ struct cg_proto;
   *	@sk_userlocks: %SO_SNDBUF and %SO_RCVBUF settings
   *	@sk_lock:	synchronizer
   *	@sk_rcvbuf: size of receive buffer in bytes
+  *	@sk_friend: loopback friend socket
   *	@sk_wq: sock wait queue and async head
   *	@sk_rx_dst: receive input route used by early tcp demux
   *	@sk_dst_cache: destination cache
@@ -287,6 +288,14 @@ struct sock {
 	socket_lock_t		sk_lock;
 	struct sk_buff_head	sk_receive_queue;
 	/*
+	 * If socket has a friend (sk_friend != NULL) then a send skb is
+	 * enqueued directly to the friend's sk_receive_queue such that:
+	 *
+	 *        sk_sndbuf -> sk_sndbuf + sk_friend->sk_rcvbuf
+	 *   sk_wmem_queued -> sk_friend->sk_rmem_alloc
+	 */
+	struct sock		*sk_friend;
+	/*
 	 * The backlog queue is special, it is always used with
 	 * the per-socket spinlock held and requires low latency
 	 * access. Therefore we special case it's implementation.
@@ -696,24 +705,40 @@ static inline bool sk_acceptq_is_full(const struct sock *sk)
 	return sk->sk_ack_backlog > sk->sk_max_ack_backlog;
 }
 
+static inline int sk_wmem_queued_get(const struct sock *sk)
+{
+	if (sk->sk_friend)
+		return atomic_read(&sk->sk_friend->sk_rmem_alloc);
+	else
+		return sk->sk_wmem_queued;
+}
+
+static inline int sk_sndbuf_get(const struct sock *sk)
+{
+	if (sk->sk_friend)
+		return sk->sk_sndbuf + sk->sk_friend->sk_rcvbuf;
+	else
+		return sk->sk_sndbuf;
+}
+
 /*
  * Compute minimal free write space needed to queue new packets.
  */
 static inline int sk_stream_min_wspace(const struct sock *sk)
 {
-	return sk->sk_wmem_queued >> 1;
+	return sk_wmem_queued_get(sk) >> 1;
 }
 
 static inline int sk_stream_wspace(const struct sock *sk)
 {
-	return sk->sk_sndbuf - sk->sk_wmem_queued;
+	return sk_sndbuf_get(sk) - sk_wmem_queued_get(sk);
 }
 
 extern void sk_stream_write_space(struct sock *sk);
 
 static inline bool sk_stream_memory_free(const struct sock *sk)
 {
-	return sk->sk_wmem_queued < sk->sk_sndbuf;
+	return sk_wmem_queued_get(sk) < sk_sndbuf_get(sk);
 }
 
 /* OOB backlog add */
@@ -822,6 +847,7 @@ static inline void sock_rps_reset_rxhash(struct sock *sk)
 	})
 
 extern int sk_stream_wait_connect(struct sock *sk, long *timeo_p);
+extern int sk_stream_wait_friend(struct sock *sk, long *timeo_p);
 extern int sk_stream_wait_memory(struct sock *sk, long *timeo_p);
 extern void sk_stream_wait_close(struct sock *sk, long timeo_p);
 extern int sk_stream_error(struct sock *sk, int flags, int err);
include/net/tcp.h +2 -1
index e19124b..baa981b 100644
@@ -266,6 +266,7 @@ extern int sysctl_tcp_thin_dupack;
 extern int sysctl_tcp_early_retrans;
 extern int sysctl_tcp_limit_output_bytes;
 extern int sysctl_tcp_challenge_ack_limit;
+extern int sysctl_tcp_friends;
 
 extern atomic_long_t tcp_memory_allocated;
 extern struct percpu_counter tcp_sockets_allocated;
@@ -1011,7 +1012,7 @@ static inline bool tcp_prequeue(struct sock *sk, struct sk_buff *skb)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 
-	if (sysctl_tcp_low_latency || !tp->ucopy.task)
+	if (sysctl_tcp_low_latency || !tp->ucopy.task || sk->sk_friend)
 		return false;
 
 	__skb_queue_tail(&tp->ucopy.prequeue, skb);
net/core/skbuff.c +1 -0
index fe00d12..7cb73e6 100644
@@ -703,6 +703,7 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
 #ifdef CONFIG_XFRM
 	new->sp			= secpath_get(old->sp);
 #endif
+	new->friend		= old->friend;
 	memcpy(new->cb, old->cb, sizeof(old->cb));
 	new->csum		= old->csum;
 	new->local_df		= old->local_df;
net/core/sock.c +1 -0
index 8f67ced..8d0707f 100644
@@ -2134,6 +2134,7 @@ void sock_init_data(struct socket *sock, struct sock *sk)
 #ifdef CONFIG_NET_DMA
 	skb_queue_head_init(&sk->sk_async_wait_queue);
 #endif
+	sk->sk_friend		=	NULL;
 
 	sk->sk_send_head	=	NULL;
 
net/core/stream.c +36 -0
index f5df85d..85e5b03 100644
@@ -83,6 +83,42 @@ int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
 EXPORT_SYMBOL(sk_stream_wait_connect);
 
 /**
+ * sk_stream_wait_friend - Wait for a socket to make friends
+ * @sk: sock to wait on
+ * @timeo_p: for how long to wait
+ *
+ * Must be called with the socket locked.
+ */
+int sk_stream_wait_friend(struct sock *sk, long *timeo_p)
+{
+	struct task_struct *tsk = current;
+	DEFINE_WAIT(wait);
+	int done;
+
+	do {
+		int err = sock_error(sk);
+		if (err)
+			return err;
+		if (!sk->sk_friend)
+			return -EBADFD;
+		if (!*timeo_p)
+			return -EAGAIN;
+		if (signal_pending(tsk))
+			return sock_intr_errno(*timeo_p);
+
+		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
+		sk->sk_write_pending++;
+		done = sk_wait_event(sk, timeo_p,
+				     !sk->sk_err &&
+				     sk->sk_friend->sk_friend);
+		finish_wait(sk_sleep(sk), &wait);
+		sk->sk_write_pending--;
+	} while (!done);
+	return 0;
+}
+EXPORT_SYMBOL(sk_stream_wait_friend);
+
+/**
  * sk_stream_closing - Return 1 if we still have things to send in our buffers.
  * @sk: socket to verify
  */
net/ipv4/inet_connection_sock.c +20 -0
index db0cf17..6b4c26c 100644
@@ -623,6 +623,26 @@ struct sock *inet_csk_clone_lock(const struct sock *sk,
 	if (newsk != NULL) {
 		struct inet_connection_sock *newicsk = inet_csk(newsk);
 
+		if (req->friend) {
+			/*
+			 * Make friends with the requestor but the ACK of
+			 * the request is already in-flight so the race is
+			 * on to make friends before the ACK is processed.
+			 * If the requestor's sk_friend value is != NULL
+			 * then the requestor has already processed the
+			 * ACK so indicate state change to wake'm up.
+			 */
+			struct sock *was;
+
+			sock_hold(req->friend);
+			newsk->sk_friend = req->friend;
+			sock_hold(newsk);
+			was = xchg(&req->friend->sk_friend, newsk);
+			/* If requester already connect()ed, maybe sleeping */
+			if (was && !sock_flag(req->friend, SOCK_DEAD))
+				sk->sk_state_change(req->friend);
+		}
+
 		newsk->sk_state = TCP_SYN_RECV;
 		newicsk->icsk_bind_hash = NULL;
 
net/ipv4/sysctl_net_ipv4.c +7 -0
index 1b5ce96..dd3936f 100644
@@ -737,6 +737,13 @@ static struct ctl_table ipv4_table[] = {
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &zero
 	},
+	{
+		.procname	= "tcp_friends",
+		.data		= &sysctl_tcp_friends,
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler	= proc_dointvec
+	},
 	{ }
 };
 
net/ipv4/tcp.c +435 -65

784 lines not on this page · raw message

net/ipv4/tcp_input.c +18 -4
index fa2c2c2..557191f 100644
@@ -530,6 +530,9 @@ void tcp_rcv_space_adjust(struct sock *sk)
 	int time;
 	int space;
 
+	if (sk->sk_friend)
+		return;
+
 	if (tp->rcvq_space.time == 0)
 		goto new_measure;
 
@@ -4358,8 +4361,9 @@ static int tcp_prune_queue(struct sock *sk);
 static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb,
 				 unsigned int size)
 {
-	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
-	    !sk_rmem_schedule(sk, skb, size)) {
+	if (!sk->sk_friend &&
+	    (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
+	    !sk_rmem_schedule(sk, skb, size))) {
 
 		if (tcp_prune_queue(sk) < 0)
 			return -1;
@@ -5742,6 +5746,16 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
 		 *    state to ESTABLISHED..."
 		 */
 
+		if (skb->friend) {
+			/*
+			 * If friends haven't been made yet, our sk_friend
+			 * still == NULL, then update with the ACK's friend
+			 * value (the listen()er's sock addr) which is used
+			 * as a place holder.
+			 */
+			cmpxchg(&sk->sk_friend, NULL, skb->friend);
+		}
+
 		TCP_ECN_rcv_synack(tp, th);
 
 		tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
@@ -5818,9 +5832,9 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
 		    tcp_rcv_fastopen_synack(sk, skb, &foc))
 			return -1;
 
-		if (sk->sk_write_pending ||
+		if (!skb->friend && (sk->sk_write_pending ||
 		    icsk->icsk_accept_queue.rskq_defer_accept ||
-		    icsk->icsk_ack.pingpong) {
+		    icsk->icsk_ack.pingpong)) {
 			/* Save one ACK. Data will be ready after
 			 * several ticks, if write_pending is set.
 			 *
net/ipv4/tcp_ipv4.c +2 -0
index 42b2a6a..90f2419 100644
@@ -1314,6 +1314,8 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
 	tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
 #endif
 
+	req->friend = skb->friend;
+
 	tcp_clear_options(&tmp_opt);
 	tmp_opt.mss_clamp = TCP_MSS_DEFAULT;
 	tmp_opt.user_mss  = tp->rx_opt.user_mss;
net/ipv4/tcp_minisocks.c +5 -0
index 232a90c..dcd2ffd 100644
@@ -268,6 +268,11 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)
 	const struct tcp_sock *tp = tcp_sk(sk);
 	bool recycle_ok = false;
 
+	if (sk->sk_friend) {
+		tcp_done(sk);
+		return;
+	}
+
 	if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)
 		recycle_ok = tcp_remember_stamp(sk);
 
net/ipv4/tcp_output.c +16 -2
index a7b3ec9..217ec9e 100644
@@ -65,6 +65,9 @@ int sysctl_tcp_base_mss __read_mostly = TCP_BASE_MSS;
 /* By default, RFC2861 behavior.  */
 int sysctl_tcp_slow_start_after_idle __read_mostly = 1;
 
+/* By default, TCP loopback bypass */
+int sysctl_tcp_friends __read_mostly = 1;
+
 int sysctl_tcp_cookie_size __read_mostly = 0; /* TCP_COOKIE_MAX */
 EXPORT_SYMBOL_GPL(sysctl_tcp_cookie_size);
 
@@ -1012,9 +1015,14 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
 	tcb = TCP_SKB_CB(skb);
 	memset(&opts, 0, sizeof(opts));
 
-	if (unlikely(tcb->tcp_flags & TCPHDR_SYN))
+	if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
+		if (sysctl_tcp_friends) {
+			/* Only try to make friends if enabled */
+			skb->friend = sk;
+		}
+
 		tcp_options_size = tcp_syn_options(sk, skb, &opts, &md5);
-	else
+	} else
 		tcp_options_size = tcp_established_options(sk, skb, &opts,
 							   &md5);
 	tcp_header_size = tcp_options_size + sizeof(struct tcphdr);
@@ -2707,6 +2715,12 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
 	}
 
 	memset(&opts, 0, sizeof(opts));
+
+	if (sysctl_tcp_friends) {
+		/* Only try to make friends if enabled */
+		skb->friend = sk;
+	}
+
 #ifdef CONFIG_SYN_COOKIES
 	if (unlikely(req->cookie_ts))
 		TCP_SKB_CB(skb)->when = cookie_init_timestamp(req);
net/ipv6/tcp_ipv6.c +1 -0
index c66b90f..bdffbb0 100644
@@ -1037,6 +1037,7 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
 	tcp_rsk(req)->af_specific = &tcp_request_sock_ipv6_ops;
 #endif
 
+	req->friend = skb->friend;
 	tcp_clear_options(&tmp_opt);
 	tmp_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
 	tmp_opt.user_mss = tp->rx_opt.user_mss;
Signature · 1 line
1.7.7.3
#1 lore raw

PATCH 1/1 TCP/IP stack bypass for loopback connections fix

I found two problems then I tested Bruce Curtis <[email protected]> loopback patch, http://patchwork.ozlabs.org/patch/176304/

1 ‘friends’ undeclared in function ‘tcp_recvmsg’
I think it should use sysctl_tcp_friends

2 scheduling while atomic

BUG: scheduling while atomic: netperf/3820/0x10000200
Modules linked in: bridge stp llc autofs4 sunrpc ipv6 uinput
iTCO_wdt iTCO_vendor_suppo]
Pid: 3820, comm: netperf Tainted: G        W    3.5.0+ #11
Call Trace:
[<ffffffff810861e2>] __schedule_bug+0x52/0x60
[<ffffffff8151e7de>] __schedule+0x68e/0x710
[<ffffffff8108a80a>] __cond_resched+0x2a/0x40
[<ffffffff8151e8f0>] _cond_resched+0x30/0x40
[<ffffffff8149b68e>] tcp_sendmsg+0x96e/0x12c0
[<ffffffff814bf928>] inet_sendmsg+0x48/0xb0
[<ffffffff812081a3>] ? selinux_socket_sendmsg+0x23/0x30
[<ffffffff8143c4be>] sock_sendmsg+0xbe/0xf0
[<ffffffff81078958>] ? finish_wait+0x68/0x90
[<ffffffff81172660>] ? fget_light+0x50/0xc0
[<ffffffff8143c629>] sys_sendto+0x139/0x190
[<ffffffff810d21cc>] ?  __audit_syscall_entry+0xcc/0x210
[<ffffffff810d20a6>] ?  __audit_syscall_exit+0x3d6/0x430
[<ffffffff81527c29>] system_call_fastpath+0x16/0x1b

The reason is that in tcp_friend_tail(), it holds a spinlock, spin_lock_bh(&friend->sk_lock.slock);
but skb_add_data_nocache() can cause schedule, so this warning happends.

Wit this patch, the original patch can apply on top of net-next tree commit 79cda75a107da(fib: use __fls() on non null argument), and I test it works fine on x86_64 system.

---
 net/ipv4/tcp.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
net/ipv4/tcp.c +6 -6
index 6dc267c..bab15da 100644
@@ -628,7 +628,7 @@ static inline struct sk_buff *tcp_friend_tail(struct sock *sk, int *copy)
 	int		sz = 0;
 
 	if (skb_peek_tail(&friend->sk_receive_queue)) {
-		spin_lock_bh(&friend->sk_lock.slock);
+		lock_sock(friend);
 		skb = skb_peek_tail(&friend->sk_receive_queue);
 		if (skb && skb->friend) {
 			if (!*copy)
@@ -637,7 +637,7 @@ static inline struct sk_buff *tcp_friend_tail(struct sock *sk, int *copy)
 				sz = *copy - skb->len;
 		}
 		if (!skb || sz <= 0)
-			spin_unlock_bh(&friend->sk_lock.slock);
+			release_sock(friend);
 	}
 
 	*copy = sz;
@@ -655,7 +655,7 @@ static inline void tcp_friend_seq(struct sock *sk, int copy, int charge)
 	}
 	tp->rcv_nxt += copy;
 	tp->rcv_wup += copy;
-	spin_unlock_bh(&friend->sk_lock.slock);
+	release_sock(friend);
 
 	friend->sk_data_ready(friend, copy);
 
@@ -676,7 +676,7 @@ static inline int tcp_friend_push(struct sock *sk, struct sk_buff *skb)
 		return -ECONNRESET;
 	}
 
-	spin_lock_bh(&friend->sk_lock.slock);
+	lock_sock(friend);
 	skb->friend = sk;
 	skb_set_owner_r(skb, friend);
 	__skb_queue_tail(&friend->sk_receive_queue, skb);
@@ -1505,7 +1505,7 @@ out:
 
 do_fault:
 	if (friend_tail)
-		spin_unlock_bh(&friend->sk_lock.slock);
+		release_sock(friend);
 	else if (!skb->len) {
 		if (friend)
 			__kfree_skb(skb);
@@ -1932,7 +1932,7 @@ int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
 			available = TCP_SKB_CB(skb)->seq + skb->len - (*seq);
 		if ((available < target) &&
 		    (len > sysctl_tcp_dma_copybreak) && !(flags & MSG_PEEK) &&
-		    !sysctl_tcp_low_latency && !friends &&
+		    !sysctl_tcp_low_latency && !sysctl_tcp_friends &&
 		    net_dma_find_channel()) {
 			preempt_enable_no_resched();
 			tp->ucopy.pinned_list =
Signature · 1 line
1.7.1
#2 lore raw
In reply to Bruce "Brutus" Curtis
Quoted 10 lines · Bruce "Brutus" Curtis

From: "Bruce \"Brutus\" Curtis" <[email protected]>

TCP/IP loopback socket pair stack bypass, based on an idea by, and rough upstream patch from, David Miller <[email protected]> called "friends", the data structure modifcations and connection scheme are reused with extensive data-path changes.

A new sysctl, net.ipv4.tcp_friends, is added:
0: disable friends and use the stock data path.
1: enable friends and bypass the stack data path, the default.

The following is from a user perspective, since I am not intimately familiar with the internals of the TCP stack.

I think tcp_friends is a poor name from a user POV. Something like tcp_bypass would be much better.

I also believe it should be disabled by default, as that is the current behavior, and those who would gain an advantage from using it can easily enable it.

Changing the behavior would violate the principle of least surprise. Loopback TCP testing of an application or system is often a useful first step in evaluating its behavior and performance. If the TCP stack is bypassed, it will give a very false impression when such tests are performed.

Does it preserve all TCP semantics for applications, including things like urgent data, ancillary data, and TCP socket options and ioctls. If it doesn't, it shouldn't be the default, and it should be documented what features do and don't work when tcp_bypass is enabled. If all TCP semantics are unchanged, that would also be good to know and document.

And there's the already mentioned issue of breaking tcpdump and related tools.

While this could be a very useful feature in some environments, it seems to me it would be safest to have it disabled by default.

-Bill

Quoted 12 lines

Note, when friends is enabled any loopback interpose, e.g. tcpdump, will only see the TCP/IP packets during connection establishment and finish, all data bypasses the stack and instead is delivered to the destination socket directly.

Testing done on a 4 socket 2.2GHz "Quad-Core AMD Opteron(tm) Processor 8354 CPU" based system, netperf results for a single connection show increased TCP_STREAM throughput, increased TCP_RR and TCP_CRR transaction rate for most message sizes vs baseline and comparable to AF_UNIX.

Significant increase (up to 4.88x) in aggregate throughput for multiple netperf runs (STREAM 32KB I/O x N) is seen.

maintainer
#3 lore raw

From: Bill Fink <[email protected]>
Date: Tue, 14 Aug 2012 02:31:55 -0400

Quoted 3 lines

I also believe it should be disabled by default, as that is the current behavior, and those who would gain an advantage from using it can easily enable it.

It benefits basically everyone, it makes things orders of magnitude faster.

Quoted 2 lines

While this could be a very useful feature in some environments, it seems to me it would be safest to have it disabled by default.

I violently disagree, and there is no way I'm having this thing off by default.

#4 lore raw
Quoted 11 lines · David Miller

From: Bill Fink<[email protected]>
Date: Tue, 14 Aug 2012 02:31:55 -0400

Quoted 4 lines

I also believe it should be disabled by default, as that is the current behavior, and those who would gain an advantage from using it can easily enable it.

It benefits basically everyone, it makes things orders of magnitude faster.

Hmm... Looking at the stats provided I didn't see any orders of magnitude faster.

Quoted 12 lines
Quoted 4 lines

While this could be a very useful feature in some environments, it seems to me it would be safest to have it disabled by default.

I violently disagree, and there is no way I'm having this thing off by default.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 5 lines
"They that give up essential liberty to obtain temporary safety,
deserve neither liberty nor safety."  (Ben Franklin)

"The course of history shows that as a government grows, liberty
decreases."  (Thomas Jefferson)
#5 lore raw
In reply to Bill Fink

Re-sending due to vger.kernel.org rejecting HTML

Quoted 37 lines · Bill Fink
Quoted 10 lines · Bruce "Brutus" Curtis

From: "Bruce \"Brutus\" Curtis" <[email protected]>

TCP/IP loopback socket pair stack bypass, based on an idea by, and rough upstream patch from, David Miller <[email protected]> called "friends", the data structure modifcations and connection scheme are reused with extensive data-path changes.

A new sysctl, net.ipv4.tcp_friends, is added:
0: disable friends and use the stock data path.
1: enable friends and bypass the stack data path, the default.

The following is from a user perspective, since I am not intimately familiar with the internals of the TCP stack.

I think tcp_friends is a poor name from a user POV. Something like tcp_bypass would be much better.

I also believe it should be disabled by default, as that is the current behavior, and those who would gain an advantage from using it can easily enable it.

Changing the behavior would violate the principle of least surprise. Loopback TCP testing of an application or system is often a useful first step in evaluating its behavior and performance. If the TCP stack is bypassed, it will give a very false impression when such tests are performed.

Does it preserve all TCP semantics for applications, including things like urgent data, ancillary data, and TCP socket options and ioctls. If it doesn't, it shouldn't be the default, and it should be documented what features do and don't work when tcp_bypass is enabled. If all TCP semantics are unchanged, that would also be good to know and document.

Quoted 8 lines

And there's the already mentioned issue of breaking tcpdump and related tools.

While this could be a very useful feature in some environments, it seems to me it would be safest to have it disabled by default.

-Bill

1) tcp_friends vs tcp_bypass, the average user will not need to know about this tunable so if there's consensus that it needs to be changed, change it?

2) this is a throughput/latency advantage for most (all?) so it benefits most (all?) production environments

3) as for breaking tcpdump and ... Again, it does maintain the connection establishment and finish packet flow so for most TCP connection related interpose uses this should work and be documented but if your trying to debug TCP's protocol state-machine, network emulation, ... then Yes a user would need to disable but IMHO this is the exception

4) all TCP socket semantics are maintained and if not it's a bug and needs to be fixed

Quoted 13 lines
Quoted 13 lines

Note, when friends is enabled any loopback interpose, e.g. tcpdump, will only see the TCP/IP packets during connection establishment and finish, all data bypasses the stack and instead is delivered to the destination socket directly.

Testing done on a 4 socket 2.2GHz "Quad-Core AMD Opteron(tm) Processor 8354 CPU" based system, netperf results for a single connection show increased TCP_STREAM throughput, increased TCP_RR and TCP_CRR transaction
rate for most message sizes vs baseline and comparable to AF_UNIX.

Significant increase (up to 4.88x) in aggregate throughput for multiple netperf runs (STREAM 32KB I/O x N) is seen.

#6 lore raw
Quoted 41 lines · Bruce Curtis
Quoted 40 lines · Bill Fink
Quoted 10 lines · Bruce "Brutus" Curtis

From: "Bruce \"Brutus\" Curtis" <[email protected]>

TCP/IP loopback socket pair stack bypass, based on an idea by, and rough upstream patch from, David Miller <[email protected]> called "friends", the data structure modifcations and connection scheme are reused with extensive data-path changes.

A new sysctl, net.ipv4.tcp_friends, is added:
0: disable friends and use the stock data path.
1: enable friends and bypass the stack data path, the default.

The following is from a user perspective, since I am not intimately familiar with the internals of the TCP stack.

I think tcp_friends is a poor name from a user POV. Something like tcp_bypass would be much better.

I also believe it should be disabled by default, as that is the current behavior, and those who would gain an advantage from using it can easily enable it.

Changing the behavior would violate the principle of least surprise. Loopback TCP testing of an application or system is often a useful first step in evaluating its behavior and performance. If the TCP stack is bypassed, it will give a very false impression when such tests are performed.

Does it preserve all TCP semantics for applications, including things like urgent data, ancillary data, and TCP socket options and ioctls. If it doesn't, it shouldn't be the default, and it should be documented what features do and don't work when tcp_bypass is enabled. If all TCP semantics are unchanged, that would also be good to know and document.

And there's the already mentioned issue of breaking tcpdump and related tools.

Quoted 8 lines
Quoted 5 lines

While this could be a very useful feature in some environments, it seems to me it would be safest to have it disabled by default.

-Bill

1) tcp_friends vs tcp_bypass, the average user will not need to know about this tunable so if there's consensus that it needs to be changed, change it?

I see no reason to make it obtuse rather than something more descriptive of its function (as opposed to how it's implemented).

Quoted 2 lines

2) this is a throughput/latency advantage for most (all?) so it benefits most (all?) production environments

I grant that given that (4) below is true.

Quoted 9 lines

3) as for breaking tcpdump and ... Again, it does maintain the connection establishment and finish packet flow so for most TCP connection related interpose uses this should work and be documented but if your trying to debug TCP's protocol state-machine, network emulation, ... then Yes a user would need to disable but IMHO this is the exception

4) all TCP socket semantics are maintained and if not it's a bug and needs to be fixed

This was my biggest concern if it wasn't true. Since you have now verified that all TCP semantics are preserved, I now don't have a major issue with it being enabled by default, since it's easy to disable for more specialized situations.

I do have some concern that since the loopback path through the TCP stack won't be heavily exercised anymore, it may be more likely for bugs or performance degradations to creep into that code.

-Thanks

-Bill

maintainer
#7 lore raw

From: Bill Fink <[email protected]>
Date: Wed, 15 Aug 2012 01:24:28 -0400

Quoted 2 lines

I see no reason to make it obtuse rather than something more descriptive of its function (as opposed to how it's implemented).

I want to live in a world where things are allowed to have some character, and some slightly amusing names. And that's why we'll call it TCP friends, thank you very much.

Quoted 3 lines

I do have some concern that since the loopback path through the TCP stack won't be heavily exercised anymore, it may be more likely for bugs or performance degradations to creep into that code.

Are you kidding me? Most people do not use loopback TCP, they use TCP to a real external entity. TCP friends only kicks in for loopback connections.

So, like all of your other concerns, this one is meritless.

#8 lore raw
Quoted 9 lines · David Miller

From: Bill Fink <[email protected]>
Date: Wed, 15 Aug 2012 01:24:28 -0400

Quoted 2 lines

I see no reason to make it obtuse rather than something more descriptive of its function (as opposed to how it's implemented).

I want to live in a world where things are allowed to have some character, and some slightly amusing names. And that's why we'll call it TCP friends, thank you very much.

That's too bad. You seem to disdain user input in favor of personal amusement. A very poor argument in my opinion.

Quoted 9 lines
Quoted 3 lines

I do have some concern that since the loopback path through the TCP stack won't be heavily exercised anymore, it may be more likely for bugs or performance degradations to creep into that code.

Are you kidding me? Most people do not use loopback TCP, they use TCP to a real external entity. TCP friends only kicks in for loopback connections.

So, like all of your other concerns, this one is meritless.

You like kicking dead horses I guess. I already agreed that having the option enabled by default was fine after Bruce's helpful info in response to my concerns.

And using the real TCP loopback can be very useful in analyzing the functionality and performance of systems and applications as an initial baseline before testing across real networks. It can help identify system bottlenecks and scope out the outer boundaries of what's possible to achieve.

Just because most people don't use loopback TCP doesn't mean it doesn't have valid and useful purposes for some. And it's interesting that your argument here that "most people do not use loopback TCP" is in opposition to your previous argument for enabling the option by default that "it benefits basically everyone".

But this is further kicking of a dead horse. I've agreed that having the option enabled by default is fine and I will continue to disagree about the option name, but that is your call of course.

-Bill

maintainer
#9 lore raw
In reply to Bruce "Brutus" Curtis

Bruce, could you integrate (and unlike your submission, actually build and run test) Weiping's bug fixes?

Actually, I'm actually a little bit alarmed at Weiping's fixes, because it makes it look as if you didn't test things at all under net-next, as it appears that without his fixes any loopback TCP connection would OOPS the kernel.

In fact, it wouldn't even build without the sysctl_tcp_friends typo. Indeed:

net/ipv4/tcp.c: In function ‘tcp_recvmsg’:
net/ipv4/tcp.c:1935:35: error: ‘friends’ undeclared (first use in this function) net/ipv4/tcp.c:1935:35: note: each undeclared identifier is reported only once for each function it appears in

I hope I don't need to tell you how unacceptable this is.

That also means that all of your measurements in the commit message weren't even made in the context where this patch will be applied.

Also unacceptable.

All of this makes for an extremely poor quality submission, please correct these issues.

#10 lore raw
Quoted 9 lines · David Miller

Bruce, could you integrate (and unlike your submission, actually build and run test) Weiping's bug fixes?

Actually, I'm actually a little bit alarmed at Weiping's fixes, because it makes it look as if you didn't test things at all under net-next, as it appears that without his fixes any loopback TCP connection would OOPS the kernel.

??? rebase, build, boot a dev machine, run several hours of testing, take numbers from test and updated commit message (commit message test results changed from patch to patch submit because of this) so ???

Quoted 7 lines

In fact, it wouldn't even build without the sysctl_tcp_friends typo. Indeed:

net/ipv4/tcp.c: In function ‘tcp_recvmsg’:
net/ipv4/tcp.c:1935:35: error: ‘friends’ undeclared (first use in this function) net/ipv4/tcp.c:1935:35: note: each undeclared identifier is reported only once for each function it appears in

Didn't build with CONFIG_NET_DMA and wasn't caught as part of rebase/local var name change.

Quoted 7 lines

I hope I don't need to tell you how unacceptable this is.

That also means that all of your measurements in the commit message weren't even made in the context where this patch will be applied.

Also unacceptable.

I stand by my numbers!!!

Quoted 2 lines

All of this makes for an extremely poor quality submission, please correct these issues.

Looking into Weiping's bug, can't simple use lock_sock()/release_sock() due to A<>B locking issues (i.e. bi-directional socket I/O).

maintainer
#11 lore raw

From: Bruce Curtis <[email protected]>
Date: Tue, 14 Aug 2012 14:45:09 -0700

Quoted 5 lines · David Miller
Quoted 4 lines

Bruce, could you integrate (and unlike your submission, actually build and run test) Weiping's bug fixes?

Quoted 8 lines
Quoted 5 lines

Actually, I'm actually a little bit alarmed at Weiping's fixes, because it makes it look as if you didn't test things at all under net-next, as it appears that without his fixes any loopback TCP connection would OOPS the kernel.

??? rebase, build, boot a dev machine, run several hours of testing, take numbers from test and updated commit message (commit message test results changed from patch to patch submit because of this) so ???

I did it for every round of the patch series removing the entire routing cache, so these kinds of excuses are going to fall on deaf ears.

It's part of doing responsible development, and it's especially critical when you are touching core parts of the kernel everyone uses.

Quoted 10 lines
Quoted 8 lines

In fact, it wouldn't even build without the sysctl_tcp_friends typo. Indeed:

net/ipv4/tcp.c: In function ‘tcp_recvmsg’:
net/ipv4/tcp.c:1935:35: error: ‘friends’ undeclared (first use in this function) net/ipv4/tcp.c:1935:35: note: each undeclared identifier is reported only once for each function it appears in

Didn't build with CONFIG_NET_DMA and wasn't caught as part of rebase/local var name change.

'allmodconfig' builds take less than 10 minutes on current hardware, and should be part of your patch validation.

Especially because this is the first thing I'm personally going to do with your patch.

#12 lore raw
In reply to Bruce "Brutus" Curtis
Quoted 16 lines · Bruce "Brutus" Curtis

From: "Bruce \"Brutus\" Curtis" <[email protected]>

TCP/IP loopback socket pair stack bypass, based on an idea by, and rough upstream patch from, David Miller <[email protected]> called "friends", the data structure modifcations and connection scheme are reused with extensive data-path changes.

A new sysctl, net.ipv4.tcp_friends, is added:
0: disable friends and use the stock data path.
1: enable friends and bypass the stack data path, the default.

Note, when friends is enabled any loopback interpose, e.g. tcpdump, will only see the TCP/IP packets during connection establishment and finish, all data bypasses the stack and instead is delivered to the destination socket directly.

Quoted 7 lines

Testing done on a 4 socket 2.2GHz "Quad-Core AMD Opteron(tm) Processor 8354 CPU" based system, netperf results for a single connection show increased TCP_STREAM throughput, increased TCP_RR and TCP_CRR transaction rate for most message sizes vs baseline and comparable to AF_UNIX.

Significant increase (up to 4.88x) in aggregate throughput for multiple netperf runs (STREAM 32KB I/O x N) is seen.

Nice!

Just to quantify the loopback testing compat issue. I often do stuff like the following to test latency. Will that be impacted?

tc qdisc add dev lo root handle 1:0 netem delay 20msec

As for the stated tcpdump change,
I don't suppose it would be possible to dynamically disable this (for new connections at least) while lo is being dumped?

cheers,
Pádraig.

#13 lore raw
Quoted 6 lines · Pádraig Brady

Just to quantify the loopback testing compat issue. I often do stuff like the following to test latency. Will that be impacted?

tc qdisc add dev lo root handle 1:0 netem delay 20msec

Yes this will. At least for tcp traffic this wont "work".

TCP friends bypass layers, by directly queuing skbs to sockets.

-> no iptables,
no qdisc (by default there is no qdisc on lo),
no loopback stats (ifconfig lo).
some SNMP stats missing as well (netstat -s)

Quoted 4 lines

As for the stated tcpdump change,
I don't suppose it would be possible to dynamically disable this (for new connections at least) while lo is being dumped?

This could be done, but it might be better to let the admin globally enable/disable TCP friends...

#14 lore raw
Quoted 6 lines · Eric Dumazet
Quoted 4 lines · Pádraig Brady

Just to quantify the loopback testing compat issue. I often do stuff like the following to test latency. Will that be impacted?

Quoted 8 lines
Quoted 2 lines

tc qdisc add dev lo root handle 1:0 netem delay 20msec

Yes this will. At least for tcp traffic this wont "work".

TCP friends bypass layers, by directly queuing skbs to sockets.

-> no iptables,

If it amounts to that, you will have upset users rather soon.

maintainer
#15 lore raw

From: Jan Engelhardt <[email protected]>
Date: Sun, 9 Sep 2012 19:54:42 +0200 (CEST)

Quoted 11 lines · Eric Dumazet
Quoted 9 lines · Pádraig Brady
Quoted 7 lines

Just to quantify the loopback testing compat issue. I often do stuff like the following to test latency. Will that be impacted?

tc qdisc add dev lo root handle 1:0 netem delay 20msec

Quoted 7 lines
Quoted 5 lines

Yes this will. At least for tcp traffic this wont "work".

TCP friends bypass layers, by directly queuing skbs to sockets.

-> no iptables,

If it amounts to that, you will have upset users rather soon.

This is over "loopback", you're just being rediculous. %99.9999 of people simply do not care.