send_packets.c 40 KB

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