1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
6 * This application is a simple Layer 2 PTP v2 client. It shows delta values
7 * which are used to synchronize the PHC clock. if the "-T 1" parameter is
8 * passed to the application the Linux kernel clock is also synchronized.
14 #include <rte_ethdev.h>
15 #include <rte_cycles.h>
16 #include <rte_lcore.h>
23 #define RX_RING_SIZE 1024
24 #define TX_RING_SIZE 1024
26 #define NUM_MBUFS 8191
27 #define MBUF_CACHE_SIZE 250
29 /* Values for the PTP messageType field. */
32 #define PDELAY_REQ 0x2
33 #define PDELAY_RESP 0x3
35 #define DELAY_RESP 0x9
36 #define PDELAY_RESP_FOLLOW_UP 0xA
39 #define MANAGEMENT 0xD
41 #define NSEC_PER_SEC 1000000000L
42 #define KERNEL_TIME_ADJUST_LIMIT 20000
43 #define PTP_PROTOCOL 0x88F7
45 struct rte_mempool *mbuf_pool;
46 uint32_t ptp_enabled_port_mask;
47 uint8_t ptp_enabled_port_nb;
48 static uint8_t ptp_enabled_ports[RTE_MAX_ETHPORTS];
50 static const struct rte_ether_addr ether_multicast = {
51 .addr_bytes = {0x01, 0x1b, 0x19, 0x0, 0x0, 0x0}
54 /* Structs used for PTP handling. */
66 struct clock_id clock_id;
73 uint16_t message_length;
74 uint8_t domain_number;
76 uint8_t flag_field[2];
79 struct port_id source_port_id;
82 int8_t log_message_interval;
86 struct ptp_header hdr;
87 struct tstamp origin_tstamp;
90 struct follow_up_msg {
91 struct ptp_header hdr;
92 struct tstamp precise_origin_tstamp;
96 struct delay_req_msg {
97 struct ptp_header hdr;
98 struct tstamp origin_tstamp;
101 struct delay_resp_msg {
102 struct ptp_header hdr;
103 struct tstamp rx_tstamp;
104 struct port_id req_port_id;
110 struct ptp_header header;
111 struct sync_msg sync;
112 struct delay_req_msg delay_req;
113 struct follow_up_msg follow_up;
114 struct delay_resp_msg delay_resp;
118 struct ptpv2_data_slave_ordinary {
120 struct timespec tstamp1;
121 struct timespec tstamp2;
122 struct timespec tstamp3;
123 struct timespec tstamp4;
124 struct clock_id client_clock_id;
125 struct clock_id master_clock_id;
126 struct timeval new_adj;
130 uint16_t seqID_FOLLOWUP;
132 uint8_t kernel_time_set;
133 uint16_t current_ptp_port;
136 static struct ptpv2_data_slave_ordinary ptp_data;
138 static inline uint64_t timespec64_to_ns(const struct timespec *ts)
140 return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
143 static struct timeval
144 ns_to_timeval(int64_t nsec)
146 struct timespec t_spec = {0, 0};
147 struct timeval t_eval = {0, 0};
152 rem = nsec % NSEC_PER_SEC;
153 t_spec.tv_sec = nsec / NSEC_PER_SEC;
160 t_spec.tv_nsec = rem;
161 t_eval.tv_sec = t_spec.tv_sec;
162 t_eval.tv_usec = t_spec.tv_nsec / 1000;
168 * Initializes a given port using global settings and with the RX buffers
169 * coming from the mbuf_pool passed as a parameter.
172 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
174 struct rte_eth_dev_info dev_info;
175 struct rte_eth_conf port_conf;
176 const uint16_t rx_rings = 1;
177 const uint16_t tx_rings = 1;
180 uint16_t nb_rxd = RX_RING_SIZE;
181 uint16_t nb_txd = TX_RING_SIZE;
183 if (!rte_eth_dev_is_valid_port(port))
186 memset(&port_conf, 0, sizeof(struct rte_eth_conf));
188 retval = rte_eth_dev_info_get(port, &dev_info);
190 printf("Error during getting device (port %u) info: %s\n",
191 port, strerror(-retval));
196 if (dev_info.rx_offload_capa & RTE_ETH_RX_OFFLOAD_TIMESTAMP)
197 port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_TIMESTAMP;
199 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
200 port_conf.txmode.offloads |=
201 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
202 /* Force full Tx path in the driver, required for IEEE1588 */
203 port_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
205 /* Configure the Ethernet device. */
206 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
210 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
214 /* Allocate and set up 1 RX queue per Ethernet port. */
215 for (q = 0; q < rx_rings; q++) {
216 struct rte_eth_rxconf *rxconf;
218 rxconf = &dev_info.default_rxconf;
219 rxconf->offloads = port_conf.rxmode.offloads;
221 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
222 rte_eth_dev_socket_id(port), rxconf, mbuf_pool);
228 /* Allocate and set up 1 TX queue per Ethernet port. */
229 for (q = 0; q < tx_rings; q++) {
230 struct rte_eth_txconf *txconf;
232 txconf = &dev_info.default_txconf;
233 txconf->offloads = port_conf.txmode.offloads;
235 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
236 rte_eth_dev_socket_id(port), txconf);
241 /* Start the Ethernet port. */
242 retval = rte_eth_dev_start(port);
246 /* Enable timesync timestamping for the Ethernet device */
247 retval = rte_eth_timesync_enable(port);
249 printf("Timesync enable failed: %d\n", retval);
253 /* Enable RX in promiscuous mode for the Ethernet device. */
254 retval = rte_eth_promiscuous_enable(port);
256 printf("Promiscuous mode enable failed: %s\n",
257 rte_strerror(-retval));
265 print_clock_info(struct ptpv2_data_slave_ordinary *ptp_data)
268 struct timespec net_time, sys_time;
270 printf("Master Clock id: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
271 ptp_data->master_clock_id.id[0],
272 ptp_data->master_clock_id.id[1],
273 ptp_data->master_clock_id.id[2],
274 ptp_data->master_clock_id.id[3],
275 ptp_data->master_clock_id.id[4],
276 ptp_data->master_clock_id.id[5],
277 ptp_data->master_clock_id.id[6],
278 ptp_data->master_clock_id.id[7]);
280 printf("\nT2 - Slave Clock. %lds %ldns",
281 (ptp_data->tstamp2.tv_sec),
282 (ptp_data->tstamp2.tv_nsec));
284 printf("\nT1 - Master Clock. %lds %ldns ",
285 ptp_data->tstamp1.tv_sec,
286 (ptp_data->tstamp1.tv_nsec));
288 printf("\nT3 - Slave Clock. %lds %ldns",
289 ptp_data->tstamp3.tv_sec,
290 (ptp_data->tstamp3.tv_nsec));
292 printf("\nT4 - Master Clock. %lds %ldns ",
293 ptp_data->tstamp4.tv_sec,
294 (ptp_data->tstamp4.tv_nsec));
296 printf("\nDelta between master and slave clocks:%"PRId64"ns\n",
299 clock_gettime(CLOCK_REALTIME, &sys_time);
300 rte_eth_timesync_read_time(ptp_data->current_ptp_port, &net_time);
302 time_t ts = net_time.tv_sec;
304 printf("\n\nComparison between Linux kernel Time and PTP:");
306 printf("\nCurrent PTP Time: %.24s %.9ld ns",
307 ctime(&ts), net_time.tv_nsec);
309 nsec = (int64_t)timespec64_to_ns(&net_time) -
310 (int64_t)timespec64_to_ns(&sys_time);
311 ptp_data->new_adj = ns_to_timeval(nsec);
313 gettimeofday(&ptp_data->new_adj, NULL);
315 time_t tp = ptp_data->new_adj.tv_sec;
317 printf("\nCurrent SYS Time: %.24s %.6ld ns",
318 ctime(&tp), ptp_data->new_adj.tv_usec);
320 printf("\nDelta between PTP and Linux Kernel time:%"PRId64"ns\n",
323 printf("[Ctrl+C to quit]\n");
325 /* Clear screen and put cursor in column 1, row 1 */
326 printf("\033[2J\033[1;1H");
330 delta_eval(struct ptpv2_data_slave_ordinary *ptp_data)
338 t1 = timespec64_to_ns(&ptp_data->tstamp1);
339 t2 = timespec64_to_ns(&ptp_data->tstamp2);
340 t3 = timespec64_to_ns(&ptp_data->tstamp3);
341 t4 = timespec64_to_ns(&ptp_data->tstamp4);
343 delta = -((int64_t)((t2 - t1) - (t4 - t3))) / 2;
349 * Parse the PTP SYNC message.
352 parse_sync(struct ptpv2_data_slave_ordinary *ptp_data, uint16_t rx_tstamp_idx)
354 struct ptp_header *ptp_hdr;
356 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(ptp_data->m, char *)
357 + sizeof(struct rte_ether_hdr));
358 ptp_data->seqID_SYNC = rte_be_to_cpu_16(ptp_hdr->seq_id);
360 if (ptp_data->ptpset == 0) {
361 rte_memcpy(&ptp_data->master_clock_id,
362 &ptp_hdr->source_port_id.clock_id,
363 sizeof(struct clock_id));
364 ptp_data->ptpset = 1;
367 if (memcmp(&ptp_hdr->source_port_id.clock_id,
368 &ptp_hdr->source_port_id.clock_id,
369 sizeof(struct clock_id)) == 0) {
371 if (ptp_data->ptpset == 1)
372 rte_eth_timesync_read_rx_timestamp(ptp_data->portid,
373 &ptp_data->tstamp2, rx_tstamp_idx);
379 * Parse the PTP FOLLOWUP message and send DELAY_REQ to the main clock.
382 parse_fup(struct ptpv2_data_slave_ordinary *ptp_data)
384 struct rte_ether_hdr *eth_hdr;
385 struct rte_ether_addr eth_addr;
386 struct ptp_header *ptp_hdr;
387 struct clock_id *client_clkid;
388 struct ptp_message *ptp_msg;
389 struct delay_req_msg *req_msg;
390 struct rte_mbuf *created_pkt;
391 struct tstamp *origin_tstamp;
392 struct rte_ether_addr eth_multicast = ether_multicast;
395 struct rte_mbuf *m = ptp_data->m;
398 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
399 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
400 + sizeof(struct rte_ether_hdr));
401 if (memcmp(&ptp_data->master_clock_id,
402 &ptp_hdr->source_port_id.clock_id,
403 sizeof(struct clock_id)) != 0)
406 ptp_data->seqID_FOLLOWUP = rte_be_to_cpu_16(ptp_hdr->seq_id);
407 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
408 sizeof(struct rte_ether_hdr));
410 origin_tstamp = &ptp_msg->follow_up.precise_origin_tstamp;
411 ptp_data->tstamp1.tv_nsec = ntohl(origin_tstamp->ns);
412 ptp_data->tstamp1.tv_sec =
413 ((uint64_t)ntohl(origin_tstamp->sec_lsb)) |
414 (((uint64_t)ntohs(origin_tstamp->sec_msb)) << 32);
416 if (ptp_data->seqID_FOLLOWUP == ptp_data->seqID_SYNC) {
417 ret = rte_eth_macaddr_get(ptp_data->portid, ð_addr);
419 printf("\nCore %u: port %u failed to get MAC address: %s\n",
420 rte_lcore_id(), ptp_data->portid,
425 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
426 pkt_size = sizeof(struct rte_ether_hdr) +
427 sizeof(struct delay_req_msg);
429 if (rte_pktmbuf_append(created_pkt, pkt_size) == NULL) {
430 rte_pktmbuf_free(created_pkt);
433 created_pkt->data_len = pkt_size;
434 created_pkt->pkt_len = pkt_size;
435 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
436 rte_ether_addr_copy(ð_addr, ð_hdr->src_addr);
438 /* Set multicast address 01-1B-19-00-00-00. */
439 rte_ether_addr_copy(ð_multicast, ð_hdr->dst_addr);
441 eth_hdr->ether_type = htons(PTP_PROTOCOL);
442 req_msg = rte_pktmbuf_mtod_offset(created_pkt,
443 struct delay_req_msg *, sizeof(struct
446 req_msg->hdr.seq_id = htons(ptp_data->seqID_SYNC);
447 req_msg->hdr.msg_type = DELAY_REQ;
448 req_msg->hdr.ver = 2;
449 req_msg->hdr.control = 1;
450 req_msg->hdr.log_message_interval = 127;
451 req_msg->hdr.message_length =
452 htons(sizeof(struct delay_req_msg));
453 req_msg->hdr.domain_number = ptp_hdr->domain_number;
455 /* Set up clock id. */
457 &req_msg->hdr.source_port_id.clock_id;
459 client_clkid->id[0] = eth_hdr->src_addr.addr_bytes[0];
460 client_clkid->id[1] = eth_hdr->src_addr.addr_bytes[1];
461 client_clkid->id[2] = eth_hdr->src_addr.addr_bytes[2];
462 client_clkid->id[3] = 0xFF;
463 client_clkid->id[4] = 0xFE;
464 client_clkid->id[5] = eth_hdr->src_addr.addr_bytes[3];
465 client_clkid->id[6] = eth_hdr->src_addr.addr_bytes[4];
466 client_clkid->id[7] = eth_hdr->src_addr.addr_bytes[5];
468 rte_memcpy(&ptp_data->client_clock_id,
470 sizeof(struct clock_id));
472 /* Enable flag for hardware timestamping. */
473 created_pkt->ol_flags |= RTE_MBUF_F_TX_IEEE1588_TMST;
475 /*Read value from NIC to prevent latching with old value. */
476 rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
479 /* Transmit the packet. */
480 rte_eth_tx_burst(ptp_data->portid, 0, &created_pkt, 1);
483 ptp_data->tstamp3.tv_nsec = 0;
484 ptp_data->tstamp3.tv_sec = 0;
486 /* Wait at least 1 us to read TX timestamp. */
487 while ((rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
488 &ptp_data->tstamp3) < 0) && (wait_us < 1000)) {
496 * Update the kernel time with the difference between it and the current NIC
500 update_kernel_time(void)
503 struct timespec net_time, sys_time;
505 clock_gettime(CLOCK_REALTIME, &sys_time);
506 rte_eth_timesync_read_time(ptp_data.current_ptp_port, &net_time);
508 nsec = (int64_t)timespec64_to_ns(&net_time) -
509 (int64_t)timespec64_to_ns(&sys_time);
511 ptp_data.new_adj = ns_to_timeval(nsec);
514 * If difference between kernel time and system time in NIC is too big
515 * (more than +/- 20 microseconds), use clock_settime to set directly
516 * the kernel time, as adjtime is better for small adjustments (takes
517 * longer to adjust the time).
520 if (nsec > KERNEL_TIME_ADJUST_LIMIT || nsec < -KERNEL_TIME_ADJUST_LIMIT)
521 clock_settime(CLOCK_REALTIME, &net_time);
523 adjtime(&ptp_data.new_adj, 0);
529 * Parse the DELAY_RESP message.
532 parse_drsp(struct ptpv2_data_slave_ordinary *ptp_data)
534 struct rte_mbuf *m = ptp_data->m;
535 struct ptp_message *ptp_msg;
536 struct tstamp *rx_tstamp;
539 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
540 sizeof(struct rte_ether_hdr));
541 seq_id = rte_be_to_cpu_16(ptp_msg->delay_resp.hdr.seq_id);
542 if (memcmp(&ptp_data->client_clock_id,
543 &ptp_msg->delay_resp.req_port_id.clock_id,
544 sizeof(struct clock_id)) == 0) {
545 if (seq_id == ptp_data->seqID_FOLLOWUP) {
546 rx_tstamp = &ptp_msg->delay_resp.rx_tstamp;
547 ptp_data->tstamp4.tv_nsec = ntohl(rx_tstamp->ns);
548 ptp_data->tstamp4.tv_sec =
549 ((uint64_t)ntohl(rx_tstamp->sec_lsb)) |
550 (((uint64_t)ntohs(rx_tstamp->sec_msb)) << 32);
552 /* Evaluate the delta for adjustment. */
553 ptp_data->delta = delta_eval(ptp_data);
555 rte_eth_timesync_adjust_time(ptp_data->portid,
558 ptp_data->current_ptp_port = ptp_data->portid;
560 /* Update kernel time if enabled in app parameters. */
561 if (ptp_data->kernel_time_set == 1)
562 update_kernel_time();
570 /* This function processes PTP packets, implementing slave PTP IEEE1588 L2
574 /* Parse ptp frames. 8< */
576 parse_ptp_frames(uint16_t portid, struct rte_mbuf *m) {
577 struct ptp_header *ptp_hdr;
578 struct rte_ether_hdr *eth_hdr;
581 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
582 eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
584 if (eth_type == PTP_PROTOCOL) {
586 ptp_data.portid = portid;
587 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
588 + sizeof(struct rte_ether_hdr));
590 switch (ptp_hdr->msg_type) {
592 parse_sync(&ptp_data, m->timesync);
595 parse_fup(&ptp_data);
598 parse_drsp(&ptp_data);
599 print_clock_info(&ptp_data);
606 /* >8 End of function processes PTP packets. */
609 * The lcore main. This is the main thread that does the work, reading from an
610 * input port and writing to an output port.
612 static __rte_noreturn void
619 printf("\nCore %u Waiting for SYNC packets. [Ctrl+C to quit]\n",
622 /* Run until the application is quit or killed. */
625 /* Read packet from RX queues. 8< */
626 for (portid = 0; portid < ptp_enabled_port_nb; portid++) {
628 portid = ptp_enabled_ports[portid];
629 nb_rx = rte_eth_rx_burst(portid, 0, &m, 1);
631 if (likely(nb_rx == 0))
634 /* Packet is parsed to determine which type. 8< */
635 if (m->ol_flags & RTE_MBUF_F_RX_IEEE1588_PTP)
636 parse_ptp_frames(portid, m);
637 /* >8 End of packet is parsed to determine which type. */
641 /* >8 End of read packets from RX queues. */
646 print_usage(const char *prgname)
648 printf("%s [EAL options] -- -p PORTMASK -T VALUE\n"
649 " -T VALUE: 0 - Disable, 1 - Enable Linux Clock"
650 " Synchronization (0 default)\n"
651 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
656 ptp_parse_portmask(const char *portmask)
661 /* Parse the hexadecimal string. */
662 pm = strtoul(portmask, &end, 16);
664 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
671 parse_ptp_kernel(const char *param)
676 /* Parse the hexadecimal string. */
677 pm = strtoul(param, &end, 16);
679 if ((param[0] == '\0') || (end == NULL) || (*end != '\0'))
687 /* Parse the commandline arguments. */
689 ptp_parse_args(int argc, char **argv)
694 char *prgname = argv[0];
695 static struct option lgopts[] = { {NULL, 0, 0, 0} };
699 while ((opt = getopt_long(argc, argvopt, "p:T:",
700 lgopts, &option_index)) != EOF) {
706 ptp_enabled_port_mask = ptp_parse_portmask(optarg);
707 if (ptp_enabled_port_mask == 0) {
708 printf("invalid portmask\n");
709 print_usage(prgname);
713 /* Time synchronization. */
715 ret = parse_ptp_kernel(optarg);
717 print_usage(prgname);
721 ptp_data.kernel_time_set = ret;
725 print_usage(prgname);
730 argv[optind-1] = prgname;
732 optind = 1; /* Reset getopt lib. */
738 * The main function, which does initialization and calls the per-lcore
742 main(int argc, char *argv[])
748 /* Initialize the Environment Abstraction Layer (EAL). 8< */
749 int ret = rte_eal_init(argc, argv);
752 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
753 /* >8 End of initialization of EAL. */
755 memset(&ptp_data, '\0', sizeof(struct ptpv2_data_slave_ordinary));
757 /* Parse specific arguments. 8< */
761 ret = ptp_parse_args(argc, argv);
763 rte_exit(EXIT_FAILURE, "Error with PTP initialization\n");
764 /* >8 End of parsing specific arguments. */
766 /* Check that there is an even number of ports to send/receive on. */
767 nb_ports = rte_eth_dev_count_avail();
769 /* Creates a new mempool in memory to hold the mbufs. 8< */
770 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
771 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
772 /* >8 End of a new mempool in memory to hold the mbufs. */
774 if (mbuf_pool == NULL)
775 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
777 /* Initialize all ports. 8< */
778 RTE_ETH_FOREACH_DEV(portid) {
779 if ((ptp_enabled_port_mask & (1 << portid)) != 0) {
780 if (port_init(portid, mbuf_pool) == 0) {
781 ptp_enabled_ports[ptp_enabled_port_nb] = portid;
782 ptp_enabled_port_nb++;
784 rte_exit(EXIT_FAILURE,
785 "Cannot init port %"PRIu8 "\n",
789 printf("Skipping disabled port %u\n", portid);
791 /* >8 End of initialization of all ports. */
793 if (ptp_enabled_port_nb == 0) {
794 rte_exit(EXIT_FAILURE,
795 "All available ports are disabled."
796 " Please set portmask.\n");
799 if (rte_lcore_count() > 1)
800 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
802 /* Call lcore_main on the main core only. */
805 /* clean up the EAL */