New upstream version 18.02
[deb_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
14
15 #include <rte_log.h>
16
17 #include "guest_channel.h"
18 #include "channel_commands.h"
19
20 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
21
22 static int global_fds[RTE_MAX_LCORE];
23
24 int
25 guest_channel_host_connect(const char *path, unsigned int lcore_id)
26 {
27         int flags, ret;
28         struct channel_packet pkt;
29         char fd_path[PATH_MAX];
30         int fd = -1;
31
32         if (lcore_id >= RTE_MAX_LCORE) {
33                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
34                                 lcore_id, RTE_MAX_LCORE-1);
35                 return -1;
36         }
37         /* check if path is already open */
38         if (global_fds[lcore_id] != 0) {
39                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is already open with fd %d\n",
40                                 lcore_id, global_fds[lcore_id]);
41                 return -1;
42         }
43
44         snprintf(fd_path, PATH_MAX, "%s.%u", path, lcore_id);
45         RTE_LOG(INFO, GUEST_CHANNEL, "Opening channel '%s' for lcore %u\n",
46                         fd_path, lcore_id);
47         fd = open(fd_path, O_RDWR);
48         if (fd < 0) {
49                 RTE_LOG(ERR, GUEST_CHANNEL, "Unable to to connect to '%s' with error "
50                                 "%s\n", fd_path, strerror(errno));
51                 return -1;
52         }
53
54         flags = fcntl(fd, F_GETFL, 0);
55         if (flags < 0) {
56                 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on fcntl get flags for file %s\n",
57                                 fd_path);
58                 goto error;
59         }
60
61         flags |= O_NONBLOCK;
62         if (fcntl(fd, F_SETFL, flags) < 0) {
63                 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on setting non-blocking mode for "
64                                 "file %s", fd_path);
65                 goto error;
66         }
67         /* QEMU needs a delay after connection */
68         sleep(1);
69
70         /* Send a test packet, this command is ignored by the host, but a successful
71          * send indicates that the host endpoint is monitoring.
72          */
73         pkt.command = CPU_POWER_CONNECT;
74         global_fds[lcore_id] = fd;
75         ret = guest_channel_send_msg(&pkt, lcore_id);
76         if (ret != 0) {
77                 RTE_LOG(ERR, GUEST_CHANNEL,
78                                 "Error on channel '%s' communications test: %s\n",
79                                 fd_path, ret > 0 ? strerror(ret) :
80                                 "channel not connected");
81                 goto error;
82         }
83         RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
84         return 0;
85 error:
86         close(fd);
87         global_fds[lcore_id] = 0;
88         return -1;
89 }
90
91 int
92 guest_channel_send_msg(struct channel_packet *pkt, unsigned int lcore_id)
93 {
94         int ret, buffer_len = sizeof(*pkt);
95         void *buffer = pkt;
96
97         if (lcore_id >= RTE_MAX_LCORE) {
98                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
99                                 lcore_id, RTE_MAX_LCORE-1);
100                 return -1;
101         }
102
103         if (global_fds[lcore_id] == 0) {
104                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
105                 return -1;
106         }
107         while (buffer_len > 0) {
108                 ret = write(global_fds[lcore_id], buffer, buffer_len);
109                 if (ret == buffer_len)
110                         return 0;
111                 if (ret == -1) {
112                         if (errno == EINTR)
113                                 continue;
114                         return errno;
115                 }
116                 buffer = (char *)buffer + ret;
117                 buffer_len -= ret;
118         }
119         return 0;
120 }
121
122 int rte_power_guest_channel_send_msg(struct channel_packet *pkt,
123                         unsigned int lcore_id)
124 {
125         return guest_channel_send_msg(pkt, lcore_id);
126 }
127
128
129 void
130 guest_channel_host_disconnect(unsigned int lcore_id)
131 {
132         if (lcore_id >= RTE_MAX_LCORE) {
133                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
134                                 lcore_id, RTE_MAX_LCORE-1);
135                 return;
136         }
137         if (global_fds[lcore_id] == 0)
138                 return;
139         close(global_fds[lcore_id]);
140         global_fds[lcore_id] = 0;
141 }