connectd: report ping latencies (from ping probes) to lightningd.

(Uninitialize ping_start on manual ping fixed by Alex Myers)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2025-11-11 11:33:22 +10:30
parent 0f07578c3f
commit 88b9b0bc28
5 changed files with 34 additions and 0 deletions

View File

@@ -2420,6 +2420,7 @@ static struct io_plan *recv_req(struct io_conn *conn,
case WIRE_CONNECTD_START_SHUTDOWN_REPLY:
case WIRE_CONNECTD_INJECT_ONIONMSG_REPLY:
case WIRE_CONNECTD_ONIONMSG_FORWARD_FAIL:
case WIRE_CONNECTD_PING_LATENCY:
break;
}

View File

@@ -96,6 +96,9 @@ struct peer {
enum pong_expect_type expecting_pong;
u64 ping_reqid;
/* Timestamp when we initially sent probe ping */
struct timemono ping_start;
/* Random ping timer, to detect dead connections. */
struct oneshot *ping_timer;

View File

@@ -156,6 +156,11 @@ msgdata,connectd_ping_done,sent,bool,
# 0 == no pong expected, otherwise length of pong.
msgdata,connectd_ping_done,totlen,u16,
# We give lightningd stats about ping latencies
msgtype,connectd_ping_latency,2038
msgdata,connectd_ping_latency,id,node_id,
msgdata,connectd_ping_latency,ping_nsec,u64,
# We tell lightningd we got an onionmsg
msgtype,connectd_got_onionmsg_to_us,2145
msgdata,connectd_got_onionmsg_to_us,path_secret,?secret,
1 #include <common/channel_id.h>
156 msgdata,connectd_inject_onionmsg_reply,err,wirestring, msgdata,connectd_inject_onionmsg,onion_len,u16,
157 # A custom message that we got from a peer and don't know how to handle, so we msgdata,connectd_inject_onionmsg,onion,u8,onion_len
158 # forward it to the master for further handling. # Reply. If error isn't empty, something went wrong.
159 msgtype,connectd_inject_onionmsg_reply,2142
160 msgdata,connectd_inject_onionmsg_reply,err,wirestring,
161 # A custom message that we got from a peer and don't know how to handle, so we
162 # forward it to the master for further handling.
163 msgtype,connectd_custommsg_in,2110
164 msgtype,connectd_custommsg_in,2110 msgdata,connectd_custommsg_in,id,node_id,
165 msgdata,connectd_custommsg_in,id,node_id, msgdata,connectd_custommsg_in,msg_len,u16,
166 msgdata,connectd_custommsg_in,msg_len,u16, msgdata,connectd_custommsg_in,msg,u8,msg_len

View File

@@ -645,6 +645,7 @@ static void send_ping(struct peer *peer)
}
inject_peer_msg(peer, take(make_ping(NULL, 1, 0)));
peer->ping_start = time_mono();
peer->expecting_pong = PONG_EXPECTED_PROBING;
}
@@ -719,6 +720,10 @@ static void handle_pong_in(struct peer *peer, const u8 *msg)
/* fall thru */
case PONG_EXPECTED_PROBING:
peer->expecting_pong = PONG_UNEXPECTED;
daemon_conn_send(peer->daemon->master,
take(towire_connectd_ping_latency(NULL,
&peer->id,
time_to_nsec(timemono_since(peer->ping_start)))));
return;
case PONG_UNEXPECTED:
status_debug("Unexpected pong?");
@@ -1532,6 +1537,7 @@ void send_manual_ping(struct daemon *daemon, const u8 *msg)
if (tal_count(ping) > 65535)
status_failed(STATUS_FAIL_MASTER_IO, "Oversize ping");
peer->ping_start = time_mono();
inject_peer_msg(peer, take(ping));
status_debug("sending ping expecting %sresponse",

View File

@@ -472,6 +472,21 @@ void tell_connectd_peer_importance(struct peer *peer,
}
}
static void handle_ping_latency(struct lightningd *ld, const u8 *msg)
{
struct node_id id;
u64 nsec;
if (!fromwire_connectd_ping_latency(msg, &id, &nsec)) {
log_broken(ld->log, "Malformed ping_latency: %s",
tal_hex(tmpctx, msg));
return;
}
log_peer_trace(ld->log, &id, "Ping latency: %"PRIu64"nsec",
nsec);
}
static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fds)
{
enum connectd_wire t = fromwire_peektype(msg);
@@ -537,6 +552,10 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd
case WIRE_CONNECTD_PING_DONE:
handle_ping_done(connectd, msg);
break;
case WIRE_CONNECTD_PING_LATENCY:
handle_ping_latency(connectd->ld, msg);
break;
}
return 0;
}