examples/vm_power: fix build without i40e
[dpdk.git] / lib / librte_power / guest_channel.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <signal.h>
9 #include <limits.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <poll.h>
14
15
16 #include <rte_log.h>
17
18 #include "guest_channel.h"
19 #include "channel_commands.h"
20
21 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
22
23 /* Timeout for incoming message in milliseconds. */
24 #define TIMEOUT 10
25
26 static int global_fds[RTE_MAX_LCORE] = { [0 ... RTE_MAX_LCORE-1] = -1 };
27
28 int
29 guest_channel_host_connect(const char *path, unsigned int lcore_id)
30 {
31         int flags, ret;
32         struct channel_packet pkt;
33         char fd_path[PATH_MAX];
34         int fd = -1;
35
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);
39                 return -1;
40         }
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]);
45                 return -1;
46         }
47
48         snprintf(fd_path, PATH_MAX, "%s.%u", path, lcore_id);
49         RTE_LOG(INFO, GUEST_CHANNEL, "Opening channel '%s' for lcore %u\n",
50                         fd_path, lcore_id);
51         fd = open(fd_path, O_RDWR);
52         if (fd < 0) {
53                 RTE_LOG(ERR, GUEST_CHANNEL, "Unable to to connect to '%s' with error "
54                                 "%s\n", fd_path, strerror(errno));
55                 return -1;
56         }
57
58         flags = fcntl(fd, F_GETFL, 0);
59         if (flags < 0) {
60                 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on fcntl get flags for file %s\n",
61                                 fd_path);
62                 goto error;
63         }
64
65         flags |= O_NONBLOCK;
66         if (fcntl(fd, F_SETFL, flags) < 0) {
67                 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on setting non-blocking mode for "
68                                 "file %s", fd_path);
69                 goto error;
70         }
71         /* QEMU needs a delay after connection */
72         sleep(1);
73
74         /* Send a test packet, this command is ignored by the host, but a successful
75          * send indicates that the host endpoint is monitoring.
76          */
77         pkt.command = CPU_POWER_CONNECT;
78         global_fds[lcore_id] = fd;
79         ret = guest_channel_send_msg(&pkt, lcore_id);
80         if (ret != 0) {
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");
85                 goto error;
86         }
87         RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
88         return 0;
89 error:
90         close(fd);
91         global_fds[lcore_id] = -1;
92         return -1;
93 }
94
95 int
96 guest_channel_send_msg(struct channel_packet *pkt, unsigned int lcore_id)
97 {
98         int ret, buffer_len = sizeof(*pkt);
99         void *buffer = pkt;
100
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);
104                 return -1;
105         }
106
107         if (global_fds[lcore_id] < 0) {
108                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
109                 return -1;
110         }
111         while (buffer_len > 0) {
112                 ret = write(global_fds[lcore_id], buffer, buffer_len);
113                 if (ret == buffer_len)
114                         return 0;
115                 if (ret == -1) {
116                         if (errno == EINTR)
117                                 continue;
118                         return errno;
119                 }
120                 buffer = (char *)buffer + ret;
121                 buffer_len -= ret;
122         }
123         return 0;
124 }
125
126 int rte_power_guest_channel_send_msg(struct channel_packet *pkt,
127                         unsigned int lcore_id)
128 {
129         return guest_channel_send_msg(pkt, lcore_id);
130 }
131
132 int power_guest_channel_read_msg(struct channel_packet *pkt,
133                         unsigned int lcore_id)
134 {
135         int ret;
136         struct pollfd fds;
137         void *buffer = pkt;
138         int buffer_len = sizeof(*pkt);
139
140         fds.fd = global_fds[lcore_id];
141         fds.events = POLLIN;
142
143         ret = poll(&fds, 1, TIMEOUT);
144         if (ret == 0) {
145                 RTE_LOG(DEBUG, GUEST_CHANNEL, "Timeout occurred during poll function.\n");
146                 return -1;
147         } else if (ret < 0) {
148                 RTE_LOG(ERR, GUEST_CHANNEL, "Error occurred during poll function: %s\n",
149                                 strerror(ret));
150                 return -1;
151         }
152
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);
156                 return -1;
157         }
158
159         if (global_fds[lcore_id] < 0) {
160                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
161                 return -1;
162         }
163
164         while (buffer_len > 0) {
165                 ret = read(global_fds[lcore_id],
166                                 buffer, buffer_len);
167                 if (ret < 0) {
168                         if (errno == EINTR)
169                                 continue;
170                         return -1;
171                 }
172                 if (ret == 0) {
173                         RTE_LOG(ERR, GUEST_CHANNEL, "Expected more data, but connection has been closed.\n");
174                         return -1;
175                 }
176                 buffer = (char *)buffer + ret;
177                 buffer_len -= ret;
178         }
179
180         return 0;
181 }
182
183 int rte_power_guest_channel_receive_msg(struct channel_packet *pkt,
184                         unsigned int lcore_id)
185 {
186         return power_guest_channel_read_msg(pkt, lcore_id);
187 }
188
189 void
190 guest_channel_host_disconnect(unsigned int lcore_id)
191 {
192         if (lcore_id >= RTE_MAX_LCORE) {
193                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
194                                 lcore_id, RTE_MAX_LCORE-1);
195                 return;
196         }
197         if (global_fds[lcore_id] < 0)
198                 return;
199         close(global_fds[lcore_id]);
200         global_fds[lcore_id] = -1;
201 }