Skip to content

Commit

Permalink
PTP: Refine timestamp printing.
Browse files Browse the repository at this point in the history
Move nanosecond-specific constants from print-arista.c to netdissect.h.
In ptp_print_timestamp_identity() and ptp_print_announce_msg() use
ptp_print_timestamp() to deduplicate code.  In ptp_print_timestamp()
change the format to SECONDS.NANOSECONDS and a bogus nanoseconds value
indicator.  Update the tests.  This resolves GH issue the-tcpdump-group#1260.
  • Loading branch information
infrastation committed Jan 9, 2025
1 parent 6cd500c commit 2386101
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 284 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group
the subrule is present (inspird by the OpenBSD tcpdump).
pflog: Handle OpenBSD's "rewritten" flag and addresses (adapted
from the OpenBSD tcpdump).
PTP: Refine timestamp printing.
SLL2: Translate interface indices to names on Linux only.
RT6: Add a bounds check for the PadN TLV in Segment Routing Header.
TCP: Add support for the AE (AccECN) flag.
Expand Down
3 changes: 3 additions & 0 deletions netdissect.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ extern int unaligned_memcmp(const void *, const void *, size_t);
#define PLURAL_SUFFIX(n) \
(((n) != 1) ? "s" : "")

#define MAX_VALID_NS 999999999U
#define BOGUS_NS_STR "(bogus ns!)"

extern const char *tok2strary_internal(const char **, int, const char *, const int);
#define tok2strary(a,f,i) tok2strary_internal(a, sizeof(a)/sizeof(a[0]),f,i)

Expand Down
3 changes: 0 additions & 3 deletions print-arista.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ static const struct tok hw_info_str[] = {
{ 0, NULL }
};

#define MAX_VALID_NS 999999999U
#define BOGUS_NS_STR "(bogus ns!)"

static inline void
arista_print_date_hms_time(netdissect_options *ndo, const uint32_t seconds,
const uint32_t nanoseconds)
Expand Down
41 changes: 11 additions & 30 deletions print-ptp.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ static const struct tok ptp_control_field[] = {
#define PTP_FALSE !PTP_TRUE

#define PTP_HDR_LEN 0x22
#define PTP_TIMESTAMP_LEN 10U

/* mask based on the first byte */
#define PTP_MAJOR_VERS_MASK 0x0F
Expand Down Expand Up @@ -495,37 +496,28 @@ ptp_print_timestamp(netdissect_options *ndo, const u_char *bp, u_int *len, const
ND_PRINT(", %s:", stype);
/* sec time stamp 6 bytes */
secs = GET_BE_U_6(bp);
ND_PRINT(" %"PRIu64" seconds,", secs);
ND_PRINT(" %"PRIu64".", secs);
*len -= 6;
bp += 6;

/* NS time stamp 4 bytes */
nsecs = GET_BE_U_4(bp);
ND_PRINT(" %u nanoseconds", nsecs);
ND_PRINT("%09u seconds", nsecs);
if (nsecs > MAX_VALID_NS)
ND_PRINT(" %s", BOGUS_NS_STR);
*len -= 4;
bp += 4;
}
static void
ptp_print_timestamp_identity(netdissect_options *ndo,
const u_char *bp, u_int *len, const char *ttype)
{
uint64_t secs;
uint32_t nsecs;
uint16_t port_id;
uint64_t port_identity;

ND_PRINT(", %s:", ttype);
/* sec time stamp 6 bytes */
secs = GET_BE_U_6(bp);
ND_PRINT(" %"PRIu64" seconds,", secs);
*len -= 6;
bp += 6;

/* NS time stamp 4 bytes */
nsecs = GET_BE_U_4(bp);
ND_PRINT(" %u nanoseconds", nsecs);
*len -= 4;
bp += 4;
ptp_print_timestamp(ndo, bp, len, ttype);
*len -= PTP_TIMESTAMP_LEN;
bp += PTP_TIMESTAMP_LEN;

/* port identity*/
port_identity = GET_BE_U_8(bp);
Expand All @@ -545,21 +537,10 @@ ptp_print_announce_msg(netdissect_options *ndo, const u_char *bp, u_int *len)
uint8_t rsvd, gm_prio_1, gm_prio_2, gm_clk_cls, gm_clk_acc, time_src;
uint16_t origin_cur_utc, gm_clk_var, steps_removed;
uint64_t gm_clock_id;
uint64_t secs;
uint32_t nsecs;

ND_PRINT(", %s:", p_origin_ts);
/* sec time stamp 6 bytes */
secs = GET_BE_U_6(bp);
ND_PRINT(" %"PRIu64" seconds", secs);
*len -= 6;
bp += 6;

/* NS time stamp 4 bytes */
nsecs = GET_BE_U_4(bp);
ND_PRINT(" %u nanoseconds", nsecs);
*len -= 4;
bp += 4;
ptp_print_timestamp(ndo, bp, len, p_origin_ts);
*len -= PTP_TIMESTAMP_LEN;
bp += PTP_TIMESTAMP_LEN;

/* origin cur utc */
origin_cur_utc = GET_BE_U_2(bp);
Expand Down
10 changes: 5 additions & 5 deletions tests/ptp.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1 2018-01-23 19:44:09.248292 IP 11.0.0.110.319 > 224.0.1.129.319: PTPv2, majorSdoId: 0x0, msg type: delay req msg, length: 44, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x7cfe90fffef950b4, port id: 1, seq id: 132, control: 1 (Delay_Req), log message interval: 127, originTimeStamp: 0 seconds, 0 nanoseconds
2 2018-01-23 19:44:09.248437 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: delay resp msg, length: 54, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 132, control: 3 (Delay_Resp), log message interval: 0, receiveTimeStamp: 1516736649 seconds, 248292005 nanoseconds, port identity: 0x7cfe90fffef950b4, port id: 1
3 2018-01-23 19:44:09.982883 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: announce msg, length: 64, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 534, control: 5 (Other), log message interval: 1, originTimeStamp: 0 seconds 0 nanoseconds, origin cur utc:36, rsvd: 0, gm priority_1: 128, gm clock class: 248, gm clock accuracy: 254, gm clock variance: 65535, gm priority_2: 128, gm clock id: 0x200fffe000001, steps removed: 0, time source: 0xa0
4 2018-01-23 19:44:10.034745 IP 11.0.0.9.319 > 224.0.1.129.319: PTPv2, majorSdoId: 0x0, msg type: sync msg, length: 44, domain: 0, reserved1: 0, Flags [two step], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 1067, control: 0 (Sync), log message interval: 0, originTimeStamp: 0 seconds, 0 nanoseconds
5 2018-01-23 19:44:10.034796 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: follow up msg, length: 44, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 1067, control: 2 (Follow_Up), log message interval: 0, preciseOriginTimeStamp: 1516736650 seconds, 34751783 nanoseconds
1 2018-01-23 19:44:09.248292 IP 11.0.0.110.319 > 224.0.1.129.319: PTPv2, majorSdoId: 0x0, msg type: delay req msg, length: 44, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x7cfe90fffef950b4, port id: 1, seq id: 132, control: 1 (Delay_Req), log message interval: 127, originTimeStamp: 0.000000000 seconds
2 2018-01-23 19:44:09.248437 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: delay resp msg, length: 54, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 132, control: 3 (Delay_Resp), log message interval: 0, receiveTimeStamp: 1516736649.248292005 seconds, port identity: 0x7cfe90fffef950b4, port id: 1
3 2018-01-23 19:44:09.982883 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: announce msg, length: 64, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 534, control: 5 (Other), log message interval: 1, originTimeStamp: 0.000000000 seconds, origin cur utc:36, rsvd: 0, gm priority_1: 128, gm clock class: 248, gm clock accuracy: 254, gm clock variance: 65535, gm priority_2: 128, gm clock id: 0x200fffe000001, steps removed: 0, time source: 0xa0
4 2018-01-23 19:44:10.034745 IP 11.0.0.9.319 > 224.0.1.129.319: PTPv2, majorSdoId: 0x0, msg type: sync msg, length: 44, domain: 0, reserved1: 0, Flags [two step], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 1067, control: 0 (Sync), log message interval: 0, originTimeStamp: 0.000000000 seconds
5 2018-01-23 19:44:10.034796 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: follow up msg, length: 44, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 1067, control: 2 (Follow_Up), log message interval: 0, preciseOriginTimeStamp: 1516736650.034751783 seconds
6 changes: 3 additions & 3 deletions tests/ptp_corrections.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
1 2022-10-11 17:52:26.679146 IP 4.5.0.2.319 > 2.2.2.2.319: PTPv2, majorSdoId: 0x0, msg type: delay req msg, length: 44, domain: 44, reserved1: 0, Flags [unicast], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0xa0369ffffe856e8a, port id: 1, seq id: 1203, control: 1 (Delay_Req), log message interval: 127, originTimeStamp: 0 seconds, 0 nanoseconds
2 2022-10-11 17:52:26.679265 IP 2.2.2.2.320 > 4.5.0.2.320: PTPv2, majorSdoId: 0x0, msg type: delay resp msg, length: 54, domain: 44, reserved1: 0, Flags [unicast], NS correction: 36035, sub NS correction: 0, reserved2: 0, clock identity: 0xe8c57affff01313f, port id: 3, seq id: 1203, control: 3 (Delay_Resp), log message interval: 127, receiveTimeStamp: 1665510783 seconds, 679015501 nanoseconds, port identity: 0xa0369ffffe856e8a, port id: 1
3 2022-10-11 17:52:26.682034 IP 2.2.2.2.319 > 4.5.0.2.319: PTPv2, majorSdoId: 0x0, msg type: sync msg, length: 44, domain: 44, reserved1: 0, Flags [unicast], NS correction: 105045, sub NS correction: 0, reserved2: 0, clock identity: 0xe8c57affff01313f, port id: 3, seq id: 1213, control: 0 (Sync), log message interval: 127, originTimeStamp: 1665510783 seconds, 681548698 nanoseconds
1 2022-10-11 17:52:26.679146 IP 4.5.0.2.319 > 2.2.2.2.319: PTPv2, majorSdoId: 0x0, msg type: delay req msg, length: 44, domain: 44, reserved1: 0, Flags [unicast], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0xa0369ffffe856e8a, port id: 1, seq id: 1203, control: 1 (Delay_Req), log message interval: 127, originTimeStamp: 0.000000000 seconds
2 2022-10-11 17:52:26.679265 IP 2.2.2.2.320 > 4.5.0.2.320: PTPv2, majorSdoId: 0x0, msg type: delay resp msg, length: 54, domain: 44, reserved1: 0, Flags [unicast], NS correction: 36035, sub NS correction: 0, reserved2: 0, clock identity: 0xe8c57affff01313f, port id: 3, seq id: 1203, control: 3 (Delay_Resp), log message interval: 127, receiveTimeStamp: 1665510783.679015501 seconds, port identity: 0xa0369ffffe856e8a, port id: 1
3 2022-10-11 17:52:26.682034 IP 2.2.2.2.319 > 4.5.0.2.319: PTPv2, majorSdoId: 0x0, msg type: sync msg, length: 44, domain: 44, reserved1: 0, Flags [unicast], NS correction: 105045, sub NS correction: 0, reserved2: 0, clock identity: 0xe8c57affff01313f, port id: 3, seq id: 1213, control: 0 (Sync), log message interval: 127, originTimeStamp: 1665510783.681548698 seconds
Loading

0 comments on commit 2386101

Please sign in to comment.