xref: /openbmc/qemu/net/dump.c (revision 91bfcdb0)
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/log.h"
29 #include "qemu/timer.h"
30 #include "hub.h"
31 
32 typedef struct DumpState {
33     NetClientState nc;
34     int64_t start_ts;
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 
60 static ssize_t dump_receive(NetClientState *nc, const uint8_t *buf, size_t size)
61 {
62     DumpState *s = DO_UPCAST(DumpState, nc, nc);
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 
72     ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL);
73     caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
74 
75     hdr.ts.tv_sec = ts / 1000000 + s->start_ts;
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 
89 static void dump_cleanup(NetClientState *nc)
90 {
91     DumpState *s = DO_UPCAST(DumpState, nc, nc);
92 
93     close(s->fd);
94 }
95 
96 static NetClientInfo net_dump_info = {
97     .type = NET_CLIENT_OPTIONS_KIND_DUMP,
98     .size = sizeof(DumpState),
99     .receive = dump_receive,
100     .cleanup = dump_cleanup,
101 };
102 
103 static int net_dump_init(NetClientState *peer, const char *device,
104                          const char *name, const char *filename, int len,
105                          Error **errp)
106 {
107     struct pcap_file_hdr hdr;
108     NetClientState *nc;
109     DumpState *s;
110     struct tm tm;
111     int fd;
112 
113     fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
114     if (fd < 0) {
115         error_setg_errno(errp, errno, "-net dump: can't open %s", filename);
116         return -1;
117     }
118 
119     hdr.magic = PCAP_MAGIC;
120     hdr.version_major = 2;
121     hdr.version_minor = 4;
122     hdr.thiszone = 0;
123     hdr.sigfigs = 0;
124     hdr.snaplen = len;
125     hdr.linktype = 1;
126 
127     if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
128         error_setg_errno(errp, errno, "-net dump write error");
129         close(fd);
130         return -1;
131     }
132 
133     nc = qemu_new_net_client(&net_dump_info, peer, device, name);
134 
135     snprintf(nc->info_str, sizeof(nc->info_str),
136              "dump to %s (len=%d)", filename, len);
137 
138     s = DO_UPCAST(DumpState, nc, nc);
139 
140     s->fd = fd;
141     s->pcap_caplen = len;
142 
143     qemu_get_timedate(&tm, 0);
144     s->start_ts = mktime(&tm);
145 
146     return 0;
147 }
148 
149 int net_init_dump(const NetClientOptions *opts, const char *name,
150                   NetClientState *peer, Error **errp)
151 {
152     int len;
153     const char *file;
154     char def_file[128];
155     const NetdevDumpOptions *dump;
156 
157     assert(opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP);
158     dump = opts->dump;
159 
160     assert(peer);
161 
162     if (dump->has_file) {
163         file = dump->file;
164     } else {
165         int id;
166         int ret;
167 
168         ret = net_hub_id_for_client(peer, &id);
169         assert(ret == 0); /* peer must be on a hub */
170 
171         snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", id);
172         file = def_file;
173     }
174 
175     if (dump->has_len) {
176         if (dump->len > INT_MAX) {
177             error_setg(errp, "invalid length: %"PRIu64, dump->len);
178             return -1;
179         }
180         len = dump->len;
181     } else {
182         len = 65536;
183     }
184 
185     return net_dump_init(peer, "dump", name, file, len, errp);
186 }
187