sendpacket.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #pragma once
  20. #include "defines.h"
  21. #include "config.h"
  22. #include <sys/socket.h>
  23. #ifdef __NetBSD__
  24. #include <net/if_ether.h>
  25. #elif ! defined(__HAIKU__)
  26. #include <netinet/if_ether.h>
  27. #endif
  28. #if defined HAVE_NETMAP
  29. #include "common/netmap.h"
  30. #include <net/netmap.h>
  31. #endif
  32. #ifdef HAVE_PF_PACKET
  33. #include <netpacket/packet.h>
  34. #endif
  35. #ifdef HAVE_TX_RING
  36. #include "txring.h"
  37. #endif
  38. #ifdef HAVE_LIBDNET
  39. /* need to undef these which are pulled in via defines.h, prior to importing dnet.h */
  40. #undef icmp_id
  41. #undef icmp_seq
  42. #undef icmp_data
  43. #undef icmp_mask
  44. #ifdef HAVE_DNET_H
  45. #include <dnet.h>
  46. #endif
  47. #ifdef HAVE_DUMBNET_H
  48. #include <dumbnet.h>
  49. #endif
  50. #endif
  51. typedef enum sendpacket_type_e {
  52. SP_TYPE_NONE,
  53. SP_TYPE_LIBNET,
  54. SP_TYPE_LIBDNET,
  55. SP_TYPE_LIBPCAP,
  56. SP_TYPE_BPF,
  57. SP_TYPE_PF_PACKET,
  58. SP_TYPE_TX_RING,
  59. SP_TYPE_KHIAL,
  60. SP_TYPE_NETMAP,
  61. SP_TYPE_TUNTAP,
  62. SP_TYPE_LIBPCAP_DUMP,
  63. SP_TYPE_LIBXDP
  64. } sendpacket_type_t;
  65. /* these are the file_operations ioctls */
  66. #define KHIAL_SET_DIRECTION (0x1)
  67. #define KHIAL_GET_DIRECTION (0x2)
  68. /* these are the directions */
  69. typedef enum khial_direction_e {
  70. KHIAL_DIRECTION_RX = 0,
  71. KHIAL_DIRECTION_TX,
  72. } khial_direction_t;
  73. typedef struct pcap_dump_s{
  74. pcap_t *pcap;
  75. pcap_dumper_t* dump;
  76. } pcap_dump_t;
  77. union sendpacket_handle {
  78. pcap_t *pcap;
  79. pcap_dump_t dump;
  80. int fd;
  81. #ifdef HAVE_LIBDNET
  82. eth_t *ldnet;
  83. #endif
  84. };
  85. #define SENDPACKET_ERRBUF_SIZE 1024
  86. #define MAX_IFNAMELEN 64
  87. #ifdef HAVE_LIBXDP
  88. #include <errno.h>
  89. #include <stdlib.h>
  90. #include <linux/if_xdp.h>
  91. #include <xdp/xsk.h>
  92. struct xsk_ring_stats {
  93. unsigned long rx_npkts;
  94. unsigned long tx_npkts;
  95. unsigned long rx_dropped_npkts;
  96. unsigned long rx_invalid_npkts;
  97. unsigned long tx_invalid_npkts;
  98. unsigned long rx_full_npkts;
  99. unsigned long rx_fill_empty_npkts;
  100. unsigned long tx_empty_npkts;
  101. unsigned long prev_rx_npkts;
  102. unsigned long prev_tx_npkts;
  103. unsigned long prev_rx_dropped_npkts;
  104. unsigned long prev_rx_invalid_npkts;
  105. unsigned long prev_tx_invalid_npkts;
  106. unsigned long prev_rx_full_npkts;
  107. unsigned long prev_rx_fill_empty_npkts;
  108. unsigned long prev_tx_empty_npkts;
  109. };
  110. struct xsk_driver_stats {
  111. unsigned long intrs;
  112. unsigned long prev_intrs;
  113. };
  114. struct xsk_app_stats {
  115. unsigned long rx_empty_polls;
  116. unsigned long fill_fail_polls;
  117. unsigned long copy_tx_sendtos;
  118. unsigned long tx_wakeup_sendtos;
  119. unsigned long opt_polls;
  120. unsigned long prev_rx_empty_polls;
  121. unsigned long prev_fill_fail_polls;
  122. unsigned long prev_copy_tx_sendtos;
  123. unsigned long prev_tx_wakeup_sendtos;
  124. unsigned long prev_opt_polls;
  125. };
  126. struct xsk_umem_info {
  127. struct xsk_ring_prod fq;
  128. struct xsk_ring_cons cq;
  129. struct xsk_umem *umem;
  130. void *buffer;
  131. };
  132. struct xsk_socket {
  133. struct xsk_ring_cons *rx;
  134. struct xsk_ring_prod *tx;
  135. struct xsk_ctx *ctx;
  136. struct xsk_socket_config config;
  137. int fd;
  138. };
  139. struct xsk_socket_info {
  140. struct xsk_ring_cons rx;
  141. struct xsk_ring_prod tx;
  142. struct xsk_umem_info *umem;
  143. struct xsk_socket *xsk;
  144. struct xsk_ring_stats ring_stats;
  145. struct xsk_app_stats app_stats;
  146. struct xsk_driver_stats drv_stats;
  147. u_int32_t outstanding_tx;
  148. };
  149. #endif /* HAVE_LIBXDP */
  150. struct sendpacket_s {
  151. tcpr_dir_t cache_dir;
  152. int open;
  153. char device[MAX_IFNAMELEN];
  154. char errbuf[SENDPACKET_ERRBUF_SIZE];
  155. COUNTER retry_enobufs;
  156. COUNTER retry_eagain;
  157. COUNTER failed;
  158. COUNTER trunc_packets;
  159. COUNTER sent;
  160. COUNTER bytes_sent;
  161. COUNTER attempt;
  162. COUNTER flow_non_flow_packets;
  163. COUNTER flows;
  164. COUNTER flow_packets;
  165. COUNTER flows_unique;
  166. COUNTER flows_expired;
  167. COUNTER flows_invalid_packets;
  168. sendpacket_type_t handle_type;
  169. union sendpacket_handle handle;
  170. struct tcpr_ether_addr ether;
  171. #if defined HAVE_NETMAP
  172. int first_packet;
  173. int netmap_delay;
  174. #endif
  175. #ifdef HAVE_NETMAP
  176. struct netmap_if *nm_if;
  177. nmreq_t nmr;
  178. void *mmap_addr;
  179. int mmap_size;
  180. uint32_t if_flags;
  181. uint32_t is_vale;
  182. int netmap_version;
  183. uint16_t first_tx_ring, last_tx_ring, cur_tx_ring;
  184. #ifdef linux
  185. uint32_t data;
  186. uint32_t gso;
  187. uint32_t tso;
  188. uint32_t rxcsum;
  189. uint32_t txcsum;
  190. #endif /* linux */
  191. #endif /* HAVE_NETMAP */
  192. #ifdef HAVE_PF_PACKET
  193. struct sockaddr_ll sa;
  194. #ifdef HAVE_TX_RING
  195. txring_t *tx_ring;
  196. #endif
  197. #endif
  198. #ifdef HAVE_LIBXDP
  199. struct xsk_socket_info *xsk_info;
  200. struct xsk_umem_info *umem_info;
  201. unsigned int batch_size;
  202. unsigned int pckt_count;
  203. int frame_size;
  204. unsigned int tx_idx;
  205. int tx_size;
  206. #endif
  207. bool abort;
  208. };
  209. typedef struct sendpacket_s sendpacket_t;
  210. #ifdef HAVE_LIBXDP
  211. struct xsk_umem_info *
  212. create_umem_area(int nb_of_frames, int frame_size, int nb_of_completion_queue_descs, int nb_of_fill_queue_descs);
  213. struct xsk_socket_info *create_xsk_socket(struct xsk_umem_info *umem,
  214. int nb_of_tx_queue_desc,
  215. int nb_of_rx_queue_desc,
  216. const char *device,
  217. u_int32_t queue_id,
  218. char *errbuf);
  219. static inline void
  220. gen_eth_frame(struct xsk_umem_info *umem, u_int64_t addr, u_char *pkt_data, COUNTER pkt_size)
  221. {
  222. memcpy(xsk_umem__get_data(umem->buffer, addr), pkt_data, pkt_size);
  223. }
  224. static inline void
  225. kick_tx(struct xsk_socket_info *xsk)
  226. {
  227. int ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
  228. if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN || errno == EBUSY || errno == ENETDOWN) {
  229. return;
  230. }
  231. printf("%s\n", "Packet sending exited with error!");
  232. exit (1);
  233. }
  234. static inline void
  235. complete_tx_only(sendpacket_t *sp)
  236. {
  237. u_int32_t completion_idx = 0;
  238. if (sp->xsk_info->outstanding_tx == 0) {
  239. return;
  240. }
  241. if (xsk_ring_prod__needs_wakeup(&(sp->xsk_info->tx))) {
  242. sp->xsk_info->app_stats.tx_wakeup_sendtos++;
  243. kick_tx(sp->xsk_info);
  244. }
  245. unsigned int rcvd = xsk_ring_cons__peek(&sp->xsk_info->umem->cq, sp->pckt_count, &completion_idx);
  246. if (rcvd > 0) {
  247. xsk_ring_cons__release(&sp->xsk_info->umem->cq, rcvd);
  248. sp->xsk_info->outstanding_tx -= rcvd;
  249. }
  250. }
  251. #endif /* HAVE_LIBXDP */
  252. int sendpacket(sendpacket_t *, const u_char *, size_t, struct pcap_pkthdr *);
  253. void sendpacket_close(sendpacket_t *);
  254. char *sendpacket_geterr(sendpacket_t *);
  255. size_t sendpacket_getstat(sendpacket_t *, char *, size_t);
  256. sendpacket_t *sendpacket_open(const char *, char *, tcpr_dir_t, sendpacket_type_t, void *arg);
  257. struct tcpr_ether_addr *sendpacket_get_hwaddr(sendpacket_t *);
  258. int sendpacket_get_dlt(sendpacket_t *);
  259. const char *sendpacket_get_method(sendpacket_t *);
  260. void sendpacket_abort(sendpacket_t *);