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 rte_mbuf *created_pkt;
390 struct tstamp *origin_tstamp;
391 struct rte_ether_addr eth_multicast = ether_multicast;
394 struct rte_mbuf *m = ptp_data->m;
397 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
398 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
399 + sizeof(struct rte_ether_hdr));
400 if (memcmp(&ptp_data->master_clock_id,
401 &ptp_hdr->source_port_id.clock_id,
402 sizeof(struct clock_id)) != 0)
405 ptp_data->seqID_FOLLOWUP = rte_be_to_cpu_16(ptp_hdr->seq_id);
406 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
407 sizeof(struct rte_ether_hdr));
409 origin_tstamp = &ptp_msg->follow_up.precise_origin_tstamp;
410 ptp_data->tstamp1.tv_nsec = ntohl(origin_tstamp->ns);
411 ptp_data->tstamp1.tv_sec =
412 ((uint64_t)ntohl(origin_tstamp->sec_lsb)) |
413 (((uint64_t)ntohs(origin_tstamp->sec_msb)) << 32);
415 if (ptp_data->seqID_FOLLOWUP == ptp_data->seqID_SYNC) {
416 ret = rte_eth_macaddr_get(ptp_data->portid, ð_addr);
418 printf("\nCore %u: port %u failed to get MAC address: %s\n",
419 rte_lcore_id(), ptp_data->portid,
424 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
425 pkt_size = sizeof(struct rte_ether_hdr) +
426 sizeof(struct ptp_message);
427 created_pkt->data_len = pkt_size;
428 created_pkt->pkt_len = pkt_size;
429 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
430 rte_ether_addr_copy(ð_addr, ð_hdr->src_addr);
432 /* Set multicast address 01-1B-19-00-00-00. */
433 rte_ether_addr_copy(ð_multicast, ð_hdr->dst_addr);
435 eth_hdr->ether_type = htons(PTP_PROTOCOL);
436 ptp_msg = (struct ptp_message *)
437 (rte_pktmbuf_mtod(created_pkt, char *) +
438 sizeof(struct rte_ether_hdr));
440 ptp_msg->delay_req.hdr.seq_id = htons(ptp_data->seqID_SYNC);
441 ptp_msg->delay_req.hdr.msg_type = DELAY_REQ;
442 ptp_msg->delay_req.hdr.ver = 2;
443 ptp_msg->delay_req.hdr.control = 1;
444 ptp_msg->delay_req.hdr.log_message_interval = 127;
445 ptp_msg->delay_req.hdr.message_length =
446 htons(sizeof(struct delay_req_msg));
447 ptp_msg->delay_req.hdr.domain_number = ptp_hdr->domain_number;
449 /* Set up clock id. */
451 &ptp_msg->delay_req.hdr.source_port_id.clock_id;
453 client_clkid->id[0] = eth_hdr->src_addr.addr_bytes[0];
454 client_clkid->id[1] = eth_hdr->src_addr.addr_bytes[1];
455 client_clkid->id[2] = eth_hdr->src_addr.addr_bytes[2];
456 client_clkid->id[3] = 0xFF;
457 client_clkid->id[4] = 0xFE;
458 client_clkid->id[5] = eth_hdr->src_addr.addr_bytes[3];
459 client_clkid->id[6] = eth_hdr->src_addr.addr_bytes[4];
460 client_clkid->id[7] = eth_hdr->src_addr.addr_bytes[5];
462 rte_memcpy(&ptp_data->client_clock_id,
464 sizeof(struct clock_id));
466 /* Enable flag for hardware timestamping. */
467 created_pkt->ol_flags |= RTE_MBUF_F_TX_IEEE1588_TMST;
469 /*Read value from NIC to prevent latching with old value. */
470 rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
473 /* Transmit the packet. */
474 rte_eth_tx_burst(ptp_data->portid, 0, &created_pkt, 1);
477 ptp_data->tstamp3.tv_nsec = 0;
478 ptp_data->tstamp3.tv_sec = 0;
480 /* Wait at least 1 us to read TX timestamp. */
481 while ((rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
482 &ptp_data->tstamp3) < 0) && (wait_us < 1000)) {
490 * Update the kernel time with the difference between it and the current NIC
494 update_kernel_time(void)
497 struct timespec net_time, sys_time;
499 clock_gettime(CLOCK_REALTIME, &sys_time);
500 rte_eth_timesync_read_time(ptp_data.current_ptp_port, &net_time);
502 nsec = (int64_t)timespec64_to_ns(&net_time) -
503 (int64_t)timespec64_to_ns(&sys_time);
505 ptp_data.new_adj = ns_to_timeval(nsec);
508 * If difference between kernel time and system time in NIC is too big
509 * (more than +/- 20 microseconds), use clock_settime to set directly
510 * the kernel time, as adjtime is better for small adjustments (takes
511 * longer to adjust the time).
514 if (nsec > KERNEL_TIME_ADJUST_LIMIT || nsec < -KERNEL_TIME_ADJUST_LIMIT)
515 clock_settime(CLOCK_REALTIME, &net_time);
517 adjtime(&ptp_data.new_adj, 0);
523 * Parse the DELAY_RESP message.
526 parse_drsp(struct ptpv2_data_slave_ordinary *ptp_data)
528 struct rte_mbuf *m = ptp_data->m;
529 struct ptp_message *ptp_msg;
530 struct tstamp *rx_tstamp;
533 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
534 sizeof(struct rte_ether_hdr));
535 seq_id = rte_be_to_cpu_16(ptp_msg->delay_resp.hdr.seq_id);
536 if (memcmp(&ptp_data->client_clock_id,
537 &ptp_msg->delay_resp.req_port_id.clock_id,
538 sizeof(struct clock_id)) == 0) {
539 if (seq_id == ptp_data->seqID_FOLLOWUP) {
540 rx_tstamp = &ptp_msg->delay_resp.rx_tstamp;
541 ptp_data->tstamp4.tv_nsec = ntohl(rx_tstamp->ns);
542 ptp_data->tstamp4.tv_sec =
543 ((uint64_t)ntohl(rx_tstamp->sec_lsb)) |
544 (((uint64_t)ntohs(rx_tstamp->sec_msb)) << 32);
546 /* Evaluate the delta for adjustment. */
547 ptp_data->delta = delta_eval(ptp_data);
549 rte_eth_timesync_adjust_time(ptp_data->portid,
552 ptp_data->current_ptp_port = ptp_data->portid;
554 /* Update kernel time if enabled in app parameters. */
555 if (ptp_data->kernel_time_set == 1)
556 update_kernel_time();
564 /* This function processes PTP packets, implementing slave PTP IEEE1588 L2
568 /* Parse ptp frames. 8< */
570 parse_ptp_frames(uint16_t portid, struct rte_mbuf *m) {
571 struct ptp_header *ptp_hdr;
572 struct rte_ether_hdr *eth_hdr;
575 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
576 eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
578 if (eth_type == PTP_PROTOCOL) {
580 ptp_data.portid = portid;
581 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
582 + sizeof(struct rte_ether_hdr));
584 switch (ptp_hdr->msg_type) {
586 parse_sync(&ptp_data, m->timesync);
589 parse_fup(&ptp_data);
592 parse_drsp(&ptp_data);
593 print_clock_info(&ptp_data);
600 /* >8 End of function processes PTP packets. */
603 * The lcore main. This is the main thread that does the work, reading from an
604 * input port and writing to an output port.
606 static __rte_noreturn void
613 printf("\nCore %u Waiting for SYNC packets. [Ctrl+C to quit]\n",
616 /* Run until the application is quit or killed. */
619 /* Read packet from RX queues. 8< */
620 for (portid = 0; portid < ptp_enabled_port_nb; portid++) {
622 portid = ptp_enabled_ports[portid];
623 nb_rx = rte_eth_rx_burst(portid, 0, &m, 1);
625 if (likely(nb_rx == 0))
628 /* Packet is parsed to determine which type. 8< */
629 if (m->ol_flags & RTE_MBUF_F_RX_IEEE1588_PTP)
630 parse_ptp_frames(portid, m);
631 /* >8 End of packet is parsed to determine which type. */
635 /* >8 End of read packets from RX queues. */
640 print_usage(const char *prgname)
642 printf("%s [EAL options] -- -p PORTMASK -T VALUE\n"
643 " -T VALUE: 0 - Disable, 1 - Enable Linux Clock"
644 " Synchronization (0 default)\n"
645 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
650 ptp_parse_portmask(const char *portmask)
655 /* Parse the hexadecimal string. */
656 pm = strtoul(portmask, &end, 16);
658 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
665 parse_ptp_kernel(const char *param)
670 /* Parse the hexadecimal string. */
671 pm = strtoul(param, &end, 16);
673 if ((param[0] == '\0') || (end == NULL) || (*end != '\0'))
681 /* Parse the commandline arguments. */
683 ptp_parse_args(int argc, char **argv)
688 char *prgname = argv[0];
689 static struct option lgopts[] = { {NULL, 0, 0, 0} };
693 while ((opt = getopt_long(argc, argvopt, "p:T:",
694 lgopts, &option_index)) != EOF) {
700 ptp_enabled_port_mask = ptp_parse_portmask(optarg);
701 if (ptp_enabled_port_mask == 0) {
702 printf("invalid portmask\n");
703 print_usage(prgname);
707 /* Time synchronization. */
709 ret = parse_ptp_kernel(optarg);
711 print_usage(prgname);
715 ptp_data.kernel_time_set = ret;
719 print_usage(prgname);
724 argv[optind-1] = prgname;
726 optind = 1; /* Reset getopt lib. */
732 * The main function, which does initialization and calls the per-lcore
736 main(int argc, char *argv[])
742 /* Initialize the Environment Abstraction Layer (EAL). 8< */
743 int ret = rte_eal_init(argc, argv);
746 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
747 /* >8 End of initialization of EAL. */
749 memset(&ptp_data, '\0', sizeof(struct ptpv2_data_slave_ordinary));
751 /* Parse specific arguments. 8< */
755 ret = ptp_parse_args(argc, argv);
757 rte_exit(EXIT_FAILURE, "Error with PTP initialization\n");
758 /* >8 End of parsing specific arguments. */
760 /* Check that there is an even number of ports to send/receive on. */
761 nb_ports = rte_eth_dev_count_avail();
763 /* Creates a new mempool in memory to hold the mbufs. 8< */
764 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
765 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
766 /* >8 End of a new mempool in memory to hold the mbufs. */
768 if (mbuf_pool == NULL)
769 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
771 /* Initialize all ports. 8< */
772 RTE_ETH_FOREACH_DEV(portid) {
773 if ((ptp_enabled_port_mask & (1 << portid)) != 0) {
774 if (port_init(portid, mbuf_pool) == 0) {
775 ptp_enabled_ports[ptp_enabled_port_nb] = portid;
776 ptp_enabled_port_nb++;
778 rte_exit(EXIT_FAILURE,
779 "Cannot init port %"PRIu8 "\n",
783 printf("Skipping disabled port %u\n", portid);
785 /* >8 End of initialization of all ports. */
787 if (ptp_enabled_port_nb == 0) {
788 rte_exit(EXIT_FAILURE,
789 "All available ports are disabled."
790 " Please set portmask.\n");
793 if (rte_lcore_count() > 1)
794 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
796 /* Call lcore_main on the main core only. */
799 /* clean up the EAL */