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