LCOV - code coverage report
Current view: top level - app/test-pmd - ieee1588fwd.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 71 0.0 %
Date: 2024-12-01 18:57:19 Functions: 0 6 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       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                 :          0 : port_ieee1588_rx_timestamp_check(portid_t pi, uint32_t index)
      56                 :            : {
      57                 :          0 :         struct timespec timestamp = {0, 0};
      58                 :            : 
      59                 :          0 :         if (rte_eth_timesync_read_rx_timestamp(pi, &timestamp, index) < 0) {
      60                 :            :                 printf("Port %u RX timestamp registers not valid\n", pi);
      61                 :          0 :                 return;
      62                 :            :         }
      63                 :          0 :         printf("Port %u RX timestamp value %ju s %lu ns\n",
      64                 :          0 :                 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                 :          0 : port_ieee1588_tx_timestamp_check(portid_t pi)
      71                 :            : {
      72                 :          0 :         struct timespec timestamp = {0, 0};
      73                 :            :         unsigned wait_us = 0;
      74                 :            : 
      75                 :          0 :         while ((rte_eth_timesync_read_tx_timestamp(pi, &timestamp) < 0) &&
      76                 :            :                (wait_us < MAX_TX_TMST_WAIT_MICROSECS)) {
      77                 :          0 :                 rte_delay_us(1);
      78                 :          0 :                 wait_us++;
      79                 :            :         }
      80                 :          0 :         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                 :          0 :                 return;
      85                 :            :         }
      86                 :          0 :         printf("Port %u TX timestamp value %ju s %lu ns validated after "
      87                 :            :                "%u micro-second%s\n",
      88                 :          0 :                pi, (uintmax_t)timestamp.tv_sec, timestamp.tv_nsec, wait_us,
      89                 :            :                (wait_us == 1) ? "" : "s");
      90                 :            : }
      91                 :            : 
      92                 :            : static bool
      93                 :          0 : 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                 :          0 :         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                 :          0 :         eth_hdr = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
     113                 :          0 :         eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
     114                 :            : 
     115                 :          0 :         if (!(mb->ol_flags & RTE_MBUF_F_RX_IEEE1588_PTP)) {
     116                 :          0 :                 if (eth_type == RTE_ETHER_TYPE_1588) {
     117                 :          0 :                         printf("Port %u Received PTP packet not filtered"
     118                 :            :                                " by hardware\n",
     119                 :          0 :                                fs->rx_port);
     120                 :            :                 } else {
     121                 :          0 :                         printf("Port %u Received non PTP packet type=0x%4x "
     122                 :            :                                "len=%u\n",
     123                 :          0 :                                fs->rx_port, eth_type,
     124                 :          0 :                                (unsigned) mb->pkt_len);
     125                 :            :                 }
     126                 :          0 :                 rte_pktmbuf_free(mb);
     127                 :          0 :                 return false;
     128                 :            :         }
     129                 :          0 :         if (eth_type != RTE_ETHER_TYPE_1588) {
     130                 :          0 :                 printf("Port %u Received NON PTP packet incorrectly"
     131                 :            :                        " detected by hardware\n",
     132                 :          0 :                        fs->rx_port);
     133                 :          0 :                 rte_pktmbuf_free(mb);
     134                 :          0 :                 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                 :          0 :         ptp_hdr = rte_pktmbuf_mtod_offset(mb, struct ptpv2_msg *,
     142                 :            :                                           sizeof(struct rte_ether_hdr));
     143                 :          0 :         if (ptp_hdr->version != 0x02) {
     144                 :          0 :                 printf("Port %u Received PTP V2 Ethernet frame with wrong PTP"
     145                 :            :                        " protocol version 0x%x (should be 0x02)\n",
     146                 :          0 :                        fs->rx_port, ptp_hdr->version);
     147                 :          0 :                 rte_pktmbuf_free(mb);
     148                 :          0 :                 return false;
     149                 :            :         }
     150                 :          0 :         if (ptp_hdr->msg_id != PTP_SYNC_MESSAGE) {
     151                 :          0 :                 printf("Port %u Received PTP V2 Ethernet frame with unexpected"
     152                 :            :                        " message ID 0x%x (expected 0x0 - PTP_SYNC_MESSAGE)\n",
     153                 :          0 :                        fs->rx_port, ptp_hdr->msg_id);
     154                 :          0 :                 rte_pktmbuf_free(mb);
     155                 :          0 :                 return false;
     156                 :            :         }
     157                 :          0 :         printf("Port %u IEEE1588 PTP V2 SYNC Message filtered by hardware\n",
     158                 :          0 :                fs->rx_port);
     159                 :            : 
     160                 :            :         /*
     161                 :            :          * Check that the received PTP packet has been timestamped by the
     162                 :            :          * hardware.
     163                 :            :          */
     164                 :          0 :         if (!(mb->ol_flags & RTE_MBUF_F_RX_IEEE1588_TMST)) {
     165                 :          0 :                 printf("Port %u Received PTP packet not timestamped"
     166                 :            :                        " by hardware\n",
     167                 :          0 :                        fs->rx_port);
     168                 :          0 :                 rte_pktmbuf_free(mb);
     169                 :          0 :                 return false;
     170                 :            :         }
     171                 :            : 
     172                 :            :         /* For i40e we need the timesync register index. It is ignored for the
     173                 :            :          * other PMDs. */
     174                 :          0 :         timesync_index = mb->timesync & 0x3;
     175                 :            :         /* Read and check the RX timestamp. */
     176                 :          0 :         port_ieee1588_rx_timestamp_check(fs->rx_port, timesync_index);
     177                 :            : 
     178                 :            :         /* Swap dest and src mac addresses. */
     179                 :            :         rte_ether_addr_copy(&eth_hdr->dst_addr, &addr);
     180                 :            :         rte_ether_addr_copy(&eth_hdr->src_addr, &eth_hdr->dst_addr);
     181                 :            :         rte_ether_addr_copy(&addr, &eth_hdr->src_addr);
     182                 :            : 
     183                 :            :         /* Forward PTP packet with hardware TX timestamp */
     184                 :          0 :         mb->ol_flags |= RTE_MBUF_F_TX_IEEE1588_TMST;
     185                 :          0 :         if (common_fwd_stream_transmit(fs, &mb, 1) == 0) {
     186                 :          0 :                 printf("Port %u sent PTP packet dropped\n", fs->tx_port);
     187                 :          0 :                 return false;
     188                 :            :         }
     189                 :            : 
     190                 :            :         /*
     191                 :            :          * Check the TX timestamp.
     192                 :            :          */
     193                 :          0 :         port_ieee1588_tx_timestamp_check(fs->tx_port);
     194                 :          0 :         return true;
     195                 :            : }
     196                 :            : 
     197                 :            : static int
     198                 :          0 : port_ieee1588_fwd_begin(portid_t pi)
     199                 :            : {
     200                 :            :         int ret;
     201                 :            : 
     202                 :          0 :         ret = rte_eth_timesync_enable(pi);
     203                 :          0 :         if (ret)
     204                 :            :                 printf("Port %u enable PTP failed, ret = %d\n", pi, ret);
     205                 :            : 
     206                 :          0 :         return ret;
     207                 :            : }
     208                 :            : 
     209                 :            : static void
     210                 :          0 : port_ieee1588_fwd_end(portid_t pi)
     211                 :            : {
     212                 :            :         int ret;
     213                 :            : 
     214                 :          0 :         ret = rte_eth_timesync_disable(pi);
     215                 :          0 :         if (ret)
     216                 :            :                 printf("Port %u disable PTP failed, ret = %d\n", pi, ret);
     217                 :          0 : }
     218                 :            : 
     219                 :            : static void
     220                 :          0 : port_ieee1588_stream_init(struct fwd_stream *fs)
     221                 :            : {
     222                 :            :         /* Force transmission on reception port */
     223                 :          0 :         fs->tx_port = fs->rx_port;
     224                 :            : 
     225                 :          0 :         common_fwd_stream_init(fs);
     226                 :          0 : }
     227                 :            : 
     228                 :            : struct fwd_engine ieee1588_fwd_engine = {
     229                 :            :         .fwd_mode_name  = "ieee1588",
     230                 :            :         .port_fwd_begin = port_ieee1588_fwd_begin,
     231                 :            :         .port_fwd_end   = port_ieee1588_fwd_end,
     232                 :            :         .stream_init    = port_ieee1588_stream_init,
     233                 :            :         .packet_fwd     = ieee1588_packet_fwd,
     234                 :            : };

Generated by: LCOV version 1.14