]> git.proxmox.com Git - ceph.git/blob - ceph/src/dpdk/lib/librte_power/guest_channel.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / dpdk / 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,
107 "Error on channel '%s' communications test: %s\n",
108 fd_path, ret > 0 ? strerror(ret) :
109 "channel not connected");
110 goto error;
111 }
112 RTE_LOG(INFO, GUEST_CHANNEL, "Channel '%s' is now connected\n", fd_path);
113 return 0;
114 error:
115 close(fd);
116 global_fds[lcore_id] = 0;
117 return -1;
118 }
119
120 int
121 guest_channel_send_msg(struct channel_packet *pkt, unsigned lcore_id)
122 {
123 int ret, buffer_len = sizeof(*pkt);
124 void *buffer = pkt;
125
126 if (lcore_id >= RTE_MAX_LCORE) {
127 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
128 lcore_id, RTE_MAX_LCORE-1);
129 return -1;
130 }
131
132 if (global_fds[lcore_id] == 0) {
133 RTE_LOG(ERR, GUEST_CHANNEL, "Channel is not connected\n");
134 return -1;
135 }
136 while (buffer_len > 0) {
137 ret = write(global_fds[lcore_id], buffer, buffer_len);
138 if (ret == buffer_len)
139 return 0;
140 if (ret == -1) {
141 if (errno == EINTR)
142 continue;
143 return errno;
144 }
145 buffer = (char *)buffer + ret;
146 buffer_len -= ret;
147 }
148 return 0;
149 }
150
151 void
152 guest_channel_host_disconnect(unsigned lcore_id)
153 {
154 if (lcore_id >= RTE_MAX_LCORE) {
155 RTE_LOG(ERR, GUEST_CHANNEL, "Channel(%u) is out of range 0...%d\n",
156 lcore_id, RTE_MAX_LCORE-1);
157 return;
158 }
159 if (global_fds[lcore_id] == 0)
160 return;
161 close(global_fds[lcore_id]);
162 global_fds[lcore_id] = 0;
163 }