blob: d50b4eeac4f2f9773e3aec59b4d15785b1519c0e [file] [log] [blame]
Mark McLoughlin1abecf72009-11-25 18:48:57 +00001/*
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"
28#include "qemu-log.h"
29
30typedef struct DumpState {
Mark McLoughlin731d5852009-11-25 18:49:09 +000031 VLANClientState nc;
Mark McLoughlin1abecf72009-11-25 18:48:57 +000032 int fd;
33 int pcap_caplen;
34} DumpState;
35
36#define PCAP_MAGIC 0xa1b2c3d4
37
38struct pcap_file_hdr {
39 uint32_t magic;
40 uint16_t version_major;
41 uint16_t version_minor;
42 int32_t thiszone;
43 uint32_t sigfigs;
44 uint32_t snaplen;
45 uint32_t linktype;
46};
47
48struct pcap_sf_pkthdr {
49 struct {
50 int32_t tv_sec;
51 int32_t tv_usec;
52 } ts;
53 uint32_t caplen;
54 uint32_t len;
55};
56
Mark McLoughlin731d5852009-11-25 18:49:09 +000057static ssize_t dump_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin1abecf72009-11-25 18:48:57 +000058{
Mark McLoughlin731d5852009-11-25 18:49:09 +000059 DumpState *s = DO_UPCAST(DumpState, nc, nc);
Mark McLoughlin1abecf72009-11-25 18:48:57 +000060 struct pcap_sf_pkthdr hdr;
61 int64_t ts;
62 int caplen;
63
64 /* Early return in case of previous error. */
65 if (s->fd < 0) {
66 return size;
67 }
68
69 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec());
70 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
71
72 hdr.ts.tv_sec = ts / 1000000;
73 hdr.ts.tv_usec = ts % 1000000;
74 hdr.caplen = caplen;
75 hdr.len = size;
76 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
77 write(s->fd, buf, caplen) != caplen) {
78 qemu_log("-net dump write error - stop dump\n");
79 close(s->fd);
80 s->fd = -1;
81 }
82
83 return size;
84}
85
Mark McLoughlin731d5852009-11-25 18:49:09 +000086static void dump_cleanup(VLANClientState *nc)
Mark McLoughlin1abecf72009-11-25 18:48:57 +000087{
Mark McLoughlin731d5852009-11-25 18:49:09 +000088 DumpState *s = DO_UPCAST(DumpState, nc, nc);
Mark McLoughlin1abecf72009-11-25 18:48:57 +000089
90 close(s->fd);
Mark McLoughlin1abecf72009-11-25 18:48:57 +000091}
92
Mark McLoughlin731d5852009-11-25 18:49:09 +000093static NetClientInfo net_dump_info = {
94 .type = NET_CLIENT_TYPE_DUMP,
95 .size = sizeof(DumpState),
96 .receive = dump_receive,
97 .cleanup = dump_cleanup,
98};
99
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000100static int net_dump_init(VLANState *vlan, const char *device,
101 const char *name, const char *filename, int len)
102{
103 struct pcap_file_hdr hdr;
Mark McLoughlin731d5852009-11-25 18:49:09 +0000104 VLANClientState *nc;
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000105 DumpState *s;
Mark McLoughlin731d5852009-11-25 18:49:09 +0000106 int fd;
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000107
Mark McLoughlin731d5852009-11-25 18:49:09 +0000108 fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
109 if (fd < 0) {
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000110 qemu_error("-net dump: can't open %s\n", filename);
111 return -1;
112 }
113
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000114 hdr.magic = PCAP_MAGIC;
115 hdr.version_major = 2;
116 hdr.version_minor = 4;
117 hdr.thiszone = 0;
118 hdr.sigfigs = 0;
Mark McLoughlin731d5852009-11-25 18:49:09 +0000119 hdr.snaplen = len;
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000120 hdr.linktype = 1;
121
Mark McLoughlin731d5852009-11-25 18:49:09 +0000122 if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000123 qemu_error("-net dump write error: %s\n", strerror(errno));
Mark McLoughlin731d5852009-11-25 18:49:09 +0000124 close(fd);
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000125 return -1;
126 }
127
Mark McLoughlin731d5852009-11-25 18:49:09 +0000128 nc = qemu_new_net_client(&net_dump_info, vlan, NULL, device, name);
129
130 snprintf(nc->info_str, sizeof(nc->info_str),
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000131 "dump to %s (len=%d)", filename, len);
Mark McLoughlin731d5852009-11-25 18:49:09 +0000132
133 s = DO_UPCAST(DumpState, nc, nc);
134
135 s->fd = fd;
136 s->pcap_caplen = len;
137
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000138 return 0;
139}
140
141int net_init_dump(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
142{
143 int len;
144 const char *file;
145 char def_file[128];
146
147 assert(vlan);
148
149 file = qemu_opt_get(opts, "file");
150 if (!file) {
151 snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id);
152 file = def_file;
153 }
154
155 len = qemu_opt_get_size(opts, "len", 65536);
156
157 return net_dump_init(vlan, "dump", name, file, len);
158}