1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2015 Intel Corporation 3 */ 4 5 6 #include <rte_cycles.h> 7 #include <rte_ethdev.h> 8 #include <rte_flow.h> 9 10 #include "testpmd.h" 11 12 /** 13 * The structure of a PTP V2 packet. 14 * 15 * Only the minimum fields used by the ieee1588 test are represented. 16 */ 17 struct ptpv2_msg { 18 uint8_t msg_id; 19 uint8_t version; /**< must be 0x02 */ 20 uint8_t unused[34]; 21 }; 22 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 33 34 /* 35 * Forwarding of IEEE1588 Precise Time Protocol (PTP) packets. 36 * 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 40 * 0 at offset 0). 41 * 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 44 * by the hardware. 45 * Check that the value of the last RX timestamp recorded by the controller 46 * is greater than the previous one. 47 * 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. 52 */ 53 54 static void 55 port_ieee1588_rx_timestamp_check(portid_t pi, uint32_t index) 56 { 57 struct timespec timestamp = {0, 0}; 58 59 if (rte_eth_timesync_read_rx_timestamp(pi, ×tamp, index) < 0) { 60 printf("Port %u RX timestamp registers not valid\n", pi); 61 return; 62 } 63 printf("Port %u RX timestamp value %ju s %lu ns\n", 64 pi, (uintmax_t)timestamp.tv_sec, timestamp.tv_nsec); 65 } 66 67 #define MAX_TX_TMST_WAIT_MICROSECS 1000 /**< 1 milli-second */ 68 69 static void 70 port_ieee1588_tx_timestamp_check(portid_t pi) 71 { 72 struct timespec timestamp = {0, 0}; 73 unsigned wait_us = 0; 74 75 while ((rte_eth_timesync_read_tx_timestamp(pi, ×tamp) < 0) && 76 (wait_us < MAX_TX_TMST_WAIT_MICROSECS)) { 77 rte_delay_us(1); 78 wait_us++; 79 } 80 if (wait_us >= MAX_TX_TMST_WAIT_MICROSECS) { 81 printf("Port %u TX timestamp registers not valid after " 82 "%u micro-seconds\n", 83 pi, MAX_TX_TMST_WAIT_MICROSECS); 84 return; 85 } 86 printf("Port %u TX timestamp value %ju s %lu ns validated after " 87 "%u micro-second%s\n", 88 pi, (uintmax_t)timestamp.tv_sec, timestamp.tv_nsec, wait_us, 89 (wait_us == 1) ? "" : "s"); 90 } 91 92 static bool 93 ieee1588_packet_fwd(struct fwd_stream *fs) 94 { 95 struct rte_mbuf *mb; 96 struct rte_ether_hdr *eth_hdr; 97 struct rte_ether_addr addr; 98 struct ptpv2_msg *ptp_hdr; 99 uint16_t eth_type; 100 uint32_t timesync_index; 101 102 /* 103 * Receive 1 packet at a time. 104 */ 105 if (common_fwd_stream_receive(fs, &mb, 1) == 0) 106 return false; 107 108 /* 109 * Check that the received packet is a PTP packet that was detected 110 * by the hardware. 111 */ 112 eth_hdr = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *); 113 eth_type = rte_be_to_cpu_16(eth_hdr->ether_type); 114 115 if (!(mb->ol_flags & RTE_MBUF_F_RX_IEEE1588_PTP)) { 116 if (eth_type == RTE_ETHER_TYPE_1588) { 117 printf("Port %u Received PTP packet not filtered" 118 " by hardware\n", 119 fs->rx_port); 120 } else { 121 printf("Port %u Received non PTP packet type=0x%4x " 122 "len=%u\n", 123 fs->rx_port, eth_type, 124 (unsigned) mb->pkt_len); 125 } 126 rte_pktmbuf_free(mb); 127 return false; 128 } 129 if (eth_type != RTE_ETHER_TYPE_1588) { 130 printf("Port %u Received NON PTP packet incorrectly" 131 " detected by hardware\n", 132 fs->rx_port); 133 rte_pktmbuf_free(mb); 134 return false; 135 } 136 137 /* 138 * Check that the received PTP packet is a PTP V2 packet of type 139 * PTP_SYNC_MESSAGE. 140 */ 141 ptp_hdr = rte_pktmbuf_mtod_offset(mb, struct ptpv2_msg *, 142 sizeof(struct rte_ether_hdr)); 143 if (ptp_hdr->version != 0x02) { 144 printf("Port %u Received PTP V2 Ethernet frame with wrong PTP" 145 " protocol version 0x%x (should be 0x02)\n", 146 fs->rx_port, ptp_hdr->version); 147 rte_pktmbuf_free(mb); 148 return false; 149 } 150 if (ptp_hdr->msg_id != PTP_SYNC_MESSAGE) { 151 printf("Port %u Received PTP V2 Ethernet frame with unexpected" 152 " message ID 0x%x (expected 0x0 - PTP_SYNC_MESSAGE)\n", 153 fs->rx_port, ptp_hdr->msg_id); 154 rte_pktmbuf_free(mb); 155 return false; 156 } 157 printf("Port %u IEEE1588 PTP V2 SYNC Message filtered by hardware\n", 158 fs->rx_port); 159 160 /* 161 * Check that the received PTP packet has been timestamped by the 162 * hardware. 163 */ 164 if (!(mb->ol_flags & RTE_MBUF_F_RX_IEEE1588_TMST)) { 165 printf("Port %u Received PTP packet not timestamped" 166 " by hardware\n", 167 fs->rx_port); 168 rte_pktmbuf_free(mb); 169 return false; 170 } 171 172 /* For i40e we need the timesync register index. It is ignored for the 173 * other PMDs. */ 174 timesync_index = mb->timesync & 0x3; 175 /* Read and check the RX timestamp. */ 176 port_ieee1588_rx_timestamp_check(fs->rx_port, timesync_index); 177 178 /* Swap dest and src mac addresses. */ 179 rte_ether_addr_copy(ð_hdr->dst_addr, &addr); 180 rte_ether_addr_copy(ð_hdr->src_addr, ð_hdr->dst_addr); 181 rte_ether_addr_copy(&addr, ð_hdr->src_addr); 182 183 /* Forward PTP packet with hardware TX timestamp */ 184 mb->ol_flags |= RTE_MBUF_F_TX_IEEE1588_TMST; 185 if (common_fwd_stream_transmit(fs, &mb, 1) == 0) { 186 printf("Port %u sent PTP packet dropped\n", fs->tx_port); 187 return false; 188 } 189 190 /* 191 * Check the TX timestamp. 192 */ 193 port_ieee1588_tx_timestamp_check(fs->tx_port); 194 return true; 195 } 196 197 static int 198 port_ieee1588_fwd_begin(portid_t pi) 199 { 200 rte_eth_timesync_enable(pi); 201 return 0; 202 } 203 204 static void 205 port_ieee1588_fwd_end(portid_t pi) 206 { 207 rte_eth_timesync_disable(pi); 208 } 209 210 static void 211 port_ieee1588_stream_init(struct fwd_stream *fs) 212 { 213 /* Force transmission on reception port */ 214 fs->tx_port = fs->rx_port; 215 216 common_fwd_stream_init(fs); 217 } 218 219 struct fwd_engine ieee1588_fwd_engine = { 220 .fwd_mode_name = "ieee1588", 221 .port_fwd_begin = port_ieee1588_fwd_begin, 222 .port_fwd_end = port_ieee1588_fwd_end, 223 .stream_init = port_ieee1588_stream_init, 224 .packet_fwd = ieee1588_packet_fwd, 225 }; 226