1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
6 #include <rte_cycles.h>
7 #include <rte_ethdev.h>
13 * The structure of a PTP V2 packet.
15 * Only the minimum fields used by the ieee1588 test are represented.
19 uint8_t version; /**< must be 0x02 */
23 #define PTP_SYNC_MESSAGE 0x0
24 #define PTP_DELAY_REQ_MESSAGE 0x1
25 #define PTP_PATH_DELAY_REQ_MESSAGE 0x2
26 #define PTP_PATH_DELAY_RESP_MESSAGE 0x3
27 #define PTP_FOLLOWUP_MESSAGE 0x8
28 #define PTP_DELAY_RESP_MESSAGE 0x9
29 #define PTP_PATH_DELAY_FOLLOWUP_MESSAGE 0xA
30 #define PTP_ANNOUNCE_MESSAGE 0xB
31 #define PTP_SIGNALLING_MESSAGE 0xC
32 #define PTP_MANAGEMENT_MESSAGE 0xD
35 * Forwarding of IEEE1588 Precise Time Protocol (PTP) packets.
37 * In this mode, packets are received one by one and are expected to be
38 * PTP V2 L2 Ethernet frames (with the specific Ethernet type "0x88F7")
39 * containing PTP "sync" messages (version 2 at offset 1, and message ID
42 * Check that each received packet is a IEEE1588 PTP V2 packet of type
43 * PTP_SYNC_MESSAGE, and that it has been identified and timestamped
45 * Check that the value of the last RX timestamp recorded by the controller
46 * is greater than the previous one.
48 * If everything is OK, send the received packet back on the same port,
49 * requesting for it to be timestamped by the hardware.
50 * Check that the value of the last TX timestamp recorded by the controller
51 * is greater than the previous one.
55 port_ieee1588_rx_timestamp_check(portid_t pi, uint32_t index)
57 struct timespec timestamp = {0, 0};
59 if (rte_eth_timesync_read_rx_timestamp(pi, ×tamp, index) < 0) {
60 printf("Port %u RX timestamp registers not valid\n", pi);
63 printf("Port %u RX timestamp value %lu s %lu ns\n",
64 pi, timestamp.tv_sec, timestamp.tv_nsec);
67 #define MAX_TX_TMST_WAIT_MICROSECS 1000 /**< 1 milli-second */
70 port_ieee1588_tx_timestamp_check(portid_t pi)
72 struct timespec timestamp = {0, 0};
75 while ((rte_eth_timesync_read_tx_timestamp(pi, ×tamp) < 0) &&
76 (wait_us < MAX_TX_TMST_WAIT_MICROSECS)) {
80 if (wait_us >= MAX_TX_TMST_WAIT_MICROSECS) {
81 printf("Port %u TX timestamp registers not valid after "
83 pi, MAX_TX_TMST_WAIT_MICROSECS);
86 printf("Port %u TX timestamp value %lu s %lu ns validated after "
87 "%u micro-second%s\n",
88 pi, timestamp.tv_sec, timestamp.tv_nsec, wait_us,
89 (wait_us == 1) ? "" : "s");
93 ieee1588_packet_fwd(struct fwd_stream *fs)
96 struct rte_ether_hdr *eth_hdr;
97 struct rte_ether_addr addr;
98 struct ptpv2_msg *ptp_hdr;
100 uint32_t timesync_index;
103 * Receive 1 packet at a time.
105 if (rte_eth_rx_burst(fs->rx_port, fs->rx_queue, &mb, 1) == 0)
111 * Check that the received packet is a PTP packet that was detected
114 eth_hdr = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
115 eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
117 if (! (mb->ol_flags & PKT_RX_IEEE1588_PTP)) {
118 if (eth_type == RTE_ETHER_TYPE_1588) {
119 printf("Port %u Received PTP packet not filtered"
123 printf("Port %u Received non PTP packet type=0x%4x "
125 fs->rx_port, eth_type,
126 (unsigned) mb->pkt_len);
128 rte_pktmbuf_free(mb);
131 if (eth_type != RTE_ETHER_TYPE_1588) {
132 printf("Port %u Received NON PTP packet incorrectly"
133 " detected by hardware\n",
135 rte_pktmbuf_free(mb);
140 * Check that the received PTP packet is a PTP V2 packet of type
143 ptp_hdr = (struct ptpv2_msg *) (rte_pktmbuf_mtod(mb, char *) +
144 sizeof(struct rte_ether_hdr));
145 if (ptp_hdr->version != 0x02) {
146 printf("Port %u Received PTP V2 Ethernet frame with wrong PTP"
147 " protocol version 0x%x (should be 0x02)\n",
148 fs->rx_port, ptp_hdr->version);
149 rte_pktmbuf_free(mb);
152 if (ptp_hdr->msg_id != PTP_SYNC_MESSAGE) {
153 printf("Port %u Received PTP V2 Ethernet frame with unexpected"
154 " message ID 0x%x (expected 0x0 - PTP_SYNC_MESSAGE)\n",
155 fs->rx_port, ptp_hdr->msg_id);
156 rte_pktmbuf_free(mb);
159 printf("Port %u IEEE1588 PTP V2 SYNC Message filtered by hardware\n",
163 * Check that the received PTP packet has been timestamped by the
166 if (! (mb->ol_flags & PKT_RX_IEEE1588_TMST)) {
167 printf("Port %u Received PTP packet not timestamped"
170 rte_pktmbuf_free(mb);
174 /* For i40e we need the timesync register index. It is ignored for the
176 timesync_index = mb->timesync & 0x3;
177 /* Read and check the RX timestamp. */
178 port_ieee1588_rx_timestamp_check(fs->rx_port, timesync_index);
180 /* Swap dest and src mac addresses. */
181 rte_ether_addr_copy(ð_hdr->d_addr, &addr);
182 rte_ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
183 rte_ether_addr_copy(&addr, ð_hdr->s_addr);
185 /* Forward PTP packet with hardware TX timestamp */
186 mb->ol_flags |= PKT_TX_IEEE1588_TMST;
188 if (rte_eth_tx_burst(fs->rx_port, fs->tx_queue, &mb, 1) == 0) {
189 printf("Port %u sent PTP packet dropped\n", fs->rx_port);
190 fs->fwd_dropped += 1;
191 rte_pktmbuf_free(mb);
196 * Check the TX timestamp.
198 port_ieee1588_tx_timestamp_check(fs->rx_port);
202 port_ieee1588_fwd_begin(portid_t pi)
204 rte_eth_timesync_enable(pi);
208 port_ieee1588_fwd_end(portid_t pi)
210 rte_eth_timesync_disable(pi);
213 struct fwd_engine ieee1588_fwd_engine = {
214 .fwd_mode_name = "ieee1588",
215 .port_fwd_begin = port_ieee1588_fwd_begin,
216 .port_fwd_end = port_ieee1588_fwd_end,
217 .packet_fwd = ieee1588_packet_fwd,