xref: /qemu/chardev/char-udp.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
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 #define UDP_CHARDEV(obj) OBJECT_CHECK(UdpChardev, (obj), TYPE_CHARDEV_UDP)
49 
50 /* Called with chr_write_lock held.  */
51 static int udp_chr_write(Chardev *chr, const uint8_t *buf, int len)
52 {
53     UdpChardev *s = UDP_CHARDEV(chr);
54 
55     return qio_channel_write(
56         s->ioc, (const char *)buf, len, NULL);
57 }
58 
59 static void udp_chr_flush_buffer(UdpChardev *s)
60 {
61     Chardev *chr = CHARDEV(s);
62 
63     while (s->max_size > 0 && s->bufptr < s->bufcnt) {
64         int n = MIN(s->max_size, s->bufcnt - s->bufptr);
65         qemu_chr_be_write(chr, &s->buf[s->bufptr], n);
66         s->bufptr += n;
67         s->max_size = qemu_chr_be_can_write(chr);
68     }
69 }
70 
71 static int udp_chr_read_poll(void *opaque)
72 {
73     Chardev *chr = CHARDEV(opaque);
74     UdpChardev *s = UDP_CHARDEV(opaque);
75 
76     s->max_size = qemu_chr_be_can_write(chr);
77 
78     /* If there were any stray characters in the queue process them
79      * first
80      */
81     udp_chr_flush_buffer(s);
82 
83     return s->max_size;
84 }
85 
86 static gboolean udp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
87 {
88     Chardev *chr = CHARDEV(opaque);
89     UdpChardev *s = UDP_CHARDEV(opaque);
90     ssize_t ret;
91 
92     if (s->max_size == 0) {
93         return TRUE;
94     }
95     ret = qio_channel_read(
96         s->ioc, (char *)s->buf, sizeof(s->buf), NULL);
97     if (ret <= 0) {
98         remove_fd_in_watch(chr);
99         return FALSE;
100     }
101     s->bufcnt = ret;
102     s->bufptr = 0;
103     udp_chr_flush_buffer(s);
104 
105     return TRUE;
106 }
107 
108 static void udp_chr_update_read_handler(Chardev *chr)
109 {
110     UdpChardev *s = UDP_CHARDEV(chr);
111 
112     remove_fd_in_watch(chr);
113     if (s->ioc) {
114         chr->gsource = io_add_watch_poll(chr, s->ioc,
115                                            udp_chr_read_poll,
116                                            udp_chr_read, chr,
117                                            chr->gcontext);
118     }
119 }
120 
121 static void char_udp_finalize(Object *obj)
122 {
123     Chardev *chr = CHARDEV(obj);
124     UdpChardev *s = UDP_CHARDEV(obj);
125 
126     remove_fd_in_watch(chr);
127     if (s->ioc) {
128         object_unref(OBJECT(s->ioc));
129     }
130     qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
131 }
132 
133 static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
134                                Error **errp)
135 {
136     const char *host = qemu_opt_get(opts, "host");
137     const char *port = qemu_opt_get(opts, "port");
138     const char *localaddr = qemu_opt_get(opts, "localaddr");
139     const char *localport = qemu_opt_get(opts, "localport");
140     bool has_local = false;
141     SocketAddressLegacy *addr;
142     ChardevUdp *udp;
143 
144     backend->type = CHARDEV_BACKEND_KIND_UDP;
145     if (host == NULL || strlen(host) == 0) {
146         host = "localhost";
147     }
148     if (port == NULL || strlen(port) == 0) {
149         error_setg(errp, "chardev: udp: remote port not specified");
150         return;
151     }
152     if (localport == NULL || strlen(localport) == 0) {
153         localport = "0";
154     } else {
155         has_local = true;
156     }
157     if (localaddr == NULL || strlen(localaddr) == 0) {
158         localaddr = "";
159     } else {
160         has_local = true;
161     }
162 
163     udp = backend->u.udp.data = g_new0(ChardevUdp, 1);
164     qemu_chr_parse_common(opts, qapi_ChardevUdp_base(udp));
165 
166     addr = g_new0(SocketAddressLegacy, 1);
167     addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
168     addr->u.inet.data = g_new(InetSocketAddress, 1);
169     *addr->u.inet.data = (InetSocketAddress) {
170         .host = g_strdup(host),
171         .port = g_strdup(port),
172         .has_ipv4 = qemu_opt_get(opts, "ipv4"),
173         .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
174         .has_ipv6 = qemu_opt_get(opts, "ipv6"),
175         .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
176     };
177     udp->remote = addr;
178 
179     if (has_local) {
180         udp->has_local = true;
181         addr = g_new0(SocketAddressLegacy, 1);
182         addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
183         addr->u.inet.data = g_new(InetSocketAddress, 1);
184         *addr->u.inet.data = (InetSocketAddress) {
185             .host = g_strdup(localaddr),
186             .port = g_strdup(localport),
187         };
188         udp->local = addr;
189     }
190 }
191 
192 static void qmp_chardev_open_udp(Chardev *chr,
193                                  ChardevBackend *backend,
194                                  bool *be_opened,
195                                  Error **errp)
196 {
197     ChardevUdp *udp = backend->u.udp.data;
198     SocketAddress *local_addr = socket_address_flatten(udp->local);
199     SocketAddress *remote_addr = socket_address_flatten(udp->remote);
200     QIOChannelSocket *sioc = qio_channel_socket_new();
201     char *name;
202     UdpChardev *s = UDP_CHARDEV(chr);
203     int ret;
204 
205     ret = qio_channel_socket_dgram_sync(sioc, local_addr, remote_addr, errp);
206     qapi_free_SocketAddress(local_addr);
207     qapi_free_SocketAddress(remote_addr);
208     if (ret < 0) {
209         object_unref(OBJECT(sioc));
210         return;
211     }
212 
213     name = g_strdup_printf("chardev-udp-%s", chr->label);
214     qio_channel_set_name(QIO_CHANNEL(sioc), name);
215     g_free(name);
216 
217     s->ioc = QIO_CHANNEL(sioc);
218     /* be isn't opened until we get a connection */
219     *be_opened = false;
220 }
221 
222 static void char_udp_class_init(ObjectClass *oc, void *data)
223 {
224     ChardevClass *cc = CHARDEV_CLASS(oc);
225 
226     cc->parse = qemu_chr_parse_udp;
227     cc->open = qmp_chardev_open_udp;
228     cc->chr_write = udp_chr_write;
229     cc->chr_update_read_handler = udp_chr_update_read_handler;
230 }
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 {
242     type_register_static(&char_udp_type_info);
243 }
244 
245 type_init(register_types);
246