iotest: fix 169: do not run qmp_cont in RUN_STATE_FINISH_MIGRATE
[qemu/ar7.git] / net / dump.c
blobf16c3545e9deb4d089aeb51c23d956bdbcb443d8
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "clients.h"
27 #include "qapi/error.h"
28 #include "qemu-common.h"
29 #include "qemu/error-report.h"
30 #include "qemu/iov.h"
31 #include "qemu/log.h"
32 #include "qemu/timer.h"
33 #include "qapi/visitor.h"
34 #include "net/filter.h"
36 typedef struct DumpState {
37 int64_t start_ts;
38 int fd;
39 int pcap_caplen;
40 } DumpState;
42 #define PCAP_MAGIC 0xa1b2c3d4
44 struct pcap_file_hdr {
45 uint32_t magic;
46 uint16_t version_major;
47 uint16_t version_minor;
48 int32_t thiszone;
49 uint32_t sigfigs;
50 uint32_t snaplen;
51 uint32_t linktype;
54 struct pcap_sf_pkthdr {
55 struct {
56 int32_t tv_sec;
57 int32_t tv_usec;
58 } ts;
59 uint32_t caplen;
60 uint32_t len;
63 static ssize_t dump_receive_iov(DumpState *s, const struct iovec *iov, int cnt)
65 struct pcap_sf_pkthdr hdr;
66 int64_t ts;
67 int caplen;
68 size_t size = iov_size(iov, cnt);
69 struct iovec dumpiov[cnt + 1];
71 /* Early return in case of previous error. */
72 if (s->fd < 0) {
73 return size;
76 ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL);
77 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
79 hdr.ts.tv_sec = ts / 1000000 + s->start_ts;
80 hdr.ts.tv_usec = ts % 1000000;
81 hdr.caplen = caplen;
82 hdr.len = size;
84 dumpiov[0].iov_base = &hdr;
85 dumpiov[0].iov_len = sizeof(hdr);
86 cnt = iov_copy(&dumpiov[1], cnt, iov, cnt, 0, caplen);
88 if (writev(s->fd, dumpiov, cnt + 1) != sizeof(hdr) + caplen) {
89 error_report("network dump write error - stopping dump");
90 close(s->fd);
91 s->fd = -1;
94 return size;
97 static void dump_cleanup(DumpState *s)
99 close(s->fd);
100 s->fd = -1;
103 static int net_dump_state_init(DumpState *s, const char *filename,
104 int len, Error **errp)
106 struct pcap_file_hdr hdr;
107 struct tm tm;
108 int fd;
110 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
111 if (fd < 0) {
112 error_setg_errno(errp, errno, "net dump: can't open %s", filename);
113 return -1;
116 hdr.magic = PCAP_MAGIC;
117 hdr.version_major = 2;
118 hdr.version_minor = 4;
119 hdr.thiszone = 0;
120 hdr.sigfigs = 0;
121 hdr.snaplen = len;
122 hdr.linktype = 1;
124 if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
125 error_setg_errno(errp, errno, "net dump write error");
126 close(fd);
127 return -1;
130 s->fd = fd;
131 s->pcap_caplen = len;
133 qemu_get_timedate(&tm, 0);
134 s->start_ts = mktime(&tm);
136 return 0;
139 #define TYPE_FILTER_DUMP "filter-dump"
141 #define FILTER_DUMP(obj) \
142 OBJECT_CHECK(NetFilterDumpState, (obj), TYPE_FILTER_DUMP)
144 struct NetFilterDumpState {
145 NetFilterState nfs;
146 DumpState ds;
147 char *filename;
148 uint32_t maxlen;
150 typedef struct NetFilterDumpState NetFilterDumpState;
152 static ssize_t filter_dump_receive_iov(NetFilterState *nf, NetClientState *sndr,
153 unsigned flags, const struct iovec *iov,
154 int iovcnt, NetPacketSent *sent_cb)
156 NetFilterDumpState *nfds = FILTER_DUMP(nf);
158 dump_receive_iov(&nfds->ds, iov, iovcnt);
159 return 0;
162 static void filter_dump_cleanup(NetFilterState *nf)
164 NetFilterDumpState *nfds = FILTER_DUMP(nf);
166 dump_cleanup(&nfds->ds);
169 static void filter_dump_setup(NetFilterState *nf, Error **errp)
171 NetFilterDumpState *nfds = FILTER_DUMP(nf);
173 if (!nfds->filename) {
174 error_setg(errp, "dump filter needs 'file' property set!");
175 return;
178 net_dump_state_init(&nfds->ds, nfds->filename, nfds->maxlen, errp);
181 static void filter_dump_get_maxlen(Object *obj, Visitor *v, const char *name,
182 void *opaque, Error **errp)
184 NetFilterDumpState *nfds = FILTER_DUMP(obj);
185 uint32_t value = nfds->maxlen;
187 visit_type_uint32(v, name, &value, errp);
190 static void filter_dump_set_maxlen(Object *obj, Visitor *v, const char *name,
191 void *opaque, Error **errp)
193 NetFilterDumpState *nfds = FILTER_DUMP(obj);
194 Error *local_err = NULL;
195 uint32_t value;
197 visit_type_uint32(v, name, &value, &local_err);
198 if (local_err) {
199 goto out;
201 if (value == 0) {
202 error_setg(&local_err, "Property '%s.%s' doesn't take value '%u'",
203 object_get_typename(obj), name, value);
204 goto out;
206 nfds->maxlen = value;
208 out:
209 error_propagate(errp, local_err);
212 static char *file_dump_get_filename(Object *obj, Error **errp)
214 NetFilterDumpState *nfds = FILTER_DUMP(obj);
216 return g_strdup(nfds->filename);
219 static void file_dump_set_filename(Object *obj, const char *value, Error **errp)
221 NetFilterDumpState *nfds = FILTER_DUMP(obj);
223 g_free(nfds->filename);
224 nfds->filename = g_strdup(value);
227 static void filter_dump_instance_init(Object *obj)
229 NetFilterDumpState *nfds = FILTER_DUMP(obj);
231 nfds->maxlen = 65536;
233 object_property_add(obj, "maxlen", "uint32", filter_dump_get_maxlen,
234 filter_dump_set_maxlen, NULL, NULL, NULL);
235 object_property_add_str(obj, "file", file_dump_get_filename,
236 file_dump_set_filename, NULL);
239 static void filter_dump_instance_finalize(Object *obj)
241 NetFilterDumpState *nfds = FILTER_DUMP(obj);
243 g_free(nfds->filename);
246 static void filter_dump_class_init(ObjectClass *oc, void *data)
248 NetFilterClass *nfc = NETFILTER_CLASS(oc);
250 nfc->setup = filter_dump_setup;
251 nfc->cleanup = filter_dump_cleanup;
252 nfc->receive_iov = filter_dump_receive_iov;
255 static const TypeInfo filter_dump_info = {
256 .name = TYPE_FILTER_DUMP,
257 .parent = TYPE_NETFILTER,
258 .class_init = filter_dump_class_init,
259 .instance_init = filter_dump_instance_init,
260 .instance_finalize = filter_dump_instance_finalize,
261 .instance_size = sizeof(NetFilterDumpState),
264 static void filter_dump_register_types(void)
266 type_register_static(&filter_dump_info);
269 type_init(filter_dump_register_types);