Skip to contentSkip to conversation

kcm: Kernel Connection Multiplexor (KCM)

RFC PATCH In review Tom Herbert 5 participants 15 messages Sep 20, 2015 – Sep 23, 2015

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

cover kcm: Kernel Connection Multiplexor (KCM)

Kernel Connection Multiplexor (KCM) is a facility that provides a message based interface over TCP for generic application protocols. The motivation for this is based on the observation that although TCP is byte stream transport protocol with no concept of message boundaries, a common use case is to implement a framed application layer protocol running over TCP. To date, most TCP stacks offer byte stream API for applications, which places the burden of message delineation, message I/O operation atomicity, and load balancing in the application. With KCM an application can efficiently send and receive application protocol messages over TCP using a datagram interface.

In order to delineate message in a TCP stream for receive in KCM, the kernel implements a message parser. For this we chose to employ BPF which is applied to the TCP stream. BPF code parses application layer messages and returns a message length. Nearly all binary application protocols are parsable in this manner, so KCM should be applicable across a wide range of applications. Other than message length determination in receive, KCM does not require any other application specific awareness. KCM does not implement any other application protocol semantics-- these are are provided in userspace or could be implemented in a kernel module layered above KCM.

KCM implements an NxM multiplexor in the kernel as diagrammed below:

+------------+   +------------+   +------------+   +------------+
| KCM socket |   | KCM socket |   | KCM socket |   | KCM socket |
+------------+   +------------+   +------------+   +------------+
      |                 |               |                |
      +-----------+     |               |     +----------+
                  |     |               |     |
               +----------------------------------+
               |           Multiplexor            |
               +----------------------------------+
                 |   |           |           |  |
       +---------+   |           |           |  ------------+
       |             |           |           |              |
+----------+  +----------+  +----------+  +----------+ +----------+
|  Psock   |  |  Psock   |  |  Psock   |  |  Psock   | |  Psock   |
+----------+  +----------+  +----------+  +----------+ +----------+
      |              |           |            |             |
+----------+  +----------+  +----------+  +----------+ +----------+
| TCP sock |  | TCP sock |  | TCP sock |  | TCP sock | | TCP sock |
+----------+  +----------+  +----------+  +----------+ +----------+

The KCM sockets provide the datagram interface to applications, Psocks are the state for each attached TCP connection (i.e. where message delineation is performed on receive).

A description of the APIs and design can be found in the included Documentation/networking/kcm.txt.

Testing:

For testing I have been developing kcmperf and super_kcmperf which should allow functional verification and some baseline comparisons with netperf using TCP. For the test results listed below, one instance of kcmperf is run which creates a trivial MUX with one KCM socket and one attached TCP connection.

netperf TCP_RR
- 1 instance, 1 byte RR size

     34219 tps

- 1 instance, 1000000 byte RR size

     464 tps

- 200 instances, 1 byte RR size

     1721552 tps
     86.86% CPU utilization

- 200 instances, 1000000 byte RR size
1165
7.16% CPU utilization

kcmperf
- 1 instance, byte RR size

     32679 tps

- 1 instance, byte RR size

     412 tps

- 200 instances, 1 byte RR size

     1420454 tps
     80.02% CPU utilization 

- 200 instances, 1000000 byte RR size

     1130 tps
     10.08% CPU utilization

Future support:

The implementation provided here should be thought of as a first cut, for which the goal is to establish a robust base implementation. There are many avenues for extending this basic implementation and improving upon this:

- Sample application support
- SOCK_SEQPACKET support
- Integration with TLS (TLS-in-kernel is a separate intiative).
- Page operations/splice support
- sendmmsg, recvmmsg support
- Unconnected KCM sockets. Will be able to attach sockets to different destinations, AF_KCM addresses with be used in sendmsg and recvmsg to indicate destination
- Explore more utility in performing BPF inline with a TCP data stream (setting SO_MARK, rxhash for messages being sent received on KCM sockets).
- Performance work
- Reduce locking (MUX lock is currently a bottleneck).
- KCM socket to TCP socket affinity
- Small message coalescing, direct calls to TCP send functions

Tom Herbert (3):
rcu: Add list_next_or_null_rcu
kcm: Kernel Connection Multiplexor module
kcm: Add statistics and proc interfaces

 Documentation/networking/kcm.txt |  173 ++++
 include/linux/rculist.h          |   21 +
 include/linux/socket.h           |    6 +-
 include/net/kcm.h                |  211 +++++
 include/uapi/linux/errqueue.h    |    1 +
 include/uapi/linux/kcm.h         |   26 +
 net/Kconfig                      |    1 +
 net/Makefile                     |    1 +
 net/kcm/Kconfig                  |   10 +
 net/kcm/Makefile                 |    3 +
 net/kcm/kcmproc.c                |  415 ++++++++++
 net/kcm/kcmsock.c                | 1629 ++++++++++++++++++++++++++++++++++++++
 12 files changed, 2496 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/networking/kcm.txt
 create mode 100644 include/net/kcm.h
 create mode 100644 include/uapi/linux/kcm.h
 create mode 100644 net/kcm/Kconfig
 create mode 100644 net/kcm/Makefile
 create mode 100644 net/kcm/kcmproc.c
 create mode 100644 net/kcm/kcmsock.c
Signature · 1 line
1.8.1
#1 lore raw

RFC PATCH 1/3 rcu: Add list_next_or_null_rcu

This is a convenienve function that returns the next enrty in abn RCU list or NULL if at the end of the list.

---
 include/linux/rculist.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
include/linux/rculist.h +21 -0
index 17c6b1f..3e8e2aa 100644
@@ -293,6 +293,27 @@ static inline void list_splice_init_rcu(struct list_head *list,
 })
 
 /**
+ * list_next_or_null_rcu - get the first element from a list
+ * @head:	the head for the list.
+ * @ptr:        the list head to take the next element from.
+ * @type:       the type of the struct this is embedded in.
+ * @member:     the name of the list_head within the struct.
+ *
+ * Note that if the ptr is at the end of the list, NULL is returned.
+ *
+ * This primitive may safely run concurrently with the _rcu list-mutation
+ * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
+ */
+#define list_next_or_null_rcu(head, ptr, type, member) \
+({ \
+	struct list_head *__head = (head); \
+	struct list_head *__ptr = (ptr); \
+	struct list_head *__next = READ_ONCE(__ptr->next); \
+	likely(__next != __head) ? list_entry_rcu(__next, type, \
+						  member) : NULL; \
+})
+
+/**
  * list_for_each_entry_rcu	-	iterate over rcu list of given type
  * @pos:	the type * to use as a loop cursor.
  * @head:	the head for your list.
Signature · 1 line
1.8.1
#2 lore raw

RFC PATCH 2/3 kcm: Kernel Connection Multiplexor module

In reply to Tom Herbert

This module implement the Kernel Connection Multiplexor.

Kernel Connection Multiplexor (KCM) is a facility that provides a message based interface over TCP for generic application protocols. With KCM an application can efficiently send and receive application protocol messages over TCP using datagram sockets.

For more information see the included Documentation/networking/kcm.txt

---
 Documentation/networking/kcm.txt |  173 +++++
 include/linux/socket.h           |    6 +-
 include/net/kcm.h                |  109 +++
 include/uapi/linux/errqueue.h    |    1 +
 include/uapi/linux/kcm.h         |   26 +
 net/Kconfig                      |    1 +
 net/Makefile                     |    1 +
 net/kcm/Kconfig                  |   10 +
 net/kcm/Makefile                 |    3 +
 net/kcm/kcmsock.c                | 1566 ++++++++++++++++++++++++++++++++++++++
 10 files changed, 1895 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/networking/kcm.txt
 create mode 100644 include/net/kcm.h
 create mode 100644 include/uapi/linux/kcm.h
 create mode 100644 net/kcm/Kconfig
 create mode 100644 net/kcm/Makefile
 create mode 100644 net/kcm/kcmsock.c
Documentation/networking/kcm.txt new file +173 -0
new file mode 100644
index 0000000..2e0968d
@@ -0,0 +1,173 @@
+Kernel Connection Mulitplexor
+-----------------------------
+
+Kernel Connection Multiplexor (KCM) is a mechanism that provides a message based
+interface over TCP for generic application protocols. With KCM an application
+can efficiently send and receive application protocol messages over TCP using
+datagram sockets.
+
+KCM implements an NxM multiplexor in the kernel as diagrammed below:
+
++------------+   +------------+   +------------+   +------------+
+| KCM socket |   | KCM socket |   | KCM socket |   | KCM socket |
++------------+   +------------+   +------------+   +------------+
+      |                 |               |                |
+      +-----------+     |               |     +----------+
+                  |     |               |     |
+               +----------------------------------+
+               |           Multiplexor            |
+               +----------------------------------+
+                 |   |           |           |  |
+       +---------+   |           |           |  ------------+
+       |             |           |           |              |
++----------+  +----------+  +----------+  +----------+ +----------+
+|  Psock   |  |  Psock   |  |  Psock   |  |  Psock   | |  Psock   |
++----------+  +----------+  +----------+  +----------+ +----------+
+      |              |           |            |             |
++----------+  +----------+  +----------+  +----------+ +----------+
+| TCP sock |  | TCP sock |  | TCP sock |  | TCP sock | | TCP sock |
++----------+  +----------+  +----------+  +----------+ +----------+
+
+KCM sockets
+-----------
+
+The KCM sockets provide the user interface to the muliplexor. All the KCM sockets
+bound to a multiplexor are considered to have equivalent function, and I/O
+operations in different sockets may be done in parallel without the need for
+synchronization between threads in userspace.
+
+Multiplexor
+-----------
+
+The multiplexor provides the message steering. In the transmit path, messages
+written on a KCM socket are sent atomically on an appropriate TCP socket.
+Similarly, in the receive path, messages are constructed on each TCP socket
+(Psock) and complete messages are steered to a KCM socket which is blocking in
+receive or poll.
+
+TCP sockets & Psocks
+--------------------
+
+TCP sockets may be bound to a KCM multiplexor. A Psock structure is allocacted
+for each bound TCP socket, this structure holds the state for constructing
+messages on receive as well as other connection specific information for KCM.
+
+Connected mode semantics
+------------------------
+
+Each multiplexor assumes that all attached TCP connections are to the same
+destination and can use the different connections for load balancing when
+transmitting. The normal send and recv calls can be used to send and receive
+messages from the KCM socket.
+
+SOCK_DGRAM
+----------
+
+KCM currently support socket type SOCK_DGRAM only.
+
+Message delineation
+-------------------
+
+Messages are sent over a TCP stream with some application protocol message
+format that typically includes a header which encapsulates messages. The length
+of a received message can be deduced from the application protocol header
+(often just a simple length field).
+
+A TCP stream must be parsed to determine message boundaries. Berkeley Packet
+Filter (BPF) is used for this. When attaching a TCP socket to a multiplexor a
+BPF program must be specified. The program is called at the start of receiving
+a new message and is given an skbuff that contains the bytes received so far.
+It parses the message header and returns the length of the message. Given this
+information, KCM will construct the message of the stated length and deliver it
+to a KCM socket.
+
+TCP socket management
+---------------------
+
+When a TCP socket is attached to a KCM multiplexor data ready (POLLIN) and
+write space available (POLLOUT) events are handled by the multiplexor. If there
+is a state change (disconnection) or other error on a TCP socket, an error is
+posted on the socket so that an POLLERR event happens and KCM discontinues using
+the socket. In the case of an error affecting the receive side of the
+connection, any partial message under construction will also be set on the error
+queue for the TCP socket. When the application gets the error notification for a
+TCP socket it should unattach the socket from KCM and then handle the error
+condition (the typical response is to close the socket and create a new
+connection if necessary).
+
+User interface
+==============
+
+Creating a multiplexor
+----------------------
+
+A new multiplexor and initial KCM socket is created by a socket call:
+
+  socket(AF_KCM, type, protocol)
+
+  - type is either SOCK_DGRAM
+  - protocols is either KCMPROTO_CONNECTED
+
+Cloning KCM sockets
+-------------------
+
+After the first KCM socket is created using the socket call as described
+above, additional sockets for the multiplexor can be created by cloning
+a KCM socket. This is accomplished by calling accept on the KCM socket:
+
+   newkcmfd = accept(kcmfd, NULL, 0)
+
+Attach transport sockets
+------------------------
+
+Attaching of transport sockets to a multiplexor is performed by calling on
+ioctl on a KCM socket for the multiplexor. e.g.:
+
+  /* From linux/kcm.h */
+  struct kcm_attach {
+        int fd;
+        int bpf_type;
+        union {
+                int bpf_fd;
+                struct sock_fprog fprog;
+        };
+  };
+
+  struct kcm_attach info;
+
+  memset(&info, 0, sizeof(info));
+
+  info.fd = tcpfd;
+  info.bpf_type = KCM_BPF_TYPE_PROG;
+  info.bpf_fprog = bpf_prog;
+
+  ioctl(kcmfd, SIOCKCMATTACH, &info);
+
+The kcm_attach structure contains:
+  fd: file descriptor for TCP socket being attached
+  bpf_type: type of BPF program to be loaded this is either:
+    KCM_BPF_TYPE_PROG: program load directly for user space
+    KCM_BPF_TYPE_FD: Complied rogram to be load for the specified file
+                     descriptor (see BPF LLVM and Clang)
+  bpf_fprog: contains pointer to user space protocol to load
+  bpf_fd: file descriptor for compiled program download
+
+Unattach transport sockets
+--------------------------
+
+Unattaching a transport socket from a multiplexor is straightforward. An
+"unattach" ioctl is done with the kcm_unattach structure as the argument:
+
+  /* From linux/kcm.h /
+  struct kcm_unattach {
+        int fd;
+  };
+
+  struct kcm_unattach info;
+
+  memset(&info, 0, sizeof(info));
+
+  info.fd = cfd;
+
+  ioctl(fd, SIOCKCMUNATTACH, &info);
+
include/linux/socket.h +5 -1
index 5bf59c8..4e1ea53 100644
@@ -200,7 +200,9 @@ struct ucred {
 #define AF_ALG		38	/* Algorithm sockets		*/
 #define AF_NFC		39	/* NFC sockets			*/
 #define AF_VSOCK	40	/* vSockets			*/
-#define AF_MAX		41	/* For now.. */
+#define AF_KCM		41	/* Kernel Connection Multiplexor*/
+
+#define AF_MAX		42	/* For now.. */
 
 /* Protocol families, same as address families. */
 #define PF_UNSPEC	AF_UNSPEC
@@ -246,6 +248,7 @@ struct ucred {
 #define PF_ALG		AF_ALG
 #define PF_NFC		AF_NFC
 #define PF_VSOCK	AF_VSOCK
+#define PF_KCM		AF_KCM
 #define PF_MAX		AF_MAX
 
 /* Maximum queue length specifiable by listen.  */
@@ -322,6 +325,7 @@ struct ucred {
 #define SOL_CAIF	278
 #define SOL_ALG		279
 #define SOL_NFC		280
+#define SOL_KCM		281
 
 /* IPX options */
 #define IPX_TYPE	1
include/net/kcm.h new file +109 -0

109 lines not on this page · raw message

include/uapi/linux/errqueue.h +1 -0
index 07bdce1..6a8e2f2 100644
@@ -18,6 +18,7 @@ struct sock_extended_err {
 #define SO_EE_ORIGIN_ICMP	2
 #define SO_EE_ORIGIN_ICMP6	3
 #define SO_EE_ORIGIN_TXSTATUS	4
+#define SO_EE_ORIGIN_KCM	5
 #define SO_EE_ORIGIN_TIMESTAMPING SO_EE_ORIGIN_TXSTATUS
 
 #define SO_EE_OFFENDER(ee)	((struct sockaddr*)((ee)+1))
include/uapi/linux/kcm.h new file +26 -0
new file mode 100644
index 0000000..196622e
@@ -0,0 +1,26 @@
+#ifndef KCM_KERNEL_H
+#define KCM_KERNEL_H
+
+struct kcm_attach {
+	int fd;
+	int bpf_type;
+	union {
+		struct sock_fprog bpf_fprog;
+		int bpf_fd;
+	};
+};
+
+#define KCM_BPF_TYPE_PROG	0x1
+#define KCM_BPF_TYPE_FD		0x2
+
+struct kcm_unattach {
+	int fd;
+};
+
+#define SIOCKCMATTACH	(SIOCPROTOPRIVATE + 0)
+#define SIOCKCMUNATTACH	(SIOCPROTOPRIVATE + 1)
+
+#define KCMPROTO_CONNECTED	0
+
+#endif
+
net/Kconfig +1 -0
index 7021c1b..cc5a020 100644
@@ -350,6 +350,7 @@ source "net/can/Kconfig"
 source "net/irda/Kconfig"
 source "net/bluetooth/Kconfig"
 source "net/rxrpc/Kconfig"
+source "net/kcm/Kconfig"
 
 config FIB_RULES
 	bool
net/Makefile +1 -0
index 3995613..2b5d24d 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_IRDA)		+= irda/
 obj-$(CONFIG_BT)		+= bluetooth/
 obj-$(CONFIG_SUNRPC)		+= sunrpc/
 obj-$(CONFIG_AF_RXRPC)		+= rxrpc/
+obj-$(CONFIG_AF_KCM)		+= kcm/
 obj-$(CONFIG_ATM)		+= atm/
 obj-$(CONFIG_L2TP)		+= l2tp/
 obj-$(CONFIG_DECNET)		+= decnet/
net/kcm/Kconfig new file +10 -0
new file mode 100644
index 0000000..5ee5294
@@ -0,0 +1,10 @@
+
+config AF_KCM
+	tristate "KCM sockets"
+	depends on INET
+	select BPF_SYSCALL
+	---help---
+	  KCM (Kernel Connection Multiplexer) sockets provide a method
+	  for multiplexing a messages based user space protocol over
+	  kernel connectons (e.g. TCP connections).
+
net/kcm/Makefile new file +3 -0
new file mode 100644
index 0000000..cb525f7
@@ -0,0 +1,3 @@
+obj-$(CONFIG_AF_KCM) += kcm.o
+
+kcm-y := kcmsock.o
net/kcm/kcmsock.c new file +1566 -0

1,566 lines not on this page · raw message

Signature · 1 line
1.8.1
#7 lore raw

RFC PATCH 3/3 kcm: Add statistics and proc interfaces

In reply to Tom Herbert

This patch add various counters for KCM. These include counters for messages and bytes received or sent, as well as counters number of attached/unattached TCP sockets and other error or edge events.

The statistics are exposed via a proc interface. /proc/net/kcm provdes statistics per KCM socket and per psock (attached TCP sockkets). /proc/net/kcm_stats provides aggregate statistics.

---
 include/net/kcm.h | 102 ++++++++++++++
 net/kcm/Makefile  |   2 +-
 net/kcm/kcmproc.c | 415 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/kcm/kcmsock.c |  63 +++++++++
 4 files changed, 581 insertions(+), 1 deletion(-)
 create mode 100644 net/kcm/kcmproc.c
include/net/kcm.h +102 -0
index 55ef56b..5076247 100644
@@ -11,6 +11,46 @@
 
 extern unsigned int kcm_net_id;
 
+#define KCM_STATS_ADD(stat, count)			\
+	((stat) += (count))
+
+#define KCM_STATS_INCR(stat)				\
+	((stat)++)
+
+struct kcm_psock_stats {
+	unsigned long long rx_msgs;
+	unsigned long long rx_bytes;
+	unsigned long long tx_msgs;
+	unsigned long long tx_bytes;
+	unsigned int rx_aborts;
+	unsigned int rx_mem_fail;
+	unsigned int rx_need_more_hdr;
+	unsigned int rx_bad_hdr_len;
+	unsigned long long reserved;
+	unsigned long long unreserved;
+	unsigned int tx_aborts;
+};
+
+struct kcm_mux_stats {
+	unsigned long long rx_msgs;
+	unsigned long long rx_bytes;
+	unsigned long long tx_msgs;
+	unsigned long long tx_bytes;
+	unsigned int rx_ready_drops;
+	unsigned int tx_no_psock;
+	unsigned int tx_retries;
+	unsigned int psock_attach;
+	unsigned int psock_unattach_rsvd;
+	unsigned int psock_unattach;
+};
+
+struct kcm_stats {
+	unsigned long long rx_msgs;
+	unsigned long long rx_bytes;
+	unsigned long long tx_msgs;
+	unsigned long long tx_bytes;
+};
+
 struct kcm_tx_msg {
 	unsigned int sent;
 	unsigned int fragidx;
@@ -30,6 +70,8 @@ struct kcm_sock {
 	struct kcm_mux *mux;
 	struct list_head kcm_sock_list;
 
+	struct kcm_stats stats;
+
 	/* Transmit */
 	struct kcm_psock *tx_psock;
 	struct work_struct tx_work;
@@ -61,6 +103,10 @@ struct kcm_psock {
 
 	struct list_head psock_list;
 
+	struct kcm_psock_stats stats;
+	unsigned long long saved_tx_bytes;
+	unsigned long long saved_tx_msgs;
+
 	/* Receive */
 	struct sk_buff *rx_skb_head;
 	struct sk_buff **rx_skb_nextp;
@@ -78,6 +124,8 @@ struct kcm_psock {
 /* Per net MUX list */
 struct kcm_net {
 	struct mutex mutex;
+	struct kcm_psock_stats aggregate_psock_stats;
+	struct kcm_mux_stats aggregate_mux_stats;
 	struct list_head mux_list;
 	int count;
 };
@@ -94,6 +142,9 @@ struct kcm_mux {
 	struct list_head psocks;	/* List of all psocks on MUX */
 	int psocks_cnt;		/* Total attached sockets */
 
+	struct kcm_mux_stats stats;
+	struct kcm_psock_stats aggregate_psock_stats;
+
 	/* Receive */
 	struct list_head kcm_rx_waiters; /* KCMs waiting for receiving */
 	struct kcm_sock *last_rx_kcm;
@@ -104,6 +155,57 @@ struct kcm_mux {
 	struct list_head kcm_tx_waiters; /* KCMs waiting for a TX psock */
 };
 
+#ifdef CONFIG_PROC_FS
+int kcm_proc_init(void);
+void kcm_proc_exit(void);
+#else
+static int kcm_proc_init(void) { return 0; }
+static void kcm_proc_exit(void) { }
+#endif
+
+
+static inline void aggregate_psock_stats(struct kcm_psock_stats *stats,
+					 struct kcm_psock_stats *agg_stats)
+{
+	/* Save psock statistics in the mux when psock is being unattached. */
+
+#define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
+
+	SAVE_PSOCK_STATS(rx_msgs);
+	SAVE_PSOCK_STATS(rx_bytes);
+	SAVE_PSOCK_STATS(rx_aborts);
+	SAVE_PSOCK_STATS(rx_mem_fail);
+	SAVE_PSOCK_STATS(rx_need_more_hdr);
+	SAVE_PSOCK_STATS(rx_bad_hdr_len);
+	SAVE_PSOCK_STATS(tx_msgs);
+	SAVE_PSOCK_STATS(tx_bytes);
+	SAVE_PSOCK_STATS(reserved);
+	SAVE_PSOCK_STATS(unreserved);
+	SAVE_PSOCK_STATS(tx_aborts);
+
+#undef SAVE_PSOCK_STATS
+}
+
+static inline void aggregate_mux_stats(struct kcm_mux_stats *stats,
+				       struct kcm_mux_stats *agg_stats)
+{
+	/* Save psock statistics in the mux when psock is being unattached. */
+
+#define SAVE_MUX_STATS(_stat) (agg_stats->_stat += stats->_stat)
+
+	SAVE_MUX_STATS(rx_msgs);
+	SAVE_MUX_STATS(rx_bytes);
+	SAVE_MUX_STATS(tx_msgs);
+	SAVE_MUX_STATS(tx_bytes);
+	SAVE_MUX_STATS(tx_no_psock);
+	SAVE_MUX_STATS(rx_ready_drops);
+	SAVE_MUX_STATS(psock_attach);
+	SAVE_MUX_STATS(psock_unattach_rsvd);
+	SAVE_MUX_STATS(psock_unattach);
+
+#undef SAVE_MUX_STATS
+}
+
 #endif /* __KERNEL__ */
 
 #endif /* __NET_KCM_H_ */
net/kcm/Makefile +1 -1
index cb525f7..7125613 100644
@@ -1,3 +1,3 @@
 obj-$(CONFIG_AF_KCM) += kcm.o
 
-kcm-y := kcmsock.o
+kcm-y := kcmsock.o kcmproc.o
net/kcm/kcmproc.c new file +415 -0

415 lines not on this page · raw message

net/kcm/kcmsock.c +63 -0

243 lines not on this page · raw message

Signature · 1 line
1.8.1
#8 lore raw
In reply to Tom Herbert
Quoted 12 lines · Tom Herbert

Kernel Connection Multiplexor (KCM) is a facility that provides a message based interface over TCP for generic application protocols. The motivation for this is based on the observation that although TCP is byte stream transport protocol with no concept of message boundaries, a common use case is to implement a framed application layer protocol running over TCP. To date, most TCP stacks offer byte stream API for applications, which places the burden of message delineation, message I/O operation atomicity, and load balancing in the application. With KCM an application can efficiently send and receive application protocol messages over TCP using a datagram interface.

A lot of this design is very similar to the PF_RDS/RDS-TCP design. There too, we have a PF_RDS dgram socket (that already supports SEQPACKET semantics today) that can be tunneled over TCP.

The biggest design difference that I see in your proposal is that you are using BPF so presumably the demux has more flexibility than RDS, which does the demux based on RDS port numbers?

Would it make sense to build your solution on top of RDS, rather than re-invent solutions for many of the challenges that one encounters when building a dgram-over-stream hybrid socket (see "lessons learned" list below)?

Some things that were not clear to me from the patch-set:

The doc statses that we re-assemble packets the "stated length" - but how will the receiver know the "stated length"? (fwiw, RDS figures that out from the header len in RDS, and elsewhere I think you allude to some similar encaps header - is that a correct understanding?)

not clear from the diagram: Is there one TCP socket per kcm-socket? what is the relation (one-one, many-one etc.) between a kcm-socket and a psock? How does the ksock-psock-tcp-sock association get set up?

the notes say one can "accept()" over a kcm socket- but "accept()" is itself a connection-oriented concept- one does not accept() on a dgram socket. So what exactly does this mean, and why not just use the well-defined TCP socket semantics at that point (with something like XDR for message boundary marking)?

In the "fwiw" bucket of lessons learned from RDS.. please ignore if you were already aware of these-

In the case of RDS, since multiple rds/dgram sockets share a single TCP socket, some issues that have to be dealt with are

- congestion/starvation: we dont want tcp to start advertising zero-window because one dgram socket pair has flooded the pipe and the peer is not reading. So the RDS protocol has port-congestion RDS control plane messages that track congestion at the RDS port.

- imposes some constraints on the TCP send side- if sock1 and sock2 are sharing a tcp socket, and both are sending dgrams over the stream, dgrams from sock1 may get interleaved (see comments above rds_send_xmit() for a note on how rds deals witt this). There are ways to fan this out over multiple tcp sockets (and I'm working on those, to improve the scaling), but just a note that there is some complexity to be dealt with here. Not sure if this was considered in the "KCM sockets" section in patch2..

- in general the "dgram-over-stream" hybrid has some peculiar issues. E.g., dgram APIs like BINDTODEVICE and IP_PKTINFO cannot be applied to the underlying stream. In the typical use case for RDS (database clusters) there's a reasonable workaround for this using network namespaces to define bundles of outgoing interfaces, but that solution may not always be workable for other use-cases. Thus it might actually be more obvious to simply use tcp sockets (and use something like XDR for message boundary markers on the stream).

--Sowmini

#9 lore raw

Hi Sowmini,

Thanks for your comments, some replies are in line.

Quoted 8 lines

A lot of this design is very similar to the PF_RDS/RDS-TCP design. There too, we have a PF_RDS dgram socket (that already supports SEQPACKET semantics today) that can be tunneled over TCP.

The biggest design difference that I see in your proposal is that you are using BPF so presumably the demux has more flexibility than RDS, which does the demux based on RDS port numbers?

I did look a bit a RDS. Major differences with KCM are:

- KCM does not implement any specific protocol in the kernel. Parsing in receive is accomplished using BPF which allows protocol parsing to be programmed from userspace.
- Connection management is done in userspace. This is particularly important when connections need to switch into a protocol mode, like when doing HTTP/2, Web sockets, SPDY, etc. over port 80.

Quoted 4 lines

Would it make sense to build your solution on top of RDS, rather than re-invent solutions for many of the challenges that one encounters when building a dgram-over-stream hybrid socket (see "lessons learned" list below)?

There might be some points of leverage, but as I pointed out the primary goal of KCM is the multiplexing and datagram interface over TCP not application protocol implementation in the kernel. It might be interesting if there were a common protocol generic library to handle the user interface.

Quoted 5 lines

Some things that were not clear to me from the patch-set:

The doc statses that we re-assemble packets the "stated length" - but how will the receiver know the "stated length"?

BPF program returns the length of the next message. In my testing so far I've been using HTTP/2 which defines a frame format with first 3 bytes being header length field . The BPF program (using LLVM/Clang-- thanks Alexei!) is just:

int bpf_prog1(struct __sk_buff *skb)

{
     return (load_word(skb, 0) >> 8) + 9;
}
Quoted 4 lines

(fwiw, RDS figures that out from the header len in RDS, and elsewhere I think you allude to some similar encaps header - is that a correct understanding?)

KCM does not define any encaps header, it is intended to support existing ones. For instance, BPF code to get length from an RDS message would be:

int bpf_prog1(struct __sk_buff *skb)

{
     return load_word(skb, 16) + 40;
}
Quoted 4 lines

not clear from the diagram: Is there one TCP socket per kcm-socket? what is the relation (one-one, many-one etc.) between a kcm-socket and a psock? How does the ksock-psock-tcp-sock association get set up?

Each multiplexor is logically one destination. At the top multiple KCM sockets allow concurrent operations in userspace, at the bottom multiple TCP connections allow for load balancing. An application controls construction of the multiplexor and would presumably create multiplexor for each peer. See Documentaiton/net/kcm.txt for the details on interfaces for plumbing.

Quoted 6 lines

the notes say one can "accept()" over a kcm socket- but "accept()" is itself a connection-oriented concept- one does not accept() on a dgram socket. So what exactly does this mean, and why not just use the well-defined TCP socket semantics at that point (with something like XDR for message boundary marking)?

The accept method is overloaded on KCM sockets to do the socket cloning operation. This is unrelated to TCP semantics, connection management is performed on TCP sockets (i.e. before being attached to a KCM multiplexor).

Quoted 11 lines

In the "fwiw" bucket of lessons learned from RDS.. please ignore if you were already aware of these-

In the case of RDS, since multiple rds/dgram sockets share a single TCP socket, some issues that have to be dealt with are

- congestion/starvation: we dont want tcp to start advertising zero-window because one dgram socket pair has flooded the pipe and the peer is not reading. So the RDS protocol has port-congestion RDS control plane messages that track congestion at the RDS port.

In KCM all upper sockets are equivalent so there is not HOL blocking on receive or transmit. A message received on a multiplexor can be steered to any socket that is receiving. Conceivably, we could implement some message affinity, for instance sending an RPC reply to same socket that made the request, but even that I think should only be best effort to avoid having to deal with blocking.

Quoted 9 lines

- imposes some constraints on the TCP send side- if sock1 and sock2 are sharing a tcp socket, and both are sending dgrams over the stream, dgrams from sock1 may get interleaved (see comments above rds_send_xmit() for a note on how rds deals witt this). There are ways to fan this out over multiple tcp sockets (and I'm working on those, to improve the scaling), but just a note that there is some complexity to be dealt with here. Not sure if this was considered in the "KCM sockets" section in patch2..

Writing and reading messages atomically is a critical operation of the multiplexor. This is implemented using a reservation model (see reserve_psock, unreserve_psock).

Quoted 9 lines

- in general the "dgram-over-stream" hybrid has some peculiar issues. E.g., dgram APIs like BINDTODEVICE and IP_PKTINFO cannot be applied to the underlying stream. In the typical use case for RDS (database clusters) there's a reasonable workaround for this using network namespaces to define bundles of outgoing interfaces, but that solution may not always be workable for other use-cases. Thus it might actually be more obvious to simply use tcp sockets (and use something like XDR for message boundary markers on the stream).

My intent is to add an "unconnected" mode to KCM which would allow connections to different destinations (represented by connection groups) to be attached to the same MUX. Destinations would be specified by some sort of AF_KCM sockaddr.

Thanks,
Tom

Quoted 4 lines

--Sowmini

#10 lore raw
Quoted 10 lines · Tom Herbert
Quoted 5 lines

Some things that were not clear to me from the patch-set:

The doc statses that we re-assemble packets the "stated length" - but how will the receiver know the "stated length"?

BPF program returns the length of the next message. In my testing so far I've been using HTTP/2 which defines a frame format with first 3 bytes being header length field . The BPF program (using LLVM/Clang-- thanks Alexei!) is just:

Maybe I dont see something about the mux/demux here (I have to take a closer look at reserve_psock/unreserve_psock), but will every tcp segment have a 3 byte length in the payload?

Not every TCP segment in the RDS-TCP case will have a RDS header, thus the comments before rds_send_xmit(), thus applying the bpf filter to a TCP segment holding some "from-the-middle" piece of the RDS dgram may not be possible

Quoted 2 lines
Quoted 2 lines

the notes say one can "accept()" over a kcm socket- but "accept()" is itself a connection-oriented concept- one does not accept() on

    :
Quoted 4 lines

The accept method is overloaded on KCM sockets to do the socket cloning operation. This is unrelated to TCP semantics, connection management is performed on TCP sockets (i.e. before being attached to a KCM multiplexor).

If possible,it might be better to use some other glibc-func/name/syscall/sockopt/whatever
for this, rather than overloading accept().. feels like that would keep the semantics cleaner, and probably less likely to trip up on accept code in the kernel..

--Sowmini

#11 lore raw
Quoted 7 lines · Sowmini Varadhan
Quoted 6 lines · Tom Herbert
Quoted 5 lines

Some things that were not clear to me from the patch-set:

The doc statses that we re-assemble packets the "stated length" - but how will the receiver know the "stated length"?

Quoted 9 lines
Quoted 4 lines

BPF program returns the length of the next message. In my testing so far I've been using HTTP/2 which defines a frame format with first 3 bytes being header length field . The BPF program (using LLVM/Clang-- thanks Alexei!) is just:

Maybe I dont see something about the mux/demux here (I have to take a closer look at reserve_psock/unreserve_psock), but will every tcp segment have a 3 byte length in the payload?

No, there is no provision in TCP that application layer headers align with TCP segments or that message boundaries are respected with TCP segments. What we need to do, which you're probably doing for RDS, is do message delineation on the stream as a sequence of:

1) Read protocol header to determine message length (BPF used here)
2) Read data up to the length of the message
3) Deliver message
4) Goto #1 (i.e. process next message in the stream).

Quoted 13 lines

Not every TCP segment in the RDS-TCP case will have a RDS header, thus the comments before rds_send_xmit(), thus applying the bpf filter to a TCP segment holding some "from-the-middle" piece of the RDS dgram may not be possible

Quoted 2 lines
Quoted 2 lines

the notes say one can "accept()" over a kcm socket- but "accept()" is itself a connection-oriented concept- one does not accept() on

    :
Quoted 4 lines

The accept method is overloaded on KCM sockets to do the socket cloning operation. This is unrelated to TCP semantics, connection management is performed on TCP sockets (i.e. before being attached to a KCM multiplexor).

Quoted 6 lines

If possible,it might be better to use some other glibc-func/name/syscall/sockopt/whatever
for this, rather than overloading accept().. feels like that would keep the semantics cleaner, and probably less likely to trip up on accept code in the kernel..

I'll a look at alternatives, but I sort of think this is okay since the semantics of accept are defined per protocol (in this case the "protocol" is KCM).

Thanks,
Tom

Quoted 1 line

--Sowmini

#12 lore raw
Quoted 4 lines · Tom Herbert

segments. What we need to do, which you're probably doing for RDS, is do message delineation on the stream as a sequence of:

1) Read protocol header to determine message length (BPF used here)

right, that's what rds does- first reads the sizeof(rds_header), and from that, figures out payload len, to stitch each rds dgram together from intermediate tcp segments..

Quoted 3 lines

2) Read data up to the length of the message
3) Deliver message
4) Goto #1 (i.e. process next message in the stream).

Thanks for the rest of the responses.

--Sowmini

#13 lore raw
In reply to Tom Herbert

Am 21.09.2015 um 00:29 schrieb Tom Herbert:

Quoted 12 lines

Kernel Connection Multiplexor (KCM) is a facility that provides a message based interface over TCP for generic application protocols. The motivation for this is based on the observation that although TCP is byte stream transport protocol with no concept of message boundaries, a common use case is to implement a framed application layer protocol running over TCP. To date, most TCP stacks offer byte stream API for applications, which places the burden of message delineation, message I/O operation atomicity, and load balancing in the application. With KCM an application can efficiently send and receive application protocol messages over TCP using a datagram interface.

Hello Tom,

on a general note I'm wondering what this offers over the existing SCTP support. SCTP offers reliable message exchange and multihoming already.

On a specific note, I'm wondering if the dup(2) familiy of system calls isn't a lot more suitable/natural instead of overloading accept.

Best regards.

#3 lore raw

Re: [PATCH RFC 2/3] kcm: Kernel Connection Multiplexor module

In reply to Tom Herbert
Quoted 21 lines · Tom Herbert
Documentation/networking/kcm.txt new file +21 -0
+Attaching of transport sockets to a multiplexor is performed by calling on
+ioctl on a KCM socket for the multiplexor. e.g.:
+
+  /* From linux/kcm.h */
+  struct kcm_attach {
+        int fd;
+        int bpf_type;
+        union {
+                int bpf_fd;
+                struct sock_fprog fprog;
+        };
+  };
+
+  struct kcm_attach info;
+
+  memset(&info, 0, sizeof(info));
+
+  info.fd = tcpfd;
+  info.bpf_type = KCM_BPF_TYPE_PROG;
+  info.bpf_fprog = bpf_prog;
+

Documentation/networking/kcm.txt line 153

Quoted 10 lines
Documentation/networking/kcm.txt new file +10 -0
+  ioctl(kcmfd, SIOCKCMATTACH, &info);
+
+The kcm_attach structure contains:
+  fd: file descriptor for TCP socket being attached
+  bpf_type: type of BPF program to be loaded this is either:
+    KCM_BPF_TYPE_PROG: program load directly for user space
+    KCM_BPF_TYPE_FD: Complied rogram to be load for the specified file
+                     descriptor (see BPF LLVM and Clang)
+  bpf_fprog: contains pointer to user space protocol to load
+  bpf_fd: file descriptor for compiled program download

Interesting approach!
I would only suggest to drop support for classic BPF. It's usable to return frame length of http2, but it won't be able to parse protocols where fields are little endian. Also it doesn't scale, since new cBPF program would be created for every KCM socket, whereas with eBPF we can use single program for all KCM sockets via single FD.

btw, did you consider to use BPF not only for frame length, but also to select KCM socket ? For example for http2 it can pick a socket based on stream id, providing affinity and further improving performance ?

#14 lore raw
In reply to Thomas Martitz
Quoted 16 lines · Thomas Martitz

Am 21.09.2015 um 00:29 schrieb Tom Herbert:

Quoted 13 lines

Kernel Connection Multiplexor (KCM) is a facility that provides a message based interface over TCP for generic application protocols. The motivation for this is based on the observation that although TCP is byte stream transport protocol with no concept of message boundaries, a common use case is to implement a framed application layer protocol running over TCP. To date, most TCP stacks offer byte stream API for applications, which places the burden of message delineation, message I/O operation atomicity, and load balancing in the application. With KCM an application can efficiently send and receive application protocol messages over TCP using a datagram interface.

Quoted 5 lines

Hello Tom,

on a general note I'm wondering what this offers over the existing SCTP support. SCTP offers reliable message exchange and multihoming already.

Hi Thomas,

The idea of KCM is to provide a an improved interface that is usable with currently deployed protocols without invoking any change on the wire whatsoever. Deploying SCTP to replace TCP use cases would entail a major operations change for us, AFAIK hardware support is scant, and we probably still can't reliably use it on the Internet (you can Google why is SCTP not widely deployed). I would point out that two of the four limitations of TCP listed in RFC4960 should be addressed by KCM :-)

Quoted 3 lines

On a specific note, I'm wondering if the dup(2) familiy of system calls isn't a lot more suitable/natural instead of overloading accept.

dup is to duplicate a file descriptor not a socket. Sockets operations on duplicated file descriptors operate on the same socket, need to contend for the socket lock, etc. This is a similar rationale as to why we need SO_REUSEPORT instead of just dup'ing listener fds.

Thanks,
Tom

Quoted 1 line

Best regards.

#4 lore raw

Re: [PATCH RFC 2/3] kcm: Kernel Connection Multiplexor module

In reply to Alexei Starovoitov
Quoted 33 lines · Alexei Starovoitov
Quoted 21 lines · Tom Herbert
Documentation/networking/kcm.txt new file +21 -0
+Attaching of transport sockets to a multiplexor is performed by calling on
+ioctl on a KCM socket for the multiplexor. e.g.:
+
+  /* From linux/kcm.h */
+  struct kcm_attach {
+        int fd;
+        int bpf_type;
+        union {
+                int bpf_fd;
+                struct sock_fprog fprog;
+        };
+  };
+
+  struct kcm_attach info;
+
+  memset(&info, 0, sizeof(info));
+
+  info.fd = tcpfd;
+  info.bpf_type = KCM_BPF_TYPE_PROG;
+  info.bpf_fprog = bpf_prog;
+

Documentation/networking/kcm.txt line 153

Quoted 10 lines
Documentation/networking/kcm.txt new file +10 -0
+  ioctl(kcmfd, SIOCKCMATTACH, &info);
+
+The kcm_attach structure contains:
+  fd: file descriptor for TCP socket being attached
+  bpf_type: type of BPF program to be loaded this is either:
+    KCM_BPF_TYPE_PROG: program load directly for user space
+    KCM_BPF_TYPE_FD: Complied rogram to be load for the specified file
+                     descriptor (see BPF LLVM and Clang)
+  bpf_fprog: contains pointer to user space protocol to load
+  bpf_fd: file descriptor for compiled program download
Quoted 8 lines

Interesting approach!
I would only suggest to drop support for classic BPF. It's usable to return frame length of http2, but it won't be able to parse protocols where fields are little endian. Also it doesn't scale, since new cBPF program would be created for every KCM socket, whereas with eBPF we can use single program for all KCM sockets via single FD.

Hi Alexei,

That makes sense, but I think there may be some use cases where we'd like lightweight methods to program filters. Writing C code for BPF is extremely cool, but integrating LLVM/Clang into our development environment may be a pain. We also might want to create a different program for every socket anyway (like from some template with parameterization). An eBPF assembler and jit support could be useful (looks like there might be some work started on both of these already).

Quoted 5 lines

btw, did you consider to use BPF not only for frame length, but also to select KCM socket ? For example for http2 it can pick a socket based on stream id, providing affinity and further improving performance ?

Yes. I am thinking that eBPF can set the stream ID/transactional identifiers in so_mark and then MUX steers to KCM sockets based on that. As Sowmini pointed out we need to be weary of HOL blocking in this...

Tom

#5 lore raw

Re: [PATCH RFC 2/3] kcm: Kernel Connection Multiplexor module

Quoted 9 lines · Tom Herbert
Quoted 7 lines

I would only suggest to drop support for classic BPF. It's usable to return frame length of http2, but it won't be able to parse protocols where fields are little endian. Also it doesn't scale, since new cBPF program would be created for every KCM socket, whereas with eBPF we can use single program for all KCM sockets via single FD.

Hi Alexei,

Quoted 8 lines

That makes sense, but I think there may be some use cases where we'd like lightweight methods to program filters. Writing C code for BPF is extremely cool, but integrating LLVM/Clang into our development environment may be a pain. We also might want to create a different program for every socket anyway (like from some template with parameterization). An eBPF assembler and jit support could be useful (looks like there might be some work started on both of these already).

coding eBPF with macros as sock_example.c, test_verifier.c, test_bpf.c do should be enough to start. User space assembler to convert text to eBPF binary obviously would be even better. But lack of tiny and standalone assembler today shouldn't be the reason to include classic as permenent kernel ABI. Not sure what you mean by 'jit support'. There is in-kernel jit and there is jit mode of llvm where bpf can be taken from memory without going to elf file.

#6 lore raw

Re: [PATCH RFC 2/3] kcm: Kernel Connection Multiplexor module

In reply to Tom Herbert
Quoted 10 lines · Tom Herbert

This module implement the Kernel Connection Multiplexor.

Kernel Connection Multiplexor (KCM) is a facility that provides a message based interface over TCP for generic application protocols. With KCM an application can efficiently send and receive application protocol messages over TCP using datagram sockets.

For more information see the included Documentation/networking/kcm.txt

This looks great!

Documentation/networking/kcm.txt line 118

Quoted 8 lines
Documentation/networking/kcm.txt new file +8 -0
+Cloning KCM sockets
+-------------------
+
+After the first KCM socket is created using the socket call as described
+above, additional sockets for the multiplexor can be created by cloning
+a KCM socket. This is accomplished by calling accept on the KCM socket:
+
+   newkcmfd = accept(kcmfd, NULL, 0)

This looks a bit ugly.

Quoted 1 line

+ ioctl(kcmfd, SIOCKCMATTACH, &info);

Use setsockopt() instead?

Quoted 20 lines
net/kcm/kcmsock.c new file +20 -0
+/* Process a new message. If there is no KCM socket waiting for a message
+ * hold it in the psock. Returns true if message is held this way, false
+ * otherwise.
+ */
+static bool new_rx_msg(struct kcm_psock *psock, struct sk_buff *head)
+{
+	struct kcm_mux *mux = psock->mux;
+	struct kcm_sock *kcm = NULL;
+	struct sock *sk;
+
+	spin_lock_bh(&mux->lock);
+
+	if (WARN_ON(psock->ready_rx_msg)) {
+		spin_unlock_bh(&mux->lock);
+		kfree_skb(head);
+		return false;
+	}
+
+	if (list_empty(&mux->kcm_rx_waiters)) {
+		psock->ready_rx_msg = head;

net/kcm/kcmsock.c line 158

Quoted 10 lines
net/kcm/kcmsock.c new file +10 -0
+
+		list_add_tail(&psock->psock_ready_list,
+			      &mux->psocks_ready);
+
+		spin_unlock_bh(&mux->lock);
+		return true;
+	}
+
+	kcm = list_first_entry(&mux->kcm_rx_waiters,
+			       struct kcm_sock, wait_rx_list);

Per CPU list of waiting sockets?