blob: 004231d48186625b21aa35a223cd354c01746d9d [file] [log] [blame]
Mark McLoughlin1abecf72009-11-25 18:48:57 +00001/*
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"
Markus Armbruster2f792012010-02-18 16:24:31 +010027#include "qemu-error.h"
Mark McLoughlin1abecf72009-11-25 18:48:57 +000028#include "qemu-log.h"
Blue Swirld8dfad92011-03-27 14:31:31 +000029#include "qemu-timer.h"
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +010030#include "hub.h"
Mark McLoughlin1abecf72009-11-25 18:48:57 +000031
32typedef struct DumpState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010033 NetClientState nc;
Hervé Poussineau0fa29912011-11-30 21:35:38 +010034 int64_t start_ts;
Mark McLoughlin1abecf72009-11-25 18:48:57 +000035 int fd;
36 int pcap_caplen;
37} DumpState;
38
39#define PCAP_MAGIC 0xa1b2c3d4
40
41struct 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
51struct 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
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010060static ssize_t dump_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin1abecf72009-11-25 18:48:57 +000061{
Mark McLoughlin731d5852009-11-25 18:49:09 +000062 DumpState *s = DO_UPCAST(DumpState, nc, nc);
Mark McLoughlin1abecf72009-11-25 18:48:57 +000063 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
Paolo Bonzini74475452011-03-11 16:47:48 +010072 ts = muldiv64(qemu_get_clock_ns(vm_clock), 1000000, get_ticks_per_sec());
Mark McLoughlin1abecf72009-11-25 18:48:57 +000073 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
74
Hervé Poussineau0fa29912011-11-30 21:35:38 +010075 hdr.ts.tv_sec = ts / 1000000 + s->start_ts;
Mark McLoughlin1abecf72009-11-25 18:48:57 +000076 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
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010089static void dump_cleanup(NetClientState *nc)
Mark McLoughlin1abecf72009-11-25 18:48:57 +000090{
Mark McLoughlin731d5852009-11-25 18:49:09 +000091 DumpState *s = DO_UPCAST(DumpState, nc, nc);
Mark McLoughlin1abecf72009-11-25 18:48:57 +000092
93 close(s->fd);
Mark McLoughlin1abecf72009-11-25 18:48:57 +000094}
95
Mark McLoughlin731d5852009-11-25 18:49:09 +000096static NetClientInfo net_dump_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +020097 .type = NET_CLIENT_OPTIONS_KIND_DUMP,
Mark McLoughlin731d5852009-11-25 18:49:09 +000098 .size = sizeof(DumpState),
99 .receive = dump_receive,
100 .cleanup = dump_cleanup,
101};
102
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100103static int net_dump_init(NetClientState *peer, const char *device,
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000104 const char *name, const char *filename, int len)
105{
106 struct pcap_file_hdr hdr;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100107 NetClientState *nc;
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000108 DumpState *s;
Hervé Poussineau0fa29912011-11-30 21:35:38 +0100109 struct tm tm;
Mark McLoughlin731d5852009-11-25 18:49:09 +0000110 int fd;
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000111
Hervé Poussineau6514ed52011-11-30 21:35:37 +0100112 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
Mark McLoughlin731d5852009-11-25 18:49:09 +0000113 if (fd < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100114 error_report("-net dump: can't open %s", filename);
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000115 return -1;
116 }
117
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000118 hdr.magic = PCAP_MAGIC;
119 hdr.version_major = 2;
120 hdr.version_minor = 4;
121 hdr.thiszone = 0;
122 hdr.sigfigs = 0;
Mark McLoughlin731d5852009-11-25 18:49:09 +0000123 hdr.snaplen = len;
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000124 hdr.linktype = 1;
125
Mark McLoughlin731d5852009-11-25 18:49:09 +0000126 if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100127 error_report("-net dump write error: %s", strerror(errno));
Mark McLoughlin731d5852009-11-25 18:49:09 +0000128 close(fd);
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000129 return -1;
130 }
131
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100132 nc = qemu_new_net_client(&net_dump_info, peer, device, name);
Mark McLoughlin731d5852009-11-25 18:49:09 +0000133
134 snprintf(nc->info_str, sizeof(nc->info_str),
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000135 "dump to %s (len=%d)", filename, len);
Mark McLoughlin731d5852009-11-25 18:49:09 +0000136
137 s = DO_UPCAST(DumpState, nc, nc);
138
139 s->fd = fd;
140 s->pcap_caplen = len;
141
Hervé Poussineau0fa29912011-11-30 21:35:38 +0100142 qemu_get_timedate(&tm, 0);
143 s->start_ts = mktime(&tm);
144
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000145 return 0;
146}
147
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200148int net_init_dump(const NetClientOptions *opts, const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100149 NetClientState *peer)
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000150{
151 int len;
152 const char *file;
153 char def_file[128];
Laszlo Ersek848040d2012-07-17 16:17:15 +0200154 const NetdevDumpOptions *dump;
155
156 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP);
157 dump = opts->dump;
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000158
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100159 assert(peer);
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000160
Laszlo Ersek848040d2012-07-17 16:17:15 +0200161 if (dump->has_file) {
162 file = dump->file;
163 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100164 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);
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000171 file = def_file;
172 }
173
Laszlo Ersek848040d2012-07-17 16:17:15 +0200174 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 }
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000183
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100184 return net_dump_init(peer, "dump", name, file, len);
Mark McLoughlin1abecf72009-11-25 18:48:57 +0000185}