1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
16 #include <rte_power.h>
18 #include "guest_channel.h"
20 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
22 /* Timeout for incoming message in milliseconds. */
25 static int global_fds[RTE_MAX_LCORE] = { [0 ... RTE_MAX_LCORE-1] = -1 };
28 guest_channel_host_check_exists(const char *path)
30 char glob_path[PATH_MAX];
34 /* we cannot know in advance which cores have VM channels, so glob */
35 snprintf(glob_path, PATH_MAX, "%s.*", path);
37 ret = glob(glob_path, GLOB_NOSORT, NULL, &g);
39 /* couldn't read anything */
44 /* do we have at least one match? */
53 guest_channel_host_connect(const char *path, unsigned int lcore_id)
56 struct rte_power_channel_packet pkt;
57 char fd_path[PATH_MAX];
60 if (lcore_id >= RTE_MAX_LCORE) {
61 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
62 lcore_id, RTE_MAX_LCORE-1);
65 /* check if path is already open */
66 if (global_fds[lcore_id] != -1) {
67 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is already open with fd %d\n",
68 lcore_id, global_fds[lcore_id]);
72 snprintf(fd_path, PATH_MAX, "%s.%u", path, lcore_id);
73 RTE_LOG(INFO, GUEST_CHANNEL, "Opening channel '%s' for lcore %u\n",
75 fd = open(fd_path, O_RDWR);
77 RTE_LOG(ERR, GUEST_CHANNEL, "Unable to to connect to '%s' with error "
78 "%s\n", fd_path, strerror(errno));
82 flags = fcntl(fd, F_GETFL, 0);
84 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on fcntl get flags for file %s\n",
90 if (fcntl(fd, F_SETFL, flags) < 0) {
91 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on setting non-blocking mode for "
95 /* QEMU needs a delay after connection */
98 /* Send a test packet, this command is ignored by the host, but a successful
99 * send indicates that the host endpoint is monitoring.
101 pkt.command = RTE_POWER_CPU_POWER_CONNECT;
102 global_fds[lcore_id] = fd;
103 ret = guest_channel_send_msg(&pkt, lcore_id);
105 RTE_LOG(ERR, GUEST_CHANNEL,
106 "Error on channel '%s' communications test: %s\n",
107 fd_path, ret > 0 ? strerror(ret) :
108 "channel not connected");
111 RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
115 global_fds[lcore_id] = -1;
120 guest_channel_send_msg(struct rte_power_channel_packet *pkt,
121 unsigned int lcore_id)
123 int ret, buffer_len = sizeof(*pkt);
126 if (lcore_id >= RTE_MAX_LCORE) {
127 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
128 lcore_id, RTE_MAX_LCORE-1);
132 if (global_fds[lcore_id] < 0) {
133 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
136 while (buffer_len > 0) {
137 ret = write(global_fds[lcore_id], buffer, buffer_len);
138 if (ret == buffer_len)
145 buffer = (char *)buffer + ret;
151 int rte_power_guest_channel_send_msg(struct rte_power_channel_packet *pkt,
152 unsigned int lcore_id)
154 return guest_channel_send_msg(pkt, lcore_id);
157 int power_guest_channel_read_msg(void *pkt,
159 unsigned int lcore_id)
164 if (pkt_len == 0 || pkt == NULL)
167 if (lcore_id >= RTE_MAX_LCORE) {
168 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
169 lcore_id, RTE_MAX_LCORE-1);
173 if (global_fds[lcore_id] < 0) {
174 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
178 fds.fd = global_fds[lcore_id];
181 ret = poll(&fds, 1, TIMEOUT);
183 RTE_LOG(DEBUG, GUEST_CHANNEL, "Timeout occurred during poll function.\n");
185 } else if (ret < 0) {
186 RTE_LOG(ERR, GUEST_CHANNEL, "Error occurred during poll function: %s\n",
191 while (pkt_len > 0) {
192 ret = read(global_fds[lcore_id],
202 RTE_LOG(ERR, GUEST_CHANNEL, "Expected more data, but connection has been closed.\n");
205 pkt = (char *)pkt + ret;
212 int rte_power_guest_channel_receive_msg(void *pkt,
214 unsigned int lcore_id)
216 return power_guest_channel_read_msg(pkt, pkt_len, lcore_id);
220 guest_channel_host_disconnect(unsigned int lcore_id)
222 if (lcore_id >= RTE_MAX_LCORE) {
223 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
224 lcore_id, RTE_MAX_LCORE-1);
227 if (global_fds[lcore_id] < 0)
229 close(global_fds[lcore_id]);
230 global_fds[lcore_id] = -1;