2016-02-19 12:45:26 +03:00
|
|
|
/*
|
|
|
|
* Terminal 3270 implementation
|
|
|
|
*
|
|
|
|
* Copyright 2017 IBM Corp.
|
|
|
|
*
|
|
|
|
* Authors: Yang Chen <bjcyang@linux.vnet.ibm.com>
|
|
|
|
* Jing Liu <liujbjl@linux.vnet.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or (at
|
|
|
|
* your option) any later version. See the COPYING file in the top-level
|
|
|
|
* directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2016-04-01 09:32:58 +03:00
|
|
|
#include "qapi/error.h"
|
2017-01-26 17:26:44 +03:00
|
|
|
#include "chardev/char-fe.h"
|
2016-02-19 12:45:26 +03:00
|
|
|
#include "hw/s390x/3270-ccw.h"
|
|
|
|
|
2016-04-01 09:32:58 +03:00
|
|
|
/* Enough spaces for different window sizes. */
|
|
|
|
#define INPUT_BUFFER_SIZE 1000
|
|
|
|
/*
|
|
|
|
* 1 for header, 1024*2 for datastream, 2 for tail
|
|
|
|
* Reserve enough spaces for telnet IAC escape.
|
|
|
|
*/
|
|
|
|
#define OUTPUT_BUFFER_SIZE 2051
|
|
|
|
|
2016-02-19 12:45:26 +03:00
|
|
|
typedef struct Terminal3270 {
|
|
|
|
EmulatedCcw3270Device cdev;
|
2016-04-01 09:32:58 +03:00
|
|
|
CharBackend chr;
|
|
|
|
uint8_t inv[INPUT_BUFFER_SIZE];
|
|
|
|
uint8_t outv[OUTPUT_BUFFER_SIZE];
|
|
|
|
int in_len;
|
|
|
|
bool handshake_done;
|
2018-01-04 17:18:35 +03:00
|
|
|
GSource *timer_src;
|
2016-02-19 12:45:26 +03:00
|
|
|
} Terminal3270;
|
|
|
|
|
|
|
|
#define TYPE_TERMINAL_3270 "x-terminal3270"
|
2016-04-01 09:32:58 +03:00
|
|
|
#define TERMINAL_3270(obj) \
|
|
|
|
OBJECT_CHECK(Terminal3270, (obj), TYPE_TERMINAL_3270)
|
|
|
|
|
|
|
|
static int terminal_can_read(void *opaque)
|
|
|
|
{
|
|
|
|
Terminal3270 *t = opaque;
|
|
|
|
|
|
|
|
return INPUT_BUFFER_SIZE - t->in_len;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:18:35 +03:00
|
|
|
static void terminal_timer_cancel(Terminal3270 *t)
|
|
|
|
{
|
|
|
|
if (t->timer_src) {
|
|
|
|
g_source_destroy(t->timer_src);
|
|
|
|
g_source_unref(t->timer_src);
|
|
|
|
t->timer_src = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 09:32:58 +03:00
|
|
|
/*
|
|
|
|
* Protocol handshake done,
|
|
|
|
* signal guest by an unsolicited DE irq.
|
|
|
|
*/
|
|
|
|
static void TN3270_handshake_done(Terminal3270 *t)
|
|
|
|
{
|
|
|
|
CcwDevice *ccw_dev = CCW_DEVICE(t);
|
|
|
|
SubchDev *sch = ccw_dev->sch;
|
|
|
|
|
|
|
|
t->handshake_done = true;
|
|
|
|
sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END;
|
|
|
|
css_conditional_io_interrupt(sch);
|
|
|
|
}
|
|
|
|
|
2016-10-14 12:16:23 +03:00
|
|
|
/*
|
|
|
|
* Called when the interval is timeout to detect
|
|
|
|
* if the client is still alive by Timing Mark.
|
|
|
|
*/
|
|
|
|
static gboolean send_timing_mark_cb(gpointer opaque)
|
|
|
|
{
|
|
|
|
Terminal3270 *t = opaque;
|
|
|
|
const uint8_t timing[] = {0xff, 0xfd, 0x06};
|
|
|
|
|
|
|
|
qemu_chr_fe_write_all(&t->chr, timing, sizeof(timing));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-01 09:32:58 +03:00
|
|
|
/*
|
|
|
|
* Receive inbound data from socket.
|
|
|
|
* For data given to guest, drop the data boundary IAC, IAC_EOR.
|
|
|
|
* TODO:
|
|
|
|
* Using "Reset" key on x3270 may result multiple commands in one packet.
|
|
|
|
* This usually happens when the user meets a poor traffic of the network.
|
|
|
|
* As of now, for such case, we simply terminate the connection,
|
|
|
|
* and we should come back here later with a better solution.
|
|
|
|
*/
|
|
|
|
static void terminal_read(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
Terminal3270 *t = opaque;
|
|
|
|
CcwDevice *ccw_dev = CCW_DEVICE(t);
|
|
|
|
SubchDev *sch = ccw_dev->sch;
|
|
|
|
int end;
|
|
|
|
|
|
|
|
assert(size <= (INPUT_BUFFER_SIZE - t->in_len));
|
|
|
|
|
2018-01-04 17:18:35 +03:00
|
|
|
terminal_timer_cancel(t);
|
|
|
|
t->timer_src = qemu_chr_timeout_add_ms(t->chr.chr, 600 * 1000,
|
|
|
|
send_timing_mark_cb, t);
|
2016-04-01 09:32:58 +03:00
|
|
|
memcpy(&t->inv[t->in_len], buf, size);
|
|
|
|
t->in_len += size;
|
|
|
|
if (t->in_len < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!t->handshake_done) {
|
|
|
|
/*
|
|
|
|
* Receiving Terminal Type is the last step of handshake.
|
|
|
|
* The data format: IAC SB Terminal-Type IS <terminal type> IAC SE
|
|
|
|
* The code for Terminal-Type is 0x18, for IS is 0.
|
|
|
|
* Simply check the data format and mark handshake_done.
|
|
|
|
*/
|
|
|
|
if (t->in_len > 6 && t->inv[2] == 0x18 && t->inv[3] == 0x0 &&
|
|
|
|
t->inv[t->in_len - 2] == IAC && t->inv[t->in_len - 1] == IAC_SE) {
|
|
|
|
TN3270_handshake_done(t);
|
|
|
|
t->in_len = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (end = 0; end < t->in_len - 1; end++) {
|
|
|
|
if (t->inv[end] == IAC && t->inv[end + 1] == IAC_EOR) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (end == t->in_len - 2) {
|
|
|
|
/* Data is valid for consuming. */
|
|
|
|
t->in_len -= 2;
|
|
|
|
sch->curr_status.scsw.dstat = SCSW_DSTAT_ATTENTION;
|
|
|
|
css_conditional_io_interrupt(sch);
|
|
|
|
} else if (end < t->in_len - 2) {
|
|
|
|
/* "Reset" key is used. */
|
|
|
|
qemu_chr_fe_disconnect(&t->chr);
|
|
|
|
} else {
|
|
|
|
/* Gathering data. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 12:45:26 +03:00
|
|
|
|
2016-07-21 09:00:58 +03:00
|
|
|
static void chr_event(void *opaque, int event)
|
|
|
|
{
|
|
|
|
Terminal3270 *t = opaque;
|
|
|
|
CcwDevice *ccw_dev = CCW_DEVICE(t);
|
|
|
|
SubchDev *sch = ccw_dev->sch;
|
|
|
|
|
|
|
|
/* Ensure the initial status correct, always reset them. */
|
|
|
|
t->in_len = 0;
|
|
|
|
t->handshake_done = false;
|
2018-01-04 17:18:35 +03:00
|
|
|
terminal_timer_cancel(t);
|
2016-07-21 09:00:58 +03:00
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case CHR_EVENT_OPENED:
|
|
|
|
/*
|
|
|
|
* 3270 does handshake firstly by the negotiate options in
|
|
|
|
* char-socket.c. Once qemu receives the terminal-type of the
|
|
|
|
* client, mark handshake done and trigger everything rolling again.
|
|
|
|
*/
|
2018-01-04 17:18:35 +03:00
|
|
|
t->timer_src = qemu_chr_timeout_add_ms(t->chr.chr, 600 * 1000,
|
|
|
|
send_timing_mark_cb, t);
|
2016-07-21 09:00:58 +03:00
|
|
|
break;
|
|
|
|
case CHR_EVENT_CLOSED:
|
|
|
|
sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END;
|
|
|
|
css_conditional_io_interrupt(sch);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 12:45:26 +03:00
|
|
|
static void terminal_init(EmulatedCcw3270Device *dev, Error **errp)
|
|
|
|
{
|
2016-04-01 09:32:58 +03:00
|
|
|
Terminal3270 *t = TERMINAL_3270(dev);
|
2016-02-19 12:45:26 +03:00
|
|
|
static bool terminal_available;
|
|
|
|
|
|
|
|
if (terminal_available) {
|
|
|
|
error_setg(errp, "Multiple 3270 terminals are not supported.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
terminal_available = true;
|
2016-04-01 09:32:58 +03:00
|
|
|
qemu_chr_fe_set_handlers(&t->chr, terminal_can_read,
|
2017-07-06 15:08:49 +03:00
|
|
|
terminal_read, chr_event, NULL, t, NULL, true);
|
2016-04-01 09:32:58 +03:00
|
|
|
}
|
|
|
|
|
2017-09-20 20:23:13 +03:00
|
|
|
static inline CcwDataStream *get_cds(Terminal3270 *t)
|
|
|
|
{
|
|
|
|
return &(CCW_DEVICE(&t->cdev)->sch->cds);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_payload_3270(EmulatedCcw3270Device *dev)
|
2016-04-01 09:32:58 +03:00
|
|
|
{
|
|
|
|
Terminal3270 *t = TERMINAL_3270(dev);
|
|
|
|
int len;
|
|
|
|
|
2017-09-20 20:23:13 +03:00
|
|
|
len = MIN(ccw_dstream_avail(get_cds(t)), t->in_len);
|
|
|
|
ccw_dstream_write_buf(get_cds(t), t->inv, len);
|
2016-04-01 09:32:58 +03:00
|
|
|
t->in_len -= len;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TN3270 uses binary transmission, which needs escape IAC to IAC IAC */
|
|
|
|
static int insert_IAC_escape_char(uint8_t *outv, int out_len)
|
|
|
|
{
|
|
|
|
int IAC_num = 0, new_out_len, i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < out_len; i++) {
|
|
|
|
if (outv[i] == IAC) {
|
|
|
|
IAC_num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IAC_num == 0) {
|
|
|
|
return out_len;
|
|
|
|
}
|
|
|
|
new_out_len = out_len + IAC_num;
|
|
|
|
for (i = out_len - 1, j = new_out_len - 1; j > i && i >= 0; i--, j--) {
|
|
|
|
outv[j] = outv[i];
|
|
|
|
if (outv[i] == IAC) {
|
|
|
|
outv[--j] = IAC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new_out_len;
|
2016-02-19 12:45:26 +03:00
|
|
|
}
|
|
|
|
|
2016-04-01 09:32:58 +03:00
|
|
|
/*
|
|
|
|
* Write 3270 outbound to socket.
|
|
|
|
* Return the count of 3270 data field if succeeded, zero if failed.
|
|
|
|
*/
|
2017-09-20 20:23:13 +03:00
|
|
|
static int write_payload_3270(EmulatedCcw3270Device *dev, uint8_t cmd)
|
2016-04-01 09:32:58 +03:00
|
|
|
{
|
|
|
|
Terminal3270 *t = TERMINAL_3270(dev);
|
|
|
|
int retval = 0;
|
2017-09-20 20:23:13 +03:00
|
|
|
int count = ccw_dstream_avail(get_cds(t));
|
2017-09-20 20:23:14 +03:00
|
|
|
int bound = (OUTPUT_BUFFER_SIZE - 3) / 2;
|
|
|
|
int len = MIN(count, bound);
|
|
|
|
int out_len = 0;
|
2016-04-01 09:32:58 +03:00
|
|
|
|
|
|
|
if (!t->handshake_done) {
|
2016-10-14 12:16:23 +03:00
|
|
|
if (!(t->outv[0] == IAC && t->outv[1] != IAC)) {
|
|
|
|
/*
|
|
|
|
* Before having finished 3270 negotiation,
|
|
|
|
* sending outbound data except protocol options is prohibited.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-01 09:32:58 +03:00
|
|
|
}
|
2017-07-06 15:08:52 +03:00
|
|
|
if (!qemu_chr_fe_backend_connected(&t->chr)) {
|
2016-04-01 09:32:58 +03:00
|
|
|
/* We just say we consumed all data if there's no backend. */
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-09-20 20:23:14 +03:00
|
|
|
t->outv[out_len++] = cmd;
|
|
|
|
do {
|
|
|
|
ccw_dstream_read_buf(get_cds(t), &t->outv[out_len], len);
|
|
|
|
count = ccw_dstream_avail(get_cds(t));
|
|
|
|
out_len += len;
|
2016-04-01 09:32:58 +03:00
|
|
|
|
2017-09-20 20:23:14 +03:00
|
|
|
out_len = insert_IAC_escape_char(t->outv, out_len);
|
|
|
|
if (!count) {
|
|
|
|
t->outv[out_len++] = IAC;
|
|
|
|
t->outv[out_len++] = IAC_EOR;
|
|
|
|
}
|
|
|
|
retval = qemu_chr_fe_write_all(&t->chr, t->outv, out_len);
|
|
|
|
len = MIN(count, bound);
|
|
|
|
out_len = 0;
|
|
|
|
} while (len && retval >= 0);
|
|
|
|
return (retval <= 0) ? 0 : get_cds(t)->count;
|
2016-04-01 09:32:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static Property terminal_properties[] = {
|
|
|
|
DEFINE_PROP_CHR("chardev", Terminal3270, chr),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
s390x/3270: Mark non-migratable and enable the device
Mark 3270 as non-migratable for the experimental stage. Enable
the 3270 device so that we can use x3270 client to operate the guest.
Run qemu with the arguments:
-chardev socket,id=char3270_0,host=0.0.0.0,port=23,nowait,server,tn3270 \
-device x-terminal3270,chardev=char3270_0,devno=fe.0.000a,id=terminal3270_0 \
There are some restrictions for the first stage: We don't support SSL
connections, multiple client connections and client resizing. Only
tested with the x3270 client.
Signed-off-by: Jing Liu <liujbjl@linux.vnet.ibm.com>
Signed-off-by: Yang Chen <bjcyang@linux.vnet.ibm.com>
Reviewed-by: QingFeng Hao <haoqf@linux.vnet.ibm.com>
Reviewed-by: Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
Signed-off-by: Cornelia Huck <cornelia.huck@de.ibm.com>
2017-01-12 12:38:25 +03:00
|
|
|
static const VMStateDescription terminal3270_vmstate = {
|
|
|
|
.name = TYPE_TERMINAL_3270,
|
|
|
|
.unmigratable = 1,
|
|
|
|
};
|
|
|
|
|
2016-02-19 12:45:26 +03:00
|
|
|
static void terminal_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2016-04-01 09:32:58 +03:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2016-02-19 12:45:26 +03:00
|
|
|
EmulatedCcw3270Class *ck = EMULATED_CCW_3270_CLASS(klass);
|
|
|
|
|
2016-04-01 09:32:58 +03:00
|
|
|
dc->props = terminal_properties;
|
s390x/3270: Mark non-migratable and enable the device
Mark 3270 as non-migratable for the experimental stage. Enable
the 3270 device so that we can use x3270 client to operate the guest.
Run qemu with the arguments:
-chardev socket,id=char3270_0,host=0.0.0.0,port=23,nowait,server,tn3270 \
-device x-terminal3270,chardev=char3270_0,devno=fe.0.000a,id=terminal3270_0 \
There are some restrictions for the first stage: We don't support SSL
connections, multiple client connections and client resizing. Only
tested with the x3270 client.
Signed-off-by: Jing Liu <liujbjl@linux.vnet.ibm.com>
Signed-off-by: Yang Chen <bjcyang@linux.vnet.ibm.com>
Reviewed-by: QingFeng Hao <haoqf@linux.vnet.ibm.com>
Reviewed-by: Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
Signed-off-by: Cornelia Huck <cornelia.huck@de.ibm.com>
2017-01-12 12:38:25 +03:00
|
|
|
dc->vmsd = &terminal3270_vmstate;
|
2016-02-19 12:45:26 +03:00
|
|
|
ck->init = terminal_init;
|
2016-04-01 09:32:58 +03:00
|
|
|
ck->read_payload_3270 = read_payload_3270;
|
|
|
|
ck->write_payload_3270 = write_payload_3270;
|
2016-02-19 12:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo ccw_terminal_info = {
|
|
|
|
.name = TYPE_TERMINAL_3270,
|
|
|
|
.parent = TYPE_EMULATED_CCW_3270,
|
|
|
|
.instance_size = sizeof(Terminal3270),
|
|
|
|
.class_init = terminal_class_init,
|
|
|
|
.class_size = sizeof(EmulatedCcw3270Class),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&ccw_terminal_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types)
|