root / net / dump.c @ f5c5e381
History | View | Annotate | Download (4.1 kB)
1 | 1abecf77 | Mark McLoughlin | /*
|
---|---|---|---|
2 | 1abecf77 | Mark McLoughlin | * QEMU System Emulator
|
3 | 1abecf77 | Mark McLoughlin | *
|
4 | 1abecf77 | Mark McLoughlin | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 1abecf77 | Mark McLoughlin | *
|
6 | 1abecf77 | Mark McLoughlin | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 1abecf77 | Mark McLoughlin | * of this software and associated documentation files (the "Software"), to deal
|
8 | 1abecf77 | Mark McLoughlin | * in the Software without restriction, including without limitation the rights
|
9 | 1abecf77 | Mark McLoughlin | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 1abecf77 | Mark McLoughlin | * copies of the Software, and to permit persons to whom the Software is
|
11 | 1abecf77 | Mark McLoughlin | * furnished to do so, subject to the following conditions:
|
12 | 1abecf77 | Mark McLoughlin | *
|
13 | 1abecf77 | Mark McLoughlin | * The above copyright notice and this permission notice shall be included in
|
14 | 1abecf77 | Mark McLoughlin | * all copies or substantial portions of the Software.
|
15 | 1abecf77 | Mark McLoughlin | *
|
16 | 1abecf77 | Mark McLoughlin | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 1abecf77 | Mark McLoughlin | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 1abecf77 | Mark McLoughlin | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 1abecf77 | Mark McLoughlin | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 1abecf77 | Mark McLoughlin | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 1abecf77 | Mark McLoughlin | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 1abecf77 | Mark McLoughlin | * THE SOFTWARE.
|
23 | 1abecf77 | Mark McLoughlin | */
|
24 | 1abecf77 | Mark McLoughlin | |
25 | 1abecf77 | Mark McLoughlin | #include "dump.h" |
26 | 1abecf77 | Mark McLoughlin | #include "qemu-common.h" |
27 | 1abecf77 | Mark McLoughlin | #include "sysemu.h" |
28 | 1abecf77 | Mark McLoughlin | #include "qemu-log.h" |
29 | 1abecf77 | Mark McLoughlin | |
30 | 1abecf77 | Mark McLoughlin | typedef struct DumpState { |
31 | 731d5856 | Mark McLoughlin | VLANClientState nc; |
32 | 1abecf77 | Mark McLoughlin | int fd;
|
33 | 1abecf77 | Mark McLoughlin | int pcap_caplen;
|
34 | 1abecf77 | Mark McLoughlin | } DumpState; |
35 | 1abecf77 | Mark McLoughlin | |
36 | 1abecf77 | Mark McLoughlin | #define PCAP_MAGIC 0xa1b2c3d4 |
37 | 1abecf77 | Mark McLoughlin | |
38 | 1abecf77 | Mark McLoughlin | struct pcap_file_hdr {
|
39 | 1abecf77 | Mark McLoughlin | uint32_t magic; |
40 | 1abecf77 | Mark McLoughlin | uint16_t version_major; |
41 | 1abecf77 | Mark McLoughlin | uint16_t version_minor; |
42 | 1abecf77 | Mark McLoughlin | int32_t thiszone; |
43 | 1abecf77 | Mark McLoughlin | uint32_t sigfigs; |
44 | 1abecf77 | Mark McLoughlin | uint32_t snaplen; |
45 | 1abecf77 | Mark McLoughlin | uint32_t linktype; |
46 | 1abecf77 | Mark McLoughlin | }; |
47 | 1abecf77 | Mark McLoughlin | |
48 | 1abecf77 | Mark McLoughlin | struct pcap_sf_pkthdr {
|
49 | 1abecf77 | Mark McLoughlin | struct {
|
50 | 1abecf77 | Mark McLoughlin | int32_t tv_sec; |
51 | 1abecf77 | Mark McLoughlin | int32_t tv_usec; |
52 | 1abecf77 | Mark McLoughlin | } ts; |
53 | 1abecf77 | Mark McLoughlin | uint32_t caplen; |
54 | 1abecf77 | Mark McLoughlin | uint32_t len; |
55 | 1abecf77 | Mark McLoughlin | }; |
56 | 1abecf77 | Mark McLoughlin | |
57 | 731d5856 | Mark McLoughlin | static ssize_t dump_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
58 | 1abecf77 | Mark McLoughlin | { |
59 | 731d5856 | Mark McLoughlin | DumpState *s = DO_UPCAST(DumpState, nc, nc); |
60 | 1abecf77 | Mark McLoughlin | struct pcap_sf_pkthdr hdr;
|
61 | 1abecf77 | Mark McLoughlin | int64_t ts; |
62 | 1abecf77 | Mark McLoughlin | int caplen;
|
63 | 1abecf77 | Mark McLoughlin | |
64 | 1abecf77 | Mark McLoughlin | /* Early return in case of previous error. */
|
65 | 1abecf77 | Mark McLoughlin | if (s->fd < 0) { |
66 | 1abecf77 | Mark McLoughlin | return size;
|
67 | 1abecf77 | Mark McLoughlin | } |
68 | 1abecf77 | Mark McLoughlin | |
69 | 1abecf77 | Mark McLoughlin | ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec());
|
70 | 1abecf77 | Mark McLoughlin | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
71 | 1abecf77 | Mark McLoughlin | |
72 | 1abecf77 | Mark McLoughlin | hdr.ts.tv_sec = ts / 1000000;
|
73 | 1abecf77 | Mark McLoughlin | hdr.ts.tv_usec = ts % 1000000;
|
74 | 1abecf77 | Mark McLoughlin | hdr.caplen = caplen; |
75 | 1abecf77 | Mark McLoughlin | hdr.len = size; |
76 | 1abecf77 | Mark McLoughlin | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || |
77 | 1abecf77 | Mark McLoughlin | write(s->fd, buf, caplen) != caplen) { |
78 | 1abecf77 | Mark McLoughlin | qemu_log("-net dump write error - stop dump\n");
|
79 | 1abecf77 | Mark McLoughlin | close(s->fd); |
80 | 1abecf77 | Mark McLoughlin | s->fd = -1;
|
81 | 1abecf77 | Mark McLoughlin | } |
82 | 1abecf77 | Mark McLoughlin | |
83 | 1abecf77 | Mark McLoughlin | return size;
|
84 | 1abecf77 | Mark McLoughlin | } |
85 | 1abecf77 | Mark McLoughlin | |
86 | 731d5856 | Mark McLoughlin | static void dump_cleanup(VLANClientState *nc) |
87 | 1abecf77 | Mark McLoughlin | { |
88 | 731d5856 | Mark McLoughlin | DumpState *s = DO_UPCAST(DumpState, nc, nc); |
89 | 1abecf77 | Mark McLoughlin | |
90 | 1abecf77 | Mark McLoughlin | close(s->fd); |
91 | 1abecf77 | Mark McLoughlin | } |
92 | 1abecf77 | Mark McLoughlin | |
93 | 731d5856 | Mark McLoughlin | static NetClientInfo net_dump_info = {
|
94 | 731d5856 | Mark McLoughlin | .type = NET_CLIENT_TYPE_DUMP, |
95 | 731d5856 | Mark McLoughlin | .size = sizeof(DumpState),
|
96 | 731d5856 | Mark McLoughlin | .receive = dump_receive, |
97 | 731d5856 | Mark McLoughlin | .cleanup = dump_cleanup, |
98 | 731d5856 | Mark McLoughlin | }; |
99 | 731d5856 | Mark McLoughlin | |
100 | 1abecf77 | Mark McLoughlin | static int net_dump_init(VLANState *vlan, const char *device, |
101 | 1abecf77 | Mark McLoughlin | const char *name, const char *filename, int len) |
102 | 1abecf77 | Mark McLoughlin | { |
103 | 1abecf77 | Mark McLoughlin | struct pcap_file_hdr hdr;
|
104 | 731d5856 | Mark McLoughlin | VLANClientState *nc; |
105 | 1abecf77 | Mark McLoughlin | DumpState *s; |
106 | 731d5856 | Mark McLoughlin | int fd;
|
107 | 1abecf77 | Mark McLoughlin | |
108 | 731d5856 | Mark McLoughlin | fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
|
109 | 731d5856 | Mark McLoughlin | if (fd < 0) { |
110 | 1abecf77 | Mark McLoughlin | qemu_error("-net dump: can't open %s\n", filename);
|
111 | 1abecf77 | Mark McLoughlin | return -1; |
112 | 1abecf77 | Mark McLoughlin | } |
113 | 1abecf77 | Mark McLoughlin | |
114 | 1abecf77 | Mark McLoughlin | hdr.magic = PCAP_MAGIC; |
115 | 1abecf77 | Mark McLoughlin | hdr.version_major = 2;
|
116 | 1abecf77 | Mark McLoughlin | hdr.version_minor = 4;
|
117 | 1abecf77 | Mark McLoughlin | hdr.thiszone = 0;
|
118 | 1abecf77 | Mark McLoughlin | hdr.sigfigs = 0;
|
119 | 731d5856 | Mark McLoughlin | hdr.snaplen = len; |
120 | 1abecf77 | Mark McLoughlin | hdr.linktype = 1;
|
121 | 1abecf77 | Mark McLoughlin | |
122 | 731d5856 | Mark McLoughlin | if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
123 | 1abecf77 | Mark McLoughlin | qemu_error("-net dump write error: %s\n", strerror(errno));
|
124 | 731d5856 | Mark McLoughlin | close(fd); |
125 | 1abecf77 | Mark McLoughlin | return -1; |
126 | 1abecf77 | Mark McLoughlin | } |
127 | 1abecf77 | Mark McLoughlin | |
128 | 731d5856 | Mark McLoughlin | nc = qemu_new_net_client(&net_dump_info, vlan, NULL, device, name);
|
129 | 731d5856 | Mark McLoughlin | |
130 | 731d5856 | Mark McLoughlin | snprintf(nc->info_str, sizeof(nc->info_str),
|
131 | 1abecf77 | Mark McLoughlin | "dump to %s (len=%d)", filename, len);
|
132 | 731d5856 | Mark McLoughlin | |
133 | 731d5856 | Mark McLoughlin | s = DO_UPCAST(DumpState, nc, nc); |
134 | 731d5856 | Mark McLoughlin | |
135 | 731d5856 | Mark McLoughlin | s->fd = fd; |
136 | 731d5856 | Mark McLoughlin | s->pcap_caplen = len; |
137 | 731d5856 | Mark McLoughlin | |
138 | 1abecf77 | Mark McLoughlin | return 0; |
139 | 1abecf77 | Mark McLoughlin | } |
140 | 1abecf77 | Mark McLoughlin | |
141 | 1abecf77 | Mark McLoughlin | int net_init_dump(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan) |
142 | 1abecf77 | Mark McLoughlin | { |
143 | 1abecf77 | Mark McLoughlin | int len;
|
144 | 1abecf77 | Mark McLoughlin | const char *file; |
145 | 1abecf77 | Mark McLoughlin | char def_file[128]; |
146 | 1abecf77 | Mark McLoughlin | |
147 | 1abecf77 | Mark McLoughlin | assert(vlan); |
148 | 1abecf77 | Mark McLoughlin | |
149 | 1abecf77 | Mark McLoughlin | file = qemu_opt_get(opts, "file");
|
150 | 1abecf77 | Mark McLoughlin | if (!file) {
|
151 | 1abecf77 | Mark McLoughlin | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); |
152 | 1abecf77 | Mark McLoughlin | file = def_file; |
153 | 1abecf77 | Mark McLoughlin | } |
154 | 1abecf77 | Mark McLoughlin | |
155 | 1abecf77 | Mark McLoughlin | len = qemu_opt_get_size(opts, "len", 65536); |
156 | 1abecf77 | Mark McLoughlin | |
157 | 1abecf77 | Mark McLoughlin | return net_dump_init(vlan, "dump", name, file, len); |
158 | 1abecf77 | Mark McLoughlin | } |