Home | History | Annotate | Line # | Download | only in shmif_dumpbus
shmif_dumpbus.c revision 1.14
      1 /*	$NetBSD: shmif_dumpbus.c,v 1.14 2014/08/18 14:23:24 pooka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * Convert shmif bus traffic to a pcap file which can be then
     30  * examined with tcpdump -r, wireshark, etc.
     31  */
     32 
     33 #include <rump/rumpuser_port.h>
     34 
     35 #ifndef lint
     36 __RCSID("$NetBSD: shmif_dumpbus.c,v 1.14 2014/08/18 14:23:24 pooka Exp $");
     37 #endif /* !lint */
     38 
     39 #include <sys/types.h>
     40 #include <sys/mman.h>
     41 #include <sys/stat.h>
     42 #ifdef __NetBSD__
     43 #include <sys/bswap.h>
     44 #endif
     45 
     46 #include <assert.h>
     47 #include <err.h>
     48 #include <fcntl.h>
     49 #include <inttypes.h>
     50 #include <pcap.h>
     51 #include <stdbool.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 
     57 #include "shmifvar.h"
     58 
     59 __dead static void
     60 usage(void)
     61 {
     62 
     63 #ifndef PLATFORM_HAS_SETGETPROGNAME
     64 #define getprogname() "shmif_dumpbus"
     65 #endif
     66 
     67 	fprintf(stderr, "usage: %s [-h] [-p pcapfile] buspath\n",getprogname());
     68 	exit(1);
     69 }
     70 
     71 #define BUFSIZE 64*1024
     72 
     73 /*
     74  * byte swapdom
     75  */
     76 static uint32_t
     77 swp32(uint32_t x)
     78 {
     79 	uint32_t v;
     80 
     81 	v = (((x) & 0xff000000) >> 24) |
     82 	    (((x) & 0x00ff0000) >>  8) |
     83 	    (((x) & 0x0000ff00) <<  8) |
     84 	    (((x) & 0x000000ff) << 24);
     85 	return v;
     86 }
     87 
     88 static uint64_t
     89 swp64(uint64_t x)
     90 {
     91 	uint64_t v;
     92 
     93 	v = (((x) & 0xff00000000000000ull) >> 56) |
     94 	    (((x) & 0x00ff000000000000ull) >> 40) |
     95 	    (((x) & 0x0000ff0000000000ull) >> 24) |
     96 	    (((x) & 0x000000ff00000000ull) >>  8) |
     97 	    (((x) & 0x00000000ff000000ull) <<  8) |
     98 	    (((x) & 0x0000000000ff0000ull) << 24) |
     99 	    (((x) & 0x000000000000ff00ull) << 40) |
    100 	    (((x) & 0x00000000000000ffull) << 56);
    101 	return v;
    102 }
    103 
    104 #define FIXENDIAN32(x) (doswap ? swp32(x) : (x))
    105 #define FIXENDIAN64(x) (doswap ? swp64(x) : (x))
    106 
    107 int
    108 main(int argc, char *argv[])
    109 {
    110 	struct stat sb;
    111 	void *busmem;
    112 	const char *pcapfile = NULL;
    113 	uint32_t curbus, buslast;
    114 	struct shmif_mem *bmem;
    115 	int fd, i, ch;
    116 	int bonus;
    117 	char *buf;
    118 	bool hflag = false, doswap = false;
    119 	pcap_dumper_t *pdump;
    120 	FILE *dumploc = stdout;
    121 
    122 #ifdef PLATFORM_HAS_SETGETPROGNAME
    123 	setprogname(argv[0]);
    124 #endif
    125 
    126 	while ((ch = getopt(argc, argv, "hp:")) != -1) {
    127 		switch (ch) {
    128 		case 'h':
    129 			hflag = true;
    130 			break;
    131 		case 'p':
    132 			pcapfile = optarg;
    133 			break;
    134 		default:
    135 			usage();
    136 		}
    137 	}
    138 
    139 	argc -= optind;
    140 	argv += optind;
    141 
    142 	if (argc != 1)
    143 		usage();
    144 
    145 	buf = malloc(BUFSIZE);
    146 	if (buf == NULL)
    147 		err(1, "malloc");
    148 
    149 	fd = open(argv[0], O_RDONLY);
    150 	if (fd == -1)
    151 		err(1, "open bus");
    152 
    153 	if (fstat(fd, &sb) == -1)
    154 		err(1, "stat");
    155 
    156 	busmem = mmap(NULL, sb.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0);
    157 	if (busmem == MAP_FAILED)
    158 		err(1, "mmap");
    159 	bmem = busmem;
    160 
    161 	if (bmem->shm_magic != SHMIF_MAGIC) {
    162 		if (bmem->shm_magic != swp32(SHMIF_MAGIC))
    163 			errx(1, "%s not a shmif bus", argv[0]);
    164 		doswap = true;
    165 	}
    166 	if (FIXENDIAN32(bmem->shm_version) != SHMIF_VERSION)
    167 		errx(1, "bus version %d, program %d",
    168 		    FIXENDIAN32(bmem->shm_version), SHMIF_VERSION);
    169 
    170 	if (pcapfile && strcmp(pcapfile, "-") == 0)
    171 		dumploc = stderr;
    172 
    173 	fprintf(dumploc, "bus version %d, lock: %d, generation: %" PRIu64
    174 	    ", firstoff: 0x%04x, lastoff: 0x%04x\n",
    175 	    FIXENDIAN32(bmem->shm_version), FIXENDIAN32(bmem->shm_lock),
    176 	    FIXENDIAN64(bmem->shm_gen),
    177 	    FIXENDIAN32(bmem->shm_first), FIXENDIAN32(bmem->shm_last));
    178 
    179 	if (hflag)
    180 		exit(0);
    181 
    182 	if (pcapfile) {
    183 		pcap_t *pcap = pcap_open_dead(DLT_EN10MB, 1518);
    184 		pdump = pcap_dump_open(pcap, pcapfile);
    185 		if (pdump == NULL)
    186 			err(1, "cannot open pcap dump file");
    187 	} else {
    188 		/* XXXgcc */
    189 		pdump = NULL;
    190 	}
    191 
    192 	curbus = FIXENDIAN32(bmem->shm_first);
    193 	buslast = FIXENDIAN32(bmem->shm_last);
    194 	if (curbus == BUSMEM_DATASIZE)
    195 		curbus = 0;
    196 
    197 	bonus = 0;
    198 	if (buslast < curbus)
    199 		bonus = 1;
    200 
    201 	i = 0;
    202 	while (curbus <= buslast || bonus) {
    203 		struct pcap_pkthdr packhdr;
    204 		struct shmif_pkthdr sp;
    205 		uint32_t oldoff;
    206 		uint32_t curlen;
    207 		bool wrap;
    208 
    209 		assert(curbus < sb.st_size);
    210 
    211 		wrap = false;
    212 		oldoff = curbus;
    213 		curbus = shmif_busread(bmem, &sp, oldoff, sizeof(sp), &wrap);
    214 		if (wrap)
    215 			bonus = 0;
    216 
    217 		assert(curbus < sb.st_size);
    218 		curlen = FIXENDIAN32(sp.sp_len);
    219 
    220 		if (curlen == 0) {
    221 			continue;
    222 		}
    223 
    224 		fprintf(dumploc, "packet %d, offset 0x%04x, length 0x%04x, "
    225 			    "ts %d/%06d\n", i++, curbus, curlen,
    226 			    FIXENDIAN32(sp.sp_sec), FIXENDIAN32(sp.sp_usec));
    227 
    228 		if (!pcapfile) {
    229 			curbus = shmif_busread(bmem,
    230 			    buf, curbus, curlen, &wrap);
    231 			if (wrap)
    232 				bonus = 0;
    233 			continue;
    234 		}
    235 
    236 		memset(&packhdr, 0, sizeof(packhdr));
    237 		packhdr.caplen = packhdr.len = curlen;
    238 		packhdr.ts.tv_sec = FIXENDIAN32(sp.sp_sec);
    239 		packhdr.ts.tv_usec = FIXENDIAN32(sp.sp_usec);
    240 		assert(curlen <= BUFSIZE);
    241 
    242 		curbus = shmif_busread(bmem, buf, curbus, curlen, &wrap);
    243 		pcap_dump((u_char *)pdump, &packhdr, (u_char *)buf);
    244 		if (wrap)
    245 			bonus = 0;
    246 	}
    247 
    248 	if (pcapfile)
    249 		pcap_dump_close(pdump);
    250 
    251 	return 0;
    252 }
    253