dpdk/app/test-pmd/ieee1588fwd.c
<<
>>
Prefs
   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 */
  17struct 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
  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, &timestamp, 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
  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, &timestamp) < 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
  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         * Receive 1 packet at a time.
 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         * Check that the received packet is a PTP packet that was detected
 112         * by the hardware.
 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         * Check that the received PTP packet is a PTP V2 packet of type
 141         * PTP_SYNC_MESSAGE.
 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         * Check that the received PTP packet has been timestamped by the
 164         * hardware.
 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        /* For i40e we need the timesync register index. It is ignored for the
 175         * other PMDs. */
 176        timesync_index = mb->timesync & 0x3;
 177        /* Read and check the RX timestamp. */
 178        port_ieee1588_rx_timestamp_check(fs->rx_port, timesync_index);
 179
 180        /* Swap dest and src mac addresses. */
 181        rte_ether_addr_copy(&eth_hdr->d_addr, &addr);
 182        rte_ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
 183        rte_ether_addr_copy(&addr, &eth_hdr->s_addr);
 184
 185        /* Forward PTP packet with hardware TX timestamp */
 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         * Check the TX timestamp.
 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