Home | History | Annotate | Line # | Download | only in shmif_dumpbus
shmif_dumpbus.c revision 1.10
      1 /*	$NetBSD: shmif_dumpbus.c,v 1.10 2013/12/20 09:36:03 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 #ifndef lint
     34 __RCSID("$NetBSD: shmif_dumpbus.c,v 1.10 2013/12/20 09:36:03 pooka Exp $");
     35 #endif /* !lint */
     36 
     37 #include <sys/types.h>
     38 #include <sys/mman.h>
     39 #include <sys/stat.h>
     40 
     41 #include <machine/bswap.h>
     42 
     43 #include <assert.h>
     44 #include <err.h>
     45 #include <fcntl.h>
     46 #include <inttypes.h>
     47 #include <pcap.h>
     48 #include <stdbool.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 
     54 #include "shmifvar.h"
     55 
     56 __dead static void
     57 usage(void)
     58 {
     59 
     60 	fprintf(stderr, "usage: %s [-h] [-p pcapfile] buspath\n",getprogname());
     61 	exit(1);
     62 }
     63 
     64 #define BUFSIZE 64*1024
     65 #define SWAPME(a) (doswap ? bswap32(a) : (a))
     66 #define SWAPME64(a) (doswap ? bswap64(a) : (a))
     67 int
     68 main(int argc, char *argv[])
     69 {
     70 	struct stat sb;
     71 	void *busmem;
     72 	const char *pcapfile = NULL;
     73 	uint32_t curbus, buslast;
     74 	struct shmif_mem *bmem;
     75 	int fd, i, ch;
     76 	int bonus;
     77 	char *buf;
     78 	bool hflag = false, doswap = false;
     79 	pcap_dumper_t *pdump;
     80 	FILE *dumploc = stdout;
     81 
     82 	setprogname(argv[0]);
     83 	while ((ch = getopt(argc, argv, "hp:")) != -1) {
     84 		switch (ch) {
     85 		case 'h':
     86 			hflag = true;
     87 			break;
     88 		case 'p':
     89 			pcapfile = optarg;
     90 			break;
     91 		default:
     92 			usage();
     93 		}
     94 	}
     95 
     96 	argc -= optind;
     97 	argv += optind;
     98 
     99 	if (argc != 1)
    100 		usage();
    101 
    102 	buf = malloc(BUFSIZE);
    103 	if (buf == NULL)
    104 		err(1, "malloc");
    105 
    106 	fd = open(argv[0], O_RDONLY);
    107 	if (fd == -1)
    108 		err(1, "open bus");
    109 
    110 	if (fstat(fd, &sb) == -1)
    111 		err(1, "stat");
    112 
    113 	busmem = mmap(NULL, sb.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0);
    114 	if (busmem == MAP_FAILED)
    115 		err(1, "mmap");
    116 	bmem = busmem;
    117 
    118 	if (bmem->shm_magic != SHMIF_MAGIC) {
    119 		if (bmem->shm_magic != bswap32(SHMIF_MAGIC))
    120 			errx(1, "%s not a shmif bus", argv[0]);
    121 		doswap = 1;
    122 	}
    123 	if (SWAPME(bmem->shm_version) != SHMIF_VERSION)
    124 		errx(1, "bus vesrsion %d, program %d",
    125 		    SWAPME(bmem->shm_version), SHMIF_VERSION);
    126 
    127 	if (pcapfile && strcmp(pcapfile, "-") == 0)
    128 		dumploc = stderr;
    129 
    130 	fprintf(dumploc, "bus version %d, lock: %d, generation: %" PRIu64
    131 	    ", firstoff: 0x%04x, lastoff: 0x%04x\n",
    132 	    SWAPME(bmem->shm_version), SWAPME(bmem->shm_lock),
    133 	    SWAPME64(bmem->shm_gen),
    134 	    SWAPME(bmem->shm_first), SWAPME(bmem->shm_last));
    135 
    136 	if (hflag)
    137 		exit(0);
    138 
    139 	if (pcapfile) {
    140 		pcap_t *pcap = pcap_open_dead(DLT_EN10MB, 1518);
    141 		pdump = pcap_dump_open(pcap, pcapfile);
    142 		if (pdump == NULL)
    143 			err(1, "cannot open pcap dump file");
    144 	} else {
    145 		/* XXXgcc */
    146 		pdump = NULL;
    147 	}
    148 
    149 	curbus = SWAPME(bmem->shm_first);
    150 	buslast = SWAPME(bmem->shm_last);
    151 	if (curbus == BUSMEM_DATASIZE)
    152 		curbus = 0;
    153 
    154 	bonus = 0;
    155 	if (buslast < curbus)
    156 		bonus = 1;
    157 
    158 	i = 0;
    159 	while (curbus <= buslast || bonus) {
    160 		struct pcap_pkthdr packhdr;
    161 		struct shmif_pkthdr sp;
    162 		uint32_t oldoff;
    163 		uint32_t curlen;
    164 		bool wrap;
    165 
    166 		assert(curbus < sb.st_size);
    167 
    168 		wrap = false;
    169 		oldoff = curbus;
    170 		curbus = shmif_busread(bmem, &sp, oldoff, sizeof(sp), &wrap);
    171 		if (wrap)
    172 			bonus = 0;
    173 
    174 		assert(curbus < sb.st_size);
    175 		curlen = SWAPME(sp.sp_len);
    176 
    177 		if (curlen == 0) {
    178 			continue;
    179 		}
    180 
    181 		fprintf(dumploc, "packet %d, offset 0x%04x, length 0x%04x, "
    182 			    "ts %d/%06d\n", i++, curbus,
    183 			    curlen, SWAPME(sp.sp_sec), SWAPME(sp.sp_usec));
    184 
    185 		if (!pcapfile) {
    186 			curbus = shmif_busread(bmem,
    187 			    buf, curbus, curlen, &wrap);
    188 			if (wrap)
    189 				bonus = 0;
    190 			continue;
    191 		}
    192 
    193 		memset(&packhdr, 0, sizeof(packhdr));
    194 		packhdr.caplen = packhdr.len = curlen;
    195 		packhdr.ts.tv_sec = SWAPME(sp.sp_sec);
    196 		packhdr.ts.tv_usec = SWAPME(sp.sp_usec);
    197 		assert(curlen <= BUFSIZE);
    198 
    199 		curbus = shmif_busread(bmem, buf, curbus, curlen, &wrap);
    200 		pcap_dump((u_char *)pdump, &packhdr, (u_char *)buf);
    201 		if (wrap)
    202 			bonus = 0;
    203 	}
    204 
    205 	if (pcapfile)
    206 		pcap_dump_close(pdump);
    207 
    208 	return 0;
    209 }
    210