Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "dump.h" |
| 26 | #include "qemu-common.h" |
| 27 | #include "sysemu.h" |
Markus Armbruster | 2f79201 | 2010-02-18 16:24:31 +0100 | [diff] [blame] | 28 | #include "qemu-error.h" |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 29 | #include "qemu-log.h" |
| 30 | |
| 31 | typedef struct DumpState { |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 32 | VLANClientState nc; |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 33 | int fd; |
| 34 | int pcap_caplen; |
| 35 | } DumpState; |
| 36 | |
| 37 | #define PCAP_MAGIC 0xa1b2c3d4 |
| 38 | |
| 39 | struct pcap_file_hdr { |
| 40 | uint32_t magic; |
| 41 | uint16_t version_major; |
| 42 | uint16_t version_minor; |
| 43 | int32_t thiszone; |
| 44 | uint32_t sigfigs; |
| 45 | uint32_t snaplen; |
| 46 | uint32_t linktype; |
| 47 | }; |
| 48 | |
| 49 | struct pcap_sf_pkthdr { |
| 50 | struct { |
| 51 | int32_t tv_sec; |
| 52 | int32_t tv_usec; |
| 53 | } ts; |
| 54 | uint32_t caplen; |
| 55 | uint32_t len; |
| 56 | }; |
| 57 | |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 58 | static ssize_t dump_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 59 | { |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 60 | DumpState *s = DO_UPCAST(DumpState, nc, nc); |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 61 | struct pcap_sf_pkthdr hdr; |
| 62 | int64_t ts; |
| 63 | int caplen; |
| 64 | |
| 65 | /* Early return in case of previous error. */ |
| 66 | if (s->fd < 0) { |
| 67 | return size; |
| 68 | } |
| 69 | |
| 70 | ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec()); |
| 71 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
| 72 | |
| 73 | hdr.ts.tv_sec = ts / 1000000; |
| 74 | hdr.ts.tv_usec = ts % 1000000; |
| 75 | hdr.caplen = caplen; |
| 76 | hdr.len = size; |
| 77 | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || |
| 78 | write(s->fd, buf, caplen) != caplen) { |
| 79 | qemu_log("-net dump write error - stop dump\n"); |
| 80 | close(s->fd); |
| 81 | s->fd = -1; |
| 82 | } |
| 83 | |
| 84 | return size; |
| 85 | } |
| 86 | |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 87 | static void dump_cleanup(VLANClientState *nc) |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 88 | { |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 89 | DumpState *s = DO_UPCAST(DumpState, nc, nc); |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 90 | |
| 91 | close(s->fd); |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 94 | static NetClientInfo net_dump_info = { |
| 95 | .type = NET_CLIENT_TYPE_DUMP, |
| 96 | .size = sizeof(DumpState), |
| 97 | .receive = dump_receive, |
| 98 | .cleanup = dump_cleanup, |
| 99 | }; |
| 100 | |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 101 | static int net_dump_init(VLANState *vlan, const char *device, |
| 102 | const char *name, const char *filename, int len) |
| 103 | { |
| 104 | struct pcap_file_hdr hdr; |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 105 | VLANClientState *nc; |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 106 | DumpState *s; |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 107 | int fd; |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 108 | |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 109 | fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644); |
| 110 | if (fd < 0) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 111 | error_report("-net dump: can't open %s", filename); |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 112 | return -1; |
| 113 | } |
| 114 | |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 115 | hdr.magic = PCAP_MAGIC; |
| 116 | hdr.version_major = 2; |
| 117 | hdr.version_minor = 4; |
| 118 | hdr.thiszone = 0; |
| 119 | hdr.sigfigs = 0; |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 120 | hdr.snaplen = len; |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 121 | hdr.linktype = 1; |
| 122 | |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 123 | if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 124 | error_report("-net dump write error: %s", strerror(errno)); |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 125 | close(fd); |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 126 | return -1; |
| 127 | } |
| 128 | |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 129 | nc = qemu_new_net_client(&net_dump_info, vlan, NULL, device, name); |
| 130 | |
| 131 | snprintf(nc->info_str, sizeof(nc->info_str), |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 132 | "dump to %s (len=%d)", filename, len); |
Mark McLoughlin | 731d585 | 2009-11-25 18:49:09 +0000 | [diff] [blame] | 133 | |
| 134 | s = DO_UPCAST(DumpState, nc, nc); |
| 135 | |
| 136 | s->fd = fd; |
| 137 | s->pcap_caplen = len; |
| 138 | |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int net_init_dump(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan) |
| 143 | { |
| 144 | int len; |
| 145 | const char *file; |
| 146 | char def_file[128]; |
| 147 | |
| 148 | assert(vlan); |
| 149 | |
| 150 | file = qemu_opt_get(opts, "file"); |
| 151 | if (!file) { |
| 152 | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); |
| 153 | file = def_file; |
| 154 | } |
| 155 | |
| 156 | len = qemu_opt_get_size(opts, "len", 65536); |
| 157 | |
| 158 | return net_dump_init(vlan, "dump", name, file, len); |
| 159 | } |