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
25 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qemu-common.h"
29 #include "qemu/error-report.h"
32 #include "qemu/timer.h"
33 #include "qapi/visitor.h"
34 #include "net/filter.h"
36 typedef struct DumpState
{
42 #define PCAP_MAGIC 0xa1b2c3d4
44 struct pcap_file_hdr
{
46 uint16_t version_major
;
47 uint16_t version_minor
;
54 struct pcap_sf_pkthdr
{
63 static ssize_t
dump_receive_iov(DumpState
*s
, const struct iovec
*iov
, int cnt
)
65 struct pcap_sf_pkthdr hdr
;
68 size_t size
= iov_size(iov
, cnt
);
69 struct iovec dumpiov
[cnt
+ 1];
71 /* Early return in case of previous error. */
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;
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");
97 static void dump_cleanup(DumpState
*s
)
103 static int net_dump_state_init(DumpState
*s
, const char *filename
,
104 int len
, Error
**errp
)
106 struct pcap_file_hdr hdr
;
110 fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
| O_BINARY
, 0644);
112 error_setg_errno(errp
, errno
, "-net dump: can't open %s", filename
);
116 hdr
.magic
= PCAP_MAGIC
;
117 hdr
.version_major
= 2;
118 hdr
.version_minor
= 4;
124 if (write(fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
125 error_setg_errno(errp
, errno
, "-net dump write error");
131 s
->pcap_caplen
= len
;
133 qemu_get_timedate(&tm
, 0);
134 s
->start_ts
= mktime(&tm
);
139 /* Dumping via VLAN netclient */
141 struct DumpNetClient
{
145 typedef struct DumpNetClient DumpNetClient
;
147 static ssize_t
dumpclient_receive(NetClientState
*nc
, const uint8_t *buf
,
150 DumpNetClient
*dc
= DO_UPCAST(DumpNetClient
, nc
, nc
);
152 .iov_base
= (void *)buf
,
156 return dump_receive_iov(&dc
->ds
, &iov
, 1);
159 static ssize_t
dumpclient_receive_iov(NetClientState
*nc
,
160 const struct iovec
*iov
, int cnt
)
162 DumpNetClient
*dc
= DO_UPCAST(DumpNetClient
, nc
, nc
);
164 return dump_receive_iov(&dc
->ds
, iov
, cnt
);
167 static void dumpclient_cleanup(NetClientState
*nc
)
169 DumpNetClient
*dc
= DO_UPCAST(DumpNetClient
, nc
, nc
);
171 dump_cleanup(&dc
->ds
);
174 static NetClientInfo net_dump_info
= {
175 .type
= NET_CLIENT_DRIVER_DUMP
,
176 .size
= sizeof(DumpNetClient
),
177 .receive
= dumpclient_receive
,
178 .receive_iov
= dumpclient_receive_iov
,
179 .cleanup
= dumpclient_cleanup
,
182 int net_init_dump(const Netdev
*netdev
, const char *name
,
183 NetClientState
*peer
, Error
**errp
)
188 const NetdevDumpOptions
*dump
;
192 assert(netdev
->type
== NET_CLIENT_DRIVER_DUMP
);
193 dump
= &netdev
->u
.dump
;
197 error_report("'-net dump' is deprecated. "
198 "Please use '-object filter-dump' instead.");
200 if (dump
->has_file
) {
206 ret
= net_hub_id_for_client(peer
, &id
);
207 assert(ret
== 0); /* peer must be on a hub */
209 snprintf(def_file
, sizeof(def_file
), "qemu-vlan%d.pcap", id
);
214 if (dump
->len
> INT_MAX
) {
215 error_setg(errp
, "invalid length: %"PRIu64
, dump
->len
);
223 nc
= qemu_new_net_client(&net_dump_info
, peer
, "dump", name
);
224 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
225 "dump to %s (len=%d)", file
, len
);
227 dnc
= DO_UPCAST(DumpNetClient
, nc
, nc
);
228 rc
= net_dump_state_init(&dnc
->ds
, file
, len
, errp
);
230 qemu_del_net_client(nc
);
235 /* Dumping via filter */
237 #define TYPE_FILTER_DUMP "filter-dump"
239 #define FILTER_DUMP(obj) \
240 OBJECT_CHECK(NetFilterDumpState, (obj), TYPE_FILTER_DUMP)
242 struct NetFilterDumpState
{
248 typedef struct NetFilterDumpState NetFilterDumpState
;
250 static ssize_t
filter_dump_receive_iov(NetFilterState
*nf
, NetClientState
*sndr
,
251 unsigned flags
, const struct iovec
*iov
,
252 int iovcnt
, NetPacketSent
*sent_cb
)
254 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
256 dump_receive_iov(&nfds
->ds
, iov
, iovcnt
);
260 static void filter_dump_cleanup(NetFilterState
*nf
)
262 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
264 dump_cleanup(&nfds
->ds
);
267 static void filter_dump_setup(NetFilterState
*nf
, Error
**errp
)
269 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
271 if (!nfds
->filename
) {
272 error_setg(errp
, "dump filter needs 'file' property set!");
276 net_dump_state_init(&nfds
->ds
, nfds
->filename
, nfds
->maxlen
, errp
);
279 static void filter_dump_get_maxlen(Object
*obj
, Visitor
*v
, const char *name
,
280 void *opaque
, Error
**errp
)
282 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
283 uint32_t value
= nfds
->maxlen
;
285 visit_type_uint32(v
, name
, &value
, errp
);
288 static void filter_dump_set_maxlen(Object
*obj
, Visitor
*v
, const char *name
,
289 void *opaque
, Error
**errp
)
291 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
292 Error
*local_err
= NULL
;
295 visit_type_uint32(v
, name
, &value
, &local_err
);
300 error_setg(&local_err
, "Property '%s.%s' doesn't take value '%u'",
301 object_get_typename(obj
), name
, value
);
304 nfds
->maxlen
= value
;
307 error_propagate(errp
, local_err
);
310 static char *file_dump_get_filename(Object
*obj
, Error
**errp
)
312 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
314 return g_strdup(nfds
->filename
);
317 static void file_dump_set_filename(Object
*obj
, const char *value
, Error
**errp
)
319 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
321 g_free(nfds
->filename
);
322 nfds
->filename
= g_strdup(value
);
325 static void filter_dump_instance_init(Object
*obj
)
327 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
329 nfds
->maxlen
= 65536;
331 object_property_add(obj
, "maxlen", "uint32", filter_dump_get_maxlen
,
332 filter_dump_set_maxlen
, NULL
, NULL
, NULL
);
333 object_property_add_str(obj
, "file", file_dump_get_filename
,
334 file_dump_set_filename
, NULL
);
337 static void filter_dump_instance_finalize(Object
*obj
)
339 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
341 g_free(nfds
->filename
);
344 static void filter_dump_class_init(ObjectClass
*oc
, void *data
)
346 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
348 nfc
->setup
= filter_dump_setup
;
349 nfc
->cleanup
= filter_dump_cleanup
;
350 nfc
->receive_iov
= filter_dump_receive_iov
;
353 static const TypeInfo filter_dump_info
= {
354 .name
= TYPE_FILTER_DUMP
,
355 .parent
= TYPE_NETFILTER
,
356 .class_init
= filter_dump_class_init
,
357 .instance_init
= filter_dump_instance_init
,
358 .instance_finalize
= filter_dump_instance_finalize
,
359 .instance_size
= sizeof(NetFilterDumpState
),
362 static void filter_dump_register_types(void)
364 type_register_static(&filter_dump_info
);
367 type_init(filter_dump_register_types
);