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