1 // SPDX-License-Identifier: GPL-2.0-only
3 * This program demonstrates how the various time stamping features in
4 * the Linux kernel work. It emulates the behavior of a PTP
5 * implementation in stand-alone master mode by sending PTPv1 Sync
6 * multicasts once every second. It looks for similar packets, but
7 * beyond that doesn't actually implement PTP.
9 * Outgoing packets are time stamped with SO_TIMESTAMPING with or
10 * without hardware support.
12 * Incoming packets are time stamped with SO_TIMESTAMPING with or
13 * without hardware support, SIOCGSTAMP[NS] (per-socket time stamp) and
16 * Copyright (C) 2009 Intel Corporation.
17 * Author: Patrick Ohly <patrick.ohly@intel.com>
26 #include <sys/socket.h>
27 #include <sys/select.h>
28 #include <sys/ioctl.h>
29 #include <arpa/inet.h>
32 #include <asm/types.h>
33 #include <linux/net_tstamp.h>
34 #include <linux/errqueue.h>
35 #include <linux/sockios.h>
37 #ifndef SO_TIMESTAMPING
38 # define SO_TIMESTAMPING 37
39 # define SCM_TIMESTAMPING SO_TIMESTAMPING
42 #ifndef SO_TIMESTAMPNS
43 # define SO_TIMESTAMPNS 35
46 static void usage(const char *error
)
49 printf("invalid option: %s\n", error
);
50 printf("timestamping interface option*\n\n"
52 " IP_MULTICAST_LOOP - looping outgoing multicasts\n"
53 " SO_TIMESTAMP - normal software time stamping, ms resolution\n"
54 " SO_TIMESTAMPNS - more accurate software time stamping\n"
55 " SOF_TIMESTAMPING_TX_HARDWARE - hardware time stamping of outgoing packets\n"
56 " SOF_TIMESTAMPING_TX_SOFTWARE - software fallback for outgoing packets\n"
57 " SOF_TIMESTAMPING_RX_HARDWARE - hardware time stamping of incoming packets\n"
58 " SOF_TIMESTAMPING_RX_SOFTWARE - software fallback for incoming packets\n"
59 " SOF_TIMESTAMPING_SOFTWARE - request reporting of software time stamps\n"
60 " SOF_TIMESTAMPING_RAW_HARDWARE - request reporting of raw HW time stamps\n"
61 " SIOCGSTAMP - check last socket time stamp\n"
62 " SIOCGSTAMPNS - more accurate socket time stamp\n"
63 " PTPV2 - use PTPv2 messages\n");
67 static void bail(const char *error
)
69 printf("%s: %s\n", error
, strerror(errno
));
73 static const unsigned char sync
[] = {
74 0x00, 0x01, 0x00, 0x01,
75 0x5f, 0x44, 0x46, 0x4c,
76 0x54, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00,
83 0x02, 0x03, 0x04, 0x05,
85 0x00, 0x01, 0x00, 0x37,
86 0x00, 0x00, 0x00, 0x08,
87 0x00, 0x00, 0x00, 0x00,
88 0x49, 0x05, 0xcd, 0x01,
89 0x29, 0xb1, 0x8d, 0xb0,
90 0x00, 0x00, 0x00, 0x00,
95 0x02, 0x03, 0x04, 0x05,
97 0x00, 0x00, 0x00, 0x37,
98 0x00, 0x00, 0x00, 0x04,
99 0x44, 0x46, 0x4c, 0x54,
100 0x00, 0x00, 0xf0, 0x60,
101 0x00, 0x01, 0x00, 0x00,
102 0x00, 0x00, 0x00, 0x01,
103 0x00, 0x00, 0xf0, 0x60,
104 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x04,
106 0x44, 0x46, 0x4c, 0x54,
111 0x02, 0x03, 0x04, 0x05,
113 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00,
116 0x00, 0x00, 0x00, 0x00
119 static const unsigned char sync_v2
[] = {
120 0x00, 0x02, 0x00, 0x2C,
121 0x00, 0x00, 0x02, 0x00,
122 0x00, 0x00, 0x00, 0x00,
123 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0xFF,
126 0xFE, 0x00, 0x00, 0x00,
127 0x00, 0x01, 0x00, 0x01,
128 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00,
130 0x00, 0x00, 0x00, 0x00,
133 static void sendpacket(int sock
, struct sockaddr
*addr
, socklen_t addr_len
, int ptpv2
)
135 size_t sync_len
= ptpv2
? sizeof(sync_v2
) : sizeof(sync
);
136 const void *sync_p
= ptpv2
? sync_v2
: sync
;
140 res
= sendto(sock
, sync_p
, sync_len
, 0, addr
, addr_len
);
141 gettimeofday(&now
, 0);
143 printf("%s: %s\n", "send", strerror(errno
));
145 printf("%ld.%06ld: sent %d bytes\n",
146 (long)now
.tv_sec
, (long)now
.tv_usec
,
150 static void printpacket(struct msghdr
*msg
, int res
,
152 int sock
, int recvmsg_flags
,
153 int siocgstamp
, int siocgstampns
, int ptpv2
)
155 struct sockaddr_in
*from_addr
= (struct sockaddr_in
*)msg
->msg_name
;
156 size_t sync_len
= ptpv2
? sizeof(sync_v2
) : sizeof(sync
);
157 const void *sync_p
= ptpv2
? sync_v2
: sync
;
158 struct cmsghdr
*cmsg
;
163 gettimeofday(&now
, 0);
165 printf("%ld.%06ld: received %s data, %d bytes from %s, %zu bytes control messages\n",
166 (long)now
.tv_sec
, (long)now
.tv_usec
,
167 (recvmsg_flags
& MSG_ERRQUEUE
) ? "error" : "regular",
169 inet_ntoa(from_addr
->sin_addr
),
170 msg
->msg_controllen
);
171 for (cmsg
= CMSG_FIRSTHDR(msg
);
173 cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
174 printf(" cmsg len %zu: ", cmsg
->cmsg_len
);
175 switch (cmsg
->cmsg_level
) {
177 printf("SOL_SOCKET ");
178 switch (cmsg
->cmsg_type
) {
180 struct timeval
*stamp
=
181 (struct timeval
*)CMSG_DATA(cmsg
);
182 printf("SO_TIMESTAMP %ld.%06ld",
184 (long)stamp
->tv_usec
);
187 case SO_TIMESTAMPNS
: {
188 struct timespec
*stamp
=
189 (struct timespec
*)CMSG_DATA(cmsg
);
190 printf("SO_TIMESTAMPNS %ld.%09ld",
192 (long)stamp
->tv_nsec
);
195 case SO_TIMESTAMPING
: {
196 struct timespec
*stamp
=
197 (struct timespec
*)CMSG_DATA(cmsg
);
198 printf("SO_TIMESTAMPING ");
199 printf("SW %ld.%09ld ",
201 (long)stamp
->tv_nsec
);
203 /* skip deprecated HW transformed */
205 printf("HW raw %ld.%09ld",
207 (long)stamp
->tv_nsec
);
211 printf("type %d", cmsg
->cmsg_type
);
216 printf("IPPROTO_IP ");
217 switch (cmsg
->cmsg_type
) {
219 struct sock_extended_err
*err
=
220 (struct sock_extended_err
*)CMSG_DATA(cmsg
);
221 printf("IP_RECVERR ee_errno '%s' ee_origin %d => %s",
222 strerror(err
->ee_errno
),
224 #ifdef SO_EE_ORIGIN_TIMESTAMPING
225 err
->ee_origin
== SO_EE_ORIGIN_TIMESTAMPING
?
226 "bounced packet" : "unexpected origin"
228 "probably SO_EE_ORIGIN_TIMESTAMPING"
232 printf(" => truncated data?!");
233 else if (!memcmp(sync_p
, data
+ res
- sync_len
, sync_len
))
234 printf(" => GOT OUR DATA BACK (HURRAY!)");
238 struct in_pktinfo
*pktinfo
=
239 (struct in_pktinfo
*)CMSG_DATA(cmsg
);
240 printf("IP_PKTINFO interface index %u",
241 pktinfo
->ipi_ifindex
);
245 printf("type %d", cmsg
->cmsg_type
);
250 printf("level %d type %d",
259 if (ioctl(sock
, SIOCGSTAMP
, &tv
))
260 printf(" %s: %s\n", "SIOCGSTAMP", strerror(errno
));
262 printf("SIOCGSTAMP %ld.%06ld\n",
267 if (ioctl(sock
, SIOCGSTAMPNS
, &ts
))
268 printf(" %s: %s\n", "SIOCGSTAMPNS", strerror(errno
));
270 printf("SIOCGSTAMPNS %ld.%09ld\n",
276 static void recvpacket(int sock
, int recvmsg_flags
,
277 int siocgstamp
, int siocgstampns
, int ptpv2
)
282 struct sockaddr_in from_addr
;
289 memset(&msg
, 0, sizeof(msg
));
290 msg
.msg_iov
= &entry
;
292 entry
.iov_base
= data
;
293 entry
.iov_len
= sizeof(data
);
294 msg
.msg_name
= (caddr_t
)&from_addr
;
295 msg
.msg_namelen
= sizeof(from_addr
);
296 msg
.msg_control
= &control
;
297 msg
.msg_controllen
= sizeof(control
);
299 res
= recvmsg(sock
, &msg
, recvmsg_flags
|MSG_DONTWAIT
);
301 printf("%s %s: %s\n",
303 (recvmsg_flags
& MSG_ERRQUEUE
) ? "error" : "regular",
306 printpacket(&msg
, res
, data
,
308 siocgstamp
, siocgstampns
, ptpv2
);
312 int main(int argc
, char **argv
)
314 int so_timestamping_flags
= 0;
315 int so_timestamp
= 0;
316 int so_timestampns
= 0;
318 int siocgstampns
= 0;
319 int ip_multicast_loop
= 0;
326 struct ifreq hwtstamp
;
327 struct hwtstamp_config hwconfig
, hwconfig_requested
;
328 struct sockaddr_in addr
;
330 struct in_addr iaddr
;
339 if_len
= strlen(interface
);
340 if (if_len
>= IFNAMSIZ
) {
341 printf("interface name exceeds IFNAMSIZ\n");
345 for (i
= 2; i
< argc
; i
++) {
346 if (!strcasecmp(argv
[i
], "SO_TIMESTAMP"))
348 else if (!strcasecmp(argv
[i
], "SO_TIMESTAMPNS"))
350 else if (!strcasecmp(argv
[i
], "SIOCGSTAMP"))
352 else if (!strcasecmp(argv
[i
], "SIOCGSTAMPNS"))
354 else if (!strcasecmp(argv
[i
], "IP_MULTICAST_LOOP"))
355 ip_multicast_loop
= 1;
356 else if (!strcasecmp(argv
[i
], "PTPV2"))
358 else if (!strcasecmp(argv
[i
], "SOF_TIMESTAMPING_TX_HARDWARE"))
359 so_timestamping_flags
|= SOF_TIMESTAMPING_TX_HARDWARE
;
360 else if (!strcasecmp(argv
[i
], "SOF_TIMESTAMPING_TX_SOFTWARE"))
361 so_timestamping_flags
|= SOF_TIMESTAMPING_TX_SOFTWARE
;
362 else if (!strcasecmp(argv
[i
], "SOF_TIMESTAMPING_RX_HARDWARE"))
363 so_timestamping_flags
|= SOF_TIMESTAMPING_RX_HARDWARE
;
364 else if (!strcasecmp(argv
[i
], "SOF_TIMESTAMPING_RX_SOFTWARE"))
365 so_timestamping_flags
|= SOF_TIMESTAMPING_RX_SOFTWARE
;
366 else if (!strcasecmp(argv
[i
], "SOF_TIMESTAMPING_SOFTWARE"))
367 so_timestamping_flags
|= SOF_TIMESTAMPING_SOFTWARE
;
368 else if (!strcasecmp(argv
[i
], "SOF_TIMESTAMPING_RAW_HARDWARE"))
369 so_timestamping_flags
|= SOF_TIMESTAMPING_RAW_HARDWARE
;
374 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
378 memset(&device
, 0, sizeof(device
));
379 memcpy(device
.ifr_name
, interface
, if_len
+ 1);
380 if (ioctl(sock
, SIOCGIFADDR
, &device
) < 0)
381 bail("getting interface IP address");
383 memset(&hwtstamp
, 0, sizeof(hwtstamp
));
384 memcpy(hwtstamp
.ifr_name
, interface
, if_len
+ 1);
385 hwtstamp
.ifr_data
= (void *)&hwconfig
;
386 memset(&hwconfig
, 0, sizeof(hwconfig
));
388 (so_timestamping_flags
& SOF_TIMESTAMPING_TX_HARDWARE
) ?
389 HWTSTAMP_TX_ON
: HWTSTAMP_TX_OFF
;
391 (so_timestamping_flags
& SOF_TIMESTAMPING_RX_HARDWARE
) ?
392 ptpv2
? HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
393 HWTSTAMP_FILTER_PTP_V1_L4_SYNC
: HWTSTAMP_FILTER_NONE
;
394 hwconfig_requested
= hwconfig
;
395 if (ioctl(sock
, SIOCSHWTSTAMP
, &hwtstamp
) < 0) {
396 if ((errno
== EINVAL
|| errno
== ENOTSUP
) &&
397 hwconfig_requested
.tx_type
== HWTSTAMP_TX_OFF
&&
398 hwconfig_requested
.rx_filter
== HWTSTAMP_FILTER_NONE
)
399 printf("SIOCSHWTSTAMP: disabling hardware time stamping not possible\n");
401 bail("SIOCSHWTSTAMP");
403 printf("SIOCSHWTSTAMP: tx_type %d requested, got %d; rx_filter %d requested, got %d\n",
404 hwconfig_requested
.tx_type
, hwconfig
.tx_type
,
405 hwconfig_requested
.rx_filter
, hwconfig
.rx_filter
);
407 /* bind to PTP port */
408 addr
.sin_family
= AF_INET
;
409 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
410 addr
.sin_port
= htons(319 /* PTP event port */);
412 (struct sockaddr
*)&addr
,
413 sizeof(struct sockaddr_in
)) < 0)
416 /* set multicast group for outgoing packets */
417 inet_aton("224.0.1.130", &iaddr
); /* alternate PTP domain 1 */
418 addr
.sin_addr
= iaddr
;
419 imr
.imr_multiaddr
.s_addr
= iaddr
.s_addr
;
420 imr
.imr_interface
.s_addr
=
421 ((struct sockaddr_in
*)&device
.ifr_addr
)->sin_addr
.s_addr
;
422 if (setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_IF
,
423 &imr
.imr_interface
.s_addr
, sizeof(struct in_addr
)) < 0)
424 bail("set multicast");
426 /* join multicast group, loop our own packet */
427 if (setsockopt(sock
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
428 &imr
, sizeof(struct ip_mreq
)) < 0)
429 bail("join multicast group");
431 if (setsockopt(sock
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
432 &ip_multicast_loop
, sizeof(enabled
)) < 0) {
433 bail("loop multicast");
436 /* set socket options for time stamping */
438 setsockopt(sock
, SOL_SOCKET
, SO_TIMESTAMP
,
439 &enabled
, sizeof(enabled
)) < 0)
440 bail("setsockopt SO_TIMESTAMP");
442 if (so_timestampns
&&
443 setsockopt(sock
, SOL_SOCKET
, SO_TIMESTAMPNS
,
444 &enabled
, sizeof(enabled
)) < 0)
445 bail("setsockopt SO_TIMESTAMPNS");
447 if (so_timestamping_flags
&&
448 setsockopt(sock
, SOL_SOCKET
, SO_TIMESTAMPING
,
449 &so_timestamping_flags
,
450 sizeof(so_timestamping_flags
)) < 0)
451 bail("setsockopt SO_TIMESTAMPING");
453 /* request IP_PKTINFO for debugging purposes */
454 if (setsockopt(sock
, SOL_IP
, IP_PKTINFO
,
455 &enabled
, sizeof(enabled
)) < 0)
456 printf("%s: %s\n", "setsockopt IP_PKTINFO", strerror(errno
));
458 /* verify socket options */
460 if (getsockopt(sock
, SOL_SOCKET
, SO_TIMESTAMP
, &val
, &len
) < 0)
461 printf("%s: %s\n", "getsockopt SO_TIMESTAMP", strerror(errno
));
463 printf("SO_TIMESTAMP %d\n", val
);
465 if (getsockopt(sock
, SOL_SOCKET
, SO_TIMESTAMPNS
, &val
, &len
) < 0)
466 printf("%s: %s\n", "getsockopt SO_TIMESTAMPNS",
469 printf("SO_TIMESTAMPNS %d\n", val
);
471 if (getsockopt(sock
, SOL_SOCKET
, SO_TIMESTAMPING
, &val
, &len
) < 0) {
472 printf("%s: %s\n", "getsockopt SO_TIMESTAMPING",
475 printf("SO_TIMESTAMPING %d\n", val
);
476 if (val
!= so_timestamping_flags
)
477 printf(" not the expected value %d\n",
478 so_timestamping_flags
);
481 /* send packets forever every five seconds */
482 gettimeofday(&next
, 0);
483 next
.tv_sec
= (next
.tv_sec
+ 1) / 5 * 5;
487 struct timeval delta
;
490 fd_set readfs
, errorfs
;
492 gettimeofday(&now
, 0);
493 delta_us
= (long)(next
.tv_sec
- now
.tv_sec
) * 1000000 +
494 (long)(next
.tv_usec
- now
.tv_usec
);
496 /* continue waiting for timeout or data */
497 delta
.tv_sec
= delta_us
/ 1000000;
498 delta
.tv_usec
= delta_us
% 1000000;
502 FD_SET(sock
, &readfs
);
503 FD_SET(sock
, &errorfs
);
504 printf("%ld.%06ld: select %ldus\n",
505 (long)now
.tv_sec
, (long)now
.tv_usec
,
507 res
= select(sock
+ 1, &readfs
, 0, &errorfs
, &delta
);
508 gettimeofday(&now
, 0);
509 printf("%ld.%06ld: select returned: %d, %s\n",
510 (long)now
.tv_sec
, (long)now
.tv_usec
,
512 res
< 0 ? strerror(errno
) : "success");
514 if (FD_ISSET(sock
, &readfs
))
515 printf("ready for reading\n");
516 if (FD_ISSET(sock
, &errorfs
))
517 printf("has error\n");
520 siocgstampns
, ptpv2
);
521 recvpacket(sock
, MSG_ERRQUEUE
,
523 siocgstampns
, ptpv2
);
526 /* write one packet */
528 (struct sockaddr
*)&addr
,
529 sizeof(addr
), ptpv2
);