Skip to content

Commit ef357e8

Browse files
committed
connectd: report ping latencies (from ping probes) to lightningd.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent bdbb97c commit ef357e8

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

connectd/connectd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,7 @@ static struct io_plan *recv_req(struct io_conn *conn,
24192419
case WIRE_CONNECTD_START_SHUTDOWN_REPLY:
24202420
case WIRE_CONNECTD_INJECT_ONIONMSG_REPLY:
24212421
case WIRE_CONNECTD_ONIONMSG_FORWARD_FAIL:
2422+
case WIRE_CONNECTD_PING_LATENCY:
24222423
break;
24232424
}
24242425

connectd/connectd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ struct peer {
9696
enum pong_expect_type expecting_pong;
9797
u64 ping_reqid;
9898

99+
/* Timestamp when we initially sent probe ping */
100+
struct timemono ping_start;
101+
99102
/* Random ping timer, to detect dead connections. */
100103
struct oneshot *ping_timer;
101104

connectd/connectd_wire.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ msgdata,connectd_ping_done,sent,bool,
156156
# 0 == no pong expected, otherwise length of pong.
157157
msgdata,connectd_ping_done,totlen,u16,
158158

159+
# We give lightningd stats about ping latencies
160+
msgtype,connectd_ping_latency,2038
161+
msgdata,connectd_ping_latency,id,node_id,
162+
msgdata,connectd_ping_latency,ping_nsec,u64,
163+
159164
# We tell lightningd we got an onionmsg
160165
msgtype,connectd_got_onionmsg_to_us,2145
161166
msgdata,connectd_got_onionmsg_to_us,path_secret,?secret,

connectd/multiplex.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ static void send_ping(struct peer *peer)
645645
}
646646

647647
inject_peer_msg(peer, take(make_ping(NULL, 1, 0)));
648+
peer->ping_start = time_mono();
648649
peer->expecting_pong = PONG_EXPECTED_PROBING;
649650
}
650651

@@ -719,6 +720,10 @@ static void handle_pong_in(struct peer *peer, const u8 *msg)
719720
/* fall thru */
720721
case PONG_EXPECTED_PROBING:
721722
peer->expecting_pong = PONG_UNEXPECTED;
723+
daemon_conn_send(peer->daemon->master,
724+
take(towire_connectd_ping_latency(NULL,
725+
&peer->id,
726+
time_to_nsec(timemono_since(peer->ping_start)))));
722727
return;
723728
case PONG_UNEXPECTED:
724729
status_debug("Unexpected pong?");

lightningd/connect_control.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,21 @@ void tell_connectd_peer_importance(struct peer *peer,
472472
}
473473
}
474474

475+
static void handle_ping_latency(struct lightningd *ld, const u8 *msg)
476+
{
477+
struct node_id id;
478+
u64 nsec;
479+
480+
if (!fromwire_connectd_ping_latency(msg, &id, &nsec)) {
481+
log_broken(ld->log, "Malformed ping_latency: %s",
482+
tal_hex(tmpctx, msg));
483+
return;
484+
}
485+
486+
log_peer_trace(ld->log, &id, "Ping latency: %"PRIu64"nsec",
487+
nsec);
488+
}
489+
475490
static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fds)
476491
{
477492
enum connectd_wire t = fromwire_peektype(msg);
@@ -537,6 +552,10 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd
537552
case WIRE_CONNECTD_PING_DONE:
538553
handle_ping_done(connectd, msg);
539554
break;
555+
556+
case WIRE_CONNECTD_PING_LATENCY:
557+
handle_ping_latency(connectd->ld, msg);
558+
break;
540559
}
541560
return 0;
542561
}

0 commit comments

Comments
 (0)