This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathchannel.go
236 lines (192 loc) · 4.51 KB
/
channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
// Copyright (c) 2017 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/hashicorp/yamux"
"github.com/mdlayher/vsock"
"golang.org/x/sys/unix"
"google.golang.org/grpc/codes"
grpcStatus "google.golang.org/grpc/status"
)
type channel interface {
setup() error
wait() error
listen() (net.Listener, error)
teardown() error
}
func newChannel() (channel, error) {
// Check for vsock support.
vSockSupported, err := isAFVSockSupported()
if err != nil {
return nil, err
}
if vSockSupported {
// Check if vsock socket exists. We want to cover the case
// where the guest OS can support vsock, but the runtime is
// still using virtio serial connection.
exist, err := vSockPathExist()
if err != nil {
return nil, err
}
if exist {
return &vSockChannel{}, nil
}
}
return &serialChannel{}, nil
}
type vSockChannel struct {
}
func (c *vSockChannel) setup() error {
return nil
}
func (c *vSockChannel) wait() error {
return nil
}
func (c *vSockChannel) listen() (net.Listener, error) {
l, err := vsock.Listen(vSockPort)
if err != nil {
return nil, err
}
return l, nil
}
func (c *vSockChannel) teardown() error {
return nil
}
type serialChannel struct {
serialConn *os.File
}
func (c *serialChannel) setup() error {
// Open serial channel.
file, err := openSerialChannel()
if err != nil {
return err
}
c.serialConn = file
return nil
}
func (c *serialChannel) wait() error {
var event syscall.EpollEvent
var events [1]syscall.EpollEvent
fd := c.serialConn.Fd()
if fd <= 0 {
return fmt.Errorf("serial port IO closed")
}
epfd, err := syscall.EpollCreate1(syscall.EPOLL_CLOEXEC)
if err != nil {
return err
}
defer syscall.Close(epfd)
// EPOLLOUT: Writable when there is a connection
// EPOLLET: Edge trigger as EPOLLHUP is always on when there is no connection
// 0xffffffff: EPOLLET is negative and cannot fit in uint32 in golang
event.Events = syscall.EPOLLOUT | syscall.EPOLLET&0xffffffff
event.Fd = int32(fd)
if err = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, int(fd), &event); err != nil {
return err
}
defer syscall.EpollCtl(epfd, syscall.EPOLL_CTL_DEL, int(fd), nil)
for {
nev, err := syscall.EpollWait(epfd, events[:], -1)
if err != nil {
return err
}
for i := 0; i < nev; i++ {
ev := events[i]
if ev.Fd == int32(fd) {
agentLog.WithField("events", ev.Events).Debug("New serial channel event")
if ev.Events&syscall.EPOLLOUT != 0 {
return nil
}
if ev.Events&syscall.EPOLLERR != 0 {
return fmt.Errorf("serial port IO failure")
}
if ev.Events&syscall.EPOLLHUP != 0 {
continue
}
}
}
}
// Never reach here
}
func (c *serialChannel) listen() (net.Listener, error) {
// Initialize Yamux server.
session, err := yamux.Server(c.serialConn, nil)
if err != nil {
return nil, err
}
return session, nil
}
func (c *serialChannel) teardown() error {
return c.serialConn.Close()
}
func isAFVSockSupported() (bool, error) {
fd, err := unix.Socket(unix.AF_VSOCK, unix.SOCK_STREAM, 0)
if err != nil {
// This case is valid. It means AF_VSOCK is not a supported
// domain on this system.
if err == unix.EAFNOSUPPORT {
return false, nil
}
return false, err
}
if err := unix.Close(fd); err != nil {
return true, err
}
return true, nil
}
func vSockPathExist() (bool, error) {
if _, err := os.Stat(vSockDevPath); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func findVirtualSerialPath(serialName string) (string, error) {
dir, err := os.Open(virtIOPath)
if err != nil {
return "", err
}
defer dir.Close()
ports, err := dir.Readdirnames(0)
if err != nil {
return "", err
}
for _, port := range ports {
path := filepath.Join(virtIOPath, port, "name")
content, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
agentLog.WithField("file", path).Debug("Skip parsing of non-existent file")
continue
}
return "", err
}
if strings.Contains(string(content), serialName) == true {
return filepath.Join(devRootPath, port), nil
}
}
return "", grpcStatus.Errorf(codes.NotFound, "Could not find virtio port %s", serialName)
}
func openSerialChannel() (*os.File, error) {
serialPath, err := findVirtualSerialPath(serialChannelName)
if err != nil {
return nil, err
}
file, err := os.OpenFile(serialPath, os.O_RDWR, os.ModeDevice)
if err != nil {
return nil, err
}
return file, nil
}