1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <glob.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <signal.h>
10 #include <limits.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <poll.h>
15 
16 
17 #include <rte_log.h>
18 
19 #include "guest_channel.h"
20 #include "channel_commands.h"
21 
22 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
23 
24 /* Timeout for incoming message in milliseconds. */
25 #define TIMEOUT 10
26 
27 static int global_fds[RTE_MAX_LCORE] = { [0 ... RTE_MAX_LCORE-1] = -1 };
28 
29 int
guest_channel_host_check_exists(const char * path)30 guest_channel_host_check_exists(const char *path)
31 {
32 	char glob_path[PATH_MAX];
33 	glob_t g;
34 	int ret;
35 
36 	/* we cannot know in advance which cores have VM channels, so glob */
37 	snprintf(glob_path, PATH_MAX, "%s.*", path);
38 
39 	ret = glob(glob_path, GLOB_NOSORT, NULL, &g);
40 	if (ret != 0) {
41 		/* couldn't read anything */
42 		ret = 0;
43 		goto out;
44 	}
45 
46 	/* do we have at least one match? */
47 	ret = g.gl_pathc > 0;
48 
49 out:
50 	globfree(&g);
51 	return ret;
52 }
53 
54 int
guest_channel_host_connect(const char * path,unsigned int lcore_id)55 guest_channel_host_connect(const char *path, unsigned int lcore_id)
56 {
57 	int flags, ret;
58 	struct channel_packet pkt;
59 	char fd_path[PATH_MAX];
60 	int fd = -1;
61 
62 	if (lcore_id >= RTE_MAX_LCORE) {
63 		RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
64 				lcore_id, RTE_MAX_LCORE-1);
65 		return -1;
66 	}
67 	/* check if path is already open */
68 	if (global_fds[lcore_id] != -1) {
69 		RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is already open with fd %d\n",
70 				lcore_id, global_fds[lcore_id]);
71 		return -1;
72 	}
73 
74 	snprintf(fd_path, PATH_MAX, "%s.%u", path, lcore_id);
75 	RTE_LOG(INFO, GUEST_CHANNEL, "Opening channel '%s' for lcore %u\n",
76 			fd_path, lcore_id);
77 	fd = open(fd_path, O_RDWR);
78 	if (fd < 0) {
79 		RTE_LOG(ERR, GUEST_CHANNEL, "Unable to to connect to '%s' with error "
80 				"%s\n", fd_path, strerror(errno));
81 		return -1;
82 	}
83 
84 	flags = fcntl(fd, F_GETFL, 0);
85 	if (flags < 0) {
86 		RTE_LOG(ERR, GUEST_CHANNEL, "Failed on fcntl get flags for file %s\n",
87 				fd_path);
88 		goto error;
89 	}
90 
91 	flags |= O_NONBLOCK;
92 	if (fcntl(fd, F_SETFL, flags) < 0) {
93 		RTE_LOG(ERR, GUEST_CHANNEL, "Failed on setting non-blocking mode for "
94 				"file %s", fd_path);
95 		goto error;
96 	}
97 	/* QEMU needs a delay after connection */
98 	sleep(1);
99 
100 	/* Send a test packet, this command is ignored by the host, but a successful
101 	 * send indicates that the host endpoint is monitoring.
102 	 */
103 	pkt.command = CPU_POWER_CONNECT;
104 	global_fds[lcore_id] = fd;
105 	ret = guest_channel_send_msg(&pkt, lcore_id);
106 	if (ret != 0) {
107 		RTE_LOG(ERR, GUEST_CHANNEL,
108 				"Error on channel '%s' communications test: %s\n",
109 				fd_path, ret > 0 ? strerror(ret) :
110 				"channel not connected");
111 		goto error;
112 	}
113 	RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
114 	return 0;
115 error:
116 	close(fd);
117 	global_fds[lcore_id] = -1;
118 	return -1;
119 }
120 
121 int
guest_channel_send_msg(struct channel_packet * pkt,unsigned int lcore_id)122 guest_channel_send_msg(struct channel_packet *pkt, unsigned int lcore_id)
123 {
124 	int ret, buffer_len = sizeof(*pkt);
125 	void *buffer = pkt;
126 
127 	if (lcore_id >= RTE_MAX_LCORE) {
128 		RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
129 				lcore_id, RTE_MAX_LCORE-1);
130 		return -1;
131 	}
132 
133 	if (global_fds[lcore_id] < 0) {
134 		RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
135 		return -1;
136 	}
137 	while (buffer_len > 0) {
138 		ret = write(global_fds[lcore_id], buffer, buffer_len);
139 		if (ret == buffer_len)
140 			return 0;
141 		if (ret == -1) {
142 			if (errno == EINTR)
143 				continue;
144 			return errno;
145 		}
146 		buffer = (char *)buffer + ret;
147 		buffer_len -= ret;
148 	}
149 	return 0;
150 }
151 
rte_power_guest_channel_send_msg(struct channel_packet * pkt,unsigned int lcore_id)152 int rte_power_guest_channel_send_msg(struct channel_packet *pkt,
153 			unsigned int lcore_id)
154 {
155 	return guest_channel_send_msg(pkt, lcore_id);
156 }
157 
power_guest_channel_read_msg(void * pkt,size_t pkt_len,unsigned int lcore_id)158 int power_guest_channel_read_msg(void *pkt,
159 		size_t pkt_len,
160 		unsigned int lcore_id)
161 {
162 	int ret;
163 	struct pollfd fds;
164 
165 	if (pkt_len == 0 || pkt == NULL)
166 		return -1;
167 
168 	fds.fd = global_fds[lcore_id];
169 	fds.events = POLLIN;
170 
171 	ret = poll(&fds, 1, TIMEOUT);
172 	if (ret == 0) {
173 		RTE_LOG(DEBUG, GUEST_CHANNEL, "Timeout occurred during poll function.\n");
174 		return -1;
175 	} else if (ret < 0) {
176 		RTE_LOG(ERR, GUEST_CHANNEL, "Error occurred during poll function: %s\n",
177 				strerror(errno));
178 		return -1;
179 	}
180 
181 	if (lcore_id >= RTE_MAX_LCORE) {
182 		RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
183 				lcore_id, RTE_MAX_LCORE-1);
184 		return -1;
185 	}
186 
187 	if (global_fds[lcore_id] < 0) {
188 		RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
189 		return -1;
190 	}
191 
192 	while (pkt_len > 0) {
193 		ret = read(global_fds[lcore_id],
194 				pkt, pkt_len);
195 
196 		if (ret < 0) {
197 			if (errno == EINTR)
198 				continue;
199 			return -1;
200 		}
201 
202 		if (ret == 0) {
203 			RTE_LOG(ERR, GUEST_CHANNEL, "Expected more data, but connection has been closed.\n");
204 			return -1;
205 		}
206 		pkt = (char *)pkt + ret;
207 		pkt_len -= ret;
208 	}
209 
210 	return 0;
211 }
212 
rte_power_guest_channel_receive_msg(void * pkt,size_t pkt_len,unsigned int lcore_id)213 int rte_power_guest_channel_receive_msg(void *pkt,
214 		size_t pkt_len,
215 		unsigned int lcore_id)
216 {
217 	return power_guest_channel_read_msg(pkt, pkt_len, lcore_id);
218 }
219 
220 void
guest_channel_host_disconnect(unsigned int lcore_id)221 guest_channel_host_disconnect(unsigned int lcore_id)
222 {
223 	if (lcore_id >= RTE_MAX_LCORE) {
224 		RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
225 				lcore_id, RTE_MAX_LCORE-1);
226 		return;
227 	}
228 	if (global_fds[lcore_id] < 0)
229 		return;
230 	close(global_fds[lcore_id]);
231 	global_fds[lcore_id] = -1;
232 }
233