nvme: use TYPE_NVME instead of constant string
[qemu/ar7.git] / chardev / char-udp.c
blob097a2f0f42d0f9c1bf6c56b35a1ddace883e5f8f
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "chardev/char.h"
27 #include "io/channel-socket.h"
28 #include "qapi/error.h"
29 #include "qemu/option.h"
31 #include "chardev/char-io.h"
33 /***********************************************************/
34 /* UDP Net console */
36 typedef struct {
37 Chardev parent;
38 QIOChannel *ioc;
39 uint8_t buf[CHR_READ_BUF_LEN];
40 int bufcnt;
41 int bufptr;
42 int max_size;
43 } UdpChardev;
45 #define UDP_CHARDEV(obj) OBJECT_CHECK(UdpChardev, (obj), TYPE_CHARDEV_UDP)
47 /* Called with chr_write_lock held. */
48 static int udp_chr_write(Chardev *chr, const uint8_t *buf, int len)
50 UdpChardev *s = UDP_CHARDEV(chr);
52 return qio_channel_write(
53 s->ioc, (const char *)buf, len, NULL);
56 static void udp_chr_flush_buffer(UdpChardev *s)
58 Chardev *chr = CHARDEV(s);
60 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
61 int n = MIN(s->max_size, s->bufcnt - s->bufptr);
62 qemu_chr_be_write(chr, &s->buf[s->bufptr], n);
63 s->bufptr += n;
64 s->max_size = qemu_chr_be_can_write(chr);
68 static int udp_chr_read_poll(void *opaque)
70 Chardev *chr = CHARDEV(opaque);
71 UdpChardev *s = UDP_CHARDEV(opaque);
73 s->max_size = qemu_chr_be_can_write(chr);
75 /* If there were any stray characters in the queue process them
76 * first
78 udp_chr_flush_buffer(s);
80 return s->max_size;
83 static gboolean udp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
85 Chardev *chr = CHARDEV(opaque);
86 UdpChardev *s = UDP_CHARDEV(opaque);
87 ssize_t ret;
89 if (s->max_size == 0) {
90 return TRUE;
92 ret = qio_channel_read(
93 s->ioc, (char *)s->buf, sizeof(s->buf), NULL);
94 if (ret <= 0) {
95 remove_fd_in_watch(chr);
96 return FALSE;
98 s->bufcnt = ret;
99 s->bufptr = 0;
100 udp_chr_flush_buffer(s);
102 return TRUE;
105 static void udp_chr_update_read_handler(Chardev *chr)
107 UdpChardev *s = UDP_CHARDEV(chr);
109 remove_fd_in_watch(chr);
110 if (s->ioc) {
111 chr->gsource = io_add_watch_poll(chr, s->ioc,
112 udp_chr_read_poll,
113 udp_chr_read, chr,
114 chr->gcontext);
118 static void char_udp_finalize(Object *obj)
120 Chardev *chr = CHARDEV(obj);
121 UdpChardev *s = UDP_CHARDEV(obj);
123 remove_fd_in_watch(chr);
124 if (s->ioc) {
125 object_unref(OBJECT(s->ioc));
127 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
130 static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
131 Error **errp)
133 const char *host = qemu_opt_get(opts, "host");
134 const char *port = qemu_opt_get(opts, "port");
135 const char *localaddr = qemu_opt_get(opts, "localaddr");
136 const char *localport = qemu_opt_get(opts, "localport");
137 bool has_local = false;
138 SocketAddressLegacy *addr;
139 ChardevUdp *udp;
141 backend->type = CHARDEV_BACKEND_KIND_UDP;
142 if (host == NULL || strlen(host) == 0) {
143 host = "localhost";
145 if (port == NULL || strlen(port) == 0) {
146 error_setg(errp, "chardev: udp: remote port not specified");
147 return;
149 if (localport == NULL || strlen(localport) == 0) {
150 localport = "0";
151 } else {
152 has_local = true;
154 if (localaddr == NULL || strlen(localaddr) == 0) {
155 localaddr = "";
156 } else {
157 has_local = true;
160 udp = backend->u.udp.data = g_new0(ChardevUdp, 1);
161 qemu_chr_parse_common(opts, qapi_ChardevUdp_base(udp));
163 addr = g_new0(SocketAddressLegacy, 1);
164 addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
165 addr->u.inet.data = g_new(InetSocketAddress, 1);
166 *addr->u.inet.data = (InetSocketAddress) {
167 .host = g_strdup(host),
168 .port = g_strdup(port),
169 .has_ipv4 = qemu_opt_get(opts, "ipv4"),
170 .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
171 .has_ipv6 = qemu_opt_get(opts, "ipv6"),
172 .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
174 udp->remote = addr;
176 if (has_local) {
177 udp->has_local = true;
178 addr = g_new0(SocketAddressLegacy, 1);
179 addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
180 addr->u.inet.data = g_new(InetSocketAddress, 1);
181 *addr->u.inet.data = (InetSocketAddress) {
182 .host = g_strdup(localaddr),
183 .port = g_strdup(localport),
185 udp->local = addr;
189 static void qmp_chardev_open_udp(Chardev *chr,
190 ChardevBackend *backend,
191 bool *be_opened,
192 Error **errp)
194 ChardevUdp *udp = backend->u.udp.data;
195 SocketAddress *local_addr = socket_address_flatten(udp->local);
196 SocketAddress *remote_addr = socket_address_flatten(udp->remote);
197 QIOChannelSocket *sioc = qio_channel_socket_new();
198 char *name;
199 UdpChardev *s = UDP_CHARDEV(chr);
200 int ret;
202 ret = qio_channel_socket_dgram_sync(sioc, local_addr, remote_addr, errp);
203 qapi_free_SocketAddress(local_addr);
204 qapi_free_SocketAddress(remote_addr);
205 if (ret < 0) {
206 object_unref(OBJECT(sioc));
207 return;
210 name = g_strdup_printf("chardev-udp-%s", chr->label);
211 qio_channel_set_name(QIO_CHANNEL(sioc), name);
212 g_free(name);
214 s->ioc = QIO_CHANNEL(sioc);
215 /* be isn't opened until we get a connection */
216 *be_opened = false;
219 static void char_udp_class_init(ObjectClass *oc, void *data)
221 ChardevClass *cc = CHARDEV_CLASS(oc);
223 cc->parse = qemu_chr_parse_udp;
224 cc->open = qmp_chardev_open_udp;
225 cc->chr_write = udp_chr_write;
226 cc->chr_update_read_handler = udp_chr_update_read_handler;
229 static const TypeInfo char_udp_type_info = {
230 .name = TYPE_CHARDEV_UDP,
231 .parent = TYPE_CHARDEV,
232 .instance_size = sizeof(UdpChardev),
233 .instance_finalize = char_udp_finalize,
234 .class_init = char_udp_class_init,
237 static void register_types(void)
239 type_register_static(&char_udp_type_info);
242 type_init(register_types);