Author: sbruno
Date: Mon Apr 23 19:51:00 2018
New Revision: 332894
URL: https://svnweb.freebsd.org/changeset/base/332894
Log:
Load balance sockets with new SO_REUSEPORT_LB option
This patch adds a new socket option, SO_REUSEPORT_LB, which allow multiple
programs or threads to bind to the same port and incoming connections will be
load balanced using a hash function.
Most of the code was copied from a similar patch for DragonflyBSD.
However, in DragonflyBSD, load balancing is a global on/off setting and can
not
be set per socket. This patch allows for simultaneous use of both the current
SO_REUSEPORT and the new SO_REUSEPORT_LB options on the same system.
Required changes to structures
Globally change so_options from 16 to 32 bit value to allow for more options.
Add hashtable in pcbinfo to hold all SO_REUSEPORT_LB sockets.
Limitations
As DragonflyBSD, a load balance group is limited to 256 pcbs
(256 programs or threads sharing the same socket).
Submitted by: Johannes Lundberg
Sponsored by: Limelight Networks
Differential Revision:https://reviews.freebsd.org/D11003
Modified:
head/cddl/lib/libdtrace/tcp.d
head/sys/kern/uipc_debug.c
head/sys/kern/uipc_socket.c
head/sys/netinet/in_pcb.c
head/sys/netinet/in_pcb.h
head/sys/netinet/ip_output.c
head/sys/netinet/tcp_subr.c
head/sys/netinet/udp_usrreq.c
head/sys/netinet6/in6_pcb.c
head/sys/netinet6/in6_src.c
head/sys/netinet6/ip6_output.c
head/sys/netinet6/udp6_usrreq.c
head/sys/sys/socket.h
head/sys/sys/socketvar.h
Modified: head/cddl/lib/libdtrace/tcp.d
==
--- head/cddl/lib/libdtrace/tcp.d Mon Apr 23 18:33:26 2018
(r332893)
+++ head/cddl/lib/libdtrace/tcp.d Mon Apr 23 19:51:00 2018
(r332894)
@@ -192,12 +192,12 @@ translator tcpsinfo_t < struct tcpcb *p > {
tcps_rport =p == NULL ? 0 :
ntohs(p->t_inpcb->inp_inc.inc_ie.ie_fport);
tcps_laddr =p == NULL ? 0 :
p->t_inpcb->inp_vflag == INP_IPV4 ?
-
inet_ntoa(>t_inpcb->inp_inc.inc_ie.ie_dependladdr.ie46_local.ia46_addr4.s_addr)
:
- inet_ntoa6(>t_inpcb->inp_inc.inc_ie.ie_dependladdr.ie6_local);
+
inet_ntoa(>t_inpcb->inp_inc.inc_ie.ie_dependladdr.id46_addr.ia46_addr4.s_addr)
:
+ inet_ntoa6(>t_inpcb->inp_inc.inc_ie.ie_dependladdr.id6_addr);
tcps_raddr =p == NULL ? 0 :
p->t_inpcb->inp_vflag == INP_IPV4 ?
-
inet_ntoa(>t_inpcb->inp_inc.inc_ie.ie_dependfaddr.ie46_foreign.ia46_addr4.s_addr)
:
- inet_ntoa6(>t_inpcb->inp_inc.inc_ie.ie_dependfaddr.ie6_foreign);
+
inet_ntoa(>t_inpcb->inp_inc.inc_ie.ie_dependfaddr.id46_addr.ia46_addr4.s_addr)
:
+ inet_ntoa6(>t_inpcb->inp_inc.inc_ie.ie_dependfaddr.id6_addr);
tcps_state =p == NULL ? -1 : p->t_state;
tcps_iss = p == NULL ? 0 : p->iss;
tcps_irs = p == NULL ? 0 : p->irs;
Modified: head/sys/kern/uipc_debug.c
==
--- head/sys/kern/uipc_debug.c Mon Apr 23 18:33:26 2018(r332893)
+++ head/sys/kern/uipc_debug.c Mon Apr 23 19:51:00 2018(r332894)
@@ -77,7 +77,7 @@ db_print_sotype(short so_type)
}
static void
-db_print_sooptions(short so_options)
+db_print_sooptions(int so_options)
{
int comma;
@@ -120,6 +120,10 @@ db_print_sooptions(short so_options)
}
if (so_options & SO_REUSEPORT) {
db_printf("%sSO_REUSEPORT", comma ? ", " : "");
+ comma = 1;
+ }
+ if (so_options & SO_REUSEPORT_LB) {
+ db_printf("%sSO_REUSEPORT_LB", comma ? ", " : "");
comma = 1;
}
if (so_options & SO_TIMESTAMP) {
Modified: head/sys/kern/uipc_socket.c
==
--- head/sys/kern/uipc_socket.c Mon Apr 23 18:33:26 2018(r332893)
+++ head/sys/kern/uipc_socket.c Mon Apr 23 19:51:00 2018(r332894)
@@ -1057,6 +1057,100 @@ sofree(struct socket *so)
}
/*
+ * Let socket in same load balance group (same port and address)
+ * inherit pending sockets of the closing socket.
+ *
+ * "so_inh" will inherit sockets from "so"
+ */
+void
+soinherit(struct socket *so, struct socket *so_inh)
+{
+ TAILQ_HEAD(, socket) comp, incomp;
+ struct socket *sp, *head, *head_inh;
+ int qlen, incqlen;
+
+ KASSERT(so->so_options & SO_ACCEPTCONN,
+ ("so does not accept connection"));
+ KASSERT(so_inh->so_options & SO_ACCEPTCONN,
+ ("so_inh does not accept connection"));
+
+
+restart:
+ SOCK_LOCK(so);
+ if ((head = so->so_listen) != NULL &&
+ __predict_false(SOLISTEN_TRYLOCK(head) == 0)) {
+ SOCK_UNLOCK(so);
+ goto restart;