]> git.ipfire.org Git - thirdparty/qemu.git/blob - include/chardev/char.h
95273e10ae9fa0b7e2942b6a62d479e28e03ddb5
[thirdparty/qemu.git] / include / chardev / char.h
1 #ifndef QEMU_CHAR_H
2 #define QEMU_CHAR_H
3
4 #include "qemu-common.h"
5 #include "qemu/option.h"
6 #include "qemu/main-loop.h"
7 #include "qemu/bitmap.h"
8 #include "qom/object.h"
9
10 #define IAC_EOR 239
11 #define IAC_SE 240
12 #define IAC_NOP 241
13 #define IAC_BREAK 243
14 #define IAC_IP 244
15 #define IAC_SB 250
16 #define IAC 255
17
18 /* character device */
19
20 typedef enum {
21 CHR_EVENT_BREAK, /* serial break char */
22 CHR_EVENT_OPENED, /* new connection established */
23 CHR_EVENT_MUX_IN, /* mux-focus was set to this terminal */
24 CHR_EVENT_MUX_OUT, /* mux-focus will move on */
25 CHR_EVENT_CLOSED /* connection closed */
26 } QEMUChrEvent;
27
28 #define CHR_READ_BUF_LEN 4096
29
30 typedef void IOEventHandler(void *opaque, int event);
31
32 typedef enum {
33 /* Whether the chardev peer is able to close and
34 * reopen the data channel, thus requiring support
35 * for qemu_chr_wait_connected() to wait for a
36 * valid connection */
37 QEMU_CHAR_FEATURE_RECONNECTABLE,
38 /* Whether it is possible to send/recv file descriptors
39 * over the data channel */
40 QEMU_CHAR_FEATURE_FD_PASS,
41 /* Whether replay or record mode is enabled */
42 QEMU_CHAR_FEATURE_REPLAY,
43
44 QEMU_CHAR_FEATURE_LAST,
45 } ChardevFeature;
46
47 /* This is the backend as seen by frontend, the actual backend is
48 * Chardev */
49 typedef struct CharBackend {
50 Chardev *chr;
51 IOEventHandler *chr_event;
52 IOCanReadHandler *chr_can_read;
53 IOReadHandler *chr_read;
54 void *opaque;
55 int tag;
56 int fe_open;
57 } CharBackend;
58
59 struct Chardev {
60 Object parent_obj;
61
62 QemuMutex chr_write_lock;
63 CharBackend *be;
64 char *label;
65 char *filename;
66 int logfd;
67 int be_open;
68 GSource *gsource;
69 DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
70 };
71
72 /**
73 * @qemu_chr_new_from_opts:
74 *
75 * Create a new character backend from a QemuOpts list.
76 *
77 * @opts see qemu-config.c for a list of valid options
78 *
79 * Returns: a new character backend
80 */
81 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
82 Error **errp);
83
84 /**
85 * @qemu_chr_parse_common:
86 *
87 * Parse the common options available to all character backends.
88 *
89 * @opts the options that still need parsing
90 * @backend a new backend
91 */
92 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
93
94 /**
95 * @qemu_chr_new:
96 *
97 * Create a new character backend from a URI.
98 *
99 * @label the name of the backend
100 * @filename the URI
101 *
102 * Returns: a new character backend
103 */
104 Chardev *qemu_chr_new(const char *label, const char *filename);
105
106
107 /**
108 * @qemu_chr_fe_disconnect:
109 *
110 * Close a fd accpeted by character backend.
111 * Without associated Chardev, do nothing.
112 */
113 void qemu_chr_fe_disconnect(CharBackend *be);
114
115 /**
116 * @qemu_chr_cleanup:
117 *
118 * Delete all chardevs (when leaving qemu)
119 */
120 void qemu_chr_cleanup(void);
121
122 /**
123 * @qemu_chr_fe_wait_connected:
124 *
125 * Wait for characted backend to be connected, return < 0 on error or
126 * if no assicated Chardev.
127 */
128 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp);
129
130 /**
131 * @qemu_chr_new_noreplay:
132 *
133 * Create a new character backend from a URI.
134 * Character device communications are not written
135 * into the replay log.
136 *
137 * @label the name of the backend
138 * @filename the URI
139 *
140 * Returns: a new character backend
141 */
142 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename);
143
144 /**
145 * @qemu_chr_fe_set_echo:
146 *
147 * Ask the backend to override its normal echo setting. This only really
148 * applies to the stdio backend and is used by the QMP server such that you
149 * can see what you type if you try to type QMP commands.
150 * Without associated Chardev, do nothing.
151 *
152 * @echo true to enable echo, false to disable echo
153 */
154 void qemu_chr_fe_set_echo(CharBackend *be, bool echo);
155
156 /**
157 * @qemu_chr_fe_set_open:
158 *
159 * Set character frontend open status. This is an indication that the
160 * front end is ready (or not) to begin doing I/O.
161 * Without associated Chardev, do nothing.
162 */
163 void qemu_chr_fe_set_open(CharBackend *be, int fe_open);
164
165 /**
166 * @qemu_chr_fe_printf:
167 *
168 * Write to a character backend using a printf style interface. This
169 * function is thread-safe. It does nothing without associated
170 * Chardev.
171 *
172 * @fmt see #printf
173 */
174 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
175 GCC_FMT_ATTR(2, 3);
176
177 /**
178 * @qemu_chr_fe_add_watch:
179 *
180 * If the backend is connected, create and add a #GSource that fires
181 * when the given condition (typically G_IO_OUT|G_IO_HUP or G_IO_HUP)
182 * is active; return the #GSource's tag. If it is disconnected,
183 * or without associated Chardev, return 0.
184 *
185 * @cond the condition to poll for
186 * @func the function to call when the condition happens
187 * @user_data the opaque pointer to pass to @func
188 *
189 * Returns: the source tag
190 */
191 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
192 GIOFunc func, void *user_data);
193
194 /**
195 * @qemu_chr_fe_write:
196 *
197 * Write data to a character backend from the front end. This function
198 * will send data from the front end to the back end. This function
199 * is thread-safe.
200 *
201 * @buf the data
202 * @len the number of bytes to send
203 *
204 * Returns: the number of bytes consumed (0 if no assicated Chardev)
205 */
206 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
207
208 /**
209 * @qemu_chr_fe_write_all:
210 *
211 * Write data to a character backend from the front end. This function will
212 * send data from the front end to the back end. Unlike @qemu_chr_fe_write,
213 * this function will block if the back end cannot consume all of the data
214 * attempted to be written. This function is thread-safe.
215 *
216 * @buf the data
217 * @len the number of bytes to send
218 *
219 * Returns: the number of bytes consumed (0 if no assicated Chardev)
220 */
221 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
222
223 /**
224 * @qemu_chr_fe_read_all:
225 *
226 * Read data to a buffer from the back end.
227 *
228 * @buf the data buffer
229 * @len the number of bytes to read
230 *
231 * Returns: the number of bytes read (0 if no assicated Chardev)
232 */
233 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len);
234
235 /**
236 * @qemu_chr_fe_ioctl:
237 *
238 * Issue a device specific ioctl to a backend. This function is thread-safe.
239 *
240 * @cmd see CHR_IOCTL_*
241 * @arg the data associated with @cmd
242 *
243 * Returns: if @cmd is not supported by the backend or there is no
244 * associated Chardev, -ENOTSUP, otherwise the return
245 * value depends on the semantics of @cmd
246 */
247 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg);
248
249 /**
250 * @qemu_chr_fe_get_msgfd:
251 *
252 * For backends capable of fd passing, return the latest file descriptor passed
253 * by a client.
254 *
255 * Returns: -1 if fd passing isn't supported or there is no pending file
256 * descriptor. If a file descriptor is returned, subsequent calls to
257 * this function will return -1 until a client sends a new file
258 * descriptor.
259 */
260 int qemu_chr_fe_get_msgfd(CharBackend *be);
261
262 /**
263 * @qemu_chr_fe_get_msgfds:
264 *
265 * For backends capable of fd passing, return the number of file received
266 * descriptors and fills the fds array up to num elements
267 *
268 * Returns: -1 if fd passing isn't supported or there are no pending file
269 * descriptors. If file descriptors are returned, subsequent calls to
270 * this function will return -1 until a client sends a new set of file
271 * descriptors.
272 */
273 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int num);
274
275 /**
276 * @qemu_chr_fe_set_msgfds:
277 *
278 * For backends capable of fd passing, set an array of fds to be passed with
279 * the next send operation.
280 * A subsequent call to this function before calling a write function will
281 * result in overwriting the fd array with the new value without being send.
282 * Upon writing the message the fd array is freed.
283 *
284 * Returns: -1 if fd passing isn't supported or no associated Chardev.
285 */
286 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num);
287
288 /**
289 * @qemu_chr_be_can_write:
290 *
291 * Determine how much data the front end can currently accept. This function
292 * returns the number of bytes the front end can accept. If it returns 0, the
293 * front end cannot receive data at the moment. The function must be polled
294 * to determine when data can be received.
295 *
296 * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
297 */
298 int qemu_chr_be_can_write(Chardev *s);
299
300 /**
301 * @qemu_chr_be_write:
302 *
303 * Write data from the back end to the front end. Before issuing this call,
304 * the caller should call @qemu_chr_be_can_write to determine how much data
305 * the front end can currently accept.
306 *
307 * @buf a buffer to receive data from the front end
308 * @len the number of bytes to receive from the front end
309 */
310 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len);
311
312 /**
313 * @qemu_chr_be_write_impl:
314 *
315 * Implementation of back end writing. Used by replay module.
316 *
317 * @buf a buffer to receive data from the front end
318 * @len the number of bytes to receive from the front end
319 */
320 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len);
321
322 /**
323 * @qemu_chr_be_event:
324 *
325 * Send an event from the back end to the front end.
326 *
327 * @event the event to send
328 */
329 void qemu_chr_be_event(Chardev *s, int event);
330
331 /**
332 * @qemu_chr_fe_init:
333 *
334 * Initializes a front end for the given CharBackend and
335 * Chardev. Call qemu_chr_fe_deinit() to remove the association and
336 * release the driver.
337 *
338 * Returns: false on error.
339 */
340 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp);
341
342 /**
343 * @qemu_chr_fe_get_driver:
344 *
345 * Returns the driver associated with a CharBackend or NULL if no
346 * associated Chardev.
347 */
348 Chardev *qemu_chr_fe_get_driver(CharBackend *be);
349
350 /**
351 * @qemu_chr_fe_deinit:
352 *
353 * Dissociate the CharBackend from the Chardev.
354 *
355 * Safe to call without associated Chardev.
356 */
357 void qemu_chr_fe_deinit(CharBackend *b);
358
359 /**
360 * @qemu_chr_fe_set_handlers:
361 * @b: a CharBackend
362 * @fd_can_read: callback to get the amount of data the frontend may
363 * receive
364 * @fd_read: callback to receive data from char
365 * @fd_event: event callback
366 * @opaque: an opaque pointer for the callbacks
367 * @context: a main loop context or NULL for the default
368 * @set_open: whether to call qemu_chr_fe_set_open() implicitely when
369 * any of the handler is non-NULL
370 *
371 * Set the front end char handlers. The front end takes the focus if
372 * any of the handler is non-NULL.
373 *
374 * Without associated Chardev, nothing is changed.
375 */
376 void qemu_chr_fe_set_handlers(CharBackend *b,
377 IOCanReadHandler *fd_can_read,
378 IOReadHandler *fd_read,
379 IOEventHandler *fd_event,
380 void *opaque,
381 GMainContext *context,
382 bool set_open);
383
384 /**
385 * @qemu_chr_fe_take_focus:
386 *
387 * Take the focus (if the front end is muxed).
388 *
389 * Without associated Chardev, nothing is changed.
390 */
391 void qemu_chr_fe_take_focus(CharBackend *b);
392
393 void qemu_chr_fe_accept_input(CharBackend *be);
394 int qemu_chr_add_client(Chardev *s, int fd);
395 Chardev *qemu_chr_find(const char *name);
396
397 bool qemu_chr_has_feature(Chardev *chr,
398 ChardevFeature feature);
399 void qemu_chr_set_feature(Chardev *chr,
400 ChardevFeature feature);
401 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
402 int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len);
403 int qemu_chr_wait_connected(Chardev *chr, Error **errp);
404
405 #define TYPE_CHARDEV "chardev"
406 #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
407 #define CHARDEV_CLASS(klass) \
408 OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV)
409 #define CHARDEV_GET_CLASS(obj) \
410 OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV)
411
412 #define TYPE_CHARDEV_NULL "chardev-null"
413 #define TYPE_CHARDEV_MUX "chardev-mux"
414 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
415 #define TYPE_CHARDEV_PTY "chardev-pty"
416 #define TYPE_CHARDEV_CONSOLE "chardev-console"
417 #define TYPE_CHARDEV_STDIO "chardev-stdio"
418 #define TYPE_CHARDEV_PIPE "chardev-pipe"
419 #define TYPE_CHARDEV_MEMORY "chardev-memory"
420 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
421 #define TYPE_CHARDEV_FILE "chardev-file"
422 #define TYPE_CHARDEV_SERIAL "chardev-serial"
423 #define TYPE_CHARDEV_SOCKET "chardev-socket"
424 #define TYPE_CHARDEV_UDP "chardev-udp"
425
426 #define CHARDEV_IS_RINGBUF(chr) \
427 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
428 #define CHARDEV_IS_PTY(chr) \
429 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
430
431 typedef struct ChardevClass {
432 ObjectClass parent_class;
433
434 bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
435 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
436
437 void (*open)(Chardev *chr, ChardevBackend *backend,
438 bool *be_opened, Error **errp);
439
440 int (*chr_write)(Chardev *s, const uint8_t *buf, int len);
441 int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len);
442 GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond);
443 void (*chr_update_read_handler)(Chardev *s, GMainContext *context);
444 int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
445 int (*get_msgfds)(Chardev *s, int* fds, int num);
446 int (*set_msgfds)(Chardev *s, int *fds, int num);
447 int (*chr_add_client)(Chardev *chr, int fd);
448 int (*chr_wait_connected)(Chardev *chr, Error **errp);
449 void (*chr_disconnect)(Chardev *chr);
450 void (*chr_accept_input)(Chardev *chr);
451 void (*chr_set_echo)(Chardev *chr, bool echo);
452 void (*chr_set_fe_open)(Chardev *chr, int fe_open);
453 } ChardevClass;
454
455 Chardev *qemu_chardev_new(const char *id, const char *typename,
456 ChardevBackend *backend, Error **errp);
457
458 extern int term_escape_char;
459
460 /* console.c */
461 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp);
462
463 #endif