]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: listener: move the receiving FD to struct receiver
authorWilly Tarreau <w@1wt.eu>
Thu, 27 Aug 2020 06:16:52 +0000 (08:16 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 16 Sep 2020 20:08:03 +0000 (22:08 +0200)
The listening socket is represented by its file descriptor, which is
generic to all receivers and not just listeners, so it must move to
the rx struct.

It's worth noting that in order to extend receivers and listeners to
other protocols such as QUIC, we'll need other handles than file
descriptors here, and that either a union or a cast to uintptr_t
will have to be used. This was not done yet and the field was
preserved under the name "fd" to avoid adding confusion.

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

index 7a07e3f2e0605ed00b250eb8cca2516cea9f527c..2bc0d0cac360c226d9cade383ba301182b9bebdf 100644 (file)
@@ -193,8 +193,8 @@ struct bind_conf {
 
 /* This describes a receiver with all its characteristics (address, status, etc) */
 struct receiver {
+       int fd;                          /* handle we receive from (fd only for now) */
        unsigned int flags;              /* receiver options (RX_F_*) */
-
        /* warning: this struct is huge, keep it at the bottom */
        struct sockaddr_storage addr;    /* the address the socket is bound to */
 };
@@ -207,7 +207,6 @@ struct listener {
        enum obj_type obj_type;         /* object type = OBJ_TYPE_LISTENER */
        enum li_state state;            /* state: NEW, INIT, ASSIGNED, LISTEN, READY, FULL */
        short int nice;                 /* nice value to assign to the instantiated tasks */
-       int fd;                         /* the listen socket */
        int luid;                       /* listener universally unique ID, used for SNMP */
        int options;                    /* socket options : LI_O_* */
        struct fe_counters *counters;   /* statistics counters */
index 5658a5f581166a395fab0c572f350ae77a214e09..3da48f2a4ec26ecffab110b2633874748081e722 100644 (file)
@@ -111,20 +111,20 @@ int frontend_accept(struct stream *s)
 
                if (!conn_get_src(conn)) {
                        chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [listener:%d] ALPN=%s\n",
-                                    s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
+                                    s->uniq_id, fe->id, (unsigned short)l->rx.fd, (unsigned short)conn->handle.fd,
                                     l->luid, alpn);
                }
                else switch (addr_to_str(conn->src, pn, sizeof(pn))) {
                case AF_INET:
                case AF_INET6:
                        chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [%s:%d] ALPN=%s\n",
-                                    s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
+                                    s->uniq_id, fe->id, (unsigned short)l->rx.fd, (unsigned short)conn->handle.fd,
                                     pn, get_host_port(conn->src), alpn);
                        break;
                case AF_UNIX:
                        /* UNIX socket, only the destination is known */
                        chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [unix:%d] ALPN=%s\n",
-                                    s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
+                                    s->uniq_id, fe->id, (unsigned short)l->rx.fd, (unsigned short)conn->handle.fd,
                                     l->luid, alpn);
                        break;
                }
index 12a5992d8bf13ad3f4ad03f6c4fb82af5df2783b..22095175acc633b2d051e53b51516806d4089dac 100644 (file)
@@ -2599,7 +2599,7 @@ void deinit(void)
                         * Close it and give the listener its real state.
                         */
                        if (p->state == PR_STSTOPPED && l->state >= LI_ZOMBIE) {
-                               close(l->fd);
+                               close(l->rx.fd);
                                l->state = LI_INIT;
                        }
                        unbind_listener(l);
index eac95e2da8e72efc026bd22a4b6e8b0be9168be4..b62d6fd9f49a447e2185ba1f98cd6daea42b9d20 100644 (file)
@@ -247,7 +247,7 @@ static void enable_listener(struct listener *listener)
                        }
                }
                else if (!listener->maxconn || listener->nbconn < listener->maxconn) {
-                       fd_want_recv(listener->fd);
+                       fd_want_recv(listener->rx.fd);
                        listener->state = LI_READY;
                }
                else {
@@ -273,7 +273,7 @@ static void disable_listener(struct listener *listener)
        if (listener->state < LI_READY)
                goto end;
        if (listener->state == LI_READY)
-               fd_stop_recv(listener->fd);
+               fd_stop_recv(listener->rx.fd);
        MT_LIST_DEL(&listener->wait_queue);
        listener->state = LI_LISTEN;
   end:
@@ -312,7 +312,7 @@ int pause_listener(struct listener *l)
 
        MT_LIST_DEL(&l->wait_queue);
 
-       fd_stop_recv(l->fd);
+       fd_stop_recv(l->rx.fd);
        l->state = LI_PAUSED;
   end:
        HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
@@ -368,7 +368,7 @@ int resume_listener(struct listener *l)
 
        if (l->proto->sock_prot == IPPROTO_TCP &&
            l->state == LI_PAUSED &&
-           listen(l->fd, listener_backlog(l)) != 0) {
+           listen(l->rx.fd, listener_backlog(l)) != 0) {
                ret = 0;
                goto end;
        }
@@ -392,7 +392,7 @@ int resume_listener(struct listener *l)
                goto end;
        }
 
-       fd_want_recv(l->fd);
+       fd_want_recv(l->rx.fd);
        l->state = LI_READY;
   end:
        HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
@@ -408,7 +408,7 @@ static void listener_full(struct listener *l)
        if (l->state >= LI_READY) {
                MT_LIST_DEL(&l->wait_queue);
                if (l->state != LI_FULL) {
-                       fd_stop_recv(l->fd);
+                       fd_stop_recv(l->rx.fd);
                        l->state = LI_FULL;
                }
        }
@@ -423,7 +423,7 @@ static void limit_listener(struct listener *l, struct mt_list *list)
        HA_SPIN_LOCK(LISTENER_LOCK, &l->lock);
        if (l->state == LI_READY) {
                MT_LIST_TRY_ADDQ(list, &l->wait_queue);
-               fd_stop_recv(l->fd);
+               fd_stop_recv(l->rx.fd);
                l->state = LI_LIMITED;
        }
        HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
@@ -496,16 +496,16 @@ void dequeue_proxy_listeners(struct proxy *px)
 void do_unbind_listener(struct listener *listener, int do_close)
 {
        if (listener->state == LI_READY && fd_updt)
-               fd_stop_recv(listener->fd);
+               fd_stop_recv(listener->rx.fd);
 
        MT_LIST_DEL(&listener->wait_queue);
 
        if (listener->state >= LI_PAUSED) {
                listener->state = LI_ASSIGNED;
-               fd_stop_both(listener->fd);
+               fd_stop_both(listener->rx.fd);
                if (do_close) {
-                       fd_delete(listener->fd);
-                       listener->fd = -1;
+                       fd_delete(listener->rx.fd);
+                       listener->rx.fd = -1;
                }
        }
 }
@@ -564,7 +564,7 @@ int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
                LIST_ADDQ(&bc->listeners, &l->by_bind);
                l->bind_conf = bc;
 
-               l->fd = fd;
+               l->rx.fd = fd;
                memcpy(&l->rx.addr, ss, sizeof(*ss));
                MT_LIST_INIT(&l->wait_queue);
                l->state = LI_INIT;
@@ -1059,7 +1059,7 @@ void listener_accept(int fd)
                else
                        fd_done_recv(fd);
        } else if (l->state > LI_ASSIGNED) {
-               fd_stop_recv(l->fd);
+               fd_stop_recv(l->rx.fd);
        }
        HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
        return;
index ff7c0a251222937e4333182245d00fbfb393646c..c73699511ae606f6f66314859451338ebd1cd207 100644 (file)
@@ -93,7 +93,7 @@ static void sockpair_add_listener(struct listener *listener, int port)
  */
 static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
 {
-       int fd = listener->fd;
+       int fd = listener->rx.fd;
        int err;
        const char *msg = NULL;
 
@@ -106,7 +106,7 @@ static int sockpair_bind_listener(struct listener *listener, char *errmsg, int e
        if (listener->state != LI_ASSIGNED)
                return ERR_NONE; /* already bound */
 
-       if (listener->fd == -1) {
+       if (listener->rx.fd == -1) {
                err |= ERR_FATAL | ERR_ALERT;
                msg = "sockpair can be only used with inherited FDs";
                goto err_return;
index a338ad83ca607a77786329e870ea1b771b2cffb8..a97cdc31c65441e6e4691bb2259dca28fe2e1be4 100644 (file)
@@ -569,15 +569,15 @@ int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
 
        err = ERR_NONE;
 
-       if (listener->fd == -1)
-               listener->fd = sock_find_compatible_fd(listener);
+       if (listener->rx.fd == -1)
+               listener->rx.fd = sock_find_compatible_fd(listener);
 
        /* if the listener already has an fd assigned, then we were offered the
         * fd by an external process (most likely the parent), and we don't want
         * to create a new socket. However we still want to set a few flags on
         * the socket.
         */
-       fd = listener->fd;
+       fd = listener->rx.fd;
        ext = (fd >= 0);
 
        if (!ext) {
@@ -765,7 +765,7 @@ int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
 #endif
 
        /* the socket is ready */
-       listener->fd = fd;
+       listener->rx.fd = fd;
        listener->state = LI_LISTEN;
 
        fd_insert(fd, listener, listener->proto->accept,
@@ -830,13 +830,13 @@ static void tcpv6_add_listener(struct listener *listener, int port)
  */
 int tcp_pause_listener(struct listener *l)
 {
-       if (shutdown(l->fd, SHUT_WR) != 0)
+       if (shutdown(l->rx.fd, SHUT_WR) != 0)
                return -1; /* Solaris dies here */
 
-       if (listen(l->fd, listener_backlog(l)) != 0)
+       if (listen(l->rx.fd, listener_backlog(l)) != 0)
                return -1; /* OpenBSD dies here */
 
-       if (shutdown(l->fd, SHUT_RD) != 0)
+       if (shutdown(l->rx.fd, SHUT_RD) != 0)
                return -1; /* should always be OK */
        return 1;
 }
index 7d190a17183bf49fcacc830d91a5ae874456e42c..38be905c59ab474e349e362030e50b0a2fc09487 100644 (file)
@@ -275,7 +275,7 @@ int udp_bind_listener(struct listener *listener, char *errmsg, int errlen)
        }
 
        /* the socket is ready */
-       listener->fd = fd;
+       listener->rx.fd = fd;
        listener->state = LI_LISTEN;
 
        if (listener->bind_conf->frontend->mode == PR_MODE_SYSLOG)
index 33768a1d5b0a5eb40ca5215c63d29d3a3e3da5cc..9b5a28d1f0f7e85d908dac3a639663c931257cbb 100644 (file)
@@ -109,9 +109,8 @@ static int uxst_bind_listener(struct listener *listener, char *errmsg, int errle
        if (listener->state != LI_ASSIGNED)
                return ERR_NONE; /* already bound */
                
-       if (listener->fd == -1)
-               listener->fd = sock_find_compatible_fd(listener);
-
+       if (listener->rx.fd == -1)
+               listener->rx.fd = sock_find_compatible_fd(listener);
        path = ((struct sockaddr_un *)&listener->rx.addr)->sun_path;
 
        maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
@@ -121,7 +120,7 @@ static int uxst_bind_listener(struct listener *listener, char *errmsg, int errle
         * to create a new socket. However we still want to set a few flags on
         * the socket.
         */
-       fd = listener->fd;
+       fd = listener->rx.fd;
        ext = (fd >= 0);
        if (ext)
                goto fd_ready;
@@ -259,7 +258,7 @@ static int uxst_bind_listener(struct listener *listener, char *errmsg, int errle
                unlink(backname);
 
        /* the socket is now listening */
-       listener->fd = fd;
+       listener->rx.fd = fd;
        listener->state = LI_LISTEN;
 
        fd_insert(fd, listener, listener->proto->accept,
index dda701cee46d45297683fd1c1fd52cfd3bfa4888..3f25daccb36c9dd1125787a5a57b73cd62b04cf2 100644 (file)
@@ -1245,7 +1245,7 @@ void soft_stop(void)
                        struct listener *l;
                        list_for_each_entry(l, &p->conf.listeners, by_fe) {
                                if (l->state > LI_ASSIGNED)
-                                       close(l->fd);
+                                       close(l->rx.fd);
                                l->state = LI_INIT;
                        }
                }