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"
26 #include "qemu-common.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
32 #include "qemu/module.h"
33 #include "qemu/timer.h"
34 #include "qapi/visitor.h"
35 #include "net/filter.h"
37 typedef struct DumpState
{
43 #define PCAP_MAGIC 0xa1b2c3d4
45 struct pcap_file_hdr
{
47 uint16_t version_major
;
48 uint16_t version_minor
;
55 struct pcap_sf_pkthdr
{
64 static ssize_t
dump_receive_iov(DumpState
*s
, const struct iovec
*iov
, int cnt
)
66 struct pcap_sf_pkthdr hdr
;
69 size_t size
= iov_size(iov
, cnt
);
70 struct iovec dumpiov
[cnt
+ 1];
72 /* Early return in case of previous error. */
77 ts
= qemu_clock_get_us(QEMU_CLOCK_VIRTUAL
);
78 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
80 hdr
.ts
.tv_sec
= ts
/ 1000000 + s
->start_ts
;
81 hdr
.ts
.tv_usec
= ts
% 1000000;
85 dumpiov
[0].iov_base
= &hdr
;
86 dumpiov
[0].iov_len
= sizeof(hdr
);
87 cnt
= iov_copy(&dumpiov
[1], cnt
, iov
, cnt
, 0, caplen
);
89 if (writev(s
->fd
, dumpiov
, cnt
+ 1) != sizeof(hdr
) + caplen
) {
90 error_report("network dump write error - stopping dump");
98 static void dump_cleanup(DumpState
*s
)
104 static int net_dump_state_init(DumpState
*s
, const char *filename
,
105 int len
, Error
**errp
)
107 struct pcap_file_hdr hdr
;
111 fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
| O_BINARY
, 0644);
113 error_setg_errno(errp
, errno
, "net dump: can't open %s", filename
);
117 hdr
.magic
= PCAP_MAGIC
;
118 hdr
.version_major
= 2;
119 hdr
.version_minor
= 4;
125 if (write(fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
126 error_setg_errno(errp
, errno
, "net dump write error");
132 s
->pcap_caplen
= len
;
134 qemu_get_timedate(&tm
, 0);
135 s
->start_ts
= mktime(&tm
);
140 #define TYPE_FILTER_DUMP "filter-dump"
142 #define FILTER_DUMP(obj) \
143 OBJECT_CHECK(NetFilterDumpState, (obj), TYPE_FILTER_DUMP)
145 struct NetFilterDumpState
{
151 typedef struct NetFilterDumpState NetFilterDumpState
;
153 static ssize_t
filter_dump_receive_iov(NetFilterState
*nf
, NetClientState
*sndr
,
154 unsigned flags
, const struct iovec
*iov
,
155 int iovcnt
, NetPacketSent
*sent_cb
)
157 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
159 dump_receive_iov(&nfds
->ds
, iov
, iovcnt
);
163 static void filter_dump_cleanup(NetFilterState
*nf
)
165 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
167 dump_cleanup(&nfds
->ds
);
170 static void filter_dump_setup(NetFilterState
*nf
, Error
**errp
)
172 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
174 if (!nfds
->filename
) {
175 error_setg(errp
, "dump filter needs 'file' property set!");
179 net_dump_state_init(&nfds
->ds
, nfds
->filename
, nfds
->maxlen
, errp
);
182 static void filter_dump_get_maxlen(Object
*obj
, Visitor
*v
, const char *name
,
183 void *opaque
, Error
**errp
)
185 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
186 uint32_t value
= nfds
->maxlen
;
188 visit_type_uint32(v
, name
, &value
, errp
);
191 static void filter_dump_set_maxlen(Object
*obj
, Visitor
*v
, const char *name
,
192 void *opaque
, Error
**errp
)
194 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
197 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
201 error_setg(errp
, "Property '%s.%s' doesn't take value '%u'",
202 object_get_typename(obj
), name
, value
);
205 nfds
->maxlen
= value
;
208 static char *file_dump_get_filename(Object
*obj
, Error
**errp
)
210 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
212 return g_strdup(nfds
->filename
);
215 static void file_dump_set_filename(Object
*obj
, const char *value
, Error
**errp
)
217 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
219 g_free(nfds
->filename
);
220 nfds
->filename
= g_strdup(value
);
223 static void filter_dump_instance_init(Object
*obj
)
225 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
227 nfds
->maxlen
= 65536;
229 object_property_add(obj
, "maxlen", "uint32", filter_dump_get_maxlen
,
230 filter_dump_set_maxlen
, NULL
, NULL
);
231 object_property_add_str(obj
, "file", file_dump_get_filename
,
232 file_dump_set_filename
);
235 static void filter_dump_instance_finalize(Object
*obj
)
237 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
239 g_free(nfds
->filename
);
242 static void filter_dump_class_init(ObjectClass
*oc
, void *data
)
244 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
246 nfc
->setup
= filter_dump_setup
;
247 nfc
->cleanup
= filter_dump_cleanup
;
248 nfc
->receive_iov
= filter_dump_receive_iov
;
251 static const TypeInfo filter_dump_info
= {
252 .name
= TYPE_FILTER_DUMP
,
253 .parent
= TYPE_NETFILTER
,
254 .class_init
= filter_dump_class_init
,
255 .instance_init
= filter_dump_instance_init
,
256 .instance_finalize
= filter_dump_instance_finalize
,
257 .instance_size
= sizeof(NetFilterDumpState
),
260 static void filter_dump_register_types(void)
262 type_register_static(&filter_dump_info
);
265 type_init(filter_dump_register_types
);