LCOV - code coverage report
Current view: top level - dpdklibs/src/udp - PacketCtor.cpp (source / functions) Coverage Total Hit
Test: code.result Lines: 0.0 % 88 0
Test Date: 2026-10-11 15:05:20 Functions: 0.0 % 7 0

            Line data    Source code
       1              : #include <rte_ethdev.h>
       2              : 
       3              : #include "dpdklibs/ipv4_addr.hpp"
       4              : #include "dpdklibs/udp/PacketCtor.hpp"
       5              : #include "dpdklibs/udp/Utils.hpp"
       6              : 
       7              : #include "detdataformats/DAQEthHeader.hpp"
       8              : 
       9              : #define IPV4_VERSION 4
      10              : 
      11              : // DEFAULT_TTL             = 8 -> TTL on wireshark 5
      12              : // DEFAULT_TTL             = 5 -> TTL on wireshark 2
      13              : // DEFAULT_TTL             = 4 -> TTL on wireshark 1
      14              : 
      15              : namespace dunedaq {
      16              : namespace dpdklibs {
      17              : namespace udp {
      18              : 
      19              : enum
      20              : {
      21              :   MIN_TOS = 0,
      22              :   DEFAULT_TOS = MIN_TOS,
      23              :   DEFAULT_TTL = 8
      24              : };
      25              : //// Defined as static in DPDK
      26              : static int8_t
      27            0 : get_xdigit(char ch)
      28              : {
      29            0 :   if (ch >= '0' && ch <= '9')
      30            0 :     return ch - '0';
      31            0 :   if (ch >= 'a' && ch <= 'f')
      32            0 :     return ch - 'a' + 10;
      33            0 :   if (ch >= 'A' && ch <= 'F')
      34            0 :     return ch - 'A' + 10;
      35              :   return -1;
      36              : }
      37              : 
      38              : bool
      39            0 : get_ether_addr6(const char* s0, struct rte_ether_addr* ea)
      40              : {
      41            0 :   const char* s = s0;
      42            0 :   int i;
      43              : 
      44            0 :   for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
      45            0 :     int8_t x;
      46              : 
      47            0 :     x = get_xdigit(*s++);
      48            0 :     if (x < 0)
      49              :       return false;
      50              : 
      51            0 :     ea->addr_bytes[i] = x << 4;
      52            0 :     x = get_xdigit(*s++);
      53            0 :     if (x < 0)
      54              :       return false;
      55            0 :     ea->addr_bytes[i] |= x;
      56              : 
      57            0 :     if (i < RTE_ETHER_ADDR_LEN - 1 && *s++ != ':')
      58              :       return false;
      59              :   }
      60              : 
      61              :   /* return true if at end of string */
      62            0 :   return *s == '\0';
      63              : }
      64              : 
      65              : rte_le16_t
      66            0 : packet_fill(struct ipv4_udp_packet_hdr* packet_hdr)
      67              : {
      68              :   // rte_le16_t total_length = 64;
      69              :   // char message[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
      70              : 
      71            0 :   rte_le16_t total_length = 2000;
      72            0 :   char message[] =
      73              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      74              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      75              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      76              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      77              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      78              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      79              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      80              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      81              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      82              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      83              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      84              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      85              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      86              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      87              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      88              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      89              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
      90              :     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
      91              : 
      92            0 :   char* payload = (char*)(packet_hdr + 1);
      93            0 :   memcpy(payload, message, total_length);
      94            0 :   return total_length;
      95              : }
      96              : 
      97              : /**************************************************************************/ /**
      98              :                                                                               *
      99              :                                                                               * pktgen_udp_hdr_ctor - UDP header
     100              :                                                                               * constructor routine.
     101              :                                                                               *
     102              :                                                                               * DESCRIPTION
     103              :                                                                               * Construct the UDP header in a packer
     104              :                                                                               * buffer.
     105              :                                                                               *
     106              :                                                                               * RETURNS: next header location
     107              :                                                                               *
     108              :                                                                               * SEE ALSO:
     109              :                                                                               */
     110              : void
     111            0 : pktgen_udp_hdr_ctor(struct ipv4_udp_packet_hdr* packet_hdr, rte_le16_t packet_len, int sport, int dport)
     112              : {
     113            0 :   packet_hdr->udp_hdr.dgram_len = rte_cpu_to_be_16(packet_len);
     114              : 
     115            0 :   packet_hdr->udp_hdr.src_port = rte_cpu_to_be_16(sport);
     116            0 :   packet_hdr->udp_hdr.dst_port = rte_cpu_to_be_16(dport);
     117              : 
     118            0 :   packet_hdr->udp_hdr.dgram_cksum = 0; // checksum must be set to 0 in the L4 header by the caller.
     119              :   // TODO  I think that the payload must be already there and continuous in memory..
     120            0 :   return;
     121              : }
     122              : 
     123              : /**************************************************************************/ /**
     124              :                                                                               *
     125              :                                                                               * pktgen_ipv4_ctor - Construct the IPv4
     126              :                                                                               * header for a packet
     127              :                                                                               *
     128              :                                                                               * DESCRIPTION
     129              :                                                                               * Constructor for the IPv4 header for a
     130              :                                                                               * given packet.
     131              :                                                                               *
     132              :                                                                               * RETURNS: N/A
     133              :                                                                               *
     134              :                                                                               * SEE ALSO:
     135              :                                                                               */
     136              : void
     137            0 : pktgen_ipv4_ctor(struct ipv4_udp_packet_hdr* packet_hdr,
     138              :                  rte_le16_t packet_len,
     139              :                  const std::string& src_ip_addr,
     140              :                  const std::string& dst_ip_addr)
     141              : {
     142              :   /* IPv4 Header constructor */
     143              : 
     144              :   /* Zero out the header space */
     145            0 :   memset((char*)&packet_hdr->ipv4_hdr, 0, sizeof(struct rte_ipv4_hdr));
     146              : 
     147            0 :   IpAddr src(src_ip_addr);
     148            0 :   IpAddr dst(dst_ip_addr);
     149              : 
     150            0 :   struct ipaddr src_reversed_order;
     151            0 :   src_reversed_order.addr_bytes[3] = src.addr_bytes[0];
     152            0 :   src_reversed_order.addr_bytes[2] = src.addr_bytes[1];
     153            0 :   src_reversed_order.addr_bytes[1] = src.addr_bytes[2];
     154            0 :   src_reversed_order.addr_bytes[0] = src.addr_bytes[3];
     155              : 
     156            0 :   struct ipaddr dst_reversed_order;
     157            0 :   dst_reversed_order.addr_bytes[3] = dst.addr_bytes[0];
     158            0 :   dst_reversed_order.addr_bytes[2] = dst.addr_bytes[1];
     159            0 :   dst_reversed_order.addr_bytes[1] = dst.addr_bytes[2];
     160            0 :   dst_reversed_order.addr_bytes[0] = dst.addr_bytes[3];
     161              : 
     162            0 :   rte_le32_t src_addr_ser = *((rte_le32_t*)&src_reversed_order);
     163            0 :   rte_le32_t dst_addr_ser = *((rte_le32_t*)&dst_reversed_order);
     164              : 
     165            0 :   packet_hdr->ipv4_hdr.src_addr = rte_cpu_to_be_32(src_addr_ser);
     166            0 :   packet_hdr->ipv4_hdr.dst_addr = rte_cpu_to_be_32(dst_addr_ser);
     167              : 
     168            0 :   packet_hdr->ipv4_hdr.version_ihl = (IPV4_VERSION << 4) | (sizeof(struct rte_ipv4_hdr) / 4);
     169              : 
     170            0 :   packet_hdr->ipv4_hdr.total_length = rte_cpu_to_be_16(packet_len); // Payload + udp_hdr + iphdr
     171            0 :   packet_hdr->ipv4_hdr.time_to_live = DEFAULT_TTL;
     172            0 :   packet_hdr->ipv4_hdr.type_of_service = DEFAULT_TOS;
     173              : 
     174              :   // https://perso.telecom-paristech.fr/drossi/paper/rossi17ipid.pdf
     175            0 :   packet_hdr->ipv4_hdr.packet_id = rte_cpu_to_be_16(0); // I put a constant here..
     176            0 :   packet_hdr->ipv4_hdr.fragment_offset = 0;
     177            0 :   packet_hdr->ipv4_hdr.next_proto_id = IPPROTO_UDP;
     178              : 
     179            0 :   packet_hdr->ipv4_hdr.hdr_checksum = 0;
     180              :   // packet_hdr->ipv4_hdr.hdr_checksum = rte_ipv4_cksum(&packet_hdr->ipv4_hdr);
     181              : 
     182              :   // UDP checksum
     183            0 :   packet_hdr->udp_hdr.dgram_cksum = 0;
     184              :   // packet_hdr->udp_hdr.dgram_cksum = rte_ipv4_udptcp_cksum(&packet_hdr->ipv4_hdr, (void *) &packet_hdr->udp_hdr);
     185              :   // if (packet_hdr->udp_hdr.dgram_cksum == 0) {
     186              :   //     packet_hdr->udp_hdr.dgram_cksum = 0xFFFF;
     187              :   // }
     188            0 :   return;
     189              : }
     190              : 
     191              : /**************************************************************************/ /**
     192              :                                                                               *
     193              :                                                                               * pktgen_ether_hdr_ctor - Ethernet header
     194              :                                                                               * constructor routine.
     195              :                                                                               *
     196              :                                                                               * DESCRIPTION
     197              :                                                                               * Construct the ethernet header for a
     198              :                                                                               * given packet buffer.
     199              :                                                                               *
     200              :                                                                               * RETURNS: Pointer to memory after the
     201              :                                                                               * ethernet header.
     202              :                                                                               *
     203              :                                                                               * SEE ALSO:
     204              :                                                                               */
     205              : void
     206            0 : pktgen_ether_hdr_ctor(struct ipv4_udp_packet_hdr* packet_hdr, const std::string& router_mac_address, const int port_id)
     207              : {
     208              :   // pg_ether_addr_copy(&pkt->eth_src_addr, &eth->src_addr);
     209              :   // pg_ether_addr_copy(&pkt->eth_dst_addr, &eth->dst_addr);
     210              : 
     211              :   /* src and dest addr */
     212              :   // See definition dpdk-20.08/lib/librte_net/rte_ether.c is defined as static -> not exposed to the lib... redefining
     213              :   // here
     214              : 
     215            0 :   get_ether_addr6(router_mac_address.c_str(), &packet_hdr->eth_hdr.dst_addr);
     216              : 
     217            0 :   rte_eth_macaddr_get(port_id, &packet_hdr->eth_hdr.src_addr);
     218              : 
     219              :   /* normal ethernet header */
     220            0 :   packet_hdr->eth_hdr.ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
     221            0 :   return;
     222              : }
     223              : 
     224              : void
     225            0 : construct_packets_for_burst(const int port_id,
     226              :                             const std::string& dst_mac_addr,
     227              :                             const int payload_bytes,
     228              :                             const int burst_size,
     229              :                             rte_mbuf** bufs)
     230              : {
     231            0 :   struct ipv4_udp_packet_hdr packet_hdr;
     232              : 
     233            0 :   constexpr int eth_header_bytes = 14;
     234            0 :   constexpr int udp_header_bytes = 8;
     235            0 :   constexpr int ipv4_header_bytes = 20;
     236              : 
     237            0 :   int eth_packet_bytes =
     238            0 :     eth_header_bytes + ipv4_header_bytes + udp_header_bytes + sizeof(detdataformats::DAQEthHeader) + payload_bytes;
     239            0 :   int ipv4_packet_bytes = eth_packet_bytes - eth_header_bytes;
     240            0 :   int udp_datagram_bytes = ipv4_packet_bytes - ipv4_header_bytes;
     241              : 
     242              :   // Get info for the ethernet header (protocol stack level 2)
     243            0 :   pktgen_ether_hdr_ctor(&packet_hdr, dst_mac_addr, port_id);
     244              : 
     245              :   // Get info for the internet header (protocol stack level 3)
     246            0 :   pktgen_ipv4_ctor(&packet_hdr, ipv4_packet_bytes);
     247              : 
     248              :   // Get info for the UDP header (protocol stack level 4)
     249            0 :   pktgen_udp_hdr_ctor(&packet_hdr, udp_datagram_bytes);
     250              : 
     251            0 :   detdataformats::DAQEthHeader daqethheader_obj;
     252            0 :   set_daqethheader_test_values(daqethheader_obj);
     253              : 
     254            0 :   void* dataloc = nullptr;
     255            0 :   for (int i_pkt = 0; i_pkt < burst_size; ++i_pkt) {
     256              : 
     257            0 :     dataloc = rte_pktmbuf_mtod(bufs[i_pkt], char*);
     258            0 :     rte_memcpy(dataloc, &packet_hdr, sizeof(packet_hdr));
     259              : 
     260            0 :     dataloc = rte_pktmbuf_mtod_offset(bufs[i_pkt], char*, sizeof(packet_hdr));
     261            0 :     rte_memcpy(dataloc, &daqethheader_obj, sizeof(daqethheader_obj));
     262              : 
     263            0 :     bufs[i_pkt]->pkt_len = eth_packet_bytes;
     264            0 :     bufs[i_pkt]->data_len = eth_packet_bytes;
     265              :   }
     266            0 : }
     267              : 
     268              : } // namespace udp
     269              : } // namespace dpdklibs
     270              : } // namespace dunedaq
        

Generated by: LCOV version 2.0-1