22956657e1407d9a84f714c0cd65b3963d2234e8
[dpdk.git] / lib / librte_power / guest_channel.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <limits.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <errno.h>
42
43
44 #include <rte_log.h>
45 #include <rte_config.h>
46
47 #include "guest_channel.h"
48 #include "channel_commands.h"
49
50 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
51
52 static int global_fds[RTE_MAX_LCORE];
53
54 int
55 guest_channel_host_connect(const char *path, unsigned lcore_id)
56 {
57         int flags, ret;
58         struct 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] != 0) {
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 = 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, "Error on channel '%s' communications "
108                                 "test: %s\n", fd_path, strerror(ret));
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] = 0;
116         return -1;
117 }
118
119 int
120 guest_channel_send_msg(struct channel_packet *pkt, unsigned lcore_id)
121 {
122         int ret, buffer_len = sizeof(*pkt);
123         void *buffer = pkt;
124
125         if (lcore_id >= RTE_MAX_LCORE) {
126                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
127                                 lcore_id, RTE_MAX_LCORE-1);
128                 return -1;
129         }
130
131         if (global_fds[lcore_id] == 0) {
132                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
133                 return -1;
134         }
135         while (buffer_len > 0) {
136                 ret = write(global_fds[lcore_id], buffer, buffer_len);
137                 if (ret == buffer_len)
138                         return 0;
139                 if (ret == -1) {
140                         if (errno == EINTR)
141                                 continue;
142                         return errno;
143                 }
144                 buffer = (char *)buffer + ret;
145                 buffer_len -= ret;
146         }
147         return 0;
148 }
149
150 void
151 guest_channel_host_disconnect(unsigned lcore_id)
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;
157         }
158         if (global_fds[lcore_id] == 0)
159                 return;
160         close(global_fds[lcore_id]);
161         global_fds[lcore_id] = 0;
162 }