4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 #include "qemu-common.h"
27 #include "qemu-error.h"
29 #include "qemu-timer.h"
31 typedef struct DumpState
{
38 #define PCAP_MAGIC 0xa1b2c3d4
40 struct pcap_file_hdr
{
42 uint16_t version_major
;
43 uint16_t version_minor
;
50 struct pcap_sf_pkthdr
{
59 static ssize_t
dump_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
61 DumpState
*s
= DO_UPCAST(DumpState
, nc
, nc
);
62 struct pcap_sf_pkthdr hdr
;
66 /* Early return in case of previous error. */
71 ts
= muldiv64(qemu_get_clock_ns(vm_clock
), 1000000, get_ticks_per_sec());
72 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
74 hdr
.ts
.tv_sec
= ts
/ 1000000 + s
->start_ts
;
75 hdr
.ts
.tv_usec
= ts
% 1000000;
78 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
79 write(s
->fd
, buf
, caplen
) != caplen
) {
80 qemu_log("-net dump write error - stop dump\n");
88 static void dump_cleanup(VLANClientState
*nc
)
90 DumpState
*s
= DO_UPCAST(DumpState
, nc
, nc
);
95 static NetClientInfo net_dump_info
= {
96 .type
= NET_CLIENT_TYPE_DUMP
,
97 .size
= sizeof(DumpState
),
98 .receive
= dump_receive
,
99 .cleanup
= dump_cleanup
,
102 static int net_dump_init(VLANState
*vlan
, const char *device
,
103 const char *name
, const char *filename
, int len
)
105 struct pcap_file_hdr hdr
;
111 fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
| O_BINARY
, 0644);
113 error_report("-net dump: can't open %s", filename
);
117 hdr
.magic
= PCAP_MAGIC
;
118 hdr
.version_major
= 2;
119 hdr
.version_minor
= 4;
125 if (write(fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
126 error_report("-net dump write error: %s", strerror(errno
));
131 nc
= qemu_new_net_client(&net_dump_info
, vlan
, NULL
, device
, name
);
133 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
134 "dump to %s (len=%d)", filename
, len
);
136 s
= DO_UPCAST(DumpState
, nc
, nc
);
139 s
->pcap_caplen
= len
;
141 qemu_get_timedate(&tm
, 0);
142 s
->start_ts
= mktime(&tm
);
147 int net_init_dump(QemuOpts
*opts
, const char *name
, VLANState
*vlan
)
155 file
= qemu_opt_get(opts
, "file");
157 snprintf(def_file
, sizeof(def_file
), "qemu-vlan%d.pcap", vlan
->id
);
161 len
= qemu_opt_get_size(opts
, "len", 65536);
163 return net_dump_init(vlan
, "dump", name
, file
, len
);