Imported Upstream version 16.04
[deb_dpdk.git] / app / test-pmd / ieee1588fwd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34
35 #include <rte_cycles.h>
36 #include <rte_ethdev.h>
37
38 #include "testpmd.h"
39
40 /**
41  * The structure of a PTP V2 packet.
42  *
43  * Only the minimum fields used by the ieee1588 test are represented.
44  */
45 struct ptpv2_msg {
46         uint8_t msg_id;
47         uint8_t version; /**< must be 0x02 */
48         uint8_t unused[34];
49 };
50
51 #define PTP_SYNC_MESSAGE                0x0
52 #define PTP_DELAY_REQ_MESSAGE           0x1
53 #define PTP_PATH_DELAY_REQ_MESSAGE      0x2
54 #define PTP_PATH_DELAY_RESP_MESSAGE     0x3
55 #define PTP_FOLLOWUP_MESSAGE            0x8
56 #define PTP_DELAY_RESP_MESSAGE          0x9
57 #define PTP_PATH_DELAY_FOLLOWUP_MESSAGE 0xA
58 #define PTP_ANNOUNCE_MESSAGE            0xB
59 #define PTP_SIGNALLING_MESSAGE          0xC
60 #define PTP_MANAGEMENT_MESSAGE          0xD
61
62 /*
63  * Forwarding of IEEE1588 Precise Time Protocol (PTP) packets.
64  *
65  * In this mode, packets are received one by one and are expected to be
66  * PTP V2 L2 Ethernet frames (with the specific Ethernet type "0x88F7")
67  * containing PTP "sync" messages (version 2 at offset 1, and message ID
68  * 0 at offset 0).
69  *
70  * Check that each received packet is a IEEE1588 PTP V2 packet of type
71  * PTP_SYNC_MESSAGE, and that it has been identified and timestamped
72  * by the hardware.
73  * Check that the value of the last RX timestamp recorded by the controller
74  * is greater than the previous one.
75  *
76  * If everything is OK, send the received packet back on the same port,
77  * requesting for it to be timestamped by the hardware.
78  * Check that the value of the last TX timestamp recorded by the controller
79  * is greater than the previous one.
80  */
81
82 static void
83 port_ieee1588_rx_timestamp_check(portid_t pi, uint32_t index)
84 {
85         struct timespec timestamp = {0, 0};
86
87         if (rte_eth_timesync_read_rx_timestamp(pi, &timestamp, index) < 0) {
88                 printf("Port %u RX timestamp registers not valid\n",
89                        (unsigned) pi);
90                 return;
91         }
92         printf("Port %u RX timestamp value %lu s %lu ns\n",
93                (unsigned) pi, timestamp.tv_sec, timestamp.tv_nsec);
94 }
95
96 #define MAX_TX_TMST_WAIT_MICROSECS 1000 /**< 1 milli-second */
97
98 static void
99 port_ieee1588_tx_timestamp_check(portid_t pi)
100 {
101         struct timespec timestamp = {0, 0};
102         unsigned wait_us = 0;
103
104         while ((rte_eth_timesync_read_tx_timestamp(pi, &timestamp) < 0) &&
105                (wait_us < MAX_TX_TMST_WAIT_MICROSECS)) {
106                 rte_delay_us(1);
107                 wait_us++;
108         }
109         if (wait_us >= MAX_TX_TMST_WAIT_MICROSECS) {
110                 printf("Port %u TX timestamp registers not valid after "
111                        "%u micro-seconds\n",
112                        (unsigned) pi, (unsigned) MAX_TX_TMST_WAIT_MICROSECS);
113                 return;
114         }
115         printf("Port %u TX timestamp value %lu s %lu ns validated after "
116                "%u micro-second%s\n",
117                (unsigned) pi, timestamp.tv_sec, timestamp.tv_nsec, wait_us,
118                (wait_us == 1) ? "" : "s");
119 }
120
121 static void
122 ieee1588_packet_fwd(struct fwd_stream *fs)
123 {
124         struct rte_mbuf  *mb;
125         struct ether_hdr *eth_hdr;
126         struct ether_addr addr;
127         struct ptpv2_msg *ptp_hdr;
128         uint16_t eth_type;
129         uint32_t timesync_index;
130
131         /*
132          * Receive 1 packet at a time.
133          */
134         if (rte_eth_rx_burst(fs->rx_port, fs->rx_queue, &mb, 1) == 0)
135                 return;
136
137         fs->rx_packets += 1;
138
139         /*
140          * Check that the received packet is a PTP packet that was detected
141          * by the hardware.
142          */
143         eth_hdr = rte_pktmbuf_mtod(mb, struct ether_hdr *);
144         eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
145
146         if (! (mb->ol_flags & PKT_RX_IEEE1588_PTP)) {
147                 if (eth_type == ETHER_TYPE_1588) {
148                         printf("Port %u Received PTP packet not filtered"
149                                " by hardware\n",
150                                (unsigned) fs->rx_port);
151                 } else {
152                         printf("Port %u Received non PTP packet type=0x%4x "
153                                "len=%u\n",
154                                (unsigned) fs->rx_port, eth_type,
155                                (unsigned) mb->pkt_len);
156                 }
157                 rte_pktmbuf_free(mb);
158                 return;
159         }
160         if (eth_type != ETHER_TYPE_1588) {
161                 printf("Port %u Received NON PTP packet incorrectly"
162                        " detected by hardware\n",
163                        (unsigned) fs->rx_port);
164                 rte_pktmbuf_free(mb);
165                 return;
166         }
167
168         /*
169          * Check that the received PTP packet is a PTP V2 packet of type
170          * PTP_SYNC_MESSAGE.
171          */
172         ptp_hdr = (struct ptpv2_msg *) (rte_pktmbuf_mtod(mb, char *) +
173                                         sizeof(struct ether_hdr));
174         if (ptp_hdr->version != 0x02) {
175                 printf("Port %u Received PTP V2 Ethernet frame with wrong PTP"
176                        " protocol version 0x%x (should be 0x02)\n",
177                        (unsigned) fs->rx_port, ptp_hdr->version);
178                 rte_pktmbuf_free(mb);
179                 return;
180         }
181         if (ptp_hdr->msg_id != PTP_SYNC_MESSAGE) {
182                 printf("Port %u Received PTP V2 Ethernet frame with unexpected"
183                        " message ID 0x%x (expected 0x0 - PTP_SYNC_MESSAGE)\n",
184                        (unsigned) fs->rx_port, ptp_hdr->msg_id);
185                 rte_pktmbuf_free(mb);
186                 return;
187         }
188         printf("Port %u IEEE1588 PTP V2 SYNC Message filtered by hardware\n",
189                (unsigned) fs->rx_port);
190
191         /*
192          * Check that the received PTP packet has been timestamped by the
193          * hardware.
194          */
195         if (! (mb->ol_flags & PKT_RX_IEEE1588_TMST)) {
196                 printf("Port %u Received PTP packet not timestamped"
197                        " by hardware\n",
198                        (unsigned) fs->rx_port);
199                 rte_pktmbuf_free(mb);
200                 return;
201         }
202
203         /* For i40e we need the timesync register index. It is ignored for the
204          * other PMDs. */
205         timesync_index = mb->timesync & 0x3;
206         /* Read and check the RX timestamp. */
207         port_ieee1588_rx_timestamp_check(fs->rx_port, timesync_index);
208
209         /* Swap dest and src mac addresses. */
210         ether_addr_copy(&eth_hdr->d_addr, &addr);
211         ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
212         ether_addr_copy(&addr, &eth_hdr->s_addr);
213
214         /* Forward PTP packet with hardware TX timestamp */
215         mb->ol_flags |= PKT_TX_IEEE1588_TMST;
216         fs->tx_packets += 1;
217         if (rte_eth_tx_burst(fs->rx_port, fs->tx_queue, &mb, 1) == 0) {
218                 printf("Port %u sent PTP packet dropped\n",
219                        (unsigned) fs->rx_port);
220                 fs->fwd_dropped += 1;
221                 rte_pktmbuf_free(mb);
222                 return;
223         }
224
225         /*
226          * Check the TX timestamp.
227          */
228         port_ieee1588_tx_timestamp_check(fs->rx_port);
229 }
230
231 static void
232 port_ieee1588_fwd_begin(portid_t pi)
233 {
234         rte_eth_timesync_enable(pi);
235 }
236
237 static void
238 port_ieee1588_fwd_end(portid_t pi)
239 {
240         rte_eth_timesync_disable(pi);
241 }
242
243 struct fwd_engine ieee1588_fwd_engine = {
244         .fwd_mode_name  = "ieee1588",
245         .port_fwd_begin = port_ieee1588_fwd_begin,
246         .port_fwd_end   = port_ieee1588_fwd_end,
247         .packet_fwd     = ieee1588_packet_fwd,
248 };