1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
18 #include "guest_channel.h"
19 #include "channel_commands.h"
21 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
23 /* Timeout for incoming message in milliseconds. */
26 static int global_fds[RTE_MAX_LCORE] = { [0 ... RTE_MAX_LCORE-1] = -1 };
29 guest_channel_host_connect(const char *path, unsigned int lcore_id)
32 struct channel_packet pkt;
33 char fd_path[PATH_MAX];
36 if (lcore_id >= RTE_MAX_LCORE) {
37 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
38 lcore_id, RTE_MAX_LCORE-1);
41 /* check if path is already open */
42 if (global_fds[lcore_id] != -1) {
43 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is already open with fd %d\n",
44 lcore_id, global_fds[lcore_id]);
48 snprintf(fd_path, PATH_MAX, "%s.%u", path, lcore_id);
49 RTE_LOG(INFO, GUEST_CHANNEL, "Opening channel '%s' for lcore %u\n",
51 fd = open(fd_path, O_RDWR);
53 RTE_LOG(ERR, GUEST_CHANNEL, "Unable to to connect to '%s' with error "
54 "%s\n", fd_path, strerror(errno));
58 flags = fcntl(fd, F_GETFL, 0);
60 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on fcntl get flags for file %s\n",
66 if (fcntl(fd, F_SETFL, flags) < 0) {
67 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on setting non-blocking mode for "
71 /* QEMU needs a delay after connection */
74 /* Send a test packet, this command is ignored by the host, but a successful
75 * send indicates that the host endpoint is monitoring.
77 pkt.command = CPU_POWER_CONNECT;
78 global_fds[lcore_id] = fd;
79 ret = guest_channel_send_msg(&pkt, lcore_id);
81 RTE_LOG(ERR, GUEST_CHANNEL,
82 "Error on channel '%s' communications test: %s\n",
83 fd_path, ret > 0 ? strerror(ret) :
84 "channel not connected");
87 RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
91 global_fds[lcore_id] = -1;
96 guest_channel_send_msg(struct channel_packet *pkt, unsigned int lcore_id)
98 int ret, buffer_len = sizeof(*pkt);
101 if (lcore_id >= RTE_MAX_LCORE) {
102 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
103 lcore_id, RTE_MAX_LCORE-1);
107 if (global_fds[lcore_id] < 0) {
108 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
111 while (buffer_len > 0) {
112 ret = write(global_fds[lcore_id], buffer, buffer_len);
113 if (ret == buffer_len)
120 buffer = (char *)buffer + ret;
126 int rte_power_guest_channel_send_msg(struct channel_packet *pkt,
127 unsigned int lcore_id)
129 return guest_channel_send_msg(pkt, lcore_id);
132 int power_guest_channel_read_msg(void *pkt,
134 unsigned int lcore_id)
139 if (pkt_len == 0 || pkt == NULL)
142 fds.fd = global_fds[lcore_id];
145 ret = poll(&fds, 1, TIMEOUT);
147 RTE_LOG(DEBUG, GUEST_CHANNEL, "Timeout occurred during poll function.\n");
149 } else if (ret < 0) {
150 RTE_LOG(ERR, GUEST_CHANNEL, "Error occurred during poll function: %s\n",
155 if (lcore_id >= RTE_MAX_LCORE) {
156 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
157 lcore_id, RTE_MAX_LCORE-1);
161 if (global_fds[lcore_id] < 0) {
162 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
166 while (pkt_len > 0) {
167 ret = read(global_fds[lcore_id],
177 RTE_LOG(ERR, GUEST_CHANNEL, "Expected more data, but connection has been closed.\n");
180 pkt = (char *)pkt + ret;
187 int rte_power_guest_channel_receive_msg(void *pkt,
189 unsigned int lcore_id)
191 return power_guest_channel_read_msg(pkt, pkt_len, lcore_id);
195 guest_channel_host_disconnect(unsigned int lcore_id)
197 if (lcore_id >= RTE_MAX_LCORE) {
198 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
199 lcore_id, RTE_MAX_LCORE-1);
202 if (global_fds[lcore_id] < 0)
204 close(global_fds[lcore_id]);
205 global_fds[lcore_id] = -1;