1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
17 #include "guest_channel.h"
18 #include "channel_commands.h"
20 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
22 static int global_fds[RTE_MAX_LCORE];
25 guest_channel_host_connect(const char *path, unsigned int lcore_id)
28 struct channel_packet pkt;
29 char fd_path[PATH_MAX];
32 if (lcore_id >= RTE_MAX_LCORE) {
33 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
34 lcore_id, RTE_MAX_LCORE-1);
37 /* check if path is already open */
38 if (global_fds[lcore_id] != 0) {
39 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is already open with fd %d\n",
40 lcore_id, global_fds[lcore_id]);
44 snprintf(fd_path, PATH_MAX, "%s.%u", path, lcore_id);
45 RTE_LOG(INFO, GUEST_CHANNEL, "Opening channel '%s' for lcore %u\n",
47 fd = open(fd_path, O_RDWR);
49 RTE_LOG(ERR, GUEST_CHANNEL, "Unable to to connect to '%s' with error "
50 "%s\n", fd_path, strerror(errno));
54 flags = fcntl(fd, F_GETFL, 0);
56 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on fcntl get flags for file %s\n",
62 if (fcntl(fd, F_SETFL, flags) < 0) {
63 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on setting non-blocking mode for "
67 /* QEMU needs a delay after connection */
70 /* Send a test packet, this command is ignored by the host, but a successful
71 * send indicates that the host endpoint is monitoring.
73 pkt.command = CPU_POWER_CONNECT;
74 global_fds[lcore_id] = fd;
75 ret = guest_channel_send_msg(&pkt, lcore_id);
77 RTE_LOG(ERR, GUEST_CHANNEL,
78 "Error on channel '%s' communications test: %s\n",
79 fd_path, ret > 0 ? strerror(ret) :
80 "channel not connected");
83 RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
87 global_fds[lcore_id] = 0;
92 guest_channel_send_msg(struct channel_packet *pkt, unsigned int lcore_id)
94 int ret, buffer_len = sizeof(*pkt);
97 if (lcore_id >= RTE_MAX_LCORE) {
98 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
99 lcore_id, RTE_MAX_LCORE-1);
103 if (global_fds[lcore_id] == 0) {
104 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
107 while (buffer_len > 0) {
108 ret = write(global_fds[lcore_id], buffer, buffer_len);
109 if (ret == buffer_len)
116 buffer = (char *)buffer + ret;
122 int rte_power_guest_channel_send_msg(struct channel_packet *pkt,
123 unsigned int lcore_id)
125 return guest_channel_send_msg(pkt, lcore_id);
130 guest_channel_host_disconnect(unsigned int lcore_id)
132 if (lcore_id >= RTE_MAX_LCORE) {
133 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
134 lcore_id, RTE_MAX_LCORE-1);
137 if (global_fds[lcore_id] == 0)
139 close(global_fds[lcore_id]);
140 global_fds[lcore_id] = 0;