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/error-report.h"
30 #include "qemu/module.h"
31 #include "qemu/timer.h"
32 #include "qapi/visitor.h"
33 #include "net/filter.h"
34 #include "qom/object.h"
35 #include "sysemu/rtc.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
,
67 struct pcap_sf_pkthdr hdr
;
70 size_t size
= iov_size(iov
, cnt
) - offset
;
71 g_autofree
struct iovec
*dumpiov
= g_new(struct iovec
, cnt
+ 1);
73 /* Early return in case of previous error. */
78 ts
= qemu_clock_get_us(QEMU_CLOCK_VIRTUAL
);
79 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
81 hdr
.ts
.tv_sec
= ts
/ 1000000 + s
->start_ts
;
82 hdr
.ts
.tv_usec
= ts
% 1000000;
86 dumpiov
[0].iov_base
= &hdr
;
87 dumpiov
[0].iov_len
= sizeof(hdr
);
88 cnt
= iov_copy(&dumpiov
[1], cnt
, iov
, cnt
, offset
, caplen
);
90 if (writev(s
->fd
, dumpiov
, cnt
+ 1) != sizeof(hdr
) + caplen
) {
91 error_report("network dump write error - stopping dump");
99 static void dump_cleanup(DumpState
*s
)
105 static int net_dump_state_init(DumpState
*s
, const char *filename
,
106 int len
, Error
**errp
)
108 struct pcap_file_hdr hdr
;
112 fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
| O_BINARY
, 0644);
114 error_setg_errno(errp
, errno
, "net dump: can't open %s", filename
);
118 hdr
.magic
= PCAP_MAGIC
;
119 hdr
.version_major
= 2;
120 hdr
.version_minor
= 4;
126 if (write(fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
127 error_setg_errno(errp
, errno
, "net dump write error");
133 s
->pcap_caplen
= len
;
135 qemu_get_timedate(&tm
, 0);
136 s
->start_ts
= mktime(&tm
);
141 #define TYPE_FILTER_DUMP "filter-dump"
143 OBJECT_DECLARE_SIMPLE_TYPE(NetFilterDumpState
, FILTER_DUMP
)
145 struct NetFilterDumpState
{
152 static ssize_t
filter_dump_receive_iov(NetFilterState
*nf
, NetClientState
*sndr
,
153 unsigned flags
, const struct iovec
*iov
,
154 int iovcnt
, NetPacketSent
*sent_cb
)
156 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
157 int offset
= qemu_get_using_vnet_hdr(nf
->netdev
) ?
158 qemu_get_vnet_hdr_len(nf
->netdev
) : 0;
160 dump_receive_iov(&nfds
->ds
, iov
, iovcnt
, offset
);
164 static void filter_dump_cleanup(NetFilterState
*nf
)
166 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
168 dump_cleanup(&nfds
->ds
);
171 static void filter_dump_setup(NetFilterState
*nf
, Error
**errp
)
173 NetFilterDumpState
*nfds
= FILTER_DUMP(nf
);
175 if (!nfds
->filename
) {
176 error_setg(errp
, "dump filter needs 'file' property set!");
180 net_dump_state_init(&nfds
->ds
, nfds
->filename
, nfds
->maxlen
, errp
);
183 static void filter_dump_get_maxlen(Object
*obj
, Visitor
*v
, const char *name
,
184 void *opaque
, Error
**errp
)
186 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
187 uint32_t value
= nfds
->maxlen
;
189 visit_type_uint32(v
, name
, &value
, errp
);
192 static void filter_dump_set_maxlen(Object
*obj
, Visitor
*v
, const char *name
,
193 void *opaque
, Error
**errp
)
195 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
198 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
202 error_setg(errp
, "Property '%s.%s' doesn't take value '%u'",
203 object_get_typename(obj
), name
, value
);
206 nfds
->maxlen
= value
;
209 static char *file_dump_get_filename(Object
*obj
, Error
**errp
)
211 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
213 return g_strdup(nfds
->filename
);
216 static void file_dump_set_filename(Object
*obj
, const char *value
, Error
**errp
)
218 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
220 g_free(nfds
->filename
);
221 nfds
->filename
= g_strdup(value
);
224 static void filter_dump_instance_init(Object
*obj
)
226 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
228 nfds
->maxlen
= 65536;
231 static void filter_dump_instance_finalize(Object
*obj
)
233 NetFilterDumpState
*nfds
= FILTER_DUMP(obj
);
235 g_free(nfds
->filename
);
238 static void filter_dump_class_init(ObjectClass
*oc
, void *data
)
240 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
242 object_class_property_add(oc
, "maxlen", "uint32", filter_dump_get_maxlen
,
243 filter_dump_set_maxlen
, NULL
, NULL
);
244 object_class_property_add_str(oc
, "file", file_dump_get_filename
,
245 file_dump_set_filename
);
247 nfc
->setup
= filter_dump_setup
;
248 nfc
->cleanup
= filter_dump_cleanup
;
249 nfc
->receive_iov
= filter_dump_receive_iov
;
252 static const TypeInfo filter_dump_info
= {
253 .name
= TYPE_FILTER_DUMP
,
254 .parent
= TYPE_NETFILTER
,
255 .class_init
= filter_dump_class_init
,
256 .instance_init
= filter_dump_instance_init
,
257 .instance_finalize
= filter_dump_instance_finalize
,
258 .instance_size
= sizeof(NetFilterDumpState
),
261 static void filter_dump_register_types(void)
263 type_register_static(&filter_dump_info
);
266 type_init(filter_dump_register_types
);