mempool: fix slow allocation of large mempools
[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(void *pkt,
133                 size_t pkt_len,
134                 unsigned int lcore_id)
135 {
136         int ret;
137         struct pollfd fds;
138
139         if (pkt_len == 0 || pkt == NULL)
140                 return -1;
141
142         fds.fd = global_fds[lcore_id];
143         fds.events = POLLIN;
144
145         ret = poll(&fds, 1, TIMEOUT);
146         if (ret == 0) {
147                 RTE_LOG(DEBUG, GUEST_CHANNEL, "Timeout occurred during poll function.\n");
148                 return -1;
149         } else if (ret < 0) {
150                 RTE_LOG(ERR, GUEST_CHANNEL, "Error occurred during poll function: %s\n",
151                                 strerror(errno));
152                 return -1;
153         }
154
155         if (lcore_id >= RTE_MAX_LCORE) {
156                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
157                                 lcore_id, RTE_MAX_LCORE-1);
158                 return -1;
159         }
160
161         if (global_fds[lcore_id] < 0) {
162                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
163                 return -1;
164         }
165
166         while (pkt_len > 0) {
167                 ret = read(global_fds[lcore_id],
168                                 pkt, pkt_len);
169
170                 if (ret < 0) {
171                         if (errno == EINTR)
172                                 continue;
173                         return -1;
174                 }
175
176                 if (ret == 0) {
177                         RTE_LOG(ERR, GUEST_CHANNEL, "Expected more data, but connection has been closed.\n");
178                         return -1;
179                 }
180                 pkt = (char *)pkt + ret;
181                 pkt_len -= ret;
182         }
183
184         return 0;
185 }
186
187 int rte_power_guest_channel_receive_msg(void *pkt,
188                 size_t pkt_len,
189                 unsigned int lcore_id)
190 {
191         return power_guest_channel_read_msg(pkt, pkt_len, lcore_id);
192 }
193
194 void
195 guest_channel_host_disconnect(unsigned int lcore_id)
196 {
197         if (lcore_id >= RTE_MAX_LCORE) {
198                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
199                                 lcore_id, RTE_MAX_LCORE-1);
200                 return;
201         }
202         if (global_fds[lcore_id] < 0)
203                 return;
204         close(global_fds[lcore_id]);
205         global_fds[lcore_id] = -1;
206 }