Skip to contentSkip to conversation

strp: Generalize stream parser to work with other socket types

PATCH In review Tom Herbert 2 participants 6 messages Aug 28, 2016 – Aug 29, 2016

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

cover strp: Generalize stream parser to work with other socket types

Add a read_sock protocol operation function that allows something like tcp_read_sock to be called for other protocol types.

Specific changes in this patch set:
- Add read_sock function to proto_ops. This has the same signature as tcp_read_sock. sk_read_actor_t is also defined in net.h.
- Set peek_len and read_sock proto_op functions for TCPv4 and TCPv6 stream ops.
- Remove references to tcp in strparser.
- Call peek_len and read_sock operations from strparser instead of calling TCP specific functions.

Tom Herbert (3):
net: Add read_sock proto_op
tcp: Set read_sock and peek_len proto_ops
kcm: Remove TCP specific references from kcm and strparser

 include/linux/net.h       |  6 ++++++
 include/net/strparser.h   |  2 +-
 include/net/tcp.h         |  4 ++--
 net/ipv4/af_inet.c        |  2 ++
 net/ipv4/tcp.c            |  6 ++++++
 net/ipv6/af_inet6.c       |  2 ++
 net/kcm/kcmsock.c         | 30 +++++++++++++----------------
 net/strparser/strparser.c | 48 ++++++++++++++++++++++++++++-------------------
 8 files changed, 61 insertions(+), 39 deletions(-)
Signature · 1 line
2.8.0.rc2
#1 lore raw

PATCH 1/3 net: Add read_sock proto_op

Add new function in proto_ops structure. This includes moving the typedef got sk_read_actor into net.h and removing the definition from tcp.h.

---
 include/linux/net.h | 6 ++++++
 include/net/tcp.h   | 2 --
 2 files changed, 6 insertions(+), 2 deletions(-)
include/linux/net.h +6 -0
index b9f0ff4..cd0c8bd 100644
@@ -25,6 +25,7 @@
 #include <linux/kmemcheck.h>
 #include <linux/rcupdate.h>
 #include <linux/once.h>
+#include <linux/fs.h>
 
 #include <uapi/linux/net.h>
 
@@ -128,6 +129,9 @@ struct page;
 struct sockaddr;
 struct msghdr;
 struct module;
+struct sk_buff;
+typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
+			       unsigned int, size_t);
 
 struct proto_ops {
 	int		family;
@@ -186,6 +190,8 @@ struct proto_ops {
 				       struct pipe_inode_info *pipe, size_t len, unsigned int flags);
 	int		(*set_peek_off)(struct sock *sk, int val);
 	int		(*peek_len)(struct socket *sock);
+	int		(*read_sock)(struct sock *sk, read_descriptor_t *desc,
+				     sk_read_actor_t recv_actor);
 };
 
 #define DECLARE_SOCKADDR(type, dst, src)	\
include/net/tcp.h +0 -2
index 25d64f6..d56666a 100644
@@ -603,8 +603,6 @@ static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
 void tcp_get_info(struct sock *, struct tcp_info *);
 
 /* Read 'sendfile()'-style from a TCP socket */
-typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
-				unsigned int, size_t);
 int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
 		  sk_read_actor_t recv_actor);
 
Signature · 1 line
2.8.0.rc2
#2 lore raw

PATCH 2/3 tcp: Set read_sock and peek_len proto_ops

In reply to Tom Herbert

In inet_stream_ops we set read_sock to tcp_read_sock and peek_len to tcp_peek_len (which is just a stub function that calls tcp_inq).

---
 include/net/tcp.h   | 2 ++
 net/ipv4/af_inet.c  | 2 ++
 net/ipv4/tcp.c      | 6 ++++++
 net/ipv6/af_inet6.c | 2 ++
 4 files changed, 12 insertions(+)
include/net/tcp.h +2 -0
index d56666a..a5af6be 100644
@@ -1848,6 +1848,8 @@ static inline int tcp_inq(struct sock *sk)
 	return answ;
 }
 
+int tcp_peek_len(struct socket *sock);
+
 static inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb)
 {
 	u16 segs_in;
net/ipv4/af_inet.c +2 -0
index 989a362..e94b47b 100644
@@ -916,6 +916,8 @@ const struct proto_ops inet_stream_ops = {
 	.mmap		   = sock_no_mmap,
 	.sendpage	   = inet_sendpage,
 	.splice_read	   = tcp_splice_read,
+	.read_sock	   = tcp_read_sock,
+	.peek_len	   = tcp_peek_len,
 #ifdef CONFIG_COMPAT
 	.compat_setsockopt = compat_sock_common_setsockopt,
 	.compat_getsockopt = compat_sock_common_getsockopt,
net/ipv4/tcp.c +6 -0
index f1a9a0a..60a4388 100644
@@ -1570,6 +1570,12 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
 }
 EXPORT_SYMBOL(tcp_read_sock);
 
+int tcp_peek_len(struct socket *sock)
+{
+	return tcp_inq(sock->sk);
+}
+EXPORT_SYMBOL(tcp_peek_len);
+
 /*
  *	This routine copies from a sock struct into the user buffer.
  *
net/ipv6/af_inet6.c +2 -0
index b454055..46ad699 100644
@@ -545,6 +545,8 @@ const struct proto_ops inet6_stream_ops = {
 	.mmap		   = sock_no_mmap,
 	.sendpage	   = inet_sendpage,
 	.splice_read	   = tcp_splice_read,
+	.read_sock	   = tcp_read_sock,
+	.peek_len	   = tcp_peek_len,
 #ifdef CONFIG_COMPAT
 	.compat_setsockopt = compat_sock_common_setsockopt,
 	.compat_getsockopt = compat_sock_common_getsockopt,
Signature · 1 line
2.8.0.rc2
#3 lore raw

PATCH 3/3 kcm: Remove TCP specific references from kcm and strparser

In reply to Tom Herbert

kcm and strparser need to work with any type of stream socket not just TCP. Eliminate references to TCP and call generic proto_ops functions of read_sock and peek_len. Also in strp_init check if the socket support the proto_ops read_sock and peek_len.

---
 include/net/strparser.h   |  2 +-
 net/kcm/kcmsock.c         | 30 +++++++++++++----------------
 net/strparser/strparser.c | 48 ++++++++++++++++++++++++++++-------------------
 3 files changed, 43 insertions(+), 37 deletions(-)
include/net/strparser.h +1 -1
index 91fa0b9..0c28ad9 100644
@@ -137,6 +137,6 @@ void strp_stop(struct strparser *strp);
 void strp_check_rcv(struct strparser *strp);
 int strp_init(struct strparser *strp, struct sock *csk,
 	      struct strp_callbacks *cb);
-void strp_tcp_data_ready(struct strparser *strp);
+void strp_data_ready(struct strparser *strp);
 
 #endif /* __NET_STRPARSER_H_ */
net/kcm/kcmsock.c +13 -17
index eb731ca..2632ac7 100644
@@ -26,7 +26,6 @@
 #include <net/kcm.h>
 #include <net/netns/generic.h>
 #include <net/sock.h>
-#include <net/tcp.h>
 #include <uapi/linux/kcm.h>
 
 unsigned int kcm_net_id;
@@ -340,7 +339,7 @@ static void unreserve_rx_kcm(struct kcm_psock *psock,
 }
 
 /* Lower sock lock held */
-static void psock_tcp_data_ready(struct sock *sk)
+static void psock_data_ready(struct sock *sk)
 {
 	struct kcm_psock *psock;
 
@@ -348,7 +347,7 @@ static void psock_tcp_data_ready(struct sock *sk)
 
 	psock = (struct kcm_psock *)sk->sk_user_data;
 	if (likely(psock))
-		strp_tcp_data_ready(&psock->strp);
+		strp_data_ready(&psock->strp);
 
 	read_unlock_bh(&sk->sk_callback_lock);
 }
@@ -392,7 +391,7 @@ static int kcm_read_sock_done(struct strparser *strp, int err)
 	return err;
 }
 
-static void psock_tcp_state_change(struct sock *sk)
+static void psock_state_change(struct sock *sk)
 {
 	/* TCP only does a POLLIN for a half close. Do a POLLHUP here
 	 * since application will normally not poll with POLLIN
@@ -402,7 +401,7 @@ static void psock_tcp_state_change(struct sock *sk)
 	report_csk_error(sk, EPIPE);
 }
 
-static void psock_tcp_write_space(struct sock *sk)
+static void psock_write_space(struct sock *sk)
 {
 	struct kcm_psock *psock;
 	struct kcm_mux *mux;
@@ -1383,19 +1382,12 @@ static int kcm_attach(struct socket *sock, struct socket *csock,
 	struct list_head *head;
 	int index = 0;
 	struct strp_callbacks cb;
-
-	if (csock->ops->family != PF_INET &&
-	    csock->ops->family != PF_INET6)
-		return -EINVAL;
+	int err;
 
 	csk = csock->sk;
 	if (!csk)
 		return -EINVAL;
 
-	/* Only support TCP for now */
-	if (csk->sk_protocol != IPPROTO_TCP)
-		return -EINVAL;
-
 	psock = kmem_cache_zalloc(kcm_psockp, GFP_KERNEL);
 	if (!psock)
 		return -ENOMEM;
@@ -1409,7 +1401,11 @@ static int kcm_attach(struct socket *sock, struct socket *csock,
 	cb.parse_msg = kcm_parse_func_strparser;
 	cb.read_sock_done = kcm_read_sock_done;
 
-	strp_init(&psock->strp, csk, &cb);
+	err = strp_init(&psock->strp, csk, &cb);
+	if (err) {
+		kmem_cache_free(kcm_psockp, psock);
+		return err;
+	}
 
 	sock_hold(csk);
 
@@ -1418,9 +1414,9 @@ static int kcm_attach(struct socket *sock, struct socket *csock,
 	psock->save_write_space = csk->sk_write_space;
 	psock->save_state_change = csk->sk_state_change;
 	csk->sk_user_data = psock;
-	csk->sk_data_ready = psock_tcp_data_ready;
-	csk->sk_write_space = psock_tcp_write_space;
-	csk->sk_state_change = psock_tcp_state_change;
+	csk->sk_data_ready = psock_data_ready;
+	csk->sk_write_space = psock_write_space;
+	csk->sk_state_change = psock_state_change;
 	write_unlock_bh(&csk->sk_callback_lock);
 
 	/* Finished initialization, now add the psock to the MUX. */
net/strparser/strparser.c +29 -19

137 lines not on this page · raw message

Signature · 1 line
2.8.0.rc2
maintainer
#4 lore raw
In reply to Tom Herbert

From: Tom Herbert <[email protected]>
Date: Sun, 28 Aug 2016 14:43:16 -0700

Quoted 11 lines

Add a read_sock protocol operation function that allows something like tcp_read_sock to be called for other protocol types.

Specific changes in this patch set:
- Add read_sock function to proto_ops. This has the same signature as tcp_read_sock. sk_read_actor_t is also defined in net.h.
- Set peek_len and read_sock proto_op functions for TCPv4 and TCPv6 stream ops.
- Remove references to tcp in strparser.
- Call peek_len and read_sock operations from strparser instead of calling TCP specific functions.

I'll apply this, but I want you to shore up these new ops.

A check has to happen somewhere to make sure the proto_ops in question have a non-NULL read_sock and peek_len method before starting to use it.

#5 lore raw
Quoted 15 lines · David Miller

From: Tom Herbert <[email protected]>
Date: Sun, 28 Aug 2016 14:43:16 -0700

Quoted 11 lines

Add a read_sock protocol operation function that allows something like tcp_read_sock to be called for other protocol types.

Specific changes in this patch set:
- Add read_sock function to proto_ops. This has the same signature as tcp_read_sock. sk_read_actor_t is also defined in net.h.
- Set peek_len and read_sock proto_op functions for TCPv4 and TCPv6 stream ops.
- Remove references to tcp in strparser.
- Call peek_len and read_sock operations from strparser instead of calling TCP specific functions.

Quoted 5 lines

I'll apply this, but I want you to shore up these new ops.

A check has to happen somewhere to make sure the proto_ops in question have a non-NULL read_sock and peek_len method before starting to use it.

Is the check in strp_init not enough?...

@@ -424,9 +429,14 @@ static void strp_rx_msg_timeout(unsigned long arg)
int strp_init(struct strparser *strp, struct sock *csk,
struct strp_callbacks *cb)
{
+ struct socket *sock = csk->sk_socket;
+

        if (!cb || !cb->rcv_msg || !cb->parse_msg)
                return -EINVAL;

+ if (!sock->ops->read_sock || !sock->ops->peek_len)
+ return -EAFNOSUPPORT;
+