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
26 #include "qemu-common.h"
27 #include "qemu/error-report.h"
30 #include "qemu/timer.h"
31 #include "qapi/visitor.h"
32 #include "net/filter.h"
34 typedef struct DumpState
{
40 #define PCAP_MAGIC 0xa1b2c3d4
42 struct pcap_file_hdr
{
44 uint16_t version_major
;
45 uint16_t version_minor
;
52 struct pcap_sf_pkthdr
{
61 static ssize_t
dump_receive_iov(DumpState
*s
, const struct iovec
*iov
, int cnt
)
63 struct pcap_sf_pkthdr hdr
;
66 size_t size
= iov_size(iov
, cnt
);
67 struct iovec dumpiov
[cnt
+ 1];
69 /* Early return in case of previous error. */
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;
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 qemu_log("-net dump write error - stop dump\n");
95 static void dump_cleanup(DumpState
*s
)
101 static int net_dump_state_init(DumpState
*s
, const char *filename
,
102 int len
, Error
**errp
)
104 struct pcap_file_hdr hdr
;
108 fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
| O_BINARY
, 0644);
110 error_setg_errno(errp
, errno
, "-net dump: can't open %s", filename
);
114 hdr
.magic
= PCAP_MAGIC
;
115 hdr
.version_major
= 2;
116 hdr
.version_minor
= 4;
122 if (write(fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
123 error_setg_errno(errp
, errno
, "-net dump write error");
129 s
->pcap_caplen
= len
;
131 qemu_get_timedate(&tm
, 0);
132 s
->start_ts
= mktime(&tm
);
137 /* Dumping via VLAN netclient */
139 struct DumpNetClient
{
143 typedef struct DumpNetClient DumpNetClient
;
145 static ssize_t
dumpclient_receive(NetClientState
*nc
, const uint8_t *buf
,
148 DumpNetClient
*dc
= DO_UPCAST(DumpNetClient
, nc
, nc
);
150 .iov_base
= (void *)buf
,
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
)
186 const NetdevDumpOptions
*dump
;
190 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_DUMP
);
195 if (dump
->has_file
) {
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
);
209 if (dump
->len
> INT_MAX
) {
210 error_setg(errp
, "invalid length: %"PRIu64
, dump
->len
);
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
);
225 qemu_del_net_client(nc
);
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
{
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
);
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!");
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
;
290 visit_type_uint32(v
, &value
, name
, &local_err
);
295 error_setg(&local_err
, "Property '%s.%s' doesn't take value '%u'",
296 object_get_typename(obj
), name
, value
);
299 nfds
->maxlen
= value
;
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_class_init(ObjectClass
*oc
, void *data
)
334 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
336 nfc
->setup
= filter_dump_setup
;
337 nfc
->cleanup
= filter_dump_cleanup
;
338 nfc
->receive_iov
= filter_dump_receive_iov
;
341 static 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
),
349 static void filter_dump_register_types(void)
351 type_register_static(&filter_dump_info
);
354 type_init(filter_dump_register_types
);