Skip to contentSkip to conversation

stap: Socket tap

RFC PATCH In review Tom Herbert 2 participants 4 messages Aug 7, 2017 – Aug 10, 2017

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

cover stap: Socket tap

This patch set introduces generic socket tap support. This allows a means to run BPF programs or implement other functionality in both the send and receive path of a socket.

The socket tap uses the ULP mechanism recently introduced for kTLS. The data operations (sendmsg, recvmsg, sendpage, and splice_recv) are intercepted. In both directions strparser is used to break the stream into discrete application layer messages. Each message is then run through a BPF verdict program that indicates the message is okay, should be dropped, or should be dropped and the connection terminated.

The data path for socket tap for TCP is illustrated below:

  +---------------------+
  |  Application        |
  |                     |
  |  sendmsg  recvmsg   |
  +---------------------+
        |         ^
        |         |
--------|---------|---------------------
        |         |
        |    +-------------+    +--------------+    +----------------+
        |    | Socket tap  |<---| Verdict prog |<---| strparser      |
	|    | recvmsg	   |	|	       |    | TCP data ready |
        |    +-------------+    +--------------+    +----------------+
        |						    ^
	|						    |
	|						TCP receive queue
        V
  +-------------+   +-----------+   +--------------+   +---------------+
  | Socket tap  |-->| strparser |-->| Verdict prog |-->| skb_send_sock |
  | sendmsg     |   | send mode |   |		   |   | locked	       |
  +-------------+   +-----------+   +--------------+   +---------------+
							     |

V
TCP write queue

Interface:

A socket tap is enabled on socket using SO_ULP socket option with ulp type "stap". The socket option takes ULP specific configuration in the stap_params structure. The parameters consist of four file descriptors for BPF programs. These are for the parser (strparser) in the send path, the verdict program for send path, the parser and verdict programs in the receive path.

Example configuration to set a socket tap. In this case the same parse and verdict programs are used on send and receive sides of the socket.:

struct {
	struct ulp_config ulpc;
	struct stap_params sp;
} my_config;

load_bpf_file("parse_kern.o");
load_bpf_file("verdict_kern.o");

my_config.ulpc.ulp_name = "stap";
my_config.sp.bpf_send_parse_fd = prog_fd[0];
my_config.sp.bpf_send_verdict_fd = prog_fd[1];
my_config.sp.bpf_recv_parse_fd = prog_fd[0];
my_config.sp.bpf_recv_verdict_fd = prog_fd[1];

setsockopt(fd, SOL_SOCKET, SO_ULP, &my_config, sizeof(myconfig)

Future work:

- Fill in all the expected semantics. A goal of socket tap is transparency with applications.
- Performance evaluation.
- Add a mechanism to allow an admin process to tap other users' sockets.
- Add userpsace tap that can diverted applciation messages through userpsace. I'm thinking to connect tapped sockets to KCM to provide for interface.
- Integrate with kTLS.
- SUpport for BFP_REDIRECT. This would be useful to redirect messages to different sockets like in John Fasatabend's socket redirect.

Tom Herbert (2):
bpf: Add a BPF return code to disconnect a connection
stap: Socket tap

 include/net/stap.h        |  43 +++
 include/uapi/linux/bpf.h  |   1 +
 include/uapi/linux/stap.h |  21 ++
 net/Kconfig               |   1 +
 net/Makefile              |   1 +
 net/stap/Kconfig          |   8 +
 net/stap/Makefile         |   3 +
 net/stap/stap_main.c      | 769 ++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 847 insertions(+)
 create mode 100644 include/net/stap.h
 create mode 100644 include/uapi/linux/stap.h
 create mode 100644 net/stap/Kconfig
 create mode 100644 net/stap/Makefile
 create mode 100644 net/stap/stap_main.c
Signature · 1 line
2.11.0
#1 lore raw

RFC PATCH 1/2 bpf: Add a BPF return code to disconnect a connection

When using BPF program against a flow a possible verdict is that the packet should not only be dropped, but that the flow the packet was received on should be terminated.

---
 include/uapi/linux/bpf.h | 1 +
 1 file changed, 1 insertion(+)
include/uapi/linux/bpf.h +1 -0
index 1d06be1569b1..324e886c3490 100644
@@ -708,6 +708,7 @@ enum bpf_ret_code {
 	BPF_DROP = 2,
 	/* 3-6 reserved */
 	BPF_REDIRECT = 7,
+	BPF_DISCONNECT = 8,
 	/* >127 are reserved for prog type specific return codes */
 };
 
Signature · 1 line
2.11.0
#2 lore raw

RFC PATCH 2/2 stap: Socket tap

In reply to Tom Herbert

The socket tap uses ULP mechanism to insert funtionality between the socket system calls and the connection itself. Basically, this is a means to tap into a socket. To implement the tap the data operations (sendmsg, recvmsg, sendpage, and read_sock) are intercepted using the ULP infrastructure. In each direction a strparser instance is used to deliniate a stream into application messages. A BPF "verdict" program is run on the output (applicaiton messages) each strparser. The verdict program can allow the message, drop it, or drop it and also kill the connection.

The sendmsg path looks something like:

sendmsg (ULP) -> strparser -> verditct -> send_sock_skb

The receive path looks like:

tcp_read_sock -> strparser -> verdict -> recvmsg

Note the socket tap does not introduce any new locks or queuing (except for messages under construction by strparser). Also, the buffer limits of the socket are respectd in all operations.

---
 include/net/stap.h        |  43 +++
 include/uapi/linux/stap.h |  21 ++
 net/Kconfig               |   1 +
 net/Makefile              |   1 +
 net/stap/Kconfig          |   8 +
 net/stap/Makefile         |   3 +
 net/stap/stap_main.c      | 769 ++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 846 insertions(+)
 create mode 100644 include/net/stap.h
 create mode 100644 include/uapi/linux/stap.h
 create mode 100644 net/stap/Kconfig
 create mode 100644 net/stap/Makefile
 create mode 100644 net/stap/stap_main.c
include/net/stap.h new file +43 -0
new file mode 100644
index 000000000000..dfc96a116db2
@@ -0,0 +1,43 @@
+/*
+ * Socket tap
+ *
+ * Copyright (c) 2017 Tom Herbert <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ */
+
+#ifndef __NET_STAP_H_
+#define __NET_STAP_H_
+
+#include <linux/skbuff.h>
+#include <net/sock.h>
+#include <net/strparser.h>
+
+struct stap_bops {
+	struct strparser strp;
+	struct bpf_prog *parse_prog;
+	struct bpf_prog *verdict_prog;
+};
+
+struct stap_sock {
+	struct sock *sk; /* Associated socket */
+
+	const struct proto_ops *orig_ops;
+
+	void (*save_data_ready)(struct sock *sk);
+	void (*save_write_space)(struct sock *sk);
+	void (*save_state_change)(struct sock *sk);
+
+	/* Send items */
+	struct stap_bops send_bops;
+	struct sk_buff_head build_list;
+	struct sk_buff_head ready_list;
+
+	/* Receive items */
+	struct stap_bops recv_bops;
+	struct sk_buff *recv_skb;
+};
+
+#endif /* __NET_STAP_H_ */
include/uapi/linux/stap.h new file +21 -0
new file mode 100644
index 000000000000..fa8545628fd2
@@ -0,0 +1,21 @@
+/*
+ * Socket tap
+ *
+ * Copyright (c) 2017 Tom Herbert <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ */
+
+#ifndef _UAPI_LINUX_STAP_H
+#define _UAPI_LINUX_STAP_H
+
+struct stap_params {
+	int bpf_send_parse_fd;
+	int bpf_send_verdict_fd;
+	int bpf_recv_parse_fd;
+	int bpf_recv_verdict_fd;
+};
+
+#endif /* _UAPI_LINUX_STAP_H */
net/Kconfig +1 -0
index 2b8d2d88bc2b..8a1bdd269e84 100644
@@ -368,6 +368,7 @@ source "net/irda/Kconfig"
 source "net/bluetooth/Kconfig"
 source "net/rxrpc/Kconfig"
 source "net/kcm/Kconfig"
+source "net/stap/Kconfig"
 source "net/strparser/Kconfig"
 
 config FIB_RULES
net/Makefile +1 -0
index bed80fa398b7..3ef1d8ae8e58 100644
@@ -36,6 +36,7 @@ obj-$(CONFIG_BT)		+= bluetooth/
 obj-$(CONFIG_SUNRPC)		+= sunrpc/
 obj-$(CONFIG_AF_RXRPC)		+= rxrpc/
 obj-$(CONFIG_AF_KCM)		+= kcm/
+obj-$(CONFIG_STAP)		+= stap/
 obj-$(CONFIG_STREAM_PARSER)	+= strparser/
 obj-$(CONFIG_ATM)		+= atm/
 obj-$(CONFIG_L2TP)		+= l2tp/
net/stap/Kconfig new file +8 -0
new file mode 100644
index 000000000000..adb3149ea624
@@ -0,0 +1,8 @@
+config STAP
+	tristate "Socket tap"
+	depends on INET
+	select BPF_SYSCALL
+	select STREAM_PARSER
+	---help---
+	  Socket tap.
+
net/stap/Makefile new file +3 -0
new file mode 100644
index 000000000000..f5bce0f59da5
@@ -0,0 +1,3 @@
+obj-$(CONFIG_STAP) += stap.o
+
+stap-y := stap_main.o
net/stap/stap_main.c new file +769 -0

769 lines not on this page · raw message

Signature · 1 line
2.11.0
#3 lore raw
In reply to Tom Herbert
Quoted 2 lines · Tom Herbert

A socket tap is enabled on socket using SO_ULP socket option with ulp type "stap". The socket option takes ULP specific configuration

How about sktap instead of stap since stap has a history of referring to system tap?