1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
19 #include "guest_channel.h"
20 #include "channel_commands.h"
22 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
24 /* Timeout for incoming message in milliseconds. */
27 static int global_fds[RTE_MAX_LCORE] = { [0 ... RTE_MAX_LCORE-1] = -1 };
30 guest_channel_host_check_exists(const char *path)
32 char glob_path[PATH_MAX];
36 /* we cannot know in advance which cores have VM channels, so glob */
37 snprintf(glob_path, PATH_MAX, "%s.*", path);
39 ret = glob(glob_path, GLOB_NOSORT, NULL, &g);
41 /* couldn't read anything */
46 /* do we have at least one match? */
55 guest_channel_host_connect(const char *path, unsigned int lcore_id)
58 struct channel_packet pkt;
59 char fd_path[PATH_MAX];
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);
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]);
74 snprintf(fd_path, PATH_MAX, "%s.%u", path, lcore_id);
75 RTE_LOG(INFO, GUEST_CHANNEL, "Opening channel '%s' for lcore %u\n",
77 fd = open(fd_path, O_RDWR);
79 RTE_LOG(ERR, GUEST_CHANNEL, "Unable to to connect to '%s' with error "
80 "%s\n", fd_path, strerror(errno));
84 flags = fcntl(fd, F_GETFL, 0);
86 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on fcntl get flags for file %s\n",
92 if (fcntl(fd, F_SETFL, flags) < 0) {
93 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on setting non-blocking mode for "
97 /* QEMU needs a delay after connection */
100 /* Send a test packet, this command is ignored by the host, but a successful
101 * send indicates that the host endpoint is monitoring.
103 pkt.command = CPU_POWER_CONNECT;
104 global_fds[lcore_id] = fd;
105 ret = guest_channel_send_msg(&pkt, lcore_id);
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");
113 RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
117 global_fds[lcore_id] = -1;
122 guest_channel_send_msg(struct channel_packet *pkt, unsigned int lcore_id)
124 int ret, buffer_len = sizeof(*pkt);
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);
133 if (global_fds[lcore_id] < 0) {
134 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
137 while (buffer_len > 0) {
138 ret = write(global_fds[lcore_id], buffer, buffer_len);
139 if (ret == buffer_len)
146 buffer = (char *)buffer + ret;
152 int rte_power_guest_channel_send_msg(struct channel_packet *pkt,
153 unsigned int lcore_id)
155 return guest_channel_send_msg(pkt, lcore_id);
158 int power_guest_channel_read_msg(void *pkt,
160 unsigned int lcore_id)
165 if (pkt_len == 0 || pkt == NULL)
168 fds.fd = global_fds[lcore_id];
171 ret = poll(&fds, 1, TIMEOUT);
173 RTE_LOG(DEBUG, GUEST_CHANNEL, "Timeout occurred during poll function.\n");
175 } else if (ret < 0) {
176 RTE_LOG(ERR, GUEST_CHANNEL, "Error occurred during poll function: %s\n",
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);
187 if (global_fds[lcore_id] < 0) {
188 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
192 while (pkt_len > 0) {
193 ret = read(global_fds[lcore_id],
203 RTE_LOG(ERR, GUEST_CHANNEL, "Expected more data, but connection has been closed.\n");
206 pkt = (char *)pkt + ret;
213 int rte_power_guest_channel_receive_msg(void *pkt,
215 unsigned int lcore_id)
217 return power_guest_channel_read_msg(pkt, pkt_len, lcore_id);
221 guest_channel_host_disconnect(unsigned int lcore_id)
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);
228 if (global_fds[lcore_id] < 0)
230 close(global_fds[lcore_id]);
231 global_fds[lcore_id] = -1;