send_packets.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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. if (!timerisset(&stats->time_delta))
  392. TIMEVAL_SET(&stats->pkt_ts_delta, &stats->pkt_ts_delta);
  393. }
  394. if (!top_speed) {
  395. now_is_now = true;
  396. gettimeofday(&now, NULL);
  397. }
  398. /*
  399. * Only if the current packet is not late.
  400. *
  401. * This also sets skip_length and skip_packets which will avoid
  402. * timestamping for a given number of packets.
  403. */
  404. calc_sleep_time(ctx, &stats->pkt_ts_delta, &stats->time_delta,
  405. pktlen, sp, packetnum, &stats->end_time,
  406. TIMEVAL_TO_MICROSEC(&stats->start_time), &skip_length);
  407. /*
  408. * Track the time of the "last packet sent".
  409. *
  410. * A number of 3rd party tools generate bad timestamps which go backwards
  411. * in time. Hence, don't update the "last" unless pkthdr.ts > last
  412. */
  413. if (timercmp(&stats->time_delta, &stats->pkt_ts_delta, <))
  414. TIMEVAL_SET(&stats->time_delta, &stats->pkt_ts_delta);
  415. /*
  416. * we know how long to sleep between sends, now do it.
  417. */
  418. if (!top_speed)
  419. tcpr_sleep(ctx, sp, &ctx->nap, &now);
  420. }
  421. #ifdef ENABLE_VERBOSE
  422. /* do we need to print the packet via tcpdump? */
  423. if (options->verbose)
  424. tcpdump_print(options->tcpdump, &pkthdr, pktdata);
  425. #endif
  426. dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
  427. /* write packet out on network */
  428. if (sendpacket(sp, pktdata, pktlen, &pkthdr) < (int)pktlen) {
  429. warnx("Unable to send packet: %s", sendpacket_geterr(sp));
  430. break;
  431. }
  432. /*
  433. * Mark the time when we sent the last packet
  434. */
  435. TIMEVAL_SET(&stats->end_time, &now);
  436. #ifdef TIMESTAMP_TRACE
  437. add_timestamp_trace_entry(pktlen, &stats->end_time, skip_length);
  438. #endif
  439. stats->pkts_sent++;
  440. stats->bytes_sent += pktlen;
  441. /* print stats during the run? */
  442. if (options->stats > 0) {
  443. if (! timerisset(&stats->last_print)) {
  444. TIMEVAL_SET(&stats->last_print, &now);
  445. } else {
  446. timersub(&now, &stats->last_print, &print_delta);
  447. if (print_delta.tv_sec >= options->stats) {
  448. TIMEVAL_SET(&stats->end_time, &now);
  449. packet_stats(stats);
  450. TIMEVAL_SET(&stats->last_print, &now);
  451. }
  452. }
  453. }
  454. #if defined HAVE_NETMAP
  455. if (sp->first_packet || timesisset(&ctx->nap)) {
  456. wake_send_queues(sp, options);
  457. sp->first_packet = false;
  458. }
  459. #endif
  460. /* stop sending based on the duration limit... */
  461. if ((end_us > 0 && (COUNTER)TIMEVAL_TO_MICROSEC(&now) > end_us) ||
  462. /* ... or stop sending based on the limit -L? */
  463. (limit_send > 0 && stats->pkts_sent >= limit_send)) {
  464. ctx->abort = true;
  465. }
  466. } /* while */
  467. #ifdef HAVE_NETMAP
  468. /* when completing test, wait until the last packet is sent */
  469. if (options->netmap && (ctx->abort || options->loop == 1)) {
  470. while (ctx->intf1 && !netmap_tx_queues_empty(ctx->intf1)) {
  471. now_is_now = true;
  472. gettimeofday(&now, NULL);
  473. }
  474. while (ctx->intf2 && !netmap_tx_queues_empty(ctx->intf2)) {
  475. now_is_now = true;
  476. gettimeofday(&now, NULL);
  477. }
  478. }
  479. #endif /* HAVE_NETMAP */
  480. if (!now_is_now)
  481. gettimeofday(&now, NULL);
  482. TIMEVAL_SET(&stats->end_time, &now);
  483. increment_iteration(ctx);
  484. }
  485. /**
  486. * the alternate main loop function for tcpreplay. This is where we figure out
  487. * what to do with each packet when processing two files a the same time
  488. */
  489. void
  490. send_dual_packets(tcpreplay_t *ctx, pcap_t *pcap1, int cache_file_idx1, pcap_t *pcap2, int cache_file_idx2)
  491. {
  492. struct timeval print_delta, now, last_pkt_ts;
  493. tcpreplay_opt_t *options = ctx->options;
  494. tcpreplay_stats_t *stats = &ctx->stats;
  495. COUNTER packetnum = 0;
  496. COUNTER limit_send = options->limit_send;
  497. int cache_file_idx;
  498. struct pcap_pkthdr pkthdr1, pkthdr2;
  499. u_char *pktdata1 = NULL, *pktdata2 = NULL, *pktdata = NULL;
  500. sendpacket_t *sp;
  501. COUNTER pktlen;
  502. packet_cache_t *cached_packet1 = NULL, *cached_packet2 = NULL;
  503. packet_cache_t **prev_packet1 = NULL, **prev_packet2 = NULL;
  504. struct pcap_pkthdr *pkthdr_ptr;
  505. int datalink;
  506. COUNTER end_us;
  507. COUNTER skip_length = 0;
  508. bool top_speed = (options->speed.mode == speed_topspeed ||
  509. (options->speed.mode == speed_mbpsrate && options->speed.speed == 0));
  510. bool now_is_now = true;
  511. gettimeofday(&now, NULL);
  512. if (!timerisset(&stats->start_time)) {
  513. TIMEVAL_SET(&stats->start_time, &now);
  514. if (ctx->options->stats >= 0) {
  515. char buf[64];
  516. if (format_date_time(&stats->start_time, buf, sizeof(buf)) > 0)
  517. printf("Dual test start: %s ...\n", buf);
  518. }
  519. }
  520. ctx->skip_packets = 0;
  521. timerclear(&last_pkt_ts);
  522. if (options->limit_time > 0)
  523. end_us = TIMEVAL_TO_MICROSEC(&stats->start_time) +
  524. SEC_TO_MICROSEC(options->limit_time);
  525. else
  526. end_us = 0;
  527. if (options->preload_pcap) {
  528. prev_packet1 = &cached_packet1;
  529. prev_packet2 = &cached_packet2;
  530. } else {
  531. prev_packet1 = NULL;
  532. prev_packet2 = NULL;
  533. }
  534. pktdata1 = get_next_packet(ctx, pcap1, &pkthdr1, cache_file_idx1, prev_packet1);
  535. pktdata2 = get_next_packet(ctx, pcap2, &pkthdr2, cache_file_idx2, prev_packet2);
  536. /* MAIN LOOP
  537. * Keep sending while we have packets or until
  538. * we've sent enough packets
  539. */
  540. while (!ctx->abort &&
  541. !(pktdata1 == NULL && pktdata2 == NULL)) {
  542. now_is_now = false;
  543. packetnum++;
  544. /* figure out which pcap file we need to process next
  545. * when get_next_packet() returns null for pktdata, the pkthdr
  546. * will still have the old values from the previous call. This
  547. * means we can't always trust the timestamps to tell us which
  548. * file to process.
  549. */
  550. if (pktdata1 == NULL) {
  551. /* file 2 is next */
  552. sp = ctx->intf2;
  553. datalink = options->file_cache[cache_file_idx2].dlt;
  554. pkthdr_ptr = &pkthdr2;
  555. cache_file_idx = cache_file_idx2;
  556. pktdata = pktdata2;
  557. } else if (pktdata2 == NULL) {
  558. /* file 1 is next */
  559. sp = ctx->intf1;
  560. datalink = options->file_cache[cache_file_idx1].dlt;
  561. pkthdr_ptr = &pkthdr1;
  562. cache_file_idx = cache_file_idx1;
  563. pktdata = pktdata1;
  564. } else if (timercmp(&pkthdr1.ts, &pkthdr2.ts, <=)) {
  565. /* file 1 is next */
  566. sp = ctx->intf1;
  567. datalink = options->file_cache[cache_file_idx1].dlt;
  568. pkthdr_ptr = &pkthdr1;
  569. cache_file_idx = cache_file_idx1;
  570. pktdata = pktdata1;
  571. } else {
  572. /* file 2 is next */
  573. sp = ctx->intf2;
  574. datalink = options->file_cache[cache_file_idx2].dlt;
  575. pkthdr_ptr = &pkthdr2;
  576. cache_file_idx = cache_file_idx2;
  577. pktdata = pktdata2;
  578. }
  579. #if defined TCPREPLAY || defined TCPREPLAY_EDIT
  580. /* do we use the snaplen (caplen) or the "actual" packet len? */
  581. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
  582. #elif TCPBRIDGE
  583. pktlen = (COUNTER)pkthdr_ptr->caplen;
  584. #else
  585. #error WTF??? We should not be here!
  586. #endif
  587. dbgx(2, "packet " COUNTER_SPEC " caplen " COUNTER_SPEC, packetnum, pktlen);
  588. #if defined TCPREPLAY && defined TCPREPLAY_EDIT
  589. if (tcpedit_packet(tcpedit, &pkthdr_ptr, &pktdata, sp->cache_dir) == -1) {
  590. errx(-1, "Error editing packet #" COUNTER_SPEC ": %s", packetnum, tcpedit_geterr(tcpedit));
  591. }
  592. pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr_ptr->len : (COUNTER)pkthdr_ptr->caplen;
  593. #endif
  594. if (ctx->options->unique_ip && ctx->unique_iteration &&
  595. ctx->unique_iteration > ctx->last_unique_iteration) {
  596. /* edit packet to ensure every pass is unique */
  597. if (fast_edit_packet(pkthdr_ptr, &pktdata, ctx->unique_iteration - 1,
  598. options->file_cache[cache_file_idx].cached, datalink) == -1) {
  599. ++stats->failed;
  600. continue;
  601. }
  602. }
  603. /* update flow stats */
  604. if (options->flow_stats && !options->file_cache[cache_file_idx].cached)
  605. update_flow_stats(ctx, sp, pkthdr_ptr, pktdata, datalink);
  606. /*
  607. * this accelerator improves performance by avoiding expensive
  608. * time stamps during periods where we have fallen behind in our
  609. * sending
  610. */
  611. if (skip_length && pktlen < skip_length) {
  612. skip_length -= pktlen;
  613. } else if (ctx->skip_packets) {
  614. --ctx->skip_packets;
  615. } else {
  616. /*
  617. * time stamping is expensive, but now is the
  618. * time to do it.
  619. */
  620. dbgx(4, "This packet time: " TIMEVAL_FORMAT, pkthdr_ptr->ts.tv_sec,
  621. pkthdr_ptr->ts.tv_usec);
  622. skip_length = 0;
  623. ctx->skip_packets = 0;
  624. if (options->speed.mode == speed_multiplier) {
  625. if (!timerisset(&last_pkt_ts)) {
  626. TIMEVAL_SET(&last_pkt_ts, &pkthdr_ptr->ts);
  627. } else if (timercmp(&pkthdr_ptr->ts, &last_pkt_ts, >)) {
  628. struct timeval delta;
  629. timersub(&pkthdr_ptr->ts, &last_pkt_ts, &delta);
  630. timeradd(&stats->pkt_ts_delta, &delta, &stats->pkt_ts_delta);
  631. TIMEVAL_SET(&last_pkt_ts, &pkthdr_ptr->ts);
  632. }
  633. if (!timerisset(&stats->time_delta))
  634. TIMEVAL_SET(&stats->pkt_ts_delta, &stats->pkt_ts_delta);
  635. }
  636. if (!top_speed) {
  637. gettimeofday(&now, NULL);
  638. now_is_now = true;
  639. }
  640. /*
  641. * Only if the current packet is not late.
  642. *
  643. * This also sets skip_length and skip_packets which will avoid
  644. * timestamping for a given number of packets.
  645. */
  646. calc_sleep_time(ctx, &stats->pkt_ts_delta, &stats->time_delta,
  647. pktlen, sp, packetnum, &stats->end_time,
  648. TIMEVAL_TO_MICROSEC(&stats->start_time), &skip_length);
  649. /*
  650. * Track the time of the "last packet sent".
  651. *
  652. * A number of 3rd party tools generate bad timestamps which go backwards
  653. * in time. Hence, don't update the "last" unless pkthdr_ptr->ts > last
  654. */
  655. if (timercmp(&stats->time_delta, &stats->pkt_ts_delta, <))
  656. TIMEVAL_SET(&stats->time_delta, &stats->pkt_ts_delta);
  657. /*
  658. * we know how long to sleep between sends, now do it.
  659. */
  660. if (!top_speed)
  661. tcpr_sleep(ctx, sp, &ctx->nap, &now);
  662. }
  663. #ifdef ENABLE_VERBOSE
  664. /* do we need to print the packet via tcpdump? */
  665. if (options->verbose)
  666. tcpdump_print(options->tcpdump, pkthdr_ptr, pktdata);
  667. #endif
  668. dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
  669. /* write packet out on network */
  670. if (sendpacket(sp, pktdata, pktlen, pkthdr_ptr) < (int)pktlen) {
  671. warnx("Unable to send packet: %s", sendpacket_geterr(sp));
  672. break;
  673. }
  674. /*
  675. * Mark the time when we sent the last packet
  676. */
  677. TIMEVAL_SET(&stats->end_time, &now);
  678. ++stats->pkts_sent;
  679. stats->bytes_sent += pktlen;
  680. /* print stats during the run? */
  681. if (options->stats > 0) {
  682. if (! timerisset(&stats->last_print)) {
  683. TIMEVAL_SET(&stats->last_print, &now);
  684. } else {
  685. timersub(&now, &stats->last_print, &print_delta);
  686. if (print_delta.tv_sec >= options->stats) {
  687. TIMEVAL_SET(&stats->end_time, &now);
  688. packet_stats(stats);
  689. TIMEVAL_SET(&stats->last_print, &now);
  690. }
  691. }
  692. }
  693. #if defined HAVE_NETMAP
  694. if (sp->first_packet || timesisset(&ctx->nap)) {
  695. wake_send_queues(sp, options);
  696. sp->first_packet = false;
  697. }
  698. #endif
  699. /* get the next packet for this file handle depending on which we last used */
  700. if (sp == ctx->intf2) {
  701. pktdata2 = get_next_packet(ctx, pcap2, &pkthdr2, cache_file_idx2, prev_packet2);
  702. } else {
  703. pktdata1 = get_next_packet(ctx, pcap1, &pkthdr1, cache_file_idx1, prev_packet1);
  704. }
  705. /* stop sending based on the duration limit... */
  706. if ((end_us > 0 && (COUNTER)TIMEVAL_TO_MICROSEC(&now) > end_us) ||
  707. /* ... or stop sending based on the limit -L? */
  708. (limit_send > 0 && stats->pkts_sent >= limit_send)) {
  709. ctx->abort = true;
  710. }
  711. } /* while */
  712. #ifdef HAVE_NETMAP
  713. /* when completing test, wait until the last packet is sent */
  714. if (options->netmap && (ctx->abort || options->loop == 1)) {
  715. while (ctx->intf1 && !netmap_tx_queues_empty(ctx->intf1)) {
  716. gettimeofday(&now, NULL);
  717. now_is_now = true;
  718. }
  719. while (ctx->intf2 && !netmap_tx_queues_empty(ctx->intf2)) {
  720. gettimeofday(&now, NULL);
  721. now_is_now = true;
  722. }
  723. }
  724. #endif /* HAVE_NETMAP */
  725. if (!now_is_now)
  726. gettimeofday(&now, NULL);
  727. TIMEVAL_SET(&stats->end_time, &now);
  728. increment_iteration(ctx);
  729. }
  730. /**
  731. * Gets the next packet to be sent out. This will either read from the pcap file
  732. * or will retrieve the packet from the internal cache.
  733. *
  734. * The parameter prev_packet is used as the parent of the new entry in the cache list.
  735. * This should be NULL on the first call to this function for each file and
  736. * will be updated as new entries are added (or retrieved) from the cache list.
  737. */
  738. u_char *
  739. get_next_packet(tcpreplay_t *ctx, pcap_t *pcap, struct pcap_pkthdr *pkthdr, int idx,
  740. packet_cache_t **prev_packet)
  741. {
  742. tcpreplay_opt_t *options = ctx->options;
  743. u_char *pktdata = NULL;
  744. uint32_t pktlen;
  745. /* pcap may be null in cache mode! */
  746. /* packet_cache_t may be null in file read mode! */
  747. assert(pkthdr);
  748. /*
  749. * Check if we're caching files
  750. */
  751. if (options->preload_pcap && (prev_packet != NULL)) {
  752. /*
  753. * Yes we are caching files - has this one been cached?
  754. */
  755. if (options->file_cache[idx].cached) {
  756. if (*prev_packet == NULL) {
  757. /*
  758. * Get the first packet in the cache list directly from the file
  759. */
  760. *prev_packet = options->file_cache[idx].packet_cache;
  761. } else {
  762. /*
  763. * Get the next packet in the cache list
  764. */
  765. *prev_packet = (*prev_packet)->next;
  766. }
  767. if (*prev_packet != NULL) {
  768. pktdata = (*prev_packet)->pktdata;
  769. memcpy(pkthdr, &((*prev_packet)->pkthdr), sizeof(struct pcap_pkthdr));
  770. }
  771. } else {
  772. /*
  773. * We should read the pcap file, and cache the results
  774. */
  775. pktdata = safe_pcap_next(pcap, pkthdr);
  776. if (pktdata != NULL) {
  777. if (*prev_packet == NULL) {
  778. /*
  779. * Create the first packet in the list
  780. */
  781. *prev_packet = safe_malloc(sizeof(packet_cache_t));
  782. options->file_cache[idx].packet_cache = *prev_packet;
  783. } else {
  784. /*
  785. * Add a packet to the end of the list
  786. */
  787. (*prev_packet)->next = safe_malloc(sizeof(packet_cache_t));
  788. *prev_packet = (*prev_packet)->next;
  789. }
  790. if (*prev_packet != NULL) {
  791. (*prev_packet)->next = NULL;
  792. pktlen = pkthdr->caplen;
  793. (*prev_packet)->pktdata = safe_malloc(pktlen + PACKET_HEADROOM);
  794. memcpy((*prev_packet)->pktdata, pktdata, pktlen);
  795. memcpy(&((*prev_packet)->pkthdr), pkthdr, sizeof(struct pcap_pkthdr));
  796. }
  797. }
  798. }
  799. } else {
  800. /*
  801. * Read pcap file as normal
  802. */
  803. pktdata = safe_pcap_next(pcap, pkthdr);
  804. }
  805. /* this gets casted to a const on the way out */
  806. return pktdata;
  807. }
  808. /**
  809. * determines based upon the cachedata which interface the given packet
  810. * should go out. Also rewrites any layer 2 data we might need to adjust.
  811. * Returns a void cased pointer to the ctx->intfX of the corresponding
  812. * interface or NULL on error
  813. */
  814. void *
  815. cache_mode(tcpreplay_t *ctx, char *cachedata, COUNTER packet_num)
  816. {
  817. tcpreplay_opt_t *options = ctx->options;
  818. void *sp = NULL;
  819. int result;
  820. if (packet_num > options->cache_packets) {
  821. tcpreplay_seterr(ctx, "%s", "Exceeded number of packets in cache file.");
  822. return NULL;
  823. }
  824. result = check_cache(cachedata, packet_num);
  825. if (result == TCPR_DIR_NOSEND) {
  826. dbgx(2, "Cache: Not sending packet " COUNTER_SPEC ".", packet_num);
  827. return NULL;
  828. }
  829. else if (result == TCPR_DIR_C2S) {
  830. dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out primary interface.", packet_num);
  831. sp = ctx->intf1;
  832. }
  833. else if (result == TCPR_DIR_S2C) {
  834. dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out secondary interface.", packet_num);
  835. sp = ctx->intf2;
  836. }
  837. else {
  838. tcpreplay_seterr(ctx, "Invalid cache value: %i", result);
  839. return NULL;
  840. }
  841. return sp;
  842. }
  843. /**
  844. * Given the timestamp on the current packet and the last packet sent,
  845. * calculate the appropriate amount of time to sleep. Sleep time
  846. * will be in ctx->nap.
  847. */
  848. static void calc_sleep_time(tcpreplay_t *ctx, struct timeval *pkt_ts_delta,
  849. struct timeval *time_delta, COUNTER len,
  850. sendpacket_t *sp, COUNTER counter, timestamp_t *sent_timestamp,
  851. COUNTER start_us, COUNTER *skip_length)
  852. {
  853. tcpreplay_opt_t *options = ctx->options;
  854. struct timeval nap_for;
  855. COUNTER now_us;
  856. timesclear(&ctx->nap);
  857. /*
  858. * pps_multi accelerator. This uses the existing send accelerator
  859. * and hence requires the funky math to get the expected timings.
  860. */
  861. if (options->speed.mode == speed_packetrate && options->speed.pps_multi) {
  862. ctx->skip_packets = options->speed.pps_multi - 1;
  863. }
  864. /* no last packet sent, just leave */
  865. if (ctx->first_time) {
  866. ctx->first_time = false;
  867. return;
  868. }
  869. switch(options->speed.mode) {
  870. case speed_multiplier:
  871. /*
  872. * Replay packets a factor of the time they were originally sent.
  873. * Make sure the packet is not late.
  874. */
  875. if (timercmp(pkt_ts_delta, time_delta, >)) {
  876. /* pkt_time_delta has increased, so handle normally */
  877. timersub(pkt_ts_delta, time_delta, &nap_for);
  878. TIMEVAL_TO_TIMESPEC(&nap_for, &ctx->nap);
  879. dbgx(3, "original packet delta time: " TIMESPEC_FORMAT,
  880. ctx->nap.tv_sec, ctx->nap.tv_nsec);
  881. timesdiv_float(&ctx->nap, options->speed.multiplier);
  882. dbgx(3, "original packet delta/div: " TIMESPEC_FORMAT,
  883. ctx->nap.tv_sec, ctx->nap.tv_nsec);
  884. }
  885. break;
  886. case speed_mbpsrate:
  887. /*
  888. * Ignore the time supplied by the capture file and send data at
  889. * a constant 'rate' (bytes per second).
  890. */
  891. now_us = TIMSTAMP_TO_MICROSEC(sent_timestamp);
  892. if (now_us) {
  893. COUNTER next_tx_us;
  894. COUNTER bps = options->speed.speed;
  895. COUNTER bits_sent = ((ctx->stats.bytes_sent + len) * 8);
  896. COUNTER tx_us = now_us - start_us;
  897. /*
  898. * bits * 1000000 divided by bps = microseconds
  899. *
  900. * ensure there is no overflow in cases where bits_sent is very high
  901. */
  902. if (bits_sent > COUNTER_OVERFLOW_RISK && bps > 500000)
  903. next_tx_us = (bits_sent * 1000) / bps * 1000;
  904. else
  905. next_tx_us = (bits_sent * 1000000) / bps;
  906. if (next_tx_us > tx_us) {
  907. NANOSEC_TO_TIMESPEC((next_tx_us - tx_us) * 1000, &ctx->nap);
  908. } else if (tx_us > next_tx_us) {
  909. tx_us = now_us - start_us;
  910. *skip_length = ((tx_us - next_tx_us) * bps) / 8000000;
  911. }
  912. update_current_timestamp_trace_entry(ctx->stats.bytes_sent +
  913. (COUNTER)len, now_us, tx_us, next_tx_us);
  914. }
  915. dbgx(3, "packet size=" COUNTER_SPEC "\t\tnap=" TIMESPEC_FORMAT, len,
  916. ctx->nap.tv_sec, ctx->nap.tv_nsec);
  917. break;
  918. case speed_packetrate:
  919. /*
  920. * Ignore the time supplied by the capture file and send data at
  921. * a constant rate (packets per second).
  922. */
  923. now_us = TIMSTAMP_TO_MICROSEC(sent_timestamp);
  924. if (now_us) {
  925. COUNTER next_tx_us;
  926. COUNTER pph = ctx->options->speed.speed;
  927. COUNTER pkts_sent = ctx->stats.pkts_sent;
  928. COUNTER tx_us = now_us - start_us;
  929. /*
  930. * packets * 1000000 divided by pps = microseconds
  931. * packets per sec (pps) = packets per hour / (60 * 60)
  932. *
  933. * Adjust for long running tests with high PPS to prevent overflow.
  934. * When active, adjusted calculation may add a bit of jitter.
  935. */
  936. if ((pkts_sent < COUNTER_OVERFLOW_RISK))
  937. next_tx_us = (pkts_sent * 1000000) * (60 * 60) / pph;
  938. else
  939. next_tx_us = ((pkts_sent * 1000000) / pph) * (60 * 60);
  940. if (next_tx_us > tx_us)
  941. NANOSEC_TO_TIMESPEC((next_tx_us - tx_us) * 1000, &ctx->nap);
  942. else
  943. ctx->skip_packets = options->speed.pps_multi;
  944. update_current_timestamp_trace_entry(ctx->stats.bytes_sent +
  945. (COUNTER)len, now_us, tx_us, next_tx_us);
  946. }
  947. dbgx(3, "packet count=" COUNTER_SPEC "\t\tnap=" TIMESPEC_FORMAT,
  948. ctx->stats.pkts_sent, ctx->nap.tv_sec, ctx->nap.tv_nsec);
  949. break;
  950. case speed_oneatatime:
  951. /* do we skip prompting for a key press? */
  952. if (ctx->skip_packets == 0) {
  953. ctx->skip_packets = get_user_count(ctx, sp, counter);
  954. }
  955. /* decrement our send counter */
  956. printf("Sending packet " COUNTER_SPEC " out: %s\n", counter,
  957. sp == ctx->intf1 ? options->intf1_name : options->intf2_name);
  958. ctx->skip_packets--;
  959. /* leave */
  960. break;
  961. case speed_topspeed:
  962. break;
  963. default:
  964. errx(-1, "Unknown/supported speed mode: %d", options->speed.mode);
  965. break;
  966. }
  967. }
  968. static void tcpr_sleep(tcpreplay_t *ctx, sendpacket_t *sp,
  969. struct timespec *nap_this_time, struct timeval *now)
  970. {
  971. tcpreplay_opt_t *options = ctx->options;
  972. bool flush =
  973. #ifdef HAVE_NETMAP
  974. true;
  975. #else
  976. false;
  977. #endif
  978. /* don't sleep if nap = {0, 0} */
  979. if (!timesisset(nap_this_time))
  980. return;
  981. /* do we need to limit the total time we sleep? */
  982. if (timesisset(&(options->maxsleep)) && (timescmp(nap_this_time, &(options->maxsleep), >))) {
  983. dbgx(2, "Was going to sleep for " TIMESPEC_FORMAT " but maxsleeping for " TIMESPEC_FORMAT,
  984. nap_this_time->tv_sec, nap_this_time->tv_nsec, options->maxsleep.tv_sec,
  985. options->maxsleep.tv_nsec);
  986. TIMESPEC_SET(nap_this_time, &options->maxsleep);
  987. }
  988. if (flush)
  989. wake_send_queues(sp, options);
  990. dbgx(2, "Sleeping: " TIMESPEC_FORMAT,
  991. nap_this_time->tv_sec, nap_this_time->tv_nsec);
  992. /*
  993. * Depending on the accurate method & packet rate computation method
  994. * We have multiple methods of sleeping, pick the right one...
  995. */
  996. switch (options->accurate) {
  997. #ifdef HAVE_SELECT
  998. case accurate_select:
  999. select_sleep(sp, nap_this_time, now, flush);
  1000. break;
  1001. #endif
  1002. #if defined HAVE_IOPORT_SLEEP
  1003. case accurate_ioport:
  1004. ioport_sleep(sp, nap_this_time, now, flush);
  1005. break;
  1006. #endif
  1007. case accurate_gtod:
  1008. gettimeofday_sleep(sp, nap_this_time, now, flush);
  1009. break;
  1010. case accurate_nanosleep:
  1011. nanosleep_sleep(sp, nap_this_time, now, flush);
  1012. break;
  1013. default:
  1014. errx(-1, "Unknown timer mode %d", options->accurate);
  1015. }
  1016. }
  1017. /**
  1018. * Ask the user how many packets they want to send.
  1019. */
  1020. static uint32_t
  1021. get_user_count(tcpreplay_t *ctx, sendpacket_t *sp, COUNTER counter)
  1022. {
  1023. tcpreplay_opt_t *options = ctx->options;
  1024. struct pollfd poller[1]; /* use poll to read from the keyboard */
  1025. char input[EBUF_SIZE];
  1026. unsigned long send = 0;
  1027. printf("**** Next packet #" COUNTER_SPEC " out %s. How many packets do you wish to send? ",
  1028. counter, (sp == ctx->intf1 ? options->intf1_name : options->intf2_name));
  1029. fflush(NULL);
  1030. poller[0].fd = STDIN_FILENO;
  1031. poller[0].events = POLLIN | POLLPRI | POLLNVAL;
  1032. poller[0].revents = 0;
  1033. if (fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK))
  1034. errx(-1, "Unable to clear non-blocking flag on stdin: %s", strerror(errno));
  1035. /* wait for the input */
  1036. if (poll(poller, 1, -1) < 0)
  1037. errx(-1, "Error reading user input from stdin: %s", strerror(errno));
  1038. /*
  1039. * read to the end of the line or EBUF_SIZE,
  1040. * Note, if people are stupid, and type in more text then EBUF_SIZE
  1041. * then the next fgets() will pull in that data, which will have poor
  1042. * results. fuck them.
  1043. */
  1044. if (fgets(input, sizeof(input), stdin) == NULL) {
  1045. errx(-1, "Unable to process user input for fd %d: %s", fileno(stdin), strerror(errno));
  1046. } else if (strlen(input) > 1) {
  1047. send = strtoul(input, NULL, 0);
  1048. }
  1049. /* how many packets should we send? */
  1050. if (send == 0) {
  1051. dbg(1, "Input was less then 1 or non-numeric, assuming 1");
  1052. /* assume send only one packet */
  1053. send = 1;
  1054. }
  1055. return(uint32_t)send;
  1056. }