Skip to contentSkip to conversation

net/ceph/messenger: print TCP connect error

PATCH In review Max Kellermann 4 participants 5 messages Sep 10 – Nov 5 Ceph common code (libceph)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/ceph/messenger: print TCP connect error

A non-blocking connect() usually returns -EINPROGRESS and fails later, asynchronously; the actual error is then only visible as sk->sk_err in the sk_state_change() callback and was silently dropped. All the user got was:

libceph: mon4 (1)192.168.1.42:6789 socket closed (con state V1_BANNER)

Save the error in the new field ceph_connection.error_code and let con_fault() print it next to error_msg:

libceph: mon4 (1)192.168.1.42:6789 socket closed (con state V1_BANNER) (-ECONNREFUSED)

---
 include/linux/ceph/messenger.h |  1 +
 net/ceph/messenger.c           | 23 ++++++++++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)
include/linux/ceph/messenger.h +1 -0
index 6aa4c6478c9f..f3aa977d7886 100644
@@ -482,6 +482,7 @@ struct ceph_connection {
 
 	unsigned long flags;  /* CEPH_CON_F_* */
 	const char *error_msg;  /* error message, if any */
+	int error_code;  /* errno (negative), if any */
 
 	struct ceph_entity_name peer_name; /* peer name */
 	struct ceph_entity_addr peer_addr; /* peer address */
net/ceph/messenger.c +18 -5
index 212e7797f9e4..f989e043686d 100644
@@ -398,6 +398,7 @@ static void ceph_sock_state_change(struct sock *sk)
 	case TCP_CLOSE_WAIT:
 		dout("%s TCP_CLOSE_WAIT\n", __func__);
 		con_sock_state_closing(con);
+		WRITE_ONCE(con->error_code, -READ_ONCE(sk->sk_err));
 		ceph_con_flag_set(con, CEPH_CON_F_SOCK_CLOSED);
 		queue_con(con);
 		break;
@@ -448,8 +449,10 @@ int ceph_tcp_connect(struct ceph_connection *con)
 	ret = sock_create_kern(read_pnet(&con->msgr->net), ss.ss_family,
 			       SOCK_STREAM, IPPROTO_TCP, &sock);
 	memalloc_noio_restore(noio_flag);
-	if (ret)
+	if (ret) {
+		con->error_code = ret;
 		return ret;
+	}
 	sock->sk->sk_allocation = GFP_NOFS;
 	sock->sk->sk_use_task_frag = false;
 
@@ -467,9 +470,9 @@ int ceph_tcp_connect(struct ceph_connection *con)
 		     ceph_pr_addr(&con->peer_addr),
 		     sock->sk->sk_state);
 	} else if (ret < 0) {
-		pr_err("connect %s error %d\n",
-		       ceph_pr_addr(&con->peer_addr), ret);
 		sock_release(sock);
+		/* reported by con_fault() */
+		con->error_code = ret;
 		return ret;
 	}
 
@@ -501,6 +504,7 @@ int ceph_con_close_socket(struct ceph_connection *con)
 	 * shut the socket down.
 	 */
 	ceph_con_flag_clear(con, CEPH_CON_F_SOCK_CLOSED);
+	con->error_code = 0;
 
 	con_sock_state_closed(con);
 	return rc;
@@ -1613,12 +1617,21 @@ static void ceph_con_workfn(struct work_struct *work)
  */
 static void con_fault(struct ceph_connection *con)
 {
+	int error_code;
+
 	dout("fault %p state %d to peer %s\n",
 	     con, con->state, ceph_pr_addr(&con->peer_addr));
 
-	pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
-		ceph_pr_addr(&con->peer_addr), con->error_msg);
+	error_code = READ_ONCE(con->error_code);
+	if (error_code)
+		pr_warn("%s%lld %s %s (%pe)\n", ENTITY_NAME(con->peer_name),
+			ceph_pr_addr(&con->peer_addr), con->error_msg,
+			ERR_PTR(error_code));
+	else
+		pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
+			ceph_pr_addr(&con->peer_addr), con->error_msg);
 	con->error_msg = NULL;
+	con->error_code = 0;
 
 	WARN_ON(con->state == CEPH_CON_S_STANDBY ||
 		con->state == CEPH_CON_S_CLOSED);
Signature · 1 line
2.47.3
maintainer
#2 lore raw
In reply to Max Kellermann
Quoted 10 lines · Max Kellermann

A non-blocking connect() usually returns -EINPROGRESS and fails later, asynchronously; the actual error is then only visible as sk->sk_err in the sk_state_change() callback and was silently dropped. All the user got was:

libceph: mon4 (1)192.168.1.42:6789 socket closed (con state V1_BANNER)

Save the error in the new field ceph_connection.error_code and let con_fault() print it next to error_msg:

Hi Max,

I wonder if saving the error that way is actually needed. Can con_fault() just reap it with sock_error() at the time it runs?

Thanks,

Ilya

Quoted 92 lines

libceph: mon4 (1)192.168.1.42:6789 socket closed (con state V1_BANNER) (-ECONNREFUSED)

---
 include/linux/ceph/messenger.h |  1 +
 net/ceph/messenger.c           | 23 ++++++++++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)
include/linux/ceph/messenger.h +1 -0
index 6aa4c6478c9f..f3aa977d7886 100644
@@ -482,6 +482,7 @@ struct ceph_connection {
 
        unsigned long flags;  /* CEPH_CON_F_* */
        const char *error_msg;  /* error message, if any */
+       int error_code;  /* errno (negative), if any */
 
        struct ceph_entity_name peer_name; /* peer name */
        struct ceph_entity_addr peer_addr; /* peer address */
net/ceph/messenger.c +18 -6
index 212e7797f9e4..f989e043686d 100644
@@ -398,6 +398,7 @@ static void ceph_sock_state_change(struct sock *sk)
        case TCP_CLOSE_WAIT:
                dout("%s TCP_CLOSE_WAIT\n", __func__);
                con_sock_state_closing(con);
+               WRITE_ONCE(con->error_code, -READ_ONCE(sk->sk_err));
                ceph_con_flag_set(con, CEPH_CON_F_SOCK_CLOSED);
                queue_con(con);
                break;
@@ -448,8 +449,10 @@ int ceph_tcp_connect(struct ceph_connection *con)
        ret = sock_create_kern(read_pnet(&con->msgr->net), ss.ss_family,
                               SOCK_STREAM, IPPROTO_TCP, &sock);
        memalloc_noio_restore(noio_flag);
-       if (ret)
+       if (ret) {
+               con->error_code = ret;
                return ret;
+       }
        sock->sk->sk_allocation = GFP_NOFS;
        sock->sk->sk_use_task_frag = false;
 
@@ -467,9 +470,9 @@ int ceph_tcp_connect(struct ceph_connection *con)
                     ceph_pr_addr(&con->peer_addr),
                     sock->sk->sk_state);
        } else if (ret < 0) {
-               pr_err("connect %s error %d\n",
-                      ceph_pr_addr(&con->peer_addr), ret);
                sock_release(sock);
+               /* reported by con_fault() */
+               con->error_code = ret;
                return ret;
        }
 
@@ -501,6 +504,7 @@ int ceph_con_close_socket(struct ceph_connection *con)
         * shut the socket down.
         */
        ceph_con_flag_clear(con, CEPH_CON_F_SOCK_CLOSED);
+       con->error_code = 0;
 
        con_sock_state_closed(con);
        return rc;
@@ -1613,12 +1617,21 @@ static void ceph_con_workfn(struct work_struct *work)
  */
 static void con_fault(struct ceph_connection *con)
 {
+       int error_code;
+
        dout("fault %p state %d to peer %s\n",
             con, con->state, ceph_pr_addr(&con->peer_addr));
 
-       pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
-               ceph_pr_addr(&con->peer_addr), con->error_msg);
+       error_code = READ_ONCE(con->error_code);
+       if (error_code)
+               pr_warn("%s%lld %s %s (%pe)\n", ENTITY_NAME(con->peer_name),
+                       ceph_pr_addr(&con->peer_addr), con->error_msg,
+                       ERR_PTR(error_code));
+       else
+               pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
+                       ceph_pr_addr(&con->peer_addr), con->error_msg);
        con->error_msg = NULL;
+       con->error_code = 0;
 
        WARN_ON(con->state == CEPH_CON_S_STANDBY ||
                con->state == CEPH_CON_S_CLOSED);
--

2.47.3

#3 lore raw
Quoted 2 lines · Ilya Dryomov

I wonder if saving the error that way is actually needed. Can con_fault() just reap it with sock_error() at the time it runs?

Maybe that would be possible. I wasn't sure if sk_err (from ceph_sock_state_change()) would remain untouched until con_fault() was eventually called, or if it somebody would clear it in the meantime.

Signature · 8 lines
Max Kellermann
Principal Architect
Hosting Technology

cm4all | Im Mediapark 6a | 50670 Köln | Germany
General information about the company can be found here:
https://www.cm4all.com/impressum
A member of the IONOS Group
Reviewed-by
#4 lore raw
Quoted 7 lines · Max Kellermann
Quoted 2 lines · Ilya Dryomov

I wonder if saving the error that way is actually needed. Can con_fault() just reap it with sock_error() at the time it runs?

Maybe that would be possible. I wasn't sure if sk_err (from ceph_sock_state_change()) would remain untouched until con_fault() was eventually called, or if it somebody would clear it in the meantime.

It does get cleared, by ceph_con_workfn() itself. SOCK_CLOSED is only checked at the top of the loop, so if the error arrives after that check, the ceph_tcp_sendmsg()/recvmsg() in the same pass reaps sk_err via sock_error() and con_fault() later sees 0.

I tried both on a 7.3-rc1 kernel. Reaping in con_fault() works for the remote ECONNREFUSED and ECONNRESET cases, but with a refused connect on loopback (ms_mode=legacy and crc) the error was lost every time:

state_change sk_state 7 sk_err 111 try_write ret -111, sk_err 0
con_fault: sock_error() = 0
libceph: mon0 (1)127.0.0.1:1 socket error on write

With this patch the same case prints "socket error on write (-ECONNREFUSED)". Using the try_read/try_write return value instead doesn't work well either, because msgr1 returns -1 for protocol errors. So saving the error in ceph_sock_state_change() looks like the simpler option to me.

A normal FIN-close prints no error code, and a synchronous connect failure prints "connect error (-ENETUNREACH)".

Nit: msgr2 still prints "ceph_tcp_connect failed: %d" in prepare_read_banner_prefix()'s caller, which is now redundant.

Thanks
- Xiubo

Quoted 19 lines · Max Kellermann <[email protected]> 于2026年11月3日周二 08:06写道:
Quoted 2 lines · Ilya Dryomov

I wonder if saving the error that way is actually needed. Can con_fault() just reap it with sock_error() at the time it runs?

Maybe that would be possible. I wasn't sure if sk_err (from ceph_sock_state_change()) would remain untouched until con_fault() was eventually called, or if it somebody would clear it in the meantime.

--
Max Kellermann
Principal Architect
Hosting Technology

cm4all | Im Mediapark 6a | 50670 Köln | Germany General information about the company can be found here:
https://www.cm4all.com/impressum
A member of the IONOS Group