]>
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 | ||
25 | #include "dump.h" | |
26 | #include "qemu-common.h" | |
2f792016 | 27 | #include "qemu-error.h" |
1abecf77 | 28 | #include "qemu-log.h" |
d8dfad9c | 29 | #include "qemu-timer.h" |
d33d93b2 | 30 | #include "hub.h" |
1abecf77 MM |
31 | |
32 | typedef struct DumpState { | |
731d5856 | 33 | VLANClientState nc; |
0fa29915 | 34 | int64_t start_ts; |
1abecf77 MM |
35 | int fd; |
36 | int pcap_caplen; | |
37 | } DumpState; | |
38 | ||
39 | #define PCAP_MAGIC 0xa1b2c3d4 | |
40 | ||
41 | struct pcap_file_hdr { | |
42 | uint32_t magic; | |
43 | uint16_t version_major; | |
44 | uint16_t version_minor; | |
45 | int32_t thiszone; | |
46 | uint32_t sigfigs; | |
47 | uint32_t snaplen; | |
48 | uint32_t linktype; | |
49 | }; | |
50 | ||
51 | struct pcap_sf_pkthdr { | |
52 | struct { | |
53 | int32_t tv_sec; | |
54 | int32_t tv_usec; | |
55 | } ts; | |
56 | uint32_t caplen; | |
57 | uint32_t len; | |
58 | }; | |
59 | ||
731d5856 | 60 | static ssize_t dump_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
1abecf77 | 61 | { |
731d5856 | 62 | DumpState *s = DO_UPCAST(DumpState, nc, nc); |
1abecf77 MM |
63 | struct pcap_sf_pkthdr hdr; |
64 | int64_t ts; | |
65 | int caplen; | |
66 | ||
67 | /* Early return in case of previous error. */ | |
68 | if (s->fd < 0) { | |
69 | return size; | |
70 | } | |
71 | ||
74475455 | 72 | ts = muldiv64(qemu_get_clock_ns(vm_clock), 1000000, get_ticks_per_sec()); |
1abecf77 MM |
73 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
74 | ||
0fa29915 | 75 | hdr.ts.tv_sec = ts / 1000000 + s->start_ts; |
1abecf77 MM |
76 | hdr.ts.tv_usec = ts % 1000000; |
77 | hdr.caplen = caplen; | |
78 | hdr.len = size; | |
79 | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || | |
80 | write(s->fd, buf, caplen) != caplen) { | |
81 | qemu_log("-net dump write error - stop dump\n"); | |
82 | close(s->fd); | |
83 | s->fd = -1; | |
84 | } | |
85 | ||
86 | return size; | |
87 | } | |
88 | ||
731d5856 | 89 | static void dump_cleanup(VLANClientState *nc) |
1abecf77 | 90 | { |
731d5856 | 91 | DumpState *s = DO_UPCAST(DumpState, nc, nc); |
1abecf77 MM |
92 | |
93 | close(s->fd); | |
1abecf77 MM |
94 | } |
95 | ||
731d5856 | 96 | static NetClientInfo net_dump_info = { |
2be64a68 | 97 | .type = NET_CLIENT_OPTIONS_KIND_DUMP, |
731d5856 MM |
98 | .size = sizeof(DumpState), |
99 | .receive = dump_receive, | |
100 | .cleanup = dump_cleanup, | |
101 | }; | |
102 | ||
d33d93b2 | 103 | static int net_dump_init(VLANClientState *peer, const char *device, |
1abecf77 MM |
104 | const char *name, const char *filename, int len) |
105 | { | |
106 | struct pcap_file_hdr hdr; | |
731d5856 | 107 | VLANClientState *nc; |
1abecf77 | 108 | DumpState *s; |
0fa29915 | 109 | struct tm tm; |
731d5856 | 110 | int fd; |
1abecf77 | 111 | |
6514ed52 | 112 | fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644); |
731d5856 | 113 | if (fd < 0) { |
1ecda02b | 114 | error_report("-net dump: can't open %s", filename); |
1abecf77 MM |
115 | return -1; |
116 | } | |
117 | ||
1abecf77 MM |
118 | hdr.magic = PCAP_MAGIC; |
119 | hdr.version_major = 2; | |
120 | hdr.version_minor = 4; | |
121 | hdr.thiszone = 0; | |
122 | hdr.sigfigs = 0; | |
731d5856 | 123 | hdr.snaplen = len; |
1abecf77 MM |
124 | hdr.linktype = 1; |
125 | ||
731d5856 | 126 | if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
1ecda02b | 127 | error_report("-net dump write error: %s", strerror(errno)); |
731d5856 | 128 | close(fd); |
1abecf77 MM |
129 | return -1; |
130 | } | |
131 | ||
d33d93b2 | 132 | nc = qemu_new_net_client(&net_dump_info, NULL, peer, device, name); |
731d5856 MM |
133 | |
134 | snprintf(nc->info_str, sizeof(nc->info_str), | |
1abecf77 | 135 | "dump to %s (len=%d)", filename, len); |
731d5856 MM |
136 | |
137 | s = DO_UPCAST(DumpState, nc, nc); | |
138 | ||
139 | s->fd = fd; | |
140 | s->pcap_caplen = len; | |
141 | ||
0fa29915 HP |
142 | qemu_get_timedate(&tm, 0); |
143 | s->start_ts = mktime(&tm); | |
144 | ||
1abecf77 MM |
145 | return 0; |
146 | } | |
147 | ||
1a0c0958 | 148 | int net_init_dump(const NetClientOptions *opts, const char *name, |
d33d93b2 | 149 | VLANClientState *peer) |
1abecf77 MM |
150 | { |
151 | int len; | |
152 | const char *file; | |
153 | char def_file[128]; | |
848040d1 LE |
154 | const NetdevDumpOptions *dump; |
155 | ||
156 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP); | |
157 | dump = opts->dump; | |
1abecf77 | 158 | |
d33d93b2 | 159 | assert(peer); |
1abecf77 | 160 | |
848040d1 LE |
161 | if (dump->has_file) { |
162 | file = dump->file; | |
163 | } else { | |
d33d93b2 SH |
164 | int id; |
165 | int ret; | |
166 | ||
167 | ret = net_hub_id_for_client(peer, &id); | |
168 | assert(ret == 0); /* peer must be on a hub */ | |
169 | ||
170 | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", id); | |
1abecf77 MM |
171 | file = def_file; |
172 | } | |
173 | ||
848040d1 LE |
174 | if (dump->has_len) { |
175 | if (dump->len > INT_MAX) { | |
176 | error_report("invalid length: %"PRIu64, dump->len); | |
177 | return -1; | |
178 | } | |
179 | len = dump->len; | |
180 | } else { | |
181 | len = 65536; | |
182 | } | |
1abecf77 | 183 | |
d33d93b2 | 184 | return net_dump_init(peer, "dump", name, file, len); |
1abecf77 | 185 | } |