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 "qemu-common.h"
28 #include "qemu/error-report.h"
31 #include "qemu/timer.h"
32 #include "qapi/visitor.h"
33 #include "net/filter.h"
35 typedef struct DumpState
{
41 #define PCAP_MAGIC 0xa1b2c3d4
43 struct pcap_file_hdr
{
45 uint16_t version_major
;
46 uint16_t version_minor
;
53 struct pcap_sf_pkthdr
{
62 static ssize_t
dump_receive_iov(DumpState
*s
, const struct iovec
*iov
, int cnt
)
64 struct pcap_sf_pkthdr hdr
;
67 size_t size
= iov_size(iov
, cnt
);
68 struct iovec dumpiov
[cnt
+ 1];
70 /* Early return in case of previous error. */
75 ts
= qemu_clock_get_us(QEMU_CLOCK_VIRTUAL
);
76 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
78 hdr
.ts
.tv_sec
= ts
/ 1000000 + s
->start_ts
;
79 hdr
.ts
.tv_usec
= ts
% 1000000;
83 dumpiov
[0].iov_base
= &hdr
;
84 dumpiov
[0].iov_len
= sizeof(hdr
);
85 cnt
= iov_copy(&dumpiov
[1], cnt
, iov
, cnt
, 0, caplen
);
87 if (writev(s
->fd
, dumpiov
, cnt
+ 1) != sizeof(hdr
) + caplen
) {
88 error_report("network dump write error - stopping dump");
96 static void dump_cleanup(DumpState
*s
)
102 static int net_dump_state_init(DumpState
*s
, const char *filename
,
103 int len
, Error
**errp
)
105 struct pcap_file_hdr hdr
;
109 fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
| O_BINARY
, 0644);
111 error_setg_errno(errp
, errno
, "-net dump: can't open %s", filename
);
115 hdr
.magic
= PCAP_MAGIC
;
116 hdr
.version_major
= 2;
117 hdr
.version_minor
= 4;
123 if (write(fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
124 error_setg_errno(errp
, errno
, "-net dump write error");
130 s
->pcap_caplen
= len
;
132 qemu_get_timedate(&tm
, 0);
133 s
->start_ts
= mktime(&tm
);
138 /* Dumping via VLAN netclient */
140 struct DumpNetClient
{
144 typedef struct DumpNetClient DumpNetClient
;
146 static ssize_t
dumpclient_receive(NetClientState
*nc
, const uint8_t *buf
,
149 DumpNetClient
*dc
= DO_UPCAST(DumpNetClient
, nc
, nc
);
151 .iov_base
= (void *)buf
,
155 return dump_receive_iov(&dc
->ds
, &iov
, 1);
158 static ssize_t
dumpclient_receive_iov(NetClientState
*nc
,
159 const struct iovec
*iov
, int cnt
)
161 DumpNetClient
*dc
= DO_UPCAST(DumpNetClient
, nc
, nc
);
163 return dump_receive_iov(&dc
->ds
, iov
, cnt
);
166 static void dumpclient_cleanup(NetClientState
*nc
)
168 DumpNetClient
*dc
= DO_UPCAST(DumpNetClient
, nc
, nc
);
170 dump_cleanup(&dc
->ds
);
173 static NetClientInfo net_dump_info
= {
174 .type
= NET_CLIENT_OPTIONS_KIND_DUMP
,
175 .size
= sizeof(DumpNetClient
),
176 .receive
= dumpclient_receive
,
177 .receive_iov
= dumpclient_receive_iov
,
178 .cleanup
= dumpclient_cleanup
,
181 int net_init_dump(const NetClientOptions
*opts
, const char *name
,
182 NetClientState
*peer
, Error
**errp
)
187 const NetdevDumpOptions
*dump
;
191 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_DUMP
);
192 dump
= opts
->u
.dump
.data
;
196 if (dump
->has_file
) {
202 ret
= net_hub_id_for_client(peer
, &id
);
203 assert(ret
== 0); /* peer must be on a hub */
205 snprintf(def_file
, sizeof(def_file
), "qemu-vlan%d.pcap", id
);
210 if (dump
->len
> INT_MAX
) {
211 error_setg(errp
, "invalid length: %"PRIu64
, dump
->len
);
219 nc
= qemu_new_net_client(&net_dump_info
, peer
, "dump", name
);
220 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
221 "dump to %s (len=%d)", file
, len
);
223 dnc
= DO_UPCAST(DumpNetClient
, nc
, nc
);
224 rc
= net_dump_state_init(&dnc
->ds
, file
, len
, errp
);
226 qemu_del_net_client(nc
);
231 /* Dumping via filter */
233 #define TYPE_FILTER_DUMP "filter-dump"
235 #define FILTER_DUMP(obj) \
236 OBJECT_CHECK(NetFilterDumpState, (obj), TYPE_FILTER_DUMP)
238 struct NetFilterDumpState
{
244 typedef struct NetFilterDumpState NetFilterDumpState
;
246 static ssize_t
filter_dump_receive_iov(NetFilterState
*nf
, NetClientState
*sndr
,
247 unsigned flags
, const struct iovec
*iov
,
248 int iovcnt
, NetPacketSent
*sent_cb
)
250 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
252 dump_receive_iov(&nfds
->ds
, iov
, iovcnt
);
256 static void filter_dump_cleanup(NetFilterState
*nf
)
258 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
260 dump_cleanup(&nfds
->ds
);
263 static void filter_dump_setup(NetFilterState
*nf
, Error
**errp
)
265 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
267 if (!nfds
->filename
) {
268 error_setg(errp
, "dump filter needs 'file' property set!");
272 net_dump_state_init(&nfds
->ds
, nfds
->filename
, nfds
->maxlen
, errp
);
275 static void filter_dump_get_maxlen(Object
*obj
, Visitor
*v
, const char *name
,
276 void *opaque
, Error
**errp
)
278 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
279 uint32_t value
= nfds
->maxlen
;
281 visit_type_uint32(v
, name
, &value
, errp
);
284 static void filter_dump_set_maxlen(Object
*obj
, Visitor
*v
, const char *name
,
285 void *opaque
, Error
**errp
)
287 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
288 Error
*local_err
= NULL
;
291 visit_type_uint32(v
, name
, &value
, &local_err
);
296 error_setg(&local_err
, "Property '%s.%s' doesn't take value '%u'",
297 object_get_typename(obj
), name
, value
);
300 nfds
->maxlen
= value
;
303 error_propagate(errp
, local_err
);
306 static char *file_dump_get_filename(Object
*obj
, Error
**errp
)
308 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
310 return g_strdup(nfds
->filename
);
313 static void file_dump_set_filename(Object
*obj
, const char *value
, Error
**errp
)
315 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
317 g_free(nfds
->filename
);
318 nfds
->filename
= g_strdup(value
);
321 static void filter_dump_instance_init(Object
*obj
)
323 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
325 nfds
->maxlen
= 65536;
327 object_property_add(obj
, "maxlen", "int", filter_dump_get_maxlen
,
328 filter_dump_set_maxlen
, NULL
, NULL
, NULL
);
329 object_property_add_str(obj
, "file", file_dump_get_filename
,
330 file_dump_set_filename
, NULL
);
333 static void filter_dump_instance_finalize(Object
*obj
)
335 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
337 g_free(nfds
->filename
);
340 static void filter_dump_class_init(ObjectClass
*oc
, void *data
)
342 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
344 nfc
->setup
= filter_dump_setup
;
345 nfc
->cleanup
= filter_dump_cleanup
;
346 nfc
->receive_iov
= filter_dump_receive_iov
;
349 static const TypeInfo filter_dump_info
= {
350 .name
= TYPE_FILTER_DUMP
,
351 .parent
= TYPE_NETFILTER
,
352 .class_init
= filter_dump_class_init
,
353 .instance_init
= filter_dump_instance_init
,
354 .instance_finalize
= filter_dump_instance_finalize
,
355 .instance_size
= sizeof(NetFilterDumpState
),
358 static void filter_dump_register_types(void)
360 type_register_static(&filter_dump_info
);
363 type_init(filter_dump_register_types
);