Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/chardev/char-udp.c
Line
Count
Source
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 "qemu/osdep.h"
26
#include "chardev/char.h"
27
#include "io/channel-socket.h"
28
#include "qapi/error.h"
29
#include "qemu/module.h"
30
#include "qemu/option.h"
31
32
#include "chardev/char-io.h"
33
#include "qom/object.h"
34
35
/***********************************************************/
36
/* UDP Net console */
37
38
struct UdpChardev {
39
    Chardev parent;
40
    QIOChannel *ioc;
41
    uint8_t buf[CHR_READ_BUF_LEN];
42
    int bufcnt;
43
    int bufptr;
44
    int max_size;
45
};
46
typedef struct UdpChardev UdpChardev;
47
48
DECLARE_INSTANCE_CHECKER(UdpChardev, UDP_CHARDEV,
49
                         TYPE_CHARDEV_UDP)
50
51
/* Called with chr_write_lock held.  */
52
static int udp_chr_write(Chardev *chr, const uint8_t *buf, int len)
53
0
{
54
0
    UdpChardev *s = UDP_CHARDEV(chr);
55
56
0
    return qio_channel_write(
57
0
        s->ioc, (const char *)buf, len, NULL);
58
0
}
59
60
static void udp_chr_flush_buffer(UdpChardev *s)
61
0
{
62
0
    Chardev *chr = CHARDEV(s);
63
64
0
    while (s->max_size > 0 && s->bufptr < s->bufcnt) {
65
0
        int n = MIN(s->max_size, s->bufcnt - s->bufptr);
66
0
        qemu_chr_be_write(chr, &s->buf[s->bufptr], n);
67
0
        s->bufptr += n;
68
0
        s->max_size = qemu_chr_be_can_write(chr);
69
0
    }
70
0
}
71
72
static int udp_chr_read_poll(void *opaque)
73
0
{
74
0
    Chardev *chr = CHARDEV(opaque);
75
0
    UdpChardev *s = UDP_CHARDEV(opaque);
76
77
0
    s->max_size = qemu_chr_be_can_write(chr);
78
79
    /* If there were any stray characters in the queue process them
80
     * first
81
     */
82
0
    udp_chr_flush_buffer(s);
83
84
    /*
85
     * udp_chr_flush_buffer() loops while max_size > 0 && bufptr < bufcnt,
86
     * so when pending data remains it must be because max_size hit 0.
87
     */
88
0
    return s->max_size;
89
0
}
90
91
static gboolean udp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
92
0
{
93
0
    Chardev *chr = CHARDEV(opaque);
94
0
    UdpChardev *s = UDP_CHARDEV(opaque);
95
0
    ssize_t ret;
96
97
0
    if (s->max_size == 0) {
98
0
        return TRUE;
99
0
    }
100
0
    ret = qio_channel_read(
101
0
        s->ioc, (char *)s->buf, sizeof(s->buf), NULL);
102
0
    if (ret <= 0) {
103
0
        remove_fd_in_watch(chr);
104
0
        return FALSE;
105
0
    }
106
0
    s->bufcnt = ret;
107
0
    s->bufptr = 0;
108
0
    udp_chr_flush_buffer(s);
109
110
0
    return TRUE;
111
0
}
112
113
static void udp_chr_update_read_handler(Chardev *chr)
114
0
{
115
0
    UdpChardev *s = UDP_CHARDEV(chr);
116
117
0
    remove_fd_in_watch(chr);
118
0
    if (s->ioc) {
119
0
        chr->gsource = io_add_watch_poll(chr, s->ioc,
120
0
                                           udp_chr_read_poll,
121
0
                                           udp_chr_read, chr,
122
0
                                           chr->gcontext);
123
0
    }
124
0
}
125
126
static void char_udp_finalize(Object *obj)
127
0
{
128
0
    Chardev *chr = CHARDEV(obj);
129
0
    UdpChardev *s = UDP_CHARDEV(obj);
130
131
0
    remove_fd_in_watch(chr);
132
0
    if (s->ioc) {
133
0
        object_unref(OBJECT(s->ioc));
134
0
    }
135
0
    qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
136
0
}
137
138
static void udp_chr_parse(QemuOpts *opts, ChardevBackend *backend, Error **errp)
139
0
{
140
0
    const char *host = qemu_opt_get(opts, "host");
141
0
    const char *port = qemu_opt_get(opts, "port");
142
0
    const char *localaddr = qemu_opt_get(opts, "localaddr");
143
0
    const char *localport = qemu_opt_get(opts, "localport");
144
0
    bool has_local = false;
145
0
    SocketAddressLegacy *addr;
146
0
    ChardevUdp *udp;
147
148
0
    backend->type = CHARDEV_BACKEND_KIND_UDP;
149
0
    if (host == NULL || strlen(host) == 0) {
150
0
        host = "localhost";
151
0
    }
152
0
    if (port == NULL || strlen(port) == 0) {
153
0
        error_setg(errp, "chardev: udp: remote port not specified");
154
0
        return;
155
0
    }
156
0
    if (localport == NULL || strlen(localport) == 0) {
157
0
        localport = "0";
158
0
    } else {
159
0
        has_local = true;
160
0
    }
161
0
    if (localaddr == NULL || strlen(localaddr) == 0) {
162
0
        localaddr = "";
163
0
    } else {
164
0
        has_local = true;
165
0
    }
166
167
0
    udp = backend->u.udp.data = g_new0(ChardevUdp, 1);
168
0
    qemu_chr_parse_common(opts, qapi_ChardevUdp_base(udp));
169
170
0
    addr = g_new0(SocketAddressLegacy, 1);
171
0
    addr->type = SOCKET_ADDRESS_TYPE_INET;
172
0
    addr->u.inet.data = g_new(InetSocketAddress, 1);
173
0
    *addr->u.inet.data = (InetSocketAddress) {
174
0
        .host = g_strdup(host),
175
0
        .port = g_strdup(port),
176
0
        .has_ipv4 = qemu_opt_get(opts, "ipv4"),
177
0
        .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
178
0
        .has_ipv6 = qemu_opt_get(opts, "ipv6"),
179
0
        .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
180
0
    };
181
0
    udp->remote = addr;
182
183
0
    if (has_local) {
184
0
        addr = g_new0(SocketAddressLegacy, 1);
185
0
        addr->type = SOCKET_ADDRESS_TYPE_INET;
186
0
        addr->u.inet.data = g_new(InetSocketAddress, 1);
187
0
        *addr->u.inet.data = (InetSocketAddress) {
188
0
            .host = g_strdup(localaddr),
189
0
            .port = g_strdup(localport),
190
0
        };
191
0
        udp->local = addr;
192
0
    }
193
0
}
194
195
static bool upd_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp)
196
0
{
197
0
    ChardevUdp *udp = backend->u.udp.data;
198
0
    SocketAddress *local_addr = socket_address_flatten(udp->local);
199
0
    SocketAddress *remote_addr = socket_address_flatten(udp->remote);
200
0
    QIOChannelSocket *sioc = qio_channel_socket_new();
201
0
    char *name;
202
0
    UdpChardev *s = UDP_CHARDEV(chr);
203
0
    int ret;
204
205
0
    ret = qio_channel_socket_dgram_sync(sioc, local_addr, remote_addr, errp);
206
0
    qapi_free_SocketAddress(local_addr);
207
0
    qapi_free_SocketAddress(remote_addr);
208
0
    if (ret < 0) {
209
0
        object_unref(OBJECT(sioc));
210
0
        return false;
211
0
    }
212
213
0
    name = g_strdup_printf("chardev-udp-%s", chr->label);
214
0
    qio_channel_set_name(QIO_CHANNEL(sioc), name);
215
0
    g_free(name);
216
217
0
    s->ioc = QIO_CHANNEL(sioc);
218
0
    qemu_chr_be_event(chr, CHR_EVENT_OPENED);
219
0
    return true;
220
0
}
221
222
static void char_udp_class_init(ObjectClass *oc, const void *data)
223
61
{
224
61
    ChardevClass *cc = CHARDEV_CLASS(oc);
225
226
61
    cc->chr_parse = udp_chr_parse;
227
61
    cc->chr_open = upd_chr_open;
228
61
    cc->chr_write = udp_chr_write;
229
61
    cc->chr_update_read_handler = udp_chr_update_read_handler;
230
61
}
231
232
static const TypeInfo char_udp_type_info = {
233
    .name = TYPE_CHARDEV_UDP,
234
    .parent = TYPE_CHARDEV,
235
    .instance_size = sizeof(UdpChardev),
236
    .instance_finalize = char_udp_finalize,
237
    .class_init = char_udp_class_init,
238
};
239
240
static void register_types(void)
241
61
{
242
61
    type_register_static(&char_udp_type_info);
243
61
}
244
245
type_init(register_types);