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