]> git.proxmox.com Git - mirror_qemu.git/blame - net/dump.c
linux-user: enable sigaltstack for all architectures
[mirror_qemu.git] / net / dump.c
CommitLineData
1abecf77
MM
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
a245fc18 25#include "clients.h"
1abecf77 26#include "qemu-common.h"
1de7afc9 27#include "qemu/error-report.h"
43192fcc 28#include "qemu/iov.h"
1de7afc9
PB
29#include "qemu/log.h"
30#include "qemu/timer.h"
9d3e12e8
TH
31#include "qapi/visitor.h"
32#include "net/filter.h"
1abecf77
MM
33
34typedef struct DumpState {
0fa29915 35 int64_t start_ts;
1abecf77
MM
36 int fd;
37 int pcap_caplen;
38} DumpState;
39
40#define PCAP_MAGIC 0xa1b2c3d4
41
42struct pcap_file_hdr {
43 uint32_t magic;
44 uint16_t version_major;
45 uint16_t version_minor;
46 int32_t thiszone;
47 uint32_t sigfigs;
48 uint32_t snaplen;
49 uint32_t linktype;
50};
51
52struct pcap_sf_pkthdr {
53 struct {
54 int32_t tv_sec;
55 int32_t tv_usec;
56 } ts;
57 uint32_t caplen;
58 uint32_t len;
59};
60
75310e34 61static ssize_t dump_receive_iov(DumpState *s, const struct iovec *iov, int cnt)
1abecf77 62{
1abecf77
MM
63 struct pcap_sf_pkthdr hdr;
64 int64_t ts;
65 int caplen;
43192fcc
TH
66 size_t size = iov_size(iov, cnt);
67 struct iovec dumpiov[cnt + 1];
1abecf77
MM
68
69 /* Early return in case of previous error. */
70 if (s->fd < 0) {
71 return size;
72 }
73
ab60b748 74 ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL);
1abecf77
MM
75 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
76
0fa29915 77 hdr.ts.tv_sec = ts / 1000000 + s->start_ts;
1abecf77
MM
78 hdr.ts.tv_usec = ts % 1000000;
79 hdr.caplen = caplen;
80 hdr.len = size;
43192fcc
TH
81
82 dumpiov[0].iov_base = &hdr;
83 dumpiov[0].iov_len = sizeof(hdr);
84 cnt = iov_copy(&dumpiov[1], cnt, iov, cnt, 0, caplen);
85
86 if (writev(s->fd, dumpiov, cnt + 1) != sizeof(hdr) + caplen) {
1abecf77
MM
87 qemu_log("-net dump write error - stop dump\n");
88 close(s->fd);
89 s->fd = -1;
90 }
91
92 return size;
93}
94
75310e34 95static void dump_cleanup(DumpState *s)
1abecf77 96{
1abecf77 97 close(s->fd);
75310e34 98 s->fd = -1;
1abecf77
MM
99}
100
7bc3074c
TH
101static int net_dump_state_init(DumpState *s, const char *filename,
102 int len, Error **errp)
1abecf77
MM
103{
104 struct pcap_file_hdr hdr;
0fa29915 105 struct tm tm;
731d5856 106 int fd;
1abecf77 107
6514ed52 108 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
731d5856 109 if (fd < 0) {
3791f83c 110 error_setg_errno(errp, errno, "-net dump: can't open %s", filename);
1abecf77
MM
111 return -1;
112 }
113
1abecf77
MM
114 hdr.magic = PCAP_MAGIC;
115 hdr.version_major = 2;
116 hdr.version_minor = 4;
117 hdr.thiszone = 0;
118 hdr.sigfigs = 0;
731d5856 119 hdr.snaplen = len;
1abecf77
MM
120 hdr.linktype = 1;
121
731d5856 122 if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
3791f83c 123 error_setg_errno(errp, errno, "-net dump write error");
731d5856 124 close(fd);
1abecf77
MM
125 return -1;
126 }
127
731d5856
MM
128 s->fd = fd;
129 s->pcap_caplen = len;
130
0fa29915
HP
131 qemu_get_timedate(&tm, 0);
132 s->start_ts = mktime(&tm);
133
1abecf77
MM
134 return 0;
135}
136
75310e34
TH
137/* Dumping via VLAN netclient */
138
139struct DumpNetClient {
140 NetClientState nc;
141 DumpState ds;
142};
143typedef struct DumpNetClient DumpNetClient;
144
145static ssize_t dumpclient_receive(NetClientState *nc, const uint8_t *buf,
146 size_t size)
147{
148 DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc);
149 struct iovec iov = {
150 .iov_base = (void *)buf,
151 .iov_len = size
152 };
153
154 return dump_receive_iov(&dc->ds, &iov, 1);
155}
156
157static ssize_t dumpclient_receive_iov(NetClientState *nc,
158 const struct iovec *iov, int cnt)
159{
160 DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc);
161
162 return dump_receive_iov(&dc->ds, iov, cnt);
163}
164
165static void dumpclient_cleanup(NetClientState *nc)
166{
167 DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc);
168
169 dump_cleanup(&dc->ds);
170}
171
172static NetClientInfo net_dump_info = {
173 .type = NET_CLIENT_OPTIONS_KIND_DUMP,
174 .size = sizeof(DumpNetClient),
175 .receive = dumpclient_receive,
176 .receive_iov = dumpclient_receive_iov,
177 .cleanup = dumpclient_cleanup,
178};
179
1a0c0958 180int net_init_dump(const NetClientOptions *opts, const char *name,
a30ecde6 181 NetClientState *peer, Error **errp)
1abecf77 182{
7bc3074c 183 int len, rc;
1abecf77
MM
184 const char *file;
185 char def_file[128];
848040d1 186 const NetdevDumpOptions *dump;
7bc3074c 187 NetClientState *nc;
75310e34 188 DumpNetClient *dnc;
848040d1 189
8d0bcba8
EB
190 assert(opts->type == NET_CLIENT_OPTIONS_KIND_DUMP);
191 dump = opts->u.dump;
1abecf77 192
d33d93b2 193 assert(peer);
1abecf77 194
848040d1
LE
195 if (dump->has_file) {
196 file = dump->file;
197 } else {
d33d93b2
SH
198 int id;
199 int ret;
200
201 ret = net_hub_id_for_client(peer, &id);
202 assert(ret == 0); /* peer must be on a hub */
203
204 snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", id);
1abecf77
MM
205 file = def_file;
206 }
207
848040d1
LE
208 if (dump->has_len) {
209 if (dump->len > INT_MAX) {
3791f83c 210 error_setg(errp, "invalid length: %"PRIu64, dump->len);
848040d1
LE
211 return -1;
212 }
213 len = dump->len;
214 } else {
215 len = 65536;
216 }
1abecf77 217
7bc3074c
TH
218 nc = qemu_new_net_client(&net_dump_info, peer, "dump", name);
219 snprintf(nc->info_str, sizeof(nc->info_str),
220 "dump to %s (len=%d)", file, len);
221
75310e34
TH
222 dnc = DO_UPCAST(DumpNetClient, nc, nc);
223 rc = net_dump_state_init(&dnc->ds, file, len, errp);
7bc3074c
TH
224 if (rc) {
225 qemu_del_net_client(nc);
226 }
227 return rc;
1abecf77 228}
9d3e12e8
TH
229
230/* Dumping via filter */
231
232#define TYPE_FILTER_DUMP "filter-dump"
233
234#define FILTER_DUMP(obj) \
235 OBJECT_CHECK(NetFilterDumpState, (obj), TYPE_FILTER_DUMP)
236
237struct NetFilterDumpState {
238 NetFilterState nfs;
239 DumpState ds;
240 char *filename;
241 uint32_t maxlen;
242};
243typedef struct NetFilterDumpState NetFilterDumpState;
244
245static ssize_t filter_dump_receive_iov(NetFilterState *nf, NetClientState *sndr,
246 unsigned flags, const struct iovec *iov,
247 int iovcnt, NetPacketSent *sent_cb)
248{
249 NetFilterDumpState *nfds = FILTER_DUMP(nf);
250
251 dump_receive_iov(&nfds->ds, iov, iovcnt);
252 return 0;
253}
254
255static void filter_dump_cleanup(NetFilterState *nf)
256{
257 NetFilterDumpState *nfds = FILTER_DUMP(nf);
258
259 dump_cleanup(&nfds->ds);
260}
261
262static void filter_dump_setup(NetFilterState *nf, Error **errp)
263{
264 NetFilterDumpState *nfds = FILTER_DUMP(nf);
265
266 if (!nfds->filename) {
267 error_setg(errp, "dump filter needs 'file' property set!");
268 return;
269 }
270
271 net_dump_state_init(&nfds->ds, nfds->filename, nfds->maxlen, errp);
272}
273
274static void filter_dump_get_maxlen(Object *obj, Visitor *v, void *opaque,
275 const char *name, Error **errp)
276{
277 NetFilterDumpState *nfds = FILTER_DUMP(obj);
278 uint32_t value = nfds->maxlen;
279
280 visit_type_uint32(v, &value, name, errp);
281}
282
283static void filter_dump_set_maxlen(Object *obj, Visitor *v, void *opaque,
284 const char *name, Error **errp)
285{
286 NetFilterDumpState *nfds = FILTER_DUMP(obj);
287 Error *local_err = NULL;
288 uint32_t value;
289
290 visit_type_uint32(v, &value, name, &local_err);
291 if (local_err) {
292 goto out;
293 }
294 if (value == 0) {
295 error_setg(&local_err, "Property '%s.%s' doesn't take value '%u'",
296 object_get_typename(obj), name, value);
297 goto out;
298 }
299 nfds->maxlen = value;
300
301out:
302 error_propagate(errp, local_err);
303}
304
305static char *file_dump_get_filename(Object *obj, Error **errp)
306{
307 NetFilterDumpState *nfds = FILTER_DUMP(obj);
308
309 return g_strdup(nfds->filename);
310}
311
312static void file_dump_set_filename(Object *obj, const char *value, Error **errp)
313{
314 NetFilterDumpState *nfds = FILTER_DUMP(obj);
315
316 g_free(nfds->filename);
317 nfds->filename = g_strdup(value);
318}
319
320static void filter_dump_instance_init(Object *obj)
321{
322 NetFilterDumpState *nfds = FILTER_DUMP(obj);
323
324 nfds->maxlen = 65536;
325
326 object_property_add(obj, "maxlen", "int", filter_dump_get_maxlen,
327 filter_dump_set_maxlen, NULL, NULL, NULL);
328 object_property_add_str(obj, "file", file_dump_get_filename,
329 file_dump_set_filename, NULL);
330}
331
332static void filter_dump_class_init(ObjectClass *oc, void *data)
333{
334 NetFilterClass *nfc = NETFILTER_CLASS(oc);
335
336 nfc->setup = filter_dump_setup;
337 nfc->cleanup = filter_dump_cleanup;
338 nfc->receive_iov = filter_dump_receive_iov;
339}
340
341static const TypeInfo filter_dump_info = {
342 .name = TYPE_FILTER_DUMP,
343 .parent = TYPE_NETFILTER,
344 .class_init = filter_dump_class_init,
345 .instance_init = filter_dump_instance_init,
346 .instance_size = sizeof(NetFilterDumpState),
347};
348
349static void filter_dump_register_types(void)
350{
351 type_register_static(&filter_dump_info);
352}
353
354type_init(filter_dump_register_types);