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