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