source: test/test-live.c @ 8decff7

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 8decff7 was 8decff7, checked in by Shane Alcock <salcock@…>, 5 years ago

Update all parallel tests so they compile and run

  • Property mode set to 100644
File size: 10.1 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand.
5 * Authors: Richard Sanger
6 *         
7 * All rights reserved.
8 *
9 * This code has been developed by the University of Waikato WAND
10 * research group. For further information please see http://www.wand.net.nz/
11 *
12 * libtrace is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * libtrace is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with libtrace; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 *
26 * $Id$
27 *
28 */
29
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <assert.h>
34#include <string.h>
35#include <sys/time.h>
36#include <sys/types.h>
37#include <time.h>
38#include <math.h>
39#include <signal.h>
40#include <unistd.h>
41#include <stdint.h>
42#include <netinet/in.h>
43#include <netinet/in_systm.h>
44#include <netinet/tcp.h>
45#include <netinet/ip.h>
46#include <netinet/ip_icmp.h>
47#include <arpa/inet.h>
48#include <sys/socket.h>
49#include "dagformat.h"
50#include "libtrace.h"
51
52
53#define ONCE(run_me) { \
54        static int hit = 0; \
55        if (!hit) { \
56                run_me \
57        } \
58        hit = 1; \
59}
60
61#define ERROR(mesg, ...) { \
62        ONCE( \
63                err = 1; \
64                fprintf(stderr, "%s[%d] Error: " mesg, uri_read, i, __VA_ARGS__); \
65        ) \
66}
67
68static const char *uri_read;
69static sig_atomic_t i = 0;
70static sig_atomic_t reading = 0;
71static libtrace_t *trace_read;
72static int test_size = 100;
73
74static const char *lookup_uri_write(const char *type)
75{
76        if (!strcmp(type, "int"))
77                return "int:veth0";
78        if (!strcmp(type, "ring"))
79                return "ring:veth0";
80        if (!strcmp(type, "pcapint"))
81                return "pcapint:veth0";
82        if (!strncmp(type, "dpdk:", sizeof("dpdk:")))
83                return type;
84        return "unknown";
85}
86
87static const char *lookup_uri_read(const char *type)
88{
89        if (!strcmp(type, "int"))
90                return "int:veth1";
91        if (!strcmp(type, "ring"))
92                return "ring:veth1";
93        if (!strcmp(type, "pcapint")) {
94                // The newer Linux memmap (ring:) implementation of PCAP only makes
95                // space for about 30 maybe 31 packet buffers. If we exceeded this we'll
96                // drop packets.
97                test_size = 30; 
98                return "pcapint:veth1";
99        }
100        if (!strncmp(type, "dpdk:", sizeof("dpdk:")))
101                return type;
102        return "unknown";
103}
104
105
106/**
107 * Source packet we modify this every write see build_packet
108 */
109static unsigned char buffer[] = {
110        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, /* Dest Mac */
111        0x00, 0x01, 0x02, 0x03, 0x04, 0x06, /* Src Mac */
112        0x01, 0x01, /* Ethertype = Experimental */
113        0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */
114};
115
116
117union uint32_char {
118        char chars[4];
119        uint32_t i32;
120};
121
122/* Build a unique packet from a seed value of i */
123static void build_packet(int i)
124{
125        // Shh about them type-punned aliasing issues
126        union uint32_char u;
127        srand(i);
128        u.i32 = (uint32_t) rand();
129        buffer[sizeof(libtrace_ether_t)] = u.chars[0];
130        buffer[sizeof(libtrace_ether_t)+1] = u.chars[1];
131        buffer[sizeof(libtrace_ether_t)+2] = u.chars[2];
132        buffer[sizeof(libtrace_ether_t)+3] = u.chars[3];
133}
134
135static void dumparray(unsigned char *arr, size_t len)
136{
137        size_t i;
138
139        fprintf(stderr, "{");
140        for (i = 0; i < len; ++i) {
141                fprintf(stderr, "0x%02X,", arr[i]);
142        }
143        fprintf(stderr, "}\n");
144}
145
146/**
147 * Verifies statistic counters at the end of the trace
148 */
149static int verify_counters(libtrace_t *trace_read)
150{
151        int err = 0;
152        libtrace_stat_t *stat;
153
154        stat = trace_create_statistics();
155
156        trace_get_statistics(trace_read, stat);
157        // Assume no loss here, if not the case we would of hung in reading loop
158        // anyway
159        if (!stat->dropped_valid) {
160                printf("\tInfo: trace does not support drop counter\n");
161        } else if (stat->dropped != 0) {
162                ERROR("Trace dropped %zu packets\n", stat->dropped);
163        }
164
165        if (!stat->filtered_valid) {
166                printf("\tInfo: trace does not support filter counter\n");
167        } else if (stat->filtered != 0) {
168                ERROR("Trace filtered %zu packets\n", stat->filtered);
169        }
170
171        if (!stat->received_valid) {
172                printf("\tInfo: trace does not support received counter\n");
173        } else if (stat->received != 100) {
174                ERROR("Trace received %zu/100 packets\n", stat->received);
175        }
176
177        if (!stat->accepted_valid) {
178                printf("\tInfo: trace does not support accepted counter\n");
179        } else if (stat->accepted != (uint32_t) test_size) {
180                ERROR("Trace only accepted %zu/%u packets\n", stat->accepted,
181                                (uint32_t)test_size);
182        }
183
184        return err;
185}
186
187static void signal_handler(int signal)
188{
189        if (signal == SIGALRM) {
190                if (reading) {
191                        verify_counters(trace_read);
192                        fprintf(stderr, "!!!Timeout after reading only %d packets of %d!!!\n", i, test_size);
193                } else {
194                        fprintf(stderr, "!!!Timeout after writing only %d packets of %d!!!\n", i, test_size);
195                }
196                exit(-1);
197        }
198}
199
200/**
201 * Verifies a packet matches with what we expected
202 */
203static int verify_packet(libtrace_packet_t *packet, int seq_num)
204{
205        int err = 0;
206        static int caplen_incld_crc = -1;
207        static double ts = -1;
208        libtrace_linktype_t linktype;
209        uint32_t remaining;
210        unsigned char* pktbuffer;
211
212        // Verify wirelen - Wirelen includes checksum of 4 bytes
213        if (trace_get_wire_length(packet) != sizeof(buffer) + 4) {
214                ERROR("Incorrect trace_get_wire_length(), read %zu expected %zu\n",
215                                trace_get_wire_length(packet), sizeof(buffer) + 4);
216        }
217
218        // Verify caplen
219        if (trace_get_capture_length(packet) == sizeof(buffer)) {
220                if (caplen_incld_crc == 1) {
221                        ERROR("Expected trace_get_capture_length() to EXCLUDE the Ethernet checksum,"
222                                " read %zu expected %zu\n", trace_get_capture_length(packet),
223                                sizeof(buffer));
224                } else {
225                        caplen_incld_crc = 0;
226                }
227        } else if (trace_get_capture_length(packet) == sizeof(buffer) + 4) {
228                if (caplen_incld_crc == 0) {
229                        ERROR("Expected trace_get_capture_length() to INCLUDE the Ethernet checksum,"
230                                " read %zu expected %zu\n", trace_get_capture_length(packet),
231                                sizeof(buffer)+4);
232                } else {
233                        caplen_incld_crc = 1;
234                }
235        } else {
236                ERROR("Incorrect trace_get_capture_length(), read %zu expected %zu (or %zu)\n",
237                        trace_get_capture_length(packet), sizeof(buffer), sizeof(buffer)+4);
238        }
239
240        // Verify a packets contents
241        pktbuffer = trace_get_packet_buffer(packet, &linktype, &remaining);
242        assert(trace_get_capture_length(packet) == remaining);
243        assert(linktype == TRACE_TYPE_ETH);
244        // Refill the buffer with the expected data
245        build_packet(seq_num);
246        if (memcmp(pktbuffer, buffer, MIN(sizeof(buffer), remaining)) != 0) {
247                ERROR("Packet contents do not match\n%s", "Received:\n");
248                ONCE(
249                dumparray(pktbuffer, remaining);
250                fprintf(stderr, "Expected:\n");
251                dumparray(buffer, sizeof(buffer));
252                )
253        }
254
255        // Check times count up like we'd expect
256        if (ts != 1 && trace_get_seconds(packet) < ts) {
257                ERROR("Timestamps aren't increasing, ts=%f last_ts=%f\n",
258                        trace_get_seconds(packet), ts);
259        }
260
261        ts = trace_get_seconds(packet);
262
263        // Verify trace_get and set direction work
264        libtrace_direction_t dir_set;
265        if ((dir_set = trace_set_direction(packet, TRACE_DIR_OUTGOING)) != -1) {
266                if (trace_get_direction(packet) != TRACE_DIR_OUTGOING) {
267                        ERROR("Trace lies about its ability to set TRACE_DIR_OUTGOING,"
268                                "read %d expected %d\n", trace_get_direction(packet),
269                                TRACE_DIR_OUTGOING);
270                }
271        }
272        if ((dir_set = trace_set_direction(packet, TRACE_DIR_INCOMING)) != -1) {
273                if (trace_get_direction(packet) != TRACE_DIR_INCOMING) {
274                        ERROR("Trace lies about its ability to set TRACE_DIR_INCOMING,"
275                                "read %d expected %d\n", trace_get_direction(packet),
276                                TRACE_DIR_INCOMING);
277                }
278        }
279        if ((dir_set = trace_set_direction(packet, TRACE_DIR_OTHER)) != -1) {
280                if (trace_get_direction(packet) != TRACE_DIR_OTHER) {
281                        ERROR("Trace lies about its ability to set TRACE_DIR_OTHER,"
282                                "read %d expected %d\n", trace_get_direction(packet),
283                                TRACE_DIR_OTHER);
284                }
285        }
286        if ((dir_set = trace_set_direction(packet, TRACE_DIR_UNKNOWN)) != -1) {
287                if (trace_get_direction(packet) != TRACE_DIR_UNKNOWN) {
288                        ERROR("Trace lies about its ability to set TRACE_DIR_UNKNOWN,"
289                                "read %d expected %d\n", trace_get_direction(packet),
290                                TRACE_DIR_UNKNOWN);
291                }
292        }
293        return err;
294}
295
296static void iferr_out(libtrace_out_t *trace)
297{
298        libtrace_err_t err = trace_get_err_output(trace);
299        if (err.err_num == 0)
300                return;
301        printf("Error: %s\n", err.problem);
302        exit(1);
303}
304
305static void iferr(libtrace_t *trace)
306{
307        libtrace_err_t err = trace_get_err(trace);
308        if (err.err_num == 0)
309                return;
310        printf("Error: %s\n", err.problem);
311        exit(1);
312}
313
314int main(int argc, char *argv[])
315{
316        libtrace_out_t *trace_write;
317        libtrace_packet_t *packet;
318        int psize;
319        int err = 0;
320
321        if (argc < 3) {
322                fprintf(stderr, "usage: %s type(write) type(read)\n", argv[0]);
323                return 1;
324        }
325
326        signal(SIGALRM, signal_handler);
327        // Timeout after 5 seconds
328        alarm(5);
329
330        trace_write = trace_create_output(lookup_uri_write(argv[1]));
331        iferr_out(trace_write);
332        uri_read = lookup_uri_read(argv[2]);
333        trace_read = trace_create(uri_read);
334        iferr(trace_read);
335
336        trace_start_output(trace_write);
337        iferr_out(trace_write);
338        trace_start(trace_read);
339        iferr(trace_read);
340
341        packet = trace_create_packet();
342
343        // Write out test_size (100) almost identical packets
344        for (i = 0; i < test_size; i++) {
345                build_packet(i);
346                trace_construct_packet(packet, TRACE_TYPE_ETH, buffer, sizeof(buffer));
347                if (trace_write_packet(trace_write, packet) == -1) {
348                        iferr_out(trace_write);
349                }
350        }
351        trace_destroy_packet(packet);
352        trace_destroy_output(trace_write);
353
354        // Now read back in, we assume that buffers internally can buffer
355        // the packets without losing them
356        packet = trace_create_packet();
357
358        reading = 1;
359        for (i = 0; i < test_size; i++) {
360                if ((psize = trace_read_packet(trace_read, packet)) < 0) {
361                        iferr(trace_read);
362                        // EOF we shouldn't hit this with a live format
363                        fprintf(stderr, "Error: looks like we lost some packets!\n");
364                        err = 1;
365                        break;
366                }
367                err |= verify_packet(packet, i);
368        }
369
370        err |= verify_counters(trace_read);
371
372        trace_destroy_packet(packet);
373        trace_destroy(trace_read);
374
375        return err;
376}
Note: See TracBrowser for help on using the repository browser.