block/export: Add query-block-exports
[qemu.git] / net / dump.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "clients.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
30 #include "qemu/iov.h"
31 #include "qemu/log.h"
32 #include "qemu/module.h"
33 #include "qemu/timer.h"
34 #include "qapi/visitor.h"
35 #include "net/filter.h"
36 #include "qom/object.h"
37
38 typedef struct DumpState {
39 int64_t start_ts;
40 int fd;
41 int pcap_caplen;
42 } DumpState;
43
44 #define PCAP_MAGIC 0xa1b2c3d4
45
46 struct pcap_file_hdr {
47 uint32_t magic;
48 uint16_t version_major;
49 uint16_t version_minor;
50 int32_t thiszone;
51 uint32_t sigfigs;
52 uint32_t snaplen;
53 uint32_t linktype;
54 };
55
56 struct pcap_sf_pkthdr {
57 struct {
58 int32_t tv_sec;
59 int32_t tv_usec;
60 } ts;
61 uint32_t caplen;
62 uint32_t len;
63 };
64
65 static ssize_t dump_receive_iov(DumpState *s, const struct iovec *iov, int cnt)
66 {
67 struct pcap_sf_pkthdr hdr;
68 int64_t ts;
69 int caplen;
70 size_t size = iov_size(iov, cnt);
71 struct iovec dumpiov[cnt + 1];
72
73 /* Early return in case of previous error. */
74 if (s->fd < 0) {
75 return size;
76 }
77
78 ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL);
79 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
80
81 hdr.ts.tv_sec = ts / 1000000 + s->start_ts;
82 hdr.ts.tv_usec = ts % 1000000;
83 hdr.caplen = caplen;
84 hdr.len = size;
85
86 dumpiov[0].iov_base = &hdr;
87 dumpiov[0].iov_len = sizeof(hdr);
88 cnt = iov_copy(&dumpiov[1], cnt, iov, cnt, 0, caplen);
89
90 if (writev(s->fd, dumpiov, cnt + 1) != sizeof(hdr) + caplen) {
91 error_report("network dump write error - stopping dump");
92 close(s->fd);
93 s->fd = -1;
94 }
95
96 return size;
97 }
98
99 static void dump_cleanup(DumpState *s)
100 {
101 close(s->fd);
102 s->fd = -1;
103 }
104
105 static int net_dump_state_init(DumpState *s, const char *filename,
106 int len, Error **errp)
107 {
108 struct pcap_file_hdr hdr;
109 struct tm tm;
110 int fd;
111
112 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
113 if (fd < 0) {
114 error_setg_errno(errp, errno, "net dump: can't open %s", filename);
115 return -1;
116 }
117
118 hdr.magic = PCAP_MAGIC;
119 hdr.version_major = 2;
120 hdr.version_minor = 4;
121 hdr.thiszone = 0;
122 hdr.sigfigs = 0;
123 hdr.snaplen = len;
124 hdr.linktype = 1;
125
126 if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
127 error_setg_errno(errp, errno, "net dump write error");
128 close(fd);
129 return -1;
130 }
131
132 s->fd = fd;
133 s->pcap_caplen = len;
134
135 qemu_get_timedate(&tm, 0);
136 s->start_ts = mktime(&tm);
137
138 return 0;
139 }
140
141 #define TYPE_FILTER_DUMP "filter-dump"
142
143 OBJECT_DECLARE_SIMPLE_TYPE(NetFilterDumpState, FILTER_DUMP)
144
145 struct NetFilterDumpState {
146 NetFilterState nfs;
147 DumpState ds;
148 char *filename;
149 uint32_t maxlen;
150 };
151
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)
155 {
156 NetFilterDumpState *nfds = FILTER_DUMP(nf);
157
158 dump_receive_iov(&nfds->ds, iov, iovcnt);
159 return 0;
160 }
161
162 static void filter_dump_cleanup(NetFilterState *nf)
163 {
164 NetFilterDumpState *nfds = FILTER_DUMP(nf);
165
166 dump_cleanup(&nfds->ds);
167 }
168
169 static void filter_dump_setup(NetFilterState *nf, Error **errp)
170 {
171 NetFilterDumpState *nfds = FILTER_DUMP(nf);
172
173 if (!nfds->filename) {
174 error_setg(errp, "dump filter needs 'file' property set!");
175 return;
176 }
177
178 net_dump_state_init(&nfds->ds, nfds->filename, nfds->maxlen, errp);
179 }
180
181 static void filter_dump_get_maxlen(Object *obj, Visitor *v, const char *name,
182 void *opaque, Error **errp)
183 {
184 NetFilterDumpState *nfds = FILTER_DUMP(obj);
185 uint32_t value = nfds->maxlen;
186
187 visit_type_uint32(v, name, &value, errp);
188 }
189
190 static void filter_dump_set_maxlen(Object *obj, Visitor *v, const char *name,
191 void *opaque, Error **errp)
192 {
193 NetFilterDumpState *nfds = FILTER_DUMP(obj);
194 uint32_t value;
195
196 if (!visit_type_uint32(v, name, &value, errp)) {
197 return;
198 }
199 if (value == 0) {
200 error_setg(errp, "Property '%s.%s' doesn't take value '%u'",
201 object_get_typename(obj), name, value);
202 return;
203 }
204 nfds->maxlen = value;
205 }
206
207 static char *file_dump_get_filename(Object *obj, Error **errp)
208 {
209 NetFilterDumpState *nfds = FILTER_DUMP(obj);
210
211 return g_strdup(nfds->filename);
212 }
213
214 static void file_dump_set_filename(Object *obj, const char *value, Error **errp)
215 {
216 NetFilterDumpState *nfds = FILTER_DUMP(obj);
217
218 g_free(nfds->filename);
219 nfds->filename = g_strdup(value);
220 }
221
222 static void filter_dump_instance_init(Object *obj)
223 {
224 NetFilterDumpState *nfds = FILTER_DUMP(obj);
225
226 nfds->maxlen = 65536;
227
228 object_property_add(obj, "maxlen", "uint32", filter_dump_get_maxlen,
229 filter_dump_set_maxlen, NULL, NULL);
230 object_property_add_str(obj, "file", file_dump_get_filename,
231 file_dump_set_filename);
232 }
233
234 static void filter_dump_instance_finalize(Object *obj)
235 {
236 NetFilterDumpState *nfds = FILTER_DUMP(obj);
237
238 g_free(nfds->filename);
239 }
240
241 static void filter_dump_class_init(ObjectClass *oc, void *data)
242 {
243 NetFilterClass *nfc = NETFILTER_CLASS(oc);
244
245 nfc->setup = filter_dump_setup;
246 nfc->cleanup = filter_dump_cleanup;
247 nfc->receive_iov = filter_dump_receive_iov;
248 }
249
250 static const TypeInfo filter_dump_info = {
251 .name = TYPE_FILTER_DUMP,
252 .parent = TYPE_NETFILTER,
253 .class_init = filter_dump_class_init,
254 .instance_init = filter_dump_instance_init,
255 .instance_finalize = filter_dump_instance_finalize,
256 .instance_size = sizeof(NetFilterDumpState),
257 };
258
259 static void filter_dump_register_types(void)
260 {
261 type_register_static(&filter_dump_info);
262 }
263
264 type_init(filter_dump_register_types);