send_packets.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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. #include "defines.h"
  20. #include "config.h"
  21. #include "common.h"
  22. #include "tcpreplay_api.h"
  23. #include "timestamp_trace.h"
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <netinet/in.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/time.h>
  30. #include <sys/types.h>
  31. #include <unistd.h>
  32. #ifdef HAVE_NETMAP
  33. #ifdef HAVE_SYS_POLL_H
  34. #include <sys/poll.h>
  35. #endif
  36. #include <net/netmap.h>
  37. #include <net/netmap_user.h>
  38. #include <sys/ioctl.h>
  39. #endif /* HAVE_NETMAP */
  40. #ifdef TCPREPLAY
  41. #ifdef TCPREPLAY_EDIT
  42. #include "tcpedit/tcpedit.h"
  43. #include "tcpreplay_edit_opts.h"
  44. extern tcpedit_t *tcpedit;
  45. #else
  46. #include "tcpreplay_opts.h"
  47. #endif /* TCPREPLAY_EDIT */
  48. #endif /* TCPREPLAY */
  49. #include "send_packets.h"
  50. #include "sleep.h"
  51. static void calc_sleep_time(tcpreplay_t *ctx,
  52. struct timespec *pkt_time,
  53. struct timespec *last,
  54. COUNTER len,
  55. sendpacket_t *sp,
  56. COUNTER counter,
  57. struct timespec *sent_timestamp,
  58. COUNTER start_us,
  59. COUNTER *skip_length);
  60. static void tcpr_sleep(tcpreplay_t *ctx, sendpacket_t *sp _U_, struct timespec *nap_this_time, struct timespec *now);
  61. static u_char *get_next_packet(tcpreplay_opt_t *options,
  62. pcap_t *pcap,
  63. struct pcap_pkthdr *pkthdr,
  64. int file_idx,
  65. packet_cache_t **prev_packet);
  66. static uint32_t get_user_count(tcpreplay_t *ctx, sendpacket_t *sp, COUNTER counter);
  67. #ifdef HAVE_NETMAP
  68. static inline void
  69. wake_send_queues(sendpacket_t *sp _U_, tcpreplay_opt_t *options _U_)
  70. {
  71. if (options->netmap)
  72. ioctl(sp->handle.fd, NIOCTXSYNC, NULL); /* flush TX buffer */
  73. }
  74. #endif
  75. static inline int
  76. fast_edit_packet(struct pcap_pkthdr *pkthdr, u_char **pktdata, COUNTER iteration, bool cached, int datalink)
  77. {
  78. uint32_t pkt_len = pkthdr->caplen;
  79. u_char *packet = *pktdata;
  80. ipv4_hdr_t *ip_hdr = NULL;
  81. ipv6_hdr_t *ip6_hdr = NULL;
  82. uint32_t src_ip, dst_ip;
  83. uint32_t src_ip_orig, dst_ip_orig;
  84. uint32_t _U_ vlan_offset;
  85. uint16_t ether_type;
  86. uint32_t l2offset;
  87. uint32_t l2len;
  88. int res;
  89. res = get_l2len_protocol(packet, pkt_len, datalink, &ether_type, &l2len, &l2offset, &vlan_offset);
  90. if (res < 0)
  91. return res;
  92. switch (ether_type) {
  93. case ETHERTYPE_IP:
  94. if (pkt_len < (bpf_u_int32)(l2len + sizeof(ipv4_hdr_t))) {
  95. dbgx(1, "IP packet too short for Unique IP feature: %u", pkthdr->caplen);
  96. return -1;
  97. }
  98. ip_hdr = (ipv4_hdr_t *)(packet + l2len);
  99. src_ip_orig = src_ip = ntohl(ip_hdr->ip_src.s_addr);
  100. dst_ip_orig = dst_ip = ntohl(ip_hdr->ip_dst.s_addr);
  101. break;
  102. case ETHERTYPE_IP6:
  103. if (pkt_len < (bpf_u_int32)(l2len + sizeof(ipv6_hdr_t))) {
  104. dbgx(1, "IP6 packet too short for Unique IP feature: %u", pkthdr->caplen);
  105. return -1;
  106. }
  107. ip6_hdr = (ipv6_hdr_t *)(packet + l2len);
  108. src_ip_orig = src_ip = ntohl(ip6_hdr->ip_src.__u6_addr.__u6_addr32[3]);
  109. dst_ip_orig = dst_ip = ntohl(ip6_hdr->ip_dst.__u6_addr.__u6_addr32[3]);
  110. break;
  111. default:
  112. return -1; /* non-IP or packet too short */
  113. }
  114. dbgx(2, "Layer 3 protocol type is: 0x%04x", ether_type);
  115. /* swap src/dst IP's in a manner that does not affect CRC */
  116. if ((!cached && dst_ip > src_ip) || (cached && (dst_ip - iteration) > (src_ip - 1 - iteration))) {
  117. if (cached) {
  118. --src_ip;
  119. ++dst_ip;
  120. } else {
  121. src_ip -= iteration;
  122. dst_ip += iteration;
  123. }
  124. /* CRC compensations for wrap conditions */
  125. if (src_ip > src_ip_orig && dst_ip > dst_ip_orig) {
  126. dbgx(1,
  127. "dst_ip > src_ip(" COUNTER_SPEC "): before(1) src_ip=0x%08x dst_ip=0x%08x",
  128. iteration,
  129. src_ip,
  130. dst_ip);
  131. --src_ip;
  132. dbgx(1,
  133. "dst_ip > src_ip(" COUNTER_SPEC "): after(1) src_ip=0x%08x dst_ip=0x%08x",
  134. iteration,
  135. src_ip,
  136. dst_ip);
  137. } else if (dst_ip < dst_ip_orig && src_ip < src_ip_orig) {
  138. dbgx(1,
  139. "dst_ip > src_ip(" COUNTER_SPEC "): before(2) src_ip=0x%08x dst_ip=0x%08x",
  140. iteration,
  141. src_ip,
  142. dst_ip);
  143. ++dst_ip;
  144. dbgx(1,
  145. "dst_ip > src_ip(" COUNTER_SPEC "): after(2) src_ip=0x%08x dst_ip=0x%08x",
  146. iteration,
  147. src_ip,
  148. dst_ip);
  149. }
  150. } else {
  151. if (cached) {
  152. ++src_ip;
  153. --dst_ip;
  154. } else {
  155. src_ip += iteration;
  156. dst_ip -= iteration;
  157. }
  158. /* CRC compensations for wrap conditions */
  159. if (dst_ip > dst_ip_orig && src_ip > src_ip_orig) {
  160. dbgx(1,
  161. "src_ip > dst_ip(" COUNTER_SPEC "): before(1) dst_ip=0x%08x src_ip=0x%08x",
  162. iteration,
  163. dst_ip,
  164. src_ip);
  165. --dst_ip;
  166. dbgx(1,
  167. "src_ip > dst_ip(" COUNTER_SPEC "): after(1) dst_ip=0x%08x src_ip=0x%08x",
  168. iteration,
  169. dst_ip,
  170. src_ip);
  171. } else if (src_ip < src_ip_orig && dst_ip < dst_ip_orig) {
  172. dbgx(1,
  173. "src_ip > dst_ip(" COUNTER_SPEC "): before(2) dst_ip=0x%08x src_ip=0x%08x",
  174. iteration,
  175. dst_ip,
  176. src_ip);
  177. ++src_ip;
  178. dbgx(1,
  179. "src_ip > dst_ip(" COUNTER_SPEC "): after(2) dst_ip=0x%08x src_ip=0x%08x",
  180. iteration,
  181. dst_ip,
  182. src_ip);
  183. }
  184. }
  185. dbgx(1, "(" COUNTER_SPEC "): final src_ip=0x%08x dst_ip=0x%08x", iteration, src_ip, dst_ip);
  186. switch (ether_type) {
  187. case ETHERTYPE_IP:
  188. ip_hdr->ip_src.s_addr = htonl(src_ip);
  189. ip_hdr->ip_dst.s_addr = htonl(dst_ip);
  190. break;
  191. case ETHERTYPE_IP6:
  192. ip6_hdr->ip_src.__u6_addr.__u6_addr32[3] = htonl(src_ip);
  193. ip6_hdr->ip_dst.__u6_addr.__u6_addr32[3] = htonl(dst_ip);
  194. break;
  195. default:;
  196. }
  197. return 0;
  198. }
  199. /**
  200. * \brief Update flow stats
  201. *
  202. * Finds out if flow is unique and updates stats.
  203. */
  204. static inline void
  205. update_flow_stats(tcpreplay_t *ctx,
  206. sendpacket_t *sp,
  207. const struct pcap_pkthdr *pkthdr,
  208. const u_char *pktdata,
  209. int datalink,
  210. COUNTER packetnum)
  211. {
  212. flow_entry_type_t res =
  213. flow_decode(ctx->flow_hash_table, pkthdr, pktdata, datalink, ctx->options->flow_expiry, packetnum);
  214. switch (res) {
  215. case FLOW_ENTRY_NEW:
  216. ++ctx->stats.flows;
  217. ++ctx->stats.flows_unique;
  218. ++ctx->stats.flow_packets;
  219. if (sp) {
  220. ++sp->flows;
  221. ++sp->flows_unique;
  222. ++sp->flow_packets;
  223. }
  224. break;
  225. case FLOW_ENTRY_EXISTING:
  226. ++ctx->stats.flow_packets;
  227. if (sp)
  228. ++sp->flow_packets;
  229. break;
  230. case FLOW_ENTRY_EXPIRED:
  231. ++ctx->stats.flows_expired;
  232. ++ctx->stats.flows;
  233. ++ctx->stats.flow_packets;
  234. if (sp) {
  235. ++sp->flows_expired;
  236. ++sp->flows;
  237. ++sp->flow_packets;
  238. }
  239. break;
  240. case FLOW_ENTRY_NON_IP:
  241. ++ctx->stats.flow_non_flow_packets;
  242. if (sp)
  243. ++sp->flow_non_flow_packets;
  244. break;
  245. case FLOW_ENTRY_INVALID:
  246. ++ctx->stats.flows_invalid_packets;
  247. if (sp)
  248. ++sp->flows_invalid_packets;
  249. break;
  250. }
  251. }
  252. /**
  253. * \brief Preloads the memory cache for the given pcap file_idx
  254. *
  255. * Preloading can be used with or without --loop
  256. */
  257. void
  258. preload_pcap_file(tcpreplay_t *ctx, int idx)
  259. {
  260. tcpreplay_opt_t *options = ctx->options;
  261. char *path = options->sources[idx].filename;
  262. pcap_t *pcap = NULL;
  263. char ebuf[PCAP_ERRBUF_SIZE];
  264. const u_char *pktdata = NULL;
  265. struct pcap_pkthdr pkthdr;
  266. packet_cache_t *cached_packet = NULL;
  267. packet_cache_t **prev_packet = &cached_packet;
  268. int dlt;
  269. /* close stdin if reading from it (needed for some OS's) */
  270. if (strncmp(path, "-", 1) == 0)
  271. if (close(1) == -1)
  272. warnx("unable to close stdin: %s", strerror(errno));
  273. if ((pcap = tcpr_pcap_open(path, ebuf)) == NULL)
  274. errx(-1, "Error opening pcap file: %s", ebuf);
  275. dlt = pcap_datalink(pcap);
  276. COUNTER packetnum = 0;
  277. /* loop through the pcap. get_next_packet() builds the cache for us! */
  278. while ((pktdata = get_next_packet(options, pcap, &pkthdr, idx, prev_packet)) != NULL) {
  279. if (options->flow_stats) {
  280. ++packetnum;
  281. update_flow_stats(ctx, NULL, &pkthdr, pktdata, dlt, packetnum);
  282. }
  283. }
  284. /* mark this file as cached */
  285. options->file_cache[idx].cached = TRUE;
  286. options->file_cache[idx].dlt = dlt;
  287. pcap_close(pcap);
  288. }
  289. static void
  290. increment_iteration(tcpreplay_t *ctx)
  291. {
  292. tcpreplay_opt_t *options = ctx->options;
  293. ctx->last_unique_iteration = ctx->unique_iteration;
  294. ++ctx->iteration;
  295. if (options->unique_ip) {
  296. assert(options->unique_loops > 0.0);
  297. ctx->unique_iteration = ((ctx->iteration * 1000) / (COUNTER)(options->unique_loops * 1000.0)) + 1;
  298. }
  299. }
  300. /**
  301. * the main loop function for tcpreplay. This is where we figure out
  302. * what to do with each packet
  303. */
  304. void
  305. send_packets(tcpreplay_t *ctx, pcap_t *pcap, int idx)
  306. {
  307. struct timespec now, print_delta, last_pkt_ts;
  308. tcpreplay_opt_t *options = ctx->options;
  309. tcpreplay_stats_t *stats = &ctx->stats;
  310. COUNTER packetnum = 0;
  311. COUNTER limit_send = options->limit_send;
  312. struct pcap_pkthdr pkthdr;
  313. u_char *pktdata = NULL;
  314. sendpacket_t *sp = ctx->intf1;
  315. COUNTER pktlen;
  316. packet_cache_t *cached_packet = NULL;
  317. packet_cache_t **prev_packet = NULL;
  318. #if defined TCPREPLAY && defined TCPREPLAY_EDIT
  319. struct pcap_pkthdr *pkthdr_ptr;
  320. #endif
  321. int datalink = options->file_cache[idx].dlt;
  322. COUNTER skip_length = 0;
  323. COUNTER end_us;
  324. bool preload = options->file_cache[idx].cached;
  325. bool top_speed = (options->speed.mode == speed_topspeed ||
  326. (options->speed.mode == speed_mbpsrate && options->speed.speed == 0));
  327. bool now_is_now = true;
  328. bool read_next_packet = true; // used for LIBXDP batch packet processing with cached packets
  329. get_current_time(&now);
  330. if (!timesisset(&stats->start_time)) {
  331. TIMESPEC_SET(&stats->start_time, &now);
  332. if (ctx->options->stats >= 0) {
  333. char buf[64];
  334. if (format_date_time(&stats->start_time, buf, sizeof(buf)) > 0)
  335. printf("Test start: %s ...\n", buf);
  336. }
  337. }
  338. ctx->skip_packets = 0;
  339. timesclear(&last_pkt_ts);
  340. if (options->limit_time > 0)
  341. end_us = TIMESPEC_TO_MICROSEC(&stats->start_time) + SEC_TO_MICROSEC(options->limit_time);
  342. else
  343. end_us = 0;
  344. if (options->preload_pcap) {
  345. prev_packet = &cached_packet;
  346. } else {
  347. prev_packet = NULL;
  348. }
  349. /* MAIN LOOP
  350. * Keep sending while we have packets or until
  351. * we've sent enough packets
  352. */
  353. while (!ctx->abort && read_next_packet &&
  354. (pktdata = get_next_packet(options, pcap, &pkthdr, idx, prev_packet)) != NULL) {
  355. struct timespec pkthdr_ts;
  356. PCAP_TIMEVAL_TO_TIMESPEC_SET(&pkthdr.ts, &pkthdr_ts);
  357. now_is_now = false;
  358. packetnum++;
  359. #if defined TCPREPLAY || defined TCPREPLAY_EDIT
  360. /* look for include or exclude LIST match */
  361. if (options->list != NULL) {
  362. bool rule_set = check_list(options->list, packetnum);
  363. if ((rule_set && options->is_exclude) || (!rule_set && !options->is_exclude)) {
  364. dbgx(2, "packet " COUNTER_SPEC " not sent due to %s rule",
  365. packetnum,
  366. options->is_exclude ? "exclude" : "include");
  367. continue;
  368. }
  369. }
  370. /* do we use the snaplen (caplen) or the "actual" packet len? */
  371. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr.len : (COUNTER)pkthdr.caplen;
  372. #elif TCPBRIDGE
  373. pktlen = (COUNTER)pkthdr.caplen;
  374. #else
  375. #error WTF??? We should not be here!
  376. #endif
  377. dbgx(2, "packet " COUNTER_SPEC " caplen " COUNTER_SPEC, packetnum, pktlen);
  378. /* Dual nic processing */
  379. if (ctx->intf2 != NULL) {
  380. sp = (sendpacket_t *)cache_mode(ctx, options->cachedata, packetnum);
  381. /* sometimes we should not send the packet */
  382. if (sp == TCPR_DIR_NOSEND)
  383. continue;
  384. }
  385. #if defined TCPREPLAY && defined TCPREPLAY_EDIT
  386. pkthdr_ptr = &pkthdr;
  387. if (tcpedit_packet(tcpedit, &pkthdr_ptr, &pktdata, sp->cache_dir) == -1) {
  388. errx(-1, "Error editing packet #" COUNTER_SPEC ": %s", packetnum, tcpedit_geterr(tcpedit));
  389. }
  390. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
  391. #endif
  392. if (ctx->options->unique_ip && ctx->unique_iteration && ctx->unique_iteration > ctx->last_unique_iteration) {
  393. /* edit packet to ensure every pass has unique IP addresses */
  394. if (fast_edit_packet(&pkthdr, &pktdata, ctx->unique_iteration - 1, preload, datalink) == -1) {
  395. ++stats->failed;
  396. continue;
  397. }
  398. }
  399. /* update flow stats */
  400. if (options->flow_stats && !preload)
  401. update_flow_stats(ctx, options->cache_packets ? sp : NULL, &pkthdr, pktdata, datalink, packetnum);
  402. /*
  403. * this accelerator improves performance by avoiding expensive
  404. * time stamps during periods where we have fallen behind in our
  405. * sending
  406. */
  407. if (skip_length && pktlen < skip_length) {
  408. skip_length -= pktlen;
  409. } else if (ctx->skip_packets) {
  410. --ctx->skip_packets;
  411. } else {
  412. /*
  413. * time stamping is expensive, but now is the
  414. * time to do it.
  415. */
  416. dbgx(4, "This packet time: " TIMESPEC_FORMAT, pkthdr_ts.tv_sec, pkthdr_ts.tv_nsec);
  417. skip_length = 0;
  418. ctx->skip_packets = 0;
  419. if (options->speed.mode == speed_multiplier) {
  420. if (!timesisset(&last_pkt_ts)) {
  421. TIMESPEC_SET(&last_pkt_ts, &pkthdr_ts);
  422. } else if (timescmp(&pkthdr_ts, &last_pkt_ts, >)) {
  423. struct timespec delta;
  424. timessub(&pkthdr_ts, &last_pkt_ts, &delta);
  425. timeradd_timespec(&stats->pkt_ts_delta, &delta, &stats->pkt_ts_delta);
  426. TIMESPEC_SET(&last_pkt_ts, &pkthdr_ts);
  427. }
  428. }
  429. if (!top_speed) {
  430. now_is_now = true;
  431. get_current_time(&now);
  432. }
  433. /*
  434. * Only if the current packet is not late.
  435. *
  436. * This also sets skip_length and skip_packets which will avoid
  437. * timestamping for a given number of packets.
  438. */
  439. calc_sleep_time(ctx,
  440. &stats->pkt_ts_delta,
  441. &stats->time_delta,
  442. pktlen,
  443. sp,
  444. packetnum,
  445. &stats->end_time,
  446. TIMESPEC_TO_NANOSEC(&stats->start_time),
  447. &skip_length);
  448. /*
  449. * Track the time of the "last packet sent".
  450. *
  451. * A number of 3rd party tools generate bad timestamps which go backwards
  452. * in time. Hence, don't update the "last" unless pkthdr.ts > last
  453. */
  454. if (timescmp(&stats->time_delta, &stats->pkt_ts_delta, <))
  455. TIMESPEC_SET(&stats->time_delta, &stats->pkt_ts_delta);
  456. /*
  457. * we know how long to sleep between sends, now do it.
  458. */
  459. if (!top_speed) {
  460. #ifndef HAVE_LIBXDP
  461. tcpr_sleep(ctx, sp, &ctx->nap, &now);
  462. #else
  463. if (sp->handle_type != SP_TYPE_LIBXDP) {
  464. tcpr_sleep(ctx, sp, &ctx->nap, &now);
  465. } else if (sp->batch_size == 1) {
  466. // In case of LIBXDP packet processing waiting only makes sense when batch size is one
  467. tcpr_sleep(ctx, sp, &ctx->nap, &now);
  468. }
  469. #endif
  470. }
  471. }
  472. #ifdef ENABLE_VERBOSE
  473. /* do we need to print the packet via tcpdump? */
  474. if (options->verbose)
  475. tcpdump_print(options->tcpdump, &pkthdr, pktdata);
  476. #endif
  477. #ifdef HAVE_LIBXDP
  478. if (sp->handle_type == SP_TYPE_LIBXDP) {
  479. /* Reserve frames for the batc h*/
  480. while (xsk_ring_prod__reserve(&(sp->xsk_info->tx), sp->batch_size, &sp->tx_idx) < sp->batch_size) {
  481. complete_tx_only(sp);
  482. }
  483. /* The first packet is already in memory */
  484. prepare_first_element_of_batch(ctx, &packetnum, pktdata, pkthdr.len);
  485. /* Read more packets and prepare batch */
  486. prepare_remaining_elements_of_batch(ctx, &packetnum, &read_next_packet, pcap, &idx, pkthdr, prev_packet);
  487. }
  488. #endif
  489. dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
  490. /* write packet out on network */
  491. if (sendpacket(sp, pktdata, pktlen, &pkthdr) < (int)pktlen) {
  492. warnx("Unable to send packet: %s", sendpacket_geterr(sp));
  493. continue;
  494. }
  495. /*
  496. * Mark the time when we sent the last packet
  497. */
  498. TIMESPEC_SET(&stats->end_time, &now);
  499. #ifdef TIMESTAMP_TRACE
  500. add_timestamp_trace_entry(pktlen, &stats->end_time, skip_length);
  501. #endif
  502. stats->pkts_sent++;
  503. #ifndef HAVE_LIBXDP
  504. stats->bytes_sent += pktlen;
  505. #else
  506. if (sp->handle_type != SP_TYPE_LIBXDP)
  507. stats->bytes_sent += pktlen;
  508. #endif
  509. /* print stats during the run? */
  510. if (options->stats > 0) {
  511. if (!timesisset(&stats->last_print)) {
  512. TIMESPEC_SET(&stats->last_print, &now);
  513. } else {
  514. timessub(&now, &stats->last_print, &print_delta);
  515. if (print_delta.tv_sec >= options->stats) {
  516. TIMESPEC_SET(&stats->end_time, &now);
  517. packet_stats(stats);
  518. TIMESPEC_SET(&stats->last_print, &now);
  519. }
  520. }
  521. }
  522. #if defined HAVE_NETMAP
  523. if (sp->first_packet || timesisset(&ctx->nap)) {
  524. wake_send_queues(sp, options);
  525. sp->first_packet = false;
  526. }
  527. #endif
  528. /* stop sending based on the duration limit... */
  529. if ((end_us > 0 && (COUNTER)TIMESPEC_TO_MICROSEC(&now) > end_us) ||
  530. /* ... or stop sending based on the limit -L? */
  531. (limit_send > 0 && stats->pkts_sent >= limit_send)) {
  532. ctx->abort = true;
  533. }
  534. } /* while */
  535. #ifdef HAVE_NETMAP
  536. /* when completing test, wait until the last packet is sent */
  537. if (options->netmap && (ctx->abort || options->loop == 1)) {
  538. while (ctx->intf1 && !netmap_tx_queues_empty(ctx->intf1)) {
  539. now_is_now = true;
  540. get_current_time(&now);
  541. }
  542. while (ctx->intf2 && !netmap_tx_queues_empty(ctx->intf2)) {
  543. now_is_now = true;
  544. get_current_time(&now);
  545. }
  546. }
  547. #endif /* HAVE_NETMAP */
  548. if (!now_is_now)
  549. get_current_time(&now);
  550. TIMESPEC_SET(&stats->end_time, &now);
  551. increment_iteration(ctx);
  552. }
  553. /**
  554. * the alternate main loop function for tcpreplay. This is where we figure out
  555. * what to do with each packet when processing two files a the same time
  556. */
  557. void
  558. send_dual_packets(tcpreplay_t *ctx, pcap_t *pcap1, int cache_file_idx1, pcap_t *pcap2, int cache_file_idx2)
  559. {
  560. struct timespec now, print_delta, last_pkt_ts;
  561. tcpreplay_opt_t *options = ctx->options;
  562. tcpreplay_stats_t *stats = &ctx->stats;
  563. COUNTER packetnum = 0;
  564. COUNTER limit_send = options->limit_send;
  565. int cache_file_idx;
  566. struct pcap_pkthdr pkthdr1, pkthdr2;
  567. u_char *pktdata1 = NULL, *pktdata2 = NULL, *pktdata = NULL;
  568. sendpacket_t *sp;
  569. COUNTER pktlen;
  570. packet_cache_t *cached_packet1 = NULL, *cached_packet2 = NULL;
  571. packet_cache_t **prev_packet1 = NULL, **prev_packet2 = NULL;
  572. struct pcap_pkthdr *pkthdr_ptr;
  573. int datalink;
  574. COUNTER end_us;
  575. COUNTER skip_length = 0;
  576. bool top_speed = (options->speed.mode == speed_topspeed ||
  577. (options->speed.mode == speed_mbpsrate && options->speed.speed == 0));
  578. bool now_is_now = true;
  579. get_current_time(&now);
  580. if (!timesisset(&stats->start_time)) {
  581. TIMESPEC_SET(&stats->start_time, &now);
  582. if (ctx->options->stats >= 0) {
  583. char buf[64];
  584. if (format_date_time(&stats->start_time, buf, sizeof(buf)) > 0)
  585. printf("Dual test start: %s ...\n", buf);
  586. }
  587. }
  588. ctx->skip_packets = 0;
  589. timesclear(&last_pkt_ts);
  590. if (options->limit_time > 0)
  591. end_us = TIMESPEC_TO_MICROSEC(&stats->start_time) + SEC_TO_MICROSEC(options->limit_time);
  592. else
  593. end_us = 0;
  594. if (options->preload_pcap) {
  595. prev_packet1 = &cached_packet1;
  596. prev_packet2 = &cached_packet2;
  597. } else {
  598. prev_packet1 = NULL;
  599. prev_packet2 = NULL;
  600. }
  601. pktdata1 = get_next_packet(options, pcap1, &pkthdr1, cache_file_idx1, prev_packet1);
  602. pktdata2 = get_next_packet(options, pcap2, &pkthdr2, cache_file_idx2, prev_packet2);
  603. /* MAIN LOOP
  604. * Keep sending while we have packets or until
  605. * we've sent enough packets
  606. */
  607. while (!ctx->abort && !(pktdata1 == NULL && pktdata2 == NULL)) {
  608. now_is_now = false;
  609. packetnum++;
  610. /* look for include or exclude LIST match */
  611. if (options->list != NULL) {
  612. bool rule_set = check_list(options->list, packetnum);
  613. if ((rule_set && options->is_exclude) || (!rule_set && !options->is_exclude)) {
  614. dbgx(2, "packet " COUNTER_SPEC " not sent due to %s rule",
  615. packetnum,
  616. options->is_exclude ? "exclude" : "include");
  617. continue;
  618. }
  619. }
  620. /* figure out which pcap file we need to process next
  621. * when get_next_packet() returns null for pktdata, the pkthdr
  622. * will still have the old values from the previous call. This
  623. * means we can't always trust the timestamps to tell us which
  624. * file to process.
  625. */
  626. if (pktdata1 == NULL || (pktdata2 != NULL && timercmp(&pkthdr1.ts, &pkthdr2.ts, >))) {
  627. /* file 2 is next */
  628. sp = ctx->intf2;
  629. datalink = options->file_cache[cache_file_idx2].dlt;
  630. pkthdr_ptr = &pkthdr2;
  631. cache_file_idx = cache_file_idx2;
  632. pktdata = pktdata2;
  633. } else {
  634. /* file 1 is next */
  635. sp = ctx->intf1;
  636. datalink = options->file_cache[cache_file_idx1].dlt;
  637. pkthdr_ptr = &pkthdr1;
  638. cache_file_idx = cache_file_idx1;
  639. pktdata = pktdata1;
  640. }
  641. #if defined TCPREPLAY || defined TCPREPLAY_EDIT
  642. /* do we use the snaplen (caplen) or the "actual" packet len? */
  643. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
  644. #elif TCPBRIDGE
  645. pktlen = (COUNTER)pkthdr_ptr->caplen;
  646. #else
  647. #error WTF??? We should not be here!
  648. #endif
  649. dbgx(2, "packet " COUNTER_SPEC " caplen " COUNTER_SPEC, packetnum, pktlen);
  650. #if defined TCPREPLAY && defined TCPREPLAY_EDIT
  651. if (tcpedit_packet(tcpedit, &pkthdr_ptr, &pktdata, sp->cache_dir) == -1) {
  652. errx(-1, "Error editing packet #" COUNTER_SPEC ": %s", packetnum, tcpedit_geterr(tcpedit));
  653. }
  654. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
  655. #endif
  656. if (ctx->options->unique_ip && ctx->unique_iteration && ctx->unique_iteration > ctx->last_unique_iteration) {
  657. /* edit packet to ensure every pass is unique */
  658. if (fast_edit_packet(pkthdr_ptr,
  659. &pktdata,
  660. ctx->unique_iteration - 1,
  661. options->file_cache[cache_file_idx].cached,
  662. datalink) == -1) {
  663. ++stats->failed;
  664. continue;
  665. }
  666. }
  667. /* update flow stats */
  668. if (options->flow_stats && !options->file_cache[cache_file_idx].cached)
  669. update_flow_stats(ctx, sp, pkthdr_ptr, pktdata, datalink, packetnum);
  670. /*
  671. * this accelerator improves performance by avoiding expensive
  672. * time stamps during periods where we have fallen behind in our
  673. * sending
  674. */
  675. if (skip_length && pktlen < skip_length) {
  676. skip_length -= pktlen;
  677. } else if (ctx->skip_packets) {
  678. --ctx->skip_packets;
  679. } else {
  680. /*
  681. * time stamping is expensive, but now is the
  682. * time to do it.
  683. */
  684. dbgx(4, "This packet time: " TIMEVAL_FORMAT, pkthdr_ptr->ts.tv_sec, pkthdr_ptr->ts.tv_usec);
  685. skip_length = 0;
  686. ctx->skip_packets = 0;
  687. if (options->speed.mode == speed_multiplier) {
  688. struct timespec pkthdr_ts;
  689. PCAP_TIMEVAL_TO_TIMESPEC_SET(&pkthdr_ptr->ts, &pkthdr_ts);
  690. if (!timesisset(&last_pkt_ts)) {
  691. PCAP_TIMEVAL_TO_TIMESPEC_SET(&pkthdr_ptr->ts, &last_pkt_ts);
  692. } else if (timescmp(&pkthdr_ts, &last_pkt_ts, >)) {
  693. struct timespec delta;
  694. timessub(&pkthdr_ts, &last_pkt_ts, &delta);
  695. timeradd_timespec(&stats->pkt_ts_delta, &delta, &stats->pkt_ts_delta);
  696. TIMESPEC_SET(&last_pkt_ts, &pkthdr_ts);
  697. }
  698. if (!timesisset(&stats->time_delta))
  699. TIMESPEC_SET(&stats->pkt_ts_delta, &stats->pkt_ts_delta);
  700. }
  701. if (!top_speed) {
  702. get_current_time(&now);
  703. now_is_now = true;
  704. }
  705. /*
  706. * Only if the current packet is not late.
  707. *
  708. * This also sets skip_length and skip_packets which will avoid
  709. * timestamping for a given number of packets.
  710. */
  711. calc_sleep_time(ctx,
  712. &stats->pkt_ts_delta,
  713. &stats->time_delta,
  714. pktlen,
  715. sp,
  716. packetnum,
  717. &stats->end_time,
  718. TIMESPEC_TO_NANOSEC(&stats->start_time),
  719. &skip_length);
  720. /*
  721. * Track the time of the "last packet sent".
  722. *
  723. * A number of 3rd party tools generate bad timestamps which go backwards
  724. * in time. Hence, don't update the "last" unless pkthdr_ptr->ts > last
  725. */
  726. if (timescmp(&stats->time_delta, &stats->pkt_ts_delta, <))
  727. TIMESPEC_SET(&stats->time_delta, &stats->pkt_ts_delta);
  728. /*
  729. * we know how long to sleep between sends, now do it.
  730. */
  731. if (!top_speed)
  732. tcpr_sleep(ctx, sp, &ctx->nap, &now);
  733. }
  734. #ifdef ENABLE_VERBOSE
  735. /* do we need to print the packet via tcpdump? */
  736. if (options->verbose)
  737. tcpdump_print(options->tcpdump, pkthdr_ptr, pktdata);
  738. #endif
  739. dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
  740. /* write packet out on network */
  741. if (sendpacket(sp, pktdata, pktlen, pkthdr_ptr) < (int)pktlen) {
  742. warnx("Unable to send packet: %s", sendpacket_geterr(sp));
  743. continue;
  744. }
  745. /*
  746. * Mark the time when we sent the last packet
  747. */
  748. TIMESPEC_SET(&stats->end_time, &now);
  749. ++stats->pkts_sent;
  750. stats->bytes_sent += pktlen;
  751. /* print stats during the run? */
  752. if (options->stats > 0) {
  753. if (!timesisset(&stats->last_print)) {
  754. TIMESPEC_SET(&stats->last_print, &now);
  755. } else {
  756. timessub(&now, &stats->last_print, &print_delta);
  757. if (print_delta.tv_sec >= options->stats) {
  758. TIMESPEC_SET(&stats->end_time, &now);
  759. packet_stats(stats);
  760. TIMESPEC_SET(&stats->last_print, &now);
  761. }
  762. }
  763. }
  764. #if defined HAVE_NETMAP
  765. if (sp->first_packet || timesisset(&ctx->nap)) {
  766. wake_send_queues(sp, options);
  767. sp->first_packet = false;
  768. }
  769. #endif
  770. /* get the next packet for this file handle depending on which we last used */
  771. if (sp == ctx->intf2) {
  772. pktdata2 = get_next_packet(options, pcap2, &pkthdr2, cache_file_idx2, prev_packet2);
  773. } else {
  774. pktdata1 = get_next_packet(options, pcap1, &pkthdr1, cache_file_idx1, prev_packet1);
  775. }
  776. /* stop sending based on the duration limit... */
  777. if ((end_us > 0 && (COUNTER)TIMESPEC_TO_MICROSEC(&now) > end_us) ||
  778. /* ... or stop sending based on the limit -L? */
  779. (limit_send > 0 && stats->pkts_sent >= limit_send)) {
  780. ctx->abort = true;
  781. }
  782. } /* while */
  783. #ifdef HAVE_NETMAP
  784. /* when completing test, wait until the last packet is sent */
  785. if (options->netmap && (ctx->abort || options->loop == 1)) {
  786. while (ctx->intf1 && !netmap_tx_queues_empty(ctx->intf1)) {
  787. get_current_time(&now);
  788. now_is_now = true;
  789. }
  790. while (ctx->intf2 && !netmap_tx_queues_empty(ctx->intf2)) {
  791. get_current_time(&now);
  792. now_is_now = true;
  793. }
  794. }
  795. #endif /* HAVE_NETMAP */
  796. if (!now_is_now)
  797. get_current_time(&now);
  798. TIMESPEC_SET(&stats->end_time, &now);
  799. increment_iteration(ctx);
  800. }
  801. /**
  802. * Gets the next packet to be sent out. This will either read from the pcap file
  803. * or will retrieve the packet from the internal cache.
  804. *
  805. * The parameter prev_packet is used as the parent of the new entry in the cache list.
  806. * This should be NULL on the first call to this function for each file and
  807. * will be updated as new entries are added (or retrieved) from the cache list.
  808. */
  809. u_char *
  810. get_next_packet(tcpreplay_opt_t *options, pcap_t *pcap, struct pcap_pkthdr *pkthdr, int idx, packet_cache_t **prev_packet)
  811. {
  812. u_char *pktdata = NULL;
  813. uint32_t pktlen;
  814. /* pcap may be null in cache mode! */
  815. /* packet_cache_t may be null in file read mode! */
  816. assert(pkthdr);
  817. /*
  818. * Check if we're caching files
  819. */
  820. if (options->preload_pcap && (prev_packet != NULL)) {
  821. /*
  822. * Yes we are caching files - has this one been cached?
  823. */
  824. if (options->file_cache[idx].cached) {
  825. if (*prev_packet == NULL) {
  826. /*
  827. * Get the first packet in the cache list directly from the file
  828. */
  829. *prev_packet = options->file_cache[idx].packet_cache;
  830. } else {
  831. /*
  832. * Get the next packet in the cache list
  833. */
  834. *prev_packet = (*prev_packet)->next;
  835. }
  836. if (*prev_packet != NULL) {
  837. pktdata = (*prev_packet)->pktdata;
  838. memcpy(pkthdr, &((*prev_packet)->pkthdr), sizeof(struct pcap_pkthdr));
  839. }
  840. } else {
  841. /*
  842. * We should read the pcap file, and cache the results
  843. */
  844. pktdata = safe_pcap_next(pcap, pkthdr);
  845. if (pktdata != NULL) {
  846. if (*prev_packet == NULL) {
  847. /*
  848. * Create the first packet in the list
  849. */
  850. *prev_packet = safe_malloc(sizeof(packet_cache_t));
  851. options->file_cache[idx].packet_cache = *prev_packet;
  852. } else {
  853. /*
  854. * Add a packet to the end of the list
  855. */
  856. (*prev_packet)->next = safe_malloc(sizeof(packet_cache_t));
  857. *prev_packet = (*prev_packet)->next;
  858. }
  859. if (*prev_packet != NULL) {
  860. (*prev_packet)->next = NULL;
  861. pktlen = pkthdr->caplen;
  862. (*prev_packet)->pktdata = safe_malloc(pktlen + PACKET_HEADROOM);
  863. memcpy((*prev_packet)->pktdata, pktdata, pktlen);
  864. memcpy(&((*prev_packet)->pkthdr), pkthdr, sizeof(struct pcap_pkthdr));
  865. }
  866. }
  867. }
  868. } else {
  869. /*
  870. * Read pcap file as normal
  871. */
  872. pktdata = safe_pcap_next(pcap, pkthdr);
  873. }
  874. /* this gets casted to a const on the way out */
  875. return pktdata;
  876. }
  877. /**
  878. * determines based upon the cachedata which interface the given packet
  879. * should go out. Also rewrites any layer 2 data we might need to adjust.
  880. * Returns a void cased pointer to the ctx->intfX of the corresponding
  881. * interface or NULL on error
  882. */
  883. void *
  884. cache_mode(tcpreplay_t *ctx, char *cachedata, COUNTER packet_num)
  885. {
  886. tcpreplay_opt_t *options = ctx->options;
  887. void *sp = NULL;
  888. int result;
  889. if (packet_num > options->cache_packets) {
  890. tcpreplay_seterr(ctx, "%s", "Exceeded number of packets in cache file.");
  891. return NULL;
  892. }
  893. result = check_cache(cachedata, packet_num);
  894. if (result == TCPR_DIR_NOSEND) {
  895. dbgx(2, "Cache: Not sending packet " COUNTER_SPEC ".", packet_num);
  896. return NULL;
  897. } else if (result == TCPR_DIR_C2S) {
  898. dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out primary interface.", packet_num);
  899. sp = ctx->intf1;
  900. } else if (result == TCPR_DIR_S2C) {
  901. dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out secondary interface.", packet_num);
  902. sp = ctx->intf2;
  903. } else {
  904. tcpreplay_seterr(ctx, "Invalid cache value: %i", result);
  905. return NULL;
  906. }
  907. return sp;
  908. }
  909. /**
  910. * Given the timestamp on the current packet and the last packet sent,
  911. * calculate the appropriate amount of time to sleep. Sleep time
  912. * will be in ctx->nap.
  913. */
  914. static void
  915. calc_sleep_time(tcpreplay_t *ctx,
  916. struct timespec *pkt_ts_delta,
  917. struct timespec *time_delta,
  918. COUNTER len,
  919. sendpacket_t *sp,
  920. COUNTER counter,
  921. struct timespec *sent_timestamp,
  922. COUNTER start_ns,
  923. COUNTER *skip_length)
  924. {
  925. tcpreplay_opt_t *options = ctx->options;
  926. struct timespec nap_for;
  927. COUNTER now_ns;
  928. timesclear(&ctx->nap);
  929. /*
  930. * pps_multi accelerator. This uses the existing send accelerator
  931. * and hence requires the funky math to get the expected timings.
  932. */
  933. if (options->speed.mode == speed_packetrate && options->speed.pps_multi) {
  934. ctx->skip_packets = options->speed.pps_multi - 1;
  935. }
  936. /* no last packet sent, just leave */
  937. if (ctx->first_time) {
  938. ctx->first_time = false;
  939. return;
  940. }
  941. switch (options->speed.mode) {
  942. case speed_multiplier:
  943. /*
  944. * Replay packets a factor of the time they were originally sent.
  945. * Make sure the packet is not late.
  946. */
  947. if (timescmp(pkt_ts_delta, time_delta, >)) {
  948. /* pkt_time_delta has increased, so handle normally */
  949. timessub(pkt_ts_delta, time_delta, &nap_for);
  950. TIMESPEC_SET(&ctx->nap, &nap_for);
  951. dbgx(3, "original packet delta time: " TIMESPEC_FORMAT, ctx->nap.tv_sec, ctx->nap.tv_nsec);
  952. timesdiv_float(&ctx->nap, options->speed.multiplier);
  953. dbgx(3, "original packet delta/div: " TIMESPEC_FORMAT, ctx->nap.tv_sec, ctx->nap.tv_nsec);
  954. }
  955. break;
  956. case speed_mbpsrate:
  957. /*
  958. * Ignore the time supplied by the capture file and send data at
  959. * a constant 'rate' (bytes per second).
  960. */
  961. now_ns = TIMESPEC_TO_NANOSEC(sent_timestamp);
  962. if (now_ns) {
  963. COUNTER next_tx_ns;
  964. COUNTER bps = options->speed.speed;
  965. COUNTER bits_sent = ((ctx->stats.bytes_sent + len) * 8);
  966. COUNTER tx_ns = now_ns - start_ns;
  967. /*
  968. * bits * 1000000000 divided by bps = nanosecond
  969. *
  970. * ensure there is no overflow in cases where bits_sent is very high
  971. */
  972. if (bits_sent > COUNTER_OVERFLOW_RISK)
  973. next_tx_ns = (bits_sent * 1000) / bps * 1000000;
  974. else
  975. next_tx_ns = (bits_sent * 1000000000) / bps;
  976. if (next_tx_ns > tx_ns) {
  977. NANOSEC_TO_TIMESPEC(next_tx_ns - tx_ns, &ctx->nap);
  978. } else if (tx_ns > next_tx_ns) {
  979. tx_ns = now_ns - start_ns;
  980. *skip_length = ((tx_ns - next_tx_ns) * bps) / 8000000000;
  981. }
  982. update_current_timestamp_trace_entry(ctx->stats.bytes_sent + (COUNTER)len, now_ns, tx_ns, next_tx_ns);
  983. }
  984. dbgx(3, "packet size=" COUNTER_SPEC "\t\tnap=" TIMESPEC_FORMAT, len, ctx->nap.tv_sec, ctx->nap.tv_nsec);
  985. break;
  986. case speed_packetrate:
  987. /*
  988. * Ignore the time supplied by the capture file and send data at
  989. * a constant rate (packets per second).
  990. */
  991. now_ns = TIMESPEC_TO_NANOSEC(sent_timestamp);
  992. if (now_ns) {
  993. COUNTER next_tx_ns;
  994. COUNTER pph = ctx->options->speed.speed;
  995. COUNTER pkts_sent = ctx->stats.pkts_sent;
  996. COUNTER tx_ns = now_ns - start_ns;
  997. /*
  998. * packets * 1000000 divided by pps = microseconds
  999. * packets per sec (pps) = packets per hour / (60 * 60)
  1000. *
  1001. * Adjust for long running tests with high PPS to prevent overflow.
  1002. * When active, adjusted calculation may add a bit of jitter.
  1003. */
  1004. if ((pkts_sent < COUNTER_OVERFLOW_RISK))
  1005. next_tx_ns = (pkts_sent * 1000000000) * (60 * 60) / pph;
  1006. else
  1007. next_tx_ns = ((pkts_sent * 1000000) / pph * 1000) * (60 * 60);
  1008. if (next_tx_ns > tx_ns)
  1009. NANOSEC_TO_TIMESPEC(next_tx_ns - tx_ns, &ctx->nap);
  1010. else
  1011. ctx->skip_packets = options->speed.pps_multi;
  1012. update_current_timestamp_trace_entry(ctx->stats.bytes_sent + (COUNTER)len, now_ns, tx_ns, next_tx_ns);
  1013. }
  1014. dbgx(3,
  1015. "packet count=" COUNTER_SPEC "\t\tnap=" TIMESPEC_FORMAT,
  1016. ctx->stats.pkts_sent,
  1017. ctx->nap.tv_sec,
  1018. ctx->nap.tv_nsec);
  1019. break;
  1020. case speed_oneatatime:
  1021. /* do we skip prompting for a key press? */
  1022. if (ctx->skip_packets == 0) {
  1023. ctx->skip_packets = get_user_count(ctx, sp, counter);
  1024. }
  1025. /* decrement our send counter */
  1026. printf("Sending packet " COUNTER_SPEC " out: %s\n",
  1027. counter,
  1028. sp == ctx->intf1 ? options->intf1_name : options->intf2_name);
  1029. ctx->skip_packets--;
  1030. /* leave */
  1031. break;
  1032. case speed_topspeed:
  1033. break;
  1034. default:
  1035. errx(-1, "Unknown/supported speed mode: %d", options->speed.mode);
  1036. }
  1037. }
  1038. static void
  1039. tcpr_sleep(tcpreplay_t *ctx, sendpacket_t *sp, struct timespec *nap_this_time, struct timespec *now)
  1040. {
  1041. tcpreplay_opt_t *options = ctx->options;
  1042. bool flush =
  1043. #ifdef HAVE_NETMAP
  1044. true;
  1045. #else
  1046. false;
  1047. #endif
  1048. /* don't sleep if nap = {0, 0} */
  1049. if (!timesisset(nap_this_time))
  1050. return;
  1051. /* do we need to limit the total time we sleep? */
  1052. if (timesisset(&(options->maxsleep)) && (timescmp(nap_this_time, &(options->maxsleep), >))) {
  1053. dbgx(2,
  1054. "Was going to sleep for " TIMESPEC_FORMAT " but maxsleeping for " TIMESPEC_FORMAT,
  1055. nap_this_time->tv_sec,
  1056. nap_this_time->tv_nsec,
  1057. options->maxsleep.tv_sec,
  1058. options->maxsleep.tv_nsec);
  1059. TIMESPEC_SET(nap_this_time, &options->maxsleep);
  1060. }
  1061. #ifdef HAVE_NETMAP
  1062. if (flush)
  1063. wake_send_queues(sp, options);
  1064. #endif
  1065. dbgx(2, "Sleeping: " TIMESPEC_FORMAT, nap_this_time->tv_sec, nap_this_time->tv_nsec);
  1066. /*
  1067. * Depending on the accurate method & packet rate computation method
  1068. * We have multiple methods of sleeping, pick the right one...
  1069. */
  1070. switch (options->accurate) {
  1071. #ifdef HAVE_SELECT
  1072. case accurate_select:
  1073. select_sleep(sp, nap_this_time, now, flush);
  1074. break;
  1075. #endif
  1076. #if defined HAVE_IOPORT_SLEEP
  1077. case accurate_ioport:
  1078. ioport_sleep(sp, nap_this_time, now, flush);
  1079. break;
  1080. #endif
  1081. case accurate_gtod:
  1082. gettimeofday_sleep(sp, nap_this_time, now, flush);
  1083. break;
  1084. case accurate_nanosleep:
  1085. nanosleep_sleep(sp, nap_this_time, now, flush);
  1086. break;
  1087. default:
  1088. errx(-1, "Unknown timer mode %d", options->accurate);
  1089. }
  1090. }
  1091. /**
  1092. * Ask the user how many packets they want to send.
  1093. */
  1094. static uint32_t
  1095. get_user_count(tcpreplay_t *ctx, sendpacket_t *sp, COUNTER counter)
  1096. {
  1097. tcpreplay_opt_t *options = ctx->options;
  1098. struct pollfd poller[1]; /* use poll to read from the keyboard */
  1099. char input[EBUF_SIZE];
  1100. unsigned long send = 0;
  1101. printf("**** Next packet #" COUNTER_SPEC " out %s. How many packets do you wish to send? ",
  1102. counter,
  1103. (sp == ctx->intf1 ? options->intf1_name : options->intf2_name));
  1104. fflush(NULL);
  1105. poller[0].fd = STDIN_FILENO;
  1106. poller[0].events = POLLIN | POLLPRI | POLLNVAL;
  1107. poller[0].revents = 0;
  1108. if (fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK))
  1109. errx(-1, "Unable to clear non-blocking flag on stdin: %s", strerror(errno));
  1110. /* wait for the input */
  1111. if (poll(poller, 1, -1) < 0)
  1112. errx(-1, "Error reading user input from stdin: %s", strerror(errno));
  1113. /*
  1114. * read to the end of the line or EBUF_SIZE,
  1115. * Note, if people are stupid, and type in more text then EBUF_SIZE
  1116. * then the next fgets() will pull in that data, which will have poor
  1117. * results. fuck them.
  1118. */
  1119. if (fgets(input, sizeof(input), stdin) == NULL) {
  1120. errx(-1, "Unable to process user input for fd %d: %s", fileno(stdin), strerror(errno));
  1121. } else if (strlen(input) > 1) {
  1122. send = strtoul(input, NULL, 0);
  1123. }
  1124. /* how many packets should we send? */
  1125. if (send == 0) {
  1126. dbg(1, "Input was less then 1 or non-numeric, assuming 1");
  1127. /* assume send only one packet */
  1128. send = 1;
  1129. }
  1130. return (uint32_t)send;
  1131. }
  1132. #ifdef HAVE_LIBXDP
  1133. void
  1134. check_packet_fits_in_umem_frame(sendpacket_t *sp, int packet_len)
  1135. {
  1136. if (packet_len > sp->frame_size) {
  1137. fprintf(stderr,
  1138. "ERROR: packet size cannot be larger than the size of an UMEM frame! Packet size: %i Frame size: %i\n",
  1139. packet_len,
  1140. sp->frame_size);
  1141. free_umem_and_xsk(sp);
  1142. exit(-1);
  1143. }
  1144. }
  1145. void
  1146. fill_umem_with_data_and_set_xdp_desc(sendpacket_t *sp, int tx_idx, COUNTER umem_index, u_char *pktdata, int len)
  1147. {
  1148. check_packet_fits_in_umem_frame(sp, len);
  1149. COUNTER umem_index_mod = (umem_index % sp->batch_size) * sp->frame_size; // packets are sent in batch, after each
  1150. // batch umem memory is reusable
  1151. gen_eth_frame(sp->umem_info, umem_index_mod, pktdata, len);
  1152. struct xdp_desc *xdp_desc = xsk_ring_prod__tx_desc(&(sp->xsk_info->tx), tx_idx);
  1153. xdp_desc->addr = (COUNTER)(umem_index_mod);
  1154. xdp_desc->len = len;
  1155. }
  1156. void
  1157. prepare_first_element_of_batch(tcpreplay_t *ctx, COUNTER *packetnum, u_char *pktdata, u_int32_t len)
  1158. {
  1159. sendpacket_t *sp = ctx->intf1;
  1160. tcpreplay_stats_t *stats = &ctx->stats;
  1161. fill_umem_with_data_and_set_xdp_desc(sp, sp->tx_idx, *packetnum - 1, pktdata, len);
  1162. sp->bytes_sent += len;
  1163. stats->bytes_sent += len;
  1164. }
  1165. void
  1166. prepare_remaining_elements_of_batch(tcpreplay_t *ctx,
  1167. COUNTER *packetnum,
  1168. bool *read_next_packet,
  1169. pcap_t *pcap,
  1170. int *idx,
  1171. struct pcap_pkthdr pkthdr,
  1172. packet_cache_t **prev_packet)
  1173. {
  1174. sendpacket_t *sp = ctx->intf1;
  1175. tcpreplay_stats_t *stats = &ctx->stats;
  1176. int datalink = ctx->options->file_cache[*idx].dlt;
  1177. bool preload = ctx->options->file_cache[*idx].cached;
  1178. u_char *pktdata = NULL;
  1179. unsigned int pckt_count = 1;
  1180. while (!ctx->abort && (pckt_count < sp->batch_size) &&
  1181. (pktdata = get_next_packet(ctx->options, pcap, &pkthdr, *idx, prev_packet)) != NULL) {
  1182. fill_umem_with_data_and_set_xdp_desc(sp, sp->tx_idx + pckt_count, *packetnum, pktdata, pkthdr.len);
  1183. ++pckt_count;
  1184. ++*packetnum;
  1185. stats->bytes_sent += pkthdr.len;
  1186. sp->bytes_sent += pkthdr.len;
  1187. stats->pkts_sent++;
  1188. if (ctx->options->flow_stats && !preload) {
  1189. update_flow_stats(ctx, ctx->options->cache_packets ? sp : NULL, &pkthdr, pktdata, datalink, *packetnum);
  1190. }
  1191. }
  1192. if (pckt_count < sp->batch_size) {
  1193. // No more packets to read, it is essential for cached packet processing
  1194. *read_next_packet = false;
  1195. }
  1196. sp->pckt_count = pckt_count;
  1197. dbgx(2,
  1198. "Sending packets with LIBXDP in batch, packet numbers from " COUNTER_SPEC " to " COUNTER_SPEC "\n",
  1199. *packetnum - pckt_count + 1,
  1200. *packetnum);
  1201. }
  1202. #endif /* HAVE_LIBXDP */