Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 2534123

Browse files
committed
support xenpv
Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent 7f8be96 commit 2534123

File tree

4 files changed

+107
-80
lines changed

4 files changed

+107
-80
lines changed

src/api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ enum {
6868
* use serial as channel passed through kernel cmdline
6969
*/
7070
#define HYPER_USE_SERAIL "hyper_use_serial"
71+
#define HYPER_P9_USE_XEN "hyper_p9_xen"
7172
#endif /* _HYPERSTART_API_H_ */

src/init.c

Lines changed: 88 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ static int hyper_setup_shm(struct hyper_pod *pod)
427427
return 0;
428428
}
429429

430+
static bool is_serial = false;
431+
static bool is_xen = false;
432+
430433
#ifdef WITH_VBOX
431434

432435
#define MAX_HOST_NAME 256
@@ -489,6 +492,8 @@ static int hyper_setup_shared(struct hyper_pod *pod)
489492
#else
490493
static int hyper_setup_shared(struct hyper_pod *pod)
491494
{
495+
int ret;
496+
492497
if (pod->share_tag == NULL) {
493498
fprintf(stdout, "no shared directory\n");
494499
return 0;
@@ -499,11 +504,14 @@ static int hyper_setup_shared(struct hyper_pod *pod)
499504
return -1;
500505
}
501506

502-
if (mount(pod->share_tag, SHARED_DIR, "9p",
503-
MS_MGC_VAL| MS_NODEV, "trans=virtio") < 0) {
507+
if (is_xen)
508+
ret = mount(pod->share_tag, SHARED_DIR, "9p", MS_NODEV, "trans=xen");
509+
else
510+
ret = mount(pod->share_tag, SHARED_DIR, "9p", MS_MGC_VAL| MS_NODEV, "trans=virtio");
504511

512+
if (ret < 0) {
505513
perror("fail to mount shared dir");
506-
return -1;
514+
return ret;
507515
}
508516

509517
return 0;
@@ -953,7 +961,7 @@ static int hyper_ctl_send_ready(int fd)
953961
return 0;
954962
}
955963

956-
static int hyper_setup_ctl_channel(char *name, bool is_serial)
964+
static int hyper_setup_ctl_channel(char *name)
957965
{
958966
int fd = hyper_open_channel(name, 0, is_serial);
959967
if (fd < 0)
@@ -967,7 +975,7 @@ static int hyper_setup_ctl_channel(char *name, bool is_serial)
967975
return fd;
968976
}
969977

970-
static int hyper_setup_tty_channel(char *name, bool is_serial)
978+
static int hyper_setup_tty_channel(char *name)
971979
{
972980
int ret = hyper_open_channel(name, O_NONBLOCK, is_serial);
973981
if (ret < 0)
@@ -980,13 +988,75 @@ static int hyper_setup_vsock_channel(void)
980988
{
981989
hyper_epoll.vsock_ctl_listener.fd = hyper_create_vsock_listener(HYPER_VSOCK_CTL_PORT);
982990
if (hyper_epoll.vsock_ctl_listener.fd < 0)
983-
return -1;
991+
goto out;
984992

985993
hyper_epoll.vsock_msg_listener.fd = hyper_create_vsock_listener(HYPER_VSOCK_MSG_PORT);
986994
if (hyper_epoll.vsock_msg_listener.fd < 0)
995+
goto out;
996+
997+
return 0;
998+
out:
999+
close(hyper_epoll.vsock_ctl_listener.fd);
1000+
close(hyper_epoll.vsock_msg_listener.fd);
1001+
return -1;
1002+
}
1003+
1004+
static int hyper_setup_normal_channel(void)
1005+
{
1006+
char *ctl_serial = NULL, *tty_serial = NULL;
1007+
1008+
#ifdef WITH_VBOX
1009+
ctl_serial = strdup("/dev/ttyS0");
1010+
tty_serial = strdup("/dev/ttyS1");
1011+
is_serial = true;
1012+
1013+
if (hyper_insmod("/vboxguest.ko") < 0 ||
1014+
hyper_insmod("/vboxsf.ko") < 0) {
1015+
fprintf(stderr, "fail to load modules\n");
9871016
return -1;
1017+
}
1018+
#else
1019+
if (is_serial) {
1020+
ctl_serial = strdup("/dev/ttyS1");
1021+
tty_serial = strdup("/dev/ttyS2");
1022+
} else if (is_xen) {
1023+
ctl_serial = strdup("/dev/hvc1");
1024+
tty_serial = strdup("/dev/hvc2");
1025+
is_serial = true;
1026+
} else {
1027+
ctl_serial = hyper_find_virtio_port("sh.hyper.channel.0");
1028+
if (ctl_serial == NULL) {
1029+
fprintf(stderr, "cannot find ctl channel\n");
1030+
goto out;
1031+
}
1032+
tty_serial = hyper_find_virtio_port("sh.hyper.channel.1");
1033+
if (tty_serial == NULL) {
1034+
fprintf(stderr, "cannot find tty channel\n");
1035+
goto out;
1036+
}
1037+
}
1038+
#endif
1039+
fprintf(stdout, "ctl: %s, tty: %s\n", ctl_serial, tty_serial);
1040+
hyper_epoll.ctl.fd = hyper_setup_ctl_channel(ctl_serial);
1041+
if (hyper_epoll.ctl.fd < 0) {
1042+
fprintf(stderr, "fail to setup hyper control serial port\n");
1043+
goto out;
1044+
}
9881045

1046+
hyper_epoll.tty.fd = hyper_setup_tty_channel(tty_serial);
1047+
if (hyper_epoll.tty.fd < 0) {
1048+
fprintf(stderr, "fail to setup hyper tty serial port\n");
1049+
goto out;
1050+
}
1051+
free(ctl_serial);
1052+
free(tty_serial);
9891053
return 0;
1054+
out:
1055+
free(ctl_serial);
1056+
free(tty_serial);
1057+
close(hyper_epoll.ctl.fd);
1058+
close(hyper_epoll.tty.fd);
1059+
return -1;
9901060
}
9911061

9921062
static int hyper_ttyfd_handle(struct hyper_event *de, uint32_t len)
@@ -1561,7 +1631,7 @@ static int hyper_setup_init_process(void)
15611631
return 0;
15621632
}
15631633

1564-
void read_cmdline(bool *use_serial)
1634+
void read_cmdline()
15651635
{
15661636
char buf[512];
15671637
int size;
@@ -1578,17 +1648,19 @@ void read_cmdline(bool *use_serial)
15781648
}
15791649

15801650
if (strstr(buf, HYPER_USE_SERAIL))
1581-
*use_serial= true;
1651+
is_serial = true;
15821652

1653+
if (strstr(buf, HYPER_P9_USE_XEN))
1654+
is_xen = true;
15831655
out:
15841656
close(fd);
15851657
return;
15861658
}
15871659

15881660
int main(int argc, char *argv[])
15891661
{
1590-
char *binary_name, *ctl_serial = NULL, *tty_serial = NULL;
1591-
bool is_init, has_vsock = false, is_serial = false;
1662+
char *binary_name = NULL;
1663+
bool is_init, has_vsock = false;
15921664

15931665
binary_name = basename(argv[0]);
15941666
is_init = strncmp(binary_name, "init", 5) == 0;
@@ -1597,25 +1669,7 @@ int main(int argc, char *argv[])
15971669
return -1;
15981670
}
15991671

1600-
read_cmdline(&is_serial);
1601-
#ifdef WITH_VBOX
1602-
ctl_serial = "/dev/ttyS0";
1603-
tty_serial = "/dev/ttyS1";
1604-
is_serial = true;
1605-
1606-
if (hyper_insmod("/vboxguest.ko") < 0 ||
1607-
hyper_insmod("/vboxsf.ko") < 0) {
1608-
fprintf(stderr, "fail to load modules\n");
1609-
return -1;
1610-
}
1611-
#else
1612-
if (is_serial) {
1613-
ctl_serial = "/dev/ttyS1";
1614-
tty_serial = "/dev/ttyS2";
1615-
} else {
1616-
ctl_serial = strdup("sh.hyper.channel.0");
1617-
tty_serial = strdup("sh.hyper.channel.1");
1618-
}
1672+
read_cmdline();
16191673

16201674
if (probe_vsock_device() <= 0) {
16211675
fprintf(stderr, "cannot find vsock device\n");
@@ -1624,37 +1678,20 @@ int main(int argc, char *argv[])
16241678
} else {
16251679
has_vsock = true;
16261680
}
1627-
#endif
1681+
16281682
if (has_vsock) {
16291683
if (hyper_setup_vsock_channel() < 0) {
16301684
fprintf(stderr, "fail to setup hyper vsock listener\n");
1631-
goto out;
1685+
return -1;
16321686
}
16331687
} else {
1634-
hyper_epoll.ctl.fd = hyper_setup_ctl_channel(ctl_serial, is_serial);
1635-
if (hyper_epoll.ctl.fd < 0) {
1636-
fprintf(stderr, "fail to setup hyper control serial port\n");
1637-
goto out;
1638-
}
1639-
1640-
hyper_epoll.tty.fd = hyper_setup_tty_channel(tty_serial, is_serial);
1641-
if (hyper_epoll.tty.fd < 0) {
1642-
fprintf(stderr, "fail to setup hyper tty serial port\n");
1643-
goto out;
1688+
if (hyper_setup_normal_channel() < 0) {
1689+
fprintf(stderr, "fail to setup hyper serial channel\n");
1690+
return -1;
16441691
}
16451692
}
16461693

16471694
hyper_loop();
16481695

1649-
out:
1650-
if (hyper_epoll.vsock_ctl_listener.fd > 0)
1651-
close(hyper_epoll.vsock_ctl_listener.fd);
1652-
if (hyper_epoll.vsock_msg_listener.fd > 0)
1653-
close(hyper_epoll.vsock_msg_listener.fd);
1654-
if (hyper_epoll.tty.fd > 0)
1655-
close(hyper_epoll.tty.fd);
1656-
if (hyper_epoll.ctl.fd > 0)
1657-
close(hyper_epoll.ctl.fd);
1658-
16591696
return 0;
16601697
}

src/util.c

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ void online_memory(void)
552552
closedir(dir);
553553
}
554554

555-
static int hyper_open_serial(char *channel, int mode)
555+
int hyper_open_channel(char *channel, int mode, bool is_serial)
556556
{
557557
struct termios term;
558558
int fd = open(channel, O_RDWR | O_CLOEXEC | mode);
@@ -562,18 +562,19 @@ static int hyper_open_serial(char *channel, int mode)
562562
}
563563

564564
fprintf(stdout, "open %s get %d\n", channel, fd);
565-
bzero(&term, sizeof(term));
566-
567-
cfmakeraw(&term);
568-
term.c_cflag |= CLOCAL | CREAD| CRTSCTS;
569-
term.c_cc[VTIME] = 0;
570-
term.c_cc[VMIN] = 0;
565+
if (is_serial) {
566+
bzero(&term, sizeof(term));
571567

572-
cfsetispeed(&term, B115200);
573-
cfsetospeed(&term, B115200);
568+
cfmakeraw(&term);
569+
term.c_cflag |= CLOCAL | CREAD| CRTSCTS;
570+
term.c_cc[VTIME] = 0;
571+
term.c_cc[VMIN] = 0;
574572

575-
tcsetattr(fd, TCSANOW, &term);
573+
cfsetispeed(&term, B115200);
574+
cfsetospeed(&term, B115200);
576575

576+
tcsetattr(fd, TCSANOW, &term);
577+
}
577578
return fd;
578579
}
579580

@@ -643,7 +644,7 @@ int hyper_insmod(char *module)
643644
goto out;
644645
}
645646

646-
static int hyper_open_virtio_port(char *channel, int mode)
647+
char* hyper_find_virtio_port(char *channel)
647648
{
648649
struct dirent **list;
649650
struct dirent *dir;
@@ -653,7 +654,7 @@ static int hyper_open_virtio_port(char *channel, int mode)
653654
num = scandir("/sys/class/virtio-ports/", &list, NULL, NULL);
654655
if (num < 0) {
655656
perror("scan /sys/class/virtio-ports/ failed");
656-
return -1;
657+
return NULL;
657658
}
658659

659660
memset(path, 0, sizeof(path));
@@ -677,9 +678,7 @@ static int hyper_open_virtio_port(char *channel, int mode)
677678
close(fd);
678679
continue;
679680
}
680-
681681
close(fd);
682-
fd = -1;
683682

684683
if (strncmp(name, channel, strlen(channel))) {
685684
continue;
@@ -690,24 +689,13 @@ static int hyper_open_virtio_port(char *channel, int mode)
690689
continue;
691690
}
692691

693-
fprintf(stdout, "open hyper channel %s\n", path);
694-
fd = open(path, O_RDWR | O_CLOEXEC | mode);
695-
if (fd < 0)
696-
perror("fail to open channel device");
697-
698-
break;
692+
fprintf(stdout, "find hyper channel %s\n", path);
693+
free(list);
694+
return strdup(path);
699695
}
700696

701697
free(list);
702-
return fd;
703-
}
704-
705-
int hyper_open_channel(char *channel, int mode, bool is_serial)
706-
{
707-
if (is_serial)
708-
return hyper_open_serial(channel, mode);
709-
710-
return hyper_open_virtio_port(channel, mode);
698+
return NULL;
711699
}
712700

713701
int hyper_setfd_cloexec(int fd)

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ int hyper_mkdir(char *path, mode_t mode);
3535
int hyper_mkdir_at(const char *root, char *path, int size);
3636
int hyper_write_file(const char *path, const char *value, size_t len);
3737
int hyper_open_channel(char *channel, int mode, bool is_serail);
38+
char* hyper_find_virtio_port(char *channel);
3839
int hyper_setfd_cloexec(int fd);
3940
int hyper_setfd_block(int fd);
4041
int hyper_setfd_nonblock(int fd);

0 commit comments

Comments
 (0)