4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 #include <rte_config.h>
47 #include "guest_channel.h"
48 #include "channel_commands.h"
50 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
52 static int global_fds[RTE_MAX_LCORE];
55 guest_channel_host_connect(const char *path, unsigned 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] != 0) {
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, "Error on channel '%s' communications "
108 "test: %s\n", fd_path, strerror(ret));
111 RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
115 global_fds[lcore_id] = 0;
120 guest_channel_send_msg(struct channel_packet *pkt, unsigned lcore_id)
122 int ret, buffer_len = sizeof(*pkt);
125 if (lcore_id >= RTE_MAX_LCORE) {
126 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
127 lcore_id, RTE_MAX_LCORE-1);
131 if (global_fds[lcore_id] == 0) {
132 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
135 while (buffer_len > 0) {
136 ret = write(global_fds[lcore_id], buffer, buffer_len);
137 if (ret == buffer_len)
144 buffer = (char *)buffer + ret;
151 guest_channel_host_disconnect(unsigned lcore_id)
153 if (lcore_id >= RTE_MAX_LCORE) {
154 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
155 lcore_id, RTE_MAX_LCORE-1);
158 if (global_fds[lcore_id] == 0)
160 close(global_fds[lcore_id]);
161 global_fds[lcore_id] = 0;