[master] 42e1ca3b4 vca: Pass higher-level types to sock_opt functions

Nils Goroll nils.goroll at uplex.de
Wed Nov 17 11:07:06 UTC 2021


commit 42e1ca3b497558dd51ce5952254bfa2627054e18
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Mon Sep 27 08:19:09 2021 +0200

    vca: Pass higher-level types to sock_opt functions
    
    The sess and listen_sock structs contain everything we need to find or
    infer the former `sock`, `uds` and `force` arguments. In particular, it
    helps distinguish between working on a connection vs listen socket.

diff --git a/bin/varnishd/cache/cache_acceptor.c b/bin/varnishd/cache/cache_acceptor.c
index ff988feb9..fe1b3071a 100644
--- a/bin/varnishd/cache/cache_acceptor.c
+++ b/bin/varnishd/cache/cache_acceptor.c
@@ -210,22 +210,25 @@ vca_sock_opt_init(void)
 }
 
 static void
-vca_sock_opt_test(const int sock, const unsigned uds)
+vca_sock_opt_test(const struct listen_sock *ls, const struct sess *sp)
 {
 	int i, n;
 	struct sock_opt *so;
 	socklen_t l;
 	void *ptr;
 
+	CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC);
+	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
+
 	for (n = 0; n < n_sock_opts; n++) {
 		so = &sock_opts[n];
-		if (so->level == IPPROTO_TCP && uds)
+		if (so->level == IPPROTO_TCP && ls->uds)
 			continue;
 		so->need = 1;
 		ptr = calloc(1, so->sz);
 		AN(ptr);
 		l = so->sz;
-		i = getsockopt(sock, so->level, so->optname, ptr, &l);
+		i = getsockopt(sp->fd, so->level, so->optname, ptr, &l);
 		if (i == 0 && !memcmp(ptr, so->ptr, so->sz))
 			so->need = 0;
 		free(ptr);
@@ -235,16 +238,20 @@ vca_sock_opt_test(const int sock, const unsigned uds)
 }
 
 static void
-vca_sock_opt_set(const int sock, const unsigned uds, const int force)
+vca_sock_opt_set(const struct listen_sock *ls, const struct sess *sp)
 {
-	int n;
+	int n, sock;
 	struct sock_opt *so;
 
+	CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC);
+	CHECK_OBJ_ORNULL(sp, SESS_MAGIC);
+	sock = sp != NULL ? sp->fd : ls->sock;
+
 	for (n = 0; n < n_sock_opts; n++) {
 		so = &sock_opts[n];
-		if (so->level == IPPROTO_TCP && uds)
+		if (so->level == IPPROTO_TCP && ls->uds)
 			continue;
-		if (so->need || force) {
+		if (so->need || sp == NULL) {
 			VTCP_Assert(setsockopt(sock,
 			    so->level, so->optname, so->ptr, so->sz));
 		}
@@ -394,10 +401,10 @@ vca_make_session(struct worker *wrk, void *arg)
 	wrk->stats->sess_conn++;
 
 	if (need_test) {
-		vca_sock_opt_test(sp->fd, wa->acceptlsock->uds);
+		vca_sock_opt_test(wa->acceptlsock, sp);
 		need_test = 0;
 	}
-	vca_sock_opt_set(sp->fd, wa->acceptlsock->uds, 0);
+	vca_sock_opt_set(wa->acceptlsock, sp);
 
 	req = Req_New(sp);
 	CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
@@ -589,7 +596,7 @@ vca_acct(void *arg)
 				if (ls->sock == -2)
 					continue;	// VCA_Shutdown
 				assert (ls->sock > 0);
-				vca_sock_opt_set(ls->sock, ls->uds, 1);
+				vca_sock_opt_set(ls, NULL);
 			}
 			AZ(pthread_mutex_unlock(&shut_mtx));
 		}
@@ -625,7 +632,7 @@ ccf_start(struct cli *cli, const char * const *av, void *priv)
 			    ls->endpoint, VAS_errtxt(errno));
 			return;
 		}
-		vca_sock_opt_set(ls->sock, ls->uds, 1);
+		vca_sock_opt_set(ls, NULL);
 		if (cache_param->accept_filter && VTCP_filter_http(ls->sock))
 			VSL(SLT_Error, 0,
 			    "Kernel filtering: sock=%d, errno=%d %s",


More information about the varnish-commit mailing list