postcopy: drop ram_pages parameter from postcopy_ram_incoming_init()
[qemu.git] / include / chardev / char.h
blob04de45795e516a1b262ff00a59c525df42187f26
1 #ifndef QEMU_CHAR_H
2 #define QEMU_CHAR_H
4 #include "qapi/qapi-types-char.h"
5 #include "qemu/main-loop.h"
6 #include "qemu/bitmap.h"
7 #include "qom/object.h"
9 #define IAC_EOR 239
10 #define IAC_SE 240
11 #define IAC_NOP 241
12 #define IAC_BREAK 243
13 #define IAC_IP 244
14 #define IAC_SB 250
15 #define IAC 255
17 /* character device */
18 typedef struct CharBackend CharBackend;
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;
28 #define CHR_READ_BUF_LEN 4096
30 typedef enum {
31 /* Whether the chardev peer is able to close and
32 * reopen the data channel, thus requiring support
33 * for qemu_chr_wait_connected() to wait for a
34 * valid connection */
35 QEMU_CHAR_FEATURE_RECONNECTABLE,
36 /* Whether it is possible to send/recv file descriptors
37 * over the data channel */
38 QEMU_CHAR_FEATURE_FD_PASS,
39 /* Whether replay or record mode is enabled */
40 QEMU_CHAR_FEATURE_REPLAY,
42 QEMU_CHAR_FEATURE_LAST,
43 } ChardevFeature;
45 #define qemu_chr_replay(chr) qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY)
47 struct Chardev {
48 Object parent_obj;
50 QemuMutex chr_write_lock;
51 CharBackend *be;
52 char *label;
53 char *filename;
54 int logfd;
55 int be_open;
56 GSource *gsource;
57 GMainContext *gcontext;
58 DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
61 /**
62 * @qemu_chr_new_from_opts:
64 * Create a new character backend from a QemuOpts list.
66 * @opts see qemu-config.c for a list of valid options
68 * Returns: on success: a new character backend
69 * otherwise: NULL; @errp specifies the error
70 * or left untouched in case of help option
72 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
73 Error **errp);
75 /**
76 * @qemu_chr_parse_common:
78 * Parse the common options available to all character backends.
80 * @opts the options that still need parsing
81 * @backend a new backend
83 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
85 /**
86 * @qemu_chr_parse_opts:
88 * Parse the options to the ChardevBackend struct.
90 * Returns: a new backend or NULL on error
92 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts,
93 Error **errp);
95 /**
96 * @qemu_chr_new:
98 * Create a new character backend from a URI.
100 * @label the name of the backend
101 * @filename the URI
103 * Returns: a new character backend
105 Chardev *qemu_chr_new(const char *label, const char *filename);
108 * @qemu_chr_change:
110 * Change an existing character backend
112 * @opts the new backend options
114 void qemu_chr_change(QemuOpts *opts, Error **errp);
117 * @qemu_chr_cleanup:
119 * Delete all chardevs (when leaving qemu)
121 void qemu_chr_cleanup(void);
124 * @qemu_chr_new_noreplay:
126 * Create a new character backend from a URI.
127 * Character device communications are not written
128 * into the replay log.
130 * @label the name of the backend
131 * @filename the URI
133 * Returns: a new character backend
135 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename);
138 * @qemu_chr_be_can_write:
140 * Determine how much data the front end can currently accept. This function
141 * returns the number of bytes the front end can accept. If it returns 0, the
142 * front end cannot receive data at the moment. The function must be polled
143 * to determine when data can be received.
145 * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
147 int qemu_chr_be_can_write(Chardev *s);
150 * @qemu_chr_be_write:
152 * Write data from the back end to the front end. Before issuing this call,
153 * the caller should call @qemu_chr_be_can_write to determine how much data
154 * the front end can currently accept.
156 * @buf a buffer to receive data from the front end
157 * @len the number of bytes to receive from the front end
159 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len);
162 * @qemu_chr_be_write_impl:
164 * Implementation of back end writing. Used by replay module.
166 * @buf a buffer to receive data from the front end
167 * @len the number of bytes to receive from the front end
169 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len);
172 * @qemu_chr_be_update_read_handlers:
174 * Invoked when frontend read handlers are setup
176 * @context the gcontext that will be used to attach the watch sources
178 void qemu_chr_be_update_read_handlers(Chardev *s,
179 GMainContext *context);
182 * @qemu_chr_be_event:
184 * Send an event from the back end to the front end.
186 * @event the event to send
188 void qemu_chr_be_event(Chardev *s, int event);
190 int qemu_chr_add_client(Chardev *s, int fd);
191 Chardev *qemu_chr_find(const char *name);
193 bool qemu_chr_has_feature(Chardev *chr,
194 ChardevFeature feature);
195 void qemu_chr_set_feature(Chardev *chr,
196 ChardevFeature feature);
197 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
198 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all);
199 #define qemu_chr_write_all(s, buf, len) qemu_chr_write(s, buf, len, true)
200 int qemu_chr_wait_connected(Chardev *chr, Error **errp);
202 #define TYPE_CHARDEV "chardev"
203 #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
204 #define CHARDEV_CLASS(klass) \
205 OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV)
206 #define CHARDEV_GET_CLASS(obj) \
207 OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV)
209 #define TYPE_CHARDEV_NULL "chardev-null"
210 #define TYPE_CHARDEV_MUX "chardev-mux"
211 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
212 #define TYPE_CHARDEV_PTY "chardev-pty"
213 #define TYPE_CHARDEV_CONSOLE "chardev-console"
214 #define TYPE_CHARDEV_STDIO "chardev-stdio"
215 #define TYPE_CHARDEV_PIPE "chardev-pipe"
216 #define TYPE_CHARDEV_MEMORY "chardev-memory"
217 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
218 #define TYPE_CHARDEV_FILE "chardev-file"
219 #define TYPE_CHARDEV_SERIAL "chardev-serial"
220 #define TYPE_CHARDEV_SOCKET "chardev-socket"
221 #define TYPE_CHARDEV_UDP "chardev-udp"
223 #define CHARDEV_IS_RINGBUF(chr) \
224 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
225 #define CHARDEV_IS_PTY(chr) \
226 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
228 typedef struct ChardevClass {
229 ObjectClass parent_class;
231 bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
232 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
234 void (*open)(Chardev *chr, ChardevBackend *backend,
235 bool *be_opened, Error **errp);
237 int (*chr_write)(Chardev *s, const uint8_t *buf, int len);
238 int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len);
239 GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond);
240 void (*chr_update_read_handler)(Chardev *s);
241 int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
242 int (*get_msgfds)(Chardev *s, int* fds, int num);
243 int (*set_msgfds)(Chardev *s, int *fds, int num);
244 int (*chr_add_client)(Chardev *chr, int fd);
245 int (*chr_wait_connected)(Chardev *chr, Error **errp);
246 void (*chr_disconnect)(Chardev *chr);
247 void (*chr_accept_input)(Chardev *chr);
248 void (*chr_set_echo)(Chardev *chr, bool echo);
249 void (*chr_set_fe_open)(Chardev *chr, int fe_open);
250 void (*chr_be_event)(Chardev *s, int event);
251 /* Return 0 if succeeded, 1 if failed */
252 int (*chr_machine_done)(Chardev *chr);
253 } ChardevClass;
255 Chardev *qemu_chardev_new(const char *id, const char *typename,
256 ChardevBackend *backend, Error **errp);
258 extern int term_escape_char;
260 GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,
261 GSourceFunc func, void *private);
263 /* console.c */
264 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp);
266 #endif