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