Imported Upstream version 16.04
[deb_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
46 #include "guest_channel.h"
47 #include "channel_commands.h"
48
49 #define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1
50
51 static int global_fds[RTE_MAX_LCORE];
52
53 int
54 guest_channel_host_connect(const char *path, unsigned lcore_id)
55 {
56         int flags, ret;
57         struct channel_packet pkt;
58         char fd_path[PATH_MAX];
59         int fd = -1;
60
61         if (lcore_id >= RTE_MAX_LCORE) {
62                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
63                                 lcore_id, RTE_MAX_LCORE-1);
64                 return -1;
65         }
66         /* check if path is already open */
67         if (global_fds[lcore_id] != 0) {
68                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is already open with fd %d\n",
69                                 lcore_id, global_fds[lcore_id]);
70                 return -1;
71         }
72
73         snprintf(fd_path, PATH_MAX, "%s.%u", path, lcore_id);
74         RTE_LOG(INFO, GUEST_CHANNEL, "Opening channel '%s' for lcore %u\n",
75                         fd_path, lcore_id);
76         fd = open(fd_path, O_RDWR);
77         if (fd < 0) {
78                 RTE_LOG(ERR, GUEST_CHANNEL, "Unable to to connect to '%s' with error "
79                                 "%s\n", fd_path, strerror(errno));
80                 return -1;
81         }
82
83         flags = fcntl(fd, F_GETFL, 0);
84         if (flags < 0) {
85                 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on fcntl get flags for file %s\n",
86                                 fd_path);
87                 goto error;
88         }
89
90         flags |= O_NONBLOCK;
91         if (fcntl(fd, F_SETFL, flags) < 0) {
92                 RTE_LOG(ERR, GUEST_CHANNEL, "Failed on setting non-blocking mode for "
93                                 "file %s", fd_path);
94                 goto error;
95         }
96         /* QEMU needs a delay after connection */
97         sleep(1);
98
99         /* Send a test packet, this command is ignored by the host, but a successful
100          * send indicates that the host endpoint is monitoring.
101          */
102         pkt.command = CPU_POWER_CONNECT;
103         global_fds[lcore_id] = fd;
104         ret = guest_channel_send_msg(&pkt, lcore_id);
105         if (ret != 0) {
106                 RTE_LOG(ERR, GUEST_CHANNEL, "Error on channel '%s' communications "
107                                 "test: %s\n", fd_path, strerror(ret));
108                 goto error;
109         }
110         RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
111         return 0;
112 error:
113         close(fd);
114         global_fds[lcore_id] = 0;
115         return -1;
116 }
117
118 int
119 guest_channel_send_msg(struct channel_packet *pkt, unsigned lcore_id)
120 {
121         int ret, buffer_len = sizeof(*pkt);
122         void *buffer = pkt;
123
124         if (lcore_id >= RTE_MAX_LCORE) {
125                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
126                                 lcore_id, RTE_MAX_LCORE-1);
127                 return -1;
128         }
129
130         if (global_fds[lcore_id] == 0) {
131                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
132                 return -1;
133         }
134         while (buffer_len > 0) {
135                 ret = write(global_fds[lcore_id], buffer, buffer_len);
136                 if (ret == buffer_len)
137                         return 0;
138                 if (ret == -1) {
139                         if (errno == EINTR)
140                                 continue;
141                         return errno;
142                 }
143                 buffer = (char *)buffer + ret;
144                 buffer_len -= ret;
145         }
146         return 0;
147 }
148
149 void
150 guest_channel_host_disconnect(unsigned lcore_id)
151 {
152         if (lcore_id >= RTE_MAX_LCORE) {
153                 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
154                                 lcore_id, RTE_MAX_LCORE-1);
155                 return;
156         }
157         if (global_fds[lcore_id] == 0)
158                 return;
159         close(global_fds[lcore_id]);
160         global_fds[lcore_id] = 0;
161 }