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