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_eth_conf port_conf_default = {
52 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
56 static const struct rte_ether_addr ether_multicast = {
57 .addr_bytes = {0x01, 0x1b, 0x19, 0x0, 0x0, 0x0}
60 /* Structs used for PTP handling. */
72 struct clock_id clock_id;
79 uint16_t message_length;
80 uint8_t domain_number;
82 uint8_t flag_field[2];
85 struct port_id source_port_id;
88 int8_t log_message_interval;
92 struct ptp_header hdr;
93 struct tstamp origin_tstamp;
96 struct follow_up_msg {
97 struct ptp_header hdr;
98 struct tstamp precise_origin_tstamp;
102 struct delay_req_msg {
103 struct ptp_header hdr;
104 struct tstamp origin_tstamp;
107 struct delay_resp_msg {
108 struct ptp_header hdr;
109 struct tstamp rx_tstamp;
110 struct port_id req_port_id;
116 struct ptp_header header;
117 struct sync_msg sync;
118 struct delay_req_msg delay_req;
119 struct follow_up_msg follow_up;
120 struct delay_resp_msg delay_resp;
124 struct ptpv2_data_slave_ordinary {
126 struct timespec tstamp1;
127 struct timespec tstamp2;
128 struct timespec tstamp3;
129 struct timespec tstamp4;
130 struct clock_id client_clock_id;
131 struct clock_id master_clock_id;
132 struct timeval new_adj;
136 uint16_t seqID_FOLLOWUP;
138 uint8_t kernel_time_set;
139 uint16_t current_ptp_port;
142 static struct ptpv2_data_slave_ordinary ptp_data;
144 static inline uint64_t timespec64_to_ns(const struct timespec *ts)
146 return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
149 static struct timeval
150 ns_to_timeval(int64_t nsec)
152 struct timespec t_spec = {0, 0};
153 struct timeval t_eval = {0, 0};
158 rem = nsec % NSEC_PER_SEC;
159 t_spec.tv_sec = nsec / NSEC_PER_SEC;
166 t_spec.tv_nsec = rem;
167 t_eval.tv_sec = t_spec.tv_sec;
168 t_eval.tv_usec = t_spec.tv_nsec / 1000;
174 * Initializes a given port using global settings and with the RX buffers
175 * coming from the mbuf_pool passed as a parameter.
178 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
180 struct rte_eth_dev_info dev_info;
181 struct rte_eth_conf port_conf = port_conf_default;
182 const uint16_t rx_rings = 1;
183 const uint16_t tx_rings = 1;
186 uint16_t nb_rxd = RX_RING_SIZE;
187 uint16_t nb_txd = TX_RING_SIZE;
189 if (!rte_eth_dev_is_valid_port(port))
192 retval = rte_eth_dev_info_get(port, &dev_info);
194 printf("Error during getting device (port %u) info: %s\n",
195 port, strerror(-retval));
200 if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TIMESTAMP)
201 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_TIMESTAMP;
203 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
204 port_conf.txmode.offloads |=
205 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
206 /* Force full Tx path in the driver, required for IEEE1588 */
207 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
209 /* Configure the Ethernet device. */
210 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
214 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
218 /* Allocate and set up 1 RX queue per Ethernet port. */
219 for (q = 0; q < rx_rings; q++) {
220 struct rte_eth_rxconf *rxconf;
222 rxconf = &dev_info.default_rxconf;
223 rxconf->offloads = port_conf.rxmode.offloads;
225 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
226 rte_eth_dev_socket_id(port), rxconf, mbuf_pool);
232 /* Allocate and set up 1 TX queue per Ethernet port. */
233 for (q = 0; q < tx_rings; q++) {
234 struct rte_eth_txconf *txconf;
236 txconf = &dev_info.default_txconf;
237 txconf->offloads = port_conf.txmode.offloads;
239 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
240 rte_eth_dev_socket_id(port), txconf);
245 /* Start the Ethernet port. */
246 retval = rte_eth_dev_start(port);
250 /* Enable timesync timestamping for the Ethernet device */
251 retval = rte_eth_timesync_enable(port);
253 printf("Timesync enable failed: %d\n", retval);
257 /* Enable RX in promiscuous mode for the Ethernet device. */
258 retval = rte_eth_promiscuous_enable(port);
260 printf("Promiscuous mode enable failed: %s\n",
261 rte_strerror(-retval));
269 print_clock_info(struct ptpv2_data_slave_ordinary *ptp_data)
272 struct timespec net_time, sys_time;
274 printf("Master Clock id: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
275 ptp_data->master_clock_id.id[0],
276 ptp_data->master_clock_id.id[1],
277 ptp_data->master_clock_id.id[2],
278 ptp_data->master_clock_id.id[3],
279 ptp_data->master_clock_id.id[4],
280 ptp_data->master_clock_id.id[5],
281 ptp_data->master_clock_id.id[6],
282 ptp_data->master_clock_id.id[7]);
284 printf("\nT2 - Slave Clock. %lds %ldns",
285 (ptp_data->tstamp2.tv_sec),
286 (ptp_data->tstamp2.tv_nsec));
288 printf("\nT1 - Master Clock. %lds %ldns ",
289 ptp_data->tstamp1.tv_sec,
290 (ptp_data->tstamp1.tv_nsec));
292 printf("\nT3 - Slave Clock. %lds %ldns",
293 ptp_data->tstamp3.tv_sec,
294 (ptp_data->tstamp3.tv_nsec));
296 printf("\nT4 - Master Clock. %lds %ldns ",
297 ptp_data->tstamp4.tv_sec,
298 (ptp_data->tstamp4.tv_nsec));
300 printf("\nDelta between master and slave clocks:%"PRId64"ns\n",
303 clock_gettime(CLOCK_REALTIME, &sys_time);
304 rte_eth_timesync_read_time(ptp_data->current_ptp_port, &net_time);
306 time_t ts = net_time.tv_sec;
308 printf("\n\nComparison between Linux kernel Time and PTP:");
310 printf("\nCurrent PTP Time: %.24s %.9ld ns",
311 ctime(&ts), net_time.tv_nsec);
313 nsec = (int64_t)timespec64_to_ns(&net_time) -
314 (int64_t)timespec64_to_ns(&sys_time);
315 ptp_data->new_adj = ns_to_timeval(nsec);
317 gettimeofday(&ptp_data->new_adj, NULL);
319 time_t tp = ptp_data->new_adj.tv_sec;
321 printf("\nCurrent SYS Time: %.24s %.6ld ns",
322 ctime(&tp), ptp_data->new_adj.tv_usec);
324 printf("\nDelta between PTP and Linux Kernel time:%"PRId64"ns\n",
327 printf("[Ctrl+C to quit]\n");
329 /* Clear screen and put cursor in column 1, row 1 */
330 printf("\033[2J\033[1;1H");
334 delta_eval(struct ptpv2_data_slave_ordinary *ptp_data)
342 t1 = timespec64_to_ns(&ptp_data->tstamp1);
343 t2 = timespec64_to_ns(&ptp_data->tstamp2);
344 t3 = timespec64_to_ns(&ptp_data->tstamp3);
345 t4 = timespec64_to_ns(&ptp_data->tstamp4);
347 delta = -((int64_t)((t2 - t1) - (t4 - t3))) / 2;
353 * Parse the PTP SYNC message.
356 parse_sync(struct ptpv2_data_slave_ordinary *ptp_data, uint16_t rx_tstamp_idx)
358 struct ptp_header *ptp_hdr;
360 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(ptp_data->m, char *)
361 + sizeof(struct rte_ether_hdr));
362 ptp_data->seqID_SYNC = rte_be_to_cpu_16(ptp_hdr->seq_id);
364 if (ptp_data->ptpset == 0) {
365 rte_memcpy(&ptp_data->master_clock_id,
366 &ptp_hdr->source_port_id.clock_id,
367 sizeof(struct clock_id));
368 ptp_data->ptpset = 1;
371 if (memcmp(&ptp_hdr->source_port_id.clock_id,
372 &ptp_hdr->source_port_id.clock_id,
373 sizeof(struct clock_id)) == 0) {
375 if (ptp_data->ptpset == 1)
376 rte_eth_timesync_read_rx_timestamp(ptp_data->portid,
377 &ptp_data->tstamp2, rx_tstamp_idx);
383 * Parse the PTP FOLLOWUP message and send DELAY_REQ to the main clock.
386 parse_fup(struct ptpv2_data_slave_ordinary *ptp_data)
388 struct rte_ether_hdr *eth_hdr;
389 struct rte_ether_addr eth_addr;
390 struct ptp_header *ptp_hdr;
391 struct clock_id *client_clkid;
392 struct ptp_message *ptp_msg;
393 struct rte_mbuf *created_pkt;
394 struct tstamp *origin_tstamp;
395 struct rte_ether_addr eth_multicast = ether_multicast;
398 struct rte_mbuf *m = ptp_data->m;
401 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
402 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
403 + sizeof(struct rte_ether_hdr));
404 if (memcmp(&ptp_data->master_clock_id,
405 &ptp_hdr->source_port_id.clock_id,
406 sizeof(struct clock_id)) != 0)
409 ptp_data->seqID_FOLLOWUP = rte_be_to_cpu_16(ptp_hdr->seq_id);
410 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
411 sizeof(struct rte_ether_hdr));
413 origin_tstamp = &ptp_msg->follow_up.precise_origin_tstamp;
414 ptp_data->tstamp1.tv_nsec = ntohl(origin_tstamp->ns);
415 ptp_data->tstamp1.tv_sec =
416 ((uint64_t)ntohl(origin_tstamp->sec_lsb)) |
417 (((uint64_t)ntohs(origin_tstamp->sec_msb)) << 32);
419 if (ptp_data->seqID_FOLLOWUP == ptp_data->seqID_SYNC) {
420 ret = rte_eth_macaddr_get(ptp_data->portid, ð_addr);
422 printf("\nCore %u: port %u failed to get MAC address: %s\n",
423 rte_lcore_id(), ptp_data->portid,
428 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
429 pkt_size = sizeof(struct rte_ether_hdr) +
430 sizeof(struct ptp_message);
431 created_pkt->data_len = pkt_size;
432 created_pkt->pkt_len = pkt_size;
433 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
434 rte_ether_addr_copy(ð_addr, ð_hdr->src_addr);
436 /* Set multicast address 01-1B-19-00-00-00. */
437 rte_ether_addr_copy(ð_multicast, ð_hdr->dst_addr);
439 eth_hdr->ether_type = htons(PTP_PROTOCOL);
440 ptp_msg = (struct ptp_message *)
441 (rte_pktmbuf_mtod(created_pkt, char *) +
442 sizeof(struct rte_ether_hdr));
444 ptp_msg->delay_req.hdr.seq_id = htons(ptp_data->seqID_SYNC);
445 ptp_msg->delay_req.hdr.msg_type = DELAY_REQ;
446 ptp_msg->delay_req.hdr.ver = 2;
447 ptp_msg->delay_req.hdr.control = 1;
448 ptp_msg->delay_req.hdr.log_message_interval = 127;
449 ptp_msg->delay_req.hdr.message_length =
450 htons(sizeof(struct delay_req_msg));
451 ptp_msg->delay_req.hdr.domain_number = ptp_hdr->domain_number;
453 /* Set up clock id. */
455 &ptp_msg->delay_req.hdr.source_port_id.clock_id;
457 client_clkid->id[0] = eth_hdr->src_addr.addr_bytes[0];
458 client_clkid->id[1] = eth_hdr->src_addr.addr_bytes[1];
459 client_clkid->id[2] = eth_hdr->src_addr.addr_bytes[2];
460 client_clkid->id[3] = 0xFF;
461 client_clkid->id[4] = 0xFE;
462 client_clkid->id[5] = eth_hdr->src_addr.addr_bytes[3];
463 client_clkid->id[6] = eth_hdr->src_addr.addr_bytes[4];
464 client_clkid->id[7] = eth_hdr->src_addr.addr_bytes[5];
466 rte_memcpy(&ptp_data->client_clock_id,
468 sizeof(struct clock_id));
470 /* Enable flag for hardware timestamping. */
471 created_pkt->ol_flags |= PKT_TX_IEEE1588_TMST;
473 /*Read value from NIC to prevent latching with old value. */
474 rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
477 /* Transmit the packet. */
478 rte_eth_tx_burst(ptp_data->portid, 0, &created_pkt, 1);
481 ptp_data->tstamp3.tv_nsec = 0;
482 ptp_data->tstamp3.tv_sec = 0;
484 /* Wait at least 1 us to read TX timestamp. */
485 while ((rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
486 &ptp_data->tstamp3) < 0) && (wait_us < 1000)) {
494 * Update the kernel time with the difference between it and the current NIC
498 update_kernel_time(void)
501 struct timespec net_time, sys_time;
503 clock_gettime(CLOCK_REALTIME, &sys_time);
504 rte_eth_timesync_read_time(ptp_data.current_ptp_port, &net_time);
506 nsec = (int64_t)timespec64_to_ns(&net_time) -
507 (int64_t)timespec64_to_ns(&sys_time);
509 ptp_data.new_adj = ns_to_timeval(nsec);
512 * If difference between kernel time and system time in NIC is too big
513 * (more than +/- 20 microseconds), use clock_settime to set directly
514 * the kernel time, as adjtime is better for small adjustments (takes
515 * longer to adjust the time).
518 if (nsec > KERNEL_TIME_ADJUST_LIMIT || nsec < -KERNEL_TIME_ADJUST_LIMIT)
519 clock_settime(CLOCK_REALTIME, &net_time);
521 adjtime(&ptp_data.new_adj, 0);
527 * Parse the DELAY_RESP message.
530 parse_drsp(struct ptpv2_data_slave_ordinary *ptp_data)
532 struct rte_mbuf *m = ptp_data->m;
533 struct ptp_message *ptp_msg;
534 struct tstamp *rx_tstamp;
537 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
538 sizeof(struct rte_ether_hdr));
539 seq_id = rte_be_to_cpu_16(ptp_msg->delay_resp.hdr.seq_id);
540 if (memcmp(&ptp_data->client_clock_id,
541 &ptp_msg->delay_resp.req_port_id.clock_id,
542 sizeof(struct clock_id)) == 0) {
543 if (seq_id == ptp_data->seqID_FOLLOWUP) {
544 rx_tstamp = &ptp_msg->delay_resp.rx_tstamp;
545 ptp_data->tstamp4.tv_nsec = ntohl(rx_tstamp->ns);
546 ptp_data->tstamp4.tv_sec =
547 ((uint64_t)ntohl(rx_tstamp->sec_lsb)) |
548 (((uint64_t)ntohs(rx_tstamp->sec_msb)) << 32);
550 /* Evaluate the delta for adjustment. */
551 ptp_data->delta = delta_eval(ptp_data);
553 rte_eth_timesync_adjust_time(ptp_data->portid,
556 ptp_data->current_ptp_port = ptp_data->portid;
558 /* Update kernel time if enabled in app parameters. */
559 if (ptp_data->kernel_time_set == 1)
560 update_kernel_time();
568 /* This function processes PTP packets, implementing slave PTP IEEE1588 L2
572 /* Parse ptp frames. 8< */
574 parse_ptp_frames(uint16_t portid, struct rte_mbuf *m) {
575 struct ptp_header *ptp_hdr;
576 struct rte_ether_hdr *eth_hdr;
579 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
580 eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
582 if (eth_type == PTP_PROTOCOL) {
584 ptp_data.portid = portid;
585 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
586 + sizeof(struct rte_ether_hdr));
588 switch (ptp_hdr->msg_type) {
590 parse_sync(&ptp_data, m->timesync);
593 parse_fup(&ptp_data);
596 parse_drsp(&ptp_data);
597 print_clock_info(&ptp_data);
604 /* >8 End of function processes PTP packets. */
607 * The lcore main. This is the main thread that does the work, reading from an
608 * input port and writing to an output port.
610 static __rte_noreturn void
617 printf("\nCore %u Waiting for SYNC packets. [Ctrl+C to quit]\n",
620 /* Run until the application is quit or killed. */
623 /* Read packet from RX queues. 8< */
624 for (portid = 0; portid < ptp_enabled_port_nb; portid++) {
626 portid = ptp_enabled_ports[portid];
627 nb_rx = rte_eth_rx_burst(portid, 0, &m, 1);
629 if (likely(nb_rx == 0))
632 /* Packet is parsed to determine which type. 8< */
633 if (m->ol_flags & PKT_RX_IEEE1588_PTP)
634 parse_ptp_frames(portid, m);
635 /* >8 End of packet is parsed to determine which type. */
639 /* >8 End of read packets from RX queues. */
644 print_usage(const char *prgname)
646 printf("%s [EAL options] -- -p PORTMASK -T VALUE\n"
647 " -T VALUE: 0 - Disable, 1 - Enable Linux Clock"
648 " Synchronization (0 default)\n"
649 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
654 ptp_parse_portmask(const char *portmask)
659 /* Parse the hexadecimal string. */
660 pm = strtoul(portmask, &end, 16);
662 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
669 parse_ptp_kernel(const char *param)
674 /* Parse the hexadecimal string. */
675 pm = strtoul(param, &end, 16);
677 if ((param[0] == '\0') || (end == NULL) || (*end != '\0'))
685 /* Parse the commandline arguments. */
687 ptp_parse_args(int argc, char **argv)
692 char *prgname = argv[0];
693 static struct option lgopts[] = { {NULL, 0, 0, 0} };
697 while ((opt = getopt_long(argc, argvopt, "p:T:",
698 lgopts, &option_index)) != EOF) {
704 ptp_enabled_port_mask = ptp_parse_portmask(optarg);
705 if (ptp_enabled_port_mask == 0) {
706 printf("invalid portmask\n");
707 print_usage(prgname);
711 /* Time synchronization. */
713 ret = parse_ptp_kernel(optarg);
715 print_usage(prgname);
719 ptp_data.kernel_time_set = ret;
723 print_usage(prgname);
728 argv[optind-1] = prgname;
730 optind = 1; /* Reset getopt lib. */
736 * The main function, which does initialization and calls the per-lcore
740 main(int argc, char *argv[])
746 /* Initialize the Environment Abstraction Layer (EAL). 8< */
747 int ret = rte_eal_init(argc, argv);
750 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
751 /* >8 End of initialization of EAL. */
753 memset(&ptp_data, '\0', sizeof(struct ptpv2_data_slave_ordinary));
755 /* Parse specific arguments. 8< */
759 ret = ptp_parse_args(argc, argv);
761 rte_exit(EXIT_FAILURE, "Error with PTP initialization\n");
762 /* >8 End of parsing specific arguments. */
764 /* Check that there is an even number of ports to send/receive on. */
765 nb_ports = rte_eth_dev_count_avail();
767 /* Creates a new mempool in memory to hold the mbufs. 8< */
768 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
769 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
770 /* >8 End of a new mempool in memory to hold the mbufs. */
772 if (mbuf_pool == NULL)
773 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
775 /* Initialize all ports. 8< */
776 RTE_ETH_FOREACH_DEV(portid) {
777 if ((ptp_enabled_port_mask & (1 << portid)) != 0) {
778 if (port_init(portid, mbuf_pool) == 0) {
779 ptp_enabled_ports[ptp_enabled_port_nb] = portid;
780 ptp_enabled_port_nb++;
782 rte_exit(EXIT_FAILURE,
783 "Cannot init port %"PRIu8 "\n",
787 printf("Skipping disabled port %u\n", portid);
789 /* >8 End of initialization of all ports. */
791 if (ptp_enabled_port_nb == 0) {
792 rte_exit(EXIT_FAILURE,
793 "All available ports are disabled."
794 " Please set portmask.\n");
797 if (rte_lcore_count() > 1)
798 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
800 /* Call lcore_main on the main core only. */
803 /* clean up the EAL */