xref: /openbmc/qemu/include/chardev/char.h (revision 2f95279a)
1 #ifndef QEMU_CHAR_H
2 #define QEMU_CHAR_H
3 
4 #include "qapi/qapi-types-char.h"
5 #include "qemu/bitmap.h"
6 #include "qemu/thread.h"
7 #include "qom/object.h"
8 
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
16 
17 /* character device */
18 typedef struct CharBackend CharBackend;
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.  NOTE: currently this event
26                       * is only bound to the read port of the chardev.
27                       * Normally the read port and write port of a
28                       * chardev should be the same, but it can be
29                       * different, e.g., for fd chardevs, when the two
30                       * fds are different.  So when we received the
31                       * CLOSED event it's still possible that the out
32                       * port is still open.  TODO: we should only send
33                       * the CLOSED event when both ports are closed.
34                       */
35 } QEMUChrEvent;
36 
37 #define CHR_READ_BUF_LEN 4096
38 
39 typedef enum {
40     /* Whether the chardev peer is able to close and
41      * reopen the data channel, thus requiring support
42      * for qemu_chr_wait_connected() to wait for a
43      * valid connection */
44     QEMU_CHAR_FEATURE_RECONNECTABLE,
45     /* Whether it is possible to send/recv file descriptors
46      * over the data channel */
47     QEMU_CHAR_FEATURE_FD_PASS,
48     /* Whether replay or record mode is enabled */
49     QEMU_CHAR_FEATURE_REPLAY,
50     /* Whether the gcontext can be changed after calling
51      * qemu_chr_be_update_read_handlers() */
52     QEMU_CHAR_FEATURE_GCONTEXT,
53 
54     QEMU_CHAR_FEATURE_LAST,
55 } ChardevFeature;
56 
57 #define qemu_chr_replay(chr) qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY)
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     /* used to coordinate the chardev-change special-case: */
69     bool handover_yank_instance;
70     GSource *gsource;
71     GMainContext *gcontext;
72     DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
73 };
74 
75 /**
76  * qemu_chr_new_from_opts:
77  * @opts: see qemu-config.c for a list of valid options
78  * @context: the #GMainContext to be used at initialization time
79  *
80  * Create a new character backend from a QemuOpts list.
81  *
82  * Returns: on success: a new character backend
83  *          otherwise:  NULL; @errp specifies the error
84  *                            or left untouched in case of help option
85  */
86 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
87                                 GMainContext *context,
88                                 Error **errp);
89 
90 /**
91  * qemu_chr_parse_common:
92  * @opts: the options that still need parsing
93  * @backend: a new backend
94  *
95  * Parse the common options available to all character backends.
96  */
97 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
98 
99 /**
100  * qemu_chr_parse_opts:
101  *
102  * Parse the options to the ChardevBackend struct.
103  *
104  * Returns: a new backend or NULL on error
105  */
106 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts,
107                                     Error **errp);
108 
109 /**
110  * qemu_chr_new:
111  * @label: the name of the backend
112  * @filename: the URI
113  * @context: the #GMainContext to be used at initialization time
114  *
115  * Create a new character backend from a URI.
116  * Do not implicitly initialize a monitor if the chardev is muxed.
117  *
118  * Returns: a new character backend
119  */
120 Chardev *qemu_chr_new(const char *label, const char *filename,
121                       GMainContext *context);
122 
123 /**
124  * qemu_chr_new_mux_mon:
125  * @label: the name of the backend
126  * @filename: the URI
127  * @context: the #GMainContext to be used at initialization time
128  *
129  * Create a new character backend from a URI.
130  * Implicitly initialize a monitor if the chardev is muxed.
131  *
132  * Returns: a new character backend
133  */
134 Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename,
135                               GMainContext *context);
136 
137 /**
138 * qemu_chr_change:
139 * @opts: the new backend options
140  *
141  * Change an existing character backend
142  */
143 void qemu_chr_change(QemuOpts *opts, Error **errp);
144 
145 /**
146  * qemu_chr_cleanup:
147  *
148  * Delete all chardevs (when leaving qemu)
149  */
150 void qemu_chr_cleanup(void);
151 
152 /**
153  * qemu_chr_new_noreplay:
154  * @label: the name of the backend
155  * @filename: the URI
156  * @permit_mux_mon: if chardev is muxed, initialize a monitor
157  * @context: the #GMainContext to be used at initialization time
158  *
159  * Create a new character backend from a URI.
160  * Character device communications are not written
161  * into the replay log.
162  *
163  * Returns: a new character backend
164  */
165 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
166                                bool permit_mux_mon, GMainContext *context);
167 
168 /**
169  * qemu_chr_be_can_write:
170  *
171  * Determine how much data the front end can currently accept.  This function
172  * returns the number of bytes the front end can accept.  If it returns 0, the
173  * front end cannot receive data at the moment.  The function must be polled
174  * to determine when data can be received.
175  *
176  * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
177  */
178 int qemu_chr_be_can_write(Chardev *s);
179 
180 /**
181  * qemu_chr_be_write:
182  * @buf: a buffer to receive data from the front end
183  * @len: the number of bytes to receive from the front end
184  *
185  * Write data from the back end to the front end.  Before issuing this call,
186  * the caller should call @qemu_chr_be_can_write to determine how much data
187  * the front end can currently accept.
188  */
189 void qemu_chr_be_write(Chardev *s, const uint8_t *buf, int len);
190 
191 /**
192  * qemu_chr_be_write_impl:
193  * @buf: a buffer to receive data from the front end
194  * @len: the number of bytes to receive from the front end
195  *
196  * Implementation of back end writing. Used by replay module.
197  */
198 void qemu_chr_be_write_impl(Chardev *s, const uint8_t *buf, int len);
199 
200 /**
201  * qemu_chr_be_update_read_handlers:
202  * @context: the gcontext that will be used to attach the watch sources
203  *
204  * Invoked when frontend read handlers are setup
205  */
206 void qemu_chr_be_update_read_handlers(Chardev *s,
207                                       GMainContext *context);
208 
209 /**
210  * qemu_chr_be_event:
211  * @event: the event to send
212  *
213  * Send an event from the back end to the front end.
214  */
215 void qemu_chr_be_event(Chardev *s, QEMUChrEvent event);
216 
217 int qemu_chr_add_client(Chardev *s, int fd);
218 Chardev *qemu_chr_find(const char *name);
219 
220 bool qemu_chr_has_feature(Chardev *chr,
221                           ChardevFeature feature);
222 void qemu_chr_set_feature(Chardev *chr,
223                           ChardevFeature feature);
224 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,
225                                 bool permit_mux_mon);
226 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all);
227 #define qemu_chr_write_all(s, buf, len) qemu_chr_write(s, buf, len, true)
228 int qemu_chr_wait_connected(Chardev *chr, Error **errp);
229 
230 #define TYPE_CHARDEV "chardev"
231 OBJECT_DECLARE_TYPE(Chardev, ChardevClass, CHARDEV)
232 
233 #define TYPE_CHARDEV_NULL "chardev-null"
234 #define TYPE_CHARDEV_MUX "chardev-mux"
235 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
236 #define TYPE_CHARDEV_PTY "chardev-pty"
237 #define TYPE_CHARDEV_CONSOLE "chardev-console"
238 #define TYPE_CHARDEV_STDIO "chardev-stdio"
239 #define TYPE_CHARDEV_PIPE "chardev-pipe"
240 #define TYPE_CHARDEV_MEMORY "chardev-memory"
241 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
242 #define TYPE_CHARDEV_FILE "chardev-file"
243 #define TYPE_CHARDEV_SERIAL "chardev-serial"
244 #define TYPE_CHARDEV_SOCKET "chardev-socket"
245 #define TYPE_CHARDEV_UDP "chardev-udp"
246 
247 #define CHARDEV_IS_RINGBUF(chr) \
248     object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
249 #define CHARDEV_IS_PTY(chr) \
250     object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
251 
252 struct ChardevClass {
253     ObjectClass parent_class;
254 
255     bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
256     bool supports_yank;
257 
258     /* parse command line options and populate QAPI @backend */
259     void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
260 
261     /* called after construction, open/starts the backend */
262     void (*open)(Chardev *chr, ChardevBackend *backend,
263                  bool *be_opened, Error **errp);
264 
265     /* write buf to the backend */
266     int (*chr_write)(Chardev *s, const uint8_t *buf, int len);
267 
268     /*
269      * Read from the backend (blocking). A typical front-end will instead rely
270      * on chr_can_read/chr_read being called when polling/looping.
271      */
272     int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len);
273 
274     /* create a watch on the backend */
275     GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond);
276 
277     /* update the backend internal sources */
278     void (*chr_update_read_handler)(Chardev *s);
279 
280     /* send an ioctl to the backend */
281     int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
282 
283     /* get ancillary-received fds during last read */
284     int (*get_msgfds)(Chardev *s, int* fds, int num);
285 
286     /* set ancillary fds to be sent with next write */
287     int (*set_msgfds)(Chardev *s, int *fds, int num);
288 
289     /* accept the given fd */
290     int (*chr_add_client)(Chardev *chr, int fd);
291 
292     /* wait for a connection */
293     int (*chr_wait_connected)(Chardev *chr, Error **errp);
294 
295     /* disconnect a connection */
296     void (*chr_disconnect)(Chardev *chr);
297 
298     /* called by frontend when it can read */
299     void (*chr_accept_input)(Chardev *chr);
300 
301     /* set terminal echo */
302     void (*chr_set_echo)(Chardev *chr, bool echo);
303 
304     /* notify the backend of frontend open state */
305     void (*chr_set_fe_open)(Chardev *chr, int fe_open);
306 
307     /* handle various events */
308     void (*chr_be_event)(Chardev *s, QEMUChrEvent event);
309 };
310 
311 Chardev *qemu_chardev_new(const char *id, const char *typename,
312                           ChardevBackend *backend, GMainContext *context,
313                           Error **errp);
314 
315 extern int term_escape_char;
316 
317 GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,
318                                  GSourceFunc func, void *private);
319 
320 void suspend_mux_open(void);
321 void resume_mux_open(void);
322 
323 #endif
324