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