lib: remove unneeded header includes
[dpdk.git] / lib / power / guest_channel.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <glob.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <poll.h>
13
14
15 #include <rte_log.h>
16 #include <rte_power.h>
17
18 #include "guest_channel.h"
19
20 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
21
22 /* Timeout for incoming message in milliseconds. */
23 #define TIMEOUT 10
24
25 static int global_fds[RTE_MAX_LCORE] = { [0 ... RTE_MAX_LCORE-1] = -1 };
26
27 int
28 guest_channel_host_check_exists(const char *path)
29 {
30         char glob_path[PATH_MAX];
31         glob_t g;
32         int ret;
33
34         /* we cannot know in advance which cores have VM channels, so glob */
35         snprintf(glob_path, PATH_MAX, "%s.*", path);
36
37         ret = glob(glob_path, GLOB_NOSORT, NULL, &g);
38         if (ret != 0) {
39                 /* couldn't read anything */
40                 ret = 0;
41                 goto out;
42         }
43
44         /* do we have at least one match? */
45         ret = g.gl_pathc > 0;
46
47 out:
48         globfree(&g);
49         return ret;
50 }
51
52 int
53 guest_channel_host_connect(const char *path, unsigned int lcore_id)
54 {
55         int flags, ret;
56         struct rte_power_channel_packet pkt;
57         char fd_path[PATH_MAX];
58         int fd = -1;
59
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);
63                 return -1;
64         }
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]);
69                 return -1;
70         }
71
72         snprintf(fd_path, PATH_MAX, "%s.%u", path, lcore_id);
73         RTE_LOG(INFO, GUEST_CHANNEL, "Opening channel '%s' for lcore %u\n",
74                         fd_path, lcore_id);
75         fd = open(fd_path, O_RDWR);
76         if (fd < 0) {
77                 RTE_LOG(ERR, GUEST_CHANNEL, "Unable to to connect to '%s' with error "
78                                 "%s\n", fd_path, strerror(errno));
79                 return -1;
80         }
81
82         flags = fcntl(fd, F_GETFL, 0);
83         if (flags < 0) {
84                 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on fcntl get flags for file %s\n",
85                                 fd_path);
86                 goto error;
87         }
88
89         flags |= O_NONBLOCK;
90         if (fcntl(fd, F_SETFL, flags) < 0) {
91                 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on setting non-blocking mode for "
92                                 "file %s", fd_path);
93                 goto error;
94         }
95         /* QEMU needs a delay after connection */
96         sleep(1);
97
98         /* Send a test packet, this command is ignored by the host, but a successful
99          * send indicates that the host endpoint is monitoring.
100          */
101         pkt.command = RTE_POWER_CPU_POWER_CONNECT;
102         global_fds[lcore_id] = fd;
103         ret = guest_channel_send_msg(&pkt, lcore_id);
104         if (ret != 0) {
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");
109                 goto error;
110         }
111         RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
112         return 0;
113 error:
114         close(fd);
115         global_fds[lcore_id] = -1;
116         return -1;
117 }
118
119 int
120 guest_channel_send_msg(struct rte_power_channel_packet *pkt,
121                 unsigned int lcore_id)
122 {
123         int ret, buffer_len = sizeof(*pkt);
124         void *buffer = pkt;
125
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);
129                 return -1;
130         }
131
132         if (global_fds[lcore_id] < 0) {
133                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
134                 return -1;
135         }
136         while (buffer_len > 0) {
137                 ret = write(global_fds[lcore_id], buffer, buffer_len);
138                 if (ret == buffer_len)
139                         return 0;
140                 if (ret == -1) {
141                         if (errno == EINTR)
142                                 continue;
143                         return errno;
144                 }
145                 buffer = (char *)buffer + ret;
146                 buffer_len -= ret;
147         }
148         return 0;
149 }
150
151 int rte_power_guest_channel_send_msg(struct rte_power_channel_packet *pkt,
152                         unsigned int lcore_id)
153 {
154         return guest_channel_send_msg(pkt, lcore_id);
155 }
156
157 int power_guest_channel_read_msg(void *pkt,
158                 size_t pkt_len,
159                 unsigned int lcore_id)
160 {
161         int ret;
162         struct pollfd fds;
163
164         if (pkt_len == 0 || pkt == NULL)
165                 return -1;
166
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);
170                 return -1;
171         }
172
173         if (global_fds[lcore_id] < 0) {
174                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
175                 return -1;
176         }
177
178         fds.fd = global_fds[lcore_id];
179         fds.events = POLLIN;
180
181         ret = poll(&fds, 1, TIMEOUT);
182         if (ret == 0) {
183                 RTE_LOG(DEBUG, GUEST_CHANNEL, "Timeout occurred during poll function.\n");
184                 return -1;
185         } else if (ret < 0) {
186                 RTE_LOG(ERR, GUEST_CHANNEL, "Error occurred during poll function: %s\n",
187                                 strerror(errno));
188                 return -1;
189         }
190
191         while (pkt_len > 0) {
192                 ret = read(global_fds[lcore_id],
193                                 pkt, pkt_len);
194
195                 if (ret < 0) {
196                         if (errno == EINTR)
197                                 continue;
198                         return -1;
199                 }
200
201                 if (ret == 0) {
202                         RTE_LOG(ERR, GUEST_CHANNEL, "Expected more data, but connection has been closed.\n");
203                         return -1;
204                 }
205                 pkt = (char *)pkt + ret;
206                 pkt_len -= ret;
207         }
208
209         return 0;
210 }
211
212 int rte_power_guest_channel_receive_msg(void *pkt,
213                 size_t pkt_len,
214                 unsigned int lcore_id)
215 {
216         return power_guest_channel_read_msg(pkt, pkt_len, lcore_id);
217 }
218
219 void
220 guest_channel_host_disconnect(unsigned int lcore_id)
221 {
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);
225                 return;
226         }
227         if (global_fds[lcore_id] < 0)
228                 return;
229         close(global_fds[lcore_id]);
230         global_fds[lcore_id] = -1;
231 }