1 1.19 christos /* $NetBSD: shmif_dumpbus.c,v 1.19 2020/04/01 21:04:34 christos 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.11 pooka #include <rump/rumpuser_port.h> 34 1.11 pooka 35 1.10 pooka #ifndef lint 36 1.19 christos __RCSID("$NetBSD: shmif_dumpbus.c,v 1.19 2020/04/01 21:04:34 christos Exp $"); 37 1.10 pooka #endif /* !lint */ 38 1.10 pooka 39 1.1 pooka #include <sys/types.h> 40 1.1 pooka #include <sys/mman.h> 41 1.1 pooka #include <sys/stat.h> 42 1.11 pooka #ifdef __NetBSD__ 43 1.11 pooka #include <sys/bswap.h> 44 1.11 pooka #endif 45 1.5 pooka 46 1.1 pooka #include <assert.h> 47 1.1 pooka #include <err.h> 48 1.1 pooka #include <fcntl.h> 49 1.1 pooka #include <inttypes.h> 50 1.1 pooka #include <pcap.h> 51 1.1 pooka #include <stdbool.h> 52 1.1 pooka #include <stdio.h> 53 1.1 pooka #include <stdlib.h> 54 1.1 pooka #include <string.h> 55 1.1 pooka #include <unistd.h> 56 1.1 pooka 57 1.1 pooka #include "shmifvar.h" 58 1.1 pooka 59 1.8 joerg __dead static void 60 1.1 pooka usage(void) 61 1.1 pooka { 62 1.1 pooka 63 1.18 pooka #ifndef HAVE_GETPROGNAME 64 1.11 pooka #define getprogname() "shmif_dumpbus" 65 1.11 pooka #endif 66 1.11 pooka 67 1.19 christos fprintf(stderr, "Usage: %s [-h] [-p pcapfile] buspath\n", 68 1.19 christos getprogname()); 69 1.19 christos exit(EXIT_FAILURE); 70 1.1 pooka } 71 1.1 pooka 72 1.1 pooka #define BUFSIZE 64*1024 73 1.12 pooka 74 1.12 pooka /* 75 1.12 pooka * byte swapdom 76 1.12 pooka */ 77 1.12 pooka static uint32_t 78 1.12 pooka swp32(uint32_t x) 79 1.12 pooka { 80 1.12 pooka uint32_t v; 81 1.12 pooka 82 1.12 pooka v = (((x) & 0xff000000) >> 24) | 83 1.12 pooka (((x) & 0x00ff0000) >> 8) | 84 1.12 pooka (((x) & 0x0000ff00) << 8) | 85 1.12 pooka (((x) & 0x000000ff) << 24); 86 1.12 pooka return v; 87 1.12 pooka } 88 1.12 pooka 89 1.12 pooka static uint64_t 90 1.12 pooka swp64(uint64_t x) 91 1.12 pooka { 92 1.12 pooka uint64_t v; 93 1.12 pooka 94 1.12 pooka v = (((x) & 0xff00000000000000ull) >> 56) | 95 1.12 pooka (((x) & 0x00ff000000000000ull) >> 40) | 96 1.12 pooka (((x) & 0x0000ff0000000000ull) >> 24) | 97 1.12 pooka (((x) & 0x000000ff00000000ull) >> 8) | 98 1.12 pooka (((x) & 0x00000000ff000000ull) << 8) | 99 1.12 pooka (((x) & 0x0000000000ff0000ull) << 24) | 100 1.12 pooka (((x) & 0x000000000000ff00ull) << 40) | 101 1.12 pooka (((x) & 0x00000000000000ffull) << 56); 102 1.12 pooka return v; 103 1.12 pooka } 104 1.12 pooka 105 1.19 christos #define FIXENDIAN32(x) (doswap ? swp32(x) : (uint32_t)(x)) 106 1.19 christos #define FIXENDIAN64(x) (doswap ? swp64(x) : (uint64_t)(x)) 107 1.12 pooka 108 1.15 pooka /* compat for bus version 2 */ 109 1.15 pooka struct shmif_pkthdr2 { 110 1.15 pooka uint32_t sp_len; 111 1.15 pooka 112 1.15 pooka uint32_t sp_sec; 113 1.15 pooka uint32_t sp_usec; 114 1.15 pooka }; 115 1.15 pooka 116 1.1 pooka int 117 1.1 pooka main(int argc, char *argv[]) 118 1.1 pooka { 119 1.1 pooka struct stat sb; 120 1.1 pooka void *busmem; 121 1.1 pooka const char *pcapfile = NULL; 122 1.1 pooka uint32_t curbus, buslast; 123 1.1 pooka struct shmif_mem *bmem; 124 1.5 pooka int fd, i, ch; 125 1.1 pooka int bonus; 126 1.1 pooka char *buf; 127 1.9 pooka bool hflag = false, doswap = false; 128 1.5 pooka pcap_dumper_t *pdump; 129 1.9 pooka FILE *dumploc = stdout; 130 1.19 christos uint32_t useversion; 131 1.1 pooka 132 1.1 pooka setprogname(argv[0]); 133 1.1 pooka while ((ch = getopt(argc, argv, "hp:")) != -1) { 134 1.1 pooka switch (ch) { 135 1.1 pooka case 'h': 136 1.1 pooka hflag = true; 137 1.1 pooka break; 138 1.1 pooka case 'p': 139 1.1 pooka pcapfile = optarg; 140 1.1 pooka break; 141 1.1 pooka default: 142 1.1 pooka usage(); 143 1.1 pooka } 144 1.1 pooka } 145 1.1 pooka 146 1.1 pooka argc -= optind; 147 1.1 pooka argv += optind; 148 1.1 pooka 149 1.1 pooka if (argc != 1) 150 1.1 pooka usage(); 151 1.1 pooka 152 1.1 pooka buf = malloc(BUFSIZE); 153 1.1 pooka if (buf == NULL) 154 1.19 christos err(EXIT_FAILURE, "malloc"); 155 1.1 pooka 156 1.1 pooka fd = open(argv[0], O_RDONLY); 157 1.1 pooka if (fd == -1) 158 1.19 christos err(EXIT_FAILURE, "Can't open bus file `%s'", argv[0]); 159 1.1 pooka 160 1.1 pooka if (fstat(fd, &sb) == -1) 161 1.19 christos err(EXIT_FAILURE, "Can't stat bus file `%s'", argv[0]); 162 1.1 pooka 163 1.19 christos busmem = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_FILE|MAP_SHARED, 164 1.19 christos fd, 0); 165 1.1 pooka if (busmem == MAP_FAILED) 166 1.19 christos err(EXIT_FAILURE, "mmap"); 167 1.1 pooka bmem = busmem; 168 1.1 pooka 169 1.5 pooka if (bmem->shm_magic != SHMIF_MAGIC) { 170 1.12 pooka if (bmem->shm_magic != swp32(SHMIF_MAGIC)) 171 1.19 christos errx(EXIT_FAILURE, "%s not a shmif bus: " 172 1.19 christos "bad magic %#x != %#x", argv[0], bmem->shm_magic, 173 1.19 christos SHMIF_MAGIC); 174 1.12 pooka doswap = true; 175 1.5 pooka } 176 1.19 christos useversion = FIXENDIAN32(bmem->shm_version); 177 1.19 christos switch (useversion) { 178 1.19 christos case 2: 179 1.19 christos case SHMIF_VERSION: 180 1.19 christos break; 181 1.19 christos default: 182 1.19 christos errx(EXIT_FAILURE, "Unhandled bus version %d, program %d", 183 1.19 christos useversion, SHMIF_VERSION); 184 1.15 pooka } 185 1.9 pooka 186 1.9 pooka if (pcapfile && strcmp(pcapfile, "-") == 0) 187 1.9 pooka dumploc = stderr; 188 1.9 pooka 189 1.9 pooka fprintf(dumploc, "bus version %d, lock: %d, generation: %" PRIu64 190 1.1 pooka ", firstoff: 0x%04x, lastoff: 0x%04x\n", 191 1.14 pooka FIXENDIAN32(bmem->shm_version), FIXENDIAN32(bmem->shm_lock), 192 1.14 pooka FIXENDIAN64(bmem->shm_gen), 193 1.14 pooka FIXENDIAN32(bmem->shm_first), FIXENDIAN32(bmem->shm_last)); 194 1.1 pooka 195 1.1 pooka if (hflag) 196 1.1 pooka exit(0); 197 1.1 pooka 198 1.1 pooka if (pcapfile) { 199 1.5 pooka pcap_t *pcap = pcap_open_dead(DLT_EN10MB, 1518); 200 1.5 pooka pdump = pcap_dump_open(pcap, pcapfile); 201 1.19 christos if (pdump == NULL) { 202 1.19 christos errx(EXIT_FAILURE, 203 1.19 christos "Cannot open pcap dump file `%s': %s", pcapfile, 204 1.19 christos pcap_geterr(pcap)); 205 1.19 christos } 206 1.1 pooka } else { 207 1.5 pooka /* XXXgcc */ 208 1.5 pooka pdump = NULL; 209 1.1 pooka } 210 1.7 pooka 211 1.14 pooka curbus = FIXENDIAN32(bmem->shm_first); 212 1.14 pooka buslast = FIXENDIAN32(bmem->shm_last); 213 1.1 pooka if (curbus == BUSMEM_DATASIZE) 214 1.1 pooka curbus = 0; 215 1.1 pooka 216 1.1 pooka bonus = 0; 217 1.1 pooka if (buslast < curbus) 218 1.1 pooka bonus = 1; 219 1.1 pooka 220 1.1 pooka i = 0; 221 1.15 pooka 222 1.1 pooka while (curbus <= buslast || bonus) { 223 1.5 pooka struct pcap_pkthdr packhdr; 224 1.1 pooka uint32_t oldoff; 225 1.5 pooka uint32_t curlen; 226 1.15 pooka uint32_t sp_sec, sp_usec, sp_len; 227 1.1 pooka bool wrap; 228 1.1 pooka 229 1.3 pooka assert(curbus < sb.st_size); 230 1.3 pooka 231 1.1 pooka wrap = false; 232 1.1 pooka oldoff = curbus; 233 1.15 pooka 234 1.15 pooka if (useversion == 3) { 235 1.16 pooka struct shmif_pkthdr sp; 236 1.16 pooka 237 1.15 pooka curbus = shmif_busread(bmem, 238 1.15 pooka &sp, oldoff, sizeof(sp), &wrap); 239 1.17 pooka sp_len = FIXENDIAN32(sp.sp_len); 240 1.17 pooka sp_sec = FIXENDIAN32(sp.sp_sec); 241 1.17 pooka sp_usec = FIXENDIAN32(sp.sp_usec); 242 1.15 pooka } else { 243 1.16 pooka struct shmif_pkthdr2 sp2; 244 1.16 pooka 245 1.15 pooka curbus = shmif_busread(bmem, 246 1.15 pooka &sp2, oldoff, sizeof(sp2), &wrap); 247 1.17 pooka sp_len = FIXENDIAN32(sp2.sp_len); 248 1.17 pooka sp_sec = FIXENDIAN32(sp2.sp_sec); 249 1.17 pooka sp_usec = FIXENDIAN32(sp2.sp_usec); 250 1.15 pooka } 251 1.1 pooka if (wrap) 252 1.1 pooka bonus = 0; 253 1.1 pooka 254 1.3 pooka assert(curbus < sb.st_size); 255 1.17 pooka curlen = sp_len; 256 1.3 pooka 257 1.5 pooka if (curlen == 0) { 258 1.1 pooka continue; 259 1.3 pooka } 260 1.1 pooka 261 1.9 pooka fprintf(dumploc, "packet %d, offset 0x%04x, length 0x%04x, " 262 1.19 christos "ts %d/%06d\n", i++, curbus, curlen, 263 1.19 christos sp_sec, sp_usec); 264 1.1 pooka 265 1.3 pooka if (!pcapfile) { 266 1.1 pooka curbus = shmif_busread(bmem, 267 1.5 pooka buf, curbus, curlen, &wrap); 268 1.1 pooka if (wrap) 269 1.1 pooka bonus = 0; 270 1.1 pooka continue; 271 1.1 pooka } 272 1.1 pooka 273 1.1 pooka memset(&packhdr, 0, sizeof(packhdr)); 274 1.5 pooka packhdr.caplen = packhdr.len = curlen; 275 1.17 pooka packhdr.ts.tv_sec = sp_sec; 276 1.19 christos packhdr.ts.tv_usec = (suseconds_t)sp_usec; 277 1.5 pooka assert(curlen <= BUFSIZE); 278 1.5 pooka 279 1.5 pooka curbus = shmif_busread(bmem, buf, curbus, curlen, &wrap); 280 1.5 pooka pcap_dump((u_char *)pdump, &packhdr, (u_char *)buf); 281 1.1 pooka if (wrap) 282 1.1 pooka bonus = 0; 283 1.1 pooka } 284 1.1 pooka 285 1.5 pooka if (pcapfile) 286 1.5 pooka pcap_dump_close(pdump); 287 1.5 pooka 288 1.19 christos return EXIT_SUCCESS; 289 1.1 pooka } 290