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

Commit 4af23f7

Browse files
committed
create hyper events for new vsock connections
Set up control channel and message channel if unset yet. Signed-off-by: Peng Tao <bergwolf@gmail.com>
1 parent 626ccb1 commit 4af23f7

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

src/init.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,8 +1262,22 @@ static struct hyper_event_ops hyper_ttyfd_ops = {
12621262
.wbuf_size = 10240,
12631263
};
12641264

1265-
static struct hyper_event_ops hyper_vsock_listen_ops = {
1266-
.read = hyper_vsock_accept,
1265+
static int hyper_vsock_ctl_accept(struct hyper_event *he, int efd, int events)
1266+
{
1267+
return hyper_vsock_accept(he, efd, &hyper_epoll.ctl, &hyper_ctlfd_ops);
1268+
}
1269+
1270+
static int hyper_vsock_msg_accept(struct hyper_event *he, int efd, int events)
1271+
{
1272+
return hyper_vsock_accept(he, efd, &hyper_epoll.tty, &hyper_ttyfd_ops);
1273+
}
1274+
1275+
static struct hyper_event_ops hyper_vsock_ctl_listen_ops = {
1276+
.read = hyper_vsock_ctl_accept,
1277+
};
1278+
1279+
static struct hyper_event_ops hyper_vsock_msg_listen_ops = {
1280+
.read = hyper_vsock_msg_accept,
12671281
};
12681282

12691283
static int hyper_loop(void)
@@ -1340,14 +1354,14 @@ static int hyper_loop(void)
13401354

13411355
if (hyper_epoll.vsock_ctl_listener.fd > 0) {
13421356
fprintf(stdout, "hyper_init_event hyper vsock control channel listener event %p, ops %p, fd %d\n",
1343-
&hyper_epoll.vsock_ctl_listener, &hyper_vsock_listen_ops, hyper_epoll.vsock_ctl_listener.fd);
1344-
if (hyper_init_event(&hyper_epoll.vsock_ctl_listener, &hyper_vsock_listen_ops, pod) < 0 ||
1357+
&hyper_epoll.vsock_ctl_listener, &hyper_vsock_ctl_listen_ops, hyper_epoll.vsock_ctl_listener.fd);
1358+
if (hyper_init_event(&hyper_epoll.vsock_ctl_listener, &hyper_vsock_ctl_listen_ops, pod) < 0 ||
13451359
hyper_add_event(hyper_epoll.efd, &hyper_epoll.vsock_ctl_listener, EPOLLIN) < 0) {
13461360
return -1;
13471361
}
13481362
fprintf(stdout, "hyper_init_event hyper vsock message channel listener event %p, ops %p, fd %d\n",
1349-
&hyper_epoll.vsock_msg_listener, &hyper_vsock_listen_ops, hyper_epoll.vsock_msg_listener.fd);
1350-
if (hyper_init_event(&hyper_epoll.vsock_msg_listener, &hyper_vsock_listen_ops, pod) < 0 ||
1363+
&hyper_epoll.vsock_msg_listener, &hyper_vsock_msg_listen_ops, hyper_epoll.vsock_msg_listener.fd);
1364+
if (hyper_init_event(&hyper_epoll.vsock_msg_listener, &hyper_vsock_msg_listen_ops, pod) < 0 ||
13511365
hyper_add_event(hyper_epoll.efd, &hyper_epoll.vsock_msg_listener, EPOLLIN) < 0) {
13521366
return -1;
13531367
}

src/vsock.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <linux/virtio_ids.h>
1313
#include <linux/vm_sockets.h>
1414

15-
#include "event.h"
15+
#include "vsock.h"
1616

1717
/* for pre-vsock kernels. */
1818
#ifndef VIRTIO_ID_VSOCK
@@ -115,7 +115,8 @@ int hyper_create_vsock_listener(unsigned short port)
115115
return fd;
116116
}
117117

118-
int hyper_vsock_accept(struct hyper_event *he, int efd, int events)
118+
int hyper_vsock_accept(struct hyper_event *he, int efd,
119+
struct hyper_event *ne, struct hyper_event_ops *ops)
119120
{
120121
int ret = 0;
121122

@@ -137,7 +138,25 @@ int hyper_vsock_accept(struct hyper_event *he, int efd, int events)
137138
}
138139
fprintf(stdout, "vsock connection from cid %u port %u\n",
139140
sa_client.svm_cid, sa_client.svm_port);
140-
close(fd);
141+
142+
/* only accept host vsock connections */
143+
if (sa_client.svm_cid != VMADDR_CID_HOST) {
144+
close(fd);
145+
continue;
146+
}
147+
148+
/* oh, we already have it! */
149+
if (ne->fd > 0) {
150+
close(fd);
151+
continue;
152+
}
153+
154+
ne->fd = fd;
155+
if (hyper_init_event(ne, ops, he->ptr) < 0 ||
156+
hyper_add_event(efd, ne, EPOLLIN) < 0) {
157+
hyper_reset_event(ne);
158+
return -1;
159+
}
141160
}
142161

143162
return ret;

src/vsock.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#ifndef _VSOCK_H_
22
#define _VSOCK_H_
33

4+
#include "event.h"
5+
46
int probe_vsock_device(void);
57
int hyper_create_vsock_listener(unsigned short port);
6-
int hyper_vsock_accept(struct hyper_event *he, int efd, int events);
8+
int hyper_vsock_accept(struct hyper_event *he, int efd,
9+
struct hyper_event *ne, struct hyper_event_ops *ops);
710

811
#endif

0 commit comments

Comments
 (0)