1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
18 #include <rte_power.h>
20 #include "guest_channel.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 rte_power_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 = RTE_POWER_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 rte_power_channel_packet *pkt,
123 unsigned int lcore_id)
125 int ret, buffer_len = sizeof(*pkt);
128 if (lcore_id >= RTE_MAX_LCORE) {
129 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
130 lcore_id, RTE_MAX_LCORE-1);
134 if (global_fds[lcore_id] < 0) {
135 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
138 while (buffer_len > 0) {
139 ret = write(global_fds[lcore_id], buffer, buffer_len);
140 if (ret == buffer_len)
147 buffer = (char *)buffer + ret;
153 int rte_power_guest_channel_send_msg(struct rte_power_channel_packet *pkt,
154 unsigned int lcore_id)
156 return guest_channel_send_msg(pkt, lcore_id);
159 int power_guest_channel_read_msg(void *pkt,
161 unsigned int lcore_id)
166 if (pkt_len == 0 || pkt == NULL)
169 if (lcore_id >= RTE_MAX_LCORE) {
170 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
171 lcore_id, RTE_MAX_LCORE-1);
175 if (global_fds[lcore_id] < 0) {
176 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
180 fds.fd = global_fds[lcore_id];
183 ret = poll(&fds, 1, TIMEOUT);
185 RTE_LOG(DEBUG, GUEST_CHANNEL, "Timeout occurred during poll function.\n");
187 } else if (ret < 0) {
188 RTE_LOG(ERR, GUEST_CHANNEL, "Error occurred during poll function: %s\n",
193 while (pkt_len > 0) {
194 ret = read(global_fds[lcore_id],
204 RTE_LOG(ERR, GUEST_CHANNEL, "Expected more data, but connection has been closed.\n");
207 pkt = (char *)pkt + ret;
214 int rte_power_guest_channel_receive_msg(void *pkt,
216 unsigned int lcore_id)
218 return power_guest_channel_read_msg(pkt, pkt_len, lcore_id);
222 guest_channel_host_disconnect(unsigned int lcore_id)
224 if (lcore_id >= RTE_MAX_LCORE) {
225 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
226 lcore_id, RTE_MAX_LCORE-1);
229 if (global_fds[lcore_id] < 0)
231 close(global_fds[lcore_id]);
232 global_fds[lcore_id] = -1;