]>
Commit | Line | Data |
---|---|---|
1abecf77 MM |
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 | ||
2744d920 | 25 | #include "qemu/osdep.h" |
a245fc18 | 26 | #include "clients.h" |
1abecf77 | 27 | #include "qemu-common.h" |
1de7afc9 | 28 | #include "qemu/error-report.h" |
43192fcc | 29 | #include "qemu/iov.h" |
1de7afc9 PB |
30 | #include "qemu/log.h" |
31 | #include "qemu/timer.h" | |
9d3e12e8 TH |
32 | #include "qapi/visitor.h" |
33 | #include "net/filter.h" | |
1abecf77 MM |
34 | |
35 | typedef struct DumpState { | |
0fa29915 | 36 | int64_t start_ts; |
1abecf77 MM |
37 | int fd; |
38 | int pcap_caplen; | |
39 | } DumpState; | |
40 | ||
41 | #define PCAP_MAGIC 0xa1b2c3d4 | |
42 | ||
43 | struct pcap_file_hdr { | |
44 | uint32_t magic; | |
45 | uint16_t version_major; | |
46 | uint16_t version_minor; | |
47 | int32_t thiszone; | |
48 | uint32_t sigfigs; | |
49 | uint32_t snaplen; | |
50 | uint32_t linktype; | |
51 | }; | |
52 | ||
53 | struct pcap_sf_pkthdr { | |
54 | struct { | |
55 | int32_t tv_sec; | |
56 | int32_t tv_usec; | |
57 | } ts; | |
58 | uint32_t caplen; | |
59 | uint32_t len; | |
60 | }; | |
61 | ||
75310e34 | 62 | static ssize_t dump_receive_iov(DumpState *s, const struct iovec *iov, int cnt) |
1abecf77 | 63 | { |
1abecf77 MM |
64 | struct pcap_sf_pkthdr hdr; |
65 | int64_t ts; | |
66 | int caplen; | |
43192fcc TH |
67 | size_t size = iov_size(iov, cnt); |
68 | struct iovec dumpiov[cnt + 1]; | |
1abecf77 MM |
69 | |
70 | /* Early return in case of previous error. */ | |
71 | if (s->fd < 0) { | |
72 | return size; | |
73 | } | |
74 | ||
ab60b748 | 75 | ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL); |
1abecf77 MM |
76 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
77 | ||
0fa29915 | 78 | hdr.ts.tv_sec = ts / 1000000 + s->start_ts; |
1abecf77 MM |
79 | hdr.ts.tv_usec = ts % 1000000; |
80 | hdr.caplen = caplen; | |
81 | hdr.len = size; | |
43192fcc TH |
82 | |
83 | dumpiov[0].iov_base = &hdr; | |
84 | dumpiov[0].iov_len = sizeof(hdr); | |
85 | cnt = iov_copy(&dumpiov[1], cnt, iov, cnt, 0, caplen); | |
86 | ||
87 | if (writev(s->fd, dumpiov, cnt + 1) != sizeof(hdr) + caplen) { | |
34f22fc0 | 88 | error_report("network dump write error - stopping dump"); |
1abecf77 MM |
89 | close(s->fd); |
90 | s->fd = -1; | |
91 | } | |
92 | ||
93 | return size; | |
94 | } | |
95 | ||
75310e34 | 96 | static void dump_cleanup(DumpState *s) |
1abecf77 | 97 | { |
1abecf77 | 98 | close(s->fd); |
75310e34 | 99 | s->fd = -1; |
1abecf77 MM |
100 | } |
101 | ||
7bc3074c TH |
102 | static int net_dump_state_init(DumpState *s, const char *filename, |
103 | int len, Error **errp) | |
1abecf77 MM |
104 | { |
105 | struct pcap_file_hdr hdr; | |
0fa29915 | 106 | struct tm tm; |
731d5856 | 107 | int fd; |
1abecf77 | 108 | |
6514ed52 | 109 | fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644); |
731d5856 | 110 | if (fd < 0) { |
3791f83c | 111 | error_setg_errno(errp, errno, "-net dump: can't open %s", filename); |
1abecf77 MM |
112 | return -1; |
113 | } | |
114 | ||
1abecf77 MM |
115 | hdr.magic = PCAP_MAGIC; |
116 | hdr.version_major = 2; | |
117 | hdr.version_minor = 4; | |
118 | hdr.thiszone = 0; | |
119 | hdr.sigfigs = 0; | |
731d5856 | 120 | hdr.snaplen = len; |
1abecf77 MM |
121 | hdr.linktype = 1; |
122 | ||
731d5856 | 123 | if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
3791f83c | 124 | error_setg_errno(errp, errno, "-net dump write error"); |
731d5856 | 125 | close(fd); |
1abecf77 MM |
126 | return -1; |
127 | } | |
128 | ||
731d5856 MM |
129 | s->fd = fd; |
130 | s->pcap_caplen = len; | |
131 | ||
0fa29915 HP |
132 | qemu_get_timedate(&tm, 0); |
133 | s->start_ts = mktime(&tm); | |
134 | ||
1abecf77 MM |
135 | return 0; |
136 | } | |
137 | ||
75310e34 TH |
138 | /* Dumping via VLAN netclient */ |
139 | ||
140 | struct DumpNetClient { | |
141 | NetClientState nc; | |
142 | DumpState ds; | |
143 | }; | |
144 | typedef struct DumpNetClient DumpNetClient; | |
145 | ||
146 | static ssize_t dumpclient_receive(NetClientState *nc, const uint8_t *buf, | |
147 | size_t size) | |
148 | { | |
149 | DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc); | |
150 | struct iovec iov = { | |
151 | .iov_base = (void *)buf, | |
152 | .iov_len = size | |
153 | }; | |
154 | ||
155 | return dump_receive_iov(&dc->ds, &iov, 1); | |
156 | } | |
157 | ||
158 | static ssize_t dumpclient_receive_iov(NetClientState *nc, | |
159 | const struct iovec *iov, int cnt) | |
160 | { | |
161 | DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc); | |
162 | ||
163 | return dump_receive_iov(&dc->ds, iov, cnt); | |
164 | } | |
165 | ||
166 | static void dumpclient_cleanup(NetClientState *nc) | |
167 | { | |
168 | DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc); | |
169 | ||
170 | dump_cleanup(&dc->ds); | |
171 | } | |
172 | ||
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, | |
179 | }; | |
180 | ||
1a0c0958 | 181 | int net_init_dump(const NetClientOptions *opts, const char *name, |
a30ecde6 | 182 | NetClientState *peer, Error **errp) |
1abecf77 | 183 | { |
7bc3074c | 184 | int len, rc; |
1abecf77 MM |
185 | const char *file; |
186 | char def_file[128]; | |
848040d1 | 187 | const NetdevDumpOptions *dump; |
7bc3074c | 188 | NetClientState *nc; |
75310e34 | 189 | DumpNetClient *dnc; |
848040d1 | 190 | |
8d0bcba8 EB |
191 | assert(opts->type == NET_CLIENT_OPTIONS_KIND_DUMP); |
192 | dump = opts->u.dump; | |
1abecf77 | 193 | |
d33d93b2 | 194 | assert(peer); |
1abecf77 | 195 | |
848040d1 LE |
196 | if (dump->has_file) { |
197 | file = dump->file; | |
198 | } else { | |
d33d93b2 SH |
199 | int id; |
200 | int ret; | |
201 | ||
202 | ret = net_hub_id_for_client(peer, &id); | |
203 | assert(ret == 0); /* peer must be on a hub */ | |
204 | ||
205 | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", id); | |
1abecf77 MM |
206 | file = def_file; |
207 | } | |
208 | ||
848040d1 LE |
209 | if (dump->has_len) { |
210 | if (dump->len > INT_MAX) { | |
3791f83c | 211 | error_setg(errp, "invalid length: %"PRIu64, dump->len); |
848040d1 LE |
212 | return -1; |
213 | } | |
214 | len = dump->len; | |
215 | } else { | |
216 | len = 65536; | |
217 | } | |
1abecf77 | 218 | |
7bc3074c TH |
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); | |
222 | ||
75310e34 TH |
223 | dnc = DO_UPCAST(DumpNetClient, nc, nc); |
224 | rc = net_dump_state_init(&dnc->ds, file, len, errp); | |
7bc3074c TH |
225 | if (rc) { |
226 | qemu_del_net_client(nc); | |
227 | } | |
228 | return rc; | |
1abecf77 | 229 | } |
9d3e12e8 TH |
230 | |
231 | /* Dumping via filter */ | |
232 | ||
233 | #define TYPE_FILTER_DUMP "filter-dump" | |
234 | ||
235 | #define FILTER_DUMP(obj) \ | |
236 | OBJECT_CHECK(NetFilterDumpState, (obj), TYPE_FILTER_DUMP) | |
237 | ||
238 | struct NetFilterDumpState { | |
239 | NetFilterState nfs; | |
240 | DumpState ds; | |
241 | char *filename; | |
242 | uint32_t maxlen; | |
243 | }; | |
244 | typedef struct NetFilterDumpState NetFilterDumpState; | |
245 | ||
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) | |
249 | { | |
250 | NetFilterDumpState *nfds = FILTER_DUMP(nf); | |
251 | ||
252 | dump_receive_iov(&nfds->ds, iov, iovcnt); | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static void filter_dump_cleanup(NetFilterState *nf) | |
257 | { | |
258 | NetFilterDumpState *nfds = FILTER_DUMP(nf); | |
259 | ||
260 | dump_cleanup(&nfds->ds); | |
261 | } | |
262 | ||
263 | static void filter_dump_setup(NetFilterState *nf, Error **errp) | |
264 | { | |
265 | NetFilterDumpState *nfds = FILTER_DUMP(nf); | |
266 | ||
267 | if (!nfds->filename) { | |
268 | error_setg(errp, "dump filter needs 'file' property set!"); | |
269 | return; | |
270 | } | |
271 | ||
272 | net_dump_state_init(&nfds->ds, nfds->filename, nfds->maxlen, errp); | |
273 | } | |
274 | ||
275 | static void filter_dump_get_maxlen(Object *obj, Visitor *v, void *opaque, | |
276 | const char *name, Error **errp) | |
277 | { | |
278 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
279 | uint32_t value = nfds->maxlen; | |
280 | ||
51e72bc1 | 281 | visit_type_uint32(v, name, &value, errp); |
9d3e12e8 TH |
282 | } |
283 | ||
284 | static void filter_dump_set_maxlen(Object *obj, Visitor *v, void *opaque, | |
285 | const char *name, Error **errp) | |
286 | { | |
287 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
288 | Error *local_err = NULL; | |
289 | uint32_t value; | |
290 | ||
51e72bc1 | 291 | visit_type_uint32(v, name, &value, &local_err); |
9d3e12e8 TH |
292 | if (local_err) { |
293 | goto out; | |
294 | } | |
295 | if (value == 0) { | |
296 | error_setg(&local_err, "Property '%s.%s' doesn't take value '%u'", | |
297 | object_get_typename(obj), name, value); | |
298 | goto out; | |
299 | } | |
300 | nfds->maxlen = value; | |
301 | ||
302 | out: | |
303 | error_propagate(errp, local_err); | |
304 | } | |
305 | ||
306 | static char *file_dump_get_filename(Object *obj, Error **errp) | |
307 | { | |
308 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
309 | ||
310 | return g_strdup(nfds->filename); | |
311 | } | |
312 | ||
313 | static void file_dump_set_filename(Object *obj, const char *value, Error **errp) | |
314 | { | |
315 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
316 | ||
317 | g_free(nfds->filename); | |
318 | nfds->filename = g_strdup(value); | |
319 | } | |
320 | ||
321 | static void filter_dump_instance_init(Object *obj) | |
322 | { | |
323 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
324 | ||
325 | nfds->maxlen = 65536; | |
326 | ||
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); | |
331 | } | |
332 | ||
b50c7d45 LZ |
333 | static void filter_dump_instance_finalize(Object *obj) |
334 | { | |
335 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
336 | ||
337 | g_free(nfds->filename); | |
338 | } | |
339 | ||
9d3e12e8 TH |
340 | static void filter_dump_class_init(ObjectClass *oc, void *data) |
341 | { | |
342 | NetFilterClass *nfc = NETFILTER_CLASS(oc); | |
343 | ||
344 | nfc->setup = filter_dump_setup; | |
345 | nfc->cleanup = filter_dump_cleanup; | |
346 | nfc->receive_iov = filter_dump_receive_iov; | |
347 | } | |
348 | ||
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, | |
b50c7d45 | 354 | .instance_finalize = filter_dump_instance_finalize, |
9d3e12e8 TH |
355 | .instance_size = sizeof(NetFilterDumpState), |
356 | }; | |
357 | ||
358 | static void filter_dump_register_types(void) | |
359 | { | |
360 | type_register_static(&filter_dump_info); | |
361 | } | |
362 | ||
363 | type_init(filter_dump_register_types); |