1
2
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
14
15
16
17struct ptpv2_msg {
18 uint8_t msg_id;
19 uint8_t version;
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54static void
55port_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 %lu s %lu ns\n",
64 pi, timestamp.tv_sec, timestamp.tv_nsec);
65}
66
67#define MAX_TX_TMST_WAIT_MICROSECS 1000
68
69static void
70port_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 %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");
90}
91
92static void
93ieee1588_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
104
105 if (rte_eth_rx_burst(fs->rx_port, fs->rx_queue, &mb, 1) == 0)
106 return;
107
108 fs->rx_packets += 1;
109
110
111
112
113
114 eth_hdr = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
115 eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
116
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"
120 " by hardware\n",
121 fs->rx_port);
122 } else {
123 printf("Port %u Received non PTP packet type=0x%4x "
124 "len=%u\n",
125 fs->rx_port, eth_type,
126 (unsigned) mb->pkt_len);
127 }
128 rte_pktmbuf_free(mb);
129 return;
130 }
131 if (eth_type != RTE_ETHER_TYPE_1588) {
132 printf("Port %u Received NON PTP packet incorrectly"
133 " detected by hardware\n",
134 fs->rx_port);
135 rte_pktmbuf_free(mb);
136 return;
137 }
138
139
140
141
142
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);
150 return;
151 }
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);
157 return;
158 }
159 printf("Port %u IEEE1588 PTP V2 SYNC Message filtered by hardware\n",
160 fs->rx_port);
161
162
163
164
165
166 if (! (mb->ol_flags & PKT_RX_IEEE1588_TMST)) {
167 printf("Port %u Received PTP packet not timestamped"
168 " by hardware\n",
169 fs->rx_port);
170 rte_pktmbuf_free(mb);
171 return;
172 }
173
174
175
176 timesync_index = mb->timesync & 0x3;
177
178 port_ieee1588_rx_timestamp_check(fs->rx_port, timesync_index);
179
180
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);
184
185
186 mb->ol_flags |= PKT_TX_IEEE1588_TMST;
187 fs->tx_packets += 1;
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);
192 return;
193 }
194
195
196
197
198 port_ieee1588_tx_timestamp_check(fs->rx_port);
199}
200
201static void
202port_ieee1588_fwd_begin(portid_t pi)
203{
204 rte_eth_timesync_enable(pi);
205}
206
207static void
208port_ieee1588_fwd_end(portid_t pi)
209{
210 rte_eth_timesync_disable(pi);
211}
212
213struct 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,
218};
219