target-ppc: Convert mmu-hash{32,64}.[ch] from CPUPPCState to PowerPCCPU
[qemu/rayw.git] / net / dump.c
blob88d9582334babab05abb92f05909e6da2bb8ca0d
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 "clients.h"
26 #include "qemu-common.h"
27 #include "qemu/error-report.h"
28 #include "qemu/iov.h"
29 #include "qemu/log.h"
30 #include "qemu/timer.h"
31 #include "qapi/visitor.h"
32 #include "net/filter.h"
34 typedef struct DumpState {
35 int64_t start_ts;
36 int fd;
37 int pcap_caplen;
38 } DumpState;
40 #define PCAP_MAGIC 0xa1b2c3d4
42 struct 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;
52 struct 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;
61 static ssize_t dump_receive_iov(DumpState *s, const struct iovec *iov, int cnt)
63 struct pcap_sf_pkthdr hdr;
64 int64_t ts;
65 int caplen;
66 size_t size = iov_size(iov, cnt);
67 struct iovec dumpiov[cnt + 1];
69 /* Early return in case of previous error. */
70 if (s->fd < 0) {
71 return size;
74 ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL);
75 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
77 hdr.ts.tv_sec = ts / 1000000 + s->start_ts;
78 hdr.ts.tv_usec = ts % 1000000;
79 hdr.caplen = caplen;
80 hdr.len = size;
82 dumpiov[0].iov_base = &hdr;
83 dumpiov[0].iov_len = sizeof(hdr);
84 cnt = iov_copy(&dumpiov[1], cnt, iov, cnt, 0, caplen);
86 if (writev(s->fd, dumpiov, cnt + 1) != sizeof(hdr) + caplen) {
87 error_report("network dump write error - stopping dump");
88 close(s->fd);
89 s->fd = -1;
92 return size;
95 static void dump_cleanup(DumpState *s)
97 close(s->fd);
98 s->fd = -1;
101 static int net_dump_state_init(DumpState *s, const char *filename,
102 int len, Error **errp)
104 struct pcap_file_hdr hdr;
105 struct tm tm;
106 int fd;
108 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
109 if (fd < 0) {
110 error_setg_errno(errp, errno, "-net dump: can't open %s", filename);
111 return -1;
114 hdr.magic = PCAP_MAGIC;
115 hdr.version_major = 2;
116 hdr.version_minor = 4;
117 hdr.thiszone = 0;
118 hdr.sigfigs = 0;
119 hdr.snaplen = len;
120 hdr.linktype = 1;
122 if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
123 error_setg_errno(errp, errno, "-net dump write error");
124 close(fd);
125 return -1;
128 s->fd = fd;
129 s->pcap_caplen = len;
131 qemu_get_timedate(&tm, 0);
132 s->start_ts = mktime(&tm);
134 return 0;
137 /* Dumping via VLAN netclient */
139 struct DumpNetClient {
140 NetClientState nc;
141 DumpState ds;
143 typedef struct DumpNetClient DumpNetClient;
145 static ssize_t dumpclient_receive(NetClientState *nc, const uint8_t *buf,
146 size_t size)
148 DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc);
149 struct iovec iov = {
150 .iov_base = (void *)buf,
151 .iov_len = size
154 return dump_receive_iov(&dc->ds, &iov, 1);
157 static ssize_t dumpclient_receive_iov(NetClientState *nc,
158 const struct iovec *iov, int cnt)
160 DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc);
162 return dump_receive_iov(&dc->ds, iov, cnt);
165 static void dumpclient_cleanup(NetClientState *nc)
167 DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc);
169 dump_cleanup(&dc->ds);
172 static 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,
180 int net_init_dump(const NetClientOptions *opts, const char *name,
181 NetClientState *peer, Error **errp)
183 int len, rc;
184 const char *file;
185 char def_file[128];
186 const NetdevDumpOptions *dump;
187 NetClientState *nc;
188 DumpNetClient *dnc;
190 assert(opts->type == NET_CLIENT_OPTIONS_KIND_DUMP);
191 dump = opts->u.dump;
193 assert(peer);
195 if (dump->has_file) {
196 file = dump->file;
197 } else {
198 int id;
199 int ret;
201 ret = net_hub_id_for_client(peer, &id);
202 assert(ret == 0); /* peer must be on a hub */
204 snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", id);
205 file = def_file;
208 if (dump->has_len) {
209 if (dump->len > INT_MAX) {
210 error_setg(errp, "invalid length: %"PRIu64, dump->len);
211 return -1;
213 len = dump->len;
214 } else {
215 len = 65536;
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);
222 dnc = DO_UPCAST(DumpNetClient, nc, nc);
223 rc = net_dump_state_init(&dnc->ds, file, len, errp);
224 if (rc) {
225 qemu_del_net_client(nc);
227 return rc;
230 /* Dumping via filter */
232 #define TYPE_FILTER_DUMP "filter-dump"
234 #define FILTER_DUMP(obj) \
235 OBJECT_CHECK(NetFilterDumpState, (obj), TYPE_FILTER_DUMP)
237 struct NetFilterDumpState {
238 NetFilterState nfs;
239 DumpState ds;
240 char *filename;
241 uint32_t maxlen;
243 typedef struct NetFilterDumpState NetFilterDumpState;
245 static ssize_t filter_dump_receive_iov(NetFilterState *nf, NetClientState *sndr,
246 unsigned flags, const struct iovec *iov,
247 int iovcnt, NetPacketSent *sent_cb)
249 NetFilterDumpState *nfds = FILTER_DUMP(nf);
251 dump_receive_iov(&nfds->ds, iov, iovcnt);
252 return 0;
255 static void filter_dump_cleanup(NetFilterState *nf)
257 NetFilterDumpState *nfds = FILTER_DUMP(nf);
259 dump_cleanup(&nfds->ds);
262 static void filter_dump_setup(NetFilterState *nf, Error **errp)
264 NetFilterDumpState *nfds = FILTER_DUMP(nf);
266 if (!nfds->filename) {
267 error_setg(errp, "dump filter needs 'file' property set!");
268 return;
271 net_dump_state_init(&nfds->ds, nfds->filename, nfds->maxlen, errp);
274 static void filter_dump_get_maxlen(Object *obj, Visitor *v, void *opaque,
275 const char *name, Error **errp)
277 NetFilterDumpState *nfds = FILTER_DUMP(obj);
278 uint32_t value = nfds->maxlen;
280 visit_type_uint32(v, &value, name, errp);
283 static void filter_dump_set_maxlen(Object *obj, Visitor *v, void *opaque,
284 const char *name, Error **errp)
286 NetFilterDumpState *nfds = FILTER_DUMP(obj);
287 Error *local_err = NULL;
288 uint32_t value;
290 visit_type_uint32(v, &value, name, &local_err);
291 if (local_err) {
292 goto out;
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;
299 nfds->maxlen = value;
301 out:
302 error_propagate(errp, local_err);
305 static char *file_dump_get_filename(Object *obj, Error **errp)
307 NetFilterDumpState *nfds = FILTER_DUMP(obj);
309 return g_strdup(nfds->filename);
312 static void file_dump_set_filename(Object *obj, const char *value, Error **errp)
314 NetFilterDumpState *nfds = FILTER_DUMP(obj);
316 g_free(nfds->filename);
317 nfds->filename = g_strdup(value);
320 static void filter_dump_instance_init(Object *obj)
322 NetFilterDumpState *nfds = FILTER_DUMP(obj);
324 nfds->maxlen = 65536;
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);
332 static void filter_dump_instance_finalize(Object *obj)
334 NetFilterDumpState *nfds = FILTER_DUMP(obj);
336 g_free(nfds->filename);
339 static void filter_dump_class_init(ObjectClass *oc, void *data)
341 NetFilterClass *nfc = NETFILTER_CLASS(oc);
343 nfc->setup = filter_dump_setup;
344 nfc->cleanup = filter_dump_cleanup;
345 nfc->receive_iov = filter_dump_receive_iov;
348 static const TypeInfo filter_dump_info = {
349 .name = TYPE_FILTER_DUMP,
350 .parent = TYPE_NETFILTER,
351 .class_init = filter_dump_class_init,
352 .instance_init = filter_dump_instance_init,
353 .instance_finalize = filter_dump_instance_finalize,
354 .instance_size = sizeof(NetFilterDumpState),
357 static void filter_dump_register_types(void)
359 type_register_static(&filter_dump_info);
362 type_init(filter_dump_register_types);