send_packets.c 45 KB

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