xref: /openbmc/qemu/include/chardev/char.h (revision 0b1183e3)
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 typedef struct CharBackend CharBackend;
20 
21 typedef enum {
22     CHR_EVENT_BREAK, /* serial break char */
23     CHR_EVENT_OPENED, /* new connection established */
24     CHR_EVENT_MUX_IN, /* mux-focus was set to this terminal */
25     CHR_EVENT_MUX_OUT, /* mux-focus will move on */
26     CHR_EVENT_CLOSED /* connection closed */
27 } QEMUChrEvent;
28 
29 #define CHR_READ_BUF_LEN 4096
30 
31 typedef enum {
32     /* Whether the chardev peer is able to close and
33      * reopen the data channel, thus requiring support
34      * for qemu_chr_wait_connected() to wait for a
35      * valid connection */
36     QEMU_CHAR_FEATURE_RECONNECTABLE,
37     /* Whether it is possible to send/recv file descriptors
38      * over the data channel */
39     QEMU_CHAR_FEATURE_FD_PASS,
40     /* Whether replay or record mode is enabled */
41     QEMU_CHAR_FEATURE_REPLAY,
42 
43     QEMU_CHAR_FEATURE_LAST,
44 } ChardevFeature;
45 
46 #define qemu_chr_replay(chr) qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY)
47 
48 struct Chardev {
49     Object parent_obj;
50 
51     QemuMutex chr_write_lock;
52     CharBackend *be;
53     char *label;
54     char *filename;
55     int logfd;
56     int be_open;
57     GSource *gsource;
58     DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
59 };
60 
61 /**
62  * @qemu_chr_new_from_opts:
63  *
64  * Create a new character backend from a QemuOpts list.
65  *
66  * @opts see qemu-config.c for a list of valid options
67  *
68  * Returns: a new character backend
69  */
70 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
71                                 Error **errp);
72 
73 /**
74  * @qemu_chr_parse_common:
75  *
76  * Parse the common options available to all character backends.
77  *
78  * @opts the options that still need parsing
79  * @backend a new backend
80  */
81 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
82 
83 /**
84  * @qemu_chr_parse_opts:
85  *
86  * Parse the options to the ChardevBackend struct.
87  *
88  * Returns: a new backend or NULL on error
89  */
90 ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts,
91                                     Error **errp);
92 
93 /**
94  * @qemu_chr_new:
95  *
96  * Create a new character backend from a URI.
97  *
98  * @label the name of the backend
99  * @filename the URI
100  *
101  * Returns: a new character backend
102  */
103 Chardev *qemu_chr_new(const char *label, const char *filename);
104 
105 /**
106  * @qemu_chr_change:
107  *
108  * Change an existing character backend
109  *
110  * @opts the new backend options
111  */
112 void qemu_chr_change(QemuOpts *opts, Error **errp);
113 
114 /**
115  * @qemu_chr_cleanup:
116  *
117  * Delete all chardevs (when leaving qemu)
118  */
119 void qemu_chr_cleanup(void);
120 
121 /**
122  * @qemu_chr_new_noreplay:
123  *
124  * Create a new character backend from a URI.
125  * Character device communications are not written
126  * into the replay log.
127  *
128  * @label the name of the backend
129  * @filename the URI
130  *
131  * Returns: a new character backend
132  */
133 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename);
134 
135 /**
136  * @qemu_chr_be_can_write:
137  *
138  * Determine how much data the front end can currently accept.  This function
139  * returns the number of bytes the front end can accept.  If it returns 0, the
140  * front end cannot receive data at the moment.  The function must be polled
141  * to determine when data can be received.
142  *
143  * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
144  */
145 int qemu_chr_be_can_write(Chardev *s);
146 
147 /**
148  * @qemu_chr_be_write:
149  *
150  * Write data from the back end to the front end.  Before issuing this call,
151  * the caller should call @qemu_chr_be_can_write to determine how much data
152  * the front end can currently accept.
153  *
154  * @buf a buffer to receive data from the front end
155  * @len the number of bytes to receive from the front end
156  */
157 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len);
158 
159 /**
160  * @qemu_chr_be_write_impl:
161  *
162  * Implementation of back end writing. Used by replay module.
163  *
164  * @buf a buffer to receive data from the front end
165  * @len the number of bytes to receive from the front end
166  */
167 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len);
168 
169 /**
170  * @qemu_chr_be_event:
171  *
172  * Send an event from the back end to the front end.
173  *
174  * @event the event to send
175  */
176 void qemu_chr_be_event(Chardev *s, int event);
177 
178 int qemu_chr_add_client(Chardev *s, int fd);
179 Chardev *qemu_chr_find(const char *name);
180 
181 bool qemu_chr_has_feature(Chardev *chr,
182                           ChardevFeature feature);
183 void qemu_chr_set_feature(Chardev *chr,
184                           ChardevFeature feature);
185 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
186 int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all);
187 #define qemu_chr_write_all(s, buf, len) qemu_chr_write(s, buf, len, true)
188 int qemu_chr_wait_connected(Chardev *chr, Error **errp);
189 
190 #define TYPE_CHARDEV "chardev"
191 #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
192 #define CHARDEV_CLASS(klass) \
193     OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV)
194 #define CHARDEV_GET_CLASS(obj) \
195     OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV)
196 
197 #define TYPE_CHARDEV_NULL "chardev-null"
198 #define TYPE_CHARDEV_MUX "chardev-mux"
199 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
200 #define TYPE_CHARDEV_PTY "chardev-pty"
201 #define TYPE_CHARDEV_CONSOLE "chardev-console"
202 #define TYPE_CHARDEV_STDIO "chardev-stdio"
203 #define TYPE_CHARDEV_PIPE "chardev-pipe"
204 #define TYPE_CHARDEV_MEMORY "chardev-memory"
205 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
206 #define TYPE_CHARDEV_FILE "chardev-file"
207 #define TYPE_CHARDEV_SERIAL "chardev-serial"
208 #define TYPE_CHARDEV_SOCKET "chardev-socket"
209 #define TYPE_CHARDEV_UDP "chardev-udp"
210 
211 #define CHARDEV_IS_RINGBUF(chr) \
212     object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
213 #define CHARDEV_IS_PTY(chr) \
214     object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
215 
216 typedef struct ChardevClass {
217     ObjectClass parent_class;
218 
219     bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
220     void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
221 
222     void (*open)(Chardev *chr, ChardevBackend *backend,
223                  bool *be_opened, Error **errp);
224 
225     int (*chr_write)(Chardev *s, const uint8_t *buf, int len);
226     int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len);
227     GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond);
228     void (*chr_update_read_handler)(Chardev *s, GMainContext *context);
229     int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
230     int (*get_msgfds)(Chardev *s, int* fds, int num);
231     int (*set_msgfds)(Chardev *s, int *fds, int num);
232     int (*chr_add_client)(Chardev *chr, int fd);
233     int (*chr_wait_connected)(Chardev *chr, Error **errp);
234     void (*chr_disconnect)(Chardev *chr);
235     void (*chr_accept_input)(Chardev *chr);
236     void (*chr_set_echo)(Chardev *chr, bool echo);
237     void (*chr_set_fe_open)(Chardev *chr, int fe_open);
238 } ChardevClass;
239 
240 Chardev *qemu_chardev_new(const char *id, const char *typename,
241                           ChardevBackend *backend, Error **errp);
242 
243 extern int term_escape_char;
244 
245 /* console.c */
246 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp);
247 
248 #endif
249