root / net / dump.c @ c0424934
History | View | Annotate | Download (4.3 kB)
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 "qemu-error.h" |
28 |
#include "qemu-log.h" |
29 |
#include "qemu-timer.h" |
30 |
|
31 |
typedef struct DumpState { |
32 |
VLANClientState nc; |
33 |
int64_t start_ts; |
34 |
int fd;
|
35 |
int pcap_caplen;
|
36 |
} DumpState; |
37 |
|
38 |
#define PCAP_MAGIC 0xa1b2c3d4 |
39 |
|
40 |
struct pcap_file_hdr {
|
41 |
uint32_t magic; |
42 |
uint16_t version_major; |
43 |
uint16_t version_minor; |
44 |
int32_t thiszone; |
45 |
uint32_t sigfigs; |
46 |
uint32_t snaplen; |
47 |
uint32_t linktype; |
48 |
}; |
49 |
|
50 |
struct pcap_sf_pkthdr {
|
51 |
struct {
|
52 |
int32_t tv_sec; |
53 |
int32_t tv_usec; |
54 |
} ts; |
55 |
uint32_t caplen; |
56 |
uint32_t len; |
57 |
}; |
58 |
|
59 |
static ssize_t dump_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
60 |
{ |
61 |
DumpState *s = DO_UPCAST(DumpState, nc, nc); |
62 |
struct pcap_sf_pkthdr hdr;
|
63 |
int64_t ts; |
64 |
int caplen;
|
65 |
|
66 |
/* Early return in case of previous error. */
|
67 |
if (s->fd < 0) { |
68 |
return size;
|
69 |
} |
70 |
|
71 |
ts = muldiv64(qemu_get_clock_ns(vm_clock), 1000000, get_ticks_per_sec());
|
72 |
caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
73 |
|
74 |
hdr.ts.tv_sec = ts / 1000000 + s->start_ts;
|
75 |
hdr.ts.tv_usec = ts % 1000000;
|
76 |
hdr.caplen = caplen; |
77 |
hdr.len = size; |
78 |
if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || |
79 |
write(s->fd, buf, caplen) != caplen) { |
80 |
qemu_log("-net dump write error - stop dump\n");
|
81 |
close(s->fd); |
82 |
s->fd = -1;
|
83 |
} |
84 |
|
85 |
return size;
|
86 |
} |
87 |
|
88 |
static void dump_cleanup(VLANClientState *nc) |
89 |
{ |
90 |
DumpState *s = DO_UPCAST(DumpState, nc, nc); |
91 |
|
92 |
close(s->fd); |
93 |
} |
94 |
|
95 |
static NetClientInfo net_dump_info = {
|
96 |
.type = NET_CLIENT_TYPE_DUMP, |
97 |
.size = sizeof(DumpState),
|
98 |
.receive = dump_receive, |
99 |
.cleanup = dump_cleanup, |
100 |
}; |
101 |
|
102 |
static int net_dump_init(VLANState *vlan, const char *device, |
103 |
const char *name, const char *filename, int len) |
104 |
{ |
105 |
struct pcap_file_hdr hdr;
|
106 |
VLANClientState *nc; |
107 |
DumpState *s; |
108 |
struct tm tm;
|
109 |
int fd;
|
110 |
|
111 |
fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
|
112 |
if (fd < 0) { |
113 |
error_report("-net dump: can't open %s", filename);
|
114 |
return -1; |
115 |
} |
116 |
|
117 |
hdr.magic = PCAP_MAGIC; |
118 |
hdr.version_major = 2;
|
119 |
hdr.version_minor = 4;
|
120 |
hdr.thiszone = 0;
|
121 |
hdr.sigfigs = 0;
|
122 |
hdr.snaplen = len; |
123 |
hdr.linktype = 1;
|
124 |
|
125 |
if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
126 |
error_report("-net dump write error: %s", strerror(errno));
|
127 |
close(fd); |
128 |
return -1; |
129 |
} |
130 |
|
131 |
nc = qemu_new_net_client(&net_dump_info, vlan, NULL, device, name);
|
132 |
|
133 |
snprintf(nc->info_str, sizeof(nc->info_str),
|
134 |
"dump to %s (len=%d)", filename, len);
|
135 |
|
136 |
s = DO_UPCAST(DumpState, nc, nc); |
137 |
|
138 |
s->fd = fd; |
139 |
s->pcap_caplen = len; |
140 |
|
141 |
qemu_get_timedate(&tm, 0);
|
142 |
s->start_ts = mktime(&tm); |
143 |
|
144 |
return 0; |
145 |
} |
146 |
|
147 |
int net_init_dump(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan) |
148 |
{ |
149 |
int len;
|
150 |
const char *file; |
151 |
char def_file[128]; |
152 |
|
153 |
assert(vlan); |
154 |
|
155 |
file = qemu_opt_get(opts, "file");
|
156 |
if (!file) {
|
157 |
snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); |
158 |
file = def_file; |
159 |
} |
160 |
|
161 |
len = qemu_opt_get_size(opts, "len", 65536); |
162 |
|
163 |
return net_dump_init(vlan, "dump", name, file, len); |
164 |
} |