]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: listener: now use a generic add_listener() function
authorWilly Tarreau <w@1wt.eu>
Fri, 4 Dec 2020 14:03:36 +0000 (15:03 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 4 Dec 2020 14:08:00 +0000 (15:08 +0100)
With the removal of the family-specific port setting, all protocol had
exactly the same implementation of ->add(). A generic one was created
with the name "default_add_listener" so that all other ones can now be
removed. The API was slightly adjusted so that the protocol and the
listener are passed instead of the listener and the port.

Note that all protocols continue to provide this ->add() method instead
of routinely calling default_add_listener() from create_listeners(). This
makes sure that any non-standard protocol will still be able to intercept
the listener addition if needed.

This could be backported to 2.3 along with the few previous patches on
listners as a pure code cleanup.

include/haproxy/listener.h
include/haproxy/protocol-t.h
src/listener.c
src/proto_sockpair.c
src/proto_tcp.c
src/proto_udp.c
src/proto_uxst.c

index 722a9ea0e6233b2d48cbb7daec3cd97330d43d33..8200395db1dfa8ceade28a7799129c40e1537c29 100644 (file)
@@ -124,6 +124,13 @@ int listener_backlog(const struct listener *l);
  */
 void listener_release(struct listener *l);
 
+/* This function adds the specified <listener> to the protocol <proto>. It
+ * does nothing if the protocol was already added. The listener's state is
+ * automatically updated from LI_INIT to LI_ASSIGNED. The number of listeners
+ * for the protocol is updated. This must be called with the proto lock held.
+ */
+void default_add_listener(struct protocol *proto, struct listener *listener);
+
 /* default function used to unbind a listener. This is for use by standard
  * protocols working on top of accepted sockets. The receiver's rx_unbind()
  * will automatically be used after the listener is disabled if the socket is
index 2a599500d2170c015368874d1cfdeff72ab95e0f..494f95d03ce8b1a895c58535e3ead373f1d36e2e 100644 (file)
@@ -88,7 +88,7 @@ struct protocol {
        int sock_prot;                                  /* socket protocol, as passed to socket() */
 
        /* functions acting on the listener */
-       void (*add)(struct listener *l, int port);      /* add a listener for this protocol and port */
+       void (*add)(struct protocol *p, struct listener *l); /* add a listener for this protocol */
        int (*listen)(struct listener *l, char *errmsg, int errlen); /* start a listener */
        void (*enable)(struct listener *l);             /* enable receipt of new connections */
        void (*disable)(struct listener *l);            /* disable receipt of new connections */
index a8c5460a912a0f955874b4ea87a4a8cbe9896ee3..397b3bed546424350ba922727cd8fecb55047b4a 100644 (file)
@@ -323,6 +323,21 @@ void stop_listener(struct listener *l, int lpx, int lpr, int lli)
                HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
 }
 
+/* This function adds the specified <listener> to the protocol <proto>. It
+ * does nothing if the protocol was already added. The listener's state is
+ * automatically updated from LI_INIT to LI_ASSIGNED. The number of listeners
+ * for the protocol is updated. This must be called with the proto lock held.
+ */
+void default_add_listener(struct protocol *proto, struct listener *listener)
+{
+       if (listener->state != LI_INIT)
+               return;
+       listener_set_state(listener, LI_ASSIGNED);
+       listener->rx.proto = proto;
+       LIST_ADDQ(&proto->receivers, &listener->rx.proto_list);
+       proto->nb_receivers++;
+}
+
 /* default function called to suspend a listener: it simply passes the call to
  * the underlying receiver. This is find for most socket-based protocols. This
  * must be called under the listener's lock. It will return non-zero on success,
@@ -616,13 +631,13 @@ int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
                l->rx.fd = fd;
 
                memcpy(&l->rx.addr, ss, sizeof(*ss));
-               if (proto->fam.set_port)
-                       proto->fam.set_port(&l->rx.addr, port);
+               if (proto->fam->set_port)
+                       proto->fam->set_port(&l->rx.addr, port);
 
                MT_LIST_INIT(&l->wait_queue);
                listener_set_state(l, LI_INIT);
 
-               proto->add(l, port);
+               proto->add(proto, l);
 
                if (fd != -1)
                        l->rx.flags |= RX_F_INHERITED;
index cc92753792005a5fb46760fc8c25e8a99ad2ea9b..fe74ff469d9517009cc76f4c2ff3481c93c2e46c 100644 (file)
@@ -43,7 +43,6 @@
 #include <haproxy/version.h>
 
 
-static void sockpair_add_listener(struct listener *listener, int port);
 static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
 static void sockpair_enable_listener(struct listener *listener);
 static void sockpair_disable_listener(struct listener *listener);
@@ -71,7 +70,7 @@ static struct protocol proto_sockpair = {
        .sock_domain = AF_CUST_SOCKPAIR,
        .sock_type = SOCK_STREAM,
        .sock_prot = 0,
-       .add = sockpair_add_listener,
+       .add = default_add_listener,
        .listen = sockpair_bind_listener,
        .enable = sockpair_enable_listener,
        .disable = sockpair_disable_listener,
@@ -89,23 +88,6 @@ static struct protocol proto_sockpair = {
 
 INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
 
-/* Add <listener> to the list of sockpair listeners (port is ignored). The
- * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
- * The number of listeners for the protocol is updated.
- *
- * Must be called with proto_lock held.
- *
- */
-static void sockpair_add_listener(struct listener *listener, int port)
-{
-       if (listener->state != LI_INIT)
-               return;
-       listener_set_state(listener, LI_ASSIGNED);
-       listener->rx.proto = &proto_sockpair;
-       LIST_ADDQ(&proto_sockpair.receivers, &listener->rx.proto_list);
-       proto_sockpair.nb_receivers++;
-}
-
 /* Enable receipt of incoming connections for listener <l>. The receiver must
  * still be valid.
  */
index 7c7670a4e98d6ec52588c6929996ba01dc2ee97a..30a0045be10a71f3e95789d5afef728dfd581ec7 100644 (file)
@@ -49,8 +49,6 @@ static int tcp_suspend_receiver(struct receiver *rx);
 static int tcp_resume_receiver(struct receiver *rx);
 static void tcp_enable_listener(struct listener *listener);
 static void tcp_disable_listener(struct listener *listener);
-static void tcpv4_add_listener(struct listener *listener, int port);
-static void tcpv6_add_listener(struct listener *listener, int port);
 
 /* Note: must not be declared <const> as its list will be overwritten */
 static struct protocol proto_tcpv4 = {
@@ -60,7 +58,7 @@ static struct protocol proto_tcpv4 = {
        .sock_domain = AF_INET,
        .sock_type = SOCK_STREAM,
        .sock_prot = IPPROTO_TCP,
-       .add = tcpv4_add_listener,
+       .add = default_add_listener,
        .listen = tcp_bind_listener,
        .enable = tcp_enable_listener,
        .disable = tcp_disable_listener,
@@ -90,7 +88,7 @@ static struct protocol proto_tcpv6 = {
        .sock_domain = AF_INET6,
        .sock_type = SOCK_STREAM,
        .sock_prot = IPPROTO_TCP,
-       .add = tcpv6_add_listener,
+       .add = default_add_listener,
        .listen = tcp_bind_listener,
        .enable = tcp_enable_listener,
        .disable = tcp_disable_listener,
@@ -712,40 +710,6 @@ int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
        return err;
 }
 
-/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
- * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
- * The number of listeners for the protocol is updated.
- *
- * Must be called with proto_lock held.
- *
- */
-static void tcpv4_add_listener(struct listener *listener, int port)
-{
-       if (listener->state != LI_INIT)
-               return;
-       listener_set_state(listener, LI_ASSIGNED);
-       listener->rx.proto = &proto_tcpv4;
-       LIST_ADDQ(&proto_tcpv4.receivers, &listener->rx.proto_list);
-       proto_tcpv4.nb_receivers++;
-}
-
-/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
- * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
- * The number of listeners for the protocol is updated.
- *
- * Must be called with proto_lock held.
- *
- */
-static void tcpv6_add_listener(struct listener *listener, int port)
-{
-       if (listener->state != LI_INIT)
-               return;
-       listener_set_state(listener, LI_ASSIGNED);
-       listener->rx.proto = &proto_tcpv6;
-       LIST_ADDQ(&proto_tcpv6.receivers, &listener->rx.proto_list);
-       proto_tcpv6.nb_receivers++;
-}
-
 /* Enable receipt of incoming connections for listener <l>. The receiver must
  * still be valid.
  */
index 436f2a01a2540a53611810323d3d21b76e1b61e5..5ac7b15880cb20ff319c895cd2e997c14ac37d4c 100644 (file)
@@ -45,8 +45,6 @@ static int udp_suspend_receiver(struct receiver *rx);
 static int udp_resume_receiver(struct receiver *rx);
 static void udp_enable_listener(struct listener *listener);
 static void udp_disable_listener(struct listener *listener);
-static void udp4_add_listener(struct listener *listener, int port);
-static void udp6_add_listener(struct listener *listener, int port);
 
 /* Note: must not be declared <const> as its list will be overwritten */
 static struct protocol proto_udp4 = {
@@ -56,7 +54,7 @@ static struct protocol proto_udp4 = {
        .sock_domain = AF_INET,
        .sock_type = SOCK_DGRAM,
        .sock_prot = IPPROTO_UDP,
-       .add = udp4_add_listener,
+       .add = default_add_listener,
        .listen = udp_bind_listener,
        .enable = udp_enable_listener,
        .disable = udp_disable_listener,
@@ -82,7 +80,7 @@ static struct protocol proto_udp6 = {
        .sock_domain = AF_INET6,
        .sock_type = SOCK_DGRAM,
        .sock_prot = IPPROTO_UDP,
-       .add = udp6_add_listener,
+       .add = default_add_listener,
        .listen = udp_bind_listener,
        .enable = udp_enable_listener,
        .disable = udp_disable_listener,
@@ -142,34 +140,6 @@ int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
        return err;
 }
 
-/* Add <listener> to the list of udp4 listeners, on port <port>. The
- * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
- * The number of listeners for the protocol is updated.
- */
-static void udp4_add_listener(struct listener *listener, int port)
-{
-       if (listener->state != LI_INIT)
-               return;
-       listener_set_state(listener, LI_ASSIGNED);
-       listener->rx.proto = &proto_udp4;
-       LIST_ADDQ(&proto_udp4.receivers, &listener->rx.proto_list);
-       proto_udp4.nb_receivers++;
-}
-
-/* Add <listener> to the list of udp6 listeners, on port <port>. The
- * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
- * The number of listeners for the protocol is updated.
- */
-static void udp6_add_listener(struct listener *listener, int port)
-{
-       if (listener->state != LI_INIT)
-               return;
-       listener_set_state(listener, LI_ASSIGNED);
-       listener->rx.proto = &proto_udp6;
-       LIST_ADDQ(&proto_udp6.receivers, &listener->rx.proto_list);
-       proto_udp6.nb_receivers++;
-}
-
 /* Enable receipt of incoming connections for listener <l>. The receiver must
  * still be valid.
  */
index f558e5f34b2fd05f59f234ad67292b9453c8da9c..9c19374f787bb00b5e090da57268a7a109ba961a 100644 (file)
@@ -42,7 +42,6 @@
 
 static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
 static int uxst_connect_server(struct connection *conn, int flags);
-static void uxst_add_listener(struct listener *listener, int port);
 static void uxst_enable_listener(struct listener *listener);
 static void uxst_disable_listener(struct listener *listener);
 static int uxst_suspend_receiver(struct receiver *rx);
@@ -55,7 +54,7 @@ static struct protocol proto_unix = {
        .sock_domain = PF_UNIX,
        .sock_type = SOCK_STREAM,
        .sock_prot = 0,
-       .add = uxst_add_listener,
+       .add = default_add_listener,
        .listen = uxst_bind_listener,
        .enable = uxst_enable_listener,
        .disable = uxst_disable_listener,
@@ -135,23 +134,6 @@ static int uxst_bind_listener(struct listener *listener, char *errmsg, int errle
        return err;
 }
 
-/* Add <listener> to the list of unix stream listeners (port is ignored). The
- * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
- * The number of listeners for the protocol is updated.
- *
- * Must be called with proto_lock held.
- *
- */
-static void uxst_add_listener(struct listener *listener, int port)
-{
-       if (listener->state != LI_INIT)
-               return;
-       listener_set_state(listener, LI_ASSIGNED);
-       listener->rx.proto = &proto_unix;
-       LIST_ADDQ(&proto_unix.receivers, &listener->rx.proto_list);
-       proto_unix.nb_receivers++;
-}
-
 /* Enable receipt of incoming connections for listener <l>. The receiver must
  * still be valid.
  */