txring_overwrite.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Verify that consecutive sends over packet tx_ring are mirrored
  4. * with their original content intact.
  5. */
  6. #define _GNU_SOURCE
  7. #include <arpa/inet.h>
  8. #include <assert.h>
  9. #include <error.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <linux/filter.h>
  13. #include <linux/if_packet.h>
  14. #include <net/ethernet.h>
  15. #include <net/if.h>
  16. #include <netinet/in.h>
  17. #include <netinet/ip.h>
  18. #include <netinet/udp.h>
  19. #include <poll.h>
  20. #include <pthread.h>
  21. #include <sched.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/mman.h>
  24. #include <sys/socket.h>
  25. #include <sys/time.h>
  26. #include <sys/types.h>
  27. #include <sys/utsname.h>
  28. #include <stdbool.h>
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. const int eth_off = TPACKET_HDRLEN - sizeof(struct sockaddr_ll);
  35. const int cfg_frame_size = 1000;
  36. static void build_packet(void *buffer, size_t blen, char payload_char)
  37. {
  38. struct udphdr *udph;
  39. struct ethhdr *eth;
  40. struct iphdr *iph;
  41. size_t off = 0;
  42. memset(buffer, 0, blen);
  43. eth = buffer;
  44. eth->h_proto = htons(ETH_P_IP);
  45. off += sizeof(*eth);
  46. iph = buffer + off;
  47. iph->ttl = 8;
  48. iph->ihl = 5;
  49. iph->version = 4;
  50. iph->saddr = htonl(INADDR_LOOPBACK);
  51. iph->daddr = htonl(INADDR_LOOPBACK + 1);
  52. iph->protocol = IPPROTO_UDP;
  53. iph->tot_len = htons(blen - off);
  54. iph->check = 0;
  55. off += sizeof(*iph);
  56. udph = buffer + off;
  57. udph->dest = htons(8000);
  58. udph->source = htons(8001);
  59. udph->len = htons(blen - off);
  60. udph->check = 0;
  61. off += sizeof(*udph);
  62. memset(buffer + off, payload_char, blen - off);
  63. }
  64. static int setup_rx(void)
  65. {
  66. int fdr;
  67. fdr = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  68. if (fdr == -1)
  69. error(1, errno, "socket r");
  70. return fdr;
  71. }
  72. static int setup_tx(char **ring)
  73. {
  74. struct sockaddr_ll laddr = {};
  75. struct tpacket_req req = {};
  76. int fdt;
  77. fdt = socket(PF_PACKET, SOCK_RAW, 0);
  78. if (fdt == -1)
  79. error(1, errno, "socket t");
  80. laddr.sll_family = AF_PACKET;
  81. laddr.sll_protocol = htons(0);
  82. laddr.sll_ifindex = if_nametoindex("lo");
  83. if (!laddr.sll_ifindex)
  84. error(1, errno, "if_nametoindex");
  85. if (bind(fdt, (void *)&laddr, sizeof(laddr)))
  86. error(1, errno, "bind fdt");
  87. req.tp_block_size = getpagesize();
  88. req.tp_block_nr = 1;
  89. req.tp_frame_size = getpagesize();
  90. req.tp_frame_nr = 1;
  91. if (setsockopt(fdt, SOL_PACKET, PACKET_TX_RING,
  92. (void *)&req, sizeof(req)))
  93. error(1, errno, "setsockopt ring");
  94. *ring = mmap(0, req.tp_block_size * req.tp_block_nr,
  95. PROT_READ | PROT_WRITE, MAP_SHARED, fdt, 0);
  96. if (*ring == MAP_FAILED)
  97. error(1, errno, "mmap");
  98. return fdt;
  99. }
  100. static void send_pkt(int fdt, void *slot, char payload_char)
  101. {
  102. struct tpacket_hdr *header = slot;
  103. int ret;
  104. while (header->tp_status != TP_STATUS_AVAILABLE)
  105. usleep(1000);
  106. build_packet(slot + eth_off, cfg_frame_size, payload_char);
  107. header->tp_len = cfg_frame_size;
  108. header->tp_status = TP_STATUS_SEND_REQUEST;
  109. ret = sendto(fdt, NULL, 0, 0, NULL, 0);
  110. if (ret == -1)
  111. error(1, errno, "kick tx");
  112. }
  113. static int read_verify_pkt(int fdr, char payload_char)
  114. {
  115. char buf[100];
  116. int ret;
  117. ret = read(fdr, buf, sizeof(buf));
  118. if (ret != sizeof(buf))
  119. error(1, errno, "read");
  120. if (buf[60] != payload_char) {
  121. printf("wrong pattern: 0x%x != 0x%x\n", buf[60], payload_char);
  122. return 1;
  123. }
  124. printf("read: %c (0x%x)\n", buf[60], buf[60]);
  125. return 0;
  126. }
  127. int main(int argc, char **argv)
  128. {
  129. const char payload_patterns[] = "ab";
  130. char *ring;
  131. int fdr, fdt, ret = 0;
  132. fdr = setup_rx();
  133. fdt = setup_tx(&ring);
  134. send_pkt(fdt, ring, payload_patterns[0]);
  135. send_pkt(fdt, ring, payload_patterns[1]);
  136. ret |= read_verify_pkt(fdr, payload_patterns[0]);
  137. ret |= read_verify_pkt(fdr, payload_patterns[1]);
  138. if (close(fdt))
  139. error(1, errno, "close t");
  140. if (close(fdr))
  141. error(1, errno, "close r");
  142. return ret;
  143. }