root / net / dump.c @ a9899996
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 | 2f792016 | Markus Armbruster | #include "qemu-error.h" |
28 | 1abecf77 | Mark McLoughlin | #include "qemu-log.h" |
29 | d8dfad9c | Blue Swirl | #include "qemu-timer.h" |
30 | 1abecf77 | Mark McLoughlin | |
31 | 1abecf77 | Mark McLoughlin | typedef struct DumpState { |
32 | 731d5856 | Mark McLoughlin | VLANClientState nc; |
33 | 1abecf77 | Mark McLoughlin | int fd;
|
34 | 1abecf77 | Mark McLoughlin | int pcap_caplen;
|
35 | 1abecf77 | Mark McLoughlin | } DumpState; |
36 | 1abecf77 | Mark McLoughlin | |
37 | 1abecf77 | Mark McLoughlin | #define PCAP_MAGIC 0xa1b2c3d4 |
38 | 1abecf77 | Mark McLoughlin | |
39 | 1abecf77 | Mark McLoughlin | struct pcap_file_hdr {
|
40 | 1abecf77 | Mark McLoughlin | uint32_t magic; |
41 | 1abecf77 | Mark McLoughlin | uint16_t version_major; |
42 | 1abecf77 | Mark McLoughlin | uint16_t version_minor; |
43 | 1abecf77 | Mark McLoughlin | int32_t thiszone; |
44 | 1abecf77 | Mark McLoughlin | uint32_t sigfigs; |
45 | 1abecf77 | Mark McLoughlin | uint32_t snaplen; |
46 | 1abecf77 | Mark McLoughlin | uint32_t linktype; |
47 | 1abecf77 | Mark McLoughlin | }; |
48 | 1abecf77 | Mark McLoughlin | |
49 | 1abecf77 | Mark McLoughlin | struct pcap_sf_pkthdr {
|
50 | 1abecf77 | Mark McLoughlin | struct {
|
51 | 1abecf77 | Mark McLoughlin | int32_t tv_sec; |
52 | 1abecf77 | Mark McLoughlin | int32_t tv_usec; |
53 | 1abecf77 | Mark McLoughlin | } ts; |
54 | 1abecf77 | Mark McLoughlin | uint32_t caplen; |
55 | 1abecf77 | Mark McLoughlin | uint32_t len; |
56 | 1abecf77 | Mark McLoughlin | }; |
57 | 1abecf77 | Mark McLoughlin | |
58 | 731d5856 | Mark McLoughlin | static ssize_t dump_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
59 | 1abecf77 | Mark McLoughlin | { |
60 | 731d5856 | Mark McLoughlin | DumpState *s = DO_UPCAST(DumpState, nc, nc); |
61 | 1abecf77 | Mark McLoughlin | struct pcap_sf_pkthdr hdr;
|
62 | 1abecf77 | Mark McLoughlin | int64_t ts; |
63 | 1abecf77 | Mark McLoughlin | int caplen;
|
64 | 1abecf77 | Mark McLoughlin | |
65 | 1abecf77 | Mark McLoughlin | /* Early return in case of previous error. */
|
66 | 1abecf77 | Mark McLoughlin | if (s->fd < 0) { |
67 | 1abecf77 | Mark McLoughlin | return size;
|
68 | 1abecf77 | Mark McLoughlin | } |
69 | 1abecf77 | Mark McLoughlin | |
70 | 74475455 | Paolo Bonzini | ts = muldiv64(qemu_get_clock_ns(vm_clock), 1000000, get_ticks_per_sec());
|
71 | 1abecf77 | Mark McLoughlin | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
72 | 1abecf77 | Mark McLoughlin | |
73 | 1abecf77 | Mark McLoughlin | hdr.ts.tv_sec = ts / 1000000;
|
74 | 1abecf77 | Mark McLoughlin | hdr.ts.tv_usec = ts % 1000000;
|
75 | 1abecf77 | Mark McLoughlin | hdr.caplen = caplen; |
76 | 1abecf77 | Mark McLoughlin | hdr.len = size; |
77 | 1abecf77 | Mark McLoughlin | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || |
78 | 1abecf77 | Mark McLoughlin | write(s->fd, buf, caplen) != caplen) { |
79 | 1abecf77 | Mark McLoughlin | qemu_log("-net dump write error - stop dump\n");
|
80 | 1abecf77 | Mark McLoughlin | close(s->fd); |
81 | 1abecf77 | Mark McLoughlin | s->fd = -1;
|
82 | 1abecf77 | Mark McLoughlin | } |
83 | 1abecf77 | Mark McLoughlin | |
84 | 1abecf77 | Mark McLoughlin | return size;
|
85 | 1abecf77 | Mark McLoughlin | } |
86 | 1abecf77 | Mark McLoughlin | |
87 | 731d5856 | Mark McLoughlin | static void dump_cleanup(VLANClientState *nc) |
88 | 1abecf77 | Mark McLoughlin | { |
89 | 731d5856 | Mark McLoughlin | DumpState *s = DO_UPCAST(DumpState, nc, nc); |
90 | 1abecf77 | Mark McLoughlin | |
91 | 1abecf77 | Mark McLoughlin | close(s->fd); |
92 | 1abecf77 | Mark McLoughlin | } |
93 | 1abecf77 | Mark McLoughlin | |
94 | 731d5856 | Mark McLoughlin | static NetClientInfo net_dump_info = {
|
95 | 731d5856 | Mark McLoughlin | .type = NET_CLIENT_TYPE_DUMP, |
96 | 731d5856 | Mark McLoughlin | .size = sizeof(DumpState),
|
97 | 731d5856 | Mark McLoughlin | .receive = dump_receive, |
98 | 731d5856 | Mark McLoughlin | .cleanup = dump_cleanup, |
99 | 731d5856 | Mark McLoughlin | }; |
100 | 731d5856 | Mark McLoughlin | |
101 | 1abecf77 | Mark McLoughlin | static int net_dump_init(VLANState *vlan, const char *device, |
102 | 1abecf77 | Mark McLoughlin | const char *name, const char *filename, int len) |
103 | 1abecf77 | Mark McLoughlin | { |
104 | 1abecf77 | Mark McLoughlin | struct pcap_file_hdr hdr;
|
105 | 731d5856 | Mark McLoughlin | VLANClientState *nc; |
106 | 1abecf77 | Mark McLoughlin | DumpState *s; |
107 | 731d5856 | Mark McLoughlin | int fd;
|
108 | 1abecf77 | Mark McLoughlin | |
109 | 731d5856 | Mark McLoughlin | fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
|
110 | 731d5856 | Mark McLoughlin | if (fd < 0) { |
111 | 1ecda02b | Markus Armbruster | error_report("-net dump: can't open %s", filename);
|
112 | 1abecf77 | Mark McLoughlin | return -1; |
113 | 1abecf77 | Mark McLoughlin | } |
114 | 1abecf77 | Mark McLoughlin | |
115 | 1abecf77 | Mark McLoughlin | hdr.magic = PCAP_MAGIC; |
116 | 1abecf77 | Mark McLoughlin | hdr.version_major = 2;
|
117 | 1abecf77 | Mark McLoughlin | hdr.version_minor = 4;
|
118 | 1abecf77 | Mark McLoughlin | hdr.thiszone = 0;
|
119 | 1abecf77 | Mark McLoughlin | hdr.sigfigs = 0;
|
120 | 731d5856 | Mark McLoughlin | hdr.snaplen = len; |
121 | 1abecf77 | Mark McLoughlin | hdr.linktype = 1;
|
122 | 1abecf77 | Mark McLoughlin | |
123 | 731d5856 | Mark McLoughlin | if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
124 | 1ecda02b | Markus Armbruster | error_report("-net dump write error: %s", strerror(errno));
|
125 | 731d5856 | Mark McLoughlin | close(fd); |
126 | 1abecf77 | Mark McLoughlin | return -1; |
127 | 1abecf77 | Mark McLoughlin | } |
128 | 1abecf77 | Mark McLoughlin | |
129 | 731d5856 | Mark McLoughlin | nc = qemu_new_net_client(&net_dump_info, vlan, NULL, device, name);
|
130 | 731d5856 | Mark McLoughlin | |
131 | 731d5856 | Mark McLoughlin | snprintf(nc->info_str, sizeof(nc->info_str),
|
132 | 1abecf77 | Mark McLoughlin | "dump to %s (len=%d)", filename, len);
|
133 | 731d5856 | Mark McLoughlin | |
134 | 731d5856 | Mark McLoughlin | s = DO_UPCAST(DumpState, nc, nc); |
135 | 731d5856 | Mark McLoughlin | |
136 | 731d5856 | Mark McLoughlin | s->fd = fd; |
137 | 731d5856 | Mark McLoughlin | s->pcap_caplen = len; |
138 | 731d5856 | Mark McLoughlin | |
139 | 1abecf77 | Mark McLoughlin | return 0; |
140 | 1abecf77 | Mark McLoughlin | } |
141 | 1abecf77 | Mark McLoughlin | |
142 | 1abecf77 | Mark McLoughlin | int net_init_dump(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan) |
143 | 1abecf77 | Mark McLoughlin | { |
144 | 1abecf77 | Mark McLoughlin | int len;
|
145 | 1abecf77 | Mark McLoughlin | const char *file; |
146 | 1abecf77 | Mark McLoughlin | char def_file[128]; |
147 | 1abecf77 | Mark McLoughlin | |
148 | 1abecf77 | Mark McLoughlin | assert(vlan); |
149 | 1abecf77 | Mark McLoughlin | |
150 | 1abecf77 | Mark McLoughlin | file = qemu_opt_get(opts, "file");
|
151 | 1abecf77 | Mark McLoughlin | if (!file) {
|
152 | 1abecf77 | Mark McLoughlin | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); |
153 | 1abecf77 | Mark McLoughlin | file = def_file; |
154 | 1abecf77 | Mark McLoughlin | } |
155 | 1abecf77 | Mark McLoughlin | |
156 | 1abecf77 | Mark McLoughlin | len = qemu_opt_get_size(opts, "len", 65536); |
157 | 1abecf77 | Mark McLoughlin | |
158 | 1abecf77 | Mark McLoughlin | return net_dump_init(vlan, "dump", name, file, len); |
159 | 1abecf77 | Mark McLoughlin | } |