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