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