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 128
24 #define TX_RING_SIZE 512
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 = {
51 .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
54 static const struct ether_addr ether_multicast = {
55 .addr_bytes = {0x01, 0x1b, 0x19, 0x0, 0x0, 0x0}
58 /* Structs used for PTP handling. */
63 } __attribute__((packed));
70 struct clock_id clock_id;
72 } __attribute__((packed));
77 uint16_t message_length;
78 uint8_t domain_number;
80 uint8_t flag_field[2];
83 struct port_id source_port_id;
86 int8_t log_message_interval;
87 } __attribute__((packed));
90 struct ptp_header hdr;
91 struct tstamp origin_tstamp;
92 } __attribute__((packed));
94 struct follow_up_msg {
95 struct ptp_header hdr;
96 struct tstamp precise_origin_tstamp;
98 } __attribute__((packed));
100 struct delay_req_msg {
101 struct ptp_header hdr;
102 struct tstamp origin_tstamp;
103 } __attribute__((packed));
105 struct delay_resp_msg {
106 struct ptp_header hdr;
107 struct tstamp rx_tstamp;
108 struct port_id req_port_id;
110 } __attribute__((packed));
114 struct ptp_header header;
115 struct sync_msg sync;
116 struct delay_req_msg delay_req;
117 struct follow_up_msg follow_up;
118 struct delay_resp_msg delay_resp;
119 } __attribute__((packed));
122 struct ptpv2_data_slave_ordinary {
124 struct timespec tstamp1;
125 struct timespec tstamp2;
126 struct timespec tstamp3;
127 struct timespec tstamp4;
128 struct clock_id client_clock_id;
129 struct clock_id master_clock_id;
130 struct timeval new_adj;
134 uint16_t seqID_FOLLOWUP;
136 uint8_t kernel_time_set;
137 uint16_t current_ptp_port;
140 static struct ptpv2_data_slave_ordinary ptp_data;
142 static inline uint64_t timespec64_to_ns(const struct timespec *ts)
144 return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
147 static struct timeval
148 ns_to_timeval(int64_t nsec)
150 struct timespec t_spec = {0, 0};
151 struct timeval t_eval = {0, 0};
156 rem = nsec % NSEC_PER_SEC;
157 t_spec.tv_sec = nsec / NSEC_PER_SEC;
164 t_spec.tv_nsec = rem;
165 t_eval.tv_sec = t_spec.tv_sec;
166 t_eval.tv_usec = t_spec.tv_nsec / 1000;
172 * Initializes a given port using global settings and with the RX buffers
173 * coming from the mbuf_pool passed as a parameter.
176 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
178 struct rte_eth_dev_info dev_info;
179 struct rte_eth_conf port_conf = port_conf_default;
180 const uint16_t rx_rings = 1;
181 const uint16_t tx_rings = 1;
184 uint16_t nb_rxd = RX_RING_SIZE;
185 uint16_t nb_txd = TX_RING_SIZE;
187 if (port >= rte_eth_dev_count())
190 /* Configure the Ethernet device. */
191 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
195 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
199 /* Allocate and set up 1 RX queue per Ethernet port. */
200 for (q = 0; q < rx_rings; q++) {
201 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
202 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
208 /* Allocate and set up 1 TX queue per Ethernet port. */
209 for (q = 0; q < tx_rings; q++) {
210 /* Setup txq_flags */
211 struct rte_eth_txconf *txconf;
213 rte_eth_dev_info_get(q, &dev_info);
214 txconf = &dev_info.default_txconf;
215 txconf->txq_flags = 0;
217 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
218 rte_eth_dev_socket_id(port), txconf);
223 /* Start the Ethernet port. */
224 retval = rte_eth_dev_start(port);
228 /* Enable timesync timestamping for the Ethernet device */
229 rte_eth_timesync_enable(port);
231 /* Enable RX in promiscuous mode for the Ethernet device. */
232 rte_eth_promiscuous_enable(port);
238 print_clock_info(struct ptpv2_data_slave_ordinary *ptp_data)
241 struct timespec net_time, sys_time;
243 printf("Master Clock id: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
244 ptp_data->master_clock_id.id[0],
245 ptp_data->master_clock_id.id[1],
246 ptp_data->master_clock_id.id[2],
247 ptp_data->master_clock_id.id[3],
248 ptp_data->master_clock_id.id[4],
249 ptp_data->master_clock_id.id[5],
250 ptp_data->master_clock_id.id[6],
251 ptp_data->master_clock_id.id[7]);
253 printf("\nT2 - Slave Clock. %lds %ldns",
254 (ptp_data->tstamp2.tv_sec),
255 (ptp_data->tstamp2.tv_nsec));
257 printf("\nT1 - Master Clock. %lds %ldns ",
258 ptp_data->tstamp1.tv_sec,
259 (ptp_data->tstamp1.tv_nsec));
261 printf("\nT3 - Slave Clock. %lds %ldns",
262 ptp_data->tstamp3.tv_sec,
263 (ptp_data->tstamp3.tv_nsec));
265 printf("\nT4 - Master Clock. %lds %ldns ",
266 ptp_data->tstamp4.tv_sec,
267 (ptp_data->tstamp4.tv_nsec));
269 printf("\nDelta between master and slave clocks:%"PRId64"ns\n",
272 clock_gettime(CLOCK_REALTIME, &sys_time);
273 rte_eth_timesync_read_time(ptp_data->current_ptp_port, &net_time);
275 time_t ts = net_time.tv_sec;
277 printf("\n\nComparison between Linux kernel Time and PTP:");
279 printf("\nCurrent PTP Time: %.24s %.9ld ns",
280 ctime(&ts), net_time.tv_nsec);
282 nsec = (int64_t)timespec64_to_ns(&net_time) -
283 (int64_t)timespec64_to_ns(&sys_time);
284 ptp_data->new_adj = ns_to_timeval(nsec);
286 gettimeofday(&ptp_data->new_adj, NULL);
288 time_t tp = ptp_data->new_adj.tv_sec;
290 printf("\nCurrent SYS Time: %.24s %.6ld ns",
291 ctime(&tp), ptp_data->new_adj.tv_usec);
293 printf("\nDelta between PTP and Linux Kernel time:%"PRId64"ns\n",
296 printf("[Ctrl+C to quit]\n");
298 /* Clear screen and put cursor in column 1, row 1 */
299 printf("\033[2J\033[1;1H");
303 delta_eval(struct ptpv2_data_slave_ordinary *ptp_data)
311 t1 = timespec64_to_ns(&ptp_data->tstamp1);
312 t2 = timespec64_to_ns(&ptp_data->tstamp2);
313 t3 = timespec64_to_ns(&ptp_data->tstamp3);
314 t4 = timespec64_to_ns(&ptp_data->tstamp4);
316 delta = -((int64_t)((t2 - t1) - (t4 - t3))) / 2;
322 * Parse the PTP SYNC message.
325 parse_sync(struct ptpv2_data_slave_ordinary *ptp_data, uint16_t rx_tstamp_idx)
327 struct ptp_header *ptp_hdr;
329 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(ptp_data->m, char *)
330 + sizeof(struct ether_hdr));
331 ptp_data->seqID_SYNC = rte_be_to_cpu_16(ptp_hdr->seq_id);
333 if (ptp_data->ptpset == 0) {
334 rte_memcpy(&ptp_data->master_clock_id,
335 &ptp_hdr->source_port_id.clock_id,
336 sizeof(struct clock_id));
337 ptp_data->ptpset = 1;
340 if (memcmp(&ptp_hdr->source_port_id.clock_id,
341 &ptp_hdr->source_port_id.clock_id,
342 sizeof(struct clock_id)) == 0) {
344 if (ptp_data->ptpset == 1)
345 rte_eth_timesync_read_rx_timestamp(ptp_data->portid,
346 &ptp_data->tstamp2, rx_tstamp_idx);
352 * Parse the PTP FOLLOWUP message and send DELAY_REQ to the master clock.
355 parse_fup(struct ptpv2_data_slave_ordinary *ptp_data)
357 struct ether_hdr *eth_hdr;
358 struct ptp_header *ptp_hdr;
359 struct clock_id *client_clkid;
360 struct ptp_message *ptp_msg;
361 struct rte_mbuf *created_pkt;
362 struct tstamp *origin_tstamp;
363 struct ether_addr eth_multicast = ether_multicast;
366 struct rte_mbuf *m = ptp_data->m;
368 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
369 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
370 + sizeof(struct ether_hdr));
371 if (memcmp(&ptp_data->master_clock_id,
372 &ptp_hdr->source_port_id.clock_id,
373 sizeof(struct clock_id)) != 0)
376 ptp_data->seqID_FOLLOWUP = rte_be_to_cpu_16(ptp_hdr->seq_id);
377 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
378 sizeof(struct ether_hdr));
380 origin_tstamp = &ptp_msg->follow_up.precise_origin_tstamp;
381 ptp_data->tstamp1.tv_nsec = ntohl(origin_tstamp->ns);
382 ptp_data->tstamp1.tv_sec =
383 ((uint64_t)ntohl(origin_tstamp->sec_lsb)) |
384 (((uint64_t)ntohs(origin_tstamp->sec_msb)) << 32);
386 if (ptp_data->seqID_FOLLOWUP == ptp_data->seqID_SYNC) {
388 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
389 pkt_size = sizeof(struct ether_hdr) +
390 sizeof(struct ptp_message);
391 created_pkt->data_len = pkt_size;
392 created_pkt->pkt_len = pkt_size;
393 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct ether_hdr *);
394 rte_eth_macaddr_get(ptp_data->portid, ð_hdr->s_addr);
396 /* Set multicast address 01-1B-19-00-00-00. */
397 ether_addr_copy(ð_multicast, ð_hdr->d_addr);
399 eth_hdr->ether_type = htons(PTP_PROTOCOL);
400 ptp_msg = (struct ptp_message *)
401 (rte_pktmbuf_mtod(created_pkt, char *) +
402 sizeof(struct ether_hdr));
404 ptp_msg->delay_req.hdr.seq_id = htons(ptp_data->seqID_SYNC);
405 ptp_msg->delay_req.hdr.msg_type = DELAY_REQ;
406 ptp_msg->delay_req.hdr.ver = 2;
407 ptp_msg->delay_req.hdr.control = 1;
408 ptp_msg->delay_req.hdr.log_message_interval = 127;
410 /* Set up clock id. */
412 &ptp_msg->delay_req.hdr.source_port_id.clock_id;
414 client_clkid->id[0] = eth_hdr->s_addr.addr_bytes[0];
415 client_clkid->id[1] = eth_hdr->s_addr.addr_bytes[1];
416 client_clkid->id[2] = eth_hdr->s_addr.addr_bytes[2];
417 client_clkid->id[3] = 0xFF;
418 client_clkid->id[4] = 0xFE;
419 client_clkid->id[5] = eth_hdr->s_addr.addr_bytes[3];
420 client_clkid->id[6] = eth_hdr->s_addr.addr_bytes[4];
421 client_clkid->id[7] = eth_hdr->s_addr.addr_bytes[5];
423 rte_memcpy(&ptp_data->client_clock_id,
425 sizeof(struct clock_id));
427 /* Enable flag for hardware timestamping. */
428 created_pkt->ol_flags |= PKT_TX_IEEE1588_TMST;
430 /*Read value from NIC to prevent latching with old value. */
431 rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
434 /* Transmit the packet. */
435 rte_eth_tx_burst(ptp_data->portid, 0, &created_pkt, 1);
438 ptp_data->tstamp3.tv_nsec = 0;
439 ptp_data->tstamp3.tv_sec = 0;
441 /* Wait at least 1 us to read TX timestamp. */
442 while ((rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
443 &ptp_data->tstamp3) < 0) && (wait_us < 1000)) {
451 * Update the kernel time with the difference between it and the current NIC
455 update_kernel_time(void)
458 struct timespec net_time, sys_time;
460 clock_gettime(CLOCK_REALTIME, &sys_time);
461 rte_eth_timesync_read_time(ptp_data.current_ptp_port, &net_time);
463 nsec = (int64_t)timespec64_to_ns(&net_time) -
464 (int64_t)timespec64_to_ns(&sys_time);
466 ptp_data.new_adj = ns_to_timeval(nsec);
469 * If difference between kernel time and system time in NIC is too big
470 * (more than +/- 20 microseconds), use clock_settime to set directly
471 * the kernel time, as adjtime is better for small adjustments (takes
472 * longer to adjust the time).
475 if (nsec > KERNEL_TIME_ADJUST_LIMIT || nsec < -KERNEL_TIME_ADJUST_LIMIT)
476 clock_settime(CLOCK_REALTIME, &net_time);
478 adjtime(&ptp_data.new_adj, 0);
484 * Parse the DELAY_RESP message.
487 parse_drsp(struct ptpv2_data_slave_ordinary *ptp_data)
489 struct rte_mbuf *m = ptp_data->m;
490 struct ptp_message *ptp_msg;
491 struct tstamp *rx_tstamp;
494 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
495 sizeof(struct ether_hdr));
496 seq_id = rte_be_to_cpu_16(ptp_msg->delay_resp.hdr.seq_id);
497 if (memcmp(&ptp_data->client_clock_id,
498 &ptp_msg->delay_resp.req_port_id.clock_id,
499 sizeof(struct clock_id)) == 0) {
500 if (seq_id == ptp_data->seqID_FOLLOWUP) {
501 rx_tstamp = &ptp_msg->delay_resp.rx_tstamp;
502 ptp_data->tstamp4.tv_nsec = ntohl(rx_tstamp->ns);
503 ptp_data->tstamp4.tv_sec =
504 ((uint64_t)ntohl(rx_tstamp->sec_lsb)) |
505 (((uint64_t)ntohs(rx_tstamp->sec_msb)) << 32);
507 /* Evaluate the delta for adjustment. */
508 ptp_data->delta = delta_eval(ptp_data);
510 rte_eth_timesync_adjust_time(ptp_data->portid,
513 ptp_data->current_ptp_port = ptp_data->portid;
515 /* Update kernel time if enabled in app parameters. */
516 if (ptp_data->kernel_time_set == 1)
517 update_kernel_time();
525 /* This function processes PTP packets, implementing slave PTP IEEE1588 L2
529 parse_ptp_frames(uint16_t portid, struct rte_mbuf *m) {
530 struct ptp_header *ptp_hdr;
531 struct ether_hdr *eth_hdr;
534 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
535 eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
537 if (eth_type == PTP_PROTOCOL) {
539 ptp_data.portid = portid;
540 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
541 + sizeof(struct ether_hdr));
543 switch (ptp_hdr->msg_type) {
545 parse_sync(&ptp_data, m->timesync);
548 parse_fup(&ptp_data);
551 parse_drsp(&ptp_data);
552 print_clock_info(&ptp_data);
561 * The lcore main. This is the main thread that does the work, reading from an
562 * input port and writing to an output port.
564 static __attribute__((noreturn)) void
572 * Check that the port is on the same NUMA node as the polling thread
573 * for best performance.
575 printf("\nCore %u Waiting for SYNC packets. [Ctrl+C to quit]\n",
578 /* Run until the application is quit or killed. */
581 /* Read packet from RX queues. */
582 for (portid = 0; portid < ptp_enabled_port_nb; portid++) {
584 portid = ptp_enabled_ports[portid];
585 nb_rx = rte_eth_rx_burst(portid, 0, &m, 1);
587 if (likely(nb_rx == 0))
590 if (m->ol_flags & PKT_RX_IEEE1588_PTP)
591 parse_ptp_frames(portid, m);
599 print_usage(const char *prgname)
601 printf("%s [EAL options] -- -p PORTMASK -T VALUE\n"
602 " -T VALUE: 0 - Disable, 1 - Enable Linux Clock"
603 " Synchronization (0 default)\n"
604 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
609 ptp_parse_portmask(const char *portmask)
614 /* Parse the hexadecimal string. */
615 pm = strtoul(portmask, &end, 16);
617 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
627 parse_ptp_kernel(const char *param)
632 /* Parse the hexadecimal string. */
633 pm = strtoul(param, &end, 16);
635 if ((param[0] == '\0') || (end == NULL) || (*end != '\0'))
643 /* Parse the commandline arguments. */
645 ptp_parse_args(int argc, char **argv)
650 char *prgname = argv[0];
651 static struct option lgopts[] = { {NULL, 0, 0, 0} };
655 while ((opt = getopt_long(argc, argvopt, "p:T:",
656 lgopts, &option_index)) != EOF) {
662 ptp_enabled_port_mask = ptp_parse_portmask(optarg);
663 if (ptp_enabled_port_mask == 0) {
664 printf("invalid portmask\n");
665 print_usage(prgname);
669 /* Time synchronization. */
671 ret = parse_ptp_kernel(optarg);
673 print_usage(prgname);
677 ptp_data.kernel_time_set = ret;
681 print_usage(prgname);
686 argv[optind-1] = prgname;
688 optind = 1; /* Reset getopt lib. */
694 * The main function, which does initialization and calls the per-lcore
698 main(int argc, char *argv[])
704 /* Initialize the Environment Abstraction Layer (EAL). */
705 int ret = rte_eal_init(argc, argv);
708 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
710 memset(&ptp_data, '\0', sizeof(struct ptpv2_data_slave_ordinary));
715 ret = ptp_parse_args(argc, argv);
717 rte_exit(EXIT_FAILURE, "Error with PTP initialization\n");
719 /* Check that there is an even number of ports to send/receive on. */
720 nb_ports = rte_eth_dev_count();
722 /* Creates a new mempool in memory to hold the mbufs. */
723 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
724 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
726 if (mbuf_pool == NULL)
727 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
729 /* Initialize all ports. */
730 for (portid = 0; portid < nb_ports; portid++) {
731 if ((ptp_enabled_port_mask & (1 << portid)) != 0) {
732 if (port_init(portid, mbuf_pool) == 0) {
733 ptp_enabled_ports[ptp_enabled_port_nb] = portid;
734 ptp_enabled_port_nb++;
736 rte_exit(EXIT_FAILURE,
737 "Cannot init port %"PRIu8 "\n",
741 printf("Skipping disabled port %u\n", portid);
744 if (ptp_enabled_port_nb == 0) {
745 rte_exit(EXIT_FAILURE,
746 "All available ports are disabled."
747 " Please set portmask.\n");
750 if (rte_lcore_count() > 1)
751 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
753 /* Call lcore_main on the master core only. */